Contiki-NG
Loading...
Searching...
No Matches
tz-radio-normal.c
1/*
2 * Copyright (c) 2026, RISE Research Institutes of Sweden
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
7 * are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * 3. Neither the name of the copyright holder nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
29 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
31 * OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33
34/*
35 * \file
36 * Normal-world radio driver proxy for TrustZone.
37 *
38 * This thin driver implements the Contiki-NG radio_driver interface
39 * by forwarding all operations to the secure world via NSC calls.
40 * A process handles asynchronous frame delivery from the secure
41 * world to the normal-world MAC layer.
42 *
43 * \author
44 * Nicolas Tsiftes <nicolas.tsiftes@ri.se>
45 */
46
47#include "contiki.h"
48#include "dev/radio.h"
49#include "net/netstack.h"
50#include "net/packetbuf.h"
51#include "trustzone/tz-radio.h"
52
53/*---------------------------------------------------------------------------*/
54#include "sys/log.h"
55#define LOG_MODULE "TZRadio"
56#define LOG_LEVEL LOG_LEVEL_INFO
57/*---------------------------------------------------------------------------*/
58static volatile bool rx_poll_requested;
59/*---------------------------------------------------------------------------*/
60PROCESS(tz_radio_process, "TZ radio process");
61/*---------------------------------------------------------------------------*/
62static bool
63rx_poll_callback(void)
64{
65 rx_poll_requested = true;
66 process_poll(&tz_radio_process);
67 return true;
68}
69/*---------------------------------------------------------------------------*/
70static int
71nr_init(void)
72{
73 int result;
74
75 result = tz_radio_init();
76
77 if(result) {
78 if(!tz_radio_register_rx_callback(rx_poll_callback)) {
79 LOG_ERR("Failed to register RX callback\n");
80 }
81 process_start(&tz_radio_process, NULL);
82 }
83
84 return result;
85}
86/*---------------------------------------------------------------------------*/
87static int
88nr_prepare(const void *payload, unsigned short payload_len)
89{
90 return tz_radio_prepare(payload, payload_len);
91}
92/*---------------------------------------------------------------------------*/
93static int
94nr_transmit(unsigned short transmit_len)
95{
96 return tz_radio_transmit(transmit_len);
97}
98/*---------------------------------------------------------------------------*/
99static int
100nr_send(const void *payload, unsigned short payload_len)
101{
102 return tz_radio_send(payload, payload_len);
103}
104/*---------------------------------------------------------------------------*/
105static int
106nr_read(void *buf, unsigned short buf_len)
107{
108 return tz_radio_read(buf, buf_len);
109}
110/*---------------------------------------------------------------------------*/
111static int
112nr_channel_clear(void)
113{
114 return tz_radio_channel_clear();
115}
116/*---------------------------------------------------------------------------*/
117static int
118nr_receiving_packet(void)
119{
120 return tz_radio_receiving_packet();
121}
122/*---------------------------------------------------------------------------*/
123static int
124nr_pending_packet(void)
125{
126 return tz_radio_pending_packet();
127}
128/*---------------------------------------------------------------------------*/
129static int
130nr_on(void)
131{
132 return tz_radio_on();
133}
134/*---------------------------------------------------------------------------*/
135static int
136nr_off(void)
137{
138 return tz_radio_off();
139}
140/*---------------------------------------------------------------------------*/
141static radio_result_t
142nr_get_value(radio_param_t param, radio_value_t *value)
143{
144 return tz_radio_get_value(param, value);
145}
146/*---------------------------------------------------------------------------*/
147static radio_result_t
148nr_set_value(radio_param_t param, radio_value_t value)
149{
150 return tz_radio_set_value(param, value);
151}
152/*---------------------------------------------------------------------------*/
153static radio_result_t
154nr_get_object(radio_param_t param, void *dest, size_t size)
155{
156 return tz_radio_get_object(param, dest, size);
157}
158/*---------------------------------------------------------------------------*/
159static radio_result_t
160nr_set_object(radio_param_t param, const void *src, size_t size)
161{
162 return tz_radio_set_object(param, src, size);
163}
164/*---------------------------------------------------------------------------*/
165const struct radio_driver tz_radio_driver = {
166 nr_init,
167 nr_prepare,
168 nr_transmit,
169 nr_send,
170 nr_read,
171 nr_channel_clear,
172 nr_receiving_packet,
173 nr_pending_packet,
174 nr_on,
175 nr_off,
176 nr_get_value,
177 nr_set_value,
178 nr_get_object,
179 nr_set_object
180};
181/*---------------------------------------------------------------------------*/
182PROCESS_THREAD(tz_radio_process, ev, data)
183{
185
186 LOG_INFO("TZ radio process started\n");
187
188 while(1) {
189 PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_POLL);
190
191 if(rx_poll_requested) {
192 rx_poll_requested = false;
193
194 if(tz_radio_pending_packet()) {
195 int8_t rssi;
196 uint8_t lqi;
197 int len;
198
199 tz_radio_get_rx_attributes(&rssi, &lqi);
200
202 len = tz_radio_read(packetbuf_dataptr(), PACKETBUF_SIZE);
203 if(len > 0) {
205 packetbuf_set_attr(PACKETBUF_ATTR_RSSI, (int)rssi);
206 packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, lqi);
207 LOG_DBG("RX %d bytes, delivering to MAC\n", len);
208 NETSTACK_MAC.input();
209 }
210 }
211 }
212 }
213
214 PROCESS_END();
215}
216/*---------------------------------------------------------------------------*/
void packetbuf_set_datalen(uint16_t len)
Set the length of the data in the packetbuf.
Definition packetbuf.c:136
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Definition packetbuf.c:143
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
Definition packetbuf.h:67
void packetbuf_clear(void)
Clear and reset the packetbuf.
Definition packetbuf.c:75
#define PROCESS(name, strname)
Declare a process.
Definition process.h:309
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition process.h:122
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition process.h:159
#define PROCESS_END()
Define the end of a process.
Definition process.h:133
void process_start(struct process *p, process_data_t data)
Start a process.
Definition process.c:121
#define PROCESS_THREAD(name, ev, data)
Define the body of a process.
Definition process.h:275
void process_poll(struct process *p)
Request a process to be polled.
Definition process.c:366
enum radio_result_e radio_result_t
Radio return values when setting or getting radio parameters.
int radio_value_t
Each radio has a set of parameters that designate the current configuration and state of the radio.
Definition radio.h:88
Header file for the logging system.
Include file for the Contiki low-layer network stack (NETSTACK)
Header file for the Packet buffer (packetbuf) management.
Header file for the radio API.
void(* input)(void)
Callback for getting notified of incoming packet.
Definition mac.h:78
The structure of a Contiki-NG radio device driver.
Definition radio.h:534