Loading...
Searching...
No Matches
rules.h
1/*
2 * SPDX-FileCopyrightText: 2018 imec IDLab
3 * SPDX-FileCopyrightText: 2022 Freie Universität Berlin
4 * SPDX-License-Identifier: GPL-3.0-or-later
5 */
6
7#pragma once
8
14
15#include "kernel_defines.h"
16
17#include "schc.h"
18#ifdef USE_COAP
19#include "net/coap.h"
20#endif
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#if USE_IP6
27static const struct schc_ipv6_rule_t ipv6_rule1 = {
28 .up = 10, .down = 10, .length = 11,
29 {
30 /* field, ML, len, pos, dir, val, MO, CDA */
31 { IP6_V, 0, 4, 1, BI, {6}, &mo_equal, NOTSENT },
32 { IP6_TC, 0, 8, 1, BI, {0}, &mo_ignore, NOTSENT },
33 { IP6_FL, 0, 20, 1, BI, {0, 0, 0}, &mo_ignore, NOTSENT },
34 { IP6_LEN, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPLENGTH },
35 { IP6_NH, 0, 8, 1, BI, {17}, &mo_equal, NOTSENT },
36 { IP6_HL, 0, 8, 1, UP, {64}, &mo_equal, NOTSENT },
37 { IP6_HL, 0, 8, 1, DOWN, {0}, &mo_ignore, VALUESENT },
38 { IP6_DEVPRE, 0, 64, 1, BI, {
39 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00
40 }, &mo_equal, NOTSENT },
41 { IP6_DEVIID, 0, 64, 1, BI, {
42 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
43 }, &mo_equal, NOTSENT },
44 { IP6_APPPRE, 4, 64, 1, BI, {
45 /* you can store as many IPs as (MAX_FIELD_LENGTH / 8) */
46 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x01, 0x00, 0x00,
47 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x02, 0x00, 0x00,
48 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x03, 0x00, 0x00,
49 0x20, 0x01, 0x0d, 0xb8, 0x00, 0x04, 0x00, 0x00
50 }, &mo_matchmap, MAPPINGSENT },
51 { IP6_APPIID, 0, 64, 1, BI, {
52 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02
53 }, &mo_equal, NOTSENT },
54 }
55};
56
57/* link local test rule */
58static const struct schc_ipv6_rule_t ipv6_rule2 = {
59 .up = 10, .down = 10, .length = 10,
60 {
61 /* field, ML, len, pos, dir, val, MO, CDA */
62 { IP6_V, 0, 4, 1, BI, {6}, &mo_equal, NOTSENT },
63 { IP6_TC, 0, 8, 1, BI, {0}, &mo_ignore, NOTSENT },
64 { IP6_FL, 0, 20, 1, BI, {0, 0, 0}, &mo_ignore, NOTSENT },
65 { IP6_LEN, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPLENGTH },
66 { IP6_NH, 2, 8, 1, BI, {17, 58}, &mo_matchmap, MAPPINGSENT },
67 { IP6_HL, 2, 8, 1, BI, {64, 255}, &mo_matchmap, NOTSENT },
68 { IP6_DEVPRE, 0, 64, 1, BI, {
69 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
70 }, &mo_equal, NOTSENT },
71 { IP6_DEVIID, 62, 64, 1, BI, {
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
73 }, &mo_MSB, LSB },
74 { IP6_APPPRE, 0, 64, 1, BI, {
75 0xFE, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
76 }, &mo_equal, NOTSENT },
77 { IP6_APPIID, 62, 64, 1, BI, {
78 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
79 }, &mo_MSB, LSB },
80 }
81};
82#endif
83
84#if USE_UDP
85static const struct schc_udp_rule_t udp_rule1 = {
86 .up = 4, .down = 4, .length = 4,
87 {
88 /* field, ML, len, pos, dir, val, MO, CDA */
89 /* set field length to 16 to indicate 16 bit values
90 * MO param length to 2 to indicate 2 indices */
91 { UDP_DEV, 2, 16, 1, BI, {
92 0x33, 0x16, /* 5683 or */
93 0x33, 0x17 /* 5684 */
94 }, &mo_matchmap, MAPPINGSENT },
95 { UDP_APP, 2, 16, 1, BI, {
96 0x33, 0x16, /* 5683 or */
97 0x33, 0x17 /* 5684 */
98 }, &mo_matchmap, MAPPINGSENT },
99 { UDP_LEN, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPLENGTH },
100 { UDP_CHK, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPCHK },
101 }
102};
103
104static const struct schc_udp_rule_t udp_rule2 = {
105 .up = 4, .down = 4, .length = 4,
106 {
107 /* field, ML, len, pos, dir, val, MO, CDA */
108 { UDP_DEV, 12, 16, 1, BI, {0x1F, 0x40}, &mo_MSB, LSB },
109 { UDP_APP, 12, 16, 1, BI, {0x1F, 0x40}, &mo_MSB, LSB },
110 { UDP_LEN, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPLENGTH },
111 { UDP_CHK, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPCHK },
112 }
113};
114
115static const struct schc_udp_rule_t udp_rule3 = {
116 .up = 4, .down = 4, .length = 4,
117 {
118 /* field, ML, len, pos, dir, val, MO, CDA */
119 { UDP_DEV, 0, 16, 1, BI, {0x13, 0x89}, &mo_equal, NOTSENT },
120 { UDP_APP, 0, 16, 1, BI, {0x13, 0x88}, &mo_equal, NOTSENT },
121 { UDP_LEN, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPLENGTH },
122 { UDP_CHK, 0, 16, 1, BI, {0, 0}, &mo_ignore, COMPCHK },
123 }
124};
125#endif
126
127#if USE_COAP
128/* It is important to use strings, identical to the ones defined in coap.h for the options. */
129
130/* GET /usage */
131static const struct schc_coap_rule_t coap_rule1 = {
132 .up = 9, .down = 7, .length = 9,
133 {
134 /* field, ML, len, pos, dir, val, MO, CDA */
135 { COAP_V, 0, 2, 1, BI, {COAP_V1}, &mo_equal, NOTSENT },
136 /* the MO_param_length (ML) is used to indicate the true length of the list */
137 { COAP_T, 4, 2, 1, BI, {
138 COAP_TYPE_CON, COAP_TYPE_NON, COAP_TYPE_ACK, COAP_TYPE_RST
139 }, &mo_matchmap, MAPPINGSENT },
140 { COAP_TKL, 0, 4, 1, BI, {4}, &mo_equal, NOTSENT },
141 { COAP_C, 0, 8, 1, BI, {COAP_METHOD_PUT}, &mo_equal, NOTSENT },
142 { COAP_MID, 0, 16, 1, BI, {0x23, 0xBB}, &mo_equal, NOTSENT },
143 { COAP_TKN, 24, 32, 1, BI, {
144 0x21, 0xFA, 0x01, 0x00
145 }, &mo_MSB, LSB },
146 { COAP_URIPATH, 0, 40, 1, BI, "usage", &mo_equal, NOTSENT },
147 { COAP_NORESP, 0, 8, 1, BI, {0x1A}, &mo_equal, NOTSENT },
148 { COAP_PAYLOAD, 0, 8, 1, BI, {0xFF}, &mo_equal, NOTSENT }
149 }
150};
151
152/* POST temperature value */
153static const struct schc_coap_rule_t coap_rule2 = {
154 .up = 7, .down = 7, .length = 10,
155 {
156 /* field, ML, len, pos, dir, val, MO, CDA */
157 { COAP_V, 0, 2, 1, BI, {COAP_V1}, &mo_equal, NOTSENT },
158 { COAP_T, 0, 2, 1, BI, {0}, &mo_ignore, VALUESENT },
159 { COAP_TKL, 0, 4, 1, BI, {4}, &mo_equal, NOTSENT },
160 { COAP_C, 0, 8, 1, UP, {COAP_CODE_CONTENT}, &mo_equal, NOTSENT },
161 { COAP_C, 0, 8, 1, DOWN, {COAP_METHOD_GET}, &mo_equal, NOTSENT },
162 /* match the first 12 bits */
163 { COAP_MID, 12, 16, 1, UP, {0x23, 0xBB}, &mo_MSB, LSB },
164 { COAP_MID, 0, 16, 1, DOWN, {0, 0}, &mo_ignore, VALUESENT },
165 { COAP_TKN, 0, 32, 1, BI, {0, 0, 0, 0}, &mo_ignore, VALUESENT },
166 { COAP_URIPATH, 0, 32, 1, DOWN, "temp", &mo_equal, NOTSENT },
167 /* respond with CONTENT */
168 { COAP_PAYLOAD, 0, 8, 1, UP, {0xFF}, &mo_equal, NOTSENT }
169 }
170};
171
172static const struct schc_coap_rule_t coap_rule3 = {
173 .up = 1, .down = 1, .length = 1,
174 {
175 /* field, ML, len, pos, dir, val, MO, CDA */
176 { COAP_V, 0, 2, 1, BI, {COAP_V1}, &mo_equal, NOTSENT },
177 }
178};
179
180static const struct schc_coap_rule_t coap_rule4 = {
181 .up = 12, .down = 12, .length = 12,
182 {
183 /* field, ML, len, pos, dir, val, MO, CDA */
184 { COAP_V, 0, 2, 1, BI, {COAP_V1}, &mo_equal, NOTSENT },
185 { COAP_T, 0, 2, 1, BI, {COAP_TYPE_CON}, &mo_equal, NOTSENT },
186 { COAP_TKL, 0, 4, 1, BI, {8}, &mo_equal, NOTSENT },
187 { COAP_C, 0, 8, 1, BI, {COAP_METHOD_POST}, &mo_equal, NOTSENT },
188 { COAP_MID, 0, 16, 1, BI, {0x23, 0xBB}, &mo_ignore, VALUESENT },
189 /* match the 24 first bits, send the last 8 */
190 { COAP_TKN, 24, 32, 1, BI, {
191 0x21, 0xFA, 0x01, 0x00
192 }, &mo_MSB, LSB },
193 { COAP_URIPATH, 0, 16, 1, BI, "rd", &mo_equal, NOTSENT },
194 { COAP_CONTENTF, 0, 8, 1, BI, {0x28}, &mo_equal, NOTSENT },
195 { COAP_URIQUERY, 0, 72, 1, BI, "lwm2m=1.0", &mo_equal, NOTSENT },
196 { COAP_URIQUERY, 0, 88, 1, BI, "ep=magician", &mo_equal, NOTSENT },
197 { COAP_URIQUERY, 0, 48, 1, BI, "lt=121", &mo_equal, NOTSENT },
198 /* respond with CONTENT */
199 { COAP_PAYLOAD, 0, 8, 1, BI, {0xff}, &mo_equal, NOTSENT }
200 }
201};
202#endif
203
204static const struct schc_compression_rule_t comp_rule_1 = {
205 .rule_id = 0x01,
206 .rule_id_size_bits = 8U,
207#if USE_IP6
208 &ipv6_rule1,
209#endif
210#if USE_UDP
211 &udp_rule1,
212#endif
213#if USE_COAP
214 &coap_rule1,
215#endif
216};
217
218static const struct schc_compression_rule_t comp_rule_2 = {
219 .rule_id = 0x02,
220 .rule_id_size_bits = 8U,
221#if USE_IP6
222 &ipv6_rule1,
223#endif
224#if USE_UDP
225 &udp_rule3,
226#endif
227#if USE_COAP
228 &coap_rule2,
229#endif
230};
231
232static const struct schc_compression_rule_t comp_rule_3 = {
233 .rule_id = 0x03,
234 .rule_id_size_bits = 8U,
235#if USE_IP6
236 &ipv6_rule2,
237#endif
238#if USE_UDP
239 &udp_rule2,
240#endif
241#if USE_COAP
242 &coap_rule3,
243#endif
244};
245
246static const struct schc_compression_rule_t comp_rule_4 = {
247 .rule_id = 0x04,
248 .rule_id_size_bits = 8U,
249#if USE_IP6
250 &ipv6_rule2,
251#endif
252#if USE_UDP
253 &udp_rule2,
254#endif
255#if USE_COAP
256 &coap_rule4,
257#endif
258};
259
260static const struct schc_fragmentation_rule_t frag_rule_21 = {
261 .rule_id = 21,
262 .rule_id_size_bits = 8,
263 .mode = NO_ACK,
264 .dir = BI,
265 .FCN_SIZE = 1, /* FCN size */
266 .MAX_WND_FCN = 0, /* Maximum fragments per window */
267 .WINDOW_SIZE = 0, /* Window size */
268 .DTAG_SIZE = 0, /* DTAG size */
269};
270
271static const struct schc_fragmentation_rule_t frag_rule_22 = {
272 .rule_id = 22,
273 .rule_id_size_bits = 8,
274 .mode = ACK_ON_ERROR,
275 .dir = BI,
276 .FCN_SIZE = 3, /* FCN size */
277 .MAX_WND_FCN = 6, /* Maximum fragments per window */
278 .WINDOW_SIZE = 1, /* Window size */
279 .DTAG_SIZE = 0, /* DTAG size */
280};
281
282static const struct schc_fragmentation_rule_t frag_rule_23 = {
283 .rule_id = 23,
284 .rule_id_size_bits = 8,
285 .mode = ACK_ALWAYS,
286 .dir = BI,
287 .FCN_SIZE = 3, /* FCN size */
288 .MAX_WND_FCN = 6, /* Maximum fragments per window */
289 .WINDOW_SIZE = 1, /* Window size */
290 .DTAG_SIZE = 0, /* DTAG size */
291};
292
293/* save compression rules in flash */
294static const struct schc_compression_rule_t* node1_compression_rules[] = {
295 &comp_rule_1, &comp_rule_2, &comp_rule_3, &comp_rule_4
296};
297
298/* save fragmentation rules in flash */
299static const struct schc_fragmentation_rule_t* node1_fragmentation_rules[] = {
300 &frag_rule_21, &frag_rule_22, &frag_rule_23,
301};
302
303/* rules for a particular device */
304static const struct schc_device node1 = {
305 .device_id = 1,
306 .uncomp_rule_id = 0,
307 .uncomp_rule_id_size_bits = 8,
308 .compression_rule_count = ARRAY_SIZE(node1_compression_rules),
309 .compression_context = &node1_compression_rules,
310 .fragmentation_rule_count = ARRAY_SIZE(node1_fragmentation_rules),
311 .fragmentation_context = &node1_fragmentation_rules
312};
313static const struct schc_device node2 = {
314 .device_id = 2,
315 .uncomp_rule_id = 0,
316 .uncomp_rule_id_size_bits = 8,
317 .compression_rule_count = ARRAY_SIZE(node1_compression_rules),
318 .compression_context = &node1_compression_rules,
319 .fragmentation_rule_count = ARRAY_SIZE(node1_fragmentation_rules),
320 .fragmentation_context = &node1_fragmentation_rules
321};
322
323/* server keeps track of multiple devices: add devices to device list */
324static const struct schc_device* devices[] = { &node1, &node2 };
325
326#define DEVICE_COUNT ((int)ARRAY_SIZE(devices))
327
328#ifdef __cplusplus
329}
330#endif
#define ARRAY_SIZE(a)
Calculate the number of elements in a static array.
Definition container.h:79
#define COAP_CODE_CONTENT
Response Code: 2.05 Content.
Definition coap.h:201
#define COAP_V1
Identifier for CoAP version 1 (RFC 7252)
Definition coap.h:39
@ COAP_METHOD_GET
GET request (no paylod)
Definition coap.h:172
@ COAP_METHOD_PUT
PUT request (update resource with payload)
Definition coap.h:174
@ COAP_METHOD_POST
POST request (resource processes payload)
Definition coap.h:173
Common macros and compiler attributes/pragmas configuration.
Generic CoAP values as defined by RFC7252.