diff options
author | Sven Gothel <[email protected]> | 2020-10-20 05:44:37 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-20 05:44:37 +0200 |
commit | 1f6e924fe05ed45bfa4d0d901af2c4df6c1abab5 (patch) | |
tree | 3e7bb612dcf35b5ef3cff10b46f4fbc95236ad6f | |
parent | 2e40a6fbb84ef21bb9551c653199964e55d7954b (diff) |
Normalize: Use size_t where appropriate; timestamp is uint64_t
timestamp is uint64_t
- AttPDUMsg
Use size_t where appropriate
- OctetTypes.hpp: TROOctets*
- ATTPDUTypes.hpp: AttPDUMsg*
- HCITypes.hpp: HCIPacket*
- HCIComm.hpp: HCIComm
- L2CAPComm.hpp: L2CAPComm
- MgmtTypes.hpp: MgmtCommand*, MgmtEvent*
- UUID.hpp: uuid_t
- DBTManager, GATTHandler, HCIHandler
- HCIComm.hpp: HCIComm
- BTTypes.hpp
- DBTTypes.hpp
24 files changed, 391 insertions, 381 deletions
diff --git a/api/direct_bt/ATTPDUTypes.hpp b/api/direct_bt/ATTPDUTypes.hpp index e890fef8..b5ac98ae 100644 --- a/api/direct_bt/ATTPDUTypes.hpp +++ b/api/direct_bt/ATTPDUTypes.hpp @@ -345,7 +345,7 @@ namespace direct_bt { POctets pdu; /** creation timestamp in milliseconds */ - const int64_t ts_creation; + const uint64_t ts_creation; /** * Return a newly created specialized instance pointer to base class. @@ -353,18 +353,18 @@ namespace direct_bt { * Returned memory reference is managed by caller (delete etc) * </p> */ - static std::shared_ptr<const AttPDUMsg> getSpecialized(const uint8_t * buffer, int const buffer_size) noexcept; + static std::shared_ptr<const AttPDUMsg> getSpecialized(const uint8_t * buffer, size_t const buffer_size) noexcept; /** Persistent memory, w/ ownership ..*/ - AttPDUMsg(const uint8_t* source, const int size) - : pdu(source, std::max(1, size)), ts_creation(jau::getCurrentMilliseconds()) + AttPDUMsg(const uint8_t* source, const size_t size) + : pdu(source, std::max<size_t>(1, size)), ts_creation(jau::getCurrentMilliseconds()) { pdu.check_range(0, getPDUMinSize()); } /** Persistent memory, w/ ownership ..*/ - AttPDUMsg(const Opcode opc, const int size) - : pdu(std::max(1, size)), ts_creation(jau::getCurrentMilliseconds()) + AttPDUMsg(const Opcode opc, const size_t size) + : pdu(std::max<size_t>(1, size)), ts_creation(jau::getCurrentMilliseconds()) { pdu.put_uint8_nc(0, opc); pdu.check_range(0, getPDUMinSize()); @@ -407,7 +407,7 @@ namespace direct_bt { * This auth-signature comes at the very last of the PDU. * </p> */ - int getAuthSigSize() const noexcept { + size_t getAuthSigSize() const noexcept { return getOpAuthSigFlag() ? 12 : 0; } @@ -427,7 +427,7 @@ namespace direct_bt { * Note that the optional auth-signature is at the end of the PDU. * </p> */ - int getPDUParamSize() const noexcept { + size_t getPDUParamSize() const noexcept { return pdu.getSize() - getAuthSigSize() - 1 /* opcode */; } @@ -449,7 +449,7 @@ namespace direct_bt { * conveniently. * </p> */ - virtual int getPDUValueOffset() const noexcept { return 1; /* default: opcode */ } + virtual size_t getPDUValueOffset() const noexcept { return 1; /* default: opcode */ } /** * Returns this PDU's minimum size, i.e. @@ -458,7 +458,7 @@ namespace direct_bt { * </pre> * Value is excluded as it might be flexible. */ - int getPDUMinSize() const noexcept { + size_t getPDUMinSize() const noexcept { return getPDUValueOffset() + getAuthSigSize(); } @@ -477,7 +477,7 @@ namespace direct_bt { * value-size := pdu.size - getAuthSigSize() - value-offset * </pre> */ - int getPDUValueSize() const noexcept { return getPDUParamSize() - getPDUValueOffset() + 1; } + size_t getPDUValueSize() const noexcept { return getPDUParamSize() - getPDUValueOffset() + 1; } /** * Returns the theoretical maximum value size of a PDU. @@ -485,7 +485,7 @@ namespace direct_bt { * ATT_MTU - getAuthSigSize() - value-offset * </pre> */ - int getMaxPDUValueSize(const int mtu) const noexcept { + size_t getMaxPDUValueSize(const size_t mtu) const noexcept { return mtu - getAuthSigSize() - getPDUValueOffset(); } @@ -507,12 +507,12 @@ namespace direct_bt { class AttPDUUndefined: public AttPDUMsg { public: - AttPDUUndefined(const uint8_t* source, const int length) : AttPDUMsg(source, length) { + AttPDUUndefined(const uint8_t* source, const size_t length) : AttPDUMsg(source, length) { checkOpcode(ATT_PDU_UNDEFINED); } /** opcode */ - int getPDUValueOffset() const noexcept override { return 1; } + size_t getPDUValueOffset() const noexcept override { return 1; } std::string getName() const noexcept override { return "AttPDUUndefined"; @@ -552,12 +552,12 @@ namespace direct_bt { static std::string getPlainErrorString(const ErrorCode errorCode) noexcept; - AttErrorRsp(const uint8_t* source, const int length) : AttPDUMsg(source, length) { + AttErrorRsp(const uint8_t* source, const size_t length) : AttPDUMsg(source, length) { checkOpcode(ATT_ERROR_RSP); } /** opcode + reqOpcodeCause + handleCause + errorCode */ - int getPDUValueOffset() const noexcept override { return 1 + 1 + 2 + 1; } + size_t getPDUValueOffset() const noexcept override { return 1 + 1 + 2 + 1; } uint8_t getRequestedOpcodeCause() const noexcept { return pdu.get_uint8_nc(1); @@ -599,7 +599,7 @@ namespace direct_bt { class AttExchangeMTU: public AttPDUMsg { public: - AttExchangeMTU(const uint8_t* source, const int length) : AttPDUMsg(source, length) { + AttExchangeMTU(const uint8_t* source, const size_t length) : AttPDUMsg(source, length) { checkOpcode(ATT_EXCHANGE_MTU_RSP); } @@ -610,7 +610,7 @@ namespace direct_bt { } /** opcode + mtu-size */ - int getPDUValueOffset() const noexcept override { return 1+2; } + size_t getPDUValueOffset() const noexcept override { return 1+2; } uint16_t getMTUSize() const noexcept { return pdu.get_uint16_nc(1); @@ -646,7 +646,7 @@ namespace direct_bt { } /** opcode + handle */ - int getPDUValueOffset() const noexcept override { return 1+2; } + size_t getPDUValueOffset() const noexcept override { return 1+2; } uint16_t getHandle() const noexcept { return pdu.get_uint16_nc( 1 ); @@ -684,13 +684,13 @@ namespace direct_bt { public: static bool instanceOf(); - AttReadRsp(const uint8_t* source, const int length) + AttReadRsp(const uint8_t* source, const size_t length) : AttPDUMsg(source, length), view(pdu, getPDUValueOffset(), getPDUValueSize()) { checkOpcode(ATT_READ_RSP); } /** opcode */ - int getPDUValueOffset() const noexcept override { return 1; } + size_t getPDUValueOffset() const noexcept override { return 1; } uint8_t const * getValuePtr() const noexcept { return pdu.get_ptr_nc(getPDUValueOffset()); } @@ -727,7 +727,7 @@ namespace direct_bt { } /** opcode + handle + value_offset */ - int getPDUValueOffset() const noexcept override { return 1 + 2 + 2; } + size_t getPDUValueOffset() const noexcept override { return 1 + 2 + 2; } uint16_t getHandle() const noexcept { return pdu.get_uint16_nc( 1 ); @@ -769,13 +769,13 @@ namespace direct_bt { public: static bool instanceOf(); - AttReadBlobRsp(const uint8_t* source, const int length) + AttReadBlobRsp(const uint8_t* source, const size_t length) : AttPDUMsg(source, length), view(pdu, getPDUValueOffset(), getPDUValueSize()) { checkOpcode(ATT_READ_BLOB_RSP); } /** opcode */ - int getPDUValueOffset() const noexcept override { return 1; } + size_t getPDUValueOffset() const noexcept override { return 1; } uint8_t const * getValuePtr() const noexcept { return pdu.get_ptr_nc(getPDUValueOffset()); } @@ -814,13 +814,13 @@ namespace direct_bt { : AttPDUMsg(ATT_WRITE_REQ, 1+2+value.getSize()), view(pdu, getPDUValueOffset(), getPDUValueSize()) { pdu.put_uint16_nc(1, handle); - for(int i=0; i<value.getSize(); i++) { + for(size_t i=0; i<value.getSize(); i++) { pdu.put_uint8_nc(3+i, value.get_uint8_nc(i)); } } /** opcode + handle */ - int getPDUValueOffset() const noexcept override { return 1 + 2; } + size_t getPDUValueOffset() const noexcept override { return 1 + 2; } uint16_t getHandle() const noexcept { return pdu.get_uint16_nc( 1 ); @@ -853,13 +853,13 @@ namespace direct_bt { class AttWriteRsp : public AttPDUMsg { public: - AttWriteRsp(const uint8_t* source, const int length) + AttWriteRsp(const uint8_t* source, const size_t length) : AttPDUMsg(source, length) { checkOpcode(ATT_WRITE_RSP); } /** opcode */ - int getPDUValueOffset() const noexcept override { return 1; } + size_t getPDUValueOffset() const noexcept override { return 1; } std::string getName() const noexcept override { return "AttWriteRsp"; @@ -886,13 +886,13 @@ namespace direct_bt { : AttPDUMsg(ATT_WRITE_CMD, 1+2+value.getSize()), view(pdu, getPDUValueOffset(), getPDUValueSize()) { pdu.put_uint16_nc(1, handle); - for(int i=0; i<value.getSize(); i++) { + for(size_t i=0; i<value.getSize(); i++) { pdu.put_uint8_nc(3+i, value.get_uint8_nc(i)); } } /** opcode + handle */ - int getPDUValueOffset() const noexcept override { return 1 + 2; } + size_t getPDUValueOffset() const noexcept override { return 1 + 2; } uint16_t getHandle() const noexcept { return pdu.get_uint16_nc( 1 ); @@ -934,13 +934,13 @@ namespace direct_bt { const TOctetSlice view; public: - AttHandleValueRcv(const uint8_t* source, const int length) + AttHandleValueRcv(const uint8_t* source, const size_t length) : AttPDUMsg(source, length), view(pdu, getPDUValueOffset(), getPDUValueSize()) { checkOpcode(ATT_HANDLE_VALUE_NTF, ATT_HANDLE_VALUE_IND); } /** opcode + handle */ - int getPDUValueOffset() const noexcept override { return 1+2; } + size_t getPDUValueOffset() const noexcept override { return 1+2; } uint16_t getHandle() const noexcept { return pdu.get_uint16_nc(1); @@ -987,7 +987,7 @@ namespace direct_bt { } /** opcode */ - int getPDUValueOffset() const noexcept override { return 1; } + size_t getPDUValueOffset() const noexcept override { return 1; } std::string getName() const noexcept override { return "AttHandleValueCfm"; @@ -997,17 +997,17 @@ namespace direct_bt { class AttElementList : public AttPDUMsg { protected: - AttElementList(const uint8_t* source, const int length) + AttElementList(const uint8_t* source, const size_t length) : AttPDUMsg(source, length) {} virtual std::string addValueString() const { return ""; } - virtual std::string elementString(const int idx) const { (void)idx; return "not implemented"; } + virtual std::string elementString(const size_t idx) const { (void)idx; return "not implemented"; } std::string valueString() const noexcept override { std::string res = "size "+std::to_string(getPDUValueSize())+", "+addValueString()+"elements[count "+std::to_string(getElementCount())+", "+ "size [total "+std::to_string(getElementTotalSize())+", value "+std::to_string(getElementValueSize())+"]: "; - const int count = getElementCount(); - for(int i=0; i<count; i++) { + const size_t count = getElementCount(); + for(size_t i=0; i<count; i++) { res += std::to_string(i)+"["+elementString(i)+"],"; } res += "]"; @@ -1017,15 +1017,15 @@ namespace direct_bt { public: virtual ~AttElementList() noexcept override {} - virtual int getElementTotalSize() const = 0; - virtual int getElementValueSize() const = 0; - virtual int getElementCount() const = 0; + virtual size_t getElementTotalSize() const = 0; + virtual size_t getElementValueSize() const = 0; + virtual size_t getElementCount() const = 0; - int getElementPDUOffset(const int elementIdx) const { + size_t getElementPDUOffset(const size_t elementIdx) const { return getPDUValueOffset() + elementIdx * getElementTotalSize(); } - uint8_t const * getElementPtr(const int elementIdx) const { + uint8_t const * getElementPtr(const size_t elementIdx) const { return pdu.get_ptr(getElementPDUOffset(elementIdx)); } @@ -1079,7 +1079,7 @@ namespace direct_bt { } /** opcode + handle-start + handle-end */ - int getPDUValueOffset() const noexcept override { return 1 + 2 + 2; } + size_t getPDUValueOffset() const noexcept override { return 1 + 2 + 2; } uint16_t getStartHandle() const noexcept { return pdu.get_uint16_nc( 1 ); @@ -1135,7 +1135,7 @@ namespace direct_bt { const TOctetSlice view; public: - Element(const AttReadByTypeRsp & p, const int idx) + Element(const AttReadByTypeRsp & p, const size_t idx) : view(p.pdu, p.getElementPDUOffset(idx), p.getElementTotalSize()) {} uint16_t getHandle() const noexcept { @@ -1146,7 +1146,7 @@ namespace direct_bt { return view.get_ptr_nc(2 /* handle size */); } - int getValueSize() const noexcept { return view.getSize() - 2 /* handle size */; } + size_t getValueSize() const noexcept { return view.getSize() - 2 /* handle size */; } std::string toString() const { return "handle "+jau::uint16HexString(getHandle(), true)+ @@ -1154,7 +1154,7 @@ namespace direct_bt { } }; - AttReadByTypeRsp(const uint8_t* source, const int length) + AttReadByTypeRsp(const uint8_t* source, const size_t length) : AttElementList(source, length) { checkOpcode(ATT_READ_BY_TYPE_RSP); @@ -1166,10 +1166,10 @@ namespace direct_bt { } /** opcode + element-size */ - int getPDUValueOffset() const noexcept override { return 1 + 1; } + size_t getPDUValueOffset() const noexcept override { return 1 + 1; } /** Returns size of each element, i.e. handle-value pair. */ - int getElementTotalSize() const noexcept override { + size_t getElementTotalSize() const noexcept override { return pdu.get_uint8_nc(1); } @@ -1179,23 +1179,23 @@ namespace direct_bt { * element := { uint16_t handle, uint8_t value[value-size] } * </p> */ - int getElementValueSize() const noexcept override { + size_t getElementValueSize() const noexcept override { return getElementTotalSize() - 2; } - int getElementCount() const noexcept override { + size_t getElementCount() const noexcept override { return getPDUValueSize() / getElementTotalSize(); } - Element getElement(const int elementIdx) const { + Element getElement(const size_t elementIdx) const { return Element(*this, elementIdx); } - uint16_t getElementHandle(const int elementIdx) const { + uint16_t getElementHandle(const size_t elementIdx) const { return pdu.get_uint16( getElementPDUOffset(elementIdx) ); } - uint8_t * getElementValuePtr(const int elementIdx) { + uint8_t * getElementValuePtr(const size_t elementIdx) { return pdu.get_wptr() + getElementPDUOffset(elementIdx) + 2 /* handle size */; } @@ -1204,7 +1204,7 @@ namespace direct_bt { } protected: - std::string elementString(const int idx) const override { + std::string elementString(const size_t idx) const override { return getElement(idx).toString(); } }; @@ -1238,7 +1238,7 @@ namespace direct_bt { const TOctetSlice view; public: - Element(const AttReadByGroupTypeRsp & p, const int idx) + Element(const AttReadByGroupTypeRsp & p, const size_t idx) : view(p.pdu, p.getElementPDUOffset(idx), p.getElementTotalSize()) {} uint16_t getStartHandle() const noexcept { @@ -1253,10 +1253,10 @@ namespace direct_bt { return view.get_ptr_nc(4 /* handle size */); } - int getValueSize() const noexcept { return view.getSize() - 4 /* handle size */; } + size_t getValueSize() const noexcept { return view.getSize() - 4 /* handle size */; } }; - AttReadByGroupTypeRsp(const uint8_t* source, const int length) + AttReadByGroupTypeRsp(const uint8_t* source, const size_t length) : AttElementList(source, length) { checkOpcode(ATT_READ_BY_GROUP_TYPE_RSP); @@ -1268,10 +1268,10 @@ namespace direct_bt { } /** opcode + element-size */ - int getPDUValueOffset() const noexcept override { return 1 + 1; } + size_t getPDUValueOffset() const noexcept override { return 1 + 1; } /** Returns size of each element, i.e. handle-value triple. */ - int getElementTotalSize() const noexcept override { + size_t getElementTotalSize() const noexcept override { return pdu.get_uint8_nc(1); } @@ -1281,27 +1281,27 @@ namespace direct_bt { * element := { uint16_t startHandle, uint16_t endHandle, uint8_t value[value-size] } * </p> */ - int getElementValueSize() const noexcept override { + size_t getElementValueSize() const noexcept override { return getElementTotalSize() - 4; } - int getElementCount() const noexcept override { + size_t getElementCount() const noexcept override { return getPDUValueSize() / getElementTotalSize(); } - Element getElement(const int elementIdx) const { + Element getElement(const size_t elementIdx) const { return Element(*this, elementIdx); } - uint16_t getElementStartHandle(const int elementIdx) const { + uint16_t getElementStartHandle(const size_t elementIdx) const { return pdu.get_uint16( getElementPDUOffset(elementIdx) ); } - uint16_t getElementEndHandle(const int elementIdx) const { + uint16_t getElementEndHandle(const size_t elementIdx) const { return pdu.get_uint16( getElementPDUOffset(elementIdx) + 2 /* 1 handle size */ ); } - uint8_t * getElementValuePtr(const int elementIdx) { + uint8_t * getElementValuePtr(const size_t elementIdx) { return pdu.get_wptr() + getElementPDUOffset(elementIdx) + 4 /* 2 handle size */; } @@ -1310,7 +1310,7 @@ namespace direct_bt { } protected: - std::string elementString(const int idx) const override { + std::string elementString(const size_t idx) const override { Element e = getElement(idx); return "handle ["+jau::uint16HexString(e.getStartHandle(), true)+".."+jau::uint16HexString(e.getEndHandle(), true)+ "], data "+jau::bytesHexString(e.getValuePtr(), 0, e.getValueSize(), true /* lsbFirst */, true /* leading0X */); @@ -1338,7 +1338,7 @@ namespace direct_bt { } /** opcode + handle_start + handle_end */ - int getPDUValueOffset() const noexcept override { return 1 + 2 + 2; } + size_t getPDUValueOffset() const noexcept override { return 1 + 2 + 2; } uint16_t getStartHandle() const noexcept { return pdu.get_uint16_nc( 1 ); @@ -1399,12 +1399,12 @@ namespace direct_bt { const uint16_t handle; const std::shared_ptr<const uuid_t> uuid; - Element(const AttFindInfoRsp & p, const int idx) + Element(const AttFindInfoRsp & p, const size_t idx) : handle( p.getElementHandle(idx) ), uuid( p.getElementValue(idx) ) { } }; - AttFindInfoRsp(const uint8_t* source, const int length) : AttElementList(source, length) { + AttFindInfoRsp(const uint8_t* source, const size_t length) : AttElementList(source, length) { checkOpcode(ATT_FIND_INFORMATION_RSP); if( getPDUValueSize() % getElementTotalSize() != 0 ) { throw AttValueException("PDUFindInfoRsp: Invalid packet size: pdu-value-size "+std::to_string(getPDUValueSize())+ @@ -1413,10 +1413,10 @@ namespace direct_bt { } /** opcode + format */ - int getPDUValueOffset() const noexcept override { return 1 + 1; } + size_t getPDUValueOffset() const noexcept override { return 1 + 1; } /** Returns size of each element, i.e. handle-value tuple. */ - int getElementTotalSize() const override { + size_t getElementTotalSize() const override { return 2 + getElementValueSize(); } @@ -1426,23 +1426,23 @@ namespace direct_bt { * element := { uint16_t handle, UUID value }, with a UUID of UUID16 or UUID128 * </p> */ - int getElementValueSize() const override { + size_t getElementValueSize() const override { return uuid_t::number(getUUIFormat()); } - int getElementCount() const override { + size_t getElementCount() const override { return getPDUValueSize() / getElementTotalSize(); } - Element getElement(const int elementIdx) const { + Element getElement(const size_t elementIdx) const { return Element(*this, elementIdx); } - uint16_t getElementHandle(const int elementIdx) const { + uint16_t getElementHandle(const size_t elementIdx) const { return pdu.get_uint16( getElementPDUOffset(elementIdx) ); } - std::shared_ptr<const uuid_t> getElementValue(const int elementIdx) const { + std::shared_ptr<const uuid_t> getElementValue(const size_t elementIdx) const { return pdu.get_uuid( getElementPDUOffset(elementIdx) + 2, getUUIFormat() ); } @@ -1453,7 +1453,7 @@ namespace direct_bt { protected: std::string addValueString() const override { return "format "+std::to_string(pdu.get_uint8_nc(1))+", "; } - std::string elementString(const int idx) const override { + std::string elementString(const size_t idx) const override { Element e = getElement(idx); return "handle "+jau::uint16HexString(e.handle, true)+ ", uuid "+e.uuid.get()->toString(); diff --git a/api/direct_bt/BTTypes.hpp b/api/direct_bt/BTTypes.hpp index e6ac2783..959bdfae 100644 --- a/api/direct_bt/BTTypes.hpp +++ b/api/direct_bt/BTTypes.hpp @@ -444,7 +444,7 @@ namespace direct_bt { POctets data; ManufactureSpecificData(uint16_t const company) noexcept; - ManufactureSpecificData(uint16_t const company, uint8_t const * const data, int const data_len) noexcept; + ManufactureSpecificData(uint16_t const company, uint8_t const * const data, size_t const data_len) noexcept; std::string toString() const noexcept; }; @@ -578,7 +578,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, uint8_t const data_length) noexcept; + static std::vector<std::shared_ptr<EInfoReport>> read_ad_reports(uint8_t const * data, size_t const data_length) noexcept; /** * Reads the Extended Inquiry Response (EIR) or Advertising Data (AD) segments diff --git a/api/direct_bt/DBTTypes.hpp b/api/direct_bt/DBTTypes.hpp index 3fd1a2a2..f185823f 100644 --- a/api/direct_bt/DBTTypes.hpp +++ b/api/direct_bt/DBTTypes.hpp @@ -100,7 +100,7 @@ namespace direct_bt { int8_t max_tx_power; public: - static int minimumDataSize() noexcept { return 6 + 1 + 1 + 1 + 1; } + static size_t minimumDataSize() noexcept { return 6 + 1 + 1 + 1 + 1; } ConnectionInfo(const EUI48 &address, BDAddressType addressType, int8_t rssi, int8_t tx_power, int8_t max_tx_power) noexcept : address(address), addressType(addressType), rssi(rssi), tx_power(tx_power), max_tx_power(max_tx_power) {} diff --git a/api/direct_bt/HCIComm.hpp b/api/direct_bt/HCIComm.hpp index 15af8881..4c66ff75 100644 --- a/api/direct_bt/HCIComm.hpp +++ b/api/direct_bt/HCIComm.hpp @@ -84,23 +84,23 @@ namespace direct_bt { inline std::recursive_mutex & mutex_write() noexcept { return mtx_write; } /** Generic read w/ own timeoutMS, w/o locking suitable for a unique ringbuffer sink. */ - int read(uint8_t* buffer, const int capacity, const int32_t timeoutMS) noexcept; + ssize_t read(uint8_t* buffer, const size_t capacity, const int32_t timeoutMS) noexcept; /** Generic write, locking {@link #mutex_write()}. */ - int write(const uint8_t* buffer, const int size) noexcept; + ssize_t write(const uint8_t* buffer, const size_t size) noexcept; private: - static inline void set_bit(int nr, void *addr) noexcept + static inline void setu32_bit(int nr, void *addr) noexcept { *((uint32_t *) addr + (nr >> 5)) |= (1 << (nr & 31)); } - static inline void clear_bit(int nr, void *addr) noexcept + static inline void clearu32_bit(int nr, void *addr) noexcept { *((uint32_t *) addr + (nr >> 5)) &= ~(1 << (nr & 31)); } - static inline int test_bit(int nr, void *addr) noexcept + static inline int testu32_bit(int nr, void *addr) noexcept { return *((uint32_t *) addr + (nr >> 5)) & (1 << (nr & 31)); } @@ -112,15 +112,15 @@ namespace direct_bt { } static inline void filter_set_ptype(int t, hci_ufilter *f) noexcept { - set_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); + setu32_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); } static inline void filter_clear_ptype(int t, hci_ufilter *f) noexcept { - clear_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); + clearu32_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); } static inline int filter_test_ptype(int t, hci_ufilter *f) noexcept { - return test_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); + return testu32_bit((t == HCI_VENDOR_PKT) ? 0 : (t & HCI_FLT_TYPE_BITS), &f->type_mask); } static inline void filter_all_ptypes(hci_ufilter *f) noexcept { @@ -128,21 +128,21 @@ namespace direct_bt { } static inline void filter_set_event(int e, hci_ufilter *f) noexcept { - set_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); + setu32_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); } static inline void filter_clear_event(int e, hci_ufilter *f) noexcept { - clear_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); + clearu32_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); } static inline int filter_test_event(int e, hci_ufilter *f) noexcept { - return test_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); + return testu32_bit((e & HCI_FLT_EVENT_BITS), &f->event_mask); } static inline void filter_all_events(hci_ufilter *f) noexcept { memset((void *) f->event_mask, 0xff, sizeof(f->event_mask)); } - static inline void filter_set_opcode(int opcode, hci_ufilter *f) noexcept + static inline void filter_set_opcode(uint16_t opcode, hci_ufilter *f) noexcept { f->opcode = opcode; } @@ -150,7 +150,7 @@ namespace direct_bt { { f->opcode = 0; } - static inline int filter_test_opcode(int opcode, hci_ufilter *f) noexcept + static inline int filter_test_opcode(uint16_t opcode, hci_ufilter *f) noexcept { return (f->opcode == opcode); } diff --git a/api/direct_bt/HCIHandler.hpp b/api/direct_bt/HCIHandler.hpp index 142d64b3..e2879f24 100644 --- a/api/direct_bt/HCIHandler.hpp +++ b/api/direct_bt/HCIHandler.hpp @@ -182,8 +182,8 @@ namespace direct_bt { */ class HCIHandler { public: - enum Defaults : int32_t { - HCI_MAX_MTU = static_cast<uint8_t>(HCIConstU8::PACKET_MAX_SIZE) + enum DefaultsSizeT : size_t { + HCI_MAX_MTU = static_cast<size_t>(HCIConstSizeT::PACKET_MAX_SIZE) }; static const pid_t pidSelf; diff --git a/api/direct_bt/HCITypes.hpp b/api/direct_bt/HCITypes.hpp index 0d7cddbd..823b3d2b 100644 --- a/api/direct_bt/HCITypes.hpp +++ b/api/direct_bt/HCITypes.hpp @@ -197,7 +197,7 @@ namespace direct_bt { } std::string getHCIStatusCodeString(const HCIStatusCode ec) noexcept; - enum class HCIConstU8 : uint8_t { + enum class HCIConstSizeT : size_t { /** HCIPacketType::COMMAND header size including HCIPacketType */ COMMAND_HDR_SIZE = 1+3, /** HCIPacketType::ACLDATA header size including HCIPacketType */ @@ -209,8 +209,8 @@ namespace direct_bt { /** Total packet size, guaranteed to be handled by adapter. */ PACKET_MAX_SIZE = 255 }; - constexpr uint8_t number(const HCIConstU8 rhs) noexcept { - return static_cast<uint8_t>(rhs); + constexpr size_t number(const HCIConstSizeT rhs) noexcept { + return static_cast<size_t>(rhs); } enum class HCIPacketType : uint8_t { @@ -440,7 +440,7 @@ namespace direct_bt { } public: - HCIPacket(const HCIPacketType type, const uint8_t total_packet_size) + HCIPacket(const HCIPacketType type, const size_t total_packet_size) : pdu(total_packet_size) { if( 0 == total_packet_size ) { @@ -450,7 +450,7 @@ namespace direct_bt { } /** Persistent memory, w/ ownership ..*/ - HCIPacket(const uint8_t *packet_data, const uint8_t total_packet_size) + HCIPacket(const uint8_t *packet_data, const size_t total_packet_size) : pdu(packet_data, total_packet_size) { if( 0 == total_packet_size ) { @@ -461,7 +461,7 @@ namespace direct_bt { virtual ~HCIPacket() noexcept {} - inline int getTotalSize() const noexcept { return pdu.getSize(); } + inline size_t getTotalSize() const noexcept { return pdu.getSize(); } /** Return the underlying octets read only */ TROOctets & getPDU() noexcept { return pdu; } @@ -496,7 +496,7 @@ namespace direct_bt { return "opcode="+jau::uint16HexString(number(getOpcode()))+" "+getOpcodeString(); } virtual std::string valueString() const noexcept { - const int psz = getParamSize(); + const size_t psz = getParamSize(); const std::string ps = psz > 0 ? jau::bytesHexString(getParam(), 0, psz, true /* lsbFirst */, true /* leading0X */) : ""; return "param[size "+std::to_string(getParamSize())+", data "+ps+"], tsz "+std::to_string(getTotalSize()); } @@ -504,8 +504,8 @@ namespace direct_bt { public: /** Enabling manual construction of command without given value. */ - HCICommand(const HCIOpcode opc, const uint8_t param_size) - : HCIPacket(HCIPacketType::COMMAND, number(HCIConstU8::COMMAND_HDR_SIZE)+param_size) + HCICommand(const HCIOpcode opc, const size_t param_size) + : HCIPacket(HCIPacketType::COMMAND, number(HCIConstSizeT::COMMAND_HDR_SIZE)+param_size) { checkOpcode(opc, HCIOpcode::SPECIAL, HCIOpcode::LE_START_ENC); @@ -514,11 +514,11 @@ namespace direct_bt { } /** Enabling manual construction of command with given value. */ - HCICommand(const HCIOpcode opc, const uint8_t* param, const uint16_t param_size) + HCICommand(const HCIOpcode opc, const uint8_t* param, const size_t param_size) : HCICommand(opc, param_size) { if( param_size > 0 ) { - memcpy(pdu.get_wptr_nc(number(HCIConstU8::COMMAND_HDR_SIZE)), param, param_size); + memcpy(pdu.get_wptr_nc(number(HCIConstSizeT::COMMAND_HDR_SIZE)), param, param_size); } } @@ -526,8 +526,8 @@ namespace direct_bt { HCIOpcode getOpcode() const noexcept { return static_cast<HCIOpcode>( pdu.get_uint16_nc(1) ); } std::string getOpcodeString() const noexcept { return getHCIOpcodeString(getOpcode()); } - uint8_t getParamSize() const noexcept { return pdu.get_uint8_nc(3); } - const uint8_t* getParam() const noexcept { return pdu.get_ptr_nc(number(HCIConstU8::COMMAND_HDR_SIZE)); } + size_t getParamSize() const noexcept { return pdu.get_uint8_nc(3); } + const uint8_t* getParam() const noexcept { return pdu.get_ptr_nc(number(HCIConstSizeT::COMMAND_HDR_SIZE)); } std::string toString() const noexcept { return "HCICommand["+baseString()+", "+valueString()+"]"; @@ -548,8 +548,8 @@ namespace direct_bt { HCIDisconnectCmd(const uint16_t handle, HCIStatusCode reason) : HCICommand(HCIOpcode::DISCONNECT, 3) { - pdu.put_uint16_nc(number(HCIConstU8::COMMAND_HDR_SIZE),handle); - pdu.put_uint8_nc(number(HCIConstU8::COMMAND_HDR_SIZE)+2, number(reason)); + pdu.put_uint16_nc(number(HCIConstSizeT::COMMAND_HDR_SIZE),handle); + pdu.put_uint8_nc(number(HCIConstSizeT::COMMAND_HDR_SIZE)+2, number(reason)); } }; @@ -575,7 +575,7 @@ namespace direct_bt { { } const hcistruct * getStruct() const noexcept { return (const hcistruct *)(getParam()); } - hcistruct * getWStruct() noexcept { return (hcistruct *)( pdu.get_wptr_nc( number(HCIConstU8::COMMAND_HDR_SIZE) ) ); } + hcistruct * getWStruct() noexcept { return (hcistruct *)( pdu.get_wptr_nc( number(HCIConstSizeT::COMMAND_HDR_SIZE) ) ); } }; /** @@ -613,13 +613,13 @@ namespace direct_bt { return "event="+jau::uint8HexString(number(getEventType()))+" "+getEventTypeString(); } virtual std::string valueString() const noexcept { - const int d_sz_base = getBaseParamSize(); - const int d_sz = getParamSize(); + const size_t d_sz_base = getBaseParamSize(); + const size_t d_sz = getParamSize(); const std::string d_str = d_sz > 0 ? jau::bytesHexString(getParam(), 0, d_sz, true /* lsbFirst */, true /* leading0X */) : ""; return "data[size "+std::to_string(d_sz)+"/"+std::to_string(d_sz_base)+", data "+d_str+"], tsz "+std::to_string(getTotalSize()); } - uint8_t getBaseParamSize() const noexcept { return pdu.get_uint8_nc(2); } + size_t getBaseParamSize() const noexcept { return pdu.get_uint8_nc(2); } public: @@ -629,14 +629,14 @@ namespace direct_bt { * Returned memory reference is managed by caller (delete etc) * </p> */ - static std::shared_ptr<HCIEvent> getSpecialized(const uint8_t * buffer, int const buffer_size) noexcept; + static std::shared_ptr<HCIEvent> getSpecialized(const uint8_t * buffer, size_t const buffer_size) noexcept; /** Persistent memory, w/ ownership ..*/ - HCIEvent(const uint8_t* buffer, const int buffer_len, const int exp_param_size) + HCIEvent(const uint8_t* buffer, const size_t buffer_len, const size_t exp_param_size) : HCIPacket(buffer, buffer_len), ts_creation(jau::getCurrentMilliseconds()) { - const int baseParamSize = getBaseParamSize(); - pdu.check_range(0, number(HCIConstU8::EVENT_HDR_SIZE)+baseParamSize); + const size_t baseParamSize = getBaseParamSize(); + pdu.check_range(0, number(HCIConstSizeT::EVENT_HDR_SIZE)+baseParamSize); if( exp_param_size > baseParamSize ) { throw jau::IndexOutOfBoundsException(exp_param_size, baseParamSize, E_FILE_LINE); } @@ -644,8 +644,8 @@ namespace direct_bt { } /** Enabling manual construction of event without given value. */ - HCIEvent(const HCIEventType evt, const uint16_t param_size=0) - : HCIPacket(HCIPacketType::EVENT, number(HCIConstU8::EVENT_HDR_SIZE)+param_size), ts_creation(jau::getCurrentMilliseconds()) + HCIEvent(const HCIEventType evt, const size_t param_size=0) + : HCIPacket(HCIPacketType::EVENT, number(HCIConstSizeT::EVENT_HDR_SIZE)+param_size), ts_creation(jau::getCurrentMilliseconds()) { checkEventType(evt, HCIEventType::INQUIRY_COMPLETE, HCIEventType::AMP_Receiver_Report); pdu.put_uint8_nc(1, number(evt)); @@ -653,11 +653,11 @@ namespace direct_bt { } /** Enabling manual construction of event with given value. */ - HCIEvent(const HCIEventType evt, const uint8_t* param, const uint16_t param_size) + HCIEvent(const HCIEventType evt, const uint8_t* param, const size_t param_size) : HCIEvent(evt, param_size) { if( param_size > 0 ) { - memcpy(pdu.get_wptr_nc(number(HCIConstU8::EVENT_HDR_SIZE)), param, param_size); + memcpy(pdu.get_wptr_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)), param, param_size); } } @@ -676,8 +676,8 @@ namespace direct_bt { std::string getMetaEventTypeString() const noexcept { return getHCIMetaEventTypeString(getMetaEventType()); } bool isMetaEvent(HCIMetaEventType t) const noexcept { return t == getMetaEventType(); } - virtual uint8_t getParamSize() const noexcept { return getBaseParamSize(); } - virtual const uint8_t* getParam() const noexcept { return pdu.get_ptr_nc(number(HCIConstU8::EVENT_HDR_SIZE)); } + virtual size_t getParamSize() const noexcept { return getBaseParamSize(); } + virtual const uint8_t* getParam() const noexcept { return pdu.get_ptr_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)); } virtual bool validate(const HCICommand & cmd) const noexcept { (void)cmd; return true; } @@ -697,19 +697,19 @@ namespace direct_bt { HCIEvent &orig; public: - HCIStructCmdCompleteEvtWrap(HCIEvent & orig) - : orig(orig) + HCIStructCmdCompleteEvtWrap(HCIEvent & orig_) + : orig(orig_) { } std::string toString() const noexcept { return orig.toString(); } bool isTypeAndSizeValid(const HCIEventType ec) const noexcept { return orig.isEvent(ec) && - orig.pdu.is_range_valid(0, number(HCIConstU8::EVENT_HDR_SIZE)+sizeof(hcistruct)); + orig.pdu.is_range_valid(0, number(HCIConstSizeT::EVENT_HDR_SIZE)+sizeof(hcistruct)); } const hcistruct * getStruct() const noexcept { return (const hcistruct *)( orig.getParam() ); } HCIStatusCode getStatus() const noexcept { return static_cast<HCIStatusCode>( getStruct()->status ); } - hcistruct * getWStruct() noexcept { return (hcistruct *)( orig.pdu.get_wptr_nc(number(HCIConstU8::EVENT_HDR_SIZE)) ); } + hcistruct * getWStruct() noexcept { return (hcistruct *)( orig.pdu.get_wptr_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)) ); } }; @@ -733,15 +733,15 @@ namespace direct_bt { } public: - HCIDisconnectionCompleteEvent(const uint8_t* buffer, const int buffer_len) + HCIDisconnectionCompleteEvent(const uint8_t* buffer, const size_t buffer_len) : HCIEvent(buffer, buffer_len, 4) { checkEventType(getEventType(), HCIEventType::DISCONN_COMPLETE); } - HCIStatusCode getStatus() const noexcept { return static_cast<HCIStatusCode>( pdu.get_uint8_nc(number(HCIConstU8::EVENT_HDR_SIZE)) ); } - uint16_t getHandle() const noexcept { return pdu.get_uint16_nc(number(HCIConstU8::EVENT_HDR_SIZE)+1); } - HCIStatusCode getReason() const noexcept { return static_cast<HCIStatusCode>( pdu.get_uint8_nc(number(HCIConstU8::EVENT_HDR_SIZE)+3) ); } + HCIStatusCode getStatus() const noexcept { return static_cast<HCIStatusCode>( pdu.get_uint8_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)) ); } + uint16_t getHandle() const noexcept { return pdu.get_uint16_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)+1); } + HCIStatusCode getReason() const noexcept { return static_cast<HCIStatusCode>( pdu.get_uint8_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)+3) ); } bool validate(const HCICommand & cmd) const noexcept override { return cmd.getOpcode() == HCIOpcode::DISCONNECT; @@ -767,7 +767,7 @@ namespace direct_bt { } public: - HCICommandCompleteEvent(const uint8_t* buffer, const int buffer_len) + HCICommandCompleteEvent(const uint8_t* buffer, const size_t buffer_len) : HCIEvent(buffer, buffer_len, 3) { checkEventType(getEventType(), HCIEventType::CMD_COMPLETE); @@ -779,21 +779,21 @@ namespace direct_bt { * Range: 0 to 255 * </p> */ - uint8_t getNumCommandPackets() const noexcept { return pdu.get_uint8_nc(number(HCIConstU8::EVENT_HDR_SIZE)+0); } + uint8_t getNumCommandPackets() const noexcept { return pdu.get_uint8_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)+0); } /** * The associated command */ - HCIOpcode getOpcode() const noexcept { return static_cast<HCIOpcode>( pdu.get_uint16_nc(number(HCIConstU8::EVENT_HDR_SIZE)+1) ); } + HCIOpcode getOpcode() const noexcept { return static_cast<HCIOpcode>( pdu.get_uint16_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)+1) ); } - uint8_t getReturnParamSize() const noexcept { return getParamSize() - 3; } - const uint8_t* getReturnParam() const { return pdu.get_ptr(number(HCIConstU8::EVENT_HDR_SIZE)+3); } - HCIStatusCode getReturnStatus(const int returnParamOffset=0) const { - const uint8_t returnParamSize = getReturnParamSize(); + size_t getReturnParamSize() const noexcept { return getParamSize() - 3; } + const uint8_t* getReturnParam() const { return pdu.get_ptr(number(HCIConstSizeT::EVENT_HDR_SIZE)+3); } + HCIStatusCode getReturnStatus(const size_t returnParamOffset=0) const { + const size_t returnParamSize = getReturnParamSize(); if( returnParamSize < returnParamOffset + 1 /* status size */ ) { return HCIStatusCode::UNKNOWN; } - return static_cast<HCIStatusCode>( pdu.get_uint8(number(HCIConstU8::EVENT_HDR_SIZE) + 3 + returnParamOffset) ); + return static_cast<HCIStatusCode>( pdu.get_uint8(number(HCIConstSizeT::EVENT_HDR_SIZE) + 3 + returnParamOffset) ); } bool validate(const HCICommand & cmd) const noexcept override { @@ -821,13 +821,13 @@ namespace direct_bt { } public: - HCICommandStatusEvent(const uint8_t* buffer, const int buffer_len) + HCICommandStatusEvent(const uint8_t* buffer, const size_t buffer_len) : HCIEvent(buffer, buffer_len, 4) { checkEventType(getEventType(), HCIEventType::CMD_STATUS); } - HCIStatusCode getStatus() const noexcept { return static_cast<HCIStatusCode>( pdu.get_uint8_nc(number(HCIConstU8::EVENT_HDR_SIZE)) ); } + HCIStatusCode getStatus() const noexcept { return static_cast<HCIStatusCode>( pdu.get_uint8_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)) ); } /** * The Number of HCI Command packets which are allowed to be sent to the Controller from the Host. @@ -835,12 +835,12 @@ namespace direct_bt { * Range: 0 to 255 * </p> */ - uint8_t getNumCommandPackets() const noexcept { return pdu.get_uint8_nc(number(HCIConstU8::EVENT_HDR_SIZE)+1); } + uint8_t getNumCommandPackets() const noexcept { return pdu.get_uint8_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)+1); } /** * The associated command */ - HCIOpcode getOpcode() const noexcept { return static_cast<HCIOpcode>( pdu.get_uint16_nc(number(HCIConstU8::EVENT_HDR_SIZE)+1+1) ); } + HCIOpcode getOpcode() const noexcept { return static_cast<HCIOpcode>( pdu.get_uint16_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)+1+1) ); } bool validate(const HCICommand & cmd) const noexcept override { return cmd.getOpcode() == getOpcode(); @@ -871,31 +871,31 @@ namespace direct_bt { public: /** Passing through preset buffer of this type */ - HCIMetaEvent(const uint8_t* buffer, const int buffer_len, const int exp_meta_param_size) + HCIMetaEvent(const uint8_t* buffer, const size_t buffer_len, const size_t exp_meta_param_size) : HCIEvent(buffer, buffer_len, 1+exp_meta_param_size) { checkEventType(getEventType(), HCIEventType::LE_META); } /** Enabling manual construction of event without given value. */ - HCIMetaEvent(const HCIMetaEventType mc, const int meta_param_size) + HCIMetaEvent(const HCIMetaEventType mc, const size_t meta_param_size) : HCIEvent(HCIEventType::LE_META, 1+meta_param_size) { - pdu.put_uint8_nc(number(HCIConstU8::EVENT_HDR_SIZE), number(mc)); + pdu.put_uint8_nc(number(HCIConstSizeT::EVENT_HDR_SIZE), number(mc)); } /** Enabling manual construction of event with given value. */ - HCIMetaEvent(const HCIMetaEventType mc, const uint8_t * meta_param, const int meta_param_size) + HCIMetaEvent(const HCIMetaEventType mc, const uint8_t * meta_param, const size_t meta_param_size) : HCIMetaEvent(mc, meta_param_size) { if( meta_param_size > 0 ) { - memcpy(pdu.get_wptr_nc(number(HCIConstU8::EVENT_HDR_SIZE)+1), meta_param, meta_param_size); + memcpy(pdu.get_wptr_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)+1), meta_param, meta_param_size); } } - HCIMetaEventType getMetaEventType() const noexcept override { return static_cast<HCIMetaEventType>( pdu.get_uint8_nc(number(HCIConstU8::EVENT_HDR_SIZE)) ); } + HCIMetaEventType getMetaEventType() const noexcept override { return static_cast<HCIMetaEventType>( pdu.get_uint8_nc(number(HCIConstSizeT::EVENT_HDR_SIZE)) ); } - uint8_t getParamSize() const noexcept override { return HCIEvent::getParamSize()-1; } + size_t getParamSize() const noexcept override { return HCIEvent::getParamSize()-1; } const uint8_t* getParam() const noexcept override { return HCIEvent::getParam()+1; } }; @@ -910,14 +910,14 @@ namespace direct_bt { HCIMetaEvent & orig; public: - HCIStructCmdCompleteMetaEvtWrap(HCIMetaEvent & orig) - : orig(orig) + HCIStructCmdCompleteMetaEvtWrap(HCIMetaEvent & orig_) + : orig(orig_) { } std::string toString() const noexcept { return orig.toString(); } bool isTypeAndSizeValid(const HCIMetaEventType mc) const noexcept { return orig.isMetaEvent(mc) && - orig.pdu.is_range_valid(0, number(HCIConstU8::EVENT_HDR_SIZE)+1+sizeof(hcistruct)); + orig.pdu.is_range_valid(0, number(HCIConstSizeT::EVENT_HDR_SIZE)+1+sizeof(hcistruct)); } const hcistruct * getStruct() const noexcept { return (const hcistruct *)( orig.getParam() ); } HCIStatusCode getStatus() const noexcept { return static_cast<HCIStatusCode>( getStruct()->status ); } diff --git a/api/direct_bt/L2CAPComm.hpp b/api/direct_bt/L2CAPComm.hpp index 8ed3d6b2..7734334c 100644 --- a/api/direct_bt/L2CAPComm.hpp +++ b/api/direct_bt/L2CAPComm.hpp @@ -165,10 +165,10 @@ namespace direct_bt { std::recursive_mutex & mutex_write() { return mtx_write; } /** Generic read, w/o locking suitable for a unique ringbuffer sink. Using L2CAPEnv::L2CAP_READER_POLL_TIMEOUT.*/ - int read(uint8_t* buffer, const int capacity); + ssize_t read(uint8_t* buffer, const size_t capacity); /** Generic write, locking {@link #mutex_write()}. */ - int write(const uint8_t *buffer, const int length); + ssize_t write(const uint8_t *buffer, const size_t length); }; } // namespace direct_bt diff --git a/api/direct_bt/MgmtTypes.hpp b/api/direct_bt/MgmtTypes.hpp index 0a7bcedf..cad4c851 100644 --- a/api/direct_bt/MgmtTypes.hpp +++ b/api/direct_bt/MgmtTypes.hpp @@ -68,7 +68,7 @@ namespace direct_bt { }; - enum MgmtConst : int { + enum MgmtSizeConst : size_t { MGMT_HEADER_SIZE = 6 }; @@ -196,7 +196,7 @@ namespace direct_bt { return "opcode="+jau::uint16HexString(static_cast<uint16_t>(getOpcode()))+" "+getOpcodeString()+", devID "+jau::uint16HexString(getDevID()); } virtual std::string valueString() const noexcept { - const int psz = getParamSize(); + const size_t psz = getParamSize(); const std::string ps = psz > 0 ? jau::bytesHexString(getParam(), 0, psz, true /* lsbFirst */, true /* leading0X */) : ""; return "param[size "+std::to_string(getParamSize())+", data "+ps+"], tsz "+std::to_string(getTotalSize()); } @@ -221,7 +221,7 @@ namespace direct_bt { } virtual ~MgmtCommand() noexcept {} - int getTotalSize() const noexcept { return pdu.getSize(); } + size_t getTotalSize() const noexcept { return pdu.getSize(); } /** Return the underlying octets read only */ TROOctets & getPDU() noexcept { return pdu; } @@ -441,18 +441,18 @@ namespace direct_bt { class MgmtLoadConnParamCmd : public MgmtCommand { private: - void checkParamIdx(const int idx) const { - const int pc = getParamCount(); - if( 0 > idx || idx >= pc ) { + void checkParamIdx(const size_t idx) const { + const size_t pc = getParamCount(); + if( idx >= pc ) { throw jau::IndexOutOfBoundsException(idx, pc, E_FILE_LINE); } } protected: std::string valueString() const noexcept override { - const int paramCount = getParamCount(); + const size_t paramCount = getParamCount(); std::string ps = "count "+std::to_string(paramCount)+": "; - for(int i=0; i<paramCount; i++) { + for(size_t i=0; i<paramCount; i++) { if( 0 < i ) { ps.append(", "); } @@ -467,7 +467,7 @@ namespace direct_bt { MgmtLoadConnParamCmd(const uint16_t dev_id, const MgmtConnParam & connParam) : MgmtCommand(MgmtOpcode::LOAD_CONN_PARAM, dev_id, 2 + 15) { - int offset = MGMT_HEADER_SIZE; + size_t offset = MGMT_HEADER_SIZE; pdu.put_uint16_nc(offset, 1); offset+= 2; pdu.put_eui48_nc(offset, connParam.address); offset+=6; @@ -481,7 +481,7 @@ namespace direct_bt { MgmtLoadConnParamCmd(const uint16_t dev_id, std::vector<std::shared_ptr<MgmtConnParam>> connParams) : MgmtCommand(MgmtOpcode::LOAD_CONN_PARAM, dev_id, 2 + connParams.size() * 15) { - int offset = MGMT_HEADER_SIZE; + size_t offset = MGMT_HEADER_SIZE; pdu.put_uint16_nc(offset, connParams.size()); offset+= 2; for(auto it = connParams.begin(); it != connParams.end(); ++it) { @@ -497,12 +497,12 @@ namespace direct_bt { } uint16_t getParamCount() const noexcept { return pdu.get_uint16_nc(MGMT_HEADER_SIZE); } - const EUI48 getAddress(int idx) const { checkParamIdx(idx); return EUI48(pdu.get_ptr(MGMT_HEADER_SIZE + 2 + 15*idx)); } // mgmt_addr_info - BDAddressType getAddressType(int idx) const { checkParamIdx(idx); return static_cast<BDAddressType>(pdu.get_uint8(MGMT_HEADER_SIZE + 2 + 15*idx + 6)); } // mgmt_addr_info - uint16_t getMinInterval(int idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1); } - uint16_t getMaxInterval(int idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1 + 2); } - uint16_t getLatency(int idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1 + 2 + 2); } - uint16_t getTimeout(int idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1 + 2 + 2 + 2); } + const EUI48 getAddress(size_t idx) const { checkParamIdx(idx); return EUI48(pdu.get_ptr(MGMT_HEADER_SIZE + 2 + 15*idx)); } // mgmt_addr_info + BDAddressType getAddressType(size_t idx) const { checkParamIdx(idx); return static_cast<BDAddressType>(pdu.get_uint8(MGMT_HEADER_SIZE + 2 + 15*idx + 6)); } // mgmt_addr_info + uint16_t getMinInterval(size_t idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1); } + uint16_t getMaxInterval(size_t idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1 + 2); } + uint16_t getLatency(size_t idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1 + 2 + 2); } + uint16_t getTimeout(size_t idx) const { checkParamIdx(idx); return pdu.get_uint16(MGMT_HEADER_SIZE + 2 + 15*idx + 6 + 1 + 2 + 2 + 2); } }; /** @@ -584,7 +584,7 @@ namespace direct_bt { " "+getOpcodeString()+", devID "+jau::uint16HexString(getDevID(), true); } virtual std::string valueString() const { - const int d_sz = getDataSize(); + const size_t d_sz = getDataSize(); const std::string d_str = d_sz > 0 ? jau::bytesHexString(getData(), 0, d_sz, true /* lsbFirst */, true /* leading0X */) : ""; return "data[size "+std::to_string(d_sz)+", data "+d_str+"], tsz "+std::to_string(getTotalSize()); } @@ -597,13 +597,13 @@ namespace direct_bt { * Returned memory reference is managed by caller (delete etc) * </p> */ - static std::shared_ptr<MgmtEvent> getSpecialized(const uint8_t * buffer, int const buffer_size) noexcept; + static std::shared_ptr<MgmtEvent> getSpecialized(const uint8_t * buffer, size_t const buffer_size) noexcept; /** Persistent memory, w/ ownership ..*/ - MgmtEvent(const uint8_t* buffer, const int buffer_len, const int exp_param_size) + MgmtEvent(const uint8_t* buffer, const size_t buffer_len, const size_t exp_param_size) : pdu(buffer, buffer_len), ts_creation(jau::getCurrentMilliseconds()) { - const int paramSize = getParamSize(); + const size_t paramSize = getParamSize(); pdu.check_range(0, MGMT_HEADER_SIZE+paramSize); if( exp_param_size > paramSize ) { throw jau::IndexOutOfBoundsException(exp_param_size, paramSize, E_FILE_LINE); @@ -629,7 +629,7 @@ namespace direct_bt { } virtual ~MgmtEvent() noexcept {} - int getTotalSize() const noexcept { return pdu.getSize(); } + size_t getTotalSize() const noexcept { return pdu.getSize(); } uint64_t getTimestamp() const noexcept { return ts_creation; } Opcode getOpcode() const noexcept { return static_cast<Opcode>( pdu.get_uint16_nc(0) ); } @@ -637,8 +637,8 @@ namespace direct_bt { uint16_t getDevID() const noexcept { return pdu.get_uint16_nc(2); } uint16_t getParamSize() const noexcept { return pdu.get_uint16_nc(4); } - virtual int getDataOffset() const noexcept { return MGMT_HEADER_SIZE; } - virtual int getDataSize() const noexcept { return getParamSize(); } + virtual size_t getDataOffset() const noexcept { return MGMT_HEADER_SIZE; } + virtual size_t getDataSize() const noexcept { return getParamSize(); } virtual const uint8_t* getData() const noexcept { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } virtual bool validate(const MgmtCommand &req) const noexcept { @@ -659,7 +659,7 @@ namespace direct_bt { ", status "+jau::uint8HexString(static_cast<uint8_t>(getStatus()), true)+" "+getMgmtStatusString(getStatus()); } - MgmtEvtCmdComplete(const uint8_t* buffer, const int buffer_len, const int exp_param_size) + MgmtEvtCmdComplete(const uint8_t* buffer, const size_t buffer_len, const size_t exp_param_size) : MgmtEvent(buffer, buffer_len, 3+exp_param_size) { checkOpcode(getOpcode(), Opcode::CMD_COMPLETE); @@ -671,7 +671,7 @@ namespace direct_bt { return static_cast<MgmtOpcode>( jau::get_uint16(data, MGMT_HEADER_SIZE, true /* littleEndian */) ); } - MgmtEvtCmdComplete(const uint8_t* buffer, const int buffer_len) + MgmtEvtCmdComplete(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 3) { checkOpcode(getOpcode(), Opcode::CMD_COMPLETE); @@ -682,8 +682,8 @@ namespace direct_bt { MgmtOpcode getReqOpcode() const noexcept { return static_cast<MgmtOpcode>( pdu.get_uint16_nc(MGMT_HEADER_SIZE) ); } MgmtStatus getStatus() const noexcept { return static_cast<MgmtStatus>( pdu.get_uint8_nc(MGMT_HEADER_SIZE+2) ); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+3; } - int getDataSize() const noexcept override { return getParamSize()-3; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+3; } + size_t getDataSize() const noexcept override { return getParamSize()-3; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } bool validate(const MgmtCommand &req) const noexcept override { @@ -717,7 +717,7 @@ namespace direct_bt { } public: - MgmtEvtCmdStatus(const uint8_t* buffer, const int buffer_len) + MgmtEvtCmdStatus(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 3) { checkOpcode(getOpcode(), Opcode::CMD_STATUS); @@ -725,8 +725,8 @@ namespace direct_bt { MgmtOpcode getReqOpcode() const noexcept { return static_cast<MgmtOpcode>( pdu.get_uint16_nc(MGMT_HEADER_SIZE) ); } MgmtStatus getStatus() const noexcept { return static_cast<MgmtStatus>( pdu.get_uint8_nc(MGMT_HEADER_SIZE+2) ); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+3; } - int getDataSize() const noexcept override { return 0; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+3; } + size_t getDataSize() const noexcept override { return 0; } const uint8_t* getData() const noexcept override { return nullptr; } bool validate(const MgmtCommand &req) const noexcept override { @@ -745,7 +745,7 @@ namespace direct_bt { } public: - MgmtEvtDiscovering(const uint8_t* buffer, const int buffer_len) + MgmtEvtDiscovering(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 2) { checkOpcode(getOpcode(), Opcode::DISCOVERING); @@ -761,8 +761,8 @@ namespace direct_bt { ScanType getScanType() const noexcept { return static_cast<ScanType>( pdu.get_uint8_nc(MGMT_HEADER_SIZE) ); } bool getEnabled() const noexcept { return 0 != pdu.get_uint8_nc(MGMT_HEADER_SIZE+1); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+2; } - int getDataSize() const noexcept override { return 0; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+2; } + size_t getDataSize() const noexcept override { return 0; } const uint8_t* getData() const noexcept override { return nullptr; } }; @@ -779,15 +779,15 @@ namespace direct_bt { } public: - MgmtEvtNewSettings(const uint8_t* buffer, const int buffer_len) + MgmtEvtNewSettings(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 4) { checkOpcode(getOpcode(), Opcode::NEW_SETTINGS); } AdapterSetting getSettings() const noexcept { return static_cast<AdapterSetting>( pdu.get_uint32_nc(MGMT_HEADER_SIZE) ); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+4; } - int getDataSize() const noexcept override { return getParamSize()-4; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+4; } + size_t getDataSize() const noexcept override { return getParamSize()-4; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -813,7 +813,7 @@ namespace direct_bt { } public: - MgmtEvtNewConnectionParam(const uint8_t* buffer, const int buffer_len) + MgmtEvtNewConnectionParam(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 16) { checkOpcode(getOpcode(), Opcode::NEW_CONN_PARAM); @@ -827,8 +827,8 @@ namespace direct_bt { uint16_t getLatency() const noexcept { return pdu.get_uint16_nc(MGMT_HEADER_SIZE+12); } uint16_t getTimeout() const noexcept { return pdu.get_uint16_nc(MGMT_HEADER_SIZE+14); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+16; } - int getDataSize() const noexcept override { return getParamSize()-16; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+16; } + size_t getDataSize() const noexcept override { return getParamSize()-16; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -857,7 +857,7 @@ namespace direct_bt { } public: - MgmtEvtDeviceFound(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceFound(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 14) { checkOpcode(getOpcode(), Opcode::DEVICE_FOUND); @@ -868,7 +868,7 @@ namespace direct_bt { { pdu.put_eui48_nc(MGMT_HEADER_SIZE, eir->getAddress()); pdu.put_uint8_nc(MGMT_HEADER_SIZE+6, eir->getAddressType()); - pdu.put_uint8_nc(MGMT_HEADER_SIZE+6+1, eir->getRSSI()); + pdu.put_int8_nc(MGMT_HEADER_SIZE+6+1, eir->getRSSI()); pdu.put_uint32_nc(MGMT_HEADER_SIZE+6+1+1, eir->getFlags()); // EIR flags only 8bit, Mgmt uses 32bit? pdu.put_uint16_nc(MGMT_HEADER_SIZE+6+1+1+4, 0); // eir_len eireport = eir; @@ -884,8 +884,8 @@ namespace direct_bt { uint32_t getFlags() const noexcept { return pdu.get_uint32_nc(MGMT_HEADER_SIZE+8); } uint16_t getEIRSize() const noexcept { return pdu.get_uint16_nc(MGMT_HEADER_SIZE+12); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+14; } - int getDataSize() const noexcept override { return getParamSize()-14; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+14; } + size_t getDataSize() const noexcept override { return getParamSize()-14; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -910,13 +910,13 @@ namespace direct_bt { } public: - MgmtEvtDeviceConnected(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceConnected(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 13), hci_conn_handle(0xffff) { checkOpcode(getOpcode(), Opcode::DEVICE_CONNECTED); } - MgmtEvtDeviceConnected(const uint16_t dev_id, const EUI48 &address, const BDAddressType addressType, const uint16_t hci_conn_handle) - : MgmtEvent(Opcode::DEVICE_CONNECTED, dev_id, 6+1+4+2), hci_conn_handle(hci_conn_handle) + MgmtEvtDeviceConnected(const uint16_t dev_id, const EUI48 &address, const BDAddressType addressType, const uint16_t hci_conn_handle_) + : MgmtEvent(Opcode::DEVICE_CONNECTED, dev_id, 6+1+4+2), hci_conn_handle(hci_conn_handle_) { pdu.put_eui48_nc(MGMT_HEADER_SIZE, address); pdu.put_uint8_nc(MGMT_HEADER_SIZE+6, addressType); @@ -933,8 +933,8 @@ namespace direct_bt { uint32_t getFlags() const noexcept { return pdu.get_uint32_nc(MGMT_HEADER_SIZE+7); } uint16_t getEIRSize() const noexcept { return pdu.get_uint16_nc(MGMT_HEADER_SIZE+11); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+13; } - int getDataSize() const noexcept override { return getParamSize()-13; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+13; } + size_t getDataSize() const noexcept override { return getParamSize()-13; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -956,7 +956,7 @@ namespace direct_bt { } public: - MgmtEvtDeviceConnectFailed(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceConnectFailed(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 8), hciStatus(HCIStatusCode::UNKNOWN) { checkOpcode(getOpcode(), Opcode::CONNECT_FAILED); @@ -976,8 +976,8 @@ namespace direct_bt { /** Return the root reason in non reduced HCIStatusCode space, if available. Otherwise this value will be HCIStatusCode::UNKNOWN. */ HCIStatusCode getHCIStatus() const noexcept { return hciStatus; } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } - int getDataSize() const noexcept override { return getParamSize()-8; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } + size_t getDataSize() const noexcept override { return getParamSize()-8; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -1027,16 +1027,16 @@ namespace direct_bt { } public: - MgmtEvtDeviceDisconnected(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceDisconnected(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 8), hciReason(HCIStatusCode::UNKNOWN), hci_conn_handle(0xffff) { checkOpcode(getOpcode(), Opcode::DEVICE_DISCONNECTED); } MgmtEvtDeviceDisconnected(const uint16_t dev_id, const EUI48 &address, const BDAddressType addressType, - HCIStatusCode hciReason, const uint16_t hci_conn_handle) - : MgmtEvent(Opcode::DEVICE_DISCONNECTED, dev_id, 6+1+1), hciReason(hciReason), hci_conn_handle(hci_conn_handle) + HCIStatusCode hciReason_, const uint16_t hci_conn_handle_) + : MgmtEvent(Opcode::DEVICE_DISCONNECTED, dev_id, 6+1+1), hciReason(hciReason_), hci_conn_handle(hci_conn_handle_) { - DisconnectReason disconnectReason = getDisconnectReason(hciReason); + DisconnectReason disconnectReason = getDisconnectReason(hciReason_); pdu.put_eui48_nc(MGMT_HEADER_SIZE, address); pdu.put_uint8_nc(MGMT_HEADER_SIZE+6, addressType); pdu.put_uint8_nc(MGMT_HEADER_SIZE+6+1, static_cast<uint8_t>(disconnectReason)); @@ -1058,8 +1058,8 @@ namespace direct_bt { /** Returns the disconnected HCI connection handle, assuming creation occurred via HCIHandler */ uint16_t getHCIHandle() const noexcept { return hci_conn_handle; } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } - int getDataSize() const noexcept override { return getParamSize()-8; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } + size_t getDataSize() const noexcept override { return getParamSize()-8; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -1079,7 +1079,7 @@ namespace direct_bt { } public: - MgmtEvtPinCodeRequest(const uint8_t* buffer, const int buffer_len) + MgmtEvtPinCodeRequest(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 8) { checkOpcode(getOpcode(), Opcode::PIN_CODE_REQUEST); @@ -1089,8 +1089,8 @@ namespace direct_bt { uint8_t getSecure() const noexcept { return pdu.get_uint8_nc(MGMT_HEADER_SIZE+7); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } - int getDataSize() const noexcept override { return getParamSize()-8; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } + size_t getDataSize() const noexcept override { return getParamSize()-8; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -1110,7 +1110,7 @@ namespace direct_bt { } public: - MgmtEvtDeviceWhitelistAdded(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceWhitelistAdded(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 8) { checkOpcode(getOpcode(), Opcode::DEVICE_WHITELIST_ADDED); @@ -1120,8 +1120,8 @@ namespace direct_bt { uint8_t getAction() const noexcept { return pdu.get_uint8_nc(MGMT_HEADER_SIZE+7); } - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } - int getDataSize() const noexcept override { return getParamSize()-8; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+8; } + size_t getDataSize() const noexcept override { return getParamSize()-8; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -1137,7 +1137,7 @@ namespace direct_bt { } public: - MgmtEvtAdressInfoMeta(const Opcode opc, const uint8_t* buffer, const int buffer_len) + MgmtEvtAdressInfoMeta(const Opcode opc, const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, 7) { checkOpcode(getOpcode(), opc); @@ -1148,8 +1148,8 @@ namespace direct_bt { const EUI48 getAddress() const noexcept { return EUI48(pdu.get_ptr_nc(MGMT_HEADER_SIZE)); } // mgmt_addr_info BDAddressType getAddressType() const noexcept { return static_cast<BDAddressType>(pdu.get_uint8_nc(MGMT_HEADER_SIZE+6)); } // mgmt_addr_info - int getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+7; } - int getDataSize() const noexcept override { return getParamSize()-7; } + size_t getDataOffset() const noexcept override { return MGMT_HEADER_SIZE+7; } + size_t getDataSize() const noexcept override { return getParamSize()-7; } const uint8_t* getData() const noexcept override { return getDataSize()>0 ? pdu.get_ptr_nc(getDataOffset()) : nullptr; } }; @@ -1159,7 +1159,7 @@ namespace direct_bt { class MgmtEvtDeviceWhitelistRemoved : public MgmtEvtAdressInfoMeta { public: - MgmtEvtDeviceWhitelistRemoved(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceWhitelistRemoved(const uint8_t* buffer, const size_t buffer_len) : MgmtEvtAdressInfoMeta(Opcode::DEVICE_WHITELIST_REMOVED, buffer, buffer_len) { } }; @@ -1170,7 +1170,7 @@ namespace direct_bt { class MgmtEvtDeviceUnpaired : public MgmtEvtAdressInfoMeta { public: - MgmtEvtDeviceUnpaired(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceUnpaired(const uint8_t* buffer, const size_t buffer_len) : MgmtEvtAdressInfoMeta(Opcode::DEVICE_UNPAIRED, buffer, buffer_len) { } }; @@ -1181,7 +1181,7 @@ namespace direct_bt { class MgmtEvtDeviceBlocked : public MgmtEvtAdressInfoMeta { public: - MgmtEvtDeviceBlocked(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceBlocked(const uint8_t* buffer, const size_t buffer_len) : MgmtEvtAdressInfoMeta(Opcode::DEVICE_BLOCKED, buffer, buffer_len) { } }; @@ -1192,7 +1192,7 @@ namespace direct_bt { class MgmtEvtDeviceUnblocked : public MgmtEvtAdressInfoMeta { public: - MgmtEvtDeviceUnblocked(const uint8_t* buffer, const int buffer_len) + MgmtEvtDeviceUnblocked(const uint8_t* buffer, const size_t buffer_len) : MgmtEvtAdressInfoMeta(Opcode::DEVICE_UNBLOCKED, buffer, buffer_len) { } }; @@ -1203,7 +1203,7 @@ namespace direct_bt { class MgmtEvtUserPasskeyRequest: public MgmtEvtAdressInfoMeta { public: - MgmtEvtUserPasskeyRequest(const uint8_t* buffer, const int buffer_len) + MgmtEvtUserPasskeyRequest(const uint8_t* buffer, const size_t buffer_len) : MgmtEvtAdressInfoMeta(Opcode::USER_PASSKEY_REQUEST, buffer, buffer_len) { } }; @@ -1220,10 +1220,10 @@ namespace direct_bt { } public: - static int namesDataSize() noexcept { return MgmtConstU16::MGMT_MAX_NAME_LENGTH + MgmtConstU16::MGMT_MAX_SHORT_NAME_LENGTH; } - static int getRequiredTotalSize() noexcept { return MGMT_HEADER_SIZE + namesDataSize(); } + static size_t namesDataSize() noexcept { return MgmtConstU16::MGMT_MAX_NAME_LENGTH + MgmtConstU16::MGMT_MAX_SHORT_NAME_LENGTH; } + static size_t getRequiredTotalSize() noexcept { return MGMT_HEADER_SIZE + namesDataSize(); } - MgmtEvtLocalNameChanged(const uint8_t* buffer, const int buffer_len) + MgmtEvtLocalNameChanged(const uint8_t* buffer, const size_t buffer_len) : MgmtEvent(buffer, buffer_len, namesDataSize()) { checkOpcode(getOpcode(), Opcode::LOCAL_NAME_CHANGED); @@ -1252,10 +1252,10 @@ namespace direct_bt { } public: - static int infoDataSize() noexcept { return 20 + MgmtConstU16::MGMT_MAX_NAME_LENGTH + MgmtConstU16::MGMT_MAX_SHORT_NAME_LENGTH; } - static int getRequiredTotalSize() noexcept { return MGMT_HEADER_SIZE + 3 + infoDataSize(); } + static size_t infoDataSize() noexcept { return 20 + MgmtConstU16::MGMT_MAX_NAME_LENGTH + MgmtConstU16::MGMT_MAX_SHORT_NAME_LENGTH; } + static size_t getRequiredTotalSize() noexcept { return MGMT_HEADER_SIZE + 3 + infoDataSize(); } - MgmtEvtAdapterInfo(const uint8_t* buffer, const int buffer_len) + MgmtEvtAdapterInfo(const uint8_t* buffer, const size_t buffer_len) : MgmtEvtCmdComplete(buffer, buffer_len, infoDataSize()) { } diff --git a/api/direct_bt/OctetTypes.hpp b/api/direct_bt/OctetTypes.hpp index 42a22bd9..7a6cfac2 100644 --- a/api/direct_bt/OctetTypes.hpp +++ b/api/direct_bt/OctetTypes.hpp @@ -60,12 +60,12 @@ namespace direct_bt { { private: /** Used memory size <= capacity, maybe zero. */ - int _size; + size_t _size; /** Non-null memory pointer. Actual capacity known by owner. */ uint8_t * _data; protected: - static inline void checkPtr(uint8_t *d, int s) { + static inline void checkPtr(uint8_t *d, size_t s) { if( nullptr == d && 0 < s ) { throw jau::IllegalArgumentException("TROOctets::setData: nullptr with size "+std::to_string(s)+" > 0", E_FILE_LINE); } @@ -77,14 +77,14 @@ namespace direct_bt { * @param d a non nullptr memory, otherwise throws exception * @param s used memory size, may be zero */ - inline void setData(uint8_t *d, int s) { + inline void setData(uint8_t *d, size_t s) { TRACE_PRINT("POctets setData: %d bytes @ %p -> %d bytes @ %p", _size, _data, s, d); checkPtr(d, s); _size = s; _data = d; } - inline void setSize(int s) noexcept { _size = s; } + inline void setSize(size_t s) noexcept { _size = s; } public: /** @@ -92,7 +92,7 @@ namespace direct_bt { * @param source a non nullptr memory, otherwise throws exception. Actual capacity known by owner. * @param len readable size of the memory, may be zero */ - TROOctets(const uint8_t *source, const int len) + TROOctets(const uint8_t *source, const size_t len) : _size( len ), _data( const_cast<uint8_t *>(source) ) { checkPtr(_data, _size); } @@ -104,102 +104,102 @@ namespace direct_bt { virtual ~TROOctets() noexcept {} - inline void check_range(const int i, const int count, const char *file, int line) const { - if( 0 > i || i+count > _size ) { + inline void check_range(const size_t i, const size_t count, const char *file, int line) const { + if( i+count > _size ) { throw jau::IndexOutOfBoundsException(i, count, _size, file, line); } } #define check_range(I,C) check_range((I), (C), E_FILE_LINE) - inline bool is_range_valid(const int i, const int count) const noexcept { - return 0 <= i && i+count <= _size; + inline bool is_range_valid(const size_t i, const size_t count) const noexcept { + return i+count <= _size; } /** Returns the used memory size for read and write operations, may be zero. */ - inline int getSize() const noexcept { return _size; } + inline size_t getSize() const noexcept { return _size; } - uint8_t get_uint8(const int i) const { + uint8_t get_uint8(const size_t i) const { check_range(i, 1); return _data[i]; } - inline uint8_t get_uint8_nc(const int i) const noexcept { + inline uint8_t get_uint8_nc(const size_t i) const noexcept { return _data[i]; } - int8_t get_int8(const int i) const { + int8_t get_int8(const size_t i) const { check_range(i, 1); return jau::get_int8(_data, i); } - inline int8_t get_int8_nc(const int i) const noexcept { + inline int8_t get_int8_nc(const size_t i) const noexcept { return jau::get_int8(_data, i); } - uint16_t get_uint16(const int i) const { + uint16_t get_uint16(const size_t i) const { check_range(i, 2); return jau::get_uint16(_data, i, true /* littleEndian */); } - inline uint16_t get_uint16_nc(const int i) const noexcept { + inline uint16_t get_uint16_nc(const size_t i) const noexcept { return jau::get_uint16(_data, i, true /* littleEndian */); } - uint32_t get_uint32(const int i) const { + uint32_t get_uint32(const size_t i) const { check_range(i, 4); return jau::get_uint32(_data, i, true /* littleEndian */); } - inline uint32_t get_uint32_nc(const int i) const noexcept { + inline uint32_t get_uint32_nc(const size_t i) const noexcept { return jau::get_uint32(_data, i, true /* littleEndian */); } - EUI48 get_eui48(const int i) const { + EUI48 get_eui48(const size_t i) const { check_range(i, sizeof(EUI48)); return EUI48(_data+i); } - inline EUI48 get_eui48_nc(const int i) const noexcept { + inline EUI48 get_eui48_nc(const size_t i) const noexcept { return EUI48(_data+i); } /** Assumes a null terminated string */ - std::string get_string(const int i) const { + std::string get_string(const size_t i) const { check_range(i, 1); // minimum size return std::string( (const char*)(_data+i) ); } /** Assumes a null terminated string */ - inline std::string get_string_nc(const int i) const noexcept { + inline std::string get_string_nc(const size_t i) const noexcept { return std::string( (const char*)(_data+i) ); } /** Assumes a string with defined length, not necessarily null terminated */ - std::string get_string(const int i, const int length) const { + std::string get_string(const size_t i, const size_t length) const { check_range(i, length); return std::string( (const char*)(_data+i), length ); } - uuid16_t get_uuid16(const int i) const { + uuid16_t get_uuid16(const size_t i) const { return uuid16_t(get_uint16(i)); } - inline uuid16_t get_uuid16_nc(const int i) const noexcept { + inline uuid16_t get_uuid16_nc(const size_t i) const noexcept { return uuid16_t(get_uint16_nc(i)); } - uuid128_t get_uuid128(const int i) const { + uuid128_t get_uuid128(const size_t i) const { check_range(i, uuid_t::number(uuid_t::TypeSize::UUID128_SZ)); return uuid128_t(jau::get_uint128(_data, i, true /* littleEndian */)); } - inline uuid128_t get_uuid128_nc(const int i) const noexcept { + inline uuid128_t get_uuid128_nc(const size_t i) const noexcept { return uuid128_t(jau::get_uint128(_data, i, true /* littleEndian */)); } - std::shared_ptr<const uuid_t> get_uuid(const int i, const uuid_t::TypeSize tsize) const { + std::shared_ptr<const uuid_t> get_uuid(const size_t i, const uuid_t::TypeSize tsize) const { check_range(i, uuid_t::number(tsize)); return uuid_t::create(tsize, _data, i, true /* littleEndian */); } inline uint8_t const * get_ptr() const noexcept { return _data; } - uint8_t const * get_ptr(const int i) const { + uint8_t const * get_ptr(const size_t i) const { check_range(i, 1); return _data + i; } - inline uint8_t const * get_ptr_nc(const int i) const noexcept { + inline uint8_t const * get_ptr_nc(const size_t i) const noexcept { return _data + i; } @@ -225,7 +225,7 @@ namespace direct_bt { { public: /** Transient passthrough r/w memory, w/o ownership ..*/ - TOctets(uint8_t *source, const int len) + TOctets(uint8_t *source, const size_t len) : TROOctets(source, len) {} TOctets(const TOctets &o) noexcept = default; @@ -235,78 +235,86 @@ namespace direct_bt { virtual ~TOctets() noexcept override {} - void put_uint8(const int i, const uint8_t v) { + void put_int8(const size_t i, const int8_t v) { check_range(i, 1); - data()[i] = v;; + data()[i] = static_cast<uint8_t>(v); } - void put_uint8_nc(const int i, const uint8_t v) noexcept { - data()[i] = v;; + void put_int8_nc(const size_t i, const int8_t v) noexcept { + data()[i] = static_cast<uint8_t>(v); } - void put_uint16(const int i, const uint16_t v) { + void put_uint8(const size_t i, const uint8_t v) { + check_range(i, 1); + data()[i] = v; + } + void put_uint8_nc(const size_t i, const uint8_t v) noexcept { + data()[i] = v; + } + + void put_uint16(const size_t i, const uint16_t v) { check_range(i, 2); jau::put_uint16(data(), i, v, true /* littleEndian */); } - void put_uint16_nc(const int i, const uint16_t v) noexcept { + void put_uint16_nc(const size_t i, const uint16_t v) noexcept { jau::put_uint16(data(), i, v, true /* littleEndian */); } - void put_uint32(const int i, const uint32_t v) { + void put_uint32(const size_t i, const uint32_t v) { check_range(i, 4); jau::put_uint32(data(), i, v, true /* littleEndian */); } - void put_uint32_nc(const int i, const uint32_t v) noexcept { + void put_uint32_nc(const size_t i, const uint32_t v) noexcept { jau::put_uint32(data(), i, v, true /* littleEndian */); } - void put_eui48(const int i, const EUI48 & v) { + void put_eui48(const size_t i, const EUI48 & v) { check_range(i, sizeof(v.b)); memcpy(data() + i, v.b, sizeof(v.b)); } - void put_eui48_nc(const int i, const EUI48 & v) noexcept { + void put_eui48_nc(const size_t i, const EUI48 & v) noexcept { memcpy(data() + i, v.b, sizeof(v.b)); } - void put_octets(const int i, const TROOctets & v) { + void put_octets(const size_t i, const TROOctets & v) { check_range(i, v.getSize()); memcpy(data() + i, v.get_ptr(), v.getSize()); } - void put_octets_nc(const int i, const TROOctets & v) noexcept { + void put_octets_nc(const size_t i, const TROOctets & v) noexcept { memcpy(data() + i, v.get_ptr(), v.getSize()); } - void put_string(const int i, const std::string & v, const int max_len, const bool includeEOS) { - const int size1 = v.size() + ( includeEOS ? 1 : 0 ); - const int size = std::min(size1, max_len); + void put_string(const size_t i, const std::string & v, const size_t max_len, const bool includeEOS) { + const size_t size1 = v.size() + ( includeEOS ? 1 : 0 ); + const size_t size = std::min(size1, max_len); check_range(i, size); memcpy(data() + i, v.c_str(), size); if( size < size1 && includeEOS ) { *(data() + i + size - 1) = 0; // ensure EOS } } - void put_string_nc(const int i, const std::string & v, const int max_len, const bool includeEOS) noexcept { - const int size1 = v.size() + ( includeEOS ? 1 : 0 ); - const int size = std::min(size1, max_len); + void put_string_nc(const size_t i, const std::string & v, const size_t max_len, const bool includeEOS) noexcept { + const size_t size1 = v.size() + ( includeEOS ? 1 : 0 ); + const size_t size = std::min(size1, max_len); memcpy(data() + i, v.c_str(), size); if( size < size1 && includeEOS ) { *(data() + i + size - 1) = 0; // ensure EOS } } - void put_uuid(const int i, const uuid_t & v) { + void put_uuid(const size_t i, const uuid_t & v) { check_range(i, v.getTypeSizeInt()); direct_bt::put_uuid(data(), i, v, true /* littleEndian */); } - void put_uuid_nc(const int i, const uuid_t & v) noexcept { + void put_uuid_nc(const size_t i, const uuid_t & v) noexcept { direct_bt::put_uuid(data(), i, v, true /* littleEndian */); } inline uint8_t * get_wptr() noexcept { return data(); } - uint8_t * get_wptr(const int i) { + uint8_t * get_wptr(const size_t i) { check_range(i, 1); return data() + i; } - uint8_t * get_wptr_nc(const int i) noexcept { + uint8_t * get_wptr_nc(const size_t i) noexcept { return data() + i; } @@ -319,40 +327,40 @@ namespace direct_bt { { private: const TOctets & parent; - int const offset; - int const size; + size_t const offset; + size_t const size; public: - TOctetSlice(const TOctets &buffer, const int offset, const int len) - : parent(buffer), offset(offset), size(len) + TOctetSlice(const TOctets &buffer_, const size_t offset_, const size_t size_) + : parent(buffer_), offset(offset_), size(size_) { - if( offset+size > buffer.getSize() ) { - throw jau::IndexOutOfBoundsException(offset, size, buffer.getSize(), E_FILE_LINE); + if( offset_+size > buffer_.getSize() ) { + throw jau::IndexOutOfBoundsException(offset_, size, buffer_.getSize(), E_FILE_LINE); } } - int getSize() const noexcept { return size; } - int getOffset() const noexcept { return offset; } + size_t getSize() const noexcept { return size; } + size_t getOffset() const noexcept { return offset; } const TOctets& getParent() const noexcept { return parent; } - uint8_t get_uint8(const int i) const { + uint8_t get_uint8(const size_t i) const { return parent.get_uint8(offset+i); } - inline uint8_t get_uint8_nc(const int i) const noexcept { + inline uint8_t get_uint8_nc(const size_t i) const noexcept { return parent.get_uint8_nc(offset+i); } - uint16_t get_uint16(const int i) const { + uint16_t get_uint16(const size_t i) const { return parent.get_uint16(offset+i); } - inline uint16_t get_uint16_nc(const int i) const noexcept { + inline uint16_t get_uint16_nc(const size_t i) const noexcept { return parent.get_uint16_nc(offset+i); } - uint8_t const * get_ptr(const int i) const { + uint8_t const * get_ptr(const size_t i) const { return parent.get_ptr(offset+i); } - inline uint8_t const * get_ptr_nc(const int i) const noexcept { + inline uint8_t const * get_ptr_nc(const size_t i) const noexcept { return parent.get_ptr_nc(offset+i); } @@ -370,7 +378,7 @@ namespace direct_bt { class POctets : public TOctets { private: - int capacity; + size_t capacity; void freeData() { uint8_t * ptr = data(); @@ -380,7 +388,7 @@ namespace direct_bt { } // else: zero sized POctets w/ nullptr are supported } - static uint8_t * allocData(const int size) { + static uint8_t * allocData(const size_t size) { if( size <= 0 ) { return nullptr; } @@ -393,7 +401,7 @@ namespace direct_bt { public: /** Returns the memory capacity, never zero, greater or equal {@link #getSize()}. */ - inline int getCapacity() const noexcept { return capacity; } + inline size_t getCapacity() const noexcept { return capacity; } /** Intentional zero sized POctets instance. */ POctets() @@ -403,17 +411,17 @@ namespace direct_bt { } /** Takes ownership (malloc and copy, free) ..*/ - POctets(const uint8_t *_source, const int _size) - : TOctets( allocData(_size), _size), - capacity( _size ) + POctets(const uint8_t *_source, const size_t size_) + : TOctets( allocData(size_), size_), + capacity( size_ ) { - std::memcpy(data(), _source, _size); + std::memcpy(data(), _source, size_); TRACE_PRINT("POctets ctor1: %p", data()); } /** New buffer (malloc, free) */ - POctets(const int _capacity, const int _size) - : TOctets( allocData(_capacity), _size), + POctets(const size_t _capacity, const size_t size_) + : TOctets( allocData(_capacity), size_), capacity( _capacity ) { if( capacity < getSize() ) { @@ -423,7 +431,7 @@ namespace direct_bt { } /** New buffer (malloc, free) */ - POctets(const int size) + POctets(const size_t size) : POctets(size, size) { TRACE_PRINT("POctets ctor3: %p", data()); @@ -516,7 +524,7 @@ namespace direct_bt { return *this; } - POctets & resize(const int newSize, const int newCapacity) { + POctets & resize(const size_t newSize, const size_t newCapacity) { if( newCapacity < newSize ) { throw jau::IllegalArgumentException("newCapacity "+std::to_string(newCapacity)+" < newSize "+std::to_string(newSize), E_FILE_LINE); } @@ -534,7 +542,7 @@ namespace direct_bt { return *this; } - POctets & resize(const int newSize) { + POctets & resize(const size_t newSize) { if( capacity < newSize ) { throw jau::IllegalArgumentException("capacity "+std::to_string(capacity)+" < newSize "+std::to_string(newSize), E_FILE_LINE); } @@ -542,7 +550,7 @@ namespace direct_bt { return *this; } - POctets & recapacity(const int newCapacity) { + POctets & recapacity(const size_t newCapacity) { if( newCapacity < getSize() ) { throw jau::IllegalArgumentException("newCapacity "+std::to_string(newCapacity)+" < size "+std::to_string(getSize()), E_FILE_LINE); } @@ -562,7 +570,7 @@ namespace direct_bt { POctets & operator+=(const TROOctets &b) { if( 0 < b.getSize() ) { - const int newSize = getSize() + b.getSize(); + const size_t newSize = getSize() + b.getSize(); if( capacity < newSize ) { recapacity( newSize ); } @@ -573,7 +581,7 @@ namespace direct_bt { } POctets & operator+=(const TOctetSlice &b) { if( 0 < b.getSize() ) { - const int newSize = getSize() + b.getSize(); + const size_t newSize = getSize() + b.getSize(); if( capacity < newSize ) { recapacity( newSize ); } diff --git a/api/direct_bt/UUID.hpp b/api/direct_bt/UUID.hpp index 04f93ff4..b96eeac9 100644 --- a/api/direct_bt/UUID.hpp +++ b/api/direct_bt/UUID.hpp @@ -55,8 +55,8 @@ public: enum class TypeSize : int { UUID16_SZ=2, UUID32_SZ=4, UUID128_SZ=16 }; - static constexpr int number(const TypeSize rhs) noexcept { - return static_cast<int>(rhs); + static constexpr size_t number(const TypeSize rhs) noexcept { + return static_cast<size_t>(rhs); } private: @@ -86,7 +86,7 @@ public: { return !(*this == o); } TypeSize getTypeSize() const noexcept { return type; } - int getTypeSizeInt() const noexcept { return uuid_t::number(type); } + size_t getTypeSizeInt() const noexcept { return uuid_t::number(type); } uuid128_t toUUID128(uuid128_t const & base_uuid=BT_BASE_UUID, int const uuid32_le_octet_index=12) const noexcept; /** returns the pointer to the uuid data of size getTypeSize() */ diff --git a/examples/direct_bt_scanner00/dbt_scanner00.cpp b/examples/direct_bt_scanner00/dbt_scanner00.cpp index 0468ba6d..17328897 100644 --- a/examples/direct_bt_scanner00/dbt_scanner00.cpp +++ b/examples/direct_bt_scanner00/dbt_scanner00.cpp @@ -112,7 +112,7 @@ class MyGATTEventListener : public AssociatedGATTCharacteristicListener { void notificationReceived(GATTCharacteristicRef charDecl, std::shared_ptr<TROOctets> charValue, const uint64_t timestamp) override { const std::shared_ptr<DBTDevice> dev = charDecl->getDeviceChecked(); - const int64_t tR = getCurrentMilliseconds(); + const uint64_t tR = getCurrentMilliseconds(); fprintf(stderr, "****** GATT Notify (td %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n", (tR-timestamp), (tR-dev->ts_creation), dev->toString().c_str()); if( nullptr != charDecl ) { @@ -126,7 +126,7 @@ class MyGATTEventListener : public AssociatedGATTCharacteristicListener { const bool confirmationSent) override { const std::shared_ptr<DBTDevice> dev = charDecl->getDeviceChecked(); - const int64_t tR = getCurrentMilliseconds(); + const uint64_t tR = getCurrentMilliseconds(); fprintf(stderr, "****** GATT Indication (confirmed %d, td(msg %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n", confirmationSent, (tR-timestamp), (tR-dev->ts_creation), dev->toString().c_str()); if( nullptr != charDecl ) { @@ -203,7 +203,7 @@ int main(int argc, char *argv[]) adapter.addStatusListener(std::shared_ptr<AdapterStatusListener>(new MyAdapterStatusListener())); - const int64_t t0 = getCurrentMilliseconds(); + const uint64_t t0 = getCurrentMilliseconds(); while( ok && ( forever || !foundDevice ) ) { ok = HCIStatusCode::SUCCESS == adapter.startDiscovery(true /* keepAlive */); diff --git a/examples/direct_bt_scanner01/dbt_scanner01.cpp b/examples/direct_bt_scanner01/dbt_scanner01.cpp index 1aed365a..9f71bf27 100644 --- a/examples/direct_bt_scanner01/dbt_scanner01.cpp +++ b/examples/direct_bt_scanner01/dbt_scanner01.cpp @@ -108,7 +108,7 @@ class MyGATTEventListener : public GATTCharacteristicListener { void notificationReceived(GATTCharacteristicRef charDecl, std::shared_ptr<TROOctets> charValue, const uint64_t timestamp) override { const std::shared_ptr<DBTDevice> dev = charDecl->getDeviceChecked(); - const int64_t tR = getCurrentMilliseconds(); + const uint64_t tR = getCurrentMilliseconds(); fprintf(stderr, "****** GATT Notify (td %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n", (tR-timestamp), (tR-dev->ts_creation), dev->toString().c_str()); if( nullptr != charDecl ) { @@ -122,7 +122,7 @@ class MyGATTEventListener : public GATTCharacteristicListener { const bool confirmationSent) override { const std::shared_ptr<DBTDevice> dev = charDecl->getDeviceChecked(); - const int64_t tR = getCurrentMilliseconds(); + const uint64_t tR = getCurrentMilliseconds(); fprintf(stderr, "****** GATT Indication (confirmed %d, td(msg %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n", confirmationSent, (tR-timestamp), (tR-dev->ts_creation), dev->toString().c_str()); if( nullptr != charDecl ) { @@ -201,7 +201,7 @@ int main(int argc, char *argv[]) adapter.addStatusListener(std::shared_ptr<AdapterStatusListener>(new MyAdapterStatusListener())); - const int64_t t0 = getCurrentMilliseconds(); + const uint64_t t0 = getCurrentMilliseconds(); while( ok && ( forever || !foundDevice ) ) { ok = HCIStatusCode::SUCCESS == adapter.startDiscovery(true /* keepAlive */); diff --git a/examples/direct_bt_scanner10/dbt_scanner10.cpp b/examples/direct_bt_scanner10/dbt_scanner10.cpp index aceebf37..c20b02cc 100644 --- a/examples/direct_bt_scanner10/dbt_scanner10.cpp +++ b/examples/direct_bt_scanner10/dbt_scanner10.cpp @@ -53,7 +53,7 @@ using namespace jau; * </p> */ -static int64_t timestamp_t0; +static uint64_t timestamp_t0; static int RESET_ADAPTER_EACH_CONN = 0; @@ -272,7 +272,7 @@ class MyGATTEventListener : public AssociatedGATTCharacteristicListener { void notificationReceived(GATTCharacteristicRef charDecl, std::shared_ptr<TROOctets> charValue, const uint64_t timestamp) override { const std::shared_ptr<DBTDevice> dev = charDecl->getDeviceChecked(); - const int64_t tR = getCurrentMilliseconds(); + const uint64_t tR = getCurrentMilliseconds(); fprintf(stderr, "****** GATT Notify (td %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n", (tR-timestamp), (tR-dev->getLastDiscoveryTimestamp()), dev->toString().c_str()); if( nullptr != charDecl ) { @@ -286,7 +286,7 @@ class MyGATTEventListener : public AssociatedGATTCharacteristicListener { const bool confirmationSent) override { const std::shared_ptr<DBTDevice> dev = charDecl->getDeviceChecked(); - const int64_t tR = getCurrentMilliseconds(); + const uint64_t tR = getCurrentMilliseconds(); fprintf(stderr, "****** GATT Indication (confirmed %d, td(msg %" PRIu64 " ms, dev-discovered %" PRIu64 " ms): From %s\n", confirmationSent, (tR-timestamp), (tR-dev->getLastDiscoveryTimestamp()), dev->toString().c_str()); if( nullptr != charDecl ) { diff --git a/java/jni/BluetoothUtils.cxx b/java/jni/BluetoothUtils.cxx index 98741e7e..84c18bcb 100644 --- a/java/jni/BluetoothUtils.cxx +++ b/java/jni/BluetoothUtils.cxx @@ -76,7 +76,7 @@ jstring Java_org_tinyb_BluetoothUtils_decodeUTF8String(JNIEnv *env, jclass clazz if( NULL == buffer_ptr ) { throw std::invalid_argument("GetPrimitiveArrayCritical(byte array) is null"); } - sres = jau::dfa_utf8_decode(buffer_ptr+offset, size); + sres = jau::dfa_utf8_decode(buffer_ptr+offset, static_cast<size_t>(size)); } return jau::from_string_to_jstring(env, sres); } diff --git a/src/direct_bt/ATTPDUTypes.cpp b/src/direct_bt/ATTPDUTypes.cpp index 675089dd..97f33bfd 100644 --- a/src/direct_bt/ATTPDUTypes.cpp +++ b/src/direct_bt/ATTPDUTypes.cpp @@ -115,7 +115,7 @@ std::string AttErrorRsp::getPlainErrorString(const ErrorCode errorCode) noexcept return "Error Reserved for future use"; } -std::shared_ptr<const AttPDUMsg> AttPDUMsg::getSpecialized(const uint8_t * buffer, int const buffer_size) noexcept { +std::shared_ptr<const AttPDUMsg> AttPDUMsg::getSpecialized(const uint8_t * buffer, size_t const buffer_size) noexcept { const uint8_t opc = *buffer; const AttPDUMsg * res; switch( opc ) { diff --git a/src/direct_bt/BTTypes.cpp b/src/direct_bt/BTTypes.cpp index f4a200be..0b91d261 100644 --- a/src/direct_bt/BTTypes.cpp +++ b/src/direct_bt/BTTypes.cpp @@ -696,20 +696,20 @@ 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, uint8_t const data_length) noexcept { - int const num_reports = (int) data[0]; +std::vector<std::shared_ptr<EInfoReport>> EInfoReport::read_ad_reports(uint8_t const * data, size_t const data_length) noexcept { + size_t const num_reports = (size_t) data[0]; std::vector<std::shared_ptr<EInfoReport>> ad_reports; - if( 0 >= num_reports || num_reports > 0x19 ) { + if( 0 == num_reports || num_reports > 0x19 ) { DBG_PRINT("AD-Reports: Invalid reports count: %d", num_reports); return ad_reports; } uint8_t const *limes = data + data_length; uint8_t const *i_octets = data + 1; uint8_t ad_data_len[0x19]; - const int segment_count = 6; - int read_segments = 0; - int i; + const size_t segment_count = 6; + size_t read_segments = 0; + size_t i; const uint64_t timestamp = jau::getCurrentMilliseconds(); for(i = 0; i < num_reports && i_octets < limes; i++) { @@ -742,10 +742,10 @@ std::vector<std::shared_ptr<EInfoReport>> EInfoReport::read_ad_reports(uint8_t c i_octets++; read_segments++; } - const int bytes_left = limes - i_octets; + const size_t bytes_left = static_cast<size_t>(limes - i_octets); if( segment_count != read_segments ) { - WARN_PRINT("AD-Reports: Incomplete %d reports within %d bytes: Segment read %d < %d, data-ptr %d bytes to limes\n", + WARN_PRINT("AD-Reports: Incomplete %zu reports within %zu bytes: Segment read %zu < %zu, data-ptr %zu bytes to limes\n", num_reports, data_length, read_segments, segment_count, bytes_left); } return ad_reports; diff --git a/src/direct_bt/DBTManager.cpp b/src/direct_bt/DBTManager.cpp index 8cf6a31e..54600070 100644 --- a/src/direct_bt/DBTManager.cpp +++ b/src/direct_bt/DBTManager.cpp @@ -89,7 +89,7 @@ void DBTManager::mgmtReaderThreadImpl() noexcept { } while( !mgmtReaderShallStop ) { - int len; + ssize_t len; if( !comm.isOpen() ) { // not open ERR_PRINT("DBTManager::reader: Not connected"); @@ -99,12 +99,13 @@ void DBTManager::mgmtReaderThreadImpl() noexcept { len = comm.read(rbuffer.get_wptr(), rbuffer.getSize(), env.MGMT_READER_THREAD_POLL_TIMEOUT); if( 0 < len ) { - const uint16_t paramSize = len >= MGMT_HEADER_SIZE ? rbuffer.get_uint16_nc(4) : 0; - if( len < MGMT_HEADER_SIZE + paramSize ) { - WARN_PRINT("DBTManager::reader: length mismatch %d < MGMT_HEADER_SIZE(%d) + %d", len, MGMT_HEADER_SIZE, paramSize); + const size_t len2 = static_cast<size_t>(len); + const size_t paramSize = len2 >= MGMT_HEADER_SIZE ? rbuffer.get_uint16_nc(4) : 0; + if( len2 < MGMT_HEADER_SIZE + paramSize ) { + WARN_PRINT("DBTManager::reader: length mismatch %zu < MGMT_HEADER_SIZE(%zu) + %zu", len2, MGMT_HEADER_SIZE, paramSize); continue; // discard data } - std::shared_ptr<MgmtEvent> event = MgmtEvent::getSpecialized(rbuffer.get_ptr(), len); + std::shared_ptr<MgmtEvent> event = MgmtEvent::getSpecialized(rbuffer.get_ptr(), len2); const MgmtEvent::Opcode opc = event->getOpcode(); if( MgmtEvent::Opcode::CMD_COMPLETE == opc || MgmtEvent::Opcode::CMD_STATUS == opc ) { COND_PRINT(env.DEBUG_EVENT, "DBTManager-IO RECV (CMD) %s", event->toString().c_str()); @@ -410,7 +411,7 @@ next1: const uint16_t num_adapter = jau::get_uint16(data, 0, true /* littleEndian */); WORDY_PRINT("Bluetooth %d adapter", num_adapter); - const int expDataSize = 2 + num_adapter * 2; + const size_t expDataSize = 2 + num_adapter * 2; if( res->getDataSize() < expDataSize ) { ERR_PRINT("Insufficient data for %d adapter indices: res %s", num_adapter, res->toString().c_str()); goto fail; diff --git a/src/direct_bt/GATTHandler.cpp b/src/direct_bt/GATTHandler.cpp index 087814c6..d881ca9f 100644 --- a/src/direct_bt/GATTHandler.cpp +++ b/src/direct_bt/GATTHandler.cpp @@ -198,7 +198,7 @@ void GATTHandler::l2capReaderThreadImpl() { } while( !l2capReaderShallStop ) { - int len; + ssize_t len; if( !validateConnected() ) { ERR_PRINT("GATTHandler::reader: Invalid IO state -> Stop"); l2capReaderShallStop = true; @@ -207,7 +207,7 @@ void GATTHandler::l2capReaderThreadImpl() { len = l2cap.read(rbuffer.get_wptr(), rbuffer.getSize()); if( 0 < len ) { - std::shared_ptr<const AttPDUMsg> attPDU = AttPDUMsg::getSpecialized(rbuffer.get_ptr(), len); + std::shared_ptr<const AttPDUMsg> attPDU = AttPDUMsg::getSpecialized(rbuffer.get_ptr(), static_cast<size_t>(len)); const AttPDUMsg::Opcode opc = attPDU->getOpcode(); if( AttPDUMsg::Opcode::ATT_HANDLE_VALUE_NTF == opc ) { @@ -262,7 +262,7 @@ void GATTHandler::l2capReaderThreadImpl() { attPDURing.putBlocking( attPDU ); } } else if( ETIMEDOUT != errno && !l2capReaderShallStop ) { // expected exits - IRQ_PRINT("GATTHandler::reader: l2cap read error -> Stop; l2cap.read %d", len); + IRQ_PRINT("GATTHandler::reader: l2cap read error -> Stop; l2cap.read %zd", len); l2capReaderShallStop = true; has_ioerror = true; } @@ -414,15 +414,15 @@ void GATTHandler::send(const AttPDUMsg & msg) { } // Thread safe l2cap.write(..) operation.. - const int res = l2cap.write(msg.pdu.get_ptr(), msg.pdu.getSize()); + const ssize_t res = l2cap.write(msg.pdu.get_ptr(), msg.pdu.getSize()); if( 0 > res ) { IRQ_PRINT("GATTHandler::send: l2cap write error -> disconnect: %s to %s", msg.toString().c_str(), deviceString.c_str()); has_ioerror = true; disconnect(true /* disconnectDevice */, true /* ioErrorCause */); // state -> Disconnected throw BluetoothException("GATTHandler::send: l2cap write error: req "+msg.toString()+" to "+deviceString, E_FILE_LINE); } - if( res != msg.pdu.getSize() ) { - ERR_PRINT("GATTHandler::send: l2cap write count error, %d != %d: %s -> disconnect: %s", + if( static_cast<size_t>(res) != msg.pdu.getSize() ) { + ERR_PRINT("GATTHandler::send: l2cap write count error, %zd != %zu: %s -> disconnect: %s", res, msg.pdu.getSize(), msg.toString().c_str(), deviceString.c_str()); has_ioerror = true; disconnect(true /* disconnectDevice */, true /* ioErrorCause */); // state -> Disconnected @@ -982,8 +982,8 @@ bool GATTHandler::ping() { std::vector<GATTCharacteristicRef> & genericAccessCharDeclList = services.at(i)->characteristicList; POctets value(32, 0); - for(size_t i=0; isOK && i<genericAccessCharDeclList.size(); i++) { - const GATTCharacteristic & charDecl = *genericAccessCharDeclList.at(i); + for(size_t j=0; isOK && j<genericAccessCharDeclList.size(); j++) { + const GATTCharacteristic & charDecl = *genericAccessCharDeclList.at(j); std::shared_ptr<GATTService> service = charDecl.getServiceUnchecked(); if( nullptr == service || _GENERIC_ACCESS != *(service->type) ) { continue; diff --git a/src/direct_bt/GATTNumbers.cpp b/src/direct_bt/GATTNumbers.cpp index 17448320..6fec1bd9 100644 --- a/src/direct_bt/GATTNumbers.cpp +++ b/src/direct_bt/GATTNumbers.cpp @@ -347,7 +347,7 @@ const GattCharacteristicSpec * direct_bt::findGattCharSpec(const uint16_t uuid16 /********************************************************/ std::string direct_bt::GattNameToString(const TROOctets &v) noexcept { - const int str_len = v.getSize(); + const size_t str_len = v.getSize(); if( 0 == str_len ) { return std::string(); // empty } @@ -364,7 +364,7 @@ GattPeriphalPreferredConnectionParameters::GattPeriphalPreferredConnectionParame } std::shared_ptr<GattPeriphalPreferredConnectionParameters> GattPeriphalPreferredConnectionParameters::get(const TROOctets &source) noexcept { - const int reqSize = 8; + const size_t reqSize = 8; if( source.getSize() < reqSize ) { ERR_PRINT("GattPeriphalPreferredConnectionParameters: Insufficient data, less than %d bytes in %s", reqSize, source.toString().c_str()); return nullptr; @@ -389,7 +389,7 @@ GattPnP_ID::GattPnP_ID(const TROOctets &source) noexcept product_id(source.get_uint16(3)), product_version(source.get_uint16(5)) {} std::shared_ptr<GattPnP_ID> GattPnP_ID::get(const TROOctets &source) noexcept { - const int reqSize = 7; + const size_t reqSize = 7; if( source.getSize() < reqSize ) { ERR_PRINT("GattPnP_ID: Insufficient data, less than %d bytes in %s", reqSize, source.toString().c_str()); return nullptr; @@ -412,8 +412,8 @@ std::string GattDeviceInformationSvc::toString() const noexcept { } std::shared_ptr<GattTemperatureMeasurement> GattTemperatureMeasurement::get(const TROOctets &source) noexcept { - const int size = source.getSize(); - int reqSize = 1 + 4; // max size = 13 + const size_t size = source.getSize(); + size_t reqSize = 1 + 4; // max size = 13 if( reqSize > size ) { // min size: flags + temperatureValue ERR_PRINT("GattTemperatureMeasurement: Insufficient data, less than %d bytes in %s", reqSize, source.toString().c_str()); diff --git a/src/direct_bt/HCIComm.cpp b/src/direct_bt/HCIComm.cpp index 9810fa1a..a99125e9 100644 --- a/src/direct_bt/HCIComm.cpp +++ b/src/direct_bt/HCIComm.cpp @@ -138,9 +138,9 @@ void HCIComm::close() noexcept { DBG_PRINT("HCIComm::close: End: dd %d", socket_descriptor.load()); } -int HCIComm::read(uint8_t* buffer, const int capacity, const int32_t timeoutMS) noexcept { - int len = 0; - if( 0 > socket_descriptor || 0 > capacity ) { +ssize_t HCIComm::read(uint8_t* buffer, const size_t capacity, const int32_t timeoutMS) noexcept { + ssize_t len = 0; + if( 0 > socket_descriptor ) { goto errout; } if( 0 == capacity ) { @@ -180,10 +180,10 @@ errout: return -1; } -int HCIComm::write(const uint8_t* buffer, const int size) noexcept { +ssize_t HCIComm::write(const uint8_t* buffer, const size_t size) noexcept { const std::lock_guard<std::recursive_mutex> lock(mtx_write); // RAII-style acquire and relinquish via destructor - int len = 0; - if( 0 > socket_descriptor || 0 > size ) { + ssize_t len = 0; + if( 0 > socket_descriptor ) { goto errout; } if( 0 == size ) { diff --git a/src/direct_bt/HCIHandler.cpp b/src/direct_bt/HCIHandler.cpp index 750386d4..9bdae951 100644 --- a/src/direct_bt/HCIHandler.cpp +++ b/src/direct_bt/HCIHandler.cpp @@ -260,7 +260,7 @@ void HCIHandler::hciReaderThreadImpl() noexcept { } while( !hciReaderShallStop ) { - int len; + ssize_t len; if( !comm.isOpen() ) { // not open ERR_PRINT("HCIHandler::reader: Not connected"); @@ -270,16 +270,17 @@ void HCIHandler::hciReaderThreadImpl() noexcept { len = comm.read(rbuffer.get_wptr(), rbuffer.getSize(), env.HCI_READER_THREAD_POLL_TIMEOUT); if( 0 < len ) { - const uint16_t paramSize = len >= number(HCIConstU8::EVENT_HDR_SIZE) ? rbuffer.get_uint8_nc(2) : 0; - if( len < number(HCIConstU8::EVENT_HDR_SIZE) + paramSize ) { - WARN_PRINT("HCIHandler::reader: length mismatch %d < EVENT_HDR_SIZE(%d) + %d", - len, number(HCIConstU8::EVENT_HDR_SIZE), paramSize); + const size_t len2 = static_cast<size_t>(len); + const size_t paramSize = len2 >= number(HCIConstSizeT::EVENT_HDR_SIZE) ? rbuffer.get_uint8_nc(2) : 0; + if( len2 < number(HCIConstSizeT::EVENT_HDR_SIZE) + paramSize ) { + WARN_PRINT("HCIHandler::reader: length mismatch %zu < EVENT_HDR_SIZE(%zu) + %zu", + len2, number(HCIConstSizeT::EVENT_HDR_SIZE), paramSize); continue; // discard data } - std::shared_ptr<HCIEvent> event = HCIEvent::getSpecialized(rbuffer.get_ptr(), len); + std::shared_ptr<HCIEvent> event = HCIEvent::getSpecialized(rbuffer.get_ptr(), len2); if( nullptr == event ) { // not an event ... - ERR_PRINT("HCIHandler-IO RECV Drop (non-event) %s", jau::bytesHexString(rbuffer.get_ptr(), 0, len, true /* lsbFirst*/).c_str()); + ERR_PRINT("HCIHandler-IO RECV Drop (non-event) %s", jau::bytesHexString(rbuffer.get_ptr(), 0, len2, true /* lsbFirst*/).c_str()); continue; } diff --git a/src/direct_bt/HCITypes.cpp b/src/direct_bt/HCITypes.cpp index 7bbd1b9f..ce334788 100644 --- a/src/direct_bt/HCITypes.cpp +++ b/src/direct_bt/HCITypes.cpp @@ -272,7 +272,7 @@ std::string getHCIMetaEventTypeString(const HCIMetaEventType op) noexcept { return "Unknown HCIMetaType"; } -std::shared_ptr<HCIEvent> HCIEvent::getSpecialized(const uint8_t * buffer, int const buffer_size) noexcept { +std::shared_ptr<HCIEvent> HCIEvent::getSpecialized(const uint8_t * buffer, size_t const buffer_size) noexcept { const HCIPacketType pc = static_cast<HCIPacketType>( jau::get_uint8(buffer, 0) ); if( HCIPacketType::EVENT != pc ) { return nullptr; diff --git a/src/direct_bt/L2CAPComm.cpp b/src/direct_bt/L2CAPComm.cpp index a20d8911..6e2514d1 100644 --- a/src/direct_bt/L2CAPComm.cpp +++ b/src/direct_bt/L2CAPComm.cpp @@ -215,14 +215,14 @@ bool L2CAPComm::disconnect() noexcept { return true; } -int L2CAPComm::read(uint8_t* buffer, const int capacity) { +ssize_t L2CAPComm::read(uint8_t* buffer, const size_t capacity) { const int32_t timeoutMS = env.L2CAP_READER_POLL_TIMEOUT; - int len = 0; - int err_res = 0; + ssize_t len = 0; + ssize_t err_res = 0; tid_read = pthread_self(); // temporary safe tid to allow interruption - if( 0 > socket_descriptor || 0 > capacity ) { + if( 0 > socket_descriptor ) { err_res = -1; // invalid socket_descriptor or capacity goto errout; } @@ -280,12 +280,12 @@ errout: return err_res; } -int L2CAPComm::write(const uint8_t * buffer, const int length) { +ssize_t L2CAPComm::write(const uint8_t * buffer, const size_t length) { const std::lock_guard<std::recursive_mutex> lock(mtx_write); // RAII-style acquire and relinquish via destructor - int len = 0; - int err_res = 0; + ssize_t len = 0; + ssize_t err_res = 0; - if( 0 > socket_descriptor || 0 > length ) { + if( 0 > socket_descriptor ) { err_res = -1; // invalid socket_descriptor or capacity goto errout; } diff --git a/src/direct_bt/MgmtTypes.cpp b/src/direct_bt/MgmtTypes.cpp index ace0b7b4..ee4e15fe 100644 --- a/src/direct_bt/MgmtTypes.cpp +++ b/src/direct_bt/MgmtTypes.cpp @@ -230,7 +230,7 @@ std::string MgmtEvent::getOpcodeString(const Opcode opc) noexcept { return "Unknown Opcode"; } -std::shared_ptr<MgmtEvent> MgmtEvent::getSpecialized(const uint8_t * buffer, int const buffer_size) noexcept { +std::shared_ptr<MgmtEvent> MgmtEvent::getSpecialized(const uint8_t * buffer, size_t const buffer_size) noexcept { const MgmtEvent::Opcode opc = static_cast<MgmtEvent::Opcode>( jau::get_uint16(buffer, 0, true /* littleEndian */) ); MgmtEvent * res; switch( opc ) { @@ -287,7 +287,7 @@ std::shared_ptr<ConnectionInfo> MgmtEvtCmdComplete::toConnectionInfo() const noe ERR_PRINT("No Success: %s", toString().c_str()); return nullptr; } - const int min_size = ConnectionInfo::minimumDataSize(); + const size_t min_size = ConnectionInfo::minimumDataSize(); if( getDataSize() < min_size ) { ERR_PRINT("Data size < %d: %s", min_size, toString().c_str()); return nullptr; @@ -311,7 +311,7 @@ std::shared_ptr<NameAndShortName> MgmtEvtCmdComplete::toNameAndShortName() const ERR_PRINT("No Success: %s", toString().c_str()); return nullptr; } - const int min_size = MgmtEvtLocalNameChanged::namesDataSize(); + const size_t min_size = MgmtEvtLocalNameChanged::namesDataSize(); if( getDataSize() < min_size ) { ERR_PRINT("Data size < %d: %s", min_size, toString().c_str()); return nullptr; |