-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·369 lines (320 loc) · 12.7 KB
/
main.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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/**
* @file main.cpp
* @author Josep Dols ([email protected])
* @brief Main file of the Weather Station v3 project
* @version 1.0.1
* @date 2019-08-05
*
* @copyright Copyright (c) 2019
*
* Central file of the Weather Station v3 project. From the main() function,
* the data retrieval process is called. After that, a new Observation object
* is created and filled with all the information available.
*
* TODO:
* - Daemonize the execution in order to keep the program executed while the
* Raspberry Pi is on.
* - Integrate the WeatherUndergroun API call from the previous Weather Station
* v2 project.
* - Implement the DB call in order to save the retrieved data.
*/
#include <iostream>
#include <chrono>
#include <thread>
#include <cstring>
#include <libusb-1.0/libusb.h>
#include "data_decoder.h"
#include "main.h"
#include "network_utils.hpp"
//#include "Observation.h"
using namespace std;
/**
* @brief Main function from where the different calls to the modules are done
* and coordinated to obtain the data, process it, and send it to the outside.
*
* @return int Result of the program execution
*/
int main() {
/* Variables declaration */
unsigned char receive_buffer[BUFLEN];
Observation current_obs;
int aux_counter = 0;
int iterCounter = 0;
short int retValue = 0;
bool keepRecording = true;
int uv_index = 0;
bool temp_warn = false;
while (keepRecording)
{
if ((iterCounter % 120) == 0)
{
/* Call to the UV API to get current value */
uv_index = obtain_current_uv_index();
if (uv_index < 0)
uv_index = 0;
}
/* Variables initialization */
current_obs = Observation();
temp_warn = false;
/* Obtain the data from the USB device */
retValue = obtain_usb_data((unsigned char*)&receive_buffer);
if (retValue < 0)
{
this_thread::sleep_for(chrono::seconds(30));
iterCounter++;
continue;
}
/* Process & decode the pressure value */
current_obs.setPressure(decode_pressure(receive_buffer));
cout << "Current PRESSURE is " << current_obs.getPressure() << endl;
if (current_obs.getPressure() < 900 || current_obs.getPressure() > 1100)
{
cerr << "Aborting parsing, invalid values" << endl;
this_thread::sleep_for(chrono::seconds(30));
iterCounter++;
continue;
}
/* Process & decode the temperature values */
aux_counter = 0;
for (auto it : decode_temperature(receive_buffer))
{
current_obs.setTemperature(it, aux_counter++);
cout << "Current TEMPERATURE " << aux_counter-1 << " is " << current_obs.getTemperature(aux_counter-1) << endl;
}
if (current_obs.getTemperature(1) == 0.0)
{
temp_warn = true;
}
/* Process & decode the humidity values */
aux_counter = 0;
for (auto it : decode_humidity(receive_buffer))
{
current_obs.setHumidity(it, aux_counter++);
cout << "Current HUMIDITY " << aux_counter-1 << " is " << current_obs.getHumidity(aux_counter-1) << endl;
}
if (temp_warn &¤t_obs.getHumidity(1) == 0)
{
cerr << "Aborting parsing, strange values" << endl;
this_thread::sleep_for(chrono::seconds(30));
iterCounter++;
continue;
}
/* Process & decode the wind chill value */
current_obs.setWindChill(decode_wind_chill(receive_buffer));
cout << "Current WIND CHILL is " << current_obs.getWindChill() << endl;
/* Process & decode the wind gust value */
current_obs.setWindGust(decode_wind_gust(receive_buffer));
cout << "Current WIND GUST is " << current_obs.getWindGust() << endl;
/* Process & decode the wind speed value */
current_obs.setWindSpeed(decode_wind_speed(receive_buffer));
cout << "Current WIND SPEED is " << current_obs.getWindSpeed() << endl;
/* Process & decode the wind direction value */
current_obs.setWindDir(decode_wind_dir(receive_buffer));
cout << "Current WIND DIR is " << current_obs.getWindDir() << endl;
/* Calculate the dew point from the current observation */
current_obs.calculateDewPoint();
cout << "Calculated DEW POINT is " << current_obs.getDewPoint() << endl;
/* Calculate the RealFeel© from the current observation */
current_obs.calculateRealFeel(uv_index);
cout << "Calculated RealFeel© is " << current_obs.getRealFeel() << endl;
if (current_obs.getRealFeel() > 70)
{
cerr << "Aborting parsing, strange values" << endl;
this_thread::sleep_for(chrono::seconds(30));
iterCounter++;
continue;
}
/* Write into the DB */
write_into_DB(current_obs);
this_thread::sleep_for(chrono::seconds(30));
iterCounter++;
}
cout << "End of the program" << endl;
return 0;
}
/**
* @brief From a USB device, it shows its information
*
* Given a USB device, this function obtains all the available info from
* it, and shows it via the standard console output. Apart from that, it
* iterates over all the available interfaces of the device, showing also
* their information in the console.
*
* @param dev Device from where to obtain the information
*/
void printdev (libusb_device *dev) {
libusb_device_descriptor usbDes;
int pDevDesc = libusb_get_device_descriptor(dev, &usbDes);
if (pDevDesc < 0) {
cout << "Error getting the USB descriptor" << endl;
return;
}
cout << "Number of possible configurations: " << (int)usbDes.bNumConfigurations << " ";
cout << "Device Class: " << (int)usbDes.bDeviceClass << " ";
cout << "VendorID: " << usbDes.idVendor << " ";
cout << "ProductID: " << usbDes.idProduct << endl;
libusb_config_descriptor *config;
libusb_get_config_descriptor(dev, 0, &config);
cout << "Interfaces: " << (int)config->bNumInterfaces << " ||| ";
const libusb_interface *inter;
const libusb_interface_descriptor *interdesc;
const libusb_endpoint_descriptor *epdesc;
for(int i = 0; i < (int)config->bNumInterfaces; i++) {
inter = &config->interface[i];
cout << "Number of alternate settings: " << inter->num_altsetting << " | ";
for(int j = 0; j < inter->num_altsetting; j++) {
interdesc = &inter->altsetting[j];
cout << "Interface Number: " << (int)interdesc->bInterfaceNumber << " | ";
cout << "Number of endpoints: " << (int)interdesc->bNumEndpoints << " | ";
for(int k = 0; k < (int)interdesc->bNumEndpoints; k++) {
epdesc = &interdesc->endpoint[k];
cout << "Descriptor Type: " << (int)epdesc->bDescriptorType << " | ";
cout << "EP Address: " << (int)epdesc->bEndpointAddress << " | ";
}
}
}
cout << endl << endl << endl;
libusb_free_config_descriptor(config);
}
/**
* @brief Function to obtain the info from the USB device
*
* Through this function, via the libusb, a dump signal is sent to the specified
* USB device. After that, the USB device returns a message with the information
* related to the Weather Station, coded into an array of unsigned chars.
*
* @param receive_buffer Buffer where the retreived information is stored.
* @return short int Result of the execution of the function.
*/
short int obtain_usb_data(unsigned char* receive_buffer)
{
libusb_device **devs;
libusb_device_handle *dev_handle;
libusb_context *ctx;
int retValue;
int iInterface;
ssize_t counter;
unsigned char control_data[] = {0x05, 0x0AF, 0x00, 0x00, 0x00, 0x00, 0xAF, 0xFE};
int control_addr = 0x020001;
int transferred_len = 0;
int bytes_transferred = 0;
int total_transferred = 0;
unsigned char crc;
bool finish = false;
int transfer_attempts;
retValue = libusb_init(&ctx);
if (retValue < 0) {
cerr << "Error initializing libusb" << endl;
return 1;
}
//libusb_set_debug(ctx, 3); Deprecated
counter = libusb_get_device_list(ctx, &devs);
if (counter < 0) {
cerr << "Error listing the devices" << endl;
return 2;
}
#ifdef DEBUG
cout << "There are " << counter << " devices in the list" << endl;
for (int i = 0; i < counter; i++) {
printdev(devs[i]);
}
#endif
dev_handle = libusb_open_device_with_vid_pid(ctx, 4400, 26625);
if (dev_handle == NULL) {
cerr << "Cannot open device" << endl;
} else {
#ifdef DEBUG
cout << "Device opened" << endl;
#endif
}
libusb_free_device_list(devs, 1);
retValue = libusb_set_auto_detach_kernel_driver(dev_handle, 1);
if (retValue < 0) {
cerr << " Error auto-setting the kernel detach: " << libusb_strerror(libusb_error(retValue)) << endl;
if (libusb_kernel_driver_active(dev_handle, 0) == 1) {
cerr << "Kernel Driver Active" << endl;
if (libusb_detach_kernel_driver(dev_handle, 0) == 0) {
cerr << "Kernel Driver Detached" << endl;
} else {
cerr << "Error detaching the Kernel Driver" << endl;
}
} else {
cerr << "Kernel Driver not loaded" << endl;
}
}
iInterface = libusb_claim_interface(dev_handle, 0);
if (iInterface < 0) {
cerr << "Cannot Claim Interface (" << libusb_strerror(libusb_error(iInterface)) << ")" << endl;
return 3;
}
#ifdef DEBUG
cout << "Interface claimed" << endl;
#endif
retValue = libusb_set_interface_alt_setting(dev_handle, 0, 0);
if (retValue < 0) {
cerr << "Alternative setting not configured: " << libusb_strerror(libusb_error(retValue)) << endl;
}
transfer_attempts = 0;
do {
// Control transferece
control_data[4] = control_addr / 0x10000;
control_data[3] = ( control_addr - ( control_data[4] * 0x10000 ) ) / 0x100;
control_data[2] = control_addr - ( control_data[4] * 0x10000 ) - ( control_data[3] * 0x100 );
control_data[5] = ( control_data[1] ^ control_data[2] ^ control_data[3] ^ control_data[4] );
retValue = libusb_control_transfer(dev_handle, 0x21 & 0xff, 0x09 & 0xff, 0x0200 & 0xffff, 0x0000 & 0xffff, control_data, 0x08 & 0xffff, 50);
if (retValue < 0) {
cerr << "Error sending request: " << libusb_strerror(libusb_error(retValue)) << endl;
} else {
std::this_thread::sleep_for(std::chrono::milliseconds(300)); // Wait for 30 ms
#ifdef DEBUG
cout << "Message sent" << endl;
#endif
}
// Receive transference
retValue = libusb_interrupt_transfer(dev_handle, 0x81 & 0xff, control_data, 0x8, &transferred_len, 50);
if (retValue < 0) {
//return -4;
cerr << "Error in the receive transfer." << endl;
}
while ( transferred_len > 0) {
std::this_thread::sleep_for(std::chrono::milliseconds(15)); // Wait for 0.15s
bytes_transferred = (int)(control_data[0]);
#ifdef DEBUG
cout << "Control data 0: " << hex << (int)(control_data[0]) << dec << endl;
cout << "Data transferred: " << transferred_len << endl;
#endif
if (( total_transferred + bytes_transferred ) < BUFLEN )
memcpy (receive_buffer + total_transferred, control_data + 1, bytes_transferred);
total_transferred += bytes_transferred;
libusb_interrupt_transfer(dev_handle, 0x81 & 0xff, control_data, 0x8, &transferred_len, 50);
}
#ifdef DEBUG
cout << "Message received" << endl;
for (int i = 0; i < BUFLEN; i++) {
cout << "Char [" << i << "]: ";
cout << hex << static_cast<int>(receive_buffer[i]) << dec << endl;
}
#endif
// CRC Calculation
crc = 0x00;
for (int i = 0; i <= 32; i++ ) {
crc = crc ^ receive_buffer[i];
}
if (crc == receive_buffer[33]){
finish = true;
}
if (crc == 0x5a) {
finish = true;
}
#ifdef DEBUG
cout << "CRC: " << hex << (int)crc << dec << endl;
cout << "Rbuf[33]: " << hex << (int)receive_buffer[33] << dec << endl;
cout << "Rbuf[0]: " << hex << (int)receive_buffer[0] << dec << endl;
#endif
transfer_attempts++;
} while (finish == false && transfer_attempts < 10);
libusb_close(dev_handle);
libusb_exit(ctx);
if (transfer_attempts == 10)
return -1;
}