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 #if OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI
41 #define OT_RADIO_URL_HELP_BUS                                                                                  \
42     "    spinel+spi://${PATH_TO_SPI_DEVICE}?${Parameters}\n"                                                   \
43     "Parameters:\n"                                                                                            \
44     "    gpio-int-device[=gpio-device-path]\n"                                                                 \
45     "                                  Specify a path to the Linux sysfs-exported GPIO device for the\n"       \
46     "                                  `I̅N̅T̅` pin. If not specified, `SPI` interface will fall back to\n" \
47     "                                  polling, which is inefficient.\n"                                       \
48     "    gpio-int-line[=line-offset]\n"                                                                        \
49     "                                  The offset index of `I̅N̅T̅` pin for the associated GPIO device.\n"  \
50     "                                  If not specified, `SPI` interface will fall back to polling,\n"         \
51     "                                  which is inefficient.\n"                                                \
52     "    gpio-reset-dev[=gpio-device-path]\n"                                                                  \
53     "                                  Specify a path to the Linux sysfs-exported GPIO device for the\n"       \
54     "                                  `R̅E̅S̅` pin.\n"                                                     \
55     "    gpio-reset-line[=line-offset]"                                                                        \
56     "                                  The offset index of `R̅E̅S̅` pin for the associated GPIO device.\n"  \
57     "    spi-mode[=mode]               Specify the SPI mode to use (0-3).\n"                                   \
58     "    spi-speed[=hertz]             Specify the SPI speed in hertz.\n"                                      \
59     "    spi-cs-delay[=usec]           Specify the delay after C̅S̅ assertion, in µsec.\n"                  \
60     "    spi-reset-delay[=ms]          Specify the delay after R̅E̅S̅E̅T̅ assertion, in milliseconds.\n"  \
61     "    spi-align-allowance[=n]       Specify the maximum number of 0xFF bytes to clip from start of\n"       \
62     "                                  MISO frame. Max value is 16.\n"                                         \
63     "    spi-small-packet=[n]          Specify the smallest packet we can receive in a single transaction.\n"  \
64     "                                  (larger packets will require two transactions). Default value is 32.\n"
65 
66 #else
67 
68 #define OT_RADIO_URL_HELP_BUS                                                                        \
69     "    forkpty-arg[=argument string]  Command line arguments for subprocess, can be repeated.\n"   \
70     "    spinel+hdlc+uart://${PATH_TO_UART_DEVICE}?${Parameters} for real uart device\n"             \
71     "    spinel+hdlc+forkpty://${PATH_TO_UART_DEVICE}?${Parameters} for forking a pty subprocess.\n" \
72     "Parameters:\n"                                                                                  \
73     "    uart-parity[=even|odd]         Uart parity config, optional.\n"                             \
74     "    uart-stop[=number-of-bits]     Uart stop bit, default is 1.\n"                              \
75     "    uart-baudrate[=baudrate]       Uart baud rate, default is 115200.\n"                        \
76     "    uart-flow-control              Enable flow control, disabled by default.\n"
77 
78 #endif // OPENTHREAD_POSIX_CONFIG_RCP_BUS == OT_POSIX_RCP_BUS_SPI
79 
80 #if OPENTHREAD_POSIX_CONFIG_MAX_POWER_TABLE_ENABLE
81 #define OT_RADIO_URL_HELP_MAX_POWER_TABLE                                                                  \
82     "    max-power-table               Max power for channels in ascending order separated by commas,\n"   \
83     "                                  If the number of values is less than that of supported channels,\n" \
84     "                                  the last value will be applied to all remaining channels.\n"        \
85     "                                  Special value 0x7f disables a channel.\n"
86 #else
87 #define OT_RADIO_URL_HELP_MAX_POWER_TABLE
88 #endif
89 
90     return "RadioURL:\n" OT_RADIO_URL_HELP_BUS OT_RADIO_URL_HELP_MAX_POWER_TABLE
91            "    region[=region-code]          Set the radio's region code.\n"
92            "    cca-threshold[=dbm]           Set the radio's CCA ED threshold in dBm measured at antenna connector.\n"
93            "    fem-lnagain[=dbm]             Set the Rx LNA gain in dBm of the external FEM.\n"
94            "    ncp-dataset                   Retrieve dataset from ncp.\n"
95            "    no-reset                      Do not send Spinel reset command to RCP on initialization.\n"
96            "    skip-rcp-compatibility-check  Skip checking RCP API version and capabilities during initialization.\n";
97 }
98 
99 namespace ot {
100 namespace Posix {
101 
RadioUrl(const char * aUrl)102 RadioUrl::RadioUrl(const char *aUrl)
103 {
104     VerifyOrDie(strnlen(aUrl, sizeof(mUrl)) < sizeof(mUrl), OT_EXIT_INVALID_ARGUMENTS);
105     strncpy(mUrl, aUrl, sizeof(mUrl) - 1);
106     SuccessOrDie(Url::Url::Init(mUrl));
107 }
108 
109 } // namespace Posix
110 } // namespace ot
111