From 847b200b92540df13acbf8099525fc330d221af5 Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Thu, 26 Sep 2024 20:02:28 +0200 Subject: [PATCH 1/3] Remove sim MotionService.h copy and use InfiniTime verison instead --- CMakeLists.txt | 7 +- sim/components/ble/MotionService.cpp | 126 --------------- sim/components/ble/MotionService.h | 39 ----- sim/components/ble/NimbleController.cpp | 6 +- sim/components/ble/NimbleController.h | 12 +- sim/host/ble_att.h | 198 ++++++++++++++++++++++++ sim/host/ble_gatt.cpp | 6 + sim/host/ble_gatt.h | 2 + sim/host/ble_hs_mbuf.cpp | 26 ++++ sim/host/ble_hs_mbuf.h | 88 +++++++++++ sim/host/os_mbuf.cpp | 6 + sim/host/os_mbuf.h | 12 ++ 12 files changed, 352 insertions(+), 176 deletions(-) delete mode 100644 sim/components/ble/MotionService.cpp delete mode 100644 sim/components/ble/MotionService.h create mode 100644 sim/host/ble_att.h create mode 100644 sim/host/ble_hs_mbuf.cpp create mode 100644 sim/host/ble_hs_mbuf.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5911991..0f8be22 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -88,9 +88,12 @@ add_library(sim-base STATIC # nrf/components/libraries/timer sim/libraries/gpiote/app_gpiote.h # includes hal/nrf_gpio.h # nibmle + sim/host/ble_att.h sim/host/ble_gap.h sim/host/ble_gatt.h sim/host/ble_gatt.cpp + sim/host/ble_hs_mbuf.h + sim/host/ble_hs_mbuf.cpp sim/host/ble_uuid.h sim/host/ble_uuid.cpp sim/host/os_mbuf.h @@ -128,8 +131,6 @@ target_sources(infinisim PUBLIC sim/components/battery/BatteryController.cpp sim/components/ble/AlertNotificationService.h sim/components/ble/AlertNotificationService.cpp - sim/components/ble/MotionService.h - sim/components/ble/MotionService.cpp sim/components/ble/MusicService.h sim/components/ble/MusicService.cpp sim/components/ble/NimbleController.h @@ -198,6 +199,8 @@ target_sources(infinisim PUBLIC ${InfiniTime_DIR}/src/components/datetime/DateTimeController.cpp ${InfiniTime_DIR}/src/components/settings/Settings.h ${InfiniTime_DIR}/src/components/settings/Settings.cpp + ${InfiniTime_DIR}/src/components/ble/MotionService.h + ${InfiniTime_DIR}/src/components/ble/MotionService.cpp ${InfiniTime_DIR}/src/components/ble/NavigationService.h ${InfiniTime_DIR}/src/components/ble/NavigationService.cpp ${InfiniTime_DIR}/src/components/ble/NotificationManager.h diff --git a/sim/components/ble/MotionService.cpp b/sim/components/ble/MotionService.cpp deleted file mode 100644 index 409d3b7..0000000 --- a/sim/components/ble/MotionService.cpp +++ /dev/null @@ -1,126 +0,0 @@ -#include "components/ble/MotionService.h" -#include "components/motion/MotionController.h" -#include "components/ble/NimbleController.h" -#include - -using namespace Pinetime::Controllers; - -// namespace { -// // 0003yyxx-78fc-48fe-8e23-433b3a1942d0 -// constexpr ble_uuid128_t CharUuid(uint8_t x, uint8_t y) { -// return ble_uuid128_t {.u = {.type = BLE_UUID_TYPE_128}, -// .value = {0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, x, y, 0x03, 0x00}}; -// } -// -// // 00030000-78fc-48fe-8e23-433b3a1942d0 -// constexpr ble_uuid128_t BaseUuid() { -// return CharUuid(0x00, 0x00); -// } -// -// constexpr ble_uuid128_t motionServiceUuid {BaseUuid()}; -// constexpr ble_uuid128_t stepCountCharUuid {CharUuid(0x01, 0x00)}; -// constexpr ble_uuid128_t motionValuesCharUuid {CharUuid(0x02, 0x00)}; -// -// int MotionServiceCallback(uint16_t /*conn_handle*/, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { -// auto* motionService = static_cast(arg); -// return motionService->OnStepCountRequested(attr_handle, ctxt); -// } -// } -// -// TODO Refactoring - remove dependency to SystemTask -MotionService::MotionService(NimbleController& nimble, Controllers::MotionController& motionController) - : nimble {nimble}, - motionController {motionController}/*, - characteristicDefinition {{.uuid = &stepCountCharUuid.u, - .access_cb = MotionServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, - .val_handle = &stepCountHandle}, - {.uuid = &motionValuesCharUuid.u, - .access_cb = MotionServiceCallback, - .arg = this, - .flags = BLE_GATT_CHR_F_READ | BLE_GATT_CHR_F_NOTIFY, - .val_handle = &motionValuesHandle}, - {0}}, - serviceDefinition { - {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &motionServiceUuid.u, .characteristics = characteristicDefinition}, - {0}, - } */{ - // TODO refactor to prevent this loop dependency (service depends on controller and controller depends on service) - motionController.SetService(this); -} -// -// void MotionService::Init() { -// int res = 0; -// res = ble_gatts_count_cfg(serviceDefinition); -// ASSERT(res == 0); -// -// res = ble_gatts_add_svcs(serviceDefinition); -// ASSERT(res == 0); -// } -// -// int MotionService::OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context) { -// if (attributeHandle == stepCountHandle) { -// NRF_LOG_INFO("Motion-stepcount : handle = %d", stepCountHandle); -// uint32_t buffer = motionController.NbSteps(); -// -// int res = os_mbuf_append(context->om, &buffer, 4); -// return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; -// } else if (attributeHandle == motionValuesHandle) { -// int16_t buffer[3] = {motionController.X(), motionController.Y(), motionController.Z()}; -// -// int res = os_mbuf_append(context->om, buffer, 3 * sizeof(int16_t)); -// return (res == 0) ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES; -// } -// return 0; -// } -// -// void MotionService::OnNewStepCountValue(uint32_t stepCount) { -// if (!stepCountNoficationEnabled) -// return; -// -// uint32_t buffer = stepCount; -// auto* om = ble_hs_mbuf_from_flat(&buffer, 4); -// -// uint16_t connectionHandle = nimble.connHandle(); -// -// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { -// return; -// } -// -// ble_gattc_notify_custom(connectionHandle, stepCountHandle, om); -// } -// -// void MotionService::OnNewMotionValues(int16_t x, int16_t y, int16_t z) { -// if (!motionValuesNoficationEnabled) -// return; -// -// int16_t buffer[3] = {x, y, z}; -// auto* om = ble_hs_mbuf_from_flat(buffer, 3 * sizeof(int16_t)); -// -// uint16_t connectionHandle = nimble.connHandle(); -// -// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { -// return; -// } -// -// ble_gattc_notify_custom(connectionHandle, motionValuesHandle, om); -// } - -void MotionService::SubscribeNotification(uint16_t attributeHandle) { - if (attributeHandle == stepCountHandle) - stepCountNoficationEnabled = true; - else if (attributeHandle == motionValuesHandle) - motionValuesNoficationEnabled = true; -} - -void MotionService::UnsubscribeNotification(uint16_t attributeHandle) { - if (attributeHandle == stepCountHandle) - stepCountNoficationEnabled = false; - else if (attributeHandle == motionValuesHandle) - motionValuesNoficationEnabled = false; -} - -bool MotionService::IsMotionNotificationSubscribed() const { - return motionValuesNoficationEnabled; -} diff --git a/sim/components/ble/MotionService.h b/sim/components/ble/MotionService.h deleted file mode 100644 index a607c3e..0000000 --- a/sim/components/ble/MotionService.h +++ /dev/null @@ -1,39 +0,0 @@ -#pragma once -// #define min // workaround: nimble's min/max macros conflict with libstdc++ -// #define max -// #include -#include -// #undef max -// #undef min - -namespace Pinetime { - namespace Controllers { - class NimbleController; - class MotionController; - - class MotionService { - public: - MotionService(NimbleController& nimble, Controllers::MotionController& motionController); - // void Init(); - // int OnStepCountRequested(uint16_t attributeHandle, ble_gatt_access_ctxt* context); - // void OnNewStepCountValue(uint32_t stepCount); - // void OnNewMotionValues(int16_t x, int16_t y, int16_t z); - - void SubscribeNotification(uint16_t attributeHandle); - void UnsubscribeNotification(uint16_t attributeHandle); - bool IsMotionNotificationSubscribed() const; - - private: - NimbleController& nimble; - Controllers::MotionController& motionController; - - // struct ble_gatt_chr_def characteristicDefinition[3]; - // struct ble_gatt_svc_def serviceDefinition[2]; - - uint16_t stepCountHandle; - uint16_t motionValuesHandle; - std::atomic_bool stepCountNoficationEnabled {false}; - std::atomic_bool motionValuesNoficationEnabled {false}; - }; - } -} diff --git a/sim/components/ble/NimbleController.cpp b/sim/components/ble/NimbleController.cpp index 8716bf2..fda4155 100644 --- a/sim/components/ble/NimbleController.cpp +++ b/sim/components/ble/NimbleController.cpp @@ -365,9 +365,9 @@ void NimbleController::StartDiscovery() { // } } -//uint16_t NimbleController::connHandle() { -// return connectionHandle; -//} +uint16_t NimbleController::connHandle() { + return connectionHandle; +} void NimbleController::NotifyBatteryLevel(uint8_t level) { // if (connectionHandle != BLE_HS_CONN_HANDLE_NONE) { diff --git a/sim/components/ble/NimbleController.h b/sim/components/ble/NimbleController.h index 9a42ea9..8566d1b 100644 --- a/sim/components/ble/NimbleController.h +++ b/sim/components/ble/NimbleController.h @@ -2,11 +2,11 @@ #include -//#define min // workaround: nimble's min/max macros conflict with libstdc++ -//#define max -//#include -//#undef max -//#undef min +#define min // workaround: nimble's min/max macros conflict with libstdc++ +#define max +#include +#undef max +#undef min //#include "components/ble/AlertNotificationClient.h" #include "components/ble/AlertNotificationService.h" //#include "components/ble/BatteryInformationService.h" @@ -121,7 +121,7 @@ namespace Pinetime { // ServiceDiscovery serviceDiscovery; uint8_t addrType; -// uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE; + uint16_t connectionHandle = BLE_HS_CONN_HANDLE_NONE; uint8_t fastAdvCount = 0; uint8_t bondId[16] = {0}; diff --git a/sim/host/ble_att.h b/sim/host/ble_att.h new file mode 100644 index 0000000..b7af472 --- /dev/null +++ b/sim/host/ble_att.h @@ -0,0 +1,198 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_BLE_ATT_ +#define H_BLE_ATT_ + +/** + * @brief Bluetooth Attribute Protocol (ATT) + * @defgroup bt_att Bluetooth Attribute Protocol (ATT) + * @ingroup bt_host + * @{ + */ + +// sim: no need for queue +// #include "os/queue.h" +// sim: add inttypes for uintX_t +#include + +#ifdef __cplusplus +extern "C" { +#endif + +struct os_mbuf; + +#define BLE_ATT_UUID_PRIMARY_SERVICE 0x2800 +#define BLE_ATT_UUID_SECONDARY_SERVICE 0x2801 +#define BLE_ATT_UUID_INCLUDE 0x2802 +#define BLE_ATT_UUID_CHARACTERISTIC 0x2803 + +#define BLE_ATT_ERR_INVALID_HANDLE 0x01 +#define BLE_ATT_ERR_READ_NOT_PERMITTED 0x02 +#define BLE_ATT_ERR_WRITE_NOT_PERMITTED 0x03 +#define BLE_ATT_ERR_INVALID_PDU 0x04 +#define BLE_ATT_ERR_INSUFFICIENT_AUTHEN 0x05 +#define BLE_ATT_ERR_REQ_NOT_SUPPORTED 0x06 +#define BLE_ATT_ERR_INVALID_OFFSET 0x07 +#define BLE_ATT_ERR_INSUFFICIENT_AUTHOR 0x08 +#define BLE_ATT_ERR_PREPARE_QUEUE_FULL 0x09 +#define BLE_ATT_ERR_ATTR_NOT_FOUND 0x0a +#define BLE_ATT_ERR_ATTR_NOT_LONG 0x0b +#define BLE_ATT_ERR_INSUFFICIENT_KEY_SZ 0x0c +#define BLE_ATT_ERR_INVALID_ATTR_VALUE_LEN 0x0d +#define BLE_ATT_ERR_UNLIKELY 0x0e +#define BLE_ATT_ERR_INSUFFICIENT_ENC 0x0f +#define BLE_ATT_ERR_UNSUPPORTED_GROUP 0x10 +#define BLE_ATT_ERR_INSUFFICIENT_RES 0x11 + +#define BLE_ATT_OP_ERROR_RSP 0x01 +#define BLE_ATT_OP_MTU_REQ 0x02 +#define BLE_ATT_OP_MTU_RSP 0x03 +#define BLE_ATT_OP_FIND_INFO_REQ 0x04 +#define BLE_ATT_OP_FIND_INFO_RSP 0x05 +#define BLE_ATT_OP_FIND_TYPE_VALUE_REQ 0x06 +#define BLE_ATT_OP_FIND_TYPE_VALUE_RSP 0x07 +#define BLE_ATT_OP_READ_TYPE_REQ 0x08 +#define BLE_ATT_OP_READ_TYPE_RSP 0x09 +#define BLE_ATT_OP_READ_REQ 0x0a +#define BLE_ATT_OP_READ_RSP 0x0b +#define BLE_ATT_OP_READ_BLOB_REQ 0x0c +#define BLE_ATT_OP_READ_BLOB_RSP 0x0d +#define BLE_ATT_OP_READ_MULT_REQ 0x0e +#define BLE_ATT_OP_READ_MULT_RSP 0x0f +#define BLE_ATT_OP_READ_GROUP_TYPE_REQ 0x10 +#define BLE_ATT_OP_READ_GROUP_TYPE_RSP 0x11 +#define BLE_ATT_OP_WRITE_REQ 0x12 +#define BLE_ATT_OP_WRITE_RSP 0x13 +#define BLE_ATT_OP_PREP_WRITE_REQ 0x16 +#define BLE_ATT_OP_PREP_WRITE_RSP 0x17 +#define BLE_ATT_OP_EXEC_WRITE_REQ 0x18 +#define BLE_ATT_OP_EXEC_WRITE_RSP 0x19 +#define BLE_ATT_OP_NOTIFY_REQ 0x1b +#define BLE_ATT_OP_INDICATE_REQ 0x1d +#define BLE_ATT_OP_INDICATE_RSP 0x1e +#define BLE_ATT_OP_WRITE_CMD 0x52 + +#define BLE_ATT_ATTR_MAX_LEN 512 + +#define BLE_ATT_F_READ 0x01 +#define BLE_ATT_F_WRITE 0x02 +#define BLE_ATT_F_READ_ENC 0x04 +#define BLE_ATT_F_READ_AUTHEN 0x08 +#define BLE_ATT_F_READ_AUTHOR 0x10 +#define BLE_ATT_F_WRITE_ENC 0x20 +#define BLE_ATT_F_WRITE_AUTHEN 0x40 +#define BLE_ATT_F_WRITE_AUTHOR 0x80 + +#define HA_FLAG_PERM_RW (BLE_ATT_F_READ | BLE_ATT_F_WRITE) + +#define BLE_ATT_ACCESS_OP_READ 1 +#define BLE_ATT_ACCESS_OP_WRITE 2 + +/** Default ATT MTU. Also the minimum. */ +#define BLE_ATT_MTU_DFLT 23 + +/** + * An ATT MTU of 527 allows the largest ATT command (signed write) to contain a + * 512-byte attribute value. + */ +#define BLE_ATT_MTU_MAX 527 + +/** + * Reads a locally registered attribute. If the specified attribute handle + * corresponds to a GATT characteristic value or descriptor, the read is + * performed by calling the registered GATT access callback. + * + * @param attr_handle The 16-bit handle of the attribute to read. + * @param out_om On success, this is made to point to a + * newly-allocated mbuf containing the + * attribute data read. + * + * @return 0 on success; + * NimBLE host ATT return code if the attribute + * access callback reports failure; + * NimBLE host core return code on unexpected + * error. + */ +int ble_att_svr_read_local(uint16_t attr_handle, struct os_mbuf **out_om); + +/** + * Writes a locally registered attribute. This function consumes the supplied + * mbuf regardless of the outcome. If the specified attribute handle + * corresponds to a GATT characteristic value or descriptor, the write is + * performed by calling the registered GATT access callback. + * + * @param attr_handle The 16-bit handle of the attribute to write. + * @param om The value to write to the attribute. + * + * @return 0 on success; + * NimBLE host ATT return code if the attribute + * access callback reports failure; + * NimBLE host core return code on unexpected + * error. + */ +int ble_att_svr_write_local(uint16_t attr_handle, struct os_mbuf *om); + +/** + * Retrieves the ATT MTU of the specified connection. If an MTU exchange for + * this connection has occurred, the MTU is the lower of the two peers' + * preferred values. Otherwise, the MTU is the default value of 23. + * + * @param conn_handle The handle of the connection to query. + * + * @return The specified connection's ATT MTU, or 0 if + * there is no such connection. + */ +uint16_t ble_att_mtu(uint16_t conn_handle); + +/** + * Retrieves the preferred ATT MTU. This is the value indicated by the device + * during an ATT MTU exchange. + * + * @return The preferred ATT MTU. + */ +uint16_t ble_att_preferred_mtu(void); + +/** + * Sets the preferred ATT MTU; the device will indicate this value in all + * subsequent ATT MTU exchanges. The ATT MTU of a connection is equal to the + * lower of the two peers' preferred MTU values. The ATT MTU is what dictates + * the maximum size of any message sent during a GATT procedure. + * + * The specified MTU must be within the following range: [23, BLE_ATT_MTU_MAX]. + * 23 is a minimum imposed by the Bluetooth specification; BLE_ATT_MTU_MAX is a + * NimBLE compile-time setting. + * + * @param mtu The preferred ATT MTU. + * + * @return 0 on success; + * BLE_HS_EINVAL if the specified value is not + * within the allowed range. + */ +int ble_att_set_preferred_mtu(uint16_t mtu); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif diff --git a/sim/host/ble_gatt.cpp b/sim/host/ble_gatt.cpp index 904929e..972c2b3 100644 --- a/sim/host/ble_gatt.cpp +++ b/sim/host/ble_gatt.cpp @@ -19,6 +19,12 @@ #include "host/ble_gatt.h" +int ble_gattc_notify_custom(uint16_t conn_handle, uint16_t att_handle, + struct os_mbuf *om) +{ + return 0; +} + int ble_gatts_count_cfg(const struct ble_gatt_svc_def *defs) { diff --git a/sim/host/ble_gatt.h b/sim/host/ble_gatt.h index 1b48d52..a8d705e 100644 --- a/sim/host/ble_gatt.h +++ b/sim/host/ble_gatt.h @@ -30,6 +30,8 @@ #include //#include "host/ble_att.h" #include "host/ble_uuid.h" +#include "host/ble_att.h" +#include "host/ble_hs_mbuf.h" // include from sim for ASSERT #include "nrf_assert.h" #include "host/os_mbuf.h" diff --git a/sim/host/ble_hs_mbuf.cpp b/sim/host/ble_hs_mbuf.cpp new file mode 100644 index 0000000..fedd967 --- /dev/null +++ b/sim/host/ble_hs_mbuf.cpp @@ -0,0 +1,26 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "host/ble_hs_mbuf.h" + + +struct os_mbuf *ble_hs_mbuf_from_flat(const void *buf, uint16_t len) +{ + return (os_mbuf *)nullptr; +} diff --git a/sim/host/ble_hs_mbuf.h b/sim/host/ble_hs_mbuf.h new file mode 100644 index 0000000..e2e9b3c --- /dev/null +++ b/sim/host/ble_hs_mbuf.h @@ -0,0 +1,88 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#ifndef H_BLE_HS_MBUF_ +#define H_BLE_HS_MBUF_ + +/** + * @brief Bluetooth Host chained memory buffer (mbuf) + * @defgroup bt_host_mbuf Bluetooth Host chained memory buffer (mbuf) + * @ingroup bt_host + * @{ + */ + +#include +#ifdef __cplusplus +extern "C" { +#endif + +// sim: copied from ble_hs.h +/** Connection handle not present */ +#define BLE_HS_CONN_HANDLE_NONE 0xffff +// sim: end copied from ble_hs.h + + +struct os_mbuf; + +/** + * Allocates an mbuf suitable for an ATT command packet. The resulting packet + * has sufficient leading space for: + * - ACL data header + * - L2CAP B-frame header + * - Largest ATT command base (prepare write request / response). + * + * @return An empty mbuf on success, NULL on error. + */ +//struct os_mbuf *ble_hs_mbuf_att_pkt(void); + +/** + * Allocates an mbuf and fills it with the contents of the specified flat + * buffer. + * + * @param buf The flat buffer to copy from. + * @param len The length of the flat buffer. + * + * @return A newly-allocated mbuf on success, NULL on error. + */ +struct os_mbuf *ble_hs_mbuf_from_flat(const void *buf, uint16_t len); + +/** + * Copies the contents of an mbuf into the specified flat buffer. If the flat + * buffer is too small to contain the mbuf's contents, it is filled to capacity + * and BLE_HS_EMSGSIZE is returned. + * + * @param om The mbuf to copy from. + * @param flat The destination flat buffer. + * @param max_len The size of the flat buffer. + * @param out_copy_len The number of bytes actually copied gets written here. + * + * @return 0 on success or BLE host core return code on error. + */ +//int ble_hs_mbuf_to_flat(const struct os_mbuf *om, void *flat, uint16_t max_len, +// uint16_t *out_copy_len); + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif diff --git a/sim/host/os_mbuf.cpp b/sim/host/os_mbuf.cpp index 5e8c128..774581c 100644 --- a/sim/host/os_mbuf.cpp +++ b/sim/host/os_mbuf.cpp @@ -25,3 +25,9 @@ os_mbuf_copydata(const struct os_mbuf *m, int off, int len, void *dst) { return 0; } + +int +os_mbuf_append(struct os_mbuf *om, const void *data, uint16_t len) +{ + return 0; +} \ No newline at end of file diff --git a/sim/host/os_mbuf.h b/sim/host/os_mbuf.h index ee6ec49..cd7dd79 100644 --- a/sim/host/os_mbuf.h +++ b/sim/host/os_mbuf.h @@ -115,6 +115,18 @@ struct os_mbuf { int os_mbuf_copydata(const struct os_mbuf *m, int off, int len, void *dst); +/** + * Append data onto a mbuf + * + * @param om The mbuf to append the data onto + * @param data The data to append onto the mbuf + * @param len The length of the data to append + * + * @return 0 on success, and an error code on failure + */ +int os_mbuf_append(struct os_mbuf *m, const void *, uint16_t); + + #ifdef __cplusplus } #endif From 16b7505c15bd660d0b6429f74e1fe90cf6a26725 Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Thu, 26 Sep 2024 20:33:46 +0200 Subject: [PATCH 2/3] Remove sim copy of AlertNotificationService and use InfiniTime files --- CMakeLists.txt | 4 +- .../ble/AlertNotificationService.cpp | 125 ------------------ sim/components/ble/AlertNotificationService.h | 68 ---------- 3 files changed, 2 insertions(+), 195 deletions(-) delete mode 100644 sim/components/ble/AlertNotificationService.cpp delete mode 100644 sim/components/ble/AlertNotificationService.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 0f8be22..5c95612 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -129,8 +129,6 @@ target_sources(infinisim PUBLIC sim/displayapp/LittleVgl.cpp sim/components/battery/BatteryController.h sim/components/battery/BatteryController.cpp - sim/components/ble/AlertNotificationService.h - sim/components/ble/AlertNotificationService.cpp sim/components/ble/MusicService.h sim/components/ble/MusicService.cpp sim/components/ble/NimbleController.h @@ -199,6 +197,8 @@ target_sources(infinisim PUBLIC ${InfiniTime_DIR}/src/components/datetime/DateTimeController.cpp ${InfiniTime_DIR}/src/components/settings/Settings.h ${InfiniTime_DIR}/src/components/settings/Settings.cpp + ${InfiniTime_DIR}/src/components/ble/AlertNotificationService.h + ${InfiniTime_DIR}/src/components/ble/AlertNotificationService.cpp ${InfiniTime_DIR}/src/components/ble/MotionService.h ${InfiniTime_DIR}/src/components/ble/MotionService.cpp ${InfiniTime_DIR}/src/components/ble/NavigationService.h diff --git a/sim/components/ble/AlertNotificationService.cpp b/sim/components/ble/AlertNotificationService.cpp deleted file mode 100644 index 5fee5d2..0000000 --- a/sim/components/ble/AlertNotificationService.cpp +++ /dev/null @@ -1,125 +0,0 @@ -#include "components/ble/AlertNotificationService.h" -#include -#include -#include -#include "components/ble/NotificationManager.h" -#include "systemtask/SystemTask.h" - -using namespace Pinetime::Controllers; - -//constexpr ble_uuid16_t AlertNotificationService::ansUuid; -//constexpr ble_uuid16_t AlertNotificationService::ansCharUuid; -//constexpr ble_uuid128_t AlertNotificationService::notificationEventUuid; - -//int AlertNotificationCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { -//auto anService = static_cast(arg); -// return anService->OnAlert(conn_handle, attr_handle, ctxt); -//} - -void AlertNotificationService::Init() { -// int res; -// res = ble_gatts_count_cfg(serviceDefinition); -// ASSERT(res == 0); -// -// res = ble_gatts_add_svcs(serviceDefinition); -// ASSERT(res == 0); -} - -AlertNotificationService::AlertNotificationService(System::SystemTask& systemTask, NotificationManager& notificationManager) - : -// : characteristicDefinition {{.uuid = &ansCharUuid.u, .access_cb = AlertNotificationCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE}, -// {.uuid = ¬ificationEventUuid.u, -// .access_cb = AlertNotificationCallback, -// .arg = this, -// .flags = BLE_GATT_CHR_F_NOTIFY, -// .val_handle = &eventHandle}, -// {0}}, -// serviceDefinition { -// {/* Device Information Service */ -// .type = BLE_GATT_SVC_TYPE_PRIMARY, -// .uuid = &ansUuid.u, -// .characteristics = characteristicDefinition}, -// {0}, -// }, - systemTask {systemTask}, - notificationManager {notificationManager} { -} - -int AlertNotificationService::OnAlert(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { -// if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { -// constexpr size_t stringTerminatorSize = 1; // end of string '\0' -// constexpr size_t headerSize = 3; -// const auto maxMessageSize {NotificationManager::MaximumMessageSize()}; -// const auto maxBufferSize {maxMessageSize + headerSize}; -// -// // Ignore notifications with empty message -// const auto packetLen = OS_MBUF_PKTLEN(ctxt->om); -// if (packetLen <= headerSize) { -// return 0; -// } -// -// size_t bufferSize = std::min(packetLen + stringTerminatorSize, maxBufferSize); -// auto messageSize = std::min(maxMessageSize, (bufferSize - headerSize)); -// Categories category; -// -// NotificationManager::Notification notif; -// os_mbuf_copydata(ctxt->om, headerSize, messageSize - 1, notif.message.data()); -// os_mbuf_copydata(ctxt->om, 0, 1, &category); -// notif.message[messageSize - 1] = '\0'; -// notif.size = messageSize; -// -// // TODO convert all ANS categories to NotificationController categories -// switch (category) { -// case Categories::Call: -// notif.category = Pinetime::Controllers::NotificationManager::Categories::IncomingCall; -// break; -// default: -// notif.category = Pinetime::Controllers::NotificationManager::Categories::SimpleAlert; -// break; -// } -// -// auto event = Pinetime::System::Messages::OnNewNotification; -// notificationManager.Push(std::move(notif)); -// systemTask.PushMessage(event); -// } - return 0; -} - -void AlertNotificationService::AcceptIncomingCall() { -// auto response = IncomingCallResponses::Answer; -// auto* om = ble_hs_mbuf_from_flat(&response, 1); -// -// uint16_t connectionHandle = systemTask.nimble().connHandle(); -// -// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { -// return; -// } -// -// ble_gattc_notify_custom(connectionHandle, eventHandle, om); -} - -void AlertNotificationService::RejectIncomingCall() { -// auto response = IncomingCallResponses::Reject; -// auto* om = ble_hs_mbuf_from_flat(&response, 1); -// -// uint16_t connectionHandle = systemTask.nimble().connHandle(); -// -// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { -// return; -// } -// -// ble_gattc_notify_custom(connectionHandle, eventHandle, om); -} - -void AlertNotificationService::MuteIncomingCall() { -// auto response = IncomingCallResponses::Mute; -// auto* om = ble_hs_mbuf_from_flat(&response, 1); -// -// uint16_t connectionHandle = systemTask.nimble().connHandle(); -// -// if (connectionHandle == 0 || connectionHandle == BLE_HS_CONN_HANDLE_NONE) { -// return; -// } -// -// ble_gattc_notify_custom(connectionHandle, eventHandle, om); -} diff --git a/sim/components/ble/AlertNotificationService.h b/sim/components/ble/AlertNotificationService.h deleted file mode 100644 index 6105c73..0000000 --- a/sim/components/ble/AlertNotificationService.h +++ /dev/null @@ -1,68 +0,0 @@ -#pragma once -#include -#include -//#define min // workaround: nimble's min/max macros conflict with libstdc++ -//#define max -//#include -//#undef max -//#undef min - -// 00020001-78fc-48fe-8e23-433b3a1942d0 -//#define NOTIFICATION_EVENT_SERVICE_UUID_BASE \ -// { 0xd0, 0x42, 0x19, 0x3a, 0x3b, 0x43, 0x23, 0x8e, 0xfe, 0x48, 0xfc, 0x78, 0x01, 0x00, 0x02, 0x00 } - -namespace Pinetime { - - namespace System { - class SystemTask; - } - namespace Controllers { - class NotificationManager; - - class AlertNotificationService { - public: - AlertNotificationService(Pinetime::System::SystemTask& systemTask, Pinetime::Controllers::NotificationManager& notificationManager); - void Init(); - - int OnAlert(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); - - void AcceptIncomingCall(); - void RejectIncomingCall(); - void MuteIncomingCall(); - - enum class IncomingCallResponses : uint8_t { Reject = 0x00, Answer = 0x01, Mute = 0x02 }; - - private: - enum class Categories : uint8_t { - SimpleAlert = 0x00, - Email = 0x01, - News = 0x02, - Call = 0x03, - MissedCall = 0x04, - MmsSms = 0x05, - VoiceMail = 0x06, - Schedule = 0x07, - HighPrioritizedAlert = 0x08, - InstantMessage = 0x09, - All = 0xff - }; - - static constexpr uint16_t ansId {0x1811}; - static constexpr uint16_t ansCharId {0x2a46}; - -// static constexpr ble_uuid16_t ansUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ansId}; - -// static constexpr ble_uuid16_t ansCharUuid {.u {.type = BLE_UUID_TYPE_16}, .value = ansCharId}; - -// static constexpr ble_uuid128_t notificationEventUuid {.u {.type = BLE_UUID_TYPE_128}, .value = NOTIFICATION_EVENT_SERVICE_UUID_BASE}; - -// struct ble_gatt_chr_def characteristicDefinition[3]; -// struct ble_gatt_svc_def serviceDefinition[2]; - - Pinetime::System::SystemTask& systemTask; - NotificationManager& notificationManager; - - uint16_t eventHandle; - }; - } -} From f57ea5eb231666468060c6146a74baf57bc8981a Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Thu, 26 Sep 2024 21:54:06 +0200 Subject: [PATCH 3/3] Remove sim copy of MotionController.h --- CMakeLists.txt | 4 +- sim/components/motion/MotionController.cpp | 147 --------------------- sim/components/motion/MotionController.h | 103 --------------- 3 files changed, 2 insertions(+), 252 deletions(-) delete mode 100644 sim/components/motion/MotionController.cpp delete mode 100644 sim/components/motion/MotionController.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 5c95612..2365a3e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -139,8 +139,6 @@ target_sources(infinisim PUBLIC sim/components/firmwarevalidator/FirmwareValidator.cpp sim/components/heartrate/HeartRateController.h sim/components/heartrate/HeartRateController.cpp - sim/components/motion/MotionController.h - sim/components/motion/MotionController.cpp sim/drivers/Bma421.h sim/drivers/Bma421.cpp sim/drivers/Cst816s.h @@ -209,6 +207,8 @@ target_sources(infinisim PUBLIC ${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.cpp ${InfiniTime_DIR}/src/components/fs/FS.h ${InfiniTime_DIR}/src/components/fs/FS.cpp + ${InfiniTime_DIR}/src/components/motion/MotionController.h + ${InfiniTime_DIR}/src/components/motion/MotionController.cpp ${InfiniTime_DIR}/src/components/motor/MotorController.h ${InfiniTime_DIR}/src/components/motor/MotorController.cpp ${InfiniTime_DIR}/src/drivers/Hrs3300.h diff --git a/sim/components/motion/MotionController.cpp b/sim/components/motion/MotionController.cpp deleted file mode 100644 index 9404339..0000000 --- a/sim/components/motion/MotionController.cpp +++ /dev/null @@ -1,147 +0,0 @@ -#include "components/motion/MotionController.h" - -//#include - -#include "utility/Math.h" - -using namespace Pinetime::Controllers; - -namespace { - constexpr inline int32_t Clamp(int32_t val, int32_t min, int32_t max) { - return val < min ? min : (val > max ? max : val); - } - - // only returns meaningful values if inputs are acceleration due to gravity - int16_t DegreesRolled(int16_t y, int16_t z, int16_t prevY, int16_t prevZ) { - int16_t prevYAngle = Pinetime::Utility::Asin(Clamp(prevY * 32, -32767, 32767)); - int16_t yAngle = Pinetime::Utility::Asin(Clamp(y * 32, -32767, 32767)); - - if (z < 0 && prevZ < 0) { - return yAngle - prevYAngle; - } - if (prevZ < 0) { - if (y < 0) { - return -prevYAngle - yAngle - 180; - } - return -prevYAngle - yAngle + 180; - } - if (z < 0) { - if (y < 0) { - return prevYAngle + yAngle + 180; - } - return prevYAngle + yAngle - 180; - } - return prevYAngle - yAngle; - } -} - -void MotionController::Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps) { -// if (this->nbSteps != nbSteps && service != nullptr) { -// service->OnNewStepCountValue(nbSteps); -// } -// -// if (service != nullptr && (this->x != x || yHistory[0] != y || zHistory[0] != z)) { -// service->OnNewMotionValues(x, y, z); -// } -// -// lastTime = time; -// time = xTaskGetTickCount(); - - lastX = this->x; - this->x = x; - yHistory++; - yHistory[0] = y; - zHistory++; - zHistory[0] = z; - - stats = GetAccelStats(); - - int32_t deltaSteps = nbSteps - this->nbSteps; - if (deltaSteps > 0) { - currentTripSteps += deltaSteps; - } - this->nbSteps = nbSteps; -} - -MotionController::AccelStats MotionController::GetAccelStats() const { - AccelStats stats; - - for (uint8_t i = 0; i < AccelStats::numHistory; i++) { - stats.yMean += yHistory[histSize - i]; - stats.zMean += zHistory[histSize - i]; - stats.prevYMean += yHistory[1 + i]; - stats.prevZMean += zHistory[1 + i]; - } - stats.yMean /= AccelStats::numHistory; - stats.zMean /= AccelStats::numHistory; - stats.prevYMean /= AccelStats::numHistory; - stats.prevZMean /= AccelStats::numHistory; - - for (uint8_t i = 0; i < AccelStats::numHistory; i++) { - stats.yVariance += (yHistory[histSize - i] - stats.yMean) * (yHistory[histSize - i] - stats.yMean); - stats.zVariance += (zHistory[histSize - i] - stats.zMean) * (zHistory[histSize - i] - stats.zMean); - } - stats.yVariance /= AccelStats::numHistory; - stats.zVariance /= AccelStats::numHistory; - - return stats; -} - -bool MotionController::ShouldRaiseWake() const { - return false; -// constexpr uint32_t varianceThresh = 56 * 56; -// constexpr int16_t xThresh = 384; -// constexpr int16_t yThresh = -64; -// constexpr int16_t rollDegreesThresh = -45; -// -// if (x < -xThresh || x > xThresh) { -// return false; -// } -// -// // if the variance is below the threshold, the accelerometer values can be considered to be from acceleration due to gravity -// if (stats.yVariance > varianceThresh || (stats.yMean < -724 && stats.zVariance > varianceThresh) || stats.yMean > yThresh) { -// return false; -// } -// -// return DegreesRolled(stats.yMean, stats.zMean, stats.prevYMean, stats.prevZMean) < rollDegreesThresh; -} - -bool MotionController::ShouldShakeWake(uint16_t thresh) { - return false; -// /* Currently Polling at 10hz, If this ever goes faster scalar and EMA might need adjusting */ -// int32_t speed = -// std::abs(zHistory[0] - zHistory[histSize - 1] + (yHistory[0] - yHistory[histSize - 1]) / 2 + (x - lastX) / 4) * 100 / (time - lastTime); -// // (.2 * speed) + ((1 - .2) * accumulatedSpeed); -// accumulatedSpeed = speed / 5 + accumulatedSpeed * 4 / 5; -// -// return accumulatedSpeed > thresh; -} - -bool MotionController::ShouldLowerSleep() const { - return false; -// if (stats.yMean < 724 || DegreesRolled(stats.yMean, stats.zMean, stats.prevYMean, stats.prevZMean) < 30) { -// return false; -// } -// -// for (uint8_t i = AccelStats::numHistory + 1; i < yHistory.Size(); i++) { -// if (yHistory[i] < 265) { -// return false; -// } -// } -// -// return true; -} - -void MotionController::Init(Pinetime::Drivers::Bma421::DeviceTypes types) { - switch (types) { - case Drivers::Bma421::DeviceTypes::BMA421: - this->deviceType = DeviceTypes::BMA421; - break; - case Drivers::Bma421::DeviceTypes::BMA425: - this->deviceType = DeviceTypes::BMA425; - break; - default: - this->deviceType = DeviceTypes::Unknown; - break; - } -} diff --git a/sim/components/motion/MotionController.h b/sim/components/motion/MotionController.h deleted file mode 100644 index 664ec8f..0000000 --- a/sim/components/motion/MotionController.h +++ /dev/null @@ -1,103 +0,0 @@ -#pragma once - -#include - -//#include - -#include "drivers/Bma421.h" -#include "components/ble/MotionService.h" -#include "utility/CircularBuffer.h" - -namespace Pinetime { - namespace Controllers { - class MotionController { - public: - enum class DeviceTypes { - Unknown, - BMA421, - BMA425, - }; - - void Update(int16_t x, int16_t y, int16_t z, uint32_t nbSteps); - - int16_t X() const { - return x; - } - - int16_t Y() const { - return yHistory[0]; - } - - int16_t Z() const { - return zHistory[0]; - } - - uint32_t NbSteps() const { - return nbSteps; - } - - void ResetTrip() { - currentTripSteps = 0; - } - - uint32_t GetTripSteps() const { - return currentTripSteps; - } - - bool ShouldShakeWake(uint16_t thresh); - bool ShouldRaiseWake() const; - bool ShouldLowerSleep() const; - - int32_t CurrentShakeSpeed() const { - return accumulatedSpeed; - } - - DeviceTypes DeviceType() const { - return deviceType; - } - - void Init(Pinetime::Drivers::Bma421::DeviceTypes types); - - void SetService(Pinetime::Controllers::MotionService* service) { - this->service = service; - } - - Pinetime::Controllers::MotionService* GetService() const { - return service; - } - - private: - uint32_t nbSteps = 0; - uint32_t currentTripSteps = 0; - -// TickType_t lastTime = 0; -// TickType_t time = 0; - - struct AccelStats { - static constexpr uint8_t numHistory = 2; - - int16_t yMean = 0; - int16_t zMean = 0; - int16_t prevYMean = 0; - int16_t prevZMean = 0; - - uint32_t yVariance = 0; - uint32_t zVariance = 0; - }; - - AccelStats GetAccelStats() const; - - AccelStats stats = {}; - - int16_t lastX = 0; - int16_t x = 0; - static constexpr uint8_t histSize = 8; - Utility::CircularBuffer yHistory = {}; - Utility::CircularBuffer zHistory = {}; - int32_t accumulatedSpeed = 0; - - DeviceTypes deviceType = DeviceTypes::Unknown; - Pinetime::Controllers::MotionService* service = nullptr; - }; - } -}