|
67 | 67 |
|
68 | 68 | # Variable CONNECT header [MQTT 3.1.2]
|
69 | 69 | MQTT_HDR_CONNECT = bytearray(b"\x04MQTT\x04\x02\0\0")
|
70 |
| -# Variable PUBLISH header [MQTT 3.3.2] |
71 |
| -MQTT_HDR_PUBLISH = bytearray(b"\x00\x01\x61\x2F\x62\x00\x0a") |
72 | 70 |
|
73 | 71 |
|
74 | 72 | CONNACK_ERRORS = {
|
@@ -444,32 +442,33 @@ def publish(self, topic, msg, retain=False, qos=0):
|
444 | 442 | raise MMQTTException("Invalid message data type.")
|
445 | 443 | if len(msg) > MQTT_MSG_MAX_SZ:
|
446 | 444 | raise MMQTTException("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ)
|
447 |
| - self._check_qos(qos) |
| 445 | + assert ( |
| 446 | + 0 <= qos <= 1 |
| 447 | + ), "Quality of Service Level 2 is unsupported by this library." |
448 | 448 |
|
449 | 449 | pub_hdr_fixed = bytearray() # fixed header
|
450 | 450 | pub_hdr_fixed.extend(MQTT_PUB)
|
451 |
| - pub_hdr_fixed[0] |= retain | qos << 1 |
| 451 | + pub_hdr_fixed[0] |= retain | qos << 1 # [3.3.1.2], [3.3.1.3] |
452 | 452 |
|
453 | 453 | pub_hdr_var = bytearray() # variable header
|
454 |
| - pub_hdr_var.append(len(topic) >> 8) # Topic len MSB |
455 |
| - pub_hdr_var.append(len(topic) & 0xFF) # Topic len LSB |
456 |
| - pub_hdr_var.extend(topic.encode("utf-8")) # Topic structure |
| 454 | + pub_hdr_var.append(len(topic) >> 8) # Topic length, MSB |
| 455 | + pub_hdr_var.append(len(topic) & 0xFF) # Topic length, LSB |
| 456 | + pub_hdr_var.extend(topic.encode("utf-8")) # Topic name |
457 | 457 |
|
458 | 458 | remaining_length = 2 + len(msg) + len(topic)
|
459 | 459 | if qos > 0:
|
| 460 | + # packet identifier where QoS level is 1 or 2. [3.3.2.2] |
460 | 461 | pid = self._pid
|
461 | 462 | remaining_length += 2
|
462 | 463 | pub_hdr_var.append(0x00)
|
463 | 464 | pub_hdr_var.append(self._pid)
|
464 | 465 | self._pid += 1
|
465 | 466 |
|
466 |
| - # Remaining length calculation |
| 467 | + # Calculate remaining length [2.2.3] |
467 | 468 | if remaining_length > 0x7F:
|
468 |
| - # Calculate Remaining Length [2.2.3] |
469 | 469 | while remaining_length > 0:
|
470 | 470 | encoded_byte = remaining_length % 0x80
|
471 | 471 | remaining_length = remaining_length // 0x80
|
472 |
| - # if there is more data to encode, set the top bit of the byte |
473 | 472 | if remaining_length > 0:
|
474 | 473 | encoded_byte |= 0x80
|
475 | 474 | pub_hdr_fixed.append(encoded_byte)
|
@@ -500,10 +499,6 @@ def publish(self, topic, msg, retain=False, qos=0):
|
500 | 499 | if self.on_publish is not None:
|
501 | 500 | self.on_publish(self, self.user_data, topic, rcv_pid)
|
502 | 501 | return
|
503 |
| - elif qos == 2: |
504 |
| - assert 0 |
505 |
| - if self.on_publish is not None: |
506 |
| - self.on_publish(self, self.user_data, topic, rcv_pid) |
507 | 502 |
|
508 | 503 | def subscribe(self, topic, qos=0):
|
509 | 504 | """Subscribes to a topic on the MQTT Broker.
|
|
0 commit comments