• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 /**
17  * @addtogroup Bluetooth
18  * @{
19  *
20  * @brief The framework interface and callback function of map server are defined.
21  *
22  * @since 6
23  */
24 
25 /**
26  * @file bluetooth_map_mse.h
27  *
28  * @brief map server interface.
29  *
30  * @since 6
31  */
32 #ifndef BLUETOOTH_MAP_MSE_H
33 #define BLUETOOTH_MAP_MSE_H
34 
35 #include "bluetooth_def.h"
36 #include "bluetooth_types.h"
37 #include "bluetooth_remote_device.h"
38 
39 namespace OHOS {
40 namespace Bluetooth {
41 /**
42  * @brief Map server API callback function, including connection, disconnection and authorization.
43  *
44  * @since 6
45  */
46 class MapServerObserver {
47 public:
48     /**
49      * @brief A destructor used to delete the Map Server Observer instance.
50      *
51      * @since 6
52      */
53     virtual ~MapServerObserver() = default;
54 
55     /**
56      * @brief Callback function when the connection state changes.
57      *
58      * @param device  the remote bluetooth device.
59      * @param state   the connection status.
60      *     @c  BTConnectState::CONNECTING : the state is connecting.
61      *     @c  BTConnectState::CONNECTED : the state is connected.
62      *     @c  BTConnectState::DISCONNECTING : the state is disconnecting.
63      *     @c  BTConnectState::DISCONNECTED : the state is disconnected.
64      * @since 6
65      */
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state)66     virtual void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state) {};
67 
68     /**
69      * @brief Called to validate if a connection to the bluetooth device should be accepted.
70      *
71      * @param device  the connecting bluetooth device.
72      * @since 6
73      */
OnPermission(const BluetoothRemoteDevice & device)74     virtual void OnPermission(const BluetoothRemoteDevice &device) {};
75 };
76 
77 /**
78  * @brief Map server API.
79  *
80  * @since 6
81  */
82 class BLUETOOTH_API MapServer {
83 public:
84     /**
85      * @brief Get map server instance.
86      *
87      * @return Returns an instance of map server.
88      * @since 6
89      */
90     static MapServer *GetProfile();
91 
92     /**
93      * @brief Register the callback of map server.
94      *
95      * @param observer  Reference to the map server observer.
96      * @since 6
97      */
98     void RegisterObserver(MapServerObserver &observer);
99 
100     /**
101      * @brief Unregister the callback of map server.
102      *
103      * @param observer  Reference to the map server observer.
104      * @since 6
105      */
106     void DeregisterObserver(MapServerObserver &observer);
107 
108     /**
109      * @brief Get the connection status of the server.
110      *
111      * @return Returns connection state of map profile.
112      * @since 6
113      */
114     int GetState() const;
115 
116     /**
117      * @brief Disconnect bluetooth connection service.
118      *
119      * @param device  Reference to the remote bluetooth device.
120      * @return Returns true if the operation is successful;returns false if the operation fails.
121      * @since 6
122      */
123     bool Disconnect(const BluetoothRemoteDevice &device);
124 
125     /**
126      * @brief Get whether bluetooth device is connected.
127      *
128      * @param device  Reference to the remote bluetooth device.
129      * @return Returns true if the connection is successful;returns false if the connection fails.
130      * @since 6
131      */
132     bool IsConnected(const BluetoothRemoteDevice &device);
133 
134     /**
135      * @brief Get the list of connected bluetooth devices.
136      *
137      * @return Returns the bluetooth address list of the map profile.
138      * @since 6
139      */
140     std::vector<BluetoothRemoteDevice> GetConnectedDevices() const;
141 
142     /**
143      * @brief Get the device list through the connection status.
144      *
145      * @param states  Reference to the connection status.
146      *     @c BTConnectState::CONNECTING : the state is connecting.
147      *     @c BTConnectState::CONNECTED : the state is connected.
148      *     @c BTConnectState::DISCONNECTING : the state is disconnecting.
149      *     @c BTConnectState::DISCONNECTED : the state is disconnected.
150      * @return Returns the bluetooth address in the specified status.
151      * @since 6
152      */
153     std::vector<BluetoothRemoteDevice> GetDevicesByStates(std::vector<int> states) const;
154 
155     /**
156      * @brief Get the connection status of the specified device.
157      *
158      * @param  device Reference to the remote bluetooth device.
159      * @return Returns the connection status of the specified bluetooth address.
160      * @since 6
161      */
162     int GetConnectionState(const BluetoothRemoteDevice &device) const;
163 
164     /**
165      * @brief Set the connection policy of the specified device.
166      *
167      * @param device Reference to the remote bluetooth device.
168      * @param strategy Reference to the connection policy,
169      *     @c BTStrategyType::CONNECTION_UNKNOWN : the connection policy for unkown state.
170      *     @c BTStrategyType::CONNECTION_ALLOWED : the connection policy for allowed state.
171      *     @c BTStrategyType::CONNECTION_FORBIDDEN : the connection policy for forbidden state.
172      * @return Returns true if the operation is successful;returns false if the operation fails.
173      * @since 6
174      */
175     bool SetConnectionStrategy(const BluetoothRemoteDevice &device, int strategy);
176 
177     /**
178      * @brief Get the connection policy of the specified device.
179      *
180      * @param device Reference to the remote bluetooth device.
181      * @return Returns the connection police of the specified bluetooth address.
182      * @since 6
183      */
184     int GetConnectionStrategy(const BluetoothRemoteDevice &device) const;
185 
186     /**
187      * @brief Set whether to authorize the connection.
188      *
189      * @param device Reference to the remote bluetooth device.
190      * @param allow Reference to grant authorization.
191      *     @c true : accept connection.
192      *     @c false : reject connection.
193      * @param save Save authorization.
194      *     @c true : always accept.
195      *     @c false : always reject.
196      * @since 6
197      */
198     void GrantPermission(const BluetoothRemoteDevice &device, bool allow, bool save = false);
199 
200 private:
201     /**
202      * @brief A constructor used to create a Map Server instance.
203      *
204      * @since 6
205      */
206     MapServer();
207 
208     /**
209      * @brief A destructor used to delete the Map Server Observer instance.
210      *
211      * @since 6
212      */
213     ~MapServer();
214     BLUETOOTH_DISALLOW_COPY_AND_ASSIGN(MapServer);
215     BLUETOOTH_DECLARE_IMPL();
216 };
217 }  // namespace Bluetooth
218 }  // namespace OHOS
219 
220 #endif  // BLUETOOTH_MAP_MSE_H