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, int 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 int bufLen); 57 int WriteBinFile(const char *pathName, const uint8_t *buf, const int 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(const string &s); 67 int CreateSocketPair(int *fds); 68 void CloseSocketPair(const int *fds); 69 int StringEndsWith(string s, string sub); 70 void BuildErrorString(const char *localPath, const char *op, const char *err, string &str); 71 const char *GetFileType(mode_t mode); 72 bool CheckDirectoryOrPath(const char *localPath, bool pathOrDir, bool readWrite, string &errStr); 73 bool CheckDirectoryOrPath(const char *localPath, bool pathOrDir, bool readWrite); 74 int Base64EncodeBuf(const uint8_t *input, const int length, uint8_t *bufOut); 75 vector<uint8_t> Base64Encode(const uint8_t *input, const int length); 76 int Base64DecodeBuf(const uint8_t *input, const int length, uint8_t *bufOut); 77 string Base64Decode(const uint8_t *input, const int length); 78 void ReverseBytes(void *start, int size); 79 string CanonicalizeSpecPath(string &src); 80 // Just zero a POD type, such as a structure or union 81 // If it contains c++ struct such as stl-string or others, please use 'T = {}' to initialize struct ZeroStruct(T & structBuf)82 template<class T> int ZeroStruct(T &structBuf) 83 { 84 return memset_s(&structBuf, sizeof(T), 0, sizeof(T)); 85 } 86 // just zero a statically allocated array of POD or built-in types ZeroArray(T (& arrayBuf)[N])87 template<class T, size_t N> int ZeroArray(T (&arrayBuf)[N]) 88 { 89 return memset_s(arrayBuf, sizeof(T) * N, 0, sizeof(T) * N); 90 } 91 // just zero memory buf, such as pointer ZeroBuf(T & arrayBuf,int size)92 template<class T> int ZeroBuf(T &arrayBuf, int size) 93 { 94 return memset_s(arrayBuf, size, 0, size); 95 } 96 // clang-format off 97 const string StringFormat(const char * const formater, ...); 98 const string StringFormat(const char * const formater, va_list &vaArgs); 99 // clang-format on 100 string GetVersion(); 101 bool IdleUvTask(uv_loop_t *loop, void *data, uv_idle_cb cb); 102 bool TimerUvTask(uv_loop_t *loop, void *data, uv_timer_cb cb, int repeatTimeout = UV_DEFAULT_INTERVAL); 103 bool DelayDo(uv_loop_t *loop, const int delayMs, const uint8_t flag, string msg, void *data, 104 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)105 inline bool DelayDoSimple(uv_loop_t *loop, const int delayMs, 106 std::function<void(const uint8_t, string &, const void *)> cb) 107 { 108 return DelayDo(loop, delayMs, 0, "", nullptr, cb); 109 } DoNextLoop(uv_loop_t * loop,void * data,std::function<void (const uint8_t,string &,const void *)> cb)110 inline bool DoNextLoop(uv_loop_t *loop, void *data, std::function<void(const uint8_t, string &, const void *)> cb) 111 { 112 return DelayDo(loop, 0, 0, "", data, cb); 113 } 114 115 // Trim from right side 116 inline string &RightTrim(string &s, const string &w = WHITE_SPACES) 117 { 118 s.erase(s.find_last_not_of(w) + 1); 119 return s; 120 } 121 122 // Trim from left side 123 inline string &LeftTrim(string &s, const string &w = WHITE_SPACES) 124 { 125 s.erase(0, s.find_first_not_of(w)); 126 return s; 127 } 128 129 // Trim from both sides 130 inline string &Trim(string &s, const string &w = WHITE_SPACES) 131 { 132 return LeftTrim(RightTrim(s, w), w); 133 } 134 string ReplaceAll(string str, const string from, const string to); 135 uint8_t CalcCheckSum(const uint8_t *data, int len); 136 string GetFileNameAny(string &path); 137 string GetCwd(); 138 string GetTmpDir(); 139 void SetLogCache(bool enable); 140 void RemoveLogFile(); 141 void RemoveLogCache(); 142 void RollLogFile(const char *path); 143 uv_os_sock_t DuplicateUvSocket(uv_tcp_t *tcp); 144 bool IsRoot(); 145 char GetPathSep(); 146 bool IsAbsolutePath(string &path); GetMaxBufSize()147 inline int GetMaxBufSize() 148 { 149 return MAX_SIZE_IOBUF; 150 } GetUsbffsBulkSize()151 inline int GetUsbffsBulkSize() 152 { 153 return MAX_USBFFS_BULK; 154 } 155 #ifdef HDC_SUPPORT_FLASHD 156 // deprecated, remove later SetHdcProperty(const char * key,const char * value)157 inline bool SetHdcProperty(const char *key, const char *value) 158 { 159 return false; 160 } 161 // deprecated, remove later GetHdcProperty(const char * key,char * value,uint16_t sizeOutBuf)162 inline bool GetHdcProperty(const char *key, char *value, uint16_t sizeOutBuf) 163 { 164 return false; 165 } 166 #endif 167 } // namespace base 168 } // namespace Hdc 169 170 #endif // HDC_BASE_H 171