Annotation of mqtt/src/aitmqtt.c, revision 1.1.1.1.2.11
1.1 misho 1: #include "global.h"
2:
3:
1.1.1.1.2.4 misho 4: #pragma GCC visibility push(hidden)
1.1 misho 5:
1.1.1.1.2.4 misho 6: int mqtt_Errno;
7: char mqtt_Error[STRSIZ];
8:
9: #pragma GCC visibility pop
1.1 misho 10:
11: //
12: // Error maintenance functions ...
13: //
14:
15: // mqtt_GetErrno() Get error code of last operation
16: inline int
17: mqtt_GetErrno()
18: {
19: return mqtt_Errno;
20: }
21:
22: // mqtt_GetError() Get error text of last operation
23: inline const char *
24: mqtt_GetError()
25: {
26: return mqtt_Error;
27: }
28:
29: // mqtt_SetErr() Set error to variables for internal use!!!
30: inline void
31: mqtt_SetErr(int eno, char *estr, ...)
32: {
33: va_list lst;
34:
35: mqtt_Errno = eno;
36: memset(mqtt_Error, 0, sizeof mqtt_Error);
37: va_start(lst, estr);
38: vsnprintf(mqtt_Error, sizeof mqtt_Error, estr, lst);
39: va_end(lst);
40: }
41:
42: // ----------------------------------------------------------
43:
1.1.1.1.2.1 misho 44: /*
1.1.1.1.2.5 misho 45: * mqtt_msgFree() Free MQTT message
1.1.1.1.2.9 misho 46: *
1.1.1.1.2.5 misho 47: * @msg = Message buffer
48: * @all = !=0 Destroy entire message, if MQTT Message allocated with mqtt_msgAlloc()
49: * return: none
50: */
51: inline void
52: mqtt_msgFree(mqtt_msg_t ** __restrict msg, int all)
53: {
54: if (msg && *msg) {
55: if ((*msg)->msg_base) {
56: free((*msg)->msg_base);
57: (*msg)->msg_base = NULL;
58: }
59: if (all) {
60: free(*msg);
61: *msg = NULL;
62: } else
63: (*msg)->msg_len ^= (*msg)->msg_len;
64: }
65: }
66:
67: /*
68: * mqtt_msgAlloc() Allocate memory for MQTT Message
1.1.1.1.2.9 misho 69: *
1.1.1.1.2.5 misho 70: * @len = >0 Allocate buffer with length
71: * return: NULL error or Message, after use must call mqtt_msgFree() with all!=0
72: */
73: inline mqtt_msg_t *
74: mqtt_msgAlloc(u_short len)
75: {
76: mqtt_msg_t *m = NULL;
77:
78: m = malloc(sizeof(mqtt_msg_t));
79: if (!m) {
80: LOGERR;
81: return NULL;
82: } else
83: memset(m, 0, sizeof(mqtt_msg_t));
84:
85: if (len) {
86: m->msg_len = len;
87: m->msg_base = malloc(m->msg_len);
88: if (!m->msg_base) {
89: LOGERR;
90: free(m);
91: return NULL;
92: } else
93: memset(m->msg_base, 0, m->msg_len);
94: }
95:
96: return m;
97: }
98:
99: /*
100: * mqtt_msgRealloc() Reallocate MQTT message buffer
1.1.1.1.2.9 misho 101: *
1.1.1.1.2.5 misho 102: * @msg = MQTT message
103: * @len = new length
104: * return: -1 error or >-1 old buffer length
105: */
106: inline int
107: mqtt_msgRealloc(mqtt_msg_t * __restrict msg, u_short len)
108: {
109: void *p = NULL;
110: int ret = 0;
111:
112: if (!msg)
113: return -1;
114:
115: if (len == msg->msg_len)
116: return len;
117:
118: p = realloc(msg->msg_base, len);
119: if (!p) {
120: LOGERR;
121: return -1;
122: }
123:
124: ret = msg->msg_len;
125: msg->msg_len = len;
126: msg->msg_base = p;
127:
128: return ret;
129: }
130:
131: /*
1.1.1.1.2.1 misho 132: * mqtt_encodeLen() Encode number to MQTT length field
1.1.1.1.2.9 misho 133: *
1.1.1.1.2.1 misho 134: * @num = number for encode
135: * return: -1 error or >-1 length
136: */
137: inline u_int
138: mqtt_encodeLen(u_int num)
139: {
140: register u_int dig, i;
141: u_int ret = 0;
142:
143: if (num > 268435455)
144: return (u_int) -1;
145:
146: for (i = 0; i < sizeof ret && num > 0; i++) {
147: dig = num % 0x80;
148: num /= 0x80;
149: if (num > 0)
150: dig |= 0x80;
151:
152: *((u_char*) &ret + i) = (u_char) dig;
153: }
154:
155: return ret;
156: }
157:
158: /*
159: * mqtt_decodeLen() Decode length from MQTT packet
1.1.1.1.2.9 misho 160: *
1.1.1.1.2.1 misho 161: * @len = length
1.1.1.1.2.2 misho 162: * @n = sizeof bytes, if !=NULL
1.1.1.1.2.1 misho 163: * return: -1 error, >-1 length of message
164: */
165: inline u_int
1.1.1.1.2.2 misho 166: mqtt_decodeLen(u_int len, char *n)
1.1.1.1.2.1 misho 167: {
168: register u_int i, dig, mul;
169: u_int ret = 0;
170: u_char *p = (u_char*) &len;
171:
172: if (len > 0xffffff7f)
173: return (u_int) -1;
174:
175: for (mul = 1, i = 0; i < sizeof ret; i++, mul *= 0x80) {
176: dig = p[i];
177: ret += (dig & 0x7f) * mul;
178:
179: if (!(dig & 0x80))
180: break;
181: }
182:
1.1.1.1.2.2 misho 183: if (n)
184: *n = (char) (i & 0x7f) + 1;
1.1.1.1.2.1 misho 185: return ret;
186: }
1.1.1.1.2.2 misho 187:
188: /*
189: * mqtt_sizeLen Return sizeof len field
1.1.1.1.2.9 misho 190: *
1.1.1.1.2.2 misho 191: * @len = length
192: * return: -1 error, >-1 sizeof len in bytes
193: */
194: inline char
195: mqtt_sizeLen(u_int len)
196: {
197: register char i;
198: u_char *p = (u_char*) &len;
199:
200: if (len > 0xffffff7f)
201: return -1;
202:
203: for (i = 0; i < sizeof len; i++)
204: if (!(*(p + i) & 0x80))
205: break;
206:
207: return ++i;
208: }
1.1.1.1.2.3 misho 209:
210: /*
1.1.1.1.2.6 misho 211: * mqtt_str2sub Create MQTT subscribe variable from string(s)
1.1.1.1.2.9 misho 212: *
1.1.1.1.2.6 misho 213: * @csStr = strings
214: * @strnum = number of strings elements
215: * @qoses = QoS elements applied to subscribe variable,
216: * count of elements must be equal with csStr elements
217: * return: NULL error or != subscribe variables array, must be free after use with mqtt_freeSub()
1.1.1.1.2.3 misho 218: */
1.1.1.1.2.6 misho 219: inline mqtt_subscr_t *
220: mqtt_str2sub(const char **csStr, u_short strnum, u_char *qoses)
1.1.1.1.2.3 misho 221: {
1.1.1.1.2.6 misho 222: mqtt_subscr_t *v;
223: register int i, items;
224: const char **strs;
1.1.1.1.2.3 misho 225:
226: if (!csStr)
227: return NULL;
1.1.1.1.2.6 misho 228: for (items = 0, strs = csStr; *strs; items++, strs++)
229: if (strnum && items >= strnum) {
230: items = strnum;
231: break;
232: }
233:
234: if (!(v = malloc((items + 1) * sizeof(mqtt_subscr_t)))) {
1.1.1.1.2.3 misho 235: LOGERR;
236: return NULL;
237: } else
1.1.1.1.2.6 misho 238: memset(v, 0, (items + 1) * sizeof(mqtt_subscr_t));
1.1.1.1.2.3 misho 239:
1.1.1.1.2.6 misho 240: for (i = 0; i < items; i++) {
1.1.1.1.2.11! misho 241: v[i].sub_topic._size = strlen(csStr[i]);
! 242: v[i].sub_topic._base = strdup(csStr[i]);
1.1.1.1.2.6 misho 243: if (qoses && qoses[i] < MQTT_QOS_RESERVED)
1.1.1.1.2.11! misho 244: v[i].sub_ret = qoses[i];
1.1.1.1.2.6 misho 245: }
1.1.1.1.2.3 misho 246:
247: return v;
248: }
1.1.1.1.2.6 misho 249:
250: /*
1.1.1.1.2.9 misho 251: * mqtt_subFree() Free array from subscribe variables
1.1.1.1.2.6 misho 252: *
253: * @subs = Subscribe variables
254: * return: none
255: */
256: inline void
1.1.1.1.2.10 misho 257: mqtt_subFree(mqtt_subscr_t ** __restrict subs)
1.1.1.1.2.6 misho 258: {
259: mqtt_subscr_t *v;
260:
261: if (!subs)
262: return;
263:
1.1.1.1.2.11! misho 264: for (v = *subs; v->sub_topic._base; v++) {
! 265: free(v->sub_topic._base);
! 266: v->sub_topic._base = NULL;
! 267: v->sub_topic._size = 0;
1.1.1.1.2.8 misho 268: }
269:
1.1.1.1.2.10 misho 270: free(*subs);
271: *subs = NULL;
1.1.1.1.2.6 misho 272: }
1.1.1.1.2.9 misho 273:
274: /*
275: * mqtt_subAlloc() Create array from subscribe variables
276: *
277: * @num = Number of elements
1.1.1.1.2.10 misho 278: * return: NULL error or subscribe array, after use must call mqtt_subFree()
1.1.1.1.2.9 misho 279: */
280: inline mqtt_subscr_t *
281: mqtt_subAlloc(u_short num)
282: {
283: mqtt_subscr_t *s = NULL;
284: register int i;
285:
286: s = malloc((num + 1) * sizeof(mqtt_subscr_t));
287: if (!s) {
288: LOGERR;
289: return NULL;
290: } else
291: memset(s, 0, (num + 1) * sizeof(mqtt_subscr_t));
292:
293: for (i = 0; i < num; i++)
1.1.1.1.2.11! misho 294: if (!(s[i].sub_topic._base = malloc(0))) {
1.1.1.1.2.9 misho 295: LOGERR;
296: break;
297: }
298:
299: return s;
300: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>