2016-11-16 15:38:25 -05:00
|
|
|
/*
|
2017-08-04 12:05:38 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-11-16 15:38:25 -05:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Glenn Ruben Bakke
|
2018-07-19 14:33:24 -04:00
|
|
|
* Copyright (c) 2018 Artur Pacholec
|
2016-11-16 15:38:25 -05:00
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
* of this software and associated documentation files (the "Software"), to deal
|
|
|
|
* in the Software without restriction, including without limitation the rights
|
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
* copies of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be included in
|
|
|
|
* all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
* THE SOFTWARE.
|
|
|
|
*/
|
|
|
|
|
2017-02-12 08:24:15 -05:00
|
|
|
#include <stdbool.h>
|
2018-07-22 10:06:19 -04:00
|
|
|
#include <stdio.h>
|
2017-02-09 17:56:32 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "ble.h"
|
2017-02-17 13:10:24 -05:00
|
|
|
#include "ble_drv.h"
|
2018-07-19 17:51:34 -04:00
|
|
|
#include "nrf_nvic.h"
|
2016-11-16 15:38:25 -05:00
|
|
|
#include "nrf_sdm.h"
|
2018-07-22 10:06:19 -04:00
|
|
|
#include "py/misc.h"
|
2017-05-14 11:14:57 -04:00
|
|
|
|
2018-07-19 17:51:34 -04:00
|
|
|
nrf_nvic_state_t nrf_nvic_state = { 0 };
|
2017-02-12 08:24:15 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
__attribute__((aligned(4)))
|
|
|
|
static uint8_t m_ble_evt_buf[sizeof(ble_evt_t) + (BLE_GATT_ATT_MTU_DEFAULT)];
|
2017-02-17 15:47:38 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
typedef struct event_handler {
|
|
|
|
struct event_handler *next;
|
|
|
|
void *param;
|
|
|
|
ble_drv_evt_handler_t func;
|
|
|
|
} event_handler_t;
|
2017-02-17 15:47:38 -05:00
|
|
|
|
2018-12-28 22:55:29 -05:00
|
|
|
static event_handler_t *m_event_handlers = NULL;
|
|
|
|
|
|
|
|
void ble_drv_reset() {
|
2018-12-31 09:02:43 -05:00
|
|
|
// Linked list items will be gc'd.
|
2018-12-28 22:55:29 -05:00
|
|
|
m_event_handlers = NULL;
|
|
|
|
}
|
2017-02-17 15:47:38 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
void ble_drv_add_event_handler(ble_drv_evt_handler_t func, void *param) {
|
|
|
|
event_handler_t *handler = m_new_ll(event_handler_t, 1);
|
|
|
|
handler->next = NULL;
|
|
|
|
handler->param = param;
|
|
|
|
handler->func = func;
|
2017-02-12 09:20:41 -05:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
if (m_event_handlers == NULL) {
|
|
|
|
m_event_handlers = handler;
|
2018-07-19 17:51:34 -04:00
|
|
|
return;
|
2017-02-18 15:11:39 -05:00
|
|
|
}
|
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
event_handler_t *it = m_event_handlers;
|
|
|
|
while (it->next != NULL) {
|
|
|
|
if ((it->func == func) && (it->param == param)) {
|
|
|
|
m_free(handler);
|
|
|
|
return;
|
2018-07-19 14:33:24 -04:00
|
|
|
}
|
2017-03-30 16:41:19 -04:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
it = it->next;
|
2017-03-30 16:41:19 -04:00
|
|
|
}
|
2017-03-28 17:37:19 -04:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
it->next = handler;
|
2017-03-26 16:59:30 -04:00
|
|
|
}
|
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
void SD_EVT_IRQHandler(void) {
|
|
|
|
uint32_t evt_id;
|
|
|
|
while (sd_evt_get(&evt_id) != NRF_ERROR_NOT_FOUND) {
|
|
|
|
// sd_evt_handler(evt_id);
|
2018-07-19 16:59:18 -04:00
|
|
|
}
|
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
while (1) {
|
|
|
|
uint16_t evt_len = sizeof(m_ble_evt_buf);
|
|
|
|
const uint32_t err_code = sd_ble_evt_get(m_ble_evt_buf, &evt_len);
|
|
|
|
if (err_code != NRF_SUCCESS) {
|
|
|
|
if (err_code == NRF_ERROR_DATA_SIZE) {
|
|
|
|
printf("NRF_ERROR_DATA_SIZE\n");
|
|
|
|
}
|
2017-03-30 17:18:49 -04:00
|
|
|
|
|
|
|
break;
|
2018-07-22 10:06:19 -04:00
|
|
|
}
|
2017-03-12 15:18:37 -04:00
|
|
|
|
2018-07-22 10:06:19 -04:00
|
|
|
event_handler_t *it = m_event_handlers;
|
|
|
|
while (it != NULL) {
|
|
|
|
it->func((ble_evt_t *)m_ble_evt_buf, it->param);
|
|
|
|
it = it->next;
|
|
|
|
}
|
2017-02-12 18:18:47 -05:00
|
|
|
}
|
|
|
|
}
|