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