1 /*
2  *  Copyright (c) 2020, The OpenThread Authors.
3  *  All rights reserved.
4  *
5  *  Redistribution and use in source and binary forms, with or without
6  *  modification, are permitted provided that the following conditions are met:
7  *  1. Redistributions of source code must retain the above copyright
8  *     notice, this list of conditions and the following disclaimer.
9  *  2. Redistributions in binary form must reproduce the above copyright
10  *     notice, this list of conditions and the following disclaimer in the
11  *     documentation and/or other materials provided with the distribution.
12  *  3. Neither the name of the copyright holder nor the
13  *     names of its contributors may be used to endorse or promote products
14  *     derived from this software without specific prior written permission.
15  *
16  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26  *  POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /**
30  * @file
31  *   This file implements the radio apis on posix platform.
32  */
33 
34 #include "platform-posix.h"
35 
36 #include <string.h>
37 
38 #include <openthread/logging.h>
39 #include <openthread/platform/diag.h>
40 
41 #include "common/code_utils.hpp"
42 #include "common/new.hpp"
43 #include "posix/platform/radio.hpp"
44 #include "posix/platform/spinel_driver_getter.hpp"
45 #include "posix/platform/spinel_manager.hpp"
46 #include "utils/parse_cmdline.hpp"
47 
48 #if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
49 #include "configuration.hpp"
50 static ot::Posix::Configuration sConfig;
51 #endif
52 
53 static ot::Posix::Radio sRadio;
54 
55 namespace ot {
56 namespace Posix {
57 
58 namespace {
platformRadioInit(const char * aUrl)59 extern "C" void platformRadioInit(const char *aUrl) { sRadio.Init(aUrl); }
60 } // namespace
61 
62 const char Radio::kLogModuleName[] = "Radio";
63 
Radio(void)64 Radio::Radio(void)
65     : mRadioUrl(nullptr)
66     , mRadioSpinel()
67 #if OPENTHREAD_POSIX_CONFIG_RCP_CAPS_DIAG_ENABLE
68     , mRcpCapsDiag(mRadioSpinel)
69 #endif
70 {
71 }
72 
Init(const char * aUrl)73 void Radio::Init(const char *aUrl)
74 {
75     bool resetRadio;
76     bool skipCompatibilityCheck;
77 #if OPENTHREAD_CONFIG_THREAD_VERSION >= OT_THREAD_VERSION_1_2 && OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE
78     bool aEnableRcpTimeSync = true;
79 #else
80     bool aEnableRcpTimeSync = false;
81 #endif
82     struct ot::Spinel::RadioSpinelCallbacks callbacks;
83 
84     mRadioUrl.Init(aUrl);
85     VerifyOrDie(mRadioUrl.GetPath() != nullptr, OT_EXIT_INVALID_ARGUMENTS);
86 
87     memset(&callbacks, 0, sizeof(callbacks));
88 #if OPENTHREAD_CONFIG_DIAG_ENABLE
89     callbacks.mDiagReceiveDone  = otPlatDiagRadioReceiveDone;
90     callbacks.mDiagTransmitDone = otPlatDiagRadioTransmitDone;
91 #endif // OPENTHREAD_CONFIG_DIAG_ENABLE
92     callbacks.mEnergyScanDone    = otPlatRadioEnergyScanDone;
93     callbacks.mBusLatencyChanged = otPlatRadioBusLatencyChanged;
94     callbacks.mReceiveDone       = otPlatRadioReceiveDone;
95     callbacks.mTransmitDone      = otPlatRadioTxDone;
96     callbacks.mTxStarted         = otPlatRadioTxStarted;
97 
98     resetRadio             = !mRadioUrl.HasParam("no-reset");
99     skipCompatibilityCheck = mRadioUrl.HasParam("skip-rcp-compatibility-check");
100 
101     mRadioSpinel.SetCallbacks(callbacks);
102     mRadioSpinel.Init(skipCompatibilityCheck, resetRadio, &GetSpinelDriver(), kRequiredRadioCaps, aEnableRcpTimeSync);
103 
104     ProcessRadioUrl(mRadioUrl);
105 }
106 
ProcessRadioUrl(const RadioUrl & aRadioUrl)107 void Radio::ProcessRadioUrl(const RadioUrl &aRadioUrl)
108 {
109     const char *region;
110     int8_t      value;
111 
112     if (aRadioUrl.HasParam("ncp-dataset"))
113     {
114         LogCrit("The argument \"ncp-dataset\" is no longer supported");
115         DieNow(OT_ERROR_FAILED);
116     }
117 
118     if (aRadioUrl.HasParam("fem-lnagain"))
119     {
120         SuccessOrDie(aRadioUrl.ParseInt8("fem-lnagain", value));
121         SuccessOrDie(mRadioSpinel.SetFemLnaGain(value));
122     }
123 
124     if (aRadioUrl.HasParam("cca-threshold"))
125     {
126         SuccessOrDie(aRadioUrl.ParseInt8("cca-threshold", value));
127         SuccessOrDie(mRadioSpinel.SetCcaEnergyDetectThreshold(value));
128     }
129 
130     if ((region = aRadioUrl.GetValue("region")) != nullptr)
131     {
132         uint16_t regionCode;
133 
134         VerifyOrDie(strnlen(region, 3) == 2, OT_EXIT_INVALID_ARGUMENTS);
135         regionCode = static_cast<uint16_t>(static_cast<uint16_t>(region[0]) << 8) + static_cast<uint16_t>(region[1]);
136         SuccessOrDie(otPlatRadioSetRegion(gInstance, regionCode));
137     }
138 
139     if (aRadioUrl.HasParam("bus-latency"))
140     {
141         uint32_t busLatency;
142         SuccessOrDie(aRadioUrl.ParseUint32("bus-latency", busLatency));
143         mRadioSpinel.SetBusLatency(busLatency);
144     }
145 
146     ProcessMaxPowerTable(aRadioUrl);
147 
148 #if OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE
149     {
150         const char *enableCoex = aRadioUrl.GetValue("enable-coex");
151         if (enableCoex != nullptr)
152         {
153             SuccessOrDie(mRadioSpinel.SetCoexEnabled(enableCoex[0] != '0'));
154         }
155     }
156 #endif // OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE
157 }
158 
ProcessMaxPowerTable(const RadioUrl & aRadioUrl)159 void Radio::ProcessMaxPowerTable(const RadioUrl &aRadioUrl)
160 {
161     OT_UNUSED_VARIABLE(aRadioUrl);
162 
163 #if OPENTHREAD_POSIX_CONFIG_MAX_POWER_TABLE_ENABLE
164     otError          error;
165     constexpr int8_t kPowerDefault = 30; // Default power 1 watt (30 dBm).
166     const char      *str           = nullptr;
167     char            *pSave         = nullptr;
168     uint8_t          channel       = ot::Radio::kChannelMin;
169     int8_t           power         = kPowerDefault;
170     const char      *maxPowerTable;
171 
172     VerifyOrExit((maxPowerTable = aRadioUrl.GetValue("max-power-table")) != nullptr);
173 
174     for (str = strtok_r(const_cast<char *>(maxPowerTable), ",", &pSave);
175          str != nullptr && channel <= ot::Radio::kChannelMax; str = strtok_r(nullptr, ",", &pSave))
176     {
177         power = static_cast<int8_t>(strtol(str, nullptr, 0));
178         error = mRadioSpinel.SetChannelMaxTransmitPower(channel, power);
179         VerifyOrDie((error == OT_ERROR_NONE) || (error == OT_ERROR_NOT_IMPLEMENTED), OT_EXIT_FAILURE);
180         if (error == OT_ERROR_NOT_IMPLEMENTED)
181         {
182             LogWarn("The RCP doesn't support setting the max transmit power");
183         }
184 
185         ++channel;
186     }
187 
188     // Use the last power if omitted.
189     while (channel <= ot::Radio::kChannelMax)
190     {
191         error = mRadioSpinel.SetChannelMaxTransmitPower(channel, power);
192         VerifyOrDie((error == OT_ERROR_NONE) || (error == OT_ERROR_NOT_IMPLEMENTED), OT_ERROR_FAILED);
193         if (error == OT_ERROR_NOT_IMPLEMENTED)
194         {
195             LogWarn("The RCP doesn't support setting the max transmit power");
196         }
197 
198         ++channel;
199     }
200 
201     VerifyOrDie(str == nullptr, OT_EXIT_INVALID_ARGUMENTS);
202 
203 exit:
204     return;
205 #endif // OPENTHREAD_POSIX_CONFIG_MAX_POWER_TABLE_ENABLE
206 }
207 
208 } // namespace Posix
209 } // namespace ot
210 
GetRadioSpinel(void)211 ot::Spinel::RadioSpinel &GetRadioSpinel(void) { return sRadio.GetRadioSpinel(); }
212 
213 #if OPENTHREAD_POSIX_CONFIG_RCP_CAPS_DIAG_ENABLE
GetRcpCapsDiag(void)214 ot::Posix::RcpCapsDiag &GetRcpCapsDiag(void) { return sRadio.GetRcpCapsDiag(); }
215 #endif
216 
platformRadioDeinit(void)217 void platformRadioDeinit(void) { GetRadioSpinel().Deinit(); }
218 
platformRadioHandleStateChange(otInstance * aInstance,otChangedFlags aFlags)219 void platformRadioHandleStateChange(otInstance *aInstance, otChangedFlags aFlags)
220 {
221     if (OT_CHANGED_THREAD_NETIF_STATE & aFlags)
222     {
223         GetRadioSpinel().SetTimeSyncState(otIp6IsEnabled(aInstance));
224     }
225 }
226 
otPlatRadioGetIeeeEui64(otInstance * aInstance,uint8_t * aIeeeEui64)227 void otPlatRadioGetIeeeEui64(otInstance *aInstance, uint8_t *aIeeeEui64)
228 {
229     OT_UNUSED_VARIABLE(aInstance);
230     SuccessOrDie(GetRadioSpinel().GetIeeeEui64(aIeeeEui64));
231 }
232 
otPlatRadioSetPanId(otInstance * aInstance,uint16_t panid)233 void otPlatRadioSetPanId(otInstance *aInstance, uint16_t panid)
234 {
235     OT_UNUSED_VARIABLE(aInstance);
236     SuccessOrDie(GetRadioSpinel().SetPanId(panid));
237 }
238 
otPlatRadioSetExtendedAddress(otInstance * aInstance,const otExtAddress * aAddress)239 void otPlatRadioSetExtendedAddress(otInstance *aInstance, const otExtAddress *aAddress)
240 {
241     OT_UNUSED_VARIABLE(aInstance);
242     otExtAddress addr;
243 
244     for (size_t i = 0; i < sizeof(addr); i++)
245     {
246         addr.m8[i] = aAddress->m8[sizeof(addr) - 1 - i];
247     }
248 
249     SuccessOrDie(GetRadioSpinel().SetExtendedAddress(addr));
250 }
251 
otPlatRadioSetShortAddress(otInstance * aInstance,uint16_t aAddress)252 void otPlatRadioSetShortAddress(otInstance *aInstance, uint16_t aAddress)
253 {
254     OT_UNUSED_VARIABLE(aInstance);
255     SuccessOrDie(GetRadioSpinel().SetShortAddress(aAddress));
256 }
257 
otPlatRadioSetPromiscuous(otInstance * aInstance,bool aEnable)258 void otPlatRadioSetPromiscuous(otInstance *aInstance, bool aEnable)
259 {
260     OT_UNUSED_VARIABLE(aInstance);
261     SuccessOrDie(GetRadioSpinel().SetPromiscuous(aEnable));
262 }
263 
otPlatRadioIsEnabled(otInstance * aInstance)264 bool otPlatRadioIsEnabled(otInstance *aInstance)
265 {
266     OT_UNUSED_VARIABLE(aInstance);
267     return GetRadioSpinel().IsEnabled();
268 }
269 
otPlatRadioEnable(otInstance * aInstance)270 otError otPlatRadioEnable(otInstance *aInstance) { return GetRadioSpinel().Enable(aInstance); }
271 
otPlatRadioDisable(otInstance * aInstance)272 otError otPlatRadioDisable(otInstance *aInstance)
273 {
274     OT_UNUSED_VARIABLE(aInstance);
275     return GetRadioSpinel().Disable();
276 }
277 
otPlatRadioSleep(otInstance * aInstance)278 otError otPlatRadioSleep(otInstance *aInstance)
279 {
280     OT_UNUSED_VARIABLE(aInstance);
281     return GetRadioSpinel().Sleep();
282 }
283 
otPlatRadioReceive(otInstance * aInstance,uint8_t aChannel)284 otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel)
285 {
286     OT_UNUSED_VARIABLE(aInstance);
287 
288     otError error;
289 
290     SuccessOrExit(error = GetRadioSpinel().Receive(aChannel));
291 
292 exit:
293     return error;
294 }
295 
otPlatRadioTransmit(otInstance * aInstance,otRadioFrame * aFrame)296 otError otPlatRadioTransmit(otInstance *aInstance, otRadioFrame *aFrame)
297 {
298     OT_UNUSED_VARIABLE(aInstance);
299     return GetRadioSpinel().Transmit(*aFrame);
300 }
301 
otPlatRadioGetTransmitBuffer(otInstance * aInstance)302 otRadioFrame *otPlatRadioGetTransmitBuffer(otInstance *aInstance)
303 {
304     OT_UNUSED_VARIABLE(aInstance);
305     return &GetRadioSpinel().GetTransmitFrame();
306 }
307 
otPlatRadioGetRssi(otInstance * aInstance)308 int8_t otPlatRadioGetRssi(otInstance *aInstance)
309 {
310     OT_UNUSED_VARIABLE(aInstance);
311     return GetRadioSpinel().GetRssi();
312 }
313 
otPlatRadioGetCaps(otInstance * aInstance)314 otRadioCaps otPlatRadioGetCaps(otInstance *aInstance)
315 {
316     OT_UNUSED_VARIABLE(aInstance);
317     return GetRadioSpinel().GetRadioCaps();
318 }
319 
otPlatRadioGetVersionString(otInstance * aInstance)320 const char *otPlatRadioGetVersionString(otInstance *aInstance)
321 {
322     OT_UNUSED_VARIABLE(aInstance);
323     return GetRadioSpinel().GetVersion();
324 }
325 
otPlatRadioGetPromiscuous(otInstance * aInstance)326 bool otPlatRadioGetPromiscuous(otInstance *aInstance)
327 {
328     OT_UNUSED_VARIABLE(aInstance);
329     return GetRadioSpinel().IsPromiscuous();
330 }
331 
platformRadioUpdateFdSet(otSysMainloopContext * aContext)332 void platformRadioUpdateFdSet(otSysMainloopContext *aContext)
333 {
334     uint64_t now      = otPlatTimeGet();
335     uint64_t deadline = GetRadioSpinel().GetNextRadioTimeRecalcStart();
336 
337     if (GetRadioSpinel().IsTransmitting())
338     {
339         uint64_t txRadioEndUs = GetRadioSpinel().GetTxRadioEndUs();
340 
341         if (txRadioEndUs < deadline)
342         {
343             deadline = txRadioEndUs;
344         }
345     }
346 
347     if (now < deadline)
348     {
349         uint64_t remain = deadline - now;
350 
351         if (remain < (static_cast<uint64_t>(aContext->mTimeout.tv_sec) * OT_US_PER_S +
352                       static_cast<uint64_t>(aContext->mTimeout.tv_usec)))
353         {
354             aContext->mTimeout.tv_sec  = static_cast<time_t>(remain / OT_US_PER_S);
355             aContext->mTimeout.tv_usec = static_cast<suseconds_t>(remain % OT_US_PER_S);
356         }
357     }
358     else
359     {
360         aContext->mTimeout.tv_sec  = 0;
361         aContext->mTimeout.tv_usec = 0;
362     }
363 
364     if (GetRadioSpinel().IsTransmitDone())
365     {
366         aContext->mTimeout.tv_sec  = 0;
367         aContext->mTimeout.tv_usec = 0;
368     }
369 }
370 
371 #if OPENTHREAD_POSIX_VIRTUAL_TIME
virtualTimeRadioProcess(otInstance * aInstance,const struct VirtualTimeEvent * aEvent)372 void virtualTimeRadioProcess(otInstance *aInstance, const struct VirtualTimeEvent *aEvent)
373 {
374     OT_UNUSED_VARIABLE(aInstance);
375     GetRadioSpinel().Process(aEvent);
376 }
377 #else
platformRadioProcess(otInstance * aInstance,const otSysMainloopContext * aContext)378 void platformRadioProcess(otInstance *aInstance, const otSysMainloopContext *aContext)
379 {
380     OT_UNUSED_VARIABLE(aInstance);
381 
382     GetRadioSpinel().Process(aContext);
383 }
384 #endif // OPENTHREAD_POSIX_VIRTUAL_TIME
385 
otPlatRadioEnableSrcMatch(otInstance * aInstance,bool aEnable)386 void otPlatRadioEnableSrcMatch(otInstance *aInstance, bool aEnable)
387 {
388     OT_UNUSED_VARIABLE(aInstance);
389     SuccessOrDie(GetRadioSpinel().EnableSrcMatch(aEnable));
390 }
391 
otPlatRadioAddSrcMatchShortEntry(otInstance * aInstance,uint16_t aShortAddress)392 otError otPlatRadioAddSrcMatchShortEntry(otInstance *aInstance, uint16_t aShortAddress)
393 {
394     OT_UNUSED_VARIABLE(aInstance);
395     return GetRadioSpinel().AddSrcMatchShortEntry(aShortAddress);
396 }
397 
otPlatRadioAddSrcMatchExtEntry(otInstance * aInstance,const otExtAddress * aExtAddress)398 otError otPlatRadioAddSrcMatchExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress)
399 {
400     OT_UNUSED_VARIABLE(aInstance);
401     otExtAddress addr;
402 
403     for (size_t i = 0; i < sizeof(addr); i++)
404     {
405         addr.m8[i] = aExtAddress->m8[sizeof(addr) - 1 - i];
406     }
407 
408     return GetRadioSpinel().AddSrcMatchExtEntry(addr);
409 }
410 
otPlatRadioClearSrcMatchShortEntry(otInstance * aInstance,uint16_t aShortAddress)411 otError otPlatRadioClearSrcMatchShortEntry(otInstance *aInstance, uint16_t aShortAddress)
412 {
413     OT_UNUSED_VARIABLE(aInstance);
414     return GetRadioSpinel().ClearSrcMatchShortEntry(aShortAddress);
415 }
416 
otPlatRadioClearSrcMatchExtEntry(otInstance * aInstance,const otExtAddress * aExtAddress)417 otError otPlatRadioClearSrcMatchExtEntry(otInstance *aInstance, const otExtAddress *aExtAddress)
418 {
419     OT_UNUSED_VARIABLE(aInstance);
420     otExtAddress addr;
421 
422     for (size_t i = 0; i < sizeof(addr); i++)
423     {
424         addr.m8[i] = aExtAddress->m8[sizeof(addr) - 1 - i];
425     }
426 
427     return GetRadioSpinel().ClearSrcMatchExtEntry(addr);
428 }
429 
otPlatRadioClearSrcMatchShortEntries(otInstance * aInstance)430 void otPlatRadioClearSrcMatchShortEntries(otInstance *aInstance)
431 {
432     OT_UNUSED_VARIABLE(aInstance);
433     SuccessOrDie(GetRadioSpinel().ClearSrcMatchShortEntries());
434 }
435 
otPlatRadioClearSrcMatchExtEntries(otInstance * aInstance)436 void otPlatRadioClearSrcMatchExtEntries(otInstance *aInstance)
437 {
438     OT_UNUSED_VARIABLE(aInstance);
439     SuccessOrDie(GetRadioSpinel().ClearSrcMatchExtEntries());
440 }
441 
otPlatRadioEnergyScan(otInstance * aInstance,uint8_t aScanChannel,uint16_t aScanDuration)442 otError otPlatRadioEnergyScan(otInstance *aInstance, uint8_t aScanChannel, uint16_t aScanDuration)
443 {
444     OT_UNUSED_VARIABLE(aInstance);
445     return GetRadioSpinel().EnergyScan(aScanChannel, aScanDuration);
446 }
447 
otPlatRadioGetTransmitPower(otInstance * aInstance,int8_t * aPower)448 otError otPlatRadioGetTransmitPower(otInstance *aInstance, int8_t *aPower)
449 {
450     OT_UNUSED_VARIABLE(aInstance);
451     assert(aPower != nullptr);
452     return GetRadioSpinel().GetTransmitPower(*aPower);
453 }
454 
otPlatRadioSetTransmitPower(otInstance * aInstance,int8_t aPower)455 otError otPlatRadioSetTransmitPower(otInstance *aInstance, int8_t aPower)
456 {
457     OT_UNUSED_VARIABLE(aInstance);
458     return GetRadioSpinel().SetTransmitPower(aPower);
459 }
460 
otPlatRadioGetCcaEnergyDetectThreshold(otInstance * aInstance,int8_t * aThreshold)461 otError otPlatRadioGetCcaEnergyDetectThreshold(otInstance *aInstance, int8_t *aThreshold)
462 {
463     OT_UNUSED_VARIABLE(aInstance);
464     assert(aThreshold != nullptr);
465     return GetRadioSpinel().GetCcaEnergyDetectThreshold(*aThreshold);
466 }
467 
otPlatRadioSetCcaEnergyDetectThreshold(otInstance * aInstance,int8_t aThreshold)468 otError otPlatRadioSetCcaEnergyDetectThreshold(otInstance *aInstance, int8_t aThreshold)
469 {
470     OT_UNUSED_VARIABLE(aInstance);
471     return GetRadioSpinel().SetCcaEnergyDetectThreshold(aThreshold);
472 }
473 
otPlatRadioGetFemLnaGain(otInstance * aInstance,int8_t * aGain)474 otError otPlatRadioGetFemLnaGain(otInstance *aInstance, int8_t *aGain)
475 {
476     OT_UNUSED_VARIABLE(aInstance);
477     assert(aGain != nullptr);
478     return GetRadioSpinel().GetFemLnaGain(*aGain);
479 }
480 
otPlatRadioSetFemLnaGain(otInstance * aInstance,int8_t aGain)481 otError otPlatRadioSetFemLnaGain(otInstance *aInstance, int8_t aGain)
482 {
483     OT_UNUSED_VARIABLE(aInstance);
484     return GetRadioSpinel().SetFemLnaGain(aGain);
485 }
486 
otPlatRadioGetReceiveSensitivity(otInstance * aInstance)487 int8_t otPlatRadioGetReceiveSensitivity(otInstance *aInstance)
488 {
489     OT_UNUSED_VARIABLE(aInstance);
490     return GetRadioSpinel().GetReceiveSensitivity();
491 }
492 
493 #if OPENTHREAD_CONFIG_PLATFORM_RADIO_COEX_ENABLE
otPlatRadioSetCoexEnabled(otInstance * aInstance,bool aEnabled)494 otError otPlatRadioSetCoexEnabled(otInstance *aInstance, bool aEnabled)
495 {
496     OT_UNUSED_VARIABLE(aInstance);
497     return GetRadioSpinel().SetCoexEnabled(aEnabled);
498 }
499 
otPlatRadioIsCoexEnabled(otInstance * aInstance)500 bool otPlatRadioIsCoexEnabled(otInstance *aInstance)
501 {
502     OT_UNUSED_VARIABLE(aInstance);
503     return GetRadioSpinel().IsCoexEnabled();
504 }
505 
otPlatRadioGetCoexMetrics(otInstance * aInstance,otRadioCoexMetrics * aCoexMetrics)506 otError otPlatRadioGetCoexMetrics(otInstance *aInstance, otRadioCoexMetrics *aCoexMetrics)
507 {
508     OT_UNUSED_VARIABLE(aInstance);
509 
510     otError error = OT_ERROR_NONE;
511 
512     VerifyOrExit(aCoexMetrics != nullptr, error = OT_ERROR_INVALID_ARGS);
513 
514     error = GetRadioSpinel().GetCoexMetrics(*aCoexMetrics);
515 
516 exit:
517     return error;
518 }
519 #endif
520 
521 #if OPENTHREAD_CONFIG_DIAG_ENABLE
522 static otPlatDiagOutputCallback sDiagOutputCallback  = nullptr;
523 static void                    *sDiagCallbackContext = nullptr;
524 static char                    *sDiagOutput          = nullptr;
525 static uint16_t                 sDiagOutputLen       = 0;
526 
handleDiagOutput(const char * aFormat,va_list aArguments,void * aContext)527 static void handleDiagOutput(const char *aFormat, va_list aArguments, void *aContext)
528 {
529     OT_UNUSED_VARIABLE(aContext);
530     int charsWritten;
531 
532     VerifyOrExit((sDiagOutput != nullptr) && (sDiagOutputLen > 0));
533     charsWritten = vsnprintf(sDiagOutput, sDiagOutputLen, aFormat, aArguments);
534     VerifyOrExit(charsWritten > 0);
535     charsWritten = (sDiagOutputLen <= charsWritten) ? sDiagOutputLen : charsWritten;
536     sDiagOutput += charsWritten;
537     sDiagOutputLen -= charsWritten;
538 
539 exit:
540     return;
541 }
542 
setDiagOutput(char * aOutput,size_t aSize)543 static void setDiagOutput(char *aOutput, size_t aSize)
544 {
545     sDiagOutput    = aOutput;
546     sDiagOutputLen = static_cast<uint16_t>(aSize);
547     GetRadioSpinel().SetDiagOutputCallback(handleDiagOutput, nullptr);
548 }
549 
freeDiagOutput(void)550 static void freeDiagOutput(void)
551 {
552     sDiagOutput    = nullptr;
553     sDiagOutputLen = 0;
554     GetRadioSpinel().SetDiagOutputCallback(sDiagOutputCallback, sDiagCallbackContext);
555 }
556 
otPlatDiagSetOutputCallback(otInstance * aInstance,otPlatDiagOutputCallback aCallback,void * aContext)557 void otPlatDiagSetOutputCallback(otInstance *aInstance, otPlatDiagOutputCallback aCallback, void *aContext)
558 {
559     OT_UNUSED_VARIABLE(aInstance);
560 
561     sDiagOutputCallback  = aCallback;
562     sDiagCallbackContext = aContext;
563 
564     GetRadioSpinel().SetDiagOutputCallback(aCallback, aContext);
565 #if OPENTHREAD_POSIX_CONFIG_RCP_CAPS_DIAG_ENABLE
566     GetRcpCapsDiag().SetDiagOutputCallback(aCallback, aContext);
567 #endif
568 }
569 
otPlatDiagProcess(otInstance * aInstance,uint8_t aArgsLength,char * aArgs[])570 otError otPlatDiagProcess(otInstance *aInstance, uint8_t aArgsLength, char *aArgs[])
571 {
572     OT_UNUSED_VARIABLE(aInstance);
573     otError error                                            = OT_ERROR_NONE;
574     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE] = {'\0'};
575     char   *cur                                              = cmd;
576     char   *end                                              = cmd + sizeof(cmd);
577 
578 #if OPENTHREAD_POSIX_CONFIG_RCP_CAPS_DIAG_ENABLE
579     if (strcmp(aArgs[0], "rcpcaps") == 0)
580     {
581         error = GetRcpCapsDiag().DiagProcess(aArgs, aArgsLength);
582         ExitNow();
583     }
584 #endif
585 
586     if (strcmp(aArgs[0], "radiospinel") == 0)
587     {
588         error = GetRadioSpinel().RadioSpinelDiagProcess(aArgs, aArgsLength);
589         ExitNow();
590     }
591 
592     for (uint8_t index = 0; (index < aArgsLength) && (cur < end); index++)
593     {
594         cur += snprintf(cur, static_cast<size_t>(end - cur), "%s ", aArgs[index]);
595     }
596 
597     // deliver the platform specific diags commands to radio only ncp.
598     error = GetRadioSpinel().PlatDiagProcess(cmd);
599 
600 exit:
601     return error;
602 }
603 
otPlatDiagModeSet(bool aMode)604 void otPlatDiagModeSet(bool aMode)
605 {
606     SuccessOrExit(GetRadioSpinel().PlatDiagProcess(aMode ? "start" : "stop"));
607     GetRadioSpinel().SetDiagEnabled(aMode);
608 
609 exit:
610     return;
611 }
612 
otPlatDiagModeGet(void)613 bool otPlatDiagModeGet(void) { return GetRadioSpinel().IsDiagEnabled(); }
614 
otPlatDiagTxPowerSet(int8_t aTxPower)615 void otPlatDiagTxPowerSet(int8_t aTxPower)
616 {
617     char cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
618 
619     snprintf(cmd, sizeof(cmd), "power %d", aTxPower);
620     SuccessOrExit(GetRadioSpinel().PlatDiagProcess(cmd));
621 
622 exit:
623     return;
624 }
625 
otPlatDiagChannelSet(uint8_t aChannel)626 void otPlatDiagChannelSet(uint8_t aChannel)
627 {
628     char cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
629 
630     snprintf(cmd, sizeof(cmd), "channel %d", aChannel);
631     SuccessOrExit(GetRadioSpinel().PlatDiagProcess(cmd));
632 
633 exit:
634     return;
635 }
636 
otPlatDiagGpioSet(uint32_t aGpio,bool aValue)637 otError otPlatDiagGpioSet(uint32_t aGpio, bool aValue)
638 {
639     otError error;
640     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
641 
642     snprintf(cmd, sizeof(cmd), "gpio set %d %d", aGpio, aValue);
643     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
644 
645 exit:
646     return error;
647 }
648 
otPlatDiagGpioGet(uint32_t aGpio,bool * aValue)649 otError otPlatDiagGpioGet(uint32_t aGpio, bool *aValue)
650 {
651     otError error;
652     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
653     char    output[OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE];
654     char   *str;
655 
656     setDiagOutput(output, sizeof(output));
657 
658     snprintf(cmd, sizeof(cmd), "gpio get %d", aGpio);
659     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
660     VerifyOrExit((str = strtok(output, "\r")) != nullptr, error = OT_ERROR_FAILED);
661     *aValue = static_cast<bool>(atoi(str));
662 
663 exit:
664     freeDiagOutput();
665 
666     return error;
667 }
668 
otPlatDiagGpioSetMode(uint32_t aGpio,otGpioMode aMode)669 otError otPlatDiagGpioSetMode(uint32_t aGpio, otGpioMode aMode)
670 {
671     otError error;
672     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
673 
674     snprintf(cmd, sizeof(cmd), "gpio mode %d %s", aGpio, aMode == OT_GPIO_MODE_INPUT ? "in" : "out");
675     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
676 
677 exit:
678     return error;
679 }
680 
otPlatDiagGpioGetMode(uint32_t aGpio,otGpioMode * aMode)681 otError otPlatDiagGpioGetMode(uint32_t aGpio, otGpioMode *aMode)
682 {
683     otError error;
684     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
685     char    output[OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE];
686     char   *str;
687 
688     setDiagOutput(output, sizeof(output));
689 
690     snprintf(cmd, sizeof(cmd), "gpio mode %d", aGpio);
691     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
692     VerifyOrExit((str = strtok(output, "\r")) != nullptr, error = OT_ERROR_FAILED);
693 
694     if (strcmp(str, "in") == 0)
695     {
696         *aMode = OT_GPIO_MODE_INPUT;
697     }
698     else if (strcmp(str, "out") == 0)
699     {
700         *aMode = OT_GPIO_MODE_OUTPUT;
701     }
702     else
703     {
704         error = OT_ERROR_FAILED;
705     }
706 
707 exit:
708     freeDiagOutput();
709 
710     return error;
711 }
712 
otPlatDiagRadioGetPowerSettings(otInstance * aInstance,uint8_t aChannel,int16_t * aTargetPower,int16_t * aActualPower,uint8_t * aRawPowerSetting,uint16_t * aRawPowerSettingLength)713 otError otPlatDiagRadioGetPowerSettings(otInstance *aInstance,
714                                         uint8_t     aChannel,
715                                         int16_t    *aTargetPower,
716                                         int16_t    *aActualPower,
717                                         uint8_t    *aRawPowerSetting,
718                                         uint16_t   *aRawPowerSettingLength)
719 {
720     OT_UNUSED_VARIABLE(aInstance);
721     static constexpr uint16_t kRawPowerStringSize = OPENTHREAD_CONFIG_POWER_CALIBRATION_RAW_POWER_SETTING_SIZE * 2 + 1;
722     static constexpr uint16_t kFmtStringSize      = 100;
723 
724     otError error;
725     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
726     char    output[OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE];
727     int     targetPower;
728     int     actualPower;
729     char    rawPowerSetting[kRawPowerStringSize];
730     char    fmt[kFmtStringSize];
731 
732     assert((aTargetPower != nullptr) && (aActualPower != nullptr) && (aRawPowerSetting != nullptr) &&
733            (aRawPowerSettingLength != nullptr));
734 
735     setDiagOutput(output, sizeof(output));
736 
737     snprintf(cmd, sizeof(cmd), "powersettings %d", aChannel);
738     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
739     snprintf(fmt, sizeof(fmt), "TargetPower(0.01dBm): %%d\r\nActualPower(0.01dBm): %%d\r\nRawPowerSetting: %%%us\r\n",
740              kRawPowerStringSize);
741     VerifyOrExit(sscanf(output, fmt, &targetPower, &actualPower, rawPowerSetting) == 3, error = OT_ERROR_FAILED);
742     SuccessOrExit(
743         error = ot::Utils::CmdLineParser::ParseAsHexString(rawPowerSetting, *aRawPowerSettingLength, aRawPowerSetting));
744     *aTargetPower = static_cast<int16_t>(targetPower);
745     *aActualPower = static_cast<int16_t>(actualPower);
746 
747 exit:
748     freeDiagOutput();
749 
750     return error;
751 }
752 
otPlatDiagRadioSetRawPowerSetting(otInstance * aInstance,const uint8_t * aRawPowerSetting,uint16_t aRawPowerSettingLength)753 otError otPlatDiagRadioSetRawPowerSetting(otInstance    *aInstance,
754                                           const uint8_t *aRawPowerSetting,
755                                           uint16_t       aRawPowerSettingLength)
756 {
757     OT_UNUSED_VARIABLE(aInstance);
758 
759     otError error;
760     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
761     int     nbytes;
762 
763     assert(aRawPowerSetting != nullptr);
764 
765     nbytes = snprintf(cmd, sizeof(cmd), "rawpowersetting ");
766 
767     for (uint16_t i = 0; i < aRawPowerSettingLength; i++)
768     {
769         nbytes += snprintf(cmd + nbytes, sizeof(cmd) - static_cast<size_t>(nbytes), "%02x", aRawPowerSetting[i]);
770         VerifyOrExit(nbytes < static_cast<int>(sizeof(cmd)), error = OT_ERROR_INVALID_ARGS);
771     }
772 
773     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
774 
775 exit:
776     return error;
777 }
778 
otPlatDiagRadioGetRawPowerSetting(otInstance * aInstance,uint8_t * aRawPowerSetting,uint16_t * aRawPowerSettingLength)779 otError otPlatDiagRadioGetRawPowerSetting(otInstance *aInstance,
780                                           uint8_t    *aRawPowerSetting,
781                                           uint16_t   *aRawPowerSettingLength)
782 {
783     OT_UNUSED_VARIABLE(aInstance);
784     otError error;
785     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
786     char    output[OPENTHREAD_CONFIG_DIAG_OUTPUT_BUFFER_SIZE];
787     char   *str;
788 
789     assert((aRawPowerSetting != nullptr) && (aRawPowerSettingLength != nullptr));
790 
791     setDiagOutput(output, sizeof(output));
792 
793     snprintf(cmd, sizeof(cmd), "rawpowersetting");
794     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
795     VerifyOrExit((str = strtok(output, "\r")) != nullptr, error = OT_ERROR_FAILED);
796     SuccessOrExit(error = ot::Utils::CmdLineParser::ParseAsHexString(str, *aRawPowerSettingLength, aRawPowerSetting));
797 
798 exit:
799     freeDiagOutput();
800 
801     return error;
802 }
803 
otPlatDiagRadioRawPowerSettingEnable(otInstance * aInstance,bool aEnable)804 otError otPlatDiagRadioRawPowerSettingEnable(otInstance *aInstance, bool aEnable)
805 {
806     OT_UNUSED_VARIABLE(aInstance);
807 
808     otError error;
809     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
810 
811     snprintf(cmd, sizeof(cmd), "rawpowersetting %s", aEnable ? "enable" : "disable");
812     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
813 
814 exit:
815     return error;
816 }
817 
otPlatDiagRadioTransmitCarrier(otInstance * aInstance,bool aEnable)818 otError otPlatDiagRadioTransmitCarrier(otInstance *aInstance, bool aEnable)
819 {
820     OT_UNUSED_VARIABLE(aInstance);
821 
822     otError error;
823     char    cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
824 
825     snprintf(cmd, sizeof(cmd), "cw %s", aEnable ? "start" : "stop");
826     SuccessOrExit(error = GetRadioSpinel().PlatDiagProcess(cmd));
827 
828 exit:
829     return error;
830 }
831 
otPlatDiagRadioTransmitStream(otInstance * aInstance,bool aEnable)832 otError otPlatDiagRadioTransmitStream(otInstance *aInstance, bool aEnable)
833 {
834     OT_UNUSED_VARIABLE(aInstance);
835 
836     char cmd[OPENTHREAD_CONFIG_DIAG_CMD_LINE_BUFFER_SIZE];
837 
838     snprintf(cmd, sizeof(cmd), "stream %s", aEnable ? "start" : "stop");
839     return GetRadioSpinel().PlatDiagProcess(cmd);
840 }
841 
otPlatDiagRadioReceived(otInstance * aInstance,otRadioFrame * aFrame,otError aError)842 void otPlatDiagRadioReceived(otInstance *aInstance, otRadioFrame *aFrame, otError aError)
843 {
844     OT_UNUSED_VARIABLE(aInstance);
845     OT_UNUSED_VARIABLE(aFrame);
846     OT_UNUSED_VARIABLE(aError);
847 }
848 
otPlatDiagAlarmCallback(otInstance * aInstance)849 void otPlatDiagAlarmCallback(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); }
850 #endif // OPENTHREAD_CONFIG_DIAG_ENABLE
851 
otPlatRadioGetSupportedChannelMask(otInstance * aInstance)852 uint32_t otPlatRadioGetSupportedChannelMask(otInstance *aInstance)
853 {
854     OT_UNUSED_VARIABLE(aInstance);
855 
856     uint32_t channelMask;
857 
858 #if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
859     if (sConfig.IsValid())
860     {
861         channelMask = sConfig.GetSupportedChannelMask();
862     }
863     else
864 #endif
865     {
866         channelMask = GetRadioSpinel().GetRadioChannelMask(false);
867     }
868 
869     return channelMask;
870 }
871 
otPlatRadioGetPreferredChannelMask(otInstance * aInstance)872 uint32_t otPlatRadioGetPreferredChannelMask(otInstance *aInstance)
873 {
874     OT_UNUSED_VARIABLE(aInstance);
875 
876     uint32_t channelMask;
877 
878 #if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
879     if (sConfig.IsValid())
880     {
881         channelMask = sConfig.GetPreferredChannelMask();
882     }
883     else
884 #endif
885     {
886         channelMask = GetRadioSpinel().GetRadioChannelMask(true);
887     }
888 
889     return channelMask;
890 }
891 
otPlatRadioGetState(otInstance * aInstance)892 otRadioState otPlatRadioGetState(otInstance *aInstance)
893 {
894     OT_UNUSED_VARIABLE(aInstance);
895     return GetRadioSpinel().GetState();
896 }
897 
otPlatRadioSetMacKey(otInstance * aInstance,uint8_t aKeyIdMode,uint8_t aKeyId,const otMacKeyMaterial * aPrevKey,const otMacKeyMaterial * aCurrKey,const otMacKeyMaterial * aNextKey,otRadioKeyType aKeyType)898 void otPlatRadioSetMacKey(otInstance             *aInstance,
899                           uint8_t                 aKeyIdMode,
900                           uint8_t                 aKeyId,
901                           const otMacKeyMaterial *aPrevKey,
902                           const otMacKeyMaterial *aCurrKey,
903                           const otMacKeyMaterial *aNextKey,
904                           otRadioKeyType          aKeyType)
905 {
906     SuccessOrDie(GetRadioSpinel().SetMacKey(aKeyIdMode, aKeyId, aPrevKey, aCurrKey, aNextKey));
907     OT_UNUSED_VARIABLE(aInstance);
908     OT_UNUSED_VARIABLE(aKeyType);
909 }
910 
otPlatRadioSetMacFrameCounter(otInstance * aInstance,uint32_t aMacFrameCounter)911 void otPlatRadioSetMacFrameCounter(otInstance *aInstance, uint32_t aMacFrameCounter)
912 {
913     SuccessOrDie(GetRadioSpinel().SetMacFrameCounter(aMacFrameCounter, /* aSetIfLarger */ false));
914     OT_UNUSED_VARIABLE(aInstance);
915 }
916 
otPlatRadioSetMacFrameCounterIfLarger(otInstance * aInstance,uint32_t aMacFrameCounter)917 void otPlatRadioSetMacFrameCounterIfLarger(otInstance *aInstance, uint32_t aMacFrameCounter)
918 {
919     SuccessOrDie(GetRadioSpinel().SetMacFrameCounter(aMacFrameCounter, /* aSetIfLarger */ true));
920     OT_UNUSED_VARIABLE(aInstance);
921 }
922 
otPlatRadioGetNow(otInstance * aInstance)923 uint64_t otPlatRadioGetNow(otInstance *aInstance)
924 {
925     OT_UNUSED_VARIABLE(aInstance);
926     return GetRadioSpinel().GetNow();
927 }
928 
otPlatRadioGetBusSpeed(otInstance * aInstance)929 uint32_t otPlatRadioGetBusSpeed(otInstance *aInstance)
930 {
931     OT_UNUSED_VARIABLE(aInstance);
932     return GetRadioSpinel().GetBusSpeed();
933 }
934 
otPlatRadioGetBusLatency(otInstance * aInstance)935 uint32_t otPlatRadioGetBusLatency(otInstance *aInstance)
936 {
937     OT_UNUSED_VARIABLE(aInstance);
938     return GetRadioSpinel().GetBusLatency();
939 }
940 
941 #if OPENTHREAD_CONFIG_MAC_CSL_RECEIVER_ENABLE || OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE
otPlatRadioGetCslAccuracy(otInstance * aInstance)942 uint8_t otPlatRadioGetCslAccuracy(otInstance *aInstance)
943 {
944     OT_UNUSED_VARIABLE(aInstance);
945 
946     return GetRadioSpinel().GetCslAccuracy();
947 }
948 #endif
949 
950 #if OPENTHREAD_CONFIG_MAC_CSL_TRANSMITTER_ENABLE
otPlatRadioGetCslUncertainty(otInstance * aInstance)951 uint8_t otPlatRadioGetCslUncertainty(otInstance *aInstance)
952 {
953     OT_UNUSED_VARIABLE(aInstance);
954 
955     return GetRadioSpinel().GetCslUncertainty();
956 }
957 #endif
958 
otPlatRadioSetChannelMaxTransmitPower(otInstance * aInstance,uint8_t aChannel,int8_t aMaxPower)959 otError otPlatRadioSetChannelMaxTransmitPower(otInstance *aInstance, uint8_t aChannel, int8_t aMaxPower)
960 {
961     OT_UNUSED_VARIABLE(aInstance);
962     return GetRadioSpinel().SetChannelMaxTransmitPower(aChannel, aMaxPower);
963 }
964 
965 #if OPENTHREAD_CONFIG_PLATFORM_POWER_CALIBRATION_ENABLE
otPlatRadioAddCalibratedPower(otInstance * aInstance,uint8_t aChannel,int16_t aActualPower,const uint8_t * aRawPowerSetting,uint16_t aRawPowerSettingLength)966 otError otPlatRadioAddCalibratedPower(otInstance    *aInstance,
967                                       uint8_t        aChannel,
968                                       int16_t        aActualPower,
969                                       const uint8_t *aRawPowerSetting,
970                                       uint16_t       aRawPowerSettingLength)
971 {
972     OT_UNUSED_VARIABLE(aInstance);
973     return GetRadioSpinel().AddCalibratedPower(aChannel, aActualPower, aRawPowerSetting, aRawPowerSettingLength);
974 }
975 
otPlatRadioClearCalibratedPowers(otInstance * aInstance)976 otError otPlatRadioClearCalibratedPowers(otInstance *aInstance)
977 {
978     OT_UNUSED_VARIABLE(aInstance);
979     return GetRadioSpinel().ClearCalibratedPowers();
980 }
981 
otPlatRadioSetChannelTargetPower(otInstance * aInstance,uint8_t aChannel,int16_t aTargetPower)982 otError otPlatRadioSetChannelTargetPower(otInstance *aInstance, uint8_t aChannel, int16_t aTargetPower)
983 {
984     OT_UNUSED_VARIABLE(aInstance);
985     return GetRadioSpinel().SetChannelTargetPower(aChannel, aTargetPower);
986 }
987 #endif
988 
otPlatRadioSetRegion(otInstance * aInstance,uint16_t aRegionCode)989 otError otPlatRadioSetRegion(otInstance *aInstance, uint16_t aRegionCode)
990 {
991     OT_UNUSED_VARIABLE(aInstance);
992 
993     otError error;
994 
995 #if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
996     if (sConfig.IsValid())
997     {
998         error = sConfig.SetRegion(aRegionCode);
999     }
1000     else
1001 #endif
1002     {
1003         error = GetRadioSpinel().SetRadioRegion(aRegionCode);
1004     }
1005 
1006     return error;
1007 }
1008 
otPlatRadioGetRegion(otInstance * aInstance,uint16_t * aRegionCode)1009 otError otPlatRadioGetRegion(otInstance *aInstance, uint16_t *aRegionCode)
1010 {
1011     OT_UNUSED_VARIABLE(aInstance);
1012 
1013     otError error;
1014 
1015 #if OPENTHREAD_POSIX_CONFIG_CONFIGURATION_FILE_ENABLE
1016     if (sConfig.IsValid())
1017     {
1018         *aRegionCode = sConfig.GetRegion();
1019         error        = OT_ERROR_NONE;
1020     }
1021     else
1022 #endif
1023     {
1024         error = GetRadioSpinel().GetRadioRegion(aRegionCode);
1025     }
1026 
1027     return error;
1028 }
1029 
1030 #if OPENTHREAD_CONFIG_MLE_LINK_METRICS_SUBJECT_ENABLE
otPlatRadioConfigureEnhAckProbing(otInstance * aInstance,otLinkMetrics aLinkMetrics,const otShortAddress aShortAddress,const otExtAddress * aExtAddress)1031 otError otPlatRadioConfigureEnhAckProbing(otInstance          *aInstance,
1032                                           otLinkMetrics        aLinkMetrics,
1033                                           const otShortAddress aShortAddress,
1034                                           const otExtAddress  *aExtAddress)
1035 {
1036     OT_UNUSED_VARIABLE(aInstance);
1037 
1038     return GetRadioSpinel().ConfigureEnhAckProbing(aLinkMetrics, aShortAddress, *aExtAddress);
1039 }
1040 #endif
1041 
otPlatRadioReceiveAt(otInstance * aInstance,uint8_t aChannel,uint32_t aStart,uint32_t aDuration)1042 otError otPlatRadioReceiveAt(otInstance *aInstance, uint8_t aChannel, uint32_t aStart, uint32_t aDuration)
1043 {
1044     OT_UNUSED_VARIABLE(aInstance);
1045     OT_UNUSED_VARIABLE(aChannel);
1046     OT_UNUSED_VARIABLE(aStart);
1047     OT_UNUSED_VARIABLE(aDuration);
1048     return OT_ERROR_NOT_IMPLEMENTED;
1049 }
1050 
1051 #if OPENTHREAD_CONFIG_PLATFORM_BOOTLOADER_MODE_ENABLE
otPlatResetToBootloader(otInstance * aInstance)1052 otError otPlatResetToBootloader(otInstance *aInstance)
1053 {
1054     OT_UNUSED_VARIABLE(aInstance);
1055 
1056     return GetRadioSpinel().SendReset(SPINEL_RESET_BOOTLOADER);
1057 }
1058 #endif
1059 
otSysGetRadioSpinelMetrics(void)1060 const otRadioSpinelMetrics *otSysGetRadioSpinelMetrics(void) { return GetRadioSpinel().GetRadioSpinelMetrics(); }
1061 
otSysGetRcpInterfaceMetrics(void)1062 const otRcpInterfaceMetrics *otSysGetRcpInterfaceMetrics(void)
1063 {
1064     return sRadio.GetSpinelInterface().GetRcpInterfaceMetrics();
1065 }
1066