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