-
Notifications
You must be signed in to change notification settings - Fork 1
/
Bluetooth.h
160 lines (132 loc) · 3.71 KB
/
Bluetooth.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
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
/**
* @file Bluetooth.h
* @Author BeeeOn team (MS)
* @date October, 2016
* @brief Availability monitoring Bluetooth devices
*/
#ifndef BLUETOOTH_H
#define BLUETOOTH_H
extern bool quit_global_flag;
#include <atomic>
#include <list>
#include <string>
#include <vector>
#include <Poco/Mutex.h>
#include "main.h"
#include "ModuleADT.h"
#include "utils.h"
class Aggregator;
class Bluetooth : public ModuleADT {
public:
/**
* Set initial values and get informations from a configuration file
*/
Bluetooth(IOTMessage msg, std::shared_ptr<Aggregator> agg);
/**
* Processing command from the server
*/
void parseCmdFromServer(const Command& cmd) override;
private:
unsigned int m_wake_up_time;
std::list<std::string> m_MAC_list; // List of mac address saved locally
/**
* @brief m_queries
* Number of attempts to get a list of paired devices from the server
* Number decreases with each attempt, or resets immediately in case of success.
*/
std::atomic<int> m_queries;
std::string m_ifname; // bluetooth interface name for hcitool
Poco::Mutex m_mutex_msg;
Poco::Mutex m_mutex_exec;
/**
* Periodically send "data" messages with status Bluetooth device
*
* Naturally end when quit_global_flag is set to true,
* so the Adaapp is terminating
*/
void threadFunction() override;
/**
* Try turn on the USB dongle
*/
bool turnOnDongle();
/**
* Packing data to struct for send
*/
IOTMessage createMsg(const std::string &mac, float value, const std::string &name = "");
/**
* Checks states of paired devices and sends to server
*/
void checkAndSendDevices();
/**
* Basic Bash command execution
* @param result - stdout from program in terminal
* @param first - program
* @param second - first agrument for program
* @param third - second agrument for program
* @return exit_code
* return 0 => OK
* return n > 0 => Some error
*/
int exec(std::string &result, const std::string &first, const std::string &second = "", const std::string &third = "");
/**
* Send new device (found by scan of bluetooth network) to server.
*/
void sendNewDeviceToServer(const std::string &mac_address, const std::string &device_name);
/**
* Parse and process the stdout message from hcitool scan.
* Example stdout:
Scanning ...
AA:BB:CC:DD:EE:FF Device_name
*/
void parseScanAndSend(const std::string &scan);
/**
* Full scan of bluetooth net
* Result is list of MAC addresses and their names
*/
void fullScan();
/**
* Search the list for MAC
*/
bool hasMac(const std::string &mac);
/**
* Test if euid is in list with paired devices
*/
bool isInList(euid_t euid);
/**
* Insert colons between each pair of string to create MAC in correct format aa:bb:cc:dd:ee:ff
*/
std::string insertColons(const std::string &input);
/**
* Convert (string) MAC address to (euid_t) EUID
* Remove ":" and convert to euid
*/
euid_t extractEUID(std::string mac);
/**
* Convert EUID (hex number) to MAC address (string)
* Cuts prefix and insert ":".
*/
std::string extractMAC(euid_t euid);
/**
* Distribution of string into the vector by divide string
*/
void split(std::vector<std::string> &output, const std::string &input, const std::string &div);
/**
* Reset atomic value m_queries
* Adaapp will not ask more (until the next scan)
*/
void resetQueries();
/**
* If MAC is valid & is'n in list, than add to list
*/
void addIfValid(const std::string &mac);
/**
* Remove MAC address from local list, if exist
*/
void removeIfExist(const std::string &mac);
/**
* Processes list of paired devices from server
* If exists any new item, add them to local list for scan state
*/
void listFromServer(const CmdParam ¶m);
};
#endif //BLUETOOTH_H