• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "bluetooth_load_system_ability.h"
17 
18 #include "bluetooth_a2dp_snk.h"
19 #include "bluetooth_a2dp_src.h"
20 #include "bluetooth_avrcp_tg.h"
21 #include "bluetooth_avrcp_ct.h"
22 #include "bluetooth_ble_advertiser.h"
23 #include "bluetooth_ble_central_manager.h"
24 #include "bluetooth_def.h"
25 #include "bluetooth_gatt_client.h"
26 #include "bluetooth_gatt_server.h"
27 #include "bluetooth_hfp_ag.h"
28 #include "bluetooth_hfp_hf.h"
29 #include "bluetooth_hid_host.h"
30 #include "bluetooth_map_mce.h"
31 #include "bluetooth_map_mse.h"
32 #include "bluetooth_opp.h"
33 #include "bluetooth_pan.h"
34 #include "bluetooth_pbap_client.h"
35 #include "bluetooth_pbap_server.h"
36 #include "log.h"
37 #include "system_ability_definition.h"
38 
39 namespace OHOS {
40 class IRemoteObject;
41 }
42 namespace OHOS {
43 namespace Bluetooth {
44 
BluetootLoadSystemAbility()45 BluetootLoadSystemAbility::BluetootLoadSystemAbility()
46 {}
47 
~BluetootLoadSystemAbility()48 BluetootLoadSystemAbility::~BluetootLoadSystemAbility()
49 {}
50 
SubScribeBluetoothSystemAbility()51 void BluetootLoadSystemAbility::SubScribeBluetoothSystemAbility()
52 {
53     std::lock_guard<std::recursive_mutex> lk(mutex_);
54     if (subscribeBluetoothSystemAbilityState != STATE_NONE) {
55         return;
56     }
57     sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
58     if (samgrProxy == nullptr) {
59         HILOGE("[BLUETOOTH_LOAD_SYSTEM] failed to get samgrProxy");
60         return;
61     }
62     int32_t ret = samgrProxy->SubscribeSystemAbility(BLUETOOTH_HOST_SYS_ABILITY_ID, this);
63     if (ret != ERR_OK) {
64         HILOGE("[BLUETOOTH_LOAD_SYSTEM] subscribe systemAbilityId: bluetooth service failed!");
65         return;
66     }
67     subscribeBluetoothSystemAbilityState = STATE_SUBSCRIBING;
68 }
69 
GetInstance()70 BluetootLoadSystemAbility &BluetootLoadSystemAbility::GetInstance()
71 {
72     static BluetootLoadSystemAbility bluetoothLoadSystemAbility;
73     return bluetoothLoadSystemAbility;
74 }
75 
HasSubscribedBluetoothSystemAbility()76 bool BluetootLoadSystemAbility::HasSubscribedBluetoothSystemAbility()
77 {
78     std::lock_guard<std::recursive_mutex> lk(mutex_);
79     HILOGI("[BLUETOOTH_LOAD_SYSTEM] subscribedState=%{public}d", subscribeBluetoothSystemAbilityState);
80     if (subscribeBluetoothSystemAbilityState != STATE_SUBSCRIBED) {
81         return false;
82     }
83     return true;
84 }
85 
RegisterNotifyMsg(const uint32_t & profileId)86 void BluetootLoadSystemAbility::RegisterNotifyMsg(const uint32_t &profileId)
87 {
88     std::lock_guard<std::recursive_mutex> lk(mutex_);
89     profileIdList_.push_back(profileId);
90 }
91 
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)92 void BluetootLoadSystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
93 {
94     std::lock_guard<std::recursive_mutex> lk(mutex_);
95     HILOGI("[BLUETOOTH_LOAD_SYSTEM]OnAddSystemAbility systemAbilityId:%{public}d", systemAbilityId);
96     switch (systemAbilityId) {
97         case BLUETOOTH_HOST_SYS_ABILITY_ID: {
98             NotifyMsgToProfile(NOTIFY_MSG_INIT);
99             subscribeBluetoothSystemAbilityState = STATE_SUBSCRIBED;
100             break;
101         }
102         default:
103             HILOGE("[BLUETOOTH_LOAD_SYSTEM]unhandled sysabilityId:%{public}d", systemAbilityId);
104             break;
105     }
106     return;
107 }
108 
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)109 void BluetootLoadSystemAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId)
110 {
111     return;
112 }
113 
NotifyMsgToProfile(NOTIFY_PROFILE_MSG notifyMsg)114 void BluetootLoadSystemAbility::NotifyMsgToProfile(NOTIFY_PROFILE_MSG notifyMsg)
115 {
116     for (auto it = profileIdList_.begin(); it != profileIdList_.end(); ++it) {
117         NotifyProfile(notifyMsg, *it);
118     }
119 }
120 
NotifyProfile(NOTIFY_PROFILE_MSG notifyMsg,const uint32_t & profileId)121 void BluetootLoadSystemAbility::NotifyProfile(NOTIFY_PROFILE_MSG notifyMsg, const uint32_t &profileId)
122 {
123     std::lock_guard<std::recursive_mutex> lk(mutex_);
124     switch (profileId) {
125         case PROFILE_ID_A2DP_SRC:
126         case PROFILE_ID_A2DP_SINK:
127         case PROFILE_ID_HFP_AG:
128         case PROFILE_ID_HFP_HF:
129         case PROFILE_ID_AVRCP_TG:
130         case PROFILE_ID_AVRCP_CT:
131             NotifyAudioProfile(notifyMsg, profileId);
132             break;
133         case PROFILE_ID_MAP_MCE:
134         case PROFILE_ID_MAP_MSE:
135         case PROFILE_ID_PBAP_PCE:
136         case PROFILE_ID_PBAP_PSE:
137             NotifyMessageProfile(notifyMsg, profileId);
138             break;
139         case PROFILE_ID_HID_HOST_SERVER:
140         case PROFILE_ID_OPP_SERVER:
141         case PROFILE_ID_PAN_SERVER:
142             NotifyTransferProfile(notifyMsg, profileId);
143             break;
144         case PROFILE_ID_HOST:
145             NotifyHostInit(notifyMsg);
146             break;
147         default:
148             break;
149     }
150 }
151 
NotifyHostInit(NOTIFY_PROFILE_MSG notifyMsg)152 void BluetootLoadSystemAbility::NotifyHostInit(NOTIFY_PROFILE_MSG notifyMsg)
153 {
154     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
155     if (host == nullptr) {
156         return;
157     }
158     if (notifyMsg == NOTIFY_MSG_INIT) {
159         host->Init();
160         return;
161     }
162 }
163 
NotifyA2dpSrcProfile(NOTIFY_PROFILE_MSG notifyMsg)164 void BluetootLoadSystemAbility::NotifyA2dpSrcProfile(NOTIFY_PROFILE_MSG notifyMsg)
165 {
166     A2dpSource *profile = A2dpSource::GetProfile();
167     if (profile == nullptr) {
168         return;
169     }
170     if (notifyMsg == NOTIFY_MSG_INIT) {
171         profile->Init();
172         return;
173     }
174 }
175 
NotifyA2dpSinkProfile(NOTIFY_PROFILE_MSG notifyMsg)176 void BluetootLoadSystemAbility::NotifyA2dpSinkProfile(NOTIFY_PROFILE_MSG notifyMsg)
177 {
178     A2dpSink *profile = A2dpSink::GetProfile();
179     if (profile == nullptr) {
180         return;
181     }
182     if (notifyMsg == NOTIFY_MSG_INIT) {
183         profile->Init();
184         return;
185     }
186 }
187 
NotifyHfpAgProfile(NOTIFY_PROFILE_MSG notifyMsg)188 void BluetootLoadSystemAbility::NotifyHfpAgProfile(NOTIFY_PROFILE_MSG notifyMsg)
189 {
190     HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
191     if (profile == nullptr) {
192         return;
193     }
194     if (notifyMsg == NOTIFY_MSG_INIT) {
195         profile->Init();
196         return;
197     }
198 }
199 
NotifyHfpHfProfile(NOTIFY_PROFILE_MSG notifyMsg)200 void BluetootLoadSystemAbility::NotifyHfpHfProfile(NOTIFY_PROFILE_MSG notifyMsg)
201 {
202     HandsFreeUnit *profile = HandsFreeUnit::GetProfile();
203     if (profile == nullptr) {
204         return;
205     }
206     if (notifyMsg == NOTIFY_MSG_INIT) {
207         profile->Init();
208         return;
209     }
210 }
211 
NotifyAvrcpTgProfile(NOTIFY_PROFILE_MSG notifyMsg)212 void BluetootLoadSystemAbility::NotifyAvrcpTgProfile(NOTIFY_PROFILE_MSG notifyMsg)
213 {
214     AvrcpTarget *profile = AvrcpTarget::GetProfile();
215     if (profile == nullptr) {
216         return;
217     }
218     if (notifyMsg == NOTIFY_MSG_INIT) {
219         profile->Init();
220         return;
221     }
222 }
223 
NotifyAvrcpCtProfile(NOTIFY_PROFILE_MSG notifyMsg)224 void BluetootLoadSystemAbility::NotifyAvrcpCtProfile(NOTIFY_PROFILE_MSG notifyMsg)
225 {
226     AvrcpController *profile = AvrcpController::GetProfile();
227     if (profile == nullptr) {
228         return;
229     }
230     if (notifyMsg == NOTIFY_MSG_INIT) {
231         profile->Init();
232         return;
233     }
234 }
235 
NotifyAudioProfile(NOTIFY_PROFILE_MSG notifyMsg,const uint32_t & profileId)236 void BluetootLoadSystemAbility::NotifyAudioProfile(NOTIFY_PROFILE_MSG notifyMsg, const uint32_t &profileId)
237 {
238     switch (profileId) {
239         case PROFILE_ID_A2DP_SRC:
240             NotifyA2dpSrcProfile(notifyMsg);
241             break;
242         case PROFILE_ID_A2DP_SINK:
243             NotifyA2dpSinkProfile(notifyMsg);
244             break;
245         case PROFILE_ID_HFP_AG:
246             NotifyHfpAgProfile(notifyMsg);
247             break;
248         case PROFILE_ID_HFP_HF:
249             NotifyHfpHfProfile(notifyMsg);
250             break;
251         case PROFILE_ID_AVRCP_TG:
252             NotifyAvrcpTgProfile(notifyMsg);
253             break;
254         case PROFILE_ID_AVRCP_CT:
255             NotifyAvrcpCtProfile(notifyMsg);
256             break;
257         default:
258             break;
259     }
260 }
261 
NotifyMapClientProfile(NOTIFY_PROFILE_MSG notifyMsg)262 void BluetootLoadSystemAbility::NotifyMapClientProfile(NOTIFY_PROFILE_MSG notifyMsg)
263 {
264     MapClient *profile = MapClient::GetProfile();
265     if (profile == nullptr) {
266         return;
267     }
268     if (notifyMsg == NOTIFY_MSG_INIT) {
269         profile->Init();
270         return;
271     }
272 }
273 
NotifyMapServerProfile(NOTIFY_PROFILE_MSG notifyMsg)274 void BluetootLoadSystemAbility::NotifyMapServerProfile(NOTIFY_PROFILE_MSG notifyMsg)
275 {
276     MapServer *profile = MapServer::GetProfile();
277     if (profile == nullptr) {
278         return;
279     }
280     if (notifyMsg == NOTIFY_MSG_INIT) {
281         profile->Init();
282         return;
283     }
284 }
285 
NotifyPbapClientProfile(NOTIFY_PROFILE_MSG notifyMsg)286 void BluetootLoadSystemAbility::NotifyPbapClientProfile(NOTIFY_PROFILE_MSG notifyMsg)
287 {
288     PbapClient *profile = PbapClient::GetProfile();
289     if (profile == nullptr) {
290         return;
291     }
292     if (notifyMsg == NOTIFY_MSG_INIT) {
293         profile->Init();
294         return;
295     }
296 }
297 
NotifyPbapServerProfile(NOTIFY_PROFILE_MSG notifyMsg)298 void BluetootLoadSystemAbility::NotifyPbapServerProfile(NOTIFY_PROFILE_MSG notifyMsg)
299 {
300     PbapServer *profile = PbapServer::GetProfile();
301     if (profile == nullptr) {
302         return;
303     }
304     if (notifyMsg == NOTIFY_MSG_INIT) {
305         profile->Init();
306         return;
307     }
308 }
309 
NotifyMessageProfile(NOTIFY_PROFILE_MSG notifyMsg,const uint32_t & profileId)310 void BluetootLoadSystemAbility::NotifyMessageProfile(NOTIFY_PROFILE_MSG notifyMsg, const uint32_t &profileId)
311 {
312     switch (profileId) {
313         case PROFILE_ID_MAP_MCE:
314             NotifyMapClientProfile(notifyMsg);
315             break;
316         case PROFILE_ID_MAP_MSE:
317             NotifyMapServerProfile(notifyMsg);
318             break;
319         case PROFILE_ID_PBAP_PCE:
320             NotifyPbapClientProfile(notifyMsg);
321             break;
322         case PROFILE_ID_PBAP_PSE:
323             NotifyPbapServerProfile(notifyMsg);
324             break;
325         default:
326             break;
327     }
328 }
329 
NotifyOppProfile(NOTIFY_PROFILE_MSG notifyMsg)330 void BluetootLoadSystemAbility::NotifyOppProfile(NOTIFY_PROFILE_MSG notifyMsg)
331 {
332     Opp *profile = Opp::GetProfile();
333     if (profile == nullptr) {
334         return;
335     }
336     if (notifyMsg == NOTIFY_MSG_INIT) {
337         profile->Init();
338         return;
339     }
340 }
341 
NotifyHidHostProfile(NOTIFY_PROFILE_MSG notifyMsg)342 void BluetootLoadSystemAbility::NotifyHidHostProfile(NOTIFY_PROFILE_MSG notifyMsg)
343 {
344     HidHost *profile = HidHost::GetProfile();
345     if (profile == nullptr) {
346         return;
347     }
348     if (notifyMsg == NOTIFY_MSG_INIT) {
349         profile->Init();
350         return;
351     }
352 }
353 
NotifyPanProfile(NOTIFY_PROFILE_MSG notifyMsg)354 void BluetootLoadSystemAbility::NotifyPanProfile(NOTIFY_PROFILE_MSG notifyMsg)
355 {
356     Pan *profile = Pan::GetProfile();
357     if (profile == nullptr) {
358         return;
359     }
360     if (notifyMsg == NOTIFY_MSG_INIT) {
361         profile->Init();
362         return;
363     }
364 }
365 
NotifyTransferProfile(NOTIFY_PROFILE_MSG notifyMsg,const uint32_t & profileId)366 void BluetootLoadSystemAbility::NotifyTransferProfile(NOTIFY_PROFILE_MSG notifyMsg, const uint32_t &profileId)
367 {
368     switch (profileId) {
369         case PROFILE_ID_OPP_SERVER:
370             NotifyOppProfile(notifyMsg);
371             break;
372         case PROFILE_ID_HID_HOST_SERVER:
373             NotifyHidHostProfile(notifyMsg);
374             break;
375         case PROFILE_ID_PAN_SERVER:
376             NotifyPanProfile(notifyMsg);
377             break;
378         default:
379             break;
380     }
381 }
382 
383 }  // namespace Bluetooth
384 }  // namespace OHOS