forked from f1xpl/openauto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHFDeviceService.cpp
135 lines (108 loc) · 3.62 KB
/
HFDeviceService.cpp
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
/*
* This file is part of openauto project.
* Copyright (C) 2018 f1x.studio (Michal Szwaj)
*
* openauto is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* openauto is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with openauto. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/trim.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/range/algorithm/transform.hpp>
#include <boost/foreach.hpp>
#include <boost/bind.hpp>
#include <f1x/openauto/autoapp/Service/HFDeviceService.hpp>
namespace f1x
{
namespace openauto
{
namespace autoapp
{
namespace service
{
std::map<std::string, std::string> HFDeviceService::getDevices(){
std::map <std::string, std::string> devices;
std::string command = "get_devices.sh";
std::string fullPath = this->bluetoothHelperPath + command;
std::string devicesString = this->exec(fullPath.c_str());
boost::trim(devicesString);
std::vector<std::string> tokens;
boost::split(tokens, devicesString, boost::is_any_of("\n"));
std::for_each(tokens.begin(), tokens.end(),
boost::bind(&boost::trim<std::string>,
_1, std::locale()));
BOOST_FOREACH(const std::string& i, tokens) {
devices.insert(std::pair<std::string, std::string>(i.substr(0,17), i.substr(18)));
}
return devices;
}
std::string getDefaultDevice() {
return "Test";
}
void HFDeviceService::pair(std::string bdAddr)
{
std::string command = "pair.sh " + bdAddr;
std::string fullPath = this->bluetoothHelperPath + command;
std::string pairResult = this->exec(fullPath.c_str());
std::cout << pairResult << std::endl;
}
void HFDeviceService::removeDevice(std::string bdAddr)
{
std::string command = "remove.sh " + bdAddr;
std::string fullPath = this->bluetoothHelperPath + command;
std::string result = this->exec(fullPath.c_str());
std::cout << result << std::endl;
}
void HFDeviceService::connect(std::string bdAddr)
{
std::string command = "connect.sh " + bdAddr;
std::string fullPath = this->bluetoothHelperPath + command;
std::string result = this->exec(fullPath.c_str());
std::cout << result << std::endl;
}
void HFDeviceService::disconnect(std::string bdAddr)
{
std::string command = "disconnect.sh " + bdAddr;
std::string fullPath = this->bluetoothHelperPath + command;
std::string result = this->exec(fullPath.c_str());
std::cout << result << std::endl;
}
void HFDeviceService::info(std::string bdAddr)
{
std::string command = "info.sh " + bdAddr;
std::string fullPath = this->bluetoothHelperPath + command;
std::string result = this->exec(fullPath.c_str());
std::cout << result << std::endl;
}
std::string HFDeviceService::exec(const char *cmd)
{
FILE *pipe = popen(cmd, "r");
if (!pipe)
return "ERROR";
char buffer[128];
std::string result = "";
while (!feof(pipe))
{
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
pclose(pipe);
return result;
}
} // namespace service
} // namespace autoapp
} // namespace openauto
} // namespace f1x