• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef HDC_BASE_H
16 #define HDC_BASE_H
17 #include "common.h"
18 
19 namespace Hdc {
20 namespace Base {
21     uint8_t GetLogLevel();
22     extern uint8_t g_logLevel;
23     void SetLogLevel(const uint8_t logLevel);
24     void PrintLogEx(const char *functionName, int line, uint8_t logLevel, const char *msg, ...);
25     void PrintMessage(const char *fmt, ...);
26     // tcpHandle can't be const as it's passed into uv_tcp_keepalive
27     void SetTcpOptions(uv_tcp_t *tcpHandle);
28     // Realloc need to update origBuf&origSize which can't be const
29     void ReallocBuf(uint8_t **origBuf, int *nOrigSize, size_t sizeWanted);
30     // handle&sendHandle must keep sync with uv_write
31     int SendToStreamEx(uv_stream_t *handleStream, const uint8_t *buf, const int bufLen, uv_stream_t *handleSend,
32                        const void *finishCallback, const void *pWriteReqData);
33     int SendToStream(uv_stream_t *handleStream, const uint8_t *buf, const int bufLen);
34     // As an uv_write_cb it must keep the same as prototype
35     void SendCallback(uv_write_t *req, int status);
36     // As an uv_alloc_cb it must keep the same as prototype
37     void AllocBufferCallback(uv_handle_t *handle, size_t sizeSuggested, uv_buf_t *buf);
38     uint64_t GetRuntimeMSec();
39     string GetRandomString(const uint16_t expectedLen);
40     int GetRandomNum(const int min, const int max);
41     uint64_t GetRandom(const uint64_t min = 0, const uint64_t max = UINT64_MAX);
42     int ConnectKey2IPPort(const char *connectKey, char *outIP, uint16_t *outPort);
43     // As an uv_work_cb it must keep the same as prototype
44     int StartWorkThread(uv_loop_t *loop, uv_work_cb pFuncWorkThread, uv_after_work_cb pFuncAfterThread,
45                         void *pThreadData);
46     // As an uv_work_cb it must keep the same as prototype
47     void FinishWorkThread(uv_work_t *req, int status);
48     int GetMaxBufSize();
49     bool TryCloseLoop(uv_loop_t *ptrLoop, const char *callerName);
50     void TryCloseHandle(const uv_handle_t *handle);
51     void TryCloseHandle(const uv_handle_t *handle, uv_close_cb closeCallBack);
52     void TryCloseHandle(const uv_handle_t *handle, bool alwaysCallback, uv_close_cb closeCallBack);
53     char **SplitCommandToArgs(const char *cmdStringLine, int *slotIndex);
54     bool RunPipeComand(const char *cmdString, char *outBuf, uint16_t sizeOutBuf, bool ignoreTailLf);
55     // results need to save in buf which can't be const
56     int ReadBinFile(const char *pathName, void **buf, const size_t bufLen);
57     int WriteBinFile(const char *pathName, const uint8_t *buf, const size_t bufLen, bool newFile = false);
58     void CloseIdleCallback(uv_handle_t *handle);
59     void CloseTimerCallback(uv_handle_t *handle);
60     int ProgramMutex(const char *procname, bool checkOrNew);
61     // result needs to save results which can't be const
62     void SplitString(const string &origString, const string &seq, vector<string> &resultStrings);
63     string GetShellPath();
64     uint64_t HostToNet(uint64_t val);
65     uint64_t NetToHost(uint64_t val);
66     string GetFullFilePath(string &s);
67     string GetPathWithoutFilename(const string &s);
68     int CreateSocketPair(int *fds);
69     void CloseSocketPair(int *fds);
70     int StringEndsWith(string s, string sub);
71     void BuildErrorString(const char *localPath, const char *op, const char *err, string &str);
72     const char *GetFileType(mode_t mode);
73     bool CheckDirectoryOrPath(const char *localPath, bool pathOrDir, bool readWrite, string &errStr, mode_t &fm);
74     bool CheckDirectoryOrPath(const char *localPath, bool pathOrDir, bool readWrite);
75     int Base64EncodeBuf(const uint8_t *input, const int length, uint8_t *bufOut);
76     vector<uint8_t> Base64Encode(const uint8_t *input, const int length);
77     int Base64DecodeBuf(const uint8_t *input, const int length, uint8_t *bufOut);
78     string Base64Decode(const uint8_t *input, const int length);
79     string UnicodeToUtf8(const char *src, bool reverse = false);
80     void ReverseBytes(void *start, int size);
81     string CanonicalizeSpecPath(string &src);
82     bool TryCreateDirectory(const string &path, string &err);
83     // Just zero a POD type, such as a structure or union
84     // If it contains c++ struct such as stl-string or others, please use 'T = {}' to initialize struct
ZeroStruct(T & structBuf)85     template<class T> int ZeroStruct(T &structBuf)
86     {
87         return memset_s(&structBuf, sizeof(T), 0, sizeof(T));
88     }
89     // just zero a statically allocated array of POD or built-in types
ZeroArray(T (& arrayBuf)[N])90     template<class T, size_t N> int ZeroArray(T (&arrayBuf)[N])
91     {
92         return memset_s(arrayBuf, sizeof(T) * N, 0, sizeof(T) * N);
93     }
94     // just zero memory buf, such as pointer
ZeroBuf(T & arrayBuf,int size)95     template<class T> int ZeroBuf(T &arrayBuf, int size)
96     {
97         return memset_s(arrayBuf, size, 0, size);
98     }
99     // clang-format off
100     const string StringFormat(const char * const formater, ...);
101     const string StringFormat(const char * const formater, va_list &vaArgs);
102     // clang-format on
103     string GetVersion();
104     bool IdleUvTask(uv_loop_t *loop, void *data, uv_idle_cb cb);
105     bool TimerUvTask(uv_loop_t *loop, void *data, uv_timer_cb cb, int repeatTimeout = UV_DEFAULT_INTERVAL);
106     bool DelayDo(uv_loop_t *loop, const int delayMs, const uint8_t flag, string msg, void *data,
107                  std::function<void(const uint8_t, string &, const void *)> cb);
DelayDoSimple(uv_loop_t * loop,const int delayMs,std::function<void (const uint8_t,string &,const void *)> cb)108     inline bool DelayDoSimple(uv_loop_t *loop, const int delayMs,
109                               std::function<void(const uint8_t, string &, const void *)> cb)
110     {
111         return DelayDo(loop, delayMs, 0, "", nullptr, cb);
112     }
DoNextLoop(uv_loop_t * loop,void * data,std::function<void (const uint8_t,string &,const void *)> cb)113     inline bool DoNextLoop(uv_loop_t *loop, void *data, std::function<void(const uint8_t, string &, const void *)> cb)
114     {
115         return DelayDo(loop, 0, 0, "", data, cb);
116     }
117 
118     // Trim from right side
119     inline string &RightTrim(string &s, const string &w = WHITE_SPACES)
120     {
121         s.erase(s.find_last_not_of(w) + 1);
122         return s;
123     }
124 
125     // Trim from left side
126     inline string &LeftTrim(string &s, const string &w = WHITE_SPACES)
127     {
128         s.erase(0, s.find_first_not_of(w));
129         return s;
130     }
131 
132     // Trim from both sides
133     inline string &Trim(string &s, const string &w = WHITE_SPACES)
134     {
135         return LeftTrim(RightTrim(s, w), w);
136     }
137     string ReplaceAll(string str, const string from, const string to);
138     uint8_t CalcCheckSum(const uint8_t *data, int len);
139     string GetFileNameAny(string &path);
140     string GetCwd();
141     string GetTmpDir();
142     void SetLogCache(bool enable);
143     void RemoveLogFile();
144     void RemoveLogCache();
145     void RollLogFile(const char *path);
146     uv_os_sock_t DuplicateUvSocket(uv_tcp_t *tcp);
147     bool IsRoot();
148     char GetPathSep();
149     string GetHdcAbsolutePath();
150     bool IsAbsolutePath(string &path);
GetMaxBufSize()151     inline int GetMaxBufSize()
152     {
153         return MAX_SIZE_IOBUF;
154     }
GetUsbffsBulkSize()155     inline int GetUsbffsBulkSize()
156     {
157         return MAX_USBFFS_BULK;
158     }
159 
160     int CloseFd(int &fd);
161     void InitProcess(void);
162     void DeInitProcess(void);
163 #ifdef HDC_SUPPORT_FLASHD
164     // deprecated, remove later
SetHdcProperty(const char * key,const char * value)165     inline bool SetHdcProperty(const char *key, const char *value)
166     {
167         return false;
168     }
169     // deprecated, remove later
GetHdcProperty(const char * key,char * value,uint16_t sizeOutBuf)170     inline bool GetHdcProperty(const char *key, char *value, uint16_t sizeOutBuf)
171     {
172         return false;
173     }
174 #endif
175 }  // namespace base
176 }  // namespace Hdc
177 
178 #endif  // HDC_BASE_H
179