1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  *  Import and export general routing data using a XML file.
19  */
20 #pragma once
21 #include "NfcJniUtil.h"
22 #include "nfa_api.h"
23 
24 #include <libxml/parser.h>
25 #include <string>
26 #include <vector>
27 
28 /*****************************************************************************
29 **
30 **  Name:           RouteData
31 **
32 **  Description:    Base class for every kind of route data.
33 **
34 *****************************************************************************/
35 class RouteData {
36  public:
37   enum RouteType { ProtocolRoute, TechnologyRoute };
38   RouteType mRouteType;
39 
40  protected:
RouteData(RouteType routeType)41   RouteData(RouteType routeType) : mRouteType(routeType) {}
42 };
43 
44 /*****************************************************************************
45 **
46 **  Name:           RouteDataForProtocol
47 **
48 **  Description:    Data for protocol routes.
49 **
50 *****************************************************************************/
51 class RouteDataForProtocol : public RouteData {
52  public:
53   int mNfaEeHandle;  // for example 0x4f3, 0x4f4
54   bool mSwitchOn;
55   bool mSwitchOff;
56   bool mBatteryOff;
57   tNFA_PROTOCOL_MASK mProtocol;
58 
RouteDataForProtocol()59   RouteDataForProtocol()
60       : RouteData(ProtocolRoute),
61         mNfaEeHandle(NFA_HANDLE_INVALID),
62         mSwitchOn(false),
63         mSwitchOff(false),
64         mBatteryOff(false),
65         mProtocol(0) {}
66 };
67 
68 /*****************************************************************************
69 **
70 **  Name:           RouteDataForTechnology
71 **
72 **  Description:    Data for technology routes.
73 **
74 *****************************************************************************/
75 class RouteDataForTechnology : public RouteData {
76  public:
77   int mNfaEeHandle;  // for example 0x4f3, 0x4f4
78   bool mSwitchOn;
79   bool mSwitchOff;
80   bool mBatteryOff;
81   tNFA_TECHNOLOGY_MASK mTechnology;
82 
RouteDataForTechnology()83   RouteDataForTechnology()
84       : RouteData(TechnologyRoute),
85         mNfaEeHandle(NFA_HANDLE_INVALID),
86         mSwitchOn(false),
87         mSwitchOff(false),
88         mBatteryOff(false),
89         mTechnology(0) {}
90 };
91 
92 /*****************************************************************************/
93 /*****************************************************************************/
94 
95 /*****************************************************************************
96 **
97 **  Name:           AidBuffer
98 **
99 **  Description:    Buffer to store AID after converting a string of hex
100 **                  values to bytes.
101 **
102 *****************************************************************************/
103 class AidBuffer {
104  public:
105   /*******************************************************************************
106   **
107   ** Function:        AidBuffer
108   **
109   ** Description:     Parse a string of hex numbers.  Store result in an array
110   *of
111   **                  bytes.
112   **                  aid: string of hex numbers.
113   **
114   ** Returns:         None.
115   **
116   *******************************************************************************/
117   AidBuffer(std::string& aid);
118 
119   /*******************************************************************************
120   **
121   ** Function:        ~AidBuffer
122   **
123   ** Description:     Release all resources.
124   **
125   ** Returns:         None.
126   **
127   *******************************************************************************/
128   ~AidBuffer();
129 
buffer()130   uint8_t* buffer() { return mBuffer; };
length()131   int length() { return mBufferLen; };
132 
133  private:
134   uint8_t* mBuffer;
135   uint32_t mBufferLen;
136 };
137 
138 /*****************************************************************************/
139 /*****************************************************************************/
140 
141 /*****************************************************************************
142 **
143 **  Name:           RouteDataSet
144 **
145 **  Description:    Import and export general routing data using a XML file.
146 **                  See /data/bcm/param/route.xml
147 **
148 *****************************************************************************/
149 class RouteDataSet {
150  public:
151   typedef std::vector<RouteData*> Database;
152   enum DatabaseSelection { DefaultRouteDatabase, SecElemRouteDatabase };
153 
154   /*******************************************************************************
155   **
156   ** Function:        ~RouteDataSet
157   **
158   ** Description:     Release all resources.
159   **
160   ** Returns:         None.
161   **
162   *******************************************************************************/
163   ~RouteDataSet();
164 
165   /*******************************************************************************
166   **
167   ** Function:        initialize
168   **
169   ** Description:     Initialize resources.
170   **
171   ** Returns:         True if ok.
172   **
173   *******************************************************************************/
174   bool initialize();
175 
176   /*******************************************************************************
177   **
178   ** Function:        import
179   **
180   ** Description:     Import data from an XML file.  Fill the database.
181   **
182   ** Returns:         True if ok.
183   **
184   *******************************************************************************/
185   bool import();
186 
187   /*******************************************************************************
188   **
189   ** Function:        getDatabase
190   **
191   ** Description:     Obtain a database of routing data.
192   **                  selection: which database.
193   **
194   ** Returns:         Pointer to database.
195   **
196   *******************************************************************************/
197   Database* getDatabase(DatabaseSelection selection);
198 
199   /*******************************************************************************
200   **
201   ** Function:        saveToFile
202   **
203   ** Description:     Save XML data from a string into a file.
204   **                  routesXml: XML that represents routes.
205   **
206   ** Returns:         True if ok.
207   **
208   *******************************************************************************/
209   static bool saveToFile(const char* routesXml);
210 
211   /*******************************************************************************
212   **
213   ** Function:        loadFromFile
214   **
215   ** Description:     Load XML data from file into a string.
216   **                  routesXml: string to receive XML data.
217   **
218   ** Returns:         True if ok.
219   **
220   *******************************************************************************/
221   static bool loadFromFile(std::string& routesXml);
222 
223   /*******************************************************************************
224   **
225   ** Function:        deleteFile
226   **
227   ** Description:     Delete route data XML file.
228   **
229   ** Returns:         True if ok.
230   **
231   *******************************************************************************/
232   static bool deleteFile();
233 
234   /*******************************************************************************
235   **
236   ** Function:        printDiagnostic
237   **
238   ** Description:     Print some diagnostic output.
239   **
240   ** Returns:         None.
241   **
242   *******************************************************************************/
243   void printDiagnostic();
244 
245  private:
246   Database mSecElemRouteDatabase;  // routes when NFC service selects sec elem
247   Database mDefaultRouteDatabase;  // routes when NFC service deselects sec elem
248   static const char* sConfigFile;
249   static const bool sDebug = false;
250 
251   /*******************************************************************************
252   **
253   ** Function:        deleteDatabase
254   **
255   ** Description:     Delete all routes stored in all databases.
256   **
257   ** Returns:         None.
258   **
259   *******************************************************************************/
260   void deleteDatabase();
261 
262   /*******************************************************************************
263   **
264   ** Function:        importProtocolRoute
265   **
266   ** Description:     Parse data for protocol routes.
267   **                  element: XML node for one protocol route.
268   **                  database: store data in this database.
269   **
270   ** Returns:         None.
271   **
272   *******************************************************************************/
273   void importProtocolRoute(xmlNodePtr& element, Database& database);
274 
275   /*******************************************************************************
276   **
277   ** Function:        importTechnologyRoute
278   **
279   ** Description:     Parse data for technology routes.
280   **                  element: XML node for one technology route.
281   **                  database: store data in this database.
282   **
283   ** Returns:         None.
284   **
285   *******************************************************************************/
286   void importTechnologyRoute(xmlNodePtr& element, Database& database);
287 };
288