2 * Copyright (c) 2013 TOYOTA MOTOR CORPORATION.
4 * Contact: shibata@mac.tec.toyota.co.jp
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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
24 #include <json/json.h>
26 #include "ambconfig.h"
29 AMBConfig::AMBConfig()
33 AMBConfig::~AMBConfig()
37 std::vector<VehicleInfoDefine>
38 AMBConfig::getVehicleInfoConfig()
40 return vehicleinfoList;
50 AMBConfig::parseJson(std::string config)
53 json_object *rootobject;
54 json_tokener *tokener = json_tokener_new();
55 if (tokener == NULL) {
58 enum json_tokener_error err;
60 rootobject = json_tokener_parse_ex(tokener, config.c_str(),
62 } while ((err = json_tokener_get_error(tokener)) == json_tokener_continue);
63 json_tokener_free(tokener);
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";
70 json_object *configobject = json_object_object_get(rootobject, "Config");
72 DebugOut(DebugOut::Error) << "Can't find key[\"Config\"].\n";
75 array_list *configlist = json_object_get_array(configobject);
76 if (configlist == NULL) {
77 DebugOut(DebugOut::Error) << "\"Config\" is not array.\n";
80 for (int i = 0; i < array_list_length(configlist); i++) {
81 json_object *obj = reinterpret_cast<json_object*>(array_list_get_idx(
86 json_object *sectionobj = json_object_object_get(obj, "Section");
87 if (sectionobj == NULL) {
90 if ("Common" != std::string(json_object_get_string(sectionobj))) {
93 json_object *defineobj = json_object_object_get(obj,
95 if (defineobj == NULL) {
98 array_list *definelist = json_object_get_array(defineobj);
99 if (definelist == NULL) {
100 DebugOut(DebugOut::Error) << "\"VehicleInfoDefine\" "
101 << "is not array.\n";
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(
110 if (obj_vi == NULL) {
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";
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";
126 strcpy(vid.KeyEventType, json_object_get_string(keyeventtypeobj));
127 json_object *statusobj = json_object_object_get(obj_vi, "Status");
128 if (statusobj == NULL) {
131 array_list *statuslist = json_object_get_array(statusobj);
132 if (statuslist == NULL) {
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(
139 if (obj_sts == NULL) {
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";
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";
159 getType(const_cast<char*>(json_object_get_string(
162 if (status.type == NONE) {
163 DebugOut(DebugOut::Warning) << "\"Type\"'s value "
164 << "is not defined.\n";
167 statuschildobj = json_object_object_get(obj_sts,
169 if (statuschildobj != NULL) {
170 status.accessControl = string(
171 json_object_get_string(statuschildobj));
173 statuschildobj = json_object_object_get(obj_sts, "Default");
174 if (statuschildobj == NULL) {
177 status.defaultvalue = string(
178 json_object_to_json_string(statuschildobj));
179 statuschildobj = json_object_object_get(obj_sts,
181 if (statuschildobj != NULL) {
182 status.dbusPropertyName = string(
183 json_object_get_string(statuschildobj));
185 statuschildobj = json_object_object_get(obj_sts,
187 if (statuschildobj != NULL) {
188 status.dbusObjectName = string(
189 json_object_get_string(statuschildobj));
191 statuschildobj = json_object_object_get(obj_sts,
193 if (statuschildobj != NULL) {
194 status.zone = getZone(string(
195 json_object_get_string(statuschildobj)));
197 vid.status.push_back(status);
200 /* Sense, EventMask */
201 json_object *priorityobj = json_object_object_get(obj_vi,
203 if (priorityobj != NULL) {
204 vid.priority = json_object_get_int(priorityobj);
206 json_object *dbusifobj = json_object_object_get(obj_vi,
208 if (dbusifobj != NULL) {
209 vid.dbusInterface = string(json_object_get_string(dbusifobj));
211 json_object *dbusobjobj = json_object_object_get(obj_vi,
213 if (dbusobjobj != NULL) {
214 vid.dbusObject = string(json_object_get_string(dbusobjobj));
216 vehicleinfoList.push_back(vid);
218 json_object *defaultportobj = json_object_object_get(obj,
220 if (defaultportobj == NULL) {
221 DebugOut(DebugOut::Error) << "\"DefaultInfoPort\" "
222 << "is not defined.\n";
225 json_object *portobj = json_object_object_get(defaultportobj,
227 if (portobj == NULL) {
228 DebugOut(DebugOut::Error) << "\"DefaultInfoPort\"->"
230 << "is not defined.\n";
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\"->"
238 << "is not defined.\n";
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";
249 portobj = json_object_object_get(customportobj, "DataPort");
250 if (portobj == NULL) {
251 DebugOut(DebugOut::Error) << "\"CustomeizeInfoPort\"->"
253 << "is not defined.\n";
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\"->"
261 << "is not defined.\n";
264 portinfo.custom.controlPort = json_object_get_int(portobj);
268 if (vehicleinfoList.empty()) {
270 DebugOut(DebugOut::Error) << "Can't load vehicle information.\n";
277 AMBConfig::getType(char *type, int *size)
279 if (strcmp(type, "int") == 0) {
283 else if (strcmp(type, "double") == 0) {
284 *size = sizeof(double);
287 else if (strcmp(type, "char") == 0) {
288 *size = sizeof(char);
291 else if (strcmp(type, "int16_t") == 0 || strcmp(type, "int16") == 0) {
292 *size = sizeof(int16_t);
295 else if (strcmp(type, "uint16_t") == 0 || strcmp(type, "uint16") == 0) {
296 *size = sizeof(uint16_t);
299 else if (strcmp(type, "uint32_t") == 0 || strcmp(type, "uint32") == 0) {
300 *size = sizeof(uint32_t);
303 else if (strcmp(type, "int64_t") == 0 || strcmp(type, "int64") == 0) {
304 *size = sizeof(int64_t);
307 else if (strcmp(type, "uint64_t") == 0 || strcmp(type, "uint64") == 0) {
308 *size = sizeof(uint64_t);
311 else if (strcmp(type, "bool") == 0 || strcmp(type, "boolean") == 0) {
312 *size = sizeof(bool);
320 AMBConfig::getZone(const std::string& zonestr) {
321 DebugOut(50) << "Zone string = " << zonestr << "\n";
323 if (zonestr == "None") {
326 else if (zonestr == "Front") {
329 else if (zonestr == "Middle") {
332 else if (zonestr == "Right") {
335 else if (zonestr == "Left") {
338 else if (zonestr == "Rear") {
341 else if (zonestr == "Center") {
344 else if (zonestr == "FrontRight") {
345 return Zone::FrontRight;
347 else if (zonestr == "FrontLeft") {
348 return Zone::FrontLeft;
350 else if (zonestr == "MiddleRight") {
351 return Zone::MiddleRight;
353 else if (zonestr == "MiddleLeft") {
354 return Zone::MiddleLeft;
356 else if (zonestr == "RearRight") {
357 return Zone::RearRight;
359 else if (zonestr == "RearLeft") {
360 return Zone::RearLeft;