1 /*
2  * Copyright (C) 2021-2022 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 #ifndef GENERIC_ACCESS_SERVICE_H
17 #define GENERIC_ACCESS_SERVICE_H
18 
19 #include <cstdint>
20 #include <memory>
21 #include <string>
22 #include "../gatt_server_service.h"
23 #include "base_def.h"
24 #include "dispatcher.h"
25 #include "gatt_data.h"
26 
27 /*
28  * @brief The bluetooth system.
29  */
30 namespace OHOS {
31 namespace bluetooth {
32 class GenericAccessService {
33 public:
34     /**
35      * @brief Destructor.
36      */
37     ~GenericAccessService();
38 
39     /**
40      * @brief Device GenericAccess Service.
41      *
42      */
43     GenericAccessService(GattServerService &service, utility::Dispatcher &dispatcher);
44 
45     int RegisterService();
46     void DeregisterService();
47 
48     BT_DISALLOW_COPY_AND_ASSIGN(GenericAccessService);
49 
50 private:
51     static const uint16_t GATT_UUID_GAP_SERVICE;
52     static const uint16_t GATT_UUID_GAP_DEVICE_NAME;
53     static const uint16_t GATT_UUID_GAP_APPEARANCE;
54     static const uint16_t GATT_UUID_GAP_CENTRAL_ADDR_RESOLUTION;
55     static const std::string DEFAULT_DEVICE_NAME;
56     static const uint16_t DEFAULT_APPEARANCE;
57     static const size_t APPEARANCE_DATE_LENGTH;
58     static const size_t CENTRAL_ADDR_RESOLUTION_DATE_LENGTH;
59 
60     class GattServerCallbackImpl;
61     class AdapterBleObserverImpl;
62     class AdapterClassicObserverImpl;
63 
64     bool bleDiscoverable_ = {false};
65     bool classicDiscoverable_ = {false};
66     uint8_t central_addr_resolution_ = {0};
67     std::unique_ptr<Service> instance_ = {nullptr};
68     std::shared_ptr<GattServerCallbackImpl> serviceCallback_;
69     std::unique_ptr<AdapterBleObserverImpl> adapterBleObserver_;
70     std::unique_ptr<AdapterClassicObserverImpl> adapterClassicObserver_;
71     GattServerService &serverService_;
72     utility::Dispatcher &dispatcher_;
73     int appId_ = -1;
74 
75     std::unique_ptr<Service> BuildService();
76     static std::string GetDeviceName();
77     std::unique_ptr<uint8_t[]> GetAppearance() const;
78     void OnDeviceNameChanged(std::string &deviceName) const;
79     void OnAdvertisingStateChanged(int state);
80     void OnScanModeChanged(int mode);
81 };
82 }  // namespace bluetooth
83 }  // namespace OHOS
84 
85 #endif  // GENERIC_ACCESS_SERVICE_H