/src/openthread/tests/nexus/platform/nexus_misc.cpp
Line | Count | Source |
1 | | /* |
2 | | * Copyright (c) 2024, 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 <stdio.h> |
30 | | #include <stdlib.h> |
31 | | |
32 | | #include <openthread/platform/entropy.h> |
33 | | #include <openthread/platform/logging.h> |
34 | | #include <openthread/platform/misc.h> |
35 | | |
36 | | #include "nexus_core.hpp" |
37 | | #include "nexus_node.hpp" |
38 | | |
39 | | namespace ot { |
40 | | namespace Nexus { |
41 | | |
42 | | static void LogVarArgs(Node *aActiveNode, const char *aFormat, va_list aArgs); |
43 | | |
44 | | extern "C" { |
45 | | |
46 | | //--------------------------------------------------------------------------------------------------------------------- |
47 | | // otTasklets |
48 | | |
49 | | void otTaskletsSignalPending(otInstance *aInstance) |
50 | | { |
51 | | OT_UNUSED_VARIABLE(aInstance); |
52 | | |
53 | | Core::Get().MarkPendingAction(); |
54 | | } |
55 | | |
56 | | //--------------------------------------------------------------------------------------------------------------------- |
57 | | // otPlatLog |
58 | | |
59 | | void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char *aFormat, ...) |
60 | 9.76M | { |
61 | 9.76M | OT_UNUSED_VARIABLE(aLogLevel); |
62 | 9.76M | OT_UNUSED_VARIABLE(aLogRegion); |
63 | | |
64 | 9.76M | va_list args; |
65 | | |
66 | 9.76M | va_start(args, aFormat); |
67 | 9.76M | LogVarArgs(Core::Get().GetActiveNode(), aFormat, args); |
68 | 9.76M | va_end(args); |
69 | 9.76M | } |
70 | | |
71 | | //--------------------------------------------------------------------------------------------------------------------- |
72 | | // Heap allocation APIs |
73 | | |
74 | | void *otPlatCAlloc(size_t aNum, size_t aSize) |
75 | 10.5M | { |
76 | 10.5M | void *ptr = calloc(aNum, aSize); |
77 | 10.5M | return ptr; |
78 | 10.5M | } |
79 | | |
80 | 13.1M | void otPlatFree(void *aPtr) { free(aPtr); } |
81 | | |
82 | | //--------------------------------------------------------------------------------------------------------------------- |
83 | | // Entropy |
84 | | |
85 | | otError otPlatEntropyGet(uint8_t *aOutput, uint16_t aOutputLength) |
86 | 28.3k | { |
87 | | #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION |
88 | | Error error = OT_ERROR_NONE; |
89 | | FILE *file = nullptr; |
90 | | size_t readLength; |
91 | | |
92 | | file = fopen("/dev/urandom", "rb"); |
93 | | VerifyOrExit(file != nullptr, error = kErrorFailed); |
94 | | |
95 | | readLength = fread(aOutput, 1, aOutputLength, file); |
96 | | |
97 | | if (readLength != aOutputLength) |
98 | | { |
99 | | error = kErrorFailed; |
100 | | } |
101 | | |
102 | | fclose(file); |
103 | | |
104 | | exit: |
105 | | return error; |
106 | | #else |
107 | 3.65M | for (uint16_t length = 0; length < aOutputLength; length++) |
108 | 3.62M | { |
109 | 3.62M | aOutput[length] = (uint8_t)rand(); |
110 | 3.62M | } |
111 | | |
112 | 28.3k | return OT_ERROR_NONE; |
113 | 28.3k | #endif |
114 | 28.3k | } |
115 | | |
116 | | //--------------------------------------------------------------------------------------------------------------------- |
117 | | // Misc |
118 | | |
119 | 0 | otError otPlatDiagProcess(otInstance *, uint8_t, char *[]) { return kErrorNotImplemented; } |
120 | 0 | void otPlatDiagModeSet(bool) {} |
121 | 0 | bool otPlatDiagModeGet() { return false; } |
122 | 0 | void otPlatDiagChannelSet(uint8_t) {} |
123 | 0 | void otPlatDiagTxPowerSet(int8_t) {} |
124 | 0 | void otPlatDiagRadioReceived(otInstance *, otRadioFrame *, otError) {} |
125 | 0 | void otPlatDiagAlarmCallback(otInstance *) {} |
126 | 0 | void otPlatUartSendDone(void) {} |
127 | 0 | void otPlatUartReceived(const uint8_t *, uint16_t) {} |
128 | 2 | void otPlatReset(otInstance *) {} |
129 | 0 | otError otPlatResetToBootloader(otInstance *) { return kErrorNotImplemented; } |
130 | 0 | otPlatResetReason otPlatGetResetReason(otInstance *) { return OT_PLAT_RESET_REASON_POWER_ON; } |
131 | 0 | void otPlatWakeHost(void) {} |
132 | | |
133 | | } // extern "C" |
134 | | |
135 | | //--------------------------------------------------------------------------------------------------------------------- |
136 | | // Log related function |
137 | | |
138 | | void Log(const char *aFormat, ...) |
139 | 113k | { |
140 | 113k | va_list args; |
141 | | |
142 | 113k | va_start(args, aFormat); |
143 | 113k | LogVarArgs(nullptr, aFormat, args); |
144 | 113k | va_end(args); |
145 | 113k | } |
146 | | |
147 | | static void LogVarArgs(Node *aActiveNode, const char *aFormat, va_list aArgs) |
148 | 9.87M | { |
149 | 9.87M | uint32_t now = Core::Get().GetNow().GetValue(); |
150 | | |
151 | 9.87M | printf("%02u:%02u:%02u.%03u ", now / 3600000, (now / 60000) % 60, (now / 1000) % 60, now % 1000); |
152 | | |
153 | 9.87M | if (aActiveNode != nullptr) |
154 | 9.76M | { |
155 | 9.76M | printf("%03u ", aActiveNode->GetInstance().GetId()); |
156 | 9.76M | } |
157 | | |
158 | 9.87M | vprintf(aFormat, aArgs); |
159 | 9.87M | printf("\n"); |
160 | 9.87M | } |
161 | | |
162 | | } // namespace Nexus |
163 | | } // namespace ot |