195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2023 Huawei Device Co., Ltd.
395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
495489c19Sopenharmony_ci * you may not use this file except in compliance with the License.
595489c19Sopenharmony_ci * You may obtain a copy of the License at
695489c19Sopenharmony_ci *
795489c19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
895489c19Sopenharmony_ci *
995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and
1395489c19Sopenharmony_ci * limitations under the License.
1495489c19Sopenharmony_ci */
1595489c19Sopenharmony_ci
1695489c19Sopenharmony_ci#ifndef BLUETOOTH_DIALOG_H
1795489c19Sopenharmony_ci#define BLUETOOTH_DIALOG_H
1895489c19Sopenharmony_ci
1995489c19Sopenharmony_ci#include <string>
2095489c19Sopenharmony_ci#include "safe_map.h"
2195489c19Sopenharmony_ci#include "singleton.h"
2295489c19Sopenharmony_ci
2395489c19Sopenharmony_ci#include "i_bluetooth_host.h"
2495489c19Sopenharmony_ci#include "bluetooth_errorcode.h"
2595489c19Sopenharmony_ci#include "bluetooth_log.h"
2695489c19Sopenharmony_ci#include "iremote_broker.h"
2795489c19Sopenharmony_ci#include "iservice_registry.h"
2895489c19Sopenharmony_ci#include "system_ability_definition.h"
2995489c19Sopenharmony_ci#include "system_ability_status_change_stub.h"
3095489c19Sopenharmony_ci#include <mutex>
3195489c19Sopenharmony_ci
3295489c19Sopenharmony_cinamespace OHOS {
3395489c19Sopenharmony_cinamespace Bluetooth {
3495489c19Sopenharmony_ciconstexpr const char *BLUETOOTH_HOST = "BluetoothHost";
3595489c19Sopenharmony_ciconst int32_t BEGIN_ID = 0;
3695489c19Sopenharmony_ci// It is recommended to ues one of the between bluetoothLoadedfunc and bleTurnOnFunc
3795489c19Sopenharmony_cistruct ProfileFunctions {
3895489c19Sopenharmony_ci    std::function<void(sptr<IRemoteObject>)> bluetoothLoadedfunc {};
3995489c19Sopenharmony_ci    std::function<void(sptr<IRemoteObject>)> bleTurnOnFunc {};
4095489c19Sopenharmony_ci    std::function<void(void)> bluetoothTurnOffFunc {};
4195489c19Sopenharmony_ci};
4295489c19Sopenharmony_cistruct ProfileIdProperty {
4395489c19Sopenharmony_ci    ProfileFunctions functions;
4495489c19Sopenharmony_ci    std::string objectName = "";
4595489c19Sopenharmony_ci};
4695489c19Sopenharmony_ciclass BluetoothProfileManager {
4795489c19Sopenharmony_cipublic:
4895489c19Sopenharmony_ci    BluetoothProfileManager();
4995489c19Sopenharmony_ci    ~BluetoothProfileManager();
5095489c19Sopenharmony_ci    /**
5195489c19Sopenharmony_ci     * @brief Get the Remote of the Profile
5295489c19Sopenharmony_ci     *
5395489c19Sopenharmony_ci     * @param objectName the objectName of profile
5495489c19Sopenharmony_ci     *
5595489c19Sopenharmony_ci     * @return Returns the Remote of the Profile.
5695489c19Sopenharmony_ci     */
5795489c19Sopenharmony_ci    sptr<IRemoteObject> GetProfileRemote(const std::string &objectName);
5895489c19Sopenharmony_ci    /**
5995489c19Sopenharmony_ci     * @brief register function for profile to get proxy when profile is init
6095489c19Sopenharmony_ci     *
6195489c19Sopenharmony_ci     * @param objectName the objectName of profile
6295489c19Sopenharmony_ci     * @param func the function for profile to register
6395489c19Sopenharmony_ci     *
6495489c19Sopenharmony_ci     * @return Returns the id of the Profile.
6595489c19Sopenharmony_ci     */
6695489c19Sopenharmony_ci    int32_t RegisterFunc(const std::string &objectName, std::function<void (sptr<IRemoteObject>)> func);
6795489c19Sopenharmony_ci    /**
6895489c19Sopenharmony_ci     * @brief register function for profile to get proxy when profile is init
6995489c19Sopenharmony_ci     *
7095489c19Sopenharmony_ci     * @param objectName the objectName of profile
7195489c19Sopenharmony_ci     * @param ProfileFunctions the function for profile to register
7295489c19Sopenharmony_ci     *
7395489c19Sopenharmony_ci     * @return Returns the id of the Profile.
7495489c19Sopenharmony_ci     */
7595489c19Sopenharmony_ci    int32_t RegisterFunc(const std::string &objectName, ProfileFunctions profileFunctions);
7695489c19Sopenharmony_ci    /**
7795489c19Sopenharmony_ci     * @brief Deregister function for profile, ensure that there is a deregister after register
7895489c19Sopenharmony_ci     *
7995489c19Sopenharmony_ci     * @param id the id of profile
8095489c19Sopenharmony_ci     */
8195489c19Sopenharmony_ci    void DeregisterFunc(int32_t id);
8295489c19Sopenharmony_ci    /**
8395489c19Sopenharmony_ci     * @brief Notify Bluetooth State Change
8495489c19Sopenharmony_ci     */
8595489c19Sopenharmony_ci    void NotifyBluetoothStateChange(int32_t transport, int32_t status);
8695489c19Sopenharmony_ci    /**
8795489c19Sopenharmony_ci     * @brief check bluetooth service is on or not
8895489c19Sopenharmony_ci     */
8995489c19Sopenharmony_ci    bool IsBluetoothServiceOn();
9095489c19Sopenharmony_ci
9195489c19Sopenharmony_ci    static BluetoothProfileManager &GetInstance();
9295489c19Sopenharmony_ci
9395489c19Sopenharmony_ciprivate:
9495489c19Sopenharmony_ci    class BluetoothSystemAbility : public SystemAbilityStatusChangeStub {
9595489c19Sopenharmony_ci        public:
9695489c19Sopenharmony_ci            void OnAddSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
9795489c19Sopenharmony_ci            void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string &deviceId) override;
9895489c19Sopenharmony_ci    };
9995489c19Sopenharmony_ci
10095489c19Sopenharmony_ci    void SubScribeBluetoothSystemAbility();
10195489c19Sopenharmony_ci    void UnSubScribeBluetoothSystemAbility();
10295489c19Sopenharmony_ci    void RunFuncWhenBluetoothServiceStarted();
10395489c19Sopenharmony_ci    sptr<IRemoteObject> GetHostRemote();
10495489c19Sopenharmony_ci    int32_t GetValidId();
10595489c19Sopenharmony_ci
10695489c19Sopenharmony_ci    SafeMap<std::string, sptr<IRemoteObject>> profileRemoteMap_;
10795489c19Sopenharmony_ci    SafeMap<int32_t, ProfileIdProperty> profileIdFuncMap_;
10895489c19Sopenharmony_ci    std::atomic_bool isBluetoothServiceOn_ = false;
10995489c19Sopenharmony_ci    std::atomic_bool isNeedCheckBluetoothServiceOn_ = true;
11095489c19Sopenharmony_ci    sptr<BluetoothSystemAbility> bluetoothSystemAbility_ = nullptr;
11195489c19Sopenharmony_ci    int32_t registerValidId_ = BEGIN_ID;
11295489c19Sopenharmony_ci    std::mutex idMutex_;
11395489c19Sopenharmony_ci    std::mutex getProfileRemoteMutex_;
11495489c19Sopenharmony_ci    std::mutex needCheckBluetoothServiceOnMutex_;
11595489c19Sopenharmony_ci};
11695489c19Sopenharmony_citemplate <typename T>
11795489c19Sopenharmony_cisptr<T> GetRemoteProxy(const std::string &objectName)
11895489c19Sopenharmony_ci{
11995489c19Sopenharmony_ci    return iface_cast<T>(BluetoothProfileManager::GetInstance().GetProfileRemote(objectName));
12095489c19Sopenharmony_ci};
12195489c19Sopenharmony_ci} // namespace bluetooth
12295489c19Sopenharmony_ci} // namespace OHOS
12395489c19Sopenharmony_ci#endif // BLUETOOTH_PROFILE_MANAGER_H