version 1.1.2.7, 2011/12/05 22:17:39
|
version 1.1.2.8, 2011/12/06 09:04:55
|
Line 18
|
Line 18
|
*/ |
*/ |
int |
int |
mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const char *csTopic, u_short msgID, |
mqtt_msgPUBLISH(mqtt_msg_t * __restrict buf, const char *csTopic, u_short msgID, |
u_char Dup, u_char QOS, u_char Retain, const void *pData, u_short datlen) | u_char Dup, u_char QOS, u_char Retain, const void *pData, int datlen) |
{ |
{ |
int siz = 0; |
int siz = 0; |
struct mqtthdr *hdr; |
struct mqtthdr *hdr; |
Line 157 mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, u_short m
|
Line 157 mqtt_msgPUBCOMP(mqtt_msg_t * __restrict buf, u_short m
|
|
|
|
|
/* ============= decode ============ */ |
/* ============= decode ============ */ |
|
|
|
/* |
|
* mqtt_readPUBLISH() Read PUBLISH message |
|
* |
|
* @buf = Message buffer |
|
* @psTopic = Topic |
|
* @topicLen = Topic length |
|
* @msgID = MessageID |
|
* @pData = Data buffer |
|
* @datLen = Data buffer length, if *datLen == 0 allocate memory for pData |
|
* return: NULL error or !=NULL MQTT fixed header |
|
*/ |
|
struct mqtthdr * |
|
mqtt_readPUBLISH(mqtt_msg_t * __restrict buf, char * __restrict psTopic, int topicLen, |
|
u_short *msgID, void * __restrict pData, int *datLen) |
|
{ |
|
int len, ret; |
|
struct mqtthdr *hdr; |
|
mqtthdr_var_t *var; |
|
mqtt_v_t *v; |
|
caddr_t pos; |
|
|
|
if (!buf || !psTopic || !msgID || !pData) |
|
return NULL; |
|
|
|
hdr = _mqtt_readHEADER(buf, MQTT_TYPE_PUBLISH, &ret, &len); |
|
if (!hdr) |
|
return NULL; |
|
if (len < sizeof(mqtt_v_t)) { |
|
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
|
return NULL; |
|
} else { |
|
pos = buf->msg_base + ret + 1; |
|
var = (mqtthdr_var_t*) pos; |
|
} |
|
|
|
/* topic */ |
|
len -= MQTTHDR_VAR_SIZEOF(var); |
|
if (len < 0) { |
|
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
|
return NULL; |
|
} else { |
|
memset(psTopic, 0, topicLen--); |
|
memcpy(psTopic, var->var_data, ntohs(var->var_sb.val) > topicLen ? |
|
topicLen : ntohs(var->var_sb.val)); |
|
pos += MQTTHDR_VAR_SIZEOF(var); |
|
v = (mqtt_v_t*) pos; |
|
} |
|
|
|
len -= sizeof(mqtt_v_t); |
|
if (len < 0) { |
|
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
|
return NULL; |
|
} else { |
|
*msgID = ntohs(v->val); |
|
pos += sizeof(mqtt_v_t); |
|
} |
|
|
|
/* data */ |
|
if (len < 0) { |
|
mqtt_SetErr(EINVAL, "Error:: short message length %d", len); |
|
return NULL; |
|
} else { |
|
if (!*datLen) { |
|
if (!(pData = malloc(len))) { |
|
LOGERR; |
|
return NULL; |
|
} else |
|
*datLen = len; |
|
} |
|
|
|
memset(pData, 0, *datLen); |
|
if (len < *datLen) |
|
*datLen = len; |
|
memcpy(pData, pos, *datLen); |
|
} |
|
|
|
return hdr; |
|
} |
|
|
/* |
/* |
* mqtt_readPUBACK() Read PUBACK message |
* mqtt_readPUBACK() Read PUBACK message |