forked from espressif/arduino-esp32
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOThreadCLI_Util.cpp
212 lines (201 loc) · 6.08 KB
/
OThreadCLI_Util.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
// Copyright 2024 Espressif Systems (Shanghai) PTE LTD
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "OThreadCLI.h"
#if SOC_IEEE802154_SUPPORTED
#if CONFIG_OPENTHREAD_ENABLED
#include "OThreadCLI_Util.h"
#include <StreamString.h>
static const char *otRoleString[] = {
"Disabled", ///< The Thread stack is disabled.
"Detached", ///< Not currently participating in a Thread network/partition.
"Child", ///< The Thread Child role.
"Router", ///< The Thread Router role.
"Leader", ///< The Thread Leader role.
};
ot_device_role_t otGetDeviceRole() {
if (!OThreadCLI) {
return OT_ROLE_DISABLED;
}
otInstance *instance = esp_openthread_get_instance();
return (ot_device_role_t)otThreadGetDeviceRole(instance);
}
const char *otGetStringDeviceRole() {
return otRoleString[otGetDeviceRole()];
}
bool otGetRespCmd(const char *cmd, char *resp, uint32_t respTimeout) {
if (!OThreadCLI) {
return false;
}
StreamString cliRespAllLines;
char cliResp[256] = {0};
if (resp != NULL) {
*resp = '\0';
}
if (cmd == NULL) {
return true;
}
OThreadCLI.println(cmd);
log_d("CMD[%s]", cmd);
uint32_t timeout = millis() + respTimeout;
while (millis() < timeout) {
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
// clip it on EOL
for (int i = 0; i < len; i++) {
if (cliResp[i] == '\r' || cliResp[i] == '\n') {
cliResp[i] = '\0';
}
}
log_d("Resp[%s]", cliResp);
if (strncmp(cliResp, "Done", 4) && strncmp(cliResp, "Error", 4)) {
cliRespAllLines += cliResp;
cliRespAllLines.println(); // Adds whatever EOL is for the OS
} else {
break;
}
}
if (!strncmp(cliResp, "Error", 4) || millis() > timeout) {
return false;
}
if (resp != NULL) {
strcpy(resp, cliRespAllLines.c_str());
}
return true;
}
bool otExecCommand(const char *cmd, const char *arg, ot_cmd_return_t *returnCode) {
if (!OThreadCLI) {
return false;
}
char cliResp[256] = {0};
if (cmd == NULL) {
return true;
}
if (arg == NULL) {
OThreadCLI.println(cmd);
} else {
OThreadCLI.print(cmd);
OThreadCLI.print(" ");
OThreadCLI.println(arg);
}
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
// clip it on EOL
for (int i = 0; i < len; i++) {
if (cliResp[i] == '\r' || cliResp[i] == '\n') {
cliResp[i] = '\0';
}
}
log_d("CMD[%s %s] Resp[%s]", cmd, arg, cliResp);
// initial returnCode is success values
if (returnCode) {
returnCode->errorCode = 0;
returnCode->errorMessage = "Done";
}
if (!strncmp(cliResp, "Done", 4)) {
return true;
} else {
if (returnCode) {
// initial setting is a bad error message or it is something else...
// return -1 and the full returned message
returnCode->errorCode = -1;
returnCode->errorMessage = cliResp;
// parse cliResp looking for errorCode and errorMessage
// OT CLI error message format is "Error ##: msg\n" - Example:
//Error 35: InvalidCommand
//Error 7: InvalidArgs
char *i = cliResp;
char *m = cliResp;
while (*i && *i != ':') {
i++;
}
if (*i) {
*i = '\0';
m = i + 2; // message is 2 characters after ':'
while (i > cliResp && *i != ' ') {
i--; // search for ' ' before ":'
}
if (*i == ' ') {
i++; // move it forward to the number beginning
returnCode->errorCode = atoi(i);
returnCode->errorMessage = m;
} // otherwise, it will keep the "bad error message" information
} // otherwise, it will keep the "bad error message" information
} // returnCode is NULL pointer
return false;
}
}
bool otPrintRespCLI(const char *cmd, Stream &output, uint32_t respTimeout) {
char cliResp[256] = {0};
if (cmd == NULL) {
return true;
}
OThreadCLI.println(cmd);
uint32_t timeout = millis() + respTimeout;
while (millis() < timeout) {
size_t len = OThreadCLI.readBytesUntil('\n', cliResp, sizeof(cliResp));
if (cliResp[0] == '\0') {
// Straem has timed out and it should try again using parameter respTimeout
continue;
}
// clip it on EOL
for (int i = 0; i < len; i++) {
if (cliResp[i] == '\r' || cliResp[i] == '\n') {
cliResp[i] = '\0';
}
}
if (strncmp(cliResp, "Done", 4) && strncmp(cliResp, "Error", 4)) {
output.println(cliResp);
memset(cliResp, 0, sizeof(cliResp));
timeout = millis() + respTimeout; // renew timeout, line per line
} else {
break;
}
}
if (!strncmp(cliResp, "Error", 4) || millis() > timeout) {
return false;
}
return true;
}
void otPrintNetworkInformation(Stream &output) {
if (!OThreadCLI) {
return;
}
char resp[512];
output.println("Thread Setup:");
if (otGetRespCmd("state", resp)) {
output.printf("Node State: \t%s", resp);
}
if (otGetRespCmd("networkname", resp)) {
output.printf("Network Name: \t%s", resp);
}
if (otGetRespCmd("channel", resp)) {
output.printf("Channel: \t%s", resp);
}
if (otGetRespCmd("panid", resp)) {
output.printf("Pan ID: \t%s", resp);
}
if (otGetRespCmd("extpanid", resp)) {
output.printf("Ext Pan ID: \t%s", resp);
}
if (otGetRespCmd("networkkey", resp)) {
output.printf("Network Key: \t%s", resp);
}
if (otGetRespCmd("ipaddr", resp)) {
output.println("Node IP Addresses are:");
output.printf("%s", resp);
}
if (otGetRespCmd("ipmaddr", resp)) {
output.println("Node Multicast Addresses are:");
output.printf("%s", resp);
}
}
#endif /* CONFIG_OPENTHREAD_ENABLED */
#endif /* SOC_IEEE802154_SUPPORTED */