static void process_data(struct net_pkt *pkt) { struct net_buf *buf = net_buf_frag_last(pkt->frags); u8_t seq, num_attr; int ret, i; seq = net_buf_pull_u8(buf); num_attr = net_buf_pull_u8(buf); SYS_LOG_DBG("seq %u num_attr %u", seq, num_attr); /** * There are some attributes sent over this protocol * discard them and return packet data report. */ for (i = 0; i < num_attr; i++) { /* attr */ net_buf_pull_u8(buf); /* value */ net_buf_pull_be16(buf); } /* Transmit data through radio */ ret = radio_api->tx(ieee802154_dev, pkt, buf); if (ret) { SYS_LOG_ERR("Error transmit data"); } /* TODO: Return correct status codes */ /* TODO: Implement re-transmissions if needed */ /* Send packet data report */ send_pkt_report(seq, ret, 1); }
static void deserialize_struct(struct net_buf *buf, const uint8_t **struct_ptr, uint8_t *struct_length) { *struct_length = net_buf_pull_u8(buf); *struct_ptr = buf->data; net_buf_pull(buf, *struct_length); }
static size_t slip_buffer(u8_t *sbuf, struct net_buf *buf) { size_t len = buf->len; u8_t *sbuf_orig = sbuf; int i; /** * This strange protocol does not require send START * *sbuf++ = SLIP_END; */ for (i = 0; i < len; i++) { u8_t byte = net_buf_pull_u8(buf); switch (byte) { case SLIP_END: *sbuf++ = SLIP_ESC; *sbuf++ = SLIP_ESC_END; break; case SLIP_ESC: *sbuf++ = SLIP_ESC; *sbuf++ = SLIP_ESC_ESC; break; default: *sbuf++ = byte; } } *sbuf++ = SLIP_END; return sbuf - sbuf_orig; }
static void rx_thread(void) { SYS_LOG_INF("RX thread started"); while (1) { struct net_pkt *pkt; struct net_buf *buf; u8_t specifier; pkt = k_fifo_get(&rx_queue, K_FOREVER); buf = net_buf_frag_last(pkt->frags); SYS_LOG_DBG("Got pkt %p buf %p", pkt, buf); hexdump("SLIP >", buf->data, buf->len); /* TODO: process */ specifier = net_buf_pull_u8(buf); switch (specifier) { case '?': process_request(buf); break; case '!': process_config(pkt); break; default: SYS_LOG_ERR("Unknown message specifier %c", specifier); break; } net_pkt_unref(pkt); k_yield(); } }
static void process_config(struct net_pkt *pkt) { struct net_buf *buf = net_buf_frag_last(pkt->frags); u8_t cmd = net_buf_pull_u8(buf); SYS_LOG_DBG("Process config %c", cmd); switch (cmd) { case 'S': process_data(pkt); break; case 'C': set_channel(net_buf_pull_u8(buf)); break; default: SYS_LOG_ERR("Unhandled cmd %u", cmd); } }
static void deserialize_buf(struct net_buf *buf, const uint8_t **buf_ptr, uint16_t *buf_len) { uint8_t b; /* Get the current byte */ b = net_buf_pull_u8(buf); *buf_len = b & 0x7F; if (b & 0x80) { /* Get the current byte */ b = net_buf_pull_u8(buf); *buf_len += (uint16_t)b << 7; } /* Return the values */ *buf_ptr = buf->data; net_buf_pull(buf, *buf_len); }
static void process_request(struct net_buf *buf) { u8_t cmd = net_buf_pull_u8(buf); switch (cmd) { case 'M': get_ieee_addr(); break; default: SYS_LOG_ERR("Not handled request %c", cmd); break; } }
static void tx_thread(void) { SYS_LOG_DBG("Tx thread started"); while (1) { uint8_t cmd; struct net_buf *pkt, *buf; pkt = net_buf_get(&tx_queue, K_FOREVER); buf = net_buf_frag_last(pkt); cmd = net_buf_pull_u8(buf); hexdump(">", buf->data, buf->len); switch (cmd) { case RESET: SYS_LOG_DBG("Reset device"); break; case TX: tx(pkt); break; case START: start(); break; case STOP: stop(); break; case SET_CHANNEL: set_channel(buf->data, buf->len); break; case SET_IEEE_ADDR: set_ieee_addr(buf->data, buf->len); break; case SET_SHORT_ADDR: set_short_addr(buf->data, buf->len); break; case SET_PAN_ID: set_pan_id(buf->data, buf->len); break; default: SYS_LOG_ERR("%x: Not handled for now", cmd); break; } net_nbuf_unref(pkt); k_yield(); } }
static int h4_send(enum bt_buf_type buf_type, struct net_buf *buf) { if (buf_type == BT_ACL_OUT) { uart_poll_out(h4_dev, H4_ACL); } else if (buf_type == BT_CMD) { uart_poll_out(h4_dev, H4_CMD); } else { return -EINVAL; } while (buf->len) { uart_poll_out(h4_dev, net_buf_pull_u8(buf)); } net_buf_unref(buf); return 0; }
static int h4_send(struct net_buf *buf) { BT_DBG("buf %p type %u len %u", buf, bt_buf_get_type(buf), buf->len); switch (bt_buf_get_type(buf)) { case BT_BUF_ACL_OUT: uart_poll_out(h4_dev, H4_ACL); break; case BT_BUF_CMD: uart_poll_out(h4_dev, H4_CMD); break; default: return -EINVAL; } while (buf->len) { uart_poll_out(h4_dev, net_buf_pull_u8(buf)); } net_buf_unref(buf); return 0; }
static int tx(struct net_buf *pkt) { struct net_buf *buf = net_buf_frag_last(pkt); uint8_t seq = net_buf_pull_u8(buf); int retries = 3; int ret; SYS_LOG_DBG("len %d seq %u", buf->len, seq); do { ret = radio_api->tx(ieee802154_dev, pkt); } while (ret && retries--); if (ret) { SYS_LOG_ERR("Error sending data, seq %u", seq); /* Send seq = 0 for unsuccessful send */ seq = 0; } try_write(WPANUSB_ENDP_BULK_IN, &seq, sizeof(seq)); return ret; }
static uint8_t h5_get_type(struct net_buf *buf) { return net_buf_pull_u8(buf); }