Contiki-NG
radio.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science.
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  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 /**
34  * \file
35  * Header file for the radio API
36  * \author
37  * Adam Dunkels <adam@sics.se>
38  * Joakim Eriksson <joakime@sics.se>
39  * Niclas Finne <nfi@sics.se>
40  * Nicolas Tsiftes <nvt@sics.se>
41  */
42 
43 /**
44  * \addtogroup dev
45  * @{
46  */
47 
48 /**
49  * \defgroup radio Radio API
50  *
51  * The radio API module defines a set of functions that a radio device
52  * driver must implement.
53  *
54  * @{
55  */
56 
57 #ifndef RADIO_H_
58 #define RADIO_H_
59 
60 #include <stddef.h>
61 
62 /**
63  * Each radio has a set of parameters that designate the current
64  * configuration and state of the radio. Parameters can either have
65  * values of type radio_value_t, or, when this type is insufficient, a
66  * generic object that is specified by a memory pointer and the size
67  * of the object.
68  *
69  * The radio_value_t type is set to an integer type that can hold most
70  * values used to configure the radio, and is therefore the most
71  * common type used for a parameter. Certain parameters require
72  * objects of a considerably larger size than radio_value_t, however,
73  * and in these cases the documentation below for the parameter will
74  * indicate this.
75  *
76  * All radio parameters that can vary during runtime are prefixed by
77  * "RADIO_PARAM", whereas those "parameters" that are guaranteed to
78  * remain immutable are prefixed by "RADIO_CONST". Each mutable
79  * parameter has a set of valid parameter values. When attempting to
80  * set a parameter to an invalid value, the radio will return
81  * RADIO_RESULT_INVALID_VALUE.
82  *
83  * Some radios support only a subset of the defined radio parameters.
84  * When trying to set or get such an unsupported parameter, the radio
85  * will return RADIO_RESULT_NOT_SUPPORTED.
86  */
87 
88 typedef int radio_value_t;
89 typedef unsigned radio_param_t;
90 
91 enum {
92 
93  /* Radio power mode determines if the radio is on
94  (RADIO_POWER_MODE_ON) or off (RADIO_POWER_MODE_OFF). */
95  RADIO_PARAM_POWER_MODE,
96 
97  /*
98  * Channel used for radio communication. The channel depends on the
99  * communication standard used by the radio. The values can range
100  * from RADIO_CONST_CHANNEL_MIN to RADIO_CONST_CHANNEL_MAX.
101  */
102  RADIO_PARAM_CHANNEL,
103 
104  /* Personal area network identifier, which is used by the address filter. */
105  RADIO_PARAM_PAN_ID,
106 
107  /* Short address (16 bits) for the radio, which is used by the address
108  filter. */
109  RADIO_PARAM_16BIT_ADDR,
110 
111  /*
112  * Radio receiver mode determines if the radio has address filter
113  * (RADIO_RX_MODE_ADDRESS_FILTER) and auto-ACK (RADIO_RX_MODE_AUTOACK)
114  * enabled. This parameter is set as a bit mask.
115  */
116  RADIO_PARAM_RX_MODE,
117 
118  /*
119  * Radio transmission mode determines if the radio has send on CCA
120  * (RADIO_TX_MODE_SEND_ON_CCA) enabled or not. This parameter is set
121  * as a bit mask.
122  */
123  RADIO_PARAM_TX_MODE,
124 
125  /*
126  * Transmission power in dBm. The values can range from
127  * RADIO_CONST_TXPOWER_MIN to RADIO_CONST_TXPOWER_MAX.
128  *
129  * Some radios restrict the available values to a subset of this
130  * range. If an unavailable TXPOWER value is requested to be set,
131  * the radio may select another TXPOWER close to the requested
132  * one. When getting the value of this parameter, the actual value
133  * used by the radio will be returned.
134  */
135  RADIO_PARAM_TXPOWER,
136 
137  /*
138  * Clear channel assessment threshold in dBm. This threshold
139  * determines the minimum RSSI level at which the radio will assume
140  * that there is a packet in the air.
141  *
142  * The CCA threshold must be set to a level above the noise floor of
143  * the deployment. Otherwise mechanisms such as send-on-CCA and
144  * low-power-listening duty cycling protocols may not work
145  * correctly. Hence, the default value of the system may not be
146  * optimal for any given deployment.
147  */
148  RADIO_PARAM_CCA_THRESHOLD,
149 
150  /* Received signal strength indicator in dBm. */
151  RADIO_PARAM_RSSI,
152 
153  /* RSSI of the last received packet */
154  RADIO_PARAM_LAST_RSSI,
155 
156  /* Link quality of the last received packet */
157  RADIO_PARAM_LAST_LINK_QUALITY,
158 
159  /*
160  * Long (64 bits) address for the radio, which is used by the address filter.
161  * The address is specified in network byte order.
162  *
163  * Because this parameter value is larger than what fits in radio_value_t,
164  * it needs to be used with radio.get_object()/set_object().
165  */
166  RADIO_PARAM_64BIT_ADDR,
167 
168  /* Last packet timestamp, of type rtimer_clock_t.
169  * Because this parameter value mat be larger than what fits in radio_value_t,
170  * it needs to be used with radio.get_object()/set_object(). */
171  RADIO_PARAM_LAST_PACKET_TIMESTAMP,
172 
173  /* For enabling and disabling the SHR search */
174  RADIO_PARAM_SHR_SEARCH,
175 
176  /* Constants (read only) */
177 
178  /* The lowest radio channel. */
179  RADIO_CONST_CHANNEL_MIN,
180  /* The highest radio channel. */
181  RADIO_CONST_CHANNEL_MAX,
182 
183  /* The minimum transmission power in dBm. */
184  RADIO_CONST_TXPOWER_MIN,
185  /* The maximum transmission power in dBm. */
186  RADIO_CONST_TXPOWER_MAX,
187 
188  /* A pointer to TSCH timings in micro-seconds (tsch_timeslot_timing_usec *) */
189  RADIO_CONST_TSCH_TIMING,
190 
191  /* The physical layer header+footer overhead in bytes, after SFD.
192  * On IEEE 802.15.4 at 2.4 GHz: 1 byte for len + 2 for CRC => 3 */
193  RADIO_CONST_PHY_OVERHEAD,
194 
195  /* The air time of one byte in usec, e.g. 32 for IEEE 802.15.4 at 2.4 GHz */
196  RADIO_CONST_BYTE_AIR_TIME,
197 
198  /* The delay in usec between a call to the radio API's transmit function and
199  * the end of SFD transmission */
200  RADIO_CONST_DELAY_BEFORE_TX,
201 
202  /* The delay in usec between turning on the radio and it being actually
203  * listening (able to hear a preamble) */
204  RADIO_CONST_DELAY_BEFORE_RX,
205 
206  /* The delay in usec between the end of SFD reception for an incoming frame
207  * and the radio API starting to return receiving_packet() != 0 */
208  RADIO_CONST_DELAY_BEFORE_DETECT,
209 
210  /*
211  * The maximum payload the radio driver is able to handle.
212  *
213  * This includes the MAC header and MAC payload, but not any tail bytes
214  * added automatically by the radio. For example, in the typical case of
215  * .15.4 operation at 2.4GHz, this will be 125 bytes (127 bytes minus the
216  * FCS / CRC16).
217  *
218  * This is the maximum number of bytes that:
219  * - The MAC layer will ask the radio driver to transmit.
220  * This corresponds to the payload_len argument of the prepare() and
221  * send() and the transmit_len argument of transmit().
222  * - The radio driver will deliver to the MAC layer after frame reception.
223  * The buf_len of the read function will typically be greater than or
224  * equal to this value.
225  *
226  * Supporting this constant in the radio driver's get_value function is
227  * mandatory.
228  */
229  RADIO_CONST_MAX_PAYLOAD_LEN,
230 };
231 
232 /* Radio power modes */
233 enum {
234  RADIO_POWER_MODE_OFF,
235  RADIO_POWER_MODE_ON,
236  RADIO_POWER_MODE_CARRIER_ON,
237  RADIO_POWER_MODE_CARRIER_OFF
238 };
239 
240 /**
241  * The radio reception mode controls address filtering and automatic
242  * transmission of acknowledgements in the radio (if such operations
243  * are supported by the radio). A single parameter is used to allow
244  * setting these features simultaneously as an atomic operation.
245  *
246  * To enable both address filter and transmissions of automatic
247  * acknowledgments:
248  *
249  * NETSTACK_RADIO.set_value(RADIO_PARAM_RX_MODE,
250  * RADIO_RX_MODE_ADDRESS_FILTER | RADIO_RX_MODE_AUTOACK);
251  */
252 #define RADIO_RX_MODE_ADDRESS_FILTER (1 << 0)
253 #define RADIO_RX_MODE_AUTOACK (1 << 1)
254 #define RADIO_RX_MODE_POLL_MODE (1 << 2)
255 
256 /**
257  * The radio transmission mode controls whether transmissions should
258  * be done using clear channel assessment (if supported by the
259  * radio). If send-on-CCA is enabled, the radio's send function will
260  * wait for a radio-specific time window for the channel to become
261  * clear. If this does not happen, the send function will return
262  * RADIO_TX_COLLISION.
263  */
264 #define RADIO_TX_MODE_SEND_ON_CCA (1 << 0)
265 
266 /* Radio return values when setting or getting radio parameters. */
267 typedef enum {
268  RADIO_RESULT_OK,
269  RADIO_RESULT_NOT_SUPPORTED,
270  RADIO_RESULT_INVALID_VALUE,
271  RADIO_RESULT_ERROR
272 } radio_result_t;
273 
274 /* Radio return values for transmissions. */
275 enum {
276  RADIO_TX_OK,
277  RADIO_TX_ERR,
278  RADIO_TX_COLLISION,
279  RADIO_TX_NOACK,
280 };
281 
282 /**
283  * The structure of a device driver for a radio in Contiki.
284  */
285 struct radio_driver {
286 
287  int (* init)(void);
288 
289  /** Prepare the radio with a packet to be sent. */
290  int (* prepare)(const void *payload, unsigned short payload_len);
291 
292  /** Send the packet that has previously been prepared. */
293  int (* transmit)(unsigned short transmit_len);
294 
295  /** Prepare & transmit a packet. */
296  int (* send)(const void *payload, unsigned short payload_len);
297 
298  /** Read a received packet into a buffer. */
299  int (* read)(void *buf, unsigned short buf_len);
300 
301  /** Perform a Clear-Channel Assessment (CCA) to find out if there is
302  a packet in the air or not. */
303  int (* channel_clear)(void);
304 
305  /** Check if the radio driver is currently receiving a packet */
306  int (* receiving_packet)(void);
307 
308  /** Check if the radio driver has just received a packet */
309  int (* pending_packet)(void);
310 
311  /** Turn the radio on. */
312  int (* on)(void);
313 
314  /** Turn the radio off. */
315  int (* off)(void);
316 
317  /** Get a radio parameter value. */
318  radio_result_t (* get_value)(radio_param_t param, radio_value_t *value);
319 
320  /** Set a radio parameter value. */
321  radio_result_t (* set_value)(radio_param_t param, radio_value_t value);
322 
323  /**
324  * Get a radio parameter object. The argument 'dest' must point to a
325  * memory area of at least 'size' bytes, and this memory area will
326  * contain the parameter object if the function succeeds.
327  */
328  radio_result_t (* get_object)(radio_param_t param, void *dest, size_t size);
329 
330  /**
331  * Set a radio parameter object. The memory area referred to by the
332  * argument 'src' will not be accessed after the function returns.
333  */
334  radio_result_t (* set_object)(radio_param_t param, const void *src,
335  size_t size);
336 
337 };
338 
339 #endif /* RADIO_H_ */
340 
341 /** @} */
342 /** @} */
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition: radio.h:328
int(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
Definition: radio.h:290
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
Definition: radio.h:306
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
Definition: radio.h:321
int(* pending_packet)(void)
Check if the radio driver has just received a packet.
Definition: radio.h:309
The structure of a device driver for a radio in Contiki.
Definition: radio.h:285
int(* channel_clear)(void)
Perform a Clear-Channel Assessment (CCA) to find out if there is a packet in the air or not...
Definition: radio.h:303
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
int(* send)(const void *payload, unsigned short payload_len)
Prepare & transmit a packet.
Definition: radio.h:296
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
Definition: radio.h:293
int(* off)(void)
Turn the radio off.
Definition: radio.h:315
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
Definition: radio.h:299
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
Definition: radio.h:318
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
Definition: radio.h:334
int(* on)(void)
Turn the radio on.
Definition: radio.h:312