-
Notifications
You must be signed in to change notification settings - Fork 5
/
custom_at.cpp
278 lines (258 loc) · 8.74 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
/**
* @file custom_at.cpp
* @author Bernd Giesecke ([email protected])
* @brief
* @version 0.1
* @date 2022-05-12
*
* @copyright Copyright (c) 2022
*
*/
#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 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);
/**
* @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, custom_parameters.send_interval / 1000);
}
else if (param->argc == 1)
{
uint32_t old_send_freq = custom_parameters.send_interval;
// 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 s", new_send_freq);
custom_parameters.send_interval = new_send_freq * 1000;
MYLOG("AT_CMD", "New interval %ld ms", custom_parameters.send_interval);
// Stop the timer
api.system.timer.stop(RAK_TIMER_0);
if (custom_parameters.send_interval != 0)
{
// Restart the timer
api.system.timer.start(RAK_TIMER_0, custom_parameters.send_interval, NULL);
}
// Save custom settings if needed
if (old_send_freq != custom_parameters.send_interval)
{
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", "LA915"};
/** 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", 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)
{
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, read 0X%08X", temp_params.send_interval);
custom_parameters.valid_flag = 0xAA;
custom_parameters.send_interval = 0;
save_at_setting();
return false;
}
custom_parameters.send_interval = temp_params.send_interval;
MYLOG("AT_CMD", "Send interval found %ld", custom_parameters.send_interval);
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 *)&custom_parameters.valid_flag;
bool wr_result = false;
MYLOG("AT_CMD", "Writing flag: %02X", custom_parameters.valid_flag);
MYLOG("AT_CMD", "Writing send interval 0X%08X ", custom_parameters.send_interval);
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;
}