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