From 5a26c600eea9bc267289b86312c923246f221f31 Mon Sep 17 00:00:00 2001 From: HuangXin Date: Tue, 24 Jul 2018 10:31:32 +0800 Subject: [PATCH] backup --- .gitignore | 4 + Example/main.c | 51 +- Framework/Configure/ini_prase.c | 72 ++ Framework/Network/inet_api.c | 2 +- Framework/Network/inet_api.c.bak | 1327 ---------------------------- Framework/SvrManager/svr_manager.c | 11 + Framework/libuvEngine/libuv_dbus.c | 123 ++- Modules/Alarm/assistant.c | 100 ++- Modules/OTA/ota.c | 3 +- include/libuv_dbus.h | 5 + include/server_addr.h | 7 +- log/log.c | 2 +- 12 files changed, 343 insertions(+), 1364 deletions(-) create mode 100644 .gitignore delete mode 100644 Framework/Network/inet_api.c.bak diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5e15cdc --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/.build/ +build/debug/ +build/release/ +build/*.so diff --git a/Example/main.c b/Example/main.c index 9f18f12..4db26ef 100644 --- a/Example/main.c +++ b/Example/main.c @@ -1395,6 +1395,7 @@ static void __uvMsgSendTestProc(void *pParams) static void __uvMsgRecvProc(void *pParams) { + while(TRUE) { PDBUS_MSG_PACK pMsg = DBusGetMessage(); @@ -1667,6 +1668,40 @@ static void __dlTestThread(void* p) } } +static void uvTimerTestCb(uv_timer_t* pTimer) +{ + LOG_EX(LOG_Debug, "Timer On(%d)...............\n", pTimer->data); +} + +static void __uvThreadTimerV2(uv_loop_t *pLoop) +{ + static uv_timer_t uvTimer; + + uv_timer_init(pLoop, &uvTimer); + LOG_EX(LOG_Debug, "Timer Begin\n"); + uvTimer.data = (intptr_t*)1; + uv_timer_start(&uvTimer, uvTimerTestCb, 3000, 0); + + sleep(10); + + LOG_EX(LOG_Debug, "Timer Stop\n"); + uv_timer_stop(&uvTimer); + + LOG_EX(LOG_Debug, "Timer Begin\n"); + uvTimer.data = (intptr_t*)2; + uv_update_time(pLoop); + uv_timer_start(&uvTimer, uvTimerTestCb, 6000, 0); + + sleep(3); + LOG_EX(LOG_Debug, "Timer Stop\n"); + uv_timer_stop(&uvTimer); + + LOG_EX(LOG_Debug, "Timer Begin\n"); + uvTimer.data = (intptr_t*)3; + uv_update_time(pLoop); + uv_timer_start(&uvTimer, uvTimerTestCb, 12000, 0); +} + int main(int argc, char **argv) { int i, j, ret = 0; @@ -1675,7 +1710,11 @@ int main(int argc, char **argv) uv_loop_t* pLoop = GetDBusDefaultLoop(); int modIdx = -1; char buf[256]; - +#if 1 + i = GetServerModeFromCC(PUBLISH_MODE, &ret); + + fprintf(stdout, "Read ServerMode = %d, errno = %d\n", i, ret); +#endif #if 0 FILE* pFile = fopen("/mnt/UDISK/time.txt", "w+"); @@ -1745,6 +1784,8 @@ int main(int argc, char **argv) return 0; } + //__uvThreadTimerV2(pLoop); + //test_task_new(__uvThreadTest, NULL); //DumpCurServerAddr("Default"); @@ -1777,7 +1818,7 @@ int main(int argc, char **argv) DumpCurServerAddr("PUBLISH_MODE"); #endif - //test_task_new(__uvMsgRecvProc, NULL); + //test_task_new(__uvThreadTimerV2, pLoop); //HttpPostLogFile("{}"); @@ -1899,13 +1940,13 @@ int main(int argc, char **argv) #endif #if 0 struct tm localTime; - time_t timeStamp = time((time_t*)NULL) + 10; + time_t timeStamp = time((time_t*)NULL) + 30; localtime_r(&timeStamp, &localTime); - AlarmTimerAdd(2018-1900, 6-1, 26, + AlarmTimerAdd(2018-1900, 7-1, 19, localTime.tm_hour, localTime.tm_min, localTime.tm_sec, 0, - REPEAT_MODE_MONTH_LAST_DAY, + 3, __onAlarmCb, 0, NULL, NULL); #endif #if 0 diff --git a/Framework/Configure/ini_prase.c b/Framework/Configure/ini_prase.c index 8a85617..3542f87 100644 --- a/Framework/Configure/ini_prase.c +++ b/Framework/Configure/ini_prase.c @@ -72,6 +72,78 @@ void InitCfgToCfgFile(config_t* pCfg) } } +int CfgGetIntValueV2(const char* pTags, int defValue, int* pErr) +{ + char* pSvrMode = NULL; + char cmdBuf[MAX_PATH]; + int iValue = defValue; + + memset(cmdBuf, 0, MAX_PATH); + + sprintf(cmdBuf, "cat %s | grep %s | awk '{print $3}' | cut -d \";\" -f 1", + DEVICE_CFG_FILE, + pTags); + + GetShellExecResult(cmdBuf, &pSvrMode); + + if(pSvrMode == NULL) + { + if(pErr) + { + *pErr = -ERR_NO_ITEMS; + } + return defValue; + } + + iValue = strtol(pSvrMode, NULL, 10); + free(pSvrMode); + + if(errno == EINVAL || errno == ERANGE) + { + if(pErr) + { + *pErr = -ERR_STR_CONVERT; + } + + return defValue; + } + + if(pErr) + { + *pErr = 0; + } + return iValue; +} + +int CfgGetIntValueV1(const char* pTags, int defValue, int* pErr) +{ + int iValue = defValue; + + if(pTags == NULL || strlen(pTags) == 0) + { + if(pErr) + { + *pErr = -ERR_INPUT_PARAMS; + } + + return defValue; + } + + if(!config_lookup_int(&g_cfgInfo, pTags, &iValue)) + { + if(pErr) + { + *pErr = -ERR_READ_FILE; + } + + return defValue; + } + + *pErr = 0; + + return iValue; +} + int CfgGetIntValue(const char* pTags, int defValue) { int iValue = defValue; diff --git a/Framework/Network/inet_api.c b/Framework/Network/inet_api.c index 0edd15e..ee3a9db 100644 --- a/Framework/Network/inet_api.c +++ b/Framework/Network/inet_api.c @@ -1073,7 +1073,7 @@ int InetHttpUploadFileSync(const char *pURL, const char* pPath, void* pAttachInf if(ret != CURLE_OK) { - LOG_EX(LOG_Error, "Upload %s File %s Error %s\n", pURL, pPath, curl_easy_strerror(ret)); + LOG_EX(LOG_Error, "Upload %s File %s Error %s(%d)\n", pURL, pPath, curl_easy_strerror(ret), ret); rc = -ERR_NETWORK_SEND; } diff --git a/Framework/Network/inet_api.c.bak b/Framework/Network/inet_api.c.bak deleted file mode 100644 index 7333176..0000000 --- a/Framework/Network/inet_api.c.bak +++ /dev/null @@ -1,1327 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "inet_api.h" - -#define MAX_TIMEOUT_VALUE (10) -#define SSL_CA_FILE ("/etc/ssl/certs/ca-certificates.crt") - -typedef enum -{ - INET_HTTP_DOWNLOAD_FILE = 0, - INET_HTTP_WEBSERVICE_POST, -} INET_ACCESS_TYPE; - -typedef struct -{ - uv_poll_t uvPool; - curl_socket_t sock; -} CURL_CONTEXT_DATA, *PCURL_CONTEXT_DATA; - -typedef struct -{ - char* pReqUrl; - char sPath[MAX_PATH]; - char sDlPath[MAX_PATH]; - char* pTaskUuid; - INET_ACCESS_TYPE type; - unsigned int dlSize; - unsigned int lastTm; - unsigned int createTm; - uv_fs_t uvFsOpen; - uv_fs_t uvFsWrite; - uv_fs_t uvFsDataSync; - uv_fs_t uvFsClose; - uv_buf_t uvFsBuf; - OnProgressNotify onPrgCb; - OnHttpResponse onRspCb; - CURL* pCurl; - void* pData; - int errCode; -} HTTP_REQ_PARAMS, *PHTTP_REQ_PARAMS; - -typedef struct -{ - char *pTaskUuid; - unsigned int uRetryTimes; - int isCancel; - PHTTP_REQ_PARAMS pCurlItem; - - UT_hash_handle hh; ///< UT Hash handle -} CURL_HANDLE_TBL, *PCURL_HANDLE_TBL; - -static uv_timer_t g_uvCurlTm; -static uv_timer_t g_uvDlTm; -static CURLM* g_pCurl = NULL; -static uv_loop_t* g_pMainLoop = NULL; -static PCURL_HANDLE_TBL g_ReqHandleTbl = NULL; -static uv_rwlock_t g_uvHashRwLock; - -static void __addReqIdToTable(const char* pTaskUuid, PHTTP_REQ_PARAMS pParams) -{ - PCURL_HANDLE_TBL pItem = NULL; - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_FIND_STR(g_ReqHandleTbl, pTaskUuid, pItem); - uv_rwlock_rdunlock(&g_uvHashRwLock); - - if(pItem == NULL) - { - pItem = (PCURL_HANDLE_TBL)malloc(sizeof(CURL_HANDLE_TBL)); - - memset(pItem, 0, sizeof(CURL_HANDLE_TBL)); - pItem->pTaskUuid = (char*)pTaskUuid; - - uv_rwlock_wrlock(&g_uvHashRwLock); - HASH_ADD_STR(g_ReqHandleTbl, pTaskUuid, pItem); - uv_rwlock_wrunlock(&g_uvHashRwLock); - } - - pItem->isCancel = FALSE; - pItem->pCurlItem = pParams; - pItem->uRetryTimes++; -} - -static void __removeReqIdFromTable(const char* pTaskUuid) -{ - PCURL_HANDLE_TBL pItem = NULL; - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_FIND_STR(g_ReqHandleTbl, pTaskUuid, pItem); - uv_rwlock_rdunlock(&g_uvHashRwLock); - - if(pItem != NULL) - { - pItem->isCancel = TRUE; - } -} - -static void __cleanUpReqIdFromTable(PCURL_HANDLE_TBL pItem) -{ - if(pItem != NULL) - { - HASH_DEL(g_ReqHandleTbl, pItem); - - if(pItem->pTaskUuid) - { - free(pItem->pTaskUuid); - } - - free(pItem); - } -} - -static void __uvFsCloseCb(uv_fs_t *puvFs) -{ - PHTTP_REQ_PARAMS pParams = (PHTTP_REQ_PARAMS)puvFs->data; - - if(puvFs->result < 0) - { - LOG_EX(LOG_Error, "[%s] Error: %d\n", __FUNCTION__, puvFs->result); - } - - uv_fs_req_cleanup(puvFs); - - if(pParams->type == INET_HTTP_DOWNLOAD_FILE) - { - if(strcmp(pParams->sDlPath, pParams->sPath) != 0) - { - CopyFile(pParams->sDlPath, pParams->sPath); - unlink(pParams->sDlPath); - } - - pParams->onRspCb(NULL, pParams->dlSize, pParams->pReqUrl, pParams->sPath, - pParams->pTaskUuid, pParams->errCode, pParams->pData); - } - - __removeReqIdFromTable(pParams->pTaskUuid); - - if(pParams->pReqUrl) - { - free(pParams->pReqUrl); - pParams->pReqUrl = NULL; - } - - free(pParams); - pParams = NULL; -} - -static void __uvFsDataSyncCb(uv_fs_t *puvFs) -{ - PHTTP_REQ_PARAMS pParams = (PHTTP_REQ_PARAMS)puvFs->data; - - if(puvFs->result < 0) - { - LOG_EX(LOG_Error, "[%s] Error: %d\n", __FUNCTION__, puvFs->result); - } - - uv_fs_req_cleanup(puvFs); - - uv_fs_close(g_pMainLoop, &pParams->uvFsClose, pParams->uvFsOpen.result, __uvFsCloseCb); -} - -static PCURL_CONTEXT_DATA __createCurlContext(curl_socket_t sock) -{ - PCURL_CONTEXT_DATA pContext = (PCURL_CONTEXT_DATA)malloc(sizeof(CURL_CONTEXT_DATA)); - - pContext->sock = sock; - - if(uv_poll_init_socket(g_pMainLoop, &pContext->uvPool, sock) != 0) - { - LOG_EX(LOG_Error, "uv_poll_init_socket Error\n"); - } - - pContext->uvPool.data = pContext; - - return (pContext); -} - -static void __uvCloseCb(uv_handle_t *puvPoll) -{ - PCURL_CONTEXT_DATA pContext = (PCURL_CONTEXT_DATA)puvPoll->data; - free(pContext); -} - -static void __destoryCurlContext(PCURL_CONTEXT_DATA pContext) -{ - uv_close((uv_handle_t *)&pContext->uvPool, __uvCloseCb); -} - -static void __checkMultiInfoTimeout(void) -{ - PHTTP_REQ_PARAMS pReq; - CURLMsg *pMsg = NULL; - int iPending; - - while((pMsg = curl_multi_info_read(g_pCurl, &iPending))) - { - switch(pMsg->msg) - { - case CURLMSG_DONE: - curl_easy_getinfo(pMsg->easy_handle, CURLINFO_PRIVATE, (void*)&pReq); - - curl_multi_remove_handle(g_pCurl, pMsg->easy_handle); - curl_easy_cleanup(pMsg->easy_handle); - - if(pReq) - { - pReq->onRspCb(NULL, 0, pReq->pReqUrl, pReq->sPath, pReq->pTaskUuid, - -CURLE_OPERATION_TIMEDOUT, pReq->pData); - - if(pReq->pReqUrl) - { - free(pReq->pReqUrl); - pReq->pReqUrl = NULL; - } - - __removeReqIdFromTable(pReq->pTaskUuid); - - free(pReq); - pReq = NULL; - } - - break; - - default: - LOG_EX(LOG_Error, "pMsg->msg(%d) != CURLMSG_DONE\n", pMsg->msg); - return; - } - } -} - -static void __checkMultiInfo(void) -{ - PHTTP_REQ_PARAMS pReq; - CURLMsg *pMsg = NULL; - int iPending; - - while((pMsg = curl_multi_info_read(g_pCurl, &iPending))) - { - switch(pMsg->msg) - { - case CURLMSG_DONE: - curl_easy_getinfo(pMsg->easy_handle, CURLINFO_PRIVATE, (void*)&pReq); - - curl_multi_remove_handle(g_pCurl, pMsg->easy_handle); - curl_easy_cleanup(pMsg->easy_handle); - - if(pReq) - { - if(pReq->type == INET_HTTP_DOWNLOAD_FILE) - { - if(pMsg->data.result != CURLE_OK) - { - pReq->errCode = pMsg->data.result; - } - else - { - pReq->errCode = 0; - } - - uv_fs_fdatasync(g_pMainLoop, &pReq->uvFsDataSync, pReq->uvFsOpen.result, __uvFsDataSyncCb); - } - else if(pReq->type == INET_HTTP_WEBSERVICE_POST) - { - if(pMsg->data.result != CURLE_OK) - { - pReq->onRspCb(pReq->uvFsBuf.base, pReq->dlSize, pReq->pReqUrl, pReq->sPath, - pReq->pTaskUuid, -pMsg->data.result, pReq->pData); - } - else - { - pReq->onRspCb(pReq->uvFsBuf.base, pReq->dlSize, pReq->pReqUrl, pReq->sPath, - pReq->pTaskUuid, 0, pReq->pData); - } - - if(pReq->uvFsBuf.base) - { - free(pReq->uvFsBuf.base); - } - - if(pReq->pReqUrl) - { - free(pReq->pReqUrl); - pReq->pReqUrl = NULL; - } - - __removeReqIdFromTable(pReq->pTaskUuid); - - free(pReq); - pReq = NULL; - } - else - { - if(pMsg->data.result != CURLE_OK) - { - pReq->onRspCb(NULL, 0, pReq->pReqUrl, pReq->sPath, pReq->pTaskUuid, -pMsg->data.result, pReq->pData); - } - else - { - pReq->onRspCb(NULL, 0, pReq->pReqUrl, pReq->sPath, pReq->pTaskUuid, 0, pReq->pData); - } - - if(pReq->pReqUrl) - { - free(pReq->pReqUrl); - pReq->pReqUrl = NULL; - } - - __removeReqIdFromTable(pReq->pTaskUuid); - - free(pReq); - pReq = NULL; - } - } - - break; - - default: - LOG_EX(LOG_Error, "pMsg->msg(%d) != CURLMSG_DONE\n", pMsg->msg); - return; - } - } -} - -static void __onDlTmoutCb(uv_timer_t *pufTimer) -{ - PCURL_HANDLE_TBL pItem = NULL, pTemp = NULL; - unsigned int curTm = (unsigned int)LIBUV_CURRENT_TIME_S(); - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_ITER(hh, g_ReqHandleTbl, pItem, pTemp) - { - int dlTime; - - if(pItem->isCancel) - { - continue; - } - - dlTime = curTm - pItem->pCurlItem->createTm; - - // 下载时间大于10s且平均下载速度小于10K/s超时 - if((dlTime * 10000 > pItem->pCurlItem->dlSize) && dlTime > 10) - { - LOG_EX(LOG_Error, "Download Speed less than 10k/s: %s (%uK/%ds)\n", - pItem->pTaskUuid, pItem->pCurlItem->dlSize / 1000, dlTime); - - InetCancelDownload(pItem->pTaskUuid); - break; - } - - // 10秒内没有下载任何数据超时 - if(pItem->pCurlItem->lastTm > 0) - { - if(curTm > pItem->pCurlItem->lastTm + MAX_TIMEOUT_VALUE) - { - LOG_EX(LOG_Error, "Download Timeout: %s\n", pItem->pTaskUuid); - InetCancelDownload(pItem->pTaskUuid); - break; - } - } - - // 下载最长时间设置为1800秒(30分钟) - if(dlTime > 1800) - { - LOG_EX(LOG_Error, "Download More than 1800 seconds: %s (%uK/%ds)\n", - pItem->pTaskUuid, pItem->pCurlItem->dlSize/1000, dlTime); - InetCancelDownload(pItem->pTaskUuid); - break; - } - } - uv_rwlock_rdunlock(&g_uvHashRwLock); - - uv_rwlock_wrlock(&g_uvHashRwLock); - HASH_ITER(hh, g_ReqHandleTbl, pItem, pTemp) - { - if(pItem->isCancel != FALSE) - { - __cleanUpReqIdFromTable(pItem); - } - } - uv_rwlock_wrunlock(&g_uvHashRwLock); -} - -static void __onTimeoutCb(uv_timer_t *pufTimer) -{ - int iRun; - - curl_multi_socket_action(g_pCurl, CURL_SOCKET_TIMEOUT, 0, &iRun); - - __checkMultiInfoTimeout(); -} - -static int __curlTimerCb(CURLM *multi, /* multi handle */ - long timeout_ms, /* see above */ - void *userp) /* private callback pointer */ -{ - if(timeout_ms <= 0) - { - timeout_ms = 1; - } - - uv_timer_start(&g_uvCurlTm, __onTimeoutCb, timeout_ms, 0); - - return 0; -} - -static void __curlPollCb(uv_poll_t *pPoll, int status, int events) -{ - int iRun; - int flags; - PCURL_CONTEXT_DATA pContext = NULL; - - uv_timer_stop(&g_uvCurlTm); - - if(events & UV_READABLE) - { - flags = CURL_CSELECT_IN; - } - else if(events & UV_WRITABLE) - { - flags = CURL_CSELECT_OUT; - } - - pContext = (PCURL_CONTEXT_DATA)pPoll; - curl_multi_socket_action(g_pCurl, pContext->sock, flags, &iRun); - __checkMultiInfo(); -} - -static int __curlSockCb(CURL *easy, /* easy handle */ - curl_socket_t s, /* socket */ - int what, /* describes the socket */ - void *userp, /* private callback pointer */ - void *socketp) /* private socket pointer */ -{ - PCURL_CONTEXT_DATA pContext = NULL; - - if(what == CURL_POLL_IN || what == CURL_POLL_OUT) - { - if(socketp) - { - pContext = (PCURL_CONTEXT_DATA)socketp; - } - else - { - pContext = __createCurlContext(s); - } - - curl_multi_assign(g_pCurl, s, (void *)pContext); - } - - switch(what) - { - case CURL_POLL_IN: - uv_poll_start(&pContext->uvPool, UV_READABLE, __curlPollCb); - break; - - case CURL_POLL_OUT: - uv_poll_start(&pContext->uvPool, UV_WRITABLE, __curlPollCb); - break; - - case CURL_POLL_REMOVE: - if(socketp) - { - uv_poll_stop(&((PCURL_CONTEXT_DATA)socketp)->uvPool); - __destoryCurlContext((PCURL_CONTEXT_DATA)socketp); - curl_multi_assign(g_pCurl, s, NULL); - } - break; - - default: - return (0); - } - - return (0); -} - -static size_t __writeDataCb(void *pData, size_t size, size_t nmemb, void *pParams) -{ - PHTTP_REQ_PARAMS pReq = (PHTTP_REQ_PARAMS)pParams; - int iMemSize = size * nmemb; - //print_hex_dump_bytes("OTA", DUMP_PREFIX_ADDRESS, pData, size * nmemb); - - pReq->lastTm = LIBUV_CURRENT_TIME_S(); - - if(pReq->type == INET_HTTP_DOWNLOAD_FILE) - { - int wr = 0; - - pReq->uvFsBuf = uv_buf_init(pData, iMemSize); - - wr = uv_fs_write(g_pMainLoop, &pReq->uvFsWrite, pReq->uvFsOpen.result, &pReq->uvFsBuf, 1, -1, NULL); - - if(wr > 0) - { - pReq->dlSize += wr; - } - } - else if(pReq->type == INET_HTTP_WEBSERVICE_POST) - { - int newSize = 0; - - if(pReq->uvFsBuf.base == NULL && pReq->uvFsBuf.len == 0) - { - newSize = iMemSize + 1; - //fprintf(stdout, "size = %d, newsize = %d, dlsize = %d\n", iMemSize, newSize, pReq->dlSize); - pReq->uvFsBuf.base = malloc(newSize); - memcpy(pReq->uvFsBuf.base, pData, iMemSize); - } - else - { - newSize = pReq->dlSize + iMemSize + 1; - //fprintf(stdout, "::size = %d, newsize = %d, dlsize = %d\n", iMemSize, newSize, pReq->dlSize); - pReq->uvFsBuf.base = realloc(pReq->uvFsBuf.base, newSize); - memcpy(pReq->uvFsBuf.base + pReq->dlSize, pData, iMemSize); - } - - pReq->uvFsBuf.base[pReq->dlSize] = 0; - pReq->dlSize += iMemSize; - } - - return (size * nmemb); -} - -static int __progressCb(void* pData, - double total, - double now, - double ultotal, - double ulnow) -{ - PHTTP_REQ_PARAMS pParams = (PHTTP_REQ_PARAMS)pData; - - if(pParams->onPrgCb) - { - if(pParams->type == INET_HTTP_DOWNLOAD_FILE) - { - pParams->onPrgCb(pParams->pReqUrl, pParams->pTaskUuid, (unsigned char)(now * 100.0 / total), pParams->pData); - } - } - - return (0); -} - -static size_t __getRemoteSizeCb(void *pData, size_t size, size_t nmemb, void *pParams) -{ - return (size * nmemb); -} - -static int __iNetGetRemoteSize(const char* pURL, unsigned int reqId) -{ - double size = 0.0; - CURL *pCurl = curl_easy_init(); - CURLcode res; - - curl_easy_setopt(pCurl, CURLOPT_URL, pURL); - curl_easy_setopt(pCurl, CURLOPT_NOBODY, 1L); - curl_easy_setopt(pCurl, CURLOPT_HEADERFUNCTION, __getRemoteSizeCb); - curl_easy_setopt(pCurl, CURLOPT_FOLLOWLOCATION, 1L); - - curl_easy_perform(pCurl); - - res = curl_easy_getinfo(pCurl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &size); - - if(res != CURLE_OK) - { - return (-1); - } - - curl_easy_cleanup(pCurl); - - return (int)(size); -} - -static const char* __restartDlFileAsync(PHTTP_REQ_PARAMS pParams) -{ - CURL *pCurl = curl_easy_init(); - - pParams->type = INET_HTTP_DOWNLOAD_FILE; - pParams->dlSize = 0; - pParams->pCurl = pCurl; - pParams->lastTm = 0; - - uv_fs_open(g_pMainLoop, - &pParams->uvFsOpen, - pParams->sDlPath, - O_RDWR | O_CREAT | O_TRUNC, - S_IRUSR | S_IWUSR, - NULL); - - curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, __writeDataCb); - curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pParams); - curl_easy_setopt(pCurl, CURLOPT_PRIVATE, pParams); - curl_easy_setopt(pCurl, CURLOPT_URL, pParams->pReqUrl); - - curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 0L); - curl_easy_setopt(pCurl, CURLOPT_PROGRESSFUNCTION, __progressCb); - curl_easy_setopt(pCurl, CURLOPT_PROGRESSDATA, pParams); - -#ifdef SKIP_PEER_VERIFICATION - /* - * If you want to connect to a site who isn't using a certificate that is - * signed by one of the certs in the CA bundle you have, you can skip the - * verification of the server's certificate. This makes the connection - * A LOT LESS SECURE. - * - * If you have a CA cert for the server stored someplace else than in the - * default bundle, then the CURLOPT_CAPATH option might come handy for - * you. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L); -#else - curl_easy_setopt(pCurl, CURLOPT_CAINFO, SSL_CA_FILE); - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L); -#endif - -#ifdef SKIP_HOSTNAME_VERIFICATION - /* - * If the site you're connecting to uses a different host name that what - * they have mentioned in their server certificate's commonName (or - * subjectAltName) fields, libcurl will refuse to connect. You can skip - * this check, but this will make the connection less secure. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0L); -#endif - - //LOG_EX(LOG_Debug, "Total Size = %d\n", __iNetGetRemoteSize(pURL, 0)); - - curl_multi_add_handle(g_pCurl, pCurl); - - __addReqIdToTable(pParams->pTaskUuid, pCurl); - - return (pParams->pTaskUuid); -} - -const char* InetHttpDlFileAsync(const char *pURL, - const char *pPath, - OnHttpResponse onRespCb, - OnProgressNotify onProgressCb, - void* pData) -{ - uuid_t msgId; - char strMsgId[64]; - PHTTP_REQ_PARAMS pParams = NULL; - CURL *pCurl = NULL; - unsigned long long uMemFreeSize = GetPartitionFreeSize("/tmp/"); - - if(pURL == NULL || strlen(pURL) == 0 || onRespCb == NULL) - { - free(pParams); - return (NULL); - } - - LOG_EX(LOG_Debug, "Begin Download: %s\n", pURL); - - pParams = (PHTTP_REQ_PARAMS)malloc(sizeof(HTTP_REQ_PARAMS)); - - memset(pParams, 0, sizeof(HTTP_REQ_PARAMS)); - - pCurl = curl_easy_init(); - - pParams->onRspCb = onRespCb; - pParams->pReqUrl = (char *)malloc(strlen(pURL) + 1); - pParams->type = INET_HTTP_DOWNLOAD_FILE; - pParams->dlSize = 0; - pParams->onPrgCb = onProgressCb; - pParams->pData = pData; - pParams->pCurl = pCurl; - pParams->lastTm = 0; - pParams->createTm = (unsigned int)LIBUV_CURRENT_TIME_S(); - - memset(pParams->pReqUrl, 0, strlen(pURL) + 1); - strcpy(pParams->pReqUrl, pURL); - - uuid_generate_random(msgId); - memset(strMsgId, 0, 64); - uuid_unparse_lower(msgId, strMsgId); - pParams->pTaskUuid = strdup(strMsgId); - - if(pPath == NULL) - { - sprintf(pParams->sPath, "./%s", basename_v2(pURL)); - } - else - { - strcpy(pParams->sPath, pPath); - } - - // Memory Free More Than 100M, Download Temp File To Memory - if(uMemFreeSize >= 100 * 1024 * 1024 && - strncmp(pParams->sPath, "/tmp/", 5) != 0) - { - int ret = system("mkdir /tmp/dl -p"); - sprintf(pParams->sDlPath, "/tmp/dl/%s_%s.dl", basename_v2(pParams->sPath), pParams->pTaskUuid); - } - else - { - strcpy(pParams->sDlPath, pParams->sPath); - } - - pParams->uvFsDataSync.data = pParams; - pParams->uvFsClose.data = pParams; - - uv_fs_open(g_pMainLoop, - &pParams->uvFsOpen, - pParams->sDlPath, - O_RDWR | O_CREAT | O_TRUNC, - S_IRUSR | S_IWUSR, - NULL); - - curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, __writeDataCb); - curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pParams); - curl_easy_setopt(pCurl, CURLOPT_PRIVATE, pParams); - curl_easy_setopt(pCurl, CURLOPT_URL, pURL); - - curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 0L); - curl_easy_setopt(pCurl, CURLOPT_PROGRESSFUNCTION, __progressCb); - curl_easy_setopt(pCurl, CURLOPT_PROGRESSDATA, pParams); - - //curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, 1800L); // Max download times (30 minutes)1800s - //curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_LIMIT, 10000L); // 10K bytes - //curl_easy_setopt(pCurl, CURLOPT_LOW_SPEED_TIME, 30L); // 30 seconds - - curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 10L); - //curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT_MS, 10L); - - - //curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1L); - -#ifdef SKIP_PEER_VERIFICATION - /* - * If you want to connect to a site who isn't using a certificate that is - * signed by one of the certs in the CA bundle you have, you can skip the - * verification of the server's certificate. This makes the connection - * A LOT LESS SECURE. - * - * If you have a CA cert for the server stored someplace else than in the - * default bundle, then the CURLOPT_CAPATH option might come handy for - * you. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L); -#else - curl_easy_setopt(pCurl, CURLOPT_CAINFO, SSL_CA_FILE); - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L); -#endif - -#ifdef SKIP_HOSTNAME_VERIFICATION - /* - * If the site you're connecting to uses a different host name that what - * they have mentioned in their server certificate's commonName (or - * subjectAltName) fields, libcurl will refuse to connect. You can skip - * this check, but this will make the connection less secure. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0L); -#endif - - //LOG_EX(LOG_Debug, "Download: %s --> %s\n", pParams->pReqUrl, pParams->sDlPath); - - curl_multi_add_handle(g_pCurl, pCurl); - - __addReqIdToTable(pParams->pTaskUuid, pParams); - - return (pParams->pTaskUuid); -} - -int InetCancelDownload(const char *pTaskUuid) -{ - if(pTaskUuid && strlen(pTaskUuid) > 0) - { - PCURL_HANDLE_TBL pItem = NULL; - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_FIND_STR(g_ReqHandleTbl, pTaskUuid, pItem); - uv_rwlock_rdunlock(&g_uvHashRwLock); - - if(pItem != NULL && pItem->isCancel != TRUE) - { - __removeReqIdFromTable(pTaskUuid); - curl_easy_pause(pItem->pCurlItem->pCurl, CURLPAUSE_ALL); - curl_easy_cleanup(pItem->pCurlItem->pCurl); - if(pItem->pCurlItem->onRspCb) - { - pItem->pCurlItem->onRspCb(NULL, - pItem->pCurlItem->dlSize, - pItem->pCurlItem->pReqUrl, - pItem->pCurlItem->sPath, - pItem->pCurlItem->pTaskUuid, - -CURLE_OPERATION_TIMEDOUT, - pItem->pCurlItem->pData); - } - } - } - - return (0); -} - -static size_t __uploadCb(char *d, size_t n, size_t l, void *p) -{ - return n*l; -} - -#ifdef LIBCURL_DEBUG -struct data { - char trace_ascii; /* 1 or 0 */ -}; - -static -void dump(const char *text, - FILE *stream, unsigned char *ptr, size_t size, - char nohex) -{ - size_t i; - size_t c; - - unsigned int width = 0x10; - - if(nohex) - /* without the hex output, we can fit more on screen */ - width = 0x40; - - fprintf(stream, "%s, %10.10ld bytes (0x%8.8lx)\n", - text, (long)size, (long)size); - - for(i = 0; i= 0x20) && (ptr[i + c]<0x80)?ptr[i + c]:'.'); - /* check again for 0D0A, to avoid an extra \n if it's at width */ - if(nohex && (i + c + 2 < size) && ptr[i + c + 1] == 0x0D && - ptr[i + c + 2] == 0x0A) { - i += (c + 3 - width); - break; - } - } - fputc('\n', stream); /* newline */ - } - fflush(stream); -} - -static -int my_trace(CURL *handle, curl_infotype type, - char *data, size_t size, - void *userp) -{ - struct data *config = (struct data *)userp; - const char *text; - (void)handle; /* prevent compiler warning */ - - switch(type) { - case CURLINFO_TEXT: - fprintf(stderr, "== Info: %s", data); - /* FALLTHROUGH */ - default: /* in case a new one is introduced to shock us */ - return 0; - - case CURLINFO_HEADER_OUT: - text = "=> Send header"; - break; - case CURLINFO_DATA_OUT: - text = "=> Send data"; - break; - case CURLINFO_SSL_DATA_OUT: - text = "=> Send SSL data"; - break; - case CURLINFO_HEADER_IN: - text = "<= Recv header"; - break; - case CURLINFO_DATA_IN: - text = "<= Recv data"; - break; - case CURLINFO_SSL_DATA_IN: - text = "<= Recv SSL data"; - break; - } - - dump(text, stderr, (unsigned char *)data, size, config->trace_ascii); - return 0; -} -#endif - -int InetHttpUploadFileSync(const char *pURL, const char* pPath, void* pAttachInfo) -{ - CURL *pCurl = NULL; - int rc = 0; - CURLcode ret; - struct curl_httppost *pPost = NULL, *pLastPtr = NULL; - -#ifdef LIBCURL_DEBUG - struct data config; - config.trace_ascii = 1; /* enable ascii tracing */ -#endif - - if(pURL == NULL || strlen(pURL) == 0) - { - LOG_EX(LOG_Error, "Url: %s(%p)\n", SAFE_STRING_VALUE(pURL), pURL); - return -ERR_INPUT_PARAMS; - } - - if(pPath == NULL || strlen(pPath) == 0) - { - LOG_EX(LOG_Error, "Url: %s(%p)\n", SAFE_STRING_VALUE(pPath), pPath); - return -ERR_INPUT_PARAMS; - } - - curl_formadd(&pPost, &pLastPtr, - CURLFORM_COPYNAME, "file", - CURLFORM_FILE, pPath, - CURLFORM_END); - - if(pAttachInfo) - { - PHTTP_POST_ATTACH pDevInfoArray = (PHTTP_POST_ATTACH)pAttachInfo; - PHTTP_POST_ATTACH pItem = NULL, pTmp = NULL; - - LL_FOREACH_SAFE(pDevInfoArray, pItem, pTmp) - { - curl_formadd(&pPost, &pLastPtr, - CURLFORM_COPYNAME, pItem->keyName, - CURLFORM_COPYCONTENTS, pItem->keyValue, - CURLFORM_END); - } - } - - pCurl = curl_easy_init(); - - if(pCurl == NULL) - { - LOG_EX(LOG_Error, "curl_easy_init() Error\n"); - return -ERR_MALLOC_MEMORY; - } - - curl_easy_setopt(pCurl, CURLOPT_ACCEPT_ENCODING, "gzip, deflate"); - curl_easy_setopt(pCurl, CURLOPT_POST, 1L); - curl_easy_setopt(pCurl, CURLOPT_URL, pURL); - curl_easy_setopt(pCurl, CURLOPT_HTTPPOST, pPost); - curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, __uploadCb); - curl_easy_setopt(pCurl, CURLOPT_CONNECTTIMEOUT, 10L); -#ifdef LIBCURL_DEBUG - curl_easy_setopt(pCurl, CURLOPT_DEBUGFUNCTION, my_trace); - curl_easy_setopt(pCurl, CURLOPT_DEBUGDATA, &config); -#endif - -#ifdef SKIP_PEER_VERIFICATION - /* - * If you want to connect to a site who isn't using a certificate that is - * signed by one of the certs in the CA bundle you have, you can skip the - * verification of the server's certificate. This makes the connection - * A LOT LESS SECURE. - * - * If you have a CA cert for the server stored someplace else than in the - * default bundle, then the CURLOPT_CAPATH option might come handy for - * you. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L); -#else - curl_easy_setopt(pCurl, CURLOPT_CAINFO, SSL_CA_FILE); - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L); -#endif - -#ifdef SKIP_HOSTNAME_VERIFICATION - /* - * If the site you're connecting to uses a different host name that what - * they have mentioned in their server certificate's commonName (or - * subjectAltName) fields, libcurl will refuse to connect. You can skip - * this check, but this will make the connection less secure. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0L); -#endif - - -#ifdef LIBCURL_DEBUG - curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 1L); -#else - curl_easy_setopt(pCurl, CURLOPT_VERBOSE, 0L); -#endif - - ret = curl_easy_perform(pCurl); - - if(ret != CURLE_OK) - { - LOG_EX(LOG_Error, "Upload %s File %s Error %s\n", pURL, pPath, curl_easy_strerror(ret)); - rc = -ERR_NETWORK_SEND; - } - - curl_easy_cleanup(pCurl); - curl_formfree(pPost); - - return rc; -} - -const char* InetHttpWebServicePostAsync(const char *pURL, const char* pPost, OnHttpResponse onRespCb, void* pData) -{ - uuid_t msgId; - char strMsgId[64]; - PHTTP_REQ_PARAMS pParams = (PHTTP_REQ_PARAMS)malloc(sizeof(HTTP_REQ_PARAMS)); - CURL *pCurl = NULL; - - if(pURL == NULL || strlen(pURL) == 0 || onRespCb == NULL) - { - free(pParams); - return (NULL); - } - - memset(pParams, 0, sizeof(HTTP_REQ_PARAMS)); - - pCurl = curl_easy_init(); - - pParams->onRspCb = onRespCb; - pParams->pReqUrl = (char *)malloc(strlen(pURL) + 1); - pParams->type = INET_HTTP_WEBSERVICE_POST; - pParams->dlSize = 0; - pParams->pData = pData; - pParams->pCurl = pCurl; - pParams->lastTm = 0; - - memset(pParams->pReqUrl, 0, strlen(pURL) + 1); - strcpy(pParams->pReqUrl, pURL); - - uuid_generate_random(msgId); - memset(strMsgId, 0, 64); - uuid_unparse_lower(msgId, strMsgId); - pParams->pTaskUuid = strdup(strMsgId); - - curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, __writeDataCb); - curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, pParams); - curl_easy_setopt(pCurl, CURLOPT_PRIVATE, pParams); - curl_easy_setopt(pCurl, CURLOPT_URL, pURL); - curl_easy_setopt(pCurl, CURLOPT_NOPROGRESS, 1L); - curl_easy_setopt(pCurl, CURLOPT_USERAGENT, "libcurl-agent/1.0"); - - if(pPost != NULL && strlen(pPost) > 0) - { - curl_easy_setopt(pCurl, CURLOPT_POSTFIELDS, pPost); - curl_easy_setopt(pCurl, CURLOPT_POSTFIELDSIZE, (long)strlen(pPost)); - } - -#ifdef SKIP_PEER_VERIFICATION - /* - * If you want to connect to a site who isn't using a certificate that is - * signed by one of the certs in the CA bundle you have, you can skip the - * verification of the server's certificate. This makes the connection - * A LOT LESS SECURE. - * - * If you have a CA cert for the server stored someplace else than in the - * default bundle, then the CURLOPT_CAPATH option might come handy for - * you. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 0L); -#else - curl_easy_setopt(pCurl, CURLOPT_CAINFO, SSL_CA_FILE); - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYPEER, 1L); -#endif - -#ifdef SKIP_HOSTNAME_VERIFICATION - /* - * If the site you're connecting to uses a different host name that what - * they have mentioned in their server certificate's commonName (or - * subjectAltName) fields, libcurl will refuse to connect. You can skip - * this check, but this will make the connection less secure. - */ - curl_easy_setopt(pCurl, CURLOPT_SSL_VERIFYHOST, 0L); -#endif - - curl_multi_add_handle(g_pCurl, pCurl); - - __addReqIdToTable(pParams->pTaskUuid, pParams); - - return (pParams->pTaskUuid); -} - -#if 0 -static void __curlTaskRuntimeCb(void *pParams) -{ - PCURL_HANDLE_TBL pItem = NULL, pTmpItem = NULL; - - while(TRUE) - { - uv_rwlock_rdlock(&g_uvHashRwLock); - - HASH_ITER(hh, g_ReqHandleTbl, pItem, pTmpItem) - { - if(pItem->pCurlItem->type == INET_HTTP_DOWNLOAD_FILE - && pItem->pCurlItem->lastTm > 0) - { - //unsigned int tmNow = LIBUV_CURRENT_TIME_S(); - - if(pItem->pCurlItem->lastTm > 0) - { - //curl_multi_cleanup(pItem->pCurlItem->pCurl); - curl_multi_remove_handle(g_pCurl, pItem->pCurlItem->pCurl); - - if(pItem->uRetryTimes >= 3) - { - if(pItem->pCurlItem->onRspCb) - { - if(strcmp(pItem->pCurlItem->sPath, pItem->pCurlItem->sDlPath) != 0) - { - unlink(pItem->pCurlItem->sDlPath); - } - - pItem->pCurlItem->onRspCb(NULL, - pItem->pCurlItem->dlSize, - pItem->pCurlItem->pReqUrl, - pItem->pCurlItem->sPath, - pItem->pCurlItem->pTaskUuid, - TRUE, - pItem->pCurlItem->pData); - } - - if(pItem->pCurlItem->pReqUrl) - { - free(pItem->pCurlItem->pReqUrl); - } - } - else - { - __restartDlFileAsync(pItem->pCurlItem); - } - } - } - } - uv_rwlock_rdunlock(&g_uvHashRwLock); - usleep(100000); - } -} -#endif - -static int __getUsernameFromMail(const char *pMailAddr, char **pUsername) -{ - char *pTail; - - if(pMailAddr == NULL || pUsername == NULL || strlen(pMailAddr) == 0) - { - LOG_EX(LOG_Error, "Input Params Error: pMailAddr = [%s], pUsername = %p\n", - pMailAddr ? pMailAddr : "NULL", pUsername); - return (-ERR_INPUT_PARAMS); - } - - *pUsername = (char *)malloc(strlen(pMailAddr) + 1); - - if(*pUsername == NULL) - { - LOG_EX(LOG_Error, "Error Malloc Memory\n"); - *pUsername = ""; - return (-ERR_MALLOC_MEMORY); - } - - memset(*pUsername, 0, strlen(pMailAddr) + 1); - - pTail = strchr(pMailAddr, '@'); - - if(pTail == NULL) - { - strcpy(*pUsername, pMailAddr); - } - else - { - memcpy(*pUsername, pMailAddr, pTail - pMailAddr); - } - - return (0); -} - -int InetSmtpSendEmail(const char* pFrom, - const char* pTo[], - const char* pCc[], - const char* pTitle, - const char* pMessage, - const char* pAttach[], - PSMTP_MAIL_CONFIG pConfig) -{ - const char *pErrMsg = NULL; - quickmail pMail; - - if(pConfig == NULL) - { - LOG_EX(LOG_Error, "Input Param pConfig = NULL\n"); - return (-ERR_INPUT_PARAMS); - } - - if(pConfig->pPassword == NULL || strlen(pConfig->pPassword) == 0) - { - LOG_EX(LOG_Error, "Input Param Error: pConfig->pPassword = [%s]\n", pConfig->pPassword ? pConfig->pPassword : "NULL"); - return (-ERR_INPUT_PARAMS); - } - - if(pConfig->pUserName == NULL || strlen(pConfig->pUserName) == 0) - { - LOG_EX(LOG_Error, "Input Param Error: pConfig->pUserName = [%s]\n", pConfig->pUserName ? pConfig->pUserName : "NULL"); - return (-ERR_INPUT_PARAMS); - } - - if(pConfig->pSmtpServer == NULL || strlen(pConfig->pSmtpServer) == 0) - { - LOG_EX(LOG_Error, "Input Param Error: pConfig->pUserName = [%s]\n", pConfig->pSmtpServer ? pConfig->pSmtpServer : "NULL"); - return (-ERR_INPUT_PARAMS); - } - - if(pFrom == NULL) - { - LOG_EX(LOG_Error, "Input Param pFrom = NULL\n"); - return (-ERR_INPUT_PARAMS); - } - - if(pTo == NULL && pCc == NULL) - { - LOG_EX(LOG_Error, "Input Param pTo = %p, pCc = %p\n", pTo, pCc); - return (-ERR_INPUT_PARAMS); - } - - if(pTitle == NULL) - { - pTitle = ""; - } - - if(pMessage == NULL) - { - pMessage = ""; - } - - quickmail_initialize(); - - pMail = quickmail_create(pFrom, pTitle); - - if(pMail == NULL) - { - LOG_EX(LOG_Error, "Create Quickmail Object Error\n"); - return (-ERR_MALLOC_MEMORY); - } - - for(const char **pValue = pTo; pTo && *pValue; pValue++) - { - quickmail_add_to(pMail, *pValue); - } - - for(const char **pValue = pCc; pCc && *pValue; pValue++) - { - quickmail_add_cc(pMail, *pValue); - } - - quickmail_add_header(pMail, "Importance: Low"); - quickmail_add_header(pMail, "X-Priority: 5"); - quickmail_add_header(pMail, "X-MSMail-Priority: Low"); - quickmail_add_body_memory(pMail, "text/html", (char*)pMessage, strlen(pMessage), 0); - - for(const char **pValue = pAttach; pAttach && *pValue; pValue++) - { - quickmail_add_attachment_file(pMail, *pValue, NULL); - } - - //quickmail_set_debug_log(pMail, stderr); - - pErrMsg = quickmail_send(pMail, pConfig->pSmtpServer, pConfig->smtpPort, pConfig->pUserName, pConfig->pPassword); - - if(pErrMsg != NULL) - { - LOG_EX(LOG_Error, "Send Mail Error: %s\n", pErrMsg); - return (-ERR_SEND_MAIL); - } - - return (0); -} - -int InetInit(void) -{ - int ret = 0; - //uv_thread_t uvThread; - - ret = curl_global_init(CURL_GLOBAL_ALL); - - if(ret != 0) - { - LOG_EX(LOG_Error, "curl init error: %d\n", ret); - return ret; - } - - g_pMainLoop = DBusLibuvGetRuntime()->pLoop; - - uv_timer_init(g_pMainLoop, &g_uvCurlTm); - uv_timer_init(g_pMainLoop, &g_uvDlTm); - - g_pCurl = curl_multi_init(); - - curl_multi_setopt(g_pCurl, CURLMOPT_SOCKETFUNCTION, __curlSockCb); - curl_multi_setopt(g_pCurl, CURLMOPT_TIMERFUNCTION, __curlTimerCb); - - uv_rwlock_init(&g_uvHashRwLock); - - uv_timer_start(&g_uvDlTm, __onDlTmoutCb, 1000, 1000); - - //uv_thread_create(&uvThread, __curlTaskRuntimeCb, NULL); - return (0); -} - -void InetUnInit(void) -{ - curl_multi_cleanup(g_pCurl); - curl_global_cleanup(); -} - diff --git a/Framework/SvrManager/svr_manager.c b/Framework/SvrManager/svr_manager.c index df0db17..2a15b1c 100644 --- a/Framework/SvrManager/svr_manager.c +++ b/Framework/SvrManager/svr_manager.c @@ -21,6 +21,7 @@ const char* g_ServerModeStr[] = "DEV_MODE", "TEST_MODE", "PUBLISH_MODE", + "PUBLISH_PREBUILD", "UNKNOWN_MODE" }; @@ -46,11 +47,13 @@ const char* g_VoiceKeyMap[VOICE_MAX][MAX_MODE] = { "vbox-dev", "vbox-dev", "vbox-online", + "vbox-online", }, { // VOICE_APP_SECRET "b1ec33c03df80ea3035bc9ccaa4af09c", "b1ec33c03df80ea3035bc9ccaa4af09c", "8714d6de1c83f21dda5fc9a905a59ac1", + "8714d6de1c83f21dda5fc9a905a59ac1", }, }; @@ -59,41 +62,49 @@ const char* g_ServerAddr[MAX_MODULE][MAX_MODE] = { "2e37bc56a9b7ec3f6b8f41f60b81eb92", "2e37bc56a9b7ec3f6b8f41f60b81eb92", "dbb00213c23ea3709aae12ceb4c4e54e", + "7fc939cdb26ec2fa343b7c47a0617190", }, { // VOICE_MODULE "ws://vbox-test.netease.com/netty/websocket", "ws://vbox-test.netease.com/netty3/websocket", "wss://vbox-asr.3.163.com/websocket", + "ws://vbox-test.netease.com/asr/websocket", }, { // VOICE_AI_MODULE "http://api.multimedia.netease.com/imgtest/yqbot31_8686/", "http://api.multimedia.netease.com/imgtest/yqbot29_8686/", "https://vbox-smart.3.163.com/ ", + "http://api.multimedia.netease.com/imgtest/yqbot30_8888/", }, { // SERVER_MODULE "http://api.multimedia.netease.com/imgtest/yqbot27_7677/", "http://api.multimedia.netease.com/imgtest/yqbot31_7676/", "https://vbox-server.3.163.com/", + "http://api.multimedia.netease.com/imgtest/yqbot22_7678/", }, { // LOG_MODULE "http://vbox-log-test.netease.com/logCollectDev/vbox/logCollect/uploadLog", "http://vbox-log-test.netease.com/logCollect/vbox/logCollect/uploadLog", "https://vbox-log.3.163.com/vbox/logCollect/uploadLog", + "http://vbox-log-test.netease.com/logCollectOnline/vbox/logCollect/uploadLog", }, { // MARK_POINT_MODULE "http://vbox-log-test.netease.com/buriedDev/vbox/log/add", "http://vbox-log-test.netease.com/buriedTest/vbox/log/add", "https://vbox-log.3.163.com/vbox/log/add", + "http://vbox-log-test.netease.com/buriedOnline/vbox/log/add", }, { // TTS_MODULE "http://api.openai.netease.com/vbox-tts-dev/vbox/tts/transform", "http://api.openai.netease.com/vbox-tts-test/vbox/tts/transform", "https://vbox-tts.3.163.com/vbox/tts/transform", + "http://api.openai.netease.com/vbox-tts-online/vbox/tts/transform", }, { // DC_MODULE "http://api.multimedia.netease.com/imgtest/yqbot27_7677/vbox/uploadFile", "http://api.multimedia.netease.com/imgtest/yqbot31_7676/vbox/uploadFile", "https://vbox-server.3.163.com/vbox/uploadFile", + "http://api.multimedia.netease.com/imgtest/yqbot22_7678/vbox/uploadFile", }, }; diff --git a/Framework/libuvEngine/libuv_dbus.c b/Framework/libuvEngine/libuv_dbus.c index f99ed88..e73264b 100644 --- a/Framework/libuvEngine/libuv_dbus.c +++ b/Framework/libuvEngine/libuv_dbus.c @@ -587,6 +587,8 @@ static int DBusOnMsgWorkAPICb(PDBUS_MSG_PACK pMsg) if(pWorkDayInfo && err == 0) { memcpy(&g_workDayArray, pWorkDayInfo, sizeof(WORKDAY_INFO)); + g_workDayArray.isReady = TRUE; + LOG_EX(LOG_Debug, "Sync Alarm Database: %s\n", pMsg->pMsg); } //LOG_EX2(LOG_Debug, "Database: %s\n", pMsg->pMsg); @@ -1463,7 +1465,7 @@ static void __waitUDISKMount(void) FILE* pFile; // wait system create setup status file - LOG_EX(LOG_Debug, "Wait boot status file create ......\n"); + fprintf(stdout, "Wait boot status file create ......\n"); while(access(pBootStatFile, F_OK) != 0) { usleep(10000); @@ -1473,11 +1475,11 @@ static void __waitUDISKMount(void) if(pFile == NULL) { - LOG_EX(LOG_Error, "Open boot status file error\n"); + fprintf(stdout, "Open boot status file error\n"); return; } - LOG_EX(LOG_Debug, "Wait boot status done ......\n"); + fprintf(stdout, "Wait boot status done ......\n"); // when UDISK mount, file /tmp/booting_state content is "done" do @@ -1492,10 +1494,47 @@ static void __waitUDISKMount(void) fclose(pFile); - LOG_EX(LOG_Debug, "Boot status done ......\n"); + fprintf(stdout, "Boot status done ......\n"); #endif } +int GetServerModeFromCC(int defValue, int* pErr) +{ + char* pSvrMode = NULL; + int iValue = defValue; + + GetShellExecResult("curl --silent http://localhost:1705/httpenv/999 | grep 'env:' | awk '{print $3}'", &pSvrMode); + + if(pSvrMode == NULL) + { + if(pErr) + { + *pErr = -ERR_NO_ITEMS; + } + return defValue; + } + + iValue = strtol(pSvrMode, NULL, 10); + free(pSvrMode); + + if(errno == EINVAL || errno == ERANGE) + { + if(pErr) + { + *pErr = -ERR_STR_CONVERT; + } + + return defValue; + } + + if(pErr) + { + *pErr = 0; + } + + return iValue; +} + DBusConnection* DBusWithLibuvInit(uv_loop_t* pUserLoop, const char* pBusName, OnDBusMessage cbOnMsg, @@ -1551,21 +1590,63 @@ DBusConnection* DBusWithLibuvInit(uv_loop_t* pUserLoop, puvFsReq = (uv_fs_t*)malloc(sizeof(uv_fs_t)); + // wait UDISK mount, configure file save in UDISK partition + __waitUDISKMount(); + CfgFileInit(); IHW_InitLOG(strrchr(pBusName, '.') + 1, NULL, TRUE); IHW_EnableLogLevel(LOG_Fatal | LOG_Error | LOG_Warn | LOG_Debug | LOG_Info, 1); - + IHW_RunLogService(); + APP_BUILD_INFO(strrchr(pBusName, '.') + 1, GetCurrentVersion()); + + i = 0; + do + { + svrMode = CfgGetIntValueV1("Global.ServerMode", PUBLISH_MODE, &ret); - // wait UDISK mount, configure file save in UDISK partition - __waitUDISKMount(); + if(ret != 0) + { + sleep(1); + } + else + { + usleep(1000); + } - svrMode = CfgGetIntValue("Global.ServerMode", PUBLISH_MODE); + LOG_EX(LOG_Debug, "ServerMode = %d, Error = %d\n", svrMode, ret); + } while (ret != 0 && i++ <= 3); + if(ret != 0) + { + LOG_EX(LOG_Error, "Read Server Mode Error: ret = %d\n", ret); + svrMode = CfgGetIntValueV2("ServerMode", PUBLISH_MODE, &ret); + + if(ret == 0) + { + LOG_EX(LOG_Warn, "Recovery Server Mode OK: ServerMode = %d\n", svrMode); + } + else + { + LOG_EX(LOG_Error, "CfgGetInvValueV2 Read Server Mode Error: ret = %d\n", ret); + svrMode = GetServerModeFromCC(PUBLISH_MODE, &ret); + + if(ret == 0) + { + LOG_EX(LOG_Warn, "Netease Controller Server Mode OK: ServerMode = %d\n", svrMode); + } + else + { + svrMode = PUBLISH_MODE; + LOG_EX(LOG_Error, "GetServerModeFromCC Read Server Mode Error: " + "ret = %d, Set to default: PUBLISH_MODE\n", ret); + } + } + } + SetCurrentServerMode(svrMode); DumpCurServerAddr("Default"); - #if USED_SHM_TO_DBUS uv_rwlock_init(&g_uvShmHashRwLock); @@ -1718,8 +1799,6 @@ DBusConnection* DBusWithLibuvInit(uv_loop_t* pUserLoop, } #endif - IHW_RunLogService(); - return pBus; } @@ -2007,6 +2086,11 @@ static int __reqWorkDayInfo(int year) reqInfo.year = year; } + if(reqInfo.year < (2018 - 1900)) + { + return 0; + } + ret = DBusJsonSendToCommand(NULL, g_pModInfoTable[MODULE_CONTROLLER].modAliase, CMD_WORKDAY_DB_REQ, @@ -2016,9 +2100,19 @@ static int __reqWorkDayInfo(int year) return (ret); } +int IsHolidayDBSynced(void) +{ + return g_workDayArray.isReady; +} + int CurrentIsWorkDay(int year, int day) { -// LOG_EX(LOG_Debug, "CurrentIsWorkDay: year = %d, day = %d\n", year, day); + static unsigned int i = 0; + + if(year != 0) + { + LOG_EX(LOG_Debug, "CurrentIsWorkDay: year = %d, day = %d\n", year, day); + } if(day > 365) { @@ -2029,7 +2123,10 @@ int CurrentIsWorkDay(int year, int day) if(g_workDayArray.year <= 0) { __reqWorkDayInfo(year); - LOG_EX(LOG_Error, "Unsync Database: year = %d\n", year); + if(i++ % 10 == 0) + { + LOG_EX(LOG_Error, "Unsync Database: year = %d\n", year); + } return (-ERR_UNINIT_ITEM); } diff --git a/Modules/Alarm/assistant.c b/Modules/Alarm/assistant.c index 040083a..e086684 100644 --- a/Modules/Alarm/assistant.c +++ b/Modules/Alarm/assistant.c @@ -25,14 +25,14 @@ #include #endif -#define SYNC_TIME_DELAY (5) +#define SYNC_TIME_DELAY (1) static PASSISTANT_ARRAY_INFO g_pAlarmArray = NULL, g_pRemaindArray = NULL, g_pSessionArray = NULL; static uv_rwlock_t g_uvListRwLock; -static unsigned char g_syncFlags; +static unsigned char g_syncFlags = 0; static unsigned int g_sessionId = 0; static void __onAlarmCb(unsigned int tmId, unsigned int status, void *pUserData); @@ -462,6 +462,12 @@ static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t *pLoop, DBusConnection *pConn, P switch(pMsg->busCmd) { case CMD_SESSION_ALARM_SYNC: + if(IsHolidayDBSynced() != TRUE) + { + LOG_EX(LOG_Warn, "CMD_SESSION_ALARM_SYNC: Wait sync holiday database, this is dirty data.\n"); + break; + } + pInfo = (PASSISTANT_SYNC_INFO)Json2Struct((const char *)pMsg->pMsg, JSON_ENGINE_ASSISTANT_SYNC_RSP, FALSE, &err); if(pInfo) @@ -487,6 +493,12 @@ static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t *pLoop, DBusConnection *pConn, P break; case CMD_ALARM_SYNC_RSP: + if(IsHolidayDBSynced() != TRUE) + { + LOG_EX(LOG_Warn, "CMD_ALARM_SYNC_RSP: Wait sync holiday database, this is dirty data.\n"); + break; + } + pInfo = (PASSISTANT_SYNC_INFO)Json2Struct((const char *)pMsg->pMsg, JSON_ENGINE_ASSISTANT_SYNC_RSP, FALSE, &err); if(pInfo) @@ -580,6 +592,12 @@ static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t *pLoop, DBusConnection *pConn, P break; case CMD_REMAIND_SYNC_RSP: + if(IsHolidayDBSynced() != TRUE) + { + LOG_EX(LOG_Warn, "CMD_REMAIND_SYNC_RSP: Wait sync holiday database, this is dirty data.\n"); + break; + } + pInfo = (PASSISTANT_SYNC_INFO)Json2Struct((const char *)pMsg->pMsg, JSON_ENGINE_ASSISTANT_SYNC_RSP, FALSE, &err); if(pInfo) @@ -705,12 +723,73 @@ static void __dBusDeameonCb(MODULE_NAME modName, int status) static void __assistantSyncThread(void *pParam) { - int ret, isSynced = -1; + int ret, i = 0, isSynced = -1; LOG_EX(LOG_Debug, "Beging Sync holiday Database\n"); + while(TRUE) + { + if(IsHolidayDBSynced() != TRUE) + { + CurrentIsWorkDay(0, 0); + if(i++ % 10 == 0 && i <= 1000) + { + LOG_EX(LOG_Debug, "Retry sync holiday database: %d\n", i); + } + sleep(SYNC_TIME_DELAY); + } + else + { + break; + } + } + LOG_EX(LOG_Debug, "Sync holiday Database Finished\n"); + LOG_EX(LOG_Debug, "Beging sync ASSISTANT_TYPE_CLOCK\n"); do { + if(g_syncFlags & (1 << ASSISTANT_TYPE_CLOCK)) + { + LOG_EX(LOG_Debug, "Sync ASSISTANT_TYPE_CLOCK Finished\n"); + break; + } + + ret = DBusSendToCommand(NULL, + g_pModInfoTable[MODULE_CONTROLLER].modAliase, + CMD_ALARM_SYNC_REQ, ""); + + LOG_EX(LOG_Debug, "Send CMD_ALARM_SYNC_REQ Command: %d\n", ret); + + sleep(SYNC_TIME_DELAY); + } while(TRUE); + + LOG_EX(LOG_Debug, "Beging sync ASSISTANT_TYPE_REMAIND\n"); + do + { + if(g_syncFlags & (1 << ASSISTANT_TYPE_REMAIND)) + { + LOG_EX(LOG_Debug, "Sync ASSISTANT_TYPE_REMAIND Finished\n"); + break; + } + + ret = DBusSendToCommand(NULL, + g_pModInfoTable[MODULE_CONTROLLER].modAliase, + CMD_REMAIND_SYNC_REQ, ""); + + LOG_EX(LOG_Debug, "Send CMD_REMAIND_SYNC_REQ Command: %d\n", ret); + + sleep(SYNC_TIME_DELAY); + } while(TRUE); + +#if 0 + do + { + if((g_syncFlags & (1 << ASSISTANT_TYPE_CLOCK)) + && (g_syncFlags & (1 << ASSISTANT_TYPE_REMAIND))) + { + LOG_EX(LOG_Debug, "Sync Alarm And Remaind Finished\n"); + break; + } + if((g_syncFlags & (1 << ASSISTANT_TYPE_CLOCK)) == 0) { ret = DBusSendToCommand(NULL, @@ -726,18 +805,13 @@ static void __assistantSyncThread(void *pParam) g_pModInfoTable[MODULE_CONTROLLER].modAliase, CMD_REMAIND_SYNC_REQ, ""); -// LOG_EX(LOG_Debug, "Send CMD_REMAIND_SYNC_REQ Command: %d\n", ret); + LOG_EX(LOG_Debug, "Send CMD_REMAIND_SYNC_REQ Command: %d\n", ret); } - if(isSynced < 0) - { - isSynced = CurrentIsWorkDay(0, 0); - } - sleep(SYNC_TIME_DELAY); - } while(g_syncFlags != ((1 << ASSISTANT_TYPE_CLOCK) | (1 << ASSISTANT_TYPE_REMAIND))); + } while(TRUE); + //while(g_syncFlags != ((1 << ASSISTANT_TYPE_CLOCK) | (1 << ASSISTANT_TYPE_REMAIND))); - LOG_EX(LOG_Debug, "Sync holiday Database Finished\n"); ret = DBusSendToCommand(NULL, g_pModInfoTable[MODULE_CONTROLLER].modAliase, @@ -750,6 +824,7 @@ static void __assistantSyncThread(void *pParam) CMD_REMAIND_SYNC_REQ, ""); LOG_EX(LOG_Debug, "Send CMD_REMAIND_SYNC_REQ Command: %d\n", ret); +#endif pthread_detach(pthread_self()); } @@ -775,8 +850,7 @@ int main(int argc, char **argv) uv_rwlock_init(&g_uvListRwLock); AlarmTimerInit(pLoop); - - g_syncFlags = 0; + uv_thread_create(&uvSyncThread, __assistantSyncThread, NULL); RunUVLoop(pLoop); diff --git a/Modules/OTA/ota.c b/Modules/OTA/ota.c index 953bb07..eeaf2ef 100644 --- a/Modules/OTA/ota.c +++ b/Modules/OTA/ota.c @@ -1010,6 +1010,7 @@ static void __otaDownloadImageCb(void *pParams) __otaRspStatus(OTA_DOWNLOAD_FILE, dlInfo.retCode); pthread_detach(pthread_self()); + free(pInfo); return; } @@ -1149,7 +1150,6 @@ static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t* pLoop, DBusConnection* pConn, P if(pInfo == NULL) { __otaRspStatus(OTA_ERR_CODE, -ERR_INPUT_PARAMS); - free(pInfo); } else { @@ -1203,6 +1203,7 @@ static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t* pLoop, DBusConnection* pConn, P g_IsOTAMode = FALSE; LOG_EX(LOG_Error, "Error: Not Enought Space, Memory Free %llu bytes\n", uMemFreeSize); __otaRspStatus(OTA_DISK_FULL, pInfo->otaCmd); + free(pInfo); break; } else diff --git a/include/libuv_dbus.h b/include/libuv_dbus.h index 1b8f350..ef1a9c7 100644 --- a/include/libuv_dbus.h +++ b/include/libuv_dbus.h @@ -39,6 +39,7 @@ typedef struct typedef struct { + int isReady; int year; unsigned char days[366]; } WORKDAY_INFO, *PWORKDAY_INFO; @@ -202,6 +203,7 @@ unsigned int AlarmTimerAdd(int year, int *pError); int CurrentIsWorkDay(int year, int day); +int IsHolidayDBSynced(void); const char* DumpTimerRepeatModeString(int mode); unsigned long long GetPartitionFreeSize(const char *pPartPath); WIFI_STATUS GetCurrWIFIConnStatus(void); @@ -215,6 +217,9 @@ char* GetCurrentVersion(void); void CfgFileInit(void); int CfgGetIntValue(const char* pTags, int defValue); +int CfgGetIntValueV1(const char* pTags, int defValue, int* pErr); +int CfgGetIntValueV2(const char* pTags, int defValue, int* pErr); +int GetServerModeFromCC(int defValue, int* pErr); char* CfgGetStringValue(const char* pTags, char* pDefValue); double CfgGetFloatValue(const char* pTags, double defValue); int CfgGetBoolValue(const char* pTags, int defValue); diff --git a/include/server_addr.h b/include/server_addr.h index 1a11667..9f5d473 100644 --- a/include/server_addr.h +++ b/include/server_addr.h @@ -19,9 +19,10 @@ typedef enum typedef enum { - DEV_MODE = 0, - TEST_MODE = 1, - PUBLISH_MODE = 2, + DEV_MODE = 0, + TEST_MODE = 1, + PUBLISH_MODE = 2, + PUBLISH_PREBUILD = 3, MAX_MODE } SERVER_MODE_TYPE; diff --git a/log/log.c b/log/log.c index bd4db80..ff4507e 100644 --- a/log/log.c +++ b/log/log.c @@ -120,7 +120,7 @@ typedef struct FILE *pLogFile; } LOG_PROCESS_INFO, *PLOG_PROCESS_INFO; -static int g_bEnableLog = TRUE; // 是否启用 Log 功能 +static int g_bEnableLog = FALSE; // 是否启用 Log 功能 static int g_bEnMailBackup = FALSE; static char* g_pEmailBox = NULL; static int g_bEnLogToFile = TRUE;