1 /*
2  * Copyright (C) 2020 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 
17 // Autogenerated by dumpstate_hal_config.xsd
18 
19 #define LOG_TAG "dumpstate.hal.configuration.V1_0"
20 
21 #include <memory>
22 
23 #include <libxml/parser.h>
24 #include <libxml/xinclude.h>
25 
26 #include "config/dumpstate_hal_configuration_V1_0.h"
27 
28 namespace dumpstate {
29 namespace hal {
30 namespace configuration {
31 namespace V1_0 {
32 template <class T>
33 constexpr void (*xmlDeleter)(T* t);
34 template <>
35 constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc;
36 template <>
__anon9cce81ad0102(xmlChar* s) 37 auto xmlDeleter<xmlChar> = [](xmlChar* s) { xmlFree(s); };
38 
39 template <class T>
make_xmlUnique(T * t)40 constexpr auto make_xmlUnique(T* t) {
41     auto deleter = [](T* t) { xmlDeleter<T>(t); };
42     return std::unique_ptr<T, decltype(deleter)>{t, deleter};
43 }
44 
getXmlAttribute(const xmlNode * cur,const char * attribute)45 static std::string getXmlAttribute(const xmlNode* cur, const char* attribute) {
46     auto xmlValue = make_xmlUnique(xmlGetProp(cur, reinterpret_cast<const xmlChar*>(attribute)));
47     if (xmlValue == nullptr) {
48         return "";
49     }
50     std::string value(reinterpret_cast<const char*>(xmlValue.get()));
51     return value;
52 }
53 
readBuffer(const std::string & xml)54 std::optional<DumpstateHalConfiguration> readBuffer(const std::string& xml) {
55     auto doc = make_xmlUnique(xmlParseMemory(xml.data(), xml.size()));
56     if (doc == nullptr) {
57         return std::nullopt;
58     }
59     xmlNodePtr child = xmlDocGetRootElement(doc.get());
60     if (child == NULL) {
61         return std::nullopt;
62     }
63 
64     if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("dumpstateHalConfiguration"))) {
65         DumpstateHalConfiguration value = DumpstateHalConfiguration::read(child);
66         return value;
67     }
68     return std::nullopt;
69 }
70 
readFile(const char * configFile)71 std::optional<DumpstateHalConfiguration> readFile(const char* configFile) {
72     auto doc = make_xmlUnique(xmlParseFile(configFile));
73     if (doc == nullptr) {
74         return std::nullopt;
75     }
76     xmlNodePtr child = xmlDocGetRootElement(doc.get());
77     if (child == NULL) {
78         return std::nullopt;
79     }
80 
81     if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("dumpstateHalConfiguration"))) {
82         DumpstateHalConfiguration value = DumpstateHalConfiguration::read(child);
83         return value;
84     }
85     return std::nullopt;
86 }
87 
Service(std::string name,std::string command)88 Service::Service(std::string name, std::string command)
89     : name_(std::move(name)), command_(std::move(command)) {}
90 
getName() const91 const std::string& Service::getName() const {
92     return name_;
93 }
94 
hasName() const95 bool Service::hasName() const {
96     return true;
97 }
98 
getCommand() const99 const std::string& Service::getCommand() const {
100     return command_;
101 }
102 
hasCommand() const103 bool Service::hasCommand() const {
104     return true;
105 }
106 
read(xmlNode * root)107 Service Service::read(xmlNode* root) {
108     std::string raw;
109     raw = getXmlAttribute(root, "name");
110     std::string name;
111     if (raw != "") {
112         std::string& value = raw;
113         name = value;
114     }
115     raw = getXmlAttribute(root, "command");
116     std::string command;
117     if (raw != "") {
118         std::string& value = raw;
119         command = value;
120     }
121     Service instance(name, command);
122     return instance;
123 }
124 
Services(std::vector<Service> service)125 Services::Services(std::vector<Service> service) : service_(std::move(service)) {}
126 
getService() const127 const std::vector<Service>& Services::getService() const {
128     return service_;
129 }
130 
hasService() const131 bool Services::hasService() const {
132     return !(service_.empty());
133 }
134 
getFirstService() const135 const Service* Services::getFirstService() const {
136     if (service_.empty()) {
137         return nullptr;
138     }
139     return &service_[0];
140 }
141 
read(xmlNode * root)142 Services Services::read(xmlNode* root) {
143     std::string raw;
144     std::vector<Service> service;
145     for (xmlNode* child = root->xmlChildrenNode; child != nullptr; child = child->next) {
146         if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("service"))) {
147             Service value = Service::read(child);
148             service.push_back(std::move(value));
149         }
150     }
151     Services instance(service);
152     return instance;
153 }
154 
SystemLogs(std::vector<Service> service)155 SystemLogs::SystemLogs(std::vector<Service> service) : service_(std::move(service)) {}
156 
getService() const157 const std::vector<Service>& SystemLogs::getService() const {
158     return service_;
159 }
160 
hasService() const161 bool SystemLogs::hasService() const {
162     return !(service_.empty());
163 }
164 
getFirstService() const165 const Service* SystemLogs::getFirstService() const {
166     if (service_.empty()) {
167         return nullptr;
168     }
169     return &service_[0];
170 }
171 
read(xmlNode * root)172 SystemLogs SystemLogs::read(xmlNode* root) {
173     std::string raw;
174     std::vector<Service> service;
175     for (xmlNode* child = root->xmlChildrenNode; child != nullptr; child = child->next) {
176         if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("service"))) {
177             Service value = Service::read(child);
178             service.push_back(std::move(value));
179         }
180     }
181     SystemLogs instance(service);
182     return instance;
183 }
184 
DumpstateHalConfiguration(std::vector<Services> services,std::vector<SystemLogs> systemLogs,float version)185 DumpstateHalConfiguration::DumpstateHalConfiguration(std::vector<Services> services,
186                                                      std::vector<SystemLogs> systemLogs,
187                                                      float version)
188     : services_(std::move(services)), systemLogs_(std::move(systemLogs)), version_(version) {}
189 
getServices() const190 const std::vector<Services>& DumpstateHalConfiguration::getServices() const {
191     return services_;
192 }
193 
hasServices() const194 bool DumpstateHalConfiguration::hasServices() const {
195     return !(services_.empty());
196 }
197 
getFirstServices() const198 const Services* DumpstateHalConfiguration::getFirstServices() const {
199     if (services_.empty()) {
200         return nullptr;
201     }
202     return &services_[0];
203 }
204 
getSystemLogs() const205 const std::vector<SystemLogs>& DumpstateHalConfiguration::getSystemLogs() const {
206     return systemLogs_;
207 }
208 
hasSystemLogs() const209 bool DumpstateHalConfiguration::hasSystemLogs() const {
210     return !(systemLogs_.empty());
211 }
212 
getFirstSystemLogs() const213 const SystemLogs* DumpstateHalConfiguration::getFirstSystemLogs() const {
214     if (systemLogs_.empty()) {
215         return nullptr;
216     }
217     return &systemLogs_[0];
218 }
219 
getVersion() const220 const float& DumpstateHalConfiguration::getVersion() const {
221     return version_;
222 }
223 
hasVersion() const224 bool DumpstateHalConfiguration::hasVersion() const {
225     return true;
226 }
227 
read(xmlNode * root)228 DumpstateHalConfiguration DumpstateHalConfiguration::read(xmlNode* root) {
229     std::string raw;
230     raw = getXmlAttribute(root, "version");
231     float version = 0;
232     if (raw != "") {
233         float value = std::stof(raw);
234         version = value;
235     }
236     std::vector<Services> services;
237     std::vector<SystemLogs> systemLogs;
238     for (xmlNode* child = root->xmlChildrenNode; child != nullptr; child = child->next) {
239         if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("services"))) {
240             Services value = Services::read(child);
241             services.push_back(std::move(value));
242         } else if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>("systemLogs"))) {
243             SystemLogs value = SystemLogs::read(child);
244             systemLogs.push_back(std::move(value));
245         }
246     }
247     DumpstateHalConfiguration instance(services, systemLogs, version);
248     return instance;
249 }
250 }  // namespace V1_0
251 }  // namespace configuration
252 }  // namespace hal
253 }  // namespace dumpstate
254