1/******************************************************************************
2 *
3 *  Copyright (C) 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_stack_config"
20
21#include "stack_config.h"
22
23#include <base/logging.h>
24
25#include "osi/include/future.h"
26#include "osi/include/log.h"
27
28const char* TRACE_CONFIG_ENABLED_KEY = "TraceConf";
29const char* PTS_SECURE_ONLY_MODE = "PTS_SecurePairOnly";
30const char* PTS_LE_CONN_UPDATED_DISABLED = "PTS_DisableConnUpdates";
31const char* PTS_DISABLE_SDP_LE_PAIR = "PTS_DisableSDPOnLEPair";
32const char* PTS_SMP_PAIRING_OPTIONS_KEY = "PTS_SmpOptions";
33const char* PTS_SMP_FAILURE_CASE_KEY = "PTS_SmpFailureCase";
34
35static config_t* config;
36
37// Module lifecycle functions
38
39static future_t* init() {
40// TODO(armansito): Find a better way than searching by a hardcoded path.
41#if defined(OS_GENERIC)
42  const char* path = "bt_stack.conf";
43#else  // !defined(OS_GENERIC)
44  const char* path = "/etc/bluetooth/bt_stack.conf";
45#endif  // defined(OS_GENERIC)
46  CHECK(path != NULL);
47
48  LOG_INFO(LOG_TAG, "%s attempt to load stack conf from %s", __func__, path);
49
50  config = config_new(path);
51  if (!config) {
52    LOG_INFO(LOG_TAG, "%s file >%s< not found", __func__, path);
53    config = config_new_empty();
54  }
55
56  return future_new_immediate(FUTURE_SUCCESS);
57}
58
59static future_t* clean_up() {
60  config_free(config);
61  config = NULL;
62  return future_new_immediate(FUTURE_SUCCESS);
63}
64
65EXPORT_SYMBOL extern const module_t stack_config_module = {
66    .name = STACK_CONFIG_MODULE,
67    .init = init,
68    .start_up = NULL,
69    .shut_down = NULL,
70    .clean_up = clean_up,
71    .dependencies = {NULL}};
72
73// Interface functions
74static bool get_trace_config_enabled(void) {
75  return config_get_bool(config, CONFIG_DEFAULT_SECTION,
76                         TRACE_CONFIG_ENABLED_KEY, false);
77}
78
79static bool get_pts_secure_only_mode(void) {
80  return config_get_bool(config, CONFIG_DEFAULT_SECTION, PTS_SECURE_ONLY_MODE,
81                         false);
82}
83
84static bool get_pts_conn_updates_disabled(void) {
85  return config_get_bool(config, CONFIG_DEFAULT_SECTION,
86                         PTS_LE_CONN_UPDATED_DISABLED, false);
87}
88
89static bool get_pts_crosskey_sdp_disable(void) {
90  return config_get_bool(config, CONFIG_DEFAULT_SECTION,
91                         PTS_DISABLE_SDP_LE_PAIR, false);
92}
93
94static const char* get_pts_smp_options(void) {
95  return config_get_string(config, CONFIG_DEFAULT_SECTION,
96                           PTS_SMP_PAIRING_OPTIONS_KEY, NULL);
97}
98
99static int get_pts_smp_failure_case(void) {
100  return config_get_int(config, CONFIG_DEFAULT_SECTION,
101                        PTS_SMP_FAILURE_CASE_KEY, 0);
102}
103
104static config_t* get_all(void) { return config; }
105
106const stack_config_t interface = {get_trace_config_enabled,
107                                  get_pts_secure_only_mode,
108                                  get_pts_conn_updates_disabled,
109                                  get_pts_crosskey_sdp_disable,
110                                  get_pts_smp_options,
111                                  get_pts_smp_failure_case,
112                                  get_all};
113
114const stack_config_t* stack_config_get_interface(void) { return &interface; }
115