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 VTP_INSTANCE_H 17 #define VTP_INSTANCE_H 18 19 #include "fillpinc.h" 20 #include "i_stream_socket.h" 21 22 namespace Communication { 23 namespace SoftBus { 24 class VtpInstance { 25 public: Create(Args &&...args)26 template<typename... Args> static auto Create(Args &&... args) 27 { 28 struct EnableMakeShared : public VtpInstance { 29 explicit EnableMakeShared(Args &&... args) : VtpInstance(std::forward<Args>(args)...) {} 30 }; 31 return std::static_pointer_cast<VtpInstance>(std::make_shared<EnableMakeShared>(std::forward<Args>(args)...)); 32 } 33 virtual ~VtpInstance() = default; 34 static std::shared_ptr<VtpInstance> GetVtpInstance(); 35 36 static std::string GetVersion(); 37 static void UpdateSocketStreamCount(bool add); 38 static bool InitVtp(const std::string &pkgName); 39 static void DestroyVtp(const std::string &pkgName); 40 static void WaitForDestroy(const int &delayTimes); 41 42 private: 43 static constexpr int MAX_DEFAULT_SOCKET_NUM = 100; 44 static constexpr int DEBUG_BUFFER_LEN = 2048; 45 static constexpr int FILLP_KEEP_ALIVE_TIME = 300000; 46 static constexpr int DESTROY_TIMEOUT_SECOND = 30; 47 48 VtpInstance() = default; 49 VtpInstance(const VtpInstance &) = delete; 50 VtpInstance(const VtpInstance &&) = delete; 51 VtpInstance &operator=(const VtpInstance &) = delete; 52 VtpInstance &operator=(const VtpInstance &&) = delete; 53 54 static FILLP_UINT32 CryptoRand(); 55 static void PrintFillpLog(FILLP_UINT32 debugType, FILLP_UINT32 debugLevel, FILLP_UINT32 debugId, FILLP_CHAR *format, 56 ...); 57 static void PreSetFillpCoreParams(); 58 59 static bool isDebuged_; 60 static std::vector<std::string> packetNameArray_; 61 static int socketStreamCount_; 62 static std::string version_; 63 static bool isDestroyed_; 64 static int initVtpCount_; 65 static std::mutex vtpLock_; 66 static std::shared_ptr<VtpInstance> instance_; 67 }; 68 } // namespace SoftBus 69 } // namespace Communication 70 71 #endif