version 1.1.1.1.2.2, 2012/04/26 11:49:12
|
version 1.3, 2012/06/28 11:06:17
|
Line 60 int
|
Line 60 int
|
mqtt_msgSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics, |
mqtt_msgSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics, |
u_short msgID, u_char Dup, u_char QOS) |
u_short msgID, u_char Dup, u_char QOS) |
{ |
{ |
int siz = 0; | int len, siz = 0; |
| u_int n, *l; |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
mqtthdr_var_t *topic; |
mqtthdr_var_t *topic; |
mqtt_len_t *mid; |
mqtt_len_t *mid; |
mqtt_subscr_t *t; |
mqtt_subscr_t *t; |
u_char *qos; |
u_char *qos; |
|
void *data; |
|
|
if (!buf || !Topics) |
if (!buf || !Topics) |
return -1; |
return -1; |
Line 78 mqtt_msgSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_su
|
Line 80 mqtt_msgSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_su
|
return -1; |
return -1; |
} |
} |
|
|
if (mqtt_msgRealloc(buf, MQTTMSG_MAX) == -1) | /* calculate message size */ |
| len = sizeof(mqtt_len_t); /* msgid */ |
| for (t = Topics; t && t->sub_topic.msg_base; t++) /* subscribes & qos */ |
| len += sizeof(mqtt_len_t) + t->sub_topic.msg_len + 1; |
| |
| /* calculate header size */ |
| siz = sizeof(struct mqtthdr); /* mqtt fixed header */ |
| n = mqtt_encodeLen(len); /* message size */ |
| siz += mqtt_sizeLen(n) - 1; /* length size */ |
| |
| if (mqtt_msgRealloc(buf, siz + len) == -1) |
return -1; |
return -1; |
else { |
else { |
hdr = (struct mqtthdr *) (buf->msg_base + siz); | data = buf->msg_base; |
siz += sizeof(struct mqtthdr); | hdr = (struct mqtthdr *) data; |
} |
} |
|
|
|
/* fixed header */ |
|
MQTTHDR_MSGINIT(hdr); |
|
hdr->mqtt_msg.type = MQTT_TYPE_SUBSCRIBE; |
|
hdr->mqtt_msg.qos = QOS; |
|
hdr->mqtt_msg.dup = Dup ? 1 : 0; |
|
hdr->mqtt_msg.retain = 0; |
|
l = (u_int*) hdr->mqtt_len; |
|
*l = n; |
|
data += siz; |
|
|
/* variable header */ |
/* variable header */ |
mid = (mqtt_len_t*) (buf->msg_base + siz); | mid = (mqtt_len_t*) data; |
mid->val = htons(msgID); |
mid->val = htons(msgID); |
siz += sizeof(mqtt_len_t); | data += sizeof(mqtt_len_t); |
|
|
/* payload with subscriptions */ |
/* payload with subscriptions */ |
for (t = Topics; t && t->sub_topic.msg_base; t++) { |
for (t = Topics; t && t->sub_topic.msg_base; t++) { |
topic = (mqtthdr_var_t*) (buf->msg_base + siz); | topic = (mqtthdr_var_t*) data; |
topic->var_sb.val = htons(t->sub_topic.msg_len); |
topic->var_sb.val = htons(t->sub_topic.msg_len); |
memcpy(topic->var_data, t->sub_topic.msg_base, ntohs(topic->var_sb.val)); |
memcpy(topic->var_data, t->sub_topic.msg_base, ntohs(topic->var_sb.val)); |
siz += MQTTHDR_VAR_SIZEOF(topic); | data += MQTTHDR_VAR_SIZEOF(topic); |
qos = (buf->msg_base + siz); | qos = data++; |
*qos = t->sub_ret; |
*qos = t->sub_ret; |
siz++; |
|
} |
} |
|
|
/* fixed header */ | return siz + len; |
MQTTHDR_MSGINIT(hdr); | |
hdr->mqtt_msg.type = MQTT_TYPE_SUBSCRIBE; | |
hdr->mqtt_msg.qos = QOS; | |
hdr->mqtt_msg.dup = Dup ? 1 : 0; | |
hdr->mqtt_msg.retain = 0; | |
*hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr)); | |
| |
mqtt_msgRealloc(buf, siz); | |
return siz; | |
} |
} |
|
|
/* |
/* |
Line 157 mqtt_msgSUBACK(mqtt_msg_t * __restrict buf, mqtt_subsc
|
Line 169 mqtt_msgSUBACK(mqtt_msg_t * __restrict buf, mqtt_subsc
|
hdr->mqtt_msg.type = MQTT_TYPE_SUBACK; |
hdr->mqtt_msg.type = MQTT_TYPE_SUBACK; |
*hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr)); |
*hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr)); |
|
|
mqtt_msgRealloc(buf, siz); |
|
return siz; |
return siz; |
} |
} |
|
|
Line 175 int
|
Line 186 int
|
mqtt_msgUNSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics, |
mqtt_msgUNSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_subscr_t * __restrict Topics, |
u_short msgID, u_char Dup, u_char QOS) |
u_short msgID, u_char Dup, u_char QOS) |
{ |
{ |
int siz = 0; | int len, siz = 0; |
| u_int n, *l; |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
mqtthdr_var_t *topic; |
mqtthdr_var_t *topic; |
mqtt_len_t *mid; |
mqtt_len_t *mid; |
mqtt_subscr_t *t; |
mqtt_subscr_t *t; |
|
void *data; |
|
|
if (!buf || !Topics) |
if (!buf || !Topics) |
return -1; |
return -1; |
Line 192 mqtt_msgUNSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_
|
Line 205 mqtt_msgUNSUBSCRIBE(mqtt_msg_t * __restrict buf, mqtt_
|
return -1; |
return -1; |
} |
} |
|
|
if (mqtt_msgRealloc(buf, MQTTMSG_MAX) == -1) | /* calculate message size */ |
| len = sizeof(mqtt_len_t); /* msgid */ |
| for (t = Topics; t && t->sub_topic.msg_base; t++) /* subscribes */ |
| len += sizeof(mqtt_len_t) + t->sub_topic.msg_len; |
| |
| /* calculate header size */ |
| siz = sizeof(struct mqtthdr); /* mqtt fixed header */ |
| n = mqtt_encodeLen(len); /* message size */ |
| siz += mqtt_sizeLen(n) - 1; /* length size */ |
| |
| if (mqtt_msgRealloc(buf, siz + len) == -1) |
return -1; |
return -1; |
else { |
else { |
hdr = (struct mqtthdr *) (buf->msg_base + siz); | data = buf->msg_base; |
siz += sizeof(struct mqtthdr); | hdr = (struct mqtthdr *) data; |
} |
} |
|
|
|
/* fixed header */ |
|
MQTTHDR_MSGINIT(hdr); |
|
hdr->mqtt_msg.type = MQTT_TYPE_UNSUBSCRIBE; |
|
hdr->mqtt_msg.qos = QOS; |
|
hdr->mqtt_msg.dup = Dup ? 1 : 0; |
|
hdr->mqtt_msg.retain = 0; |
|
l = (u_int*) hdr->mqtt_len; |
|
*l = n; |
|
data += siz; |
|
|
/* variable header */ |
/* variable header */ |
mid = (mqtt_len_t*) (buf->msg_base + siz); |
mid = (mqtt_len_t*) (buf->msg_base + siz); |
mid->val = htons(msgID); |
mid->val = htons(msgID); |
siz += sizeof(mqtt_len_t); | data += sizeof(mqtt_len_t); |
|
|
/* payload with subscriptions */ |
/* payload with subscriptions */ |
for (t = Topics; t && t->sub_topic.msg_base; t++) { |
for (t = Topics; t && t->sub_topic.msg_base; t++) { |
topic = (mqtthdr_var_t*) (buf->msg_base + siz); | topic = (mqtthdr_var_t*) data; |
topic->var_sb.val = htons(t->sub_topic.msg_len); |
topic->var_sb.val = htons(t->sub_topic.msg_len); |
memcpy(topic->var_data, t->sub_topic.msg_base, ntohs(topic->var_sb.val)); |
memcpy(topic->var_data, t->sub_topic.msg_base, ntohs(topic->var_sb.val)); |
siz += MQTTHDR_VAR_SIZEOF(topic); | data += MQTTHDR_VAR_SIZEOF(topic); |
} |
} |
|
|
/* fixed header */ | return siz + len; |
MQTTHDR_MSGINIT(hdr); | |
hdr->mqtt_msg.type = MQTT_TYPE_UNSUBSCRIBE; | |
hdr->mqtt_msg.qos = QOS; | |
hdr->mqtt_msg.dup = Dup ? 1 : 0; | |
hdr->mqtt_msg.retain = 0; | |
*hdr->mqtt_len = mqtt_encodeLen(siz - sizeof(struct mqtthdr)); | |
| |
mqtt_msgRealloc(buf, siz); | |
return siz; | |
} |
} |
|
|
/* |
/* |
Line 317 mqtt_readSUBSCRIBE(mqtt_msg_t * __restrict buf, u_shor
|
Line 341 mqtt_readSUBSCRIBE(mqtt_msg_t * __restrict buf, u_shor
|
mqtt_SetErr(EINVAL, "Short message length %d", len); |
mqtt_SetErr(EINVAL, "Short message length %d", len); |
return -1; |
return -1; |
} |
} |
subs = mqtt_subRealloc(subs, i + 1); | if (!mqtt_subRealloc(&subs, i + 1)) { |
if (!subs) { | |
mqtt_subFree(subscr); |
mqtt_subFree(subscr); |
return -1; |
return -1; |
} else |
} else |
Line 326 mqtt_readSUBSCRIBE(mqtt_msg_t * __restrict buf, u_shor
|
Line 349 mqtt_readSUBSCRIBE(mqtt_msg_t * __restrict buf, u_shor
|
|
|
memset(&subs[i], 0, sizeof subs[i]); |
memset(&subs[i], 0, sizeof subs[i]); |
subs[i].sub_topic.msg_len = ntohs(var->var_sb.val); |
subs[i].sub_topic.msg_len = ntohs(var->var_sb.val); |
subs[i].sub_topic.msg_base = malloc(subs[i].sub_topic.msg_len); | subs[i].sub_topic.msg_base = malloc(subs[i].sub_topic.msg_len + 1); |
if (!subs[i].sub_topic.msg_base) { |
if (!subs[i].sub_topic.msg_base) { |
LOGERR; |
LOGERR; |
mqtt_subFree(subscr); |
mqtt_subFree(subscr); |
return -1; |
return -1; |
} else | } else { |
memcpy(subs[i].sub_topic.msg_base, var->var_data, subs[i].sub_topic.msg_len); |
memcpy(subs[i].sub_topic.msg_base, var->var_data, subs[i].sub_topic.msg_len); |
|
((char*) subs[i].sub_topic.msg_base)[subs[i].sub_topic.msg_len] = 0; |
|
} |
pos += MQTTHDR_VAR_SIZEOF(var); |
pos += MQTTHDR_VAR_SIZEOF(var); |
|
|
subs[i].sub_ret = *pos; |
subs[i].sub_ret = *pos; |
Line 441 mqtt_readUNSUBSCRIBE(mqtt_msg_t * __restrict buf, u_sh
|
Line 466 mqtt_readUNSUBSCRIBE(mqtt_msg_t * __restrict buf, u_sh
|
mqtt_SetErr(EINVAL, "Short message length %d", len); |
mqtt_SetErr(EINVAL, "Short message length %d", len); |
return -1; |
return -1; |
} |
} |
subs = mqtt_subRealloc(subs, i + 1); | if (!mqtt_subRealloc(&subs, i + 1)) { |
if (!subs) { | |
mqtt_subFree(subscr); |
mqtt_subFree(subscr); |
return -1; |
return -1; |
} else |
} else |
Line 450 mqtt_readUNSUBSCRIBE(mqtt_msg_t * __restrict buf, u_sh
|
Line 474 mqtt_readUNSUBSCRIBE(mqtt_msg_t * __restrict buf, u_sh
|
|
|
memset(&subs[i], 0, sizeof subs[i]); |
memset(&subs[i], 0, sizeof subs[i]); |
subs[i].sub_topic.msg_len = ntohs(var->var_sb.val); |
subs[i].sub_topic.msg_len = ntohs(var->var_sb.val); |
subs[i].sub_topic.msg_base = malloc(subs[i].sub_topic.msg_len); | subs[i].sub_topic.msg_base = malloc(subs[i].sub_topic.msg_len + 1); |
if (!subs[i].sub_topic.msg_base) { |
if (!subs[i].sub_topic.msg_base) { |
LOGERR; |
LOGERR; |
mqtt_subFree(subscr); |
mqtt_subFree(subscr); |
return -1; |
return -1; |
} else | } else { |
memcpy(subs[i].sub_topic.msg_base, var->var_data, subs[i].sub_topic.msg_len); |
memcpy(subs[i].sub_topic.msg_base, var->var_data, subs[i].sub_topic.msg_len); |
|
((char*) subs[i].sub_topic.msg_base)[subs[i].sub_topic.msg_len] = 0; |
|
} |
pos += MQTTHDR_VAR_SIZEOF(var); |
pos += MQTTHDR_VAR_SIZEOF(var); |
} |
} |
|
|