1 /******************************************************************************
2  *
3  *  Copyright 2009-2012 Broadcom Corporation
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_bte_conf"
20 
21 #include <bluetooth/log.h>
22 
23 #include <cstdint>
24 #include <cstdio>
25 #include <memory>
26 
27 #include "bta/include/bta_api.h"
28 #include "osi/include/compat.h"  // strlcpy
29 #include "osi/include/config.h"
30 #include "stack/include/hcidefs.h"
31 #include "stack/include/sdpdefs.h"
32 
33 using namespace bluetooth;
34 
35 // Parses the specified Device ID configuration file and registers the
36 // Device ID records with SDP.
bte_load_did_conf(const char * p_path)37 void bte_load_did_conf(const char* p_path) {
38   log::assert_that(p_path != NULL, "assert failed: p_path != NULL");
39 
40   std::unique_ptr<config_t> config = config_new(p_path);
41   if (!config) {
42     log::error("unable to load DID config '{}'.", p_path);
43     return;
44   }
45 
46   for (int i = 1; i <= BTA_DI_NUM_MAX; ++i) {
47     char section_name[16] = {0};
48     snprintf(section_name, sizeof(section_name), "DID%d", i);
49 
50     if (!config_has_section(*config, section_name)) {
51       log::info("no section named {}.", section_name);
52       break;
53     }
54 
55     tSDP_DI_RECORD record;
56     record.vendor =
57         config_get_int(*config, section_name, "vendorId", LMP_COMPID_GOOGLE);
58     record.vendor_id_source = config_get_int(
59         *config, section_name, "vendorIdSource", DI_VENDOR_ID_SOURCE_BTSIG);
60     record.product = config_get_int(*config, section_name, "productId", 0);
61     record.version = config_get_int(*config, section_name, "version", 0);
62     record.primary_record =
63         config_get_bool(*config, section_name, "primaryRecord", false);
64     std::string empty = "";
65     strlcpy(
66         record.client_executable_url,
67         config_get_string(*config, section_name, "clientExecutableURL", &empty)
68             ->c_str(),
69         sizeof(record.client_executable_url));
70     strlcpy(
71         record.service_description,
72         config_get_string(*config, section_name, "serviceDescription", &empty)
73             ->c_str(),
74         sizeof(record.service_description));
75     strlcpy(record.documentation_url,
76             config_get_string(*config, section_name, "documentationURL", &empty)
77                 ->c_str(),
78             sizeof(record.documentation_url));
79 
80     if (record.vendor_id_source != DI_VENDOR_ID_SOURCE_BTSIG &&
81         record.vendor_id_source != DI_VENDOR_ID_SOURCE_USBIF) {
82       log::error("invalid vendor id source {}; ignoring DID record {}.",
83                  record.vendor_id_source, i);
84       continue;
85     }
86 
87     log::info("Device ID record {} : {}", i,
88               record.primary_record ? "primary" : "not primary");
89     log::info("vendorId            = {:04x}", record.vendor);
90     log::info("vendorIdSource      = {:04x}", record.vendor_id_source);
91     log::info("product             = {:04x}", record.product);
92     log::info("version             = {:04x}", record.version);
93     log::info("clientExecutableURL = {}", record.client_executable_url);
94     log::info("serviceDescription  = {}", record.service_description);
95     log::info("documentationURL    = {}", record.documentation_url);
96 
97     uint32_t record_handle;
98     tBTA_STATUS status = BTA_DmSetLocalDiRecord(&record, &record_handle);
99     if (status != BTA_SUCCESS) {
100       log::error("unable to set device ID record {}: error {}.", i, status);
101     }
102   }
103 }
104