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 MAP_MSE_SERVICE_H 17 #define MAP_MSE_SERVICE_H 18 19 #include <cstdint> 20 #include <iostream> 21 #include <list> 22 #include <mutex> 23 #include <string> 24 #include <unordered_map> 25 #include <vector> 26 #include "adapter_config.h" 27 #include "base_observer_list.h" 28 #include "class_creator.h" 29 #include "context.h" 30 #include "interface_profile_map_mse.h" 31 #include "map_mse_instance.h" 32 #include "power_manager.h" 33 #include "profile_config.h" 34 #include "profile_service_manager.h" 35 #include "raw_address.h" 36 37 namespace OHOS { 38 namespace bluetooth { 39 class MapMseService : public IProfileMapMse, public utility::Context { 40 public: 41 enum { INIT_INSTANCE, DISCONNECT, START_INSTANCE, START_UP_COMPLETE, SHUT_DOWN, SHUT_DOWN_COMPLETE }; 42 /** 43 * @brief Construct a new Map Mse Service object 44 * 45 */ 46 explicit MapMseService(); 47 /** 48 * @brief Destroy the Map Mse Service object 49 * 50 */ 51 virtual ~MapMseService(); 52 utility::Context *GetContext() override; 53 void Enable(void) override; 54 void Disable(void) override; 55 int Connect(const RawAddress &device) override; 56 std::list<RawAddress> GetConnectDevices() override; 57 int GetConnectState(void) override; 58 int GetMaxConnectNum(void) override; 59 int GetState(void) override; 60 int Disconnect(const RawAddress &device) override; 61 bool IsConnected(const RawAddress &device) override; 62 std::vector<RawAddress> GetDevicesByStates(std::vector<int> states) override; 63 int GetConnectionState(const RawAddress &device) override; 64 bool SetConnectionStrategy(const RawAddress &device, int strategy) override; 65 int GetConnectionStrategy(const RawAddress &device) override; 66 void GrantPermission(const RawAddress &device, bool allow, bool save = false) override; 67 void RegisterObserver(IMapMseObserver &mapMseObserver) override; 68 void DeregisterObserver(IMapMseObserver &mapMseObserver) override; 69 70 private: 71 // The initial value of the Map instance Id 72 static const int MAS_ID_SMS_MMS = 0; 73 // The initial value of the GoepL2capPsm 74 static const uint8_t LPSM_SIZE = 12; 75 static const uint16_t GOEP_L2CAP_PSM_VALUE[LPSM_SIZE]; 76 // The maximum default number of connection devices 77 static const int MAP_MSE_MAX_DEFAULT_CONNECTIONS_NUMR = 6; 78 std::vector<MapAccountItem> GetAccountItems(void) const; 79 void PostMessage(int what); 80 void ProcessMessage(const utility::Message &msg); 81 void InitMasInstances(void); 82 void InstancesStarting(void); 83 void ServiceStartUpComplete(bool whether); 84 void ServiceShutDown(void); 85 void DisableComplete(void); 86 void ServiceShutDownComplete(void); 87 void DisConnectInternal(const utility::Message &msg) const; 88 void RegisterObserver(MapMseInstance &masInstance, const int masId); 89 void DeregisterObserver(MapMseInstance &masInstance, const int masId); 90 void GrantPermissionNative(const RawAddress &device, bool allow, bool save); 91 void NotifyConnectionState(const RawAddress &remoteAddr, const int masInstanceId, BTConnectState state); 92 void ProcessGrantPermission(const RawAddress &device, bool allow, bool save); 93 void GrantPermissionTimer(const RawAddress &device, bool allow, bool save); 94 void RejectConnection(std::string &addr, const int masInstanceId); 95 void ProcessReject(std::string &addr, const int masInstanceId); 96 std::unordered_map<int, std::unique_ptr<MapMseInstance>> masInstanceMap_ {}; 97 std::unordered_map<std::string, int> incomingConnectMap_ {}; 98 std::recursive_mutex mseMapMutex_ {}; 99 bool shutDownEnable_ = false; 100 std::unordered_map<std::string, std::unique_ptr<utility::Timer>> grantTimer_ {}; 101 std::unordered_map<std::string, std::unique_ptr<utility::Timer>> rejectTimer_ {}; 102 std::unique_ptr<MapMseMnscli> mnsClient_ = nullptr; 103 class MnsCallback : MnsObserver { 104 public: 105 /** 106 * @brief Construct a new ObServer object 107 * 108 * @param mseService Reference to the Mse Service 109 */ 110 explicit MnsCallback(MapMseService &mseService); 111 112 private: 113 /** 114 * @brief Called OnDisconnected 115 * 116 * @param client ObexClient 117 */ 118 void OnDisconnected() override; 119 // Reference to the Mse Service 120 MapMseService &mseService_; 121 BT_DISALLOW_COPY_AND_ASSIGN(MnsCallback); 122 }; 123 std::unique_ptr<MnsCallback> mnsObserver_ = nullptr; 124 class Observer : public MapObserver { 125 public: 126 /** 127 * @brief Construct a new ObServer object 128 * 129 * @param mseService Reference to the Mse Service 130 */ 131 explicit Observer(MapMseService &mseService); 132 133 private: 134 // The function is called when MCE connect is incoming 135 void OnIncomingConnect(const RawAddress &remoteAddr, const int masInstanceId) override; 136 137 // The function is called when MCE transport connect is successful 138 void OnTransportConnected(const RawAddress &remoteAddr, const int masInstanceId) override; 139 140 // The function is called when MSE disconnect is incoming 141 void OnTransportDisconnected(const RawAddress &remoteAddr, const int masInstanceId) override; 142 143 // The function is called when MSE busy or not busy 144 void OnBusy(const RawAddress &remoteAddr, bool isBusy) override; 145 146 // The function is called when Connection State Changed 147 void OnConnectionStateChanged( 148 const RawAddress &remoteAddr, const int masInstanceId, BTConnectState state) override; 149 150 // The function is called when MCE connect is incoming timeout 151 virtual void OnIncomingTimeout(const RawAddress &remoteAddr, const int masInstanceId) override; 152 153 // The function is called when MCE connect is incoming timeout 154 virtual void OnRejectConnection(const RawAddress &remoteAddr, const int masInstanceId) override; 155 156 // Reference to the Mse Service 157 MapMseService &mseService_; 158 BT_DISALLOW_COPY_AND_ASSIGN(Observer); 159 }; 160 std::unordered_map<int, std::unique_ptr<Observer>> observerMap_ {}; 161 BaseObserverList<IMapMseObserver> mapMseObservers_ {}; 162 BT_DISALLOW_COPY_AND_ASSIGN(MapMseService); 163 }; 164 } // namespace bluetooth 165 } // namespace OHOS 166 167 #endif // MAP_MSE_SERVICE_H 168