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 #ifndef DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H 17 #define DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H 18 #include <concurrent_map.h> 19 #include <condition_variable> 20 #include <functional> 21 #include <map> 22 #include <memory> 23 #include <mutex> 24 #include <set> 25 #include <tuple> 26 #include <vector> 27 28 #include "app_data_change_listener.h" 29 #include "app_device_change_listener.h" 30 #include "block_data.h" 31 #include "session.h" 32 #include "softbus_bus_center.h" 33 #include "softbus_client.h" 34 namespace OHOS { 35 namespace AppDistributedKv { 36 class SoftBusAdapter { 37 public: 38 SoftBusAdapter(); 39 ~SoftBusAdapter(); 40 static std::shared_ptr<SoftBusAdapter> GetInstance(); 41 42 // add DataChangeListener to watch data change; 43 Status StartWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 44 45 // stop DataChangeListener to watch data change; 46 Status StopWatchDataChange(const AppDataChangeListener *observer, const PipeInfo &pipeInfo); 47 48 // Send data to other device, function will be called back after sent to notify send result. 49 Status SendData(const PipeInfo &pipeInfo, const DeviceId &deviceId, const DataInfo &dataInfo, 50 uint32_t totalLength, const MessageInfo &info); 51 52 bool IsSameStartedOnPeer(const struct PipeInfo &pipeInfo, const struct DeviceId &peer); 53 54 void SetMessageTransFlag(const PipeInfo &pipeInfo, bool flag); 55 56 int CreateSessionServerAdapter(const std::string &sessionName); 57 58 int RemoveSessionServerAdapter(const std::string &sessionName) const; 59 60 void NotifyDataListeners(const uint8_t *data, int size, const std::string &deviceId, const PipeInfo &pipeInfo); 61 62 int32_t GetSessionStatus(int32_t connId); 63 64 void OnSessionOpen(int32_t connId, int32_t status); 65 66 std::string OnSessionClose(int32_t connId); 67 68 int32_t Broadcast(const PipeInfo &pipeInfo, uint16_t mask); 69 void OnBroadcast(const DeviceId &device, uint16_t mask); 70 int32_t ListenBroadcastMsg(const PipeInfo &pipeInfo, std::function<void(const std::string &, uint16_t)> listener); 71 72 uint32_t GetMtuSize(const DeviceId &deviceId); 73 std::shared_ptr<SoftBusClient> GetConnect(const std::string &deviceId); 74 75 class SofBusDeviceChangeListenerImpl : public AppDistributedKv::AppDeviceChangeListener { 76 void OnDeviceChanged(const AppDistributedKv::DeviceInfo &info, 77 const AppDistributedKv::DeviceChangeType &type) const override; 78 }; 79 private: 80 std::shared_ptr<BlockData<int32_t>> GetSemaphore(int32_t connId); 81 std::string DelConnect(int32_t connId); 82 void DelSessionStatus(int32_t connId); 83 void AfterStrategyUpdate(const std::string &deviceId); 84 static constexpr uint32_t WAIT_MAX_TIME = 10; 85 static std::shared_ptr<SoftBusAdapter> instance_; 86 ConcurrentMap<std::string, const AppDataChangeListener *> dataChangeListeners_{}; 87 std::mutex connMutex_{}; 88 std::map<std::string, std::shared_ptr<SoftBusClient>> connects_ {}; 89 bool flag_ = true; // only for br flag 90 ISessionListener sessionListener_{}; 91 std::mutex statusMutex_{}; 92 std::map<int32_t, std::shared_ptr<BlockData<int32_t>>> sessionsStatus_; 93 std::function<void(const std::string &, uint16_t)> onBroadcast_; 94 static SofBusDeviceChangeListenerImpl listener_; 95 }; 96 } // namespace AppDistributedKv 97 } // namespace OHOS 98 #endif /* DISTRIBUTEDDATAFWK_SRC_SOFTBUS_ADAPTER_H */ 99