示例#1
0
文件: Messages.cpp 项目: jpfy/MQTT-SN
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();

}
示例#2
0
文件: Messages.cpp 项目: jpfy/MQTT-SN
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();
}
示例#3
0
文件: Messages.cpp 项目: jpfy/MQTT-SN
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();
}
示例#4
0
文件: Messages.cpp 项目: jpfy/MQTT-SN
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;
}
示例#5
0
文件: Messages.cpp 项目: jpfy/MQTT-SN
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();
}
示例#6
0
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();
}
示例#7
0
文件: Messages.cpp 项目: jpfy/MQTT-SN
uint8_t MQTTMessage::getRemainLengthSize(){
	RemainingLength remLen;
	remLen.encode(_remainLength);
	return remLen.getSize();
}