Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found
Select Git revision
  • add_menu_vibration
  • blinkisync-as-preload
  • ch3/api-speed-eval2
  • ch3/dual-core
  • ch3/genapi-refactor
  • ch3/leds-api
  • ch3/splashscreen
  • dualcore
  • dx/flatten-config-module
  • dx/meh-bdf-to-stm
  • freertos-btle
  • genofire/ble-follow-py
  • koalo/bhi160-works-but-dirty
  • koalo/factory-reset
  • koalo/wip/i2c-for-python
  • master
  • msgctl/faultscreen
  • msgctl/textbuffer_api
  • plaetzchen/ios-workaround
  • rahix/bhi
  • rahix/bluetooth-app-favorite
  • rahix/bma
  • rahix/user-space-ctx
  • renze/hatchery_apps
  • renze/safe_mode
  • schleicher-test
  • schneider/212-reset-hardware-when-entering-repl
  • schneider/ancs
  • schneider/ble-buffers
  • schneider/ble-central
  • schneider/ble-ecg-stream-visu
  • schneider/ble-fixes-2020-3
  • schneider/ble-mini-demo
  • schneider/ble-stability
  • schneider/ble-stability-new-phy
  • schneider/bonding
  • schneider/bonding-fail-if-full
  • schneider/bootloader-update-9a0d158
  • schneider/deepsleep
  • schneider/deepsleep2
  • schneider/deepsleep4
  • schneider/default-main
  • schneider/freertos-list-debug
  • schneider/fundamental-test
  • schneider/iaq-python
  • schneider/ir
  • schneider/max30001
  • schneider/max30001-epicaridum
  • schneider/max30001-pycardium
  • schneider/maxim-sdk-update
  • schneider/mp-exception-print
  • schneider/mp-for-old-bl
  • schneider/png
  • schneider/schleicher-test
  • schneider/sdk-0.2.1-11
  • schneider/sdk-0.2.1-7
  • schneider/sleep-display
  • schneider/spo2-playground
  • schneider/stream-locks
  • schneider/v1.17-changelog
  • bootloader-v1
  • release-1
  • v0.0
  • v1.0
  • v1.1
  • v1.10
  • v1.11
  • v1.12
  • v1.13
  • v1.14
  • v1.15
  • v1.16
  • v1.17
  • v1.18
  • v1.2
  • v1.3
  • v1.4
  • v1.5
  • v1.6
  • v1.7
  • v1.8
  • v1.9
82 results

Target

Select target project
  • card10/firmware
  • annejan/firmware
  • astro/firmware
  • fpletz/firmware
  • gerd/firmware
  • fleur/firmware
  • swym/firmware
  • l/firmware
  • uberardy/firmware
  • wink/firmware
  • madonius/firmware
  • mot/firmware
  • filid/firmware
  • q3k/firmware
  • hauke/firmware
  • Woazboat/firmware
  • pink/firmware
  • mossmann/firmware
  • omniskop/firmware
  • zenox/firmware
  • trilader/firmware
  • Danukeru/firmware
  • shoragan/firmware
  • zlatko/firmware
  • sistason/firmware
  • datenwolf/firmware
  • bene/firmware
  • amedee/firmware
  • martinling/firmware
  • griffon/firmware
  • chris007/firmware
  • adisbladis/firmware
  • dbrgn/firmware
  • jelly/firmware
  • rnestler/firmware
  • mh/firmware
  • ln/firmware
  • penguineer/firmware
  • monkeydom/firmware
  • jens/firmware
  • jnaulty/firmware
  • jeffmakes/firmware
  • marekventur/firmware
  • pete/firmware
  • h2obrain/firmware
  • DooMMasteR/firmware
  • jackie/firmware
  • prof_r/firmware
  • Draradech/firmware
  • Kartoffel/firmware
  • hinerk/firmware
  • abbradar/firmware
  • JustTB/firmware
  • LuKaRo/firmware
  • iggy/firmware
  • ente/firmware
  • flgr/firmware
  • Lorphos/firmware
  • matejo/firmware
  • ceddral7/firmware
  • danb/firmware
  • joshi/firmware
  • melle/firmware
  • fitch/firmware
  • deurknop/firmware
  • sargon/firmware
  • markus/firmware
  • kloenk/firmware
  • lucaswerkmeister/firmware
  • derf/firmware
  • meh/firmware
  • dx/card10-firmware
  • torben/firmware
  • yuvadm/firmware
  • AndyBS/firmware
  • klausdieter1/firmware
  • katzenparadoxon/firmware
  • xiretza/firmware
  • ole/firmware
  • techy/firmware
  • thor77/firmware
  • TilCreator/firmware
  • fuchsi/firmware
  • dos/firmware
  • yrlf/firmware
  • PetePriority/firmware
  • SuperVirus/firmware
  • sur5r/firmware
  • tazz/firmware
  • Alienmaster/firmware
  • flo_h/firmware
  • baldo/firmware
  • mmu_man/firmware
  • Foaly/firmware
  • sodoku/firmware
  • Guinness/firmware
  • ssp/firmware
  • led02/firmware
  • Stormwind/firmware
  • arist/firmware
  • coon/firmware
  • mdik/firmware
  • pippin/firmware
  • royrobotiks/firmware
  • zigot83/firmware
  • mo_k/firmware
106 results
Select Git revision
  • dualcore
  • fix-nix
  • master
  • rahix/freertos10
  • rahix/new-build
  • unjailbreak
6 results
Show changes
Showing
with 2872 additions and 2196 deletions
FROM ubuntu:bionic
FROM ubuntu:focal
RUN set -e -x ;\
export DEBIAN_FRONTEND=noninteractive ;\
......@@ -10,7 +10,7 @@ RUN set -e -x ;\
llvm \
python3-pip ;\
pip3 install \
clang \
clang==10.0.1 \
sphinx \
sphinx_rtd_theme ;\
rm -rf /var/lib/apt/lists
......
#ifndef FREERTOS_CONFIG_H
#define FREERTOS_CONFIG_H
#define MXC_ASSERT_ENABLE
#include "mxc_assert.h"
#include "max32665.h"
#include <assert.h>
/* CMSIS keeps a global updated with current system clock in Hz */
#define configCPU_CLOCK_HZ ((unsigned long)96000000)
......@@ -51,7 +50,10 @@
#define INCLUDE_vTaskSuspend 1
#define INCLUDE_vTaskDelay 1
#define INCLUDE_vTaskDelete 1
#define INCLUDE_uxTaskGetStackHighWaterMark 1
#define INCLUDE_xTimerPendFunctionCall 1
#define INCLUDE_xSemaphoreGetMutexHolder 1
/* Allow static allocation of data structures */
#define configSUPPORT_STATIC_ALLOCATION 1
......@@ -69,7 +71,7 @@
#define xPortSysTickHandler SysTick_Handler
/* Assert */
#define configASSERT(x) MXC_ASSERT(x)
#define configASSERT(x) assert(x)
/* Tickless idle hooks */
typedef uint32_t TickType_t;
......
......@@ -5,8 +5,17 @@
#define MXC_ASSERT_ENABLE
#include "mxc_assert.h"
static uint32_t irq_save = 0;
void *_api_call_start(api_id_t id, uintptr_t size)
{
/*
* Disable all maskable interrupts here, to be turned on again at the
* end of _api_call_transact().
*/
irq_save = __get_PRIMASK();
__set_PRIMASK(1);
while (SEMA_GetSema(_API_SEMAPHORE) == E_BUSY) {
}
......@@ -51,6 +60,12 @@ void *_api_call_transact(void *buffer)
API_CALL_MEM->call_flag = _API_FLAG_IDLE;
SEMA_FreeSema(_API_SEMAPHORE);
/*
* Re-enable interrupts (if previously enabled) after completing the API
* call.
*/
__set_PRIMASK(irq_save);
return API_CALL_MEM->buffer;
}
......@@ -109,7 +124,7 @@ int api_fetch_args(char *buf, size_t cnt)
return 0;
}
int i;
size_t i;
for (i = 0; i < cnt && API_CALL_MEM->buffer[i + 0x20] != '\0'; i++) {
buf[i] = API_CALL_MEM->buffer[i + 0x20];
}
......
......@@ -38,7 +38,7 @@ struct api_call_mem {
api_id_t id;
/* ID of the current interrupt */
api_int_id_t int_id;
volatile api_int_id_t int_id;
/*
* Buffer for arguments/return value. This buffer will be
......
#include "epicardium.h"
#include "api/dispatcher.h"
#include "api/interrupt-sender.h"
#include "modules/log.h"
#include "card10.h"
......@@ -10,6 +8,7 @@
#include "tmr.h"
static void __core1_init(void);
extern void interrupt_trigger_sync(api_int_id_t id);
struct core1_info {
/* Location of core1's interrupt vector table */
......@@ -76,6 +75,11 @@ void __core1_init(void)
*/
TMR_IntClear(MXC_TMR5);
/*
* Disable the SysTick
*/
SysTick->CTRL = SysTick_CTRL_CLKSOURCE_Msk;
/*
* Reset Interrupts
*
......@@ -201,14 +205,19 @@ void core1_boot(void)
void core1_trigger_reset(void)
{
/* Signal core 1 that we intend to load a new payload. */
api_interrupt_trigger(EPIC_INT_RESET);
/*
* Signal core 1 that we intend to load a new payload.
*
* This needs to be synchroneous because otherwise we will deadlock
* (Lifecycle task busy-spins and interrupt can never get dispatched).
*/
interrupt_trigger_sync(EPIC_INT_RESET);
}
void core1_wait_ready(void)
bool core1_is_ready(void)
{
/* Wait for the core to accept */
while (1) {
bool ready;
while (SEMA_GetSema(_CONTROL_SEMAPHORE) == E_BUSY) {
}
......@@ -216,12 +225,21 @@ void core1_wait_ready(void)
* core 1 will set the ready flag once it is spinning in the
* above loop, waiting for a new IVT.
*/
if (core1_info.ready) {
break;
}
ready = core1_info.ready;
SEMA_FreeSema(_CONTROL_SEMAPHORE);
return ready;
}
void core1_wait_ready(void)
{
/* Wait for the core to accept */
while (1) {
if (core1_is_ready()) {
break;
}
for (int i = 0; i < 10000; i++) {
}
}
......@@ -235,6 +253,9 @@ void core1_wait_ready(void)
void core1_load(void *ivt, char *args)
{
while (SEMA_GetSema(_CONTROL_SEMAPHORE) == E_BUSY) {
}
/* If the core is currently in an API call, reset it. */
API_CALL_MEM->call_flag = _API_FLAG_IDLE;
API_CALL_MEM->id = 0;
......
......@@ -9,7 +9,7 @@
/* This function is defined by the generated dispatcher code */
void __api_dispatch_call(api_id_t id, void *buffer);
static volatile bool event_ready = false;
static volatile bool call_pending = false;
int api_dispatcher_init()
{
......@@ -34,7 +34,7 @@ int api_dispatcher_init()
bool api_dispatcher_poll_once()
{
if (event_ready) {
if (call_pending) {
return false;
}
......@@ -46,22 +46,27 @@ bool api_dispatcher_poll_once()
return false;
}
event_ready = true;
call_pending = true;
return true;
}
bool api_dispatcher_poll()
{
if (event_ready) {
if (call_pending) {
return true;
}
return api_dispatcher_poll_once();
}
bool api_dispatcher_call_pending()
{
return call_pending;
}
api_id_t api_dispatcher_exec()
{
if (!event_ready) {
if (!call_pending) {
return 0;
}
......@@ -69,7 +74,7 @@ api_id_t api_dispatcher_exec()
__api_dispatch_call(id, API_CALL_MEM->buffer);
API_CALL_MEM->call_flag = _API_FLAG_RETURNED;
event_ready = false;
call_pending = false;
SEMA_FreeSema(_API_SEMAPHORE);
/* Notify the caller that we returned */
......@@ -86,7 +91,7 @@ void api_prepare_args(char *args)
* collide with any integer return value of API calls like epic_exec().
*/
API_CALL_MEM->id = 0;
for (int i = 0; i <= strlen(args); i++) {
for (size_t i = 0; i <= strlen(args); i++) {
API_CALL_MEM->buffer[i + 0x20] = args[i];
}
}
......@@ -15,6 +15,12 @@ int api_dispatcher_init();
bool api_dispatcher_poll_once();
bool api_dispatcher_poll();
/*
* Check if the other core requested a call or if we are already excuting it.
* Only returns a cached version, without acquiring any locks.
*/
bool api_dispatcher_call_pending();
/*
* Attempt to dispatch a call, if one had been polled using
* api_dispatcher_poll(). Will return 0 if no call was dispatched or the ID of
......@@ -39,6 +45,9 @@ void core1_boot(void);
/* Reset core 1 into a state where it can accept a new payload */
void core1_trigger_reset(void);
/* Check if core 1 is ready for a new payload */
bool core1_is_ready(void);
/* Wait for core 1 to respond that it is ready for a new payload */
void core1_wait_ready(void);
......
......@@ -246,7 +246,7 @@ void __dispatch_isr(api_int_id_t id)
# Generate Dispatcher {{{
with open(args.server, "w") as f_dispatcher:
tmp = """\
#include "modules/log.h"
#include "os/core.h"
#include "{header}"
void __api_dispatch_call(uint32_t id, void*epc__apistub_buffer)
......
#include "api/interrupt-sender.h"
#include "api/common.h"
#include "tmr_utils.h"
static bool int_enabled[EPIC_INT_NUM];
int api_interrupt_trigger(api_int_id_t id)
{
if (id >= EPIC_INT_NUM) {
return -EINVAL;
}
if (int_enabled[id]) {
while (API_CALL_MEM->int_id != (-1))
;
API_CALL_MEM->int_id = id;
TMR_TO_Start(MXC_TMR5, 1, 0);
}
return 0;
}
#include <assert.h>
void api_interrupt_init(void)
{
API_CALL_MEM->int_id = (-1);
for (int i = 0; i < EPIC_INT_NUM; i++) {
int_enabled[i] = false;
}
/* Reset interrupt is always enabled */
int_enabled[EPIC_INT_RESET] = true;
}
int epic_interrupt_enable(api_int_id_t int_id)
bool api_interrupt_is_ready(void)
{
if (int_id >= EPIC_INT_NUM) {
return -EINVAL;
return API_CALL_MEM->int_id == (api_int_id_t)(-1);
}
int_enabled[int_id] = true;
return 0;
}
int epic_interrupt_disable(api_int_id_t int_id)
void api_interrupt_trigger(api_int_id_t id)
{
if (int_id >= EPIC_INT_NUM || int_id == EPIC_INT_RESET) {
return -EINVAL;
}
assert(API_CALL_MEM->int_id == (api_int_id_t)(-1));
int_enabled[int_id] = false;
return 0;
API_CALL_MEM->int_id = id;
TMR_TO_Start(MXC_TMR5, 1, 0);
}
......@@ -2,4 +2,5 @@
#include "api/common.h"
void api_interrupt_init(void);
int api_interrupt_trigger(api_int_id_t id);
bool api_interrupt_is_ready(void);
void api_interrupt_trigger(api_int_id_t id);
/*************************************************************************************************/
/*!
* \file
*
* \brief Application framework main module.
*
* Copyright (c) 2011-2018 Arm Ltd. All Rights Reserved.
* ARM Ltd. confidential and proprietary.
*
* IMPORTANT. Your use of this file is governed by a Software License Agreement
* ("Agreement") that must be accepted in order to download or otherwise receive a
* copy of this file. You may not use or copy this file for any purpose other than
* as described in the Agreement. If you do not agree to all of the terms of the
* Agreement do not use this file and delete all copies in your possession or control;
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
* to any use, copying or further distribution of this software.
*/
/*************************************************************************************************/
/* card10:
* copied from: lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/app_main.c
*
* Reason: we need to correctly implement AppHandleNumericComparison
*/
/* clang-format off */
/* clang-formet turned off for easier diffing against orginal file */
#include <string.h>
#include "wsf_types.h"
#include "wsf_msg.h"
#include "sec_api.h"
#include "wsf_trace.h"
#include "wsf_timer.h"
#include "wsf_assert.h"
#include "util/bstream.h"
#include "dm_api.h"
#include "app_api.h"
#include "app_main.h"
#include "app_ui.h"
/**************************************************************************************************
Global Variables
**************************************************************************************************/
/*! Configuration pointer for advertising */
appAdvCfg_t *pAppAdvCfg;
/*! Configuration pointer for extended and periodic advertising */
appExtAdvCfg_t *pAppExtAdvCfg;
/*! Configuration pointer for slave */
appSlaveCfg_t *pAppSlaveCfg;
/*! Configuration pointer for master */
appMasterCfg_t *pAppMasterCfg;
/*! Configuration pointer for extended master */
appExtMasterCfg_t *pAppExtMasterCfg;
/*! Configuration pointer for security */
appSecCfg_t *pAppSecCfg;
/*! Configuration pointer for connection parameter update */
appUpdateCfg_t *pAppUpdateCfg;
/*! Configuration pointer for discovery */
appDiscCfg_t *pAppDiscCfg;
/*! Configuration pointer for application */
appCfg_t *pAppCfg;
/*! Connection control block array */
appConnCb_t appConnCb[DM_CONN_MAX];
/*! WSF handler ID */
wsfHandlerId_t appHandlerId;
/*! Main control block */
appCb_t appCb;
/*! Configuration structure for incoming request actions */
const appReqActCfg_t appReqActCfg =
{
APP_ACT_ACCEPT /*! Action for the remote connection parameter request */
};
/*! Configuration pointer for incoming request actions on master */
appReqActCfg_t *pAppMasterReqActCfg = (appReqActCfg_t *) &appReqActCfg;
/*! Configurable pointer for incoming request actions on slave */
appReqActCfg_t *pAppSlaveReqActCfg = (appReqActCfg_t *) &appReqActCfg;
/*************************************************************************************************/
/*!
* \brief Process messages from the event handler.
*
* \param pMsg Pointer to message.
*
* \return None.
*/
/*************************************************************************************************/
static void appProcMsg(wsfMsgHdr_t *pMsg)
{
switch(pMsg->event)
{
case APP_BTN_POLL_IND:
appUiBtnPoll();
break;
case APP_UI_TIMER_IND:
appUiTimerExpired(pMsg);
break;
default:
break;
}
}
/*************************************************************************************************/
/*!
* \brief Check the bonded state of a connection.
*
* \param connId DM connection ID.
*
* \return Bonded state.
*/
/*************************************************************************************************/
bool_t appCheckBonded(dmConnId_t connId)
{
WSF_ASSERT((connId > 0) && (connId <= DM_CONN_MAX));
return appConnCb[connId - 1].bonded;
}
/*************************************************************************************************/
/*!
* \brief Check the bond-by-LTK state of a connection.
*
* \param connId DM connection ID.
*
* \return Bond-by-LTK state.
*/
/*************************************************************************************************/
bool_t appCheckBondByLtk(dmConnId_t connId)
{
WSF_ASSERT((connId > 0) && (connId <= DM_CONN_MAX));
return appConnCb[connId - 1].bondByLtk;
}
/*************************************************************************************************/
/*!
* \brief Return the number of existing connections of the given role.
*
* \param role Connection role
*
* \return Number of connections.
*/
/*************************************************************************************************/
uint8_t appNumConns(uint8_t role)
{
appConnCb_t *pCcb = appConnCb;
uint8_t i, j;
for (i = DM_CONN_MAX, j = 0; i > 0; i--, pCcb++)
{
if ((pCcb->connId != DM_CONN_ID_NONE) && (DmConnRole(pCcb->connId) == role))
{
j++;
}
}
return j;
}
/*************************************************************************************************/
/*!
* \brief App framework handler init function called during system initialization.
*
* \param handlerID WSF handler ID for App.
*
* \return None.
*/
/*************************************************************************************************/
void AppInit(void)
{
appHandlerId = WsfOsSetNextHandler(AppHandler);
AppDbInit();
}
/*************************************************************************************************/
/*!
* \brief WSF event handler for app framework.
*
* \param event WSF event mask.
* \param pMsg WSF message.
*
* \return None.
*/
/*************************************************************************************************/
void AppHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg)
{
if (pMsg != NULL)
{
APP_TRACE_INFO1("App got evt %d", pMsg->event);
if (pMsg->event >= APP_MASTER_MSG_START)
{
/* pass event to master handler */
(*appCb.masterCback)(pMsg);
}
else if (pMsg->event >= APP_SLAVE_MSG_START)
{
/* pass event to slave handler */
(*appCb.slaveCback)(pMsg);
}
else
{
appProcMsg(pMsg);
}
}
else
{
if (event & APP_BTN_DOWN_EVT)
{
AppUiBtnPressed();
}
}
}
/*************************************************************************************************/
/*!
* \brief Handle a passkey request during pairing. If the passkey is to displayed, a
* random passkey is generated and displayed. If the passkey is to be entered
* the user is prompted to enter the passkey.
*
* \param pAuthReq DM authentication requested event structure.
*
* \return None.
*/
/*************************************************************************************************/
void AppHandlePasskey(dmSecAuthReqIndEvt_t *pAuthReq)
{
uint32_t passkey;
uint8_t buf[SMP_PIN_LEN];
if (pAuthReq->display)
{
/* generate random passkey, limit to 6 digit max */
SecRand((uint8_t *) &passkey, sizeof(uint32_t));
passkey %= 1000000;
/* convert to byte buffer */
buf[0] = UINT32_TO_BYTE0(passkey);
buf[1] = UINT32_TO_BYTE1(passkey);
buf[2] = UINT32_TO_BYTE2(passkey);
/* send authentication response to DM */
DmSecAuthRsp((dmConnId_t) pAuthReq->hdr.param, SMP_PIN_LEN, buf);
/* display passkey */
AppUiDisplayPasskey(passkey);
}
else
{
/* prompt user to enter passkey */
AppUiAction(APP_UI_PASSKEY_PROMPT);
}
}
/*************************************************************************************************/
/*!
* \brief Handle a numeric comparison indication during pairing. The confirmation value is
* displayed and the user is prompted to verify that the local and peer confirmation
* values match.
*
* \param pCnfInd DM confirmation indication event structure.
*
* \return None.
*/
/*************************************************************************************************/
void AppHandleNumericComparison(dmSecCnfIndEvt_t *pCnfInd)
{
uint32_t confirm = DmSecGetCompareValue(pCnfInd->confirm);
/* display confirmation value */
AppUiDisplayConfirmValue(confirm);
/* TODO: Verify that local and peer confirmation values match */
DmSecCompareRsp((dmConnId_t)pCnfInd->hdr.param, TRUE);
}
/*************************************************************************************************/
/*!
* \brief Close the connection with the give connection identifier.
*
* \param connId Connection identifier.
*
* \return None.
*/
/*************************************************************************************************/
void AppConnClose(dmConnId_t connId)
{
DmConnClose(DM_CLIENT_ID_APP, connId, HCI_ERR_REMOTE_TERMINATED);
}
/*************************************************************************************************/
/*!
* \brief Get a list of connection identifiers of open connections.
*
* \param pConnIdList Buffer to hold connection IDs (must be DM_CONN_MAX bytes).
*
* \return Number of open connections.
*
*/
/*************************************************************************************************/
uint8_t AppConnOpenList(dmConnId_t *pConnIdList)
{
appConnCb_t *pCcb = appConnCb;
uint8_t i;
uint8_t pos = 0;
memset(pConnIdList, DM_CONN_ID_NONE, DM_CONN_MAX);
for (i = DM_CONN_MAX; i > 0; i--, pCcb++)
{
if (pCcb->connId != DM_CONN_ID_NONE)
{
pConnIdList[pos++] = pCcb->connId;
}
}
return pos;
}
/*************************************************************************************************/
/*!
* \brief Check if a connection is open.
*
* \return Connection ID of open connection or DM_CONN_ID_NONE if no open connections.
*/
/*************************************************************************************************/
dmConnId_t AppConnIsOpen(void)
{
appConnCb_t *pCcb = appConnCb;
uint8_t i;
for (i = DM_CONN_MAX; i > 0; i--, pCcb++)
{
if (pCcb->connId != DM_CONN_ID_NONE)
{
return pCcb->connId;
}
}
return DM_CONN_ID_NONE;
}
/*************************************************************************************************/
/*!
* \brief Get the device database record handle associated with an open connection.
*
* \param connId Connection identifier.
*
* \return Database record handle or APP_DB_HDL_NONE.
*/
/*************************************************************************************************/
appDbHdl_t AppDbGetHdl(dmConnId_t connId)
{
return appConnCb[connId-1].dbHdl;
}
/*************************************************************************************************/
/*!
* \brief Add device to resolving list.
*
* \param pMsg Pointer to DM callback event message.
* \param connId Connection identifier.
*
* \return None.
*/
/*************************************************************************************************/
void AppAddDevToResList(dmEvt_t *pMsg, dmConnId_t connId)
{
dmSecKey_t *pPeerKey;
appDbHdl_t hdl = appConnCb[connId - 1].dbHdl;
/* if LL Privacy is supported and the peer device has distributed its IRK */
if (HciLlPrivacySupported() && ((pPeerKey = AppDbGetKey(hdl, DM_KEY_IRK, NULL))!= NULL))
{
/* add peer device to resolving list. If all-zero local or peer IRK is used then
LL will only use or accept local or peer identity address respectively. */
DmPrivAddDevToResList(pPeerKey->irk.addrType, pPeerKey->irk.bdAddr, pPeerKey->irk.key,
DmSecGetLocalIrk(), TRUE, pMsg->hdr.param);
}
}
/*************************************************************************************************/
/*!
* \brief Update privacy mode for a given peer device.
*
* \param hdl Database record handle.
*
* \return None.
*/
/*************************************************************************************************/
void AppUpdatePrivacyMode(appDbHdl_t hdl)
{
/* if peer device's been added to resolving list but RPA Only attribute not found on peer device */
if ((hdl != APP_DB_HDL_NONE) && AppDbGetPeerAddedToRl(hdl) && !AppDbGetPeerRpao(hdl))
{
dmSecKey_t *pPeerKey = AppDbGetKey(hdl, DM_KEY_IRK, NULL);
if (pPeerKey != NULL)
{
/* set device privacy mode for this peer device */
DmPrivSetPrivacyMode(pPeerKey->irk.addrType, pPeerKey->irk.bdAddr, DM_PRIV_MODE_DEVICE);
/* make sure resolving list flag cleared */
AppDbSetPeerAddedToRl(hdl, FALSE);
}
}
}
/* clang-format on */
/*************************************************************************************************/
/*!
* \file
*
* \brief Application framework device database example, using simple RAM-based storage.
*
* Copyright (c) 2011-2018 Arm Ltd. All Rights Reserved.
* ARM Ltd. confidential and proprietary.
*
* IMPORTANT. Your use of this file is governed by a Software License Agreement
* ("Agreement") that must be accepted in order to download or otherwise receive a
* copy of this file. You may not use or copy this file for any purpose other than
* as described in the Agreement. If you do not agree to all of the terms of the
* Agreement do not use this file and delete all copies in your possession or control;
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
* to any use, copying or further distribution of this software.
*/
/*************************************************************************************************/
/* card10:
* copied from: lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/common/app_db.c
*
* Reason: we need to implement persistent storage for pairings
*/
/* clang-format off */
/* clang-formet turned off for easier diffing against orginal file */
#include "wsf_types.h"
#include "wsf_assert.h"
#include "util/bda.h"
#include "app_api.h"
#include "app_main.h"
#include "app_db.h"
#include "app_cfg.h"
#include "epicardium.h"
#include <string.h>
#include <stdio.h>
/**************************************************************************************************
Data Types
**************************************************************************************************/
/*! Database record */
typedef struct
{
/*! Common for all roles */
bdAddr_t peerAddr; /*! Peer address */
uint8_t addrType; /*! Peer address type */
dmSecIrk_t peerIrk; /*! Peer IRK */
dmSecCsrk_t peerCsrk; /*! Peer CSRK */
uint8_t keyValidMask; /*! Valid keys in this record */
bool_t inUse; /*! TRUE if record in use */
bool_t valid; /*! TRUE if record is valid */
bool_t peerAddedToRl; /*! TRUE if peer device's been added to resolving list */
bool_t peerRpao; /*! TRUE if RPA Only attribute's present on peer device */
/*! For slave local device */
dmSecLtk_t localLtk; /*! Local LTK */
uint8_t localLtkSecLevel; /*! Local LTK security level */
bool_t peerAddrRes; /*! TRUE if address resolution's supported on peer device (master) */
/*! For master local device */
dmSecLtk_t peerLtk; /*! Peer LTK */
uint8_t peerLtkSecLevel; /*! Peer LTK security level */
/*! for ATT server local device */
uint16_t cccTbl[APP_DB_NUM_CCCD]; /*! Client characteristic configuration descriptors */
uint32_t peerSignCounter; /*! Peer Sign Counter */
/*! for ATT client */
uint16_t hdlList[APP_DB_HDL_LIST_LEN]; /*! Cached handle list */
uint8_t discStatus; /*! Service discovery and configuration status */
} appDbRec_t;
/*! Database type */
typedef struct
{
appDbRec_t rec[APP_DB_NUM_RECS]; /*! Device database records */
char devName[ATT_DEFAULT_PAYLOAD_LEN]; /*! Device name */
uint8_t devNameLen; /*! Device name length */
} appDb_t;
/**************************************************************************************************
Local Variables
**************************************************************************************************/
/*! Database */
static appDb_t appDb;
/*! When all records are allocated use this index to determine which to overwrite */
static appDbRec_t *pAppDbNewRec = appDb.rec;
/*************************************************************************************************/
/*!
* \brief Initialize the device database.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbInit(void)
{
int fd = epic_file_open("pairings.bin", "r");
if(fd >= 0) {
if(epic_file_read(fd, &appDb, sizeof(appDb)) != sizeof(appDb)) {
memset(&appDb, 0, sizeof(appDb));
}
epic_file_close(fd);
}
}
static void store(void)
{
int fd = epic_file_open("pairings.bin", "w");
if(fd >= 0) {
if(epic_file_write(fd, &appDb, sizeof(appDb)) != sizeof(appDb)) {
}
epic_file_close(fd);
}
}
/*************************************************************************************************/
/*!
* \brief Create a new device database record.
*
* \param addrType Address type.
* \param pAddr Peer device address.
*
* \return Database record handle.
*/
/*************************************************************************************************/
appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr)
{
appDbRec_t *pRec = appDb.rec;
uint8_t i;
/* find a free record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (!pRec->inUse)
{
break;
}
}
/* if all records were allocated */
if (i == 0)
{
/* overwrite a record */
pRec = pAppDbNewRec;
/* get next record to overwrite */
pAppDbNewRec++;
if (pAppDbNewRec == &appDb.rec[APP_DB_NUM_RECS])
{
pAppDbNewRec = appDb.rec;
}
}
/* initialize record */
memset(pRec, 0, sizeof(appDbRec_t));
pRec->inUse = TRUE;
pRec->addrType = addrType;
BdaCpy(pRec->peerAddr, pAddr);
pRec->peerAddedToRl = FALSE;
pRec->peerRpao = FALSE;
store();
return (appDbHdl_t) pRec;
}
/*************************************************************************************************/
/*!
* \brief Get the next database record for a given record. For the first record, the function
* should be called with 'hdl' set to 'APP_DB_HDL_NONE'.
*
* \param hdl Database record handle.
*
* \return Next record handle found. APP_DB_HDL_NONE, otherwise.
*/
/*************************************************************************************************/
appDbHdl_t AppDbGetNextRecord(appDbHdl_t hdl)
{
appDbRec_t *pRec;
/* if first record is requested */
if (hdl == APP_DB_HDL_NONE)
{
pRec = appDb.rec;
}
/* if valid record passed in */
else if (AppDbRecordInUse(hdl))
{
pRec = (appDbRec_t *)hdl;
pRec++;
}
/* invalid record passed in */
else
{
return APP_DB_HDL_NONE;
}
/* look for next valid record */
while (pRec < &appDb.rec[APP_DB_NUM_RECS])
{
/* if record is in use */
if (pRec->inUse && pRec->valid)
{
/* record found */
return (appDbHdl_t)pRec;
}
/* look for next record */
pRec++;
}
/* end of records */
return APP_DB_HDL_NONE;
}
/*************************************************************************************************/
/*!
* \brief Delete a new device database record.
*
* \param hdl Database record handle.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbDeleteRecord(appDbHdl_t hdl)
{
((appDbRec_t *) hdl)->inUse = FALSE;
store();
}
/*************************************************************************************************/
/*!
* \brief Validate a new device database record. This function is called when pairing is
* successful and the devices are bonded.
*
* \param hdl Database record handle.
* \param keyMask Bitmask of keys to validate.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbValidateRecord(appDbHdl_t hdl, uint8_t keyMask)
{
((appDbRec_t *) hdl)->valid = TRUE;
((appDbRec_t *) hdl)->keyValidMask = keyMask;
store();
}
/*************************************************************************************************/
/*!
* \brief Check if a record has been validated. If it has not, delete it. This function
* is typically called when the connection is closed.
*
* \param hdl Database record handle.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbCheckValidRecord(appDbHdl_t hdl)
{
if (((appDbRec_t *) hdl)->valid == FALSE)
{
AppDbDeleteRecord(hdl);
}
}
/*************************************************************************************************/
/*!
* \brief Check if a database record is in use.
* \param hdl Database record handle.
*
* \return TURE if record in use. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbRecordInUse(appDbHdl_t hdl)
{
appDbRec_t *pRec = appDb.rec;
uint8_t i;
/* see if record is in database record list */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse && pRec->valid && (pRec == ((appDbRec_t *)hdl)))
{
return TRUE;
}
}
return FALSE;
}
/*************************************************************************************************/
/*!
* \brief Check if there is a stored bond with any device.
*
* \param hdl Database record handle.
*
* \return TRUE if a bonded device is found, FALSE otherwise.
*/
/*************************************************************************************************/
bool_t AppDbCheckBonded(void)
{
appDbRec_t *pRec = appDb.rec;
uint8_t i;
/* find a record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse)
{
return TRUE;
}
}
return FALSE;
}
/*************************************************************************************************/
/*!
* \brief Delete all database records.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbDeleteAllRecords(void)
{
appDbRec_t *pRec = appDb.rec;
uint8_t i;
/* set in use to false for all records */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
pRec->inUse = FALSE;
}
store();
}
/*************************************************************************************************/
/*!
* \brief Find a device database record by peer address.
*
* \param addrType Address type.
* \param pAddr Peer device address.
*
* \return Database record handle or APP_DB_HDL_NONE if not found.
*/
/*************************************************************************************************/
appDbHdl_t AppDbFindByAddr(uint8_t addrType, uint8_t *pAddr)
{
appDbRec_t *pRec = appDb.rec;
uint8_t peerAddrType = DmHostAddrType(addrType);
uint8_t i;
/* find matching record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse && (pRec->addrType == peerAddrType) && BdaCmp(pRec->peerAddr, pAddr))
{
return (appDbHdl_t) pRec;
}
}
return APP_DB_HDL_NONE;
}
/*************************************************************************************************/
/*!
* \brief Find a device database record by data in an LTK request.
*
* \param encDiversifier Encryption diversifier associated with key.
* \param pRandNum Pointer to random number associated with key.
*
* \return Database record handle or APP_DB_HDL_NONE if not found.
*/
/*************************************************************************************************/
appDbHdl_t AppDbFindByLtkReq(uint16_t encDiversifier, uint8_t *pRandNum)
{
appDbRec_t *pRec = appDb.rec;
uint8_t i;
/* find matching record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse && (pRec->localLtk.ediv == encDiversifier) &&
(memcmp(pRec->localLtk.rand, pRandNum, SMP_RAND8_LEN) == 0))
{
return (appDbHdl_t) pRec;
}
}
return APP_DB_HDL_NONE;
}
/*************************************************************************************************/
/*!
* \brief Get a key from a device database record.
*
* \param hdl Database record handle.
* \param type Type of key to get.
* \param pSecLevel If the key is valid, the security level of the key.
*
* \return Pointer to key if key is valid or NULL if not valid.
*/
/*************************************************************************************************/
dmSecKey_t *AppDbGetKey(appDbHdl_t hdl, uint8_t type, uint8_t *pSecLevel)
{
dmSecKey_t *pKey = NULL;
/* if key valid */
if ((type & ((appDbRec_t *) hdl)->keyValidMask) != 0)
{
switch(type)
{
case DM_KEY_LOCAL_LTK:
*pSecLevel = ((appDbRec_t *) hdl)->localLtkSecLevel;
pKey = (dmSecKey_t *) &((appDbRec_t *) hdl)->localLtk;
break;
case DM_KEY_PEER_LTK:
*pSecLevel = ((appDbRec_t *) hdl)->peerLtkSecLevel;
pKey = (dmSecKey_t *) &((appDbRec_t *) hdl)->peerLtk;
break;
case DM_KEY_IRK:
pKey = (dmSecKey_t *)&((appDbRec_t *)hdl)->peerIrk;
break;
case DM_KEY_CSRK:
pKey = (dmSecKey_t *)&((appDbRec_t *)hdl)->peerCsrk;
break;
default:
break;
}
}
return pKey;
}
/*************************************************************************************************/
/*!
* \brief Set a key in a device database record.
*
* \param hdl Database record handle.
* \param pKey Key data.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetKey(appDbHdl_t hdl, dmSecKeyIndEvt_t *pKey)
{
switch(pKey->type)
{
case DM_KEY_LOCAL_LTK:
((appDbRec_t *) hdl)->localLtkSecLevel = pKey->secLevel;
((appDbRec_t *) hdl)->localLtk = pKey->keyData.ltk;
break;
case DM_KEY_PEER_LTK:
((appDbRec_t *) hdl)->peerLtkSecLevel = pKey->secLevel;
((appDbRec_t *) hdl)->peerLtk = pKey->keyData.ltk;
break;
case DM_KEY_IRK:
((appDbRec_t *)hdl)->peerIrk = pKey->keyData.irk;
/* make sure peer record is stored using its identity address */
((appDbRec_t *)hdl)->addrType = pKey->keyData.irk.addrType;
BdaCpy(((appDbRec_t *)hdl)->peerAddr, pKey->keyData.irk.bdAddr);
break;
case DM_KEY_CSRK:
((appDbRec_t *)hdl)->peerCsrk = pKey->keyData.csrk;
/* sign counter must be initialized to zero when CSRK is generated */
((appDbRec_t *)hdl)->peerSignCounter = 0;
break;
default:
break;
}
store();
}
/*************************************************************************************************/
/*!
* \brief Get the client characteristic configuration descriptor table.
*
* \param hdl Database record handle.
*
* \return Pointer to client characteristic configuration descriptor table.
*/
/*************************************************************************************************/
uint16_t *AppDbGetCccTbl(appDbHdl_t hdl)
{
return ((appDbRec_t *) hdl)->cccTbl;
}
/*************************************************************************************************/
/*!
* \brief Set a value in the client characteristic configuration table.
*
* \param hdl Database record handle.
* \param idx Table index.
* \param value client characteristic configuration value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetCccTblValue(appDbHdl_t hdl, uint16_t idx, uint16_t value)
{
WSF_ASSERT(idx < APP_DB_NUM_CCCD);
((appDbRec_t *) hdl)->cccTbl[idx] = value;
store();
}
/*************************************************************************************************/
/*!
* \brief Get the discovery status.
*
* \param hdl Database record handle.
*
* \return Discovery status.
*/
/*************************************************************************************************/
uint8_t AppDbGetDiscStatus(appDbHdl_t hdl)
{
return ((appDbRec_t *) hdl)->discStatus;
}
/*************************************************************************************************/
/*!
* \brief Set the discovery status.
*
* \param hdl Database record handle.
* \param state Discovery status.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetDiscStatus(appDbHdl_t hdl, uint8_t status)
{
((appDbRec_t *) hdl)->discStatus = status;
store();
}
/*************************************************************************************************/
/*!
* \brief Get the cached handle list.
*
* \param hdl Database record handle.
*
* \return Pointer to handle list.
*/
/*************************************************************************************************/
uint16_t *AppDbGetHdlList(appDbHdl_t hdl)
{
return ((appDbRec_t *) hdl)->hdlList;
}
/*************************************************************************************************/
/*!
* \brief Set the cached handle list.
*
* \param hdl Database record handle.
* \param pHdlList Pointer to handle list.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetHdlList(appDbHdl_t hdl, uint16_t *pHdlList)
{
memcpy(((appDbRec_t *) hdl)->hdlList, pHdlList, sizeof(((appDbRec_t *) hdl)->hdlList));
store();
}
/*************************************************************************************************/
/*!
* \brief Get the device name.
*
* \param pLen Returned device name length.
*
* \return Pointer to UTF-8 string containing device name or NULL if not set.
*/
/*************************************************************************************************/
char *AppDbGetDevName(uint8_t *pLen)
{
/* if first character of name is NULL assume it is uninitialized */
if (appDb.devName[0] == 0)
{
*pLen = 0;
return NULL;
}
else
{
*pLen = appDb.devNameLen;
return appDb.devName;
}
}
/*************************************************************************************************/
/*!
* \brief Set the device name.
*
* \param len Device name length.
* \param pStr UTF-8 string containing device name.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetDevName(uint8_t len, char *pStr)
{
/* check for maximum device length */
len = (len <= sizeof(appDb.devName)) ? len : sizeof(appDb.devName);
memcpy(appDb.devName, pStr, len);
store();
}
/*************************************************************************************************/
/*!
* \brief Get address resolution attribute value read from a peer device.
*
* \param hdl Database record handle.
*
* \return TRUE if address resolution is supported in peer device. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbGetPeerAddrRes(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerAddrRes;
}
/*************************************************************************************************/
/*!
* \brief Set address resolution attribute value for a peer device.
*
* \param hdl Database record handle.
* \param addrRes Address resolution attribue value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerAddrRes(appDbHdl_t hdl, uint8_t addrRes)
{
((appDbRec_t *)hdl)->peerAddrRes = addrRes;
store();
}
/*************************************************************************************************/
/*!
* \brief Get sign counter for a peer device.
*
* \param hdl Database record handle.
*
* \return Sign counter for peer device.
*/
/*************************************************************************************************/
uint32_t AppDbGetPeerSignCounter(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerSignCounter;
}
/*************************************************************************************************/
/*!
* \brief Set sign counter for a peer device.
*
* \param hdl Database record handle.
* \param signCounter Sign counter for peer device.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerSignCounter(appDbHdl_t hdl, uint32_t signCounter)
{
((appDbRec_t *)hdl)->peerSignCounter = signCounter;
store();
}
/*************************************************************************************************/
/*!
* \brief Get the peer device added to resolving list flag value.
*
* \param hdl Database record handle.
*
* \return TRUE if peer device's been added to resolving list. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbGetPeerAddedToRl(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerAddedToRl;
}
/*************************************************************************************************/
/*!
* \brief Set the peer device added to resolving list flag to a given value.
*
* \param hdl Database record handle.
* \param peerAddedToRl Peer device added to resolving list flag value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerAddedToRl(appDbHdl_t hdl, bool_t peerAddedToRl)
{
((appDbRec_t *)hdl)->peerAddedToRl = peerAddedToRl;
store();
}
/*************************************************************************************************/
/*!
* \brief Get the resolvable private address only attribute flag for a given peer device.
*
* \param hdl Database record handle.
*
* \return TRUE if RPA Only attribute is present on peer device. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbGetPeerRpao(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerRpao;
}
/*************************************************************************************************/
/*!
* \brief Set the resolvable private address only attribute flag for a given peer device.
*
* \param hdl Database record handle.
* \param peerRpao Resolvable private address only attribute flag value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerRpao(appDbHdl_t hdl, bool_t peerRpao)
{
((appDbRec_t *)hdl)->peerRpao = peerRpao;
store();
}
/* clang-format on */
/*************************************************************************************************/
/*!
* \file
*
* \brief Application framework user interface.
*
* Copyright (c) 2011-2018 Arm Ltd. All Rights Reserved.
* ARM Ltd. confidential and proprietary.
*
* IMPORTANT. Your use of this file is governed by a Software License Agreement
* ("Agreement") that must be accepted in order to download or otherwise receive a
* copy of this file. You may not use or copy this file for any purpose other than
* as described in the Agreement. If you do not agree to all of the terms of the
* Agreement do not use this file and delete all copies in your possession or control;
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
* to any use, copying or further distribution of this software.
*/
/*************************************************************************************************/
#include "wsf_types.h"
#include "wsf_os.h"
#include "wsf_trace.h"
#include "app_ui.h"
/* card10:
* copied from: lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/common/app_ui.c
*
* Reason: has several user interactions which we likley have to implement
*/
/* clang-format off */
/* clang-formet turned off for easier diffing against orginal file */
/**************************************************************************************************
Global Variables
**************************************************************************************************/
/*! \brief Callback struct */
appUiCback_t appUiCbackTbl;
/*************************************************************************************************/
/*!
* \brief card10 - Should disable encryption. MAXIM bug reported to us in current static library. Requires
* this to be called before the BTLE app starts making advertisements. Avoids encryption
* rendering the frame unreadable.
*
* \return None.
*/
/*************************************************************************************************/
void llc_api_crypto_disable_tx();
/*************************************************************************************************/
/*!
* \brief Perform a user interface action based on the event value passed to the function.
*
* \param event User interface event value.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiAction(uint8_t event)
{
switch (event)
{
case APP_UI_NONE:
/* no action */
break;
case APP_UI_RESET_CMPL:
APP_TRACE_INFO0(">>> Reset complete <<<");
break;
case APP_UI_ADV_START:
llc_api_crypto_disable_tx();
APP_TRACE_INFO0(">>> Advertising started <<<");
break;
case APP_UI_ADV_STOP:
APP_TRACE_INFO0(">>> Advertising stopped <<<");
break;
case APP_UI_SCAN_START:
APP_TRACE_INFO0(">>> Scanning started <<<");
break;
case APP_UI_SCAN_STOP:
APP_TRACE_INFO0(">>> Scanning stopped <<<");
break;
case APP_UI_SCAN_REPORT:
APP_TRACE_INFO0(">>> Scan data received from peer <<<");
break;
case APP_UI_CONN_OPEN:
APP_TRACE_INFO0(">>> Connection opened <<<");
break;
case APP_UI_CONN_CLOSE:
APP_TRACE_INFO0(">>> Connection closed <<<");
break;
case APP_UI_SEC_PAIR_CMPL:
APP_TRACE_INFO0(">>> Pairing completed successfully <<<");
break;
case APP_UI_SEC_PAIR_FAIL:
APP_TRACE_INFO0(">>> Pairing failed <<<");
break;
case APP_UI_SEC_ENCRYPT:
APP_TRACE_INFO0(">>> Connection encrypted <<<");
break;
case APP_UI_SEC_ENCRYPT_FAIL:
APP_TRACE_INFO0(">>> Encryption failed <<<");
break;
case APP_UI_PASSKEY_PROMPT:
APP_TRACE_INFO0(">>> Prompt user to enter passkey <<<");
break;
case APP_UI_ALERT_CANCEL:
APP_TRACE_INFO0(">>> Cancel a low or high alert <<<");
break;
case APP_UI_ALERT_LOW:
APP_TRACE_INFO0(">>> Low alert <<<");
break;
case APP_UI_ALERT_HIGH:
APP_TRACE_INFO0(">>> High alert <<<");
break;
case APP_UI_ADV_SET_START_IND:
APP_TRACE_INFO0(">>> Advertising set(s) started <<<");
break;
case APP_UI_ADV_SET_STOP_IND:
APP_TRACE_INFO0(">>> Advertising set(s) stopped <<<");
break;
case APP_UI_SCAN_REQ_RCVD_IND:
APP_TRACE_INFO0(">>> Scan request received <<<");
break;
case APP_UI_EXT_SCAN_START_IND:
APP_TRACE_INFO0(">>> Extended scanning started <<<");
break;
case APP_UI_EXT_SCAN_STOP_IND:
APP_TRACE_INFO0(">>> Extended scanning stopped <<<");
break;
case APP_UI_PER_ADV_SET_START_IND:
APP_TRACE_INFO0(">>> Periodic advertising set started <<<");
break;
case APP_UI_PER_ADV_SET_STOP_IND:
APP_TRACE_INFO0(">>> Periodic advertising set stopped <<<");
break;
case APP_UI_PER_ADV_SYNC_EST_IND:
APP_TRACE_INFO0(">>> Periodic advertising sync established <<<");
break;
case APP_UI_PER_ADV_SYNC_LOST_IND:
APP_TRACE_INFO0(">>> Periodic advertising sync lost <<<");
break;
default:
break;
}
if (appUiCbackTbl.actionCback)
{
(*appUiCbackTbl.actionCback)(event);
}
}
/*************************************************************************************************/
/*!
* \brief Display a passkey.
*
* \param passkey Passkey to display.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiDisplayPasskey(uint32_t passkey)
{
APP_TRACE_INFO1(">>> Passkey: %d <<<", passkey);
}
/*************************************************************************************************/
/*!
* \brief Display a confirmation value.
*
* \param confirm Confirm value to display.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiDisplayConfirmValue(uint32_t confirm)
{
APP_TRACE_INFO1(">>> Confirm Value: %d <<<", confirm);
}
/*************************************************************************************************/
/*!
* \brief Display an RSSI value.
*
* \param rssi Rssi value to display.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiDisplayRssi(int8_t rssi)
{
APP_TRACE_INFO1(">>> RSSI: %d dBm<<<", rssi);
}
/*************************************************************************************************/
/*!
* \brief Handle a UI timer expiration event.
*
* \param pMsg Pointer to message.
*
* \return None.
*/
/*************************************************************************************************/
void appUiTimerExpired(wsfMsgHdr_t *pMsg)
{
}
/*************************************************************************************************/
/*!
* \brief Perform button press polling. This function is called to handle WSF
* message APP_BTN_POLL_IND.
*
* \return None.
*/
/*************************************************************************************************/
void appUiBtnPoll(void)
{
if (appUiCbackTbl.btnPollCback)
{
(*appUiCbackTbl.btnPollCback)();
}
}
/*************************************************************************************************/
/*!
* \brief Handle a hardware button press. This function is called to handle WSF
* event APP_BTN_DOWN_EVT.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiBtnPressed(void)
{
}
/*************************************************************************************************/
/*!
* \brief Register a callback function to receive application button press events.
*
* \return None.
*
* \note Registered by application to receive button events
*/
/*************************************************************************************************/
void AppUiBtnRegister(appUiBtnCback_t btnCback)
{
appUiCbackTbl.btnCback = btnCback;
}
/*************************************************************************************************/
/*!
* \brief Register a callback function to receive action events.
*
* \return None.
*
* \note Registered by platform
*/
/*************************************************************************************************/
void AppUiActionRegister(appUiActionCback_t actionCback)
{
appUiCbackTbl.actionCback = actionCback;
}
/*************************************************************************************************/
/*!
* \brief Register a callback function to receive APP_BTN_POLL_IND events.
*
* \return None.
*
* \note Registered by platform
*/
/*************************************************************************************************/
void AppUiBtnPollRegister(appUiBtnPollCback_t btnPollCback)
{
appUiCbackTbl.btnPollCback = btnPollCback;
}
/*************************************************************************************************/
/*!
* \brief Play a sound.
*
* \param pSound Pointer to sound tone/duration array.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiSoundPlay(const appUiSound_t *pSound)
{
}
/*************************************************************************************************/
/*!
* \brief Stop the sound that is currently playing.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiSoundStop(void)
{
}
/*************************************************************************************************/
/*!
* \brief Button test function-- for test purposes only.
*
* \return None.
*/
/*************************************************************************************************/
void AppUiBtnTest(uint8_t btn)
{
if (appUiCbackTbl.btnCback)
{
(*appUiCbackTbl.btnCback)(btn);
}
}
/* clang-format on */
#include "epicardium.h"
#include "modules/log.h"
#include "os/core.h"
#include "os/config.h"
#include "fs_util.h"
#include "fs/fs_util.h"
#include "wsf_types.h"
#include "wsf_buf.h"
#include "wsf_trace.h"
......@@ -12,12 +13,29 @@
#include "FreeRTOS.h"
#include "timers.h"
#include <machine/endian.h>
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#define FACTOR 2
#define WSF_BUF_POOLS 6
#define WSF_BUF_SIZE 0x1048
#define WSF_BUF_SIZE (0x1048 * FACTOR)
struct log_packet_header {
uint32_t original_length;
uint32_t included_length;
uint32_t packet_flags;
uint32_t cumulative_drops;
uint32_t timestamp_us_h;
uint32_t timestamp_us_l;
};
static uint8_t bdAddr[6] = { 0xCA, 0x4D, 0x10, 0x00, 0x00, 0x00 };
static const uint8_t log_header[] = {
'b', 't', 's', 'n', 'o', 'o', 'p', 0, 0, 0, 0, 1, 0, 0, 0x03, 0xea
};
uint32_t SystemHeapSize = WSF_BUF_SIZE;
uint32_t SystemHeap[WSF_BUF_SIZE / 4];
......@@ -30,12 +48,12 @@ static TaskHandle_t ble_task_id = NULL;
/* clang-format off */
static wsfBufPoolDesc_t mainPoolDesc[WSF_BUF_POOLS] =
{
{ 16, 8 },
{ 32, 4 },
{ 64, 4 },
{ 128, 4 },
{ 256, 4 },
{ 512, 4 }
{ 16, 8*FACTOR },
{ 32, 4*FACTOR },
{ 64, 4*FACTOR },
{ 128, 4*FACTOR },
{ 256, 4*FACTOR },
{ 512, 4*FACTOR }
};
/* clang-format on */
......@@ -43,20 +61,20 @@ static StaticTimer_t x;
static TimerHandle_t timerWakeup = NULL;
static int lasttick = 0;
static int log_fd;
static bool log_dirty = false;
static bool log_enabled = false;
static int log_lastflushtick = 0;
/*! \brief Stack initialization for app. */
extern void LlStackInit(void);
extern void StackInit(void);
extern void AppInit(void);
extern void bleuart_init(void);
extern void bleFileTransfer_init(void);
extern void bleCard10_init(void);
extern void bleESS_init(void);
extern void BbBleDrvSetTxPower(int8_t power);
/*************************************************************************************************/
void PalSysAssertTrap(void)
{
while (1) {
}
}
/*************************************************************************************************/
static bool_t myTrace(const uint8_t *pBuf, uint32_t len)
{
......@@ -69,6 +87,79 @@ static bool_t myTrace(const uint8_t *pBuf, uint32_t len)
return FALSE;
}
/*************************************************************************************************/
void WsfPDump(wsfPDumpType_t pdType, uint16_t length, uint8_t *pBuffer)
{
uint32_t direction;
uint8_t type;
int ret;
if (log_enabled) {
switch (pdType) {
case WSF_PDUMP_TYPE_HCI_CMD:
direction = 0;
type = 0x01;
break;
case WSF_PDUMP_TYPE_HCI_EVT:
direction = 1;
type = 0x04;
break;
case WSF_PDUMP_TYPE_HCI_TX_ACL:
direction = 0;
type = 0x02;
break;
case WSF_PDUMP_TYPE_HCI_RX_ACL:
direction = 1;
type = 0x02;
break;
default:
LOG_WARN("ble", "Unknown packet type to be logged");
return;
}
uint64_t tick = xTaskGetTickCount();
uint64_t timestamp_us = tick * 1000;
struct log_packet_header header = {
.original_length = __htonl(length + 1),
.included_length = __htonl(length + 1),
.packet_flags = __htonl(direction),
.cumulative_drops = __htonl(0),
.timestamp_us_h = __htonl(timestamp_us >> 32),
.timestamp_us_l = __htonl(timestamp_us & 0xFFFFFFFF)
};
ret = epic_file_write(log_fd, &header, sizeof(header));
if (ret != sizeof(header)) {
goto out_err;
}
ret = epic_file_write(log_fd, &type, sizeof(type));
if (ret != sizeof(type)) {
goto out_err;
}
ret = epic_file_write(log_fd, pBuffer, length);
if (ret != length) {
goto out_err;
}
log_dirty = true;
}
return;
out_err:
LOG_WARN("ble", "Log file write failed. Logging diabled");
log_enabled = false;
}
/*************************************************************************************************/
void __wrap_BbBleDrvRand(uint8_t *pBuf, uint8_t len)
{
epic_csprng_read(pBuf, len);
//printf("BbBleDrvRand(%d) = %02x %02x ...\n", len, pBuf[0], pBuf[1]);
}
/*************************************************************************************************/
static void WsfInit(void)
{
......@@ -90,14 +181,13 @@ static void WsfInit(void)
/* TODO: We need a source of MACs */
static void setAddress(void)
{
uint8_t bdAddr[6] = { 0xCA, 0x4D, 0x10, 0x00, 0x00, 0x00 };
char buf[32];
int result = fs_read_text_file("mac.txt", buf, sizeof(buf));
int result = epic_config_get_string("ble_mac", buf, sizeof(buf));
if (result == -1) {
APP_TRACE_INFO0("mac.txt not found, generating random MAC");
epic_trng_read(bdAddr + 3, 3);
if (result < 0) {
APP_TRACE_INFO0("ble_mac not set. Generating random MAC");
epic_csprng_read(bdAddr + 3, 3);
sprintf(buf,
"%02x:%02x:%02x:%02x:%02x:%02x\n",
bdAddr[0],
......@@ -106,9 +196,9 @@ static void setAddress(void)
bdAddr[3],
bdAddr[4],
bdAddr[5]);
fs_write_file("mac.txt", buf, strlen(buf));
epic_config_set_string("ble_mac", buf);
} else {
APP_TRACE_INFO1("mac file contents: %s", buf);
APP_TRACE_INFO1("ble_mac: %s", buf);
}
int a, b, c, d, e, f;
......@@ -134,6 +224,11 @@ static void setAddress(void)
HciVsSetBdAddr(bdAddr);
}
/*************************************************************************************************/
void epic_ble_get_address(uint8_t *addr)
{
memcpy(addr, bdAddr, sizeof(bdAddr));
}
/*************************************************************************************************/
static void vTimerCallback(xTimerHandle pxTimer)
{
//printf("wake\n");
......@@ -160,44 +255,40 @@ void WsfTimerNotify(void)
//printf("WsfTimerNotify\n");
// TODO: Can we do this without waking up the task?
// xTimerChangePeriodFromISR exists
notify();
NVIC->STIR = RSV11_IRQn;
}
/*************************************************************************************************/
void wsf_ble_signal_event(void)
{
//printf("wsf_ble_signal_event\n");
notify();
NVIC->STIR = RSV11_IRQn;
}
/*************************************************************************************************/
#define BLEMAXCFGBYTES 100
bool ble_shall_start(void)
void RSV11_IRQHandler(void)
{
int bleConfigFile = epic_file_open("ble.txt", "r");
if (bleConfigFile < 0) {
LOG_INFO("ble", "can not open ble.txt -> BLE is not started");
epic_file_close(bleConfigFile);
return false;
}
char cfgBuf[BLEMAXCFGBYTES + 1];
int readNum = epic_file_read(bleConfigFile, cfgBuf, BLEMAXCFGBYTES);
epic_file_close(bleConfigFile);
if (readNum < 0) {
LOG_WARN("ble", "can not read ble.txt -> BLE is not started");
return false;
notify();
}
cfgBuf[readNum] = '\0';
char bleActiveStr[] = "active=true";
cfgBuf[sizeof(bleActiveStr) - 1] = '\0';
/*************************************************************************************************/
bool ble_is_enabled(void)
{
/*
* 0 = unknown, check config
* 1 = disabled
* 2 = enabled
*/
static int ble_state = 0;
if (strcmp(cfgBuf, "active=true") != 0) {
LOG_INFO("ble", "BLE is disabled.");
return false;
} else {
if (ble_state == 0) {
if (config_get_boolean_with_default("ble_enable", false)) {
ble_state = 2;
LOG_INFO("ble", "BLE is enabled.");
return true;
} else {
ble_state = 1;
LOG_INFO("ble", "BLE is disabled.");
}
}
return ble_state == 2;
}
/*************************************************************************************************/
static void scheduleTimer(void)
......@@ -233,6 +324,95 @@ static void scheduleTimer(void)
}
}
/*************************************************************************************************/
static void log_flush(void)
{
int tick = xTaskGetTickCount();
if (tick - log_lastflushtick > 5000) {
log_lastflushtick = tick;
if (log_dirty) {
log_dirty = false;
LOG_INFO("ble", "Flushing log");
epic_file_flush(log_fd);
}
}
}
/*************************************************************************************************/
static int log_rotate(void)
{
int i;
char filename_old[16];
char filename_new[16];
struct epic_stat stat;
int ret;
epic_file_stat("logs", &stat);
if (stat.type == EPICSTAT_FILE) {
return -1;
}
if (stat.type == EPICSTAT_NONE) {
ret = epic_file_mkdir("logs");
if (ret < 0) {
return ret;
}
}
if (epic_file_stat("logs/ble9.log", &stat) == 0) {
epic_file_unlink("logs/ble9.log");
}
for (i = 8; i > 0; i--) {
sprintf(filename_old, "logs/ble%d.log", i);
sprintf(filename_new, "logs/ble%d.log", i + 1);
if (epic_file_stat(filename_old, &stat) == 0) {
epic_file_rename(filename_old, filename_new);
}
}
if (epic_file_stat("logs/ble.log", &stat) == 0) {
epic_file_rename("logs/ble.log", "logs/ble1.log");
}
return 0;
}
/*************************************************************************************************/
static void log_init(void)
{
int ret;
log_enabled = config_get_boolean_with_default("ble_log_enable", false);
if (!log_enabled) {
return;
}
LOG_INFO("ble", "Log is enabled");
if (log_rotate() < 0) {
log_enabled = false;
LOG_WARN("ble", "Can not rotate logs. Logging disabled.");
return;
}
log_fd = epic_file_open("logs/ble.log", "w");
if (log_fd < 0) {
log_enabled = false;
LOG_WARN("ble", "Can not create log file. Logging disabled.");
return;
}
ret = epic_file_write(log_fd, log_header, sizeof(log_header));
if (ret != sizeof(log_header)) {
log_enabled = false;
LOG_WARN(
"ble",
"Can not create log file header. Logging disabled."
);
return;
}
}
/*************************************************************************************************/
void vBleTask(void *pvParameters)
{
ble_task_id = xTaskGetCurrentTaskHandle();
......@@ -242,21 +422,33 @@ void vBleTask(void *pvParameters)
*/
vTaskDelay(pdMS_TO_TICKS(500));
log_init();
/* We are going to execute FreeRTOS functions from callbacks
* coming from this interrupt. Its priority needs to be
* reduced to allow this. */
NVIC_SetPriority(RSV11_IRQn, 2);
NVIC_EnableIRQ(RSV11_IRQn);
WsfInit();
taskENTER_CRITICAL();
/* Critical section to prevent a loop in iq_capture2 / meas_freq in
* /home/maxim/Documents/src/BLE/mcbusw/Hardware/Micro/ME14/Firmware/trunk/NDALibraries/BTLE/phy/dbb/prot/ble/pan2g5/afe/max32665/board_config.c:275
* if BHI160 and -Ddebug_prints=true is enabled. See #115. */
LlStackInit();
taskEXIT_CRITICAL();
StackInit();
BbBleDrvSetTxPower(0);
setAddress();
NVIC_SetPriority(BTLE_SFD_TO_IRQn, 2);
NVIC_SetPriority(BTLE_TX_DONE_IRQn, 2);
NVIC_SetPriority(BTLE_RX_RCVD_IRQn, 2);
AppInit();
BleStart();
AttsDynInit();
bleuart_init();
bleFileTransfer_init();
bleCard10_init();
bleESS_init();
lasttick = xTaskGetTickCount();
......@@ -272,5 +464,8 @@ void vBleTask(void *pvParameters)
ulTaskNotifyTake(pdTRUE, portTICK_PERIOD_MS * 1000);
wsfOsDispatcher();
scheduleTimer();
if (log_enabled) {
log_flush();
}
}
}
#include "ble_api.h"
#include "epicardium.h"
#include "os/core.h"
#include "os/config.h"
#include "wsf_types.h"
#include "util/bstream.h"
#include "wsf_msg.h"
#include "wsf_trace.h"
#include "app_api.h"
#include "app_db.h"
#include "svc_ch.h"
#include "profiles/gap_api.h"
#include <stdio.h>
#include <string.h>
#define DEFAULT_ADV_INTERVAL_US 500000
/*! configurable parameters for advertising */
static appAdvCfg_t bleAdvCfg = {
{ 0, 0 }, /*! Advertising durations in ms */
{ DEFAULT_ADV_INTERVAL_US / 625,
0 } /*! Advertising intervals in 0.625 ms units */
};
static bool tainted;
/**************************************************************************************************
Advertising Data
**************************************************************************************************/
/* clang-format off */
/*! advertising data, discoverable mode */
static const uint8_t bleAdvDataDisc[] = {
/*! flags */
2, /*! length */
DM_ADV_TYPE_FLAGS, /*! AD type */
DM_FLAG_LE_LIMITED_DISC | /*! flags */
DM_FLAG_LE_BREDR_NOT_SUP,
3,
DM_ADV_TYPE_APPEARANCE,
UINT16_TO_BYTES(CH_APPEAR_WATCH),
/*! service UUID list */
17,
DM_ADV_TYPE_128_UUID_PART,
CARD10_UUID_SUFFIX,
0x0,
CARD10_UUID_PREFIX,
2, /*! length */
DM_ADV_TYPE_TX_POWER, /*! AD type */
0, /*! tx power */
};
/*! advertising data, discoverable mode with HID service*/
static const uint8_t bleAdvDataDiscHID[] = {
/*! flags */
2, /*! length */
DM_ADV_TYPE_FLAGS, /*! AD type */
DM_FLAG_LE_LIMITED_DISC | /*! flags */
DM_FLAG_LE_BREDR_NOT_SUP,
3,
DM_ADV_TYPE_APPEARANCE,
UINT16_TO_BYTES(CH_APPEAR_WATCH),
/*! service UUID list */
17,
DM_ADV_TYPE_128_UUID_PART,
CARD10_UUID_SUFFIX,
0x0,
CARD10_UUID_PREFIX,
3, /*! length */
DM_ADV_TYPE_16_UUID_PART, /*! AD type */
UINT16_TO_BYTES(ATT_UUID_HID_SERVICE)
};
/*! scan data, discoverable mode */
uint8_t bleScanDataDisc[] = {
/*! device name */
14, /*! length */
DM_ADV_TYPE_LOCAL_NAME, /*! AD type */
'c','a','r','d','1','0','-','0','0','0','0','0','0',
3, /*! length */
DM_ADV_TYPE_16_SOLICIT, /*! AD type */
UINT16_TO_BYTES(ATT_UUID_CURRENT_TIME_SERVICE),
};
/* clang-format on */
/*! advertising data, connectable mode */
static const uint8_t bleAdvDataConn[] = {
/*! flags */
2, /*! length */
DM_ADV_TYPE_FLAGS, /*! AD type */
DM_FLAG_LE_BREDR_NOT_SUP,
};
static uint8_t advertising_mode = APP_MODE_NONE;
static uint8_t advertising_mode_target = APP_MODE_NONE;
void ble_adv_proc_msg(bleMsg_t *pMsg)
{
switch (pMsg->hdr.event) {
case DM_ADV_START_IND:
LOG_INFO(
"ble",
"Advertisement started %u %u",
advertising_mode,
advertising_mode_target
);
if (advertising_mode != advertising_mode_target ||
advertising_mode_target == APP_MODE_NONE) {
AppAdvStop();
}
break;
case DM_ADV_STOP_IND:
LOG_INFO(
"ble",
"Advertisement stopped %u %u",
advertising_mode,
advertising_mode_target
);
if (advertising_mode != advertising_mode_target) {
advertising_mode = advertising_mode_target;
AppAdvStart(advertising_mode);
}
break;
case DM_CONN_CLOSE_IND:
/* Stack overwrites advertising mode after connection close.
* Force our desired mode.
*/
advertising_mode = APP_MODE_NONE;
AppAdvStop();
break;
};
}
void ble_adv_init(void)
{
char buf[32];
char a, b, c, d, e, f, K;
/* clang-format off */
int result = epic_config_get_string("ble_mac", buf, sizeof(buf));
if (result == 0) {
if (sscanf(buf,
"%c%c:%c%c:%c%c:%c%c:%c%c:%c%c",
&K, &K, &K, &K, &K, &K,
&a, &b, &c, &d, &e, &f) == 12) {
bleScanDataDisc[9] = a;
bleScanDataDisc[10] = b;
bleScanDataDisc[11] = c;
bleScanDataDisc[12] = d;
bleScanDataDisc[13] = e;
bleScanDataDisc[14] = f;
}
}
/* clang-format on */
pAppAdvCfg = (appAdvCfg_t *)&bleAdvCfg;
}
void ble_adv_setup(void)
{
/* set advertising and scan response data for discoverable mode */
if (config_get_boolean_with_default("ble_hid_enable", false)) {
AppAdvSetData(
APP_ADV_DATA_DISCOVERABLE,
sizeof(bleAdvDataDiscHID),
(uint8_t *)bleAdvDataDiscHID
);
} else {
AppAdvSetData(
APP_ADV_DATA_DISCOVERABLE,
sizeof(bleAdvDataDisc),
(uint8_t *)bleAdvDataDisc
);
}
AppAdvSetData(
APP_SCAN_DATA_DISCOVERABLE,
sizeof(bleScanDataDisc),
(uint8_t *)bleScanDataDisc
);
/* set advertising and scan response data for connectable mode */
AppAdvSetData(
APP_ADV_DATA_CONNECTABLE,
sizeof(bleAdvDataConn),
(uint8_t *)bleAdvDataConn
);
AppAdvSetData(APP_SCAN_DATA_CONNECTABLE, 0, NULL);
bleAdvCfg.advInterval[0] = DEFAULT_ADV_INTERVAL_US / 625;
}
void ble_adv_set_interval(uint32_t interval_us)
{
bleAdvCfg.advInterval[0] = interval_us / 625;
tainted = true;
}
void ble_adv_stop(void)
{
if (advertising_mode != APP_MODE_NONE) {
advertising_mode_target = APP_MODE_NONE;
advertising_mode = APP_MODE_NONE;
AppAdvStop();
}
}
static void adv_start(uint8_t mode)
{
if (advertising_mode != APP_MODE_NONE) {
/* We need to stop advertising in between or the
* adv set will not be changed.
* Also need to wait for the stop operation to finish
* before we can start again
* Also need to set the variables first as we don't
* have a lock on the stack.*/
advertising_mode_target = mode;
advertising_mode = APP_MODE_NONE;
AppAdvStop();
} else {
advertising_mode = mode;
advertising_mode_target = mode;
AppAdvStart(advertising_mode);
}
tainted = false;
}
void ble_adv_start(uint8_t mode)
{
adv_start(mode);
tainted = true;
}
void ble_adv_discoverable(bool discoverable)
{
if (discoverable) {
if (advertising_mode != APP_MODE_DISCOVERABLE || tainted) {
LOG_INFO("ble", "Making bondable and discoverable");
adv_start(APP_MODE_DISCOVERABLE);
}
} else {
/* TODO: This does way more than the function name indicates */
if (AppDbCheckBonded()) {
if (advertising_mode != APP_MODE_CONNECTABLE ||
tainted) {
LOG_INFO("ble", "Bonded. Making connectable");
adv_start(APP_MODE_CONNECTABLE);
}
} else {
LOG_INFO("ble", "Not bonded. Stop advertising");
ble_adv_stop();
}
}
}
#pragma once
#include "epicardium.h"
#include <stdint.h>
#include "wsf_types.h"
#include "att_api.h"
#include "dm_api.h"
#define CARD10_UUID_SUFFIX \
0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23
#define CARD10_UUID_PREFIX 0x02, 0x23, 0x42
/**************************************************************************************************
Data Types
**************************************************************************************************/
/*! Application message type */
typedef union
{
wsfMsgHdr_t hdr;
dmEvt_t dm;
attsCccEvt_t ccc;
attEvt_t att;
} bleMsg_t;
/**************************************************************************************************
Function Declarations
**************************************************************************************************/
......@@ -11,3 +37,23 @@
/*************************************************************************************************/
void BleStart(void);
/* ATT client module interface. Used by main BLE module */
void bleValueUpdate(attEvt_t *pMsg);
void bleDiscCback(dmConnId_t connId, uint8_t status);
void ble_epic_att_api_init(void);
void ble_epic_att_api_event(attEvt_t *att_event);
void ble_epic_att_api_free_att_write_data(struct epic_att_write *w);
void ble_epic_ble_api_trigger_event(enum epic_ble_event_type type, void *data);
void ble_epic_ble_api_init(void);
void ble_epic_dm_api_event(dmEvt_t *dm_event);
void ble_epic_disc_cfg_complete(void);
void ble_adv_init(void);
void ble_adv_setup(void);
void ble_adv_set_interval(uint32_t interval_ms);
void ble_adv_stop(void);
void ble_adv_start(uint8_t mode);
void ble_adv_discoverable(bool discoverable);
void ble_adv_proc_msg(bleMsg_t *pMsg);
#include <stdint.h>
#include <stdbool.h>
#include "ble_api.h"
#include "wsf_types.h"
#include "util/bstream.h"
#include "app_api.h"
#include "app_cfg.h"
#include "att_api.h"
#include "dm_api.h"
#include "gatt/gatt_api.h"
#include "profiles/gap_api.h"
#include "tipc/tipc_api.h"
#include "os/core.h"
/* card10:
* copied from lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/tag/tag_main.c
* and lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/watch/watch_main.c
*/
/* clang-format off */
/* clang-formet turned off for easier diffing against orginal file */
/**************************************************************************************************
ATT Client Discovery Data
**************************************************************************************************/
static uint16_t s_hdlList[APP_DB_HDL_LIST_LEN];
/*! the Client handle list, s_hdlList[], is set as follows:
*
* ------------------------------- <- BLE_DISC_GATT_START
* | GATT svc changed handle |
* -------------------------------
* | GATT svc changed ccc handle |
* ------------------------------- <- BLE_DISC_GAP_START
* | GAP central addr res handle |
* -------------------------------
* | GAP RPA Only handle |
* ------------------------------- <- BLE_DISC_CTS_START
* | TIPC_CTS_CT_HDL_IDX |
* -------------------------------
* | TIPC_CTS_CT_CCC_HDL_IDX |
* -------------------------------
* | TIPC_CTS_LTI_HDL_IDX |
* -------------------------------
* | TIPC_CTS_RTI_HDL_IDX |
* -------------------------------
*/
/*! Start of each service's handles in the the handle list */
#define BLE_DISC_GATT_START 0
#define BLE_DISC_GAP_START (BLE_DISC_GATT_START + GATT_HDL_LIST_LEN)
#define BLE_DISC_CTS_START (BLE_DISC_GAP_START + GAP_HDL_LIST_LEN)
#define BLE_DISC_HDL_LIST_LEN (BLE_DISC_CTS_START + TIPC_CTS_HDL_LIST_LEN)
/*! Pointers into handle list for each service's handles */
static uint16_t *pBleGattHdlList = &s_hdlList[BLE_DISC_GATT_START];
static uint16_t *pBleGapHdlList = &s_hdlList[BLE_DISC_GAP_START];
static uint16_t *pBleCtsHdlList = &s_hdlList[BLE_DISC_CTS_START];
/* sanity check: make sure handle list length is <= app db handle list length */
extern char wsf_ct_assert[(BLE_DISC_HDL_LIST_LEN <= APP_DB_HDL_LIST_LEN) ? 1 : -1];
/**************************************************************************************************
ATT Client Data
**************************************************************************************************/
/* Default value for GATT service changed ccc descriptor */
static const uint8_t bleGattScCccVal[] = {UINT16_TO_BYTES(ATT_CLIENT_CFG_INDICATE)};
/* List of characteristics to configure */
static const attcDiscCfg_t bleDiscCfgList[] =
{
/* Write: GATT service changed ccc descriptor */
{bleGattScCccVal, sizeof(bleGattScCccVal), (GATT_SC_CCC_HDL_IDX + BLE_DISC_GATT_START)},
{NULL, 0, (GAP_DN_HDL_IDX + BLE_DISC_GAP_START)},
/* Read: GAP central address resolution attribute */
{NULL, 0, (GAP_CAR_HDL_IDX + BLE_DISC_GAP_START)},
/* Read: CTS Current time */
{NULL, 0, (TIPC_CTS_CT_HDL_IDX + BLE_DISC_CTS_START)},
/* Read: CTS Local time information */
{NULL, 0, (TIPC_CTS_LTI_HDL_IDX + BLE_DISC_CTS_START)},
/* Read: CTS Reference time information */
{NULL, 0, (TIPC_CTS_RTI_HDL_IDX + BLE_DISC_CTS_START)},
};
/* Characteristic configuration list length */
#define BLE_DISC_CFG_LIST_LEN (sizeof(bleDiscCfgList) / sizeof(attcDiscCfg_t))
/* sanity check: make sure configuration list length is <= handle list length */
extern char wsf_ct_assert[(BLE_DISC_CFG_LIST_LEN <= BLE_DISC_HDL_LIST_LEN) ? 1 : -1];
/**************************************************************************************************
ATT Client Discovery Data
**************************************************************************************************/
/*! Discovery states: enumeration of services to be discovered */
enum
{
BLE_DISC_GATT_SVC, /* GATT service */
BLE_DISC_GAP_SVC, /* GAP service */
BLE_DISC_SLAVE_CTS_SVC,
BLE_DISC_SVC_MAX /* Discovery complete */
};
/*************************************************************************************************/
/*!
* \brief Process a received ATT indication.
*
* \param pMsg Pointer to ATT callback event message.
*
* \return None.
*/
/*************************************************************************************************/
void bleValueUpdate(attEvt_t *pMsg)
{
if (pMsg->hdr.status == ATT_SUCCESS)
{
/* determine which profile the handle belongs to */
/* GATT */
if (GattValueUpdate(pBleGattHdlList, pMsg) == ATT_SUCCESS)
{
return;
}
/* GAP */
if (GapValueUpdate(pBleGapHdlList, pMsg) == ATT_SUCCESS)
{
return;
}
/* current time */
if (TipcCtsValueUpdate(pBleCtsHdlList, pMsg) == ATT_SUCCESS)
{
return;
}
}
}
/*************************************************************************************************/
/*!
* \brief GAP service discovery has completed.
*
* \param connId Connection identifier.
*
* \return None.
*/
/*************************************************************************************************/
static void bleDiscGapCmpl(dmConnId_t connId)
{
appDbHdl_t dbHdl;
/* if RPA Only attribute found on peer device */
if ((pBleGapHdlList[GAP_RPAO_HDL_IDX] != ATT_HANDLE_NONE) &&
((dbHdl = AppDbGetHdl(connId)) != APP_DB_HDL_NONE))
{
/* update DB */
AppDbSetPeerRpao(dbHdl, TRUE);
}
}
/*************************************************************************************************/
/*!
* \brief Discovery callback.
*
* \param connId Connection identifier.
* \param status Service or configuration status.
*
* \return None.
*/
/*************************************************************************************************/
void bleDiscCback(dmConnId_t connId, uint8_t status)
{
static uint8_t discState;
static const char * const disc_status[] = {
"APP_DISC_INIT", /*!< \brief No discovery or configuration complete */
"APP_DISC_SEC_REQUIRED", /*!< \brief Security required to complete configuration */
"APP_DISC_START", /*!< \brief Service discovery started */
"APP_DISC_CMPL", /*!< \brief Service discovery complete */
"APP_DISC_FAILED", /*!< \brief Service discovery failed */
"APP_DISC_CFG_START", /*!< \brief Service configuration started */
"APP_DISC_CFG_CONN_START", /*!< \brief Configuration for connection setup started */
"APP_DISC_CFG_CMPL" /*!< \brief Service configuration complete */
};
LOG_INFO("ble", "bleDiscCback: %s (%d)", disc_status[status], status);
switch(status)
{
case APP_DISC_INIT:
/* set handle list when initialization requested */
AppDiscSetHdlList(connId, BLE_DISC_HDL_LIST_LEN, s_hdlList);
break;
case APP_DISC_SEC_REQUIRED:
/* request security */
AppSlaveSecurityReq(connId);
break;
case APP_DISC_START:
/* initialize discovery state */
discState = BLE_DISC_GATT_SVC;
GattDiscover(connId, pBleGattHdlList);
break;
case APP_DISC_FAILED:
case APP_DISC_CMPL:
if (status == APP_DISC_FAILED && pAppCfg->abortDisc)
{
if (discState == BLE_DISC_GATT_SVC)
{
/* discovery failed */
AppDiscComplete(connId, APP_DISC_FAILED);
break;
}
}
/* next discovery state */
discState++;
if (discState == BLE_DISC_GAP_SVC)
{
/* discover GAP service */
GapDiscover(connId, pBleGapHdlList);
}
else if (discState == BLE_DISC_SLAVE_CTS_SVC)
{
/* discover current time service */
TipcCtsDiscover(connId, pBleCtsHdlList);
}
else
{
/* discovery complete */
AppDiscComplete(connId, APP_DISC_CMPL);
/* GAP service discovery completed */
bleDiscGapCmpl(connId);
/* start configuration */
AppDiscConfigure(connId, APP_DISC_CFG_START, BLE_DISC_CFG_LIST_LEN,
(attcDiscCfg_t *) bleDiscCfgList, BLE_DISC_HDL_LIST_LEN, s_hdlList);
}
break;
case APP_DISC_CFG_START:
/* start configuration */
AppDiscConfigure(connId, APP_DISC_CFG_START, BLE_DISC_CFG_LIST_LEN,
(attcDiscCfg_t *) bleDiscCfgList, BLE_DISC_HDL_LIST_LEN, s_hdlList);
break;
case APP_DISC_CFG_CMPL:
AppDiscComplete(connId, APP_DISC_CFG_CMPL);
ble_epic_disc_cfg_complete();
break;
case APP_DISC_CFG_CONN_START:
/* no connection setup configuration for this application */
ble_epic_disc_cfg_complete();
break;
default:
break;
}
}
/* clang-format on */
......@@ -17,64 +17,42 @@
#include <string.h>
#include "wsf_types.h"
#include "util/bstream.h"
#include "fs_util.h"
#include "wsf_msg.h"
#include "wsf_trace.h"
#include "hci_api.h"
#include "l2c_api.h"
#include "dm_api.h"
#include "att_api.h"
#include "gatt/gatt_api.h"
#include "smp_api.h"
#include "app_api.h"
#include "app_db.h"
#include "app_ui.h"
#include "app_hw.h"
#include "svc_ch.h"
#include "svc_core.h"
#include "svc_hrs.h"
#include "svc_dis.h"
#include "svc_batt.h"
#include "svc_rscs.h"
#include "bas/bas_api.h"
#include "hrps/hrps_api.h"
#include "rscp/rscp_api.h"
#include "svc_hid.h"
#include "profiles/gap_api.h"
#include "cccd.h"
#include "ess.h"
#include "hid.h"
#include "uart.h"
/**************************************************************************************************
Macros
**************************************************************************************************/
/*! WSF message event starting value */
#define BLE_MSG_START 0xA0
#include "ble_api.h"
#include "epicardium.h"
#include "os/core.h"
#include "os/config.h"
/*! WSF message event enumeration */
enum
{
BLE_BATT_TIMER_IND = BLE_MSG_START, /*! Battery measurement timer expired */
};
#define SCAN_REPORTS_NUM 16
/**************************************************************************************************
Data Types
**************************************************************************************************/
/*! Application message type */
typedef union
{
wsfMsgHdr_t hdr;
dmEvt_t dm;
attsCccEvt_t ccc;
attEvt_t att;
} bleMsg_t;
static bool active;
static struct epic_scan_report scan_reports[SCAN_REPORTS_NUM];
static int scan_reports_head;
static int scan_reports_tail;
/**************************************************************************************************
Configurable Parameters
**************************************************************************************************/
/*! configurable parameters for advertising */
static const appAdvCfg_t bleAdvCfg =
{
{0, 0, 0}, /*! Advertising durations in ms */
{500/0.625, 4000/0.625, 0} /*! Advertising intervals in 0.625 ms units */
};
/*! configurable parameters for slave */
static const appSlaveCfg_t bleSlaveCfg =
{
......@@ -84,11 +62,11 @@ static const appSlaveCfg_t bleSlaveCfg =
/*! configurable parameters for security */
static const appSecCfg_t bleSecCfg =
{
DM_AUTH_BOND_FLAG | DM_AUTH_SC_FLAG, /*! Authentication and bonding flags */
0, /*! Initiator key distribution flags */
DM_KEY_DIST_LTK, /*! Responder key distribution flags */
FALSE, /*! TRUE if Out-of-band pairing data is present */
TRUE /*! TRUE to initiate security upon connection */
.auth = DM_AUTH_MITM_FLAG | DM_AUTH_BOND_FLAG | DM_AUTH_SC_FLAG, /*! Authentication and bonding flags */
.iKeyDist = 0, /*! Initiator key distribution flags */
.rKeyDist = DM_KEY_DIST_LTK, /*! Responder key distribution flags */
.oob=FALSE, /*! TRUE if Out-of-band pairing data is present */
.initiateSec = TRUE /*! TRUE to initiate security upon connection */
};
/*! configurable parameters for connection parameter update */
......@@ -96,30 +74,32 @@ static const appUpdateCfg_t bleUpdateCfg =
{
6000, /*! Connection idle period in ms before attempting
connection parameter update; set to zero to disable */
800/1.25, /*! Minimum connection interval in 1.25ms units */
1000/1.25, /*! Maximum connection interval in 1.25ms units */
30/1.25, /*! Minimum connection interval in 1.25ms units.
Values < 8 didn't work with my Tinkpad T470 */
40/1.25, /*! Maximum connection interval in 1.25ms units */
0, /*! Connection latency */
9000/10, /*! Supervision timeout in 10ms units */
5 /*! Number of update attempts before giving up */
};
/*! battery measurement configuration */
static const basCfg_t bleBasCfg =
/*! SMP security parameter configuration */
static const smpCfg_t bleSmpCfg =
{
30, /*! Battery measurement timer expiration period in seconds */
1, /*! Perform battery measurement after this many timer periods */
100 /*! Send battery level notification to peer when below this level. */
.attemptTimeout = 3000, /*! 'Repeated attempts' timeout in msec */
.ioCap = SMP_IO_DISP_YES_NO, /*! I/O Capability */
.minKeyLen = 16, /*! Minimum encryption key length */
.maxKeyLen = 16, /*! Maximum encryption key length */
.maxAttempts = 3, /*! Attempts to trigger 'repeated attempts' timeout */
.auth = DM_AUTH_MITM_FLAG | DM_AUTH_SC_FLAG, /*! Device authentication requirements */
.maxAttemptTimeout = 64000, /*! Maximum 'Repeated attempts' timeout in msec */
.attemptDecTimeout = 64000, /*! Time msec before attemptExp decreases */
.attemptExp = 2, /*! Exponent to raise attemptTimeout on maxAttempts */
};
/*! SMP security parameter configuration */
static const smpCfg_t bleSmpCfg =
/*! Configurable parameters for service and characteristic discovery */
static const appDiscCfg_t bleDiscCfg =
{
3000, /*! 'Repeated attempts' timeout in msec */
SMP_IO_DISP_YES_NO, /*! I/O Capability */
7, /*! Minimum encryption key length */
16, /*! Maximum encryption key length */
3, /*! Attempts to trigger 'repeated attempts' timeout */
DM_AUTH_MITM_FLAG, /*! Device authentication requirements */
FALSE /*! TRUE to wait for a secure connection before initiating discovery */
};
/* Configuration structure */
......@@ -131,58 +111,36 @@ static const attCfg_t bleAttCfg =
1 /* number of queued prepare writes supported by server */
};
/**************************************************************************************************
Advertising Data
**************************************************************************************************/
/*! advertising data, discoverable mode */
static const uint8_t bleAdvDataDisc[] =
{
/*! flags */
2, /*! length */
DM_ADV_TYPE_FLAGS, /*! AD type */
DM_FLAG_LE_GENERAL_DISC | /*! flags */
DM_FLAG_LE_BREDR_NOT_SUP,
/*! tx power */
2, /*! length */
DM_ADV_TYPE_TX_POWER, /*! AD type */
0, /*! tx power */
/*! service UUID list */
5, /*! length */
DM_ADV_TYPE_16_UUID, /*! AD type */
UINT16_TO_BYTES(ATT_UUID_DEVICE_INFO_SERVICE),
UINT16_TO_BYTES(ATT_UUID_BATTERY_SERVICE)
};
/*! scan data, discoverable mode */
uint8_t bleScanDataDisc[] =
static const appMasterCfg_t scannerMasterCfg =
{
/*! device name */
14, /*! length */
DM_ADV_TYPE_LOCAL_NAME, /*! AD type */
'c','a','r','d','1','0','-','0','0','0','0','0','0'
420, /*! The scan interval, in 0.625 ms units */
420, /*! The scan window, in 0.625 ms units */
0, /*! The scan duration in ms */
DM_DISC_MODE_NONE, /*! The GAP discovery mode */
DM_SCAN_TYPE_PASSIVE
/*!< The scan type (active or passive) */
};
/**************************************************************************************************
Client Characteristic Configuration Descriptors
**************************************************************************************************/
/*! enumeration of client characteristic configuration descriptors */
enum
{
BLE_GATT_SC_CCC_IDX, /*! GATT service, service changed characteristic */
BLE_BATT_LVL_CCC_IDX, /*! Battery service, battery level characteristic */
BLE_NUM_CCC_IDX
};
/*! client characteristic configuration descriptors settings, indexed by above enumeration */
static const attsCccSet_t bleCccSet[BLE_NUM_CCC_IDX] =
{
/* cccd handle value range security level */
{GATT_SC_CH_CCC_HDL, ATT_CLIENT_CFG_INDICATE, DM_SEC_LEVEL_NONE}, /* BLE_GATT_SC_CCC_IDX */
{BATT_LVL_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* BLE_BATT_LVL_CCC_IDX */
{ESS_TEMP_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* BLE_ESS_TEMP_CCC_IDX */
{ESS_HUMI_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* BLE_ESS_HUMI_CCC_IDX */
{ESS_PRES_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* BLE_ESS_PRES_CCC_IDX */
{HID_MOUSE_BOOT_IN_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* HIDAPP_MBI_CCC_HDL */
{HID_KEYBOARD_BOOT_IN_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* HIDAPP_KBI_CCC_HDL */
{HID_INPUT_REPORT_1_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* HIDAPP_IN_KEYBOARD_CCC_HDL */
{HID_INPUT_REPORT_2_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* HIDAPP_IN_MOUSE_CCC_HDL */
{HID_INPUT_REPORT_3_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* HIDAPP_IN_CONSUMER_CCC_HDL */
{ESS_IAQ_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* BLE_ESS_IAQ_CCC_IDX */
{UART_TX_CH_CCC_HDL, ATT_CLIENT_CFG_NOTIFY, DM_SEC_LEVEL_NONE}, /* BLE_ESS_IAQ_CCC_IDX */
};
/**************************************************************************************************
......@@ -192,8 +150,114 @@ static const attsCccSet_t bleCccSet[BLE_NUM_CCC_IDX] =
/*! WSF handler ID */
wsfHandlerId_t bleHandlerId;
static dmConnId_t pair_connId = DM_CONN_ID_NONE;
static uint32_t pair_confirm_value;
static appDbHdl_t last_pairing = NULL;
static void BleHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg);
static const char * const att_events[] = {
"ATTC_FIND_INFO_RSP",
"ATTC_FIND_BY_TYPE_VALUE_RSP",
"ATTC_READ_BY_TYPE_RSP",
"ATTC_READ_RSP",
"ATTC_READ_LONG_RSP",
"ATTC_READ_MULTIPLE_RSP",
"ATTC_READ_BY_GROUP_TYPE_RSP",
"ATTC_WRITE_RSP",
"ATTC_WRITE_CMD_RSP",
"ATTC_PREPARE_WRITE_RSP",
"ATTC_EXECUTE_WRITE_RSP",
"ATTC_HANDLE_VALUE_NTF",
"ATTC_HANDLE_VALUE_IND",
/* ATT server callback events */
"ATTS_HANDLE_VALUE_CNF",
"ATTS_CCC_STATE_IND",
"ATTS_DB_HASH_CALC_CMPL_IND",
/* ATT common callback events */
"ATT_MTU_UPDATE_IND"
};
static const char * const dm_events[] = {
"DM_RESET_CMPL_IND",
"DM_ADV_START_IND",
"DM_ADV_STOP_IND",
"DM_ADV_NEW_ADDR_IND",
"DM_SCAN_START_IND",
"DM_SCAN_STOP_IND",
"DM_SCAN_REPORT_IND",
"DM_CONN_OPEN_IND",
"DM_CONN_CLOSE_IND",
"DM_CONN_UPDATE_IND",
"DM_SEC_PAIR_CMPL_IND",
"DM_SEC_PAIR_FAIL_IND",
"DM_SEC_ENCRYPT_IND",
"DM_SEC_ENCRYPT_FAIL_IND",
"DM_SEC_AUTH_REQ_IND",
"DM_SEC_KEY_IND",
"DM_SEC_LTK_REQ_IND",
"DM_SEC_PAIR_IND",
"DM_SEC_SLAVE_REQ_IND",
"DM_SEC_CALC_OOB_IND",
"DM_SEC_ECC_KEY_IND",
"DM_SEC_COMPARE_IND",
"DM_SEC_KEYPRESS_IND",
"DM_PRIV_RESOLVED_ADDR_IND",
"DM_PRIV_GENERATE_ADDR_IND",
"DM_CONN_READ_RSSI_IND",
"DM_PRIV_ADD_DEV_TO_RES_LIST_IND",
"DM_PRIV_REM_DEV_FROM_RES_LIST_IND",
"DM_PRIV_CLEAR_RES_LIST_IND",
"DM_PRIV_READ_PEER_RES_ADDR_IND",
"DM_PRIV_READ_LOCAL_RES_ADDR_IND",
"DM_PRIV_SET_ADDR_RES_ENABLE_IND",
"DM_REM_CONN_PARAM_REQ_IND",
"DM_CONN_DATA_LEN_CHANGE_IND",
"DM_CONN_WRITE_AUTH_TO_IND",
"DM_CONN_AUTH_TO_EXPIRED_IND",
"DM_PHY_READ_IND",
"DM_PHY_SET_DEF_IND",
"DM_PHY_UPDATE_IND",
"DM_ADV_SET_START_IND",
"DM_ADV_SET_STOP_IND",
"DM_SCAN_REQ_RCVD_IND",
"DM_EXT_SCAN_START_IND",
"DM_EXT_SCAN_STOP_IND",
"DM_EXT_SCAN_REPORT_IND",
"DM_PER_ADV_SET_START_IND",
"DM_PER_ADV_SET_STOP_IND",
"DM_PER_ADV_SYNC_EST_IND",
"DM_PER_ADV_SYNC_EST_FAIL_IND",
"DM_PER_ADV_SYNC_LOST_IND",
"DM_PER_ADV_SYNC_TRSF_EST_IND",
"DM_PER_ADV_SYNC_TRSF_EST_FAIL_IND",
"DM_PER_ADV_SYNC_TRSF_IND",
"DM_PER_ADV_SET_INFO_TRSF_IND",
"DM_PER_ADV_REPORT_IND",
"DM_REMOTE_FEATURES_IND",
"DM_READ_REMOTE_VER_INFO_IND",
"DM_CONN_IQ_REPORT_IND",
"DM_CTE_REQ_FAIL_IND",
"DM_CONN_CTE_RX_SAMPLE_START_IND",
"DM_CONN_CTE_RX_SAMPLE_STOP_IND",
"DM_CONN_CTE_TX_CFG_IND",
"DM_CONN_CTE_REQ_START_IND",
"DM_CONN_CTE_REQ_STOP_IND",
"DM_CONN_CTE_RSP_START_IND",
"DM_CONN_CTE_RSP_STOP_IND",
"DM_READ_ANTENNA_INFO_IND",
"DM_L2C_CMD_REJ_IND",
"DM_ERROR_IND",
"DM_HW_ERROR_IND",
"DM_VENDOR_SPEC_IND"
};
static const char * const l2c_coc_events[] = {
"L2C_COC_CONNECT_IND",
"L2C_COC_DISCONNECT_IND",
"L2C_COC_DATA_IND",
"L2C_COC_DATA_CNF"
};
/*************************************************************************************************/
/*!
* \brief Application DM callback.
......@@ -259,6 +323,7 @@ static void bleCccCback(attsCccEvt_t *pEvt)
{
/* store value in device database */
AppDbSetCccTblValue(dbHdl, pEvt->idx, pEvt->value);
AppDbNvmStoreCccTbl(dbHdl);
}
if ((pMsg = WsfMsgAlloc(sizeof(attsCccEvt_t))) != NULL)
......@@ -268,8 +333,6 @@ static void bleCccCback(attsCccEvt_t *pEvt)
}
}
/*************************************************************************************************/
/*!
* \brief Process CCC state change.
......@@ -284,18 +347,14 @@ static void bleProcCccState(bleMsg_t *pMsg)
APP_TRACE_INFO3("ccc state ind value:%d handle:%d idx:%d", pMsg->ccc.value, pMsg->ccc.handle, pMsg->ccc.idx);
/* handle battery level CCC */
if (pMsg->ccc.idx == BLE_BATT_LVL_CCC_IDX)
{
if (pMsg->ccc.value == ATT_CLIENT_CFG_NOTIFY)
{
BasMeasBattStart((dmConnId_t) pMsg->ccc.hdr.param, BLE_BATT_TIMER_IND, BLE_BATT_LVL_CCC_IDX);
}
else
{
BasMeasBattStop((dmConnId_t) pMsg->ccc.hdr.param);
}
return;
}
switch(pMsg->ccc.idx) {
case BLE_ESS_TEMP_CCC_IDX:
case BLE_ESS_HUMI_CCC_IDX:
case BLE_ESS_PRES_CCC_IDX:
case BLE_ESS_IAQ_CCC_IDX:
bleESS_ccc_update();
break;
};
}
/*************************************************************************************************/
......@@ -310,7 +369,8 @@ static void bleProcCccState(bleMsg_t *pMsg)
static void bleClose(bleMsg_t *pMsg)
{
/* stop battery measurement */
BasMeasBattStop((dmConnId_t) pMsg->hdr.param);
bleESS_ccc_update();
GapClearDeviceName();
}
/*************************************************************************************************/
......@@ -325,41 +385,143 @@ static void bleClose(bleMsg_t *pMsg)
/*************************************************************************************************/
static void bleSetup(bleMsg_t *pMsg)
{
char buf[32];
char a, b, c, d, e, f, K;
ble_adv_setup();
if (fs_read_text_file("mac.txt", buf, sizeof(buf)))
{
if (sscanf(buf, "%c%c:%c%c:%c%c:%c%c:%c%c:%c%c", &K,&K,&K,&K,&K,&K, &a, &b, &c, &d, &e, &f) == 12)
active = true;
/* TODO: Sadly, not advertising leads to a higher current consumption... */
epic_ble_set_mode(false, false);
}
void epic_ble_set_mode(bool bondable, bool scanner)
{
bleScanDataDisc[9] = a;
bleScanDataDisc[10] = b;
bleScanDataDisc[11] = c;
bleScanDataDisc[12] = d;
bleScanDataDisc[13] = e;
bleScanDataDisc[14] = f;
if(!active) {
return;
}
if(scanner && bondable) {
/* TODO: return error */
return;
}
if(scanner) {
ble_adv_stop();
dmConnId_t connId;
if ((connId = AppConnIsOpen()) != DM_CONN_ID_NONE) {
AppConnClose(connId);
}
/* set advertising and scan response data for discoverable mode */
AppAdvSetData(APP_ADV_DATA_DISCOVERABLE, sizeof(bleAdvDataDisc), (uint8_t *) bleAdvDataDisc);
AppAdvSetData(APP_SCAN_DATA_DISCOVERABLE, sizeof(bleScanDataDisc), (uint8_t *) bleScanDataDisc);
/* Normal scanning filters out duplicates. We don't
* want that for now... */
//AppScanStart(scannerMasterCfg.discMode, scannerMasterCfg.scanType, scannerMasterCfg.scanDuration);
DmScanSetInterval(HCI_SCAN_PHY_LE_1M_BIT, &pAppMasterCfg->scanInterval,
&pAppMasterCfg->scanWindow);
DmScanStart(HCI_SCAN_PHY_LE_1M_BIT, scannerMasterCfg.discMode,
&scannerMasterCfg.scanType, FALSE, scannerMasterCfg.scanDuration, 0);
/* set advertising and scan response data for connectable mode */
AppAdvSetData(APP_ADV_DATA_CONNECTABLE, 0, NULL);
AppAdvSetData(APP_SCAN_DATA_CONNECTABLE, 0, NULL);
return;
} else {
AppScanStop();
}
#if 0
/* TODO: card10: until we have an BLE dialog, be discoverable and bondable always */
/* start advertising; automatically set connectable/discoverable mode and bondable mode */
AppAdvStart(APP_MODE_AUTO_INIT);
#else
/* enter discoverable and bondable mode mode by default */
if(bondable) {
AppSetBondable(TRUE);
AppAdvStart(APP_MODE_DISCOVERABLE);
#endif
ble_adv_discoverable(true);
} else {
AppSetBondable(FALSE);
ble_adv_discoverable(false);
}
}
uint32_t epic_ble_get_compare_value(void)
{
return pair_confirm_value;
}
int epic_ble_get_peer_device_name(char *buf, size_t buf_size)
{
if (AppConnIsOpen() != DM_CONN_ID_NONE) {
return GapGetDeviceName(buf, buf_size);
} else {
return -ENOENT;
}
}
int epic_ble_get_last_pairing_name(char *buf, size_t buf_size)
{
if(last_pairing == NULL) {
return -ENOENT;
}
return AppDbGetPairingName(last_pairing, buf, buf_size);
}
void epic_ble_compare_response(bool confirmed)
{
if(!active) {
return;
}
if(pair_connId != DM_CONN_ID_NONE) {
LOG_INFO("ble", "Value confirmed: %u", confirmed);
DmSecCompareRsp(pair_connId, confirmed);
} else {
/* error condition */
}
}
static void bleHandleNumericComparison(dmSecCnfIndEvt_t *pCnfInd)
{
if(!active) {
return;
}
pair_connId = (dmConnId_t)pCnfInd->hdr.param;
pair_confirm_value = DmSecGetCompareValue(pCnfInd->confirm);
LOG_INFO("ble", "Confirm Value: %ld", pair_confirm_value);
ble_epic_ble_api_trigger_event(BLE_EVENT_HANDLE_NUMERIC_COMPARISON, NULL);
}
int epic_ble_get_scan_report(struct epic_scan_report *rpt)
{
if(scan_reports_head == scan_reports_tail) {
return -ENOENT;
}
int new_tail = (scan_reports_tail + 1) % SCAN_REPORTS_NUM;
*rpt = scan_reports[new_tail];
scan_reports_tail = new_tail;
return 0;
}
static void scannerScanReport(dmEvt_t *pMsg)
{
struct epic_scan_report *scan_report;
int next_head = (scan_reports_head + 1) % SCAN_REPORTS_NUM;
if(next_head == scan_reports_tail) {
ble_epic_ble_api_trigger_event(BLE_EVENT_SCAN_REPORT, NULL);
return;
}
scan_reports_head = next_head;
scan_report = &scan_reports[scan_reports_head];
memset(scan_report->data, 0, sizeof(scan_report->data));
memccpy(scan_report->data, pMsg->scanReport.pData, pMsg->scanReport.len, sizeof(scan_report->data));
scan_report->len = pMsg->scanReport.len;
scan_report->rssi = pMsg->scanReport.rssi;
scan_report->eventType = pMsg->scanReport.eventType;
scan_report->addrType = pMsg->scanReport.addrType;
memcpy(scan_report->addr, pMsg->scanReport.addr, BDA_ADDR_LEN);
scan_report->directAddrType = pMsg->scanReport.directAddrType;
memcpy(scan_report->directAddr, pMsg->scanReport.directAddr, BDA_ADDR_LEN);
ble_epic_ble_api_trigger_event(BLE_EVENT_SCAN_REPORT, NULL);
if((scan_reports_head + 1) % SCAN_REPORTS_NUM == scan_reports_tail) {
LOG_WARN("ble", "Application missing scan results");
}
}
/*************************************************************************************************/
/*!
......@@ -372,16 +534,18 @@ static void bleSetup(bleMsg_t *pMsg)
/*************************************************************************************************/
static void bleProcMsg(bleMsg_t *pMsg)
{
uint8_t uiEvent = APP_UI_NONE;
hciLeConnCmplEvt_t *connOpen;
switch(pMsg->hdr.event)
{
case BLE_BATT_TIMER_IND:
BasProcMsg(&pMsg->hdr);
case ATTC_READ_RSP:
case ATTC_HANDLE_VALUE_IND:
bleValueUpdate((attEvt_t *) pMsg);
break;
case ATTS_HANDLE_VALUE_CNF:
BasProcMsg(&pMsg->hdr);
HidProcMsg(&pMsg->hdr);
UartProcMsg(pMsg);
break;
case ATTS_CCC_STATE_IND:
......@@ -391,41 +555,102 @@ static void bleProcMsg(bleMsg_t *pMsg)
case DM_RESET_CMPL_IND:
DmSecGenerateEccKeyReq();
bleSetup(pMsg);
uiEvent = APP_UI_RESET_CMPL;
break;
case DM_ADV_START_IND:
uiEvent = APP_UI_ADV_START;
ble_adv_proc_msg(pMsg);
break;
case DM_ADV_STOP_IND:
uiEvent = APP_UI_ADV_STOP;
ble_adv_proc_msg(pMsg);
break;
case DM_CONN_OPEN_IND:
BasProcMsg(&pMsg->hdr);
uiEvent = APP_UI_CONN_OPEN;
connOpen = &pMsg->dm.connOpen;
LOG_INFO("ble", "connection from %02X:%02X:%02X:%02X:%02X:%02X opened",
connOpen->peerAddr[5], connOpen->peerAddr[4],
connOpen->peerAddr[3], connOpen->peerAddr[2],
connOpen->peerAddr[1], connOpen->peerAddr[0]);
bleESS_ccc_update();
HidProcMsg(&pMsg->hdr);
UartProcMsg(pMsg);
break;
case DM_CONN_CLOSE_IND:
switch (pMsg->dm.connClose.reason)
{
case HCI_ERR_CONN_TIMEOUT:
LOG_INFO("ble", "Connection closed (0x%02X), Connection timeout",
pMsg->dm.connClose.reason);
break;
case HCI_ERR_LOCAL_TERMINATED:
LOG_INFO("ble", "Connection closed (0x%02X), Connection terminated by local host",
pMsg->dm.connClose.reason);
break;
case HCI_ERR_REMOTE_TERMINATED:
LOG_INFO("ble", "Connection closed (0x%02X), Remote user terminated connection",
pMsg->dm.connClose.reason);
break;
case HCI_ERR_CONN_FAIL:
LOG_INFO("ble", "Connection closed (0x%02X), Connection failed to be established",
pMsg->dm.connClose.reason);
break;
case HCI_ERR_MIC_FAILURE:
LOG_INFO("ble", "Connection closed (0x%02X), Connection terminated due to MIC failure",
pMsg->dm.connClose.reason);
break;
default:
LOG_INFO("ble", "Connection closed (0x%02X)",
pMsg->dm.connClose.reason);
break;
}
ble_adv_proc_msg(pMsg);
bleClose(pMsg);
uiEvent = APP_UI_CONN_CLOSE;
break;
case DM_SEC_PAIR_CMPL_IND:
uiEvent = APP_UI_SEC_PAIR_CMPL;
LOG_INFO("ble", "Secure pairing successful, auth: 0x%02X",
pMsg->dm.pairCmpl.auth);
DmSecGenerateEccKeyReq();
last_pairing = AppDbGetHdl((dmConnId_t) pMsg->hdr.param);
AppDbNvmStoreBond(last_pairing);
pair_connId = DM_CONN_ID_NONE;
ble_epic_ble_api_trigger_event(BLE_EVENT_PAIRING_COMPLETE, NULL);
/* After a successful pairing, bonding is disabled again.
* We don't want that for now. */
AppSetBondable(TRUE);
break;
case DM_SEC_PAIR_FAIL_IND:
uiEvent = APP_UI_SEC_PAIR_FAIL;
switch (pMsg->hdr.status) {
case SMP_ERR_TIMEOUT:
LOG_INFO("ble", "Secure pairing failed (0x%02X), Transaction timeout",
pMsg->hdr.status);
break;
case SMP_ERR_ATTEMPTS:
LOG_INFO("ble", "Secure pairing failed (0x%02X), Repeated attempts",
pMsg->hdr.status);
break;
default:
LOG_INFO("ble", "Secure pairing failed (0x%02X)",
pMsg->hdr.status);
break;
}
DmSecGenerateEccKeyReq();
pair_connId = DM_CONN_ID_NONE;
ble_epic_ble_api_trigger_event(BLE_EVENT_PAIRING_FAILED, NULL);
break;
case DM_SEC_ENCRYPT_IND:
uiEvent = APP_UI_SEC_ENCRYPT;
LOG_INFO("ble", "Encrypted handshake successful");
break;
case DM_SEC_ENCRYPT_FAIL_IND:
uiEvent = APP_UI_SEC_ENCRYPT_FAIL;
LOG_INFO("ble", "Encrypted handshake failed");
break;
case DM_SEC_AUTH_REQ_IND:
......@@ -437,21 +662,20 @@ static void bleProcMsg(bleMsg_t *pMsg)
break;
case DM_SEC_COMPARE_IND:
AppHandleNumericComparison(&pMsg->dm.cnfInd);
bleHandleNumericComparison(&pMsg->dm.cnfInd);
break;
case DM_SCAN_REPORT_IND:
scannerScanReport((dmEvt_t *)pMsg);
break;
case DM_HW_ERROR_IND:
uiEvent = APP_UI_HW_ERROR;
LOG_ERR("ble", "HW Error");
break;
default:
break;
}
if (uiEvent != APP_UI_NONE)
{
AppUiAction(uiEvent);
}
}
/*************************************************************************************************/
......@@ -471,20 +695,21 @@ static void BleHandlerInit(void)
bleHandlerId =WsfOsSetNextHandler(BleHandler);
/* Set configuration pointers */
pAppAdvCfg = (appAdvCfg_t *) &bleAdvCfg;
pAppSlaveCfg = (appSlaveCfg_t *) &bleSlaveCfg;
pAppSecCfg = (appSecCfg_t *) &bleSecCfg;
pAppUpdateCfg = (appUpdateCfg_t *) &bleUpdateCfg;
pAppDiscCfg = (appDiscCfg_t *) &bleDiscCfg;
pAppMasterCfg = (appMasterCfg_t *) &scannerMasterCfg;
ble_adv_init();
/* Initialize application framework */
AppSlaveInit();
AppDiscInit();
/* Set stack configuration pointers */
pSmpCfg = (smpCfg_t *) &bleSmpCfg;
pAttCfg = (attCfg_t *) &bleAttCfg;
/* initialize battery service server */
BasInit(bleHandlerId, (basCfg_t *) &bleBasCfg);
}
/*************************************************************************************************/
......@@ -505,11 +730,37 @@ static void BleHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg)
if (pMsg->event >= DM_CBACK_START && pMsg->event <= DM_CBACK_END)
{
if(pMsg->event != DM_SCAN_REPORT_IND) LOG_INFO("ble", "Ble got evt %d: %s", pMsg->event, dm_events[pMsg->event - DM_CBACK_START]);
/* process advertising and connection-related messages */
AppSlaveProcDmMsg((dmEvt_t *) pMsg);
/* process security-related messages */
AppSlaveSecProcDmMsg((dmEvt_t *) pMsg);
/* Needs to be before AppDiscProcDmMsg, to get DM_CONN_OPEN_IND first */
ble_epic_dm_api_event((dmEvt_t *)pMsg);
/* process discovery-related messages */
AppDiscProcDmMsg((dmEvt_t *) pMsg);
}
else if (pMsg->event >= ATT_CBACK_START && pMsg->event <= ATT_CBACK_END)
{
/* Don't spam the console with successful notfication/indications */
if (!(pMsg->event == ATTS_HANDLE_VALUE_CNF && pMsg->status == ATT_SUCCESS)) {
LOG_INFO("ble", "Ble got evt %d (%s): %d %d", pMsg->event, att_events[pMsg->event - ATT_CBACK_START], ((bleMsg_t *)pMsg)->att.handle, pMsg->status);
}
/* process discovery-related ATT messages */
AppDiscProcAttMsg((attEvt_t *) pMsg);
ble_epic_att_api_event((attEvt_t *)pMsg);
}
else if (pMsg->event >= L2C_COC_CBACK_START && pMsg->event <= L2C_COC_CBACK_CBACK_END)
{
LOG_INFO("ble", "Ble got evt %d: %s", pMsg->event, l2c_coc_events[pMsg->event - L2C_COC_CBACK_START]);
}
else
{
LOG_INFO("ble", "Ble got evt %d", pMsg->event);
}
/* perform profile and user interface-related operations */
......@@ -526,7 +777,6 @@ static void BleHandler(wsfEventMask_t event, wsfMsgHdr_t *pMsg)
/*************************************************************************************************/
void BleStart(void)
{
BleHandlerInit();
/* Register for stack callbacks */
......@@ -536,13 +786,24 @@ void BleStart(void)
AttConnRegister(AppServerConnCback);
AttsCccRegister(BLE_NUM_CCC_IDX, (attsCccSet_t *) bleCccSet, bleCccCback);
/* Register for app framework discovery callbacks */
AppDiscRegister(bleDiscCback);
/* Initialize attribute server database */
SvcCoreAddGroup();
SvcDisAddGroup(); // Device Information Service
SvcBattCbackRegister(BasReadCback, NULL);
SvcBattAddGroup();
if(config_get_boolean_with_default("ble_hid_enable", false)) {
hid_init();
}
ble_epic_ble_api_init();
/* Set Service Changed CCCD index. */
GattSetSvcChangedIdx(BLE_GATT_SC_CCC_IDX);
/* Reset the device */
DmDevReset();
}
/* clang-format on */
/*************************************************************************************************/
/*!
* \file
*
* \brief Application framework device database example, using simple RAM-based storage.
*
* Copyright (c) 2011-2018 Arm Ltd. All Rights Reserved.
* ARM Ltd. confidential and proprietary.
*
* IMPORTANT. Your use of this file is governed by a Software License Agreement
* ("Agreement") that must be accepted in order to download or otherwise receive a
* copy of this file. You may not use or copy this file for any purpose other than
* as described in the Agreement. If you do not agree to all of the terms of the
* Agreement do not use this file and delete all copies in your possession or control;
* if you do not have a copy of the Agreement, you must contact ARM Ltd. prior
* to any use, copying or further distribution of this software.
*/
/*************************************************************************************************/
/* card10:
* copied from: lib/sdk/Libraries/BTLE/stack/ble-profiles/sources/apps/app/common/app_db.c
*
* Reason: we need to implement persistent storage for bondings
*/
/* clang-format off */
/* clang-formet turned off for easier diffing against orginal file */
#include "wsf_types.h"
#include "wsf_assert.h"
#include "util/bda.h"
#include "app_api.h"
#include "app_main.h"
#include "app_db.h"
#include "app_cfg.h"
#include "epicardium.h"
#include "os/core.h"
#include "FreeRTOS.h"
#include "timers.h"
#include "mxc_sys.h"
#include "wdt.h"
#include <string.h>
#include <stdio.h>
/**************************************************************************************************
Macros
**************************************************************************************************/
/* App DB NVM record parameter indicies from upstream:
* https://github.com/packetcraft-inc/stacks/blob/master/ble-profiles/sources/af/common/app_db.c#L46
*/
#define APP_DB_NVM_IN_USE_ID 0
#define APP_DB_NVM_PEER_ADDR_ID 1
#define APP_DB_NVM_ADDR_TYPE_ID 2
#define APP_DB_NVM_PEER_IRK_ID 3
#define APP_DB_NVM_PEER_CSRK_ID 4
#define APP_DB_NVM_KV_MASK_ID 5
#define APP_DB_NVM_VALID_ID 6
#define APP_DB_NVM_PEER_RAPO_ID 7
#define APP_DB_NVM_LOCAL_LTK_ID 8
#define APP_DB_NVM_LOCAL_SEC_LVL_ID 9
#define APP_DB_NVM_PEER_ADDR_RES_ID 10
#define APP_DB_NVM_PEER_LTK_ID 11
#define APP_DB_NVM_PEER_SEC_LVL_ID 12
#define APP_DB_NVM_CCC_TBL_ID 13
#define APP_DB_NVM_PEER_SIGN_CTR_ID 14
#define APP_DB_NVM_CAS_ID 15
#define APP_DB_NVM_CSF_ID 16
#define APP_DB_NVM_CACHE_HASH_ID 17
#define APP_DB_NVM_HASH_ID 18
#define APP_DB_NVM_HDL_LIST_ID 19
#define APP_DB_NVM_DISC_STATUS_ID 20
#define APP_DB_NVM_SEQUENCE_NUMBER_ID 100
/**************************************************************************************************
Data Types
**************************************************************************************************/
/*! Database record */
typedef struct
{
/*! Common for all roles */
bdAddr_t peerAddr; /*! Peer address */
uint8_t addrType; /*! Peer address type */
dmSecIrk_t peerIrk; /*! Peer IRK */
dmSecCsrk_t peerCsrk; /*! Peer CSRK */
uint8_t keyValidMask; /*! Valid keys in this record */
bool_t inUse; /*! TRUE if record in use */
bool_t valid; /*! TRUE if record is valid */
bool_t peerAddedToRl; /*! TRUE if peer device's been added to resolving list */
bool_t peerRpao; /*! TRUE if RPA Only attribute's present on peer device */
/*! For slave local device */
dmSecLtk_t localLtk; /*! Local LTK */
uint8_t localLtkSecLevel; /*! Local LTK security level */
bool_t peerAddrRes; /*! TRUE if address resolution's supported on peer device (master) */
/*! For master local device */
dmSecLtk_t peerLtk; /*! Peer LTK */
uint8_t peerLtkSecLevel; /*! Peer LTK security level */
/*! for ATT server local device */
uint16_t cccTbl[APP_DB_NUM_CCCD]; /*! Client characteristic configuration descriptors */
uint32_t peerSignCounter; /*! Peer Sign Counter */
/*! for ATT client */
uint16_t hdlList[APP_DB_HDL_LIST_LEN]; /*! Cached handle list */
uint8_t discStatus; /*! Service discovery and configuration status */
uint32_t sequenceNumber;
} appDbRec_t;
/**************************************************************************************************
Local Variables
**************************************************************************************************/
/*! Database */
static appDbRec_t records[APP_DB_NUM_RECS];
/* clang-format on */
/* Translate a pointer to a record into the filename to be used for it. */
static int record_to_filename(appDbRec_t *record, char *buf, size_t buf_size)
{
int id = record - records;
int ret = snprintf(buf, buf_size, "pairings/pairing%d.bin", id + 1);
if (ret >= (int)buf_size) {
ret = -1;
}
return ret;
}
static appDbRec_t *record_with_highest_seq_number()
{
appDbRec_t *r = &records[0];
for (int i = 0; i < APP_DB_NUM_RECS; i++) {
if (records[i].sequenceNumber > r->sequenceNumber) {
r = &records[i];
}
}
return r;
}
static appDbRec_t *record_with_lowest_seq_number()
{
appDbRec_t *r = &records[0];
for (int i = 0; i < APP_DB_NUM_RECS; i++) {
if (records[i].sequenceNumber < r->sequenceNumber) {
r = &records[i];
}
}
return r;
}
/* Write a TLV to a file. */
static int write_tlv(int fd, uint32_t t, uint32_t l, void *v)
{
int ret;
ret = epic_file_write(fd, &t, sizeof(t));
if (ret != sizeof(t))
return ret;
ret = epic_file_write(fd, &l, sizeof(l));
if (ret != sizeof(l))
return ret;
ret = epic_file_write(fd, v, l);
if (ret != (int)l)
return ret;
return 0;
}
/* Read a TLV from a file.
*
* Super naive implementation assuming that the next TLV is
* the expected one. */
static int read_tlv(int fd, uint32_t t, uint32_t l, void *v)
{
int ret;
uint32_t t_r;
ret = epic_file_read(fd, &t_r, sizeof(t_r));
if (ret != sizeof(t))
return ret;
if (t != t_r)
return -ENOENT;
uint32_t l_r;
ret = epic_file_read(fd, &l_r, sizeof(l_r));
if (ret != sizeof(l_r))
return ret;
if (l_r > l)
return -EINVAL;
memset(v, 0, l);
ret = epic_file_read(fd, v, l_r);
if (ret != (int)l_r)
return ret;
return 0;
}
static int write_bond_to_file(appDbRec_t *r, char *filename)
{
if (!r->inUse) {
return -EINVAL;
}
int fd = epic_file_open(filename, "w");
int ret;
if (fd < 0) {
return fd;
}
static const uint8_t version = 1;
ret = epic_file_write(fd, &version, sizeof(version));
if (ret != sizeof(version))
goto out;
#define write_element(t, x) \
if ((ret = write_tlv(fd, t, sizeof(r->x), &r->x))) \
goto out;
write_element(APP_DB_NVM_PEER_ADDR_ID, peerAddr);
write_element(APP_DB_NVM_ADDR_TYPE_ID, addrType);
write_element(APP_DB_NVM_PEER_IRK_ID, peerIrk);
write_element(APP_DB_NVM_PEER_CSRK_ID, peerCsrk);
write_element(APP_DB_NVM_KV_MASK_ID, keyValidMask);
/* peerAddedToRl not persisted by upstream */
/* write_element(, peerAddedToRl); */
write_element(APP_DB_NVM_PEER_RAPO_ID, peerRpao);
write_element(APP_DB_NVM_LOCAL_LTK_ID, localLtk);
write_element(APP_DB_NVM_LOCAL_SEC_LVL_ID, localLtkSecLevel);
write_element(APP_DB_NVM_PEER_ADDR_RES_ID, peerAddrRes);
write_element(APP_DB_NVM_PEER_LTK_ID, peerLtk);
write_element(APP_DB_NVM_PEER_SEC_LVL_ID, peerLtkSecLevel);
write_element(APP_DB_NVM_CCC_TBL_ID, cccTbl);
write_element(APP_DB_NVM_PEER_SIGN_CTR_ID, peerSignCounter);
write_element(APP_DB_NVM_HDL_LIST_ID, hdlList);
write_element(APP_DB_NVM_DISC_STATUS_ID, discStatus);
write_element(APP_DB_NVM_SEQUENCE_NUMBER_ID, sequenceNumber);
write_element(APP_DB_NVM_VALID_ID, valid);
#undef write_element
out:
epic_file_close(fd);
return ret;
}
static int read_bond_from_file(appDbRec_t *r, char *filename)
{
int fd = epic_file_open(filename, "r");
if (fd < 0) {
return fd;
}
uint8_t version;
int ret = epic_file_read(fd, &version, sizeof(version));
if (ret != sizeof(version)) {
goto out;
}
if (version != 1) {
ret = -EINVAL;
goto out;
}
#define read_element(t, x) \
if ((ret = read_tlv(fd, t, sizeof(r->x), &r->x))) \
goto out;
read_element(APP_DB_NVM_PEER_ADDR_ID, peerAddr);
read_element(APP_DB_NVM_ADDR_TYPE_ID, addrType);
read_element(APP_DB_NVM_PEER_IRK_ID, peerIrk);
read_element(APP_DB_NVM_PEER_CSRK_ID, peerCsrk);
read_element(APP_DB_NVM_KV_MASK_ID, keyValidMask);
/* peerAddedToRl not persisted by upstream */
/* read_element(, peerAddedToRl); */
read_element(APP_DB_NVM_PEER_RAPO_ID, peerRpao);
read_element(APP_DB_NVM_LOCAL_LTK_ID, localLtk);
read_element(APP_DB_NVM_LOCAL_SEC_LVL_ID, localLtkSecLevel);
read_element(APP_DB_NVM_PEER_ADDR_RES_ID, peerAddrRes);
read_element(APP_DB_NVM_PEER_LTK_ID, peerLtk);
read_element(APP_DB_NVM_PEER_SEC_LVL_ID, peerLtkSecLevel);
read_element(APP_DB_NVM_CCC_TBL_ID, cccTbl);
read_element(APP_DB_NVM_PEER_SIGN_CTR_ID, peerSignCounter);
read_element(APP_DB_NVM_HDL_LIST_ID, hdlList);
read_element(APP_DB_NVM_DISC_STATUS_ID, discStatus);
read_element(APP_DB_NVM_SEQUENCE_NUMBER_ID, sequenceNumber);
read_element(APP_DB_NVM_VALID_ID, valid);
#undef read_element
r->inUse = true;
out:
epic_file_close(fd);
return ret;
}
static int delete_bond(char *filename)
{
return epic_file_unlink(filename);
}
/*************************************************************************************************/
/*!
* \brief Initialize the device database.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbInit(void)
{
memset(&records, 0, sizeof(records));
char filename[32];
for (int i = 0; i < APP_DB_NUM_RECS; i++) {
record_to_filename(&records[i], filename, sizeof(filename));
int ret = read_bond_from_file(&records[i], filename);
if (ret < 0) {
if (ret != -ENOENT) {
LOG_WARN(
"bondings",
"Reading pairing '%s' failed: %d",
filename,
ret
);
}
memset(&records[i], 0, sizeof(records[i]));
}
}
}
/* clang-format off */
/*************************************************************************************************/
/*!
* \brief Create a new device database record.
*
* \param addrType Address type.
* \param pAddr Peer device address.
*
* \return Database record handle.
*/
/*************************************************************************************************/
appDbHdl_t AppDbNewRecord(uint8_t addrType, uint8_t *pAddr)
{
appDbRec_t *pRec = records;
uint8_t i;
/* find a free record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (!pRec->inUse)
{
break;
}
}
/* if all records were allocated */
if (i == 0)
{
/* overwrite the oldest record */
pRec = record_with_lowest_seq_number();
}
/* initialize record */
memset(pRec, 0, sizeof(appDbRec_t));
pRec->inUse = TRUE;
pRec->addrType = addrType;
BdaCpy(pRec->peerAddr, pAddr);
pRec->peerAddedToRl = FALSE;
pRec->peerRpao = FALSE;
pRec->sequenceNumber = record_with_highest_seq_number()->sequenceNumber + 1;
return (appDbHdl_t) pRec;
}
/*************************************************************************************************/
/*!
* \brief Get the next database record for a given record. For the first record, the function
* should be called with 'hdl' set to 'APP_DB_HDL_NONE'.
*
* \param hdl Database record handle.
*
* \return Next record handle found. APP_DB_HDL_NONE, otherwise.
*/
/*************************************************************************************************/
appDbHdl_t AppDbGetNextRecord(appDbHdl_t hdl)
{
appDbRec_t *pRec;
/* if first record is requested */
if (hdl == APP_DB_HDL_NONE)
{
pRec = records;
}
/* if valid record passed in */
else if (AppDbRecordInUse(hdl))
{
pRec = (appDbRec_t *)hdl;
pRec++;
}
/* invalid record passed in */
else
{
return APP_DB_HDL_NONE;
}
/* look for next valid record */
while (pRec < &records[APP_DB_NUM_RECS])
{
/* if record is in use */
if (pRec->inUse && pRec->valid)
{
/* record found */
return (appDbHdl_t)pRec;
}
/* look for next record */
pRec++;
}
/* end of records */
return APP_DB_HDL_NONE;
}
/*************************************************************************************************/
/*!
* \brief Delete a new device database record.
*
* \param hdl Database record handle.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbDeleteRecord(appDbHdl_t hdl)
{
((appDbRec_t *) hdl)->inUse = FALSE;
char filename[32];
record_to_filename((appDbRec_t *) hdl, filename, sizeof(filename));
delete_bond(filename);
}
/*************************************************************************************************/
/*!
* \brief Validate a new device database record. This function is called when pairing is
* successful and the devices are bonded.
*
* \param hdl Database record handle.
* \param keyMask Bitmask of keys to validate.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbValidateRecord(appDbHdl_t hdl, uint8_t keyMask)
{
((appDbRec_t *) hdl)->valid = TRUE;
((appDbRec_t *) hdl)->keyValidMask = keyMask;
}
/*************************************************************************************************/
/*!
* \brief Check if a record has been validated. If it has not, delete it. This function
* is typically called when the connection is closed.
*
* \param hdl Database record handle.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbCheckValidRecord(appDbHdl_t hdl)
{
if (((appDbRec_t *) hdl)->valid == FALSE)
{
AppDbDeleteRecord(hdl);
}
}
/*************************************************************************************************/
/*!
* \brief Check if a database record is in use.
* \param hdl Database record handle.
*
* \return TURE if record in use. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbRecordInUse(appDbHdl_t hdl)
{
appDbRec_t *pRec = records;
uint8_t i;
/* see if record is in database record list */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse && pRec->valid && (pRec == ((appDbRec_t *)hdl)))
{
return TRUE;
}
}
return FALSE;
}
/*************************************************************************************************/
/*!
* \brief Check if there is a stored bond with any device.
*
* \param hdl Database record handle.
*
* \return TRUE if a bonded device is found, FALSE otherwise.
*/
/*************************************************************************************************/
bool_t AppDbCheckBonded(void)
{
appDbRec_t *pRec = records;
uint8_t i;
/* find a record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse)
{
return TRUE;
}
}
return FALSE;
}
/*************************************************************************************************/
/*!
* \brief Delete all database records.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbDeleteAllRecords(void)
{
appDbRec_t *pRec = records;
uint8_t i;
/* set in use to false for all records */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
pRec->inUse = FALSE;
}
}
/*************************************************************************************************/
/*!
* \brief Find a device database record by peer address.
*
* \param addrType Address type.
* \param pAddr Peer device address.
*
* \return Database record handle or APP_DB_HDL_NONE if not found.
*/
/*************************************************************************************************/
appDbHdl_t AppDbFindByAddr(uint8_t addrType, uint8_t *pAddr)
{
appDbRec_t *pRec = records;
uint8_t peerAddrType = DmHostAddrType(addrType);
uint8_t i;
/* find matching record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse && (pRec->addrType == peerAddrType) && BdaCmp(pRec->peerAddr, pAddr))
{
return (appDbHdl_t) pRec;
}
}
return APP_DB_HDL_NONE;
}
/*************************************************************************************************/
/*!
* \brief Find a device database record by data in an LTK request.
*
* \param encDiversifier Encryption diversifier associated with key.
* \param pRandNum Pointer to random number associated with key.
*
* \return Database record handle or APP_DB_HDL_NONE if not found.
*/
/*************************************************************************************************/
appDbHdl_t AppDbFindByLtkReq(uint16_t encDiversifier, uint8_t *pRandNum)
{
appDbRec_t *pRec = records;
uint8_t i;
/* find matching record */
for (i = APP_DB_NUM_RECS; i > 0; i--, pRec++)
{
if (pRec->inUse && (pRec->localLtk.ediv == encDiversifier) &&
(memcmp(pRec->localLtk.rand, pRandNum, SMP_RAND8_LEN) == 0))
{
return (appDbHdl_t) pRec;
}
}
return APP_DB_HDL_NONE;
}
/*************************************************************************************************/
/*!
* \brief Get a key from a device database record.
*
* \param hdl Database record handle.
* \param type Type of key to get.
* \param pSecLevel If the key is valid, the security level of the key.
*
* \return Pointer to key if key is valid or NULL if not valid.
*/
/*************************************************************************************************/
dmSecKey_t *AppDbGetKey(appDbHdl_t hdl, uint8_t type, uint8_t *pSecLevel)
{
dmSecKey_t *pKey = NULL;
/* if key valid */
if ((type & ((appDbRec_t *) hdl)->keyValidMask) != 0)
{
switch(type)
{
case DM_KEY_LOCAL_LTK:
*pSecLevel = ((appDbRec_t *) hdl)->localLtkSecLevel;
pKey = (dmSecKey_t *) &((appDbRec_t *) hdl)->localLtk;
break;
case DM_KEY_PEER_LTK:
*pSecLevel = ((appDbRec_t *) hdl)->peerLtkSecLevel;
pKey = (dmSecKey_t *) &((appDbRec_t *) hdl)->peerLtk;
break;
case DM_KEY_IRK:
pKey = (dmSecKey_t *)&((appDbRec_t *)hdl)->peerIrk;
break;
case DM_KEY_CSRK:
pKey = (dmSecKey_t *)&((appDbRec_t *)hdl)->peerCsrk;
break;
default:
break;
}
}
return pKey;
}
/*************************************************************************************************/
/*!
* \brief Set a key in a device database record.
*
* \param hdl Database record handle.
* \param pKey Key data.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetKey(appDbHdl_t hdl, dmSecKeyIndEvt_t *pKey)
{
switch(pKey->type)
{
case DM_KEY_LOCAL_LTK:
((appDbRec_t *) hdl)->localLtkSecLevel = pKey->secLevel;
((appDbRec_t *) hdl)->localLtk = pKey->keyData.ltk;
break;
case DM_KEY_PEER_LTK:
((appDbRec_t *) hdl)->peerLtkSecLevel = pKey->secLevel;
((appDbRec_t *) hdl)->peerLtk = pKey->keyData.ltk;
break;
case DM_KEY_IRK:
((appDbRec_t *)hdl)->peerIrk = pKey->keyData.irk;
/* make sure peer record is stored using its identity address */
((appDbRec_t *)hdl)->addrType = pKey->keyData.irk.addrType;
BdaCpy(((appDbRec_t *)hdl)->peerAddr, pKey->keyData.irk.bdAddr);
break;
case DM_KEY_CSRK:
((appDbRec_t *)hdl)->peerCsrk = pKey->keyData.csrk;
/* sign counter must be initialized to zero when CSRK is generated */
((appDbRec_t *)hdl)->peerSignCounter = 0;
break;
default:
break;
}
}
/*************************************************************************************************/
/*!
* \brief Get the client characteristic configuration descriptor table.
*
* \param hdl Database record handle.
*
* \return Pointer to client characteristic configuration descriptor table.
*/
/*************************************************************************************************/
uint16_t *AppDbGetCccTbl(appDbHdl_t hdl)
{
return ((appDbRec_t *) hdl)->cccTbl;
}
/*************************************************************************************************/
/*!
* \brief Set a value in the client characteristic configuration table.
*
* \param hdl Database record handle.
* \param idx Table index.
* \param value client characteristic configuration value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetCccTblValue(appDbHdl_t hdl, uint16_t idx, uint16_t value)
{
WSF_ASSERT(idx < APP_DB_NUM_CCCD);
((appDbRec_t *) hdl)->cccTbl[idx] = value;
}
/*************************************************************************************************/
/*!
* \brief Get the discovery status.
*
* \param hdl Database record handle.
*
* \return Discovery status.
*/
/*************************************************************************************************/
uint8_t AppDbGetDiscStatus(appDbHdl_t hdl)
{
return ((appDbRec_t *) hdl)->discStatus;
}
/*************************************************************************************************/
/*!
* \brief Set the discovery status.
*
* \param hdl Database record handle.
* \param state Discovery status.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetDiscStatus(appDbHdl_t hdl, uint8_t status)
{
((appDbRec_t *) hdl)->discStatus = status;
}
/*************************************************************************************************/
/*!
* \brief Get the cached handle list.
*
* \param hdl Database record handle.
*
* \return Pointer to handle list.
*/
/*************************************************************************************************/
uint16_t *AppDbGetHdlList(appDbHdl_t hdl)
{
return ((appDbRec_t *) hdl)->hdlList;
}
/*************************************************************************************************/
/*!
* \brief Set the cached handle list.
*
* \param hdl Database record handle.
* \param pHdlList Pointer to handle list.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetHdlList(appDbHdl_t hdl, uint16_t *pHdlList)
{
memcpy(((appDbRec_t *) hdl)->hdlList, pHdlList, sizeof(((appDbRec_t *) hdl)->hdlList));
}
/*************************************************************************************************/
/*!
* \brief Get address resolution attribute value read from a peer device.
*
* \param hdl Database record handle.
*
* \return TRUE if address resolution is supported in peer device. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbGetPeerAddrRes(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerAddrRes;
}
/*************************************************************************************************/
/*!
* \brief Set address resolution attribute value for a peer device.
*
* \param hdl Database record handle.
* \param addrRes Address resolution attribue value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerAddrRes(appDbHdl_t hdl, uint8_t addrRes)
{
((appDbRec_t *)hdl)->peerAddrRes = addrRes;
}
/*************************************************************************************************/
/*!
* \brief Get sign counter for a peer device.
*
* \param hdl Database record handle.
*
* \return Sign counter for peer device.
*/
/*************************************************************************************************/
uint32_t AppDbGetPeerSignCounter(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerSignCounter;
}
/*************************************************************************************************/
/*!
* \brief Set sign counter for a peer device.
*
* \param hdl Database record handle.
* \param signCounter Sign counter for peer device.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerSignCounter(appDbHdl_t hdl, uint32_t signCounter)
{
if(((appDbRec_t *)hdl)->peerSignCounter != signCounter) {
((appDbRec_t *)hdl)->peerSignCounter = signCounter;
}
}
/*************************************************************************************************/
/*!
* \brief Get the peer device added to resolving list flag value.
*
* \param hdl Database record handle.
*
* \return TRUE if peer device's been added to resolving list. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbGetPeerAddedToRl(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerAddedToRl;
}
/*************************************************************************************************/
/*!
* \brief Set the peer device added to resolving list flag to a given value.
*
* \param hdl Database record handle.
* \param peerAddedToRl Peer device added to resolving list flag value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerAddedToRl(appDbHdl_t hdl, bool_t peerAddedToRl)
{
((appDbRec_t *)hdl)->peerAddedToRl = peerAddedToRl;
}
/*************************************************************************************************/
/*!
* \brief Get the resolvable private address only attribute flag for a given peer device.
*
* \param hdl Database record handle.
*
* \return TRUE if RPA Only attribute is present on peer device. FALSE, otherwise.
*/
/*************************************************************************************************/
bool_t AppDbGetPeerRpao(appDbHdl_t hdl)
{
return ((appDbRec_t *)hdl)->peerRpao;
}
/*************************************************************************************************/
/*!
* \brief Set the resolvable private address only attribute flag for a given peer device.
*
* \param hdl Database record handle.
* \param peerRpao Resolvable private address only attribute flag value.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbSetPeerRpao(appDbHdl_t hdl, bool_t peerRpao)
{
((appDbRec_t *)hdl)->peerRpao = peerRpao;
}
/*************************************************************************************************/
/*!
* \brief Store the client characteristic configuration table for a device record in NVM.
*
* \param hdl Database record handle.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbNvmStoreCccTbl(appDbHdl_t hdl)
{
/* We take a short cut and simply write the whole file again. */
AppDbNvmStoreBond(hdl);
}
/*************************************************************************************************/
/*!
* \brief Store bonding information for device record in NVM.
*
* \param hdl Database record handle.
*
* \return None.
*/
/*************************************************************************************************/
void AppDbNvmStoreBond(appDbHdl_t hdl)
{
appDbRec_t *pRec = (appDbRec_t *) hdl;
if (pRec->inUse && pRec->valid) {
char filename[32];
record_to_filename(pRec, filename, sizeof(filename));
/* Directory might exist already. Call will fail silently in that case. */
epic_file_mkdir("pairings");
int ret = write_bond_to_file(pRec, filename);
if(ret < 0) {
LOG_WARN(
"bondings",
"Writing pairing '%s' failed: %d",
filename,
ret
);
}
}
}
/* clang-format on */
int AppDbGetPairingName(appDbHdl_t hdl, char *buf, size_t buf_size)
{
appDbRec_t *pRec = (appDbRec_t *)hdl;
return record_to_filename(pRec, buf, buf_size);
}
#include "wsf_types.h"
#include "wsf_os.h"
#include "wsf_buf.h"
#include "wsf_timer.h"
#include "wsf_trace.h"
#include "app_ui.h"
#include "fit/fit_api.h"
#include "hci_vs.h"
#include "ble_api.h"
#include "ff.h"
#include "epicardium.h"
#include "wsf_types.h"
#include "util/bstream.h"
#include "wsf_assert.h"
#include "att_api.h"
#include "FreeRTOS.h"
#include "crc32.h"
#include "epicardium.h"
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <machine/endian.h>
#define CARD10_UUID_SUFFIX \
0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23, 0x42, 0x23
#define CARD10_UUID_PREFIX 0x02, 0x23, 0x42
/*!< \brief Service start handle. */
#define CARD10_START_HDL 0x920
/*!< \brief Service end handle. */
......@@ -39,8 +25,8 @@ enum {
/*!< \brief card10 service declaration */
CARD10_SVC_HDL = CARD10_START_HDL,
/*!< \brief time update characteristic */
CARD10_TIME_UPDATE_CH_HDL,
CARD10_TIME_UPDATE_VAL_HDL,
CARD10_TIME_CH_HDL,
CARD10_TIME_VAL_HDL,
/*!< \brief vibra characteristic */
CARD10_VIRBA_CH_HDL,
CARD10_VIBRA_VAL_HDL,
......@@ -71,6 +57,9 @@ enum {
/*!< \brief flashlight characteristic */
CARD10_FLASHLIGHT_CH_HDL,
CARD10_FLASHLIGHT_VAL_HDL,
/*!< \brief flashlight characteristic */
CARD10_PERSONAL_STATE_CH_HDL,
CARD10_PERSONAL_STATE_VAL_HDL,
/*!< \brief leds above characteristic */
CARD10_LEDS_ABOVE_CH_HDL,
CARD10_LEDS_ABOVE_VAL_HDL,
......@@ -85,24 +74,31 @@ enum {
/* BLE UUID for card10 service*/
static const uint8_t UUID_svc[] = { CARD10_UUID_SUFFIX, 0x0, CARD10_UUID_PREFIX };
// works vor everyone?
static const uint16_t UUID_len = sizeof(UUID_svc);
// starting at 0x01 with write (non visual) charateristics
/* BLE UUID for card10 time update */
/* BLE UUID for card10 time */
static const uint8_t UUID_char_time[] = {
ATT_PROP_WRITE,
UINT16_TO_BYTES(CARD10_TIME_UPDATE_VAL_HDL),
(ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP),
UINT16_TO_BYTES(CARD10_TIME_VAL_HDL),
CARD10_UUID_SUFFIX, 0x01, CARD10_UUID_PREFIX
};
static uint8_t timeValue[] = { UINT32_TO_BYTES(0), UINT32_TO_BYTES(0) };
static uint16_t timeLen = sizeof(timeValue);
// works vor everyone?
static const uint16_t UUID_char_len = sizeof(UUID_char_time);
static const uint8_t UUID_attChar_time[] = {
CARD10_UUID_SUFFIX, 0x01, CARD10_UUID_PREFIX
};
/* BLE UUID for card10 char vibra */
static const uint8_t UUID_char_vibra[] = {
ATT_PROP_WRITE,
ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_VIBRA_VAL_HDL),
CARD10_UUID_SUFFIX, 0x0f, CARD10_UUID_PREFIX
};
......@@ -116,7 +112,7 @@ static const uint8_t UUID_attChar_vibra[] = {
/* BLE UUID for card10 char rockets */
static const uint8_t UUID_char_rockets[] = {
ATT_PROP_WRITE,
ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_ROCKETS_VAL_HDL),
CARD10_UUID_SUFFIX, 0x10, CARD10_UUID_PREFIX
};
......@@ -125,9 +121,12 @@ static const uint8_t UUID_attChar_rockets[] = {
CARD10_UUID_SUFFIX, 0x10, CARD10_UUID_PREFIX
};
static uint8_t rocketsValue[] = { 0, 0, 0 };
static uint16_t rocketsLen = sizeof(rocketsValue);
/* BLE UUID for card10 led background bottom left */
static const uint8_t UUID_char_led_bg_bottom_left[] = {
ATT_PROP_WRITE,
ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL),
CARD10_UUID_SUFFIX, 0x11, CARD10_UUID_PREFIX
};
......@@ -136,9 +135,13 @@ static const uint8_t UUID_attChar_led_bg_bottom_left[] = {
CARD10_UUID_SUFFIX, 0x11, CARD10_UUID_PREFIX
};
static uint8_t ledBGBottomLeftValue[] = { 0,0,0 };
// works vor everyone?
static uint16_t rgbLen = sizeof(ledBGBottomLeftValue);
/* BLE UUID for card10 led background bottom right */
static const uint8_t UUID_char_led_bg_bottom_right[] = {
ATT_PROP_WRITE,
ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL),
CARD10_UUID_SUFFIX, 0x12, CARD10_UUID_PREFIX
};
......@@ -147,9 +150,11 @@ static const uint8_t UUID_attChar_led_bg_bottom_right[] = {
CARD10_UUID_SUFFIX, 0x12, CARD10_UUID_PREFIX
};
static uint8_t ledBGBottomRightValue[] = { 0,0,0 };
/* BLE UUID for card10 led background top right */
static const uint8_t UUID_char_led_bg_top_right[] = {
ATT_PROP_WRITE,
ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_LED_BG_TOP_RIGHT_VAL_HDL),
CARD10_UUID_SUFFIX, 0x13, CARD10_UUID_PREFIX
};
......@@ -158,9 +163,11 @@ static const uint8_t UUID_attChar_led_bg_top_right[] = {
CARD10_UUID_SUFFIX, 0x13, CARD10_UUID_PREFIX
};
static uint8_t ledBGTopRightValue[] = { 0,0,0 };
/* BLE UUID for card10 led background top left */
static const uint8_t UUID_char_led_bg_top_left[] = {
ATT_PROP_WRITE,
ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_LED_BG_TOP_LEFT_VAL_HDL),
CARD10_UUID_SUFFIX, 0x14, CARD10_UUID_PREFIX
};
......@@ -169,6 +176,8 @@ static const uint8_t UUID_attChar_led_bg_top_left[] = {
CARD10_UUID_SUFFIX, 0x14, CARD10_UUID_PREFIX
};
static uint8_t ledBGTopLeftValue[] = { 0,0,0 };
/* BLE UUID for card10 dim leds on bottom */
static const uint8_t UUID_char_leds_bottom_dim[] = {
ATT_PROP_WRITE,
......@@ -193,7 +202,7 @@ static const uint8_t UUID_attChar_leds_top_dim[] = {
/* BLE UUID for card10 powersafe */
static const uint8_t UUID_char_led_powersafe[] = {
ATT_PROP_WRITE,
ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_LED_POWERSAFE_VAL_HDL),
CARD10_UUID_SUFFIX, 0x17, CARD10_UUID_PREFIX
};
......@@ -204,7 +213,7 @@ static const uint8_t UUID_attChar_led_powersafe[] = {
/* BLE UUID for card10 flashlight */
static const uint8_t UUID_char_flashlight[] = {
ATT_PROP_WRITE,
ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_FLASHLIGHT_VAL_HDL),
CARD10_UUID_SUFFIX, 0x18, CARD10_UUID_PREFIX
};
......@@ -213,9 +222,23 @@ static const uint8_t UUID_attChar_flashlight[] = {
CARD10_UUID_SUFFIX, 0x18, CARD10_UUID_PREFIX
};
/* BLE UUID for card10 personal state */
static const uint8_t UUID_char_personal_state[] = {
ATT_PROP_READ | ATT_PROP_WRITE,
UINT16_TO_BYTES(CARD10_PERSONAL_STATE_VAL_HDL),
CARD10_UUID_SUFFIX, 0x19, CARD10_UUID_PREFIX
};
static const uint8_t UUID_attChar_personal_state[] = {
CARD10_UUID_SUFFIX, 0x19, CARD10_UUID_PREFIX
};
static uint8_t personalStateValue = 0;
static uint16_t personalStateLen = sizeof(personalStateValue);
/* BLE UUID for card10 above leds */
static const uint8_t UUID_char_leds_above[] = {
ATT_PROP_WRITE,
ATT_PROP_READ | ATT_PROP_WRITE_NO_RSP,
UINT16_TO_BYTES(CARD10_LEDS_ABOVE_VAL_HDL),
CARD10_UUID_SUFFIX, 0x20, CARD10_UUID_PREFIX
};
......@@ -223,6 +246,21 @@ static const uint8_t UUID_char_leds_above[] = {
static const uint8_t UUID_attChar_leds_above[] = {
CARD10_UUID_SUFFIX, 0x20, CARD10_UUID_PREFIX
};
static uint8_t aboveLEDsValue[] = {
0,0,0, // 0
0,0,0, // 1
0,0,0, // 2
0,0,0, // 3
0,0,0, // 4
0,0,0, // 5
0,0,0, // 6
0,0,0, // 7
0,0,0, // 8
0,0,0, // 9
0,0,0, // 10
};
static uint16_t aboveLEDsLen = sizeof(aboveLEDsValue);
// starting at 0xf0 with read only characteristics
/* BLE UUID for card10 char light sensor */
......@@ -234,308 +272,299 @@ static const uint8_t UUID_char_light_sensor[] = {
static const uint8_t UUID_attChar_light_sensor[] = {
CARD10_UUID_SUFFIX, 0xf0, CARD10_UUID_PREFIX
};
static uint8_t initLightSensorValue[] = { UINT16_TO_BYTES(0) };
static uint16_t initLightSensorLen = sizeof(initLightSensorValue);
/* clang-format on */
/*
* Create the BLE service description.
*/
static void *addCard10GroupDyn(void)
{
void *pSHdl;
uint8_t initLightSensorValue[] = { UINT16_TO_BYTES(0) };
/* Create the service */
pSHdl = AttsDynCreateGroup(CARD10_START_HDL, CARD10_END_HDL);
if (pSHdl != NULL) {
/* Primary service */
AttsDynAddAttrConst(
pSHdl,
attPrimSvcUuid,
UUID_svc,
sizeof(UUID_svc),
0,
ATTS_PERMIT_READ
);
// TIME UPDTAE
static const attsAttr_t card10SvcAttrList[] = {
{
.pUuid = attPrimSvcUuid,
.pValue = (uint8_t *)UUID_svc,
.pLen = (uint16_t *)&UUID_len,
.maxLen = sizeof(UUID_svc),
.permissions = ATTS_PERMIT_READ,
},
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_time,
sizeof(UUID_char_time),
0,
ATTS_PERMIT_READ
);
// TIME
AttsDynAddAttr(
pSHdl,
UUID_attChar_time,
NULL,
0,
sizeof(uint64_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_time,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_time),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_time,
.pValue = timeValue,
.pLen = &timeLen,
.maxLen = sizeof(uint64_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
// VIBRA
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_vibra,
sizeof(UUID_char_vibra),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_vibra,
NULL,
0,
sizeof(uint16_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_vibra,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_vibra),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_vibra,
.pValue = NULL,
.maxLen = sizeof(uint16_t),
.settings = ATTS_SET_WRITE_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH,
},
// ROCKETS
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_rockets,
sizeof(UUID_char_rockets),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_rockets,
NULL,
0,
3 * sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_rockets,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_rockets),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_rockets,
.pValue = rocketsValue,
.pLen = &rocketsLen,
.maxLen = 3 * sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
// BG LED Bottom left
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_led_bg_bottom_left,
sizeof(UUID_char_led_bg_bottom_left),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_led_bg_bottom_left,
NULL,
0,
3 * sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_led_bg_bottom_left,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_led_bg_bottom_left),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_led_bg_bottom_left,
.pValue = ledBGBottomLeftValue,
.pLen = &rgbLen,
.maxLen = 3 * sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
// BG LED Bottom right
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_led_bg_bottom_right,
sizeof(UUID_char_led_bg_bottom_right),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_led_bg_bottom_right,
NULL,
0,
3 * sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_led_bg_bottom_right,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_led_bg_bottom_right),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_led_bg_bottom_right,
.pValue = ledBGBottomRightValue,
.pLen = &rgbLen,
.maxLen = 3 * sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
// BG LED top right
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_led_bg_top_right,
sizeof(UUID_char_led_bg_top_right),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_led_bg_top_right,
NULL,
0,
3 * sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_led_bg_top_right,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_led_bg_top_right),
.settings = 0,
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_led_bg_top_right,
.pValue = ledBGTopRightValue,
.pLen = &rgbLen,
.maxLen = 3 * sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
// BG LED top left
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_led_bg_top_left,
sizeof(UUID_char_led_bg_top_left),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_led_bg_top_left,
NULL,
0,
3 * sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_led_bg_top_left,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_led_bg_top_left),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_led_bg_top_left,
.pValue = ledBGTopLeftValue,
.pLen = &rgbLen,
.maxLen = 3 * sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
// Dim bottom module
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_leds_bottom_dim,
sizeof(UUID_char_leds_bottom_dim),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_leds_bottom_dim,
NULL,
0,
sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_leds_bottom_dim,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_leds_bottom_dim),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_leds_bottom_dim,
.pValue = NULL,
.pLen = 0,
.maxLen = sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH,
},
// Dim top module
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_leds_top_dim,
sizeof(UUID_char_leds_top_dim),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_leds_top_dim,
NULL,
0,
sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_leds_top_dim,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_leds_top_dim),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_leds_top_dim,
.pValue = NULL,
.maxLen = sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH,
},
// led powersafe
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_led_powersafe,
sizeof(UUID_char_led_powersafe),
0,
ATTS_PERMIT_READ
);
AttsDynAddAttr(
pSHdl,
UUID_attChar_led_powersafe,
NULL,
0,
sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_led_powersafe,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_led_powersafe),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_led_powersafe,
.pValue = NULL,
.maxLen = sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH,
},
// flashlight
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_flashlight,
sizeof(UUID_char_flashlight),
0,
ATTS_PERMIT_READ
);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_flashlight,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_flashlight),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_flashlight,
.pValue = NULL,
.maxLen = sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH,
},
AttsDynAddAttr(
pSHdl,
UUID_attChar_flashlight,
NULL,
0,
sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
// personal state
// ABOVE LEDS
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_personal_state,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_personal_state),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_personal_state,
.pValue = &personalStateValue,
.pLen = &personalStateLen,
.maxLen = sizeof(uint16_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_leds_above,
sizeof(UUID_char_leds_above),
0,
ATTS_PERMIT_READ
);
// ABOVE LEDS
AttsDynAddAttr(
pSHdl,
UUID_attChar_leds_above,
NULL,
0,
11 * 3 * sizeof(uint8_t),
ATTS_SET_WRITE_CBACK,
ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH);
// LIGHT_SENSOR
AttsDynAddAttrConst(
pSHdl,
attChUuid,
UUID_char_light_sensor,
sizeof(UUID_char_light_sensor),
0,
ATTS_PERMIT_READ
);
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_leds_above,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_leds_above),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_leds_above,
.pValue = aboveLEDsValue,
.pLen = &aboveLEDsLen,
.maxLen = 11 * 3 * sizeof(uint8_t),
.settings = ATTS_SET_WRITE_CBACK | ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_WRITE | ATTS_PERMIT_WRITE_ENC |
ATTS_PERMIT_WRITE_AUTH | ATTS_PERMIT_READ |
ATTS_PERMIT_READ_ENC | ATTS_PERMIT_READ_AUTH,
},
// Light sensor
AttsDynAddAttr(
pSHdl,
UUID_attChar_light_sensor,
initLightSensorValue,
sizeof(uint8_t),
sizeof(uint8_t),
ATTS_SET_READ_CBACK,
ATTS_PERMIT_READ | ATTS_PERMIT_READ_ENC |
ATTS_PERMIT_READ_AUTH);
APP_TRACE_INFO0("ble-card10: services bound\n");
}
return pSHdl;
}
{
.pUuid = attChUuid,
.pValue = (uint8_t *)UUID_char_light_sensor,
.pLen = (uint16_t *)&UUID_char_len,
.maxLen = sizeof(UUID_char_light_sensor),
.permissions = ATTS_PERMIT_READ,
},
{
.pUuid = UUID_attChar_light_sensor,
.pValue = initLightSensorValue,
.pLen = &initLightSensorLen,
.maxLen = sizeof(uint8_t),
.settings = ATTS_SET_READ_CBACK,
.permissions = ATTS_PERMIT_READ | ATTS_PERMIT_READ_ENC |
ATTS_PERMIT_READ_AUTH,
},
};
// validating, that the service really get all charateristics
WSF_CT_ASSERT(
((sizeof(card10SvcAttrList) / sizeof(card10SvcAttrList[0])) ==
CARD10_END_HDL - CARD10_START_HDL + 1));
/*
* Set the time given in milliseconds since 1.1.1970 as 64 bit integer.
*/
......@@ -548,7 +577,39 @@ static uint8_t setTime(uint8_t *pValue)
time = __bswap64(timeNet);
epic_rtc_set_milliseconds(time);
APP_TRACE_INFO1("set time to: %d\n", time);
APP_TRACE_INFO0("ble-card10: set time");
return ATT_SUCCESS;
}
/*
* Set a rgb led
*/
static uint8_t setRGBLed(uint8_t led, uint8_t *pValue)
{
epic_leds_set(led, pValue[0], pValue[1], pValue[2]);
APP_TRACE_INFO4(
"ble-card10: set rgb led %d: #%02x%02x%02x\n",
led,
pValue[0],
pValue[1],
pValue[2]
);
return ATT_SUCCESS;
}
/*
* Get value of a rgb led
*/
static uint8_t getRGBLed(uint8_t led, attsAttr_t *pAttr)
{
epic_leds_get_rgb(led, pAttr->pValue);
APP_TRACE_INFO4(
"ble-card10: set rgb led %d: #%02x%02x%02x\n",
led,
pAttr->pValue[0],
pAttr->pValue[1],
pAttr->pValue[2]
);
return ATT_SUCCESS;
}
......@@ -570,7 +631,7 @@ static uint8_t writeCard10CB(
switch (handle) {
// time
case CARD10_TIME_UPDATE_VAL_HDL:
case CARD10_TIME_VAL_HDL:
return setTime(pValue);
// vibra
case CARD10_VIBRA_VAL_HDL:
......@@ -592,60 +653,80 @@ static uint8_t writeCard10CB(
return ATT_SUCCESS;
// bg leds
case CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL:
epic_leds_set(11, pValue[0], pValue[1], pValue[2]);
APP_TRACE_INFO3(
"ble-card10: set bg bottom left: #%02x%02x%02x\n",
pValue[0],
pValue[1],
pValue[2]
);
return ATT_SUCCESS;
return setRGBLed(11, pValue);
case CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL:
epic_leds_set(12, pValue[0], pValue[1], pValue[2]);
APP_TRACE_INFO3(
"ble-card10: set bg bottom right: #%02x%02x%02x\n",
pValue[0],
pValue[1],
pValue[2]
);
return ATT_SUCCESS;
return setRGBLed(12, pValue);
case CARD10_LED_BG_TOP_RIGHT_VAL_HDL:
epic_leds_set(13, pValue[0], pValue[1], pValue[2]);
APP_TRACE_INFO3(
"ble-card10: set bg top right: #%02x%02x%02x\n",
pValue[0],
pValue[1],
pValue[2]
);
return ATT_SUCCESS;
return setRGBLed(13, pValue);
case CARD10_LED_BG_TOP_LEFT_VAL_HDL:
epic_leds_set(14, pValue[0], pValue[1], pValue[2]);
APP_TRACE_INFO3(
"ble-card10: set bg top left: #%02x%02x%02x\n",
pValue[0],
pValue[1],
pValue[2]
);
return ATT_SUCCESS;
return setRGBLed(14, pValue);
// dim
case CARD10_LEDS_BOTTOM_DIM_VAL_HDL:
ui8 = pValue[0];
if (ui8 >= 1 && ui8 <= 8) {
if (operation == ATT_PDU_WRITE_CMD ||
operation == ATT_PDU_SIGNED_WRITE_CMD ||
operation == ATT_PDU_WRITE_REQ ||
operation == ATT_PDU_EXEC_WRITE_REQ) {
epic_leds_dim_bottom(pValue[0]);
APP_TRACE_INFO1("dim bottom to: %d\n", pValue[0]);
APP_TRACE_INFO1(
"ble-card10: dim bottom to: %d\n",
pValue[0]
);
return ATT_SUCCESS;
} else if (operation == ATT_PDU_PREP_WRITE_REQ) {
APP_TRACE_INFO1(
"ble_card10: value for dim bottom would be okay: %d\n",
pValue[0]
);
return ATT_SUCCESS;
} else {
APP_TRACE_INFO1(
"ble-card10: dim bottom with unknown operation: %d\n",
operation
);
return ATT_ERR_INVALID_PDU;
}
APP_TRACE_INFO1("dim bottom invalid value (1-8): %d\n", ui8);
} else {
APP_TRACE_INFO1(
"ble-card: prep dim bottom invalid value (1-8): %d\n",
ui8
);
return ATT_ERR_RANGE;
}
case CARD10_LEDS_TOP_DIM_VAL_HDL:
ui8 = pValue[0];
if (ui8 >= 1 && ui8 <= 8) {
epic_leds_dim_top(ui8);
APP_TRACE_INFO1("dim top to: %d\n", ui8);
if (operation == ATT_PDU_WRITE_CMD ||
operation == ATT_PDU_SIGNED_WRITE_CMD ||
operation == ATT_PDU_WRITE_REQ ||
operation == ATT_PDU_EXEC_WRITE_REQ) {
epic_leds_dim_top(pValue[0]);
APP_TRACE_INFO1(
"ble-card10: dim top to: %d\n",
pValue[0]
);
return ATT_SUCCESS;
} else if (operation == ATT_PDU_PREP_WRITE_REQ) {
APP_TRACE_INFO1(
"ble_card10: value for dim top would be okay: %d\n",
pValue[0]
);
return ATT_SUCCESS;
} else {
APP_TRACE_INFO1(
"ble-card10: dim top with unknown operation: %d\n",
operation
);
return ATT_ERR_INVALID_PDU;
}
APP_TRACE_INFO1("dim top invalid value (1-8): %d\n", ui8);
} else {
APP_TRACE_INFO1(
"ble-card: prep dim top invalid value (1-8): %d\n",
ui8
);
return ATT_ERR_RANGE;
}
// led powersafe
case CARD10_LED_POWERSAFE_VAL_HDL:
epic_leds_set_powersave(pValue[0]);
......@@ -656,27 +737,57 @@ static uint8_t writeCard10CB(
epic_set_flashlight(pValue[0]);
APP_TRACE_INFO1("set flashlight to: %d\n", pValue[0]);
return ATT_SUCCESS;
// personal state
case CARD10_PERSONAL_STATE_VAL_HDL:
BYTES_TO_UINT16(ui16, pValue);
if (ui16 <= STATE_MAX) {
if (operation == ATT_PDU_WRITE_CMD ||
operation == ATT_PDU_SIGNED_WRITE_CMD ||
operation == ATT_PDU_WRITE_REQ ||
operation == ATT_PDU_EXEC_WRITE_REQ) {
epic_personal_state_set(ui16, true);
APP_TRACE_INFO1(
"ble-card10: set personal state to: %d\n",
ui16
);
return ATT_SUCCESS;
} else if (operation == ATT_PDU_PREP_WRITE_REQ) {
APP_TRACE_INFO1(
"ble_card10: personal state would be okay: %d\n",
ui16
);
return ATT_SUCCESS;
} else {
APP_TRACE_INFO1(
"ble-card10: personal state with unknown operation: %d\n",
operation
);
return ATT_ERR_INVALID_PDU;
}
} else {
APP_TRACE_INFO2(
"ble-card: personal state invalid value (0-%d): %d\n",
STATE_MAX - 1,
ui16
);
return ATT_ERR_RANGE;
}
// leds above
case CARD10_LEDS_ABOVE_VAL_HDL:
APP_TRACE_INFO0("ble-card10: update LEDs above");
for (ui16 = 0; ui16 < 11; ui16++) {
epic_leds_set(
ui16,
pValue[ui16 * 3],
pValue[ui16 * 3 + 1],
pValue[ui16 * 3 + 2]
);
APP_TRACE_INFO4(
"ble-card10: set led %ld above to #%02x%02x%02x\n",
epic_leds_prep(
ui16,
pValue[ui16 * 3],
pValue[ui16 * 3 + 1],
pValue[ui16 * 3 + 2]
);
}
epic_leds_update();
return ATT_SUCCESS;
default:
APP_TRACE_INFO1(
"ble-card10: unsupported characteristic: %c\n", handle
);
APP_TRACE_INFO1("ble-card10: unsupported handle: %x\n", handle);
return ATT_ERR_HANDLE;
}
}
......@@ -693,8 +804,63 @@ static uint8_t readCard10CB(
attsAttr_t *pAttr
) {
uint16_t ui16 = 0;
uint64_t ui64 = 0;
uint8_t rgb[] = { 0, 0, 0 };
switch (handle) {
// time
case CARD10_TIME_VAL_HDL:
ui64 = epic_rtc_get_milliseconds();
uint64_t time;
time = __bswap64(ui64);
memcpy(pAttr->pValue, &time, sizeof(time));
APP_TRACE_INFO0("ble-card10: read time\n");
return ATT_SUCCESS;
case CARD10_ROCKETS_VAL_HDL:
pAttr->pValue[0] = epic_leds_get_rocket(0);
pAttr->pValue[1] = epic_leds_get_rocket(1);
pAttr->pValue[2] = epic_leds_get_rocket(2);
APP_TRACE_INFO3(
"ble-card10: get rockets 0:%d, 1:%d, 2:%d\n",
pAttr->pValue[0],
pAttr->pValue[1],
pAttr->pValue[2]
);
return ATT_SUCCESS;
// background leds
case CARD10_LED_BG_BOTTOM_LEFT_VAL_HDL:
return getRGBLed(11, pAttr);
case CARD10_LED_BG_BOTTOM_RIGHT_VAL_HDL:
return getRGBLed(12, pAttr);
case CARD10_LED_BG_TOP_RIGHT_VAL_HDL:
return getRGBLed(13, pAttr);
case CARD10_LED_BG_TOP_LEFT_VAL_HDL:
return getRGBLed(14, pAttr);
// personal state
case CARD10_PERSONAL_STATE_VAL_HDL:
ui16 = epic_personal_state_get();
*pAttr->pValue = ui16;
APP_TRACE_INFO1("ble-card10: read personal state: %d\n", ui16);
return ATT_SUCCESS;
// leds above
case CARD10_LEDS_ABOVE_VAL_HDL:
for (ui16 = 0; ui16 < 11; ui16++) {
epic_leds_get_rgb(ui16, rgb);
pAttr->pValue[ui16 * 3] = rgb[0];
pAttr->pValue[ui16 * 3 + 1] = rgb[1];
pAttr->pValue[ui16 * 3 + 2] = rgb[2];
APP_TRACE_INFO4(
"ble-card10: get led %ld above to #%02x%02x%02x\n",
ui16,
pAttr->pValue[ui16 * 3],
pAttr->pValue[ui16 * 3 + 1],
pAttr->pValue[ui16 * 3 + 2]
);
}
return ATT_SUCCESS;
// light sensor
case CARD10_LIGHT_SENSOR_VAL_HDL:
epic_light_sensor_get(&ui16);
*pAttr->pValue = ui16;
......@@ -706,12 +872,20 @@ static uint8_t readCard10CB(
}
}
static attsGroup_t svcCard10Group = {
.pNext = NULL,
.pAttr = (attsAttr_t *)card10SvcAttrList,
.readCback = readCard10CB,
.writeCback = writeCard10CB,
.startHandle = CARD10_START_HDL,
.endHandle = CARD10_END_HDL,
};
/*
* This registers and starts the BLE card10 service.
*/
void bleCard10_init(void)
{
void *pSHdl = addCard10GroupDyn();
AttsDynRegister(pSHdl, readCard10CB, writeCard10CB);
AttsAddGroup(&svcCard10Group);
}