summaryrefslogtreecommitdiffstats
path: root/nci/jni/RouteDataSet.cpp
blob: 1458776bb1c788cee9eb9dc073d13e4e11482700 (plain)
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
 *  Import and export general routing data using a XML file.
 */
#include "OverrideLog.h"
#include "RouteDataSet.h"
#include "libxml/xmlmemory.h"
#include <errno.h>
#include <sys/stat.h>

extern char bcm_nfc_location[];


/*******************************************************************************
**
** Function:        AidBuffer
**
** Description:     Parse a string of hex numbers.  Store result in an array of
**                  bytes.
**                  aid: string of hex numbers.
**
** Returns:         None.
**
*******************************************************************************/
AidBuffer::AidBuffer (std::string& aid)
:   mBuffer (NULL),
    mBufferLen (0)
{
    unsigned int num = 0;
    const char delimiter = ':';
    std::string::size_type pos1 = 0;
    std::string::size_type pos2 = aid.find_first_of (delimiter);

    //parse the AID string; each hex number is separated by a colon;
    mBuffer = new UINT8 [aid.length()];
    while (true)
    {
        num = 0;
        if (pos2 == std::string::npos)
        {
            sscanf (aid.substr(pos1).c_str(), "%x", &num);
            mBuffer [mBufferLen] = (UINT8) num;
            mBufferLen++;
            break;
        }
        else
        {
            sscanf (aid.substr(pos1, pos2-pos1+1).c_str(), "%x", &num);
            mBuffer [mBufferLen] = (UINT8) num;
            mBufferLen++;
            pos1 = pos2 + 1;
            pos2 = aid.find_first_of (delimiter, pos1);
        }
    }
}


/*******************************************************************************
**
** Function:        ~AidBuffer
**
** Description:     Release all resources.
**
** Returns:         None.
**
*******************************************************************************/
AidBuffer::~AidBuffer ()
{
    delete [] mBuffer;
}


/*******************************************************************************/
/*******************************************************************************/


const char* RouteDataSet::sConfigFile = "/param/route.xml";


/*******************************************************************************
**
** Function:        ~RouteDataSet
**
** Description:     Release all resources.
**
** Returns:         None.
**
*******************************************************************************/
RouteDataSet::~RouteDataSet ()
{
    deleteDatabase ();
}


/*******************************************************************************
**
** Function:        initialize
**
** Description:     Initialize resources.
**
** Returns:         True if ok.
**
*******************************************************************************/
bool RouteDataSet::initialize ()
{
    static const char fn [] = "RouteDataSet::initialize";
    ALOGD ("%s: enter", fn);
    //check that the libxml2 version in use is compatible
    //with the version the software has been compiled with
    LIBXML_TEST_VERSION
    ALOGD ("%s: exit; return=true", fn);
    return true;
}


/*******************************************************************************
**
** Function:        deleteDatabase
**
** Description:     Delete all routes stored in all databases.
**
** Returns:         None.
**
*******************************************************************************/
void RouteDataSet::deleteDatabase ()
{
    static const char fn [] = "RouteDataSet::deleteDatabase";
    ALOGD ("%s: default db size=%u; sec elem db size=%u", fn, mDefaultRouteDatabase.size(), mSecElemRouteDatabase.size());
    Database::iterator it;

    for (it = mDefaultRouteDatabase.begin(); it != mDefaultRouteDatabase.end(); it++)
        delete (*it);
    mDefaultRouteDatabase.clear ();

    for (it = mSecElemRouteDatabase.begin(); it != mSecElemRouteDatabase.end(); it++)
        delete (*it);
    mSecElemRouteDatabase.clear ();
}


/*******************************************************************************
**
** Function:        import
**
** Description:     Import data from an XML file.  Fill the databases.
**
** Returns:         True if ok.
**
*******************************************************************************/
bool RouteDataSet::import ()
{
    static const char fn [] = "RouteDataSet::import";
    ALOGD ("%s: enter", fn);
    bool retval = false;
    xmlDocPtr doc;
    xmlNodePtr node1;
    std::string strFilename(bcm_nfc_location);
    strFilename += sConfigFile;

    deleteDatabase ();

    doc = xmlParseFile (strFilename.c_str());
    if (doc == NULL)
    {
        ALOGD ("%s: fail parse", fn);
        goto TheEnd;
    }

    node1 = xmlDocGetRootElement (doc);
    if (node1 == NULL)
    {
        ALOGE ("%s: fail root element", fn);
        goto TheEnd;
    }
    ALOGD ("%s: root=%s", fn, node1->name);

    node1 = node1->xmlChildrenNode;
    while (node1) //loop through all elements in <Routes ...
    {
        if (xmlStrcmp(node1->name, (const xmlChar*) "Route")==0)
        {
            xmlChar* value = xmlGetProp (node1, (const xmlChar*) "Type");
            if (value && (xmlStrcmp (value, (const xmlChar*) "SecElemSelectedRoutes") == 0))
            {
                ALOGD ("%s: found SecElemSelectedRoutes", fn);
                xmlNodePtr node2 = node1->xmlChildrenNode;
                while (node2) //loop all elements in <Route Type="SecElemSelectedRoutes" ...
                {
                    if (xmlStrcmp(node2->name, (const xmlChar*) "Proto")==0)
                        importProtocolRoute (node2, mSecElemRouteDatabase);
                    else if (xmlStrcmp(node2->name, (const xmlChar*) "Tech")==0)
                        importTechnologyRoute (node2, mSecElemRouteDatabase);
                    node2 = node2->next;
                } //loop all elements in <Route Type="SecElemSelectedRoutes" ...
            }
            else if (value && (xmlStrcmp (value, (const xmlChar*) "DefaultRoutes") == 0))
            {
                ALOGD ("%s: found DefaultRoutes", fn);
                xmlNodePtr node2 = node1->xmlChildrenNode;
                while (node2) //loop all elements in <Route Type="DefaultRoutes" ...
                {
                    if (xmlStrcmp(node2->name, (const xmlChar*) "Proto")==0)
                        importProtocolRoute (node2, mDefaultRouteDatabase);
                    else if (xmlStrcmp(node2->name, (const xmlChar*) "Tech")==0)
                        importTechnologyRoute (node2, mDefaultRouteDatabase);
                    node2 = node2->next;
                } //loop all elements in <Route Type="DefaultRoutes" ...
            }
            if (value)
                xmlFree (value);
        } //check <Route ...
        node1 = node1->next;
    } //loop through all elements in <Routes ...
    retval = true;

TheEnd:
    xmlFreeDoc (doc);
    xmlCleanupParser ();
    ALOGD ("%s: exit; return=%u", fn, retval);
    return retval;
}


/*******************************************************************************
**
** Function:        saveToFile
**
** Description:     Save XML data from a string into a file.
**                  routesXml: XML that represents routes.
**
** Returns:         True if ok.
**
*******************************************************************************/
bool RouteDataSet::saveToFile (const char* routesXml)
{
    static const char fn [] = "RouteDataSet::saveToFile";
    FILE* fh = NULL;
    size_t actualWritten = 0;
    bool retval = false;
    std::string filename (bcm_nfc_location);

    filename.append (sConfigFile);
    fh = fopen (filename.c_str (), "w");
    if (fh == NULL)
    {
        ALOGE ("%s: fail to open file", fn);
        return false;
    }

    actualWritten = fwrite (routesXml, sizeof(char), strlen(routesXml), fh);
    retval = actualWritten == strlen(routesXml);
    fclose (fh);
    ALOGD ("%s: wrote %u bytes", fn, actualWritten);
    if (retval == false)
        ALOGE ("%s: error during write", fn);

    //set file permission to
    //owner read, write; group read; other read
    chmod (filename.c_str (), S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
    return retval;
}


/*******************************************************************************
**
** Function:        loadFromFile
**
** Description:     Load XML data from file into a string.
**                  routesXml: string to receive XML data.
**
** Returns:         True if ok.
**
*******************************************************************************/
bool RouteDataSet::loadFromFile (std::string& routesXml)
{
    static const char fn [] = "RouteDataSet::loadFromFile";
    FILE* fh = NULL;
    size_t actual = 0;
    char buffer [1024];
    std::string filename (bcm_nfc_location);

    filename.append (sConfigFile);
    fh = fopen (filename.c_str (), "r");
    if (fh == NULL)
    {
        ALOGD ("%s: fail to open file", fn);
        return false;
    }

    while (true)
    {
        actual = fread (buffer, sizeof(char), sizeof(buffer), fh);
        if (actual == 0)
            break;
        routesXml.append (buffer, actual);
    }
    fclose (fh);
    ALOGD ("%s: read %u bytes", fn, routesXml.length());
    return true;
}




/*******************************************************************************
**
** Function:        importProtocolRoute
**
** Description:     Parse data for protocol routes.
**                  element: XML node for one protocol route.
**                  database: store data in this database.
**
** Returns:         None.
**
*******************************************************************************/
void RouteDataSet::importProtocolRoute (xmlNodePtr& element, Database& database)
{
    static const char fn [] = "RouteDataSet::importProtocolRoute";
    const xmlChar* id = (const xmlChar*) "Id";
    const xmlChar* secElem = (const xmlChar*) "SecElem";
    const xmlChar* trueString = (const xmlChar*) "true";
    const xmlChar* switchOn = (const xmlChar*) "SwitchOn";
    const xmlChar* switchOff = (const xmlChar*) "SwitchOff";
    const xmlChar* batteryOff = (const xmlChar*) "BatteryOff";
    RouteDataForProtocol* data = new RouteDataForProtocol;
    xmlChar* value = NULL;

    ALOGD_IF (sDebug, "%s: element=%s", fn, element->name);
    value = xmlGetProp (element, id);
    if (value)
    {
        if (xmlStrcmp (value, (const xmlChar*) "T1T") == 0)
            data->mProtocol = NFA_PROTOCOL_MASK_T1T;
        else if (xmlStrcmp (value, (const xmlChar*) "T2T") == 0)
            data->mProtocol = NFA_PROTOCOL_MASK_T2T;
        else if (xmlStrcmp (value, (const xmlChar*) "T3T") == 0)
            data->mProtocol = NFA_PROTOCOL_MASK_T3T;
        else if (xmlStrcmp (value, (const xmlChar*) "IsoDep") == 0)
            data->mProtocol = NFA_PROTOCOL_MASK_ISO_DEP;
        xmlFree (value);
        ALOGD_IF (sDebug, "%s: %s=0x%X", fn, id, data->mProtocol);
    }

    value = xmlGetProp (element, secElem);
    if (value)
    {
        data->mNfaEeHandle = strtol ((char*) value, NULL, 16);
        xmlFree (value);
        data->mNfaEeHandle = data->mNfaEeHandle | NFA_HANDLE_GROUP_EE;
        ALOGD_IF (sDebug, "%s: %s=0x%X", fn, secElem, data->mNfaEeHandle);
    }

    value = xmlGetProp (element, switchOn);
    if (value)
    {
        data->mSwitchOn = (xmlStrcmp (value, trueString) == 0);
        xmlFree (value);
    }

    value = xmlGetProp (element, switchOff);
    if (value)
    {
        data->mSwitchOff = (xmlStrcmp (value, trueString) == 0);
        xmlFree (value);
    }

    value = xmlGetProp (element, batteryOff);
    if (value)
    {
        data->mBatteryOff = (xmlStrcmp (value, trueString) == 0);
        xmlFree (value);
    }
    database.push_back (data);
}


/*******************************************************************************
**
** Function:        importTechnologyRoute
**
** Description:     Parse data for technology routes.
**                  element: XML node for one technology route.
**                  database: store data in this database.
**
** Returns:         None.
**
*******************************************************************************/
void RouteDataSet::importTechnologyRoute (xmlNodePtr& element, Database& database)
{
    static const char fn [] = "RouteDataSet::importTechnologyRoute";
    const xmlChar* id = (const xmlChar*) "Id";
    const xmlChar* secElem = (const xmlChar*) "SecElem";
    const xmlChar* trueString = (const xmlChar*) "true";
    const xmlChar* switchOn = (const xmlChar*) "SwitchOn";
    const xmlChar* switchOff = (const xmlChar*) "SwitchOff";
    const xmlChar* batteryOff = (const xmlChar*) "BatteryOff";
    RouteDataForTechnology* data = new RouteDataForTechnology;
    xmlChar* value = NULL;

    ALOGD_IF (sDebug, "%s: element=%s", fn, element->name);
    value = xmlGetProp (element, id);
    if (value)
    {
        if (xmlStrcmp (value, (const xmlChar*) "NfcA") == 0)
            data->mTechnology = NFA_TECHNOLOGY_MASK_A;
        else if (xmlStrcmp (value, (const xmlChar*) "NfcB") == 0)
            data->mTechnology = NFA_TECHNOLOGY_MASK_B;
        else if (xmlStrcmp (value, (const xmlChar*) "NfcF") == 0)
            data->mTechnology = NFA_TECHNOLOGY_MASK_F;
        xmlFree (value);
        ALOGD_IF (sDebug, "%s: %s=0x%X", fn, id, data->mTechnology);
    }

    value = xmlGetProp (element, secElem);
    if (value)
    {
        data->mNfaEeHandle = strtol ((char*) value, NULL, 16);
        xmlFree (value);
        data->mNfaEeHandle = data->mNfaEeHandle | NFA_HANDLE_GROUP_EE;
        ALOGD_IF (sDebug, "%s: %s=0x%X", fn, secElem, data->mNfaEeHandle);
    }

    value = xmlGetProp (element, switchOn);
    if (value)
    {
        data->mSwitchOn = (xmlStrcmp (value, trueString) == 0);
        xmlFree (value);
    }

    value = xmlGetProp (element, switchOff);
    if (value)
    {
        data->mSwitchOff = (xmlStrcmp (value, trueString) == 0);
        xmlFree (value);
    }

    value = xmlGetProp (element, batteryOff);
    if (value)
    {
        data->mBatteryOff = (xmlStrcmp (value, trueString) == 0);
        xmlFree (value);
    }
    database.push_back (data);
}


/*******************************************************************************
**
** Function:        deleteFile
**
** Description:     Delete route data XML file.
**
** Returns:         True if ok.
**
*******************************************************************************/
bool RouteDataSet::deleteFile ()
{
    static const char fn [] = "RouteDataSet::deleteFile";
    std::string filename (bcm_nfc_location);
    filename.append (sConfigFile);
    int stat = remove (filename.c_str());
    ALOGD ("%s: exit %u", fn, stat==0);
    return stat == 0;
}


/*******************************************************************************
**
** Function:        getDatabase
**
** Description:     Obtain a database of routing data.
**                  selection: which database.
**
** Returns:         Pointer to database.
**
*******************************************************************************/
RouteDataSet::Database* RouteDataSet::getDatabase (DatabaseSelection selection)
{
    switch (selection)
    {
    case DefaultRouteDatabase:
        return &mDefaultRouteDatabase;
    case SecElemRouteDatabase:
        return &mSecElemRouteDatabase;
    }
    return NULL;
}


/*******************************************************************************
**
** Function:        printDiagnostic
**
** Description:     Print some diagnostic output.
**
** Returns:         None.
**
*******************************************************************************/
void RouteDataSet::printDiagnostic ()
{
    static const char fn [] = "RouteDataSet::printDiagnostic";
    Database* db = getDatabase (DefaultRouteDatabase);

    ALOGD ("%s: default route database", fn);
    for (Database::iterator iter = db->begin(); iter != db->end(); iter++)
    {
        RouteData* routeData = *iter;
        switch (routeData->mRouteType)
        {
        case RouteData::ProtocolRoute:
            {
                RouteDataForProtocol* proto = (RouteDataForProtocol*) routeData;
                ALOGD ("%s: ee h=0x%X; protocol=0x%X", fn, proto->mNfaEeHandle, proto->mProtocol);
            }
            break;
        }
    }

    ALOGD ("%s: sec elem route database", fn);
    db = getDatabase (SecElemRouteDatabase);
    for (Database::iterator iter2 = db->begin(); iter2 != db->end(); iter2++)
    {
        RouteData* routeData = *iter2;
        switch (routeData->mRouteType)
        {
        case RouteData::ProtocolRoute:
            {
                RouteDataForProtocol* proto = (RouteDataForProtocol*) routeData;
                ALOGD ("%s: ee h=0x%X; protocol=0x%X", fn, proto->mNfaEeHandle, proto->mProtocol);
            }
            break;
        case RouteData::TechnologyRoute:
            {
                RouteDataForTechnology* tech = (RouteDataForTechnology*) routeData;
                ALOGD ("%s: ee h=0x%X; technology=0x%X", fn, tech->mNfaEeHandle, tech->mTechnology);
            }
            break;
        }
    }
}