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 extern void otAppNcpInit(otInstance *aInstance);
61 extern void otAppNcpInitMulti(otInstance **aInstances, uint8_t count);
62
63 #if OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE
otPlatCAlloc(size_t aNum,size_t aSize)64 OT_TOOL_WEAK void *otPlatCAlloc(size_t aNum, size_t aSize) { return calloc(aNum, aSize); }
65
otPlatFree(void * aPtr)66 OT_TOOL_WEAK void otPlatFree(void *aPtr) { free(aPtr); }
67 #endif
68
main(int argc,char * argv[])69 int main(int argc, char *argv[])
70 {
71 otInstance *instance;
72
73 OT_SETUP_RESET_JUMP(argv);
74
75 #ifdef __linux__
76 // Ensure we terminate this process if the
77 // parent process dies.
78 prctl(PR_SET_PDEATHSIG, SIGHUP);
79 #endif
80
81 #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
82 otInstance *instances[ENDPOINT_CT] = {NULL};
83 #elif OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
84 size_t otInstanceBufferLength = 0;
85 uint8_t *otInstanceBuffer = NULL;
86 #endif
87
88 pseudo_reset:
89
90 otSysInit(argc, argv);
91
92 #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
93 for (int i = 0; i < ENDPOINT_CT; i++)
94 {
95 instances[i] = otInstanceInitMultiple(i);
96
97 assert(instances[i]);
98 }
99 instance = instances[0];
100 #elif OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
101
102 // Call to query the buffer size
103 (void)otInstanceInit(NULL, &otInstanceBufferLength);
104
105 // Call to allocate the buffer
106 otInstanceBuffer = (uint8_t *)malloc(otInstanceBufferLength);
107 assert(otInstanceBuffer);
108
109 // Initialize OpenThread with the buffer
110 instance = otInstanceInit(otInstanceBuffer, &otInstanceBufferLength);
111 #else
112 instance = otInstanceInitSingle();
113 #endif
114 assert(instance);
115
116 #if OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
117 otAppNcpInitMulti(instances, ENDPOINT_CT);
118 #else
119 otAppNcpInit(instance);
120 #endif
121
122 while (!otSysPseudoResetWasRequested())
123 {
124 otTaskletsProcess(instance);
125 otSysProcessDrivers(instance);
126 }
127
128 otInstanceFinalize(instance);
129 #if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE && !OPENTHREAD_CONFIG_MULTIPAN_RCP_ENABLE
130 free(otInstanceBuffer);
131 #endif
132
133 goto pseudo_reset;
134
135 return 0;
136 }
137