1 #define LOG_TAG "aidl.android.hardware.bluetooth.audio.hfp.setting" 2 #include "aidl_android_hardware_bluetooth_audio_hfp_setting.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 aidl { 11 namespace android { 12 namespace hardware { 13 namespace bluetooth { 14 namespace audio { 15 namespace hfp { 16 namespace setting { 17 template <class T> 18 constexpr void (*xmlDeleter)(T* t); 19 template <> 20 constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc; 21 template <> __anon0595bfd80102(xmlChar *s) 22 auto xmlDeleter<xmlChar> = [](xmlChar *s) { xmlFree(s); }; 23 24 template <class T> make_xmlUnique(T * t)25 constexpr auto make_xmlUnique(T *t) { 26 auto deleter = [](T *t) { xmlDeleter<T>(t); }; 27 return std::unique_ptr<T, decltype(deleter)>{t, deleter}; 28 } 29 getXmlAttribute(const xmlNode * cur,const char * attribute)30 static std::string getXmlAttribute(const xmlNode *cur, const char *attribute) { 31 auto xmlValue = make_xmlUnique(xmlGetProp(cur, reinterpret_cast<const xmlChar*>(attribute))); 32 if (xmlValue == nullptr) { 33 return ""; 34 } 35 std::string value(reinterpret_cast<const char*>(xmlValue.get())); 36 return value; 37 } 38 readHfpOffloadSetting(const char * configFile)39 std::optional<HfpOffloadSetting> readHfpOffloadSetting(const char* configFile) { 40 auto doc = make_xmlUnique(xmlParseFile(configFile)); 41 if (doc == nullptr) { 42 return std::nullopt; 43 } 44 xmlNodePtr _child = xmlDocGetRootElement(doc.get()); 45 if (_child == nullptr) { 46 return std::nullopt; 47 } 48 if (xmlXIncludeProcess(doc.get()) < 0) { 49 return std::nullopt; 50 } 51 52 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("hfpOffloadSetting"))) { 53 HfpOffloadSetting _value = HfpOffloadSetting::read(_child); 54 return _value; 55 } 56 return std::nullopt; 57 } 58 parseHfpOffloadSetting(const char * xml)59 std::optional<HfpOffloadSetting> parseHfpOffloadSetting(const char* xml) { 60 auto doc = make_xmlUnique(xmlParseDoc(reinterpret_cast<const xmlChar*>(xml))); 61 if (doc == nullptr) { 62 return std::nullopt; 63 } 64 xmlNodePtr _child = xmlDocGetRootElement(doc.get()); 65 if (_child == nullptr) { 66 return std::nullopt; 67 } 68 if (xmlXIncludeProcess(doc.get()) < 0) { 69 return std::nullopt; 70 } 71 72 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("hfpOffloadSetting"))) { 73 HfpOffloadSetting _value = HfpOffloadSetting::read(_child); 74 return _value; 75 } 76 return std::nullopt; 77 } 78 79 HfpOffloadSetting(std::vector<PathConfiguration> pathConfiguration,std::vector<TransportConfiguration> transportConfiguration,std::vector<Configuration> configuration)80 HfpOffloadSetting::HfpOffloadSetting(std::vector<PathConfiguration> pathConfiguration, std::vector<TransportConfiguration> transportConfiguration, std::vector<Configuration> configuration) : pathConfiguration_(std::move(pathConfiguration)), transportConfiguration_(std::move(transportConfiguration)), configuration_(std::move(configuration)) { 81 } 82 getPathConfiguration() const83 const std::vector<PathConfiguration>& HfpOffloadSetting::getPathConfiguration() const { 84 return pathConfiguration_; 85 } 86 hasPathConfiguration() const87 bool HfpOffloadSetting::hasPathConfiguration() const { 88 return !(pathConfiguration_.empty()); 89 } 90 getFirstPathConfiguration() const91 const PathConfiguration* HfpOffloadSetting::getFirstPathConfiguration() const { 92 if (pathConfiguration_.empty()) { 93 return nullptr; 94 } 95 return &pathConfiguration_[0]; 96 } 97 getTransportConfiguration() const98 const std::vector<TransportConfiguration>& HfpOffloadSetting::getTransportConfiguration() const { 99 return transportConfiguration_; 100 } 101 hasTransportConfiguration() const102 bool HfpOffloadSetting::hasTransportConfiguration() const { 103 return !(transportConfiguration_.empty()); 104 } 105 getFirstTransportConfiguration() const106 const TransportConfiguration* HfpOffloadSetting::getFirstTransportConfiguration() const { 107 if (transportConfiguration_.empty()) { 108 return nullptr; 109 } 110 return &transportConfiguration_[0]; 111 } 112 getConfiguration() const113 const std::vector<Configuration>& HfpOffloadSetting::getConfiguration() const { 114 return configuration_; 115 } 116 hasConfiguration() const117 bool HfpOffloadSetting::hasConfiguration() const { 118 return !(configuration_.empty()); 119 } 120 getFirstConfiguration() const121 const Configuration* HfpOffloadSetting::getFirstConfiguration() const { 122 if (configuration_.empty()) { 123 return nullptr; 124 } 125 return &configuration_[0]; 126 } 127 read(xmlNode * root)128 HfpOffloadSetting HfpOffloadSetting::read(xmlNode *root) { 129 std::string _raw; 130 std::vector<PathConfiguration> pathConfiguration; 131 std::vector<TransportConfiguration> transportConfiguration; 132 std::vector<Configuration> configuration; 133 for (auto *_child = root->xmlChildrenNode; _child != nullptr; _child = _child->next) { 134 if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("pathConfiguration"))) { 135 PathConfiguration _value = PathConfiguration::read(_child); 136 pathConfiguration.push_back(std::move(_value)); 137 } else if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("transportConfiguration"))) { 138 TransportConfiguration _value = TransportConfiguration::read(_child); 139 transportConfiguration.push_back(std::move(_value)); 140 } else if (!xmlStrcmp(_child->name, reinterpret_cast<const xmlChar*>("configuration"))) { 141 Configuration _value = Configuration::read(_child); 142 configuration.push_back(std::move(_value)); 143 } 144 } 145 HfpOffloadSetting instance(pathConfiguration, transportConfiguration, configuration); 146 return instance; 147 } 148 PathConfiguration(std::optional<std::string> name,std::optional<unsigned int> bandwidth,std::optional<CodecType> codec,std::optional<unsigned short> codedDataSize,std::optional<unsigned char> pcmDataFormat,std::optional<unsigned char> pcmPayloadMsbPosition,std::optional<unsigned char> dataPath,std::optional<unsigned char> transportUnitSize)149 PathConfiguration::PathConfiguration(std::optional<std::string> name, std::optional<unsigned int> bandwidth, std::optional<CodecType> codec, std::optional<unsigned short> codedDataSize, std::optional<unsigned char> pcmDataFormat, std::optional<unsigned char> pcmPayloadMsbPosition, std::optional<unsigned char> dataPath, std::optional<unsigned char> transportUnitSize) : name_(std::move(name)), bandwidth_(bandwidth), codec_(codec), codedDataSize_(codedDataSize), pcmDataFormat_(pcmDataFormat), pcmPayloadMsbPosition_(pcmPayloadMsbPosition), dataPath_(dataPath), transportUnitSize_(transportUnitSize) { 150 } 151 getName() const152 const std::string& PathConfiguration::getName() const { 153 _xsdc_assert(hasName()); 154 return name_.value(); 155 } 156 hasName() const157 bool PathConfiguration::hasName() const { 158 return name_.has_value(); 159 } 160 getBandwidth() const161 const unsigned int& PathConfiguration::getBandwidth() const { 162 _xsdc_assert(hasBandwidth()); 163 return bandwidth_.value(); 164 } 165 hasBandwidth() const166 bool PathConfiguration::hasBandwidth() const { 167 return bandwidth_.has_value(); 168 } 169 getCodec() const170 const CodecType& PathConfiguration::getCodec() const { 171 _xsdc_assert(hasCodec()); 172 return codec_.value(); 173 } 174 hasCodec() const175 bool PathConfiguration::hasCodec() const { 176 return codec_.has_value(); 177 } 178 getCodedDataSize() const179 const unsigned short& PathConfiguration::getCodedDataSize() const { 180 _xsdc_assert(hasCodedDataSize()); 181 return codedDataSize_.value(); 182 } 183 hasCodedDataSize() const184 bool PathConfiguration::hasCodedDataSize() const { 185 return codedDataSize_.has_value(); 186 } 187 getPcmDataFormat() const188 const unsigned char& PathConfiguration::getPcmDataFormat() const { 189 _xsdc_assert(hasPcmDataFormat()); 190 return pcmDataFormat_.value(); 191 } 192 hasPcmDataFormat() const193 bool PathConfiguration::hasPcmDataFormat() const { 194 return pcmDataFormat_.has_value(); 195 } 196 getPcmPayloadMsbPosition() const197 const unsigned char& PathConfiguration::getPcmPayloadMsbPosition() const { 198 _xsdc_assert(hasPcmPayloadMsbPosition()); 199 return pcmPayloadMsbPosition_.value(); 200 } 201 hasPcmPayloadMsbPosition() const202 bool PathConfiguration::hasPcmPayloadMsbPosition() const { 203 return pcmPayloadMsbPosition_.has_value(); 204 } 205 getDataPath() const206 const unsigned char& PathConfiguration::getDataPath() const { 207 _xsdc_assert(hasDataPath()); 208 return dataPath_.value(); 209 } 210 hasDataPath() const211 bool PathConfiguration::hasDataPath() const { 212 return dataPath_.has_value(); 213 } 214 getTransportUnitSize() const215 const unsigned char& PathConfiguration::getTransportUnitSize() const { 216 _xsdc_assert(hasTransportUnitSize()); 217 return transportUnitSize_.value(); 218 } 219 hasTransportUnitSize() const220 bool PathConfiguration::hasTransportUnitSize() const { 221 return transportUnitSize_.has_value(); 222 } 223 read(xmlNode * root)224 PathConfiguration PathConfiguration::read(xmlNode *root) { 225 std::string _raw; 226 _raw = getXmlAttribute(root, "name"); 227 std::optional<std::string> name = std::nullopt; 228 if (_raw != "") { 229 std::string &_value = _raw; 230 name = _value; 231 } 232 _raw = getXmlAttribute(root, "bandwidth"); 233 std::optional<unsigned int> bandwidth = std::nullopt; 234 if (_raw != "") { 235 unsigned int _value = static_cast<unsigned int>(stoul(_raw)); 236 bandwidth = _value; 237 } 238 _raw = getXmlAttribute(root, "codec"); 239 std::optional<CodecType> codec = std::nullopt; 240 if (_raw != "") { 241 CodecType _value = stringToCodecType(_raw); 242 codec = _value; 243 } 244 _raw = getXmlAttribute(root, "codedDataSize"); 245 std::optional<unsigned short> codedDataSize = std::nullopt; 246 if (_raw != "") { 247 unsigned short _value = static_cast<unsigned short>(std::stoi(_raw)); 248 codedDataSize = _value; 249 } 250 _raw = getXmlAttribute(root, "pcmDataFormat"); 251 std::optional<unsigned char> pcmDataFormat = std::nullopt; 252 if (_raw != "") { 253 unsigned char _value = static_cast<unsigned char>(std::stoi(_raw)); 254 pcmDataFormat = _value; 255 } 256 _raw = getXmlAttribute(root, "pcmPayloadMsbPosition"); 257 std::optional<unsigned char> pcmPayloadMsbPosition = std::nullopt; 258 if (_raw != "") { 259 unsigned char _value = static_cast<unsigned char>(std::stoi(_raw)); 260 pcmPayloadMsbPosition = _value; 261 } 262 _raw = getXmlAttribute(root, "dataPath"); 263 std::optional<unsigned char> dataPath = std::nullopt; 264 if (_raw != "") { 265 unsigned char _value = static_cast<unsigned char>(std::stoi(_raw)); 266 dataPath = _value; 267 } 268 _raw = getXmlAttribute(root, "transportUnitSize"); 269 std::optional<unsigned char> transportUnitSize = std::nullopt; 270 if (_raw != "") { 271 unsigned char _value = static_cast<unsigned char>(std::stoi(_raw)); 272 transportUnitSize = _value; 273 } 274 PathConfiguration instance(name, bandwidth, codec, codedDataSize, pcmDataFormat, pcmPayloadMsbPosition, dataPath, transportUnitSize); 275 return instance; 276 } 277 TransportConfiguration(std::optional<std::string> name,std::optional<unsigned int> bandwidth,std::optional<CodecType> codec,std::optional<unsigned short> codedFrameSize)278 TransportConfiguration::TransportConfiguration(std::optional<std::string> name, std::optional<unsigned int> bandwidth, std::optional<CodecType> codec, std::optional<unsigned short> codedFrameSize) : name_(std::move(name)), bandwidth_(bandwidth), codec_(codec), codedFrameSize_(codedFrameSize) { 279 } 280 getName() const281 const std::string& TransportConfiguration::getName() const { 282 _xsdc_assert(hasName()); 283 return name_.value(); 284 } 285 hasName() const286 bool TransportConfiguration::hasName() const { 287 return name_.has_value(); 288 } 289 getBandwidth() const290 const unsigned int& TransportConfiguration::getBandwidth() const { 291 _xsdc_assert(hasBandwidth()); 292 return bandwidth_.value(); 293 } 294 hasBandwidth() const295 bool TransportConfiguration::hasBandwidth() const { 296 return bandwidth_.has_value(); 297 } 298 getCodec() const299 const CodecType& TransportConfiguration::getCodec() const { 300 _xsdc_assert(hasCodec()); 301 return codec_.value(); 302 } 303 hasCodec() const304 bool TransportConfiguration::hasCodec() const { 305 return codec_.has_value(); 306 } 307 getCodedFrameSize() const308 const unsigned short& TransportConfiguration::getCodedFrameSize() const { 309 _xsdc_assert(hasCodedFrameSize()); 310 return codedFrameSize_.value(); 311 } 312 hasCodedFrameSize() const313 bool TransportConfiguration::hasCodedFrameSize() const { 314 return codedFrameSize_.has_value(); 315 } 316 read(xmlNode * root)317 TransportConfiguration TransportConfiguration::read(xmlNode *root) { 318 std::string _raw; 319 _raw = getXmlAttribute(root, "name"); 320 std::optional<std::string> name = std::nullopt; 321 if (_raw != "") { 322 std::string &_value = _raw; 323 name = _value; 324 } 325 _raw = getXmlAttribute(root, "bandwidth"); 326 std::optional<unsigned int> bandwidth = std::nullopt; 327 if (_raw != "") { 328 unsigned int _value = static_cast<unsigned int>(stoul(_raw)); 329 bandwidth = _value; 330 } 331 _raw = getXmlAttribute(root, "codec"); 332 std::optional<CodecType> codec = std::nullopt; 333 if (_raw != "") { 334 CodecType _value = stringToCodecType(_raw); 335 codec = _value; 336 } 337 _raw = getXmlAttribute(root, "codedFrameSize"); 338 std::optional<unsigned short> codedFrameSize = std::nullopt; 339 if (_raw != "") { 340 unsigned short _value = static_cast<unsigned short>(std::stoi(_raw)); 341 codedFrameSize = _value; 342 } 343 TransportConfiguration instance(name, bandwidth, codec, codedFrameSize); 344 return instance; 345 } 346 Configuration(std::optional<std::string> name,std::optional<CodecType> codec,std::optional<unsigned short> maxLatencyMs,std::optional<unsigned short> packetTypes,std::optional<unsigned char> retransmissionEffort,std::optional<bool> useControllerCodec,std::optional<std::string> inputPathConfiguration,std::optional<std::string> outputPathConfiguration,std::optional<std::string> inputTransportConfiguration,std::optional<std::string> outputTransportConfiguration)347 Configuration::Configuration(std::optional<std::string> name, std::optional<CodecType> codec, std::optional<unsigned short> maxLatencyMs, std::optional<unsigned short> packetTypes, std::optional<unsigned char> retransmissionEffort, std::optional<bool> useControllerCodec, std::optional<std::string> inputPathConfiguration, std::optional<std::string> outputPathConfiguration, std::optional<std::string> inputTransportConfiguration, std::optional<std::string> outputTransportConfiguration) : name_(std::move(name)), codec_(codec), maxLatencyMs_(maxLatencyMs), packetTypes_(packetTypes), retransmissionEffort_(retransmissionEffort), useControllerCodec_(useControllerCodec), inputPathConfiguration_(std::move(inputPathConfiguration)), outputPathConfiguration_(std::move(outputPathConfiguration)), inputTransportConfiguration_(std::move(inputTransportConfiguration)), outputTransportConfiguration_(std::move(outputTransportConfiguration)) { 348 } 349 getName() const350 const std::string& Configuration::getName() const { 351 _xsdc_assert(hasName()); 352 return name_.value(); 353 } 354 hasName() const355 bool Configuration::hasName() const { 356 return name_.has_value(); 357 } 358 getCodec() const359 const CodecType& Configuration::getCodec() const { 360 _xsdc_assert(hasCodec()); 361 return codec_.value(); 362 } 363 hasCodec() const364 bool Configuration::hasCodec() const { 365 return codec_.has_value(); 366 } 367 getMaxLatencyMs() const368 const unsigned short& Configuration::getMaxLatencyMs() const { 369 _xsdc_assert(hasMaxLatencyMs()); 370 return maxLatencyMs_.value(); 371 } 372 hasMaxLatencyMs() const373 bool Configuration::hasMaxLatencyMs() const { 374 return maxLatencyMs_.has_value(); 375 } 376 getPacketTypes() const377 const unsigned short& Configuration::getPacketTypes() const { 378 _xsdc_assert(hasPacketTypes()); 379 return packetTypes_.value(); 380 } 381 hasPacketTypes() const382 bool Configuration::hasPacketTypes() const { 383 return packetTypes_.has_value(); 384 } 385 getRetransmissionEffort() const386 const unsigned char& Configuration::getRetransmissionEffort() const { 387 _xsdc_assert(hasRetransmissionEffort()); 388 return retransmissionEffort_.value(); 389 } 390 hasRetransmissionEffort() const391 bool Configuration::hasRetransmissionEffort() const { 392 return retransmissionEffort_.has_value(); 393 } 394 getUseControllerCodec() const395 const bool& Configuration::getUseControllerCodec() const { 396 _xsdc_assert(hasUseControllerCodec()); 397 return useControllerCodec_.value(); 398 } 399 hasUseControllerCodec() const400 bool Configuration::hasUseControllerCodec() const { 401 return useControllerCodec_.has_value(); 402 } 403 getInputPathConfiguration() const404 const std::string& Configuration::getInputPathConfiguration() const { 405 _xsdc_assert(hasInputPathConfiguration()); 406 return inputPathConfiguration_.value(); 407 } 408 hasInputPathConfiguration() const409 bool Configuration::hasInputPathConfiguration() const { 410 return inputPathConfiguration_.has_value(); 411 } 412 getOutputPathConfiguration() const413 const std::string& Configuration::getOutputPathConfiguration() const { 414 _xsdc_assert(hasOutputPathConfiguration()); 415 return outputPathConfiguration_.value(); 416 } 417 hasOutputPathConfiguration() const418 bool Configuration::hasOutputPathConfiguration() const { 419 return outputPathConfiguration_.has_value(); 420 } 421 getInputTransportConfiguration() const422 const std::string& Configuration::getInputTransportConfiguration() const { 423 _xsdc_assert(hasInputTransportConfiguration()); 424 return inputTransportConfiguration_.value(); 425 } 426 hasInputTransportConfiguration() const427 bool Configuration::hasInputTransportConfiguration() const { 428 return inputTransportConfiguration_.has_value(); 429 } 430 getOutputTransportConfiguration() const431 const std::string& Configuration::getOutputTransportConfiguration() const { 432 _xsdc_assert(hasOutputTransportConfiguration()); 433 return outputTransportConfiguration_.value(); 434 } 435 hasOutputTransportConfiguration() const436 bool Configuration::hasOutputTransportConfiguration() const { 437 return outputTransportConfiguration_.has_value(); 438 } 439 read(xmlNode * root)440 Configuration Configuration::read(xmlNode *root) { 441 std::string _raw; 442 _raw = getXmlAttribute(root, "name"); 443 std::optional<std::string> name = std::nullopt; 444 if (_raw != "") { 445 std::string &_value = _raw; 446 name = _value; 447 } 448 _raw = getXmlAttribute(root, "codec"); 449 std::optional<CodecType> codec = std::nullopt; 450 if (_raw != "") { 451 CodecType _value = stringToCodecType(_raw); 452 codec = _value; 453 } 454 _raw = getXmlAttribute(root, "maxLatencyMs"); 455 std::optional<unsigned short> maxLatencyMs = std::nullopt; 456 if (_raw != "") { 457 unsigned short _value = static_cast<unsigned short>(std::stoi(_raw)); 458 maxLatencyMs = _value; 459 } 460 _raw = getXmlAttribute(root, "packetTypes"); 461 std::optional<unsigned short> packetTypes = std::nullopt; 462 if (_raw != "") { 463 unsigned short _value = static_cast<unsigned short>(std::stoi(_raw)); 464 packetTypes = _value; 465 } 466 _raw = getXmlAttribute(root, "retransmissionEffort"); 467 std::optional<unsigned char> retransmissionEffort = std::nullopt; 468 if (_raw != "") { 469 unsigned char _value = static_cast<unsigned char>(std::stoi(_raw)); 470 retransmissionEffort = _value; 471 } 472 _raw = getXmlAttribute(root, "useControllerCodec"); 473 std::optional<bool> useControllerCodec = std::nullopt; 474 if (_raw != "") { 475 bool _value = _raw == "true"; 476 useControllerCodec = _value; 477 } 478 _raw = getXmlAttribute(root, "inputPathConfiguration"); 479 std::optional<std::string> inputPathConfiguration = std::nullopt; 480 if (_raw != "") { 481 std::string &_value = _raw; 482 inputPathConfiguration = _value; 483 } 484 _raw = getXmlAttribute(root, "outputPathConfiguration"); 485 std::optional<std::string> outputPathConfiguration = std::nullopt; 486 if (_raw != "") { 487 std::string &_value = _raw; 488 outputPathConfiguration = _value; 489 } 490 _raw = getXmlAttribute(root, "inputTransportConfiguration"); 491 std::optional<std::string> inputTransportConfiguration = std::nullopt; 492 if (_raw != "") { 493 std::string &_value = _raw; 494 inputTransportConfiguration = _value; 495 } 496 _raw = getXmlAttribute(root, "outputTransportConfiguration"); 497 std::optional<std::string> outputTransportConfiguration = std::nullopt; 498 if (_raw != "") { 499 std::string &_value = _raw; 500 outputTransportConfiguration = _value; 501 } 502 Configuration instance(name, codec, maxLatencyMs, packetTypes, retransmissionEffort, useControllerCodec, inputPathConfiguration, outputPathConfiguration, inputTransportConfiguration, outputTransportConfiguration); 503 return instance; 504 } 505 } // setting 506 } // hfp 507 } // audio 508 } // bluetooth 509 } // hardware 510 } // android 511 } // aidl 512