1 /*
2 * Copyright (c) 2020 - 2023, Nordic Semiconductor ASA
3 * All rights reserved.
4 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright notice, this
11 * list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
32 *
33 */
34
35 /**
36 * @file nrf_802154_spinel_net.c
37 *
38 * @brief Client of nRF 802.15.4 Radio Driver for the network core
39 * responsible for serializing callouts through spinel to application core.
40 *
41 * This file is responsible for providing implementation callouts defined by
42 * defined by nrf_802154.h as required by nRF 802.15.4 API. The implementation
43 * of callouts send commands through spinel to application core effectively
44 * causing calling of corresponding callouts in the application core.
45 */
46
47 #include <stdint.h>
48
49 #include "../spinel_base/spinel.h"
50 #include "nrf_802154_spinel.h"
51 #include "nrf_802154_spinel_datatypes.h"
52 #include "nrf_802154_spinel_enc_net.h"
53 #include "nrf_802154_spinel_log.h"
54 #include "nrf_802154_spinel_response_notifier.h"
55 #include "nrf_802154_serialization_error.h"
56 #include "nrf_802154_serialization_error_helper.h"
57 #include "nrf_802154_buffer_mgr_dst.h"
58 #include "nrf_802154_buffer_mgr_src.h"
59
60 #include "nrf_802154.h"
61
62 /**@brief A pointer to the last transmitted ACK frame. */
63 static const uint8_t * volatile mp_last_tx_ack;
64
local_transmitted_frame_ptr_free(void * p_frame)65 static void local_transmitted_frame_ptr_free(void * p_frame)
66 {
67 SERIALIZATION_ERROR_INIT(error);
68
69 bool frame_found = nrf_802154_buffer_mgr_dst_remove_by_local_pointer(
70 nrf_802154_spinel_dst_buffer_mgr_get(),
71 p_frame);
72
73 SERIALIZATION_ERROR_IF(!frame_found,
74 NRF_802154_SERIALIZATION_ERROR_INVALID_BUFFER,
75 error,
76 bail);
77
78 bail:
79 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
80
81 return;
82 }
83
nrf_802154_cca_done(bool channel_free)84 void nrf_802154_cca_done(bool channel_free)
85 {
86 nrf_802154_ser_err_t res;
87
88 SERIALIZATION_ERROR_INIT(error);
89
90 NRF_802154_SPINEL_LOG_BANNER_CALLING();
91 NRF_802154_SPINEL_LOG_VAR_NAMED("%s", channel_free ? "true" : "false", "channel_free");
92
93 res = nrf_802154_spinel_send_cmd_prop_value_is(
94 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_CCA_DONE,
95 SPINEL_DATATYPE_NRF_802154_CCA_DONE,
96 channel_free);
97
98 SERIALIZATION_ERROR_CHECK(res, error, bail);
99
100 bail:
101 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
102
103 return;
104 }
105
nrf_802154_cca_failed(nrf_802154_cca_error_t err)106 void nrf_802154_cca_failed(nrf_802154_cca_error_t err)
107 {
108 nrf_802154_ser_err_t res;
109
110 SERIALIZATION_ERROR_INIT(error);
111
112 NRF_802154_SPINEL_LOG_BANNER_CALLING();
113 NRF_802154_SPINEL_LOG_VAR("%u", err);
114
115 res = nrf_802154_spinel_send_cmd_prop_value_is(
116 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_CCA_FAILED,
117 SPINEL_DATATYPE_NRF_802154_CCA_FAILED,
118 err);
119
120 SERIALIZATION_ERROR_CHECK(res, error, bail);
121
122 bail:
123 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
124
125 return;
126 }
127
nrf_802154_energy_detected(uint8_t result)128 void nrf_802154_energy_detected(uint8_t result)
129 {
130 nrf_802154_ser_err_t res;
131
132 SERIALIZATION_ERROR_INIT(error);
133
134 NRF_802154_SPINEL_LOG_BANNER_CALLING();
135 NRF_802154_SPINEL_LOG_VAR("%u", result);
136
137 res = nrf_802154_spinel_send_cmd_prop_value_is(
138 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_ENERGY_DETECTED,
139 SPINEL_DATATYPE_NRF_802154_ENERGY_DETECTED,
140 result);
141
142 SERIALIZATION_ERROR_CHECK(res, error, bail);
143
144 bail:
145 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
146
147 return;
148 }
149
nrf_802154_energy_detection_failed(nrf_802154_ed_error_t err)150 void nrf_802154_energy_detection_failed(nrf_802154_ed_error_t err)
151 {
152 nrf_802154_ser_err_t res;
153
154 SERIALIZATION_ERROR_INIT(error);
155
156 NRF_802154_SPINEL_LOG_BANNER_CALLING();
157 NRF_802154_SPINEL_LOG_VAR("%u", err);
158
159 res = nrf_802154_spinel_send_cmd_prop_value_is(
160 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_ENERGY_DETECTION_FAILED,
161 SPINEL_DATATYPE_NRF_802154_ENERGY_DETECTION_FAILED,
162 err);
163
164 SERIALIZATION_ERROR_CHECK(res, error, bail);
165
166 bail:
167 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
168
169 return;
170 }
171
nrf_802154_tx_ack_started(const uint8_t * p_data)172 void nrf_802154_tx_ack_started(const uint8_t * p_data)
173 {
174 /* Due to timing restrictions this function cannot be serialized directly.
175 * Instead parameter is memorized and serialization is triggered by
176 * later call to last_tx_ack_started_send
177 */
178 mp_last_tx_ack = p_data;
179 }
180
last_tx_ack_started_send(void)181 static nrf_802154_ser_err_t last_tx_ack_started_send(void)
182 {
183 nrf_802154_ser_err_t res = NRF_802154_SERIALIZATION_ERROR_OK;
184 const uint8_t * p_last_tx_ack = mp_last_tx_ack;
185
186 NRF_802154_SPINEL_LOG_BANNER_CALLING();
187
188 if (p_last_tx_ack != NULL)
189 {
190 mp_last_tx_ack = NULL;
191
192 res = nrf_802154_spinel_send_cmd_prop_value_is(
193 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_TX_ACK_STARTED,
194 SPINEL_DATATYPE_NRF_802154_TX_ACK_STARTED,
195 p_last_tx_ack,
196 (size_t)(p_last_tx_ack[0]));
197 }
198
199 return res;
200 }
201
nrf_802154_received_timestamp_raw(uint8_t * p_data,int8_t power,uint8_t lqi,uint64_t time)202 void nrf_802154_received_timestamp_raw(uint8_t * p_data,
203 int8_t power,
204 uint8_t lqi,
205 uint64_t time)
206 {
207 nrf_802154_ser_err_t res;
208 uint32_t local_data_handle;
209
210 SERIALIZATION_ERROR_INIT(error);
211
212 res = last_tx_ack_started_send();
213 SERIALIZATION_ERROR_CHECK(res, error, bail);
214
215 NRF_802154_SPINEL_LOG_BANNER_CALLING();
216 NRF_802154_SPINEL_LOG_BUFF(p_data, p_data[0]);
217
218 // Create a handle to the original frame buffer
219 bool handle_added = nrf_802154_buffer_mgr_src_add(nrf_802154_spinel_src_buffer_mgr_get(),
220 (void *)p_data,
221 &local_data_handle);
222
223 if (!handle_added)
224 {
225 // Handle could not be created. Drop the frame and throw an error
226 nrf_802154_buffer_free_raw(p_data);
227 SERIALIZATION_ERROR(NRF_802154_SERIALIZATION_ERROR_NO_MEMORY, error, bail);
228 }
229
230 // Serialize the call
231 res = nrf_802154_spinel_send_cmd_prop_value_is(
232 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_RECEIVED_TIMESTAMP_RAW,
233 SPINEL_DATATYPE_NRF_802154_RECEIVED_TIMESTAMP_RAW,
234 NRF_802154_HDATA_ENCODE(local_data_handle, p_data, p_data[0]),
235 power,
236 lqi,
237 time);
238
239 if (res < 0)
240 {
241 // Serialization failed. Drop the frame, clean up and throw an error
242 nrf_802154_buffer_mgr_src_remove_by_buffer_handle(nrf_802154_spinel_src_buffer_mgr_get(),
243 local_data_handle);
244
245 nrf_802154_buffer_free_raw(p_data);
246
247 SERIALIZATION_ERROR(res, error, bail);
248 }
249
250 bail:
251 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
252
253 return;
254 }
255
nrf_802154_receive_failed(nrf_802154_rx_error_t error,uint32_t id)256 void nrf_802154_receive_failed(nrf_802154_rx_error_t error, uint32_t id)
257 {
258 nrf_802154_ser_err_t res;
259
260 SERIALIZATION_ERROR_INIT(ser_error);
261
262 res = last_tx_ack_started_send();
263 SERIALIZATION_ERROR_CHECK(res, ser_error, bail);
264
265 NRF_802154_SPINEL_LOG_BANNER_CALLING();
266 NRF_802154_SPINEL_LOG_VAR("%u", error);
267
268 // Serialize the call
269 res = nrf_802154_spinel_send_cmd_prop_value_is(
270 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_RECEIVE_FAILED,
271 SPINEL_DATATYPE_NRF_802154_RECEIVE_FAILED,
272 error,
273 id);
274
275 SERIALIZATION_ERROR_CHECK(res, ser_error, bail);
276
277 bail:
278 SERIALIZATION_ERROR_RAISE_IF_FAILED(ser_error);
279 }
280
nrf_802154_transmitted_raw(uint8_t * p_frame,const nrf_802154_transmit_done_metadata_t * p_metadata)281 void nrf_802154_transmitted_raw(uint8_t * p_frame,
282 const nrf_802154_transmit_done_metadata_t * p_metadata)
283 {
284 uint32_t remote_frame_handle;
285 uint32_t ack_handle = 0;
286 uint8_t * p_ack = p_metadata->data.transmitted.p_ack;
287
288 SERIALIZATION_ERROR_INIT(error);
289
290 NRF_802154_SPINEL_LOG_BANNER_CALLING();
291 NRF_802154_SPINEL_LOG_BUFF(p_frame, p_frame[0]);
292
293 // Search for the handle to the original frame buffer based on the local pointer
294 bool frame_found = nrf_802154_buffer_mgr_dst_search_by_local_pointer(
295 nrf_802154_spinel_dst_buffer_mgr_get(),
296 (void *)p_frame,
297 &remote_frame_handle);
298
299 // The handle is expected to be found, throw an error if it was not found
300 SERIALIZATION_ERROR_IF(!frame_found,
301 NRF_802154_SERIALIZATION_ERROR_INVALID_BUFFER,
302 error,
303 bail);
304
305 if (p_ack != NULL)
306 {
307 // Create a handle to the original Ack buffer
308 bool ack_handle_added = nrf_802154_buffer_mgr_src_add(
309 nrf_802154_spinel_src_buffer_mgr_get(),
310 (void *)p_ack,
311 &ack_handle);
312
313 if (!ack_handle_added)
314 {
315 // Drop the transmitted frame and throw an error if Ack could not be stored
316 local_transmitted_frame_ptr_free((void *)p_frame);
317 SERIALIZATION_ERROR(NRF_802154_SERIALIZATION_ERROR_NO_MEMORY, error, bail);
318 }
319 }
320
321 // Serialize the call
322 nrf_802154_ser_err_t res = nrf_802154_spinel_send_cmd_prop_value_is(
323 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_TRANSMITTED_RAW,
324 SPINEL_DATATYPE_NRF_802154_TRANSMITTED_RAW,
325 NRF_802154_TRANSMITTED_RAW_ENCODE(remote_frame_handle, p_frame, *p_metadata, ack_handle));
326
327 // Free the local frame pointer no matter the result of serialization
328 local_transmitted_frame_ptr_free((void *)p_frame);
329
330 // Throw an error if serialization failed
331 SERIALIZATION_ERROR_CHECK(res, error, bail);
332
333 bail:
334 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
335
336 return;
337 }
338
nrf_802154_transmit_failed(uint8_t * p_frame,nrf_802154_tx_error_t tx_error,const nrf_802154_transmit_done_metadata_t * p_metadata)339 void nrf_802154_transmit_failed(uint8_t * p_frame,
340 nrf_802154_tx_error_t tx_error,
341 const nrf_802154_transmit_done_metadata_t * p_metadata)
342 {
343 uint32_t remote_frame_handle;
344
345 SERIALIZATION_ERROR_INIT(error);
346
347 NRF_802154_SPINEL_LOG_BANNER_CALLING();
348 NRF_802154_SPINEL_LOG_BUFF(p_frame, p_frame[0]);
349
350 // Search for the handle to the original frame buffer based on the local pointer
351 bool frame_found = nrf_802154_buffer_mgr_dst_search_by_local_pointer(
352 nrf_802154_spinel_dst_buffer_mgr_get(),
353 (void *)p_frame,
354 &remote_frame_handle);
355
356 // The handle is expected to be found, throw an error if it was not found
357 SERIALIZATION_ERROR_IF(!frame_found,
358 NRF_802154_SERIALIZATION_ERROR_INVALID_BUFFER,
359 error,
360 bail);
361
362 // Serialize the call
363 nrf_802154_ser_err_t res = nrf_802154_spinel_send_cmd_prop_value_is(
364 SPINEL_PROP_VENDOR_NORDIC_NRF_802154_TRANSMIT_FAILED,
365 SPINEL_DATATYPE_NRF_802154_TRANSMIT_FAILED,
366 NRF_802154_TRANSMIT_FAILED_ENCODE(remote_frame_handle, p_frame, tx_error, *p_metadata));
367
368 // Free the local frame pointer no matter the result of serialization
369 local_transmitted_frame_ptr_free((void *)p_frame);
370
371 // Throw an error if serialization failed
372 SERIALIZATION_ERROR_CHECK(res, error, bail);
373
374 bail:
375 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
376
377 return;
378 }
379
380 #ifdef TEST
381 /**@brief Unit test facility */
nrf_802154_spinel_net_module_reset(void)382 void nrf_802154_spinel_net_module_reset(void)
383 {
384 mp_last_tx_ack = NULL;
385 }
386
387 #endif
388