diff --git a/.gitignore b/.gitignore
index 93ec1aa434..841674a95f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -20,3 +20,4 @@ crypto/tests/libtrezor-crypto.so.dSYM/
/west/tools/
/west/zephyr/
/west/trezor/build/
+/west/trezor/trezor-ble/build/
diff --git a/tools/style.c.include b/tools/style.c.include
index 553e574026..8e87505314 100644
--- a/tools/style.c.include
+++ b/tools/style.c.include
@@ -3,4 +3,4 @@
^\./crypto/
^\./legacy/
^\./storage/
-^\./west/workspace/
+^\./west/trezor
diff --git a/west/trezor/trezor-ble/CMakeLists.txt b/west/trezor/trezor-ble/CMakeLists.txt
index 77927b7634..452e96d4ad 100644
--- a/west/trezor/trezor-ble/CMakeLists.txt
+++ b/west/trezor/trezor-ble/CMakeLists.txt
@@ -10,16 +10,23 @@ project(NONE)
# NORDIC SDK APP START
target_sources(app PRIVATE
- src/main.c
- src/connection.c
- src/advertising.c
- src/events.c
- src/uart.c
- src/spi.c
- src/int_comm.c
- src/trz_nus.c
+ src/main.c
+ src/ble/connection.c
+ src/ble/advertising.c
+ src/ble/management.c
+ src/ble/service.c
+ src/ble/bonds.c
+ src/ble/pairing.c
+ src/ble/ble.c
+ src/trz_comm/uart.c
+ src/trz_comm/spi.c
+ src/trz_comm/trz_comm.c
+ src/signals/signals.c
)
+include_directories(src/signals/inc)
+include_directories(src/trz_comm/inc)
+include_directories(src/ble/inc)
# NORDIC SDK APP END
diff --git a/west/trezor/trezor-ble/Kconfig b/west/trezor/trezor-ble/Kconfig
index 200403dfdd..a4145e0e38 100644
--- a/west/trezor/trezor-ble/Kconfig
+++ b/west/trezor/trezor-ble/Kconfig
@@ -1,43 +1,28 @@
-#
-# Copyright (c) 2018 Nordic Semiconductor
-#
-# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
-#
+# * This file is part of the Trezor project, https://trezor.io/
+# *
+# * Copyright (c) SatoshiLabs
+# *
+# * This program is free software: you can redistribute it and/or modify
+# * it under the terms of the GNU General Public License as published by
+# * the Free Software Foundation, either version 3 of the License, or
+# * (at your option) any later version.
+# *
+# * This program is distributed in the hope that it will be useful,
+# * but WITHOUT ANY WARRANTY; without even the implied warranty of
+# * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# * GNU General Public License for more details.
+# *
+# * You should have received a copy of the GNU General Public License
+# * along with this program. If not, see .
source "Kconfig.zephyr"
-menu "Nordic UART BLE GATT service sample"
+menu "Trezor BLE gateway configuration"
-config BT_NUS_THREAD_STACK_SIZE
+config DEFAULT_THREAD_STACK_SIZE
int "Thread stack size"
default 1024
help
- Stack size used in each of the two threads
-
-config BT_NUS_UART_BUFFER_SIZE
- int "UART payload buffer element size"
- default 40
- help
- Size of the payload buffer in each RX and TX FIFO element
-
-config BT_NUS_SECURITY_ENABLED
- bool "Enable security"
- default y
- select BT_SMP
- help
- "Enable BLE security for the UART service"
-
-config BT_NUS_UART_RX_WAIT_TIME
- int "Timeout for UART RX complete event"
- default 50000
- help
- Wait for RX complete event time in microseconds
-
-config BT_NUS_UART_ASYNC_ADAPTER
- bool "Enable UART async adapter"
- select SERIAL_SUPPORT_ASYNC
- help
- Enables asynchronous adapter for UART drives that supports only
- IRQ interface.
+ Common stack size used
endmenu
diff --git a/west/trezor/trezor-ble/prj.conf b/west/trezor/trezor-ble/prj.conf
index 07a21bcffe..4fc08a4347 100644
--- a/west/trezor/trezor-ble/prj.conf
+++ b/west/trezor/trezor-ble/prj.conf
@@ -3,6 +3,8 @@
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
+CONFIG_NCS_APPLICATION_BOOT_BANNER_STRING="Trezor BLE Gateway"
+
CONFIG_CLOCK_CONTROL=y
CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC=y
CONFIG_CLOCK_CONTROL_NRF_K32SRC_RC_CALIBRATION=y
diff --git a/west/trezor/trezor-ble/src/advertising.h b/west/trezor/trezor-ble/src/advertising.h
deleted file mode 100644
index 870bf5e646..0000000000
--- a/west/trezor/trezor-ble/src/advertising.h
+++ /dev/null
@@ -1,12 +0,0 @@
-
-
-#include
-
-void advertising_start(bool wl);
-void advertising_stop(void);
-bool is_advertising(void);
-bool is_advertising_whitelist(void);
-void advertising_init(void);
-void advertising_setup_wl(void);
-int advertising_get_bond_count(void);
-void erase_bonds(void);
diff --git a/west/trezor/trezor-ble/src/advertising.c b/west/trezor/trezor-ble/src/ble/advertising.c
similarity index 69%
rename from west/trezor/trezor-ble/src/advertising.c
rename to west/trezor/trezor-ble/src/ble/advertising.c
index c265373a79..039747ba88 100644
--- a/west/trezor/trezor-ble/src/advertising.c
+++ b/west/trezor/trezor-ble/src/ble/advertising.c
@@ -1,3 +1,21 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
#include
#include
@@ -6,11 +24,9 @@
#include
-#include "connection.h"
-#include "int_comm.h"
-#include "trz_nus.h"
+#include "ble_internal.h"
-#define LOG_MODULE_NAME fw_int_advertising
+#define LOG_MODULE_NAME ble_advertising
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
@@ -18,8 +34,6 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
bool advertising = false;
bool advertising_wl = false;
-int bond_cnt = 0;
-int bond_cnt_tmp = 0;
uint8_t manufacturer_data[8] = {0xff, 0xff, 0, 3, 'T', '3', 'W', '1'};
@@ -29,7 +43,7 @@ static const struct bt_data advertising_data[] = {
};
static const struct bt_data scan_response_data[] = {
- BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
+ BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_TRZ_VAL),
BT_DATA(BT_DATA_MANUFACTURER_DATA, manufacturer_data, 8),
};
@@ -43,15 +57,11 @@ static void add_to_whitelist(const struct bt_bond_info *info, void *user_data) {
} else {
LOG_INF("whitelist add: %s\n", addr);
}
-
- bond_cnt_tmp++;
}
void advertising_setup_wl(void) {
bt_le_filter_accept_list_clear();
- bond_cnt_tmp = 0;
bt_foreach_bond(BT_ID_DEFAULT, add_to_whitelist, NULL);
- bond_cnt = bond_cnt_tmp;
}
void advertising_start(bool wl) {
@@ -62,7 +72,7 @@ void advertising_start(bool wl) {
} else {
LOG_WRN("Already advertising");
- send_status_event();
+ management_send_status_event();
return;
}
}
@@ -97,54 +107,39 @@ void advertising_start(bool wl) {
}
if (err) {
LOG_ERR("Advertising failed to start (err %d)", err);
- send_status_event();
+ management_send_status_event();
return;
}
advertising = true;
advertising_wl = wl;
- // oob_fetch_addr();
-
- send_status_event();
+ management_send_status_event();
}
void advertising_stop(void) {
if (!advertising) {
LOG_WRN("Not advertising");
- send_status_event();
+ management_send_status_event();
return;
}
int err = bt_le_adv_stop();
if (err) {
LOG_ERR("Advertising failed to stop (err %d)", err);
- send_status_event();
+ management_send_status_event();
return;
}
advertising = false;
advertising_wl = false;
- send_status_event();
+ management_send_status_event();
}
-bool is_advertising(void) { return advertising; }
+bool advertising_is_advertising(void) { return advertising; }
-bool is_advertising_whitelist(void) { return advertising_wl; }
+bool advertising_is_advertising_whitelist(void) { return advertising_wl; }
void advertising_init(void) {
LOG_INF("Advertising init");
advertising_setup_wl();
}
-
-void erase_bonds(void) {
- int err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY);
- if (err) {
- LOG_INF("Cannot delete bonds (err: %d)\n", err);
- } else {
- bt_le_filter_accept_list_clear();
- bond_cnt = 0;
- LOG_INF("Bonds deleted successfully \n");
- }
-}
-
-int advertising_get_bond_count(void) { return bond_cnt; }
diff --git a/west/trezor/trezor-ble/src/ble/ble.c b/west/trezor/trezor-ble/src/ble/ble.c
new file mode 100644
index 0000000000..38a2731c3a
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/ble.c
@@ -0,0 +1,107 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+#include
+
+#include
+#include
+#include
+#include
+#include
+
+#include
+
+#include "ble_internal.h"
+
+#define LOG_MODULE_NAME ble
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
+
+static K_SEM_DEFINE(ble_init_ok, 0, 1);
+
+static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data,
+ uint16_t len) {
+ if (!signals_is_trz_ready()) {
+ LOG_INF("Trezor not ready, rejecting data");
+ // send_error_response();
+ return;
+ }
+
+ char addr[BT_ADDR_LE_STR_LEN] = {0};
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, ARRAY_SIZE(addr));
+
+ LOG_DBG("Received data from: %s, %d", addr, len);
+
+ trz_comm_send_msg(NRF_SERVICE_BLE, data, len);
+}
+
+bool ble_init(void) {
+ int err = 0;
+
+ connection_init();
+ pairing_init();
+
+ err = bt_enable(NULL);
+ if (err) {
+ return false;
+ }
+
+ if (IS_ENABLED(CONFIG_SETTINGS)) {
+ settings_load();
+ }
+
+ err = service_init(bt_receive_cb);
+ if (err) {
+ LOG_ERR("Failed to initialize UART service (err: %d)", err);
+ return 0;
+ }
+
+ bt_set_name("TrezorGAP");
+
+ advertising_init();
+ management_init();
+
+ k_sem_give(&ble_init_ok);
+ LOG_INF("Bluetooth initialized");
+
+ management_send_status_event();
+
+ return true;
+}
+
+void ble_write_thread(void) {
+ /* Don't go any further until BLE is initialized */
+ k_sem_take(&ble_init_ok, K_FOREVER);
+
+ for (;;) {
+ /* Wait indefinitely for data to be sent over bluetooth */
+ trz_packet_t *buf = trz_comm_poll_data(NRF_SERVICE_BLE);
+
+ if (service_send(connection_get_current(), buf)) {
+ LOG_WRN("Failed to send data over BLE connection: %d", buf->len);
+ k_free(buf);
+ }
+
+ LOG_DBG("Freeing UART data");
+ }
+}
+
+K_THREAD_DEFINE(ble_write_thread_id, CONFIG_DEFAULT_THREAD_STACK_SIZE,
+ ble_write_thread, NULL, NULL, NULL, 7, 0, 0);
diff --git a/west/trezor/trezor-ble/src/ble/ble_internal.h b/west/trezor/trezor-ble/src/ble/ble_internal.h
new file mode 100644
index 0000000000..232659656a
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/ble_internal.h
@@ -0,0 +1,139 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include
+
+#include
+
+#include
+#include
+#include
+#include
+
+#include
+
+/** @brief UUID of the NUS Service. **/
+#define BT_UUID_TRZ_VAL \
+ BT_UUID_128_ENCODE(0x8c000001, 0xa59b, 0x4d58, 0xa9ad, 0x073df69fa1b1)
+
+/** @brief UUID of the TX Characteristic. **/
+#define BT_UUID_TRZ_TX_VAL \
+ BT_UUID_128_ENCODE(0x8c000003, 0xa59b, 0x4d58, 0xa9ad, 0x073df69fa1b1)
+
+/** @brief UUID of the RX Characteristic. **/
+#define BT_UUID_TRZ_RX_VAL \
+ BT_UUID_128_ENCODE(0x8c000002, 0xa59b, 0x4d58, 0xa9ad, 0x073df69fa1b1)
+
+#define BT_UUID_TRZ_SERVICE BT_UUID_DECLARE_128(BT_UUID_TRZ_VAL)
+#define BT_UUID_TRZ_RX BT_UUID_DECLARE_128(BT_UUID_TRZ_RX_VAL)
+#define BT_UUID_TRZ_TX BT_UUID_DECLARE_128(BT_UUID_TRZ_TX_VAL)
+
+typedef struct {
+ uint8_t msg_id;
+ uint8_t connected;
+ uint8_t advertising;
+ uint8_t advertising_whitelist;
+
+ uint8_t peer_count;
+ uint8_t reserved[2];
+ uint8_t sd_version_number;
+
+ uint16_t sd_company_id;
+ uint16_t sd_subversion_number;
+
+ uint32_t app_version;
+ uint32_t bld_version;
+
+} event_status_msg_t;
+
+typedef enum {
+ INTERNAL_EVENT_STATUS = 0x01,
+ INTERNAL_EVENT_SUCCESS = 0x02,
+ INTERNAL_EVENT_FAILURE = 0x03,
+ INTERNAL_EVENT_PAIRING_REQUEST = 0x04,
+ INTERNAL_EVENT_PAIRING_CANCELLED = 0x05,
+} internal_event_t;
+
+typedef enum {
+ INTERNAL_CMD_SEND_STATE = 0x00,
+ INTERNAL_CMD_ADVERTISING_ON = 0x01,
+ INTERNAL_CMD_ADVERTISING_OFF = 0x02,
+ INTERNAL_CMD_ERASE_BONDS = 0x03,
+ INTERNAL_CMD_DISCONNECT = 0x04,
+ INTERNAL_CMD_ACK = 0x05,
+ INTERNAL_CMD_ALLOW_PAIRING = 0x06,
+ INTERNAL_CMD_REJECT_PAIRING = 0x07,
+} internal_cmd_t;
+
+// BLE management functions
+// Initialization
+void management_init(void);
+// Send status event
+void management_send_status_event(void);
+// Send Pairing Request event, data is the pairing code
+void management_send_pairing_request_event(uint8_t *data, uint16_t len);
+// Send Pairing Cancelled event
+void management_send_pairing_cancelled_event(void);
+
+// Bonds
+// Erase all bonds
+bool bonds_erase_all(void);
+// Get number of bonded devices
+int bonds_get_count(void);
+
+// Advertising functions
+// Initialization
+void advertising_init(void);
+// Start advertising, with or without whitelist
+void advertising_start(bool wl);
+// Stop advertising
+void advertising_stop(void);
+// Check if advertising is active
+bool advertising_is_advertising(void);
+// Check if advertising is active with whitelist
+bool advertising_is_advertising_whitelist(void);
+
+// Connection functions
+// Initialization
+bool connection_init(void);
+// Disconnect current connection
+void connection_disconnect(void);
+// Check if there is an active connection
+bool connection_is_connected(void);
+// Get current connection
+struct bt_conn *connection_get_current(void);
+
+// Pairing functions
+// Initialization
+bool pairing_init(void);
+// Reset pairing process
+void pairing_reset(void);
+// Respond to pairing request
+void pairing_num_comp_reply(bool accept);
+
+// Service functions
+// Callback definition for received data
+typedef void (*service_received_cb)(struct bt_conn *conn,
+ const uint8_t *const data, uint16_t len);
+// Initialize of the BLE service
+int service_init(service_received_cb callbacks);
+// Send data to the connected device
+int service_send(struct bt_conn *conn, trz_packet_t *data);
diff --git a/west/trezor/trezor-ble/src/ble/bonds.c b/west/trezor/trezor-ble/src/ble/bonds.c
new file mode 100644
index 0000000000..7725e10ebb
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/bonds.c
@@ -0,0 +1,59 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+#include
+
+#include
+
+#include
+
+#include "ble_internal.h"
+
+#define LOG_MODULE_NAME ble_bonds
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
+
+#include "ble_internal.h"
+
+
+
+bool bonds_erase_all(void) {
+ int err = bt_unpair(BT_ID_DEFAULT, BT_ADDR_LE_ANY);
+ if (err) {
+ LOG_INF("Cannot delete bonds (err: %d)\n", err);
+ return false;
+ } else {
+ bt_le_filter_accept_list_clear();
+ LOG_INF("Bonds deleted successfully \n");
+ return true;
+ }
+}
+
+static void count_bonds(const struct bt_bond_info *info, void *user_data) {
+ int *bond_cnt = (int *)user_data;
+ *bond_cnt += 1;
+}
+
+int bonds_get_count(void) {
+ int bond_cnt = 0;
+
+ bt_foreach_bond(BT_ID_DEFAULT, count_bonds, &bond_cnt);
+
+ return bond_cnt;
+}
diff --git a/west/trezor/trezor-ble/src/ble/connection.c b/west/trezor/trezor-ble/src/ble/connection.c
new file mode 100644
index 0000000000..ca761adae2
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/connection.c
@@ -0,0 +1,110 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+#include
+
+#include
+#include
+#include
+#include
+
+#include
+
+#include "ble_internal.h"
+
+#define LOG_MODULE_NAME ble_connection
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
+
+static struct bt_conn *current_conn;
+
+void connected(struct bt_conn *conn, uint8_t err) {
+ char addr[BT_ADDR_LE_STR_LEN];
+
+ if (err) {
+ LOG_ERR("Connection failed (err %u)", err);
+ return;
+ }
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+ LOG_INF("Connected %s", addr);
+
+ current_conn = bt_conn_ref(conn);
+
+ // struct bt_le_conn_param params = BT_LE_CONN_PARAM_INIT(6,6,0,400);
+ //
+ // bt_conn_le_param_update(conn, ¶ms);
+
+ // err = bt_conn_le_phy_update(current_conn, BT_CONN_LE_PHY_PARAM_2M);
+ // if (err) {
+ // LOG_ERR("Phy update request failed: %d", err);
+ // }
+
+ management_send_status_event();
+}
+
+void disconnected(struct bt_conn *conn, uint8_t reason) {
+ char addr[BT_ADDR_LE_STR_LEN];
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+
+ LOG_INF("Disconnected: %s (reason %u)", addr, reason);
+
+ pairing_reset();
+
+ if (current_conn) {
+ bt_conn_unref(current_conn);
+ current_conn = NULL;
+ }
+
+ management_send_status_event();
+}
+
+static void security_changed(struct bt_conn *conn, bt_security_t level,
+ enum bt_security_err err) {
+ char addr[BT_ADDR_LE_STR_LEN];
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+
+ if (!err) {
+ LOG_INF("Security changed: %s level %u", addr, level);
+ } else {
+ LOG_WRN("Security failed: %s level %u err %d", addr, level, err);
+ }
+}
+
+BT_CONN_CB_DEFINE(conn_callbacks) = {
+ .connected = connected,
+ .disconnected = disconnected,
+ .security_changed = security_changed,
+};
+
+bool connection_init(void) { return true; }
+
+bool connection_is_connected(void) { return current_conn != NULL; }
+
+void connection_disconnect(void) {
+ if (current_conn) {
+ LOG_INF("Remotely disconnected");
+ bt_conn_disconnect(current_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
+ bt_conn_unref(current_conn);
+ }
+}
+
+struct bt_conn *connection_get_current(void) { return current_conn; }
diff --git a/west/trezor/trezor-ble/src/ble/inc/ble/ble.h b/west/trezor/trezor-ble/src/ble/inc/ble/ble.h
new file mode 100644
index 0000000000..3df3990321
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/inc/ble/ble.h
@@ -0,0 +1,25 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include
+
+// Initializes the BLE module
+bool ble_init(void);
diff --git a/west/trezor/trezor-ble/src/ble/management.c b/west/trezor/trezor-ble/src/ble/management.c
new file mode 100644
index 0000000000..a03d7a79f5
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/management.c
@@ -0,0 +1,141 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+#include
+
+#include
+#include
+
+#include
+#include
+
+#include "ble_internal.h"
+
+#include
+
+#define LOG_MODULE_NAME ble_manangement
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
+
+static K_SEM_DEFINE(management_ok, 0, 1);
+
+void management_send_status_event(void) {
+ // ble_version_t version = {0};
+ //
+ // sd_ble_version_get(&version);
+ LOG_WRN(
+ "Sending status event: connected: %d, advertising: %d, "
+ "advertising_whitelist: %d, peer_count: %d",
+ connection_is_connected(), advertising_is_advertising(),
+ advertising_is_advertising_whitelist(), bonds_get_count());
+
+ event_status_msg_t msg = {0};
+ msg.msg_id = INTERNAL_EVENT_STATUS;
+ msg.connected = connection_is_connected();
+ msg.advertising = advertising_is_advertising();
+ msg.advertising_whitelist = advertising_is_advertising_whitelist();
+ msg.peer_count = bonds_get_count();
+ msg.sd_version_number = 0;
+ msg.sd_company_id = 0;
+ msg.sd_subversion_number = 0;
+ msg.app_version = 0;
+ msg.bld_version = 0;
+
+ trz_comm_send_msg(NRF_SERVICE_BLE_MANAGER, (uint8_t *)&msg, sizeof(msg));
+}
+
+void management_send_success_event(void) {
+ uint8_t tx_data[] = {
+ INTERNAL_EVENT_SUCCESS,
+ };
+ trz_comm_send_msg(NRF_SERVICE_BLE_MANAGER, tx_data, sizeof(tx_data));
+}
+
+void management_send_pairing_cancelled_event(void) {
+ uint8_t tx_data[1] = {0};
+
+ tx_data[0] = INTERNAL_EVENT_PAIRING_CANCELLED;
+
+ trz_comm_send_msg(NRF_SERVICE_BLE_MANAGER, tx_data, sizeof(tx_data));
+}
+
+void management_send_pairing_request_event(uint8_t *data, uint16_t len) {
+ uint8_t tx_data[7] = {0};
+
+ tx_data[0] = INTERNAL_EVENT_PAIRING_REQUEST;
+ tx_data[1] = data[0];
+ tx_data[2] = data[1];
+ tx_data[3] = data[2];
+ tx_data[4] = data[3];
+ tx_data[5] = data[4];
+ tx_data[6] = data[5];
+
+ trz_comm_send_msg(NRF_SERVICE_BLE_MANAGER, tx_data, sizeof(tx_data));
+}
+
+static void process_command(uint8_t *data, uint16_t len) {
+ uint8_t cmd = data[0];
+ switch (cmd) {
+ case INTERNAL_CMD_SEND_STATE:
+ management_send_status_event();
+ break;
+ case INTERNAL_CMD_ADVERTISING_ON:
+ advertising_start(data[1] != 0);
+ break;
+ case INTERNAL_CMD_ADVERTISING_OFF:
+ advertising_stop();
+ break;
+ case INTERNAL_CMD_ERASE_BONDS:
+ bonds_erase_all();
+ management_send_success_event();
+ break;
+ case INTERNAL_CMD_DISCONNECT:
+ connection_disconnect();
+ management_send_success_event();
+ case INTERNAL_CMD_ACK:
+ // pb_msg_ack();
+ break;
+ case INTERNAL_CMD_ALLOW_PAIRING:
+ pairing_num_comp_reply(true);
+ management_send_success_event();
+ break;
+ case INTERNAL_CMD_REJECT_PAIRING:
+ pairing_num_comp_reply(false);
+ management_send_success_event();
+ break;
+ default:
+ break;
+ }
+}
+
+void management_init(void) { k_sem_give(&management_ok); }
+
+void management_thread(void) {
+ /* Don't go any further until BLE is initialized */
+ k_sem_take(&management_ok, K_FOREVER);
+
+ for (;;) {
+ trz_packet_t *buf = trz_comm_poll_data(NRF_SERVICE_BLE_MANAGER);
+ process_command(buf->data, buf->len);
+ k_free(buf);
+ }
+}
+
+K_THREAD_DEFINE(management_thread_id, CONFIG_DEFAULT_THREAD_STACK_SIZE,
+ management_thread, NULL, NULL, NULL, 7, 0, 0);
diff --git a/west/trezor/trezor-ble/src/ble/pairing.c b/west/trezor/trezor-ble/src/ble/pairing.c
new file mode 100644
index 0000000000..91c034b3fc
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/pairing.c
@@ -0,0 +1,138 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+#include
+
+#include
+#include
+#include
+#include
+
+#include
+
+#include "ble_internal.h"
+
+#define LOG_MODULE_NAME ble_pairing
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
+
+static struct bt_conn *auth_conn;
+
+void passkey_to_str(uint8_t buf[6], unsigned int passkey) {
+ buf[5] = (passkey % 10) + '0';
+ buf[4] = ((passkey / 10) % 10) + '0';
+ buf[3] = ((passkey / 100) % 10) + '0';
+ buf[2] = ((passkey / 1000) % 10) + '0';
+ buf[1] = ((passkey / 10000) % 10) + '0';
+ buf[0] = ((passkey / 100000) % 10) + '0';
+}
+
+void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) {}
+
+void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey) {
+ char addr[BT_ADDR_LE_STR_LEN];
+
+ auth_conn = bt_conn_ref(conn);
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+
+ uint8_t passkey_str[6];
+ passkey_to_str(passkey_str, passkey);
+ management_send_pairing_request_event(passkey_str, 6);
+
+ management_send_status_event();
+}
+
+void pairing_auth_cancel(struct bt_conn *conn) {
+ char addr[BT_ADDR_LE_STR_LEN];
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+
+ connection_disconnect();
+
+ management_send_pairing_cancelled_event();
+ management_send_status_event();
+
+ LOG_INF("Pairing cancelled: %s", addr);
+}
+
+static struct bt_conn_auth_cb conn_auth_callbacks = {
+ // .pairing_accept = pairing_accept,
+ .passkey_display = auth_passkey_display,
+ .passkey_confirm = auth_passkey_confirm,
+ .cancel = pairing_auth_cancel,
+};
+
+void pairing_complete(struct bt_conn *conn, bool bonded) {
+ char addr[BT_ADDR_LE_STR_LEN];
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+
+ LOG_INF("Pairing completed: %s, bonded: %d", addr, bonded);
+}
+
+void pairing_failed(struct bt_conn *conn, enum bt_security_err reason) {
+ char addr[BT_ADDR_LE_STR_LEN];
+
+ bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
+
+ LOG_INF("Pairing failed conn: %s, reason %d", addr, reason);
+}
+
+static struct bt_conn_auth_info_cb conn_auth_info_callbacks = {
+ .pairing_complete = pairing_complete, .pairing_failed = pairing_failed};
+
+void pairing_num_comp_reply(bool accept) {
+ if (auth_conn != NULL) {
+ if (accept) {
+ bt_conn_auth_passkey_confirm(auth_conn);
+ LOG_INF("Numeric Match, conn %p", (void *)auth_conn);
+ } else {
+ bt_conn_auth_cancel(auth_conn);
+ LOG_INF("Numeric Reject, conn %p", (void *)auth_conn);
+ bt_conn_disconnect(auth_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
+ }
+
+ bt_conn_unref(auth_conn);
+ auth_conn = NULL;
+ }
+}
+
+void pairing_reset(void) {
+ if (auth_conn) {
+ bt_conn_unref(auth_conn);
+ auth_conn = NULL;
+ }
+}
+
+bool pairing_init(void) {
+ int err = bt_conn_auth_cb_register(&conn_auth_callbacks);
+ if (err) {
+ printk("Failed to register authorization callbacks.\n");
+ return false;
+ }
+
+ err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
+ if (err) {
+ printk("Failed to register authorization info callbacks.\n");
+ return false;
+ }
+
+ return true;
+}
diff --git a/west/trezor/trezor-ble/src/ble/service.c b/west/trezor/trezor-ble/src/ble/service.c
new file mode 100644
index 0000000000..0540a72755
--- /dev/null
+++ b/west/trezor/trezor-ble/src/ble/service.c
@@ -0,0 +1,90 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+#include
+#include
+
+#include
+
+#include "ble_internal.h"
+
+LOG_MODULE_REGISTER(ble_service);
+
+static service_received_cb received_cb;
+
+static void service_ccc_cfg_changed(const struct bt_gatt_attr *attr,
+ uint16_t value) {
+ LOG_DBG("Notification has been turned %s",
+ value == BT_GATT_CCC_NOTIFY ? "on" : "off");
+}
+
+static ssize_t on_receive(struct bt_conn *conn, const struct bt_gatt_attr *attr,
+ const void *buf, uint16_t len, uint16_t offset,
+ uint8_t flags) {
+ LOG_DBG("Received data, handle %d, conn %p", attr->handle, (void *)conn);
+
+ if (received_cb != NULL) {
+ received_cb(conn, buf, len);
+ }
+ return len;
+}
+
+static void on_sent(struct bt_conn *conn, void *user_data) {
+ trz_packet_t *data = (trz_packet_t *)user_data;
+
+ k_free(data);
+
+ LOG_DBG("Data send, conn %p", (void *)conn);
+}
+
+/* Trezor Service Declaration */
+BT_GATT_SERVICE_DEFINE(
+ trz_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_TRZ_SERVICE),
+ BT_GATT_CHARACTERISTIC(BT_UUID_TRZ_TX, BT_GATT_CHRC_NOTIFY,
+ BT_GATT_PERM_READ_ENCRYPT, NULL, NULL, NULL),
+ BT_GATT_CCC(service_ccc_cfg_changed,
+ BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
+ BT_GATT_CHARACTERISTIC(BT_UUID_TRZ_RX,
+ BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
+ BT_GATT_PERM_READ_ENCRYPT |
+ BT_GATT_PERM_WRITE_ENCRYPT,
+ NULL, on_receive, NULL), );
+
+int service_init(service_received_cb callback) {
+ received_cb = callback;
+ return 0;
+}
+
+int service_send(struct bt_conn *conn, trz_packet_t *data) {
+ struct bt_gatt_notify_params params = {0};
+ const struct bt_gatt_attr *attr = &trz_svc.attrs[2];
+
+ params.attr = attr;
+ params.data = data->data;
+ params.len = data->len;
+ params.func = on_sent;
+ params.user_data = (void *)data;
+
+ if (conn && bt_gatt_is_subscribed(conn, attr, BT_GATT_CCC_NOTIFY)) {
+ return bt_gatt_notify_cb(conn, ¶ms);
+ } else {
+ return -EINVAL;
+ }
+}
diff --git a/west/trezor/trezor-ble/src/connection.c b/west/trezor/trezor-ble/src/connection.c
deleted file mode 100644
index e10ef2c97e..0000000000
--- a/west/trezor/trezor-ble/src/connection.c
+++ /dev/null
@@ -1,169 +0,0 @@
-
-#include
-#include
-
-#include
-#include
-#include
-#include
-
-#include
-
-#include
-
-#include "advertising.h"
-#include "connection.h"
-#include "int_comm.h"
-
-#define LOG_MODULE_NAME fw_int_connection
-LOG_MODULE_REGISTER(LOG_MODULE_NAME);
-
-static struct bt_conn *current_conn;
-static struct bt_conn *auth_conn;
-
-void connected(struct bt_conn *conn, uint8_t err) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- if (err) {
- LOG_ERR("Connection failed (err %u)", err);
- return;
- }
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
- LOG_INF("Connected %s", addr);
-
- current_conn = bt_conn_ref(conn);
-
- // struct bt_le_conn_param params = BT_LE_CONN_PARAM_INIT(6,6,0,400);
- //
- // bt_conn_le_param_update(conn, ¶ms);
-
- // err = bt_conn_le_phy_update(current_conn, BT_CONN_LE_PHY_PARAM_2M);
- // if (err) {
- // LOG_ERR("Phy update request failed: %d", err);
- // }
-
- send_status_event();
-}
-
-void disconnected(struct bt_conn *conn, uint8_t reason) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
-
- LOG_INF("Disconnected: %s (reason %u)", addr, reason);
-
- if (auth_conn) {
- bt_conn_unref(auth_conn);
- auth_conn = NULL;
- }
-
- if (current_conn) {
- bt_conn_unref(current_conn);
- current_conn = NULL;
- }
-
- send_status_event();
-}
-
-bool is_connected(void) { return current_conn != NULL; }
-
-void disconnect(void) {
- if (current_conn) {
- LOG_INF("Remotely disconnected");
- bt_conn_disconnect(current_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
- bt_conn_unref(current_conn);
- }
-}
-
-void num_comp_reply(bool accept) {
- if (auth_conn != NULL) {
- if (accept) {
- bt_conn_auth_passkey_confirm(auth_conn);
- LOG_INF("Numeric Match, conn %p", (void *)auth_conn);
- } else {
- bt_conn_auth_cancel(auth_conn);
- LOG_INF("Numeric Reject, conn %p", (void *)auth_conn);
- bt_conn_disconnect(auth_conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN);
- }
-
- bt_conn_unref(auth_conn);
- auth_conn = NULL;
- }
-}
-
-void passkey_to_str(uint8_t buf[6], unsigned int passkey) {
- buf[5] = (passkey % 10) + '0';
- buf[4] = ((passkey / 10) % 10) + '0';
- buf[3] = ((passkey / 100) % 10) + '0';
- buf[2] = ((passkey / 1000) % 10) + '0';
- buf[1] = ((passkey / 10000) % 10) + '0';
- buf[0] = ((passkey / 100000) % 10) + '0';
-}
-
-void auth_passkey_display(struct bt_conn *conn, unsigned int passkey) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- uint8_t passkey_str[6];
- passkey_to_str(passkey_str, passkey);
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
-
- // pb_comm_enqueue(PASSKEY_DISPLAY, passkey_str, 6);
-}
-
-void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- auth_conn = bt_conn_ref(conn);
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
-
- uint8_t passkey_str[6];
- passkey_to_str(passkey_str, passkey);
- send_pairing_request_event(passkey_str, 6);
-
- send_status_event();
-}
-
-void auth_cancel(struct bt_conn *conn) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
-
- disconnect();
-
- send_pairing_cancelled_event();
- send_status_event();
-
- LOG_INF("Pairing cancelled: %s", addr);
-}
-
-void pairing_complete(struct bt_conn *conn, bool bonded) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
-
- // oob_signal();
- // bt_le_oob_set_sc_flag(false);
- // bt_le_oob_set_legacy_flag(false);
-
- if (bonded) {
- advertising_setup_wl();
- }
-
- LOG_INF("Pairing completed: %s, bonded: %d", addr, bonded);
-}
-
-void pairing_failed(struct bt_conn *conn, enum bt_security_err reason) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
-
- // oob_signal();
- // bt_le_oob_set_sc_flag(false);
- // bt_le_oob_set_legacy_flag(false);
-
- LOG_INF("Pairing failed conn: %s, reason %d", addr, reason);
-}
-
-struct bt_conn *conn_get_current(void) { return current_conn; }
diff --git a/west/trezor/trezor-ble/src/connection.h b/west/trezor/trezor-ble/src/connection.h
deleted file mode 100644
index c134392fc8..0000000000
--- a/west/trezor/trezor-ble/src/connection.h
+++ /dev/null
@@ -1,30 +0,0 @@
-
-
-#include
-#include
-
-#include
-#include
-
-#include
-#include
-#include
-#include
-
-void connected(struct bt_conn *conn, uint8_t err);
-
-void disconnect(void);
-
-void disconnected(struct bt_conn *conn, uint8_t reason);
-
-bool is_connected(void);
-
-void num_comp_reply(bool accept);
-void auth_passkey_display(struct bt_conn *conn, unsigned int passkey);
-void auth_passkey_confirm(struct bt_conn *conn, unsigned int passkey);
-void auth_cancel(struct bt_conn *conn);
-void pairing_complete(struct bt_conn *conn, bool bonded);
-void pairing_failed(struct bt_conn *conn, enum bt_security_err reason);
-
-void num_comp_reply(bool accept);
-struct bt_conn *conn_get_current(void);
diff --git a/west/trezor/trezor-ble/src/events.c b/west/trezor/trezor-ble/src/events.c
deleted file mode 100644
index a610132ae4..0000000000
--- a/west/trezor/trezor-ble/src/events.c
+++ /dev/null
@@ -1,13 +0,0 @@
-
-
-#include
-
-#define K_POOL_EVENTS_CNT (4)
-
-static struct k_poll_event events[K_POOL_EVENTS_CNT];
-
-void events_poll(void) { k_poll(events, ARRAY_SIZE(events), K_FOREVER); }
-
-void events_init(void) {}
-
-struct k_poll_event* events_get(int idx) { return &events[idx]; }
diff --git a/west/trezor/trezor-ble/src/events.h b/west/trezor/trezor-ble/src/events.h
deleted file mode 100644
index c6a83107ef..0000000000
--- a/west/trezor/trezor-ble/src/events.h
+++ /dev/null
@@ -1,11 +0,0 @@
-
-
-#include
-
-#define INT_COMM_EVENT_NUM 3
-
-void events_poll(void);
-
-void events_init(void);
-
-struct k_poll_event* events_get(int idx);
diff --git a/west/trezor/trezor-ble/src/int_comm.c b/west/trezor/trezor-ble/src/int_comm.c
deleted file mode 100644
index bf1798ea39..0000000000
--- a/west/trezor/trezor-ble/src/int_comm.c
+++ /dev/null
@@ -1,161 +0,0 @@
-
-
-#include
-#include
-
-#include
-#include
-
-#include
-#include
-
-#include "advertising.h"
-#include "connection.h"
-#include "events.h"
-#include "int_comm_defs.h"
-#include "uart.h"
-
-#define LOG_MODULE_NAME fw_int_comm
-LOG_MODULE_REGISTER(LOG_MODULE_NAME);
-
-static K_SEM_DEFINE(int_comm_ok, 0, 1);
-
-void send_packet(uint8_t message_type, const uint8_t *tx_data, uint8_t len) {
- uart_data_t *tx = k_malloc(sizeof(*tx));
-
- if (tx == NULL) {
- LOG_WRN("Not able to allocate UART send data buffer");
- return;
- }
-
- LOG_DBG("ALLOC: Sending UART data");
-
- tx->len = len + OVERHEAD_SIZE;
-
- tx->data[0] = message_type;
- tx->data[1] = tx->len;
- memcpy(&tx->data[COMM_HEADER_SIZE], tx_data, len);
-
- uint8_t crc = crc8(tx->data, tx->len - 1, 0x07, 0x00, false);
-
- tx->data[tx->len - 1] = crc;
-
- uart_send(tx);
-}
-
-void send_status_event(void) {
- // ble_version_t version = {0};
- //
- // sd_ble_version_get(&version);
- LOG_WRN(
- "Sending status event: connected: %d, advertising: %d, "
- "advertising_whitelist: %d, peer_count: %d",
- is_connected(), is_advertising(), is_advertising_whitelist(),
- advertising_get_bond_count());
-
- event_status_msg_t msg = {0};
- msg.msg_id = INTERNAL_EVENT_STATUS;
- msg.connected = is_connected();
- msg.advertising = is_advertising();
- msg.advertising_whitelist = is_advertising_whitelist();
- msg.peer_count = advertising_get_bond_count();
- msg.sd_version_number = 0;
- msg.sd_company_id = 0;
- msg.sd_subversion_number = 0;
- msg.app_version = 0;
- msg.bld_version = 0;
-
- send_packet(INTERNAL_EVENT, (uint8_t *)&msg, sizeof(msg));
-}
-
-void send_success_event(void) {
- uint8_t tx_data[] = {
- INTERNAL_EVENT_SUCCESS,
- };
- send_packet(INTERNAL_EVENT, tx_data, sizeof(tx_data));
-}
-
-void send_pairing_cancelled_event(void) {
- uint8_t tx_data[1] = {0};
-
- tx_data[0] = INTERNAL_EVENT_PAIRING_CANCELLED;
-
- send_packet(INTERNAL_EVENT, tx_data, sizeof(tx_data));
-}
-
-void send_pairing_request_event(uint8_t *data, uint16_t len) {
- uint8_t tx_data[7] = {0};
-
- tx_data[0] = INTERNAL_EVENT_PAIRING_REQUEST;
- tx_data[1] = data[0];
- tx_data[2] = data[1];
- tx_data[3] = data[2];
- tx_data[4] = data[3];
- tx_data[5] = data[4];
- tx_data[6] = data[5];
-
- send_packet(INTERNAL_EVENT, tx_data, sizeof(tx_data));
-}
-
-uint16_t get_message_type(const uint8_t *rx_data) {
- return (rx_data[3] << 8) | rx_data[4];
-}
-
-void process_command(uint8_t *data, uint16_t len) {
- uint8_t cmd = data[0];
- switch (cmd) {
- case INTERNAL_CMD_SEND_STATE:
- send_status_event();
- break;
- case INTERNAL_CMD_ADVERTISING_ON:
- advertising_start(data[1] != 0);
- break;
- case INTERNAL_CMD_ADVERTISING_OFF:
- advertising_stop();
- break;
- case INTERNAL_CMD_ERASE_BONDS:
- erase_bonds();
- send_success_event();
- break;
- case INTERNAL_CMD_DISCONNECT:
- disconnect();
- send_success_event();
- case INTERNAL_CMD_ACK:
- // pb_msg_ack();
- break;
- case INTERNAL_CMD_ALLOW_PAIRING:
- num_comp_reply(true);
- send_success_event();
- break;
- case INTERNAL_CMD_REJECT_PAIRING:
- num_comp_reply(false);
- send_success_event();
- break;
- default:
- break;
- }
-}
-
-void int_comm_start(void) { k_sem_give(&int_comm_ok); }
-
-void int_comm_thread(void) {
- /* Don't go any further until BLE is initialized */
- k_sem_take(&int_comm_ok, K_FOREVER);
-
- for (;;) {
- // events_poll();
-
- // if (events_get(INT_COMM_EVENT_NUM)->state == K_POLL_STATE_SIGNALED) {
-
- uart_data_t *buf = uart_get_data_int();
- process_command(buf->data, buf->len);
- k_free(buf);
-
- // k_poll_signal_reset(events_get(INT_COMM_EVENT_NUM)->signal);
- // events_get(INT_COMM_EVENT_NUM)->state = K_POLL_STATE_NOT_READY;
- //}
- }
-}
-
-K_THREAD_DEFINE(int_comm_thread_id, CONFIG_BT_NUS_THREAD_STACK_SIZE,
- int_comm_thread, NULL, NULL, NULL, 7, 0, 0);
diff --git a/west/trezor/trezor-ble/src/int_comm.h b/west/trezor/trezor-ble/src/int_comm.h
deleted file mode 100644
index ace1d2a2b2..0000000000
--- a/west/trezor/trezor-ble/src/int_comm.h
+++ /dev/null
@@ -1,21 +0,0 @@
-#ifndef INT_COMM__
-#define INT_COMM__
-
-#include
-
-void process_command(uint8_t *data, uint16_t len);
-
-void send_status_event(void);
-
-void send_pairing_request_event(uint8_t *data, uint16_t len);
-void send_pairing_cancelled_event(void);
-
-void int_comm_start(void);
-
-void int_comm_thread(void);
-
-void send_packet(uint8_t message_type, const uint8_t *tx_data, uint16_t len);
-
-void pb_msg_ack(void);
-
-#endif
diff --git a/west/trezor/trezor-ble/src/int_comm_defs.h b/west/trezor/trezor-ble/src/int_comm_defs.h
deleted file mode 100644
index dbd9a43ee6..0000000000
--- a/west/trezor/trezor-ble/src/int_comm_defs.h
+++ /dev/null
@@ -1,52 +0,0 @@
-
-#ifndef __INT_COMM_DEFS__
-#define __INT_COMM_DEFS__
-
-#define BLE_PACKET_SIZE (244)
-#define USB_DATA_SIZE (64)
-
-#define COMM_HEADER_SIZE (2)
-#define COMM_FOOTER_SIZE (1)
-#define OVERHEAD_SIZE (COMM_HEADER_SIZE + COMM_FOOTER_SIZE)
-#define UART_PACKET_SIZE (USB_DATA_SIZE + OVERHEAD_SIZE)
-
-#define EXTERNAL_MESSAGE (0xA0)
-#define INTERNAL_EVENT (0xA1)
-
-typedef struct {
- uint8_t msg_id;
- uint8_t connected;
- uint8_t advertising;
- uint8_t advertising_whitelist;
-
- uint8_t peer_count;
- uint8_t reserved[2];
- uint8_t sd_version_number;
-
- uint16_t sd_company_id;
- uint16_t sd_subversion_number;
-
- uint32_t app_version;
- uint32_t bld_version;
-
-} event_status_msg_t;
-
-typedef enum {
- INTERNAL_EVENT_STATUS = 0x01,
- INTERNAL_EVENT_SUCCESS = 0x02,
- INTERNAL_EVENT_FAILURE = 0x03,
- INTERNAL_EVENT_PAIRING_REQUEST = 0x04,
- INTERNAL_EVENT_PAIRING_CANCELLED = 0x05,
-} InternalEvent_t;
-
-typedef enum {
- INTERNAL_CMD_SEND_STATE = 0x00,
- INTERNAL_CMD_ADVERTISING_ON = 0x01,
- INTERNAL_CMD_ADVERTISING_OFF = 0x02,
- INTERNAL_CMD_ERASE_BONDS = 0x03,
- INTERNAL_CMD_DISCONNECT = 0x04,
- INTERNAL_CMD_ACK = 0x05,
- INTERNAL_CMD_ALLOW_PAIRING = 0x06,
- INTERNAL_CMD_REJECT_PAIRING = 0x07,
-} InternalCmd_t;
-#endif
diff --git a/west/trezor/trezor-ble/src/main.c b/west/trezor/trezor-ble/src/main.c
index b3ec826300..2cc7acb88f 100644
--- a/west/trezor/trezor-ble/src/main.c
+++ b/west/trezor/trezor-ble/src/main.c
@@ -1,11 +1,20 @@
/*
- * Copyright (c) 2018 Nordic Semiconductor ASA
+ * This file is part of the Trezor project, https://trezor.io/
*
- * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
- */
-
-/** @file
- * @brief Nordic UART Bridge Service (NUS) sample
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
*/
#include
@@ -29,234 +38,25 @@
#include
-#include "advertising.h"
-#include "connection.h"
-#include "events.h"
-#include "int_comm.h"
-#include "spi.h"
-#include "trz_nus.h"
-#include "uart.h"
+#include
+#include
+#include
-#define LOG_MODULE_NAME fw
+#define LOG_MODULE_NAME main
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
-#define STACKSIZE CONFIG_BT_NUS_THREAD_STACK_SIZE
-#define PRIORITY 7
-
-#define RUN_STATUS_LED DK_LED1
-#define RUN_LED_BLINK_INTERVAL 1000
-
-#define FW_RUNNING_SIG DK_LED2
-
-static K_SEM_DEFINE(ble_init_ok, 0, 1);
-static K_SEM_DEFINE(led_init_ok, 0, 1);
-
-#define AUTH_SC_FLAG 0x08
-
-static void security_changed(struct bt_conn *conn, bt_security_t level,
- enum bt_security_err err) {
- char addr[BT_ADDR_LE_STR_LEN];
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, sizeof(addr));
-
- if (!err) {
- LOG_INF("Security changed: %s level %u", addr, level);
- } else {
- LOG_WRN("Security failed: %s level %u err %d", addr, level, err);
- }
-}
-
-BT_CONN_CB_DEFINE(conn_callbacks) = {
- .connected = connected,
- .disconnected = disconnected,
- .security_changed = security_changed,
-};
-
-// static enum bt_security_err pairing_accept(struct bt_conn *conn,
-// const struct bt_conn_pairing_feat
-// *const feat)
-//{
-// if (feat->oob_data_flag && (!(feat->auth_req & AUTH_SC_FLAG))) {
-// bt_le_oob_set_legacy_flag(true);
-// }
-//
-// return BT_SECURITY_ERR_SUCCESS;
-//
-// }
-
-static struct bt_conn_auth_cb conn_auth_callbacks = {
- // .pairing_accept = pairing_accept,
- .passkey_display = auth_passkey_display,
- .passkey_confirm = auth_passkey_confirm,
- .cancel = auth_cancel,
-};
-
-static struct bt_conn_auth_info_cb conn_auth_info_callbacks = {
- .pairing_complete = pairing_complete, .pairing_failed = pairing_failed};
-
-static void bt_receive_cb(struct bt_conn *conn, const uint8_t *const data,
- uint16_t len) {
- if ((dk_get_buttons() & DK_BTN2_MSK) == 0) {
- LOG_INF("Trezor not ready, rejecting data");
- // send_error_response();
- return;
- }
-
- char addr[BT_ADDR_LE_STR_LEN] = {0};
-
- bt_addr_le_to_str(bt_conn_get_dst(conn), addr, ARRAY_SIZE(addr));
-
- LOG_DBG("Received data from: %s, %d", addr, len);
-
- spi_send(data, len);
-}
-
-static struct bt_nus_cb nus_cb = {
- .received = bt_receive_cb,
-};
-
-void error(void) {
- dk_set_leds_state(DK_ALL_LEDS_MSK, DK_NO_LEDS_MSK);
-
- while (true) {
- /* Spin for ever */
- k_sleep(K_MSEC(1000));
- }
-}
-
-void button_changed(uint32_t button_state, uint32_t has_changed) {}
-
-static void configure_gpio(void) {
- int err;
-
- err = dk_buttons_init(button_changed);
- if (err) {
- LOG_ERR("Cannot init buttons (err: %d)", err);
- }
-
- err = dk_leds_init();
- if (err) {
- LOG_ERR("Cannot init LEDs (err: %d)", err);
- }
-}
-
int main(void) {
- int err = 0;
-
LOG_INF("Initializing");
- configure_gpio();
+ signals_init();
- err = uart_init();
- if (err) {
- error();
- }
+ trz_comm_init();
- spi_init();
+ ble_init();
- err = bt_conn_auth_cb_register(&conn_auth_callbacks);
- if (err) {
- printk("Failed to register authorization callbacks.\n");
- return 0;
- }
-
- err = bt_conn_auth_info_cb_register(&conn_auth_info_callbacks);
- if (err) {
- printk("Failed to register authorization info callbacks.\n");
- return 0;
- }
-
- err = bt_enable(NULL);
- if (err) {
- error();
- }
-
- LOG_INF("Bluetooth initialized");
-
- k_sem_give(&ble_init_ok);
-
- if (IS_ENABLED(CONFIG_SETTINGS)) {
- settings_load();
- }
-
- err = bt_nus_init(&nus_cb);
- if (err) {
- LOG_ERR("Failed to initialize UART service (err: %d)", err);
- return 0;
- }
-
- bt_set_name("TrezorGAP");
-
- events_init();
- advertising_init();
- int_comm_start();
-
- dk_set_led(FW_RUNNING_SIG, 1);
- // dk_set_led(FW_RUNNING_SIG, 0);
- // while(true) {
- // dk_set_led(FW_RUNNING_SIG, 1);
- // dk_set_led(FW_RUNNING_SIG, 0);
- // }
- send_status_event();
-
- // oob_init();
-
- k_sem_give(&led_init_ok);
+ signals_fw_running(true);
for (;;) {
- events_poll();
- printk("Event occurred\n");
-
- // oob_process();
- // int_comm_thread();
+ k_sleep(K_FOREVER);
}
}
-
-void ble_write_thread(void) {
- /* Don't go any further until BLE is initialized */
- k_sem_take(&ble_init_ok, K_FOREVER);
-
- for (;;) {
- /* Wait indefinitely for data to be sent over bluetooth */
- uart_data_t *buf = uart_get_data_ext();
-
- if (bt_nus_send(conn_get_current(), buf)) {
- LOG_WRN("Failed to send data over BLE connection: %d", buf->len);
- k_free(buf);
- }
-
- LOG_DBG("Freeing UART data");
- }
-}
-
-void led_thread(void) {
-// bool connected = false;
- int blink_status = 0;
- /* Don't go any further until BLE is initialized */
- k_sem_take(&led_init_ok, K_FOREVER);
-
- for (;;) {
- blink_status++;
- dk_set_led(RUN_STATUS_LED, (blink_status) % 2);
-
-// connected = is_connected();
-//
-// if (connected) {
-// dk_set_led_on(CON_STATUS_LED);
-// } else {
-// if (is_advertising() && !is_advertising_whitelist()) {
-// dk_set_led(CON_STATUS_LED, (blink_status) % 2);
-// } else {
-// dk_set_led_off(CON_STATUS_LED);
-// }
-// }
-
- k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
- }
-}
-
-K_THREAD_DEFINE(ble_write_thread_id, STACKSIZE, ble_write_thread, NULL, NULL,
- NULL, PRIORITY, 0, 0);
-
-K_THREAD_DEFINE(led_thread_id, STACKSIZE, led_thread, NULL, NULL, NULL,
- PRIORITY, 0, 0);
diff --git a/west/trezor/trezor-ble/src/oob.c b/west/trezor/trezor-ble/src/oob.c
deleted file mode 100644
index 81de6bc732..0000000000
--- a/west/trezor/trezor-ble/src/oob.c
+++ /dev/null
@@ -1,499 +0,0 @@
-
-#include
-
-#include
-#include
-#include
-#include
-
-#include
-#include
-
-#include
-#include
-#include
-#include
-#include
-
-#include
-#include
-
-#include "events.h"
-
-#define NDEF_MSG_BUF_SIZE 256
-#define AUTH_SC_FLAG 0x08
-
-// #define NFC_FIELD_LED DK_LED2
-// #define CON_STATUS_LED DK_LED1
-//
-// #define KEY_BOND_REMOVE_MASK DK_BTN4_MSK
-
-#define NFC_NDEF_LE_OOB_REC_PARSER_BUFF_SIZE 150
-#define NFC_TNEP_BUFFER_SIZE 1024
-
-static struct bt_le_oob oob_local;
-static struct k_work adv_work;
-static uint8_t conn_cnt;
-static uint8_t tk_value[NFC_NDEF_LE_OOB_REC_TK_LEN];
-static uint8_t remote_tk_value[NFC_NDEF_LE_OOB_REC_TK_LEN];
-static struct bt_le_oob oob_remote;
-
-/* Bonded address queue. */
-K_MSGQ_DEFINE(bonds_queue, sizeof(bt_addr_le_t), CONFIG_BT_MAX_PAIRED, 4);
-
-static struct k_poll_signal pair_signal;
-static uint8_t tnep_buffer[NFC_TNEP_BUFFER_SIZE];
-static uint8_t tnep_swap_buffer[NFC_TNEP_BUFFER_SIZE];
-static bool use_remote_tk;
-static bool adv_permission;
-
-static int tk_value_generate(void) {
- int err;
-
- err = bt_rand(tk_value, sizeof(tk_value));
- if (err) {
- printk("Random TK value generation failed: %d\n", err);
- }
-
- return err;
-}
-
-static void pair_key_generate_init(void) {
- k_poll_signal_init(&pair_signal);
- k_poll_event_init(events_get(NFC_TNEP_EVENTS_NUMBER), K_POLL_TYPE_SIGNAL,
- K_POLL_MODE_NOTIFY_ONLY, &pair_signal);
-}
-
-static int paring_key_generate(void) {
- int err;
-
- printk("Generating new pairing keys\n");
-
- err = bt_le_oob_get_local(BT_ID_DEFAULT, &oob_local);
- if (err) {
- printk("Error while fetching local OOB data: %d\n", err);
- }
-
- return tk_value_generate();
-}
-
-static void paring_key_process(void) {
- int err;
-
- if (events_get(NFC_TNEP_EVENTS_NUMBER)->state == K_POLL_STATE_SIGNALED) {
- err = paring_key_generate();
- if (err) {
- printk("Pairing key generation error: %d\n", err);
- }
-
- k_poll_signal_reset(events_get(NFC_TNEP_EVENTS_NUMBER)->signal);
- events_get(NFC_TNEP_EVENTS_NUMBER)->state = K_POLL_STATE_NOT_READY;
- }
-}
-
-static void bond_find(const struct bt_bond_info *info, void *user_data) {
- int err;
- struct bt_conn *conn;
-
- /* Filter already connected peers. */
- conn = bt_conn_lookup_addr_le(BT_ID_DEFAULT, &info->addr);
- if (conn) {
- bt_conn_unref(conn);
- return;
- }
-
- err = k_msgq_put(&bonds_queue, (void *)&info->addr, K_NO_WAIT);
- if (err) {
- printk("No space in the queue for the bond\n");
- }
-}
-
-/**
- * @brief Callback function for handling NFC events.
- */
-static void nfc_callback(void *context, nfc_t4t_event_t event,
- const uint8_t *data, size_t data_length,
- uint32_t flags) {
- ARG_UNUSED(context);
- ARG_UNUSED(data);
- ARG_UNUSED(flags);
-
- switch (event) {
- case NFC_T4T_EVENT_FIELD_ON:
- printk("NFC callback 1\n");
- nfc_tnep_tag_on_selected();
- // dk_set_led_on(NFC_FIELD_LED);
-
- // adv_permission = true;
-
- break;
-
- case NFC_T4T_EVENT_FIELD_OFF:
- printk("NFC callback 2\n");
- nfc_tnep_tag_on_selected();
- // dk_set_led_off(NFC_FIELD_LED);
- break;
-
- case NFC_T4T_EVENT_NDEF_READ:
- printk("NFC callback 3\n");
- // if (adv_permission) {
- // advertising_start();
- // adv_permission = false;
- // }
-
- break;
-
- case NFC_T4T_EVENT_NDEF_UPDATED:
- printk("NFC callback 4\n");
- if (data_length > 0) {
- nfc_tnep_tag_rx_msg_indicate(nfc_t4t_ndef_file_msg_get(data),
- data_length);
- }
-
- default:
- break;
- }
-}
-
-/** .. include_startingpoint_pair_msg_rst */
-static int tnep_initial_msg_encode(struct nfc_ndef_msg_desc *msg) {
- int err;
- struct nfc_ndef_ch_msg_records ch_records;
- static struct nfc_ndef_le_oob_rec_payload_desc rec_payload;
-
- NFC_NDEF_LE_OOB_RECORD_DESC_DEF(oob_rec, '0', &rec_payload);
- NFC_NDEF_CH_AC_RECORD_DESC_DEF(oob_ac, NFC_AC_CPS_ACTIVE, 1, "0", 0);
- NFC_NDEF_CH_HS_RECORD_DESC_DEF(hs_rec, NFC_NDEF_CH_MSG_MAJOR_VER,
- NFC_NDEF_CH_MSG_MINOR_VER, 1);
-
- memset(&rec_payload, 0, sizeof(rec_payload));
-
- rec_payload.addr = &oob_local.addr;
- rec_payload.le_sc_data = &oob_local.le_sc_data;
- rec_payload.tk_value = tk_value;
- rec_payload.local_name = bt_get_name();
- rec_payload.le_role =
- NFC_NDEF_LE_OOB_REC_LE_ROLE(NFC_NDEF_LE_OOB_REC_LE_ROLE_PERIPH_ONLY);
- rec_payload.appearance =
- NFC_NDEF_LE_OOB_REC_APPEARANCE(CONFIG_BT_DEVICE_APPEARANCE);
- rec_payload.flags = NFC_NDEF_LE_OOB_REC_FLAGS(BT_LE_AD_NO_BREDR);
-
- ch_records.ac = &NFC_NDEF_CH_AC_RECORD_DESC(oob_ac);
- ch_records.carrier = &NFC_NDEF_LE_OOB_RECORD_DESC(oob_rec);
- ch_records.cnt = 1;
-
- err = nfc_ndef_ch_msg_hs_create(msg, &NFC_NDEF_CH_RECORD_DESC(hs_rec),
- &ch_records);
-
- printk("mac0: %X:%X:%X:%X:%X:%X\n", rec_payload.addr->a.val[5],
- rec_payload.addr->a.val[4], rec_payload.addr->a.val[3],
- rec_payload.addr->a.val[2], rec_payload.addr->a.val[1],
- rec_payload.addr->a.val[0]);
-
- if (err) {
- return err;
- }
-
- return nfc_tnep_initial_msg_encode(msg, NULL, 0);
-}
-/** .. include_endpoint_pair_msg_rst */
-
-static int check_oob_carrier(const struct nfc_tnep_ch_record *ch_record,
- const struct nfc_ndef_record_desc **oob_data) {
- const struct nfc_ndef_ch_ac_rec *ac_rec = NULL;
-
- for (size_t i = 0; i < ch_record->count; i++) {
- if (nfc_ndef_le_oob_rec_check(ch_record->carrier[i])) {
- *oob_data = ch_record->carrier[i];
- }
- }
-
- if (!oob_data) {
- printk("Connection Handover Requester not supporting OOB BLE\n");
- return -EINVAL;
- }
-
- /* Look for the corresponding Alternative Carrier Record. */
- for (size_t i = 0; i < ch_record->count; i++) {
- if (((*oob_data)->id_length == ch_record->ac[i].carrier_data_ref.length) &&
- (memcmp((*oob_data)->id, ch_record->ac[i].carrier_data_ref.data,
- (*oob_data)->id_length) == 0)) {
- ac_rec = &ch_record->ac[i];
- }
- }
-
- if (!ac_rec) {
- printk("No Alternative Carrier Record for OOB LE carrier\n");
- return -EINVAL;
- }
-
- /* Check carrier state */
- if ((ac_rec->cps != NFC_AC_CPS_ACTIVE) &&
- (ac_rec->cps != NFC_AC_CPS_ACTIVATING)) {
- printk("LE OBB Carrier inactive\n");
- return -EINVAL;
- }
-
- return 0;
-}
-
-static void lesc_oob_data_set(struct bt_conn *conn,
- struct bt_conn_oob_info *oob_info) {
- int err;
- char addr[BT_ADDR_LE_STR_LEN];
- struct bt_conn_info info;
-
- err = bt_conn_get_info(conn, &info);
- if (err) {
- return;
- }
-
- struct bt_le_oob_sc_data *oob_data_local =
- oob_info->lesc.oob_config != BT_CONN_OOB_REMOTE_ONLY
- ? &oob_local.le_sc_data
- : NULL;
- struct bt_le_oob_sc_data *oob_data_remote =
- oob_info->lesc.oob_config != BT_CONN_OOB_LOCAL_ONLY
- ? &oob_remote.le_sc_data
- : NULL;
-
- if (oob_data_remote && bt_addr_le_cmp(info.le.remote, &oob_remote.addr)) {
- bt_addr_le_to_str(info.le.remote, addr, sizeof(addr));
- printk("No OOB data available for remote %s", addr);
- bt_conn_auth_cancel(conn);
- return;
- }
-
- if (oob_data_local && bt_addr_le_cmp(info.le.local, &oob_local.addr)) {
- bt_addr_le_to_str(info.le.local, addr, sizeof(addr));
- printk("No OOB data available for local %s", addr);
- bt_conn_auth_cancel(conn);
- return;
- }
-
- err = bt_le_oob_set_sc_data(conn, oob_data_local, oob_data_remote);
- if (err) {
- printk("Error while setting OOB data: %d\n", err);
- }
-}
-
-// static void legacy_tk_value_set(struct bt_conn *conn)
-//{
-// int err;
-// const uint8_t *tk = use_remote_tk ? remote_tk_value : tk_value;
-//
-// err = bt_le_oob_set_legacy_tk(conn, tk);
-// if (err) {
-// printk("TK value set error: %d\n", err);
-// }
-//
-// use_remote_tk = false;
-// }
-
-void auth_oob_data_request(struct bt_conn *conn,
- struct bt_conn_oob_info *info) {
- printk("OOB data requested\n");
-
- if (info->type == BT_CONN_OOB_LE_SC) {
- printk("LESC OOB data requested\n");
- lesc_oob_data_set(conn, info);
- }
-
- // if (info->type == BT_CONN_OOB_LE_LEGACY) {
- // printk("Legacy TK value requested\n");
- // legacy_tk_value_set(conn);
- // }
-}
-
-static int oob_le_data_handle(const struct nfc_ndef_record_desc *rec,
- bool request) {
- int err;
- const struct nfc_ndef_le_oob_rec_payload_desc *oob;
- uint8_t desc_buf[NFC_NDEF_LE_OOB_REC_PARSER_BUFF_SIZE];
- uint32_t desc_buf_len = sizeof(desc_buf);
-
- err = nfc_ndef_le_oob_rec_parse(rec, desc_buf, &desc_buf_len);
- if (err) {
- printk("Error during NDEF LE OOB Record parsing, err: %d.\n", err);
- }
-
- oob = (struct nfc_ndef_le_oob_rec_payload_desc *)desc_buf;
-
- nfc_ndef_le_oob_rec_printout(oob);
-
- if ((*oob->le_role != NFC_NDEF_LE_OOB_REC_LE_ROLE_CENTRAL_ONLY) &&
- (*oob->le_role != NFC_NDEF_LE_OOB_REC_LE_ROLE_CENTRAL_PREFFERED)) {
- printk("Unsupported Device LE Role\n");
- return -EINVAL;
- }
-
- if (oob->le_sc_data) {
- bt_le_oob_set_sc_flag(true);
- oob_remote.le_sc_data = *oob->le_sc_data;
- bt_addr_le_copy(&oob_remote.addr, oob->addr);
- }
-
- if (oob->tk_value) {
- bt_le_oob_set_legacy_flag(true);
- memcpy(remote_tk_value, oob->tk_value, sizeof(remote_tk_value));
- use_remote_tk = request;
- }
-
- // advertising_start();
-
- return 0;
-}
-
-/** .. include_startingpoint_nfc_tnep_ch_tag_rst */
-static int carrier_prepare(void) {
- static struct nfc_ndef_le_oob_rec_payload_desc rec_payload;
-
- NFC_NDEF_LE_OOB_RECORD_DESC_DEF(oob_rec, '0', &rec_payload);
- NFC_NDEF_CH_AC_RECORD_DESC_DEF(oob_ac, NFC_AC_CPS_ACTIVE, 1, "0", 0);
-
- memset(&rec_payload, 0, sizeof(rec_payload));
-
- rec_payload.addr = &oob_local.addr;
- rec_payload.le_sc_data = &oob_local.le_sc_data;
- rec_payload.tk_value = tk_value;
- rec_payload.local_name = bt_get_name();
- rec_payload.le_role =
- NFC_NDEF_LE_OOB_REC_LE_ROLE(NFC_NDEF_LE_OOB_REC_LE_ROLE_PERIPH_ONLY);
- rec_payload.appearance =
- NFC_NDEF_LE_OOB_REC_APPEARANCE(CONFIG_BT_DEVICE_APPEARANCE);
- rec_payload.flags = NFC_NDEF_LE_OOB_REC_FLAGS(BT_LE_AD_NO_BREDR);
-
- return nfc_tnep_ch_carrier_set(&NFC_NDEF_CH_AC_RECORD_DESC(oob_ac),
- &NFC_NDEF_LE_OOB_RECORD_DESC(oob_rec), 1);
-}
-
-#if defined(CONFIG_NFC_TAG_CH_REQUESTER)
-static int tnep_ch_request_prepare(void) {
- bt_le_adv_stop();
- return carrier_prepare();
-}
-
-static int tnep_ch_select_received(const struct nfc_tnep_ch_record *ch_select,
- bool inactive) {
- int err;
- const struct nfc_ndef_record_desc *oob_data = NULL;
-
- if (!ch_select->count) {
- return -EINVAL;
- }
-
- /* All alternative carrier are inactive */
- if (inactive) {
- /* Try send request again. */
- return carrier_prepare();
- }
-
- err = check_oob_carrier(ch_select, &oob_data);
- if (err) {
- return err;
- }
-
- err = oob_le_data_handle(oob_data, false);
- if (err) {
- return err;
- }
-
- return 0;
-}
-#endif /* defined(CONFIG_NFC_TAG_CH_REQUESTER) */
-
-static int tnep_ch_request_received(const struct nfc_tnep_ch_request *ch_req) {
- int err;
- const struct nfc_ndef_record_desc *oob_data = NULL;
-
- if (!ch_req->ch_record.count) {
- return -EINVAL;
- }
-
- err = check_oob_carrier(&ch_req->ch_record, &oob_data);
- if (err) {
- return err;
- }
-
- bt_le_adv_stop();
-
- err = oob_le_data_handle(oob_data, true);
- if (err) {
- return err;
- }
-
- return carrier_prepare();
-}
-
-static struct nfc_tnep_ch_cb ch_cb = {
-#if defined(CONFIG_NFC_TAG_CH_REQUESTER)
- .request_msg_prepare = tnep_ch_request_prepare,
- .select_msg_recv = tnep_ch_select_received,
-#endif
- .request_msg_recv = tnep_ch_request_received};
-/** .. include_endpoint_nfc_tnep_ch_tag_rst */
-
-static void nfc_init(void) {
- int err;
-
- /* TNEP init */
- err = nfc_tnep_tag_tx_msg_buffer_register(tnep_buffer, tnep_swap_buffer,
- sizeof(tnep_buffer));
- if (err) {
- printk("Cannot register tnep buffer, err: %d\n", err);
- return;
- }
-
- err = nfc_tnep_tag_init(events_get(0), NFC_TNEP_EVENTS_NUMBER,
- nfc_t4t_ndef_rwpayload_set);
- if (err) {
- printk("Cannot initialize TNEP protocol, err: %d\n", err);
- return;
- }
-
- /* Set up NFC */
- err = nfc_t4t_setup(nfc_callback, NULL);
- if (err) {
- printk("Cannot setup NFC T4T library!\n");
- return;
- }
-
- err = nfc_tnep_tag_initial_msg_create(2, tnep_initial_msg_encode);
- if (err) {
- printk("Cannot create initial TNEP message, err: %d\n", err);
- }
-
- err = nfc_tnep_ch_service_init(&ch_cb);
- if (err) {
- printk("TNEP CH Service init error: %d\n", err);
- return;
- }
-
- /* Start sensing NFC field */
- err = nfc_t4t_emulation_start();
- if (err) {
- printk("Cannot start emulation!\n");
- return;
- }
-
- printk("NFC configuration done\n");
-}
-
-void oob_init(void) {
- paring_key_generate();
- pair_key_generate_init();
- nfc_init();
-}
-
-void oob_process(void) {
- nfc_tnep_tag_process();
- paring_key_process();
-}
-
-// K_THREAD_DEFINE(oob_thread_id, CONFIG_BT_NUS_THREAD_STACK_SIZE, oob_thread,
-// NULL, NULL,
-// NULL, 7, 0, 0);
-
-void oob_signal(void) { k_poll_signal_raise(&pair_signal, 0); }
-
-void oob_fetch_addr(void) { bt_le_oob_get_local(BT_ID_DEFAULT, &oob_local); }
diff --git a/west/trezor/trezor-ble/src/oob.h b/west/trezor/trezor-ble/src/oob.h
deleted file mode 100644
index c3865be55f..0000000000
--- a/west/trezor/trezor-ble/src/oob.h
+++ /dev/null
@@ -1,14 +0,0 @@
-
-
-#include
-#include
-
-void auth_oob_data_request(struct bt_conn *conn, struct bt_conn_oob_info *info);
-
-void oob_init(void);
-
-void oob_signal(void);
-
-void oob_process(void);
-
-void oob_fetch_addr(void);
diff --git a/west/trezor/trezor-ble/src/signals/inc/signals/signals.h b/west/trezor/trezor-ble/src/signals/inc/signals/signals.h
new file mode 100644
index 0000000000..1766d38b4f
--- /dev/null
+++ b/west/trezor/trezor-ble/src/signals/inc/signals/signals.h
@@ -0,0 +1,31 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include
+
+// Initializes the Signals module
+bool signals_init(void);
+
+// Checks if the TRZ is ready to communicate
+bool signals_is_trz_ready(void);
+
+// Signals that NRF firmware is running and initialized
+void signals_fw_running(bool set);
diff --git a/west/trezor/trezor-ble/src/signals/signals.c b/west/trezor/trezor-ble/src/signals/signals.c
new file mode 100644
index 0000000000..141359a2ec
--- /dev/null
+++ b/west/trezor/trezor-ble/src/signals/signals.c
@@ -0,0 +1,95 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+#include
+#include
+#include
+#include
+
+#include
+
+#include
+
+#define LOG_MODULE_NAME signals
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
+
+#define RUN_STATUS_LED DK_LED1
+#define RUN_LED_BLINK_INTERVAL 1000
+
+#define FW_RUNNING_SIG DK_LED2
+
+static K_SEM_DEFINE(led_init_ok, 0, 1);
+
+void button_changed(uint32_t button_state, uint32_t has_changed) {}
+
+static void configure_gpio(void) {
+ int err;
+
+ err = dk_buttons_init(button_changed);
+ if (err) {
+ LOG_ERR("Cannot init buttons (err: %d)", err);
+ }
+
+ err = dk_leds_init();
+ if (err) {
+ LOG_ERR("Cannot init LEDs (err: %d)", err);
+ }
+}
+
+bool signals_is_trz_ready(void) {
+ return (dk_get_buttons() & DK_BTN2_MSK) != 0;
+}
+
+bool signals_init(void) {
+ configure_gpio();
+
+ k_sem_give(&led_init_ok);
+
+ return true;
+}
+
+void signals_fw_running(bool set) { dk_set_led(FW_RUNNING_SIG, set); }
+
+void led_thread(void) {
+ // bool connected = false;
+ int blink_status = 0;
+ /* Don't go any further until BLE is initialized */
+ k_sem_take(&led_init_ok, K_FOREVER);
+
+ for (;;) {
+ blink_status++;
+ dk_set_led(RUN_STATUS_LED, (blink_status) % 2);
+
+ // connected = is_connected();
+ //
+ // if (connected) {
+ // dk_set_led_on(CON_STATUS_LED);
+ // } else {
+ // if (is_advertising() && !is_advertising_whitelist()) {
+ // dk_set_led(CON_STATUS_LED, (blink_status) % 2);
+ // } else {
+ // dk_set_led_off(CON_STATUS_LED);
+ // }
+ // }
+
+ k_sleep(K_MSEC(RUN_LED_BLINK_INTERVAL));
+ }
+}
+
+K_THREAD_DEFINE(led_thread_id, CONFIG_DEFAULT_THREAD_STACK_SIZE, led_thread,
+ NULL, NULL, NULL, 7, 0, 0);
diff --git a/west/trezor/trezor-ble/src/spi.h b/west/trezor/trezor-ble/src/spi.h
deleted file mode 100644
index ab517f8768..0000000000
--- a/west/trezor/trezor-ble/src/spi.h
+++ /dev/null
@@ -1,6 +0,0 @@
-
-
-#include
-
-void spi_init(void);
-void spi_send(const uint8_t* data, uint32_t len);
diff --git a/west/trezor/trezor-ble/src/trz_comm/inc/trz_comm/trz_comm.h b/west/trezor/trezor-ble/src/trz_comm/inc/trz_comm/trz_comm.h
new file mode 100644
index 0000000000..2955f171c5
--- /dev/null
+++ b/west/trezor/trezor-ble/src/trz_comm/inc/trz_comm/trz_comm.h
@@ -0,0 +1,48 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include
+#include
+
+#define PACKET_DATA_SIZE 246
+
+typedef enum {
+ NRF_SERVICE_BLE = 0,
+ NRF_SERVICE_BLE_MANAGER = 1,
+
+ NRF_SERVICE_CNT // Number of services
+} nrf_service_id_t;
+
+typedef struct {
+ void *fifo_reserved;
+ uint8_t data[PACKET_DATA_SIZE];
+ uint16_t len;
+} trz_packet_t;
+
+// Initialized the communication module
+void trz_comm_init(void);
+
+// Sends a message to the specified service over fitting communication channel
+bool trz_comm_send_msg(nrf_service_id_t service, const uint8_t *data,
+ uint32_t len);
+
+// Polls for incoming data from the specified service
+trz_packet_t *trz_comm_poll_data(nrf_service_id_t service);
diff --git a/west/trezor/trezor-ble/src/spi.c b/west/trezor/trezor-ble/src/trz_comm/spi.c
similarity index 61%
rename from west/trezor/trezor-ble/src/spi.c
rename to west/trezor/trezor-ble/src/trz_comm/spi.c
index 38b3967577..1d398b5e24 100644
--- a/west/trezor/trezor-ble/src/spi.c
+++ b/west/trezor/trezor-ble/src/trz_comm/spi.c
@@ -1,32 +1,44 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
#include
-#include
-
-#include
-
-#include
-
-#include
-#include
#include
#include
+#include
+#include
+#include
#include
+#include
-#include "int_comm_defs.h"
-#include "spi.h"
+#include
+
+#include "trz_comm_internal.h"
+
+#define LOG_MODULE_NAME trz_comm_spi
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define MY_SPI_MASTER DT_NODELABEL(spi0)
static K_SEM_DEFINE(spi_comm_ok, 0, 1);
static K_FIFO_DEFINE(fifo_spi_tx_data);
-typedef struct {
- void *fifo_reserved;
- uint8_t data[BLE_PACKET_SIZE + 2];
- uint16_t len;
-} spi_data_t;
-
const struct device *spi_dev;
static struct k_poll_signal spi_done_sig =
K_POLL_SIGNAL_INITIALIZER(spi_done_sig);
@@ -59,27 +71,29 @@ void spi_init(void) {
k_sem_give(&spi_comm_ok);
}
-void spi_send(const uint8_t *data, uint32_t len) {
+bool spi_send(uint8_t service_id, const uint8_t *data, uint32_t len) {
if (len != 244) {
// unexpected length
- return;
+ return false;
}
- spi_data_t *tx = k_malloc(sizeof(*tx));
+ trz_packet_t *tx = k_malloc(sizeof(*tx));
if (!tx) {
printk("Not able to allocate SPI send data buffer\n");
- return;
+ return false;
}
tx->len = len + 2;
- tx->data[0] = EXTERNAL_MESSAGE;
+ tx->data[0] = 0xA0 | service_id;
memcpy(&tx->data[1], data, len);
uint8_t crc = crc8(tx->data, len + 1, 0x07, 0x00, false);
tx->data[len + 1] = crc;
k_fifo_put(&fifo_spi_tx_data, tx);
+
+ return true;
}
void spi_thread(void) {
@@ -88,7 +102,7 @@ void spi_thread(void) {
for (;;) {
/* Wait indefinitely for data to process */
- spi_data_t *buf = k_fifo_get(&fifo_spi_tx_data, K_FOREVER);
+ trz_packet_t *buf = k_fifo_get(&fifo_spi_tx_data, K_FOREVER);
const struct spi_buf tx_buf = {
.buf = buf->data,
@@ -104,5 +118,5 @@ void spi_thread(void) {
}
}
-K_THREAD_DEFINE(spi_thread_id, CONFIG_BT_NUS_THREAD_STACK_SIZE, spi_thread,
+K_THREAD_DEFINE(spi_thread_id, CONFIG_DEFAULT_THREAD_STACK_SIZE, spi_thread,
NULL, NULL, NULL, 7, 0, 0);
diff --git a/west/trezor/trezor-ble/src/trz_comm/trz_comm.c b/west/trezor/trezor-ble/src/trz_comm/trz_comm.c
new file mode 100644
index 0000000000..20e59413b2
--- /dev/null
+++ b/west/trezor/trezor-ble/src/trz_comm/trz_comm.c
@@ -0,0 +1,84 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#include
+#include
+#include
+#include
+#include
+#include
+#include
+
+#include
+
+#include "trz_comm_internal.h"
+
+#define LOG_MODULE_NAME trz_comm
+LOG_MODULE_REGISTER(LOG_MODULE_NAME);
+
+static K_FIFO_DEFINE(fifo_uart_rx_ble);
+static K_FIFO_DEFINE(fifo_uart_rx_ble_manager);
+
+void trz_comm_init(void) {
+ spi_init();
+ uart_init();
+}
+
+bool trz_comm_send_msg(nrf_service_id_t service, const uint8_t *data,
+ uint32_t len) {
+ if (len == SPI_TX_DATA_LEN) {
+ return spi_send(service, data, len);
+ } else if (len <= MAX_UART_DATA_SIZE) {
+ return uart_send(service, data, len);
+ }
+ return false;
+}
+
+void process_rx_msg(uint8_t service_id, uint8_t *data, uint32_t len) {
+ trz_packet_t *buf = k_malloc(sizeof(*buf));
+
+ if (!buf) {
+ LOG_WRN("Not able to allocate UART receive buffer");
+ return;
+ }
+
+ buf->len = len;
+ memcpy(buf->data, data, len);
+
+ if (service_id == NRF_SERVICE_BLE) {
+ k_fifo_put(&fifo_uart_rx_ble, buf);
+ } else if (service_id == NRF_SERVICE_BLE_MANAGER) {
+ k_fifo_put(&fifo_uart_rx_ble_manager, buf);
+ } else {
+ LOG_WRN("UART_RX unknown service");
+ k_free(buf);
+ }
+}
+
+trz_packet_t *trz_comm_poll_data(nrf_service_id_t service) {
+ switch (service) {
+ case NRF_SERVICE_BLE:
+ return k_fifo_get(&fifo_uart_rx_ble, K_FOREVER);
+ case NRF_SERVICE_BLE_MANAGER:
+ return k_fifo_get(&fifo_uart_rx_ble_manager, K_FOREVER);
+ default:
+ LOG_WRN("UART_RX unknown service");
+ return NULL;
+ }
+}
diff --git a/west/trezor/trezor-ble/src/trz_comm/trz_comm_internal.h b/west/trezor/trezor-ble/src/trz_comm/trz_comm_internal.h
new file mode 100644
index 0000000000..d5af4856c9
--- /dev/null
+++ b/west/trezor/trezor-ble/src/trz_comm/trz_comm_internal.h
@@ -0,0 +1,37 @@
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
+
+#pragma once
+
+#include
+
+#include
+
+#define SPI_TX_DATA_LEN 244
+#define MAX_UART_DATA_SIZE 64
+
+void spi_init(void);
+
+int uart_init(void);
+
+bool spi_send(uint8_t service_id, const uint8_t *data, uint32_t len);
+
+bool uart_send(uint8_t service_id, const uint8_t *tx_data, uint8_t len);
+
+void process_rx_msg(uint8_t service_id, uint8_t *data, uint32_t len);
diff --git a/west/trezor/trezor-ble/src/uart.c b/west/trezor/trezor-ble/src/trz_comm/uart.c
similarity index 74%
rename from west/trezor/trezor-ble/src/uart.c
rename to west/trezor/trezor-ble/src/trz_comm/uart.c
index 6acf4de7ed..aa39075417 100644
--- a/west/trezor/trezor-ble/src/uart.c
+++ b/west/trezor/trezor-ble/src/trz_comm/uart.c
@@ -1,22 +1,36 @@
-#include
-#include
-#include
+/*
+ * This file is part of the Trezor project, https://trezor.io/
+ *
+ * Copyright (c) SatoshiLabs
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
+ */
#include
#include
-
-#include
-
+#include
+#include
#include
#include
#include
+#include
-#include "events.h"
-#include "int_comm.h"
-#include "int_comm_defs.h"
-#include "uart.h"
+#include
-#define LOG_MODULE_NAME fw_uart
+#include "trz_comm_internal.h"
+
+#define LOG_MODULE_NAME trz_comm_uart
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
#define UART_WAIT_FOR_BUF_DELAY K_MSEC(50)
@@ -25,19 +39,33 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME);
static const struct device *uart = DEVICE_DT_GET(DT_CHOSEN(nordic_nus_uart));
static K_FIFO_DEFINE(fifo_uart_tx_data);
-static K_FIFO_DEFINE(fifo_uart_rx_data);
-static K_FIFO_DEFINE(fifo_uart_rx_data_int);
+
+#define COMM_HEADER_SIZE (2)
+#define COMM_FOOTER_SIZE (1)
+#define OVERHEAD_SIZE (COMM_HEADER_SIZE + COMM_FOOTER_SIZE)
static struct k_work_delayable uart_work;
static volatile bool g_uart_rx_running = false;
+static bool nrf_is_valid_startbyte(uint8_t val) {
+ if ((val & 0xF0) != 0xA0) {
+ return false;
+ }
+
+ if ((val & 0x0F) >= NRF_SERVICE_CNT) {
+ return false;
+ }
+
+ return true;
+}
+
static void uart_cb(const struct device *dev, struct uart_event *evt,
void *user_data) {
ARG_UNUSED(dev);
static size_t aborted_len;
- uart_data_t *buf;
+ trz_packet_t *buf;
static uint8_t *aborted_buf;
static bool disable_req;
static uint8_t rx_phase = 0;
@@ -55,7 +83,7 @@ static void uart_cb(const struct device *dev, struct uart_event *evt,
}
if (evt->data.tx.len == 0) {
- buf = CONTAINER_OF(evt->data.tx.buf, uart_data_t, data[0]);
+ buf = CONTAINER_OF(evt->data.tx.buf, trz_packet_t, data[0]);
LOG_DBG("Free uart data");
k_free(buf);
@@ -63,11 +91,11 @@ static void uart_cb(const struct device *dev, struct uart_event *evt,
}
if (aborted_buf) {
- buf = CONTAINER_OF(aborted_buf, uart_data_t, data[0]);
+ buf = CONTAINER_OF(aborted_buf, trz_packet_t, data[0]);
aborted_buf = NULL;
aborted_len = 0;
} else {
- buf = CONTAINER_OF(evt->data.tx.buf, uart_data_t, data[0]);
+ buf = CONTAINER_OF(evt->data.tx.buf, trz_packet_t, data[0]);
}
LOG_DBG("Free uart data");
@@ -86,13 +114,12 @@ static void uart_cb(const struct device *dev, struct uart_event *evt,
case UART_RX_RDY:
// LOG_WRN("UART_RX_RDY");
- buf = CONTAINER_OF(evt->data.rx.buf, uart_data_t, data[0]);
+ buf = CONTAINER_OF(evt->data.rx.buf, trz_packet_t, data[0]);
buf->len += evt->data.rx.len;
switch (rx_phase) {
case 0:
- if (buf->len == 1 && (buf->data[0] == INTERNAL_EVENT ||
- buf->data[0] == EXTERNAL_MESSAGE)) {
+ if (buf->len == 1 && nrf_is_valid_startbyte(buf->data[0])) {
rx_phase = 1;
rx_msg_type = buf->data[0];
crc = crc8(buf->data, buf->len, 0x07, 0x00, false);
@@ -215,25 +242,20 @@ static void uart_cb(const struct device *dev, struct uart_event *evt,
case UART_RX_BUF_RELEASED:
LOG_DBG("UART_RX_BUF_RELEASED");
- buf = CONTAINER_OF(evt->data.rx_buf.buf, uart_data_t, data[0]);
+ buf = CONTAINER_OF(evt->data.rx_buf.buf, trz_packet_t, data[0]);
if (rx_phase == 3 && buf->len > 0) {
buf->len -= COMM_FOOTER_SIZE;
- if (rx_msg_type == EXTERNAL_MESSAGE) {
- k_fifo_put(&fifo_uart_rx_data, buf);
- } else if (rx_msg_type == INTERNAL_EVENT) {
- k_fifo_put(&fifo_uart_rx_data_int, buf);
- } else {
- // LOG_WRN("UART_RX BAD MASSAGE TYPE");
- k_free(buf);
- }
+
+ process_rx_msg(rx_msg_type & 0x0F, buf->data,
+ rx_data_len - OVERHEAD_SIZE);
+
rx_data_len = 0;
rx_len = 0;
rx_msg_type = 0;
rx_phase = 0;
- } else {
- k_free(buf);
}
+ k_free(buf);
break;
case UART_RX_STOPPED:
LOG_DBG("UART_RX_STOPPED");
@@ -252,7 +274,7 @@ static void uart_cb(const struct device *dev, struct uart_event *evt,
}
aborted_len += evt->data.tx.len;
- buf = CONTAINER_OF(aborted_buf, uart_data_t, data[0]);
+ buf = CONTAINER_OF(aborted_buf, trz_packet_t, data[0]);
uart_tx(uart, &buf->data[aborted_len], buf->len - aborted_len,
SYS_FOREVER_MS);
@@ -266,9 +288,7 @@ static void uart_cb(const struct device *dev, struct uart_event *evt,
int uart_start_rx(void) {
int err;
- uart_data_t *rx;
-
- rx = k_malloc(sizeof(*rx));
+ trz_packet_t *rx = k_malloc(sizeof(*rx));
if (rx) {
rx->len = 0;
} else {
@@ -290,7 +310,7 @@ int uart_start_rx(void) {
}
static void uart_work_handler(struct k_work *item) {
- uart_data_t *buf;
+ trz_packet_t *buf;
buf = k_malloc(sizeof(*buf));
if (buf) {
@@ -333,28 +353,29 @@ int uart_init(void) {
return uart_start_rx();
}
-void uart_send_ext(uart_data_t *tx) { k_fifo_put(&fifo_uart_rx_data, tx); }
+bool uart_send(uint8_t service_id, const uint8_t *tx_data, uint8_t len) {
+ trz_packet_t *tx = k_malloc(sizeof(*tx));
-uart_data_t *uart_get_data_ext(void) {
- return k_fifo_get(&fifo_uart_rx_data, K_FOREVER);
-}
+ if (tx == NULL) {
+ LOG_WRN("Not able to allocate UART send data buffer");
+ return false;
+ }
-uart_data_t *uart_get_data_int(void) {
- return k_fifo_get(&fifo_uart_rx_data_int, K_FOREVER);
-}
-//
-// uart_data_t *uart_get_data_pb(void)
-//{
-// return k_fifo_get(&fifo_uart_rx_data_pb, K_MSEC(100));
-//}
-//
-// void uart_data_pb_flush(void){
-// while(uart_get_data_pb() != NULL);
-//}
+ LOG_DBG("ALLOC: Sending UART data");
+
+ tx->len = len + OVERHEAD_SIZE;
+
+ tx->data[0] = 0xA0 | service_id;
+ tx->data[1] = tx->len;
+ memcpy(&tx->data[COMM_HEADER_SIZE], tx_data, len);
+
+ uint8_t crc = crc8(tx->data, tx->len - 1, 0x07, 0x00, false);
+
+ tx->data[tx->len - 1] = crc;
-void uart_send(uart_data_t *tx) {
int err = uart_tx(uart, tx->data, tx->len, SYS_FOREVER_MS);
if (err) {
k_fifo_put(&fifo_uart_tx_data, tx);
}
+ return true;
}
diff --git a/west/trezor/trezor-ble/src/trz_nus.c b/west/trezor/trezor-ble/src/trz_nus.c
deleted file mode 100644
index 1fb2356052..0000000000
--- a/west/trezor/trezor-ble/src/trz_nus.c
+++ /dev/null
@@ -1,90 +0,0 @@
-/*
- * Copyright (c) 2018 Nordic Semiconductor ASA
- *
- * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
- */
-
-#include
-#include
-#include
-
-#include
-
-#include "trz_nus.h"
-
-LOG_MODULE_REGISTER(trznus);
-
-static struct bt_nus_cb nus_cb;
-
-static void nus_ccc_cfg_changed(const struct bt_gatt_attr *attr,
- uint16_t value) {
- if (nus_cb.send_enabled) {
- LOG_DBG("Notification has been turned %s",
- value == BT_GATT_CCC_NOTIFY ? "on" : "off");
- nus_cb.send_enabled(value == BT_GATT_CCC_NOTIFY
- ? BT_NUS_SEND_STATUS_ENABLED
- : BT_NUS_SEND_STATUS_DISABLED);
- }
-}
-
-static ssize_t on_receive(struct bt_conn *conn, const struct bt_gatt_attr *attr,
- const void *buf, uint16_t len, uint16_t offset,
- uint8_t flags) {
- LOG_DBG("Received data, handle %d, conn %p", attr->handle, (void *)conn);
-
- if (nus_cb.received) {
- nus_cb.received(conn, buf, len);
- }
- return len;
-}
-
-static void on_sent(struct bt_conn *conn, void *user_data) {
- uart_data_t *data = (uart_data_t *)user_data;
-
- k_free(data);
-
- LOG_DBG("Data send, conn %p", (void *)conn);
-
- if (nus_cb.sent) {
- nus_cb.sent(conn);
- }
-}
-
-/* UART Service Declaration */
-BT_GATT_SERVICE_DEFINE(
- nus_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_NUS_SERVICE),
- BT_GATT_CHARACTERISTIC(BT_UUID_NUS_TX, BT_GATT_CHRC_NOTIFY,
- BT_GATT_PERM_READ_ENCRYPT, NULL, NULL, NULL),
- BT_GATT_CCC(nus_ccc_cfg_changed,
- BT_GATT_PERM_READ_ENCRYPT | BT_GATT_PERM_WRITE_ENCRYPT),
- BT_GATT_CHARACTERISTIC(BT_UUID_NUS_RX,
- BT_GATT_CHRC_WRITE | BT_GATT_CHRC_WRITE_WITHOUT_RESP,
- BT_GATT_PERM_READ_ENCRYPT |
- BT_GATT_PERM_WRITE_ENCRYPT,
- NULL, on_receive, NULL), );
-
-int bt_nus_init(struct bt_nus_cb *callbacks) {
- if (callbacks) {
- nus_cb.received = callbacks->received;
- nus_cb.sent = callbacks->sent;
- }
-
- return 0;
-}
-
-int bt_nus_send(struct bt_conn *conn, uart_data_t *data) {
- struct bt_gatt_notify_params params = {0};
- const struct bt_gatt_attr *attr = &nus_svc.attrs[2];
-
- params.attr = attr;
- params.data = data->data;
- params.len = data->len;
- params.func = on_sent;
- params.user_data = (void *)data;
-
- if (conn && bt_gatt_is_subscribed(conn, attr, BT_GATT_CCC_NOTIFY)) {
- return bt_gatt_notify_cb(conn, ¶ms);
- } else {
- return -EINVAL;
- }
-}
diff --git a/west/trezor/trezor-ble/src/trz_nus.h b/west/trezor/trezor-ble/src/trz_nus.h
deleted file mode 100644
index 3f2f9ba240..0000000000
--- a/west/trezor/trezor-ble/src/trz_nus.h
+++ /dev/null
@@ -1,137 +0,0 @@
-/*
- * Copyright (c) 2018 Nordic Semiconductor ASA
- *
- * SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
- */
-
-#ifndef BT_NUS_H_
-#define BT_NUS_H_
-
-/**
- * @file
- * @defgroup bt_nus Nordic UART (NUS) GATT Service
- * @{
- * @brief Nordic UART (NUS) GATT Service API.
- */
-
-#include
-#include
-#include
-#include
-
-#include "uart.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-/** @brief UUID of the NUS Service. **/
-#define BT_UUID_NUS_VAL \
- BT_UUID_128_ENCODE(0x8c000001, 0xa59b, 0x4d58, 0xa9ad, 0x073df69fa1b1)
-
-/** @brief UUID of the TX Characteristic. **/
-#define BT_UUID_NUS_TX_VAL \
- BT_UUID_128_ENCODE(0x8c000003, 0xa59b, 0x4d58, 0xa9ad, 0x073df69fa1b1)
-
-/** @brief UUID of the RX Characteristic. **/
-#define BT_UUID_NUS_RX_VAL \
- BT_UUID_128_ENCODE(0x8c000002, 0xa59b, 0x4d58, 0xa9ad, 0x073df69fa1b1)
-
-#define BT_UUID_NUS_SERVICE BT_UUID_DECLARE_128(BT_UUID_NUS_VAL)
-#define BT_UUID_NUS_RX BT_UUID_DECLARE_128(BT_UUID_NUS_RX_VAL)
-#define BT_UUID_NUS_TX BT_UUID_DECLARE_128(BT_UUID_NUS_TX_VAL)
-
-/** @brief NUS send status. */
-enum bt_nus_send_status {
- /** Send notification enabled. */
- BT_NUS_SEND_STATUS_ENABLED,
- /** Send notification disabled. */
- BT_NUS_SEND_STATUS_DISABLED,
-};
-
-/** @brief Pointers to the callback functions for service events. */
-struct bt_nus_cb {
- /** @brief Data received callback.
- *
- * The data has been received as a write request on the NUS RX
- * Characteristic.
- *
- * @param[in] conn Pointer to connection object that has received data.
- * @param[in] data Received data.
- * @param[in] len Length of received data.
- */
- void (*received)(struct bt_conn *conn, const uint8_t *const data,
- uint16_t len);
-
- /** @brief Data sent callback.
- *
- * The data has been sent as a notification and written on the NUS TX
- * Characteristic.
- *
- * @param[in] conn Pointer to connection object, or NULL if sent to all
- * connected peers.
- */
- void (*sent)(struct bt_conn *conn);
-
- /** @brief Send state callback.
- *
- * Indicate the
- * CCCD descriptor status of the NUS TX characteristic.
- *
- * @param[in] status Send notification status.
- */
- void (*send_enabled)(enum bt_nus_send_status status);
-};
-
-/**@brief Initialize the service.
- *
- * @details This function registers a GATT service with two characteristics,
- * TX and RX. A remote device that is connected to this service
- * can send data to the RX Characteristic. When the remote enables
- * notifications, it is notified when data is sent to the TX
- * Characteristic.
- *
- * @param[in] callbacks Struct with function pointers to callbacks for service
- * events. If no callbacks are needed, this parameter can
- * be NULL.
- *
- * @retval 0 If initialization is successful.
- * Otherwise, a negative value is returned.
- */
-int bt_nus_init(struct bt_nus_cb *callbacks);
-
-/**@brief Send data.
- *
- * @details This function sends data to a connected peer, or all connected
- * peers.
- *
- * @param[in] conn Pointer to connection object, or NULL to send to all
- * connected peers.
- * @param[in] buf Pointer to a data buffer.
- *
- * @retval 0 If the data is sent.
- * Otherwise, a negative value is returned.
- */
-int bt_nus_send(struct bt_conn *conn, uart_data_t *data);
-
-/**@brief Get maximum data length that can be used for @ref bt_nus_send.
- *
- * @param[in] conn Pointer to connection Object.
- *
- * @return Maximum data length.
- */
-static inline uint32_t bt_nus_get_mtu(struct bt_conn *conn) {
- /* According to 3.4.7.1 Handle Value Notification off the ATT protocol.
- * Maximum supported notification is ATT_MTU - 3 */
- return bt_gatt_get_mtu(conn) - 3;
-}
-
-#ifdef __cplusplus
-}
-#endif
-
-/**
- *@}
- */
-
-#endif /* BT_NUS_H_ */
diff --git a/west/trezor/trezor-ble/src/uart.h b/west/trezor/trezor-ble/src/uart.h
deleted file mode 100644
index 5accb06f44..0000000000
--- a/west/trezor/trezor-ble/src/uart.h
+++ /dev/null
@@ -1,22 +0,0 @@
-#ifndef UART_H
-#define UART_H
-
-#define UART_BUF_SIZE 247
-
-typedef struct {
- void *fifo_reserved;
- uint8_t data[UART_BUF_SIZE];
- uint16_t len;
-} uart_data_t;
-
-int uart_init(void);
-
-uart_data_t *uart_get_data_ext(void);
-uart_data_t *uart_get_data_int(void);
-// uart_data_t * uart_get_data_pb(void);
-// void uart_data_pb_flush(void);
-
-void uart_send(uart_data_t *data);
-void uart_send_ext(uart_data_t *tx);
-
-#endif