mqtt5_props.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import struct
  2. PROP_PAYLOAD_FORMAT_INDICATOR = 1
  3. PROP_MESSAGE_EXPIRY_INTERVAL = 2
  4. PROP_CONTENT_TYPE = 3
  5. PROP_RESPONSE_TOPIC = 8
  6. PROP_CORRELATION_DATA = 9
  7. PROP_SUBSCRIPTION_IDENTIFIER = 11
  8. PROP_SESSION_EXPIRY_INTERVAL = 17
  9. PROP_ASSIGNED_CLIENT_IDENTIFIER = 18
  10. PROP_SERVER_KEEP_ALIVE = 19
  11. PROP_AUTHENTICATION_METHOD = 21
  12. PROP_AUTHENTICATION_DATA = 22
  13. PROP_REQUEST_PROBLEM_INFO = 23
  14. PROP_WILL_DELAY_INTERVAL = 24
  15. PROP_REQUEST_RESPONSE_INFO = 25
  16. PROP_RESPONSE_INFO = 26
  17. PROP_SERVER_REFERENCE = 28
  18. PROP_REASON_STRING = 31
  19. PROP_RECEIVE_MAXIMUM = 33
  20. PROP_TOPIC_ALIAS_MAXIMUM = 34
  21. PROP_TOPIC_ALIAS = 35
  22. PROP_MAXIMUM_QOS = 36
  23. PROP_RETAIN_AVAILABLE = 37
  24. PROP_USER_PROPERTY = 38
  25. PROP_MAXIMUM_PACKET_SIZE = 39
  26. PROP_WILDCARD_SUB_AVAILABLE = 40
  27. PROP_SUBSCRIPTION_ID_AVAILABLE = 41
  28. PROP_SHARED_SUB_AVAILABLE = 42
  29. def gen_byte_prop(identifier, byte):
  30. prop = struct.pack('BB', identifier, byte)
  31. return prop
  32. def gen_uint16_prop(identifier, word):
  33. prop = struct.pack('!BH', identifier, word)
  34. return prop
  35. def gen_uint32_prop(identifier, word):
  36. prop = struct.pack('!BI', identifier, word)
  37. return prop
  38. def gen_string_prop(identifier, s):
  39. s = s.encode("utf-8")
  40. prop = struct.pack('!BH%ds'%(len(s)), identifier, len(s), s)
  41. return prop
  42. def gen_string_pair_prop(identifier, s1, s2):
  43. s1 = s1.encode("utf-8")
  44. s2 = s2.encode("utf-8")
  45. prop = struct.pack('!BH%dsH%ds'%(len(s1), len(s2)), identifier, len(s1), s1, len(s2), s2)
  46. return prop
  47. def gen_varint_prop(identifier, val):
  48. v = pack_varint(val)
  49. return struct.pack("!B"+str(len(v))+"s", identifier, v)
  50. def pack_varint(varint):
  51. s = b""
  52. while True:
  53. byte = varint % 128
  54. varint = varint // 128
  55. # If there are more digits to encode, set the top bit of this digit
  56. if varint > 0:
  57. byte = byte | 0x80
  58. s = s + struct.pack("!B", byte)
  59. if varint == 0:
  60. return s
  61. def prop_finalise(props):
  62. return pack_varint(len(props)) + props