xref: /aosp_15_r20/external/openthread/examples/apps/ncp/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/diag.h>
39*cfb92d14SAndroid Build Coastguard Worker #include <openthread/ncp.h>
40*cfb92d14SAndroid Build Coastguard Worker #include <openthread/tasklet.h>
41*cfb92d14SAndroid Build Coastguard Worker 
42*cfb92d14SAndroid Build Coastguard Worker #include "openthread-system.h"
43*cfb92d14SAndroid Build Coastguard Worker 
44*cfb92d14SAndroid Build Coastguard Worker #include "lib/platform/reset_util.h"
45*cfb92d14SAndroid Build Coastguard Worker 
46*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
47*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPLE_STATIC_INSTANCE_ENABLE == 0
48*cfb92d14SAndroid Build Coastguard Worker #error "Support for multiple OpenThread static instance is disabled."
49*cfb92d14SAndroid Build Coastguard Worker #endif
50*cfb92d14SAndroid Build Coastguard Worker #define ENDPOINT_CT OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_NUM
51*cfb92d14SAndroid Build Coastguard Worker #else
52*cfb92d14SAndroid Build Coastguard Worker #define ENDPOINT_CT 1
53*cfb92d14SAndroid Build Coastguard Worker #endif /* OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE */
54*cfb92d14SAndroid Build Coastguard Worker 
55*cfb92d14SAndroid Build Coastguard Worker /**
56*cfb92d14SAndroid Build Coastguard Worker  * Initializes the NCP app.
57*cfb92d14SAndroid Build Coastguard Worker  *
58*cfb92d14SAndroid Build Coastguard Worker  * @param[in]  aInstance  The OpenThread instance structure.
59*cfb92d14SAndroid Build Coastguard Worker  *
60*cfb92d14SAndroid Build Coastguard Worker  */
61*cfb92d14SAndroid Build Coastguard Worker extern void otAppNcpInit(otInstance *aInstance);
62*cfb92d14SAndroid Build Coastguard Worker extern void otAppNcpInitMulti(otInstance **aInstances, uint8_t count);
63*cfb92d14SAndroid Build Coastguard Worker 
64*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
otPlatCAlloc(size_t aNum,size_t aSize)65*cfb92d14SAndroid Build Coastguard Worker OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
66*cfb92d14SAndroid Build Coastguard Worker 
otPlatFree(void * aPtr)67*cfb92d14SAndroid Build Coastguard Worker OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
68*cfb92d14SAndroid Build Coastguard Worker #endif
69*cfb92d14SAndroid Build Coastguard Worker 
otTaskletsSignalPending(otInstance * aInstance)70*cfb92d14SAndroid Build Coastguard Worker void otTaskletsSignalPending(otInstance *aInstance) { OT_UNUSED_VARIABLE(aInstance); }
71*cfb92d14SAndroid Build Coastguard Worker 
main(int argc,char * argv[])72*cfb92d14SAndroid Build Coastguard Worker int main(int argc, char *argv[])
73*cfb92d14SAndroid Build Coastguard Worker {
74*cfb92d14SAndroid Build Coastguard Worker     otInstance *instance;
75*cfb92d14SAndroid Build Coastguard Worker 
76*cfb92d14SAndroid Build Coastguard Worker     OT_SETUP_RESET_JUMP(argv);
77*cfb92d14SAndroid Build Coastguard Worker 
78*cfb92d14SAndroid Build Coastguard Worker #ifdef __linux__
79*cfb92d14SAndroid Build Coastguard Worker     // Ensure we terminate this process if the
80*cfb92d14SAndroid Build Coastguard Worker     // parent process dies.
81*cfb92d14SAndroid Build Coastguard Worker     prctl(PR_SET_PDEATHSIG, SIGHUP);
82*cfb92d14SAndroid Build Coastguard Worker #endif
83*cfb92d14SAndroid Build Coastguard Worker 
84*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
85*cfb92d14SAndroid Build Coastguard Worker     otInstance *instances[ENDPOINT_CT] = {NULL};
86*cfb92d14SAndroid Build Coastguard Worker #elif OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
87*cfb92d14SAndroid Build Coastguard Worker     size_t   otInstanceBufferLength = 0;
88*cfb92d14SAndroid Build Coastguard Worker     uint8_t *otInstanceBuffer       = NULL;
89*cfb92d14SAndroid Build Coastguard Worker #endif
90*cfb92d14SAndroid Build Coastguard Worker 
91*cfb92d14SAndroid Build Coastguard Worker pseudo_reset:
92*cfb92d14SAndroid Build Coastguard Worker 
93*cfb92d14SAndroid Build Coastguard Worker     otSysInit(argc, argv);
94*cfb92d14SAndroid Build Coastguard Worker 
95*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
96*cfb92d14SAndroid Build Coastguard Worker     for (int i = 0; i < ENDPOINT_CT; i++)
97*cfb92d14SAndroid Build Coastguard Worker     {
98*cfb92d14SAndroid Build Coastguard Worker         instances[i] = otInstanceInitMultiple(i);
99*cfb92d14SAndroid Build Coastguard Worker 
100*cfb92d14SAndroid Build Coastguard Worker         assert(instances[i]);
101*cfb92d14SAndroid Build Coastguard Worker     }
102*cfb92d14SAndroid Build Coastguard Worker     instance = instances[0];
103*cfb92d14SAndroid Build Coastguard Worker #elif OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
104*cfb92d14SAndroid Build Coastguard Worker 
105*cfb92d14SAndroid Build Coastguard Worker     // Call to query the buffer size
106*cfb92d14SAndroid Build Coastguard Worker     (void)otInstanceInit(NULL, &otInstanceBufferLength);
107*cfb92d14SAndroid Build Coastguard Worker 
108*cfb92d14SAndroid Build Coastguard Worker     // Call to allocate the buffer
109*cfb92d14SAndroid Build Coastguard Worker     otInstanceBuffer = (uint8_t *)malloc(otInstanceBufferLength);
110*cfb92d14SAndroid Build Coastguard Worker     assert(otInstanceBuffer);
111*cfb92d14SAndroid Build Coastguard Worker 
112*cfb92d14SAndroid Build Coastguard Worker     // Initialize OpenThread with the buffer
113*cfb92d14SAndroid Build Coastguard Worker     instance = otInstanceInit(otInstanceBuffer, &otInstanceBufferLength);
114*cfb92d14SAndroid Build Coastguard Worker #else
115*cfb92d14SAndroid Build Coastguard Worker     instance = otInstanceInitSingle();
116*cfb92d14SAndroid Build Coastguard Worker #endif
117*cfb92d14SAndroid Build Coastguard Worker     assert(instance);
118*cfb92d14SAndroid Build Coastguard Worker 
119*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
120*cfb92d14SAndroid Build Coastguard Worker     otAppNcpInitMulti(instances, ENDPOINT_CT);
121*cfb92d14SAndroid Build Coastguard Worker #else
122*cfb92d14SAndroid Build Coastguard Worker     otAppNcpInit(instance);
123*cfb92d14SAndroid Build Coastguard Worker #endif
124*cfb92d14SAndroid Build Coastguard Worker 
125*cfb92d14SAndroid Build Coastguard Worker     while (!otSysPseudoResetWasRequested())
126*cfb92d14SAndroid Build Coastguard Worker     {
127*cfb92d14SAndroid Build Coastguard Worker         otTaskletsProcess(instance);
128*cfb92d14SAndroid Build Coastguard Worker         otSysProcessDrivers(instance);
129*cfb92d14SAndroid Build Coastguard Worker     }
130*cfb92d14SAndroid Build Coastguard Worker 
131*cfb92d14SAndroid Build Coastguard Worker     otInstanceFinalize(instance);
132*cfb92d14SAndroid Build Coastguard Worker #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE && !OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
133*cfb92d14SAndroid Build Coastguard Worker     free(otInstanceBuffer);
134*cfb92d14SAndroid Build Coastguard Worker #endif
135*cfb92d14SAndroid Build Coastguard Worker 
136*cfb92d14SAndroid Build Coastguard Worker     goto pseudo_reset;
137*cfb92d14SAndroid Build Coastguard Worker 
138*cfb92d14SAndroid Build Coastguard Worker     return 0;
139*cfb92d14SAndroid Build Coastguard Worker }
140