xref: /aosp_15_r20/external/openthread/examples/apps/cli/main.c (revision cfb92d1480a9e65faed56933e9c12405f45898b4)
1*cfb92d14SAndroid Build Coastguard Worker /*
2*cfb92d14SAndroid Build Coastguard Worker  *  Copyright (c) 2016, The OpenThread Authors.
3*cfb92d14SAndroid Build Coastguard Worker  *  All rights reserved.
4*cfb92d14SAndroid Build Coastguard Worker  *
5*cfb92d14SAndroid Build Coastguard Worker  *  Redistribution and use in source and binary forms, with or without
6*cfb92d14SAndroid Build Coastguard Worker  *  modification, are permitted provided that the following conditions are met:
7*cfb92d14SAndroid Build Coastguard Worker  *  1. Redistributions of source code must retain the above copyright
8*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer.
9*cfb92d14SAndroid Build Coastguard Worker  *  2. Redistributions in binary form must reproduce the above copyright
10*cfb92d14SAndroid Build Coastguard Worker  *     notice, this list of conditions and the following disclaimer in the
11*cfb92d14SAndroid Build Coastguard Worker  *     documentation and/or other materials provided with the distribution.
12*cfb92d14SAndroid Build Coastguard Worker  *  3. Neither the name of the copyright holder nor the
13*cfb92d14SAndroid Build Coastguard Worker  *     names of its contributors may be used to endorse or promote products
14*cfb92d14SAndroid Build Coastguard Worker  *     derived from this software without specific prior written permission.
15*cfb92d14SAndroid Build Coastguard Worker  *
16*cfb92d14SAndroid Build Coastguard Worker  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17*cfb92d14SAndroid Build Coastguard Worker  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18*cfb92d14SAndroid Build Coastguard Worker  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19*cfb92d14SAndroid Build Coastguard Worker  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20*cfb92d14SAndroid Build Coastguard Worker  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21*cfb92d14SAndroid Build Coastguard Worker  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22*cfb92d14SAndroid Build Coastguard Worker  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23*cfb92d14SAndroid Build Coastguard Worker  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24*cfb92d14SAndroid Build Coastguard Worker  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25*cfb92d14SAndroid Build Coastguard Worker  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26*cfb92d14SAndroid Build Coastguard Worker  *  POSSIBILITY OF SUCH DAMAGE.
27*cfb92d14SAndroid Build Coastguard Worker  */
28*cfb92d14SAndroid Build Coastguard Worker 
29*cfb92d14SAndroid Build Coastguard Worker #include <assert.h>
30*cfb92d14SAndroid Build Coastguard Worker #ifdef __linux__
31*cfb92d14SAndroid Build Coastguard Worker #include <signal.h>
32*cfb92d14SAndroid Build Coastguard Worker #include <sys/prctl.h>
33*cfb92d14SAndroid Build Coastguard Worker #endif
34*cfb92d14SAndroid Build Coastguard Worker 
35*cfb92d14SAndroid Build Coastguard Worker #include <openthread-core-config.h>
36*cfb92d14SAndroid Build Coastguard Worker #include <openthread/config.h>
37*cfb92d14SAndroid Build Coastguard Worker 
38*cfb92d14SAndroid Build Coastguard Worker #include <openthread/cli.h>
39*cfb92d14SAndroid Build Coastguard Worker #include <openthread/diag.h>
40*cfb92d14SAndroid Build Coastguard Worker #include <openthread/tasklet.h>
41*cfb92d14SAndroid Build Coastguard Worker #include <openthread/platform/logging.h>
42*cfb92d14SAndroid Build Coastguard Worker #include <openthread/platform/misc.h>
43*cfb92d14SAndroid Build Coastguard Worker 
44*cfb92d14SAndroid Build Coastguard Worker #include "openthread-system.h"
45*cfb92d14SAndroid Build Coastguard Worker #include "cli/cli_config.h"
46*cfb92d14SAndroid Build Coastguard Worker #include "common/code_utils.hpp"
47*cfb92d14SAndroid Build Coastguard Worker 
48*cfb92d14SAndroid Build Coastguard Worker #include "lib/platform/reset_util.h"
49*cfb92d14SAndroid Build Coastguard Worker 
50*cfb92d14SAndroid Build Coastguard Worker /**
51*cfb92d14SAndroid Build Coastguard Worker  * Initializes the CLI app.
52*cfb92d14SAndroid Build Coastguard Worker  *
53*cfb92d14SAndroid Build Coastguard Worker  * @param[in]  aInstance  The OpenThread instance structure.
54*cfb92d14SAndroid Build Coastguard Worker  *
55*cfb92d14SAndroid Build Coastguard Worker  */
56*cfb92d14SAndroid Build Coastguard Worker extern void otAppCliInit(otInstance *aInstance);
57*cfb92d14SAndroid Build Coastguard Worker 
58*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
otPlatCAlloc(size_t aNum,size_t aSize)59*cfb92d14SAndroid Build Coastguard Worker OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
60*cfb92d14SAndroid Build Coastguard Worker 
otPlatFree(void * aPtr)61*cfb92d14SAndroid Build Coastguard Worker OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
62*cfb92d14SAndroid Build Coastguard Worker #endif
63*cfb92d14SAndroid Build Coastguard Worker 
otTaskletsSignalPending(otInstance * aInstance)64*cfb92d14SAndroid Build Coastguard Worker void otTaskletsSignalPending(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); }
65*cfb92d14SAndroid Build Coastguard Worker 
66*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_POSIX && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
ProcessExit(void * aContext,uint8_t aArgsLength,char * aArgs[])67*cfb92d14SAndroid Build Coastguard Worker static otError ProcessExit(void *aContext, uint8_t aArgsLength, char *aArgs[])
68*cfb92d14SAndroid Build Coastguard Worker {
69*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aContext);
70*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aArgsLength);
71*cfb92d14SAndroid Build Coastguard Worker     OT_UNUSED_VARIABLE(aArgs);
72*cfb92d14SAndroid Build Coastguard Worker 
73*cfb92d14SAndroid Build Coastguard Worker     exit(EXIT_SUCCESS);
74*cfb92d14SAndroid Build Coastguard Worker }
75*cfb92d14SAndroid Build Coastguard Worker 
76*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_EXAMPLES_SIMULATION
77*cfb92d14SAndroid Build Coastguard Worker extern otError ProcessNodeIdFilter(void *aContext, uint8_t aArgsLength, char *aArgs[]);
78*cfb92d14SAndroid Build Coastguard Worker #endif
79*cfb92d14SAndroid Build Coastguard Worker 
80*cfb92d14SAndroid Build Coastguard Worker static const otCliCommand kCommands[] = {
81*cfb92d14SAndroid Build Coastguard Worker     {"exit", ProcessExit},
82*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_EXAMPLES_SIMULATION
83*cfb92d14SAndroid Build Coastguard Worker     /*
84*cfb92d14SAndroid Build Coastguard Worker      * The CLI command `nodeidfilter` only works for simulation in real time.
85*cfb92d14SAndroid Build Coastguard Worker      *
86*cfb92d14SAndroid Build Coastguard Worker      * It can be used either as an allow list or a deny list. Once the filter is cleared, the first `nodeidfilter allow`
87*cfb92d14SAndroid Build Coastguard Worker      * or `nodeidfilter deny` will determine whether it is set up as an allow or deny list. Subsequent calls should
88*cfb92d14SAndroid Build Coastguard Worker      * use the same sub-command to add new node IDs, e.g., if we first call `nodeidfilter allow` (which sets the filter
89*cfb92d14SAndroid Build Coastguard Worker      * up  as an allow list), a subsequent `nodeidfilter deny` will result in `InvalidState` error.
90*cfb92d14SAndroid Build Coastguard Worker      *
91*cfb92d14SAndroid Build Coastguard Worker      * The usage of the command `nodeidfilter`:
92*cfb92d14SAndroid Build Coastguard Worker      *     - `nodeidfilter deny <nodeid>` :  It denies the connection to a specified node (use as deny-list).
93*cfb92d14SAndroid Build Coastguard Worker      *     - `nodeidfilter allow <nodeid> :  It allows the connection to a specified node (use as allow-list).
94*cfb92d14SAndroid Build Coastguard Worker      *     - `nodeidfilter clear`         :  It restores the filter state to default.
95*cfb92d14SAndroid Build Coastguard Worker      *     - `nodeidfilter`               :  Outputs filter mode (allow-list or deny-list) and filtered node IDs.
96*cfb92d14SAndroid Build Coastguard Worker      */
97*cfb92d14SAndroid Build Coastguard Worker     {"nodeidfilter", ProcessNodeIdFilter},
98*cfb92d14SAndroid Build Coastguard Worker #endif
99*cfb92d14SAndroid Build Coastguard Worker };
100*cfb92d14SAndroid Build Coastguard Worker #endif // OPENTHREAD_POSIX && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
101*cfb92d14SAndroid Build Coastguard Worker 
main(int argc,char * argv[])102*cfb92d14SAndroid Build Coastguard Worker int main(int argc, char *argv[])
103*cfb92d14SAndroid Build Coastguard Worker {
104*cfb92d14SAndroid Build Coastguard Worker     otInstance *instance;
105*cfb92d14SAndroid Build Coastguard Worker 
106*cfb92d14SAndroid Build Coastguard Worker #ifdef __linux__
107*cfb92d14SAndroid Build Coastguard Worker     // Ensure we terminate this process if the
108*cfb92d14SAndroid Build Coastguard Worker     // parent process dies.
109*cfb92d14SAndroid Build Coastguard Worker     prctl(PR_SET_PDEATHSIG, SIGHUP);
110*cfb92d14SAndroid Build Coastguard Worker #endif
111*cfb92d14SAndroid Build Coastguard Worker 
112*cfb92d14SAndroid Build Coastguard Worker     OT_SETUP_RESET_JUMP(argv);
113*cfb92d14SAndroid Build Coastguard Worker 
114*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
115*cfb92d14SAndroid Build Coastguard Worker     size_t   otInstanceBufferLength = 0;
116*cfb92d14SAndroid Build Coastguard Worker     uint8_t *otInstanceBuffer       = NULL;
117*cfb92d14SAndroid Build Coastguard Worker #endif
118*cfb92d14SAndroid Build Coastguard Worker 
119*cfb92d14SAndroid Build Coastguard Worker pseudo_reset:
120*cfb92d14SAndroid Build Coastguard Worker 
121*cfb92d14SAndroid Build Coastguard Worker     otSysInit(argc, argv);
122*cfb92d14SAndroid Build Coastguard Worker 
123*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
124*cfb92d14SAndroid Build Coastguard Worker     // Call to query the buffer size
125*cfb92d14SAndroid Build Coastguard Worker     (void)otInstanceInit(NULL, &otInstanceBufferLength);
126*cfb92d14SAndroid Build Coastguard Worker 
127*cfb92d14SAndroid Build Coastguard Worker     // Call to allocate the buffer
128*cfb92d14SAndroid Build Coastguard Worker     otInstanceBuffer = (uint8_t *)malloc(otInstanceBufferLength);
129*cfb92d14SAndroid Build Coastguard Worker     assert(otInstanceBuffer);
130*cfb92d14SAndroid Build Coastguard Worker 
131*cfb92d14SAndroid Build Coastguard Worker     // Initialize OpenThread with the buffer
132*cfb92d14SAndroid Build Coastguard Worker     instance = otInstanceInit(otInstanceBuffer, &otInstanceBufferLength);
133*cfb92d14SAndroid Build Coastguard Worker #else
134*cfb92d14SAndroid Build Coastguard Worker     instance = otInstanceInitSingle();
135*cfb92d14SAndroid Build Coastguard Worker #endif
136*cfb92d14SAndroid Build Coastguard Worker     assert(instance);
137*cfb92d14SAndroid Build Coastguard Worker 
138*cfb92d14SAndroid Build Coastguard Worker     otAppCliInit(instance);
139*cfb92d14SAndroid Build Coastguard Worker 
140*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_POSIX && !defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
141*cfb92d14SAndroid Build Coastguard Worker     IgnoreError(otCliSetUserCommands(kCommands, OT_ARRAY_LENGTH(kCommands), instance));
142*cfb92d14SAndroid Build Coastguard Worker #endif
143*cfb92d14SAndroid Build Coastguard Worker 
144*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_PLATFORM_LOG_CRASH_DUMP_ENABLE
145*cfb92d14SAndroid Build Coastguard Worker     IgnoreError(otPlatLogCrashDump());
146*cfb92d14SAndroid Build Coastguard Worker #endif
147*cfb92d14SAndroid Build Coastguard Worker 
148*cfb92d14SAndroid Build Coastguard Worker     while (!otSysPseudoResetWasRequested())
149*cfb92d14SAndroid Build Coastguard Worker     {
150*cfb92d14SAndroid Build Coastguard Worker         otTaskletsProcess(instance);
151*cfb92d14SAndroid Build Coastguard Worker         otSysProcessDrivers(instance);
152*cfb92d14SAndroid Build Coastguard Worker     }
153*cfb92d14SAndroid Build Coastguard Worker 
154*cfb92d14SAndroid Build Coastguard Worker     otInstanceFinalize(instance);
155*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
156*cfb92d14SAndroid Build Coastguard Worker     free(otInstanceBuffer);
157*cfb92d14SAndroid Build Coastguard Worker #endif
158*cfb92d14SAndroid Build Coastguard Worker 
159*cfb92d14SAndroid Build Coastguard Worker     goto pseudo_reset;
160*cfb92d14SAndroid Build Coastguard Worker 
161*cfb92d14SAndroid Build Coastguard Worker     return 0;
162*cfb92d14SAndroid Build Coastguard Worker }
163*cfb92d14SAndroid Build Coastguard Worker 
164*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP
otPlatLog(otLogLevel aLogLevel,otLogRegion aLogRegion,const char * aFormat,...)165*cfb92d14SAndroid Build Coastguard Worker void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
166*cfb92d14SAndroid Build Coastguard Worker {
167*cfb92d14SAndroid Build Coastguard Worker     va_list ap;
168*cfb92d14SAndroid Build Coastguard Worker 
169*cfb92d14SAndroid Build Coastguard Worker     va_start(ap, aFormat);
170*cfb92d14SAndroid Build Coastguard Worker     otCliPlatLogv(aLogLevel, aLogRegion, aFormat, ap);
171*cfb92d14SAndroid Build Coastguard Worker     va_end(ap);
172*cfb92d14SAndroid Build Coastguard Worker }
173*cfb92d14SAndroid Build Coastguard Worker #endif
174