aboutsummaryrefslogtreecommitdiff
path: root/ble/Bluetooth.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'ble/Bluetooth.cpp')
-rw-r--r--ble/Bluetooth.cpp126
1 files changed, 3 insertions, 123 deletions
diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp
index 4160acc..2528416 100644
--- a/ble/Bluetooth.cpp
+++ b/ble/Bluetooth.cpp
@@ -54,124 +54,12 @@ Mac Mac::parseMac(string s) {
&bytes[5], &bytes[4], &bytes[3], &bytes[2], &bytes[1], &bytes[0]);
if (count != 6) {
- throw BluetoothException("Unable to parse mac: " + s);
+ throw BluetoothException("Unable to parseAttributeData mac: " + s);
}
return Mac((uint8_t) bytes[5], (uint8_t) bytes[4], (uint8_t) bytes[3], (uint8_t) bytes[2], (uint8_t) bytes[1], (uint8_t) bytes[0]);
}
-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, ShortUuid 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, ShortUuid 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<AttributeData> AttPdu::parse(ByteBuffer &bytes, AttPduType type) {
- // cout << "bytes: " << bytes.toString();
-
- auto t = static_cast<AttPduType>(bytes.read8());
-
- if (t == ERROR) {
- return vector<AttributeData>();
- }
-
- if (t != type) {
- throw BluetoothException("Unexpected type: " + to_string(t) + ", expected " + to_string(type));
- }
-
- 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<AttributeData> 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<AttributeData> AttPdu::parseReadByGroupType(ByteBuffer &bytes) {
- return parse(bytes, READ_BY_GROUP_TYPE_RES);
-}
-
-vector<AttributeData> AttPdu::parseReadByType(ByteBuffer &bytes) {
- return parse(bytes, READ_BY_TYPE_RES);
-}
-
-static uint16_t parseExchangeMtuReq(ByteBuffer &bytes);
-
-void AttPdu::parseRead(ByteBuffer &bytes) {
- AttPduType t = (AttPduType) bytes.read8();
-
- if (t != READ_RES) {
- throw BluetoothException("Unexpected type: " + to_string(t) + ", expected " + to_string(READ_RES));
- }
-}
-
-void AttPdu::parseWrite(ByteBuffer &bytes) {
- AttPduType t = (AttPduType) bytes.read8();
-
- if (t != WRITE_RES) {
- throw BluetoothException("Unexpected type: " + to_string(t) + ", expected " + to_string(WRITE_RES));
- }
-}
-
-// -----------------------------------------------------------------------
-// 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() = default;
-
// -----------------------------------------------------------------------
// Gatt
// -----------------------------------------------------------------------
@@ -216,13 +104,5 @@ shared_ptr<BluetoothAdapter> BluetoothSystem::getAdapter(string name) {
return it->second;
}
-Uuid makeUuid(const Uuid &base, uint8_t a, uint8_t b) {
- uint8_t value[16];
- memcpy(value, base.value, 16);
- value[2] = a;
- value[3] = b;
- return Uuid{value};
-}
-
-}
-};
+} // namespace bluetooth
+} // namespace trygvis