Firmware SDK
twr_at_lora.c
1 #include <twr.h>
2 
3 static struct
4 {
5  twr_cmwx1zzabz_t *lora;
6  char tmp[36];
7 
8 } _at;
9 
10 static bool _twr_at_lora_param_format_and_test(twr_atci_param_t *param, uint8_t length);
11 
12 void twr_at_lora_init(twr_cmwx1zzabz_t *lora)
13 {
14  _at.lora = lora;
15 }
16 
17 bool twr_at_lora_deveui_read(void)
18 {
19  twr_cmwx1zzabz_get_deveui(_at.lora, _at.tmp);
20 
21  twr_atci_printfln("$DEVEUI: %s", _at.tmp);
22 
23  return true;
24 }
25 
26 bool twr_at_lora_deveui_set(twr_atci_param_t *param)
27 {
28  if (!_twr_at_lora_param_format_and_test(param, 16))
29  {
30  return false;
31  }
32 
33  twr_cmwx1zzabz_set_deveui(_at.lora, param->txt);
34 
35  return true;
36 }
37 
38 bool twr_at_lora_devaddr_read(void)
39 {
40  twr_cmwx1zzabz_get_devaddr(_at.lora, _at.tmp);
41 
42  twr_atci_printfln("$DEVADDR: %s", _at.tmp);
43 
44  return true;
45 }
46 
47 bool twr_at_lora_devaddr_set(twr_atci_param_t *param)
48 {
49 
50  twr_cmwx1zzabz_set_devaddr(_at.lora, param->txt);
51 
52  return true;
53 }
54 
55 bool twr_at_lora_nwkskey_read(void)
56 {
57  twr_cmwx1zzabz_get_nwkskey(_at.lora, _at.tmp);
58 
59  twr_atci_printfln("$NWKSKEY: %s", _at.tmp);
60 
61  return true;
62 }
63 
64 bool twr_at_lora_nwkskey_set(twr_atci_param_t *param)
65 {
66  if (!_twr_at_lora_param_format_and_test(param, 32))
67  {
68  return false;
69  }
70 
71  twr_cmwx1zzabz_set_nwkskey(_at.lora, param->txt);
72 
73  return true;
74 }
75 
76 bool twr_at_lora_appkey_read(void)
77 {
78  twr_cmwx1zzabz_get_appkey(_at.lora, _at.tmp);
79 
80  twr_atci_printfln("$APPKEY: %s", _at.tmp);
81 
82  return true;
83 }
84 
85 bool twr_at_lora_appkey_set(twr_atci_param_t *param)
86 {
87  if (!_twr_at_lora_param_format_and_test(param, 32))
88  {
89  return false;
90  }
91 
92  twr_cmwx1zzabz_set_appkey(_at.lora, param->txt);
93 
94  return true;
95 }
96 
97 bool twr_at_lora_appeui_read(void)
98 {
99  twr_cmwx1zzabz_get_appeui(_at.lora, _at.tmp);
100 
101  twr_atci_printfln("$APPEUI: %s", _at.tmp);
102 
103  return true;
104 }
105 
106 bool twr_at_lora_appeui_set(twr_atci_param_t *param)
107 {
108  if (!_twr_at_lora_param_format_and_test(param, 16))
109  {
110  return false;
111  }
112 
113  twr_cmwx1zzabz_set_appeui(_at.lora, param->txt);
114 
115  return true;
116 }
117 
118 bool twr_at_lora_appskey_read(void)
119 {
120  twr_cmwx1zzabz_get_appskey(_at.lora, _at.tmp);
121 
122  twr_atci_printfln("$APPSKEY: %s", _at.tmp);
123 
124  return true;
125 }
126 
127 bool twr_at_lora_appskey_set(twr_atci_param_t *param)
128 {
129  if (!_twr_at_lora_param_format_and_test(param, 32))
130  {
131  return false;
132  }
133 
134  twr_cmwx1zzabz_set_appskey(_at.lora, param->txt);
135 
136  return true;
137 }
138 
139 bool twr_at_lora_band_read(void)
140 {
142 
143  twr_atci_printfln("$BAND: %d", band);
144 
145  return true;
146 }
147 
148 bool twr_at_lora_band_set(twr_atci_param_t *param)
149 {
150  uint8_t band = atoi(param->txt);
151 
152  if (band > 8)
153  {
154  return false;
155  }
156 
157  twr_cmwx1zzabz_set_band(_at.lora, band);
158 
159  return true;
160 }
161 
162 bool twr_at_lora_mode_read(void)
163 {
165 
166  twr_atci_printfln("$MODE: %d", mode);
167 
168  return true;
169 }
170 
171 bool twr_at_lora_mode_set(twr_atci_param_t *param)
172 {
173  uint8_t mode = atoi(param->txt);
174 
175  if (mode > 1)
176  {
177  return false;
178  }
179 
180  twr_cmwx1zzabz_set_mode(_at.lora, mode);
181 
182  return true;
183 }
184 
185 bool twr_at_lora_join(void)
186 {
187  twr_cmwx1zzabz_join(_at.lora);
188 
189  return true;
190 }
191 
192 bool twr_at_lora_frmcnt(void)
193 {
195 
196  return true;
197 }
198 
199 bool twr_at_lora_reboot(void)
200 {
201  twr_system_reset();
202 
203  return true;
204 }
205 
206 bool twr_at_lora_freset(void)
207 {
209 
210  return true;
211 }
212 
213 bool twr_at_lora_link_check(void)
214 {
215  twr_cmwx1zzabz_link_check(_at.lora);
216 
217  return true;
218 }
219 
220 bool twr_at_lora_custom_at_set(twr_atci_param_t *param)
221 {
222  // Skip 6 characters (AT$AT=)
223  twr_cmwx1zzabz_custom_at(_at.lora, param->txt);
224 
225  return true;
226 }
227 
228 bool twr_at_lora_rfq(void)
229 {
230  twr_cmwx1zzabz_rfq(_at.lora);
231 
232  return true;
233 }
234 
235 bool twr_at_lora_nwk_read(void)
236 {
237  uint8_t nwk_public = twr_cmwx1zzabz_get_nwk_public(_at.lora);
238 
239  twr_atci_printfln("$NWK: %d", nwk_public);
240 
241  return true;
242 }
243 
244 bool twr_at_lora_nwk_set(twr_atci_param_t *param)
245 {
246  uint8_t nwk_public = atoi(param->txt);
247 
248  if (nwk_public > 1)
249  {
250  return false;
251  }
252 
253  twr_cmwx1zzabz_set_nwk_public(_at.lora, nwk_public);
254 
255  return true;
256 }
257 
258 bool twr_at_lora_adr_read(void)
259 {
260  uint8_t adr = twr_cmwx1zzabz_get_adaptive_datarate(_at.lora);
261 
262  twr_atci_printfln("$ADR: %d", adr);
263 
264  return true;
265 }
266 
267 bool twr_at_lora_adr_set(twr_atci_param_t *param)
268 {
269  uint8_t adr = atoi(param->txt);
270 
271  if (adr > 1)
272  {
273  return false;
274  }
275 
277 
278  return true;
279 }
280 
281 bool twr_at_lora_dr_read(void)
282 {
283  uint8_t dr = twr_cmwx1zzabz_get_datarate(_at.lora);
284 
285  twr_atci_printfln("$DR: %d", dr);
286 
287  return true;
288 }
289 
290 bool twr_at_lora_dr_set(twr_atci_param_t *param)
291 {
292  uint8_t dr = atoi(param->txt);
293 
294  if (dr > 15)
295  {
296  return false;
297  }
298 
299  twr_cmwx1zzabz_set_datarate(_at.lora, dr);
300 
301  return true;
302 }
303 
304 bool twr_at_lora_repu_read(void)
305 {
306  uint8_t repeat = twr_cmwx1zzabz_get_repeat_unconfirmed(_at.lora);
307 
308  twr_atci_printfln("$REPU: %d", repeat);
309 
310  return true;
311 }
312 
313 bool twr_at_lora_repu_set(twr_atci_param_t *param)
314 {
315  uint8_t repeat = atoi(param->txt);
316 
317  if (repeat < 1 || repeat > 15)
318  {
319  return false;
320  }
321 
322  twr_cmwx1zzabz_set_repeat_unconfirmed(_at.lora, repeat);
323 
324  return true;
325 }
326 
327 bool twr_at_lora_repc_read(void)
328 {
329  uint8_t repeat = twr_cmwx1zzabz_get_repeat_confirmed(_at.lora);
330 
331  twr_atci_printfln("$REPC: %d", repeat);
332 
333  return true;
334 }
335 
336 bool twr_at_lora_repc_set(twr_atci_param_t *param)
337 {
338  uint8_t repeat = atoi(param->txt);
339 
340  if (repeat < 1 || repeat > 8)
341  {
342  return false;
343  }
344 
345  twr_cmwx1zzabz_set_repeat_confirmed(_at.lora, repeat);
346 
347  return true;
348 }
349 
350 bool twr_at_lora_ver_read(void)
351 {
352  const char *version = twr_cmwx1zzabz_get_fw_version(_at.lora);
353 
354  twr_atci_printfln("$VER: %s", version);
355 
356  return true;
357 }
358 
359 bool twr_at_lora_debug_set(twr_atci_param_t *param)
360 {
361  uint8_t debug = atoi(param->txt);
362 
363  twr_cmwx1zzabz_set_debug(_at.lora, (debug == 1) ? true : false);
364 
365  return true;
366 }
367 
368 static bool _twr_at_lora_param_format_and_test(twr_atci_param_t *param, uint8_t length)
369 {
370 
371  // Capitalize letters
372  for (uint32_t i = 0; param->txt[i] != '\0'; i++) {
373  if (param->txt[i] >= 'a' && param->txt[i] <= 'z') {
374  param->txt[i] = param->txt[i] - 32;
375  }
376  }
377 
378  // Skip spaces
379  for (uint32_t i = 0; i < strlen(param->txt); i++)
380  {
381  while (param->txt[i] == ' ')
382  {
383  for (uint32_t q = 0; q < strlen(param->txt); q++)
384  {
385  param->txt[i + q] = param->txt[i + q + 1];
386  }
387  }
388  }
389 
390  // Correct new string length
391  param->length = strlen(param->txt);
392 
393  if (param->length != length)
394  {
395  return false;
396  }
397 
398  // Check the string is HEX
399  for (size_t i = 0; i < strlen(param->txt); i++)
400  {
401  if ((param->txt[i] >= '0' && param->txt[i] <= '9') || (param->txt[i] >= 'A' && param->txt[i] <= 'F'))
402  {
403  continue;
404  }
405 
406  return false;
407  }
408 
409  return true;
410 }
size_t twr_atci_printfln(const char *format,...)
Prinf message and add CR LF.
Definition: twr_atci.c:84
void twr_cmwx1zzabz_set_adaptive_datarate(twr_cmwx1zzabz_t *self, bool enable)
Set the configuration adaptive data rate.
bool twr_cmwx1zzabz_link_check(twr_cmwx1zzabz_t *self)
Request send of link check packet.
void twr_cmwx1zzabz_set_appeui(twr_cmwx1zzabz_t *self, char *appeui)
Set APPEUI.
void twr_cmwx1zzabz_get_deveui(twr_cmwx1zzabz_t *self, char *deveui)
Get DEVEUI.
uint8_t twr_cmwx1zzabz_get_nwk_public(twr_cmwx1zzabz_t *self)
Get the configuration if public networks are enabled.
void twr_cmwx1zzabz_set_nwkskey(twr_cmwx1zzabz_t *self, char *nwkskey)
Set NWKSKEY.
void twr_cmwx1zzabz_get_appkey(twr_cmwx1zzabz_t *self, char *appkey)
Get APPKEY.
uint8_t twr_cmwx1zzabz_get_datarate(twr_cmwx1zzabz_t *self)
Get the configuration of datarate.
void twr_cmwx1zzabz_set_debug(twr_cmwx1zzabz_t *self, bool debug)
Set debugging flag which prints modem communication to twr_log.
void twr_cmwx1zzabz_get_nwkskey(twr_cmwx1zzabz_t *self, char *nwkskey)
Set NWKSKEY.
void twr_cmwx1zzabz_set_band(twr_cmwx1zzabz_t *self, twr_cmwx1zzabz_config_band_t band)
Set BAND.
void twr_cmwx1zzabz_set_repeat_unconfirmed(twr_cmwx1zzabz_t *self, uint8_t repeat)
Set number of transmissions of unconfirmed message.
bool twr_cmwx1zzabz_frame_counter(twr_cmwx1zzabz_t *self)
Request frame counter value.
twr_cmwx1zzabz_config_mode_t
LoRa mode ABP/OTAA.
void twr_cmwx1zzabz_set_devaddr(twr_cmwx1zzabz_t *self, char *devaddr)
Set DEVADDR.
void twr_cmwx1zzabz_join(twr_cmwx1zzabz_t *self)
Start LoRa OTAA join procedure.
char * twr_cmwx1zzabz_get_fw_version(twr_cmwx1zzabz_t *self)
Get firmware version string.
twr_cmwx1zzabz_config_band_t twr_cmwx1zzabz_get_band(twr_cmwx1zzabz_t *self)
Get BAND.
void twr_cmwx1zzabz_get_devaddr(twr_cmwx1zzabz_t *self, char *devaddr)
Get DEVADDR.
bool twr_cmwx1zzabz_rfq(twr_cmwx1zzabz_t *self)
Request RF quality of the last received packet (JOIN, LNPARAM, confirmed message)
void twr_cmwx1zzabz_get_appskey(twr_cmwx1zzabz_t *self, char *appskey)
Get APPSKEY.
void twr_cmwx1zzabz_set_nwk_public(twr_cmwx1zzabz_t *self, uint8_t public)
Set the configuration enabling public networks.
void twr_cmwx1zzabz_set_deveui(twr_cmwx1zzabz_t *self, char *deveui)
Set DEVEUI.
twr_cmwx1zzabz_config_band_t
Frequency modes and standards.
uint8_t twr_cmwx1zzabz_get_repeat_confirmed(twr_cmwx1zzabz_t *self)
Get number of transmissions of confirmed message.
bool twr_cmwx1zzabz_custom_at(twr_cmwx1zzabz_t *self, char *at_command)
Send custom AT command to LoRa Module.
uint8_t twr_cmwx1zzabz_get_repeat_unconfirmed(twr_cmwx1zzabz_t *self)
Get number of transmissions of unconfirmed message.
void twr_cmwx1zzabz_set_datarate(twr_cmwx1zzabz_t *self, uint8_t datarate)
Set the configuration of datarate.
void twr_cmwx1zzabz_set_appkey(twr_cmwx1zzabz_t *self, char *appkey)
Set APPKEY.
void twr_cmwx1zzabz_set_appskey(twr_cmwx1zzabz_t *self, char *appskey)
Set APPSKEY.
twr_cmwx1zzabz_config_mode_t twr_cmwx1zzabz_get_mode(twr_cmwx1zzabz_t *self)
Get ABP/OTAA mode.
void twr_cmwx1zzabz_get_appeui(twr_cmwx1zzabz_t *self, char *appeui)
Get APPEUI.
bool twr_cmwx1zzabz_get_adaptive_datarate(twr_cmwx1zzabz_t *self)
Get the configuration if adaptive data rate are enabled.
bool twr_cmwx1zzabz_factory_reset(twr_cmwx1zzabz_t *self)
Send factory reset command to LoRa Module.
struct twr_cmwx1zzabz_t twr_cmwx1zzabz_t
CMWX1ZZABZ instance.
void twr_cmwx1zzabz_set_mode(twr_cmwx1zzabz_t *self, twr_cmwx1zzabz_config_mode_t mode)
Set ABP/OTAA mode.
void twr_cmwx1zzabz_set_repeat_confirmed(twr_cmwx1zzabz_t *self, uint8_t repeat)
Set number of transmissions of confirmed message.