Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
TychoVrahe committed Oct 30, 2024
1 parent b3e6df6 commit ee3118c
Show file tree
Hide file tree
Showing 59 changed files with 2,626 additions and 699 deletions.
1 change: 0 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# This CMake file is picked by the Zephyr build system because it is defined
# as the module CMake entry point (see zephyr/module.yml).


# This is needed so that custom driver classes using system calls are taken into
# account
zephyr_syscall_include_directories(include)
Expand Down
2 changes: 0 additions & 2 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,3 @@
# as the module Kconfig entry point (see zephyr/module.yml). You can browse
# module options by going to Zephyr -> Modules in Kconfig.

rsource "drivers/Kconfig"
rsource "lib/Kconfig"
29 changes: 22 additions & 7 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
#-------------------------------------------------------------------------------
# Zephyr Example Application
#
# Copyright (c) 2021 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
# Copyright (c) 2018 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
cmake_minimum_required(VERSION 3.20.0)

cmake_minimum_required(VERSION 3.13.1)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
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/oob.c
src/trz_nus.c
)


project(app LANGUAGES C)
# NORDIC SDK APP END

target_sources(app PRIVATE src/main.c)
zephyr_library_include_directories(.)
50 changes: 39 additions & 11 deletions app/Kconfig
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
# Copyright (c) 2021 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
#
# This file is the application Kconfig entry point. All application Kconfig
# options can be defined here or included via other application Kconfig files.
# You can browse these options using the west targets menuconfig (terminal) or
# guiconfig (GUI).
# Copyright (c) 2018 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

menu "Zephyr"
source "Kconfig.zephyr"
endmenu

module = APP
module-str = APP
source "subsys/logging/Kconfig.template.log_config"
menu "Nordic UART BLE GATT service sample"

config BT_NUS_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.

endmenu
102 changes: 97 additions & 5 deletions app/prj.conf
Original file line number Diff line number Diff line change
@@ -1,7 +1,99 @@
# Copyright (c) 2021 Nordic Semiconductor ASA
# SPDX-License-Identifier: Apache-2.0
#
# This file contains selected Kconfig options for the application.
# Copyright (c) 2018 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#

# Enable the UART driver
CONFIG_UART_ASYNC_API=y
CONFIG_NRFX_UARTE0=y
CONFIG_SERIAL=y

CONFIG_NANOPB=y

# Enable the SPI driver
CONFIG_SPI=y
CONFIG_NRFX_SPIM0=y

CONFIG_GPIO=y



CONFIG_HEAP_MEM_POOL_SIZE=2048




CONFIG_BT=y
CONFIG_BT_PERIPHERAL=y
CONFIG_BT_DEVICE_NAME="TrezorZephyr"
CONFIG_BT_DEVICE_APPEARANCE=833
CONFIG_BT_MAX_CONN=1
CONFIG_BT_MAX_PAIRED=8
CONFIG_BT_SMP=y
# CONFIG_BT_SMP_APP_PAIRING_ACCEPT=y
CONFIG_BT_SMP_ENFORCE_MITM=y
CONFIG_BT_SMP_SC_ONLY=y
CONFIG_BT_FILTER_ACCEPT_LIST=y
CONFIG_BT_BONDING_REQUIRED=y
CONFIG_BT_PRIVACY=y
CONFIG_BT_TINYCRYPT_ECC=y
CONFIG_BT_LL_SW_SPLIT=y

#CONFIG_BT_DEBUG_SMP=y

#PHY update needed for updating PHY request
CONFIG_BT_USER_PHY_UPDATE=y

# HCI ACL buffers size
# BT_L2CAP_RX_MTU = CONFIG_BT_BUF_ACL_RX_SIZE - BT_L2CAP_HDR_SIZE
CONFIG_BT_BUF_ACL_RX_SIZE=251

# L2CAP SDU/PDU TX MTU
CONFIG_BT_L2CAP_TX_MTU=247


# Enable bonding
CONFIG_BT_SETTINGS=y
CONFIG_FLASH=y
CONFIG_FLASH_PAGE_LAYOUT=y
CONFIG_FLASH_MAP=y
CONFIG_NVS=y
CONFIG_SETTINGS=y

# Enable DK LED and Buttons library
CONFIG_DK_LIBRARY=y

# This example requires more stack
CONFIG_MAIN_STACK_SIZE=1152
CONFIG_SYSTEM_WORKQUEUE_STACK_SIZE=4096

# NFC
CONFIG_NFC_T4T_NRFXLIB=y

CONFIG_NFC_NDEF=y
CONFIG_NFC_NDEF_MSG=y
CONFIG_NFC_NDEF_RECORD=y
CONFIG_NFC_NDEF_LE_OOB_REC=y
CONFIG_NFC_NDEF_CH_MSG=y

CONFIG_NFC_NDEF_TNEP_RECORD=y
CONFIG_NFC_TNEP_TAG=y
CONFIG_NFC_NDEF_PARSER=y
CONFIG_NFC_NDEF_CH_PARSER=y
CONFIG_NFC_NDEF_LE_OOB_REC_PARSER=y
CONFIG_NFC_TNEP_CH=y

CONFIG_POLL=y


# Config logger
CONFIG_LOG=y
CONFIG_RTT_CONSOLE=y
CONFIG_UART_CONSOLE=n
CONFIG_USE_SEGGER_RTT=y
CONFIG_LOG_BACKEND_RTT=y
CONFIG_LOG_BACKEND_UART=n
CONFIG_LOG_PRINTK=n

CONFIG_SENSOR=y
CONFIG_BLINK=y
CONFIG_ASSERT=y
6 changes: 3 additions & 3 deletions app/sample.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
# the Zephyr testing tool. In this file, multiple combinations can be specified,
# so that you can easily test all of them locally or in CI.
sample:
description: Example application
name: example-application
description: Trezor BLE gateway
name: W001
common:
sysbuild: true
build_only: true
integration_platforms:
- custom_plank
- t3w1_nrf52833
tests:
app.default: {}
app.debug:
Expand Down
153 changes: 153 additions & 0 deletions app/src/advertising.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@

#include <zephyr/types.h>
#include <zephyr/kernel.h>

#include <zephyr/bluetooth/bluetooth.h>
#include <bluetooth/services/nus.h>

#include <zephyr/logging/log.h>

#include "int_comm.h"
#include "connection.h"
#include "oob.h"


#define LOG_MODULE_NAME fw_int_advertising
LOG_MODULE_REGISTER(LOG_MODULE_NAME);


#define DEVICE_NAME CONFIG_BT_DEVICE_NAME
#define DEVICE_NAME_LEN (sizeof(DEVICE_NAME) - 1)

bool advertising = false;
bool advertising_wl = false;
int bond_cnt = 0;
int bond_cnt_tmp = 0;

static const struct bt_data ad[] = {
BT_DATA_BYTES(BT_DATA_FLAGS, (BT_LE_AD_GENERAL | BT_LE_AD_NO_BREDR)),
BT_DATA(BT_DATA_NAME_COMPLETE, DEVICE_NAME, DEVICE_NAME_LEN),
};

static const struct bt_data sd[] = {
BT_DATA_BYTES(BT_DATA_UUID128_ALL, BT_UUID_NUS_VAL),
};


static void add_to_whitelist(const struct bt_bond_info *info, void *user_data){
char addr[BT_ADDR_LE_STR_LEN];
bt_addr_le_to_str(&info->addr, addr, sizeof(addr));

int err = bt_le_filter_accept_list_add(&info->addr);
if (err){
LOG_WRN("whitelist add: %s FAILED!\n", addr);
}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){

if (advertising) {
if (wl != advertising_wl) {
LOG_WRN("Restarting advertising");
bt_le_adv_stop();
}else {
LOG_WRN("Already advertising");

send_status_event();
return;
}
}

int err;

if (wl) {
advertising_setup_wl();
LOG_INF("Advertising with whitelist");
err = bt_le_adv_start(
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_FILTER_CONN | BT_LE_ADV_OPT_FILTER_SCAN_REQ,
160, 1600, NULL),
ad, ARRAY_SIZE(ad),
sd, ARRAY_SIZE(sd));
}
else {
LOG_INF("Advertising no whitelist");
err = bt_le_adv_start(
BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE,
160, 1600, NULL),
ad, ARRAY_SIZE(ad),
sd, ARRAY_SIZE(sd));
}
if (err) {
LOG_ERR("Advertising failed to start (err %d)", err);
send_status_event();
return;
}
advertising = true;
advertising_wl = wl;


oob_fetch_addr();

send_status_event();
}

void advertising_stop(void){

if (!advertising) {
LOG_WRN("Not advertising");

send_status_event();
return;
}

int err = bt_le_adv_stop();
if (err) {
LOG_ERR("Advertising failed to stop (err %d)", err);
send_status_event();
return;
}
advertising = false;
advertising_wl = false;
send_status_event();
}

bool is_advertising(void){
return advertising;
}

bool 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;
}
14 changes: 14 additions & 0 deletions app/src/advertising.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@



#include <stdbool.h>


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);
Loading

0 comments on commit ee3118c

Please sign in to comment.