MyoLinux
gattclient.h
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2  * License, v. 2.0. If a copy of the MPL was not distributed with this
3  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4 
5 #pragma once
6 #ifndef MYOLINUX_GATTCLIENT_H
7 #define MYOLINUX_GATTCLIENT_H
8 
9 #include "myolinux.h"
10 #include "bled112client.h"
11 #include "buffer.h"
12 
13 #include <array>
14 #include <cinttypes>
15 #include <iomanip>
16 #include <iostream>
17 #include <map>
18 
19 namespace MYOLINUX_NAMESPACE {
20 
22 namespace gatt {
23 
24 namespace notifications {
25 const Buffer enable{0x1, 0x0};
26 const Buffer disable{0x0, 0x0};
27 }
28 
32 using Address = std::array<std::uint8_t, 6>;
33 
35 using Characteristics = std::map<Buffer, std::uint16_t>;
36 
38 class DisconnectedException : public std::exception
39 { };
40 
42 class Client {
43 public:
44  Client(const bled112::Client &);
45 
46  void discover(std::function<bool(std::int8_t, Address, Buffer)>);
47  Characteristics characteristics();
48  void connect(const Address &);
49  void connect(const std::string &);
50  bool connected();
51  Address address();
52 
53  void disconnect();
54  void disconnectAll();
55 
56  void writeAttribute(const std::uint16_t, const Buffer &);
57  Buffer readAttribute(const std::uint16_t);
58  void listen(const std::function<void(std::uint16_t, Buffer)> &);
59 
60 private:
61  using Event = std::pair<std::uint16_t, Buffer>;
62 
63  void disconnect(const std::uint8_t);
64 
65  template <typename T>
66  T readResponse();
67 
68  bled112::Client client;
69  bool connected_ = false;
70  Address address_;
71  std::uint8_t connection;
72  std::vector<Event> event_queue;
73 };
74 
75 }
76 
77 void print_address(const gatt::Address &);
78 
79 }
80 
81 #endif // MYOLINUX_GATTCLIENT_H
Class for communication using the BlueGiga protocol.
Definition: bled112client.h:24
std::map< Buffer, std::uint16_t > Characteristics
A dictionary mapping characteristics UUIDs to handles.
Definition: gattclient.h:35
Class for communication using the GATT protocol.
Definition: gattclient.h:42
std::vector< unsigned char > Buffer
Buffer used for packing and unpacking packets.
Definition: buffer.h:16
std::array< std::uint8_t, 6 > Address
Address of the device.
Definition: gattclient.h:32
Exception thrown when the device disconnects.
Definition: gattclient.h:38