• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "chre/core/settings.h"
18 
19 #include <cstddef>
20 
21 #include "chre/core/event_loop_manager.h"
22 #include "chre/platform/log.h"
23 #include "chre/util/macros.h"
24 #include "chre/util/nested_data_ptr.h"
25 #include "chre/util/system/event_callbacks.h"
26 
27 #include "chre_api/chre/user_settings.h"
28 
29 namespace chre {
30 
31 namespace {
32 
33 /**
34  * @param setting The setting to get the index for.
35  * @param index A non-null pointer to store the index.
36  *
37  * @return false if the setting was invalid.
38  */
getIndexForSetting(Setting setting,size_t * index)39 bool getIndexForSetting(Setting setting, size_t *index) {
40   if (setting < Setting::SETTING_MAX) {
41     *index = static_cast<size_t>(setting);
42     return true;
43   }
44 
45   return false;
46 }
47 
sendSettingChangedNotification(Setting setting,bool enabled)48 void sendSettingChangedNotification(Setting setting, bool enabled) {
49   auto *eventData = memoryAlloc<struct chreUserSettingChangedEvent>();
50   auto settingAsInt = static_cast<uint8_t>(setting);
51   uint16_t eventType = CHRE_EVENT_SETTING_CHANGED_FIRST_EVENT + settingAsInt;
52 
53   if (eventData != nullptr) {
54     eventData->setting = settingAsInt;
55     eventData->settingState = enabled ? CHRE_USER_SETTING_STATE_ENABLED
56                                       : CHRE_USER_SETTING_STATE_DISABLED;
57 
58     EventLoopManagerSingleton::get()->getEventLoop().postEventOrDie(
59         eventType, eventData, freeEventDataCallback, kBroadcastInstanceId);
60   } else {
61     LOG_OOM();
62   }
63 }
64 
65 }  // anonymous namespace
66 
SettingManager()67 SettingManager::SettingManager() {
68   // Default most settings to true until the host tells us otherwise so
69   // nanoapps can begin work during boot which will get canceled if the user has
70   // disabled the feature.
71   for (size_t i = 0; i < ARRAY_SIZE(mSettingStateList); ++i) {
72     mSettingStateList[i] = true;
73   }
74 
75   // Airplane mode should be disabled since it being enabled causes API usage
76   // restrictions.
77   auto airplaneIndex = static_cast<uint8_t>(Setting::AIRPLANE_MODE);
78   mSettingStateList[airplaneIndex] = false;
79 }
80 
postSettingChange(Setting setting,bool enabled)81 void SettingManager::postSettingChange(Setting setting, bool enabled) {
82   LOGD("Posting setting change: setting type %" PRIu8 " enabled %d",
83        static_cast<uint8_t>(setting), enabled);
84 
85   EventLoopManagerSingleton::get()->deferCallback(
86       SystemCallbackType::SettingChangeEvent, NestedDataPtr<Setting>(setting),
87       settingChangedCallback, NestedDataPtr<bool>(enabled));
88 }
89 
getSettingEnabled(Setting setting)90 bool SettingManager::getSettingEnabled(Setting setting) {
91   size_t index;
92   if (getIndexForSetting(setting, &index)) {
93     return mSettingStateList[index];
94   }
95 
96   LOGE("Unknown setting %" PRIu8, static_cast<uint8_t>(setting));
97   return false;
98 }
99 
getSettingStateAsInt8(uint8_t setting)100 int8_t SettingManager::getSettingStateAsInt8(uint8_t setting) {
101   int8_t state = CHRE_USER_SETTING_STATE_UNKNOWN;
102   if (setting < static_cast<uint8_t>(Setting::SETTING_MAX)) {
103     auto settingEnum = static_cast<Setting>(setting);
104     state = static_cast<int8_t>(getSettingEnabled(settingEnum));
105   }
106   return state;
107 }
108 
logStateToBuffer(DebugDumpWrapper & debugDump)109 void SettingManager::logStateToBuffer(DebugDumpWrapper &debugDump) {
110   debugDump.print("\nSettings:");
111   debugDump.print("\n Location %s", getSettingEnabledString(Setting::LOCATION));
112   debugDump.print("\n WiFi available %s",
113                   getSettingEnabledString(Setting::WIFI_AVAILABLE));
114   debugDump.print("\n Airplane mode %s",
115                   getSettingEnabledString(Setting::AIRPLANE_MODE));
116   debugDump.print("\n Microphone Access %s",
117                   getSettingEnabledString(Setting::MICROPHONE));
118   debugDump.print("\n BLE available %s",
119                   getSettingEnabledString(Setting::BLE_AVAILABLE));
120 }
121 
settingChangedCallback(uint16_t,void * data,void * extraData)122 void SettingManager::settingChangedCallback(uint16_t /* type */, void *data,
123                                             void *extraData) {
124   Setting setting = NestedDataPtr<Setting>(data);
125   bool settingEnabled = NestedDataPtr<bool>(extraData);
126 
127   EventLoopManagerSingleton::get()->getSettingManager().setSettingState(
128       setting, settingEnabled);
129 
130   LOGD("Setting changed callback called for setting %u enabled %d",
131        static_cast<uint8_t>(setting), settingEnabled);
132 
133 #ifdef CHRE_GNSS_SUPPORT_ENABLED
134   EventLoopManagerSingleton::get()->getGnssManager().onSettingChanged(
135       setting, settingEnabled);
136 #endif  // CHRE_GNSS_SUPPORT_ENABLED
137 
138 #ifdef CHRE_AUDIO_SUPPORT_ENABLED
139   EventLoopManagerSingleton::get()->getAudioRequestManager().onSettingChanged(
140       setting, settingEnabled);
141 #endif  // CHRE_AUDIO_SUPPORT_ENABLED
142 
143 #ifdef CHRE_BLE_SUPPORT_ENABLED
144   EventLoopManagerSingleton::get()->getBleRequestManager().onSettingChanged(
145       setting, settingEnabled);
146 #endif  // CHRE_BLE_SUPPORT_ENABLED
147 
148 #ifdef CHRE_WIFI_SUPPORT_ENABLED
149   EventLoopManagerSingleton::get()->getWifiRequestManager().onSettingChanged(
150       setting, settingEnabled);
151 #endif  // CHRE_WIFI_SUPPORT_ENABLED
152 
153   sendSettingChangedNotification(setting, settingEnabled);
154 }
155 
setSettingState(Setting setting,bool enabled)156 void SettingManager::setSettingState(Setting setting, bool enabled) {
157   size_t index;
158   if (!getIndexForSetting(setting, &index)) {
159     LOGE("Unknown setting %" PRId8, static_cast<int8_t>(setting));
160   } else {
161     mSettingStateList[index] = enabled;
162   }
163 }
164 
getSettingEnabledString(Setting setting)165 const char *SettingManager::getSettingEnabledString(Setting setting) {
166   if (getSettingEnabled(setting)) {
167     return "enabled";
168   } else {
169     return "disabled";
170   }
171 }
172 
173 }  // namespace chre
174