42 #include "app_error.h" 45 #include "iot_defines.h" 55 #include "dev/watchdog.h" 60 #define PRINTF(...) printf(__VA_ARGS__) 65 #ifndef BLE_MAC_MAX_INTERFACE_NUM 66 #define BLE_MAC_MAX_INTERFACE_NUM 1 74 PROCESS(ble_ipsp_process,
"BLE IPSP process");
82 ble_ipsp_handle_t handle;
83 } ble_mac_interface_t;
97 static mac_callback_t mac_sent_cb;
98 static void *mac_sent_ptr;
108 static ble_mac_interface_t *
113 if(interfaces[i].handle.conn_handle == handle->conn_handle &&
114 interfaces[i].handle.cid == handle->cid) {
115 return &interfaces[i];
132 static ble_mac_interface_t *
137 if(interfaces[i].handle.conn_handle == 0 && interfaces[i].handle.cid == 0) {
138 memcpy(&interfaces[i].handle, handle,
sizeof(ble_ipsp_handle_t));
139 memcpy(&interfaces[i].peer_addr, peer,
sizeof(eui64_t));
141 return &interfaces[i];
154 memset(interface, 0,
sizeof(ble_mac_interface_t));
171 uint32_t retval = NRF_SUCCESS;
173 ble_mac_interface_t *p_instance = NULL;
177 PRINTF(
"ble-mac: IPSP event [handle:%d CID 0x%04X]\n", p_handle->conn_handle, p_handle->cid);
180 switch(p_evt->evt_id) {
181 case BLE_IPSP_EVT_CHANNEL_CONNECTED: {
184 PRINTF(
"ble-mac: channel connected\n");
186 IPV6_EUI64_CREATE_FROM_EUI48(
187 peer_addr.identifier,
188 p_evt->evt_param->params.ch_conn_request.peer_addr.addr,
189 p_evt->evt_param->params.ch_conn_request.peer_addr.addr_type);
193 if(p_instance != NULL) {
194 PRINTF(
"ble-mac: added new IPSP interface\n");
196 PRINTF(
"ble-mac: cannot add new interface. Table is full\n");
197 ble_ipsp_disconnect(p_handle);
202 case BLE_IPSP_EVT_CHANNEL_DISCONNECTED: {
203 PRINTF(
"ble-mac: channel disconnected\n");
204 if(p_instance != NULL) {
205 PRINTF(
"ble-mac: removed IPSP interface\n");
211 case BLE_IPSP_EVT_CHANNEL_DATA_RX: {
212 PRINTF(
"ble-mac: data received\n");
213 if(p_instance != NULL) {
215 PRINTF(
"ble-mac: packet dropped as input buffer is busy\n");
220 PRINTF(
"ble-mac: packet buffer is too small!\n");
228 memcpy(
input_packet.src.identifier, p_instance->peer_addr.identifier,
sizeof(eui64_t));
229 sd_ble_gap_rssi_get(p_handle->conn_handle, &
input_packet.rssi);
233 PRINTF(
"ble-mac: got data to unknown interface!\n");
238 case BLE_IPSP_EVT_CHANNEL_DATA_TX_COMPLETE: {
239 PRINTF(
"ble-mac: data transmitted\n");
254 if(ev == PROCESS_EVENT_POLL) {
256 packetbuf_set_attr(PACKETBUF_ATTR_RSSI,
input_packet.rssi);
257 packetbuf_set_addr(PACKETBUF_ADDR_SENDER, (
const linkaddr_t *)
input_packet.src.identifier);
260 NETSTACK_NETWORK.input();
275 static ble_ipsp_handle_t *
280 if(
linkaddr_cmp((
const linkaddr_t *)&interfaces[i].peer_addr, addr)) {
281 return &interfaces[i].handle;
296 PRINTF(
"ble-mac: sending packet[GAP handle:%d CID:0x%04X]\n", handle->conn_handle, handle->cid);
304 const linkaddr_t *dest;
305 ble_ipsp_handle_t *handle;
311 dest = packetbuf_addr(PACKETBUF_ADDR_RECEIVER);
315 if(interfaces[i].handle.cid != 0 && interfaces[i].handle.conn_handle != 0) {
323 PRINTF(
"ble-mac: no connection found for peer");
332 mac_call_sent_callback(sent, ptr,
MAC_TX_OK, 1);
334 mac_call_sent_callback(sent, ptr,
MAC_TX_ERR, 1);
355 ble_ipsp_init_t ipsp_init_params;
357 memset(&ipsp_init_params, 0,
sizeof(ipsp_init_params));
359 err_code = ble_ipsp_init(&ipsp_init_params);
360 APP_ERROR_CHECK(err_code);
void * packetbuf_dataptr(void)
Get a pointer to the data in the packetbuf.
Header for the Contiki/uIP interface.
#define PROCESS(name, strname)
Declare a process.
static int send_to_peer(ble_ipsp_handle_t *handle)
Send packet on a given IPSP handle.
int(* on)(void)
Turn the MAC layer on.
The MAC layer transmission could not be performed because of a fatal error.
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
static uip_ds6_addr_t * addr
Pointer to a nbr cache entry.
The structure of a MAC protocol driver in Contiki.
#define PROCESS_BEGIN()
Define the beginning of a process.
static ble_mac_interface_t * ble_mac_interface_lookup(ble_ipsp_handle_t *handle)
Lookup interface by IPSP connection.
Header file for the link-layer address representation
#define PROCESS_END()
Define the end of a process.
static void send_packet(linkaddr_t *dest)
This function is called by the 6lowpan code to send out a packet.
process_event_t ble_event_interface_added
This event is broadcast when BLE connection is established.
#define BLE_MAC_MAX_INTERFACE_NUM
Maximum number of interfaces, i.e., connection to master devices.
const linkaddr_t linkaddr_null
The null link-layer address.
int(* off)(void)
Turn the MAC layer off.
uint16_t packetbuf_datalen(void)
Get the length of the data in the packetbuf.
linkaddr_t linkaddr_node_addr
The link-layer address of the node.
static volatile int busy_rx
Flag is set to 1 when there is a received packet pending.
A MAC protocol implementation that does not do anything.
void process_poll(struct process *p)
Request a process to be polled.
process_event_t ble_event_interface_deleted
This event is broadcast when BLE connection is destroyed.
The MAC layer transmission was OK.
#define PACKETBUF_SIZE
The size of the packetbuf, in bytes.
static volatile int busy_tx
Flag is set to 1 when the driver is busy transmitting a packet.
int packetbuf_copyfrom(const void *from, uint16_t len)
Copy from external data into the packetbuf.
void(* init)(void)
Initialize the network driver.
process_event_t process_alloc_event(void)
Allocate a global event number.
int linkaddr_cmp(const linkaddr_t *addr1, const linkaddr_t *addr2)
Compare two link-layer addresses.
static void ble_mac_interface_delete(ble_mac_interface_t *interface)
Remove interface from the interface table.
Header file for the uIP TCP/IP stack.
const struct mac_driver ble_ipsp_mac_driver
BLE over IPSP MAC driver structure.
static ble_ipsp_handle_t * find_handle(const linkaddr_t *addr)
Lookup IPSP handle by peer address.
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Header file for the Packet buffer (packetbuf) management
static uint32_t ble_mac_ipsp_evt_handler_irq(ble_ipsp_handle_t *p_handle, ble_ipsp_evt_t *p_evt)
Callback registered with IPSP to receive asynchronous events from the module.
Include file for the Contiki low-layer network stack (NETSTACK)
void watchdog_periodic(void)
Writes the WDT clear sequence.
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
static ble_mac_interface_t * ble_mac_interface_add(eui64_t *peer, ble_ipsp_handle_t *handle)
Add IPSP connection to the interface table.
void process_start(struct process *p, process_data_t data)
Start a process.