xref: /aosp_15_r20/external/openthread/src/posix/platform/radio_url.cpp (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
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 #include "posix/platform/radio_url.hpp"
30 
31 #include <stdio.h>
32 
33 #include <openthread/openthread-system.h>
34 
35 #include "core/common/code_utils.hpp"
36 #include "posix/platform/platform-posix.h"
37 
otSysGetRadioUrlHelpString(void)38 const char *otSysGetRadioUrlHelpString(void)
39 {
40 #define OT_RADIO_URL_HELP_BUS                            \
41     "Radio Url format:"                                  \
42     "    {Protocol}://${PATH_TO_DEVICE}?${Parameters}\n" \
43     "\n"
44 
45 #if OPENTHREAD_POSIX_CONFIG_SPINEL_SPI_INTERFACE_ENABLE
46 #define OT_SPINEL_SPI_RADIO_URL_HELP_BUS                                                                       \
47     "Protocol=[spinel+spi*]           Specify the Spinel interface as the Spinel SPI interface\n"              \
48     "    spinel+spi://${PATH_TO_SPI_DEVICE}?${Parameters}\n"                                                   \
49     "Parameters:\n"                                                                                            \
50     "    gpio-int-device[=gpio-device-path]\n"                                                                 \
51     "                                  Specify a path to the Linux sysfs-exported GPIO device for the\n"       \
52     "                                  `I̅N̅T̅` pin. If not specified, `SPI` interface will fall back to\n" \
53     "                                  polling, which is inefficient.\n"                                       \
54     "    gpio-int-line[=line-offset]\n"                                                                        \
55     "                                  The offset index of `I̅N̅T̅` pin for the associated GPIO device.\n"  \
56     "                                  If not specified, `SPI` interface will fall back to polling,\n"         \
57     "                                  which is inefficient.\n"                                                \
58     "    gpio-reset-dev[=gpio-device-path]\n"                                                                  \
59     "                                  Specify a path to the Linux sysfs-exported GPIO device for the\n"       \
60     "                                  `R̅E̅S̅` pin.\n"                                                     \
61     "    gpio-reset-line[=line-offset]"                                                                        \
62     "                                  The offset index of `R̅E̅S̅` pin for the associated GPIO device.\n"  \
63     "    spi-mode[=mode]               Specify the SPI mode to use (0-3).\n"                                   \
64     "    spi-speed[=hertz]             Specify the SPI speed in hertz.\n"                                      \
65     "    spi-cs-delay[=usec]           Specify the delay after C̅S̅ assertion, in µsec.\n"                  \
66     "    spi-reset-delay[=ms]          Specify the delay after R̅E̅S̅E̅T̅ assertion, in milliseconds.\n"  \
67     "    spi-align-allowance[=n]       Specify the maximum number of 0xFF bytes to clip from start of\n"       \
68     "                                  MISO frame. Max value is 16.\n"                                         \
69     "    spi-small-packet=[n]          Specify the smallest packet we can receive in a single transaction.\n"  \
70     "                                  (larger packets will require two transactions). Default value is 32.\n" \
71     "\n"
72 #else
73 #define OT_SPINEL_SPI_RADIO_URL_HELP_BUS
74 #endif // OPENTHREAD_POSIX_CONFIG_SPINEL_SPI_INTERFACE_ENABLE
75 
76 #if OPENTHREAD_POSIX_CONFIG_SPINEL_HDLC_INTERFACE_ENABLE
77 #define OT_SPINEL_HDLC_RADIO_URL_HELP_BUS                                                            \
78     "Protocol=[spinel+hdlc*]           Specify the Spinel interface as the Spinel HDLC interface\n"  \
79     "    forkpty-arg[=argument string]  Command line arguments for subprocess, can be repeated.\n"   \
80     "    spinel+hdlc+uart://${PATH_TO_UART_DEVICE}?${Parameters} for real uart device\n"             \
81     "    spinel+hdlc+forkpty://${PATH_TO_UART_DEVICE}?${Parameters} for forking a pty subprocess.\n" \
82     "Parameters:\n"                                                                                  \
83     "    uart-parity[=even|odd]         Uart parity config, optional.\n"                             \
84     "    uart-stop[=number-of-bits]     Uart stop bit, default is 1.\n"                              \
85     "    uart-baudrate[=baudrate]       Uart baud rate, default is 115200.\n"                        \
86     "    uart-flow-control              Enable flow control, disabled by default.\n"                 \
87     "    uart-init-deassert             Deassert lines on init when flow control is disabled.\n"     \
88     "    uart-reset                     Reset connection after hard resetting RCP(USB CDC ACM).\n"   \
89     "\n"
90 #else
91 #define OT_SPINEL_HDLC_RADIO_URL_HELP_BUS
92 #endif // OPENTHREAD_POSIX_CONFIG_SPINEL_HDLC_INTERFACE_ENABLE
93 
94 #if OPENTHREAD_POSIX_CONFIG_SPINEL_VENDOR_INTERFACE_ENABLE
95 
96 #ifndef OT_VENDOR_RADIO_URL_HELP_BUS
97 #define OT_VENDOR_RADIO_URL_HELP_BUS "\n"
98 #endif // OT_VENDOR_RADIO_URL_HELP_BUS
99 
100 #define OT_SPINEL_VENDOR_RADIO_URL_HELP_BUS OT_VENDOR_RADIO_URL_HELP_BUS
101 #else
102 #define OT_SPINEL_VENDOR_RADIO_URL_HELP_BUS
103 #endif // OPENTHREAD_POSIX_CONFIG_SPINEL_VENDOR_INTERFACE_ENABLE
104 
105 #if OPENTHREAD_POSIX_CONFIG_MAX_POWER_TABLE_ENABLE
106 #define OT_RADIO_URL_HELP_MAX_POWER_TABLE                                                                  \
107     "    max-power-table               Max power for channels in ascending order separated by commas,\n"   \
108     "                                  If the number of values is less than that of supported channels,\n" \
109     "                                  the last value will be applied to all remaining channels.\n"        \
110     "                                  Special value 0x7f disables a channel.\n"
111 #else
112 #define OT_RADIO_URL_HELP_MAX_POWER_TABLE
113 #endif
114 
115     return "RadioURL:\n" OT_RADIO_URL_HELP_BUS OT_SPINEL_SPI_RADIO_URL_HELP_BUS OT_SPINEL_HDLC_RADIO_URL_HELP_BUS
116         OT_SPINEL_VENDOR_RADIO_URL_HELP_BUS OT_RADIO_URL_HELP_MAX_POWER_TABLE
117            "    region[=region-code]          Set the radio's region code. The region code must be an\n"
118            "                                  ISO 3166 alpha-2 code.\n"
119            "    cca-threshold[=dbm]           Set the radio's CCA ED threshold in dBm measured at antenna connector.\n"
120            "    enable-coex[=1|0]             If not specified, RCP coex operates with its default configuration.\n"
121            "                                  Disable coex with 0, and enable it with other values.\n"
122            "    fem-lnagain[=dbm]             Set the Rx LNA gain in dBm of the external FEM.\n"
123            "    no-reset                      Do not send Spinel reset command to RCP on initialization.\n"
124            "    skip-rcp-compatibility-check  Skip checking RCP API version and capabilities during initialization.\n"
125            "    bus-latency[=usec]            Communication latency in usec, default is 0.\n"
126 #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
127            "    iid                           Set the Spinel Interface ID for this process. Valid values are 0-3.\n"
128            "    iid-list                      List of IIDs a host can subscribe to receive spinel frames other than \n"
129            "                                  provided in 'iid' argument. If not specified, host will subscribe to \n"
130            "                                  the interface ID provided in 'iid` argument. Valid values are 0-3. \n"
131            "                                  Upto three IIDs can be provided with each IID separated by ',' \n"
132            "                                  e.g. iid-list=1,2,3 \n"
133 #endif
134         ;
135 }
136 
137 namespace ot {
138 namespace Posix {
139 
Init(const char * aUrl)140 void RadioUrl::Init(const char *aUrl)
141 {
142     if (aUrl != nullptr)
143     {
144         VerifyOrDie(strnlen(aUrl, sizeof(mUrl)) < sizeof(mUrl), OT_EXIT_INVALID_ARGUMENTS);
145         strncpy(mUrl, aUrl, sizeof(mUrl) - 1);
146         SuccessOrDie(Url::Url::Init(mUrl));
147     }
148 }
149 
150 } // namespace Posix
151 } // namespace ot
152