From c7e512a6a8e8134a92b7872eb36c2c8bd800900c Mon Sep 17 00:00:00 2001 From: Reinhold Gschweicher Date: Wed, 25 Sep 2024 23:17:53 +0200 Subject: [PATCH] Use NavigationService from InfiniTime, improve nimble headers Improve the nimble headers such that we can use the NavigationService from InfiniTime instead of having a slightly modified verison in InfiniSim. --- CMakeLists.txt | 7 +- main.cpp | 9 +- sim/components/ble/NavigationService.cpp | 111 -------------------- sim/components/ble/NavigationService.h | 63 ------------ sim/components/ble/NimbleController.cpp | 1 - sim/host/ble_gatt.cpp | 2 +- sim/host/ble_gatt.h | 40 +------ sim/host/ble_uuid.cpp | 27 +++++ sim/host/ble_uuid.h | 9 ++ sim/host/os_mbuf.cpp | 27 +++++ sim/host/os_mbuf.h | 126 +++++++++++++++++++++++ sim/nrf_assert.h | 5 + 12 files changed, 209 insertions(+), 218 deletions(-) delete mode 100644 sim/components/ble/NavigationService.cpp delete mode 100644 sim/components/ble/NavigationService.h create mode 100644 sim/host/ble_uuid.cpp create mode 100644 sim/host/os_mbuf.cpp create mode 100644 sim/host/os_mbuf.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7d7b97a..5911991 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -92,6 +92,9 @@ add_library(sim-base STATIC sim/host/ble_gatt.h sim/host/ble_gatt.cpp sim/host/ble_uuid.h + sim/host/ble_uuid.cpp + sim/host/os_mbuf.h + sim/host/os_mbuf.cpp ) # include the generated lv_conf.h file before anything else target_include_directories(sim-base PUBLIC "${CMAKE_CURRENT_BINARY_DIR}") # lv_conf.h @@ -129,8 +132,6 @@ target_sources(infinisim PUBLIC sim/components/ble/MotionService.cpp sim/components/ble/MusicService.h sim/components/ble/MusicService.cpp - sim/components/ble/NavigationService.h - sim/components/ble/NavigationService.cpp sim/components/ble/NimbleController.h sim/components/ble/NimbleController.cpp sim/components/brightness/BrightnessController.h @@ -197,6 +198,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/NavigationService.h + ${InfiniTime_DIR}/src/components/ble/NavigationService.cpp ${InfiniTime_DIR}/src/components/ble/NotificationManager.h ${InfiniTime_DIR}/src/components/ble/NotificationManager.cpp ${InfiniTime_DIR}/src/components/ble/SimpleWeatherService.h diff --git a/main.cpp b/main.cpp index ffc4299..70f0aa4 100644 --- a/main.cpp +++ b/main.cpp @@ -20,15 +20,16 @@ #include "lv_drivers/indev/keyboard.h" #include "lv_drivers/indev/mousewheel.h" -// get PineTime header -#include "displayapp/InfiniTimeTheme.h" -#include -#include # // be sure to get the sim headers for SimpleWeatherService.h #include "host/ble_gatt.h" #include "host/ble_uuid.h" +// get PineTime header +#include "displayapp/InfiniTimeTheme.h" +#include +#include + #include "BootloaderVersion.h" #include "components/battery/BatteryController.h" #include "components/ble/BleController.h" diff --git a/sim/components/ble/NavigationService.cpp b/sim/components/ble/NavigationService.cpp deleted file mode 100644 index 38a3d85..0000000 --- a/sim/components/ble/NavigationService.cpp +++ /dev/null @@ -1,111 +0,0 @@ -/* Copyright (C) 2021 Adam Pigg - - This file is part of InfiniTime. - - InfiniTime is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - InfiniTime is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ - -#include "components/ble/NavigationService.h" - -#include "systemtask/SystemTask.h" - -namespace { -// // 0001yyxx-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, 0x01, 0x00}}; -// } -// -// // 00010000-78fc-48fe-8e23-433b3a1942d0 -// constexpr ble_uuid128_t BaseUuid() { -// return CharUuid(0x00, 0x00); -// } -// -// constexpr ble_uuid128_t navUuid {BaseUuid()}; -// -// constexpr ble_uuid128_t navFlagCharUuid {CharUuid(0x01, 0x00)}; -// constexpr ble_uuid128_t navNarrativeCharUuid {CharUuid(0x02, 0x00)}; -// constexpr ble_uuid128_t navManDistCharUuid {CharUuid(0x03, 0x00)}; -// constexpr ble_uuid128_t navProgressCharUuid {CharUuid(0x04, 0x00)}; -// -// int NAVCallback(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt, void* arg) { -// auto navService = static_cast(arg); -// return navService->OnCommand(conn_handle, attr_handle, ctxt); -// } -} // namespace - -Pinetime::Controllers::NavigationService::NavigationService(Pinetime::System::SystemTask& system) : m_system(system) { -// characteristicDefinition[0] = { -// .uuid = &navFlagCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; -// -// characteristicDefinition[1] = { -// .uuid = &navNarrativeCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; -// characteristicDefinition[2] = { -// .uuid = &navManDistCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; -// characteristicDefinition[3] = { -// .uuid = &navProgressCharUuid.u, .access_cb = NAVCallback, .arg = this, .flags = BLE_GATT_CHR_F_WRITE | BLE_GATT_CHR_F_READ}; -// -// characteristicDefinition[4] = {0}; -// -// serviceDefinition[0] = {.type = BLE_GATT_SVC_TYPE_PRIMARY, .uuid = &navUuid.u, .characteristics = characteristicDefinition}; -// serviceDefinition[1] = {0}; - - m_progress = 0; -} - -void Pinetime::Controllers::NavigationService::Init() { -// int res = 0; -// res = ble_gatts_count_cfg(serviceDefinition); -// ASSERT(res == 0); -// -// res = ble_gatts_add_svcs(serviceDefinition); -// ASSERT(res == 0); -} - -//int Pinetime::Controllers::NavigationService::OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt) { -// -// if (ctxt->op == BLE_GATT_ACCESS_OP_WRITE_CHR) { -// size_t notifSize = OS_MBUF_PKTLEN(ctxt->om); -// uint8_t data[notifSize + 1]; -// data[notifSize] = '\0'; -// os_mbuf_copydata(ctxt->om, 0, notifSize, data); -// char* s = (char*) &data[0]; -// if (ble_uuid_cmp(ctxt->chr->uuid, &navFlagCharUuid.u) == 0) { -// m_flag = s; -// } else if (ble_uuid_cmp(ctxt->chr->uuid, &navNarrativeCharUuid.u) == 0) { -// m_narrative = s; -// } else if (ble_uuid_cmp(ctxt->chr->uuid, &navManDistCharUuid.u) == 0) { -// m_manDist = s; -// } else if (ble_uuid_cmp(ctxt->chr->uuid, &navProgressCharUuid.u) == 0) { -// m_progress = data[0]; -// } -// } -// return 0; -//} - -std::string Pinetime::Controllers::NavigationService::getFlag() { - return m_flag; -} - -std::string Pinetime::Controllers::NavigationService::getNarrative() { - return m_narrative; -} - -std::string Pinetime::Controllers::NavigationService::getManDist() { - return m_manDist; -} - -int Pinetime::Controllers::NavigationService::getProgress() { - return m_progress; -} diff --git a/sim/components/ble/NavigationService.h b/sim/components/ble/NavigationService.h deleted file mode 100644 index afa89eb..0000000 --- a/sim/components/ble/NavigationService.h +++ /dev/null @@ -1,63 +0,0 @@ -/* Copyright (C) 2021 Adam Pigg - - This file is part of InfiniTime. - - InfiniTime is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published - by the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - InfiniTime is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program. If not, see . -*/ -#pragma once - -#include -#include -//#define min // workaround: nimble's min/max macros conflict with libstdc++ -//#define max -//#include -//#include -//#undef max -//#undef min - -namespace Pinetime { - namespace System { - class SystemTask; - } - namespace Controllers { - - class NavigationService { - public: - explicit NavigationService(Pinetime::System::SystemTask& system); - - void Init(); - -// int OnCommand(uint16_t conn_handle, uint16_t attr_handle, struct ble_gatt_access_ctxt* ctxt); - - std::string getFlag(); - - std::string getNarrative(); - - std::string getManDist(); - - int getProgress(); - - private: -// struct ble_gatt_chr_def characteristicDefinition[5]; -// struct ble_gatt_svc_def serviceDefinition[2]; - - std::string m_flag; - std::string m_narrative; - std::string m_manDist; - int m_progress = 0; - - Pinetime::System::SystemTask& m_system; - }; - } -} diff --git a/sim/components/ble/NimbleController.cpp b/sim/components/ble/NimbleController.cpp index 44a2d9a..8716bf2 100644 --- a/sim/components/ble/NimbleController.cpp +++ b/sim/components/ble/NimbleController.cpp @@ -45,7 +45,6 @@ NimbleController::NimbleController(Pinetime::System::SystemTask& systemTask, // currentTimeService {dateTimeController}, musicService {systemTask}, weatherService {dateTimeController}, - navService {systemTask}, // batteryInformationService {batteryController}, // immediateAlertService {systemTask, notificationManager}, // heartRateService {systemTask, heartRateController}, diff --git a/sim/host/ble_gatt.cpp b/sim/host/ble_gatt.cpp index e2bf35c..904929e 100644 --- a/sim/host/ble_gatt.cpp +++ b/sim/host/ble_gatt.cpp @@ -69,4 +69,4 @@ ble_gatts_add_svcs(const struct ble_gatt_svc_def *svcs) // ble_hs_unlock(); // return rc; return 0; -} \ No newline at end of file +} diff --git a/sim/host/ble_gatt.h b/sim/host/ble_gatt.h index 402ffdd..1b48d52 100644 --- a/sim/host/ble_gatt.h +++ b/sim/host/ble_gatt.h @@ -30,6 +30,10 @@ #include //#include "host/ble_att.h" #include "host/ble_uuid.h" +// include from sim for ASSERT +#include "nrf_assert.h" +#include "host/os_mbuf.h" + #ifdef __cplusplus extern "C" { #endif @@ -613,42 +617,6 @@ struct ble_gatt_dsc_def { void *arg; }; -// buffer from os_mbuf.h -/** - * Chained memory buffer. - */ -struct os_mbuf { - /** - * Current pointer to data in the structure - */ - uint8_t *om_data; - /** - * Flags associated with this buffer, see OS_MBUF_F_* defintions - */ - uint8_t om_flags; - /** - * Length of packet header - */ - uint8_t om_pkthdr_len; - /** - * Length of data in this buffer - */ - uint16_t om_len; - - // sim: disabled, not used by InfiniSim - ///** - // * The mbuf pool this mbuf was allocated out of - // */ - //struct os_mbuf_pool *om_omp; - - //SLIST_ENTRY(os_mbuf) om_next; - - /** - * Pointer to the beginning of the data, after this buffer - */ - uint8_t om_databuf[0]; -}; - /** * Context for an access to a GATT characteristic or descriptor. When a client diff --git a/sim/host/ble_uuid.cpp b/sim/host/ble_uuid.cpp new file mode 100644 index 0000000..65145d8 --- /dev/null +++ b/sim/host/ble_uuid.cpp @@ -0,0 +1,27 @@ +/* + * 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_uuid.h" + + +int +ble_uuid_cmp(const ble_uuid_t *uuid1, const ble_uuid_t *uuid2) +{ + return 0; +} diff --git a/sim/host/ble_uuid.h b/sim/host/ble_uuid.h index 80af9f3..05fdf01 100644 --- a/sim/host/ble_uuid.h +++ b/sim/host/ble_uuid.h @@ -80,6 +80,15 @@ typedef union { ble_uuid128_t u128; } ble_uuid_any_t; +/** @brief Compares two Bluetooth UUIDs. + * + * @param uuid1 The first UUID to compare. + * @param uuid2 The second UUID to compare. + * + * @return 0 if the two UUIDs are equal, nonzero if the UUIDs differ. + */ +int ble_uuid_cmp(const ble_uuid_t *uuid1, const ble_uuid_t *uuid2); + #ifdef __cplusplus } #endif diff --git a/sim/host/os_mbuf.cpp b/sim/host/os_mbuf.cpp new file mode 100644 index 0000000..5e8c128 --- /dev/null +++ b/sim/host/os_mbuf.cpp @@ -0,0 +1,27 @@ +/* + * 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/os_mbuf.h" + + +int +os_mbuf_copydata(const struct os_mbuf *m, int off, int len, void *dst) +{ + return 0; +} diff --git a/sim/host/os_mbuf.h b/sim/host/os_mbuf.h new file mode 100644 index 0000000..ee6ec49 --- /dev/null +++ b/sim/host/os_mbuf.h @@ -0,0 +1,126 @@ +/* + * 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. + */ + + +/** + * @addtogroup OSKernel + * @{ + * @defgroup OSMbuf Chained Memory Buffers + * @{ + */ + + +#ifndef _OS_MBUF_H +#define _OS_MBUF_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + + +/** + * A packet header structure that preceeds the mbuf packet headers. + */ +struct os_mbuf_pkthdr { + /** + * Overall length of the packet. + */ + uint16_t omp_len; + /** + * Flags + */ + uint16_t omp_flags; + + // sim: disabled + // STAILQ_ENTRY(os_mbuf_pkthdr) omp_next; +}; + +/** + * Chained memory buffer. + */ +struct os_mbuf { + /** + * Current pointer to data in the structure + */ + uint8_t *om_data; + /** + * Flags associated with this buffer, see OS_MBUF_F_* defintions + */ + uint8_t om_flags; + /** + * Length of packet header + */ + uint8_t om_pkthdr_len; + /** + * Length of data in this buffer + */ + uint16_t om_len; + + // sim: disabled, not used by InfiniSim + ///** + // * The mbuf pool this mbuf was allocated out of + // */ + //struct os_mbuf_pool *om_omp; + + //SLIST_ENTRY(os_mbuf) om_next; + + /** + * Pointer to the beginning of the data, after this buffer + */ + uint8_t om_databuf[0]; +}; + + +/** Get a packet header pointer given an mbuf pointer */ +#define OS_MBUF_PKTHDR(__om) ((struct os_mbuf_pkthdr *) \ + ((uint8_t *)&(__om)->om_data + sizeof(struct os_mbuf))) + +/** + * Gets the length of an entire mbuf chain. The specified mbuf must have a + * packet header. + */ +#define OS_MBUF_PKTLEN(__om) (OS_MBUF_PKTHDR(__om)->omp_len) + +// sim: function from os_mbuf.h +/* + * Copy data from an mbuf chain starting "off" bytes from the beginning, + * continuing for "len" bytes, into the indicated buffer. + * + * @param m The mbuf chain to copy from + * @param off The offset into the mbuf chain to begin copying from + * @param len The length of the data to copy + * @param dst The destination buffer to copy into + * + * @return 0 on success; + * -1 if the mbuf does not contain enough data. + */ +int os_mbuf_copydata(const struct os_mbuf *m, int off, int len, void *dst); + + +#ifdef __cplusplus +} +#endif + +/** + * @} + */ + +#endif diff --git a/sim/nrf_assert.h b/sim/nrf_assert.h index ac5aa76..1391df1 100644 --- a/sim/nrf_assert.h +++ b/sim/nrf_assert.h @@ -1 +1,6 @@ +#pragma once +#include + +#ifndef ASSERT #define ASSERT(expr) assert(expr) +#endif