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