Skip to content

Commit 6e78862

Browse files
[diag] Move otPlatDiagModeGet() check to ProcessCmd
All diagnostic commands (instead of `diag start`) should fail if device is not in diagnostic mode. Previously it was verified by each command's process method (with a missing check in `ProcessEcho` and `ProcessGpio`). This commit moves the check directly to `ProcessCmd` and cleans up redundant code. Additionally clean the documentation and align the code as for some commands `status 0x00` was added on success and for some not. Signed-off-by: Maciej Baczmanski <maciej.baczmanski@nordicsemi.no>
1 parent 971f05f commit 6e78862

File tree

2 files changed

+32
-43
lines changed

2 files changed

+32
-43
lines changed

src/core/diags/README.md

+14-9
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ Start diagnostics mode.
3838
```bash
3939
> diag start
4040
start diagnostics mode
41-
status 0x00
41+
Done
4242
```
4343

4444
### diag channel
@@ -48,6 +48,7 @@ Get the IEEE 802.15.4 Channel value for diagnostics module.
4848
```bash
4949
> diag channel
5050
channel: 11
51+
Done
5152
```
5253

5354
### diag channel \<channel\>
@@ -57,7 +58,7 @@ Set the IEEE 802.15.4 Channel value for diagnostics module.
5758
```bash
5859
> diag channel 11
5960
set channel to 11
60-
status 0x00
61+
Done
6162
```
6263

6364
### diag cw start
@@ -123,6 +124,7 @@ Get the tx power value(dBm) for diagnostics module.
123124
```bash
124125
> diag power
125126
tx power: -10 dBm
127+
Done
126128
```
127129

128130
### diag power \<power\>
@@ -132,7 +134,7 @@ Set the tx power value(dBm) for diagnostics module.
132134
```bash
133135
> diag power -10
134136
set tx power to -10 dBm
135-
status 0x00
137+
Done
136138
```
137139

138140
### diag powersettings
@@ -173,7 +175,7 @@ Send the frame set by `diag frame` if length is omitted. Otherwise overwrite the
173175
```bash
174176
> diag send 20 100
175177
sending 0x14 packet(s), length 0x64
176-
status 0x00
178+
Done
177179
```
178180

179181
### diag repeat \<delay\> [length]
@@ -185,7 +187,7 @@ Send the frame set by `diag frame` if length is omitted. Otherwise overwrite the
185187
```bash
186188
> diag repeat 100 100
187189
sending packets of length 0x64 at the delay of 0x64 ms
188-
status 0x00
190+
Done
189191
```
190192

191193
### diag repeat stop
@@ -195,7 +197,7 @@ Stop repeated packet transmission.
195197
```bash
196198
> diag repeat stop
197199
repeated packet transmission is stopped
198-
status 0x00
200+
Done
199201
```
200202

201203
### diag radio sleep
@@ -205,7 +207,7 @@ Enter radio sleep mode.
205207
```bash
206208
> diag radio sleep
207209
set radio from receive to sleep
208-
status 0x00
210+
Done
209211
```
210212

211213
### diag radio receive
@@ -215,7 +217,7 @@ Set radio from sleep mode to receive mode.
215217
```bash
216218
> diag radio receive
217219
set radio from sleep to receive on channel 11
218-
status 0x00
220+
Done
219221
```
220222

221223
### diag radio receive \[async\] \<number\> \[lpr\]
@@ -245,6 +247,7 @@ Return the state of the radio.
245247
```bash
246248
> diag radio state
247249
sleep
250+
Done
248251
```
249252

250253
### diag radio enable
@@ -312,6 +315,7 @@ received packets: 10
312315
sent packets: 10
313316
first received packet: rssi=-65, lqi=101
314317
last received packet: rssi=-64, lqi=98
318+
Done
315319
```
316320

317321
### diag stats clear
@@ -321,6 +325,7 @@ Clear statistics during diagnostics mode.
321325
```bash
322326
> diag stats clear
323327
stats cleared
328+
Done
324329
```
325330

326331
### diag gpio get \<gpio\>
@@ -384,7 +389,7 @@ first received packet: rssi=-65, lqi=101
384389
last received packet: rssi=-61, lqi=98
385390

386391
stop diagnostics mode
387-
status 0x00
392+
Done
388393
```
389394

390395
### diag rcp

src/core/diags/factory_diags.cpp

+18-34
Original file line numberDiff line numberDiff line change
@@ -334,8 +334,6 @@ Error Diags::ProcessChannel(uint8_t aArgsLength, char *aArgs[])
334334
{
335335
Error error = kErrorNone;
336336

337-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
338-
339337
if (aArgsLength == 0)
340338
{
341339
Output("channel: %d\r\n", mChannel);
@@ -351,7 +349,7 @@ Error Diags::ProcessChannel(uint8_t aArgsLength, char *aArgs[])
351349
IgnoreError(Get<Radio>().Receive(mChannel));
352350
otPlatDiagChannelSet(mChannel);
353351

354-
Output("set channel to %d\r\nstatus 0x%02x\r\n", mChannel, error);
352+
Output("set channel to %d\r\n", mChannel);
355353
}
356354

357355
exit:
@@ -363,8 +361,6 @@ Error Diags::ProcessPower(uint8_t aArgsLength, char *aArgs[])
363361
{
364362
Error error = kErrorNone;
365363

366-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
367-
368364
if (aArgsLength == 0)
369365
{
370366
Output("tx power: %d dBm\r\n", mTxPower);
@@ -379,7 +375,7 @@ Error Diags::ProcessPower(uint8_t aArgsLength, char *aArgs[])
379375
SuccessOrExit(error = Get<Radio>().SetTransmitPower(mTxPower));
380376
otPlatDiagTxPowerSet(mTxPower);
381377

382-
Output("set tx power to %d dBm\r\nstatus 0x%02x\r\n", mTxPower, error);
378+
Output("set tx power to %d dBm\r\n", mTxPower);
383379
}
384380

385381
exit:
@@ -391,14 +387,13 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[])
391387
{
392388
Error error = kErrorNone;
393389

394-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
395390
VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs);
396391

397392
if (StringMatch(aArgs[0], "stop"))
398393
{
399394
otPlatAlarmMilliStop(&GetInstance());
400395
mRepeatActive = false;
401-
Output("repeated packet transmission is stopped\r\nstatus 0x%02x\r\n", error);
396+
Output("repeated packet transmission is stopped\r\n");
402397
}
403398
else
404399
{
@@ -431,8 +426,8 @@ Error Diags::ProcessRepeat(uint8_t aArgsLength, char *aArgs[])
431426
mRepeatActive = true;
432427
uint32_t now = otPlatAlarmMilliGetNow();
433428
otPlatAlarmMilliStartAt(&GetInstance(), now, mTxPeriod);
434-
Output("sending packets of length %#x at the delay of %#x ms\r\nstatus 0x%02x\r\n", static_cast<int>(mTxLen),
435-
static_cast<int>(mTxPeriod), error);
429+
Output("sending packets of length %#x at the delay of %#x ms\r\n", static_cast<int>(mTxLen),
430+
static_cast<int>(mTxPeriod));
436431
}
437432

438433
exit:
@@ -446,7 +441,6 @@ Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[])
446441
uint32_t txPackets;
447442
uint8_t txLength;
448443

449-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
450444
VerifyOrExit(aArgsLength >= 1, error = kErrorInvalidArgs);
451445

452446
SuccessOrExit(error = Utils::CmdLineParser::ParseAsUint32(aArgs[0], txPackets));
@@ -470,8 +464,7 @@ Error Diags::ProcessSend(uint8_t aArgsLength, char *aArgs[])
470464
VerifyOrExit(txLength >= OT_RADIO_FRAME_MIN_SIZE, error = kErrorInvalidArgs);
471465
mTxLen = txLength;
472466

473-
Output("sending %#x packet(s), length %#x\r\nstatus 0x%02x\r\n", static_cast<int>(mTxPackets),
474-
static_cast<int>(mTxLen), error);
467+
Output("sending %#x packet(s), length %#x\r\n", static_cast<int>(mTxPackets), static_cast<int>(mTxLen));
475468
TransmitPacket();
476469

477470
exit:
@@ -501,7 +494,7 @@ Error Diags::ProcessStart(uint8_t aArgsLength, char *aArgs[])
501494
SuccessOrExit(error = Get<Radio>().SetTransmitPower(mTxPower));
502495
otPlatDiagModeSet(true);
503496
mStats.Clear();
504-
Output("start diagnostics mode\r\nstatus 0x%02x\r\n", error);
497+
Output("start diagnostics mode\r\n");
505498

506499
exit:
507500
AppendErrorResult(error);
@@ -512,8 +505,6 @@ Error Diags::ProcessStats(uint8_t aArgsLength, char *aArgs[])
512505
{
513506
Error error = kErrorNone;
514507

515-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
516-
517508
if ((aArgsLength == 1) && StringMatch(aArgs[0], "clear"))
518509
{
519510
mStats.Clear();
@@ -540,10 +531,6 @@ Error Diags::ProcessStop(uint8_t aArgsLength, char *aArgs[])
540531
OT_UNUSED_VARIABLE(aArgsLength);
541532
OT_UNUSED_VARIABLE(aArgs);
542533

543-
Error error = kErrorNone;
544-
545-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
546-
547534
otPlatAlarmMilliStop(&GetInstance());
548535
otPlatDiagModeSet(false);
549536
Get<Radio>().SetPromiscuous(false);
@@ -552,14 +539,12 @@ Error Diags::ProcessStop(uint8_t aArgsLength, char *aArgs[])
552539
Output("received packets: %d\r\nsent packets: %d\r\n"
553540
"first received packet: rssi=%d, lqi=%d\r\n"
554541
"last received packet: rssi=%d, lqi=%d\r\n"
555-
"\nstop diagnostics mode\r\nstatus 0x%02x\r\n",
542+
"\nstop diagnostics mode\r\n",
556543
static_cast<int>(mStats.mReceivedPackets), static_cast<int>(mStats.mSentPackets),
557544
static_cast<int>(mStats.mFirstRssi), static_cast<int>(mStats.mFirstLqi), static_cast<int>(mStats.mLastRssi),
558-
static_cast<int>(mStats.mLastLqi), error);
545+
static_cast<int>(mStats.mLastLqi));
559546

560-
exit:
561-
AppendErrorResult(error);
562-
return error;
547+
return kErrorNone;
563548
}
564549

565550
void Diags::TransmitPacket(void)
@@ -635,13 +620,12 @@ Error Diags::ProcessRadio(uint8_t aArgsLength, char *aArgs[])
635620
{
636621
Error error = kErrorInvalidArgs;
637622

638-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
639623
VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs);
640624

641625
if (StringMatch(aArgs[0], "sleep"))
642626
{
643627
SuccessOrExit(error = Get<Radio>().Sleep());
644-
Output("set radio from receive to sleep \r\nstatus 0x%02x\r\n", error);
628+
Output("set radio from receive to sleep \r\n");
645629
}
646630
else if (StringMatch(aArgs[0], "receive"))
647631
{
@@ -653,7 +637,7 @@ Error Diags::ProcessRadio(uint8_t aArgsLength, char *aArgs[])
653637
if (aArgsLength == 0)
654638
{
655639
SuccessOrExit(error = RadioReceive());
656-
Output("set radio from sleep to receive on channel %d\r\nstatus 0x%02x\r\n", mChannel, error);
640+
Output("set radio from sleep to receive on channel %d\r\n", mChannel);
657641
ExitNow();
658642
}
659643

@@ -840,7 +824,6 @@ Error Diags::ProcessContinuousWave(uint8_t aArgsLength, char *aArgs[])
840824
{
841825
Error error = kErrorInvalidArgs;
842826

843-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
844827
VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs);
845828

846829
if (StringMatch(aArgs[0], "start"))
@@ -861,7 +844,6 @@ Error Diags::ProcessStream(uint8_t aArgsLength, char *aArgs[])
861844
{
862845
Error error = kErrorInvalidArgs;
863846

864-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
865847
VerifyOrExit(aArgsLength > 0, error = kErrorInvalidArgs);
866848

867849
if (StringMatch(aArgs[0], "start"))
@@ -892,8 +874,6 @@ Error Diags::ProcessPowerSettings(uint8_t aArgsLength, char *aArgs[])
892874
uint8_t channel;
893875
PowerSettings powerSettings;
894876

895-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
896-
897877
if (aArgsLength == 0)
898878
{
899879
bool isPrePowerSettingsValid = false;
@@ -951,8 +931,6 @@ Error Diags::ProcessRawPowerSetting(uint8_t aArgsLength, char *aArgs[])
951931
Error error = kErrorInvalidArgs;
952932
RawPowerSetting setting;
953933

954-
VerifyOrExit(otPlatDiagModeGet(), error = kErrorInvalidState);
955-
956934
if (aArgsLength == 0)
957935
{
958936
SuccessOrExit(error = GetRawPowerSetting(setting));
@@ -1112,6 +1090,12 @@ Error Diags::ProcessCmd(uint8_t aArgsLength, char *aArgs[])
11121090
ExitNow();
11131091
}
11141092

1093+
if (!otPlatDiagModeGet() && !StringMatch(aArgs[0], "start"))
1094+
{
1095+
Output("diagnostics mode is disabled\r\n");
1096+
ExitNow(error = kErrorInvalidState);
1097+
}
1098+
11151099
for (const Command &command : sCommands)
11161100
{
11171101
if (StringMatch(aArgs[0], command.mName))

0 commit comments

Comments
 (0)