1 #define LOG_TAG "sensor.hal.configuration.V1_0" 2 #include "sensor_hal_configuration_V1_0.h" 3 4 #include <assert.h> 5 #ifndef __BIONIC__ 6 #define __assert2(f,n,fun,e) do { fprintf(stderr, "%s:%d: %s: Assertion `%s' failed", (f), (n), (fun), (e)); abort(); } while (false) 7 #endif 8 #define _xsdc_assert(e) do if (!(e)) __assert2(__FILE__, __LINE__, __PRETTY_FUNCTION__, #e); while (false) 9 10 namespace sensor { 11 namespace hal { 12 namespace configuration { 13 namespace V1_0 { 14 template <class T> 15 constexpr void (*xmlDeleter)(T* t); 16 template <> 17 constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc; 18 template <> __anon52ab3a480102(xmlChar *s) 19 auto xmlDeleter<xmlChar> = [](xmlChar *s) { xmlFree(s); }; 20 21 template <class T> make_xmlUnique(T * t)22 constexpr auto make_xmlUnique(T *t) { 23 auto deleter = [](T *t) { xmlDeleter<T>(t); }; 24 return std::unique_ptr<T, decltype(deleter)>{t, deleter}; 25 } 26 getXmlAttribute(const xmlNode * cur,const char * attribute)27 static std::string getXmlAttribute(const xmlNode *cur, const char *attribute) { 28 auto xmlValue = make_xmlUnique(xmlGetProp(cur, reinterpret_cast<const xmlChar*>(attribute))); 29 if (xmlValue == nullptr) { 30 return ""; 31 } 32 std::string value(reinterpret_cast<const char*>(xmlValue.get())); 33 return value; 34 } 35 read(const char * configFile)36 std::optional<SensorHalConfiguration> read(const char* configFile) { 37 auto doc = make_xmlUnique(xmlParseFile(configFile)); 38 if (doc == nullptr) { 39 return std::nullopt; 40 } 41 xmlNodePtr _child = xmlDocGetRootElement(doc.get()); 42 if (_child == nullptr) { 43 return std::nullopt; 44 } 45 if (xmlXIncludeProcess(doc.get()) < 0) { 46 return std::nullopt; 47 } 48 49 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("sensorHalConfiguration"))) { 50 SensorHalConfiguration _value = SensorHalConfiguration::read(_child); 51 return _value; 52 } 53 return std::nullopt; 54 } 55 parse(const char * xml)56 std::optional<SensorHalConfiguration> parse(const char* xml) { 57 auto doc = make_xmlUnique(xmlParseDoc(reinterpret_cast<const xmlChar*>(xml))); 58 if (doc == nullptr) { 59 return std::nullopt; 60 } 61 xmlNodePtr _child = xmlDocGetRootElement(doc.get()); 62 if (_child == nullptr) { 63 return std::nullopt; 64 } 65 if (xmlXIncludeProcess(doc.get()) < 0) { 66 return std::nullopt; 67 } 68 69 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("sensorHalConfiguration"))) { 70 SensorHalConfiguration _value = SensorHalConfiguration::read(_child); 71 return _value; 72 } 73 return std::nullopt; 74 } 75 76 AxisType(int64_t map,bool negate)77 AxisType::AxisType(int64_t map, bool negate) : map_(map), negate_(negate) { 78 } 79 getMap() const80 const int64_t& AxisType::getMap() const { 81 return map_; 82 } 83 hasMap() const84 bool AxisType::hasMap() const { 85 return true; 86 } 87 getNegate() const88 const bool& AxisType::getNegate() const { 89 return negate_; 90 } 91 hasNegate() const92 bool AxisType::hasNegate() const { 93 return true; 94 } 95 read(xmlNode * root)96 AxisType AxisType::read(xmlNode *root) { 97 std::string _raw; 98 _raw = getXmlAttribute(root, "map"); 99 int64_t map{}; 100 if (_raw != "") { 101 int64_t _value = std::stoll(_raw); 102 map = _value; 103 } 104 _raw = getXmlAttribute(root, "negate"); 105 bool negate{}; 106 if (_raw != "") { 107 bool _value = _raw == "true"; 108 negate = _value; 109 } 110 AxisType instance(map, negate); 111 return instance; 112 } 113 Orientation(std::vector<AxisType> x,std::vector<AxisType> y,std::vector<AxisType> z,bool rotate)114 Orientation::Orientation(std::vector<AxisType> x, std::vector<AxisType> y, std::vector<AxisType> z, bool rotate) : x_(std::move(x)), y_(std::move(y)), z_(std::move(z)), rotate_(rotate) { 115 } 116 getX() const117 const std::vector<AxisType>& Orientation::getX() const { 118 return x_; 119 } 120 hasX() const121 bool Orientation::hasX() const { 122 return !(x_.empty()); 123 } 124 getFirstX() const125 const AxisType* Orientation::getFirstX() const { 126 if (x_.empty()) { 127 return nullptr; 128 } 129 return &x_[0]; 130 } 131 getY() const132 const std::vector<AxisType>& Orientation::getY() const { 133 return y_; 134 } 135 hasY() const136 bool Orientation::hasY() const { 137 return !(y_.empty()); 138 } 139 getFirstY() const140 const AxisType* Orientation::getFirstY() const { 141 if (y_.empty()) { 142 return nullptr; 143 } 144 return &y_[0]; 145 } 146 getZ() const147 const std::vector<AxisType>& Orientation::getZ() const { 148 return z_; 149 } 150 hasZ() const151 bool Orientation::hasZ() const { 152 return !(z_.empty()); 153 } 154 getFirstZ() const155 const AxisType* Orientation::getFirstZ() const { 156 if (z_.empty()) { 157 return nullptr; 158 } 159 return &z_[0]; 160 } 161 getRotate() const162 const bool& Orientation::getRotate() const { 163 return rotate_; 164 } 165 hasRotate() const166 bool Orientation::hasRotate() const { 167 return true; 168 } 169 read(xmlNode * root)170 Orientation Orientation::read(xmlNode *root) { 171 std::string _raw; 172 _raw = getXmlAttribute(root, "rotate"); 173 bool rotate{}; 174 if (_raw != "") { 175 bool _value = _raw == "true"; 176 rotate = _value; 177 } 178 std::vector<AxisType> x; 179 std::vector<AxisType> y; 180 std::vector<AxisType> z; 181 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 182 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("x"))) { 183 AxisType _value = AxisType::read(_child); 184 x.push_back(std::move(_value)); 185 } else if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("y"))) { 186 AxisType _value = AxisType::read(_child); 187 y.push_back(std::move(_value)); 188 } else if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("z"))) { 189 AxisType _value = AxisType::read(_child); 190 z.push_back(std::move(_value)); 191 } 192 } 193 Orientation instance(x, y, z, rotate); 194 return instance; 195 } 196 Location(std::optional<double> x,std::optional<double> y,std::optional<double> z)197 Location::Location(std::optional<double> x, std::optional<double> y, std::optional<double> z) : x_(x), y_(y), z_(z) { 198 } 199 getX() const200 const double& Location::getX() const { 201 _xsdc_assert(hasX()); 202 return x_.value(); 203 } 204 hasX() const205 bool Location::hasX() const { 206 return x_.has_value(); 207 } 208 getY() const209 const double& Location::getY() const { 210 _xsdc_assert(hasY()); 211 return y_.value(); 212 } 213 hasY() const214 bool Location::hasY() const { 215 return y_.has_value(); 216 } 217 getZ() const218 const double& Location::getZ() const { 219 _xsdc_assert(hasZ()); 220 return z_.value(); 221 } 222 hasZ() const223 bool Location::hasZ() const { 224 return z_.has_value(); 225 } 226 read(xmlNode * root)227 Location Location::read(xmlNode *root) { 228 std::string _raw; 229 std::optional<double> x; 230 std::optional<double> y; 231 std::optional<double> z; 232 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 233 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("x"))) { 234 auto xmlValue = make_xmlUnique(xmlNodeListGetString(_child->doc, _child->xmlChildrenNode, 1)); 235 if (xmlValue == nullptr) { 236 _raw = ""; 237 } else { 238 _raw = reinterpret_cast<const char*>(xmlValue.get()); 239 } 240 double _value = std::stod(_raw); 241 x = std::move(_value); 242 } else if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("y"))) { 243 auto xmlValue = make_xmlUnique(xmlNodeListGetString(_child->doc, _child->xmlChildrenNode, 1)); 244 if (xmlValue == nullptr) { 245 _raw = ""; 246 } else { 247 _raw = reinterpret_cast<const char*>(xmlValue.get()); 248 } 249 double _value = std::stod(_raw); 250 y = std::move(_value); 251 } else if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("z"))) { 252 auto xmlValue = make_xmlUnique(xmlNodeListGetString(_child->doc, _child->xmlChildrenNode, 1)); 253 if (xmlValue == nullptr) { 254 _raw = ""; 255 } else { 256 _raw = reinterpret_cast<const char*>(xmlValue.get()); 257 } 258 double _value = std::stod(_raw); 259 z = std::move(_value); 260 } 261 } 262 Location instance(x, y, z); 263 return instance; 264 } 265 Configuration(std::vector<Orientation> orientation,std::vector<Location> location)266 Configuration::Configuration(std::vector<Orientation> orientation, std::vector<Location> location) : orientation_(std::move(orientation)), location_(std::move(location)) { 267 } 268 getOrientation() const269 const std::vector<Orientation>& Configuration::getOrientation() const { 270 return orientation_; 271 } 272 hasOrientation() const273 bool Configuration::hasOrientation() const { 274 return !(orientation_.empty()); 275 } 276 getFirstOrientation() const277 const Orientation* Configuration::getFirstOrientation() const { 278 if (orientation_.empty()) { 279 return nullptr; 280 } 281 return &orientation_[0]; 282 } 283 getLocation() const284 const std::vector<Location>& Configuration::getLocation() const { 285 return location_; 286 } 287 hasLocation() const288 bool Configuration::hasLocation() const { 289 return !(location_.empty()); 290 } 291 getFirstLocation() const292 const Location* Configuration::getFirstLocation() const { 293 if (location_.empty()) { 294 return nullptr; 295 } 296 return &location_[0]; 297 } 298 read(xmlNode * root)299 Configuration Configuration::read(xmlNode *root) { 300 std::string _raw; 301 std::vector<Orientation> orientation; 302 std::vector<Location> location; 303 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 304 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("orientation"))) { 305 Orientation _value = Orientation::read(_child); 306 orientation.push_back(std::move(_value)); 307 } else if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("location"))) { 308 Location _value = Location::read(_child); 309 location.push_back(std::move(_value)); 310 } 311 } 312 Configuration instance(orientation, location); 313 return instance; 314 } 315 Sensor(std::vector<Configuration> configuration,std::string name,int64_t type)316 Sensor::Sensor(std::vector<Configuration> configuration, std::string name, int64_t type) : configuration_(std::move(configuration)), name_(std::move(name)), type_(type) { 317 } 318 getConfiguration() const319 const std::vector<Configuration>& Sensor::getConfiguration() const { 320 return configuration_; 321 } 322 hasConfiguration() const323 bool Sensor::hasConfiguration() const { 324 return !(configuration_.empty()); 325 } 326 getFirstConfiguration() const327 const Configuration* Sensor::getFirstConfiguration() const { 328 if (configuration_.empty()) { 329 return nullptr; 330 } 331 return &configuration_[0]; 332 } 333 getName() const334 const std::string& Sensor::getName() const { 335 return name_; 336 } 337 hasName() const338 bool Sensor::hasName() const { 339 return true; 340 } 341 getType() const342 const int64_t& Sensor::getType() const { 343 return type_; 344 } 345 hasType() const346 bool Sensor::hasType() const { 347 return true; 348 } 349 read(xmlNode * root)350 Sensor Sensor::read(xmlNode *root) { 351 std::string _raw; 352 _raw = getXmlAttribute(root, "name"); 353 std::string name{}; 354 if (_raw != "") { 355 std::string &_value = _raw; 356 name = _value; 357 } 358 _raw = getXmlAttribute(root, "type"); 359 int64_t type{}; 360 if (_raw != "") { 361 int64_t _value = std::stoll(_raw); 362 type = _value; 363 } 364 std::vector<Configuration> configuration; 365 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 366 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("configuration"))) { 367 Configuration _value = Configuration::read(_child); 368 configuration.push_back(std::move(_value)); 369 } 370 } 371 Sensor instance(configuration, name, type); 372 return instance; 373 } 374 Sensors(std::vector<Sensor> sensor)375 Sensors::Sensors(std::vector<Sensor> sensor) : sensor_(std::move(sensor)) { 376 } 377 getSensor() const378 const std::vector<Sensor>& Sensors::getSensor() const { 379 return sensor_; 380 } 381 hasSensor() const382 bool Sensors::hasSensor() const { 383 return !(sensor_.empty()); 384 } 385 getFirstSensor() const386 const Sensor* Sensors::getFirstSensor() const { 387 if (sensor_.empty()) { 388 return nullptr; 389 } 390 return &sensor_[0]; 391 } 392 read(xmlNode * root)393 Sensors Sensors::read(xmlNode *root) { 394 std::string _raw; 395 std::vector<Sensor> sensor; 396 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 397 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("sensor"))) { 398 Sensor _value = Sensor::read(_child); 399 sensor.push_back(std::move(_value)); 400 } 401 } 402 Sensors instance(sensor); 403 return instance; 404 } 405 Module(std::vector<Sensors> sensors,std::string halName,float halVersion)406 Modules::Module::Module(std::vector<Sensors> sensors, std::string halName, float halVersion) : sensors_(std::move(sensors)), halName_(std::move(halName)), halVersion_(halVersion) { 407 } 408 getSensors() const409 const std::vector<Sensors>& Modules::Module::getSensors() const { 410 return sensors_; 411 } 412 hasSensors() const413 bool Modules::Module::hasSensors() const { 414 return !(sensors_.empty()); 415 } 416 getFirstSensors() const417 const Sensors* Modules::Module::getFirstSensors() const { 418 if (sensors_.empty()) { 419 return nullptr; 420 } 421 return &sensors_[0]; 422 } 423 getHalName() const424 const std::string& Modules::Module::getHalName() const { 425 return halName_; 426 } 427 hasHalName() const428 bool Modules::Module::hasHalName() const { 429 return true; 430 } 431 getHalVersion() const432 const float& Modules::Module::getHalVersion() const { 433 return halVersion_; 434 } 435 hasHalVersion() const436 bool Modules::Module::hasHalVersion() const { 437 return true; 438 } 439 read(xmlNode * root)440 Modules::Module Modules::Module::read(xmlNode *root) { 441 std::string _raw; 442 _raw = getXmlAttribute(root, "halName"); 443 std::string halName{}; 444 if (_raw != "") { 445 std::string &_value = _raw; 446 halName = _value; 447 } 448 _raw = getXmlAttribute(root, "halVersion"); 449 float halVersion{}; 450 if (_raw != "") { 451 float _value = std::stof(_raw); 452 halVersion = _value; 453 } 454 std::vector<Sensors> sensors; 455 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 456 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("sensors"))) { 457 Sensors _value = Sensors::read(_child); 458 sensors.push_back(std::move(_value)); 459 } 460 } 461 Modules::Module instance(sensors, halName, halVersion); 462 return instance; 463 } 464 Modules(std::vector<Module> _module)465 Modules::Modules(std::vector<Module> _module) : _module_(std::move(_module)) { 466 } 467 get_module() const468 const std::vector<Modules::Module>& Modules::get_module() const { 469 return _module_; 470 } 471 has_module() const472 bool Modules::has_module() const { 473 return !(_module_.empty()); 474 } 475 getFirst_module() const476 const Modules::Module* Modules::getFirst_module() const { 477 if (_module_.empty()) { 478 return nullptr; 479 } 480 return &_module_[0]; 481 } 482 read(xmlNode * root)483 Modules Modules::read(xmlNode *root) { 484 std::string _raw; 485 std::vector<Module> _module; 486 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 487 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("module"))) { 488 Module _value = Module::read(_child); 489 _module.push_back(std::move(_value)); 490 } 491 } 492 Modules instance(_module); 493 return instance; 494 } 495 SensorHalConfiguration(std::vector<Modules> modules,float version)496 SensorHalConfiguration::SensorHalConfiguration(std::vector<Modules> modules, float version) : modules_(std::move(modules)), version_(version) { 497 } 498 getModules() const499 const std::vector<Modules>& SensorHalConfiguration::getModules() const { 500 return modules_; 501 } 502 hasModules() const503 bool SensorHalConfiguration::hasModules() const { 504 return !(modules_.empty()); 505 } 506 getFirstModules() const507 const Modules* SensorHalConfiguration::getFirstModules() const { 508 if (modules_.empty()) { 509 return nullptr; 510 } 511 return &modules_[0]; 512 } 513 getVersion() const514 const float& SensorHalConfiguration::getVersion() const { 515 return version_; 516 } 517 hasVersion() const518 bool SensorHalConfiguration::hasVersion() const { 519 return true; 520 } 521 read(xmlNode * root)522 SensorHalConfiguration SensorHalConfiguration::read(xmlNode *root) { 523 std::string _raw; 524 _raw = getXmlAttribute(root, "version"); 525 float version{}; 526 if (_raw != "") { 527 float _value = std::stof(_raw); 528 version = _value; 529 } 530 std::vector<Modules> modules; 531 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 532 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("modules"))) { 533 Modules _value = Modules::read(_child); 534 modules.push_back(std::move(_value)); 535 } 536 } 537 SensorHalConfiguration instance(modules, version); 538 return instance; 539 } 540 } // V1_0 541 } // configuration 542 } // hal 543 } // sensor 544