42#if BORDER_ROUTER_SERIAL_RADIO
54#define LOG_MODULE "BR-CBOR"
55#define LOG_LEVEL LOG_LEVEL_NONE
58void packet_sent(uint8_t sessionid, uint8_t status, uint8_t tx);
62#define BR_CBOR_BUF_SIZE 280
68cbor_frame_send(uint8_t *buf,
size_t len)
72 if(len == 0 || len + 2 > BR_CBOR_BUF_SIZE) {
73 LOG_WARN(
"dropping oversized CBOR frame (%u bytes)\n", (
unsigned)len);
78 buf[len] = crc & 0xff;
79 buf[len + 1] = (crc >> 8) & 0xff;
81 write_to_slip(buf, (
int)(len + 2));
87 static uint8_t buf[BR_CBOR_BUF_SIZE];
106 static uint8_t buf[BR_CBOR_BUF_SIZE];
127 static uint8_t buf[BR_CBOR_BUF_SIZE];
144 static uint8_t buf[BR_CBOR_BUF_SIZE];
171 case CBOR_MAJOR_TYPE_UNSIGNED:
173 case CBOR_MAJOR_TYPE_SIGNED:
175 case CBOR_MAJOR_TYPE_BYTE_STRING:
177 case CBOR_MAJOR_TYPE_TEXT_STRING:
179 case CBOR_MAJOR_TYPE_ARRAY:
181 if(len == SIZE_MAX) {
184 for(
size_t i = 0; i < len; i++) {
185 if(!cbor_skip_value(reader)) {
190 case CBOR_MAJOR_TYPE_MAP:
192 if(len == SIZE_MAX) {
195 for(
size_t i = 0; i < len; i++) {
196 if(!cbor_skip_value(reader) || !cbor_skip_value(reader)) {
201 case CBOR_MAJOR_TYPE_SIMPLE:
213 uint16_t received_crc, computed_crc;
214 uint8_t msg_type = 0;
220 uint8_t error_code = 0;
221 const uint8_t *frame = NULL;
222 size_t frame_len = 0;
228 received_crc = data[len - 2] | (data[len - 1] << 8);
230 if(received_crc != computed_crc) {
231 LOG_WARN(
"CRC mismatch: got 0x%04x expected 0x%04x\n",
232 received_crc, computed_crc);
238 if(num_pairs == SIZE_MAX) {
239 LOG_WARN(
"not a CBOR map\n");
243 for(
size_t i = 0; i < num_pairs; i++) {
250 if(key == NULL || key_len != 1) {
251 if(key == NULL || !cbor_skip_value(&reader)) {
260 msg_type = (uint8_t)u;
290 error_code = (uint8_t)u;
300 if(!cbor_skip_value(&reader)) {
308 case SRADIO_EVT_RX_FRAME:
309 if(frame != NULL && frame_len > 0) {
313 LOG_WARN(
"RX frame too large (%u > %u), dropping\n",
318 packetbuf_set_attr(PACKETBUF_ATTR_RSSI, rssi);
319 packetbuf_set_attr(PACKETBUF_ATTR_LINK_QUALITY, lqi);
320 NETSTACK_MAC.input();
324 case SRADIO_EVT_TX_RESPONSE:
328 case SRADIO_EVT_ADDR64_RESPONSE:
329 if(frame != NULL && frame_len == 8) {
330 LOG_INFO(
"Got radio EUI-64\n");
331 border_router_set_mac(frame);
333 LOG_WARN(
"ADDR64 response with bad length %u\n", (
unsigned)frame_len);
337 case SRADIO_EVT_PARAM_RESPONSE:
338 LOG_DBG(
"Param %u = %ld\n", param, (
long)
value);
341 case SRADIO_EVT_HEARTBEAT:
342 LOG_DBG(
"Radio heartbeat\n");
345 case SRADIO_EVT_PONG:
346 LOG_DBG(
"Radio pong\n");
349 case SRADIO_EVT_ERROR:
350 LOG_WARN(
"Radio reported error %u\n", error_code);
354 LOG_DBG(
"Unhandled radio message type %u\n", msg_type);
CBOR-over-SLIP protocol for talking to the serialradio firmware (examples/serialradio) from the nativ...
void br_cbor_send_router_mode(uint8_t msg_id, bool enable)
Enable or disable border-router radio mode (ROUTER_MODE).
void br_cbor_send_get_addr64(uint8_t msg_id)
Request the radio's EUI-64 link-layer address (GET_ADDR64).
void br_cbor_send_tx_frame(uint8_t msg_id, const uint8_t *frame, uint16_t len)
Send a raw 802.15.4 frame to the radio for transmission.
void br_cbor_send_set_param(uint8_t msg_id, uint16_t param, int32_t value)
Set a radio parameter on the serial radio (SET_PARAM).
void border_router_cbor_input(const uint8_t *data, int len)
Parse and dispatch a complete CBOR frame (including trailing CRC16) received from the serial radio.
Border router header file.
Header file for the CRC16 calculcation.
size_t cbor_end_writer(cbor_writer_state_t *state)
Finishes writing CBOR output.
void cbor_open_map(cbor_writer_state_t *state)
Adds subsequent entries to a map.
cbor_size_t cbor_read_signed(cbor_reader_state_t *state, int64_t *value)
Reads a signed integer.
size_t cbor_read_array(cbor_reader_state_t *state)
Reads the number of elements of an array.
cbor_major_type_t
Enumeration of major types.
cbor_size_t cbor_read_unsigned(cbor_reader_state_t *state, uint64_t *value)
Reads an unsigned integer.
cbor_simple_value_t cbor_read_simple(cbor_reader_state_t *state)
Reads a simple value.
void cbor_write_text(cbor_writer_state_t *state, const char *text, size_t text_size)
Appends a text string.
void cbor_write_unsigned(cbor_writer_state_t *state, uint64_t value)
Appends an unsigned integer.
cbor_major_type_t cbor_peek_next(cbor_reader_state_t *state)
Inspects the next major type.
const uint8_t * cbor_read_data(cbor_reader_state_t *state, size_t *data_size)
Reads a byte string.
void cbor_close_map(cbor_writer_state_t *state)
Stops adding subsequent entries to the innermost map.
void cbor_init_writer(cbor_writer_state_t *state, uint8_t *buffer, size_t buffer_size)
Prepares for writing CBOR output.
void cbor_init_reader(cbor_reader_state_t *state, const uint8_t *cbor, size_t cbor_size)
Prepares for reading CBOR input.
void cbor_write_data(cbor_writer_state_t *state, const uint8_t *data, size_t data_size)
Appends a byte string.
void cbor_write_signed(cbor_writer_state_t *state, int64_t value)
Appends a signed integer.
size_t cbor_read_map(cbor_reader_state_t *state)
Reads the number of entries of a map.
const char * cbor_read_text(cbor_reader_state_t *state, size_t *text_size)
Reads a text string.
@ CBOR_SIZE_NONE
error condition
static int value(int type)
unsigned short crc16_data(const unsigned char *data, int len, unsigned short acc)
Calculate the CRC16 over a data area.
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
static void packet_sent(void *ptr, int status, int transmissions)
Callback function for the MAC packet sent callback.
Header file for the logging system.
Include file for the Contiki low-layer network stack (NETSTACK).
Header file for the Packet buffer (packetbuf) management.
Structure of the internal state of a CBOR reader.
Structure of the internal state of a CBOR writer.