-
Notifications
You must be signed in to change notification settings - Fork 1
/
CScanner.cpp
332 lines (282 loc) · 7.85 KB
/
CScanner.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
/*
* Copyright (c) 2012 Alexandros Nikolopoulos <[email protected]>
*
* This program 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 2 of the License, or (at your
* option) any later version.
*/
#include "CScanner.h"
CScanner::CScanner() {
}
CScanner::~CScanner() {
}
CProbe*
CScanner::FindNetwork(void)
{
CProbe *ActiveProbe = 0;
list<int> BlueDevices = EnumBlueTooth();
syslog(LOG_INFO, "Found %lu Bluetooth Adapters", BlueDevices.size());
ActiveProbe = FindSMABlueTooth(BlueDevices);
if(ActiveProbe)
return ActiveProbe;
list<string> SerialPorts = EnumSerialPorts();
syslog(LOG_INFO, "Found %lu Serial Adapters", SerialPorts.size());
if(SerialPorts.size() == 0)
return 0;
syslog(LOG_INFO, "Scanning for Fronius\n");
ActiveProbe = FindFronius(SerialPorts);
if(ActiveProbe)
return ActiveProbe;
syslog(LOG_INFO, "Scanning for Sunergy\n");
ActiveProbe = FindSunergy(SerialPorts);
if(SigTermFlag == true)
return 0;
return ActiveProbe;
}
CProbe*
CScanner::FindSunergy(list<string> &SerialPorts)
{
for (list<string>::iterator Send = SerialPorts.begin(); Send != SerialPorts.end(); Send++)
{
for(speed_t spd = B2400; spd<=B19200; spd++)
{
if(SigTermFlag == true)
return 0;
CModbusMTU *SerialLine = new CModbusMTU(*Send, spd);
switch(spd)
{
case B2400:
syslog(LOG_INFO, "Trying on [%s] @ 2400 bps\n", Send->c_str());
break;
case B4800:
syslog(LOG_INFO, "Trying on [%s] @ 4800 bps\n", Send->c_str());
break;
case B9600:
syslog(LOG_INFO, "Trying on [%s] @ 9600 bps\n", Send->c_str());
break;
case B19200:
syslog(LOG_INFO, "Trying on [%s] @ 19200 bps\n", Send->c_str());
break;
default:
syslog(LOG_INFO, "Trying on [%s] @ ???? bps\n", Send->c_str());
break;
}
if(!SerialLine->Connect())
{
syslog(LOG_INFO, "cannot open serial\n");
delete(SerialLine);
continue;
}
CSunergyProbe *CurrProbe = new CSunergyProbe(SerialLine, "12345");
if(!CurrProbe->Start())
{
delete(CurrProbe);
usleep(10000);
continue;
}
else
{
syslog(LOG_INFO, "Found Sunergy Inverters on [%s]", Send->c_str());
return dynamic_cast<CProbe *>(CurrProbe);
//return 0;
}
}
}
return 0;
}
CProbe*
CScanner::FindFronius(list<string> &SerialPorts)
{
for (list<string>::iterator Send = SerialPorts.begin(); Send != SerialPorts.end(); Send++)
{
for (list<string>::iterator Receive = SerialPorts.begin(); Receive != SerialPorts.end(); Receive++)
{
if(SigTermFlag == true)
return 0;
for(speed_t spd = B2400; spd<=B19200; spd++)
{
CSerial *SerialLine = new CSerial(*Send, spd, *Receive, spd);
if(debugFlag == true)
{
switch(spd)
{
case B2400:
syslog(LOG_INFO, "Trying on [%s]-[%s] @ 2400 bps\n", Send->c_str(), Receive->c_str());
break;
case B4800:
syslog(LOG_INFO, "Trying on [%s]-[%s] @ 4800 bps\n", Send->c_str(), Receive->c_str());
break;
case B9600:
syslog(LOG_INFO, "Trying on [%s]-[%s] @ 9600 bps\n", Send->c_str(), Receive->c_str());
break;
case B19200:
syslog(LOG_INFO, "Trying on [%s]-[%s] @ 19200 bps\n", Send->c_str(), Receive->c_str());
break;
default:
syslog(LOG_INFO, "Trying on [%s]-[%s] @ ???? bps\n", Send->c_str(), Receive->c_str());
break;
}
}
if(!SerialLine->Connect())
{
syslog(LOG_INFO, "cannot open serial\n");
delete(SerialLine);
continue;
}
CFroniusProbe *CurrProbe = new CFroniusProbe(dynamic_cast<CInterface *>(SerialLine), "12345");
if(!CurrProbe->Start())
{
delete(CurrProbe);
usleep(10000);
continue;
}
else
{
syslog(LOG_INFO, "Found Fronius Inverters on [%s]-[%s]", Send->c_str(), Receive->c_str());
return dynamic_cast<CProbe *>(CurrProbe);
}
}
}
}
return 0;
}
CProbe*
CScanner::FindSMABlueTooth(list<int> &BlueDevices)
{
for (list<int>::iterator BTDev = BlueDevices.begin(); BTDev != BlueDevices.end(); BTDev++)
{
if(SigTermFlag == true)
return 0;
syslog(LOG_INFO, "Scanning for BlueTooth SMA inverters on device hci%d\n", *BTDev);
inquiry_info *ii = NULL;
int max_rsp, num_rsp;
int dev_id = 0, sock, len, flags;
int i;
char addr[19] = { 0 };
char name[248] = { 0 };
sock = hci_open_dev( *BTDev );
if (sock < 0)
{
perror("opening socket");
return 0;
}
len = 8;
max_rsp = 255;
flags = IREQ_CACHE_FLUSH;
ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));
num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);
if( num_rsp < 0 )
perror("hci_inquiry");
for (i = 0; i < num_rsp; i++)
{
ba2str(&(ii+i)->bdaddr, addr);
memset(name, 0, sizeof(name));
if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name), name, 0) < 0)
strcpy(name, "[unknown]");
if(strstr(name, "SMA"))
syslog(LOG_INFO, "Found SMA at: [%s] (%s) - hci%d\n", addr, name, dev_id);
}
close( sock );
free( ii );
// Sleep while the bt stack cleans up the whole scanning
// thing before trying to connect or it gives an error
sleep(3);
CBlueTooth *BtConnx;//= new CBlueTooth(addr);
if(!BtConnx->Connect())
{
syslog(LOG_INFO, "cannot connect to %s\n", addr);
delete(BtConnx);
continue;
}
else
syslog(LOG_INFO, "Connected to %s\n", addr);
CSmaProbe *CurrProbe = new CSmaProbe(BtConnx, "12345");
if(!CurrProbe->Start())
{
delete(CurrProbe);
usleep(10000);
continue;
}
else
return dynamic_cast<CProbe *>(CurrProbe);
}
return 0;
}
list<string>
CScanner::EnumSerialPorts(void)
{
struct udev *udev;
struct udev_enumerate *enumerate;
struct udev_list_entry *devices, *dev_list_entry;
struct udev_device *dev;
list<string> PortList;
udev = udev_new();
if (!udev)
{
syslog(LOG_ERR, "Can't create udev object\n");
return PortList;
}
enumerate = udev_enumerate_new(udev);
udev_enumerate_add_match_subsystem(enumerate, "tty");
udev_enumerate_scan_devices(enumerate);
devices = udev_enumerate_get_list_entry(enumerate);
udev_list_entry_foreach(dev_list_entry, devices)
{
const char *path;
path = udev_list_entry_get_name(dev_list_entry);
dev = udev_device_new_from_syspath(udev, path);
if(!dev || !udev_device_get_devnode(dev))
continue;
string PortPath;
PortPath = udev_device_get_devnode(dev);
dev = udev_device_get_parent(dev);
if (!dev)
continue;
if(udev_device_get_subsystem(dev) && !strcmp(udev_device_get_subsystem(dev), "usb-serial"))
{
syslog(LOG_INFO, "Found USB-Serial on %s\n", PortPath.c_str());
PortList.push_back(PortPath);
}
else if(udev_device_get_driver(dev))
{
const char *driver = udev_device_get_driver(dev);
if(!strcmp(driver, "uart-pl011") || !strcmp(driver, "uart-pl010"))
{
syslog(LOG_INFO, "Found ARM AMBA-type serial port on %s\n", PortPath.c_str());
PortList.push_back(PortPath);
}
else if(!strcmp(driver, "atmel_usart"))
{
syslog(LOG_INFO, "Found Atmel AT91 / AT32 Serial on %s\n", PortPath.c_str());
PortList.push_back(PortPath);
}
else if(!strcmp(driver, "serial"))
{
if(udev_device_get_subsystem(dev) && !strcmp(udev_device_get_subsystem(dev), "pnp"))
{
syslog(LOG_INFO, "Found 16550A Serial on %s\n", PortPath.c_str());
PortList.push_back(PortPath);
}
}
}
udev_device_unref(dev);
}
/* Free the enumerator object */
udev_enumerate_unref(enumerate);
udev_unref(udev);
return PortList;
}
static list<int> BlueToothDevices;
static int dev_info(int s, int dev_id, long arg)
{
BlueToothDevices.push_back(dev_id);
return 0;
}
list<int>
CScanner::EnumBlueTooth(void)
{
BlueToothDevices.clear();
hci_for_each_dev(HCI_UP, dev_info, 0);
return BlueToothDevices;
}