Commit c1a010a3a44953841de4801bee86b2edd33ae112
1 parent
25268f92
Add writeUint16() and use it
Showing
2 changed files
with
18 additions
and
18 deletions
mqttpacket.cpp
| @@ -128,8 +128,7 @@ MqttPacket::MqttPacket(const SubAck &subAck) : | @@ -128,8 +128,7 @@ MqttPacket::MqttPacket(const SubAck &subAck) : | ||
| 128 | { | 128 | { |
| 129 | packetType = PacketType::SUBACK; | 129 | packetType = PacketType::SUBACK; |
| 130 | first_byte = static_cast<char>(packetType) << 4; | 130 | first_byte = static_cast<char>(packetType) << 4; |
| 131 | - writeByte((subAck.packet_id & 0xFF00) >> 8); | ||
| 132 | - writeByte(subAck.packet_id & 0x00FF); | 131 | + writeUint16(subAck.packet_id); |
| 133 | 132 | ||
| 134 | std::vector<char> returnList; | 133 | std::vector<char> returnList; |
| 135 | for (SubAckReturnCodes code : subAck.responses) | 134 | for (SubAckReturnCodes code : subAck.responses) |
| @@ -146,8 +145,7 @@ MqttPacket::MqttPacket(const UnsubAck &unsubAck) : | @@ -146,8 +145,7 @@ MqttPacket::MqttPacket(const UnsubAck &unsubAck) : | ||
| 146 | { | 145 | { |
| 147 | packetType = PacketType::SUBACK; | 146 | packetType = PacketType::SUBACK; |
| 148 | first_byte = static_cast<char>(packetType) << 4; | 147 | first_byte = static_cast<char>(packetType) << 4; |
| 149 | - writeByte((unsubAck.packet_id & 0xFF00) >> 8); | ||
| 150 | - writeByte(unsubAck.packet_id & 0x00FF); | 148 | + writeUint16(unsubAck.packet_id); |
| 151 | calculateRemainingLength(); | 149 | calculateRemainingLength(); |
| 152 | } | 150 | } |
| 153 | 151 | ||
| @@ -168,10 +166,7 @@ MqttPacket::MqttPacket(const Publish &publish) : | @@ -168,10 +166,7 @@ MqttPacket::MqttPacket(const Publish &publish) : | ||
| 168 | first_byte |= (publish.qos << 1); | 166 | first_byte |= (publish.qos << 1); |
| 169 | first_byte |= (static_cast<char>(publish.retain) & 0b00000001); | 167 | first_byte |= (static_cast<char>(publish.retain) & 0b00000001); |
| 170 | 168 | ||
| 171 | - char topicLenMSB = (topic.length() & 0xFF00) >> 8; | ||
| 172 | - char topicLenLSB = topic.length() & 0x00FF; | ||
| 173 | - writeByte(topicLenMSB); | ||
| 174 | - writeByte(topicLenLSB); | 169 | + writeUint16(topic.length()); |
| 175 | writeBytes(topic.c_str(), topic.length()); | 170 | writeBytes(topic.c_str(), topic.length()); |
| 176 | 171 | ||
| 177 | if (publish.qos) | 172 | if (publish.qos) |
| @@ -204,11 +199,8 @@ void MqttPacket::pubCommonConstruct(const uint16_t packet_id, PacketType packetT | @@ -204,11 +199,8 @@ void MqttPacket::pubCommonConstruct(const uint16_t packet_id, PacketType packetT | ||
| 204 | first_byte = (static_cast<uint8_t>(packetType) << 4) | firstByteDefaultBits; | 199 | first_byte = (static_cast<uint8_t>(packetType) << 4) | firstByteDefaultBits; |
| 205 | writeByte(first_byte); | 200 | writeByte(first_byte); |
| 206 | writeByte(2); // length is always 2. | 201 | writeByte(2); // length is always 2. |
| 207 | - uint8_t packetIdMSB = (packet_id & 0xFF00) >> 8; | ||
| 208 | - uint8_t packetIdLSB = (packet_id & 0x00FF); | ||
| 209 | packet_id_pos = pos; | 202 | packet_id_pos = pos; |
| 210 | - writeByte(packetIdMSB); | ||
| 211 | - writeByte(packetIdLSB); | 203 | + writeUint16(packet_id); |
| 212 | } | 204 | } |
| 213 | 205 | ||
| 214 | MqttPacket::MqttPacket(const PubAck &pubAck) : | 206 | MqttPacket::MqttPacket(const PubAck &pubAck) : |
| @@ -766,13 +758,8 @@ void MqttPacket::setPacketId(uint16_t packet_id) | @@ -766,13 +758,8 @@ void MqttPacket::setPacketId(uint16_t packet_id) | ||
| 766 | assert(qos > 0); | 758 | assert(qos > 0); |
| 767 | 759 | ||
| 768 | this->packet_id = packet_id; | 760 | this->packet_id = packet_id; |
| 769 | - | ||
| 770 | pos = packet_id_pos; | 761 | pos = packet_id_pos; |
| 771 | - | ||
| 772 | - char topicLenMSB = (packet_id & 0xFF00) >> 8; | ||
| 773 | - char topicLenLSB = (packet_id & 0x00FF); | ||
| 774 | - writeByte(topicLenMSB); | ||
| 775 | - writeByte(topicLenLSB); | 762 | + writeUint16(packet_id); |
| 776 | } | 763 | } |
| 777 | 764 | ||
| 778 | uint16_t MqttPacket::getPacketId() const | 765 | uint16_t MqttPacket::getPacketId() const |
| @@ -901,6 +888,18 @@ void MqttPacket::writeByte(char b) | @@ -901,6 +888,18 @@ void MqttPacket::writeByte(char b) | ||
| 901 | bites[pos++] = b; | 888 | bites[pos++] = b; |
| 902 | } | 889 | } |
| 903 | 890 | ||
| 891 | +void MqttPacket::writeUint16(uint16_t x) | ||
| 892 | +{ | ||
| 893 | + if (pos + 2 > bites.size()) | ||
| 894 | + throw ProtocolError("Exceeding packet size"); | ||
| 895 | + | ||
| 896 | + const uint8_t a = static_cast<uint8_t>(x >> 8); | ||
| 897 | + const uint8_t b = static_cast<uint8_t>(x); | ||
| 898 | + | ||
| 899 | + bites[pos++] = a; | ||
| 900 | + bites[pos++] = b; | ||
| 901 | +} | ||
| 902 | + | ||
| 904 | void MqttPacket::writeBytes(const char *b, size_t len) | 903 | void MqttPacket::writeBytes(const char *b, size_t len) |
| 905 | { | 904 | { |
| 906 | if (pos + len > bites.size()) | 905 | if (pos + len > bites.size()) |
mqttpacket.h
| @@ -60,6 +60,7 @@ class MqttPacket | @@ -60,6 +60,7 @@ class MqttPacket | ||
| 60 | char *readBytes(size_t length); | 60 | char *readBytes(size_t length); |
| 61 | char readByte(); | 61 | char readByte(); |
| 62 | void writeByte(char b); | 62 | void writeByte(char b); |
| 63 | + void writeUint16(uint16_t x); | ||
| 63 | void writeBytes(const char *b, size_t len); | 64 | void writeBytes(const char *b, size_t len); |
| 64 | uint16_t readTwoBytesToUInt16(); | 65 | uint16_t readTwoBytesToUInt16(); |
| 65 | size_t remainingAfterPos(); | 66 | size_t remainingAfterPos(); |