#include "ble/Bluetooth.h" #include "BluetoothImpl.h" #include #include namespace trygvis { namespace bluetooth { using namespace std; // ----------------------------------------------------------------------- // Mac // ----------------------------------------------------------------------- string Mac::str() const { std::ostringstream buf; buf << setw(2) << hex << setfill('0') << (int) bytes[0] << ":" << setw(2) << hex << setfill('0') << (int) bytes[1] << ":" << setw(2) << hex << setfill('0') << (int) bytes[2] << ":" << setw(2) << hex << setfill('0') << (int) bytes[3] << ":" << setw(2) << hex << setfill('0') << (int) bytes[4] << ":" << setw(2) << hex << setfill('0') << (int) bytes[5]; return buf.str(); } bool Mac::operator==(Mac &other) const { const uint8_t *b = bytes; return memcmp(b, other.bytes, sizeof(bytes)) == 0; } bool Mac::operator!=(Mac &other) const { return !operator==(other); } bool operator<(const Mac &a, const Mac &b) { return memcmp(a.bytes, b.bytes, sizeof(a.bytes)) < 0; } void Mac::copy(uint8_t &_0, uint8_t &_1, uint8_t &_2, uint8_t &_3, uint8_t &_4, uint8_t &_5) const { _0 = bytes[0]; _1 = bytes[1]; _2 = bytes[2]; _3 = bytes[3]; _4 = bytes[4]; _5 = bytes[5]; } Mac Mac::parseMac(string s) { unsigned int bytes[6]; int count = sscanf(s.c_str(), "%02x:%02x:%02x:%02x:%02x:%02x", &bytes[0], &bytes[1], &bytes[2], &bytes[3], &bytes[4], &bytes[5]); if (count != 6) { throw BluetoothException("Unable to parse mac: " + s); } return Mac((uint8_t) bytes[0], (uint8_t) bytes[1], (uint8_t) bytes[2], (uint8_t) bytes[3], (uint8_t) bytes[4], (uint8_t) bytes[5]); } AttPdu::AttPdu(ByteBuffer &bytes) : bytes(bytes) { } AttPdu::AttPdu(ByteBuffer &bytes, AttPduType type) : bytes(bytes) { bytes.write8(type); } AttPduType AttPdu::getType() { return (AttPduType) bytes.get8(0); } void AttPdu::makeReadByGroupType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, SpecUuid uuid) { bytes.write8(AttPduType::READ_BY_GROUP_TYPE_REQ); bytes.write16le(startHandle); bytes.write16le(endHandle); bytes.write16le(uuid.value); } void AttPdu::makeReadByType(ByteBuffer &bytes, uint16_t startHandle, uint16_t endHandle, SpecUuid uuid) { bytes.write8(AttPduType::READ_BY_TYPE_REQ); bytes.write16le(startHandle); bytes.write16le(endHandle); bytes.write16le(uuid.value); } void AttPdu::makeRead(ByteBuffer &bytes, uint16_t handle) { bytes.write8(AttPduType::READ_REQ); bytes.write16le(handle); } void AttPdu::makeWrite(ByteBuffer &req, uint16_t handle, const ByteBuffer &bytes) { req.write8(AttPduType::WRITE_REQ); req.write16le(handle); req.write(bytes); } vector AttPdu::parse(ByteBuffer &bytes, AttPduType type) { // cout << "bytes: " << bytes.toString(); AttPduType t = (AttPduType) bytes.read8(); if (t == INVALID_HANDLE) { return vector(); } if (t != type) { throw BluetoothException("Unexpected type: " + to_string(t)); } if (bytes.getSize() < 4) { throw BluetoothException("Bad READ_BY_GROUP_TYPE_RES packet, expected at least 4 octets, got " + to_string(bytes.getSize())); } uint8_t length = bytes.read8(); // cout << "length=" << (int) length; size_t count = (bytes.getSize() - 2) / length; // cout << "count=" << count; vector values; for (int i = 0; i < count; i++) { auto data = bytes.view(length); // cout << "data, size=" << data.getSize() << ", bytes=" << data.toString(); bytes.skip(length); values.push_back(AttributeData::fromByteBuffer(data)); } return values; } vector AttPdu::parseReadByGroupType(ByteBuffer &bytes) { return parse(bytes, READ_BY_GROUP_TYPE_RES); } vector AttPdu::parseReadByType(ByteBuffer &bytes) { return parse(bytes, READ_BY_TYPE_RES); } void AttPdu::parseRead(ByteBuffer &bytes) { AttPduType t = (AttPduType) bytes.read8(); if (t != READ_RES) { throw BluetoothException("Unexpected type: " + to_string(t)); } } void AttPdu::parseWrite(ByteBuffer &bytes) { AttPduType t = (AttPduType) bytes.read8(); if (t != WRITE_RES) { throw BluetoothException("Unexpected type: " + to_string(t)); } } // ----------------------------------------------------------------------- // AttributeData // ----------------------------------------------------------------------- AttributeData AttributeData::fromByteBuffer(ByteBuffer &bytes) { uint16_t handle = bytes.read16le(); return AttributeData(handle, bytes.view()); } AttributeData::AttributeData(uint16_t handle, ByteBuffer value) : handle(handle), value(value) { } AttributeData::~AttributeData() { } // ----------------------------------------------------------------------- // Gatt // ----------------------------------------------------------------------- BluetoothGatt::BluetoothGatt() { } BluetoothGatt::~BluetoothGatt() { } // ----------------------------------------------------------------------- // Device // ----------------------------------------------------------------------- BluetoothDevice::BluetoothDevice() { } BluetoothDevice::~BluetoothDevice() { } // ----------------------------------------------------------------------- // Adapter // ----------------------------------------------------------------------- BluetoothAdapter::BluetoothAdapter() { } BluetoothAdapter::~BluetoothAdapter() { } BluetoothSystem::BluetoothSystem() { } BluetoothSystem::~BluetoothSystem() { shutdown(); } /* map adapters; BluetoothAdapter &getAdapter(int hciDevice) { map::iterator it = adapters.find(hciDevice); if (it == adapters.end()) { LinuxBluetoothAdapter *adapter = new LinuxBluetoothAdapter(hciDevice); adapters[hciDevice] = adapter; return *adapter; } return *it->second; } */ BluetoothAdapter &getAdapter(int hciDevice) { return getAdapterImpl(hciDevice); } void shutdown() { shutdownImpl(); } uuid_t makeUuid(const uuid_t base, uint8_t a, uint8_t b) { uuid_t c = base; c.data[2] = a; c.data[3] = b; return c; } } }; 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173