diff options
27 files changed, 264 insertions, 290 deletions
diff --git a/api/direct_bt/BTTypes.hpp b/api/direct_bt/BTTypes.hpp index 96396d03..641b5e20 100644 --- a/api/direct_bt/BTTypes.hpp +++ b/api/direct_bt/BTTypes.hpp @@ -30,9 +30,9 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <jau/basic_types.hpp> +#include <jau/darray.hpp> #include "OctetTypes.hpp" #include "BTAddress.hpp" @@ -641,7 +641,7 @@ namespace direct_bt { int8_t rssi = 127; // The core spec defines 127 as the "not available" value int8_t tx_power = 127; // The core spec defines 127 as the "not available" value std::shared_ptr<ManufactureSpecificData> msd = nullptr; - std::vector<std::shared_ptr<uuid_t>> services; + jau::darray<std::shared_ptr<uuid_t>> services; uint32_t device_class = 0; AppearanceCat appearance = AppearanceCat::UNKNOWN; POctets hash; @@ -687,7 +687,7 @@ namespace direct_bt { * https://www.bluetooth.com/specifications/archived-specifications/ * </p> */ - static std::vector<std::shared_ptr<EInfoReport>> read_ad_reports(uint8_t const * data, jau::nsize_t const data_length) noexcept; + static jau::darray<std::shared_ptr<EInfoReport>> read_ad_reports(uint8_t const * data, jau::nsize_t const data_length) noexcept; /** * Reads the Extended Inquiry Response (EIR) or Advertising Data (AD) segments @@ -732,7 +732,7 @@ namespace direct_bt { int8_t getTxPower() const noexcept { return tx_power; } std::shared_ptr<ManufactureSpecificData> getManufactureSpecificData() const noexcept { return msd; } - std::vector<std::shared_ptr<uuid_t>> getServices() const noexcept { return services; } + jau::darray<std::shared_ptr<uuid_t>> getServices() const noexcept { return services; } uint32_t getDeviceClass() const noexcept { return device_class; } AppearanceCat getAppearance() const noexcept { return appearance; } diff --git a/api/direct_bt/DBTAdapter.hpp b/api/direct_bt/DBTAdapter.hpp index 7aee541c..32aed98e 100644 --- a/api/direct_bt/DBTAdapter.hpp +++ b/api/direct_bt/DBTAdapter.hpp @@ -30,11 +30,11 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> #include <atomic> +#include <jau/darray.hpp> #include <jau/cow_darray.hpp> #include "DBTTypes.hpp" @@ -220,10 +220,11 @@ namespace direct_bt { std::mutex mtx_single_conn_device; std::condition_variable cv_single_conn_device; - std::vector<std::shared_ptr<DBTDevice>> connectedDevices; - std::vector<std::shared_ptr<DBTDevice>> discoveredDevices; // all discovered devices - std::vector<std::shared_ptr<DBTDevice>> sharedDevices; // All active shared devices. Final holder of DBTDevice lifecycle! - jau::cow_darray<std::shared_ptr<AdapterStatusListener>> statusListenerList; + jau::darray<std::shared_ptr<DBTDevice>> connectedDevices; + jau::darray<std::shared_ptr<DBTDevice>> discoveredDevices; // all discovered devices + jau::darray<std::shared_ptr<DBTDevice>> sharedDevices; // All active shared devices. Final holder of DBTDevice lifecycle! + typedef jau::cow_darray<std::shared_ptr<AdapterStatusListener>> statusListenerList_t; + statusListenerList_t statusListenerList; std::mutex mtx_discoveredDevices; std::mutex mtx_connectedDevices; std::mutex mtx_discovery; @@ -231,8 +232,8 @@ namespace direct_bt { bool validateDevInfo() noexcept; - static std::shared_ptr<DBTDevice> findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, const EUI48 & address, const BDAddressType addressType) noexcept; - std::shared_ptr<DBTDevice> findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, DBTDevice const & device) noexcept; + static std::shared_ptr<DBTDevice> findDevice(jau::darray<std::shared_ptr<DBTDevice>> & devices, const EUI48 & address, const BDAddressType addressType) noexcept; + std::shared_ptr<DBTDevice> findDevice(jau::darray<std::shared_ptr<DBTDevice>> & devices, DBTDevice const & device) noexcept; /** * Closes all device connections, stops discovery and cleans up all references. @@ -255,7 +256,7 @@ namespace direct_bt { friend bool DBTDevice::updatePairingState(std::shared_ptr<DBTDevice> sthis, const MgmtEvent& evt, const HCIStatusCode evtStatus, SMPPairingState claimed_state) noexcept; friend void DBTDevice::hciSMPMsgCallback(std::shared_ptr<DBTDevice> sthis, const SMPPDUMsg& msg, const HCIACLData::l2cap_frame& source) noexcept; friend void DBTDevice::processDeviceReady(std::shared_ptr<DBTDevice> sthis, const uint64_t timestamp); - friend std::vector<std::shared_ptr<GATTService>> DBTDevice::getGATTServices() noexcept; + friend jau::darray<std::shared_ptr<GATTService>> DBTDevice::getGATTServices() noexcept; bool lockConnect(const DBTDevice & device, const bool wait, const SMPIOCapability io_cap) noexcept; bool unlockConnect(const DBTDevice & device) noexcept; @@ -651,7 +652,7 @@ namespace direct_bt { * use 'DeviceStatusListener::deviceFound(..)' callback. * </p> */ - std::vector<std::shared_ptr<DBTDevice>> getDiscoveredDevices() const noexcept; + jau::darray<std::shared_ptr<DBTDevice>> getDiscoveredDevices() const noexcept; /** Discards all discovered devices. Returns number of removed discovered devices. */ int removeDiscoveredDevices() noexcept; diff --git a/api/direct_bt/DBTDevice.hpp b/api/direct_bt/DBTDevice.hpp index f40411c7..b1b111d4 100644 --- a/api/direct_bt/DBTDevice.hpp +++ b/api/direct_bt/DBTDevice.hpp @@ -30,10 +30,11 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> +#include <jau/darray.hpp> + #include "DBTTypes.hpp" #include "HCIIoctl.hpp" @@ -68,7 +69,7 @@ namespace direct_bt { jau::relaxed_atomic_uint16 hciConnHandle; jau::ordered_atomic<LEFeatures, std::memory_order_relaxed> le_features; std::shared_ptr<ManufactureSpecificData> advMSD = nullptr; - std::vector<std::shared_ptr<uuid_t>> advServices; + jau::darray<std::shared_ptr<uuid_t>> advServices; #if SMP_SUPPORTED_BY_OS std::shared_ptr<SMPHandler> smpHandler = nullptr; std::recursive_mutex mtx_smpHandler; @@ -118,7 +119,7 @@ namespace direct_bt { /** Add advertised service (GAP discovery) */ bool addAdvService(std::shared_ptr<uuid_t> const &uuid) noexcept; /** Add advertised service (GAP discovery) */ - bool addAdvServices(std::vector<std::shared_ptr<uuid_t>> const & services) noexcept; + bool addAdvServices(jau::darray<std::shared_ptr<uuid_t>> const & services) noexcept; /** * Find advertised service (GAP discovery) index * @return index >= 0 if found, otherwise -1 @@ -285,7 +286,7 @@ namespace direct_bt { * use {@link #getGATTServices()}. * </p> */ - std::vector<std::shared_ptr<uuid_t>> getAdvertisedServices() const noexcept; + jau::darray<std::shared_ptr<uuid_t>> getAdvertisedServices() const noexcept; std::string toString() const noexcept override { return toString(false); } @@ -739,7 +740,7 @@ namespace direct_bt { * In case no GATT connection has been established it will be created via connectGATT(). * </p> */ - std::vector<std::shared_ptr<GATTService>> getGATTServices() noexcept; + jau::darray<std::shared_ptr<GATTService>> getGATTServices() noexcept; /** * Returns the matching GATTService for the given uuid. diff --git a/api/direct_bt/DBTManager.hpp b/api/direct_bt/DBTManager.hpp index d3694a3f..f158f89d 100644 --- a/api/direct_bt/DBTManager.hpp +++ b/api/direct_bt/DBTManager.hpp @@ -29,8 +29,6 @@ #include <cstring> #include <string> #include <cstdint> -#include <array> -#include <vector> #include <mutex> #include <atomic> @@ -39,6 +37,7 @@ #include <jau/environment.hpp> #include <jau/ringbuffer.hpp> #include <jau/java_uplink.hpp> +#include <jau/darray.hpp> #include <jau/cow_darray.hpp> #include "BTTypes.hpp" @@ -208,7 +207,7 @@ namespace direct_bt { WhitelistElem(uint16_t dev_id_, BDAddressAndType address_and_type_, HCIWhitelistConnectType ctype_) : dev_id(dev_id_), address_and_type(address_and_type_), ctype(ctype_) { } }; - std::vector<std::shared_ptr<WhitelistElem>> whitelist; + jau::darray<std::shared_ptr<WhitelistElem>> whitelist; const MgmtEnv & env; const BTMode defaultBTMode; @@ -238,14 +237,15 @@ namespace direct_bt { ChangedAdapterSetCallbackList mgmtChangedAdapterSetCallbackList; - jau::cow_darray<std::shared_ptr<AdapterInfo>> adapterInfos; + typedef jau::cow_darray<std::shared_ptr<AdapterInfo>> adapterInfos_t; + adapterInfos_t adapterInfos; /** * Using defaultIOCapability on added AdapterInfo. * Sharing same dev_id <-> index mapping of adapterInfos using findAdapterInfoIndex(). * Piggy back reusing adapterInfos.get_write_mutex(). */ - std::vector<SMPIOCapability> adapterIOCapability; + jau::darray<SMPIOCapability> adapterIOCapability; void mgmtReaderThreadImpl() noexcept; diff --git a/api/direct_bt/GATTCharacteristic.hpp b/api/direct_bt/GATTCharacteristic.hpp index a7676b05..c663be02 100644 --- a/api/direct_bt/GATTCharacteristic.hpp +++ b/api/direct_bt/GATTCharacteristic.hpp @@ -30,7 +30,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> #include <atomic> @@ -105,7 +104,7 @@ namespace direct_bt { static std::string getPropertyString(const PropertyBitVal prop) noexcept; static std::string getPropertiesString(const PropertyBitVal properties) noexcept; - static std::vector<std::unique_ptr<std::string>> getPropertiesStringList(const PropertyBitVal properties) noexcept; + static jau::darray<std::unique_ptr<std::string>> getPropertiesStringList(const PropertyBitVal properties) noexcept; /** * Characteristics's Service Handle - key to service's handle range, retrieved from Characteristics data. @@ -138,7 +137,7 @@ namespace direct_bt { std::unique_ptr<const uuid_t> value_type; /** List of Characteristic Descriptions as shared reference */ - std::vector<GATTDescriptorRef> descriptorList; + jau::darray<GATTDescriptorRef> descriptorList; /* Optional Client Characteristic Configuration index within descriptorList */ int clientCharacteristicsConfigIndex = -1; diff --git a/api/direct_bt/GATTDescriptor.hpp b/api/direct_bt/GATTDescriptor.hpp index fb852113..0ee77667 100644 --- a/api/direct_bt/GATTDescriptor.hpp +++ b/api/direct_bt/GATTDescriptor.hpp @@ -30,7 +30,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> #include <atomic> diff --git a/api/direct_bt/GATTHandler.hpp b/api/direct_bt/GATTHandler.hpp index 2dd53019..1e2767e8 100644 --- a/api/direct_bt/GATTHandler.hpp +++ b/api/direct_bt/GATTHandler.hpp @@ -30,7 +30,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> #include <atomic> @@ -173,11 +172,12 @@ namespace direct_bt { /** send immediate confirmation of indication events from device, defaults to true. */ jau::relaxed_atomic_bool sendIndicationConfirmation = true; - jau::cow_darray<std::shared_ptr<GATTCharacteristicListener>> characteristicListenerList; + typedef jau::cow_darray<std::shared_ptr<GATTCharacteristicListener>> characteristicListenerList_t; + characteristicListenerList_t characteristicListenerList; uint16_t serverMTU; std::atomic<uint16_t> usedMTU; // concurrent use in ctor(set), send and l2capReaderThreadImpl - std::vector<GATTServiceRef> services; + jau::darray<GATTServiceRef> services; std::shared_ptr<GattGenericAccessSvc> genericAccess = nullptr; bool validateConnected() noexcept; @@ -281,7 +281,7 @@ namespace direct_bt { * Returns nullptr if not found. * </p> */ - GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, std::vector<GATTServiceRef> &services) noexcept; + GATTCharacteristicRef findCharacterisicsByValueHandle(const uint16_t charValueHandle, jau::darray<GATTServiceRef> &services) noexcept; /** * Find and return the GATTCharacterisicsDecl within given primary service @@ -303,7 +303,7 @@ namespace direct_bt { * @param shared_this shared pointer of this instance, used to forward a weak_ptr to GATTService for back-reference. Reference is validated. * @return GATTHandler's internal GATTService vector of discovered services */ - std::vector<GATTServiceRef> & discoverCompletePrimaryServices(std::shared_ptr<GATTHandler> shared_this); + jau::darray<GATTServiceRef> & discoverCompletePrimaryServices(std::shared_ptr<GATTHandler> shared_this); /** * Returns a reference of the internal kept GATTService list. @@ -311,7 +311,7 @@ namespace direct_bt { * The internal list will be populated via {@link #discoverCompletePrimaryServices()}. * </p> */ - inline std::vector<GATTServiceRef> & getServices() noexcept { return services; } + inline jau::darray<GATTServiceRef> & getServices() noexcept { return services; } /** * Returns the internal kept shared GattGenericAccessSvc instance. @@ -330,7 +330,7 @@ namespace direct_bt { * @param result vector containing all discovered primary services * @return true on success, otherwise false */ - bool discoverPrimaryServices(std::shared_ptr<GATTHandler> shared_this, std::vector<GATTServiceRef> & result); + bool discoverPrimaryServices(std::shared_ptr<GATTHandler> shared_this, jau::darray<GATTServiceRef> & result); /** * Discover all characteristics of a service and declaration attributes _only_. @@ -518,11 +518,11 @@ namespace direct_bt { /** Higher level semantic functionality **/ /*****************************************************/ - std::shared_ptr<GattGenericAccessSvc> getGenericAccess(std::vector<GATTServiceRef> & primServices); - std::shared_ptr<GattGenericAccessSvc> getGenericAccess(std::vector<GATTCharacteristicRef> & genericAccessCharDeclList); + std::shared_ptr<GattGenericAccessSvc> getGenericAccess(jau::darray<GATTServiceRef> & primServices); + std::shared_ptr<GattGenericAccessSvc> getGenericAccess(jau::darray<GATTCharacteristicRef> & genericAccessCharDeclList); - std::shared_ptr<GattDeviceInformationSvc> getDeviceInformation(std::vector<GATTServiceRef> & primServices); - std::shared_ptr<GattDeviceInformationSvc> getDeviceInformation(std::vector<GATTCharacteristicRef> & deviceInfoCharDeclList); + std::shared_ptr<GattDeviceInformationSvc> getDeviceInformation(jau::darray<GATTServiceRef> & primServices); + std::shared_ptr<GattDeviceInformationSvc> getDeviceInformation(jau::darray<GATTCharacteristicRef> & deviceInfoCharDeclList); /** * Issues a ping to the device, validating whether it is still reachable. diff --git a/api/direct_bt/GATTNumbers.hpp b/api/direct_bt/GATTNumbers.hpp index 437a757c..8b23ce38 100644 --- a/api/direct_bt/GATTNumbers.hpp +++ b/api/direct_bt/GATTNumbers.hpp @@ -29,6 +29,7 @@ #include <cstdint> #include <jau/basic_types.hpp> +#include <jau/darray.hpp> #include "UUID.hpp" #include "OctetTypes.hpp" @@ -184,7 +185,7 @@ struct GattCharacteristicSpec { BroadcastIdx }; /** Aggregated in PropertySpecIdx order */ - const std::vector<GattCharacteristicPropertySpec> propertySpec; + const jau::darray<GattCharacteristicPropertySpec> propertySpec; const GattClientCharacteristicConfigSpec clientConfig; @@ -193,7 +194,7 @@ struct GattCharacteristicSpec { struct GattServiceCharacteristic { const GattServiceType service; - const std::vector<GattCharacteristicSpec> characteristics; + const jau::darray<GattCharacteristicSpec> characteristics; std::string toString() const noexcept; }; @@ -206,7 +207,7 @@ struct GattServiceCharacteristic { extern const GattServiceCharacteristic GATT_GENERIC_ACCESS_SRVC; extern const GattServiceCharacteristic GATT_HEALTH_THERMOMETER_SRVC; extern const GattServiceCharacteristic GATT_DEVICE_INFORMATION_SRVC; -extern const std::vector<const GattServiceCharacteristic*> GATT_SERVICES; +extern const jau::darray<const GattServiceCharacteristic*> GATT_SERVICES; /** * Find the GattServiceCharacteristic entry by given uuid16, diff --git a/api/direct_bt/GATTService.hpp b/api/direct_bt/GATTService.hpp index 8506465e..f81f4661 100644 --- a/api/direct_bt/GATTService.hpp +++ b/api/direct_bt/GATTService.hpp @@ -30,12 +30,12 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> #include <atomic> #include <jau/java_uplink.hpp> +#include <jau/darray.hpp> #include "UUID.hpp" #include "BTTypes.hpp" @@ -94,7 +94,7 @@ namespace direct_bt { std::unique_ptr<const uuid_t> type; /** List of Characteristic Declarations as shared reference */ - std::vector<GATTCharacteristicRef> characteristicList; + jau::darray<GATTCharacteristicRef> characteristicList; GATTService(const std::shared_ptr<GATTHandler> &handler_, const bool isPrimary_, const uint16_t startHandle_, const uint16_t endHandle_, std::unique_ptr<const uuid_t> && type_) noexcept diff --git a/api/direct_bt/GATTTypes.hpp b/api/direct_bt/GATTTypes.hpp index 385f44b2..96c8f372 100644 --- a/api/direct_bt/GATTTypes.hpp +++ b/api/direct_bt/GATTTypes.hpp @@ -30,7 +30,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> #include <atomic> diff --git a/api/direct_bt/HCIHandler.hpp b/api/direct_bt/HCIHandler.hpp index b42489c0..eca18053 100644 --- a/api/direct_bt/HCIHandler.hpp +++ b/api/direct_bt/HCIHandler.hpp @@ -29,12 +29,12 @@ #include <cstring> #include <string> #include <cstdint> -#include <array> #include <mutex> #include <atomic> #include <thread> +#include <jau/darray.hpp> #include <jau/environment.hpp> #include <jau/ringbuffer.hpp> #include <jau/java_uplink.hpp> @@ -251,8 +251,8 @@ namespace direct_bt { std::atomic<ScanType> currentScanType; - std::vector<HCIConnectionRef> connectionList; - std::vector<HCIConnectionRef> disconnectCmdList; + jau::darray<HCIConnectionRef> connectionList; + jau::darray<HCIConnectionRef> disconnectCmdList; std::recursive_mutex mtx_connectionList; // Recurses from disconnect -> findTrackerConnection, addOrUpdateTrackerConnection /** Exclusive [le] connection command (status + pending completed) one at a time */ @@ -270,7 +270,7 @@ namespace direct_bt { * @param addrType key to matching connection * @param handle ignored for existing tracker _if_ invalid, i.e. zero. */ - HCIConnectionRef addOrUpdateHCIConnection(std::vector<HCIConnectionRef> &list, + HCIConnectionRef addOrUpdateHCIConnection(jau::darray<HCIConnectionRef> &list, const BDAddressAndType& addressAndType, const uint16_t handle) noexcept; HCIConnectionRef addOrUpdateTrackerConnection(const BDAddressAndType& addressAndType, const uint16_t handle) noexcept { return addOrUpdateHCIConnection(connectionList, addressAndType, handle); @@ -279,7 +279,7 @@ namespace direct_bt { return addOrUpdateHCIConnection(disconnectCmdList, addressAndType, handle); } - HCIConnectionRef findHCIConnection(std::vector<HCIConnectionRef> &list, const BDAddressAndType& addressAndType) noexcept; + HCIConnectionRef findHCIConnection(jau::darray<HCIConnectionRef> &list, const BDAddressAndType& addressAndType) noexcept; HCIConnectionRef findTrackerConnection(const BDAddressAndType& addressAndType) noexcept { return findHCIConnection(connectionList, addressAndType); } @@ -291,7 +291,7 @@ namespace direct_bt { HCIConnectionRef removeTrackerConnection(const HCIConnectionRef conn) noexcept; int countPendingTrackerConnections() noexcept; - HCIConnectionRef removeHCIConnection(std::vector<HCIConnectionRef> &list, const uint16_t handle) noexcept; + HCIConnectionRef removeHCIConnection(jau::darray<HCIConnectionRef> &list, const uint16_t handle) noexcept; HCIConnectionRef removeTrackerConnection(const uint16_t handle) noexcept { return removeHCIConnection(connectionList, handle); } diff --git a/api/direct_bt/L2CAPComm.hpp b/api/direct_bt/L2CAPComm.hpp index 64979acf..01dbbefa 100644 --- a/api/direct_bt/L2CAPComm.hpp +++ b/api/direct_bt/L2CAPComm.hpp @@ -30,7 +30,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <mutex> #include <atomic> diff --git a/api/direct_bt/MgmtTypes.hpp b/api/direct_bt/MgmtTypes.hpp index ade7be2c..d304ad4c 100644 --- a/api/direct_bt/MgmtTypes.hpp +++ b/api/direct_bt/MgmtTypes.hpp @@ -609,7 +609,7 @@ namespace direct_bt { memcpy(pdu.get_wptr_nc(MGMT_HEADER_SIZE+1+2), &key, sizeof(MgmtLinkKeyInfo)); } - MgmtLoadLinkKeyCmd(const uint16_t dev_id, const bool debug_keys, const std::vector<MgmtLinkKeyInfo> &keys) + MgmtLoadLinkKeyCmd(const uint16_t dev_id, const bool debug_keys, const jau::darray<MgmtLinkKeyInfo> &keys) : MgmtCommand(Opcode::LOAD_LINK_KEYS, dev_id, 1 + 2 + keys.size() * sizeof(MgmtLinkKeyInfo)) { jau::nsize_t offset = MGMT_HEADER_SIZE; @@ -667,7 +667,7 @@ namespace direct_bt { memcpy(pdu.get_wptr_nc(MGMT_HEADER_SIZE+2), &key, sizeof(MgmtLongTermKeyInfo)); } - MgmtLoadLongTermKeyCmd(const uint16_t dev_id, const std::vector<MgmtLongTermKeyInfo> &keys) + MgmtLoadLongTermKeyCmd(const uint16_t dev_id, const jau::darray<MgmtLongTermKeyInfo> &keys) : MgmtCommand(Opcode::LOAD_LONG_TERM_KEYS, dev_id, 2 + keys.size() * sizeof(MgmtLongTermKeyInfo)) { jau::nsize_t offset = MGMT_HEADER_SIZE; @@ -757,7 +757,7 @@ namespace direct_bt { memcpy(pdu.get_wptr_nc(MGMT_HEADER_SIZE+2), &key, sizeof(MgmtIdentityResolvingKeyInfo)); } - MgmtLoadIdentityResolvingKeyCmd(const uint16_t dev_id, const std::vector<MgmtIdentityResolvingKeyInfo> &keys) + MgmtLoadIdentityResolvingKeyCmd(const uint16_t dev_id, const jau::darray<MgmtIdentityResolvingKeyInfo> &keys) : MgmtCommand(Opcode::LOAD_IRKS, dev_id, 2 + keys.size() * sizeof(MgmtIdentityResolvingKeyInfo)) { jau::nsize_t offset = MGMT_HEADER_SIZE; @@ -1055,7 +1055,7 @@ namespace direct_bt { memcpy(pdu.get_wptr_nc(MGMT_HEADER_SIZE+2), &connParam, sizeof(MgmtConnParam)); } - MgmtLoadConnParamCmd(const uint16_t dev_id, const std::vector<MgmtConnParam> &connParams) + MgmtLoadConnParamCmd(const uint16_t dev_id, const jau::darray<MgmtConnParam> &connParams) : MgmtCommand(Opcode::LOAD_CONN_PARAM, dev_id, 2 + connParams.size() * sizeof(MgmtConnParam)) { jau::nsize_t offset = MGMT_HEADER_SIZE; diff --git a/api/direct_bt/OctetTypes.hpp b/api/direct_bt/OctetTypes.hpp index c9ac6ca1..bdbc0e2e 100644 --- a/api/direct_bt/OctetTypes.hpp +++ b/api/direct_bt/OctetTypes.hpp @@ -30,7 +30,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <algorithm> #include <mutex> diff --git a/api/direct_bt/SMPHandler.hpp b/api/direct_bt/SMPHandler.hpp index 8e5d32fa..09e4e590 100644 --- a/api/direct_bt/SMPHandler.hpp +++ b/api/direct_bt/SMPHandler.hpp @@ -29,7 +29,6 @@ #include <cstring> #include <string> #include <cstdint> -#include <array> #include <mutex> #include <atomic> @@ -38,6 +37,7 @@ #include <jau/environment.hpp> #include <jau/ringbuffer.hpp> #include <jau/function_def.hpp> +#include <jau/darray.hpp> #include <jau/cow_darray.hpp> #include "UUID.hpp" diff --git a/examples/direct_bt_scanner00/dbt_scanner00.cpp b/examples/direct_bt_scanner00/dbt_scanner00.cpp index cb922ccf..9d6d5b81 100644 --- a/examples/direct_bt_scanner00/dbt_scanner00.cpp +++ b/examples/direct_bt_scanner00/dbt_scanner00.cpp @@ -260,7 +260,7 @@ int main(int argc, char *argv[]) // // GATT Service Processing // - std::vector<GATTServiceRef> primServices = device->getGATTServices(); // implicit GATT connect... + jau::darray<GATTServiceRef> primServices = device->getGATTServices(); // implicit GATT connect... if( primServices.size() > 0 ) { const uint64_t t5 = getCurrentMilliseconds(); { @@ -291,7 +291,7 @@ int main(int argc, char *argv[]) GATTService & primService = *primServices.at(i); fprintf(stderr, " [%2.2d] Service %s\n", (int)i, primService.toString().c_str()); fprintf(stderr, " [%2.2d] Service Characteristics\n", (int)i); - std::vector<GATTCharacteristicRef> & serviceCharacteristics = primService.characteristicList; + jau::darray<GATTCharacteristicRef> & serviceCharacteristics = primService.characteristicList; for(size_t j=0; j<serviceCharacteristics.size(); j++) { GATTCharacteristic & serviceChar = *serviceCharacteristics.at(j); fprintf(stderr, " [%2.2d.%2.2d] Decla: %s\n", (int)i, (int)j, serviceChar.toString().c_str()); diff --git a/examples/direct_bt_scanner01/dbt_scanner01.cpp b/examples/direct_bt_scanner01/dbt_scanner01.cpp index f9c844ff..86bb2627 100644 --- a/examples/direct_bt_scanner01/dbt_scanner01.cpp +++ b/examples/direct_bt_scanner01/dbt_scanner01.cpp @@ -275,7 +275,7 @@ int main(int argc, char *argv[]) gatt->addCharacteristicListener( std::shared_ptr<GATTCharacteristicListener>( new MyGATTEventListener() ) ); - std::vector<GATTServiceRef> & primServices = gatt->discoverCompletePrimaryServices(gatt); + jau::darray<GATTServiceRef> & primServices = gatt->discoverCompletePrimaryServices(gatt); const uint64_t t5 = getCurrentMilliseconds(); { const uint64_t td45 = t5 - t4; // connect -> gatt complete @@ -302,7 +302,7 @@ int main(int argc, char *argv[]) GATTService & primService = *primServices.at(i); fprintf(stderr, " [%2.2d] Service %s\n", (int)i, primService.toString().c_str()); fprintf(stderr, " [%2.2d] Service Characteristics\n", (int)i); - std::vector<GATTCharacteristicRef> & serviceCharacteristics = primService.characteristicList; + jau::darray<GATTCharacteristicRef> & serviceCharacteristics = primService.characteristicList; for(size_t j=0; j<serviceCharacteristics.size() && gatt->isConnected(); j++) { GATTCharacteristic & serviceChar = *serviceCharacteristics.at(j); fprintf(stderr, " [%2.2d.%2.2d] Decla: %s\n", (int)i, (int)j, serviceChar.toString().c_str()); diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index 3119e369..6e5b0ba0 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -27,7 +27,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <fstream> #include <iostream> #include <unordered_set> @@ -36,9 +35,11 @@ #include <cinttypes> #include <pthread.h> +#include <csignal> #include <jau/dfa_utf8_decode.hpp> #include <jau/basic_algos.hpp> +#include <jau/darray.hpp> #include <direct_bt/DirectBT.hpp> @@ -74,7 +75,7 @@ static bool GATT_PING_ENABLED = false; static bool REMOVE_DEVICE = true; static bool USE_WHITELIST = false; -static std::vector<BDAddressAndType> WHITELIST; +static jau::darray<BDAddressAndType> WHITELIST; static std::string charIdentifier = ""; static int charValue = 0; @@ -82,7 +83,7 @@ static int charValue = 0; static bool SHOW_UPDATE_EVENTS = false; static bool QUIET = false; -static std::vector<BDAddressAndType> waitForDevices; +static jau::darray<BDAddressAndType> waitForDevices; const static int NO_PASSKEY = -1; static int pairing_passkey = NO_PASSKEY; @@ -104,7 +105,7 @@ static std::recursive_mutex mtx_devicesProcessing; static std::recursive_mutex mtx_devicesProcessed; static std::unordered_set<BDAddressAndType> devicesProcessed; -bool matches(std::vector<BDAddressAndType> &cont, const BDAddressAndType &mac) { +bool matches(jau::darray<BDAddressAndType> &cont, const BDAddressAndType &mac) { return cont.end() != jau::find_if(cont.begin(), cont.end(), [&](const BDAddressAndType & it)->bool { return it.matches(mac); }); @@ -114,7 +115,7 @@ bool contains(std::unordered_set<direct_bt::BDAddressAndType> &cont, const BDAdd return cont.end() != cont.find(mac); } -void printList(const std::string &msg, std::vector<BDAddressAndType> &cont) { +void printList(const std::string &msg, jau::darray<BDAddressAndType> &cont) { fprintf(stderr, "%s ", msg.c_str()); std::for_each(cont.begin(), cont.end(), [](const BDAddressAndType &mac) { fprintf(stderr, "%s, ", mac.toString().c_str()); }); @@ -140,7 +141,7 @@ static size_t getDeviceProcessedCount() { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor return devicesProcessed.size(); } -static bool allDevicesProcessed(std::vector<BDAddressAndType> &cont) { +static bool allDevicesProcessed(jau::darray<BDAddressAndType> &cont) { const std::lock_guard<std::recursive_mutex> lock(mtx_devicesProcessed); // RAII-style acquire and relinquish via destructor for (auto it = cont.begin(); it != cont.end(); ++it) { if( !contains(devicesProcessed, *it) ) { @@ -537,11 +538,18 @@ static void processReadyDevice(std::shared_ptr<DBTDevice> device) { device->getAdapter().printSharedPtrListOfDevices(); } try { - std::vector<GATTServiceRef> primServices = device->getGATTServices(); + jau::darray<GATTServiceRef> primServices = device->getGATTServices(); if( 0 == primServices.size() ) { fprintf(stderr, "****** Processing Ready Device: getServices() failed %s\n", device->toString().c_str()); goto exit; } + fprintf(stderr, " **** JAU C-01 Service Count %d\n", (int)primServices.size()); + int _i=0; + for(auto it = primServices.begin(); it != primServices.end(); it++) { // JAU FIXME + GATTServiceRef primSrv = *it; + fprintf(stderr, " [%2.2d] JAU C-01 Service %s\n", (int)_i, primSrv->toString().c_str()); + ++_i; + } const uint64_t t5 = getCurrentMilliseconds(); if( !QUIET ) { @@ -600,13 +608,22 @@ static void processReadyDevice(std::shared_ptr<DBTDevice> device) { } } + for(size_t i=0; i<primServices.size(); i++) { // JAU FIXME + GATTServiceRef primServiceRef = primServices.at(i); + fprintf(stderr, " [%2.2d] Service - Ptr %p\n", (int)i, primServiceRef.get()); + if( nullptr != primServiceRef ) { + GATTService & primService = *primServiceRef; + fprintf(stderr, " [%2.2d] Service %s\n", (int)i, primService.toString().c_str()); + } + } + // raise (SIGABRT); // JAU FIXME for(size_t i=0; i<primServices.size(); i++) { GATTService & primService = *primServices.at(i); if( !QUIET ) { - fprintf(stderr, " [%2.2d] Service %s\n", (int)i, primService.toString().c_str()); + // fprintf(stderr, " [%2.2d] Service %s\n", (int)i, primService.toString().c_str()); fprintf(stderr, " [%2.2d] Service Characteristics\n", (int)i); } - std::vector<GATTCharacteristicRef> & serviceCharacteristics = primService.characteristicList; + jau::darray<GATTCharacteristicRef> & serviceCharacteristics = primService.characteristicList; for(size_t j=0; j<serviceCharacteristics.size(); j++) { GATTCharacteristic & serviceChar = *serviceCharacteristics.at(j); if( !QUIET ) { @@ -621,7 +638,7 @@ static void processReadyDevice(std::shared_ptr<DBTDevice> device) { } } } - std::vector<GATTDescriptorRef> & charDescList = serviceChar.descriptorList; + jau::darray<GATTDescriptorRef> & charDescList = serviceChar.descriptorList; for(size_t k=0; k<charDescList.size(); k++) { GATTDescriptor & charDesc = *charDescList.at(k); if( !QUIET ) { @@ -766,15 +783,16 @@ static std::shared_ptr<DBTAdapter> createAdapter(const int dev_id0, const bool v return adapter; } -static jau::cow_darray<std::shared_ptr<DBTAdapter>> adapterList; +typedef jau::cow_darray<std::shared_ptr<DBTAdapter>> cow_adapter_list_t; +static cow_adapter_list_t adapterList; static std::shared_ptr<DBTAdapter> getAdapter(const uint16_t dev_id) { - std::shared_ptr<jau::darray<std::shared_ptr<DBTAdapter>>> snapshot = adapterList.get_snapshot(); - auto begin = snapshot->begin(); - auto it = std::find_if(begin, snapshot->end(), [&](std::shared_ptr<DBTAdapter> const& p) -> bool { + cow_adapter_list_t::const_iterator begin = adapterList.cbegin(); + cow_adapter_list_t::const_iterator end = begin.cend(); + auto it = jau::find_if(begin, end, [&](std::shared_ptr<DBTAdapter> const& p) -> bool { return p->dev_id == dev_id; }); - if ( it == std::end(*snapshot) ) { + if ( it == end ) { return nullptr; } else { return *it; @@ -782,6 +800,7 @@ static std::shared_ptr<DBTAdapter> getAdapter(const uint16_t dev_id) { } static std::shared_ptr<DBTAdapter> removeAdapter(const uint16_t dev_id) { std::shared_ptr<DBTAdapter> res = nullptr; + cow_adapter_list_t::iterator begin = adapterList.begin(); // lock, copy_store and set_store @ dtor! const std::lock_guard<std::recursive_mutex> lock(adapterList.get_write_mutex()); std::shared_ptr<jau::darray<std::shared_ptr<DBTAdapter>>> store = adapterList.copy_store(); for(auto it = store->begin(); it != store->end(); ) { @@ -839,7 +858,7 @@ void test() { ) { fprintf(stderr, "****** EOL Test MULTI_MEASUREMENTS left %d, processed %zu/%zu\n", - MULTI_MEASUREMENTS.load(), getDeviceProcessedCount(), waitForDevices.size()); + MULTI_MEASUREMENTS.load(), getDeviceProcessedCount(), (size_t)waitForDevices.size()); printList("****** WaitForDevice ", waitForDevices); printDevicesProcessed("****** DevicesProcessed "); done = true; @@ -848,7 +867,7 @@ void test() { } } - jau::for_each_fidelity(adapterList.cbegin(), adapterList.cend(), [](std::shared_ptr<DBTAdapter>& adapter) { + jau::for_each_fidelity(adapterList, [](std::shared_ptr<DBTAdapter>& adapter) { fprintf(stderr, "****** EOL Adapter's Devices - pre close: %s\n", adapter->toString().c_str()); adapter->printSharedPtrListOfDevices(); adapter->close(); diff --git a/src/direct_bt/BTTypes.cpp b/src/direct_bt/BTTypes.cpp index 1e8e7daf..ad6b0695 100644 --- a/src/direct_bt/BTTypes.cpp +++ b/src/direct_bt/BTTypes.cpp @@ -27,12 +27,12 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <cstdio> #include <algorithm> #include <jau/debug.hpp> +#include <jau/darray.hpp> #include "BTTypes.hpp" @@ -706,9 +706,9 @@ int EInfoReport::read_data(uint8_t const * data, uint8_t const data_length) noex return count; } -std::vector<std::shared_ptr<EInfoReport>> EInfoReport::read_ad_reports(uint8_t const * data, jau::nsize_t const data_length) noexcept { +jau::darray<std::shared_ptr<EInfoReport>> EInfoReport::read_ad_reports(uint8_t const * data, jau::nsize_t const data_length) noexcept { jau::nsize_t const num_reports = (jau::nsize_t) data[0]; - std::vector<std::shared_ptr<EInfoReport>> ad_reports; + jau::darray<std::shared_ptr<EInfoReport>> ad_reports; if( 0 == num_reports || num_reports > 0x19 ) { DBG_PRINT("AD-Reports: Invalid reports count: %d", num_reports); diff --git a/src/direct_bt/DBTAdapter.cpp b/src/direct_bt/DBTAdapter.cpp index 937c430c..443d5f3b 100644 --- a/src/direct_bt/DBTAdapter.cpp +++ b/src/direct_bt/DBTAdapter.cpp @@ -27,7 +27,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <cstdio> #include <algorithm> @@ -50,9 +49,9 @@ extern "C" { using namespace direct_bt; -std::shared_ptr<DBTDevice> DBTAdapter::findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, const EUI48 & address, const BDAddressType addressType) noexcept { - const size_t size = devices.size(); - for (size_t i = 0; i < size; i++) { +std::shared_ptr<DBTDevice> DBTAdapter::findDevice(jau::darray<std::shared_ptr<DBTDevice>> & devices, const EUI48 & address, const BDAddressType addressType) noexcept { + const jau::nsize_t size = devices.size(); + for (jau::nsize_t i = 0; i < size; i++) { std::shared_ptr<DBTDevice> & e = devices[i]; if ( nullptr != e && address == e->getAddressAndType().address && addressType == e->getAddressAndType().type) { return e; @@ -61,9 +60,9 @@ std::shared_ptr<DBTDevice> DBTAdapter::findDevice(std::vector<std::shared_ptr<DB return nullptr; } -std::shared_ptr<DBTDevice> DBTAdapter::findDevice(std::vector<std::shared_ptr<DBTDevice>> & devices, DBTDevice const & device) noexcept { - const size_t size = devices.size(); - for (size_t i = 0; i < size; i++) { +std::shared_ptr<DBTDevice> DBTAdapter::findDevice(jau::darray<std::shared_ptr<DBTDevice>> & devices, DBTDevice const & device) noexcept { + const jau::nsize_t size = devices.size(); + for (jau::nsize_t i = 0; i < size; i++) { std::shared_ptr<DBTDevice> & e = devices[i]; if ( nullptr != e && device == *e ) { return e; @@ -83,25 +82,25 @@ bool DBTAdapter::addConnectedDevice(const std::shared_ptr<DBTDevice> & device) n bool DBTAdapter::removeConnectedDevice(const DBTDevice & device) noexcept { const std::lock_guard<std::mutex> lock(mtx_connectedDevices); // RAII-style acquire and relinquish via destructor - for (auto it = connectedDevices.begin(); it != connectedDevices.end(); ) { + auto end = connectedDevices.end(); + for (auto it = connectedDevices.begin(); it != end; ++it) { if ( nullptr != *it && device == **it ) { - it = connectedDevices.erase(it); + connectedDevices.erase(it); return true; - } else { - ++it; } } return false; } int DBTAdapter::disconnectAllDevices(const HCIStatusCode reason) noexcept { - std::vector<std::shared_ptr<DBTDevice>> devices; + jau::darray<std::shared_ptr<DBTDevice>> devices; { const std::lock_guard<std::mutex> lock(mtx_connectedDevices); // RAII-style acquire and relinquish via destructor devices = connectedDevices; // copy! } const int count = devices.size(); - for (auto it = devices.begin(); it != devices.end(); ++it) { + auto end = devices.end(); + for (auto it = devices.begin(); it != end; ++it) { if( nullptr != *it ) { (*it)->disconnect(reason); // will erase device from list via removeConnectedDevice(..) above } @@ -550,22 +549,18 @@ bool DBTAdapter::removeStatusListener(const AdapterStatusListener * l) { if( nullptr == l ) { throw jau::IllegalArgumentException("DBTAdapterStatusListener ref is null", E_FILE_LINE); } - const std::lock_guard<std::recursive_mutex> lock(statusListenerList.get_write_mutex()); - std::shared_ptr<jau::darray<std::shared_ptr<AdapterStatusListener>>> store = statusListenerList.copy_store(); - int count = 0; - for(auto it = store->begin(); it != store->end(); ) { - if ( **it == *l ) { - it = store->erase(it); - count++; - break; - } else { - ++it; + { + auto begin = statusListenerList.begin(); // lock mutex and copy_store + while ( !begin.is_end() ) { + if ( **begin == *l ) { + begin.erase(); + begin.write_back(); + return true; + } else { + ++begin; + } } } - if( 0 < count ) { - statusListenerList.set_store(std::move(store)); - return true; - } return false; } @@ -785,9 +780,9 @@ int DBTAdapter::removeDiscoveredDevices() noexcept { return res; } -std::vector<std::shared_ptr<DBTDevice>> DBTAdapter::getDiscoveredDevices() const noexcept { +jau::darray<std::shared_ptr<DBTDevice>> DBTAdapter::getDiscoveredDevices() const noexcept { const std::lock_guard<std::mutex> lock(const_cast<DBTAdapter*>(this)->mtx_discoveredDevices); // RAII-style acquire and relinquish via destructor - std::vector<std::shared_ptr<DBTDevice>> res = discoveredDevices; + jau::darray<std::shared_ptr<DBTDevice>> res = discoveredDevices; return res; } @@ -840,7 +835,7 @@ std::string DBTAdapter::toString(bool includeDiscoveredDevices) const noexcept { ", scanType[native "+getScanTypeString(hci.getCurrentScanType())+", meta "+getScanTypeString(currentMetaScanType)+"]" ", valid "+std::to_string(isValid())+", open[mgmt, "+std::to_string(mgmt.isOpen())+", hci "+std::to_string(hci.isOpen())+ "], "+javaObjectToString()+"]"); - std::vector<std::shared_ptr<DBTDevice>> devices = getDiscoveredDevices(); + jau::darray<std::shared_ptr<DBTDevice>> devices = getDiscoveredDevices(); if( includeDiscoveredDevices && devices.size() > 0 ) { out.append("\n"); for(auto it = devices.begin(); it != devices.end(); it++) { @@ -859,7 +854,7 @@ void DBTAdapter::sendAdapterSettingsChanged(const AdapterSetting old_settings_, const uint64_t timestampMS) noexcept { int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { l->adapterSettingsChanged(*this, old_settings_, current_settings, changes, timestampMS); } catch (std::exception &e) { @@ -886,7 +881,7 @@ void DBTAdapter::sendAdapterSettingsInitial(AdapterStatusListener & asl, const u void DBTAdapter::sendDeviceUpdated(std::string cause, std::shared_ptr<DBTDevice> device, uint64_t timestamp, EIRDataType updateMask) noexcept { int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { if( l->matchDevice(*device) ) { l->deviceUpdated(device, updateMask, timestamp); @@ -958,7 +953,7 @@ bool DBTAdapter::mgmtEvDeviceDiscoveringAny(const MgmtEvent& e, const bool hciSo checkDiscoveryState(); int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { l->discoveringChanged(*this, currentMetaScanType, eventScanType, eventEnabled, keep_le_scan_alive, event.getTimestamp()); } catch (std::exception &except) { @@ -1100,7 +1095,7 @@ bool DBTAdapter::mgmtEvDeviceConnectedHCI(const MgmtEvent& e) noexcept { device->notifyConnected(device, event.getHCIHandle(), io_cap_conn); int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { if( l->matchDevice(*device) ) { if( EIRDataType::NONE != updateMask ) { @@ -1136,7 +1131,7 @@ bool DBTAdapter::mgmtEvConnectFailedHCI(const MgmtEvent& e) noexcept { removeConnectedDevice(*device); int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { if( l->matchDevice(*device) ) { l->deviceDisconnected(device, event.getHCIStatus(), handle, event.getTimestamp()); @@ -1226,7 +1221,7 @@ bool DBTAdapter::mgmtEvDeviceDisconnectedHCI(const MgmtEvent& e) noexcept { removeConnectedDevice(*device); int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { if( l->matchDevice(*device) ) { l->deviceDisconnected(device, event.getHCIReason(), event.getHCIHandle(), event.getTimestamp()); @@ -1334,7 +1329,7 @@ bool DBTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept { dev->getAddressAndType().toString().c_str(), eir->toString().c_str()); int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { if( l->matchDevice(*dev) ) { l->deviceFound(dev, eir->getTimestamp()); @@ -1363,7 +1358,7 @@ bool DBTAdapter::mgmtEvDeviceFoundHCI(const MgmtEvent& e) noexcept { dev->getAddressAndType().toString().c_str(), eir->toString().c_str()); int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { if( l->matchDevice(*dev) ) { l->deviceFound(dev, eir->getTimestamp()); @@ -1455,7 +1450,7 @@ bool DBTAdapter::hciSMPMsgCallback(const BDAddressAndType & addressAndType, void DBTAdapter::sendDevicePairingState(std::shared_ptr<DBTDevice> device, const SMPPairingState state, const PairingMode mode, uint64_t timestamp) noexcept { int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { if( l->matchDevice(*device) ) { l->devicePairingState(device, state, mode, timestamp); @@ -1471,7 +1466,7 @@ void DBTAdapter::sendDevicePairingState(std::shared_ptr<DBTDevice> device, const void DBTAdapter::sendDeviceReady(std::shared_ptr<DBTDevice> device, uint64_t timestamp) noexcept { int i=0; - jau::for_each_fidelity(statusListenerList.cbegin(), statusListenerList.cend(), [&](std::shared_ptr<AdapterStatusListener> &l) { + jau::for_each_fidelity(statusListenerList, [&](std::shared_ptr<AdapterStatusListener> &l) { try { // Only issue if valid && received connected confirmation (HCI) && not have called disconnect yet. if( device->isValid() && device->getConnected() && device->allowDisconnect ) { diff --git a/src/direct_bt/DBTDevice.cpp b/src/direct_bt/DBTDevice.cpp index 5a558751..c0b1fbea 100644 --- a/src/direct_bt/DBTDevice.cpp +++ b/src/direct_bt/DBTDevice.cpp @@ -27,7 +27,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <cstdio> #include <algorithm> @@ -95,7 +94,7 @@ bool DBTDevice::addAdvService(std::shared_ptr<uuid_t> const &uuid) noexcept } return false; } -bool DBTDevice::addAdvServices(std::vector<std::shared_ptr<uuid_t>> const & services) noexcept +bool DBTDevice::addAdvServices(jau::darray<std::shared_ptr<uuid_t>> const & services) noexcept { bool res = false; for(size_t j=0; j<services.size(); j++) { @@ -127,7 +126,7 @@ std::shared_ptr<ManufactureSpecificData> const DBTDevice::getManufactureSpecific return advMSD; } -std::vector<std::shared_ptr<uuid_t>> DBTDevice::getAdvertisedServices() const noexcept { +jau::darray<std::shared_ptr<uuid_t>> DBTDevice::getAdvertisedServices() const noexcept { const std::lock_guard<std::recursive_mutex> lock(const_cast<DBTDevice*>(this)->mtx_data); // RAII-style acquire and relinquish via destructor return advServices; } @@ -1311,13 +1310,13 @@ std::shared_ptr<GATTHandler> DBTDevice::getGATTHandler() noexcept { return gattHandler; } -std::vector<std::shared_ptr<GATTService>> DBTDevice::getGATTServices() noexcept { +jau::darray<std::shared_ptr<GATTService>> DBTDevice::getGATTServices() noexcept { std::shared_ptr<GATTHandler> gh = getGATTHandler(); if( nullptr == gh ) { ERR_PRINT("DBTDevice::getGATTServices: GATTHandler nullptr"); - return std::vector<std::shared_ptr<GATTService>>(); + return jau::darray<std::shared_ptr<GATTService>>(); } - std::vector<std::shared_ptr<GATTService>> & gattServices = gh->getServices(); // reference of the GATTHandler's list + jau::darray<std::shared_ptr<GATTService>> gattServices = gh->getServices(); if( gattServices.size() > 0 ) { // reuse previous discovery result return gattServices; } @@ -1350,7 +1349,7 @@ std::vector<std::shared_ptr<GATTService>> DBTDevice::getGATTServices() noexcept } std::shared_ptr<GATTService> DBTDevice::findGATTService(std::shared_ptr<uuid_t> const &uuid) { - const std::vector<std::shared_ptr<GATTService>> & gattServices = getGATTServices(); // reference of the GATTHandler's list + const jau::darray<std::shared_ptr<GATTService>> & gattServices = getGATTServices(); // reference of the GATTHandler's list const size_t size = gattServices.size(); for (size_t i = 0; i < size; i++) { const std::shared_ptr<GATTService> & e = gattServices[i]; diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp index aa996c33..bbfaab89 100644 --- a/src/direct_bt/DBTManager.cpp +++ b/src/direct_bt/DBTManager.cpp @@ -27,7 +27,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <cstdio> #include <algorithm> @@ -156,7 +155,7 @@ void DBTManager::sendMgmtEvent(const MgmtEvent& event) noexcept { MgmtAdapterEventCallbackList & mgmtEventCallbackList = mgmtAdapterEventCallbackLists[static_cast<uint16_t>(event.getOpcode())]; int invokeCount = 0; - jau::for_each_fidelity(mgmtEventCallbackList.cbegin(), mgmtEventCallbackList.cend(), [&](MgmtAdapterEventCallback &cb) { + jau::for_each_fidelity(mgmtEventCallbackList, [&](MgmtAdapterEventCallback &cb) { if( 0 > cb.getDevID() || dev_id == cb.getDevID() ) { try { cb.getCallback().invoke(event); @@ -478,15 +477,11 @@ next1: goto fail; } { - // Not required: CTOR: const std::lock_guard<std::recursive_mutex> lock(adapterInfos.get_write_mutex()); - // Not required: CTOR: std::shared_ptr<std::vector<std::shared_ptr<AdapterInfo>>> store = adapterInfos.copy_store(); - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> snapshot = adapterInfos.get_snapshot(); - for(int i=0; i < num_adapter; i++) { const uint16_t dev_id = jau::get_uint16(data, 2+i*2, true /* littleEndian */); std::shared_ptr<AdapterInfo> adapterInfo = initAdapter(dev_id, defaultBTMode); if( nullptr != adapterInfo ) { - snapshot->push_back(adapterInfo); + adapterInfos.push_back(adapterInfo); adapterIOCapability.push_back(defaultIOCapability); DBG_PRINT("DBTManager::adapters %d/%d: dev_id %d: %s", i, num_adapter, dev_id, adapterInfo->toString().c_str()); } else { @@ -561,7 +556,7 @@ void DBTManager::close() noexcept { removeAllDevicesFromWhitelist(); clearAllCallbacks(); - jau::for_each(adapterInfos.cbegin(), adapterInfos.cend(), [&](const std::shared_ptr<AdapterInfo> & a) { + jau::for_each_const(adapterInfos, [&](const std::shared_ptr<AdapterInfo> & a) { shutdownAdapter(a->dev_id); }); adapterInfos.clear(); @@ -611,81 +606,63 @@ void DBTManager::close() noexcept { } int DBTManager::findAdapterInfoIndex(const uint16_t dev_id) const noexcept { - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> snapshot = adapterInfos.get_snapshot(); - auto begin = snapshot->begin(); - auto it = std::find_if(begin, snapshot->end(), [&](std::shared_ptr<AdapterInfo> const& p) -> bool { - return p->dev_id == dev_id; - }); - if ( it == std::end(*snapshot) ) { - return -1; - } else { - return it - begin; + auto it = adapterInfos.cbegin(); + for (; !it.is_end(); ++it) { + if ( (*it)->dev_id == dev_id ) { + return it - it.cend(); + } } + return -1; } int DBTManager::findAdapterInfoDevId(const EUI48 &mac) const noexcept { - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> snapshot = adapterInfos.get_snapshot(); - auto begin = snapshot->begin(); - auto it = std::find_if(begin, snapshot->end(), [&](std::shared_ptr<AdapterInfo> const& p) -> bool { - return p->address == mac; - }); - if ( it == std::end(*snapshot) ) { - return -1; - } else { - return (*it)->dev_id; + auto it = adapterInfos.cbegin(); + for (; !it.is_end(); ++it) { + if ( (*it)->address == mac ) { + return (*it)->dev_id; + } } + return -1; } std::shared_ptr<AdapterInfo> DBTManager::findAdapterInfo(const EUI48 &mac) const noexcept { - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> snapshot = adapterInfos.get_snapshot(); - auto begin = snapshot->begin(); - auto it = std::find_if(begin, snapshot->end(), [&](std::shared_ptr<AdapterInfo> const& p) -> bool { - return p->address == mac; - }); - if ( it == std::end(*snapshot) ) { - return nullptr; - } else { - return *it; + auto it = adapterInfos.cbegin(); + for (; !it.is_end(); ++it) { + if ( (*it)->address == mac ) { + return *it; + } } + return nullptr; } std::shared_ptr<AdapterInfo> DBTManager::getAdapterInfo(const uint16_t dev_id) const noexcept { - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> snapshot = adapterInfos.get_snapshot(); - auto begin = snapshot->begin(); - auto it = std::find_if(begin, snapshot->end(), [&](std::shared_ptr<AdapterInfo> const& p) -> bool { - return p->dev_id == dev_id; - }); - if ( it == std::end(*snapshot) ) { - return nullptr; - } else { - return *it; + auto it = adapterInfos.cbegin(); + for (; !it.is_end(); ++it) { + if ( (*it)->dev_id == dev_id ) { + return *it; + } } + return nullptr; } bool DBTManager::addAdapterInfo(std::shared_ptr<AdapterInfo> ai) noexcept { - const std::lock_guard<std::recursive_mutex> lock(adapterInfos.get_write_mutex()); - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> store = adapterInfos.copy_store(); - - auto begin = store->begin(); - auto it = std::find_if(begin, store->end(), [&](std::shared_ptr<AdapterInfo> const& p) -> bool { - return p->dev_id == ai->dev_id; - }); - if ( it != std::end(*store) ) { - // already existing - return false; + auto it = adapterInfos.begin(); // lock mutex and copy_store + for (; !it.is_end(); ++it) { + if ( (*it)->dev_id == ai->dev_id ) { + it.push_back(ai); + it.write_back(); + adapterIOCapability.push_back(defaultIOCapability); + return true; + } } - store->push_back(ai); - adapterInfos.set_store(std::move(store)); - adapterIOCapability.push_back(defaultIOCapability); - return true; + // already existing + return false; } std::shared_ptr<AdapterInfo> DBTManager::removeAdapterInfo(const uint16_t dev_id) noexcept { - const std::lock_guard<std::recursive_mutex> lock(adapterInfos.get_write_mutex()); - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> store = adapterInfos.copy_store(); - - for(auto it = store->begin(); it != store->end(); ) { + typename adapterInfos_t::iterator it = adapterInfos.begin(); // lock mutex and copy_store + while ( !it.is_end() ) { std::shared_ptr<AdapterInfo> & ai = *it; if( ai->dev_id == dev_id ) { - adapterIOCapability.erase( adapterIOCapability.begin() + ( it - store->begin() ) ); - std::shared_ptr<AdapterInfo> res = ai; - it = store->erase(it); - adapterInfos.set_store(std::move(store)); + adapterIOCapability.erase( adapterIOCapability.begin() + ( it - it.begin() ) ); + std::shared_ptr<AdapterInfo> res = ai; // copy + it.erase(); + it.write_back(); return res; } else { ++it; @@ -704,16 +681,13 @@ BTMode DBTManager::getCurrentBTMode(uint16_t dev_id) const noexcept { } std::shared_ptr<AdapterInfo> DBTManager::getDefaultAdapterInfo() const noexcept { - std::shared_ptr<jau::darray<std::shared_ptr<AdapterInfo>>> snapshot = adapterInfos.get_snapshot(); - auto begin = snapshot->begin(); - auto it = std::find_if(begin, snapshot->end(), [](std::shared_ptr<AdapterInfo> const& p) -> bool { - return p->isCurrentSettingBitSet(AdapterSetting::POWERED); - }); - if ( it == std::end(*snapshot) ) { - return nullptr; - } else { - return *it; + auto it = adapterInfos.cbegin(); + for (; !it.is_end(); ++it) { + if( (*it)->isCurrentSettingBitSet(AdapterSetting::POWERED) ) { + return *it; + } } + return nullptr; } int DBTManager::getDefaultAdapterDevID() const noexcept { @@ -727,15 +701,17 @@ int DBTManager::getDefaultAdapterDevID() const noexcept { bool DBTManager::setIOCapability(const uint16_t dev_id, const SMPIOCapability io_cap, SMPIOCapability& pre_io_cap) noexcept { if( SMPIOCapability::UNSET != io_cap ) { #if USE_LINUX_BT_SECURITY - const std::lock_guard<std::recursive_mutex> lock( adapterInfos.get_write_mutex() ); - const int index = findAdapterInfoIndex(dev_id); - if( 0 <= index ) { - const SMPIOCapability o = adapterIOCapability.at(index); - AdapterSetting current_settings { AdapterSetting::NONE }; // throw away return value, unchanged on SET_IO_CAPABILITY - if( setMode(dev_id, MgmtCommand::Opcode::SET_IO_CAPABILITY, direct_bt::number(io_cap), current_settings) ) { - adapterIOCapability.at(index) = io_cap; - pre_io_cap = o; - return true; + auto it = adapterInfos.cbegin(); + for (; !it.is_end(); ++it) { + if( (*it)->dev_id == dev_id ) { + const typename adapterInfos_t::difference_type index = it-it.cbegin(); + const SMPIOCapability o = adapterIOCapability.at(index); + AdapterSetting current_settings { AdapterSetting::NONE }; // throw away return value, unchanged on SET_IO_CAPABILITY + if( setMode(dev_id, MgmtCommand::Opcode::SET_IO_CAPABILITY, direct_bt::number(io_cap), current_settings) ) { + adapterIOCapability.at(index) = io_cap; + pre_io_cap = o; + return true; + } } } #endif @@ -744,10 +720,11 @@ bool DBTManager::setIOCapability(const uint16_t dev_id, const SMPIOCapability io } SMPIOCapability DBTManager::getIOCapability(const uint16_t dev_id) const noexcept { - const std::lock_guard<std::recursive_mutex> lock( const_cast<DBTManager *>(this)->adapterInfos.get_write_mutex() ); - const int index = findAdapterInfoIndex(dev_id); - if( 0 <= index ) { - return adapterIOCapability.at(index); + auto it = adapterInfos.cbegin(); + for (; !it.is_end(); ++it) { + if( (*it)->dev_id == dev_id ) { + return adapterIOCapability.at(it-it.cbegin()); + } } return SMPIOCapability::UNSET; } @@ -958,12 +935,11 @@ MgmtStatus DBTManager::unpairDevice(const uint16_t dev_id, const BDAddressAndTyp } bool DBTManager::isDeviceWhitelisted(const uint16_t dev_id, const BDAddressAndType & addressAndType) noexcept { - for(auto it = whitelist.begin(); it != whitelist.end(); ) { + auto it = whitelist.cbegin(); + for( auto end = whitelist.cend(); it != end; ++it) { std::shared_ptr<WhitelistElem> wle = *it; if( wle->dev_id == dev_id && wle->address_and_type == addressAndType ) { return true; - } else { - ++it; } } return false; @@ -990,21 +966,23 @@ bool DBTManager::addDeviceToWhitelist(const uint16_t dev_id, const BDAddressAndT int DBTManager::removeAllDevicesFromWhitelist() noexcept { #if 0 - std::vector<std::shared_ptr<WhitelistElem>> whitelist_copy = whitelist; + jau::darray<std::shared_ptr<WhitelistElem>> whitelist_copy = whitelist; int count = 0; DBG_PRINT("DBTManager::removeAllDevicesFromWhitelist.A: Start %zd elements", whitelist_copy.size()); - for(auto it = whitelist_copy.begin(); it != whitelist_copy.end(); ++it) { + for(auto it = whitelist_copy.cbegin(); it != whitelist_copy.cend(); ++it) { std::shared_ptr<WhitelistElem> wle = *it; removeDeviceFromWhitelist(wle->dev_id, wle->address, wle->address_type); - count++; + ++count; } #else - int count = whitelist.size(); + int count = 0; DBG_PRINT("DBTManager::removeAllDevicesFromWhitelist.B: Start %d elements", count); whitelist.clear(); - jau::for_each(adapterInfos.cbegin(), adapterInfos.cend(), [&](const std::shared_ptr<AdapterInfo> & a) { - removeDeviceFromWhitelist(a->dev_id, BDAddressAndType::ANY_BREDR_DEVICE); // flush whitelist! + jau::for_each_const(adapterInfos, [&](const std::shared_ptr<AdapterInfo> & a) { + if( removeDeviceFromWhitelist(a->dev_id, BDAddressAndType::ANY_BREDR_DEVICE) ) { // flush whitelist! + ++count; + } }); #endif @@ -1016,7 +994,8 @@ int DBTManager::removeAllDevicesFromWhitelist() noexcept { bool DBTManager::removeDeviceFromWhitelist(const uint16_t dev_id, const BDAddressAndType & addressAndType) noexcept { // Remove from our local whitelist first { - for(auto it = whitelist.begin(); it != whitelist.end(); ) { + auto it = whitelist.cbegin(); + for( auto end = whitelist.cend(); it != end; ) { std::shared_ptr<WhitelistElem> wle = *it; if( wle->dev_id == dev_id && wle->address_and_type == addressAndType ) { it = whitelist.erase(it); @@ -1163,7 +1142,7 @@ void DBTManager::processAdapterAdded(std::unique_ptr<MgmtEvent> e) noexcept { DBG_PRINT("DBTManager::Adapter[%d] Added: Start %s, added %d", dev_id, ai->toString().c_str(), added); sendMgmtEvent(*e); DBG_PRINT("DBTManager::Adapter[%d] Added: User_ %s", dev_id, ai->toString().c_str()); - jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList.cbegin(), mgmtChangedAdapterSetCallbackList.cend(), [&](ChangedAdapterSetCallback &cb) { + jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) { cb.invoke(true /* added */, *ai); }); DBG_PRINT("DBTManager::Adapter[%d] Added: End__ %s", dev_id, ai->toString().c_str()); @@ -1178,7 +1157,7 @@ void DBTManager::processAdapterRemoved(std::unique_ptr<MgmtEvent> e) noexcept { DBG_PRINT("DBTManager::Adapter[%d] Removed: Start: %s", dev_id, ai->toString().c_str()); sendMgmtEvent(*e); DBG_PRINT("DBTManager::Adapter[%d] Removed: User_: %s", dev_id, ai->toString().c_str()); - jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList.cbegin(), mgmtChangedAdapterSetCallbackList.cend(), [&](ChangedAdapterSetCallback &cb) { + jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) { cb.invoke(false /* added */, *ai); }); DBG_PRINT("DBTManager::Adapter[%d] Removed: End__: %s", dev_id, ai->toString().c_str()); @@ -1223,8 +1202,8 @@ static ChangedAdapterSetCallbackList::equal_comparator _changedAdapterSetCallbac void DBTManager::addChangedAdapterSetCallback(const ChangedAdapterSetCallback & l) { mgmtChangedAdapterSetCallbackList.push_back(l); - jau::for_each(adapterInfos.cbegin(), adapterInfos.cend(), [&](const std::shared_ptr<AdapterInfo>& ai) { - jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList.cbegin(), mgmtChangedAdapterSetCallbackList.cend(), [&](ChangedAdapterSetCallback &cb) { + jau::for_each_const(adapterInfos, [&](const std::shared_ptr<AdapterInfo>& ai) { + jau::for_each_fidelity(mgmtChangedAdapterSetCallbackList, [&](ChangedAdapterSetCallback &cb) { cb.invoke(true /* added */, *ai); }); }); diff --git a/src/direct_bt/GATTCharacteristic.cpp b/src/direct_bt/GATTCharacteristic.cpp index f67a1f1c..4e200204 100644 --- a/src/direct_bt/GATTCharacteristic.cpp +++ b/src/direct_bt/GATTCharacteristic.cpp @@ -90,8 +90,8 @@ std::string GATTCharacteristic::getPropertiesString(const PropertyBitVal propert return out; } -std::vector<std::unique_ptr<std::string>> GATTCharacteristic::getPropertiesStringList(const PropertyBitVal properties) noexcept { - std::vector<std::unique_ptr<std::string>> out; +jau::darray<std::unique_ptr<std::string>> GATTCharacteristic::getPropertiesStringList(const PropertyBitVal properties) noexcept { + jau::darray<std::unique_ptr<std::string>> out; const PropertyBitVal none = static_cast<PropertyBitVal>(0); const uint8_t one = 1; for(int i=0; i<8; i++) { diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp index 253ff2e1..374176f3 100644 --- a/src/direct_bt/GATTHandler.cpp +++ b/src/direct_bt/GATTHandler.cpp @@ -27,7 +27,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <cstdio> #include <algorithm> @@ -123,22 +122,14 @@ bool GATTHandler::removeCharacteristicListener(const GATTCharacteristicListener ERR_PRINT("Given GATTCharacteristicListener ref is null"); return false; } - const std::lock_guard<std::recursive_mutex> lock(characteristicListenerList.get_write_mutex()); - std::shared_ptr<jau::darray<std::shared_ptr<GATTCharacteristicListener>>> store = characteristicListenerList.copy_store(); - int count = 0; - for(auto it = store->begin(); it != store->end(); ) { + auto it = characteristicListenerList.begin(); // lock mutex and copy_store + for (; !it.is_end(); ++it ) { if ( **it == *l ) { - it = store->erase(it); - count++; - break; - } else { - ++it; + it.erase(); + it.write_back(); + return true; } } - if( 0 < count ) { - characteristicListenerList.set_store(std::move(store)); - return true; - } return false; } @@ -155,23 +146,20 @@ int GATTHandler::removeAllAssociatedCharacteristicListener(const GATTCharacteris ERR_PRINT("Given GATTCharacteristic ref is null"); return false; } - const std::lock_guard<std::recursive_mutex> lock(characteristicListenerList.get_write_mutex()); - std::shared_ptr<jau::darray<std::shared_ptr<GATTCharacteristicListener>>> store = characteristicListenerList.copy_store(); int count = 0; - for(auto it = store->begin(); it != store->end(); ) { + auto it = characteristicListenerList.begin(); // lock mutex and copy_store + while( !it.is_end() ) { if ( (*it)->match(*associatedCharacteristic) ) { - it = store->erase(it); - count++; - break; + it.erase(); + ++count; } else { ++it; } } if( 0 < count ) { - characteristicListenerList.set_store(std::move(store)); - return true; + it.write_back(); } - return false; + return count; } int GATTHandler::removeAllCharacteristicListener() noexcept { @@ -223,7 +211,7 @@ void GATTHandler::l2capReaderThreadImpl() { // const std::shared_ptr<TROOctets> data( std::make_shared<POctets>(a->getValue()) ); const uint64_t timestamp = a->ts_creation; int i=0; - jau::for_each_fidelity(characteristicListenerList.cbegin(), characteristicListenerList.cend(), [&](std::shared_ptr<GATTCharacteristicListener> &l) { + jau::for_each_fidelity(characteristicListenerList, [&](std::shared_ptr<GATTCharacteristicListener> &l) { try { if( l->match(*decl) ) { l->notificationReceived(decl, data_view, timestamp); @@ -251,7 +239,7 @@ void GATTHandler::l2capReaderThreadImpl() { // const std::shared_ptr<TROOctets> data( std::make_shared<POctets>(a->getValue()) ); const uint64_t timestamp = a->ts_creation; int i=0; - jau::for_each_fidelity(characteristicListenerList.cbegin(), characteristicListenerList.cend(), [&](std::shared_ptr<GATTCharacteristicListener> &l) { + jau::for_each_fidelity(characteristicListenerList, [&](std::shared_ptr<GATTCharacteristicListener> &l) { try { if( l->match(*decl) ) { l->indicationReceived(decl, data_view, timestamp, cfmSent); @@ -501,7 +489,7 @@ GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_ return findCharacterisicsByValueHandle(charValueHandle, services); } -GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle, std::vector<GATTServiceRef> &services_) noexcept { +GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_t charValueHandle, jau::darray<GATTServiceRef> &services_) noexcept { for(auto it = services_.begin(); it != services_.end(); it++) { GATTCharacteristicRef decl = findCharacterisicsByValueHandle(charValueHandle, *it); if( nullptr != decl ) { @@ -521,7 +509,7 @@ GATTCharacteristicRef GATTHandler::findCharacterisicsByValueHandle(const uint16_ return nullptr; } -std::vector<GATTServiceRef> & GATTHandler::discoverCompletePrimaryServices(std::shared_ptr<GATTHandler> shared_this) { +jau::darray<GATTServiceRef> & GATTHandler::discoverCompletePrimaryServices(std::shared_ptr<GATTHandler> shared_this) { const std::lock_guard<std::recursive_mutex> lock(mtx_command); // RAII-style acquire and relinquish via destructor if( !discoverPrimaryServices(shared_this, services) ) { return services; @@ -536,7 +524,7 @@ std::vector<GATTServiceRef> & GATTHandler::discoverCompletePrimaryServices(std:: return services; } -bool GATTHandler::discoverPrimaryServices(std::shared_ptr<GATTHandler> shared_this, std::vector<GATTServiceRef> & result) { +bool GATTHandler::discoverPrimaryServices(std::shared_ptr<GATTHandler> shared_this, jau::darray<GATTServiceRef> & result) { { // validate shared_this first! GATTHandler *given_this = shared_this.get(); @@ -958,7 +946,7 @@ static const uuid16_t _MANUFACTURER_NAME_STRING(GattCharacteristicType::MANUFACT static const uuid16_t _REGULATORY_CERT_DATA_LIST(GattCharacteristicType::REGULATORY_CERT_DATA_LIST); static const uuid16_t _PNP_ID(GattCharacteristicType::PNP_ID); -std::shared_ptr<GattGenericAccessSvc> GATTHandler::getGenericAccess(std::vector<GATTCharacteristicRef> & genericAccessCharDeclList) { +std::shared_ptr<GattGenericAccessSvc> GATTHandler::getGenericAccess(jau::darray<GATTCharacteristicRef> & genericAccessCharDeclList) { std::shared_ptr<GattGenericAccessSvc> res = nullptr; POctets value(number(Defaults::MAX_ATT_MTU), 0); std::string deviceName = ""; @@ -993,7 +981,7 @@ std::shared_ptr<GattGenericAccessSvc> GATTHandler::getGenericAccess(std::vector< return res; } -std::shared_ptr<GattGenericAccessSvc> GATTHandler::getGenericAccess(std::vector<GATTServiceRef> & primServices) { +std::shared_ptr<GattGenericAccessSvc> GATTHandler::getGenericAccess(jau::darray<GATTServiceRef> & primServices) { std::shared_ptr<GattGenericAccessSvc> res = nullptr; for(size_t i=0; i<primServices.size() && nullptr == res; i++) { res = getGenericAccess(primServices.at(i)->characteristicList); @@ -1006,7 +994,7 @@ bool GATTHandler::ping() { bool isOK = true; for(size_t i=0; isOK && i<services.size(); i++) { - std::vector<GATTCharacteristicRef> & genericAccessCharDeclList = services.at(i)->characteristicList; + jau::darray<GATTCharacteristicRef> & genericAccessCharDeclList = services.at(i)->characteristicList; POctets value(32, 0); for(size_t j=0; isOK && j<genericAccessCharDeclList.size(); j++) { @@ -1033,7 +1021,7 @@ bool GATTHandler::ping() { return false; } -std::shared_ptr<GattDeviceInformationSvc> GATTHandler::getDeviceInformation(std::vector<GATTCharacteristicRef> & characteristicDeclList) { +std::shared_ptr<GattDeviceInformationSvc> GATTHandler::getDeviceInformation(jau::darray<GATTCharacteristicRef> & characteristicDeclList) { std::shared_ptr<GattDeviceInformationSvc> res = nullptr; POctets value(number(Defaults::MAX_ATT_MTU), 0); @@ -1103,7 +1091,7 @@ std::shared_ptr<GattDeviceInformationSvc> GATTHandler::getDeviceInformation(std: return res; } -std::shared_ptr<GattDeviceInformationSvc> GATTHandler::getDeviceInformation(std::vector<GATTServiceRef> & primServices) { +std::shared_ptr<GattDeviceInformationSvc> GATTHandler::getDeviceInformation(jau::darray<GATTServiceRef> & primServices) { std::shared_ptr<GattDeviceInformationSvc> res = nullptr; for(size_t i=0; i<primServices.size() && nullptr == res; i++) { res = getDeviceInformation(primServices.at(i)->characteristicList); diff --git a/src/direct_bt/GATTNumbers.cpp b/src/direct_bt/GATTNumbers.cpp index b34e3514..85696f2b 100644 --- a/src/direct_bt/GATTNumbers.cpp +++ b/src/direct_bt/GATTNumbers.cpp @@ -27,11 +27,11 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <cstdio> #include <algorithm> +#include <jau/darray.hpp> #include <jau/debug.hpp> #include "GATTNumbers.hpp" @@ -193,7 +193,7 @@ const GattServiceCharacteristic direct_bt::GATT_DEVICE_INFORMATION_SRVC = { DEVI } } }; -const std::vector<const GattServiceCharacteristic*> direct_bt::GATT_SERVICES = { +const jau::darray<const GattServiceCharacteristic*> direct_bt::GATT_SERVICES = { &direct_bt::GATT_GENERIC_ACCESS_SRVC, &direct_bt::GATT_HEALTH_THERMOMETER_SRVC, &direct_bt::GATT_DEVICE_INFORMATION_SRVC }; #define CASE_TO_STRING(V) case V: return #V; diff --git a/src/direct_bt/HCIHandler.cpp b/src/direct_bt/HCIHandler.cpp index 235bfc2a..2d500068 100644 --- a/src/direct_bt/HCIHandler.cpp +++ b/src/direct_bt/HCIHandler.cpp @@ -27,7 +27,6 @@ #include <string> #include <memory> #include <cstdint> -#include <vector> #include <cstdio> #include <algorithm> @@ -76,11 +75,12 @@ __pack( struct hci_rp_status { __u8 status; } ); -HCIHandler::HCIConnectionRef HCIHandler::addOrUpdateHCIConnection(std::vector<HCIConnectionRef> &list, +HCIHandler::HCIConnectionRef HCIHandler::addOrUpdateHCIConnection(jau::darray<HCIConnectionRef> &list, const BDAddressAndType& addressAndType, const uint16_t handle) noexcept { const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor // remove all old entry with given address first - for (auto it = list.begin(); it != list.end(); ) { + auto end = list.end(); + for (auto it = list.begin(); it != end; ++it) { HCIConnectionRef conn = *it; if ( conn->equals(addressAndType) ) { // reuse same entry @@ -97,8 +97,6 @@ HCIHandler::HCIConnectionRef HCIHandler::addOrUpdateHCIConnection(std::vector<HC conn->setHandle( handle ); } return conn; // done - } else { - ++it; } } HCIConnectionRef res( new HCIConnection(addressAndType, handle) ); @@ -106,10 +104,10 @@ HCIHandler::HCIConnectionRef HCIHandler::addOrUpdateHCIConnection(std::vector<HC return res; } -HCIHandler::HCIConnectionRef HCIHandler::findHCIConnection(std::vector<HCIConnectionRef> &list, const BDAddressAndType& addressAndType) noexcept { +HCIHandler::HCIConnectionRef HCIHandler::findHCIConnection(jau::darray<HCIConnectionRef> &list, const BDAddressAndType& addressAndType) noexcept { const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor - const size_t size = list.size(); - for (size_t i = 0; i < size; i++) { + const jau::nsize_t size = list.size(); + for (jau::nsize_t i = 0; i < size; i++) { HCIConnectionRef & e = list[i]; if( e->equals(addressAndType) ) { return e; @@ -120,8 +118,8 @@ HCIHandler::HCIConnectionRef HCIHandler::findHCIConnection(std::vector<HCIConnec HCIHandler::HCIConnectionRef HCIHandler::findTrackerConnection(const uint16_t handle) noexcept { const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor - const size_t size = connectionList.size(); - for (size_t i = 0; i < size; i++) { + const jau::nsize_t size = connectionList.size(); + for (jau::nsize_t i = 0; i < size; i++) { HCIConnectionRef & e = connectionList[i]; if ( handle == e->getHandle() ) { return e; @@ -132,13 +130,12 @@ HCIHandler::HCIConnectionRef HCIHandler::findTrackerConnection(const uint16_t ha HCIHandler::HCIConnectionRef HCIHandler::removeTrackerConnection(const HCIConnectionRef conn) noexcept { const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor - for (auto it = connectionList.begin(); it != connectionList.end(); ) { + auto end = connectionList.end(); + for (auto it = connectionList.begin(); it != end; ++it) { HCIConnectionRef e = *it; if ( *e == *conn ) { - it = connectionList.erase(it); + connectionList.erase(it); return e; // done - } else { - ++it; } } return nullptr; @@ -154,15 +151,14 @@ int HCIHandler::countPendingTrackerConnections() noexcept { } return count; } -HCIHandler::HCIConnectionRef HCIHandler::removeHCIConnection(std::vector<HCIConnectionRef> &list, const uint16_t handle) noexcept { +HCIHandler::HCIConnectionRef HCIHandler::removeHCIConnection(jau::darray<HCIConnectionRef> &list, const uint16_t handle) noexcept { const std::lock_guard<std::recursive_mutex> lock(mtx_connectionList); // RAII-style acquire and relinquish via destructor - for (auto it = list.begin(); it != list.end(); ) { + auto end = list.end(); + for (auto it = list.begin(); it != end; ++it) { HCIConnectionRef e = *it; if ( e->getHandle() == handle ) { - it = list.erase(it); + list.erase(it); return e; // done - } else { - ++it; } } return nullptr; @@ -380,7 +376,7 @@ void HCIHandler::hciReaderThreadImpl() noexcept { if( nullptr != conn ) { COND_PRINT(env.DEBUG_EVENT, "HCIHandler-IO RECV (ACL.SMP) %s for %s", smpPDU->toString().c_str(), conn->toString().c_str()); - jau::for_each_fidelity(hciSMPMsgCallbackList.cbegin(), hciSMPMsgCallbackList.cend(), [&](HCISMPMsgCallback &cb) { + jau::for_each_fidelity(hciSMPMsgCallbackList, [&](HCISMPMsgCallback &cb) { cb.invoke(conn->getAddressAndType(), *smpPDU, l2cap); }); } else { @@ -428,7 +424,7 @@ void HCIHandler::hciReaderThreadImpl() noexcept { hciEventRing.putBlocking( std::move( event ) ); } else if( event->isMetaEvent(HCIMetaEventType::LE_ADVERTISING_REPORT) ) { // issue callbacks for the translated AD events - std::vector<std::shared_ptr<EInfoReport>> eirlist = EInfoReport::read_ad_reports(event->getParam(), event->getParamSize()); + jau::darray<std::shared_ptr<EInfoReport>> eirlist = EInfoReport::read_ad_reports(event->getParam(), event->getParamSize()); jau::for_each_idx(eirlist, [&](std::shared_ptr<EInfoReport> & eir) { // COND_PRINT(env.DEBUG_EVENT, "HCIHandler-IO RECV (AD EIR) %s", eir->toString().c_str()); const MgmtEvtDeviceFound e(dev_id, eir); @@ -461,7 +457,7 @@ void HCIHandler::sendMgmtEvent(const MgmtEvent& event) noexcept { MgmtEventCallbackList & mgmtEventCallbackList = mgmtEventCallbackLists[static_cast<uint16_t>(event.getOpcode())]; int invokeCount = 0; - jau::for_each_fidelity(mgmtEventCallbackList.cbegin(), mgmtEventCallbackList.cend(), [&](MgmtEventCallback &cb) { + jau::for_each_fidelity(mgmtEventCallbackList, [&](MgmtEventCallback &cb) { try { cb.invoke(event); } catch (std::exception &e) { diff --git a/src/direct_bt/SMPHandler.cpp b/src/direct_bt/SMPHandler.cpp index f366cd35..cc4e97ff 100644 --- a/src/direct_bt/SMPHandler.cpp +++ b/src/direct_bt/SMPHandler.cpp @@ -127,7 +127,7 @@ void SMPHandler::l2capReaderThreadImpl() { if( SMPPDUMsg::Opcode::SECURITY_REQUEST == opc ) { COND_PRINT(env.DEBUG_DATA, "SMPHandler-IO RECV (SEC_REQ) %s", smpPDU->toString().c_str()); - jau::for_each_fidelity(smpSecurityReqCallbackList.cbegin(), smpSecurityReqCallbackList.cend(), [&](SMPSecurityReqCallback &cb) { + jau::for_each_fidelity(smpSecurityReqCallbackList, [&](SMPSecurityReqCallback &cb) { cb.invoke(*smpPDU); }); } else { |