Contiki 3.x
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  /*
154  * Long (64 bits) address for the radio, which is used by the address filter.
155  * The address is specified in network byte order.
156  *
157  * Because this parameter value is larger than what fits in radio_value_t,
158  * it needs to be used with radio.get_object()/set_object().
159  */
160  RADIO_PARAM_64BIT_ADDR,
161 
162  /* Constants (read only) */
163 
164  /* The lowest radio channel. */
165  RADIO_CONST_CHANNEL_MIN,
166  /* The highest radio channel. */
167  RADIO_CONST_CHANNEL_MAX,
168 
169  /* The minimum transmission power in dBm. */
170  RADIO_CONST_TXPOWER_MIN,
171  /* The maximum transmission power in dBm. */
172  RADIO_CONST_TXPOWER_MAX
173 };
174 
175 /* Radio power modes */
176 enum {
177  RADIO_POWER_MODE_OFF,
178  RADIO_POWER_MODE_ON
179 };
180 
181 /**
182  * The radio reception mode controls address filtering and automatic
183  * transmission of acknowledgements in the radio (if such operations
184  * are supported by the radio). A single parameter is used to allow
185  * setting these features simultaneously as an atomic operation.
186  *
187  * To enable both address filter and transmissions of automatic
188  * acknowledgments:
189  *
190  * NETSTACK_RADIO.set_value(RADIO_PARAM_RX_MODE,
191  * RADIO_RX_MODE_ADDRESS_FILTER | RADIO_RX_MODE_AUTOACK);
192  */
193 #define RADIO_RX_MODE_ADDRESS_FILTER (1 << 0)
194 #define RADIO_RX_MODE_AUTOACK (1 << 1)
195 
196 /**
197  * The radio transmission mode controls whether transmissions should
198  * be done using clear channel assessment (if supported by the
199  * radio). If send-on-CCA is enabled, the radio's send function will
200  * wait for a radio-specific time window for the channel to become
201  * clear. If this does not happen, the send function will return
202  * RADIO_TX_COLLISION.
203  */
204 #define RADIO_TX_MODE_SEND_ON_CCA (1 << 0)
205 
206 /* Radio return values when setting or getting radio parameters. */
207 typedef enum {
208  RADIO_RESULT_OK,
209  RADIO_RESULT_NOT_SUPPORTED,
210  RADIO_RESULT_INVALID_VALUE,
211  RADIO_RESULT_ERROR
212 } radio_result_t;
213 
214 /* Radio return values for transmissions. */
215 enum {
216  RADIO_TX_OK,
217  RADIO_TX_ERR,
218  RADIO_TX_COLLISION,
219  RADIO_TX_NOACK,
220 };
221 
222 /**
223  * The structure of a device driver for a radio in Contiki.
224  */
225 struct radio_driver {
226 
227  int (* init)(void);
228 
229  /** Prepare the radio with a packet to be sent. */
230  int (* prepare)(const void *payload, unsigned short payload_len);
231 
232  /** Send the packet that has previously been prepared. */
233  int (* transmit)(unsigned short transmit_len);
234 
235  /** Prepare & transmit a packet. */
236  int (* send)(const void *payload, unsigned short payload_len);
237 
238  /** Read a received packet into a buffer. */
239  int (* read)(void *buf, unsigned short buf_len);
240 
241  /** Perform a Clear-Channel Assessment (CCA) to find out if there is
242  a packet in the air or not. */
243  int (* channel_clear)(void);
244 
245  /** Check if the radio driver is currently receiving a packet */
246  int (* receiving_packet)(void);
247 
248  /** Check if the radio driver has just received a packet */
249  int (* pending_packet)(void);
250 
251  /** Turn the radio on. */
252  int (* on)(void);
253 
254  /** Turn the radio off. */
255  int (* off)(void);
256 
257  /** Get a radio parameter value. */
258  radio_result_t (* get_value)(radio_param_t param, radio_value_t *value);
259 
260  /** Set a radio parameter value. */
261  radio_result_t (* set_value)(radio_param_t param, radio_value_t value);
262 
263  /**
264  * Get a radio parameter object. The argument 'dest' must point to a
265  * memory area of at least 'size' bytes, and this memory area will
266  * contain the parameter object if the function succeeds.
267  */
268  radio_result_t (* get_object)(radio_param_t param, void *dest, size_t size);
269 
270  /**
271  * Set a radio parameter object. The memory area referred to by the
272  * argument 'src' will not be accessed after the function returns.
273  */
274  radio_result_t (* set_object)(radio_param_t param, const void *src,
275  size_t size);
276 
277 };
278 
279 #endif /* RADIO_H_ */
280 
281 /** @} */
282 /** @} */
The structure of a device driver for a radio in Contiki.
Definition: radio.h:225
int(* pending_packet)(void)
Check if the radio driver has just received a packet.
Definition: radio.h:249
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(* prepare)(const void *payload, unsigned short payload_len)
Prepare the radio with a packet to be sent.
Definition: radio.h:230
int(* send)(const void *payload, unsigned short payload_len)
Prepare &amp; transmit a packet.
Definition: radio.h:236
int(* receiving_packet)(void)
Check if the radio driver is currently receiving a packet.
Definition: radio.h:246
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:243
int(* on)(void)
Turn the radio on.
Definition: radio.h:252
radio_result_t(* get_value)(radio_param_t param, radio_value_t *value)
Get a radio parameter value.
Definition: radio.h:258
radio_result_t(* get_object)(radio_param_t param, void *dest, size_t size)
Get a radio parameter object.
Definition: radio.h:268
int(* transmit)(unsigned short transmit_len)
Send the packet that has previously been prepared.
Definition: radio.h:233
radio_result_t(* set_object)(radio_param_t param, const void *src, size_t size)
Set a radio parameter object.
Definition: radio.h:274
radio_result_t(* set_value)(radio_param_t param, radio_value_t value)
Set a radio parameter value.
Definition: radio.h:261
int(* read)(void *buf, unsigned short buf_len)
Read a received packet into a buffer.
Definition: radio.h:239
int(* off)(void)
Turn the radio off.
Definition: radio.h:255