aboutsummaryrefslogtreecommitdiff
path: root/ble
diff options
context:
space:
mode:
Diffstat (limited to 'ble')
-rw-r--r--ble/Bluetooth.cpp16
-rw-r--r--ble/BluetoothImpl.h45
-rw-r--r--ble/ByteBuffer.cpp1
-rw-r--r--ble/LinuxBluetooth.cpp60
4 files changed, 64 insertions, 58 deletions
diff --git a/ble/Bluetooth.cpp b/ble/Bluetooth.cpp
index 66f97b7..b672dba 100644
--- a/ble/Bluetooth.cpp
+++ b/ble/Bluetooth.cpp
@@ -97,7 +97,7 @@ void AttPdu::makeWrite(ByteBuffer &req, uint16_t handle, const ByteBuffer &bytes
}
vector<AttributeData> AttPdu::parse(ByteBuffer &bytes, AttPduType type) {
- DF << "bytes: " << bytes.toString();
+ // cout << "bytes: " << bytes.toString();
AttPduType t = (AttPduType) bytes.read8();
@@ -114,15 +114,15 @@ vector<AttributeData> AttPdu::parse(ByteBuffer &bytes, AttPduType type) {
}
uint8_t length = bytes.read8();
- D << "length=" << (int) length;
+ // cout << "length=" << (int) length;
size_t count = (bytes.getSize() - 2) / length;
- D << "count=" << count;
+ // cout << "count=" << count;
vector<AttributeData> values;
for (int i = 0; i < count; i++) {
auto data = bytes.view(length);
- D << "data, size=" << data.getSize() << ", bytes=" << data.toString();
+ // cout << "data, size=" << data.getSize() << ", bytes=" << data.toString();
bytes.skip(length);
values.push_back(AttributeData::fromByteBuffer(data));
}
@@ -233,10 +233,10 @@ void shutdown() {
}
uuid_t makeUuid(const uuid_t base, uint8_t a, uint8_t b) {
- uuid_t copy = base;
- copy.data[2] = a;
- copy.data[3] = b;
- return copy;
+ uuid_t c = base;
+ c.data[2] = a;
+ c.data[3] = b;
+ return c;
}
}
diff --git a/ble/BluetoothImpl.h b/ble/BluetoothImpl.h
index 967c2e7..204b051 100644
--- a/ble/BluetoothImpl.h
+++ b/ble/BluetoothImpl.h
@@ -2,9 +2,10 @@
#define BLUETOOTH_IMPL_H
#include "ble/Bluetooth.h"
-#include "log.h"
#include <boost/uuid/uuid_io.hpp>
#include <cstring>
+#include <log4cplus/logger.h>
+#include <log4cplus/loggingmacros.h>
#define BLUETOOTH_UUID_INITIALIZER \
{ \
@@ -15,17 +16,36 @@
0x00, 0x80, 0x5f, 0x9b, 0x34, 0xfb \
};
+#define LOG_DEBUG(body) LOG4CPLUS_DEBUG(logger, body)
+#define LOG_INFO(body) LOG4CPLUS_INFO(logger, body)
+#define LOG_WARN(body) LOG4CPLUS_WARN(logger, body)
+
namespace trygvis {
namespace bluetooth {
+// Utility typedefs
typedef boost::uuids::uuid uuid_t;
template<class t>
using o = boost::optional<t>;
+using namespace log4cplus;
+
+// Logging
+
+class LogSetup {
+public:
+ LogSetup(std::string name) : logger(Logger::getInstance(LOG4CPLUS_TEXT(name))) {
+ }
+
+protected:
+ Logger logger;
+};
+
+// Shared classes
-class DefaultBluetoothGattCharacteristic : public BluetoothGattCharacteristic {
+class DefaultBluetoothGattCharacteristic : LogSetup, public BluetoothGattCharacteristic {
public:
DefaultBluetoothGattCharacteristic(BluetoothGattService &service, uint16_t handle, uuid_t uuid, uint8_t properties, uint16_t valueHandle)
- : service(service), handle(handle), uuid(uuid), properties(properties), valueHandle(valueHandle) {
+ : LogSetup("DefaultBluetoothGattCharacteristic"), service(service), handle(handle), uuid(uuid), properties(properties), valueHandle(valueHandle) {
}
virtual ~DefaultBluetoothGattCharacteristic() {
@@ -63,11 +83,9 @@ class DefaultBluetoothGattService : public BluetoothGattService {
public:
DefaultBluetoothGattService(BluetoothDevice &device, const uuid_t uuid, const uint16_t handle, const uint16_t endGroupHandle)
: device(device), uuid(uuid), handle(handle), endGroupHandle(endGroupHandle) {
- DF;
}
virtual ~DefaultBluetoothGattService() {
- DF;
removeCharacteristics();
}
@@ -113,7 +131,6 @@ protected:
vector<BluetoothGattCharacteristic *> characteristics;
void removeCharacteristics() {
- DF;
for (auto &c: characteristics) {
delete c;
}
@@ -122,7 +139,7 @@ protected:
};
template<class _D>
-class DefaultBluetoothGatt : public BluetoothGatt {
+class DefaultBluetoothGatt : protected LogSetup, public BluetoothGatt {
public:
virtual _D &getDevice() const {
return device;
@@ -147,12 +164,10 @@ public:
}
protected:
- DefaultBluetoothGatt(_D &device) : device(device) {
- DF;
+ DefaultBluetoothGatt(_D &device) : LogSetup("BluetoothGatt"), device(device) {
}
virtual ~DefaultBluetoothGatt() {
- DF;
removeServices();
}
@@ -182,11 +197,9 @@ public:
protected:
DefaultBluetoothDevice(A &adapter, Mac &mac) :
adapter(adapter), mac(mac) {
- DF;
}
virtual ~DefaultBluetoothDevice() {
- DF;
removeServices();
}
@@ -202,6 +215,14 @@ protected:
vector<BluetoothGattService *> services;
};
+class DefaultBluetoothAdapter : protected LogSetup, public BluetoothAdapter {
+public:
+protected:
+ DefaultBluetoothAdapter() :
+ LogSetup("BluetoothAdapter") {
+ }
+};
+
BluetoothAdapter &getAdapterImpl(int hciDevice);
void shutdownImpl();
diff --git a/ble/ByteBuffer.cpp b/ble/ByteBuffer.cpp
index d1c923e..66f9270 100644
--- a/ble/ByteBuffer.cpp
+++ b/ble/ByteBuffer.cpp
@@ -3,7 +3,6 @@
#include <sstream>
#include <iomanip>
#include <cassert>
-#include "log.h"
using namespace std;
diff --git a/ble/LinuxBluetooth.cpp b/ble/LinuxBluetooth.cpp
index 435dd63..80e68cb 100644
--- a/ble/LinuxBluetooth.cpp
+++ b/ble/LinuxBluetooth.cpp
@@ -27,7 +27,7 @@ class LinuxBluetoothAdapter;
class LinuxBluetoothManager;
-class LinuxBluetoothAdapter : public BluetoothAdapter {
+class LinuxBluetoothAdapter : public DefaultBluetoothAdapter {
public:
LinuxBluetoothAdapter(int hciDeviceId);
@@ -141,7 +141,7 @@ LinuxBluetoothGatt::~LinuxBluetoothGatt() {
void LinuxBluetoothGatt::connect() {
struct sockaddr_l2 addr;
- D << "connect: mac=" << device.getMac().str();
+ LOG_DEBUG("connect: mac=" << device.getMac().str());
l2cap = socket(PF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);
if (l2cap < 0) {
@@ -186,7 +186,7 @@ void LinuxBluetoothGatt::connect() {
}
void LinuxBluetoothGatt::disconnect() {
- DF << "mac = " << device.getMac().str();
+ LOG_DEBUG("mac = " << device.getMac().str());
close(l2cap);
}
@@ -227,9 +227,7 @@ uuid_t readUuid(BluetoothDevice *device, const ByteBuffer &bytes) {
}
void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristic &c, const ByteBuffer &bytes) {
- DF;
-
- D << "Writing to characteristic " << c.getUuid() << ": " << bytes.toString();
+ LOG_DEBUG("Writing to characteristic " << c.getUuid() << ": " << bytes.toString());
shared_ptr<uint8_t> buffer(new uint8_t[MAX_MTU]);
ByteBuffer out = ByteBuffer(buffer, MAX_MTU);
@@ -242,8 +240,6 @@ void LinuxBluetoothGatt::writeValue(const BluetoothGattCharacteristic &c, const
}
ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) {
- DF;
-
shared_ptr<uint8_t> buffer(new uint8_t[MAX_MTU]);
ByteBuffer out = ByteBuffer(buffer, MAX_MTU);
@@ -257,7 +253,7 @@ ByteBuffer LinuxBluetoothGatt::readValue(const BluetoothGattCharacteristic &c) {
auto response = in.view();
- D << "Value of characteristic " << c.getUuid() << "=" << response.toString();
+ LOG_DEBUG("Value of characteristic " << c.getUuid() << "=" << response.toString());
return response;
}
@@ -347,10 +343,10 @@ void LinuxBluetoothGatt::discoverServices() {
}
ByteBuffer LinuxBluetoothGatt::writeAndRead(ByteBuffer &out, shared_ptr<uint8_t> buffer, size_t size) {
- D << "pdu size=" << out.getCursor();
+ LOG_DEBUG("pdu size=" << out.getCursor());
ssize_t written = write(l2cap, buffer.get(), out.getCursor());
- D << "written=" << written;
+ LOG_DEBUG("written=" << written);
ssize_t r = read(l2cap, buffer.get(), size);
@@ -360,14 +356,12 @@ ByteBuffer LinuxBluetoothGatt::writeAndRead(ByteBuffer &out, shared_ptr<uint8_t>
auto in = ByteBuffer(buffer, (size_t) r, (size_t) r);
- D << "read: " << r << " bytes: " << in.toString();
+ LOG_DEBUG("read: " << r << " bytes: " << in.toString());
return in;
}
vector<AttributeData> LinuxBluetoothGatt::discoverServices(uint16_t startHandle) {
- DF;
-
shared_ptr<uint8_t> buffer(new uint8_t[MAX_MTU]);
ByteBuffer out = ByteBuffer(buffer, MAX_MTU);
@@ -377,14 +371,12 @@ vector<AttributeData> LinuxBluetoothGatt::discoverServices(uint16_t startHandle)
vector<AttributeData> values = AttPdu::parseReadByGroupType(in);
- D << "READ_BY_GROUP_TYPE response has " + to_string(values.size()) + " values";
+ LOG_DEBUG("READ_BY_GROUP_TYPE response has " + to_string(values.size()) + " values");
return values;
}
vector<AttributeData> LinuxBluetoothGatt::discoverCharacteristics(uint16_t startHandle, uint16_t endHandle) {
- DF;
-
shared_ptr<uint8_t> buffer(new uint8_t[MAX_MTU]);
ByteBuffer out = ByteBuffer(buffer, MAX_MTU);
@@ -394,7 +386,7 @@ vector<AttributeData> LinuxBluetoothGatt::discoverCharacteristics(uint16_t start
vector<AttributeData> values = AttPdu::parseReadByType(in);
- D << "READ_BY_TYPE response has " + to_string(values.size()) + " values";
+ LOG_DEBUG("READ_BY_TYPE response has " + to_string(values.size()) + " values");
return values;
}
@@ -405,12 +397,12 @@ vector<AttributeData> LinuxBluetoothGatt::discoverCharacteristics(uint16_t start
LinuxBluetoothAdapter::LinuxBluetoothAdapter(int hciDeviceId) :
scanning(false) {
- DF << "hciDeviceId=" << hciDeviceId;
+ LOG_DEBUG("hciDeviceId=" << hciDeviceId);
this->hciDeviceId = hciDeviceId;
hciSocket = ::hci_open_dev(hciDeviceId);
- D << "HCI socket: " << hciSocket;
+ LOG_DEBUG("HCI socket: " << hciSocket);
if (hciSocket == -1) {
throw BluetoothException(this, "Could not open HCI device " + to_string(hciDeviceId));
@@ -424,8 +416,6 @@ LinuxBluetoothAdapter::LinuxBluetoothAdapter(int hciDeviceId) :
}
LinuxBluetoothAdapter::~LinuxBluetoothAdapter() {
- DF;
-
stopScan();
close(hciSocket);
@@ -436,22 +426,20 @@ LinuxBluetoothAdapter::~LinuxBluetoothAdapter() {
}
void LinuxBluetoothAdapter::startScan() {
- DF;
-
struct hci_dev_info di;
if (hci_devinfo(hciDeviceId, &di) < 0) {
throw BluetoothException(this, "HCI adapter is not up: " + to_string(hciDeviceId));
}
- D << "hciDeviceId.dev_id=" << di.dev_id;
- D << "hciDeviceId.bdaddr=" << parseMac(di.bdaddr).str();
- D << "hciDeviceId.flags=" << setw(8) << setfill('0') << hex << di.flags;
- D << "hciDeviceId.flags RUNNING = " << hci_test_bit(HCI_RUNNING, &di.flags);
- D << "hciDeviceId.flags UP = " << hci_test_bit(HCI_UP, &di.flags);
- D << "hciDeviceId.flags PSCAN = " << hci_test_bit(HCI_PSCAN, &di.flags);
- D << "hciDeviceId.flags ISCAN = " << hci_test_bit(HCI_ISCAN, &di.flags);
- D << "hciDeviceId.name=" << di.name;
+ LOG_DEBUG("hciDeviceId.dev_id=" << di.dev_id);
+ LOG_DEBUG("hciDeviceId.bdaddr=" << parseMac(di.bdaddr).str());
+ LOG_DEBUG("hciDeviceId.flags=" << setw(8) << setfill('0') << hex << di.flags);
+ LOG_DEBUG("hciDeviceId.flags RUNNING = " << hci_test_bit(HCI_RUNNING, &di.flags));
+ LOG_DEBUG("hciDeviceId.flags UP = " << hci_test_bit(HCI_UP, &di.flags));
+ LOG_DEBUG("hciDeviceId.flags PSCAN = " << hci_test_bit(HCI_PSCAN, &di.flags));
+ LOG_DEBUG("hciDeviceId.flags ISCAN = " << hci_test_bit(HCI_ISCAN, &di.flags));
+ LOG_DEBUG("hciDeviceId.name=" << di.name);
int up = hci_test_bit(HCI_UP, &di.flags);
@@ -471,8 +459,6 @@ void LinuxBluetoothAdapter::startScan() {
}
void LinuxBluetoothAdapter::stopScan() {
- DF;
-
if (!scanning) {
return;
}
@@ -480,7 +466,7 @@ void LinuxBluetoothAdapter::stopScan() {
scanning = false;
if (hci_le_set_scan_enable(hciSocket, 0, 0, 1000) < 0) {
- W << "stopScan: hci_le_set_scan_enable: " << errnoAsString();
+ LOG_WARN("stopScan: hci_le_set_scan_enable: " << errnoAsString());
}
}
@@ -516,7 +502,7 @@ void LinuxBluetoothAdapter::runScan(void (*callback)(BluetoothDevice &device)) {
}
if (selected == 0) {
- D << "timeout";
+ LOG_DEBUG("timeout");
// Timeout, just continue
continue;
}
@@ -527,7 +513,7 @@ void LinuxBluetoothAdapter::runScan(void (*callback)(BluetoothDevice &device)) {
evt_le_meta_event *metaEvent = (evt_le_meta_event *) (hciEventBuf + (1 + HCI_EVENT_HDR_SIZE));
len -= (1 + HCI_EVENT_HDR_SIZE);
- D << "metaEvent->subevent = " << std::hex << (int) metaEvent->subevent;
+ LOG_DEBUG("metaEvent->subevent = " << std::hex << (int) metaEvent->subevent);
if (metaEvent->subevent == EVT_LE_ADVERTISING_REPORT) {
le_advertising_info *advertisingInfo = (le_advertising_info *) (metaEvent->data + 1);