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
#include <openthread-core-config.h>
31
#include <openthread/config.h>
32

33
#include <openthread/cli.h>
34
#include <openthread/diag.h>
35
#include <openthread/tasklet.h>
36
#include <openthread/platform/logging.h>
37

38
#include "openthread-system.h"
39
#include "cli/cli_config.h"
40

41
#if OPENTHREAD_EXAMPLES_SIMULATION
42
#include <setjmp.h>
43
#include <unistd.h>
44

45
jmp_buf gResetJump;
46

47
void __gcov_flush();
48
#endif
49

50
#ifndef OPENTHREAD_ENABLE_COVERAGE
51
#define OPENTHREAD_ENABLE_COVERAGE 0
52
#endif
53

54
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
55
void *otPlatCAlloc(size_t aNum, size_t aSize)
56
{
57
    return calloc(aNum, aSize);
58
}
59

60
void otPlatFree(void *aPtr)
61
{
62
    free(aPtr);
63
}
64
#endif
65

66 14
void otTaskletsSignalPending(otInstance *aInstance)
67
{
68 0
    OT_UNUSED_VARIABLE(aInstance);
69
}
70

71 14
int main(int argc, char *argv[])
72
{
73
    otInstance *instance;
74

75
#if OPENTHREAD_EXAMPLES_SIMULATION
76 14
    if (setjmp(gResetJump))
77
    {
78 6
        alarm(0);
79
#if OPENTHREAD_ENABLE_COVERAGE
80 6
        __gcov_flush();
81
#endif
82 0
        execvp(argv[0], argv);
83
    }
84
#endif
85

86
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
87
    size_t   otInstanceBufferLength = 0;
88
    uint8_t *otInstanceBuffer       = NULL;
89
#endif
90

91 14
pseudo_reset:
92

93 14
    otSysInit(argc, argv);
94

95
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
96
    // Call to query the buffer size
97
    (void)otInstanceInit(NULL, &otInstanceBufferLength);
98

99
    // Call to allocate the buffer
100
    otInstanceBuffer = (uint8_t *)malloc(otInstanceBufferLength);
101
    assert(otInstanceBuffer);
102

103
    // Initialize OpenThread with the buffer
104
    instance = otInstanceInit(otInstanceBuffer, &otInstanceBufferLength);
105
#else
106 14
    instance = otInstanceInitSingle();
107
#endif
108 14
    assert(instance);
109

110
#if OPENTHREAD_CONFIG_CLI_TRANSPORT == OT_CLI_TRANSPORT_UART
111 14
    otCliUartInit(instance);
112
#endif
113

114 14
    while (!otSysPseudoResetWasRequested())
115
    {
116 14
        otTaskletsProcess(instance);
117 14
        otSysProcessDrivers(instance);
118
    }
119

120 0
    otInstanceFinalize(instance);
121
#if OPENTHREAD_CONFIG_MULTIPLE_INSTANCE_ENABLE
122
    free(otInstanceBuffer);
123
#endif
124

125 0
    goto pseudo_reset;
126

127
    return 0;
128
}
129

130
/*
131
 * Provide, if required an "otPlatLog()" function
132
 */
133
#if OPENTHREAD_CONFIG_LOG_OUTPUT == OPENTHREAD_CONFIG_LOG_OUTPUT_APP
134 5
void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...)
135
{
136 0
    OT_UNUSED_VARIABLE(aLogLevel);
137 0
    OT_UNUSED_VARIABLE(aLogRegion);
138 0
    OT_UNUSED_VARIABLE(aFormat);
139

140
    va_list ap;
141 5
    va_start(ap, aFormat);
142 5
    otCliPlatLogv(aLogLevel, aLogRegion, aFormat, ap);
143 5
    va_end(ap);
144
}
145
#endif

Read our documentation on viewing source code .

Loading