#include #include #include #include "Bluetooth.h" namespace trygvis { using namespace std; // ----------------------------------------------------------------------- // Mac // ----------------------------------------------------------------------- string Mac::str() const { std::ostringstream buf; buf << setw(2) << hex << setfill('0') << (int) bytes[5] << ":" << setw(2) << hex << setfill('0') << (int) bytes[4] << ":" << setw(2) << hex << setfill('0') << (int) bytes[3] << ":" << setw(2) << hex << setfill('0') << (int) bytes[2] << ":" << setw(2) << hex << setfill('0') << (int) bytes[1] << ":" << setw(2) << hex << setfill('0') << (int) bytes[0]; return buf.str(); } bool Mac::operator==(Mac &other) const { return memcmp(bytes, other.bytes, sizeof(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) { uint8_t bytes[6]; sscanf("%02x:%02x:%02x:%02x:%02x:%02x", s.c_str(), &bytes[0], &bytes[1], &bytes[2], &bytes[3], &bytes[4], &bytes[5]); return new Mac(bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], bytes[5]); } AttPdu::AttPdu(ByteBuffer &bytes) : bytes(bytes) { } AttPdu::AttPdu(ByteBuffer &bytes, AttPduType type) : bytes(bytes) { bytes.add8(type); } AttPduType AttPdu::getType() { return (AttPduType) bytes.get8(0); } AttPdu AttPdu::parse(ByteBuffer & bytes) { if(size == 0) { throw BluetoothException("PDU is too small"); } AttPdu pdu = AttPdu(bytes, size); AttPduType type = pdu.getType(); switch (type) { case READ_BY_GROUP_TYPE_RES: if (size < 4) { throw BluetoothException("Bad READ_BY_GROUP_TYPE_RES packet, expected at least 4 octets, got " + size); } return pdu; default: throw BluetoothException("Uknown PDU type: " + type); } return pdu; } // ----------------------------------------------------------------------- // Adapter // ----------------------------------------------------------------------- BluetoothAdapter::~BluetoothAdapter() { } };