Skip to content
Snippets Groups Projects
Commit 09e5dbf0 authored by schneider's avatar schneider
Browse files

hack

parent bc6d8102
No related branches found
No related tags found
No related merge requests found
......@@ -57,3 +57,4 @@ void ble_adv_stop(void);
void ble_adv_start(uint8_t mode);
void ble_adv_discoverable(bool discoverable);
void ble_adv_proc_msg(bleMsg_t *pMsg);
void ble_close_connections_to_peripherals(void);
......@@ -258,6 +258,20 @@ static const char * const l2c_coc_events[] = {
"L2C_COC_DATA_IND",
"L2C_COC_DATA_CNF"
};
static const char * const att_internal_error_code[] = {
"ATT_ERR_MEMORY",
"ATT_ERR_TIMEOUT",
"ATT_ERR_OVERFLOW",
"ATT_ERR_INVALID_RSP",
"ATT_ERR_CANCELLED",
"ATT_ERR_UNDEFINED",
"ATT_ERR_REQ_NOT_FOUND",
"ATT_ERR_MTU_EXCEEDED",
"ATT_CONTINUING",
"ATT_RSP_PENDING"
};
/*************************************************************************************************/
/*!
* \brief Application DM callback.
......@@ -392,6 +406,16 @@ static void bleSetup(bleMsg_t *pMsg)
epic_ble_set_mode(false, false);
}
void ble_close_connections_to_peripherals(void)
{
dmConnId_t connId = AppConnIsOpen();
if (connId != DM_CONN_ID_NONE) {
if(DmConnRole(connId) == DM_ROLE_MASTER) {
AppConnClose(connId);
}
}
}
void epic_ble_set_mode(bool bondable, bool scanner)
{
if(!active) {
......@@ -704,6 +728,7 @@ static void BleHandlerInit(void)
ble_adv_init();
/* Initialize application framework */
AppMasterInit();
AppSlaveInit();
AppDiscInit();
......@@ -737,17 +762,28 @@ static void BleHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg)
/* process security-related messages */
AppSlaveSecProcDmMsg((dmEvt_t *) pMsg);
/* process discovery-related messages */
if(DmConnRole(pMsg->param) == DM_ROLE_SLAVE) {
/* process discovery-related messages only if we are peripheral */
AppDiscProcDmMsg((dmEvt_t *) pMsg);
}
ble_epic_dm_api_event((dmEvt_t *)pMsg);
if(DmConnRole(pMsg->param) == DM_ROLE_MASTER && pMsg->event == DM_CONN_OPEN_IND) {
/* fake the discovery so the epic api can forward the connection open */
ble_epic_disc_cfg_complete();
}
}
else if (pMsg->event >= ATT_CBACK_START && pMsg->event <= ATT_CBACK_END)
{
/* Don't spam the console with successful notfication/indications */
if (!(pMsg->event == ATTS_HANDLE_VALUE_CNF && pMsg->status == ATT_SUCCESS)) {
if(pMsg->status >= ATT_ERR_MEMORY && pMsg->status <= ATT_RSP_PENDING) {
LOG_INFO("ble", "Ble got evt %d (%s): %d %d (%s)", pMsg->event, att_events[pMsg->event - ATT_CBACK_START], ((bleMsg_t *)pMsg)->att.handle, pMsg->status, att_internal_error_code[pMsg->status-ATT_ERR_MEMORY]);
} else {
LOG_INFO("ble", "Ble got evt %d (%s): %d %d", pMsg->event, att_events[pMsg->event - ATT_CBACK_START], ((bleMsg_t *)pMsg)->att.handle, pMsg->status);
}
}
/* process discovery-related ATT messages */
AppDiscProcAttMsg((attEvt_t *) pMsg);
ble_epic_att_api_event((attEvt_t *)pMsg);
......
......@@ -31,6 +31,8 @@ void ble_epic_att_api_event(attEvt_t *att_event)
size_t value_len = 0;
if (att_event->hdr.event == ATTC_READ_BY_GROUP_TYPE_RSP ||
att_event->hdr.event == ATTC_READ_BY_TYPE_RSP ||
att_event->hdr.event == ATTC_READ_RSP ||
att_event->hdr.event == ATTC_READ_LONG_RSP ||
att_event->hdr.event == ATTC_FIND_INFO_RSP ||
att_event->hdr.event == ATTC_HANDLE_VALUE_NTF ||
att_event->hdr.event == ATTC_HANDLE_VALUE_IND) {
......
......@@ -53,6 +53,7 @@ void ble_epic_ble_api_trigger_event(enum epic_ble_event_type type, void *data)
struct epic_ble_event e = { .type = type, .data = data };
bool bypass = false;
if (type == BLE_EVENT_DM_EVENT) {
dmEvt_t *dm_event = data;
if (dm_event->hdr.event == DM_CONN_OPEN_IND) {
......@@ -62,10 +63,15 @@ void ble_epic_ble_api_trigger_event(enum epic_ble_event_type type, void *data)
if (dm_event->hdr.event == DM_CONN_CLOSE_IND) {
connection_open = false;
}
if (dm_event->hdr.event == DM_SCAN_STOP_IND) {
bypass = true;
}
}
if (!connection_open &&
(type == BLE_EVENT_ATT_EVENT || type == BLE_EVENT_DM_EVENT)) {
(type == BLE_EVENT_ATT_EVENT || type == BLE_EVENT_DM_EVENT) &&
!bypass) {
// Don't forward DM and ATT events until epicardium is done setting up
// the connection
epic_ble_free_event(&e);
......@@ -126,6 +132,10 @@ static void send_dm_event(dmEvt_t *dm_event)
void ble_epic_dm_api_event(dmEvt_t *dm_event)
{
if (dm_event->hdr.event == DM_CONN_CLOSE_IND) {
xTimerStop(dm_timer, 0);
}
if (dm_event->hdr.event == DM_CONN_OPEN_IND) {
/* Cache the connection open indication until
* epicardium is done dicovering services. */
......@@ -246,3 +256,8 @@ int epic_ble_advertise_stop(void)
ble_adv_stop();
return 0;
}
void epic_ble_connect(uint8_t addr_type, const uint8_t *addr)
{
AppConnOpen(addr_type, (uint8_t *)addr, APP_DB_HDL_NONE);
}
......@@ -168,6 +168,7 @@ void StackInit(void)
DmAdvInit();
DmScanInit();
DmConnInit();
DmConnMasterInit();
DmConnSlaveInit();
DmSecInit();
DmSecLescInit();
......@@ -178,6 +179,7 @@ void StackInit(void)
handlerId = WsfOsSetNextHandler(L2cSlaveHandler);
L2cSlaveHandlerInit(handlerId);
L2cInit();
L2cMasterInit();
L2cSlaveInit();
handlerId = WsfOsSetNextHandler(AttHandler);
......@@ -189,7 +191,9 @@ void StackInit(void)
handlerId = WsfOsSetNextHandler(SmpHandler);
SmpHandlerInit(handlerId);
SmpiInit();
SmprInit();
SmpiScInit();
SmprScInit();
/*TODO card10: Probably want to adjust this */
......
......@@ -193,6 +193,8 @@ typedef _Bool bool;
#define API_BLE_INIT 0x190
#define API_BLE_DEINIT 0x191
#define API_BLE_CONNECT 0x1A0
/* clang-format on */
typedef uint32_t api_int_id_t;
......@@ -2742,5 +2744,6 @@ API(API_BLE_ATTC_WRITE_NO_RSP, int epic_ble_attc_write_no_rsp(uint8_t connId, ui
/** Private API call for Pycardium BLE support. */
API(API_BLE_ATTC_WRITE, int epic_ble_attc_write(uint8_t connId, uint16_t value_handle, const uint8_t *value, uint16_t value_len));
API(API_BLE_CONNECT, void epic_ble_connect(uint8_t addr_type, const uint8_t *addr));
#endif /* _EPICARDIUM_H */
......@@ -296,6 +296,8 @@ int hardware_reset(void)
/* Reset advertisement data */
ble_adv_setup();
ble_close_connections_to_peripherals();
/* Start advertising again if needed */
epic_ble_set_mode(false, false);
......
......@@ -427,6 +427,7 @@ ble_compileargs = [
'-DINIT_PERIPHERAL',
'-DINIT_ENCRYPTED',
'-DINIT_OBSERVER',
'-DINIT_CENTRAL',
'-DINIT_PHY',
]
......
......@@ -43,6 +43,7 @@ typedef struct {
static bool active = false;
static mp_obj_bluetooth_uuid_t uuid_filter;
static uint8_t roles[8];
const char *const not_implemented_message =
"Not (yet) implemented on card10. See https://git.card10.badge.events.ccc.de/card10/firmware/-/issues/8";
......@@ -353,11 +354,21 @@ static void handle_att_event(struct epic_att_event *att_event)
static void handle_dm_event(struct epic_dm_event *dm_event)
{
struct epic_wsf_header *hdr = (struct epic_wsf_header *)dm_event;
if (hdr->event == DM_CONN_OPEN_IND) {
struct epic_hciLeConnCmpl_event *e =
(struct epic_hciLeConnCmpl_event *)dm_event;
roles[e->hdr.param] = e->role;
uint8_t event;
if(e->role == 1) {
event = MP_BLUETOOTH_IRQ_CENTRAL_CONNECT;
} else {
event = MP_BLUETOOTH_IRQ_PERIPHERAL_CONNECT;
}
mp_bluetooth_gap_on_connected_disconnected(
MP_BLUETOOTH_IRQ_CENTRAL_CONNECT,
event,
e->hdr.param,
e->addrType,
e->peerAddr
......@@ -366,12 +377,22 @@ static void handle_dm_event(struct epic_dm_event *dm_event)
struct epic_hciDisconnectCmpl_event *e =
(struct epic_hciDisconnectCmpl_event *)dm_event;
uint8_t addr[6] = {};
uint8_t event;
if(roles[e->hdr.param] == 1) {
event = MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT;
} else {
event = MP_BLUETOOTH_IRQ_PERIPHERAL_DISCONNECT;
}
mp_bluetooth_gap_on_connected_disconnected(
MP_BLUETOOTH_IRQ_CENTRAL_DISCONNECT,
event,
e->hdr.param,
0xFF,
addr
);
} else if (hdr->event == DM_SCAN_STOP_IND) {
mp_bluetooth_gap_on_scan_complete();
}
}
......@@ -397,6 +418,17 @@ static void handle_att_write(struct epic_att_write *att_write)
mp_bluetooth_gatts_on_write(att_write->hdr.param, att_write->handle);
}
static void handle_scan_report(void)
{
struct epic_scan_report scan_report;
while(epic_ble_get_scan_report(&scan_report) == 0) {
uint8_t adv_type = scan_report.eventType;
mp_bluetooth_gap_on_scan_result(scan_report.addrType, scan_report.addr,
adv_type, scan_report.rssi, scan_report.data, scan_report.len);
}
}
static mp_obj_t mp_ble_poll_events(mp_obj_t interrupt_id)
{
struct epic_ble_event ble_event;
......@@ -414,6 +446,9 @@ static mp_obj_t mp_ble_poll_events(mp_obj_t interrupt_id)
if (ble_event.type == BLE_EVENT_ATT_WRITE) {
handle_att_write(ble_event.att_write);
}
if (ble_event.type == BLE_EVENT_SCAN_REPORT) {
handle_scan_report();
}
epic_ble_free_event(&ble_event);
}
} while (ret >= 0);
......@@ -802,14 +837,15 @@ int mp_bluetooth_gap_scan_start(
int32_t window_us,
bool active_scan
) {
raise();
if(active_scan) raise();
epic_ble_set_mode(false, true);
return 0;
}
// Stop discovery (if currently active).
int mp_bluetooth_gap_scan_stop(void)
{
raise();
epic_ble_set_mode(false, false);
return 0;
}
......@@ -817,7 +853,7 @@ int mp_bluetooth_gap_scan_stop(void)
int mp_bluetooth_gap_peripheral_connect(
uint8_t addr_type, const uint8_t *addr, int32_t duration_ms
) {
raise();
epic_ble_connect(addr_type, addr);
return 0;
}
......
......@@ -117,10 +117,20 @@ class PyCard10(Pyboard):
self.serial.write(b"\x04")
while True:
# check if we could exec command
data = self.serial.read(2)
# Catch log lines from epicardium
if data == b'\x1b[':
logline = self.serial.readline()
stdout_write_bytes(data + logline)
continue
if data != b"OK":
raise PyboardError("could not exec command (response: %r)" % data)
else:
break
def enter_raw_repl(self):
"""
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment