-
Notifications
You must be signed in to change notification settings - Fork 5
/
custom_at.cpp
373 lines (344 loc) · 11.3 KB
/
custom_at.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
/**
* @file custom_at.cpp
* @author Bernd Giesecke ([email protected])
* @brief Custom AT commands for the application
* @version 0.1
* @date 2023-12-29
*
* @copyright Copyright (c) 2023
*
*/
#include "app.h"
#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
/** Custom flash parameters */
custom_param_s g_custom_parameters;
// Forward declarations
int interval_send_handler(SERIAL_PORT port, char *cmd, stParam *param);
int status_handler(SERIAL_PORT port, char *cmd, stParam *param);
int test_mode_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;
}
bool init_test_mode_at(void)
{
return api.system.atMode.add((char *)"MODE",
(char *)"Set/Get the test mode. 0 = LPWAN LinkCheck, 1 = LPWAN CFM, 2 = LoRa P2P",
(char *)"MODE", test_mode_handler,
RAK_ATCMD_PERM_WRITE | RAK_ATCMD_PERM_READ);
}
int test_mode_handler(SERIAL_PORT port, char *cmd, stParam *param)
{
if (param->argc == 1 && !strcmp(param->argv[0], "?"))
{
AT_PRINTF("%s=%d", cmd, g_custom_parameters.test_mode);
}
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_mode = strtoul(param->argv[0], NULL, 10);
uint8_t old_mode = g_custom_parameters.test_mode;
MYLOG("AT_CMD", "Requested mode %ld", new_mode);
if (new_mode > 2)
{
return AT_PARAM_ERROR;
}
if (new_mode != old_mode)
{
bool restart = true;
if (((old_mode == 0) && (new_mode == 1)) || ((old_mode == 1) && (new_mode == 0)))
{
MYLOG("AT_CMD", "Switch within LPWAN modes");
restart = false;
}
g_custom_parameters.test_mode = new_mode;
MYLOG("AT_CMD", "New test mode %ld", g_custom_parameters.test_mode);
// Save custom settings
save_at_setting();
// Switch mode
switch (g_custom_parameters.test_mode)
{
case MODE_LINKCHECK:
set_linkcheck();
break;
case MODE_CFM:
set_cfm();
break;
case MODE_P2P:
set_p2p();
break;
}
// If switching between LoRaWAN and LoRa P2P the device needs to restart
if (restart)
{
AT_PRINTF("+EVT:RESTART_FOR_MODE_CHANGE");
delay(5000);
api.system.reboot();
}
}
}
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", "LA915"};
/** Network modes as text array*/
char *nwm_list[] = {"P2P", "LoRaWAN", "FSK"};
/** Available test modes as text array */
char *test_mode_list[] = {"LinkCheck", "Confirmed Packet", "LoRa P2P"};
/**
* @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:");
AT_PRINTF("Test Mode: %s", test_mode_list[g_custom_parameters.test_mode]);
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 Get setting from flash
*
* @return false read from flash failed or invalid settings type
*/
bool get_at_setting(void)
{
bool found_problem = false;
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 settings found, set to default, read 0X%08X", temp_params.send_interval);
g_custom_parameters.send_interval = 0;
g_custom_parameters.test_mode = 0;
save_at_setting();
return false;
}
g_custom_parameters.send_interval = temp_params.send_interval;
if (temp_params.test_mode > 2)
{
MYLOG("AT_CMD", "Invalid test mode found %d", temp_params.test_mode);
g_custom_parameters.test_mode = 0;
save_at_setting();
}
else
{
g_custom_parameters.test_mode = temp_params.test_mode;
}
MYLOG("AT_CMD", "Send interval found %ld", g_custom_parameters.send_interval);
MYLOG("AT_CMD", "Test mode found %d", g_custom_parameters.test_mode);
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)
{
custom_param_s temp_params;
uint8_t *flash_value = (uint8_t *)&temp_params.valid_flag;
temp_params.send_interval = g_custom_parameters.send_interval;
temp_params.test_mode = g_custom_parameters.test_mode;
bool wr_result = false;
MYLOG("AT_CMD", "Writing flag: %02X", temp_params.valid_flag);
MYLOG("AT_CMD", "Writing send interval 0X%08X ", temp_params.send_interval);
MYLOG("AT_CMD", "Writing test mode %d ", temp_params.test_mode);
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));
}
return wr_result;
}