Add property writing support, untested.

pull/1022/head
Roger A. Light 7 years ago
parent 5edc87bcdf
commit 4daaaaf4a7

@ -225,3 +225,27 @@ int packet__read_varint(struct mosquitto__packet *packet, int32_t *word, int8_t
return MOSQ_ERR_PROTOCOL;
}
int packet__write_varint(struct mosquitto__packet *packet, int32_t word)
{
uint8_t byte;
int count = 0;
packet->payload = NULL;
packet->remaining_count = 0;
do{
byte = word % 128;
word = word / 128;
/* If there are more digits to encode, set the top bit of this digit */
if(word > 0){
byte = byte | 0x80;
}
packet__write_byte(packet, byte);
count++;
}while(word > 0 && count < 5);
if(count == 5){
return MOSQ_ERR_PROTOCOL;
}
return MOSQ_ERR_SUCCESS;
}

@ -40,6 +40,7 @@ void packet__write_bytes(struct mosquitto__packet *packet, const void *bytes, ui
void packet__write_string(struct mosquitto__packet *packet, const char *str, uint16_t length);
void packet__write_uint16(struct mosquitto__packet *packet, uint16_t word);
void packet__write_uint32(struct mosquitto__packet *packet, uint32_t word);
int packet__write_varint(struct mosquitto__packet *packet, int32_t word);
int packet__write(struct mosquitto *mosq);
#ifdef WITH_BROKER

@ -358,9 +358,86 @@ int property__get_length_all(struct mqtt5__property *property)
}
int property__write_all(struct mosquitto__packet *packet, struct mqtt5__property **property)
int property__write(struct mosquitto__packet *packet, struct mqtt5__property *property)
{
packet__write_byte(packet, 0);
int rc;
rc = packet__write_varint(packet, property->identifier);
if(rc) return rc;
switch(property->identifier){
case PROP_PAYLOAD_FORMAT_INDICATOR:
case PROP_REQUEST_PROBLEM_INFO:
case PROP_REQUEST_RESPONSE_INFO:
case PROP_MAXIMUM_QOS:
case PROP_RETAIN_AVAILABLE:
case PROP_WILDCARD_SUB_AVAILABLE:
case PROP_SUBSCRIPTION_ID_AVAILABLE:
case PROP_SHARED_SUB_AVAILABLE:
packet__write_byte(packet, property->value.i8);
break;
case PROP_SERVER_KEEP_ALIVE:
case PROP_RECEIVE_MAXIMUM:
case PROP_TOPIC_ALIAS_MAXIMUM:
case PROP_TOPIC_ALIAS:
packet__write_uint16(packet, property->value.i16);
break;
case PROP_MESSAGE_EXPIRY_INTERVAL:
case PROP_SESSION_EXPIRY_INTERVAL:
case PROP_WILL_DELAY_INTERVAL:
case PROP_MAXIMUM_PACKET_SIZE:
packet__write_uint32(packet, property->value.i32);
break;
case PROP_SUBSCRIPTION_IDENTIFIER:
return packet__write_varint(packet, property->value.varint);
case PROP_CONTENT_TYPE:
case PROP_RESPONSE_TOPIC:
case PROP_ASSIGNED_CLIENT_IDENTIFIER:
case PROP_AUTHENTICATION_METHOD:
case PROP_RESPONSE_INFO:
case PROP_SERVER_REFERENCE:
case PROP_REASON_STRING:
packet__write_string(packet, property->value.s.v, property->value.s.len);
break;
case PROP_AUTHENTICATION_DATA:
case PROP_CORRELATION_DATA:
packet__write_uint16(packet, property->value.bin.len);
packet__write_bytes(packet, property->value.bin.v, property->value.bin.len);
case PROP_USER_PROPERTY:
packet__write_string(packet, property->name.v, property->name.len);
packet__write_string(packet, property->value.s.v, property->value.s.len);
break;
default:
log__printf(NULL, MOSQ_LOG_DEBUG, "Unsupported property type: %d", property->identifier);
return MOSQ_ERR_INVAL;
}
return MOSQ_ERR_SUCCESS;
}
int property__write_all(struct mosquitto__packet *packet, struct mqtt5__property *properties)
{
int rc;
struct mqtt5__property *p;
rc = packet__write_varint(packet, property__get_length_all(properties));
if(rc) return rc;
p = properties;
while(p){
rc = property__write(packet, p);
if(rc) return rc;
p = p->next;
}
return MOSQ_ERR_SUCCESS;
}

@ -40,7 +40,7 @@ struct mqtt5__property {
int property__read_all(struct mosquitto__packet *packet, struct mqtt5__property **property);
int property__write_all(struct mosquitto__packet *packet, struct mqtt5__property **property);
int property__write_all(struct mosquitto__packet *packet, struct mqtt5__property *property);
void property__free(struct mqtt5__property **property);
void property__free_all(struct mqtt5__property **property);

Loading…
Cancel
Save