-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmqttmsg.c
153 lines (148 loc) · 3.43 KB
/
mqttmsg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// MQTT message formatting
#include <string.h>
#include <stdlib.h>
#include "AJL/ajl.h"
#include "mqttmsg.h"
void
mqtt_topic (j_t j, const char *topic, int tlen)
{ // Break down _meta.topic in to prefix and suffix (optionally store it too)
if (!j)
return;
j_t meta = j_path (j, "_meta");
if (topic)
{
if (tlen < 0)
j_store_string (meta, "topic", topic);
else if (tlen)
j_store_stringn (meta, "topic", topic, tlen);
}
topic = j_get (meta, "topic");
if (!topic)
return;
int n;
for (n = 0; topic[n] && topic[n] != '/'; n++);
if (topic[n])
{
j_store_stringn (meta, "prefix", topic, n);
n++;
while (topic[n] && topic[n] != '/')
n++; // SS
if (topic[n])
{
n++;
int id = n;
while (topic[n] && topic[n] != '/')
n++; // ID
j_store_stringn (meta, "target", topic + id, n - id);
if (topic[n])
{
n++;
if (topic[n])
j_store_string (meta, "suffix", topic + n);
}
}
}
}
void
mqtt_dataonly (j_t j)
{ // Remove meta
j_t meta = j_find (j, "_meta");
if (meta)
j_delete (&meta);
j_t data = j_find (j, "_data");
if (data)
j_replace (j, &data);
}
j_t
mqtt_decode (unsigned char *buf, size_t len)
{ // Decode and MQTT message, return JSON payload, with topic in _meta.topic
if ((*buf & 0xF0) != 0x30)
return NULL;
unsigned char *b = buf,
*e = buf + len;
unsigned char qos = (*b >> 1) & 3;
unsigned char retain = (*b & 1);
unsigned char dup = ((*b >> 3) & 1);
b++;
int l = 0,
s = 0;
while (b < e && (*b & 0x80))
{
l |= (*b & 0x7F) << s;
s += 7;
b++;
}
l |= (*b++ << s);
if (b + l != e)
return NULL;
int id = 0;
int tlen = (b[0] << 8) + b[1];
b += 2;
char *topic = (char *) b;
b += tlen;
if (qos)
{
id = (b[0] << 8) + b[1];
b += 2;
}
int plen = len - (b - buf);
j_t j = j_create ();
if (plen)
{
const char *fail = j_read_mem (j, (char *) b, plen);
if (fail)
j_stringn (j, (char *) b, plen);
}
if (!j_isobject (j))
{
j_t n = j_create ();
j_store_json (n, "_data", &j);
j = n;
}
mqtt_topic (j, topic, tlen);
if (qos)
{
j_int (j_path (j, "_meta.id"), id);
j_int (j_path (j, "_meta.qos"), qos);
if (retain)
j_true (j_path (j, "_meta.retain"));
if (dup)
j_true (j_path (j, "_meta.dup"));
}
return j;
}
size_t
mqtt_encode (unsigned char *tx, size_t max, const char *topic, j_t j)
{
// Make publish
unsigned int txp = 0;
tx[txp++] = 0x30; // Not dup, qos=0, no retain
tx[txp++] = 0; // Len TBA
tx[txp++] = 0;
unsigned int l = strlen (topic ? : "");
if (txp + l > max)
return 0;
tx[txp++] = (l >> 8);
tx[txp++] = l;
if (l)
memcpy (tx + txp, topic, l);
txp += l;
// QoS 0 so not packet ID
size_t len = 0;
char *buf = NULL;
if (j && !j_isnull (j) && j_write_mem (j, &buf, &len))
return 0;
if (txp + len > max)
{
free (buf);
return 0;
}
if (len)
memcpy (tx + txp, buf, len);
txp += len;
free (buf);
// Store len
tx[1] = ((txp - 3) & 0x7F) + 0x80;
tx[2] = ((txp - 3) >> 7);
return txp;
}