forked from adiesner/GarminPlugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeviceManager.cpp
More file actions
309 lines (270 loc) · 11.6 KB
/
deviceManager.cpp
File metadata and controls
309 lines (270 loc) · 11.6 KB
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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
* GarminPlugin
* Copyright (C) Andreas Diesner 2010 <andreas.diesner [AT] gmx [DOT] de>
*
* GarminPlugin 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.
*
* GarminPlugin 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "deviceManager.h"
#include "log.h"
#include <mntent.h>
#include <dirent.h>
#include "edge705Device.h"
#include "oregonDevice.h"
#include "edge305Device.h"
#include "sdCardDevice.h"
DeviceManager::DeviceManager()
{
}
DeviceManager::~DeviceManager() {
if (Log::enabledDbg()) Log::dbg("DeviceManager destructor");
while (gpsDeviceList.size() > 0)
{
GpsDevice *dev = gpsDeviceList.back();
gpsDeviceList.pop_back();
delete(dev);
}
}
const std::string DeviceManager::getDevicesXML()
{
// <?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\" ?>\n<Devices xmlns=\"http://www.garmin.com/xmlschemas/PluginAPI/v1\">\n<Device DisplayName=\"Oregon (/mnt/Oregon/)\" Number=\"0\"/>\n</Devices>\n
TiXmlDocument doc;
TiXmlDeclaration * decl = new TiXmlDeclaration( "1.0", "UTF-8", "no" );
TiXmlElement * devices = new TiXmlElement( "Devices" );
devices->SetAttribute("xmlns", "http://www.garmin.com/xmlschemas/PluginAPI/v1");
int deviceCount = 0;
vector<GpsDevice*>::iterator it=gpsDeviceList.begin();
while(it != gpsDeviceList.end()){
// Delete devices that are no longer available
if( !(*it)->isDeviceAvailable() ){
delete *it;
it = gpsDeviceList.erase(it);
continue;
} else {
TiXmlElement *device = new TiXmlElement ( "Device" );
device->SetAttribute("DisplayName", (*it)->getDisplayName());
device->SetAttribute("Number", deviceCount);
devices->LinkEndChild( device );
deviceCount++;
}
++it;
}
if (Log::enabledDbg()) {
std::ostringstream dbgOut;
dbgOut << "getDeviceXML returns " << deviceCount << " devices";
Log::dbg(dbgOut.str());
}
doc.LinkEndChild( decl );
doc.LinkEndChild( devices );
TiXmlPrinter printer;
printer.SetIndent( "\t" );
doc.Accept( &printer );
string str = printer.Str();
return str;
}
void DeviceManager::startFindDevices() {
// Think about putting this routine into a thread when devices will be supported that take more time to search for
// Remove active devices
while (gpsDeviceList.size() > 0)
{
GpsDevice *dev = gpsDeviceList.back();
gpsDeviceList.pop_back();
delete(dev);
}
FILE *mounts = NULL;
struct mntent *ent = NULL;
mounts = setmntent("/etc/mtab", "r");
Log::dbg("Searching for Edge705/Oregon300/...");
while ( (ent = getmntent(mounts)) != NULL ) {
string filesystype = ent->mnt_type;
if (filesystype.compare("vfat") == 0) {
string mountPath = ent->mnt_dir;
DIR *dp;
struct dirent *dirp;
if((dp = opendir(mountPath.c_str())) == NULL) {
Log::err("Error opening directory: "+mountPath);
break;
}
bool garminDirFound = false;
while ((dirp = readdir(dp)) != NULL) {
string dir = string(dirp->d_name);
if (dir.compare("Garmin") == 0) {
garminDirFound = true;
break;
}
}
closedir(dp);
if (garminDirFound) {
string fullPath = mountPath + "/Garmin/GarminDevice.xml";
TiXmlDocument doc(fullPath);
if (doc.LoadFile()) {
// Perfect, seems to be a Garmin Device
TiXmlElement * node = doc.FirstChildElement("Device");
if (node!=NULL) { node = node->FirstChildElement("Model"); }
if (node!=NULL) { node = node->FirstChildElement("Description"); }
if (node!=NULL) {
string deviceName = node->GetText();
GpsDevice * device = NULL;
string::size_type position = deviceName.find( "Oregon", 0 );
if (position == string::npos) {
// Treat the Dakota Model the same as an Oregon Model
position = deviceName.find( "Dakota", 0 );
}
if ((device == NULL) && (position != string::npos)) { // Found Oregon in deviceName
OregonDevice * oregon = new OregonDevice();
oregon->setBaseDirectory(mountPath);
oregon->setDeviceDescription(&doc);
oregon->setDisplayName(deviceName);
device = oregon;
}
position = deviceName.find( "EDGE", 0 );
if ((device == NULL) && (position != string::npos)) {
Edge705Device * edge = new Edge705Device();
edge->setBaseDirectory(mountPath);
edge->setDeviceDescription(&doc);
edge->setDisplayName(deviceName);
device = edge;
}
if (device != NULL) {
Log::dbg("Found "+deviceName+" at "+mountPath);
gpsDeviceList.push_back(device);
} else {
Log::err("Unknown device "+deviceName+" at "+mountPath);
}
} else {
Log::err("GarminDevice.xml has unexpected format!");
}
} else {
Log::err("Not yet implemented new SD-Card"); //@TODO
}
} else {
Log::dbg("Garmin directory not found at "+mountPath);
}
}
}
bool searchGarmin = true;
if (this->configuration != NULL) {
TiXmlElement * pRoot = this->configuration->FirstChildElement( "GarminPlugin" );
TiXmlElement * settings = NULL;
TiXmlElement * ftools = NULL;
if (pRoot != NULL) { settings = pRoot->FirstChildElement("Settings"); }
if (settings != NULL) { ftools = settings->FirstChildElement("ForerunnerTools"); } else { Log::dbg("settings is null!"); }
if (ftools != NULL) {
const char * ftoolsEnabled = ftools->Attribute("enabled");
if (ftoolsEnabled != NULL) {
string enabledStr = ftoolsEnabled;
if ((enabledStr == "yes") || (enabledStr == "YES") || (enabledStr == "true") || (enabledStr == "TRUE") || (enabledStr == "1")) {
searchGarmin = true;
} else {
searchGarmin = false;
}
} else {
Log::dbg("ftoolsEnabled is null!");
}
} else {
Log::dbg("ftools is null!");
}
}
string deviceName;
if (searchGarmin) {
// Search for garmin 305
deviceName = Edge305Device::getAttachedDeviceName();
if (deviceName.length() > 0) { // Found a device
Log::dbg("Found device via garmintools: "+deviceName);
Edge305Device * device = new Edge305Device(deviceName);
gpsDeviceList.push_back(device);
}
} else {
Log::dbg("Search via garmintools is disabled!");
}
// Now create virtual SD Card devices from configuration
if (this->configuration != NULL) {
TiXmlElement * pRoot = this->configuration->FirstChildElement( "GarminPlugin" );
if (pRoot != NULL) {
TiXmlElement * devices = pRoot->FirstChildElement("Devices");
TiXmlElement * device = devices->FirstChildElement("Device");
while ( device != NULL )
{
string storagePath = "";
string storageCmd = "";
string fitnessPath = "";
TiXmlElement * dir = device->FirstChildElement("StoragePath");
if ((dir) && (dir->GetText() != NULL)) {
storagePath = dir->GetText();
}
TiXmlElement * cmd = device->FirstChildElement("StorageCommand");
if ((cmd) && (cmd->GetText() != NULL)) {
storageCmd = cmd->GetText();
}
TiXmlElement * fitness = device->FirstChildElement("FitnessDataPath");
if ((fitness) && (fitness->GetText() != NULL)) {
fitnessPath = fitness->GetText();
}
GpsDevice * currentDevice = NULL;
TiXmlElement * name = device->FirstChildElement("Name");
if (name!=NULL) {
if (name->GetText() != NULL) {
for(unsigned int i=0; i < gpsDeviceList.size(); i++)
{
// Device exists, and is configured in configuration
if (gpsDeviceList[i]->getDisplayName().compare(name->GetText()) == 0) {
currentDevice = gpsDeviceList[i];
}
}
if (currentDevice == NULL) { // no device found
deviceName = name->GetText();
Log::info("Creating new SD Card Device from configuration: "+deviceName);
SDCardDevice * sdcard = new SDCardDevice();
sdcard->setDisplayName(name->GetText());
sdcard->setBaseDirectory("");
sdcard->setDeviceDescription(SDCardDevice::getDefaultConfiguration(deviceName, storagePath, fitnessPath));
if (sdcard->isDeviceAvailable()) {
currentDevice = sdcard;
gpsDeviceList.push_back(currentDevice);
} else {
delete(sdcard);
currentDevice = NULL;
Log::dbg("Device "+deviceName+" is not available.");
}
}
}
}
if ((storageCmd.length() > 0) && (currentDevice!=NULL)) {
Log::dbg("Setting Storage Command for "+currentDevice->getDisplayName()+": "+storageCmd);
currentDevice->setStorageCommand(storageCmd);
}
device = device->NextSiblingElement( "Device" );
}
}
}
std::ostringstream infoOut;
infoOut << "Number of devices found: " << gpsDeviceList.size();
Log::info(infoOut.str());
}
void DeviceManager::setConfiguration(TiXmlDocument * config) {
// Memory will be freed from configManager
this->configuration = config;
}
void DeviceManager::cancelFindDevices() {
}
int DeviceManager::finishedFindDevices() {
return 1;
}
GpsDevice * DeviceManager::getGpsDevice(int number)
{
if (number < (int)gpsDeviceList.size()) {
return gpsDeviceList[number];
}
return NULL;
}