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