1 /******************************************************************************
2 *
3 * Copyright 2014 Google, Inc.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at:
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 ******************************************************************************/
18
19 #define LOG_TAG "bt_btif_config_transcode"
20
21 #include "osi/include/config.h"
22
23 #ifndef OS_ANDROID
btif_config_transcode(const char * xml_filename)24 std::unique_ptr<config_t> btif_config_transcode(const char* xml_filename) {
25 // Legacy XML config never exists for non-Android
26 return nullptr;
27 }
28 #else
29 #include <tinyxml2.h>
30
31 #include "osi/include/log.h"
32
33 using namespace tinyxml2;
34
btif_config_transcode(const char * xml_filename)35 std::unique_ptr<config_t> btif_config_transcode(const char* xml_filename) {
36 XMLDocument document;
37 int error = document.LoadFile(xml_filename);
38 if (error != XML_SUCCESS) {
39 LOG_ERROR("%s unable to load XML file '%s': %d", __func__, xml_filename,
40 error);
41 return NULL;
42 }
43
44 XMLElement* rootElement = document.RootElement();
45 if (!rootElement) {
46 LOG_ERROR("%s unable to find root element; assuming corrupted config file.",
47 __func__);
48 return NULL;
49 }
50
51 std::unique_ptr<config_t> config = config_new_empty();
52
53 for (XMLElement* i = rootElement->FirstChildElement(); i != NULL;
54 i = i->NextSiblingElement())
55 for (XMLElement* j = i->FirstChildElement(); j != NULL;
56 j = j->NextSiblingElement()) {
57 const char* section = j->Attribute("Tag");
58 for (XMLElement* k = j->FirstChildElement(); k != NULL;
59 k = k->NextSiblingElement()) {
60 const char* key = k->Attribute("Tag");
61 const char* value = k->GetText();
62 if (section && key && value)
63 config_set_string(config.get(), section, key, value);
64 }
65 }
66
67 return config;
68 }
69 #endif
70