remove hard coded path into CMake files
[profile/ivi/ico-vic-amb-plugin.git] / src / ambconfig.cc
1 /*
2  * Copyright (c) 2013 TOYOTA MOTOR CORPORATION.
3  *
4  * Contact: shibata@mac.tec.toyota.co.jp
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 #include <string.h>
19
20 #include <fstream>
21 #include <iostream>
22 #include <stdexcept>
23
24 #include <json/json.h>
25
26 #include "ambconfig.h"
27 #include "debugout.h"
28
29 AMBConfig::AMBConfig()
30 {
31 }
32
33 AMBConfig::~AMBConfig()
34 {
35 }
36
37 std::vector<VehicleInfoDefine>
38 AMBConfig::getVehicleInfoConfig()
39 {
40     return vehicleinfoList;
41 }
42
43 PortInfo
44 AMBConfig::getPort()
45 {
46     return portinfo;
47 }
48
49 bool
50 AMBConfig::parseJson(std::string config)
51 {
52     bool ret = false;
53     json_object *rootobject;
54     json_tokener *tokener = json_tokener_new();
55     if (tokener == NULL) {
56         return ret;
57     }
58     enum json_tokener_error err;
59     do {
60         rootobject = json_tokener_parse_ex(tokener, config.c_str(),
61                                            config.length());
62     } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
63     json_tokener_free(tokener);
64
65     if (err != json_tokener_success) {
66         std::cerr << "Error: " << json_tokener_error_desc(err) << "\n";
67         DebugOut(DebugOut::Error) << json_tokener_error_desc(err) << "\n";
68         return ret;
69     }
70     json_object *configobject = json_object_object_get(rootobject, "Config");
71     if (!configobject) {
72         DebugOut(DebugOut::Error) << "Can't find key[\"Config\"].\n";
73         return ret;
74     }
75     array_list *configlist = json_object_get_array(configobject);
76     if (configlist == NULL) {
77         DebugOut(DebugOut::Error) << "\"Config\" is not array.\n";
78         return ret;
79     }
80     for (int i = 0; i < array_list_length(configlist); i++) {
81         json_object *obj = reinterpret_cast<json_object*>(array_list_get_idx(
82                 configlist, i));
83         if (obj == NULL) {
84             break;
85         }
86         json_object *sectionobj = json_object_object_get(obj, "Section");
87         if (sectionobj == NULL) {
88             break;
89         }
90         if ("Common" != std::string(json_object_get_string(sectionobj))) {
91             continue;
92         }
93         json_object *defineobj = json_object_object_get(obj,
94                                                         "VehicleInfoDefine");
95         if (defineobj == NULL) {
96             break;
97         }
98         array_list *definelist = json_object_get_array(defineobj);
99         if (definelist == NULL) {
100             DebugOut(DebugOut::Error) << "\"VehicleInfoDefine\" "
101                                       << "is not array.\n";
102             break;
103         }
104         for (int j = 0; j < array_list_length(definelist); j++) {
105             DebugOut(10) << "VehicleInfoDefine : " << j << "/"
106                        << array_list_length(definelist) << "\n";
107             json_object *obj_vi =
108                     reinterpret_cast<json_object*>(array_list_get_idx(
109                             definelist, j));
110             if (obj_vi == NULL) {
111                 break;
112             }
113             VehicleInfoDefine vid;
114             json_object *keyeventtypeobj = json_object_object_get(
115                     obj_vi, "KeyEventType");
116             if (keyeventtypeobj == NULL) {
117                 DebugOut(DebugOut::Warning) << "\"KeyEventType\" "
118                                             << "is not defined.\n";
119                 continue;
120             }
121             if (std::string(json_object_get_string(keyeventtypeobj)).size() > 63) {
122                 DebugOut(DebugOut::Warning) << "Don't allow length of "
123                                             << "\"KeyEventType\"'s value.\n";
124                 break;
125             }
126             strcpy(vid.KeyEventType, json_object_get_string(keyeventtypeobj));
127             json_object *statusobj = json_object_object_get(obj_vi, "Status");
128             if (statusobj == NULL) {
129                 continue;
130             }
131             array_list *statuslist = json_object_get_array(statusobj);
132             if (statuslist == NULL) {
133                 continue;
134             }
135             for (int k = 0; k < array_list_length(statuslist); k++) {
136                 json_object *obj_sts =
137                         reinterpret_cast<json_object*>(array_list_get_idx(
138                                 statuslist, k));
139                 if (obj_sts == NULL) {
140                     break;
141                 }
142                 VehicleInfoDefine::Status status;
143                 json_object *statuschildobj = json_object_object_get(
144                         obj_sts, "AMBPropertyName");
145                 if (statuschildobj == NULL) {
146                     DebugOut(DebugOut::Warning) << "\"AMBPropertyName\" "
147                                                 << "is not defined.\n";
148                     continue;
149                 }
150                 status.ambPropertyName = string(
151                         json_object_get_string(statuschildobj));
152                 statuschildobj = json_object_object_get(obj_sts, "Type");
153                 if (statuschildobj == NULL) {
154                     DebugOut(DebugOut::Warning) << "\"Type\" "
155                                                 << "is not defined.\n";
156                     continue;
157                 }
158                 status.type =
159                         getType(const_cast<char*>(json_object_get_string(
160                                 statuschildobj)),
161                                 &status.typesize);
162                 if (status.type == NONE) {
163                     DebugOut(DebugOut::Warning) << "\"Type\"'s value "
164                                                 << "is not defined.\n";
165                     continue;
166                 }
167                 statuschildobj = json_object_object_get(obj_sts,
168                                                         "AccessControl");
169                 if (statuschildobj != NULL) {
170                     status.accessControl = string(
171                             json_object_get_string(statuschildobj));
172                 }
173                 statuschildobj = json_object_object_get(obj_sts, "Default");
174                 if (statuschildobj == NULL) {
175                     continue;
176                 }
177                 status.defaultvalue = string(
178                         json_object_to_json_string(statuschildobj));
179                 statuschildobj = json_object_object_get(obj_sts,
180                                                         "DBusProperty");
181                 if (statuschildobj != NULL) {
182                     status.dbusPropertyName = string(
183                             json_object_get_string(statuschildobj));
184                 }
185                 statuschildobj = json_object_object_get(obj_sts,
186                                                         "DBusObjectName");
187                 if (statuschildobj != NULL) {
188                     status.dbusObjectName = string(
189                             json_object_get_string(statuschildobj));
190                 }
191                 statuschildobj = json_object_object_get(obj_sts,
192                                                         "Zone");
193                 if (statuschildobj != NULL) {
194                     status.zone = getZone(string(
195                             json_object_get_string(statuschildobj)));
196                 }
197                 vid.status.push_back(status);
198             }
199             /* ToDo */
200             /* Sense, EventMask */
201             json_object *priorityobj = json_object_object_get(obj_vi,
202                                                               "Priority");
203             if (priorityobj != NULL) {
204                 vid.priority = json_object_get_int(priorityobj);
205             }
206             json_object *dbusifobj = json_object_object_get(obj_vi,
207                                                             "DBusInterface");
208             if (dbusifobj != NULL) {
209                 vid.dbusInterface = string(json_object_get_string(dbusifobj));
210             }
211             json_object *dbusobjobj = json_object_object_get(obj_vi,
212                                                              "DBusObject");
213             if (dbusobjobj != NULL) {
214                 vid.dbusObject = string(json_object_get_string(dbusobjobj));
215             }
216             vehicleinfoList.push_back(vid);
217         }
218         json_object *defaultportobj = json_object_object_get(obj,
219                                                              "DefaultInfoPort");
220         if (defaultportobj == NULL) {
221             DebugOut(DebugOut::Error) << "\"DefaultInfoPort\" "
222                                       << "is not defined.\n";
223             break;
224         }
225         json_object *portobj = json_object_object_get(defaultportobj,
226                                                       "DataPort");
227         if (portobj == NULL) {
228             DebugOut(DebugOut::Error) << "\"DefaultInfoPort\"->"
229                                       << "\"DataPort\" "
230                                       << "is not defined.\n";
231             break;
232         }
233         portinfo.standard.dataPort = json_object_get_int(portobj);
234         portobj = json_object_object_get(defaultportobj, "CtrlPort");
235         if (portobj == NULL) {
236             DebugOut(DebugOut::Error) << "\"DefaultInfoPort\"->"
237                                       << "\"CtrlPort\" "
238                                       << "is not defined.\n";
239             break;
240         }
241         portinfo.standard.controlPort = json_object_get_int(portobj);
242         json_object *customportobj = json_object_object_get(
243                 obj, "CustomizeInfoPort");
244         if (customportobj == NULL) {
245             DebugOut(DebugOut::Error) << "\"CustomeizeInfoPort\"->"
246                                       << "is not defined.\n";
247             break;
248         }
249         portobj = json_object_object_get(customportobj, "DataPort");
250         if (portobj == NULL) {
251             DebugOut(DebugOut::Error) << "\"CustomeizeInfoPort\"->"
252                                       << "\"DataPort\" "
253                                       << "is not defined.\n";
254             break;
255         }
256         portinfo.custom.dataPort = json_object_get_int(portobj);
257         portobj = json_object_object_get(customportobj, "CtrlPort");
258         if (portobj == NULL) {
259             DebugOut(DebugOut::Error) << "\"CustomeizeInfoPort\"->"
260                                       << "\"CtrlPort\" "
261                                       << "is not defined.\n";
262             break;
263         }
264         portinfo.custom.controlPort = json_object_get_int(portobj);
265         ret = true;
266         break;
267     }
268     if (vehicleinfoList.empty()) {
269         ret = false;
270         DebugOut(DebugOut::Error) << "Can't load vehicle information.\n";
271     }
272
273     return ret;
274 }
275
276 DataType
277 AMBConfig::getType(char *type, int *size)
278 {
279     if (strcmp(type, "int") == 0) {
280         *size = sizeof(int);
281         return INT;
282     }
283     else if (strcmp(type, "double") == 0) {
284         *size = sizeof(double);
285         return DOUBLE;
286     }
287     else if (strcmp(type, "char") == 0) {
288         *size = sizeof(char);
289         return CHAR;
290     }
291     else if (strcmp(type, "int16_t") == 0 || strcmp(type, "int16") == 0) {
292         *size = sizeof(int16_t);
293         return INT16;
294     }
295     else if (strcmp(type, "uint16_t") == 0 || strcmp(type, "uint16") == 0) {
296         *size = sizeof(uint16_t);
297         return UINT16;
298     }
299     else if (strcmp(type, "uint32_t") == 0 || strcmp(type, "uint32") == 0) {
300         *size = sizeof(uint32_t);
301         return UINT32;
302     }
303     else if (strcmp(type, "int64_t") == 0 || strcmp(type, "int64") == 0) {
304         *size = sizeof(int64_t);
305         return INT64;
306     }
307     else if (strcmp(type, "uint64_t") == 0 || strcmp(type, "uint64") == 0) {
308         *size = sizeof(uint64_t);
309         return UINT64;
310     }
311     else if (strcmp(type, "bool") == 0 || strcmp(type, "boolean") == 0) {
312         *size = sizeof(bool);
313         return BOOL;
314     }
315     *size = 0;
316     return NONE;
317 }
318
319 Zone::Type 
320 AMBConfig::getZone(const std::string& zonestr) {
321     DebugOut(50) << "Zone string = " << zonestr << "\n";
322 #if LATER1024
323     if (zonestr == "None") {
324         return Zone::None;
325     }
326     else if (zonestr == "Front") {
327         return Zone::Front;
328     }
329     else if (zonestr == "Middle") {
330         return Zone::Middle;
331     }
332     else if (zonestr == "Right") {
333         return Zone::Right;
334     }
335     else if (zonestr == "Left") {
336         return Zone::Left;
337     }
338     else if (zonestr == "Rear") {
339         return Zone::Rear;
340     }
341     else if (zonestr == "Center") {
342         return Zone::Center;
343     }
344     else if (zonestr == "FrontRight") {
345         return Zone::FrontRight;
346     }
347     else if (zonestr == "FrontLeft") {
348         return Zone::FrontLeft;
349     }
350     else if (zonestr == "MiddleRight") {
351         return Zone::MiddleRight;
352     }
353     else if (zonestr == "MiddleLeft") {
354         return Zone::MiddleLeft;
355     }
356     else if (zonestr == "RearRight") {
357         return Zone::RearRight;
358     }
359     else if (zonestr == "RearLeft") {
360         return Zone::RearLeft;
361     }
362 #endif
363     return Zone::None;
364 }