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 #include "nrf_802154_spinel.h"
36
37 #include <stdint.h>
38 #include <stddef.h>
39
40 #include "../spinel_base/spinel.h"
41 #include "nrf_802154_spinel_dec.h"
42 #include "nrf_802154_spinel_backend.h"
43 #include "nrf_802154_spinel_log.h"
44 #include "nrf_802154_spinel_response_notifier.h"
45 #include "nrf_802154_serialization_error.h"
46 #include "nrf_802154_serialization_error_helper.h"
47 #include "nrf_802154_buffer_mgr_dst.h"
48 #include "nrf_802154_buffer_mgr_src.h"
49 #include "nrf_802154_serialization_config.h"
50
51 #if CONFIG_NRF_802154_SER_HOST
52 NRF_802154_BUFFER_MGR_SRC_INST_DECL_STATIC(m_src_mgr, NRF_802154_TX_BUFFERS);
53 NRF_802154_BUFFER_MGR_DST_INST_DECL_STATIC(m_dst_mgr, NRF_802154_RX_BUFFERS);
54 #else // CONFIG_NRF_802154_SER_HOST
55 NRF_802154_BUFFER_MGR_DST_INST_DECL_STATIC(m_dst_mgr, NRF_802154_TX_BUFFERS);
56 NRF_802154_BUFFER_MGR_SRC_INST_DECL_STATIC(m_src_mgr, NRF_802154_RX_BUFFERS);
57 #endif // CONFIG_NRF_802154_SER_HOST
58
buffer_mgr_init(void)59 static void buffer_mgr_init(void)
60 {
61 NRF_802154_BUFFER_MGR_SRC_INIT(m_src_mgr);
62 NRF_802154_BUFFER_MGR_DST_INIT(m_dst_mgr);
63 }
64
nrf_802154_spinel_dst_buffer_mgr_get(void)65 nrf_802154_buffer_mgr_dst_t * nrf_802154_spinel_dst_buffer_mgr_get(void)
66 {
67 return &m_dst_mgr;
68 }
69
nrf_802154_spinel_src_buffer_mgr_get(void)70 nrf_802154_buffer_mgr_src_t * nrf_802154_spinel_src_buffer_mgr_get(void)
71 {
72 return &m_src_mgr;
73 }
74
nrf_802154_serialization_init(void)75 void nrf_802154_serialization_init(void)
76 {
77 SERIALIZATION_ERROR_INIT(error);
78
79 buffer_mgr_init();
80 nrf_802154_spinel_response_notifier_init();
81
82 nrf_802154_ser_err_t ret = nrf_802154_backend_init();
83
84 SERIALIZATION_ERROR_CHECK(ret, error, bail);
85
86 bail:
87 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
88
89 return;
90 }
91
nrf_802154_spinel_send(const char * p_fmt,...)92 nrf_802154_ser_err_t nrf_802154_spinel_send(const char * p_fmt, ...)
93 {
94 uint8_t command_buff[NRF_802154_SPINEL_FRAME_BUFFER_SIZE];
95 spinel_ssize_t siz;
96
97 va_list args;
98
99 va_start(args, p_fmt);
100
101 siz = spinel_datatype_vpack(command_buff, sizeof(command_buff), p_fmt, args);
102
103 va_end(args);
104
105 if (siz < 0)
106 {
107 return NRF_802154_SERIALIZATION_ERROR_ENCODING_FAILURE;
108 }
109
110 NRF_802154_SPINEL_LOG_RAW("Sending spinel frame\n");
111 NRF_802154_SPINEL_LOG_BUFF_NAMED(command_buff, siz, "data");
112
113 return nrf_802154_spinel_encoded_packet_send(command_buff, (size_t)siz);
114 }
115
nrf_802154_spinel_encoded_packet_received(const void * p_data,size_t data_len)116 void nrf_802154_spinel_encoded_packet_received(const void * p_data, size_t data_len)
117 {
118 NRF_802154_SPINEL_LOG_RAW("Received spinel frame\n");
119 NRF_802154_SPINEL_LOG_BUFF_NAMED(p_data, data_len, "data");
120
121 SERIALIZATION_ERROR_INIT(error);
122
123 nrf_802154_ser_err_t err = nrf_802154_spinel_decode_cmd(p_data, data_len);
124
125 SERIALIZATION_ERROR_CHECK(err, error, bail);
126
127 bail:
128 SERIALIZATION_ERROR_RAISE_IF_FAILED(error);
129
130 return;
131 }
132