-
Notifications
You must be signed in to change notification settings - Fork 3
/
user_at_cmd.cpp
422 lines (390 loc) · 12.9 KB
/
user_at_cmd.cpp
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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
/**
* @file custom_at.cpp
* @author Bernd Giesecke (bernd@giesecke.tk)
* @brief
* @version 0.1
* @date 2022-05-12
*
* @copyright Copyright (c) 2022
*
*/
#include "main.h"
custom_param_s g_custom_parameters;
#if defined(_VARIANT_RAK3172_) || defined(_VARIANT_RAK3172_SIP_)
#define AT_PRINTF(...) \
do \
{ \
Serial.printf(__VA_ARGS__); \
Serial.printf("\r\n"); \
} while (0); \
delay(100)
#else // RAK4630 || RAK11720
#define AT_PRINTF(...) \
do \
{ \
Serial.printf(__VA_ARGS__); \
Serial.printf("\r\n"); \
Serial6.printf(__VA_ARGS__); \
Serial6.printf("\r\n"); \
} while (0); \
delay(100)
#endif
// Forward declarations
int interval_send_handler(SERIAL_PORT port, char *cmd, stParam *param);
int status_handler(SERIAL_PORT port, char *cmd, stParam *param);
/**
* @brief Add send interval AT command
*
* @return true if success
* @return false if failed
*/
bool init_interval_at(void)
{
return api.system.atMode.add((char *)"SENDINT",
(char *)"Set/Get the interval sending time values in seconds 0 = off, max 2,147,483 seconds",
(char *)"SENDINT", interval_send_handler,
RAK_ATCMD_PERM_WRITE | RAK_ATCMD_PERM_READ);
}
/**
* @brief Handler for send interval AT commands
*
* @param port Serial port used
* @param cmd char array with the received AT command
* @param param char array with the received AT command parameters
* @return int result of command parsing
* AT_OK AT command & parameters valid
* AT_PARAM_ERROR command or parameters invalid
*/
int interval_send_handler(SERIAL_PORT port, char *cmd, stParam *param)
{
if (param->argc == 1 && !strcmp(param->argv[0], "?"))
{
AT_PRINTF("%s=%ld", cmd, g_custom_parameters.send_interval / 1000);
}
else if (param->argc == 1)
{
MYLOG("AT_CMD", "param->argv[0] >> %s", param->argv[0]);
for (int i = 0; i < strlen(param->argv[0]); i++)
{
if (!isdigit(*(param->argv[0] + i)))
{
MYLOG("AT_CMD", "%d is no digit", i);
return AT_PARAM_ERROR;
}
}
uint32_t new_send_freq = strtoul(param->argv[0], NULL, 10);
MYLOG("AT_CMD", "Requested interval %ld", new_send_freq);
g_custom_parameters.send_interval = new_send_freq * 1000;
MYLOG("AT_CMD", "New interval %ld", g_custom_parameters.send_interval);
// Stop the timer
api.system.timer.stop(RAK_TIMER_0);
if (g_custom_parameters.send_interval != 0)
{
// Restart the timer
api.system.timer.start(RAK_TIMER_0, g_custom_parameters.send_interval, NULL);
}
// Save custom settings
save_at_setting();
}
else
{
return AT_PARAM_ERROR;
}
return AT_OK;
}
/**
* @brief Add custom Status AT commands
*
* @return true AT commands were added
* @return false AT commands couldn't be added
*/
bool init_status_at(void)
{
return api.system.atMode.add((char *)"STATUS",
(char *)"Get device information",
(char *)"STATUS", status_handler,
RAK_ATCMD_PERM_READ);
}
/** Regions as text array */
char *regions_list[] = {"EU433", "CN470", "RU864", "IN865", "EU868", "US915", "AU915", "KR920", "AS923", "AS923-2", "AS923-3", "AS923-4"};
/** Network modes as text array*/
char *nwm_list[] = {"P2P", "LoRaWAN", "FSK"};
/**
* @brief Print device status over Serial
*
* @param port Serial port used
* @param cmd char array with the received AT command
* @param param char array with the received AT command parameters
* @return int result of command parsing
* AT_OK AT command & parameters valid
* AT_PARAM_ERROR command or parameters invalid
*/
int status_handler(SERIAL_PORT port, char *cmd, stParam *param)
{
String value_str = "";
int nw_mode = 0;
int region_set = 0;
uint8_t key_eui[16] = {0}; // efadff29c77b4829acf71e1a6e76f713
if ((param->argc == 1 && !strcmp(param->argv[0], "?")) || (param->argc == 0))
{
AT_PRINTF("Device Status:");
value_str = api.system.hwModel.get();
value_str.toUpperCase();
AT_PRINTF("Module: %s", value_str.c_str());
AT_PRINTF("Version: %s", api.system.firmwareVer.get().c_str());
AT_PRINTF("Send time: %d s", g_custom_parameters.send_interval / 1000);
/// \todo
nw_mode = api.lorawan.nwm.get();
AT_PRINTF("Network mode %s", nwm_list[nw_mode]);
if (nw_mode == 1)
{
AT_PRINTF("Network %s", api.lorawan.njs.get() ? "joined" : "not joined");
region_set = api.lorawan.band.get();
AT_PRINTF("Region: %d", region_set);
AT_PRINTF("Region: %s", regions_list[region_set]);
if (api.lorawan.njm.get())
{
AT_PRINTF("OTAA mode");
api.lorawan.deui.get(key_eui, 8);
AT_PRINTF("DevEUI=%02X%02X%02X%02X%02X%02X%02X%02X",
key_eui[0], key_eui[1], key_eui[2], key_eui[3],
key_eui[4], key_eui[5], key_eui[6], key_eui[7]);
api.lorawan.appeui.get(key_eui, 8);
AT_PRINTF("AppEUI=%02X%02X%02X%02X%02X%02X%02X%02X",
key_eui[0], key_eui[1], key_eui[2], key_eui[3],
key_eui[4], key_eui[5], key_eui[6], key_eui[7]);
api.lorawan.appkey.get(key_eui, 16);
AT_PRINTF("AppKey=%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
key_eui[0], key_eui[1], key_eui[2], key_eui[3],
key_eui[4], key_eui[5], key_eui[6], key_eui[7],
key_eui[8], key_eui[9], key_eui[10], key_eui[11],
key_eui[12], key_eui[13], key_eui[14], key_eui[15]);
}
else
{
AT_PRINTF("ABP mode");
api.lorawan.appskey.get(key_eui, 16);
AT_PRINTF("AppsKey=%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
key_eui[0], key_eui[1], key_eui[2], key_eui[3],
key_eui[4], key_eui[5], key_eui[6], key_eui[7],
key_eui[8], key_eui[9], key_eui[10], key_eui[11],
key_eui[12], key_eui[13], key_eui[14], key_eui[15]);
api.lorawan.nwkskey.get(key_eui, 16);
AT_PRINTF("NwsKey=%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
key_eui[0], key_eui[1], key_eui[2], key_eui[3],
key_eui[4], key_eui[5], key_eui[6], key_eui[7],
key_eui[8], key_eui[9], key_eui[10], key_eui[11],
key_eui[12], key_eui[13], key_eui[14], key_eui[15]);
api.lorawan.daddr.get(key_eui, 4);
AT_PRINTF("DevAddr=%02X%02X%02X%02X",
key_eui[0], key_eui[1], key_eui[2], key_eui[3]);
}
}
else if (nw_mode == 0)
{
AT_PRINTF("Frequency = %d", api.lora.pfreq.get());
AT_PRINTF("SF = %d", api.lora.psf.get());
AT_PRINTF("BW = %d", api.lora.pbw.get());
AT_PRINTF("CR = %d", api.lora.pcr.get());
AT_PRINTF("Preamble length = %d", api.lora.ppl.get());
AT_PRINTF("TX power = %d", api.lora.ptp.get());
}
else
{
AT_PRINTF("Frequency = %d", api.lora.pfreq.get());
AT_PRINTF("Bitrate = %d", api.lora.pbr.get());
AT_PRINTF("Deviaton = %d", api.lora.pfdev.get());
}
}
else
{
return AT_PARAM_ERROR;
}
return AT_OK;
}
/**
* @brief Add custom Map Mesh AT command
*
* @return true AT commands were added
* @return false AT commands couldn't be added
*/
bool init_map_at(void)
{
return api.system.atMode.add((char *)"MESH",
(char *)"Get the Mesh map",
(char *)"MESH", mesh_map_handler,
RAK_ATCMD_PERM_READ);
}
/**
* @brief Return the current Mesh map
*
* @param port Serial port used
* @param cmd char array with the received AT command
* @param param char array with the received AT command parameters
* @return int result of command parsing
* AT_OK AT command & parameters valid
* AT_PARAM_ERROR command or parameters invalid
* @return int
*/
int mesh_map_handler(SERIAL_PORT port, char *cmd, stParam *param)
{
String value_str = "";
int nw_mode = 0;
int region_set = 0;
uint8_t key_eui[16] = {0}; // efadff29c77b4829acf71e1a6e76f713
if (param->argc == 0)
{
/** Node ID of the selected receiver node */
uint32_t node_id[48];
/** First hop ID of the selected receiver node */
uint32_t first_hop[48];
/** Number of hops to the selected receiver node */
uint8_t num_hops[48];
/** Number of nodes in the map */
uint8_t num_elements;
AT_PRINTF("\n---------------------------------------------");
/** Number of nodes in the map */
num_elements = nodes_in_map();
for (int idx = 0; idx < num_elements; idx++)
{
get_node(idx, node_id[idx], first_hop[idx], num_hops[idx]);
}
// Display the nodes
AT_PRINTF("%d nodes in the map", num_elements + 1);
AT_PRINTF("Node #01 id: %08lX this node", g_this_device_addr);
for (int idx = 0; idx < num_elements; idx++)
{
if (first_hop[idx] == 0)
{
AT_PRINTF("Node #%02d id: %08lX direct", idx + 2, node_id[idx]);
}
else
{
AT_PRINTF("Node #%02d id: %08lX first hop %08lX #hops %d", idx + 2, node_id[idx], first_hop[idx], num_hops[idx]);
}
}
AT_PRINTF("---------------------------------------------");
return AT_OK;
}
return AT_PARAM_ERROR;
}
/**
* @brief Add custom set master node AT commands
*
* @return true AT commands were added
* @return false AT commands couldn't be added
*/
bool init_master_at(void)
{
return api.system.atMode.add((char *)"MASTER",
(char *)"Set/get Master Node address",
(char *)"MASTER", mesh_master_handler,
RAK_ATCMD_PERM_READ | RAK_ATCMD_PERM_WRITE);
}
/**
* @brief Return the current Mesh map
*
* @param port Serial port used
* @param cmd char array with the received AT command
* @param param char array with the received AT command parameters
* @return int result of command parsing
* AT_OK AT command & parameters valid
* AT_PARAM_ERROR command or parameters invalid
* @return int
*/
int mesh_master_handler(SERIAL_PORT port, char *cmd, stParam *param)
{
if (param->argc == 1 && !strcmp(param->argv[0], "?"))
{
AT_PRINTF("%s=0X%08X", cmd, g_custom_parameters.master_address);
}
else if (param->argc == 1)
{
MYLOG("AT_CMD", "param->argv[0] >> %s", param->argv[0]);
// for (int i = 0; i < strlen(param->argv[0]); i++)
// {
// if (!isdigit(*(param->argv[0] + i)))
// {
// MYLOG("AT_CMD", "%d is no digit", i);
// return AT_PARAM_ERROR;
// }
// }
uint32_t new_master_addr = strtoul(param->argv[0], NULL, 16);
MYLOG("AT_CMD", "Got master node address 0X%08X", new_master_addr);
if (new_master_addr == g_this_device_addr)
{
return AT_PARAM_ERROR;
}
g_custom_parameters.master_address = new_master_addr;
MYLOG("AT_CMD", "New master node address 0X%08X", g_custom_parameters.master_address);
// Save custom settings
save_at_setting();
}
else
{
return AT_PARAM_ERROR;
}
return AT_OK;
}
/**
* @brief Get setting from flash
*
* @return false read from flash failed or invalid settings type
*/
bool get_at_setting(void)
{
custom_param_s temp_params;
uint8_t *flash_value = (uint8_t *)&temp_params.valid_flag;
if (!api.system.flash.get(0, flash_value, sizeof(custom_param_s)))
{
MYLOG("AT_CMD", "Failed to read send interval from Flash");
return false;
}
// MYLOG("AT_CMD", "Got flag: %02X", temp_params.valid_flag);
// MYLOG("AT_CMD", "Got send interval: %08X", temp_params.send_interval);
if (flash_value[0] != 0xAA)
{
MYLOG("AT_CMD", "No valid send interval found, set to default");
g_custom_parameters.send_interval = 0;
save_at_setting();
return false;
}
// MYLOG("AT_CMD", "Read send interval 0X%08X", temp_params.send_interval);
g_custom_parameters.send_interval = temp_params.send_interval;
/// \todo find a option to set the master address initialy to 0
/// \todo here we just filter for a RAK assigned DevEUI
if ((temp_params.master_address & 0xFE000000) != 0xFE000000)
{
g_custom_parameters.master_address = 0x00;
MYLOG("AT_CMD", "Master node invalid %08X %08X", temp_params.master_address, (temp_params.master_address && 0xFE000000));
}
else
{
g_custom_parameters.master_address = temp_params.master_address;
}
MYLOG("AT_CMD", "Send interval found %ld", g_custom_parameters.send_interval);
MYLOG("AT_CMD", "Master node found 0X%08X", g_custom_parameters.master_address);
return true;
}
/**
* @brief Save setting to flash
*
* @return true write to flash was successful
* @return false write to flash failed or invalid settings type
*/
bool save_at_setting(void)
{
uint8_t *flash_value = (uint8_t *)&g_custom_parameters.valid_flag;
bool wr_result = false;
MYLOG("AT_CMD", "Writing flag: %02X", g_custom_parameters.valid_flag);
MYLOG("AT_CMD", "Writing send interval 0X%08X ", g_custom_parameters.send_interval);
MYLOG("AT_CMD", "Writing master node address 0X%08X ", g_custom_parameters.master_address);
wr_result = api.system.flash.set(0, flash_value, sizeof(custom_param_s));
if (!wr_result)
{
// Retry
wr_result = api.system.flash.set(0, flash_value, sizeof(custom_param_s));
}
wr_result = true;
return wr_result;
}