1 /*
2  * Copyright (c) 2022 René Beckmann
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /** @file mqtt_sn_msg.h
8  *
9  * @brief Function and data structures internal to MQTT-SN module.
10  */
11 
12 #ifndef MQTT_SN_MSG_H_
13 #define MQTT_SN_MSG_H_
14 
15 #include <zephyr/net/mqtt_sn.h>
16 #include <zephyr/net/buf.h>
17 
18 #ifdef __cplusplus
19 extern "C" {
20 #endif
21 
22 #define MQTT_SN_LENGTH_FIELD_EXTENDED_PREFIX 0x01
23 #define MQTT_SN_PROTOCOL_ID 0x01
24 
25 struct mqtt_sn_flags {
26 	bool dup;
27 	enum mqtt_sn_qos qos;
28 	bool retain;
29 	bool will;
30 	bool clean_session;
31 	enum mqtt_sn_topic_type topic_type;
32 };
33 
34 enum mqtt_sn_msg_type {
35 	MQTT_SN_MSG_TYPE_ADVERTISE = 0x00,
36 	MQTT_SN_MSG_TYPE_SEARCHGW = 0x01,
37 	MQTT_SN_MSG_TYPE_GWINFO = 0x02,
38 	MQTT_SN_MSG_TYPE_CONNECT = 0x04,
39 	MQTT_SN_MSG_TYPE_CONNACK = 0x05,
40 	MQTT_SN_MSG_TYPE_WILLTOPICREQ = 0x06,
41 	MQTT_SN_MSG_TYPE_WILLTOPIC = 0x07,
42 	MQTT_SN_MSG_TYPE_WILLMSGREQ = 0x08,
43 	MQTT_SN_MSG_TYPE_WILLMSG = 0x09,
44 	MQTT_SN_MSG_TYPE_REGISTER = 0x0A,
45 	MQTT_SN_MSG_TYPE_REGACK = 0x0B,
46 	MQTT_SN_MSG_TYPE_PUBLISH = 0x0C,
47 	MQTT_SN_MSG_TYPE_PUBACK = 0x0D,
48 	MQTT_SN_MSG_TYPE_PUBCOMP = 0x0E,
49 	MQTT_SN_MSG_TYPE_PUBREC = 0x0F,
50 	MQTT_SN_MSG_TYPE_PUBREL = 0x10,
51 	MQTT_SN_MSG_TYPE_SUBSCRIBE = 0x12,
52 	MQTT_SN_MSG_TYPE_SUBACK = 0x13,
53 	MQTT_SN_MSG_TYPE_UNSUBSCRIBE = 0x14,
54 	MQTT_SN_MSG_TYPE_UNSUBACK = 0x15,
55 	MQTT_SN_MSG_TYPE_PINGREQ = 0x16,
56 	MQTT_SN_MSG_TYPE_PINGRESP = 0x17,
57 	MQTT_SN_MSG_TYPE_DISCONNECT = 0x18,
58 	MQTT_SN_MSG_TYPE_WILLTOPICUPD = 0x1A,
59 	MQTT_SN_MSG_TYPE_WILLTOPICRESP = 0x1B,
60 	MQTT_SN_MSG_TYPE_WILLMSGUPD = 0x1C,
61 	MQTT_SN_MSG_TYPE_WILLMSGRESP = 0x1D,
62 	MQTT_SN_MSG_TYPE_ENCAPSULATED = 0xFE,
63 };
64 
65 struct mqtt_sn_param_advertise {
66 	uint8_t gw_id;
67 	uint16_t duration;
68 };
69 
70 struct mqtt_sn_param_searchgw {
71 	uint8_t radius;
72 };
73 
74 struct mqtt_sn_param_gwinfo {
75 	uint8_t gw_id;
76 	struct mqtt_sn_data gw_add;
77 };
78 
79 struct mqtt_sn_param_connect {
80 	bool will;
81 	bool clean_session;
82 	uint16_t duration;
83 	struct mqtt_sn_data client_id;
84 };
85 
86 struct mqtt_sn_param_connack {
87 	enum mqtt_sn_return_code ret_code;
88 };
89 
90 struct mqtt_sn_param_willtopic {
91 	enum mqtt_sn_qos qos;
92 	bool retain;
93 	struct mqtt_sn_data topic;
94 };
95 
96 struct mqtt_sn_param_willmsg {
97 	struct mqtt_sn_data msg;
98 };
99 
100 struct mqtt_sn_param_register {
101 	uint16_t msg_id;
102 	uint16_t topic_id;
103 	struct mqtt_sn_data topic;
104 };
105 
106 struct mqtt_sn_param_regack {
107 	uint16_t msg_id;
108 	uint16_t topic_id;
109 	enum mqtt_sn_return_code ret_code;
110 };
111 
112 struct mqtt_sn_param_publish {
113 	bool dup;
114 	bool retain;
115 	enum mqtt_sn_qos qos;
116 	enum mqtt_sn_topic_type topic_type;
117 	uint16_t topic_id;
118 	uint16_t msg_id;
119 	struct mqtt_sn_data data;
120 };
121 
122 struct mqtt_sn_param_puback {
123 	uint16_t msg_id;
124 	uint16_t topic_id;
125 	enum mqtt_sn_return_code ret_code;
126 };
127 
128 struct mqtt_sn_param_pubrec {
129 	uint16_t msg_id;
130 };
131 
132 struct mqtt_sn_param_pubrel {
133 	uint16_t msg_id;
134 };
135 
136 struct mqtt_sn_param_pubcomp {
137 	uint16_t msg_id;
138 };
139 
140 struct mqtt_sn_param_subscribe {
141 	bool dup;
142 	enum mqtt_sn_qos qos;
143 	enum mqtt_sn_topic_type topic_type;
144 	uint16_t msg_id;
145 	struct {
146 		struct mqtt_sn_data topic_name;
147 		uint16_t topic_id;
148 	} topic;
149 };
150 
151 struct mqtt_sn_param_suback {
152 	enum mqtt_sn_qos qos;
153 	uint16_t topic_id;
154 	uint16_t msg_id;
155 	enum mqtt_sn_return_code ret_code;
156 };
157 
158 struct mqtt_sn_param_unsubscribe {
159 	enum mqtt_sn_topic_type topic_type;
160 	uint16_t msg_id;
161 	union {
162 		struct mqtt_sn_data topic_name;
163 		uint16_t topic_id;
164 	} topic;
165 };
166 
167 struct mqtt_sn_param_unsuback {
168 	uint16_t msg_id;
169 };
170 
171 struct mqtt_sn_param_pingreq {
172 	struct mqtt_sn_data client_id;
173 };
174 
175 struct mqtt_sn_param_disconnect {
176 	uint16_t duration;
177 };
178 
179 struct mqtt_sn_param_willtopicupd {
180 	enum mqtt_sn_qos qos;
181 	bool retain;
182 	struct mqtt_sn_data topic;
183 };
184 
185 struct mqtt_sn_param_willmsgupd {
186 	struct mqtt_sn_data msg;
187 };
188 
189 struct mqtt_sn_param_willtopicresp {
190 	enum mqtt_sn_return_code ret_code;
191 };
192 
193 struct mqtt_sn_param_willmsgresp {
194 	enum mqtt_sn_return_code ret_code;
195 };
196 
197 struct mqtt_sn_param {
198 	enum mqtt_sn_msg_type type;
199 	union {
200 		struct mqtt_sn_param_advertise advertise;
201 		struct mqtt_sn_param_searchgw searchgw;
202 		struct mqtt_sn_param_gwinfo gwinfo;
203 		struct mqtt_sn_param_connect connect;
204 		struct mqtt_sn_param_connack connack;
205 		struct mqtt_sn_param_willtopic willtopic;
206 		struct mqtt_sn_param_willmsg willmsg;
207 		struct mqtt_sn_param_register reg;
208 		struct mqtt_sn_param_regack regack;
209 		struct mqtt_sn_param_publish publish;
210 		struct mqtt_sn_param_puback puback;
211 		struct mqtt_sn_param_pubrec pubrec;
212 		struct mqtt_sn_param_pubrel pubrel;
213 		struct mqtt_sn_param_pubcomp pubcomp;
214 		struct mqtt_sn_param_subscribe subscribe;
215 		struct mqtt_sn_param_suback suback;
216 		struct mqtt_sn_param_unsubscribe unsubscribe;
217 		struct mqtt_sn_param_unsuback unsuback;
218 		struct mqtt_sn_param_pingreq pingreq;
219 		struct mqtt_sn_param_disconnect disconnect;
220 		struct mqtt_sn_param_willtopicupd willtopicupd;
221 		struct mqtt_sn_param_willmsgupd willmsgupd;
222 		struct mqtt_sn_param_willtopicresp willtopicresp;
223 		struct mqtt_sn_param_willmsgresp willmsgresp;
224 	} params;
225 };
226 
227 /**@brief MQTT-SN Flags-field bitmasks */
228 #define MQTT_SN_FLAGS_DUP BIT(7)
229 #define MQTT_SN_FLAGS_QOS_0 0
230 #define MQTT_SN_FLAGS_QOS_1 BIT(5)
231 #define MQTT_SN_FLAGS_QOS_2 BIT(6)
232 #define MQTT_SN_FLAGS_QOS_M1 BIT(5) | BIT(6)
233 #define MQTT_SN_FLAGS_MASK_QOS (BIT(5) | BIT(6))
234 #define MQTT_SN_FLAGS_SHIFT_QOS 5
235 #define MQTT_SN_FLAGS_RETAIN BIT(4)
236 #define MQTT_SN_FLAGS_WILL BIT(3)
237 #define MQTT_SN_FLAGS_CLEANSESSION BIT(2)
238 #define MQTT_SN_FLAGS_TOPICID_TYPE_NORMAL 0
239 #define MQTT_SN_FLAGS_TOPICID_TYPE_PREDEF BIT(0)
240 #define MQTT_SN_FLAGS_TOPICID_TYPE_SHORT BIT(1)
241 #define MQTT_SN_FLAGS_MASK_TOPICID_TYPE (BIT(0) | BIT(1))
242 #define MQTT_SN_FLAGS_SHIFT_TOPICID_TYPE 0
243 
net_buf_simple_add_data(struct net_buf_simple * buf,struct mqtt_sn_data * data)244 static inline void *net_buf_simple_add_data(struct net_buf_simple *buf, struct mqtt_sn_data *data)
245 {
246 	return net_buf_simple_add_mem(buf, data->data, data->size);
247 }
248 
249 int mqtt_sn_encode_msg(struct net_buf_simple *buf, struct mqtt_sn_param *params);
250 
251 int mqtt_sn_decode_msg(struct net_buf_simple *buf, struct mqtt_sn_param *params);
252 
253 #ifdef __cplusplus
254 }
255 #endif
256 
257 #endif /* MQTT_SN_MSG_H_ */
258