-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEZB.cpp
280 lines (218 loc) · 6.18 KB
/
EZB.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include "EZB.h"
#include "version.h"
EZB::EZB(){
Servo = NULL;
Digital = NULL;
ADC = NULL;
Configuration = NULL;
m_mac_address = NULL;
m_connected = false;
m_socket = 0;
m_keepalive[0] = EZB::Ping;
m_verbose = true;
m_firmware = -1;
strcpy(m_firmware_str, "");
m_lastcommand_time = time(NULL);
m_exit = false;
}
EZB::~EZB(){
m_exit = true;
if(m_keepalive_thread){
#ifdef _WINDOWS
WaitForSingleObject(m_keepalive_thread, INFINITE);
#else
pthread_join(m_keepalive_thread, NULL);
#endif
m_keepalive_thread = 0;
}
Disconnect();
if(m_mac_address){
free(m_mac_address);
m_mac_address = NULL;
}
}
void EZB::Disconnect(){
if(m_socket){
close(m_socket);
m_socket = 0;
}
}
void EZB::Connect(char* mac_address){
if(m_connected)
throw std::runtime_error("Already connected");
if(mac_address){
m_mac_address = (char*)malloc(sizeof(char) * (strlen(mac_address)+1));
strcpy(m_mac_address, mac_address);
}else{
}
// allocate a socket
m_socket = socket(AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM);
// set the connection parameters (who to connect to)
#ifdef _WINDOWS
m_addr.addressFamily = AF_BLUETOOTH;
m_addr.port = 1;
str2ba(m_mac_address, &m_addr.btAddr);
#else
m_addr.rc_family = AF_BLUETOOTH;
m_addr.rc_channel = (uint8_t) 1;
str2ba( m_mac_address, &m_addr.rc_bdaddr );
#endif
// connect to server
int status = connect(m_socket, (struct sockaddr *)&m_addr, sizeof(m_addr));
if(status < 0){
char error[512];
sprintf(error, "%s", strerror(errno));
throw std::runtime_error(error);
}
CreateObjects();
#ifdef _WINDOWS
InitializeCriticalSection(&m_send_mutex);
#else
pthread_mutex_init(&m_send_mutex, NULL);
#endif
m_connected = true;
SendCommand(EZB::Ping);
usleep(1000000);
if(!KeepAlive())
throw std::runtime_error("Controller not responding");
#ifdef _WINDOWS
m_keepalive_thread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ConnectionCheckStub, (void*)this, NULL, 0);
#else
pthread_create(&m_keepalive_thread, NULL, ConnectionCheckStub, (void*)this);
#endif
}
void EZB::CreateObjects(){
ADC = new ADCClass(this);
Configuration = new ConfigurationClass(this);
Digital = new DigitalClass(this);
HCSR04 = new HC_SR04Class(this);
I2C = new I2CClass(this);
Movement = new MovementClass(this);
PWM = new PWMClass(this);
Servo = new ServoClass(this);
SureDualAxisCompass = new SureDualAxisCompassClass(this);
Tone = new ToneClass(this);
Uart = new UartClass(this);
}
bool EZB::IsConnected(){
return m_connected;
}
char* EZB::GetFirmwareVersion(){
if(m_firmware == 0)
sprintf(m_firmware_str, "Unknown Firmware");
else
sprintf(m_firmware_str, "EZ-B Firmware V%.1f", m_firmware);
return m_firmware_str;
}
double EZB::GetFirmwareVersionRaw(){
return m_firmware;
}
void EZB::SetVerboseLogging(bool verbose){
m_verbose = verbose;
}
void EZB::SendCommand(unsigned char command){
SendCommand(command, NULL, 0, 0);
}
void EZB::SendCommand(unsigned char command, unsigned char* args, int num_args){
SendCommand(command, args, num_args, 0);
}
unsigned char* EZB::SendCommand(unsigned char command, int expected_ret_bytes){
return SendCommand(command, NULL, 0, expected_ret_bytes);
}
unsigned char* EZB::SendCommand(unsigned char command, unsigned char* args, int num_args, int expected_ret_bytes){
#ifdef _WINDOWS
EnterCriticalSection(&m_send_mutex);
#else
pthread_mutex_lock(&m_send_mutex);
#endif
unsigned char* bytestosend = (unsigned char*)malloc(sizeof(unsigned char) * (1 + num_args));
bytestosend[0] = command;
for(int i = 0; i < num_args; i++)
bytestosend[i+1] = args[0];
if(m_verbose){
printf("Sending: ");
for(int i = 0; i <= num_args; i++)
printf("%d ", bytestosend[i]);
printf("\n");
}
if(command == EZB::Unknown)
throw std::runtime_error("Unknown command");
else if(!m_connected)
throw std::runtime_error("Not connected");
send(m_socket, (char*)bytestosend, num_args+1, 0);
struct timespec now;
clock_gettime(1, &now);
m_lastcommand_time = (now.tv_sec * 1000) + (now.tv_nsec / 1000000);
free(bytestosend);
unsigned char* retval = NULL;
if(expected_ret_bytes){
if(m_verbose)
printf("Expecting bytes: %d\n", expected_ret_bytes);
retval = new unsigned char[expected_ret_bytes];
memset(retval, 0, expected_ret_bytes);
recv(m_socket, (char*)retval, expected_ret_bytes, 0);
if(m_verbose){
printf("Received: ");
for(int i = 0; i < expected_ret_bytes; i++)
printf("%d ", retval[i]);
printf("\n");
}
}
#ifdef _WINDOWS
LeaveCriticalSection(&m_send_mutex);
#else
pthread_mutex_unlock(&m_send_mutex);
#endif
return retval;
}
void EZB::SetLEDStatus(bool status){
unsigned char arg[1];
if(status)
arg[0] = 1;
else
arg[0] = 0;
SendCommand(EZB::StatusLED, arg, 1);
}
bool EZB::KeepAlive(){
unsigned char* retval = SendCommand(EZB::Ping, 1);
if(retval[0] <= 15){
m_firmware = retval[0];
}else{
m_firmware = (double)((double)retval[0] / 10);
}
delete [] retval;
return m_firmware != 0;
}
void EZB::ConnectionCheck(){
while(!m_exit){
if(m_lastcommand_time + (KEEP_ALIVE_INTERVAL/1000) <= time(NULL) && !KeepAlive()){
m_connected = false;
break;
}
usleep(KEEP_ALIVE_INTERVAL * 1000);
}
}
const char* EZB::VersionNumber(){
return STR_EZ_OPENBOT_VERSION_NUM;
}
void* ConnectionCheckStub(void* lParam){
((EZB*)lParam)->ConnectionCheck();
return NULL;
}
#ifdef _WINDOWS
/* Convert mac address string to BTH_ADDR */
int str2ba(const char* straddr, BTH_ADDR* btaddr)
{
int i;
unsigned int aaddr[6 ];
BTH_ADDR tmpaddr = 0;
if (sscanf(straddr, "%02x:%02x:%02x:%02x:%02x:%02x", &aaddr[0], &aaddr[1], &aaddr[2], &aaddr[3], &aaddr[4], &aaddr[5]) != 6)
return -1;
*btaddr = 0;
for (i = 0; i < 6; i++) {
tmpaddr = (BTH_ADDR) (aaddr[i ] & 0xff);
*btaddr = ((*btaddr) << 8) + tmpaddr;
}
return 0;
}
#endif