1 /* 2 * Copyright (c) 2023 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 OHOS_FILEMGMT_SOFTBUS_ADAPTER_H 17 #define OHOS_FILEMGMT_SOFTBUS_ADAPTER_H 18 19 #include <memory> 20 #include <shared_mutex> 21 #include <string> 22 #include <unordered_map> 23 #include <vector> 24 25 #include "nocopyable.h" 26 #include "session.h" 27 28 #include "i_softbus_listener.h" 29 30 namespace OHOS::FileManagement::CloudSync { 31 class SoftbusAdapter : public NoCopyable { 32 public: 33 static SoftbusAdapter &GetInstance(); 34 35 int32_t CreateSessionServer(const char *packageName, const char *sessionName); 36 int32_t RemoveSessionServer(const char *packageName, const char *sessionName); 37 void SetFileReceiveListener(const char *packageName, const char *sessionName); 38 39 int OpenSession(const char *sessionName, 40 const char *peerDeviceId, 41 const char *groupId, 42 const SessionAttribute *attr); 43 44 int OpenSessionByP2P(const char *sessionName, const char *peerDeviceId, const char *groupId, bool isFileType); 45 void CloseSession(int sessionId); 46 47 static int OnSessionOpened(int sessionId, int result); 48 static void OnSessionClosed(int sessionId); 49 static void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen); 50 static int OnReceiveFileStarted(int sessionId, const char *files, int fileCnt); 51 static int OnReceiveFileProcess(int sessionId, const char *firstFile, uint64_t bytesUpload, uint64_t bytesTotal); 52 static void OnReceiveFileFinished(int sessionId, const char *files, int fileCnt); 53 static void OnFileTransError(int sessionId); 54 55 int SendBytes(int sessionId, const void *data, unsigned int dataLen); 56 int SendFile(int sessionId, const std::vector<std::string> &sFileList, const std::vector<std::string> &dFileList); 57 std::string GetSessionNameById(int sessionId); 58 std::string GetPeerNetworkId(int sessionId); 59 bool IsFromServer(int sessionId); 60 61 void RegisterSessionListener(std::string sessionName, std::shared_ptr<ISoftbusListener> listener); 62 void UnRegisterSessionListener(std::string sessionName); 63 64 std::shared_ptr<ISoftbusListener> GetListener(std::string sessionName); 65 bool IsSessionOpened(int sessionId); 66 void AcceptSesion(int sessionId); 67 void RemoveSesion(int sessionId); 68 69 private: 70 SoftbusAdapter() = default; 71 std::unordered_map<std::string, std::shared_ptr<ISoftbusListener>> listeners_; 72 std::shared_mutex listenerMutex_; 73 74 std::mutex sessionMutex_; 75 std::unordered_map<int32_t, bool> SessionOpenedMap_; 76 }; 77 } // namespace OHOS::FileManagement::CloudSync 78 79 #endif // OHOS_FILEMGMT_SOFTBUS_ADAPTER_H