version 1.1.1.1.2.4, 2011/11/18 10:11:37
|
version 1.1.1.1.2.12, 2011/11/28 13:19:39
|
Line 42 mqtt_SetErr(int eno, char *estr, ...)
|
Line 42 mqtt_SetErr(int eno, char *estr, ...)
|
// ---------------------------------------------------------- |
// ---------------------------------------------------------- |
|
|
/* |
/* |
|
* mqtt_msgFree() Free MQTT message |
|
* |
|
* @msg = Message buffer |
|
* @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc() |
|
* return: none |
|
*/ |
|
inline void |
|
mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all) |
|
{ |
|
if (msg && *msg) { |
|
if ((*msg)->msg_base) { |
|
free((*msg)->msg_base); |
|
(*msg)->msg_base = NULL; |
|
} |
|
if (all) { |
|
free(*msg); |
|
*msg = NULL; |
|
} else |
|
(*msg)->msg_len ^= (*msg)->msg_len; |
|
} |
|
} |
|
|
|
/* |
|
* mqtt_msgAlloc() Allocate memory for MQTT Message |
|
* |
|
* @len = >0 Allocate buffer with length |
|
* return: NULL error or Message, after use must call mqtt_msgFree() with all!=0 |
|
*/ |
|
inline mqtt_msg_t * |
|
mqtt_msgAlloc(u_short len) |
|
{ |
|
mqtt_msg_t *m = NULL; |
|
|
|
m = malloc(sizeof(mqtt_msg_t)); |
|
if (!m) { |
|
LOGERR; |
|
return NULL; |
|
} else |
|
memset(m, 0, sizeof(mqtt_msg_t)); |
|
|
|
if (len) { |
|
m->msg_len = len; |
|
m->msg_base = malloc(m->msg_len); |
|
if (!m->msg_base) { |
|
LOGERR; |
|
free(m); |
|
return NULL; |
|
} else |
|
memset(m->msg_base, 0, m->msg_len); |
|
} |
|
|
|
return m; |
|
} |
|
|
|
/* |
|
* mqtt_msgRealloc() Reallocate MQTT message buffer |
|
* |
|
* @msg = MQTT message |
|
* @len = new length |
|
* return: -1 error or >-1 old buffer length |
|
*/ |
|
inline int |
|
mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_short len) |
|
{ |
|
void *p = NULL; |
|
int ret = 0; |
|
|
|
if (!msg) |
|
return -1; |
|
|
|
if (len == msg->msg_len) |
|
return len; |
|
|
|
p = realloc(msg->msg_base, len); |
|
if (!p) { |
|
LOGERR; |
|
return -1; |
|
} |
|
|
|
ret = msg->msg_len; |
|
msg->msg_len = len; |
|
msg->msg_base = p; |
|
|
|
return ret; |
|
} |
|
|
|
/* |
* mqtt_encodeLen() Encode number to MQTT length field |
* mqtt_encodeLen() Encode number to MQTT length field |
|
* |
* @num = number for encode |
* @num = number for encode |
* return: -1 error or >-1 length |
* return: -1 error or >-1 length |
*/ |
*/ |
Line 69 mqtt_encodeLen(u_int num)
|
Line 157 mqtt_encodeLen(u_int num)
|
|
|
/* |
/* |
* mqtt_decodeLen() Decode length from MQTT packet |
* mqtt_decodeLen() Decode length from MQTT packet |
|
* |
* @len = length |
* @len = length |
* @n = sizeof bytes, if !=NULL |
* @n = sizeof bytes, if !=NULL |
* return: -1 error, >-1 length of message |
* return: -1 error, >-1 length of message |
Line 98 mqtt_decodeLen(u_int len, char *n)
|
Line 187 mqtt_decodeLen(u_int len, char *n)
|
|
|
/* |
/* |
* mqtt_sizeLen Return sizeof len field |
* mqtt_sizeLen Return sizeof len field |
|
* |
* @len = length |
* @len = length |
* return: -1 error, >-1 sizeof len in bytes |
* return: -1 error, >-1 sizeof len in bytes |
*/ |
*/ |
Line 118 mqtt_sizeLen(u_int len)
|
Line 208 mqtt_sizeLen(u_int len)
|
} |
} |
|
|
/* |
/* |
* mqtt_str2var Create MQTT variable from string | * mqtt_str2sub Create MQTT subscribe variable from string(s) |
* @csStr = string | * |
* @strLen = string length | * @csStr = strings |
* return: NULL error or != ok variable, must be free after use! | * @strnum = number of strings elements |
| * @qoses = QoS elements applied to subscribe variable, |
| * count of elements must be equal with csStr elements |
| * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub() |
*/ |
*/ |
inline mqtthdr_var_t * | inline mqtt_subscr_t * |
mqtt_str2var(const u_char *csStr, u_short strLen) | mqtt_str2sub(const char **csStr, u_short strnum, u_char *qoses) |
{ |
{ |
mqtthdr_var_t *v; | mqtt_subscr_t *v; |
| register int i, items; |
| const char **strs; |
|
|
if (!csStr) |
if (!csStr) |
return NULL; |
return NULL; |
if (!(v = malloc(strLen + sizeof(mqtthdr_var_t)))) { | for (items = 0, strs = csStr; *strs; items++, strs++) |
| if (strnum && items >= strnum) { |
| items = strnum; |
| break; |
| } |
| |
| if (!(v = malloc((items + 1) * sizeof(mqtt_subscr_t)))) { |
LOGERR; |
LOGERR; |
return NULL; |
return NULL; |
} else |
} else |
memset(v, 0, strLen + sizeof(mqtthdr_var_t)); | memset(v, 0, (items + 1) * sizeof(mqtt_subscr_t)); |
|
|
memcpy(v->var_data, csStr, strLen); | for (i = 0; i < items; i++) { |
v->var_sb.val = htons(strLen); | v[i].sub_topic._size = strlen(csStr[i]); |
| v[i].sub_topic._base = strdup(csStr[i]); |
| if (qoses && qoses[i] < MQTT_QOS_RESERVED) |
| v[i].sub_ret = qoses[i]; |
| } |
|
|
return v; |
return v; |
|
} |
|
|
|
/* |
|
* mqtt_subFree() Free array from subscribe variables |
|
* |
|
* @subs = Subscribe variables |
|
* return: none |
|
*/ |
|
inline void |
|
mqtt_subFree(mqtt_subscr_t ** __restrict subs) |
|
{ |
|
mqtt_subscr_t *v; |
|
|
|
if (!subs) |
|
return; |
|
|
|
for (v = *subs; v->sub_topic._base; v++) { |
|
free(v->sub_topic._base); |
|
v->sub_topic._base = NULL; |
|
v->sub_topic._size = 0; |
|
|
|
if (v->sub_value._base) { |
|
free(v->sub_value._base); |
|
v->sub_value._base = NULL; |
|
v->sub_value._size = 0; |
|
} |
|
} |
|
|
|
free(*subs); |
|
*subs = NULL; |
|
} |
|
|
|
/* |
|
* mqtt_subAlloc() Create array from subscribe variables |
|
* |
|
* @num = Number of elements |
|
* return: NULL error or subscribe array, after use must call mqtt_subFree() |
|
*/ |
|
inline mqtt_subscr_t * |
|
mqtt_subAlloc(u_short num) |
|
{ |
|
mqtt_subscr_t *s = NULL; |
|
|
|
s = malloc((num + 1) * sizeof(mqtt_subscr_t)); |
|
if (!s) { |
|
LOGERR; |
|
return NULL; |
|
} else |
|
memset(s, 0, (num + 1) * sizeof(mqtt_subscr_t)); |
|
|
|
return s; |
} |
} |