parse_xml.cpp 50 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. * Copyright (C) 2017 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. // Convert objects from and to xml.
  17. #define LOG_TAG "libvintf"
  18. #include <android-base/logging.h>
  19. #include "parse_xml.h"
  20. #include <type_traits>
  21. #include <tinyxml2.h>
  22. #include "Regex.h"
  23. #include "constants.h"
  24. #include "parse_string.h"
  25. namespace android {
  26. namespace vintf {
  27. // --------------- tinyxml2 details
  28. using NodeType = tinyxml2::XMLElement;
  29. using DocType = tinyxml2::XMLDocument;
  30. // caller is responsible for deleteDocument() call
  31. inline DocType *createDocument() {
  32. return new tinyxml2::XMLDocument();
  33. }
  34. // caller is responsible for deleteDocument() call
  35. inline DocType *createDocument(const std::string &xml) {
  36. DocType *doc = new tinyxml2::XMLDocument();
  37. if (doc->Parse(xml.c_str()) == tinyxml2::XML_SUCCESS) {
  38. return doc;
  39. }
  40. delete doc;
  41. return nullptr;
  42. }
  43. inline void deleteDocument(DocType *d) {
  44. delete d;
  45. }
  46. inline std::string printDocument(DocType *d) {
  47. tinyxml2::XMLPrinter p;
  48. d->Print(&p);
  49. return std::string{p.CStr()};
  50. }
  51. inline NodeType *createNode(const std::string &name, DocType *d) {
  52. return d->NewElement(name.c_str());
  53. }
  54. inline void appendChild(NodeType *parent, NodeType *child) {
  55. parent->InsertEndChild(child);
  56. }
  57. inline void appendChild(DocType *parent, NodeType *child) {
  58. parent->InsertEndChild(child);
  59. }
  60. inline void appendStrAttr(NodeType *e, const std::string &attrName, const std::string &attr) {
  61. e->SetAttribute(attrName.c_str(), attr.c_str());
  62. }
  63. // text -> text
  64. inline void appendText(NodeType *parent, const std::string &text, DocType *d) {
  65. parent->InsertEndChild(d->NewText(text.c_str()));
  66. }
  67. inline std::string nameOf(NodeType *root) {
  68. return root->Name() == NULL ? "" : root->Name();
  69. }
  70. inline std::string getText(NodeType *root) {
  71. return root->GetText() == NULL ? "" : root->GetText();
  72. }
  73. inline NodeType *getChild(NodeType *parent, const std::string &name) {
  74. return parent->FirstChildElement(name.c_str());
  75. }
  76. inline NodeType *getRootChild(DocType *parent) {
  77. return parent->FirstChildElement();
  78. }
  79. inline std::vector<NodeType *> getChildren(NodeType *parent, const std::string &name) {
  80. std::vector<NodeType *> v;
  81. for (NodeType *child = parent->FirstChildElement(name.c_str());
  82. child != nullptr;
  83. child = child->NextSiblingElement(name.c_str())) {
  84. v.push_back(child);
  85. }
  86. return v;
  87. }
  88. inline bool getAttr(NodeType *root, const std::string &attrName, std::string *s) {
  89. const char *c = root->Attribute(attrName.c_str());
  90. if (c == NULL)
  91. return false;
  92. *s = c;
  93. return true;
  94. }
  95. // --------------- tinyxml2 details end.
  96. // Helper functions for XmlConverter
  97. static bool parse(const std::string &attrText, bool *attr) {
  98. if (attrText == "true" || attrText == "1") {
  99. *attr = true;
  100. return true;
  101. }
  102. if (attrText == "false" || attrText == "0") {
  103. *attr = false;
  104. return true;
  105. }
  106. return false;
  107. }
  108. // ---------------------- XmlNodeConverter definitions
  109. template<typename Object>
  110. struct XmlNodeConverter : public XmlConverter<Object> {
  111. XmlNodeConverter() {}
  112. virtual ~XmlNodeConverter() {}
  113. // sub-types should implement these.
  114. virtual void mutateNode(const Object &o, NodeType *n, DocType *d) const = 0;
  115. virtual void mutateNode(const Object& o, NodeType* n, DocType* d, SerializeFlags::Type) const {
  116. mutateNode(o, n, d);
  117. }
  118. virtual bool buildObject(Object* o, NodeType* n, std::string* error) const = 0;
  119. virtual std::string elementName() const = 0;
  120. // convenience methods for user
  121. inline const std::string& lastError() const override { return mLastError; }
  122. inline NodeType* serialize(const Object& o, DocType* d,
  123. SerializeFlags::Type flags = SerializeFlags::EVERYTHING) const {
  124. NodeType *root = createNode(this->elementName(), d);
  125. this->mutateNode(o, root, d, flags);
  126. return root;
  127. }
  128. inline std::string serialize(const Object& o, SerializeFlags::Type flags) const override {
  129. DocType *doc = createDocument();
  130. appendChild(doc, serialize(o, doc, flags));
  131. std::string s = printDocument(doc);
  132. deleteDocument(doc);
  133. return s;
  134. }
  135. inline bool deserialize(Object* object, NodeType* root) {
  136. bool ret = deserialize(object, root, &mLastError);
  137. return ret;
  138. }
  139. inline bool deserialize(Object* o, const std::string& xml) override {
  140. bool ret = (*this)(o, xml, &mLastError);
  141. return ret;
  142. }
  143. inline bool deserialize(Object* object, NodeType* root, std::string* error) const {
  144. if (nameOf(root) != this->elementName()) {
  145. return false;
  146. }
  147. return this->buildObject(object, root, error);
  148. }
  149. inline bool operator()(Object* o, const std::string& xml, std::string* error) const override {
  150. std::string errorBuffer;
  151. if (error == nullptr) error = &errorBuffer;
  152. auto doc = createDocument(xml);
  153. if (doc == nullptr) {
  154. *error = "Not a valid XML";
  155. return false;
  156. }
  157. bool ret = deserialize(o, getRootChild(doc), error);
  158. deleteDocument(doc);
  159. return ret;
  160. }
  161. inline NodeType *operator()(const Object &o, DocType *d) const {
  162. return serialize(o, d);
  163. }
  164. inline std::string operator()(const Object& o, SerializeFlags::Type flags) const override {
  165. return serialize(o, flags);
  166. }
  167. inline bool operator()(Object* o, NodeType* node) { return deserialize(o, node); }
  168. inline bool operator()(Object* o, const std::string& xml) override {
  169. return deserialize(o, xml);
  170. }
  171. // convenience methods for implementor.
  172. // All append* functions helps mutateNode() to serialize the object into XML.
  173. template <typename T>
  174. inline void appendAttr(NodeType *e, const std::string &attrName, const T &attr) const {
  175. return appendStrAttr(e, attrName, ::android::vintf::to_string(attr));
  176. }
  177. inline void appendAttr(NodeType *e, const std::string &attrName, bool attr) const {
  178. return appendStrAttr(e, attrName, attr ? "true" : "false");
  179. }
  180. // text -> <name>text</name>
  181. inline void appendTextElement(NodeType *parent, const std::string &name,
  182. const std::string &text, DocType *d) const {
  183. NodeType *c = createNode(name, d);
  184. appendText(c, text, d);
  185. appendChild(parent, c);
  186. }
  187. // text -> <name>text</name>
  188. template<typename Array>
  189. inline void appendTextElements(NodeType *parent, const std::string &name,
  190. const Array &array, DocType *d) const {
  191. for (const std::string &text : array) {
  192. NodeType *c = createNode(name, d);
  193. appendText(c, text, d);
  194. appendChild(parent, c);
  195. }
  196. }
  197. template <typename T, typename Array>
  198. inline void appendChildren(NodeType* parent, const XmlNodeConverter<T>& conv,
  199. const Array& array, DocType* d,
  200. SerializeFlags::Type flags = SerializeFlags::EVERYTHING) const {
  201. for (const T &t : array) {
  202. appendChild(parent, conv.serialize(t, d, flags));
  203. }
  204. }
  205. // All parse* functions helps buildObject() to deserialize XML to the object. Returns
  206. // true if deserialization is successful, false if any error, and "error" will be
  207. // set to error message.
  208. template <typename T>
  209. inline bool parseOptionalAttr(NodeType* root, const std::string& attrName, T&& defaultValue,
  210. T* attr, std::string* /* error */) const {
  211. std::string attrText;
  212. bool success = getAttr(root, attrName, &attrText) &&
  213. ::android::vintf::parse(attrText, attr);
  214. if (!success) {
  215. *attr = std::move(defaultValue);
  216. }
  217. return true;
  218. }
  219. template <typename T>
  220. inline bool parseAttr(NodeType* root, const std::string& attrName, T* attr,
  221. std::string* error) const {
  222. std::string attrText;
  223. bool ret = getAttr(root, attrName, &attrText) && ::android::vintf::parse(attrText, attr);
  224. if (!ret) {
  225. *error = "Could not find/parse attr with name \"" + attrName + "\" and value \"" +
  226. attrText + "\" for element <" + elementName() + ">";
  227. }
  228. return ret;
  229. }
  230. inline bool parseAttr(NodeType* root, const std::string& attrName, std::string* attr,
  231. std::string* error) const {
  232. bool ret = getAttr(root, attrName, attr);
  233. if (!ret) {
  234. *error = "Could not find attr with name \"" + attrName + "\" for element <" +
  235. elementName() + ">";
  236. }
  237. return ret;
  238. }
  239. inline bool parseTextElement(NodeType* root, const std::string& elementName, std::string* s,
  240. std::string* error) const {
  241. NodeType *child = getChild(root, elementName);
  242. if (child == nullptr) {
  243. *error = "Could not find element with name <" + elementName + "> in element <" +
  244. this->elementName() + ">";
  245. return false;
  246. }
  247. *s = getText(child);
  248. return true;
  249. }
  250. inline bool parseOptionalTextElement(NodeType* root, const std::string& elementName,
  251. std::string&& defaultValue, std::string* s,
  252. std::string* /* error */) const {
  253. NodeType* child = getChild(root, elementName);
  254. *s = child == nullptr ? std::move(defaultValue) : getText(child);
  255. return true;
  256. }
  257. inline bool parseTextElements(NodeType* root, const std::string& elementName,
  258. std::vector<std::string>* v, std::string* /* error */) const {
  259. auto nodes = getChildren(root, elementName);
  260. v->resize(nodes.size());
  261. for (size_t i = 0; i < nodes.size(); ++i) {
  262. v->at(i) = getText(nodes[i]);
  263. }
  264. return true;
  265. }
  266. template <typename T>
  267. inline bool parseChild(NodeType* root, const XmlNodeConverter<T>& conv, T* t,
  268. std::string* error) const {
  269. NodeType *child = getChild(root, conv.elementName());
  270. if (child == nullptr) {
  271. *error = "Could not find element with name <" + conv.elementName() + "> in element <" +
  272. this->elementName() + ">";
  273. return false;
  274. }
  275. return conv.deserialize(t, child, error);
  276. }
  277. template <typename T>
  278. inline bool parseOptionalChild(NodeType* root, const XmlNodeConverter<T>& conv,
  279. T&& defaultValue, T* t, std::string* error) const {
  280. NodeType *child = getChild(root, conv.elementName());
  281. if (child == nullptr) {
  282. *t = std::move(defaultValue);
  283. return true;
  284. }
  285. return conv.deserialize(t, child, error);
  286. }
  287. template <typename T>
  288. inline bool parseOptionalChild(NodeType* root, const XmlNodeConverter<T>& conv,
  289. std::optional<T>* t, std::string* error) const {
  290. NodeType* child = getChild(root, conv.elementName());
  291. if (child == nullptr) {
  292. *t = std::nullopt;
  293. return true;
  294. }
  295. *t = std::make_optional<T>();
  296. return conv.deserialize(&**t, child, error);
  297. }
  298. template <typename T>
  299. inline bool parseChildren(NodeType* root, const XmlNodeConverter<T>& conv, std::vector<T>* v,
  300. std::string* error) const {
  301. auto nodes = getChildren(root, conv.elementName());
  302. v->resize(nodes.size());
  303. for (size_t i = 0; i < nodes.size(); ++i) {
  304. if (!conv.deserialize(&v->at(i), nodes[i], error)) {
  305. *error = "Could not parse element with name <" + conv.elementName() +
  306. "> in element <" + this->elementName() + ">: " + *error;
  307. return false;
  308. }
  309. }
  310. return true;
  311. }
  312. template <typename Container, typename T = typename Container::value_type,
  313. typename = typename Container::key_compare>
  314. inline bool parseChildren(NodeType* root, const XmlNodeConverter<T>& conv, Container* s,
  315. std::string* error) const {
  316. std::vector<T> vec;
  317. if (!parseChildren(root, conv, &vec, error)) {
  318. return false;
  319. }
  320. s->clear();
  321. s->insert(vec.begin(), vec.end());
  322. if (s->size() != vec.size()) {
  323. *error = "Duplicated elements <" + conv.elementName() + "> in element <" +
  324. this->elementName() + ">";
  325. s->clear();
  326. return false;
  327. }
  328. return true;
  329. }
  330. template <typename K, typename V>
  331. inline bool parseChildren(NodeType* root, const XmlNodeConverter<std::pair<K, V>>& conv,
  332. std::map<K, V>* s, std::string* error) const {
  333. return parseChildren<std::map<K, V>, std::pair<K, V>>(root, conv, s, error);
  334. }
  335. inline bool parseText(NodeType* node, std::string* s, std::string* /* error */) const {
  336. *s = getText(node);
  337. return true;
  338. }
  339. template <typename T>
  340. inline bool parseText(NodeType* node, T* s, std::string* error) const {
  341. std::string text = getText(node);
  342. bool ret = ::android::vintf::parse(text, s);
  343. if (!ret) {
  344. *error = "Could not parse text \"" + text + "\" in element <" + elementName() + ">";
  345. }
  346. return ret;
  347. }
  348. private:
  349. mutable std::string mLastError;
  350. };
  351. template<typename Object>
  352. struct XmlTextConverter : public XmlNodeConverter<Object> {
  353. XmlTextConverter(const std::string &elementName)
  354. : mElementName(elementName) {}
  355. virtual void mutateNode(const Object &object, NodeType *root, DocType *d) const override {
  356. appendText(root, ::android::vintf::to_string(object), d);
  357. }
  358. virtual bool buildObject(Object* object, NodeType* root, std::string* error) const override {
  359. return this->parseText(root, object, error);
  360. }
  361. virtual std::string elementName() const { return mElementName; }
  362. private:
  363. std::string mElementName;
  364. };
  365. template <typename Pair>
  366. struct XmlPairConverter : public XmlNodeConverter<Pair> {
  367. XmlPairConverter(
  368. const std::string& elementName,
  369. std::unique_ptr<XmlNodeConverter<typename Pair::first_type>>&& firstConverter,
  370. std::unique_ptr<XmlNodeConverter<typename Pair::second_type>>&& secondConverter)
  371. : mElementName(elementName),
  372. mFirstConverter(std::move(firstConverter)),
  373. mSecondConverter(std::move(secondConverter)) {}
  374. virtual void mutateNode(const Pair& pair, NodeType* root, DocType* d) const override {
  375. appendChild(root, mFirstConverter->serialize(pair.first, d));
  376. appendChild(root, mSecondConverter->serialize(pair.second, d));
  377. }
  378. virtual bool buildObject(Pair* pair, NodeType* root, std::string* error) const override {
  379. return this->parseChild(root, *mFirstConverter, &pair->first, error) &&
  380. this->parseChild(root, *mSecondConverter, &pair->second, error);
  381. }
  382. virtual std::string elementName() const { return mElementName; }
  383. private:
  384. std::string mElementName;
  385. std::unique_ptr<XmlNodeConverter<typename Pair::first_type>> mFirstConverter;
  386. std::unique_ptr<XmlNodeConverter<typename Pair::second_type>> mSecondConverter;
  387. };
  388. // ---------------------- XmlNodeConverter definitions end
  389. XmlTextConverter<Version> versionConverter{"version"};
  390. XmlTextConverter<VersionRange> versionRangeConverter{"version"};
  391. struct TransportArchConverter : public XmlNodeConverter<TransportArch> {
  392. std::string elementName() const override { return "transport"; }
  393. void mutateNode(const TransportArch &object, NodeType *root, DocType *d) const override {
  394. if (object.arch != Arch::ARCH_EMPTY) {
  395. appendAttr(root, "arch", object.arch);
  396. }
  397. appendText(root, ::android::vintf::to_string(object.transport), d);
  398. }
  399. bool buildObject(TransportArch* object, NodeType* root, std::string* error) const override {
  400. if (!parseOptionalAttr(root, "arch", Arch::ARCH_EMPTY, &object->arch, error) ||
  401. !parseText(root, &object->transport, error)) {
  402. return false;
  403. }
  404. if (!object->isValid()) {
  405. *error = "transport == " + ::android::vintf::to_string(object->transport) +
  406. " and arch == " + ::android::vintf::to_string(object->arch) +
  407. " is not a valid combination.";
  408. return false;
  409. }
  410. return true;
  411. }
  412. };
  413. TransportArchConverter transportArchConverter{};
  414. struct KernelConfigTypedValueConverter : public XmlNodeConverter<KernelConfigTypedValue> {
  415. std::string elementName() const override { return "value"; }
  416. void mutateNode(const KernelConfigTypedValue &object, NodeType *root, DocType *d) const override {
  417. appendAttr(root, "type", object.mType);
  418. appendText(root, ::android::vintf::to_string(object), d);
  419. }
  420. bool buildObject(KernelConfigTypedValue* object, NodeType* root,
  421. std::string* error) const override {
  422. std::string stringValue;
  423. if (!parseAttr(root, "type", &object->mType, error) ||
  424. !parseText(root, &stringValue, error)) {
  425. return false;
  426. }
  427. if (!::android::vintf::parseKernelConfigValue(stringValue, object)) {
  428. *error = "Could not parse kernel config value \"" + stringValue + "\"";
  429. return false;
  430. }
  431. return true;
  432. }
  433. };
  434. KernelConfigTypedValueConverter kernelConfigTypedValueConverter{};
  435. XmlPairConverter<KernelConfig> matrixKernelConfigConverter{
  436. "config", std::make_unique<XmlTextConverter<KernelConfigKey>>("key"),
  437. std::make_unique<KernelConfigTypedValueConverter>(kernelConfigTypedValueConverter)};
  438. struct HalInterfaceConverter : public XmlNodeConverter<HalInterface> {
  439. std::string elementName() const override { return "interface"; }
  440. void mutateNode(const HalInterface &intf, NodeType *root, DocType *d) const override {
  441. appendTextElement(root, "name", intf.name(), d);
  442. appendTextElements(root, "instance", intf.mInstances, d);
  443. appendTextElements(root, "regex-instance", intf.mRegexes, d);
  444. }
  445. bool buildObject(HalInterface* intf, NodeType* root, std::string* error) const override {
  446. std::vector<std::string> instances;
  447. std::vector<std::string> regexes;
  448. if (!parseTextElement(root, "name", &intf->mName, error) ||
  449. !parseTextElements(root, "instance", &instances, error) ||
  450. !parseTextElements(root, "regex-instance", &regexes, error)) {
  451. return false;
  452. }
  453. bool success = true;
  454. for (const auto& e : instances) {
  455. if (!intf->insertInstance(e, false /* isRegex */)) {
  456. if (!error->empty()) *error += "\n";
  457. *error += "Duplicated instance '" + e + "' in " + intf->name();
  458. success = false;
  459. }
  460. }
  461. for (const auto& e : regexes) {
  462. details::Regex regex;
  463. if (!regex.compile(e)) {
  464. if (!error->empty()) *error += "\n";
  465. *error += "Invalid regular expression '" + e + "' in " + intf->name();
  466. success = false;
  467. }
  468. if (!intf->insertInstance(e, true /* isRegex */)) {
  469. if (!error->empty()) *error += "\n";
  470. *error += "Duplicated regex-instance '" + e + "' in " + intf->name();
  471. success = false;
  472. }
  473. }
  474. return success;
  475. }
  476. };
  477. HalInterfaceConverter halInterfaceConverter{};
  478. struct MatrixHalConverter : public XmlNodeConverter<MatrixHal> {
  479. std::string elementName() const override { return "hal"; }
  480. void mutateNode(const MatrixHal &hal, NodeType *root, DocType *d) const override {
  481. appendAttr(root, "format", hal.format);
  482. appendAttr(root, "optional", hal.optional);
  483. appendTextElement(root, "name", hal.name, d);
  484. appendChildren(root, versionRangeConverter, hal.versionRanges, d);
  485. appendChildren(root, halInterfaceConverter, iterateValues(hal.interfaces), d);
  486. }
  487. bool buildObject(MatrixHal* object, NodeType* root, std::string* error) const override {
  488. std::vector<HalInterface> interfaces;
  489. if (!parseOptionalAttr(root, "format", HalFormat::HIDL, &object->format, error) ||
  490. !parseOptionalAttr(root, "optional", false /* defaultValue */, &object->optional,
  491. error) ||
  492. !parseTextElement(root, "name", &object->name, error) ||
  493. !parseChildren(root, versionRangeConverter, &object->versionRanges, error) ||
  494. !parseChildren(root, halInterfaceConverter, &interfaces, error)) {
  495. return false;
  496. }
  497. for (auto&& interface : interfaces) {
  498. std::string name{interface.name()};
  499. auto res = object->interfaces.emplace(std::move(name), std::move(interface));
  500. if (!res.second) {
  501. *error = "Duplicated interface entry \"" + res.first->first +
  502. "\"; if additional instances are needed, add them to the "
  503. "existing <interface> node.";
  504. return false;
  505. }
  506. }
  507. // Do not check for target-side libvintf to avoid restricting ability for upgrade accidentally.
  508. #ifndef LIBVINTF_TARGET
  509. if (!checkAdditionalRestrictionsOnHal(*object, error)) {
  510. return false;
  511. }
  512. #endif
  513. return true;
  514. }
  515. #ifndef LIBVINTF_TARGET
  516. private:
  517. bool checkAdditionalRestrictionsOnHal(const MatrixHal& hal, std::string* error) const {
  518. if (hal.getName() == "netutils-wrapper") {
  519. if (hal.versionRanges.size() != 1) {
  520. *error =
  521. "netutils-wrapper HAL must specify exactly one version x.0, "
  522. "but multiple <version> element is specified.";
  523. return false;
  524. }
  525. const VersionRange& v = hal.versionRanges.at(0);
  526. if (!v.isSingleVersion()) {
  527. *error =
  528. "netutils-wrapper HAL must specify exactly one version x.0, "
  529. "but a range is provided. Perhaps you mean '" +
  530. to_string(Version{v.majorVer, 0}) + "'?";
  531. return false;
  532. }
  533. if (v.minMinor != 0) {
  534. *error =
  535. "netutils-wrapper HAL must specify exactly one version x.0, "
  536. "but minor version is not 0. Perhaps you mean '" +
  537. to_string(Version{v.majorVer, 0}) + "'?";
  538. return false;
  539. }
  540. }
  541. return true;
  542. }
  543. #endif
  544. };
  545. MatrixHalConverter matrixHalConverter{};
  546. struct MatrixKernelConditionsConverter : public XmlNodeConverter<std::vector<KernelConfig>> {
  547. std::string elementName() const override { return "conditions"; }
  548. void mutateNode(const std::vector<KernelConfig>& conds, NodeType* root,
  549. DocType* d) const override {
  550. appendChildren(root, matrixKernelConfigConverter, conds, d);
  551. }
  552. bool buildObject(std::vector<KernelConfig>* object, NodeType* root,
  553. std::string* error) const override {
  554. return parseChildren(root, matrixKernelConfigConverter, object, error);
  555. }
  556. };
  557. MatrixKernelConditionsConverter matrixKernelConditionsConverter{};
  558. struct MatrixKernelConverter : public XmlNodeConverter<MatrixKernel> {
  559. std::string elementName() const override { return "kernel"; }
  560. void mutateNode(const MatrixKernel& kernel, NodeType* root, DocType* d) const override {
  561. mutateNode(kernel, root, d, SerializeFlags::EVERYTHING);
  562. }
  563. void mutateNode(const MatrixKernel& kernel, NodeType* root, DocType* d,
  564. SerializeFlags::Type flags) const override {
  565. KernelVersion kv = kernel.mMinLts;
  566. if (!flags.isKernelMinorRevisionEnabled()) {
  567. kv.minorRev = 0u;
  568. }
  569. appendAttr(root, "version", kv);
  570. if (!kernel.mConditions.empty()) {
  571. appendChild(root, matrixKernelConditionsConverter(kernel.mConditions, d));
  572. }
  573. if (flags.isKernelConfigsEnabled()) {
  574. appendChildren(root, matrixKernelConfigConverter, kernel.mConfigs, d);
  575. }
  576. }
  577. bool buildObject(MatrixKernel* object, NodeType* root, std::string* error) const override {
  578. if (!parseAttr(root, "version", &object->mMinLts, error) ||
  579. !parseOptionalChild(root, matrixKernelConditionsConverter, {}, &object->mConditions,
  580. error) ||
  581. !parseChildren(root, matrixKernelConfigConverter, &object->mConfigs, error)) {
  582. return false;
  583. }
  584. return true;
  585. }
  586. };
  587. MatrixKernelConverter matrixKernelConverter{};
  588. XmlTextConverter<FqInstance> fqInstanceConverter{"fqname"};
  589. struct ManifestHalConverter : public XmlNodeConverter<ManifestHal> {
  590. std::string elementName() const override { return "hal"; }
  591. void mutateNode(const ManifestHal& m, NodeType* root, DocType* d) const override {
  592. mutateNode(m, root, d, SerializeFlags::EVERYTHING);
  593. }
  594. void mutateNode(const ManifestHal& hal, NodeType* root, DocType* d,
  595. SerializeFlags::Type flags) const override {
  596. appendAttr(root, "format", hal.format);
  597. appendTextElement(root, "name", hal.name, d);
  598. appendChild(root, transportArchConverter(hal.transportArch, d));
  599. appendChildren(root, versionConverter, hal.versions, d);
  600. appendChildren(root, halInterfaceConverter, iterateValues(hal.interfaces), d);
  601. if (hal.isOverride()) {
  602. appendAttr(root, "override", hal.isOverride());
  603. }
  604. if (flags.isFqnameEnabled()) {
  605. std::set<FqInstance> fqInstances;
  606. hal.forEachInstance([&fqInstances](const auto& manifestInstance) {
  607. fqInstances.emplace(manifestInstance.getFqInstanceNoPackage());
  608. return true;
  609. });
  610. appendChildren(root, fqInstanceConverter, fqInstances, d);
  611. }
  612. }
  613. bool buildObject(ManifestHal* object, NodeType* root, std::string* error) const override {
  614. std::vector<HalInterface> interfaces;
  615. if (!parseOptionalAttr(root, "format", HalFormat::HIDL, &object->format, error) ||
  616. !parseOptionalAttr(root, "override", false, &object->mIsOverride, error) ||
  617. !parseTextElement(root, "name", &object->name, error) ||
  618. !parseOptionalChild(root, transportArchConverter, {}, &object->transportArch, error) ||
  619. !parseChildren(root, versionConverter, &object->versions, error) ||
  620. !parseChildren(root, halInterfaceConverter, &interfaces, error)) {
  621. return false;
  622. }
  623. switch (object->format) {
  624. case HalFormat::HIDL: {
  625. if (object->transportArch.empty()) {
  626. *error = "HIDL HAL '" + object->name + "' should have <transport> defined.";
  627. return false;
  628. }
  629. } break;
  630. case HalFormat::NATIVE: {
  631. if (!object->transportArch.empty()) {
  632. *error =
  633. "Native HAL '" + object->name + "' should not have <transport> defined.";
  634. return false;
  635. }
  636. } break;
  637. default: {
  638. LOG(FATAL) << "Unhandled HalFormat "
  639. << static_cast<typename std::underlying_type<HalFormat>::type>(
  640. object->format);
  641. } break;
  642. }
  643. if (!object->transportArch.isValid()) return false;
  644. object->interfaces.clear();
  645. for (auto &&interface : interfaces) {
  646. auto res = object->interfaces.emplace(interface.name(), std::move(interface));
  647. if (!res.second) {
  648. *error = "Duplicated interface entry \"" + res.first->first +
  649. "\"; if additional instances are needed, add them to the "
  650. "existing <interface> node.";
  651. return false;
  652. }
  653. }
  654. if (!object->isValid()) {
  655. *error = "'" + object->name + "' is not a valid Manifest HAL.";
  656. return false;
  657. }
  658. // Do not check for target-side libvintf to avoid restricting upgrade accidentally.
  659. #ifndef LIBVINTF_TARGET
  660. if (!checkAdditionalRestrictionsOnHal(*object, error)) {
  661. return false;
  662. }
  663. #endif
  664. std::set<FqInstance> fqInstances;
  665. if (!parseChildren(root, fqInstanceConverter, &fqInstances, error)) {
  666. return false;
  667. }
  668. for (const auto& e : fqInstances) {
  669. if (e.hasPackage()) {
  670. *error = "Should not specify package: \"" + e.string() + "\"";
  671. return false;
  672. }
  673. }
  674. if (!object->insertInstances(fqInstances, error)) {
  675. return false;
  676. }
  677. return true;
  678. }
  679. #ifndef LIBVINTF_TARGET
  680. private:
  681. bool checkAdditionalRestrictionsOnHal(const ManifestHal& hal, std::string* error) const {
  682. if (hal.getName() == "netutils-wrapper") {
  683. for (const Version& v : hal.versions) {
  684. if (v.minorVer != 0) {
  685. *error =
  686. "netutils-wrapper HAL must specify exactly one version x.0, "
  687. "but minor version is not 0. Perhaps you mean '" +
  688. to_string(Version{v.majorVer, 0}) + "'?";
  689. return false;
  690. }
  691. }
  692. }
  693. return true;
  694. }
  695. #endif
  696. };
  697. // Convert ManifestHal from and to XML. Returned object is guaranteed to have
  698. // .isValid() == true.
  699. ManifestHalConverter manifestHalConverter{};
  700. XmlTextConverter<KernelSepolicyVersion> kernelSepolicyVersionConverter{"kernel-sepolicy-version"};
  701. XmlTextConverter<VersionRange> sepolicyVersionConverter{"sepolicy-version"};
  702. struct SepolicyConverter : public XmlNodeConverter<Sepolicy> {
  703. std::string elementName() const override { return "sepolicy"; }
  704. void mutateNode(const Sepolicy &object, NodeType *root, DocType *d) const override {
  705. appendChild(root, kernelSepolicyVersionConverter(object.kernelSepolicyVersion(), d));
  706. appendChildren(root, sepolicyVersionConverter, object.sepolicyVersions(), d);
  707. }
  708. bool buildObject(Sepolicy* object, NodeType* root, std::string* error) const override {
  709. if (!parseChild(root, kernelSepolicyVersionConverter, &object->mKernelSepolicyVersion,
  710. error) ||
  711. !parseChildren(root, sepolicyVersionConverter, &object->mSepolicyVersionRanges,
  712. error)) {
  713. return false;
  714. }
  715. return true;
  716. }
  717. };
  718. SepolicyConverter sepolicyConverter{};
  719. [[deprecated]] XmlTextConverter<VndkVersionRange> vndkVersionRangeConverter{"version"};
  720. XmlTextConverter<std::string> vndkVersionConverter{"version"};
  721. XmlTextConverter<std::string> vndkLibraryConverter{"library"};
  722. struct [[deprecated]] VndkConverter : public XmlNodeConverter<Vndk> {
  723. std::string elementName() const override { return "vndk"; }
  724. void mutateNode(const Vndk &object, NodeType *root, DocType *d) const override {
  725. appendChild(root, vndkVersionRangeConverter(object.mVersionRange, d));
  726. appendChildren(root, vndkLibraryConverter, object.mLibraries, d);
  727. }
  728. bool buildObject(Vndk* object, NodeType* root, std::string* error) const override {
  729. if (!parseChild(root, vndkVersionRangeConverter, &object->mVersionRange, error) ||
  730. !parseChildren(root, vndkLibraryConverter, &object->mLibraries, error)) {
  731. return false;
  732. }
  733. return true;
  734. }
  735. };
  736. [[deprecated]] VndkConverter vndkConverter{};
  737. struct VendorNdkConverter : public XmlNodeConverter<VendorNdk> {
  738. std::string elementName() const override { return "vendor-ndk"; }
  739. void mutateNode(const VendorNdk& object, NodeType* root, DocType* d) const override {
  740. appendChild(root, vndkVersionConverter(object.mVersion, d));
  741. appendChildren(root, vndkLibraryConverter, object.mLibraries, d);
  742. }
  743. bool buildObject(VendorNdk* object, NodeType* root, std::string* error) const override {
  744. if (!parseChild(root, vndkVersionConverter, &object->mVersion, error) ||
  745. !parseChildren(root, vndkLibraryConverter, &object->mLibraries, error)) {
  746. return false;
  747. }
  748. return true;
  749. }
  750. };
  751. VendorNdkConverter vendorNdkConverter{};
  752. XmlTextConverter<std::string> systemSdkVersionConverter{"version"};
  753. struct SystemSdkConverter : public XmlNodeConverter<SystemSdk> {
  754. std::string elementName() const override { return "system-sdk"; }
  755. void mutateNode(const SystemSdk& object, NodeType* root, DocType* d) const override {
  756. appendChildren(root, systemSdkVersionConverter, object.versions(), d);
  757. }
  758. bool buildObject(SystemSdk* object, NodeType* root, std::string* error) const override {
  759. return parseChildren(root, systemSdkVersionConverter, &object->mVersions, error);
  760. }
  761. };
  762. SystemSdkConverter systemSdkConverter{};
  763. struct HalManifestSepolicyConverter : public XmlNodeConverter<Version> {
  764. std::string elementName() const override { return "sepolicy"; }
  765. void mutateNode(const Version &m, NodeType *root, DocType *d) const override {
  766. appendChild(root, versionConverter(m, d));
  767. }
  768. bool buildObject(Version* object, NodeType* root, std::string* error) const override {
  769. return parseChild(root, versionConverter, object, error);
  770. }
  771. };
  772. HalManifestSepolicyConverter halManifestSepolicyConverter{};
  773. struct ManifestXmlFileConverter : public XmlNodeConverter<ManifestXmlFile> {
  774. std::string elementName() const override { return "xmlfile"; }
  775. void mutateNode(const ManifestXmlFile& f, NodeType* root, DocType* d) const override {
  776. appendTextElement(root, "name", f.name(), d);
  777. appendChild(root, versionConverter(f.version(), d));
  778. if (!f.overriddenPath().empty()) {
  779. appendTextElement(root, "path", f.overriddenPath(), d);
  780. }
  781. }
  782. bool buildObject(ManifestXmlFile* object, NodeType* root, std::string* error) const override {
  783. if (!parseTextElement(root, "name", &object->mName, error) ||
  784. !parseChild(root, versionConverter, &object->mVersion, error) ||
  785. !parseOptionalTextElement(root, "path", {}, &object->mOverriddenPath, error)) {
  786. return false;
  787. }
  788. return true;
  789. }
  790. };
  791. ManifestXmlFileConverter manifestXmlFileConverter{};
  792. XmlPairConverter<std::pair<std::string, std::string>> kernelConfigConverter{
  793. "config", std::make_unique<XmlTextConverter<std::string>>("key"),
  794. std::make_unique<XmlTextConverter<std::string>>("value")};
  795. struct KernelInfoConverter : public XmlNodeConverter<KernelInfo> {
  796. std::string elementName() const override { return "kernel"; }
  797. void mutateNode(const KernelInfo& o, NodeType* root, DocType* d) const override {
  798. mutateNode(o, root, d, SerializeFlags::EVERYTHING);
  799. }
  800. void mutateNode(const KernelInfo& o, NodeType* root, DocType* d,
  801. SerializeFlags::Type flags) const override {
  802. if (o.version() != KernelVersion{}) {
  803. appendAttr(root, "version", o.version());
  804. }
  805. if (flags.isKernelConfigsEnabled()) {
  806. appendChildren(root, kernelConfigConverter, o.configs(), d);
  807. }
  808. }
  809. bool buildObject(KernelInfo* o, NodeType* root, std::string* error) const override {
  810. return parseOptionalAttr(root, "version", {}, &o->mVersion, error) &&
  811. parseChildren(root, kernelConfigConverter, &o->mConfigs, error);
  812. }
  813. };
  814. KernelInfoConverter kernelInfoConverter{};
  815. struct HalManifestConverter : public XmlNodeConverter<HalManifest> {
  816. std::string elementName() const override { return "manifest"; }
  817. void mutateNode(const HalManifest &m, NodeType *root, DocType *d) const override {
  818. mutateNode(m, root, d, SerializeFlags::EVERYTHING);
  819. }
  820. void mutateNode(const HalManifest& m, NodeType* root, DocType* d,
  821. SerializeFlags::Type flags) const override {
  822. if (flags.isMetaVersionEnabled()) {
  823. appendAttr(root, "version", m.getMetaVersion());
  824. }
  825. if (flags.isSchemaTypeEnabled()) {
  826. appendAttr(root, "type", m.mType);
  827. }
  828. if (flags.isHalsEnabled()) {
  829. appendChildren(root, manifestHalConverter, m.getHals(), d, flags);
  830. }
  831. if (m.mType == SchemaType::DEVICE) {
  832. if (flags.isSepolicyEnabled()) {
  833. if (m.device.mSepolicyVersion != Version{}) {
  834. appendChild(root, halManifestSepolicyConverter(m.device.mSepolicyVersion, d));
  835. }
  836. }
  837. if (m.mLevel != Level::UNSPECIFIED) {
  838. this->appendAttr(root, "target-level", m.mLevel);
  839. }
  840. if (flags.isKernelEnabled()) {
  841. if (!!m.kernel()) {
  842. appendChild(root, kernelInfoConverter.serialize(*m.kernel(), d, flags));
  843. }
  844. }
  845. } else if (m.mType == SchemaType::FRAMEWORK) {
  846. if (flags.isVndkEnabled()) {
  847. #pragma clang diagnostic push
  848. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  849. appendChildren(root, vndkConverter, m.framework.mVndks, d);
  850. #pragma clang diagnostic pop
  851. appendChildren(root, vendorNdkConverter, m.framework.mVendorNdks, d);
  852. }
  853. if (flags.isSsdkEnabled()) {
  854. if (!m.framework.mSystemSdk.empty()) {
  855. appendChild(root, systemSdkConverter(m.framework.mSystemSdk, d));
  856. }
  857. }
  858. }
  859. if (flags.isXmlFilesEnabled()) {
  860. appendChildren(root, manifestXmlFileConverter, m.getXmlFiles(), d);
  861. }
  862. }
  863. bool buildObject(HalManifest* object, NodeType* root, std::string* error) const override {
  864. std::vector<ManifestHal> hals;
  865. if (!parseAttr(root, "version", &object->mMetaVersion, error) ||
  866. !parseAttr(root, "type", &object->mType, error) ||
  867. !parseChildren(root, manifestHalConverter, &hals, error)) {
  868. return false;
  869. }
  870. if (!kMetaVersion.minorAtLeast(object->mMetaVersion)) {
  871. *error = "Unrecognized manifest.version " + to_string(object->mMetaVersion) +
  872. " (libvintf@" + to_string(kMetaVersion) + ")";
  873. return false;
  874. }
  875. if (object->mType == SchemaType::DEVICE) {
  876. // tags for device hal manifest only.
  877. // <sepolicy> can be missing because it can be determined at build time, not hard-coded
  878. // in the XML file.
  879. if (!parseOptionalChild(root, halManifestSepolicyConverter, {},
  880. &object->device.mSepolicyVersion, error)) {
  881. return false;
  882. }
  883. if (!parseOptionalAttr(root, "target-level", Level::UNSPECIFIED, &object->mLevel,
  884. error)) {
  885. return false;
  886. }
  887. if (!parseOptionalChild(root, kernelInfoConverter, &object->device.mKernel, error)) {
  888. return false;
  889. }
  890. } else if (object->mType == SchemaType::FRAMEWORK) {
  891. #pragma clang diagnostic push
  892. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  893. if (!parseChildren(root, vndkConverter, &object->framework.mVndks, error)) {
  894. return false;
  895. }
  896. for (const auto &vndk : object->framework.mVndks) {
  897. if (!vndk.mVersionRange.isSingleVersion()) {
  898. *error = "vndk.version " + to_string(vndk.mVersionRange) +
  899. " cannot be a range for manifests";
  900. return false;
  901. }
  902. }
  903. #pragma clang diagnostic pop
  904. if (!parseChildren(root, vendorNdkConverter, &object->framework.mVendorNdks, error)) {
  905. return false;
  906. }
  907. std::set<std::string> vendorNdkVersions;
  908. for (const auto& vendorNdk : object->framework.mVendorNdks) {
  909. if (vendorNdkVersions.find(vendorNdk.version()) != vendorNdkVersions.end()) {
  910. *error = "Duplicated manifest.vendor-ndk.version " + vendorNdk.version();
  911. return false;
  912. }
  913. vendorNdkVersions.insert(vendorNdk.version());
  914. }
  915. if (!parseOptionalChild(root, systemSdkConverter, {}, &object->framework.mSystemSdk,
  916. error)) {
  917. return false;
  918. }
  919. }
  920. for (auto &&hal : hals) {
  921. std::string description{hal.name};
  922. if (!object->add(std::move(hal))) {
  923. *error = "Duplicated manifest.hal entry " + description;
  924. return false;
  925. }
  926. }
  927. std::vector<ManifestXmlFile> xmlFiles;
  928. if (!parseChildren(root, manifestXmlFileConverter, &xmlFiles, error)) {
  929. return false;
  930. }
  931. for (auto&& xmlFile : xmlFiles) {
  932. std::string description{xmlFile.name()};
  933. if (!object->addXmlFile(std::move(xmlFile))) {
  934. *error = "Duplicated manifest.xmlfile entry " + description +
  935. "; entries cannot have duplicated name and version";
  936. return false;
  937. }
  938. }
  939. return true;
  940. }
  941. };
  942. HalManifestConverter halManifestConverter{};
  943. XmlTextConverter<Version> avbVersionConverter{"vbmeta-version"};
  944. struct AvbConverter : public XmlNodeConverter<Version> {
  945. std::string elementName() const override { return "avb"; }
  946. void mutateNode(const Version &m, NodeType *root, DocType *d) const override {
  947. appendChild(root, avbVersionConverter(m, d));
  948. }
  949. bool buildObject(Version* object, NodeType* root, std::string* error) const override {
  950. return parseChild(root, avbVersionConverter, object, error);
  951. }
  952. };
  953. AvbConverter avbConverter{};
  954. struct MatrixXmlFileConverter : public XmlNodeConverter<MatrixXmlFile> {
  955. std::string elementName() const override { return "xmlfile"; }
  956. void mutateNode(const MatrixXmlFile& f, NodeType* root, DocType* d) const override {
  957. appendTextElement(root, "name", f.name(), d);
  958. appendAttr(root, "format", f.format());
  959. appendAttr(root, "optional", f.optional());
  960. appendChild(root, versionRangeConverter(f.versionRange(), d));
  961. if (!f.overriddenPath().empty()) {
  962. appendTextElement(root, "path", f.overriddenPath(), d);
  963. }
  964. }
  965. bool buildObject(MatrixXmlFile* object, NodeType* root, std::string* error) const override {
  966. if (!parseTextElement(root, "name", &object->mName, error) ||
  967. !parseAttr(root, "format", &object->mFormat, error) ||
  968. !parseOptionalAttr(root, "optional", false, &object->mOptional, error) ||
  969. !parseChild(root, versionRangeConverter, &object->mVersionRange, error) ||
  970. !parseOptionalTextElement(root, "path", {}, &object->mOverriddenPath, error)) {
  971. return false;
  972. }
  973. return true;
  974. }
  975. };
  976. MatrixXmlFileConverter matrixXmlFileConverter{};
  977. struct CompatibilityMatrixConverter : public XmlNodeConverter<CompatibilityMatrix> {
  978. std::string elementName() const override { return "compatibility-matrix"; }
  979. void mutateNode(const CompatibilityMatrix &m, NodeType *root, DocType *d) const override {
  980. mutateNode(m, root, d, SerializeFlags::EVERYTHING);
  981. }
  982. void mutateNode(const CompatibilityMatrix& m, NodeType* root, DocType* d,
  983. SerializeFlags::Type flags) const override {
  984. if (flags.isMetaVersionEnabled()) {
  985. appendAttr(root, "version", m.getMinimumMetaVersion());
  986. }
  987. if (flags.isSchemaTypeEnabled()) {
  988. appendAttr(root, "type", m.mType);
  989. }
  990. if (flags.isHalsEnabled()) {
  991. appendChildren(root, matrixHalConverter, iterateValues(m.mHals), d);
  992. }
  993. if (m.mType == SchemaType::FRAMEWORK) {
  994. if (flags.isKernelEnabled()) {
  995. appendChildren(root, matrixKernelConverter, m.framework.mKernels, d, flags);
  996. }
  997. if (flags.isSepolicyEnabled()) {
  998. if (!(m.framework.mSepolicy == Sepolicy{})) {
  999. appendChild(root, sepolicyConverter(m.framework.mSepolicy, d));
  1000. }
  1001. }
  1002. if (flags.isAvbEnabled()) {
  1003. if (!(m.framework.mAvbMetaVersion == Version{})) {
  1004. appendChild(root, avbConverter(m.framework.mAvbMetaVersion, d));
  1005. }
  1006. }
  1007. if (m.mLevel != Level::UNSPECIFIED) {
  1008. this->appendAttr(root, "level", m.mLevel);
  1009. }
  1010. } else if (m.mType == SchemaType::DEVICE) {
  1011. if (flags.isVndkEnabled()) {
  1012. #pragma clang diagnostic push
  1013. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1014. if (!(m.device.mVndk == Vndk{})) {
  1015. appendChild(root, vndkConverter(m.device.mVndk, d));
  1016. }
  1017. #pragma clang diagnostic pop
  1018. if (!(m.device.mVendorNdk == VendorNdk{})) {
  1019. appendChild(root, vendorNdkConverter(m.device.mVendorNdk, d));
  1020. }
  1021. }
  1022. if (flags.isSsdkEnabled()) {
  1023. if (!m.device.mSystemSdk.empty()) {
  1024. appendChild(root, systemSdkConverter(m.device.mSystemSdk, d));
  1025. }
  1026. }
  1027. }
  1028. if (flags.isXmlFilesEnabled()) {
  1029. appendChildren(root, matrixXmlFileConverter, m.getXmlFiles(), d);
  1030. }
  1031. }
  1032. bool buildObject(CompatibilityMatrix* object, NodeType* root,
  1033. std::string* error) const override {
  1034. Version version;
  1035. std::vector<MatrixHal> hals;
  1036. if (!parseAttr(root, "version", &version, error) ||
  1037. !parseAttr(root, "type", &object->mType, error) ||
  1038. !parseChildren(root, matrixHalConverter, &hals, error)) {
  1039. return false;
  1040. }
  1041. if (object->mType == SchemaType::FRAMEWORK) {
  1042. // <avb> and <sepolicy> can be missing because it can be determined at build time, not
  1043. // hard-coded in the XML file.
  1044. if (!parseChildren(root, matrixKernelConverter, &object->framework.mKernels, error) ||
  1045. !parseOptionalChild(root, sepolicyConverter, {}, &object->framework.mSepolicy,
  1046. error) ||
  1047. !parseOptionalChild(root, avbConverter, {}, &object->framework.mAvbMetaVersion,
  1048. error)) {
  1049. return false;
  1050. }
  1051. std::set<Version> seenKernelVersions;
  1052. for (const auto& kernel : object->framework.mKernels) {
  1053. Version minLts(kernel.minLts().version, kernel.minLts().majorRev);
  1054. if (seenKernelVersions.find(minLts) != seenKernelVersions.end()) {
  1055. continue;
  1056. }
  1057. if (!kernel.conditions().empty()) {
  1058. *error = "First <kernel> for version " + to_string(minLts) +
  1059. " must have empty <conditions> for backwards compatibility.";
  1060. return false;
  1061. }
  1062. seenKernelVersions.insert(minLts);
  1063. }
  1064. if (!parseOptionalAttr(root, "level", Level::UNSPECIFIED, &object->mLevel, error)) {
  1065. return false;
  1066. }
  1067. } else if (object->mType == SchemaType::DEVICE) {
  1068. // <vndk> can be missing because it can be determined at build time, not hard-coded
  1069. // in the XML file.
  1070. #pragma clang diagnostic push
  1071. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  1072. if (!parseOptionalChild(root, vndkConverter, {}, &object->device.mVndk, error)) {
  1073. return false;
  1074. }
  1075. #pragma clang diagnostic pop
  1076. if (!parseOptionalChild(root, vendorNdkConverter, {}, &object->device.mVendorNdk,
  1077. error)) {
  1078. return false;
  1079. }
  1080. if (!parseOptionalChild(root, systemSdkConverter, {}, &object->device.mSystemSdk,
  1081. error)) {
  1082. return false;
  1083. }
  1084. }
  1085. if (!kMetaVersion.minorAtLeast(version)) {
  1086. *error = "Unrecognized compatibility-matrix.version " + to_string(version) +
  1087. " (libvintf@" + to_string(kMetaVersion) + ")";
  1088. return false;
  1089. }
  1090. for (auto &&hal : hals) {
  1091. if (!object->add(std::move(hal))) {
  1092. *error = "Duplicated compatibility-matrix.hal entry";
  1093. return false;
  1094. }
  1095. }
  1096. std::vector<MatrixXmlFile> xmlFiles;
  1097. if (!parseChildren(root, matrixXmlFileConverter, &xmlFiles, error)) {
  1098. return false;
  1099. }
  1100. for (auto&& xmlFile : xmlFiles) {
  1101. if (!xmlFile.optional()) {
  1102. *error = "compatibility-matrix.xmlfile entry " + xmlFile.name() +
  1103. " has to be optional for compatibility matrix version 1.0";
  1104. return false;
  1105. }
  1106. std::string description{xmlFile.name()};
  1107. if (!object->addXmlFile(std::move(xmlFile))) {
  1108. *error = "Duplicated compatibility-matrix.xmlfile entry " + description;
  1109. return false;
  1110. }
  1111. }
  1112. return true;
  1113. }
  1114. };
  1115. CompatibilityMatrixConverter compatibilityMatrixConverter{};
  1116. // Publicly available as in parse_xml.h
  1117. XmlConverter<HalManifest>& gHalManifestConverter = halManifestConverter;
  1118. XmlConverter<CompatibilityMatrix>& gCompatibilityMatrixConverter = compatibilityMatrixConverter;
  1119. // For testing in LibVintfTest
  1120. XmlConverter<Version>& gVersionConverter = versionConverter;
  1121. XmlConverter<KernelConfigTypedValue>& gKernelConfigTypedValueConverter =
  1122. kernelConfigTypedValueConverter;
  1123. XmlConverter<MatrixHal>& gMatrixHalConverter = matrixHalConverter;
  1124. XmlConverter<ManifestHal>& gManifestHalConverter = manifestHalConverter;
  1125. XmlConverter<KernelInfo>& gKernelInfoConverter = kernelInfoConverter;
  1126. } // namespace vintf
  1127. } // namespace android