bool MQTTPublish::deserialize(uint8_t* buf){ uint8_t pos = 2; _type = *buf & 0xf0; if(_type != MQTT_TYPE_PUBLISH){ return false; } RemainingLength remLen; _flags = *buf & 0x0f; remLen.deserialize(buf + 1); _remainLength = remLen.decode(); buf += 1 + remLen.getSize(); _topic = string((char*)buf + 2, getUint16(buf)); buf += _topic.size() + 2; if(getQos()){ _messageId = getUint16(buf); buf += 2; pos += 2; } _len = _remainLength - _topic.size() - pos; _payload = mqcalloc(_len); memcpy(_payload, buf, _len); return true; }
bool MQTTSubAck::deserialize(uint8_t* buf){ RemainingLength remLen; remLen.encode(_remainLength); _type = *buf & 0xf0; if(_type != MQTT_TYPE_SUBACK){ return false; } _flags = *buf++ & 0x0f; remLen.deserialize(buf); _messageId = getUint16(buf + remLen.getSize()); _qos = *(buf + remLen.getSize() + 2); return true; }
bool MQTTMessage::deserialize(uint8_t* buf){ RemainingLength remLen; _type = *buf & 0xf0; _flags = *buf & 0x0f; remLen.deserialize(buf + 1); _remainLength = remLen.decode(); switch(_type){ case MQTT_TYPE_PINGRESP: return true; break; case MQTT_TYPE_UNSUBACK: case MQTT_TYPE_PUBACK: _messageId = getUint16(buf + 2); break; default: return false; } return true; }
uint16_t MQTTConnect::serialize(uint8_t* buf){ uint16_t len; string protocol; if(_protocol == MQTT_PROTOCOL_VER4){ protocol = MQTT_PROTOCOL_NAME4; len = 10; }else{ protocol = MQTT_PROTOCOL_NAME3; len = 12; } len += _clientId.size() + 2; if(_connectFlags & 0x0c){ // Will Topic & Message len += _willTopic.size() + 2; len += _willMessage.size() + 2; } if(_connectFlags & 0x80){ // User Name len += _userName.size() + 2; } if(_connectFlags & 0x40){ // Password len += _password.size() + 2; } RemainingLength remLen; remLen.encode(len); _remainLength = len; *buf++ = _type | _flags; remLen.serialize(buf); buf += remLen.getSize(); utfSerialize(buf, protocol); buf += protocol.size() + 2; *buf++ = _protocol; *buf++ = _connectFlags; setUint16(buf++, _keepAliveTime); utfSerialize(++buf,_clientId); // copy clienId buf += _clientId.size() + 2; if(_connectFlags & 0x0c){ // Will Topic & Message utfSerialize(buf,_willTopic); buf += _willTopic.size() + 2; utfSerialize(buf,_willMessage); buf += _willMessage.size() + 2; } if(_connectFlags & 0x80){ // User Name utfSerialize(buf,_userName); buf += _userName.size() + 2; } if(_connectFlags & 0x40){ // Password utfSerialize(buf,_password); } return 1 + remLen.getSize() + remLen.decode(); }
uint16_t MQTTMessage::serialize(uint8_t* buf){ RemainingLength remLen; remLen.encode(_remainLength); *buf++ = _type | _flags; remLen.serialize(buf); buf += remLen.getSize(); if(remLen.decode() > 0){ setUint16(buf, _messageId); } return 1 + remLen.getSize() + remLen.decode(); }
uint16_t MQTTUnsubscribe::serialize(uint8_t* buf){ RemainingLength remLen; _remainLength = _topic.size() + 4; remLen.encode(_remainLength); *buf++ = _type | 0x02; remLen.serialize(buf); buf += remLen.getSize(); setUint16(buf, _messageId); buf += 2; utfSerialize(buf,_topic); return 1 + remLen.getSize() + remLen.decode(); }
uint16_t MQTTSubscribe::serialize(uint8_t* buf){ RemainingLength remLen; _remainLength = _topic.size() + 5; //length:2, Topic:n, QoS:1 remLen.encode(_remainLength); *buf++ = _type | 0x02; //SUBSCRIBE QoS1 remLen.serialize(buf); buf += remLen.getSize(); setUint16(buf, _messageId); buf += 2; utfSerialize(buf,_topic); buf += 2 + _topic.size(); *buf = _qos; return 1 + remLen.getSize() + remLen.decode(); }
uint16_t MQTTPublish::serialize(uint8_t* buf){ RemainingLength remLen; _remainLength = _topic.size() + 2 + 2 + _len; // topic,MessageID,Payload remLen.encode(_remainLength); *buf++ = (_type & 0xf0) | (_flags & 0x0f); remLen.serialize(buf); buf += remLen.getSize(); utfSerialize(buf, _topic); buf += _topic.size() + 2; setUint16(buf, _messageId); memcpy(buf + 2, _payload, _len); return 1 + remLen.getSize() + remLen.decode(); }
uint8_t MQTTMessage::getRemainLengthSize(){ RemainingLength remLen; remLen.encode(_remainLength); return remLen.getSize(); }