diff --git a/src/Example/main.c b/src/Example/main.c deleted file mode 100644 index 5a378ab..0000000 --- a/src/Example/main.c +++ /dev/null @@ -1,2318 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "libuv_dbus.h" -#include "crypto.h" -#include "json_struct.h" -#include "config_engine.h" -#include "ota.h" -#include "skins.h" -#include "assistant.h" -#include "inet_api.h" -#include "skins_res.h" -#include "server_addr.h" -#include "boardlink_iot.h" - -static MOD_INFO_TABLE g_ModInfo; -static uv_timer_t g_tmTest; -static uv_barrier_t g_tmSync; -static uv_loop_t* pUserLoop = NULL; -static int g_isExitLoop = 0; - -static void test_task_new(uv_thread_cb pCallback, void* pParams); - -static void OnCfgMsgCb(DBUS_CMD cmd, PCFG_ITEM pMsg, int err) -{ - fprintf(stdout, "Cmd = %d, Cfg = %s\n", cmd, pMsg->pKeyName); - - if(err != 0) - { - LOG_EX(LOG_Error, "Cmd %d Error: %d\n", cmd, err); - return; - } - - switch(cmd) - { - case CMD_CFG_GET_RSP: - CfgItemPrint("Add Configure: ", pMsg); - break; - } -} - -static void __dBusDeameonCb(MODULE_NAME modName, int status) -{ -#if 1 - if(status) - { - LOG_EX(LOG_Error, "Daemon(%d) Msg: [%s]\n", modName, status == 0 ? "Connect" : "Disconnect"); - } - else - { - LOG_EX(LOG_Info, "Daemon(%d) Msg: [%s]\n", modName, status == 0 ? "Connect" : "Disconnect"); - } -#endif - if(modName == MODULE_OTA && status == 0) - { - int ret = DBusSendToCommand(NULL, - g_pModInfoTable[MODULE_OTA].modAliase, - CMD_SYSTEM_STANDBY, "{\"status\" : 0}"); - - LOG_EX(LOG_Debug, "Send Standby Command: %d\n", ret); - } - - LOG_EX(LOG_Debug, "Set Exit Loop\n"); - g_isExitLoop = TRUE; -} - -static void KeyEventCb(uint16_t uType, uint16_t uKey, int32_t iValue) -{ - LOG_EX(LOG_Info, "type = %u, code = %u, value = %u\n", uType, uKey, iValue); -} - -static void __uvThreadOTA(void *pParams) -{ - OTA_DATA_INFO otaInfo; - POTA_DATA_INFO pInfo = NULL; - int ret; - int version = (int)(intptr_t)pParams; - - memset(&otaInfo, 0, sizeof(OTA_DATA_INFO)); - -#if 0 - if(version == 130) - { - otaInfo.version = 132; - strcpy(otaInfo.otaFileInfo.url, "http://10.241.0.70/tina/tina_r16_ota_132.tar.gz"); - strcpy(otaInfo.otaFileInfo.md5, "3bda58a2eb4e90fbb35b0d33db44cf57"); - otaInfo.otaFileInfo.size = 22966488; - } - else if(version == 132) - { - otaInfo.version = 133; - strcpy(otaInfo.otaFileInfo.url, "http://10.241.0.70/tina/tina_r16_ota_133.tar.gz"); - strcpy(otaInfo.otaFileInfo.md5, "c4085cb33d6dbd5ae5fdae948365023c"); - otaInfo.otaFileInfo.size = 22966524; - } - else - { - otaInfo.version = 4; - strcpy(otaInfo.otaFileInfo.url, "http://api.multimedia.netease.com/imgtest/ota/tina_r16_ota_133.tar.gz"); - strcpy(otaInfo.otaFileInfo.md5, "c4085cb33d6dbd5ae5fdae948365023c"); - otaInfo.otaFileInfo.size = 22963814; - } -#else - otaInfo.version = 0x00000004; - strcpy(otaInfo.otaFileInfo.url, "http://10.240.84.163/tina_r311_ota_34.tar.gz"); - strcpy(otaInfo.otaFileInfo.md5, "34c06f79806be978a328194fd883365d"); - otaInfo.otaFileInfo.size = 31581616; -#endif - otaInfo.otaCmd = OTA_CMD_DOWNLOAD; - otaInfo.otaMode = OTA_MODE_FORCE_NOW; - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_OTA].modAliase, - CMD_OTA_NOTIFY, - JSON_ENGINE_OTA_REQ, - &otaInfo, TRUE); - - LOG_EX(LOG_Debug, "Send OTA Command: OTA_CMD_DOWNLOAD --> %d\n", ret); -} - -#if 0 -static void __radomAlarmItem(PASSISTANT_ITEM_INFO pInfo) -{ - static unsigned int alarmId = 0; - time_t tmVal = time(NULL); - struct tm *pTmNow = localtime(&tmVal); - - if(pInfo == NULL) - { - return; - } - - memset(pInfo, 0, sizeof(ALARM_ITEM_INFO)); - - pInfo->alarmId = __sync_fetch_and_add(&alarmId, 1); - pInfo->itemType = (random() % 2 == 0) ? ALARM_TYPE_REMAIND : ALARM_TYPE_CLOCK; - pInfo->priority = random() % 10; - pInfo->repeatMode = random() % (REPEAT_MODE_EVERY_YEAR_DAY + 1); - - pTmNow->tm_hour = random() % 24; - pTmNow->tm_min = random() % 60; - - //strftime(pInfo->strDateTime, MAX_DT_LEN, "%Y-%m-%d %H:%M", pTmNow); - sprintf(pInfo->strTips, "TTS %llu", pInfo->itemId); -} -#endif - -static PDBUS_MSG_PACK DBusOnMessage(uv_loop_t* pLoop, DBusConnection* pConn, PDBUS_MSG_PACK pMsg) -{ - int i, ret, err; - uint32_t tm = LIBUV_CURRENT_TIME_US(); - PCFG_API_RSP pRsp = NULL; - POTA_RSP_STATUS pOTAStatus = NULL; - //PALARM_RSP_STATUS pAlarmStatus = NULL; - //PALARM_SYNC_INFO pSyncInfo = NULL; - static unsigned int iCnt = 0; - - if(!pMsg || !pLoop || !pConn) - { - return NULL; - } - -#if 1 - if(iCnt++ % 1000 == 0) - { - LOG_EX(LOG_Info, "Receive: %s\n", pMsg->pMsg); - } -#endif - - //LOG_EX(LOG_Info, "Process Message(%u --> 0x%08X) at [%lu.%06lu]: cmd = %u, size = %u, key = %d, msg(%d) = [%s]\n", - // pMsg->msgSrc, pMsg->msgDests, tm / 1000000, tm % 1000000, pMsg->busCmd, pMsg->msgSize, pMsg->msgKey, pMsg->msgSize, pMsg->pMsg); - - switch(pMsg->busCmd) - { - case CMD_CALL_DIAL: - LOG_EX(LOG_Debug, "Recv: %s\n", pMsg->pMsg); - break; - - case CMD_MISC_OTA: - print_hex_dump_bytes("OTA", DUMP_PREFIX_ADDRESS, pMsg->pMsg, (pMsg->msgSize > 128) ? 128 : pMsg->msgSize); - break; - - case CMD_CFG_GET_RSP: - pRsp = (PCFG_API_RSP)Json2Struct(pMsg->pMsg, JSON_ENGINE_CFG_RSP, TRUE, &err); - - if(pRsp == NULL || err != 0) - { - LOG_EX(LOG_Error, "CMD_CFG_GET: pRsp = %p, err = %d\n", pRsp, err); - return (NULL); - } - - LOG_EX(LOG_Debug, "Get Configure: [%s](%d) --> \"%s\"\n", pRsp->keyName, pRsp->keyType, pRsp->keyValue); - - free(pRsp); - break; - - case CMD_OTA_STATUS: - pOTAStatus = (POTA_RSP_STATUS)Json2Struct(pMsg->pMsg, JSON_ENGINE_OTA_RSP, TRUE, &err); - - LOG_EX(LOG_Debug, "%s --> %d(0x%08X)\n", otaStatusName(pOTAStatus->status), pOTAStatus->val, pOTAStatus->val); - - if(pOTAStatus->status == OTA_CURRENT_VERSION) - { - test_task_new(__uvThreadOTA, (void*)(intptr_t)pOTAStatus->val); - } - - free(pOTAStatus); - break; - - case CMD_ALARM_SYNC_REQ: - LOG_EX(LOG_Debug, "Receive CMD_ALARM_SYNC_REQ Command\n"); - - if(iCnt++ > 0) - { - break; - } -#if 0 - const char *pAddCmd = "{\"data\":[{\"createTime\":1509419640000,\"id\":672,\"label\":1,\"type\":8,\"status\":1," - "\"updateTime\":1509419640000,\"vboxDate\":{\"dayofMonth\":\"17\",\"dayofWeek\":" - "\"1,2,3,4,5\",\"hour\":\"0\",\"minute\":\"0\",\"month\":\"11\",\"second\":\"10\",\"year\":\"\"}," - "\"vboxid\":\"0-52AEDB2DF9F45FEB\",\"voiceName\":\"音效2\",\"voiceType\":2}," - "{\"createTime\":1509419547000,\"id\":671,\"label\":0,\"type\":1,\"status\":1,\"updateTime\":" - "1509419547000,\"vboxDate\":{\"dayofMonth\":\"\",\"dayofWeek\":\"\",\"hour\":\"0\"," - "\"minute\":\"0\",\"month\":\"\",\"second\":\"10\",\"year\":\"\"},\"vboxid\":\"0-52AEDB2DF9F45FEB\"," - "\"voiceName\":\"音效2\",\"voiceType\":2}]}"; -#else -#if 0 - const char *pAddCmd = "{\"data\":[{\"id\":6734,\"vboxid\":\"0-D04CDF28BB622AF7\",\"label\":0," - "\"type\":1,\"voiceType\":1,\"voiceName\":\"音效1\",\"status\":2," - "\"updateTime\":1515576709000,\"createTime\":1515576709000," - "\"vboxDate\":{\"second\":\"0\",\"minute\":\"41\",\"hour\":\"18\"," - "\"dayofMonth\":\"10\",\"month\":\"1\",\"dayofWeek\":\"3\",\"year\":" - "\"2018\"}},{\"id\":6733,\"vboxid\":\"0-D04CDF28BB622AF7\",\"label\":0," - "\"type\":1,\"voiceType\":1,\"voiceName\":\"音效1\",\"status\":2," - "\"updateTime\":1515576701000,\"createTime\":1515576701000,\"vboxDate\":" - "{\"second\":\"0\",\"minute\":\"42\",\"hour\":\"18\",\"dayofMonth\":\"10\"," - "\"month\":\"1\",\"dayofWeek\":\"3\",\"year\":\"2018\"}}]}"; -#else - const char *pAddCmd = "{\"data\":[{\"id\":8136,\"vboxid\":\"0-ADC8ED8631B2032D\",\"label\":0," - "\"type\":3,\"voiceType\":1,\"voiceName\":\"音效1\",\"status\":2," - "\"updateTime\":1520841240000,\"createTime\":1520841240000," - "\"vboxDate\":{\"second\":\"\",\"minute\":\"56\",\"hour\":\"9\"," - "\"dayofMonth\":\"13\",\"month\":\"3\",\"dayofWeek\":\"2\"," - "\"year\":\"2018\"}}]}"; -#endif -#endif -#if 0 - ALARM_SYNC_INFO alarmInfo; - ALARM_REMOVE_INFO rmInfo; - - memset(&alarmInfo, 0, sizeof(ALARM_SYNC_INFO)); - memset(&rmInfo, 0, sizeof(ALARM_REMOVE_INFO)); - - alarmInfo.nItems = 10; - //alarmInfo.cmd = CMD_ALARM_SYNC_RSP; - alarmInfo.pAlarmInfo = (PALARM_ITEM_INFO)malloc(sizeof(ALARM_ITEM_INFO) * 10); - - for(i = 0; i < 10; i++) - { - __radomAlarmItem(&alarmInfo.pAlarmInfo[i]); - } - - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_ALARM].modAliase, - CMD_ALARM_SYNC_RSP, - JSON_ENGINE_ALARM_SYNC_RSP, - &alarmInfo, TRUE); -#endif - ret = DBusSendToCommand(NULL, - g_pModInfoTable[MODULE_ALARM].modAliase, - CMD_ALARM_SYNC_RSP, - pAddCmd); - - LOG_EX(LOG_Debug, "Send Alarm Command: JSON_ENGINE_ALARM_SYNC_RSP --> %d\n", ret); -#if 0 - rmInfo.nItems = 3; - - for(i = 0; i < 3; i++) - { - // rmInfo.rmItemId[i] = alarmInfo.pAlarmInfo[i * 2].alarmId; - } - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_ALARM].modAliase, - CMD_ALARM_REMOVE, - JSON_ENGINE_ALARM_REMOVE, - &rmInfo, TRUE); - - LOG_EX(LOG_Debug, "Send Alarm Command: JSON_ENGINE_ALARM_SYNC_RSP --> %d\n", ret); - - free(alarmInfo.pAlarmInfo); -#endif - break; -#if 0 - case CMD_ALARM_STATUS: - //LOG_EX(LOG_Debug, "CMD_ALARM_STATUS: %s\n", (const char *)pMsg->pMsg); - pAlarmStatus = (PALARM_RSP_STATUS)Json2Struct(pMsg->pMsg, JSON_ENGINE_ALARM_STATUS, TRUE, &err); - LOG_EX(LOG_Debug, "%s --> %d(0x%08X)\n", alarmStatusName(pAlarmStatus->cmd), pAlarmStatus->val, pAlarmStatus->val); - - free(pAlarmStatus); - break; - - case CMD_ALARM_NOTIFY_EVT: - //LOG_EX(LOG_Debug, "CMD_ALARM_NOTIFY_EVT: %s\n", (const char *)pMsg->pMsg); - pSyncInfo = (PALARM_SYNC_INFO)Json2Struct((const char *)pMsg->pMsg, JSON_ENGINE_ALARM_SYNC_RSP, TRUE, &err); - - if(pSyncInfo) - { - __printAlarmSyncInfo("CMD_ALARM_NOTIFY_EVT", pSyncInfo); - free(pSyncInfo); - } - - break; -#endif - default: - break; - } - - return NULL; -} - -static unsigned int g_Delay = 1000; -static void uvSndLessCb(uv_timer_t* pTimer) -{ -#if 0 - DBusConnection* pBus = (DBusConnection*)pTimer->data; - unsigned char* pData = (unsigned char*)malloc(2048); - - memset(pData, 0x3f, 2048); - *pData = 0; - - if(g_ModInfo.modName != MODULE_CONTROLLER) - { - DBusSendToCommand(pBus, g_pModInfoTable[MODULE_CONTROLLER].modAliase, CMD_MISC_OTA, pData, 2048); - } - - free(pData); -#endif - LOG_EX(LOG_Info, "Timer Runing delay = %u ....\n", g_Delay); - uv_barrier_wait(&g_tmSync); -// AESEncrypto(); - -} - -#define TEST_AES_LEN (93) - -void test_evp_aes(void) -{ - int i; - unsigned char enData[ALIGN_AES_BLOCK(TEST_AES_LEN)]; - unsigned char* pData = (unsigned char*)malloc(ALIGN_AES_BLOCK(TEST_AES_LEN)); - int outSize = 0; - char* pKey = "0123456789abcedf"; - - for(i = 0; i < TEST_AES_LEN; i++) - { - enData[i] = i % 0xFF; - } - - strcpy(enData, "hello world"); - - fprintf(stdout, "EvpAESEncrypto: %s\n", - EvpAESEncrypto(enData, TEST_AES_LEN, pData, &outSize, (unsigned char*)pKey) == 0 ? "Success" : "Fail"); - - print_hex_dump_bytes("en_", 2, pData, outSize); - fprintf(stdout, "enc: %s --> %s\n",enData, EvpBase64EncodeNoAlignV2(pData, outSize)); - - memset(enData, 0, ALIGN_AES_BLOCK(TEST_AES_LEN)); - - fprintf(stdout, "EvpAESDecrypto: %s\n", - EvpAESDecrypto(pData, outSize, enData, &outSize, (unsigned char*)pKey) == 0 ? "Success" : "Fail"); - - fprintf(stdout, "dec:%s\n", (char*)enData); - free(pData); -} - -static void uvTimeoutCb(uv_timer_t* pTimer) -{ - PCFG_ITEM pItem = NULL; - - int ret = 0; - - if((ret = CfgGetKeyValue("Age", &pItem)) == 0) - { - CfgItemPrint("Get KeyValue: ", pItem); - } - else - { - LOG_EX(LOG_Error, "Get Key [Age] Error: %d\n", ret); - } -} - -static void EVPEncryptCb(CRYPTO_TYPE type, - const unsigned char* pData, - int iSize, - const unsigned char* pSrcData, - int iError) -{ - int i = 0; - - if(iError == 0) - { - if(type == CRYPTO_AES_ENCRYPT) - { - char* pKey = "xajhuang12345678"; - unsigned char* enData = (unsigned char*)malloc(ALIGN_AES_BLOCK(TEST_AES_LEN)); - - memset(enData, 0, ALIGN_AES_BLOCK(TEST_AES_LEN)); - - print_hex_dump_bytes("AES_Encrypt_", DUMP_PREFIX_ADDRESS, pData, iSize); - - i = EvpAddCryptoTask(CRYPTO_AES_DECRYPT, (unsigned char*)pData, iSize, enData, pKey, EVPEncryptCb); - - if(i != 0) - { - free(enData); - LOG_EX(LOG_Error, "EvpAddCryptoTask Error: %d\n", i); - } - } - else if(type == CRYPTO_AES_DECRYPT) - { - print_hex_dump_bytes("AES_Decrypt_", DUMP_PREFIX_ADDRESS, pData, iSize); - } - else if(type == CRYPTO_BASE64_ENCODE) - { - int ret = 0; - - LOG_EX(LOG_Info, "Base64(%d):[\n%s]\n", iSize, (char*)pData); - ret = EvpAddCryptoTask(CRYPTO_BASE64_DECODE, (unsigned char*)pData, iSize, NULL, NULL, EVPEncryptCb); - - if(ret != 0) - { - LOG_EX(LOG_Error, "EvpAddCryptoTask Error: %d\n", ret); - } - } - else if(type == CRYPTO_BASE64_DECODE) - { - LOG_EX(LOG_Info, "Base64(%d):[\n%s]\n", iSize, (char*)pData); - } - else if(type == CRYPTO_MD5_FILE) - { - LOG_EX(LOG_Info, "%s MD5 value [%s]\n", (char*)pSrcData, (char*)pData); - } - } - else - { - LOG_EX(LOG_Error, "pData = %p, iError = %d\n", pData, iError); - } - - free((void*)pData); -} - -static void test_aes_async(void) -{ - int i; - unsigned char enData[ALIGN_AES_BLOCK(TEST_AES_LEN)]; - unsigned char* pData = (unsigned char*)malloc(ALIGN_AES_BLOCK(TEST_AES_LEN)); - int outSize = 0; - char* pKey = "xajhuang12345678"; - - for(i = 0; i < TEST_AES_LEN; i++) - { - enData[i] = i % 0xFF; - } - - i = EvpAddCryptoTask(CRYPTO_BASE64_ENCODE, enData, TEST_AES_LEN, pData, pKey, EVPEncryptCb); - - if(i != 0) - { - LOG_EX(LOG_Error, "EvpAddCryptoTask Error: %d\n", i); - free(pData); - } - -} - -static void test_base64_async(void) -{ - int ret = 0; -#if 1 - const char* pSrc = "Wikiversity is a Wikimedia Foundation project devoted to learning resources, learning projects, \ -and research for use in all levels, types, and styles of education from pre-school to university, including professional \ -training and informal learning. We invite teachers, students, and researchers to join us in creating open educational \ -resources and collaborative learning communities. \ -This page is an index of all Wikiversity help and maintenance pages. The following articles contain guidance and \ -information about Editing, Policies and guidelines, and participating in the Wikiversity community. \ -Having trouble finding out what you need to know? If it is not covered by the FAQ, try asking at the Colloquium."; -#else - const char *pSrc = "http://www.164.com"; -#endif - const char *pBase64 = EvpBase64EncodeNoAlign(pSrc); - const char *pDecode = EvpBase64DecodeNoAlign(pBase64); - - fprintf(stdout, "Encode:\n%s\n", pBase64); - fprintf(stdout, "Decode:\n%s\n", pDecode); - -#if 0 - ret = EvpAddCryptoTask(CRYPTO_BASE64_ENCODE, (unsigned char*)pSrc, strlen(pSrc), NULL, NULL, EVPEncryptCb); - - if(ret != 0) - { - LOG_EX(LOG_Error, "EvpAddCryptoTask Error: %d\n", ret); - } -#endif - -} - -static void test_md5file_async(void) -{ - int ret = 0; - const char* pFile = "/root/time.txt"; - - ret = EvpAddCryptoTask(CRYPTO_MD5_FILE, (unsigned char*)pFile, strlen(pFile), NULL, NULL, EVPEncryptCb); - - if(ret != 0) - { - LOG_EX(LOG_Error, "EvpAddCryptoTask Error: %d\n", ret); - } -} - -static void test_s2j_task(void) -{ - PLAYER_TO_CTRL plyCtl; - PPLAYER_TO_CTRL pPlyCtl = NULL; - int err; - const char* pJson = NULL; - - strcpy(plyCtl.musicUuid, "http://www.164.com"); - plyCtl.plySt = 1; - plyCtl.curPos = 12133; - plyCtl.duration = 321000; - - pJson = Struct2Json(&plyCtl, JSON_ENGINE_P2C, TRUE, &err); - - if(pJson != NULL) - { - LOG_EX(LOG_Debug, "Json:\n %s\n", pJson); - - pPlyCtl = (PPLAYER_TO_CTRL)Json2Struct(pJson, JSON_ENGINE_P2C, TRUE, &err); - - if(pPlyCtl != NULL) - { - LOG_EX(LOG_Debug, "Struct:\nmusicUuid: %s\nplySt: %d\ncurPos: %d\nduration: %d\n", - pPlyCtl->musicUuid, pPlyCtl->plySt, pPlyCtl->curPos, pPlyCtl->duration); - - free(pPlyCtl); - } - else - { - LOG_EX(LOG_Debug, "Json2Struct Test Error\n"); - } - - free((void*)pJson); - } - else - { - LOG_EX(LOG_Debug, "test error\n"); - } -} - -extern const char* __ota_Notify_REQ2Json(void *pData); -extern void* __json2OTA_Notify_REQ(const char *pJsonS); -static void __uvThreadTestOTA(void *pParam) -{ - int err = 0, i = 0; - int ret; - const char *pMD5 = NULL; - - char* strMenu[] = { - "Usage:\n", - - "OTA Cmd download : download\n", - "OTA Cmd local : local\n", - "OTA Cmd exec : exec\n", - "Batch Test : batch\n", - "Set Config : ready\n", - "Exit : quit\n", - }; - - while(TRUE) - { - char *pInput = NULL; - char *pSub = NULL; - - - for(i = 0; i < sizeof(strMenu) / sizeof(strMenu[0]); i++) - { - fprintf(stdout, "%s", strMenu[i]); - } - - pInput = readline("Please Choise:"); - - if(pInput == NULL) - { - continue; - } - - pSub = strtok(pInput, " "); - i = 0; - - while(pSub != NULL) - { - OTA_DATA_INFO otaInfo; - POTA_DATA_INFO pInfo = NULL; - - memset(&otaInfo, 0, sizeof(OTA_DATA_INFO)); - - otaInfo.version = 1; - - if(strcmp(pSub, "download") == 0) - { - otaInfo.otaCmd = OTA_CMD_DOWNLOAD; - otaInfo.otaMode = OTA_MODE_NORMAL; - otaInfo.otaFileInfo.size = 21484772; - strcpy(otaInfo.otaFileInfo.url, "http://10.241.0.70/tina/tina_r16_ota.tar.gz"); - strcpy(otaInfo.otaFileInfo.md5, "2d58366a0846f6345730e28b021816d0"); - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_OTA].modAliase, - CMD_OTA_NOTIFY, - JSON_ENGINE_OTA_REQ, - &otaInfo, TRUE); - - LOG_EX(LOG_Debug, "Send OTA Command %s: %d\n", pSub, ret); - } - else if(strcmp(pSub, "local") == 0) - { - otaInfo.otaCmd = OTA_CMD_USED_LOCAL_IMAGE; - otaInfo.otaMode = OTA_MODE_FORCE_NOW; - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_OTA].modAliase, - CMD_OTA_NOTIFY, - JSON_ENGINE_OTA_REQ, - &otaInfo, TRUE); - - LOG_EX(LOG_Debug, "Send OTA Command %s: %d\n", pSub, ret); - } - else if(strcmp(pSub, "exec") == 0) - { - otaInfo.otaCmd = OTA_CMD_EXEC; - otaInfo.otaMode = OTA_MODE_FORCE_NOW; - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_OTA].modAliase, - CMD_OTA_NOTIFY, - JSON_ENGINE_OTA_REQ, - &otaInfo, TRUE); - - LOG_EX(LOG_Debug, "Send OTA Command %s: %d\n", pSub, ret); - } - else if(strcmp(pSub, "batch") == 0) - { -#if 0 - int err; - POTA_DATA_INFO pInfo = NULL; - const char *pJson = NULL; -#endif - -#if 0 - pJson = Struct2Json(&otaInfo, JSON_ENGINE_OTA_REQ, TRUE, &err); - fprintf(stdout, "Json(%p->%d): \n%s\n", &otaInfo, err, pJson); - - - - pInfo = (POTA_DATA_INFO)Json2Struct(pJson, JSON_ENGINE_OTA_REQ, TRUE, &err); - if(pInfo) - { - fprintf(stdout, "JsonInfo: \n%s\n", __ota_Notify_REQ2Json(pInfo)); - } - -#else - pMD5 = EvpMD5HashBufV2((const unsigned char *)&otaInfo, sizeof(OTA_DATA_INFO)); - - err = 0; - for(i = 0; i < 1000000; i++) - { - POTA_DATA_INFO pOInfo = (POTA_DATA_INFO)malloc(sizeof(OTA_DATA_INFO)); - - memcpy(pOInfo, &otaInfo, sizeof(OTA_DATA_INFO)); - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_OTA].modAliase, - CMD_OTA_NOTIFY, - JSON_ENGINE_OTA_REQ, - pOInfo, TRUE); - - free(pOInfo); - - if(ret != 0) - { - err++; - break; - //fprintf(stderr, "ret = %d\n", ret); - } - - - - //fprintf(stdout, "send [%d]: %d\n", i, err); - //LOG_EX(LOG_Debug, "Send OTA Command: %d\n", ret); - //usleep(1000); - } - - fprintf(stdout, "%s\n", pMD5); - free((void*)pMD5); - //fprintf(stdout, "send [%d]: %d\n", i, err); -#endif - } - else if(strcmp(pSub, "ready") == 0) - { - ret = DBusSendToCommand(NULL, - g_pModInfoTable[MODULE_OTA].modAliase, - CMD_SYSTEM_STANDBY, "{\"status\" : 0}"); - - LOG_EX(LOG_Debug, "Send Standby Command: %d\n", ret); - } - else if(strcmp(pSub, "quit") == 0) - { - return; - } - - pSub = strtok(NULL, " "); - } - - free(pInput); - - usleep(1000); - } -} - -static void __uvThreadTimer(void *pParams) -{ - while(TRUE) - { - uv_barrier_init(&g_tmSync, 2); - - g_Delay += 1000; - uv_timer_stop(&g_tmTest); - LOG_EX(LOG_Info, "Re Start Timer %u...\n", g_Delay); - uv_timer_start(&g_tmTest, uvSndLessCb, g_Delay, 0); - - uv_barrier_wait(&g_tmSync); - uv_barrier_destroy(&g_tmSync); - - sleep(1); - } -} - -static void __uvThreadTestConfigure(void *pParams) -{ - int i = 0; - int ret; - - char* g_Menu[] = { - "Usage:\n", - "Get Config : get \n", - "Set Config : set \n", - "Change Config : change \n", - "Exit: quit\n", - }; - - while(TRUE) - { - char *pInput = NULL; - char *pSub = NULL; - - - for(i = 0; i < sizeof(g_Menu) / sizeof(g_Menu[0]); i++) - { - fprintf(stdout, "%s", g_Menu[i]); - } - - pInput = readline("Please Choise:"); - - if(pInput == NULL) - { - continue; - } - - pSub = strtok(pInput, " "); - i = 0; - - while(pSub != NULL) - { - if(strcmp(pSub, "get") == 0) - { - char* pKeyName = strtok(NULL, " "); - - if(pKeyName != NULL && strlen(pKeyName) > 0) - { - CFG_ITEM* pCfg; - ret = CfgGetKeyValue(pKeyName, &pCfg); - - if(ret == 0) - { - CfgItemPrint("GetLocalCfg: ", pCfg); - } - else if(ret == -ERR_CFG_WAIT_RSP) - { - LOG_EX(LOG_Debug, "Wait for server response\n"); - } - else - { - LOG_EX(LOG_Error, "Get Configure [%s] Error: %d\n", pKeyName, ret); - } - } - } - else if(strcmp(pSub, "set") == 0) - { - char* pKeyName = strtok(NULL, " "); - char *pKeyType = strtok(NULL, " "); - char* pKeyValue = strtok(NULL, " "); - char* pSaveTo = strtok(NULL, " "); - - if(pKeyName && pKeyValue && pKeyType && pSaveTo - && strlen(pKeyName) > 0 && strlen(pKeyValue) > 0 && strlen(pKeyType) > 0 && strlen(pSaveTo) > 0) - { - CFG_ITEM item; - - item.pKeyName = pKeyName; - item.keyType = strtoul(pKeyType, NULL, 10); - - if(item.keyType == CFG_TYPE_STRING) - { - item.pStrValue = pKeyValue; - } - else if(item.keyType == CFG_TYPE_INT) - { - item.intValue = strtol(pKeyValue, NULL, 10); - } - else if(item.keyType == CFG_TYPE_DOUBLE) - { - item.doubleValue = strtod(pKeyValue, NULL); - } - - ret = CfgAddKeyValue(pKeyName, &item, strtol(pSaveTo, NULL, 10)); - - if(ret == 0) - { - CfgItemPrint("Add Configure: ", &item); - } - else - { - LOG_EX(LOG_Error, "Add Configure [%s] Error: %d\n", pKeyName, ret); - } - } - } - else if(strcmp(pSub, "quit") == 0) - { - return; - } - - pSub = strtok(NULL, " "); - } - - usleep(1000); - } -} - -static void test_task_new(uv_thread_cb pCallback, void* pParams) -{ - uv_thread_t uvSyncThread; - - uv_thread_create(&uvSyncThread, pCallback, pParams); -} - -#define WAKE_CACHE_SIZE (10) - -static unsigned char g_AudioCache[WAKE_CACHE_SIZE]; -static unsigned int g_wtPos = 0; -static unsigned int g_tolCache = 0; - -static void __cacheCAEAudio(unsigned char* pBuf, unsigned int iSize) -{ - if(iSize > WAKE_CACHE_SIZE) - { - return; - } - - memmove(g_AudioCache, g_AudioCache + iSize, WAKE_CACHE_SIZE - iSize); - memcpy(g_AudioCache + WAKE_CACHE_SIZE - iSize, pBuf, iSize); -} - -static void test_move_data(void) -{ - -} - -static void __onAlarmCb(unsigned int tmId, unsigned int status, void *pUserData) -{ - LOG_EX(LOG_Debug, "Time %u On......\n", tmId); -} - -static int g_isExitApp = FALSE; - -static void uvExitLoopCb(uv_timer_t *pParams) -{ - DEBUG_CODE_LINE(); - uv_stop(GetDBusDefaultLoop()); -} - -static void __uvLoopExit(void *pParams) -{ - int ret = 0; - uv_timer_t tmExit; - uv_loop_t* pLoop = GetDBusDefaultLoop(); - DBusConnection* pBus = DBusWithLibuvInit(pLoop, g_ModInfo.modAliase, - DBusOnMessage, - __dBusDeameonCb, - NULL, //KeyEventCb, - &ret); - - if(pBus == NULL) - { - fprintf(stderr, "DBusWithLibuvInit Error: %d\n", ret); - return; - } - - DBusBoardcastCommand(NULL, 0x11, 10, "xajhuang"); - - //test_task_new(uvExitLoopCb, NULL); - uv_timer_init(pLoop, &tmExit); - uv_timer_start(&tmExit, uvExitLoopCb, 3000, 3000); - - DEBUG_CODE_LINE(); - //RunUVLoop(pLoop); - uv_run(pLoop, UV_RUN_DEFAULT); - //uv_loop_close(pLoop); - DEBUG_CODE_LINE(); - - g_isExitApp = TRUE; - return; -} - -static void __fs_event_handle_cb(uv_fs_event_t *pEvent, const char *pFileName, int events, int status) -{ - char path[MAX_PATH]; - size_t pathSize = MAX_PATH - 1; - - memset(path, 0, MAX_PATH); - uv_fs_event_getpath(pEvent, path, &pathSize); - - fprintf(stdout, "%s: %d, %d\n", path, events, status); -} - -static void __onPrgCb(const char *pReqUrl, const char *pTaskUuid, unsigned char uPercent, void *pUserData) -{ - static unsigned char preCnt = 0; - - if(preCnt != uPercent) - { - preCnt = uPercent; - LOG_EX(LOG_Debug, "[%s](%s): %u%%\n", pReqUrl, pTaskUuid, uPercent); - } - - if(uPercent > 50) - { - //InetCancelDownload(pTaskUuid); - } -} - -static unsigned int g_DlCount = 0; -static void __onDlCb(void *pData, unsigned int size, const char *pReqUrl, const char* pDlPath, const char *pTaskUuid, int iFinished, void *pUserData) -{ - if(g_DlCount > 0) - { - g_DlCount--; - } - - if(iFinished == 0) - { - LOG_EX(LOG_Debug, "Request(%s): [%s] --> [%s] Response: [%u] OK\n", pTaskUuid, pReqUrl, pDlPath, size); - } - else if(iFinished == 1) - { - LOG_EX(LOG_Error, "Request(%s): [%s] --> [%s] Response: [%u] Error\n", pTaskUuid, pReqUrl, pDlPath, size); - } - else - { - LOG_EX(LOG_Error, "Download Error Code: %d\n", iFinished); - } -#if 1 - if(iFinished != 0) - { - const char* pId = InetHttpDlFileAsync(pReqUrl, - pDlPath, - __onDlCb, - NULL,//__onPrgCb, - NULL); - - LOG_EX(LOG_Debug, "Download ID: %s\n", pId); - } -#endif -} - -static void __uvTestSkin(void *pParams) -{ - int tolItems = sizeof(g_SkinDefaultResTable) / sizeof(g_SkinDefaultResTable[0]); -#if 0 - char *pUpg = "[ \ - { \ - \"createTime\": 1510570174000, \ - \"enable\": 1, \ - \"id\": 3, \ - \"md5\": \"af065860c3667ca2508c283d660990f7\", \ - \"osType\": 0, \ - \"remark\": \"音效1文件\", \ - \"resourceName\": \"v111\", \ - \"resourceType\": 1, \ - \"resourceVersion\": \"v0.3.1\", \ - \"size\": 164460, \ - \"updateTime\": 1510570227000, \ - \"url\": \"http://vbox-resource.nos.netease.com/d627159f-bccd-4364-bccc-f196c2b83ffd.mp3\", \ - \"versionId\": 10, \ - \"versionName\": \"0.0.9\", \ - \"versionOsType\": \"0\" \ - } ]"; -#endif -#if 0 - static int i = 0; - - while(i++ < 100) - { - char *pKeyName = (char*)(g_SkinDefaultResTable[random() % tolItems].pKeyName); - char *pResPath = GetSkinsResource(pKeyName, NULL, NULL, NULL); - LOG_EX(LOG_Debug, "[%s] = {%s}\n", pKeyName, pResPath); - - free(pResPath); - sleep(3); - } -#else - for(int i = 0; i < tolItems; i++) - { - int tmUsed = 0; - struct timeval tmStart, tmEnd; - char *pKeyName = (char*)(g_SkinDefaultResTable[i].pKeyName); - char *pResPath = NULL; - - gettimeofday(&tmStart, NULL); - pResPath = GetSkinsResource(pKeyName, NULL, NULL, NULL); - gettimeofday(&tmEnd, NULL); - - tmUsed = (tmEnd.tv_sec * 1000000 + tmEnd.tv_usec) - (tmStart.tv_sec * 1000000 + tmStart.tv_usec); - LOG_EX(LOG_Debug, "(%d):[%s] = {%s}\n", tmUsed, pKeyName, pResPath); - - free(pResPath); - } -#if 0 - fprintf(stdout, "-----------------------\n"); - for(int i = 0; i < tolItems; i++) - { - int tmUsed = 0; - struct timeval tmStart, tmEnd; - char *pKeyName = (char*)(g_SkinDefaultResTable[i].pKeyName); - char *pResPath = NULL; - - gettimeofday(&tmStart, NULL); - pResPath = GetSkinsResource(pKeyName, NULL, NULL, NULL); - gettimeofday(&tmEnd, NULL); - - tmUsed = (tmEnd.tv_sec * 1000000 + tmEnd.tv_usec) - (tmStart.tv_sec * 1000000 + tmStart.tv_usec); - LOG_EX(LOG_Debug, "(%d):[%s] = {%s}\n", tmUsed, pKeyName, pResPath); - - free(pResPath); - } -#endif -#endif -#if 0 - SkinUpgrade(pUpg); - - sleep(5); - - GetSkinsResource("v111", NULL, NULL, NULL); -#endif -} - -static LOG_LEVEL g_logLevelInfo[] = { - LOG_Fatal, LOG_Error, LOG_Warn, LOG_Debug, LOG_Info, - LOG_Test, LOG_Call, LOG_Devp, LOG_Step, LOG_Unknown, LOG_All, LOG_Close - }; - -static void __uvLogTestProc(void *pParams) -{ - while(TRUE) - { - //SysPointMark("-----------------------------------------------------\n"); - - for(int i = 0; i < sizeof(g_logLevelInfo) / sizeof(g_logLevelInfo[0]); i++) - { - UT_string* pString = NULL; - - utstring_new(pString); - utstring_printf(pString, "This is LogLevel %s\n", LogLeveToString(g_logLevelInfo[i])); - //SysPointMark(utstring_body(pString)); - LOG_EX(g_logLevelInfo[i], "This is LogLevel %s\n", LogLeveToString(g_logLevelInfo[i])); - utstring_free(pString); - } - - sleep(1); - } -} - -static void __uvLogCtrlProc(void *pParams) -{ - int i = 0; - int ret; - - const char* g_Menu[] = { - "Usage: enable/disable [params1] [params2]\n" - " help\n", - " quit\n", - }; - - const char* pOptMenu[] = { - "\n operation:\n" - " |--------------------------------------------------------------------------|\n", - " | command | operation | params1 | params2 |\n", - " |--------------------------------------------------------------------------|\n", - " | enable | | | |\n", - " |----------| 0: Set log level | Log level | Unused |\n", - " | disable | | | |\n", - " |----------|---------------------|-----------------------|-----------------|\n", - " | enable | | | |\n", - " |----------| 1: Print to file | Unused | Unused |\n", - " | disable | | | |\n", - " |----------|---------------------|-----------------------|-----------------|\n", - " | enable | | | |\n", - " |----------| 2: Backup to email | Unused | Unused |\n", - " | disable | | | |\n", - " |----------|---------------------|-----------------------|-----------------|\n", - " | enable | | Log server ip address | Log server port |\n", - " |----------| 3: Send to network |-----------------------|-----------------|\n", - " | disable | | Unused | Unused |\n", - " |--------------------------------------------------------------------------|\n", - }; - - while(TRUE) - { - char *pInput = NULL; - char *pCmd = NULL; - - for(i = 0; i < sizeof(g_Menu) / sizeof(g_Menu[0]); i++) - { - fprintf(stdout, "%s", g_Menu[i]); - } - - pInput = readline("Enter Command:"); - - if(pInput == NULL) - { - continue; - } -#if 1 - pCmd = strtok(pInput, " "); - i = 0; - - while(pCmd != NULL) - { - if(strcmp(pCmd, "help") == 0) - { - for(i = 0; i < sizeof(g_pModInfoTable) / sizeof(g_pModInfoTable[0]); i++) - { - if(i == 0) - { - fprintf(stdout, " modName : %-2d --> %s\n", - i, ModuleNameToString(g_pModInfoTable[i].modName)); - } - else - { - fprintf(stdout, "%15s%-2d --> %s\n", "", - i, ModuleNameToString(g_pModInfoTable[i].modName)); - } - } - - for(i = 0; i < sizeof(g_logLevelInfo) / sizeof(g_logLevelInfo[0]); i++) - { - if(i == 0) - { - fprintf(stdout, "\n Log Level: %-2d --> %s\n", i, LogLeveToString(g_logLevelInfo[i])); - } - else - { - fprintf(stdout, "%15s%-2d --> %s\n", "", i, LogLeveToString(g_logLevelInfo[i])); - } - } - - for(i = 0; i < sizeof(pOptMenu) / sizeof(pOptMenu[0]); i++) - { - fprintf(stdout, "%s", pOptMenu[i]); - } - } - else if(strcmp(pCmd, "enable") == 0 - || strcmp(pCmd, "disable") == 0) - { - LOG_CFG_PROTOCOL logItem; - int iCmd, iMod; - char *pParams1 = NULL, *pParams2 = NULL; - char* pMod = strtok(NULL, " "); - char* pOperat = strtok(NULL, " "); - - if(pMod == NULL || strlen(pMod) == 0) - { - LOG_EX(LOG_Error, "Input error, see help\n"); - break; - } - - if(pOperat == NULL || strlen(pOperat) == 0) - { - LOG_EX(LOG_Error, "Input error, see help\n"); - break; - } - - iMod = strtol(pMod, NULL, 10); - - if(iMod < 0 || iMod >= MODULE_MAX) - { - LOG_EX(LOG_Error, "Input error: %s(%d)\n", pMod, iMod); - break; - } - - iCmd = strtol(pOperat, NULL, 10); - - memset(&logItem, 0, sizeof(LOG_CFG_PROTOCOL)); - - - - switch(iCmd) - { - case 0: - pParams1 = strtok(NULL, " "); - if(pParams1 && strlen(pParams1) > 0) - { - int logLevel = strtol(pParams1, NULL, 10); - - if(logLevel >= 0 && logLevel < sizeof(g_logLevelInfo) / sizeof(g_logLevelInfo[0])) - { - fprintf(stdout, "%s %d 0x%08X\n", pCmd, iCmd, g_logLevelInfo[logLevel]); - - logItem.cfgCmd = CMD_LOG_LEVEL; - logItem.iParams2 = (strcmp(pCmd, "disable") == 0) ? FALSE : TRUE; - logItem.iParams1 = g_logLevelInfo[logLevel]; - - DBusJsonSendToCommand(NULL, - g_pModInfoTable[iMod].modAliase, - CMD_LOG_CONFIG, - JSON_ENGINE_LOG_CFG_CMD, - &logItem, TRUE); - } - else - { - LOG_EX(LOG_Error, "Input error, see help\n"); - } - } - else - { - LOG_EX(LOG_Error, "Input error, see help\n"); - } - break; - - case 1: - logItem.cfgCmd = CMD_LOG_FILE; - logItem.iParams1 = (strcmp(pCmd, "disable") == 0) ? FALSE : TRUE; - DBusJsonSendToCommand(NULL, - g_pModInfoTable[iMod].modAliase, - CMD_LOG_CONFIG, - JSON_ENGINE_LOG_CFG_CMD, - &logItem, TRUE); - break; - - case 2: - logItem.cfgCmd = CMD_LOG_MAIL; - logItem.iParams1 = (strcmp(pCmd, "disable") == 0) ? FALSE : TRUE; - DBusJsonSendToCommand(NULL, - g_pModInfoTable[iMod].modAliase, - CMD_LOG_CONFIG, - JSON_ENGINE_LOG_CFG_CMD, - &logItem, TRUE); - break; - - case 3: - pParams1 = strtok(NULL, " "); - - logItem.cfgCmd = CMD_LOG_NETWORK; - if(pParams1 == NULL || strlen(pParams1) == 0) - { - - logItem.iParams1 = 0; - DBusJsonSendToCommand(NULL, - g_pModInfoTable[iMod].modAliase, - CMD_LOG_CONFIG, - JSON_ENGINE_LOG_CFG_CMD, - &logItem, TRUE); - } - else - { - pParams2 = strtok(NULL, " "); - - if(pParams2 != NULL && strlen(pParams2) > 0) - { - logItem.iParams1 = inet_addr(pParams1); - logItem.iParams2 = strtol(pParams2, NULL, 10); - DBusJsonSendToCommand(NULL, - g_pModInfoTable[iMod].modAliase, - CMD_LOG_CONFIG, - JSON_ENGINE_LOG_CFG_CMD, - &logItem, TRUE); - } - else - { - LOG_EX(LOG_Error, "Input error, pParams2 = %s\n", - SAFE_STRING_VALUE(pParams2)); - } - } - - break; - - default: - LOG_EX(LOG_Error, "Unknown operation(0-3) %s, see help\n", pOperat); - break; - } - -#if 0 - char* pKeyName = strtok(NULL, " "); - char *pKeyType = strtok(NULL, " "); - char* pKeyValue = strtok(NULL, " "); - char* pSaveTo = strtok(NULL, " "); - - if(pKeyName && pKeyValue && pKeyType && pSaveTo - && strlen(pKeyName) > 0 && strlen(pKeyValue) > 0 && strlen(pKeyType) > 0 && strlen(pSaveTo) > 0) - { - CFG_ITEM item; - - item.pKeyName = pKeyName; - item.keyType = strtoul(pKeyType, NULL, 10); - - if(item.keyType == CFG_TYPE_STRING) - { - item.pStrValue = pKeyValue; - } - else if(item.keyType == CFG_TYPE_INT) - { - item.intValue = strtol(pKeyValue, NULL, 10); - } - else if(item.keyType == CFG_TYPE_DOUBLE) - { - item.doubleValue = strtod(pKeyValue, NULL); - } - - ret = CfgAddKeyValue(pKeyName, &item, strtol(pSaveTo, NULL, 10)); - - if(ret == 0) - { - CfgItemPrint("Add Configure: ", &item); - } - else - { - LOG_EX(LOG_Error, "Add Configure [%s] Error: %d\n", pKeyName, ret); - } - } -#endif - } - else if(strcmp(pCmd, "quit") == 0) - { - return; - } - - pCmd = strtok(NULL, " "); - } -#endif - usleep(1000); - } -} - -static void __uvMsgSendTestProc(void *pParams) -{ - char buf[DBUS_MSG_MAX_PAD_SIZE]; - unsigned int i = 0; - - memset(buf, 'a', DBUS_MSG_MAX_PAD_SIZE - 1); - buf[DBUS_MSG_MAX_PAD_SIZE - 1] = 0; - - while(TRUE) - { - // memset(buf, 0, 256); - // sprintf(buf, "%u", i); - - fprintf(stdout, "Send Message: %u\n", i); - DBusSendToCommand(NULL, g_pModInfoTable[MODULE_CONTROLLER].modAliase, CMD_CALL_DIAL, buf); - i++; - sleep(3); - } -} - -static void __uvMsgRecvProc(void *pParams) -{ - - while(TRUE) - { - PDBUS_MSG_PACK pMsg = DBusGetMessage(); - PLIBUV_DBUS_PARAMS pInfo = DBusLibuvGetRuntime(); - - do - { - if(pMsg && pInfo) - { - //static PDBUS_MSG_PACK DBusOnMessage(uv_loop_t* pLoop, DBusConnection* pConn, PDBUS_MSG_PACK pMsg) - DBusOnMessage(pInfo->pLoop, pInfo->pBus, pMsg); - DBusMsgCleanup(pMsg); - } - - pMsg = DBusGetMessage(); - } while(pMsg && pInfo); - - - usleep(100); - } -} - -void test_dbus(void) -{ - DBusError error; - DBusConnection* pBus = NULL; - - dbus_error_init(&error); - - pBus = dbus_bus_get(DBUS_BUS_SYSTEM, &error); - - if (dbus_error_is_set(&error)) - { - fprintf(stdout, "dbus: Could not acquire the session bus\n"); - - dbus_error_free(&error); - return; - } - else - { - fprintf(stdout, "Create Session OK\n"); - } -} - -static unsigned int g_Count1 = 0; -static void __uvThreadRun(void *pParams) -{ - g_Count1++; - usleep(10); -} - -static void __uvThreadTestProc(void *pParams) -{ - LOG_EX(LOG_Debug, "Test Create Thread\n"); - - for(int i = 0; i < 1000; i++) - { - test_task_new(__uvThreadRun, NULL); - usleep(1); - } - - while(TRUE) - { - LOG_EX(LOG_Debug, "Current Cnt: %u\n", g_Count1); - sleep(1); - } -} - -#include "fifo.h" - -static DECLARE_KFIFO(g_TestFifo, unsigned char, 128); - -static void __fifoPut(void *pParams) -{ - int ret = 0; - static unsigned char val = 0; - while(TRUE) - { - for(int i = 0; i < 130; i++) - { - if(kfifo_avail(&g_TestFifo) < 1) - { - ret = kfifo_out(&g_TestFifo, NULL, 1); - } - - ret = kfifo_in_locked(&g_TestFifo, &val, 1); - - if(ret != 1) - { - LOG_EX(LOG_Error, "Add %d ret %d\n", val, ret); - } - val++; - } - - - usleep(10); - return; - } -} - -static void __fifoGet(void *pParams) -{ - unsigned char val[128]; - - while(TRUE) - { - int len = kfifo_len(&g_TestFifo); - - if(len > 0) - { - len = kfifo_out_locked(&g_TestFifo, val, len); - - print_hex_dump_bytes("", DUMP_PREFIX_OFFSET, val, len); - fprintf(stdout, "Peek %d\n", len); - } - - usleep(1000); - } -} - -static void __uvThreadTestFifo(void *pParams) -{ - INIT_KFIFO(g_TestFifo); - - test_task_new(__fifoPut, NULL); - sleep(3); - test_task_new(__fifoGet, NULL); -} - -#if 0 -static inline int round_up_pow_of_two(unsigned long x) -{ - x |= (x >> 1); - x |= (x >> 2); - x |= (x >> 4); - x |= (x >> 8); - x |= (x >> 16); - return x + 1; -} - -#define WAKE_CACHE_DATA_SIZE1 (1920000U) -#endif - -typedef struct -{ - char* mcuCmd; -} BYPASS_INFO, *PBYPASS_INFO; - -typedef struct -{ - BYPASS_INFO reported; -} SHADOW_STATE, *PSHADOW_STATE; - -typedef struct -{ - char* method; - int version; - SHADOW_STATE state; -} SHADOW_UPDATE, *PSHADOW_UPDATE; - -static const char* __shadow2Json(PSHADOW_UPDATE pInfo) -{ - cJSON *pRoot, *pStatus, *pReports; - - const char* pJsonS; - - pRoot = cJSON_CreateObject(); - - cJSON_AddNumberToObject(pRoot, "version", pInfo->version); - cJSON_AddItemToObject(pRoot, "method", cJSON_CreateString(pInfo->method)); - - pStatus = cJSON_CreateObject(); - pReports = cJSON_CreateObject(); - - cJSON_AddItemToObject(pRoot, "status", pStatus); - cJSON_AddItemToObject(pStatus, "reports", pReports); - cJSON_AddItemToObject(pReports, "mcuCmd", cJSON_CreateString(pInfo->state.reported.mcuCmd)); - - - pJsonS = cJSON_Print(pRoot); - - - cJSON_Delete(pRoot); - - return pJsonS; -} - -static void __dlThreadRuntimeCb(void *pParams) -{ - pthread_detach(pthread_self()); - pthread_exit(0); -} - -static void __uvThreadTest(void *pParams) -{ -#ifndef PLATFORM_CPU - for(int i = 0; i < 100; i++) - { - char buf[256]; - memset(buf, 0, 256); - sprintf(buf, "Send %d times\n", i); - //BL_SendMessage(buf, strlen(buf)); - sleep(1); - } -#endif -#if 0 - for(int i = 0; i < 100000; i++) - { - uv_thread_t uvThread; - int err = uv_thread_create(&uvThread, __dlThreadRuntimeCb, NULL); - - if(err != 0) - { - LOG_EX(LOG_Error, "Create Thread Error: %d(%d)\n", err, i); - return; - } - - if(i % 1000 == 0) - { - LOG_EX(LOG_Debug, "Create Thread: %d\n", i); - } - usleep(1000); - } -#endif -} - -static void __dlTestThread(void* p) -{ - static char* testURL[] = { - "http://vbox-resource.nos.netease.com/20180309192932533Alianwang003.mp3", - "http://vbox-resource.nos.netease.com/20180309192932594Alianwang004.mp3", - "http://vbox-resource.nos.netease.com/20180309192932882Alianwang005.mp3", - "http://vbox-resource.nos.netease.com/20180309192933094Alianwang006.mp3", - "http://vbox-resource.nos.netease.com/20180309192933355Alianwang007.mp3", - "http://vbox-resource.nos.netease.com/20180309192933571Alianwang008.mp3", - "http://vbox-resource.nos.netease.com/20180309192933805Alianwang009.mp3", - "http://vbox-resource.nos.netease.com/20180309192934929Ashiyong002.mp3", - "http://vbox-resource.nos.netease.com/20180309192935659Ashiyong003.mp3", - "http://vbox-resource.nos.netease.com/20180309192935729Ashiyong004.mp3", - "http://vbox-resource.nos.netease.com/20180309192936130Ashiyong005.mp3", - "http://vbox-resource.nos.netease.com/20180309192936192Ashiyong006.mp3", - "http://vbox-resource.nos.netease.com/20180309192936289S001.mp3", - "http://vbox-resource.nos.netease.com/20180309192936471S002.mp3", - "http://vbox-resource.nos.netease.com/20180309192937566S006.mp3", - "http://vbox-resource.nos.netease.com/20180309192937735S007.mp3", - "http://vbox-resource.nos.netease.com/20180309192937793S008.mp3", - "http://vbox-resource.nos.netease.com/20180309192938129S009.mp3", - "http://vbox-resource.nos.netease.com/20180309192938262S010.mp3", - "http://vbox-resource.nos.netease.com/20180309192938975S011.mp3", - "http://vbox-resource.nos.netease.com/20180309192933845Alianwang010.mp3", - "http://vbox-resource.nos.netease.com/20180309192933937Alianwang011.mp3", - "http://vbox-resource.nos.netease.com/20180309192934678Ashiyong001.mp3", - "http://vbox-resource.nos.netease.com/20180605170040224Alianwang012.mp3", - "http://vbox-resource.nos.netease.com/20180605170146178Alianwang014.mp3", - "http://vbox-resource.nos.netease.com/20180605170324285Alianwang013.mp3", - "http://vbox-resource.nos.netease.com/20180605165854636Alianwang002.mp3", - "http://vbox-resource.nos.netease.com/20180607095314669S004-13DB.mp3", - "http://vbox-resource.nos.netease.com/20180607095347590S0053DB.mp3", - "https://vbox-resource.nos-hz.163yun.com/20180625120349098S003+3.mp3", - }; - - //while(TRUE) - { - for(int i = 0; i < sizeof(testURL) / sizeof(testURL[2]); i++) - { - char buf[256]; - - g_DlCount++; - memset(buf, 0, 256); - sprintf(buf, "./dl_%d.mp3", i); - InetHttpDlFileAsync(testURL[i], - buf, - __onDlCb, - NULL,//__onPrgCb, - NULL); - } - - while(g_DlCount) - { - usleep(100000); - } - } -} - -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); -} - -#define MAX_PAYLOAD 1024 // maximum payload size -#define NETLINK_TEST 30 //自定义的协议 - -void test_netlink(void) -{ - struct sockaddr_nl src_addr, dest_addr; - struct nlmsghdr *nlh = NULL; //Netlink数据包头 - struct msghdr msg; - int sock_fd; - int retval; - struct iovec iov; - - sock_fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_TEST); - - if(sock_fd == -1) - { - LOG_EX(LOG_Error, "Init socket error: %s\n", strerror(errno)); - return; - } - - // To prepare binding - memset(&src_addr, 0, sizeof(src_addr)); - src_addr.nl_family = AF_NETLINK; - src_addr.nl_pid = getpid(); //A:设置源端端口号 - src_addr.nl_groups = 0; - - //Bind - retval = bind(sock_fd, (struct sockaddr*)&src_addr, sizeof(src_addr)); - if(retval < 0) - { - LOG_EX(LOG_Error, "bind failed: %s", strerror(errno)); - close(sock_fd); - return; - } - - // To orepare create mssage - nlh = (struct nlmsghdr *)malloc(NLMSG_SPACE(MAX_PAYLOAD)); - if(!nlh) - { - LOG_EX(LOG_Error, "malloc nlmsghdr error!\n"); - close(sock_fd); - return; - } - - memset(&dest_addr, 0, sizeof(dest_addr)); - dest_addr.nl_family = AF_NETLINK; - dest_addr.nl_pid = 0; //B:设置目的端口号 - dest_addr.nl_groups = 0; - nlh->nlmsg_len = NLMSG_SPACE(MAX_PAYLOAD); - nlh->nlmsg_pid = getpid(); //C:设置源端口 - nlh->nlmsg_flags = 0; - strcpy(NLMSG_DATA(nlh),"Hello you!"); //设置消息体 - - iov.iov_base = (void *)nlh; - iov.iov_len = NLMSG_SPACE(MAX_PAYLOAD); - - //Create mssage - memset(&msg, 0, sizeof(msg)); - msg.msg_name = (void *)&dest_addr; - msg.msg_namelen = sizeof(dest_addr); - msg.msg_iov = &iov; - msg.msg_iovlen = 1; - - retval = sendmsg(sock_fd, &msg, 0); - - if(retval == -1) - { - LOG_EX(LOG_Error, "sendmsg failed: %s", strerror(errno)); - } - - memset(nlh,0,NLMSG_SPACE(MAX_PAYLOAD)); - - while(TRUE) - { - retval = recvmsg(sock_fd, &msg, 0); - if(retval < 0) - { - printf("state<1"); - } - else - { - //LOG_EX(LOG_Debug, "Received message: %s\n",(char *) NLMSG_DATA(nlh)); - print_hex_dump_bytes("Msg", 0, NLMSG_DATA(nlh), retval); - } - } - - close(sock_fd); -} - -static void __uvThreadSysPointUpload(void *pParams) -{ - sleep(10); - while(TRUE) - { - printf("++++++++++++++++++++++++++++++++++\n"); - SysPointMarkUpload(); - printf("----------------------------------\n"); - sleep(60); - } -} - -#ifndef PLATFORM_CPU -static void __uvThreadNetlinkSend(void *pParams) -{ - char* pData = "0123456789abcdef"; - int lastSt = MSG_IN_DISCOVERY_MODE; - - for(int i = 0; i < 100; i++) - { - BL_IOT_MSG blMsg; - - memset(&blMsg, 0, sizeof(BL_IOT_MSG)); - strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS); - memcpy(blMsg.msgData, pData, strlen(pData)); - blMsg.msglen = strlen(pData); - blMsg.msgType = MSG_BYPASS_MODE; - LOG_EX(LOG_Debug, "Send Message : %d\n", i); - BL_SendBLMsg(MSG_GET_WLAN_INFO, NULL, 0); - - if(i % 10 == 0) - { - BL_SendBLMsg(lastSt, NULL, 0); - - if(lastSt == MSG_IN_DISCOVERY_MODE) - { - lastSt = MSG_OUT_DISCOVERY_MODE; - } - else - { - lastSt = MSG_IN_DISCOVERY_MODE; - } - } - - //BL_SendMessage((unsigned char*)&blMsg, BL_IOT_MSG_LEN(blMsg.msglen)); - - sleep(1); - } -} -#endif - -static int expectedId; - -static int nlCallback(void* msg, void* arg) -{ - struct ucred * p = (struct ucred *)msg; - LOG_EX(LOG_Debug, "receive message\n"); -} -#if 0 -void test_nl80211(void) -{ - int ret; - //allocate socket - nl_sock* sk = nl_socket_alloc(); - - //connect to generic netlink - genl_connect(sk); - - //find the nl80211 driver ID - expectedId = genl_ctrl_resolve(sk, "nl80211"); - - //attach a callback - nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM, - nlCallback, NULL); - - //allocate a message - nl_msg* msg = nlmsg_alloc(); - - nl80211_commands cmd = NL80211_CMD_GET_INTERFACE; - int ifIndex = if_nametoindex("wlan0"); - int flags = 0; - - // setup the message - genlmsg_put(msg, 0, 0, expectedId, 0, flags, cmd, 0); - - //add message attributes - NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifIndex); - - //send the messge (this frees it) - ret = nl_send_auto_complete(sk, msg); - - //block for message to return - nl_recvmsgs_default(sk); -} -#endif - - -int main(int argc, char **argv) -{ - int i, j, ret = 0; - DBusConnection* pBus = NULL; - uv_timer_t uvSndLess, uvSndMore; - uv_loop_t* pLoop = GetDBusDefaultLoop(); - int modIdx = -1; - char buf[256]; - - - struct tm tm; - - memset(&tm, 0, sizeof(struct tm)); - strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm); - strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm); - puts(buf); -#if 0 - 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+"); - - fprintf(stdout, "WAKE_CACHE_DATA_SIZE1(%u) --> %u\n", WAKE_CACHE_DATA_SIZE1, round_up_pow_of_two(WAKE_CACHE_DATA_SIZE1)); - - fprintf(stdout, "127 --> %u\n", roundup_pow_of_two(127)); - - fprintf(stdout, "3 --> %u\n", roundup_pow_of_two(3)); - - fprintf(stdout, "256 --> %u\n", roundup_pow_of_two(256)); - - fprintf(stdout, "1024 * 2 - 1 --> %u\n", roundup_pow_of_two(1024 * 2 - 1)); -#endif -#if 0 - fprintf(stdout, "Wait system reboot for 10s"); - ret = system("sync && ubus call system watchdog \'{\"stop\" : true}\'"); - - sleep(20); - - fprintf(stdout, "Wait WDT 20s End\n"); -#endif - -#if 0 - int fd = open("/dev/watchdog", O_WRONLY); - - if(fd == -1) - { - perror("Open WDT"); - } - - sleep(20); - - fprintf(stdout, "Wait WDT 20s\n"); -#endif - -#ifdef PLATFORM_R16 -#endif - - //fprintf(stdout, "uv_default_loop = %p\n", pLoop); - - if(argc <= 1) - { - fprintf(stderr, "usage: ./dbus-r16.exe <0-%d>\n", MODULE_MAX - 1); - return 0; - } - - modIdx = strtol(argv[1], NULL, 10); - - if(modIdx >= MODULE_MAX || modIdx < 0) - { - fprintf(stderr, "Bad params of %s, max value is %d\n", argv[1], MODULE_MAX - 1); - return 0; - } - - memcpy(&g_ModInfo, &g_pModInfoTable[modIdx], sizeof(MOD_INFO_TABLE)); - - pBus = DBusWithLibuvInit(pLoop, g_ModInfo.modAliase, - DBusOnMessage, - NULL, - NULL, - &ret); - - if(pBus == NULL) - { - fprintf(stderr, "DBusWithLibuvInit Error: %d\n", ret); - exit(1); - return 0; - } - - //SysPointMarkInit(NULL, -1, -1); - - //test_task_new(__uvThreadSysPointUpload, NULL); - - //test_netlink(); -#ifndef PLATFORM_CPU - //BL_Init(NULL); - - //L_SendMessage((unsigned char*)&blMsg, sizeof(BL_IOT_MSG)); - //test_netlink(); - //__uvThreadTimerV2(pLoop); - - //test_task_new(__uvThreadNetlinkSend, NULL); -#endif - - //DumpCurServerAddr("Default"); - -#if 0 - for(int i = 0; i < 1; i++) - { - char buf[256]; - - memset(buf, 0, 256); - - sprintf(buf, "dl_%d.bin", i); - - InetHttpDlFileAsync("http://10.240.84.163/tina_r16_ota_21.tar.gz", - //"https://vbox-resource.nos-hz.163yun.com/20180628092806976tina_r16_ota_32.tar.gz", - buf, - __onDlCb, - NULL,//__onPrgCb, - NULL); - } -#endif - //SetCurrentServerMode(DEV_MODE); - //DumpCurServerAddr("DEV_MODE"); - -#if 0 - SetCurrentServerMode(TEST_MODE); - DumpCurServerAddr("TEST_MODE"); - - SetCurrentServerMode(PUBLISH_MODE); - DumpCurServerAddr("PUBLISH_MODE"); -#endif - - //test_task_new(__uvThreadTimerV2, pLoop); - - //HttpPostLogFile("{}"); - - //SysPointMarkInit("123456", -1, -1); - - //SysPointMarkUpload(); -#if 0 - for(int i = 0; i < 1000; i++) - { - void* pMalloc = malloc(1024 * 1024); - - if(pMalloc == NULL) - { - LOG_EX(LOG_Error, "%d: Malloc Error\n", i); - } - else - { - LOG_EX(LOG_Debug, "%d: Malloc %p\n", i, pMalloc); - - memset(pMalloc, 0, 1024 * 1024); - } - - usleep(100000); - } -#endif - -#if 0 - g_pOnKevEvt = (uv_fs_event_t*)malloc(sizeof(uv_fs_event_t)); - - uv_fs_event_init(pLoop, g_pOnKevEvt); - - uv_fs_event_start(g_pOnKevEvt, run_command, "./1.txt", UV_FS_EVENT_RECURSIVE); -#endif - - //test_task_new(__dlTestThread, NULL); -#if 0 - if(modIdx == 20) - { - IHW_EnableLogLevel(LOG_All, 1); - test_task_new(__uvLogCtrlProc, NULL); - } - else if(modIdx == 19) - { - test_task_new(__uvLogTestProc, NULL); - } - else if(modIdx == 21) - { - test_task_new(__uvMsgSendTestProc, NULL); - } - else if(modIdx == 15) - { - test_task_new(__uvThreadTestProc, NULL); - } - else if(modIdx == 16) - { - test_task_new(__uvThreadTestFifo, NULL); - } -#endif - //LOG_EX(LOG_Debug, "Chip Serial: [%s]\n", GetCpuSerial()); - //LOG_EX(LOG_Debug, "Chip ID: [%s]\n", GetCpuChipId()); - - //LOG_BUF(LOG_Error, g_pModInfoTable, 64); - -// DBusWithLibuvCfgInit(OnCfgMsgCb); - -#if 0 - uv_timer_init(pLoop, &uvSndLess); - uvSndLess.data = pBus; - uv_timer_start(&uvSndLess, uvSndLessCb, 1000, 3000); -#endif - //uv_timer_init(pLoop, &uvSndMore); - //uv_timer_start(&uvSndMore, uvTimeoutCb, 3000, 10000); - //EvpEncrypto(); - //LOG_EX(LOG_Info, "Struct --> Json: [%s]\n", Struct2Json(NULL, 0)); - - //Json2Struct(NULL, 0); - - //free((void*)EvpBase64Encode(pSrc)); - - //LOG_EX(LOG_Debug, "Base") - //free((void*)EvpMD5HashFile("/root/time.txt")); - //test_evp_aes(); - //test_aes_async(); - //test_base64_async(); - //test_md5file_async(); - //test_task_new(__uvThreadTestConfigure); - //test_task_new(__uvThreadTestOTA, NULL); - //test_task_new(__uvThreadTimer, NULL); - //uv_timer_init(GetDBusDefaultLoop(), &g_tmTest); - //LOG_EX(LOG_Info, "Start Timer............\n"); - //uv_timer_start(&g_tmTest, uvSndLessCb, g_Delay, 0); - - //test_task_new(__uvThreadTimer, NULL); - //test_s2j_task(); - - //test_base64_async(); - - //test_move_data(); - - //AlarmTimerInit(pLoop); - -#if 0 - for(i = REPEAT_MODE_NONE; i <= REPEAT_MODE_HOLIDAY; i++) - { - { - AlarmTimerAdd2(2017 - 1900, 11 - 1, 28, - 13, 50, 30, - 0, - i, - NULL, 0, 0, NULL, NULL); - - AlarmTimerAdd2(2017-1900, 11-1, 28, - 23, 50, 30, - 0, - i, - NULL, 0, 1, NULL, NULL); - } - } -#endif -#if 0 - struct tm localTime; - time_t timeStamp = time((time_t*)NULL) + 30; - localtime_r(&timeStamp, &localTime); - - AlarmTimerAdd(2018-1900, 7-1, 19, - localTime.tm_hour, localTime.tm_min, localTime.tm_sec, - 0, - 3, - __onAlarmCb, 0, NULL, NULL); -#endif -#if 0 - for(i = REPEAT_MODE_NONE; i <= REPEAT_MODE_HOLIDAY; i++) - { - struct tm localTime; - - localtime_r(&timeStamp, &localTime); - - AlarmTimerAdd(2017-1900, 11-1, 29, - localTime.tm_hour, localTime.tm_min, localTime.tm_sec, - 0, - 1, - NULL, 0, i % 2, NULL, NULL); - } -#endif - - //InetSmtpSendEmail(); - -#if 1 -#if 1 - -#endif -#if 1 - if(modIdx == 18) - { - SkinInit(); - //SkinIsVerifyRes(TRUE); -#if 0 - char* path = "/mnt/UDISK/skinupgrade.txt"; - char* pUpgCmd = "[{\"createTime\":1517832224000,\"enable\":1,\"id\":170," - "\"md5\":\"0c8d2d487e485cf3553d7e2818ac42d9\",\"osType\":0," - "\"remark\":\"101\",\"resourceName\":\"v101\",\"resourceType\":1," - "\"resourceVersion\":\"0.3.2\",\"size\":33734,\"updateTime\":1517832224000," - "\"url\":\"http://vbox-resource.nos.netease.com/2536ded2-71a5-40ab-98dc-fe824fbbfb64.mp3\"," - "\"versionId\":68,\"versionName\":\"0.3.2\",\"versionOsType\":\"0\"}]"; - - - FILE* pFp = fopen(path, "rw"); - int fileSize = 0; - - GET_FILE_SIZE(path, fileSize); - - - fprintf(stdout, "%s size = %d\n", path, fileSize); - - if(pFp && fileSize > 0) - { - char* pData = (char*)malloc(fileSize); - - ret = fread(pData, fileSize, 1, pFp); - - fprintf(stdout, "Read: %s\n", pData); - - fclose(pFp); - - SkinUpgrade(pData); - - free(pData); - } -#else - //const char* GetSkinsResource(const char [in] *pKeyName, int [out] *pResType, int [out] *pVersion, const char [out] **pComeFrom) - - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v109] = {%s}\n", GetSkinsResource("v109", NULL, NULL, NULL)); - fprintf(stdout, "[v311] = {%s}\n", GetSkinsResource("v311", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - fprintf(stdout, "[v401] = {%s}\n", GetSkinsResource("v401", NULL, NULL, NULL)); - - test_task_new(__uvTestSkin, NULL); -#endif - } -#endif -#endif - -#if 0 -#define MAIL_SENDER ("pv1_es2@163.com") -#define SENDER_PASSWORD ("pv1Dev163") -#define MAIL_RECEIVER ("pv1_es2@163.com") -#define MAIL_SERVER ("smtps://smtp.163.com") -//#else -#define MAIL_SENDER ("pv1_es2@hotmail.com") -#define SENDER_PASSWORD ("netEase163") -#define MAIL_RECEIVER ("pv1_es2@163.com") -#define MAIL_SERVER ("smtp://smtp-mail.outlook.com") -#endif - -#if 0 - SMTP_MAIL_CONFIG smtpCfg; - memset(&smtpCfg, 0, sizeof(SMTP_MAIL_CONFIG)); - - smtpCfg.pUserName = MAIL_SENDER; - smtpCfg.pPassword = SENDER_PASSWORD; - smtpCfg.pSmtpServer = MAIL_SERVER; - - const char *pFrom = MAIL_SENDER; - const char *pTo[] = {"pv1_es2@163.com", "pv1_es2@126.com", NULL}; - const char *pCc[] = {"xajhuang@qq.com", "xajhuang@163.com", NULL}; - const char *pTitle = "Log Message"; - const char *pMessage = "Hi All: This is log backup mail."; - const char *pAttact[] = {"mail.txt", "time.txt", NULL}; - - InetSmtpSendEmail(pFrom, pTo, pCc, pTitle, pMessage, pAttact, &smtpCfg); -#endif - - // InetHttpUploadFileSync(UPL_HTTP_URL, "./time.txt"); -#if 0 - if((i = GZipFileCompress("./input.txt", "./input.gz")) != 0) - { - LOG_EX(LOG_Error, "Create Gzip File Error: %d\n", i); - } -//#else - - while(TRUE) - { - for(i = 0; i < 3010; i++) - { - LOG_EX(LOG_Debug, "This is log %d\n", j++); - } - - sleep(1); - } -#endif - - RunUVLoop(pLoop); - - while(TRUE) - { - usleep(1000); - } - - return (0); -} diff --git a/src/Framework/Compress/zlib.c b/src/Framework/Compress/zlib.c deleted file mode 100644 index b78f863..0000000 --- a/src/Framework/Compress/zlib.c +++ /dev/null @@ -1,110 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "crypto.h" - -#define CHUNK_BLOCK (16384) - -/* Compress from file source to file dest until EOF on source. - def() returns Z_OK on success, Z_MEM_ERROR if memory could not be - allocated for processing, Z_STREAM_ERROR if an invalid compression - level is supplied, Z_VERSION_ERROR if the version of zlib.h and the - version of the library linked do not match, or Z_ERRNO if there is - an error reading or writing the files. */ -int GZipFileCompress(const char* pInput, const char* pOutput) -{ - int ret, isFlush; - unsigned int have; - z_stream strm; - char strPath[256]; - unsigned char in[CHUNK_BLOCK]; - unsigned char out[CHUNK_BLOCK]; - FILE *source, *dest; - - if (pInput == NULL) - { - return (Z_ERRNO); - } - - if(access(pInput, F_OK) != 0) - { - return (Z_ERRNO); - } - - //fprintf(stdout, "in: %s\n", pInput); - - source = fopen(pInput, "r+"); - - memset(strPath, 0, 256); - if (pOutput == NULL || strlen(pOutput) == 0) - { - sprintf(strPath, "%s.gz", pInput); - dest = fopen(strPath, "w+"); - - //fprintf(stdout, "out: %s\n", strPath); - } - else - { - dest = fopen(pOutput, "w+"); - //fprintf(stdout, "out: %s\n", pOutput); - } - - /* allocate deflate state */ - strm.zalloc = Z_NULL; - strm.zfree = Z_NULL; - strm.opaque = Z_NULL; - //ret = deflateInit(&strm, level); - ret = deflateInit2(&strm, Z_DEFAULT_COMPRESSION, Z_DEFLATED, MAX_WBITS + 16, 8, Z_DEFAULT_STRATEGY); - - if (ret != Z_OK) - { - fclose(source); - fclose(dest); - return ret; - } - - /* compress until end of file */ - do { - strm.avail_in = fread(in, 1, CHUNK_BLOCK, source); - if (ferror(source)) { - (void)deflateEnd(&strm); - fclose(source); - fclose(dest); - return Z_ERRNO; - } - isFlush = feof(source) ? Z_FINISH : Z_NO_FLUSH; - strm.next_in = in; - - /* run deflate() on input until output buffer not full, finish - compression if all of source has been read in */ - do { - strm.avail_out = CHUNK_BLOCK; - strm.next_out = out; - ret = deflate(&strm, isFlush); /* no bad return value */ - have = CHUNK_BLOCK - strm.avail_out; - if (fwrite(out, 1, have, dest) != have || ferror(dest)) { - (void)deflateEnd(&strm); - fclose(source); - fclose(dest); - return Z_ERRNO; - } - } while (strm.avail_out == 0); - - /* done when last data in file processed */ - } while (isFlush != Z_FINISH); - - /* clean up and return */ - (void)deflateEnd(&strm); - - fflush(dest); - - fclose(source); - fclose(dest); - return Z_OK; -} - diff --git a/src/Framework/Configure/config_engine.c b/src/Framework/Configure/config_engine.c deleted file mode 100644 index 4b5c276..0000000 --- a/src/Framework/Configure/config_engine.c +++ /dev/null @@ -1,1042 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "config_engine.h" -#include "libuv_dbus.h" - -#define PROCESS_MUTEX_KEY ("/root/config_server.pid") -#define CFG_SYNC_TIME_OF_SECONDS (1) - - -static PCFG_ITEM __cfgAddItem(const char *pKeyName, const char *pKeyValue, int iType, int overWrite, int saveToDB, int* pErr); - -typedef struct -{ - char keyName[MAX_CFG_KEY_NAME]; - uint32_t tmCached; - - UT_hash_handle hh; ///< UT Hash handle -} CFG_CACHE_ITEM, *PCFG_CACHE_ITEM; - -static sqlite3* g_pSqlFileDB = NULL; -static PCFG_ITEM g_pCfgItems = NULL; -static PCFG_CACHE_ITEM g_pCfgCacheItems = NULL; -static int g_bIsCfgSvr = FALSE; -static uv_rwlock_t g_uvHashRwLock; -static uv_rwlock_t g_uvCacheRwLock; -static uv_timer_t g_uvSyncSvr; - -#if 0 -#define INT_TBL_SQL_CMD ("CREATE TABLE IF NOT EXISTS integer(" \ - "keyName TEXT PRIMARY KEY NOT NULL," \ - "keyValue INTEGER NOT NULL," \ - "keyModule INTEGER NOT NULL," \ - "CreatedTime TimeStamp NOT NULL DEFAULT (datetime('now','localtime')));") - -#define STRING_TBL_SQL_CMD ("CREATE TABLE IF NOT EXISTS string(" \ - "keyName TEXT PRIMARY KEY NOT NULL," \ - "keyValue TEXT NOT NULL," \ - "keyModule INTEGER NOT NULL," \ - "CreatedTime TimeStamp NOT NULL DEFAULT (datetime('now','localtime')));") - -#define FLOAT_TBL_SQL_CMD ("CREATE TABLE IF NOT EXISTS double(" \ - "keyName TEXT PRIMARY KEY NOT NULL," \ - "keyValue NUMERIC NOT NULL," \ - "keyModule INTEGER NOT NULL," \ - "CreatedTime TimeStamp NOT NULL DEFAULT (datetime('now','localtime')));") -#endif - -#define FIRMWARE_TBL_SQL_CMD ("CREATE TABLE IF NOT EXISTS %s (" \ - "ID INTEGER PRIMARY KEY AUTOINCREMENT," \ - "keyName TEXT UNIQUE NOT NULL," \ - "keyValue TEXT NOT NULL," \ - "keyType INTEGER NOT NULL);") - - -static const char* __cfgGetKeyTypeString(int keyType) -{ - switch(keyType) - { - case 0: - return ("string"); - case 1: - return ("integer"); - case 2: - return ("double"); - } - - return ("Unknown Type"); -} - - -static int __cfgItem2CfgRsp(PCFG_ITEM pCfgItem, PCFG_API_RSP pRsp) -{ - if(pCfgItem == NULL || pRsp == NULL) - { - return (-ERR_INPUT_PARAMS); - } - - memset(pRsp->keyValue, 0, MAX_CFG_KEY_VALUE); - pRsp->keyType = pCfgItem->keyType; - - if(pCfgItem->keyType == CFG_TYPE_STRING) - { - strncpy(pRsp->keyValue, pCfgItem->pStrValue, MAX_CFG_KEY_VALUE); - } - else if(pCfgItem->keyType == CFG_TYPE_INT) - { - sprintf(pRsp->keyValue, "%d", pCfgItem->intValue); - } - else if(pCfgItem->keyType == CFG_TYPE_DOUBLE) - { - sprintf(pRsp->keyValue, "%f", pCfgItem->doubleValue); - } - - pRsp->errNo = 0; - - return (0); -} - -static int __cfgMsg2CfgItem(int keyType, const char *pKeyName, const char *pKeyValue, PCFG_ITEM pCfgItem) -{ - int iValue; - double dValue; - char *pEndPtr = NULL; - - if(pKeyName == NULL || pKeyValue == NULL || pCfgItem == NULL) - { - return (-ERR_INPUT_PARAMS); - } - - strcpy(pCfgItem->pKeyName, pKeyName); - pCfgItem->keyType = keyType; - pCfgItem->keyModule = DBusLibuvGetRuntime()->modName; - - switch(keyType) - { - case CFG_TYPE_STRING: - strcpy(pCfgItem->pStrValue, pKeyValue); - break; - - case CFG_TYPE_INT: - iValue = strtol(pKeyValue, NULL, 10); - - if(errno == ERANGE || errno == EINVAL) - { - return (-ERR_STR_CONVERT); - } - else - { - pCfgItem->intValue = iValue; - } - break; - - case CFG_TYPE_DOUBLE: - dValue = strtod(pKeyValue, &pEndPtr); - - if((errno == ERANGE) || (dValue == 0 && (strcmp(pKeyValue, pEndPtr) == 0))) - { - return (-ERR_STR_CONVERT); - } - else - { - pCfgItem->doubleValue = dValue; - } - - break; - - default : - return (-ERR_UNKNOWN_TYPE); - } - - return (0); -} - -static int __cfgMsg2CfgItemV2(int keyType, const char *pKeyName, const char *pKeyValue, PCFG_ITEM* pItem) -{ - int iSize; - PCFG_ITEM pCfgItem = (PCFG_ITEM)malloc(sizeof(CFG_ITEM)); - char* pCache = NULL; - - if(pKeyName == NULL || pKeyValue == NULL || pCfgItem == NULL) - { - free(pCfgItem); - return (-ERR_INPUT_PARAMS); - } - - switch(keyType) - { - case CFG_TYPE_STRING: - iSize += strlen(pKeyValue) + 1; - pCache = (char*)malloc(iSize); - memset(pCache, 0, iSize); - pCfgItem->pKeyName = pCache; - pCfgItem->pStrValue = pCache + strlen(pKeyName) + 1; - __cfgMsg2CfgItem(pCfgItem->keyType, pKeyName, pKeyValue, pCfgItem);; - break; - - case CFG_TYPE_INT: - pCfgItem->pKeyName = (char*)malloc(iSize); - memset(pCfgItem->pKeyName, 0, iSize); - strcpy(pCfgItem->pKeyName, pKeyName); - __cfgMsg2CfgItem(pCfgItem->keyType, pKeyName, pKeyValue, pCfgItem);; - break; - - case CFG_TYPE_DOUBLE: - pCfgItem->pKeyName = (char*)malloc(iSize); - memset(pCfgItem->pKeyName, 0, iSize); - strcpy(pCfgItem->pKeyName, pKeyName); - __cfgMsg2CfgItem(pCfgItem->keyType, pKeyName, pKeyValue, pCfgItem);; - break; - - default: - free(pCfgItem); - return -ERR_UNKNOWN_TYPE; - } - - *pItem = pCfgItem; - - return 0; -} - -int Sqlite3SyncDB(sqlite3* pSqlMemory, const char* pMemDbName, const char* pDBFilePath, const char* pFileDbName, int isSave) -{ - int rc; - sqlite3* pSqlFile; - sqlite3_backup* pSqlBackup; - sqlite3* pSyncDest; - sqlite3* pSyncSrc; - - rc = sqlite3_open(pDBFilePath, &pSqlFile); - - if(rc != SQLITE_OK) - { - return -ERR_OPEN_SQLITE3_DB; - } - - if(pDBFilePath == NULL || strlen(pDBFilePath) == 0) - { - pMemDbName = "main"; - } - - if(pFileDbName == NULL || strlen(pFileDbName) == 0) - { - pFileDbName = "main"; - } - - pSyncSrc = isSave ? pSqlMemory : pSqlFile; - pSyncDest = isSave ? pSqlFile : pSqlMemory; - - pSqlBackup = sqlite3_backup_init(pSyncDest, pMemDbName, pSyncSrc, pFileDbName); - - if(pSqlBackup != NULL) - { -#if 1 - do - { - rc = sqlite3_backup_step(pSqlBackup, 10); - - if(rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED) - { - sqlite3_sleep(100); - } - } while (rc == SQLITE_OK || rc == SQLITE_BUSY || rc == SQLITE_LOCKED); -#else - sqlite3_backup_step(pSqlBackup, -1); -#endif - sqlite3_backup_finish(pSqlBackup); - } - else - { - LOG_EX(LOG_Error, "Backup Database Error\n"); - } - - rc = sqlite3_errcode(pSyncDest); - - return rc; -} - -static int __sqlite3LoadCb(void* data, int argc, char** argv, char** azColName) -{ - - if(argc == 4) - { - int iSize; - PCFG_ITEM pCfgItem = (PCFG_ITEM)malloc(sizeof(CFG_ITEM)); - char* pCache = NULL; - int ret = 0; - - pCfgItem->keyType = atoi(argv[3]); - pCfgItem->keyModule = *(int*)data; - iSize = strlen(argv[1]) + 1; - - switch(pCfgItem->keyType) - { - case CFG_TYPE_STRING: - iSize += strlen(argv[2]) + 1; - pCache = (char*)malloc(iSize); - memset(pCache, 0, iSize); - pCfgItem->pKeyName = pCache; - pCfgItem->pStrValue = pCache + strlen(argv[1]) + 1; - ret = __cfgMsg2CfgItem(pCfgItem->keyType, argv[1], argv[2], pCfgItem);; - break; - - case CFG_TYPE_INT: - pCfgItem->pKeyName = (char*)malloc(iSize); - memset(pCfgItem->pKeyName, 0, iSize); - strcpy(pCfgItem->pKeyName, argv[1]); - ret = __cfgMsg2CfgItem(pCfgItem->keyType, argv[1], argv[2], pCfgItem);; - break; - - case CFG_TYPE_DOUBLE: - pCfgItem->pKeyName = (char*)malloc(iSize); - memset(pCfgItem->pKeyName, 0, iSize); - strcpy(pCfgItem->pKeyName, argv[1]); - ret = __cfgMsg2CfgItem(pCfgItem->keyType, argv[1], argv[2], pCfgItem);; - break; - - default: - free(pCfgItem); - return -ERR_UNKNOWN_TYPE; - } - - uv_rwlock_wrlock(&g_uvHashRwLock); - HASH_ADD_STR(g_pCfgItems, pKeyName, pCfgItem); - uv_rwlock_wrunlock(&g_uvHashRwLock); - - if(ret == 0) - { - CfgItemPrint("Load Configure: ", pCfgItem); - } - } - else - { - LOG_EX(LOG_Error, "argc = %d\n", argc); - } - - return 0; -} - -static int __cfgLoadGlobalConfig(void) -{ - int rc = 0; - char* pErrMsg = NULL; - char sqlCmd[1024]; - PLIBUV_DBUS_PARAMS pContext = DBusLibuvGetRuntime(); - - memset(sqlCmd, 0, 1024); - sprintf(sqlCmd, "SELECT * FROM %s", MODULE_ALIAS_NAME(MODULE_CONFIGURE)); - - // get global configure - rc = sqlite3_exec(g_pSqlFileDB, sqlCmd, __sqlite3LoadCb, (void*)MODULE_CONFIGURE, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQLite3 Query Error: %s\n", pErrMsg); - return -ERR_SQL_QUERY; - } - - if(pContext->modName != MODULE_CONFIGURE) - { - memset(sqlCmd, 0, 1024); - sprintf(sqlCmd, "SELECT * FROM %s", MODULE_ALIAS_NAME(pContext->modName)); - - // get local configure - rc = sqlite3_exec(g_pSqlFileDB, sqlCmd, __sqlite3LoadCb, (void*)&pContext->modName, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQLite3 Query Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - return -ERR_SQL_QUERY; - } - } - - return 0; -} - - -void CfgItemPrint(const char* pPrefix, PCFG_ITEM pCfgItem) -{ - if(pCfgItem == NULL) - { - fprintf(stderr, "CfgItemPrint: NULL\n"); - return; - } - - switch(pCfgItem->keyType) - { - case 0: - LOG_EX(LOG_Debug, "%s[%s] = \"%s\", \ttype = %s, \tmodule = %s\n", pPrefix ? pPrefix : "", - pCfgItem->pKeyName, pCfgItem->pStrValue, __cfgGetKeyTypeString(pCfgItem->keyType), ModuleNameToString(pCfgItem->keyModule)); - break; - - case 1: - LOG_EX(LOG_Debug, "%s[%s] = %d, \ttype = %s, \tmodule = %s\n", pPrefix ? pPrefix : "", - pCfgItem->pKeyName, pCfgItem->intValue, __cfgGetKeyTypeString(pCfgItem->keyType), ModuleNameToString(pCfgItem->keyModule)); - break; - - case 2: - LOG_EX(LOG_Debug, "%s[%s] = %f, \ttype = %s, \tmodule = %s\n", pPrefix ? pPrefix : "", - pCfgItem->pKeyName, pCfgItem->doubleValue, __cfgGetKeyTypeString(pCfgItem->keyType), ModuleNameToString(pCfgItem->keyModule)); - break; - - default: - LOG_EX(LOG_Error, "Unknown type = %d\n", pCfgItem->keyType); - break; - } - -} - -int CfgAddGlobalConfig(const char *pKeyName, const char *pKeyValue, CFG_DATA_TYPE keyType) -{ - CFG_API_REQ req; - int ret; - - if(pKeyName == NULL || pKeyValue == NULL || strlen(pKeyName) == 0 || strlen(pKeyValue) == 0) - { - return (-ERR_INPUT_PARAMS); - } - - if(keyType < 0 || keyType > CFG_TYPE_DOUBLE) - { - return (-ERR_INPUT_PARAMS); - } - - memset(&req, 0, sizeof(CFG_API_REQ)); - - strncpy(req.keyName, pKeyName, MAX_CFG_KEY_NAME); - strncpy(req.keyValue, pKeyValue, MAX_CFG_KEY_VALUE); - - ret = DBusJsonSendToCommand(NULL, - g_pModInfoTable[MODULE_CONFIGURE].modAliase, - CMD_CFG_ADD_REQ, - JSON_ENGINE_CFG_REQ, - &req, TRUE); - - if(ret == 0) - { - return (-ERR_CFG_WAIT_RSP); - } - else - { - return (ret); - } -} - -int CfgAddKeyValue(const char *pKeyName, PCFG_ITEM pItem, int saveToDB) -{ - int err = 0; - PCFG_ITEM pCfgItem = NULL; - - if(pItem == NULL || pKeyName == NULL || strlen(pKeyName) == 0) - { - return (-ERR_INPUT_PARAMS); - } - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_FIND_STR(g_pCfgItems, pKeyName, pCfgItem); - uv_rwlock_rdunlock(&g_uvHashRwLock); - - if(pCfgItem != NULL) - { - return (-ERR_CFG_ITEM_EXIST); - } - - if(pItem->keyType == CFG_TYPE_STRING) - { - __cfgAddItem(pKeyName, (const char*)pItem->pStrValue, pItem->keyType, FALSE, saveToDB, &err); - } - else - { - char buf[256]; - memset(buf, 0, 256); - - if(pItem->keyType == CFG_TYPE_INT) - { - sprintf(buf, "%d", pItem->intValue); - } - else if(pItem->keyType == CFG_TYPE_DOUBLE) - { - sprintf(buf, "%f", pItem->doubleValue); - } - - __cfgAddItem(pKeyName, (const char*)buf, pItem->keyType, FALSE, saveToDB, &err); - } - - return (err); -} - -int CfgChangeKeyValue(const char *pKeyName, PCFG_ITEM pItem, int saveToDB) -{ - int err = 0; - PCFG_ITEM pCfgItem = NULL; - - if(pItem == NULL || pKeyName == NULL || strlen(pKeyName) == 0) - { - return (-ERR_INPUT_PARAMS); - } - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_FIND_STR(g_pCfgItems, pKeyName, pCfgItem); - uv_rwlock_rdunlock(&g_uvHashRwLock); - - if(pCfgItem == NULL) - { - return (-ERR_CFG_NOITEM); - } - - HASH_DEL(g_pCfgItems, pCfgItem); - - if(pItem->keyType == CFG_TYPE_STRING) - { - __cfgAddItem(pKeyName, (const char*)pItem->pStrValue, pItem->keyType, FALSE, saveToDB, &err); - } - else - { - char buf[256]; - memset(buf, 0, 256); - - if(pItem->keyType == CFG_TYPE_INT) - { - sprintf(buf, "%d", pItem->intValue); - } - else if(pItem->keyType == CFG_TYPE_DOUBLE) - { - sprintf(buf, "%f", pItem->doubleValue); - } - - __cfgAddItem(pKeyName, (const char*)buf, pItem->keyType, FALSE, saveToDB, &err); - } - - return (err); -} - -int CfgGetKeyValue(const char* pKeyName, PCFG_ITEM* pItem) -{ - PCFG_ITEM pCfgItem = NULL; - PLIBUV_DBUS_PARAMS pContext = DBusLibuvGetRuntime(); - - if(pItem == NULL || pKeyName == NULL || strlen(pKeyName) == 0) - { - return (-ERR_INPUT_PARAMS); - } - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_FIND_STR(g_pCfgItems, pKeyName, pCfgItem); - uv_rwlock_rdunlock(&g_uvHashRwLock); - - if(pCfgItem == NULL) - { - if(pContext->modName == MODULE_CONFIGURE) - { - return (-ERR_CFG_NOITEM); - } - else - { - CFG_API_REQ req; - int ret = 0; - memset(&req, 0, sizeof(CFG_API_REQ)); - strncpy(req.keyName, pKeyName, MAX_CFG_KEY_NAME); - ret = DBusJsonSendToCommand(NULL, g_pModInfoTable[MODULE_CONFIGURE].modAliase, CMD_CFG_GET_REQ, JSON_ENGINE_CFG_REQ, &req, TRUE); - - if(ret == 0) - { - return (-ERR_CFG_WAIT_RSP); - } - else - { - return (ret); - } - } - } - - *pItem = pCfgItem; - - return 0; -} - -static int __cfgCreateCfgFile(const char* pCfgFilePath) -{ - /*const char* pSqlIntDB = NULL; - const char* pSqlFloatDB = NULL; - const char* pSqlStringDB = NULL; */ - char* pErrMsg = NULL; - int rc = 0; - int i = 0; - - rc = sqlite3_open_v2(pCfgFilePath, &g_pSqlFileDB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); - - if(rc) - { - LOG_EX(LOG_Error, "sqlite3_open_v2 error: %s\n", pCfgFilePath); - return -ERR_OPEN_SQLITE3_DB; - } - - for(i = 0; (i < sizeof(g_pModInfoTable) / sizeof(g_pModInfoTable[0])); i++) - { - char sqlBuf[1024]; - - memset(sqlBuf, 0, 1024); - - sprintf(sqlBuf, FIRMWARE_TBL_SQL_CMD, MODULE_ALIAS_NAME(i)); - - rc = sqlite3_exec(g_pSqlFileDB, sqlBuf, NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - uv_fs_t uvFs; - LOG_EX(LOG_Error, "Create Tbl[%d] Error: %s\n", i, pErrMsg); - sqlite3_free(pErrMsg); - uv_fs_unlink(GetDBusDefaultLoop(), &uvFs, pCfgFilePath, NULL); - return -ERR_SQLITE3_CREATE_TABLE; - } - } - - return 0; -} - -#if 0 -static void onSigTermProgressExit(int sign) -{ - if(g_bIsCfgSvr) - { - } - - kill(getpid(), SIGKILL); -} - -static void onProgressExit(void) -{ - onSigTermProgressExit(0); -} -#endif - - -static int __cfgCheckCfgSvrRun(void) -{ - int rc; - int fd = open(PROCESS_MUTEX_KEY, O_CREAT | O_RDWR, 0666); - - if(fd == -1) - { - LOG_EX(LOG_Debug, "Open File Error\n"); - } - - rc = flock(fd, LOCK_EX | LOCK_NB); - - if(rc == -1) - { - LOG_EX(LOG_Debug, "Configure server running\n"); - return FALSE; - } - else - { - LOG_EX(LOG_Debug, "Configure server stoped\n"); - return TRUE; - } -} - -static int __cfgAddToCache(const char *pKeyName) -{ - PCFG_CACHE_ITEM pItem = NULL; - - uv_rwlock_rdlock(&g_uvCacheRwLock); - HASH_FIND_STR(g_pCfgCacheItems, pKeyName, pItem); - uv_rwlock_rdunlock(&g_uvCacheRwLock); - - if(pItem != NULL) - { - HASH_DEL(g_pCfgCacheItems, pItem); - free(pItem); - } - - pItem = (PCFG_CACHE_ITEM)malloc(sizeof(CFG_CACHE_ITEM)); - - strncpy(pItem->keyName, pKeyName, MAX_CFG_KEY_NAME); - pItem->tmCached = LIBUV_CURRENT_TIME_MS(); - - uv_rwlock_wrlock(&g_uvCacheRwLock); - HASH_ADD_STR(g_pCfgCacheItems, keyName, pItem); - uv_rwlock_wrunlock(&g_uvCacheRwLock); - - return (0); -} - -static PCFG_ITEM __cfgAddItem(const char *pKeyName, const char *pKeyValue, int iType, int overWrite, int saveToDB, int* pErr) -{ - PCFG_ITEM pCfgItem = NULL; - int iSize = 0; - char *pCache = NULL; - - uv_rwlock_rdlock(&g_uvHashRwLock); - HASH_FIND_STR(g_pCfgItems, pKeyName, pCfgItem); - uv_rwlock_rdunlock(&g_uvHashRwLock); - - if(pCfgItem != NULL) - { - if(overWrite) - { - HASH_DEL(g_pCfgItems, pCfgItem); - free(pCfgItem->pKeyName); - free(pCfgItem); - } - else - { - if(pErr) - { - *pErr = -ERR_CFG_NOITEM; - } - - return NULL; - } - } - - if(iType == CFG_TYPE_STRING) - { - iSize = strlen(pKeyName) + strlen(pKeyValue) + 2; - } - else - { - iSize = strlen(pKeyName) + 1; - } - - pCache = (char *)malloc(iSize); - - memset(pCache, 0, iSize); - pCfgItem = (PCFG_ITEM)malloc(sizeof(CFG_ITEM)); - - - pCfgItem->pKeyName = pCache; - - if(iType == CFG_TYPE_STRING) - { - pCfgItem->pStrValue = pCache + strlen(pKeyName) + 1; - } - - __cfgMsg2CfgItem(iType, pKeyName, pKeyValue, pCfgItem); - - uv_rwlock_wrlock(&g_uvHashRwLock); - HASH_ADD_STR(g_pCfgItems, pKeyName, pCfgItem); - uv_rwlock_wrunlock(&g_uvHashRwLock); - - // if need save this configure item to database - if(saveToDB) - { - __cfgAddToCache(pKeyName); - } - - if(pErr) - { - *pErr = 0; - } - - return (pCfgItem); -} - -static void __cfgSyncServer(void) -{ - if(HASH_COUNT(g_pCfgCacheItems) > 0) - { - PCFG_CACHE_ITEM pItem = NULL, pTemp = NULL; - - sqlite3_exec(g_pSqlFileDB, "BEGIN TRANSACTION;", NULL, NULL, NULL); - - HASH_ITER(hh, g_pCfgCacheItems, pItem, pTemp) - { - PCFG_ITEM pCfgItem = NULL; - - if(CfgGetKeyValue(pItem->keyName, &pCfgItem) == 0) - { - char* pSqlInsertCmd = (char*)malloc(1024 * 4); - char* pSqlUpgradeCmd = (char*)malloc(1024 * 4); - memset(pSqlInsertCmd, 0, 1024 * 4); - memset(pSqlUpgradeCmd, 0, 1024 * 4); - - if(pCfgItem->keyType == CFG_TYPE_STRING) - { - sprintf(pSqlInsertCmd, "INSERT OR IGNORE INTO %s (keyName, keyValue, keyType) VALUES (\'%s\', \'%s\', %d)", - MODULE_ALIAS_NAME(pCfgItem->keyModule), pCfgItem->pKeyName, pCfgItem->pStrValue, pCfgItem->keyType); - - sprintf(pSqlUpgradeCmd, "UPDATE %s SET keyValue = \'%s\', keyType = %d WHERE keyName = \'%s\'", - MODULE_ALIAS_NAME(pCfgItem->keyModule), pCfgItem->pStrValue, pCfgItem->keyType, pCfgItem->pKeyName); - } - else if(pCfgItem->keyType == CFG_TYPE_INT) - { - sprintf(pSqlInsertCmd, "INSERT OR IGNORE INTO %s (keyName, keyValue, keyType) VALUES (\'%s\', \'%d\', %d)", - MODULE_ALIAS_NAME(pCfgItem->keyModule), pCfgItem->pKeyName, pCfgItem->intValue, pCfgItem->keyType); - - sprintf(pSqlUpgradeCmd, "UPDATE %s SET keyValue = \'%d\', keyType = %d WHERE keyName = \'%s\'", - MODULE_ALIAS_NAME(pCfgItem->keyModule), pCfgItem->intValue, pCfgItem->keyType, pCfgItem->pKeyName); - } - else if(pCfgItem->keyType == CFG_TYPE_DOUBLE) - { - sprintf(pSqlInsertCmd, "INSERT OR IGNORE INTO %s (keyName, keyValue, keyType) VALUES (\'%s\', \'%f\', %d)", - MODULE_ALIAS_NAME(pCfgItem->keyModule), pCfgItem->pKeyName, pCfgItem->doubleValue, pCfgItem->keyType); - - sprintf(pSqlUpgradeCmd, "UPDATE %s SET keyValue = \'%f\', keyType = %d WHERE keyName = \'%s\'", - MODULE_ALIAS_NAME(pCfgItem->keyModule), pCfgItem->doubleValue, pCfgItem->keyType, pCfgItem->pKeyName); - } - - if((sqlite3_exec(g_pSqlFileDB, pSqlInsertCmd, NULL, NULL, NULL) == SQLITE_OK) - && (sqlite3_exec(g_pSqlFileDB, pSqlUpgradeCmd, NULL, NULL, NULL) == SQLITE_OK)) - { - // Delete this Key - HASH_DEL(g_pCfgCacheItems, pItem); - LOG_EX(LOG_Debug, "Sync Configure %s To Database\n", pCfgItem->pKeyName); - } - else - { - free(pSqlInsertCmd); - free(pSqlUpgradeCmd); - return; - } - - free(pSqlInsertCmd); - free(pSqlUpgradeCmd); - } - else - { - // Delete this Key - HASH_DEL(g_pCfgCacheItems, pItem); - } - } - - sqlite3_exec(g_pSqlFileDB, "END TRANSACTION;", NULL, NULL, NULL); - } -} -static void __uvThreadSyncCfg(void *pParams) -{ - while(TRUE) - { - __cfgSyncServer(); - sleep(CFG_SYNC_TIME_OF_SECONDS); - } - - pthread_detach(pthread_self()); -} - -static void __cfgStartSyncGlobalCfgSvr(void) -{ - uv_thread_t uvSyncThread; - - uv_rwlock_init(&g_uvCacheRwLock); - - uv_thread_create(&uvSyncThread, __uvThreadSyncCfg, NULL); -} - -static PCFG_ITEM __onCMD_CFG_GET_REQ(const char* pKeyName, int* pErr) -{ - PCFG_ITEM pItem = NULL; - - *pErr = CfgGetKeyValue(pKeyName, &pItem); - - if(*pErr == 0) - { - return (pItem); - } - - return (NULL); -} - -static int __onCMD_CFG_ADD_REQ(const char* pKeyName, const char* pKeyValue, int keyType) -{ - PCFG_ITEM pCfgItem = NULL; - int ret; - - if(pKeyName == NULL || pKeyValue == NULL || strlen(pKeyName) == 0 || strlen(pKeyValue) == 0) - { - return (-ERR_INPUT_PARAMS); - } - - if(keyType < 0 || keyType > CFG_TYPE_DOUBLE) - { - return (-ERR_INPUT_PARAMS); - } - - ret = __cfgMsg2CfgItemV2(keyType, pKeyName, pKeyValue, &pCfgItem); - - if(ret != 0) - { - return (ret); - } - - return CfgAddKeyValue(pKeyName, pCfgItem, TRUE); -} - -static void __cfgSvrProcessCmd(DBUS_CMD cmd, const char* pKeyName, const char* pKeyValue, int keyType, PCFG_API_RSP pRsp) -{ - PCFG_ITEM pItem = NULL; - int err; - - memset(pRsp, 0, sizeof(CFG_API_RSP)); - pRsp->errNo = -ERR_UNKNOWN_TYPE; - - if(pKeyName == NULL) - { - pRsp->errNo = -ERR_INPUT_PARAMS; - return; - } - - strncpy(pRsp->keyName, pKeyName, MAX_CFG_KEY_NAME); - - switch(cmd) - { - case CMD_CFG_ADD_REQ: - err = __onCMD_CFG_ADD_REQ(pKeyName, pKeyValue, keyType); - - pRsp->keyType = keyType; - pRsp->errNo = err; - strncpy(pRsp->keyName, pKeyName, MAX_CFG_KEY_NAME); - strncpy(pRsp->keyValue, pKeyValue, MAX_CFG_KEY_VALUE); - - break; - - case CMD_CFG_GET_REQ: - - pItem = __onCMD_CFG_GET_REQ(pKeyName, &err); - - if(pItem == NULL || err != 0) - { - pRsp->keyType = -1; - pRsp->errNo = err; - strncpy(pRsp->keyName, pKeyName, MAX_CFG_KEY_NAME); - return; - } - else - { - __cfgItem2CfgRsp(pItem, pRsp); - return; - } - - break; - } - - return; -} - -int CfgGlobalEnvInit(void) -{ - int rc = 0; - - uv_rwlock_init(&g_uvHashRwLock); - - rc = __cfgCreateCfgFile(GLOBAL_CFG_FILE_PATH); - - if(rc != SQLITE_OK) - { - return rc; - } - -#if 0 - rc = sqlite3_open_v2(":memory:", &g_pSqlMemDB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Memory SQLite3 DB Error\n"); - return -ERR_CREATE_SQLITE3_DB; - } - - //rc = sqlite3SyncDatabase(g_pSqlMemDB, GLOBAL_CFG_FILE_PATH, 0); -#endif - rc = __cfgLoadGlobalConfig(); - - if(rc != SQLITE_OK) - { - return (-ERR_OPEN_SQLITE3_DB); - //sqlite3_close(g_pSqlFileDB); - } - -#if 0 - g_bIsCfgSvr = cfgCheckCfgSvrRun(); - - if(atexit(onProgressExit) != 0) - { - fprintf(stderr, "atexit error\n"); - } - - signal(SIGINT, onSigTermProgressExit); -#endif - - __cfgStartSyncGlobalCfgSvr(); - - return 0; -} - -void OnCfgMsgProcess(MODULE_NAME dest, DBUS_CMD busCmd, const char* pJsonStr) -{ - CFG_API_RSP cfgRsp; - int err = 0; - PCFG_API_REQ pReq = NULL; - PCFG_API_RSP pRsp = NULL; - PLIBUV_DBUS_PARAMS pParams = DBusLibuvGetRuntime(); - - switch(busCmd) - { - case CMD_CFG_ADD_REQ: - case CMD_CFG_GET_REQ: - pReq = Json2Struct(pJsonStr, JSON_ENGINE_CFG_REQ, TRUE, &err); - - if(pReq != NULL && err == 0) - { - pRsp = &cfgRsp; - __cfgSvrProcessCmd(busCmd, pReq->keyName, pReq->keyValue, pReq->keyType, pRsp); - DBusJsonSendToCommand(NULL, g_pModInfoTable[dest].modAliase, busCmd + 1, JSON_ENGINE_CFG_RSP, pRsp, TRUE); - } - - free(pReq); - break; -#if 0 - case CMD_CFG_GET_REQ: - pReq = Json2Struct(pJsonStr, JSON_ENGINE_CFG_REQ, TRUE, &err); - - if(pReq != NULL && err == 0) - { - pRsp = &cfgRsp; - __cfgSvrProcessCmd(busCmd, pReq->keyName, NULL, pReq->keyType, pRsp); - DBusJsonSendToCommand(NULL, g_pModInfoTable[dest].modAliase, CMD_CFG_GET_RSP, JSON_ENGINE_CFG_RSP, pRsp, TRUE); - } - break; -#endif - case CMD_CFG_ADD_RSP: - case CMD_CFG_GET_RSP: - pRsp = Json2Struct(pJsonStr, JSON_ENGINE_CFG_RSP, TRUE, &err); - - if(pRsp != NULL && err == 0 && pRsp->errNo == 0) - { - // Add to local Hash Table cache - PCFG_ITEM pCfgItem = __cfgAddItem(pRsp->keyName, pRsp->keyValue, pRsp->keyType, TRUE, FALSE, &err); - - if(err == 0 && pCfgItem != NULL) - { - if(pParams->onCfgCb) - { - pParams->onCfgCb(busCmd, pCfgItem, 0); - } - } - else - { - LOG_EX(LOG_Error, "Add Configure {[%s]<%d> = \"%s\"} Error: %d\n", - pRsp->keyName, pRsp->keyValue, pRsp->keyType, err); - } - } - else - { - if(pParams->onCfgCb) - { - pParams->onCfgCb(busCmd, NULL, (err == 0) ? pRsp->errNo : err); - } - - LOG_EX(LOG_Error, "pRsp = %p, err = %d, rspErr = %d\n", pRsp, err, pRsp ? pRsp->errNo : -err); - } - - free(pRsp); - break; - } -} diff --git a/src/Framework/Configure/ini_prase.c b/src/Framework/Configure/ini_prase.c deleted file mode 100644 index 3542f87..0000000 --- a/src/Framework/Configure/ini_prase.c +++ /dev/null @@ -1,289 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "libuv_dbus.h" -#include "server_addr.h" - -#ifdef PLATFORM_R16 -#define DEVICE_CFG_FILE ("/mnt/UDISK/dev.conf") -#else -#define DEVICE_CFG_FILE ("./dev.conf") -#endif - -static config_t g_cfgInfo; - -void InitCfgToCfgFile(config_t* pCfg) -{ - config_setting_t *pRoot, *pSetting, *pGlobalgrp, *pLogGrp, *pSubGrp; - - if(pCfg == NULL) - { - return; - } - - pRoot = config_root_setting(pCfg); - - pGlobalgrp = config_setting_add(pRoot, "Global", CONFIG_TYPE_GROUP); - - pSetting = config_setting_add(pGlobalgrp, "ServerMode", CONFIG_TYPE_INT); - config_setting_set_int(pSetting, PUBLISH_MODE); - - pLogGrp = config_setting_add(pGlobalgrp, "Log", CONFIG_TYPE_GROUP); - - pSetting = config_setting_add(pLogGrp, "Enable", CONFIG_TYPE_BOOL); - config_setting_set_bool(pSetting, TRUE); - - pSetting = config_setting_add(pLogGrp, "Level", CONFIG_TYPE_INT); - config_setting_set_format(pSetting, CONFIG_FORMAT_HEX); - config_setting_set_int(pSetting, 0x000003FF); - - pSubGrp = config_setting_add(pLogGrp, "LogToEMail", CONFIG_TYPE_GROUP); - pSetting = config_setting_add(pSubGrp, "Enable", CONFIG_TYPE_BOOL); - config_setting_set_bool(pSetting, FALSE); - pSetting = config_setting_add(pSubGrp, "EMail", CONFIG_TYPE_STRING); - config_setting_set_string(pSetting, "pv1_es2@163.com"); - - pSetting = config_setting_add(pLogGrp, "LogToFile", CONFIG_TYPE_BOOL); - config_setting_set_bool(pSetting, TRUE); - - pSetting = config_setting_add(pLogGrp, "LogToServer", CONFIG_TYPE_BOOL); - config_setting_set_bool(pSetting, TRUE); - - pSubGrp = config_setting_add(pLogGrp, "LogToUDPServer", CONFIG_TYPE_GROUP); - pSetting = config_setting_add(pSubGrp, "Enable", CONFIG_TYPE_BOOL); - config_setting_set_bool(pSetting, FALSE); - pSetting = config_setting_add(pSubGrp, "UdpServerIp", CONFIG_TYPE_STRING); - config_setting_set_string(pSetting, "10.240.84.163"); - pSetting = config_setting_add(pSubGrp, "UdpBasePort", CONFIG_TYPE_INT); - config_setting_set_int(pSetting, 10000); - - - if(!config_write_file(pCfg, DEVICE_CFG_FILE)) - { - LOG_EX(LOG_Error, "Create Configure File %s Error\n", DEVICE_CFG_FILE); - } -} - -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; - - if(pTags == NULL || strlen(pTags) == 0) - { - return defValue; - } - - if(!config_lookup_int(&g_cfgInfo, pTags, &iValue)) - { - return defValue; - } - - return iValue; -} - -void CfgSetIntValue(const char* pTags, int iValue) -{ - config_setting_t *pRoot, *pGlobalgrp, *pSet; - - LOG_EX(LOG_Debug, "Set: %s --> %d\n", pTags, iValue); - - if(pTags == NULL || strlen(pTags) == 0) - { - LOG_EX(LOG_Error, "Input Params error: pTags = [%s]\n", pTags ? pTags : "NULL"); - return; - } - - pRoot = config_root_setting(&g_cfgInfo); - - if(pRoot == NULL) - { - LOG_EX(LOG_Error, "pRoot = NULL\n"); - return; - } - - pGlobalgrp = config_setting_get_member(pRoot, "Global"); - - if(pGlobalgrp == NULL) - { - LOG_EX(LOG_Error, "pGlobalgrp = NULL\n"); - return; - } - - pSet = config_setting_get_member(pGlobalgrp, pTags); - - if(!pSet) - { - pSet = config_setting_add(pGlobalgrp, pTags, CONFIG_TYPE_INT); - } - - if(pSet == NULL) - { - LOG_EX(LOG_Error, "pSet = NULL\n"); - return; - } - - config_setting_set_int(pSet, iValue); - - - if(!config_write_file(&g_cfgInfo, DEVICE_CFG_FILE)) - { - LOG_EX(LOG_Error, "Set %s Value Error\n", pTags); - } -} - -char* CfgGetStringValue(const char* pTags, char* pDefValue) -{ - char* pValue = pDefValue; - - if(pTags == NULL || strlen(pTags) == 0) - { - return pDefValue; - } - - if(!config_lookup_string(&g_cfgInfo, pTags, (const char**)&pValue)) - { - return pDefValue; - } - - return pValue; -} - -double CfgGetFloatValue(const char* pTags, double defValue) -{ - double dValue = defValue; - - if(pTags == NULL || strlen(pTags) == 0) - { - return defValue; - } - - if(!config_lookup_float(&g_cfgInfo, pTags, &dValue)) - { - return defValue; - } - - return dValue; -} - -int CfgGetBoolValue(const char* pTags, int defValue) -{ - int iValue = defValue; - - if(pTags == NULL || strlen(pTags) == 0) - { - return defValue; - } - - if(!config_lookup_bool(&g_cfgInfo, pTags, &iValue)) - { - return defValue; - } - - return iValue; -} - -void CfgFileInit(void) -{ - //config_setting_t *pRoot, *pSetting; - - config_init(&g_cfgInfo); - -#if 0 - config_set_options(&g_cfgInfo, - (CONFIG_OPTION_SEMICOLON_SEPARATORS - | CONFIG_OPTION_COLON_ASSIGNMENT_FOR_GROUPS - | CONFIG_OPTION_OPEN_BRACE_ON_SEPARATE_LINE)); -#endif - - config_set_tab_width(&g_cfgInfo, 4); - - if(access(DEVICE_CFG_FILE, F_OK) != 0) - { - InitCfgToCfgFile(&g_cfgInfo); - } - else if(!config_read_file(&g_cfgInfo, DEVICE_CFG_FILE)) - { - LOG_EX(LOG_Error, "Read Configure File %s Error\n", DEVICE_CFG_FILE); - return; - } -} diff --git a/src/Framework/Crypto/aes.c b/src/Framework/Crypto/aes.c deleted file mode 100644 index afff0ac..0000000 --- a/src/Framework/Crypto/aes.c +++ /dev/null @@ -1,104 +0,0 @@ -#include -#include -#include -#include - -#include "log.h" -#include "smart_sound.h" -#include "crypto.h" -#include "libuv_dbus.h" - -int EvpAESEncrypto(unsigned char* pInBuf, - int iSize, - unsigned char* pOutBuf, - int* pOutSize, - unsigned char* pKey) -{ - int enBytes = 0; - EVP_CIPHER_CTX ctx; - //int decDataLen = 0; - if(!pInBuf || !pOutBuf || !pOutSize || !pKey) - { - return -ERR_INPUT_PARAMS; - } - - *pOutSize = 0; - //decDataLen = ((iSize + AES_BLOCK_SIZE - 1) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE; - - EVP_CIPHER_CTX_init(&ctx); - - if(EVP_EncryptInit_ex(&ctx, EVP_aes_128_ecb(), NULL, pKey, NULL) == 0) - { - LOG_EX(LOG_Error, "EVP_EncryptInit_ex Error\n"); - return -ERR_EVP_INIT_KEY; - } - - if(EVP_EncryptUpdate(&ctx, pOutBuf, &enBytes, pInBuf, iSize) == 0) - { - LOG_EX(LOG_Error, "EVP_EncryptUpdate Error\n"); - return -ERR_EVP_UPDATE; - } - - pOutBuf += enBytes; - pInBuf += enBytes; - *pOutSize += enBytes; - - if(EVP_EncryptFinal_ex(&ctx, pOutBuf, &enBytes) == 0) - { - LOG_EX(LOG_Error, "EVP_EncryptFinal_ex Error\n"); - return -ERR_EVP_FINALE; - } - - *pOutSize += enBytes; - - EVP_CIPHER_CTX_cleanup(&ctx); - - return 0; -} - -int EvpAESDecrypto(unsigned char* pInBuf, - int iSize, - unsigned char* pOutBuf, - int* pOutSize, - unsigned char* pKey) -{ - int deBytes = 0; - EVP_CIPHER_CTX ctx; - - if(!pInBuf || !pOutBuf || !pOutSize || !pKey) - { - return -ERR_INPUT_PARAMS; - } - - EVP_CIPHER_CTX_init(&ctx); - - *pOutSize = 0; - - if(EVP_DecryptInit_ex(&ctx, EVP_aes_128_ecb(), NULL, pKey, NULL) == 0) - { - LOG_EX(LOG_Error, "EVP_DecryptInit_ex Error\n"); - return -ERR_EVP_INIT_KEY; - } - - if(EVP_DecryptUpdate(&ctx, pOutBuf, &deBytes, pInBuf, iSize) == 0) - { - LOG_EX(LOG_Error, "EVP_EncryptUpdate Error\n"); - return -ERR_EVP_UPDATE; - } - - pOutBuf += deBytes; - pInBuf += deBytes; - *pOutSize += deBytes; - - if(EVP_DecryptFinal_ex(&ctx, pOutBuf, &deBytes) == 0) - { - LOG_EX(LOG_Error, "EVP_EncryptFinal_ex Error\n"); - return -ERR_EVP_FINALE; - } - - *pOutSize += deBytes; - EVP_CIPHER_CTX_cleanup(&ctx); - - return 0; -} - diff --git a/src/Framework/Crypto/base64.c b/src/Framework/Crypto/base64.c deleted file mode 100644 index 6941073..0000000 --- a/src/Framework/Crypto/base64.c +++ /dev/null @@ -1,158 +0,0 @@ -#include -#include -#include -#include - -#include "log.h" -#include "crypto.h" - -const char* EvpBase64Encode(const char* pSrc) -{ - EVP_ENCODE_CTX ctx; - int enSize = 0; - int sLen, size; - char* pEncode = NULL; - - if(pSrc == NULL || strlen(pSrc) == 0) - { - return (NULL); - } - - sLen = strlen(pSrc); - size = ((sLen / 3) * 4) + 4 + (sLen / 64) + sLen % 64; - - pEncode = (char*)malloc(size); - memset(pEncode, 0, size); - - EVP_EncodeInit(&ctx); - EVP_EncodeUpdate(&ctx, pEncode, &enSize, pSrc, strlen(pSrc)); - EVP_EncodeFinal(&ctx, pEncode + enSize, &enSize); - -// fprintf(stdout, "Src: \n[%s]\n", pSrc); -// fprintf(stdout, "Base64(%d --> %d | %d) Bytes: \n[%s]\n", sLen, size, strlen(pEncode), pEncode); - - return pEncode; -} - -const char* EvpBase64Decode(const char* pBase64) -{ - EVP_ENCODE_CTX ctx; - int enSize = 0; - int size = 0; - char *pDecode = NULL; - - if(pBase64 == NULL || strlen(pBase64) == 0) - { - return (NULL); - } - - size = strlen(pBase64); - pDecode = (char*)malloc(size); - memset(pDecode, 0, size); - - EVP_DecodeInit(&ctx); - EVP_DecodeUpdate(&ctx, pDecode, &enSize, pBase64, strlen(pBase64)); - EVP_DecodeFinal(&ctx, pDecode + enSize, &enSize); - -// fprintf(stdout, "Decode(%d --> %d) Bytes: \n[%s]\n", size, strlen(pDecode), pDecode); - - return pDecode; -} - -const char* EvpBase64EncodeNoAlign(const char* pSrc) -{ - int size, sLen; - char* pEncode = NULL; - - if(pSrc == NULL || strlen(pSrc) == 0) - { - return (NULL); - } - - sLen = strlen(pSrc); - size = ((sLen / 3) * 4) + 4 + (sLen / 64) + sLen % 64; - - pEncode = (char*)malloc(size); - memset(pEncode, 0, size); - - EVP_EncodeBlock(pEncode, (const unsigned char *)pSrc, sLen); -// fprintf(stdout, "Src: \n[%s]\n", pSrc); -// fprintf(stdout, "Base64(%d --> %d | %d) Bytes: \n[%s]\n", sLen, size, strlen(pEncode), pEncode); - - return pEncode; -} - -const char* EvpBase64EncodeNoAlignV2(unsigned char* pSrc, int sLen) -{ - int size; - char* pEncode = NULL; - - if(pSrc == NULL || sLen <= 0) - { - return (NULL); - } - - size = ((sLen / 3) * 4) + 4 + (sLen / 64) + sLen % 64; - - pEncode = (char*)malloc(size); - memset(pEncode, 0, size); - - EVP_EncodeBlock(pEncode, (const unsigned char *)pSrc, sLen); -// fprintf(stdout, "Src: \n[%s]\n", pSrc); -// fprintf(stdout, "Base64(%d --> %d | %d) Bytes: \n[%s]\n", sLen, size, strlen(pEncode), pEncode); - - return pEncode; -} - -const char* EvpBase64DecodeNoAlign(const char *pBase64) -{ - int size = 0; - char *pDecode = NULL; - - if(pBase64 == NULL || strlen(pBase64) == 0) - { - return (NULL); - } - - size = strlen(pBase64); - - pDecode = (char*)malloc(size); - memset(pDecode, 0, size); - - //CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); - EVP_DecodeBlock(pDecode, (const unsigned char *)pBase64, size); - //CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); - -// fprintf(stdout, "Decode(%d --> %d) Bytes: \n[%s]\n", size, strlen(pDecode), pDecode); - - return pDecode; -} - -unsigned char* EvpBase64DecodeNoAlignV2(const char *pBase64, int* pOutSize) -{ - int size = 0; - unsigned char *pDecode = NULL; - - if(pBase64 == NULL || strlen(pBase64) == 0) - { - return (NULL); - } - - size = strlen(pBase64); - - pDecode = (char*)malloc(size); - memset(pDecode, 0, size); - - //CRYPTO_w_lock(CRYPTO_LOCK_DYNLOCK); - size = EVP_DecodeBlock(pDecode, (const unsigned char *)pBase64, size); - //CRYPTO_w_unlock(CRYPTO_LOCK_DYNLOCK); - -// fprintf(stdout, "Decode(%d --> %d) Bytes: \n[%s]\n", size, strlen(pDecode), pDecode); - - if(pOutSize) - { - *pOutSize = size; - } - - return (pDecode); -} diff --git a/src/Framework/Crypto/crypto.c b/src/Framework/Crypto/crypto.c deleted file mode 100644 index 226f1a3..0000000 --- a/src/Framework/Crypto/crypto.c +++ /dev/null @@ -1,187 +0,0 @@ -#include -#include -#include -#include - -#include "log.h" -#include "smart_sound.h" -#include "crypto.h" -#include "libuv_dbus.h" - -static uv_mutex_t *pEvpMutex = NULL; -typedef struct -{ - CRYPTO_TYPE type; - unsigned char* pInData; - int iInSize; - unsigned char* pOutData; - unsigned char pKey[EVP_MAX_KEY_LENGTH + 1]; - OnEVPCrypto onEvpEventCb; -} CRYPT_TASK_PARAMS, *PCRYPT_TASK_PARAMS; - -static void FreeEVPWorkCb(uv_work_t* pWork, int status) -{ - PCRYPT_TASK_PARAMS pTask = (PCRYPT_TASK_PARAMS)pWork->data; - free(pTask->pInData); - free(pTask); - free(pWork); -} - -static void OnEVPWorkCb(uv_work_t* pWork) -{ - PCRYPT_TASK_PARAMS pTask = (PCRYPT_TASK_PARAMS)pWork->data; - int iOutSize = 0; - int iError = 0; - - switch(pTask->type) - { - case CRYPTO_AES_ENCRYPT: - iError = EvpAESEncrypto(pTask->pInData, - pTask->iInSize, - pTask->pOutData, - &iOutSize, - pTask->pKey); - break; - - case CRYPTO_AES_DECRYPT: - iError = EvpAESDecrypto(pTask->pInData, - pTask->iInSize, - pTask->pOutData, - &iOutSize, - pTask->pKey); - break; - - case CRYPTO_BASE64_ENCODE: - pTask->pOutData = (unsigned char*)EvpBase64Encode((const char*)pTask->pInData); - iOutSize = strlen((char*)pTask->pOutData); - break; - - case CRYPTO_BASE64_DECODE: - pTask->pOutData = (unsigned char*)EvpBase64Decode((const char*)pTask->pInData); - iOutSize = strlen((char*)pTask->pOutData); - break; - - case CRYPTO_MD5_FILE: - pTask->pOutData = (unsigned char*)EvpMD5HashFile((const char*)pTask->pInData); - iOutSize = strlen((char*)pTask->pOutData); - break; - - default: - iError = -ERR_UNSUP_EVP_TYPE; - } - - if(iError != 0) - { - pTask->onEvpEventCb(pTask->type, pTask->pOutData, 0, pTask->pInData, iError); - } - else - { - pTask->onEvpEventCb(pTask->type, pTask->pOutData, iOutSize, pTask->pInData, 0); - } -} - -int EvpAddCryptoTask(CRYPTO_TYPE type, - unsigned char* pInBuf, - int iSize, - unsigned char* pOutBuf, - char* pKey, - OnEVPCrypto onEvpCryptCb) -{ - uv_work_t* puvWork = NULL; - PCRYPT_TASK_PARAMS pTask = NULL; - PLIBUV_DBUS_PARAMS pContext = DBusLibuvGetRuntime(); - - if(!pContext || !onEvpCryptCb) - { - return -ERR_INPUT_PARAMS; - } - - if(type == CRYPTO_AES_ENCRYPT || type == CRYPTO_AES_DECRYPT) - { - if(pKey == NULL || pOutBuf == NULL) - { - return -ERR_INPUT_PARAMS; - } - - switch(strlen(pKey) * 8) - { - case 128: - case 192: - case 256: - break; - default: - return -ERR_EVP_KEY_SIZE; - } - } - else if(type == CRYPTO_MD5_FILE) - { - uv_fs_t uvFs; - if(uv_fs_access(pContext->pLoop, &uvFs, (const char*)pInBuf, F_OK, NULL) != 0) - { - return -ERR_FILE_NOT_EXISTS; - } - } - - pTask = (PCRYPT_TASK_PARAMS)malloc(sizeof(CRYPT_TASK_PARAMS)); - puvWork = (uv_work_t*)malloc(sizeof(uv_work_t)); - - puvWork->data = (void*)pTask; - - pTask->type = type; - pTask->pInData = (unsigned char*)malloc(iSize + 1); - pTask->iInSize = iSize; - pTask->pOutData = pOutBuf; - pTask->onEvpEventCb = onEvpCryptCb; - - memset(pTask->pInData, 0, iSize + 1); - memset(pTask->pKey, 0, EVP_MAX_KEY_LENGTH + 1); - - if(pKey) - { - strncpy(pTask->pKey, pKey, EVP_MAX_KEY_LENGTH); - } - - memcpy(pTask->pInData, pInBuf, iSize); - - uv_queue_work(pContext->pLoop, puvWork, OnEVPWorkCb, FreeEVPWorkCb); - - return 0; -} - -static void __evpLockCb(int mode, int n, const char *pFile, int line) -{ - if(n >= CRYPTO_num_locks()) - { - return; - } - - if(mode & CRYPTO_LOCK) - { - uv_mutex_lock(&pEvpMutex[n]); - } - else - { - uv_mutex_unlock(&pEvpMutex[n]); - } -} - - -static unsigned long __evpIdCb(void) -{ - return ((unsigned long)uv_thread_self()); -} - -void EvpSystemInit(void) -{ - int i = 0; - - pEvpMutex = (uv_mutex_t *)malloc(CRYPTO_num_locks() * sizeof(uv_mutex_t)); - - for(i = 0; i < CRYPTO_num_locks(); i++) - { - uv_mutex_init(&pEvpMutex[i]); - } - - CRYPTO_set_id_callback(__evpIdCb); - CRYPTO_set_locking_callback(__evpLockCb); -} diff --git a/src/Framework/Crypto/md5.c b/src/Framework/Crypto/md5.c deleted file mode 100644 index 655e7b0..0000000 --- a/src/Framework/Crypto/md5.c +++ /dev/null @@ -1,156 +0,0 @@ -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "crypto.h" -#include "libuv_dbus.h" - -const char* EvpMD5HashFile(const char* pFileName) -{ - int rdSize = 0; - int fd, size; - uint8_t md5[EVP_MAX_MD_SIZE]; - uint8_t buf[1024]; - EVP_MD_CTX ctx; - char* pString = NULL; - - memset(md5, 0, EVP_MAX_MD_SIZE); - EVP_MD_CTX_init(&ctx); - EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); - - fd = open(pFileName, O_RDONLY); - - if(fd == -1) - { - LOG_EX(LOG_Error, "Open File \'%s\' error\n", pFileName); - return NULL; - } - do - { - rdSize = read(fd, buf, 1024); - - if(rdSize > 0) - { - EVP_DigestUpdate(&ctx, buf, rdSize); - } - } while(rdSize > 0); - - close(fd); - - EVP_DigestFinal_ex(&ctx, md5, &rdSize); - EVP_MD_CTX_cleanup(&ctx); - - size = rdSize * 2 + 1; - - pString = (char*)malloc(size); - memset(pString, 0, size); - - IHW_bin2hex(pString, md5, rdSize); - -// print_hex_dump_bytes("MD5_", DUMP_PREFIX_ADDRESS, md5, rdSize); -// fprintf(stdout, "MD5: [%s]\n", pString); - - return (const char*)pString; -} - -int EvpMD5HashFileV2(const char* pFileName, unsigned char md5[16]) -{ - int rdSize = 0; - int fd; - uint8_t buf[1024]; - EVP_MD_CTX ctx; - //char* pString = NULL; - - memset(md5, 0, 16); - EVP_MD_CTX_init(&ctx); - EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); - - fd = open(pFileName, O_RDONLY); - - if(fd == -1) - { - LOG_EX(LOG_Error, "Open File %s error\n", pFileName); - return (-ERR_OPEN_FILE); - } - do - { - rdSize = read(fd, buf, 1024); - - EVP_DigestUpdate(&ctx, buf, rdSize); - } while(rdSize > 0); - - close(fd); - - EVP_DigestFinal_ex(&ctx, md5, &rdSize); - EVP_MD_CTX_cleanup(&ctx); - - -// print_hex_dump_bytes("MD5_", DUMP_PREFIX_ADDRESS, md5, rdSize); -// fprintf(stdout, "MD5: [%s]\n", pString); - - return 0; -} - -int EvpMD5HashBuf(const unsigned char* pBuf, int iBufLen, unsigned char* pOutBuf, int* pOutSize) -{ - EVP_MD_CTX ctx; - - if(pBuf == NULL || pOutBuf == NULL) - { - return (-ERR_INPUT_PARAMS); - } - - memset(pOutBuf, 0, EVP_MAX_MD_SIZE); - EVP_MD_CTX_init(&ctx); - EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); - EVP_DigestUpdate(&ctx, pBuf, iBufLen); - EVP_DigestFinal_ex(&ctx, pOutBuf, &iBufLen); - EVP_MD_CTX_cleanup(&ctx); - - if(pOutSize) - { - *pOutSize = iBufLen; - } - - //print_hex_dump_bytes("MD5_", DUMP_PREFIX_ADDRESS, pOutBuf, iBufLen); - - return 0; -} - -const char* EvpMD5HashBufV2(const unsigned char* pBuf, int iBufLen) -{ - int size = 0; - EVP_MD_CTX ctx; - uint8_t md5[EVP_MAX_MD_SIZE]; - char* pString = NULL; - - if(pBuf == NULL || iBufLen <= 0) - { - return NULL; - } - - memset(md5, 0, EVP_MAX_MD_SIZE); - EVP_MD_CTX_init(&ctx); - EVP_DigestInit_ex(&ctx, EVP_md5(), NULL); - EVP_DigestUpdate(&ctx, pBuf, iBufLen); - EVP_DigestFinal_ex(&ctx, md5, &iBufLen); - EVP_MD_CTX_cleanup(&ctx); - - //print_hex_dump_bytes("MD5_", DUMP_PREFIX_ADDRESS, pOutBuf, iBufLen); - - size = iBufLen * 2 + 1; - - pString = (char*)malloc(size); - memset(pString, 0, size); - - IHW_bin2hex(pString, md5, iBufLen); - -// print_hex_dump_bytes("MD5_", DUMP_PREFIX_ADDRESS, md5, rdSize); -// fprintf(stdout, "MD5: [%s]\n", pString); - - return (const char*)pString; -} diff --git a/src/Framework/HeartDaemon/heart_daemon.c b/src/Framework/HeartDaemon/heart_daemon.c deleted file mode 100644 index 80e190b..0000000 --- a/src/Framework/HeartDaemon/heart_daemon.c +++ /dev/null @@ -1,197 +0,0 @@ -#include -#include -#include -#include -#include "log.h" -#include "libuv_dbus.h" - -typedef struct -{ - MODULE_NAME modName; - uint32_t hTm[MODULE_MAX]; - int isDaemonWork[MODULE_MAX]; - int isConnected[MODULE_MAX]; - OnDaemonMsg pOnHeartLostCb; -} HEART_DAEMON, *PHEART_DAEMON; - -uint32_t g_hblTout = HEART_LOST_DELAY; ///< nano second: heart lost timeout, default 1s -static uv_loop_t *g_DeamonLoop; -static uv_idle_t g_uvDeamonIdle; -static HEART_DAEMON g_heartDaemon; - -static unsigned int g_Cnt = 0; -static int timerExpire(uint32_t tm, uint32_t tExp) -{ - uint32_t now = LIBUV_CURRENT_TIME_MS(); - int64_t diff = now - tm; - - if(tm == 0 || tExp == 0) - { - return 0; - } - - if(diff > tExp * 1000) - { - return 0; - } - else if(diff >= tExp) - { - return 1; - } - - return (0); -} - -static void RunPingSvr(void) -{ - int ret = 0; - unsigned int tm = LIBUV_CURRENT_TIME_MS(); - PING_MSG pMsg; - struct timeval tv; - - gettimeofday(&tv, NULL); - - pMsg.PING = (double)tm / 1000; - pMsg.tmSec = tv.tv_sec; - pMsg.tmMSec = tv.tv_usec; - - ret = DBusJsonBoardcastCommand(NULL, 0xFFFFFFFF, CMD_MISC_PING, JSON_ENGINE_PING, &pMsg, FALSE); - - if(ret != 0) - { - LOG_EX(LOG_Error, "DBus boardcast message error: %d\n", ret); - } -} - -void HeartDaemonHblCheck(void) -{ - if(g_heartDaemon.modName != MODULE_CONTROLLER) - { - if(g_heartDaemon.isDaemonWork[MODULE_CONTROLLER] && timerExpire(g_heartDaemon.hTm[MODULE_CONTROLLER], g_hblTout)) - { - g_heartDaemon.pOnHeartLostCb(MODULE_CONTROLLER, TRUE); - g_heartDaemon.hTm[MODULE_CONTROLLER] = 0; - g_heartDaemon.isConnected[MODULE_CONTROLLER] = FALSE; - } - } - else - { - int i; - - for(i = 0; i < MODULE_MAX; i++) - { - if(g_heartDaemon.isDaemonWork[i] - && i != MODULE_CONTROLLER - && timerExpire(g_heartDaemon.hTm[i], g_hblTout)) - { - g_heartDaemon.pOnHeartLostCb(i, TRUE); - g_heartDaemon.hTm[i] = 0; - g_heartDaemon.isConnected[i] = FALSE; - } - } - } -} - -void HeartDaemonUpgrade(int iWatcher) -{ - if(iWatcher >= MODULE_MAX) - { - return; - } - - if(g_heartDaemon.hTm[iWatcher] == 0) - { - if(g_heartDaemon.modName == MODULE_CONTROLLER) - { - g_heartDaemon.pOnHeartLostCb(iWatcher, FALSE); - } - else if(iWatcher == MODULE_CONTROLLER) - { - g_heartDaemon.pOnHeartLostCb(iWatcher, FALSE); - } - - g_heartDaemon.isConnected[iWatcher] = TRUE; - RunPingSvr(); - } - - g_heartDaemon.hTm[iWatcher] = LIBUV_CURRENT_TIME_MS(); -} - -static int __isSendPingOnTime(void) -{ - static unsigned int tmPre = 0; - unsigned int tm = LIBUV_CURRENT_TIME_MS(); - unsigned int tmOut = HEART_SEND_DELAY; - - if(g_heartDaemon.modName != MODULE_CONTROLLER - && g_heartDaemon.isConnected[MODULE_CONTROLLER] == FALSE) - { - tmOut = 5000; - } - - if(tmPre != 0 && tm - tmPre < tmOut) - { - return (FALSE); - } - - tmPre = tm; - - return (TRUE); -} - -static void __uvIdleCb(uv_idle_t* phuvIdle) -{ - if(DBusLibuvGetRuntime()->onHblCb - && __isSendPingOnTime()) - { - RunPingSvr(); - } - - HeartDaemonHblCheck(); - sleep(1); -} - -static void __uvThreadDaemon(void *pParams) -{ - g_DeamonLoop = uv_loop_new(); - - uv_idle_init(g_DeamonLoop, &g_uvDeamonIdle); - uv_idle_start(&g_uvDeamonIdle, __uvIdleCb); - - uv_run(g_DeamonLoop, UV_RUN_DEFAULT); - - pthread_detach(pthread_self()); -} - -void HeartDaemonInit(MODULE_NAME mod, int msHblTout, OnDaemonMsg cb) -{ - uv_thread_t uvDaemonThread; - int i; - - memset(&g_heartDaemon, 0, sizeof(HEART_DAEMON)); - - if(msHblTout > 0) - { - g_hblTout = msHblTout; - } - - g_heartDaemon.modName = mod; - - for(i = 0; i < MODULE_MAX; i++) - { - if(mod == MODULE_CONTROLLER) - { - g_heartDaemon.isDaemonWork[i] = (g_pModInfoTable[i].modName != MODULE_CONTROLLER) ? TRUE : FALSE; - } - else - { - g_heartDaemon.isDaemonWork[i] = (g_pModInfoTable[i].modName == MODULE_CONTROLLER) ? TRUE : FALSE; - } - - g_heartDaemon.isConnected[i] = FALSE; - } - - g_heartDaemon.pOnHeartLostCb = cb; - - uv_thread_create(&uvDaemonThread, __uvThreadDaemon, NULL); -} diff --git a/src/Framework/IoT/Boardlink/boardlink_iot.c b/src/Framework/IoT/Boardlink/boardlink_iot.c deleted file mode 100644 index 0abd09b..0000000 --- a/src/Framework/IoT/Boardlink/boardlink_iot.c +++ /dev/null @@ -1,181 +0,0 @@ -#ifndef PLATFORM_CPU -#include -#include -#include -#include -#include -#include -#include -#include - -#if defined(PLATFORM_R16) || defined (PLATFORM_CPU) || defined(PLATFORM_R311) -#include "log.h" -#include "libuv_dbus.h" -#include "boardlink_iot.h" -#else -#include -#include -#include -#endif - -#define BL_IOT_PROTO (30) -#define BL_MSG_BUF_MAX (1024 * 4) - -static int g_nlSock = -1; -static unsigned char g_MsgSendBuf[BL_MSG_BUF_MAX]; -static BlMsgCb g_blMsgCb = NULL; - -static void __recvMsgThread(void *pParam) -{ - unsigned char msgBuf[BL_MSG_BUF_MAX]; - struct iovec iov = {msgBuf, BL_MSG_BUF_MAX}; - struct nlmsghdr *pMsgHdr; - struct sockaddr_nl fromAddr; - struct msghdr msgHdr = {&fromAddr, sizeof(fromAddr), &iov, 1, NULL, 0, 0};; - - memset(&fromAddr, 0, sizeof(fromAddr)); - fromAddr.nl_family = AF_NETLINK; - fromAddr.nl_pid = 0; - fromAddr.nl_groups = 0; - - while(TRUE) - { - int len = recvmsg(g_nlSock, &msgHdr, 0); - - if(len > 0) - { - for(pMsgHdr = (struct nlmsghdr *)msgBuf; - NLMSG_OK(pMsgHdr, len); - pMsgHdr = NLMSG_NEXT(pMsgHdr, len)) - { - if(pMsgHdr->nlmsg_type == NLMSG_DONE) - { - continue; - } - else if(pMsgHdr->nlmsg_type == NLMSG_ERROR) - { - continue; - } - else - { - PBL_IOT_MSG pMsg = (PBL_IOT_MSG)NLMSG_DATA(pMsgHdr); - //LOG_EX(LOG_Debug, "Recv Message: %s\n", NLMSG_DATA(pMsgHdr)); - print_hex_dump_bytes("Msg", 0, pMsg, BL_IOT_MSG_LEN(pMsg->msglen)); - if(g_blMsgCb) - { - g_blMsgCb(pMsg); - } - } - } - } - - usleep(1000); - } - pthread_detach(pthread_self()); -} - -static int __nlCreateSocket(void) -{ - struct sockaddr_nl srcAddr; - - g_nlSock = socket(AF_NETLINK, SOCK_RAW, BL_IOT_PROTO); - - if(g_nlSock == -1) - { - LOG_EX(LOG_Error, "Create netlink socker error: %s\n", strerror(errno)); - return -ERR_CREATE_SOCKET; - } - - memset(&srcAddr, 0, sizeof(srcAddr)); - srcAddr.nl_family = AF_NETLINK; - srcAddr.nl_pid = getpid(); - srcAddr.nl_groups = 0; - - if(bind(g_nlSock, (struct sockaddr*)&srcAddr, sizeof(srcAddr)) < 0) - { - LOG_EX(LOG_Error, "Bind netlink socket failed: %s", strerror(errno)); - close(g_nlSock); - return -ERR_BIND_SOCKET; - } - - return 0; -} - -static int __sendMessage(unsigned char* pData, unsigned int len) -{ - struct iovec iov; - struct sockaddr_nl dstAddr; - struct nlmsghdr* pMsgHdr = (struct nlmsghdr*)g_MsgSendBuf; - struct msghdr msgHdr = {&dstAddr, sizeof(dstAddr), &iov, 1, NULL, 0, 0}; - - memset(&dstAddr, 0, sizeof(struct sockaddr_nl)); - dstAddr.nl_family = AF_NETLINK; - dstAddr.nl_pid = 0; - dstAddr.nl_groups = 0; - - memset(pMsgHdr, 0, BL_MSG_BUF_MAX); - pMsgHdr->nlmsg_len = len; - pMsgHdr->nlmsg_pid = getpid(); - pMsgHdr->nlmsg_flags = 0; - memcpy(NLMSG_DATA(pMsgHdr), pData, len); - - iov.iov_base = pMsgHdr; - iov.iov_len = NLMSG_SPACE(BL_MSG_BUF_MAX); - - return sendmsg(g_nlSock, &msgHdr, 0); -} - -int BL_SendBLMsg(BL_IOT_MSG_TYPE msgType, unsigned char* pData, unsigned int len) -{ - BL_IOT_MSG blMsg; - - memset(&blMsg, 0, sizeof(BL_IOT_MSG)); - blMsg.msgType = msgType; - blMsg.msglen = len; - strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS); - - if(pData && len > 0) - { - memcpy(blMsg.msgData, pData, len); - } - - return __sendMessage((unsigned char*)&blMsg, BL_IOT_MSG_LEN(len)); -} - -int BL_SendBLMsgTo(BL_IOT_MSG_TYPE msgType, unsigned char* pData, unsigned int len, unsigned char dstMac[ETH_ALEN]) -{ - BL_IOT_MSG blMsg; - - memset(&blMsg, 0, sizeof(BL_IOT_MSG)); - blMsg.msgType = msgType; - blMsg.msglen = len; - memcpy(blMsg.dstMac, dstMac, ETH_ALEN); - strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS); - - if(pData && len > 0) - { - memcpy(blMsg.msgData, pData, len); - } - - return __sendMessage((unsigned char*)&blMsg, BL_IOT_MSG_LEN(len)); -} - -int BL_Init(BlMsgCb cbOnMsg) -{ - int ret = 0; - uv_thread_t uvRecvThread; - - ret = __nlCreateSocket(); - - if(ret != 0) - { - return ret; - } - - g_blMsgCb = cbOnMsg; - - uv_thread_create(&uvRecvThread, __recvMsgThread, NULL); - - return 0; -} -#endif diff --git a/src/Framework/JsonUtils/json_struct.c b/src/Framework/JsonUtils/json_struct.c deleted file mode 100644 index c9be8c3..0000000 --- a/src/Framework/JsonUtils/json_struct.c +++ /dev/null @@ -1,1218 +0,0 @@ -#include -#include -#include -#include - -#include "log.h" -#include "smart_sound.h" -#include "libuv_dbus.h" -#include "json_struct.h" -#include "crypto.h" -#include "ota.h" -#include "assistant.h" - -typedef const char* (*StructToJsonCb)(void* pStruct); -typedef void* (*JsonToStructCb)(const char* pJsonStr); -typedef void (*Base64Code)(void *pStruct, int enCode); - -typedef struct -{ - JSON_ENGINE_TYPE typeId; - StructToJsonCb s2jCb; - JsonToStructCb j2sCb; - Base64Code base64Cb; -} JSON_ENGINE, *PJSON_ENGINE; - -static const char* __ping_MSG2Json(void* pData) -{ - PPING_MSG pReq = (PPING_MSG)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, double, PING); - s2j_json_set_basic_element(jObject, pReq, int, tmSec); - s2j_json_set_basic_element(jObject, pReq, int, tmMSec); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -static void* __json2PING_MSG(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, PING_MSG); - - memset(sObject, 0, sizeof(PING_MSG)); - - s2j_struct_get_basic_element(sObject, pJson, double, PING); - s2j_struct_get_basic_element(sObject, pJson, int, tmSec); - s2j_struct_get_basic_element(sObject, pJson, int, tmMSec); - - cJSON_Delete(pJson); - - return sObject; -} - -static const char* __cfg_API_REQ2Json(void* pData) -{ - PCFG_API_REQ pReq = (PCFG_API_REQ)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, string, keyName); - s2j_json_set_basic_element(jObject, pReq, string, keyValue); - s2j_json_set_basic_element(jObject, pReq, int, keyType); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -static void* __json2CFG_API_REQ(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, CFG_API_REQ); - - memset(sObject, 0, sizeof(CFG_API_REQ)); - - s2j_struct_get_basic_element(sObject, pJson, string, keyName); - s2j_struct_get_basic_element(sObject, pJson, string, keyValue); - s2j_struct_get_basic_element(sObject, pJson, int, keyType); - - cJSON_Delete(pJson); - - return sObject; -} - -static void __cfg_API_REQBase64(void *pData, int enCode) -{ - PCFG_API_REQ pReq = (PCFG_API_REQ)pData; - - if(enCode) - { - if(strlen(pReq->keyName) > 0) - { - char* pBase64 = (char *)EvpBase64Encode(pReq->keyName); - memset(pReq->keyName, 0, MAX_CFG_KEY_NAME); - strcpy(pReq->keyName, pBase64); - free(pBase64); - } - - if(strlen(pReq->keyValue) > 0) - { - char* pBase64 = (char *)EvpBase64Encode(pReq->keyValue); - memset(pReq->keyValue, 0, MAX_CFG_KEY_VALUE); - strcpy(pReq->keyValue, pBase64); - free(pBase64); - } - } - else - { - if(strlen(pReq->keyName) > 0) - { - - char *pBase64 = (char *)EvpBase64Decode(pReq->keyName); - memset(pReq->keyName, 0, MAX_CFG_KEY_NAME); - strcpy(pReq->keyName, pBase64); - free(pBase64); - } - - if(strlen(pReq->keyValue) > 0) - { - char *pBase64 = (char *)EvpBase64Decode(pReq->keyValue); - memset(pReq->keyValue, 0, MAX_CFG_KEY_VALUE); - strcpy(pReq->keyValue, pBase64); - free(pBase64); - } - } -} - -static const char* __cfg_API_RSP2Json(void* pData) -{ - PCFG_API_RSP pReq = (PCFG_API_RSP)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, string, keyName); - s2j_json_set_basic_element(jObject, pReq, string, keyValue); - s2j_json_set_basic_element(jObject, pReq, int, keyType); - s2j_json_set_basic_element(jObject, pReq, int, errNo); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -static void* __json2CFG_API_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, CFG_API_RSP); - - memset(sObject, 0, sizeof(CFG_API_RSP)); - - s2j_struct_get_basic_element(sObject, pJson, string, keyName); - s2j_struct_get_basic_element(sObject, pJson, string, keyValue); - s2j_struct_get_basic_element(sObject, pJson, int, keyType); - s2j_struct_get_basic_element(sObject, pJson, int, errNo); - - cJSON_Delete(pJson); - - return sObject; -} - -static void __cfg_API_RSPBase64(void *pData, int enCode) -{ - PCFG_API_RSP pReq = (PCFG_API_RSP)pData; - - if(enCode) - { - if(strlen(pReq->keyName) > 0) - { - char *pBase64 = (char *)EvpBase64Encode(pReq->keyName); - memset(pReq->keyName, 0, MAX_CFG_KEY_NAME); - strcpy(pReq->keyName, pBase64); - free(pBase64); - } - - if(strlen(pReq->keyValue) > 0) - { - char *pBase64 = (char *)EvpBase64Encode(pReq->keyValue); - memset(pReq->keyValue, 0, MAX_CFG_KEY_VALUE); - strcpy(pReq->keyValue, pBase64); - free(pBase64); - } - } - else - { - if(strlen(pReq->keyName) > 0) - { - char *pBase64 = (char *)EvpBase64Decode(pReq->keyName); - memset(pReq->keyName, 0, MAX_CFG_KEY_NAME); - strcpy(pReq->keyName, pBase64); - free(pBase64); - } - - if(strlen(pReq->keyValue) > 0) - { - char *pBase64 = (char *)EvpBase64Decode(pReq->keyValue); - memset(pReq->keyValue, 0, MAX_CFG_KEY_VALUE); - strcpy(pReq->keyValue, pBase64); - free(pBase64); - } - } -} - -static const char* __player_TO_CTRL2Json(void* pData) -{ - PPLAYER_TO_CTRL pP2C = (PPLAYER_TO_CTRL)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pP2C, string, musicUuid); - s2j_json_set_basic_element(jObject, pP2C, int, plySt); - s2j_json_set_basic_element(jObject, pP2C, int, curPos); - s2j_json_set_basic_element(jObject, pP2C, int, duration); - - s2j_json_set_basic_element(jObject, pP2C, int, playerId); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -static void* __json2PLAYER_TO_CTRL(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, PLAYER_TO_CTRL); - - memset(sObject, 0, sizeof(PLAYER_TO_CTRL)); - - s2j_struct_get_basic_element(sObject, pJson, string, musicUuid); - s2j_struct_get_basic_element(sObject, pJson, int, plySt); - s2j_struct_get_basic_element(sObject, pJson, int, curPos); - s2j_struct_get_basic_element(sObject, pJson, int, duration); - - cJSON_Delete(pJson); - - return sObject; -} - -static void __player_TO_CTRLBase64(void *pData, int enCode) -{ - PPLAYER_TO_CTRL pReq = (PPLAYER_TO_CTRL)pData; - - if(enCode) - { - if(strlen(pReq->musicUuid) > 0) - { - char *pBase64 = (char *)EvpBase64Encode(pReq->musicUuid); - memset(pReq->musicUuid, 0, MAX_MUSIC_UUID); - strcpy(pReq->musicUuid, pBase64); - free(pBase64); - } - } - else - { - if(strlen(pReq->musicUuid) > 0) - { - char *pBase64 = (char *)EvpBase64Decode(pReq->musicUuid); - memset(pReq->musicUuid, 0, MAX_MUSIC_UUID); - strcpy(pReq->musicUuid, pBase64); - free(pBase64); - } - } -} - -static const char* __ctrl_TO_PLAYER2Json(void* pData) -{ - PCTRL_TO_PLAYER pC2P = (PCTRL_TO_PLAYER)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pC2P, string, src); - s2j_json_set_basic_element(jObject, pC2P, string, srcUuid); - s2j_json_set_basic_element(jObject, pC2P, string, ttsText); - s2j_json_set_basic_element(jObject, pC2P, int, skTime); - s2j_json_set_basic_element(jObject, pC2P, int, plyMode); - s2j_json_set_basic_element(jObject, pC2P, int, plyListType); - s2j_json_set_basic_element(jObject, pC2P, int, adSrcType); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -static void* __json2CTRL_TO_PLAYER(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, CTRL_TO_PLAYER); - - memset(sObject, 0, sizeof(CTRL_TO_PLAYER)); - - s2j_struct_get_basic_element(sObject, pJson, string, src); - s2j_struct_get_basic_element(sObject, pJson, string, srcUuid); - s2j_struct_get_basic_element(sObject, pJson, string, ttsText); - s2j_struct_get_basic_element(sObject, pJson, int, skTime); - s2j_struct_get_basic_element(sObject, pJson, int, plyMode); - s2j_struct_get_basic_element(sObject, pJson, int, plyListType); - s2j_struct_get_basic_element(sObject, pJson, int, adSrcType); - s2j_struct_get_basic_element(sObject, pJson, int, duration); - s2j_struct_get_basic_element(sObject, pJson, int, volRestoreTime); - s2j_struct_get_basic_element(sObject, pJson, int, volBegin); - s2j_struct_get_basic_element(sObject, pJson, string, fifo); - s2j_struct_get_basic_element(sObject, pJson, int, channel); - s2j_struct_get_basic_element(sObject, pJson, int, bytes); - s2j_struct_get_basic_element(sObject, pJson, int, sampleRate); - s2j_struct_get_basic_element(sObject, pJson, int, playerId); - s2j_struct_get_basic_element(sObject, pJson, double, gain); - s2j_struct_get_basic_element(sObject, pJson, string, backGroundUrl); - - cJSON_Delete(pJson); - - return sObject; -} - -static void __ctrl_TO_PLAYERBase64(void *pData, int enCode) -{ - PCTRL_TO_PLAYER pReq = (PCTRL_TO_PLAYER)pData; - - if(enCode) - { - if(strlen(pReq->src) > 0) - { - char *pBase64 = (char *)EvpBase64Encode(pReq->src); - memset(pReq->src, 0, MAX_MUSIC_UUID); - strcpy(pReq->src, pBase64); - free(pBase64); - } - - if(strlen(pReq->srcUuid) > 0) - { - char *pBase64 = (char *)EvpBase64Encode(pReq->srcUuid); - memset(pReq->srcUuid, 0, MAX_MUSIC_UUID); - strcpy(pReq->srcUuid, pBase64); - free(pBase64); - } - } - else - { - if(strlen(pReq->src) > 0) - { - char *pBase64 = (char *)EvpBase64Decode(pReq->src); - memset(pReq->src, 0, MAX_MUSIC_UUID); - strcpy(pReq->src, pBase64); - free(pBase64); - } - - if(strlen(pReq->srcUuid) > 0) - { - char *pBase64 = (char *)EvpBase64Decode(pReq->srcUuid); - memset(pReq->srcUuid, 0, MAX_MUSIC_UUID); - strcpy(pReq->srcUuid, pBase64); - free(pBase64); - } - } -} - -#if 0 -const char* __ota_Notify_REQ2Json(void* pData) -{ - int i; - POTA_DATA_INFO pReq = (POTA_DATA_INFO)pData; - cJSON* pRoot = NULL; - cJSON* pSubArray = NULL; - const char* pJsonS; - - pRoot = cJSON_CreateObject(); - - cJSON_AddNumberToObject(pRoot, "version", pReq->version); - cJSON_AddNumberToObject(pRoot, "otaMode", pReq->otaMode); - - pSubArray = cJSON_CreateArray(); - cJSON_AddItemToObject(pRoot, "otaFile", pSubArray); - - for(i = 0; i < sizeof(pReq->otaFile) / sizeof(pReq->otaFile[0]); i++) - { - cJSON *pItem = cJSON_CreateObject(); - - cJSON_AddItemToObject(pSubArray, "", pItem); - - - cJSON_AddStringToObject(pItem, "url", pReq->otaFile[i].url); - cJSON_AddStringToObject(pItem, "md5", pReq->otaFile[i].md5); - cJSON_AddNumberToObject(pItem, "size", pReq->otaFile[i].size); - } - - pJsonS = cJSON_Print(pRoot); - cJSON_Delete(pRoot); - - return pJsonS; -} - -void* __json2OTA_Notify_REQ(const char* pJsonS) -{ - POTA_DATA_INFO pInfo; - cJSON* pSubArray = NULL; - cJSON* pItem = NULL; - - cJSON* pRoot = cJSON_Parse(pJsonS); - if(pRoot == NULL) - { - return (NULL); - } - - pSubArray = cJSON_GetObjectItem(pRoot, "otaFile"); - - pInfo = (POTA_DATA_INFO)malloc(sizeof(OTA_DATA_INFO)); - memset(pInfo, 0, sizeof(OTA_DATA_INFO)); - - pItem = cJSON_GetObjectItem(pRoot, "version"); - pInfo->version = pItem->valueint; - - pItem = cJSON_GetObjectItem(pRoot, "otaMode"); - pInfo->otaMode = pItem->valueint; - - if(pSubArray) - { - int i; - cJSON *pList = pSubArray->child; - - for(i = 0; i < cJSON_GetArraySize(pSubArray); i++) - { - strcpy(pInfo->otaFile[i].url, cJSON_GetObjectItem(pList, "url")->valuestring); - strcpy(pInfo->otaFile[i].md5, cJSON_GetObjectItem(pList, "md5")->valuestring); - pInfo->otaFile[i].size = cJSON_GetObjectItem(pList, "size")->valueint; - - pList = pList->next; - } - } - - cJSON_Delete(pRoot); - - return pInfo; -} -#else -const char* __ota_Notify_REQ2Json(void* pData) -{ - POTA_DATA_INFO pReq = (POTA_DATA_INFO)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, int, version); - s2j_json_set_basic_element(jObject, pReq, int, otaCmd); - s2j_json_set_basic_element(jObject, pReq, int, otaMode); - - s2j_json_set_struct_element(json_otaRes, jObject, struct_otaRes, pReq, OTA_FILE_INFO, otaFileInfo); - s2j_json_set_basic_element(json_otaRes, struct_otaRes, string, url); - s2j_json_set_basic_element(json_otaRes, struct_otaRes, string, md5); - s2j_json_set_basic_element(json_otaRes, struct_otaRes, int, size); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -void* __json2OTA_Notify_REQ(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, OTA_DATA_INFO); - - memset(sObject, 0, sizeof(OTA_DATA_INFO)); - - s2j_struct_get_basic_element(sObject, pJson, int, version); - s2j_struct_get_basic_element(sObject, pJson, int, otaCmd); - s2j_struct_get_basic_element(sObject, pJson, int, otaMode); - - s2j_struct_get_struct_element(struct_otaRes, sObject, json_otaRes, pJson, OTA_FILE_INFO, otaFileInfo); - s2j_struct_get_basic_element(struct_otaRes, json_otaRes, string, url); - s2j_struct_get_basic_element(struct_otaRes, json_otaRes, string, md5); - s2j_struct_get_basic_element(struct_otaRes, json_otaRes, int, size); - - cJSON_Delete(pJson); - - return sObject; -} -#endif -static void __ota_NotifyBase64(void *pData, int enCode) -{ - POTA_DATA_INFO pReq = (POTA_DATA_INFO)pData; - - if(enCode) - { - if(strlen(pReq->otaFileInfo.url) > 0) - { - char *pBase64 = (char *)EvpBase64EncodeNoAlign(pReq->otaFileInfo.url); - memset(pReq->otaFileInfo.url, 0, SIZE_1K); - strcpy(pReq->otaFileInfo.url, pBase64); - free(pBase64); - } - } - else - { - if(strlen(pReq->otaFileInfo.url) > 0) - { - char *pBase64 = (char *)EvpBase64DecodeNoAlign(pReq->otaFileInfo.url); - memset(pReq->otaFileInfo.url, 0, SIZE_1K); - strcpy(pReq->otaFileInfo.url, pBase64); - free(pBase64); - } - } -} - -const char* __ota_Status_RSP2Json(void* pData) -{ - POTA_RSP_STATUS pReq = (POTA_RSP_STATUS)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, int, status); - s2j_json_set_basic_element(jObject, pReq, int, val); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -void* __json2OTA_Status_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, OTA_RSP_STATUS); - - memset(sObject, 0, sizeof(OTA_RSP_STATUS)); - - s2j_struct_get_basic_element(sObject, pJson, int, status); - s2j_struct_get_basic_element(sObject, pJson, int, val); - - cJSON_Delete(pJson); - - return sObject; -} - -const char* __alarm_Run_Event_RSP2Json(void* pData) -{ - const char* pJsonS; - PASSISTANT_ITEM_INFO pReq = (PASSISTANT_ITEM_INFO)pData; - cJSON* pRoot = cJSON_CreateObject(); - cJSON* pSubArray = cJSON_CreateArray(); - - cJSON_AddItemToObject(pRoot, "data", pSubArray); - - cJSON *pItem = cJSON_CreateObject(); - - cJSON_AddItemToObject(pSubArray, "", pItem); - - cJSON_AddNumberToObject(pItem, "alarmId", pReq->itemId); - cJSON_AddNumberToObject(pItem, "itemType", pReq->itemType); - cJSON_AddNumberToObject(pItem, "priority", pReq->priority); - cJSON_AddNumberToObject(pItem, "repeatMode", pReq->repeatMode); - - cJSON_AddNumberToObject(pItem, "year", pReq->year); - cJSON_AddNumberToObject(pItem, "month", pReq->month); - cJSON_AddNumberToObject(pItem, "day", pReq->day); - cJSON_AddNumberToObject(pItem, "hour", pReq->hour); - cJSON_AddNumberToObject(pItem, "minute", pReq->minute); - cJSON_AddNumberToObject(pItem, "second", pReq->second); - cJSON_AddNumberToObject(pItem, "weekDay", pReq->weekDay); - - cJSON_AddNumberToObject(pItem, "voiceId", pReq->voiceId); - cJSON_AddStringToObject(pItem, "strTips", pReq->strTips); - cJSON_AddStringToObject(pItem, "resUrl", pReq->resUrl); - - cJSON_AddStringToObject(pItem, "voiceResType", pReq->voiceResType); - cJSON_AddStringToObject(pItem, "voiceRes", pReq->voiceRes); - - pJsonS = cJSON_Print(pRoot); - cJSON_Delete(pRoot); - - return pJsonS; -} - -static void __getAlarmDateTimeInfo(cJSON* pJson, PASSISTANT_ITEM_INFO pAlarmInfo) -{ - struct tm tmNow; - time_t timep; - - const char *pStrYear = cJSON_GetObjectItem(pJson, "year")->valuestring; - const char *pStrMonth = cJSON_GetObjectItem(pJson, "month")->valuestring; - const char *pStrDay = cJSON_GetObjectItem(pJson, "dayofMonth")->valuestring; - const char *pStrHour = cJSON_GetObjectItem(pJson, "hour")->valuestring; - const char *pStrMinute = cJSON_GetObjectItem(pJson, "minute")->valuestring; - const char *pStrSecond = cJSON_GetObjectItem(pJson, "second")->valuestring; - const char *pStrWeekDay = cJSON_GetObjectItem(pJson, "dayofWeek")->valuestring; - - time(&timep); - localtime_r(&timep, &tmNow); - - if(pStrYear != NULL && strlen(pStrYear) > 0) - { - pAlarmInfo->year = strtoul(pStrYear, NULL, 10) - 1900; - } - else - { - pAlarmInfo->year = -1;//tmNow.tm_year; - } - - if(pStrMonth != NULL && strlen(pStrMonth) > 0) - { - pAlarmInfo->month = strtoul(pStrMonth, NULL, 10) - 1; - } - else - { - pAlarmInfo->month = -1;//tmNow.tm_mon; - } - - if(pStrDay != NULL && strlen(pStrDay) > 0) - { - pAlarmInfo->day = strtoul(pStrDay, NULL, 10); - } - else - { - pAlarmInfo->day = -1; //tmNow.tm_mday; - } - - if(pStrHour != NULL && strlen(pStrHour) > 0) - { - pAlarmInfo->hour = strtoul(pStrHour, NULL, 10); - } - else - { - pAlarmInfo->hour = -1; - } - - if(pStrMinute != NULL && strlen(pStrMinute) > 0) - { - pAlarmInfo->minute = strtoul(pStrMinute, NULL, 10); - } - else - { - pAlarmInfo->minute = -1; - } - - if(pStrSecond != NULL && strlen(pStrSecond) > 0) - { - pAlarmInfo->second = strtoul(pStrSecond, NULL, 10); - } - else - { - pAlarmInfo->second = -1; - } - - if(pStrWeekDay != NULL && strlen(pStrWeekDay) > 0) - { - if(strchr(pStrWeekDay, '7') != NULL) - { - pAlarmInfo->weekDay |= 1; - } - - if(strchr(pStrWeekDay, '1') != NULL) - { - pAlarmInfo->weekDay |= 1 << 1; - } - - if(strchr(pStrWeekDay, '2') != NULL) - { - pAlarmInfo->weekDay |= 1 << 2; - } - - if(strchr(pStrWeekDay, '3') != NULL) - { - pAlarmInfo->weekDay |= 1 << 3; - } - - if(strchr(pStrWeekDay, '4') != NULL) - { - pAlarmInfo->weekDay |= 1 << 4; - } - - if(strchr(pStrWeekDay, '5') != NULL) - { - pAlarmInfo->weekDay |= 1 << 5; - } - - if(strchr(pStrWeekDay, '6') != NULL) - { - pAlarmInfo->weekDay |= 1 << 6; - } - } - else - { - pAlarmInfo->weekDay = 0; - } -} - -void* __json2Alarm_sync_RSP(const char *pJsonS) -{ - PASSISTANT_SYNC_INFO pInfo; - cJSON* pSubArray = NULL; - //cJSON* pItem = NULL; - - cJSON* pRoot = cJSON_Parse(pJsonS); - if(pRoot == NULL) - { - return (NULL); - } - - pSubArray = cJSON_GetObjectItem(pRoot, "data"); - - pInfo = (PASSISTANT_SYNC_INFO)malloc(sizeof(ASSISTANT_SYNC_INFO)); - memset(pInfo, 0, sizeof(ASSISTANT_SYNC_INFO)); - - pInfo->pAlarmInfo = NULL; - - if(pSubArray) - { - cJSON *pList = pSubArray->child; - int arraySize = cJSON_GetArraySize(pSubArray); - - if(arraySize > 0) - { - pInfo->pAlarmInfo = (PASSISTANT_ITEM_INFO)malloc(sizeof(ASSISTANT_ITEM_INFO) * arraySize); - - pInfo->nItems = arraySize; - - for(int i = 0; i < arraySize; i++) - { - cJSON* pData = cJSON_GetObjectItem(pList, "vboxDate"); - - memset(&pInfo->pAlarmInfo[i], 0, sizeof(ASSISTANT_ITEM_INFO)); - - if(pData != NULL) - { - cJSON *pItem; - - __getAlarmDateTimeInfo(pData, &pInfo->pAlarmInfo[i]); - - pInfo->pAlarmInfo[i].itemType = cJSON_GetObjectItem(pList, "label")->valueint; - pInfo->pAlarmInfo[i].itemId = cJSON_GetObjectItem(pList, "id")->valueint; - pInfo->pAlarmInfo[i].repeatMode = cJSON_GetObjectItem(pList, "type")->valueint; - pInfo->pAlarmInfo[i].voiceId = cJSON_GetObjectItem(pList, "voiceType")->valueint; - - pItem = cJSON_GetObjectItem(pList, "remark"); - - if(pInfo->pAlarmInfo[i].itemType != ASSISTANT_TYPE_CLOCK && pItem) - { - strcpy(pInfo->pAlarmInfo[i].strTips, pItem->valuestring); - } - - pItem = cJSON_GetObjectItem(pList, "resUrl"); - - if(pItem) - { - strcpy(pInfo->pAlarmInfo[i].resUrl, pItem->valuestring); - } - - pItem = cJSON_GetObjectItem(pList, "voiceRes"); - - if(pItem) - { - strcpy(pInfo->pAlarmInfo[i].voiceRes, pItem->valuestring); - } - - pItem = cJSON_GetObjectItem(pList, "voiceResType"); - - if(pItem) - { - strcpy(pInfo->pAlarmInfo[i].voiceResType, pItem->valuestring); - } - - pList = pList->next; -#if 0 - fprintf(stdout, "%04u-%02u-%02u %02u:%02u:%02u %u\n", - pInfo->pAlarmInfo[i].year, - pInfo->pAlarmInfo[i].month, - pInfo->pAlarmInfo[i].day, - pInfo->pAlarmInfo[i].hour, - pInfo->pAlarmInfo[i].minute, - pInfo->pAlarmInfo[i].second, - pInfo->pAlarmInfo[i].weekDay); -#endif - } - } - } - } - - cJSON_Delete(pRoot); - - return pInfo; -} - -const char* __alarm_Status_RSP2Json(void* pData) -{ - const char* pJsonS; - - PASSISTANT_RSP_STATUS pReq = (PASSISTANT_RSP_STATUS)pData; - - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, int, cmd); - s2j_json_set_basic_element(jObject, pReq, int, val); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -void* __json2Alarm_Status_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, ASSISTANT_RSP_STATUS); - - memset(sObject, 0, sizeof(ASSISTANT_RSP_STATUS)); - - s2j_struct_get_basic_element(sObject, pJson, int, cmd); - s2j_struct_get_basic_element(sObject, pJson, int, val); - - cJSON_Delete(pJson); - - return sObject; - -} - -const char* __alarm_change_RSP2Json(void* pData) -{ - const char* pJsonS; - PASSISTANT_NOTIFY_INFO pReq = (PASSISTANT_NOTIFY_INFO)pData; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, int, cmd); - s2j_json_set_basic_element(jObject, pReq, int, type); - s2j_json_set_array_element(jObject, pReq, double, ids, pReq->nItems); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -void* __json2Alarm_change_RSP(const char* pJsonS) -{ - PASSISTANT_NOTIFY_INFO pInfo = NULL; - cJSON* pSubArray = NULL; - cJSON* pItem = NULL; - - cJSON* pRoot = cJSON_Parse(pJsonS); - if(pRoot == NULL) - { - return (NULL); - } - - pSubArray = cJSON_GetObjectItem(pRoot, "ids"); - - pInfo = (PASSISTANT_NOTIFY_INFO)malloc(sizeof(ASSISTANT_NOTIFY_INFO)); - memset(pInfo, 0, sizeof(ASSISTANT_NOTIFY_INFO)); - - pItem = cJSON_GetObjectItem(pRoot, "type"); - pInfo->type = pItem->valueint; - -#if 0 - pItem = cJSON_GetObjectItem(pRoot, "cmd"); - pInfo->cmd = pItem->valueint; -#endif - - if(pSubArray) - { - int arraySize = cJSON_GetArraySize(pSubArray); - - if(arraySize > 0) - { - pInfo->nItems = arraySize; - - for(int i = 0; i < arraySize; i++) - { - pInfo->ids[i] = (unsigned long long)(cJSON_GetArrayItem(pSubArray, i)->valuedouble); - } - } - } - - cJSON_Delete(pRoot); - - return pInfo; -} -#if 0 -const char* __alarm_change_RSP2Json(void* pData) -{ - int i = 0; - PALARM_REMOVE_INFO pReq = (PALARM_REMOVE_INFO)pData; - cJSON* pRoot = NULL; - cJSON* pSubArray = NULL; - const char* pJsonS; - - pRoot = cJSON_CreateObject(); - - cJSON_AddNumberToObject(pRoot, "nItems", pReq->nItems); - - pSubArray = cJSON_CreateArray(); - cJSON_AddItemToObject(pRoot, "data", pSubArray); - - for(i = 0; i < pReq->nItems; i++) - { - cJSON *pItem = cJSON_CreateObject(); - - cJSON_AddItemToObject(pSubArray, "", pItem); - - cJSON_AddNumberToObject(pItem, "alarmId", pReq->pItemInfo[i].alarmId); - cJSON_AddNumberToObject(pItem, "itemType", pReq->pItemInfo[i].itemType); - } - - pJsonS = cJSON_Print(pRoot); - cJSON_Delete(pRoot); - - return pJsonS; -} - -void* __json2Alarm_change_RSP(const char* pJsonS) -{ - PALARM_REMOVE_INFO pInfo = NULL; - cJSON* pRoot = cJSON_Parse(pJsonS); - cJSON* pSubArray = NULL; - cJSON* pItem = NULL; - int itemType = 0; - - if(pRoot == NULL) - { - return (NULL); - } - - pSubArray = cJSON_GetObjectItem(pRoot, "data"); - - pInfo = (PALARM_REMOVE_INFO)malloc(sizeof(ALARM_REMOVE_INFO)); - memset(pInfo, 0, sizeof(ALARM_REMOVE_INFO)); - - pItem = cJSON_GetObjectItem(pRoot, "type"); - itemType = pItem->valueint; - - pInfo->pItemInfo = NULL; - - if(pSubArray) - { - int i; - cJSON *pList = pSubArray->child; - int arraySize = cJSON_GetArraySize(pSubArray); - - if(arraySize > 0) - { - pInfo->pItemInfo = (PITEM_INFO)malloc(sizeof(ITEM_INFO) * arraySize); - pInfo->nItems = arraySize; - - for(i = 0; i < arraySize; i++) - { - memset(&pInfo->pItemInfo[i], 0, sizeof(ALARM_ITEM_INFO)); - - pInfo->pItemInfo[i].itemType = itemType; - pInfo->pItemInfo[i].alarmId = cJSON_GetObjectItem(pList, "id")->valueint; - - pList = pList->next; - } - } - } - - cJSON_Delete(pRoot); - - return pInfo; -} -#endif - -const char* __alarm_WorkData_RSP2Json(void* pData) -{ - const char* pJsonS; - PWORKDAY_INFO pReq = (PWORKDAY_INFO)pData; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, int, year); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -void* __json2Alarm_WorkData_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, WORKDAY_INFO); - - memset(sObject, 0, sizeof(WORKDAY_INFO)); - - s2j_struct_get_basic_element(sObject, pJson, int, year); - s2j_struct_get_array_element(sObject, pJson, int, days); - - cJSON_Delete(pJson); - - return sObject; -} - - -const char* __logCfg_RSP2Json(void* pData) -{ - PLOG_CFG_PROTOCOL pReq = (PLOG_CFG_PROTOCOL)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, int, cfgCmd); - s2j_json_set_basic_element(jObject, pReq, int, iParams1); - s2j_json_set_basic_element(jObject, pReq, int, iParams2); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -void* __json2LogCfg_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, LOG_CFG_PROTOCOL); - - memset(sObject, 0, sizeof(LOG_CFG_PROTOCOL)); - - s2j_struct_get_basic_element(sObject, pJson, int, cfgCmd); - s2j_struct_get_basic_element(sObject, pJson, int, iParams1); - s2j_struct_get_basic_element(sObject, pJson, int, iParams2); - - cJSON_Delete(pJson); - - return sObject; -} - -const char* __wifiStatus_RSP2Json(void* pData) -{ - PWIFI_STATUS_PRO pReq = (PWIFI_STATUS_PRO)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, int, wifi_evt); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -void* __json2WifiStatus_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, WIFI_STATUS_PRO); - - memset(sObject, 0, sizeof(WIFI_STATUS_PRO)); - - s2j_struct_get_basic_element(sObject, pJson, int, wifi_evt); - - cJSON_Delete(pJson); - - return sObject; -} - -void* __json2McuGuideCmd_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, MCU_TEST_GUIDE_CMD); - - memset(sObject, 0, sizeof(MCU_TEST_GUIDE_CMD)); - - s2j_struct_get_basic_element(sObject, pJson, int, red); - s2j_struct_get_basic_element(sObject, pJson, int, green); - s2j_struct_get_basic_element(sObject, pJson, int, blue); - - cJSON_Delete(pJson); - - return sObject; -} - -void* __json2McuMatrixCmd_RSP(const char* pJsonS) -{ - cJSON* pJson = cJSON_Parse(pJsonS); - - s2j_create_struct_obj(sObject, MCU_TEST_MATRIX_CMD); - - memset(sObject, 0, sizeof(MCU_TEST_MATRIX_CMD)); - - s2j_struct_get_basic_element(sObject, pJson, int, level); - - cJSON_Delete(pJson); - - return sObject; -} - -const char* __mcuVersion_RSP2Json(void* pData) -{ - PMCU_TEST_VER_CMD pReq = (PMCU_TEST_VER_CMD)pData; - const char* pJsonS; - - s2j_create_json_obj(jObject); - - s2j_json_set_basic_element(jObject, pReq, string, McuVer); - - pJsonS = cJSON_Print(jObject); - cJSON_Delete(jObject); - - return pJsonS; -} - -static JSON_ENGINE g_jSonEngine[] = -{ - {JSON_ENGINE_P2C, __player_TO_CTRL2Json, __json2PLAYER_TO_CTRL, __player_TO_CTRLBase64}, - {JSON_ENGINE_C2P, __ctrl_TO_PLAYER2Json, __json2CTRL_TO_PLAYER, __ctrl_TO_PLAYERBase64}, - {JSON_ENGINE_CFG_REQ, __cfg_API_REQ2Json, __json2CFG_API_REQ, __cfg_API_REQBase64}, - {JSON_ENGINE_CFG_RSP, __cfg_API_RSP2Json, __json2CFG_API_RSP, __cfg_API_RSPBase64 }, - {JSON_ENGINE_ASSISTANT_SYNC_RSP, NULL, __json2Alarm_sync_RSP, NULL}, - {JSON_ENGINE_ASSISTANT_NOTIFY, __alarm_change_RSP2Json, __json2Alarm_change_RSP, NULL}, - {JSON_ENGINE_ASSISTANT_STATUS, __alarm_Status_RSP2Json, __json2Alarm_Status_RSP, NULL}, - {JSON_ENGINE_ASSISTANT_RUNNING, __alarm_Run_Event_RSP2Json, NULL, NULL}, - {JSON_ENGINE_WORKDAY_REQ, __alarm_WorkData_RSP2Json, __json2Alarm_WorkData_RSP, NULL}, - {JSON_ENGINE_PING, __ping_MSG2Json, __json2PING_MSG, NULL}, - {JSON_ENGINE_OTA_REQ, __ota_Notify_REQ2Json, __json2OTA_Notify_REQ, __ota_NotifyBase64}, - {JSON_ENGINE_OTA_RSP, __ota_Status_RSP2Json, __json2OTA_Status_RSP, NULL}, - {JSON_ENGINE_LOG_CFG_CMD, __logCfg_RSP2Json, __json2LogCfg_RSP, NULL}, - {JSON_WIFI_STATUS_NOTIFY, __wifiStatus_RSP2Json, __json2WifiStatus_RSP, NULL}, - {JSON_MCU_GUIDE_TEST_CMD, NULL, __json2McuGuideCmd_RSP, NULL}, - {JSON_MCU_MATRIX_TEST_CMD, NULL, __json2McuMatrixCmd_RSP, NULL}, - {JSON_MCU_TEST_GET_VER_CMD, __mcuVersion_RSP2Json, NULL, NULL}, -}; - -void* Json2Struct(const char* pJsonStr, JSON_ENGINE_TYPE type, int enBase64, int* pErr) -{ - if(pJsonStr == NULL || pErr == NULL) - { - if(pErr) - { - *pErr = -ERR_INPUT_PARAMS; - } - return NULL; - } - - if(type < 0 || type >= JSON_ENGINE_MAX) - { - *pErr = -ERR_INPUT_PARAMS; - return NULL; - } - - *pErr = 0; - - //LOG_EX(LOG_Debug, "Json:\n%s\n", pJsonStr); - - if(g_jSonEngine[type].j2sCb) - { - void *pStruct = g_jSonEngine[type].j2sCb(pJsonStr); - - if(enBase64 && g_jSonEngine[type].base64Cb) - { - g_jSonEngine[type].base64Cb(pStruct, FALSE); - } - - return (pStruct); - } - else - { - return (NULL); - } -} - -const char* Struct2Json(void* pStruct, JSON_ENGINE_TYPE type, int enBase64, int* pErr) -{ - if(pStruct == NULL || pErr == NULL) - { - if(pErr) - { - *pErr = -ERR_INPUT_PARAMS; - } - return NULL; - } - - if(type < 0 || type >= JSON_ENGINE_MAX) - { - *pErr = -ERR_INPUT_PARAMS; - return NULL; - } - - *pErr = 0; - - if(enBase64 && g_jSonEngine[type].base64Cb) - { - g_jSonEngine[type].base64Cb(pStruct, TRUE); - } - - if(g_jSonEngine[type].s2jCb) - { - const char *pJsongStr = g_jSonEngine[type].s2jCb(pStruct); - - //LOG_EX(LOG_Debug, "Json: \n%s\n", pJsongStr); - return (pJsongStr); - } - else - { - return NULL; - } -} diff --git a/src/Framework/Monitor/monitor.c b/src/Framework/Monitor/monitor.c deleted file mode 100644 index 6e78f6b..0000000 --- a/src/Framework/Monitor/monitor.c +++ /dev/null @@ -1,347 +0,0 @@ -#ifdef ENABLE_COUNT_DEBUG -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "libuv_dbus.h" - -#define LOG_SAVE_TIME (1000) - -typedef struct -{ - long long maxValue; - long long minValue; - long long avgValue; -} STATISTICAL_VALUE, *PSTATISTICAL_VALUE; - -typedef struct -{ - long long curVaule; - long long tolValue; - unsigned int tolCount; - STATISTICAL_VALUE cVal; -} CSTATISTICAL_INFO, *PSTATISTICAL_INFO; - -typedef struct -{ - char* pMonName; - unsigned long nCount; - CSTATISTICAL_INFO nCstInfo; - uv_rwlock_t rwLock; - - uv_timer_t logTimer; - unsigned int logTime; - - UT_hash_handle hh; ///< UT Hash handle -} MONITOR_INFO, *PMONITOR_INFO; - -static uv_rwlock_t g_uvMonRwLock; -static PMONITOR_INFO g_MonTbl = NULL; -static uv_loop_t* g_MonLogLoop = NULL; - -static void __uvMonLogProc(void *pParams) -{ - RunUVLoop(g_MonLogLoop); - - while(TRUE) - { - usleep(1000); - } - - pthread_detach(pthread_self()); -} - -static void __logMonTimerProc(uv_timer_t* pTimer) -{ - PMONITOR_INFO pInfo = (PMONITOR_INFO)pTimer->data; - - if(pInfo && (pInfo->nCount + pInfo->nCstInfo.tolCount > 0)) - { - UT_string* pMsg = NULL; - utstring_new(pMsg); - - uv_rwlock_rdlock(&pInfo->rwLock); - - utstring_printf(pMsg, "%s Statistical Information:\n", pInfo->pMonName); - //LOG_EX(LOG_Debug, "%s Statistical Information:\n", pInfo->pMonName); - - if(pInfo->nCount) - { - UT_string* pMsgCount = NULL; - utstring_new(pMsgCount); - utstring_printf(pMsgCount, " Total Count = %lu\n", pInfo->nCount); - utstring_concat(pMsg, pMsgCount); - utstring_free(pMsgCount); - //LOG_EX(LOG_Debug, " Total Count = %u\n", pInfo->nCount); - } - - if(pInfo->nCstInfo.tolCount > 0) - { - UT_string* pMsgStat = NULL; - utstring_new(pMsgStat); - utstring_printf(pMsgStat, " Max Value = %lld\n" - " Min Value = %lld\n" - " Avg Value = %lld\n" - " ---- Statistical by total %lld of %u times\n", - pInfo->nCstInfo.cVal.maxValue, - pInfo->nCstInfo.cVal.minValue, - pInfo->nCstInfo.cVal.avgValue, - pInfo->nCstInfo.tolValue, - pInfo->nCstInfo.tolCount); - utstring_concat(pMsg, pMsgStat); - utstring_free(pMsgStat); -#if 0 - LOG_EX(LOG_Debug, " Max Value = %lld\n", pInfo->nCstInfo.cVal.maxValue); - LOG_EX(LOG_Debug, " Min Value = %lld\n", pInfo->nCstInfo.cVal.minValue); - LOG_EX(LOG_Debug, " Avg Value = %lld\n", pInfo->nCstInfo.cVal.avgValue); - LOG_EX(LOG_Debug, " ---- Statistical by total %lld of %u times\n", - pInfo->nCstInfo.tolValue, pInfo->nCstInfo.tolCount); -#endif - } - - LOG_EX(LOG_Debug, "%s", utstring_body(pMsg)); - - uv_rwlock_rdunlock(&pInfo->rwLock); - utstring_free(pMsg); - } -} - -int MonitorInit(void) -{ - uv_thread_t uvMonLogThread; - - g_MonLogLoop = uv_loop_new(); - - uv_rwlock_init(&g_uvMonRwLock); - - uv_thread_create(&uvMonLogThread, __uvMonLogProc, NULL); - - return 0; -} - -int MonAddNewItem(const char* pName, int logSaveTime) -{ - PMONITOR_INFO pInfo; - - if(pName == NULL || strlen(pName) == 0) - { - return -ERR_INPUT_PARAMS; - } - - uv_rwlock_rdlock(&g_uvMonRwLock); - HASH_FIND_STR(g_MonTbl, pName, pInfo); - uv_rwlock_rdunlock(&g_uvMonRwLock); - - if(pInfo != NULL) - { - return -ERR_CFG_ITEM_EXIST; - } - - pInfo = (PMONITOR_INFO)malloc(sizeof(MONITOR_INFO)); - - if(pInfo == NULL) - { - return -ERR_MALLOC_MEMORY; - } - - memset(pInfo, 0, sizeof(MONITOR_INFO)); - - pInfo->nCstInfo.cVal.minValue = INT_MAX; - pInfo->logTime = logSaveTime; - pInfo->pMonName = strdup(pName); - - if(pInfo->logTime > 0) - { - pInfo->logTimer.data = pInfo; - uv_timer_init(g_MonLogLoop, &pInfo->logTimer); - uv_timer_start(&pInfo->logTimer, __logMonTimerProc, pInfo->logTime, pInfo->logTime); - } - else - { - pInfo->logTime = 0; - } - - uv_rwlock_init(&pInfo->rwLock); - - uv_rwlock_wrlock(&g_uvMonRwLock); - HASH_ADD_STR(g_MonTbl, pMonName, pInfo); - uv_rwlock_wrunlock(&g_uvMonRwLock); - - return 0; -} - -int MonIncreaseCount(const char* pName) -{ - PMONITOR_INFO pInfo; - - if(pName == NULL || strlen(pName) == 0) - { - return -ERR_INPUT_PARAMS; - } - - uv_rwlock_rdlock(&g_uvMonRwLock); - HASH_FIND_STR(g_MonTbl, pName, pInfo); - uv_rwlock_rdunlock(&g_uvMonRwLock); - - if(pInfo == NULL) - { - return -ERR_CFG_NOITEM; - } - - uv_rwlock_wrlock(&pInfo->rwLock); - pInfo->nCount++; - uv_rwlock_wrunlock(&pInfo->rwLock); - - return 0; -} - -int MonDiffStatistical(const char* pName, long long newVal) -{ - PMONITOR_INFO pInfo; - - if(pName == NULL || strlen(pName) == 0) - { - return -ERR_INPUT_PARAMS; - } - - uv_rwlock_rdlock(&g_uvMonRwLock); - HASH_FIND_STR(g_MonTbl, pName, pInfo); - uv_rwlock_rdunlock(&g_uvMonRwLock); - - if(pInfo == NULL) - { - return -ERR_CFG_NOITEM; - } - - uv_rwlock_wrlock(&pInfo->rwLock); - - if(pInfo->nCstInfo.curVaule != 0) - { - long long diffValue = newVal - pInfo->nCstInfo.curVaule; - - pInfo->nCstInfo.tolValue += diffValue; - pInfo->nCstInfo.tolCount++; - - if(pInfo->nCstInfo.tolCount > 10) - { - pInfo->nCstInfo.cVal.avgValue = pInfo->nCstInfo.tolValue / pInfo->nCstInfo.tolCount; - - if(pInfo->nCstInfo.cVal.maxValue < diffValue) - { - pInfo->nCstInfo.cVal.maxValue = diffValue; - } - - if(pInfo->nCstInfo.cVal.minValue > diffValue) - { - pInfo->nCstInfo.cVal.minValue = diffValue; - } - } - } - - pInfo->nCstInfo.curVaule = newVal; - uv_rwlock_wrunlock(&pInfo->rwLock); - - //fprintf(stdout, "%s value %lld diffValue %lld\n", pName, newVal, diffValue); - return 0; -} - -int MonUpgradeStatistical(const char* pName, long newVal) -{ - PMONITOR_INFO pInfo; - - if(pName == NULL || strlen(pName) == 0) - { - return -ERR_INPUT_PARAMS; - } - - uv_rwlock_rdlock(&g_uvMonRwLock); - HASH_FIND_STR(g_MonTbl, pName, pInfo); - uv_rwlock_rdunlock(&g_uvMonRwLock); - - if(pInfo == NULL) - { - return -ERR_CFG_NOITEM; - } - - uv_rwlock_wrlock(&pInfo->rwLock); - - pInfo->nCstInfo.curVaule = newVal; - pInfo->nCstInfo.tolValue += newVal; - pInfo->nCstInfo.tolCount++; - pInfo->nCstInfo.cVal.avgValue = pInfo->nCstInfo.tolValue / pInfo->nCstInfo.tolCount; - - if(pInfo->nCstInfo.cVal.maxValue < newVal) - { - pInfo->nCstInfo.cVal.maxValue = newVal; - } - - if(pInfo->nCstInfo.cVal.minValue > newVal) - { - pInfo->nCstInfo.cVal.minValue = newVal; - } - - uv_rwlock_wrunlock(&pInfo->rwLock); - - //fprintf(stdout, "%s value %ld\n", pName, newVal); - return 0; -} - -int MonItemLogout(const char* pName) -{ - PMONITOR_INFO pInfo; - - if(pName == NULL || strlen(pName) == 0) - { - return -ERR_INPUT_PARAMS; - } - - uv_rwlock_rdlock(&g_uvMonRwLock); - HASH_FIND_STR(g_MonTbl, pName, pInfo); - uv_rwlock_rdunlock(&g_uvMonRwLock); - - if(pInfo == NULL) - { - return -ERR_CFG_NOITEM; - } - - if(pInfo->nCount + pInfo->nCstInfo.tolCount == 0) - { - LOG_EX(LOG_Debug, "%s Statistical Unchanged\n", pInfo->pMonName); - return 0; - } - - uv_rwlock_rdlock(&pInfo->rwLock); - - LOG_EX(LOG_Debug, "%s Statistical Information:\n", pInfo->pMonName); - - if(pInfo->nCount) - { - LOG_EX(LOG_Debug, " Total Count = %u\n", pInfo->nCount); - } - - if(pInfo->nCstInfo.tolCount > 0) - { - LOG_EX(LOG_Debug, " Max Value = %lld\n", pInfo->nCstInfo.cVal.maxValue); - LOG_EX(LOG_Debug, " Min Value = %lld\n", pInfo->nCstInfo.cVal.minValue); - LOG_EX(LOG_Debug, " Avg Value = %lld\n", pInfo->nCstInfo.cVal.avgValue); - LOG_EX(LOG_Debug, " ---- Statistical by total %lld of %u times\n", - pInfo->nCstInfo.tolValue, pInfo->nCstInfo.tolCount); - } - - uv_rwlock_rdunlock(&pInfo->rwLock); - - return 0; -} -#endif diff --git a/src/Framework/Skins/skins.c b/src/Framework/Skins/skins.c deleted file mode 100644 index 2fc624b..0000000 --- a/src/Framework/Skins/skins.c +++ /dev/null @@ -1,1442 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "log.h" -#include "crypto.h" -#include "json_struct.h" -#include "config_engine.h" -#include "skins_res.h" -#include "inet_api.h" - -#define MAX_DOWNLOAD_ITEMS (10) - -#define DEBUG_SQL_CMD (0) - -typedef enum -{ - STATUS_CREATE_NEW = 0, - STATUS_DOWNLOADING, - STATUS_DOWNLOADED, -} RES_DOWNLOAD_STATUS; - -typedef enum -{ - UPG_STATUS_ADDNEW = (1 << 0), - UPG_STATUS_DL_NEW = (1 << 1), - UPG_STATUS_DOWNLOADED = (1 << 2), -} UPGRADE_RES_STATUS; - -typedef struct -{ - char* pKeyName; - char* pResPath; - - UT_hash_handle hh; ///< UT Hash handle -} SKINS_CACHE_ITEM, *PSKINS_CACHE_ITEM; -static uv_rwlock_t g_SkinCacheRwLock; - -#ifdef PLATFORM_CPU -#define DOWNLOAD_RES_ROOT_PATH (DEF_SKINS_ROOT_PATH) -#else -#define DOWNLOAD_RES_ROOT_PATH ("/mnt/UDISK/resources/") -#endif - -//select * FROM upgrade_skin WHERE resVersion == 4 ORDER BY resType DESC, RANDOM() LIMIT 1 -//#define SKIN_GET_SQL_CMD ("") - -#define ATTACH_DB_SQL_CMD ("ATTACH \'%s\' AS \'%s\'") -#define UPGRADE_TBL_SQL_CMD ("CREATE TABLE IF NOT EXISTS %supgrade (" \ - "ID INTEGER PRIMARY KEY AUTOINCREMENT," \ - "keyName TEXT NOT NULL," \ - "resType INTEGER NOT NULL," \ - "priority INTEGER NOT NULL DEFAULT (1)," \ - "resVersion TEXT NOT NULL," \ - "localPath TEXT NOT NULL," \ - "serverURL TEXT NOT NULL," \ - "md5Chksum TEXT NOT NULL," \ - "resReadme TEXT NOT NULL DEFAULT (\'\')," \ - "resStatus INTEGER NOT NULL DEFAULT (1)," \ - "dlId TEXT NOT NULL DEFAULT (\'\')," \ - "dlStatus INTEGER NOT NULL DEFAULT (0)," \ - "dlRetrys INTEGER NOT NULL DEFAULT (0)," \ - "dlTime TimeStamp NOT NULL DEFAULT (datetime('now','localtime')));") - -#define CREATE_TMP_TBL_SQL "CREATE TABLE IF NOT EXISTS %stmp (" \ - "ID INTEGER PRIMARY KEY AUTOINCREMENT," \ - "localPath TEXT NOT NULL);" - -static int g_isDlFinished = FALSE; -static unsigned int g_curDlItems = 0; -static sqlite3* g_pMemDb = NULL; -static uv_rwlock_t g_uvSkinRwLock; -static uv_rwlock_t g_uvDlRwLock; -static const unsigned int g_tolDefaultSkins = (sizeof(g_SkinDefaultResTable) / sizeof(g_SkinDefaultResTable[0])); -static PSKINS_CACHE_ITEM g_pSkinCackeTbl = NULL; - -char* TblGetSkinsResource(char *pKeyName, int *pResType, int *pVersion, char **pComeFrom); -static int __getUpgradeInfo(const char *pUpdFilePath); -static int __reTryDlSkinResCb(void *pData, int argc, char **argv, char **azColName); -extern int InitSkinRomDatabase(sqlite3 *pDataBase); -static int g_isVerifyRes = FALSE; -static int g_isUpgradeRunning = FALSE; - -static int __dumpSkinsInfo(PSKIN_RES_INFO pItem, int nItems) -{ - LOG_EX2(LOG_Debug, "Skins Items Information:\n"); - - LOG_EX2(LOG_Debug, "-------------------------------------------------------------" - "--------------------------------------------------------\n"); - - LOG_EX2(LOG_Debug, "| Key Name | Type | MD5 Checksum |" - " Contexts " - " |\n"); - - LOG_EX2(LOG_Debug, "-------------------------------------------------------------" - "--------------------------------------------------------\n"); - - for(int i = 0; i < nItems && pItem; i++, pItem++) - { - LOG_EX2(LOG_Debug, "| %-8s | %-8s | %-32s | %-80s |\n", - pItem->pKeyName, pItem->pResVer, pItem->pMD5Chksum, pItem->pLocalPath); - } - - LOG_EX2(LOG_Debug, "-------------------------------------------------------------" - "--------------------------------------------------------\n"); -} - -static int __verifyResMD5Chksum(const char* pPath, const char* pChksum, int resType) -{ - const char* pMD5Chksum; - - if(resType == TEXT_RES) - { - return (TRUE); - } - - if(pPath == NULL || pChksum == NULL) - { - return (FALSE); - } - - switch(resType) - { - case IMAGE_RES: - case VOICE_RES: - pMD5Chksum = EvpMD5HashFile(pPath); - - if(pMD5Chksum == NULL) - { - return (FALSE); - } - - if(strcmp(pMD5Chksum, pChksum) != 0) - { - LOG_EX(LOG_Error, "%s MD5 Verify Error: %s -> %s\n", pPath, pMD5Chksum, pChksum); - - free((char*)pMD5Chksum); - return (FALSE); - } - else - { - free((char*)pMD5Chksum); - } - - break; - - default: - return (FALSE); - } - - return (TRUE); -} - -static void __checkRes(sqlite3_context *pContext, int argc, sqlite3_value **argv) -{ - if(access(sqlite3_value_text(argv[0]), F_OK) != 0) - { - sqlite3_result_int(pContext, -1); - } - else if(__verifyResMD5Chksum(sqlite3_value_text(argv[0]), - sqlite3_value_text(argv[1]), - sqlite3_value_int(argv[2])) == FALSE) - { - sqlite3_result_int(pContext, -2); - } - else - { - sqlite3_result_int(pContext, 0); - } -} - -static int __cleanupSkinTbl(void) -{ - int rc; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "DROP TABLE %s.%s", SKIN_USER_DB, SKIN_TBL_NAME); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Delete Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, CREATE_SKIN_TBL_SQL, SKIN_USER_DB"."); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_free(pSqlCmd); - return 0; -} - -static int __cleanupResTbl(void) -{ - int rc; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "DROP TABLE %s.%s", SKIN_USER_DB, RES_TBL_NAME); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Delete Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, CREATE_RES_TBL_SQL, SKIN_USER_DB"."); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_free(pSqlCmd); - return 0; -} - -static int __cleanupUpgTbl(void) -{ - int rc; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "DROP TABLE %s.upgrade", SKIN_USER_DB); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Delete Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, UPGRADE_TBL_SQL_CMD, SKIN_USER_DB"."); - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_free(pSqlCmd); - return 0; -} - -static int __cleanupTmpTbl(void) -{ - int rc; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "DROP TABLE main.tmp"); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Delete Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, CREATE_TMP_TBL_SQL, "main."); - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_free(pSqlCmd); - return 0; -} - -static int __skinCreateCfgDB(void) -{ - int rc = 0; - char* pErrMsg = NULL; - UT_string *pSqlCmd = NULL; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "CREATE VIRTUAL TABLE main.%s USING %s;" - "CREATE VIRTUAL TABLE main.%s USING %s;", - SKIN_TBL_NAME, SKIN_MODE_NAME, RES_TBL_NAME, RES_MODE_NAME); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_free(pSqlCmd); - - return (0); -} - -static int __skinCreateCfgFile(const char* pCfgFilePath) -{ - char* pErrMsg = NULL; - int rc = 0; - static sqlite3* pSqlFileDB = NULL; - UT_string *pSqlCmd = NULL; - - rc = sqlite3_open(":memory:", &g_pMemDb); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "sqlite3_open memory: %s\n", pCfgFilePath); - return -ERR_OPEN_SQLITE3_DB; - } - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, CREATE_TMP_TBL_SQL, ""); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - unlink(pCfgFilePath); - sqlite3_close(g_pMemDb); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - rc = sqlite3_open_v2(pCfgFilePath, &pSqlFileDB, SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE, NULL); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "sqlite3_open_v2 error: %s\n", pCfgFilePath); - sqlite3_close(g_pMemDb); - utstring_free(pSqlCmd); - return -ERR_OPEN_SQLITE3_DB; - } - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, CREATE_SKIN_TBL_SQL""CREATE_RES_TBL_SQL, "", ""); - - rc = sqlite3_exec(pSqlFileDB, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - unlink(pCfgFilePath); - sqlite3_close(g_pMemDb); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, UPGRADE_TBL_SQL_CMD, ""); - rc = sqlite3_exec(pSqlFileDB, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Create Tbl %s Error: %s\n", UPGRADE_TBL_SQL_CMD, pErrMsg); - sqlite3_free(pErrMsg); - unlink(pCfgFilePath); - sqlite3_close(g_pMemDb); - utstring_free(pSqlCmd); - return -ERR_SQLITE3_CREATE_TABLE; - } - - sqlite3_close(pSqlFileDB); - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, ATTACH_DB_SQL_CMD, pCfgFilePath, SKIN_USER_DB); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, 0, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Attach Database %s Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - utstring_free(pSqlCmd); - sqlite3_free(pErrMsg); - unlink(pCfgFilePath); - sqlite3_close(g_pMemDb); - return -ERR_SQLITE3_CREATE_TABLE; - } - - utstring_free(pSqlCmd); - - rc = sqlite3_create_function(g_pMemDb, "ResCheck", 3, SQLITE_UTF8, NULL, __checkRes, NULL, NULL); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Don't Support Resurces Verify Function\n"); - } - - if(InitSkinRomDatabase(g_pMemDb) != 0) - { - sqlite3_close(g_pMemDb); - return (-ERR_SQL_REG_MODULE); - } - - rc = __skinCreateCfgDB(); - - if(rc != 0) - { - sqlite3_close(g_pMemDb); - return rc; - } - - return (0); -} - -static void __onDlFileCb(void *pData, unsigned int size, - const char *pReqUrl, const char* pDlPath, - const char *pTaskUuid, int iFinished, void *pUserData) -{ - char* pId = (char*)pUserData; - UT_string *pSqlCmd = NULL; - int rc = 0; - char* pErrMsg = NULL; - - if(g_curDlItems > 0) - { - g_curDlItems--; - } - - if(iFinished == 0 && pId != NULL) - { - //LOG_EX(LOG_Debug, "Request(%s): [%s] --> [%s] Response: [%u] OK\n", pTaskUuid, pReqUrl, pDlPath, size); - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "UPDATE %s.upgrade SET dlStatus = %d, dlRetrys = %d, " - "dlTime = datetime(\'now\', \'localtime\') WHERE ID = %s AND " - "ResCheck(localPath, md5Chksum, resType) = 0", - SKIN_USER_DB, STATUS_DOWNLOADED, 0, pId); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQL Query Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - } - - utstring_free(pSqlCmd); - } - else - { - if(iFinished == 1) - { - LOG_EX(LOG_Error, "Request(%s): [%s] --> [%s] Response: [%u] Error\n", pTaskUuid, pReqUrl, pDlPath, size); - } - else - { - LOG_EX(LOG_Error, "Download %s Error: %d\n", pReqUrl, iFinished); - } - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "SELECT * FROM %s.upgrade WHERE ID = %s AND dlRetrys < 5 ", SKIN_USER_DB, pId); - - //fprintf(stdout, "SQL:\n%s\n", utstring_body(pSqlCmd)); - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), __reTryDlSkinResCb, NULL, &pErrMsg); - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQL %s Query Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - } - - utstring_free(pSqlCmd); - } - - if(pId) - { - free(pId); - } -} - -static int __reTryDlSkinResCb(void *pData, int argc, char **argv, char **azColName) -{ - UT_string *pSqlCmd = NULL; - char* pErrMsg = NULL; - - const char* pDlTaskId = InetHttpDlFileAsync(argv[6], - argv[5], - __onDlFileCb, - NULL, - strdup(argv[0])); - - if(pDlTaskId != NULL) - { - int rc = 0; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "UPDATE %s.upgrade SET dlId = \'%s\', dlRetrys = dlRetrys + 1, " - "dlTime = datetime(\'now\', \'localtime\') WHERE ID = %s", - SKIN_USER_DB, pDlTaskId, argv[0]); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQL Query Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - } - - utstring_free(pSqlCmd); - } - - return 0; -} - -static int __loadDownloadSkinsCb(void *pData, int argc, char **argv, char **azColName) -{ - const char *pDlTaskId; - char* pItemId = argv[0]; - char* pURL = argv[6]; - char* pDlPath = argv[5]; - char* pDlId = argv[10]; - int dlStatus = strtol(argv[3], NULL, 10); - int dlRetry = strtol(argv[4], NULL, 10) + 1; - - if(dlStatus == STATUS_DOWNLOADING) - { - if(strlen(pDlId) > 0) - { - InetCancelDownload(pDlId); - } - - dlRetry++; - } - - //LOG_EX(LOG_Debug, "Download: %s --> %s\n", pURL, pDlPath); - - pDlTaskId = InetHttpDlFileAsync(pURL, - pDlPath, - __onDlFileCb, - NULL, - strdup(pItemId)); - - if(pDlTaskId) - { - UT_string *pSqlCmd = NULL; - int rc = 0; - char* pErrMsg = NULL; - - g_curDlItems++; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "UPDATE %s.upgrade SET dlStatus = %d, dlRetrys = %d, " - "dlId = \'%s\', dlTime = datetime(\'now\', \'localtime\') WHERE ID = %s", - SKIN_USER_DB, STATUS_DOWNLOADING, dlRetry, pDlTaskId, pItemId); -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQL Query Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - } - - utstring_free(pSqlCmd); - } - else - { - LOG_EX(LOG_Error, "Download %s error\n", pURL); - } - - return (0); -} - -static void __uvDownloadResThread(void *pParams) -{ - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - LOG_EX(LOG_Debug, "Beging Download Skins\n"); - - while(!g_isDlFinished) - { - int rc = 0; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "SELECT *, STRFTIME(\"%%s\", datetime(\'now\', \'localtime\')) " - "- STRFTIME(\"%%s\", upgrade.dlTime) AS diff FROM %s.upgrade " - "WHERE resStatus = %d AND (dlRetrys < 5 OR diff > 600) " - "AND (dlStatus = %d OR (dlStatus = %d AND diff > 600)) " - "LIMIT 10 - %d", - SKIN_USER_DB, UPG_STATUS_DL_NEW, STATUS_CREATE_NEW, STATUS_DOWNLOADING, g_curDlItems); -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), __loadDownloadSkinsCb, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQL Query Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - } - - utstring_free(pSqlCmd); - - sleep(1); - } - - LOG_EX(LOG_Debug, "Download Skins Finished\n"); - - pthread_detach(pthread_self()); -} - -static int __sqlCbRemoveUnusedFile(void *pData, int argc, char **argv, char **azColName) -{ - LOG_EX(LOG_Debug, "Clean up resources: [%s]\n", argv[1]); - - if(access(argv[1], F_OK) == 0 - && strncmp(argv[1], DEF_SKINS_ROOT_PATH, strlen(DEF_SKINS_ROOT_PATH)) != 0) - { - unlink(argv[1]); - } -} - -static int __createResTblCb(void *pData, int argc, char **argv, char **azColName) -{ - int rc = 0; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - //DEBUG_SQL_CALLBACK_DATA(argc, argv, azColName); - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "INSERT INTO %s.%s " - "VALUES (NULL, \'%s\', \'%s\', \'%s\', \'%s\')", - SKIN_USER_DB, RES_TBL_NAME, argv[0], argv[1], argv[2], argv[3]); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "[%s] Query Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -1; - } - - utstring_free(pSqlCmd); - - return 0; -} - -static int __createSkinTblCb(void *pData, int argc, char **argv, char **azColName) -{ - int rc = 0; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - //DEBUG_SQL_CALLBACK_DATA(argc, argv, azColName); - - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, "INSERT INTO %s.%s " - "VALUES (NULL, \'%s\', %s, %s, %s, \'%s\')", - SKIN_USER_DB, SKIN_TBL_NAME, - argv[1], argv[2], argv[3], argv[0], argv[4]); - - //fprintf(stdout, "AutoId: %lld: %s\n", sqlite3_last_insert_rowid(g_pMemDb), argv[3]); - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "[%s] Query Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - return -1; - } - - utstring_free(pSqlCmd); - return 0; -} - -static void __uvSyncResThread(void *pParams) -{ - int tolCnt = 0; - PSKINS_CACHE_ITEM pItem = NULL, pTemp = NULL; - - LOG_EX(LOG_Debug, "Beging Sync Download Skins\n"); - - while(TRUE) - { - UT_string *pSqlCmd = NULL; - int rc = 0; - char* pErrMsg = NULL; - char **pResult = NULL; - int iRow = 0, iCol = 0; - int nItems = 0; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "SELECT COUNT(*) FROM %s.upgrade " - "WHERE resStatus = %d AND dlStatus != %d", - SKIN_USER_DB, UPG_STATUS_DL_NEW, STATUS_DOWNLOADED); -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - rc = sqlite3_get_table(g_pMemDb, utstring_body(pSqlCmd), &pResult, &iRow, &iCol, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Run SQL \n[%s]\n Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - } - - utstring_free(pSqlCmd); - - nItems = strtol(pResult[1], NULL, 10); - sqlite3_free_table(pResult); - - if(tolCnt++ % 20 == 0) - { - LOG_EX(LOG_Debug, "Download: Remain %d\n", nItems); - } - - if(nItems == 0) - { - g_isDlFinished = TRUE; - utstring_new(pSqlCmd); - sqlite3_exec(g_pMemDb, "BEGIN TRANSACTION;", NULL, NULL, NULL); - - utstring_printf(pSqlCmd, "INSERT INTO main.tmp " - "SELECT %s.ID, %s.localPath FROM %s.%s " - "LEFT JOIN %s.upgrade on %s.md5Chksum = upgrade.md5Chksum " - "WHERE upgrade.md5Chksum IS NULL;", - RES_TBL_NAME, RES_TBL_NAME, SKIN_USER_DB, RES_TBL_NAME, - SKIN_USER_DB, RES_TBL_NAME); -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Run SQL \n[%s]\n Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - sqlite3_exec(g_pMemDb, "ROLLBACK;", NULL, NULL, NULL); - utstring_free(pSqlCmd); - break; - } - - __cleanupSkinTbl(); - __cleanupResTbl(); - - utstring_renew(pSqlCmd); - - utstring_printf(pSqlCmd, "SELECT resVersion, localPath, serverURL, md5Chksum, " - "keyName, resType, priority, resReadme, resStatus, dlStatus " - "FROM %s.upgrade WHERE resStatus = %d " - "OR (resStatus = %d AND dlStatus = %d) " - "GROUP BY md5Chksum", - SKIN_USER_DB, UPG_STATUS_DOWNLOADED, - UPG_STATUS_DL_NEW, STATUS_DOWNLOADED); -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), __createResTblCb, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Run SQL \n[%s]\n Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - sqlite3_exec(g_pMemDb, "ROLLBACK;", NULL, NULL, NULL); - utstring_free(pSqlCmd); - break; - } - else - { - utstring_renew(pSqlCmd); - utstring_printf(pSqlCmd, "SELECT %s.ID, keyName, resType, priority, " - "resReadme, resStatus, dlStatus, %s.md5Chksum " - "FROM %s.upgrade, %s.res WHERE %s.md5Chksum = upgrade.md5Chksum" - " AND (resStatus = %d OR (resStatus = %d AND dlStatus = %d)) " - "ORDER BY keyName", - RES_TBL_NAME, RES_TBL_NAME, SKIN_USER_DB, SKIN_USER_DB, RES_TBL_NAME, - UPG_STATUS_DOWNLOADED, UPG_STATUS_DL_NEW, STATUS_DOWNLOADED); -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), __createSkinTblCb, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Run SQL \n[%s]\n Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - sqlite3_exec(g_pMemDb, "ROLLBACK;", NULL, NULL, NULL); - utstring_free(pSqlCmd); - break; - } - else - { - sqlite3_exec(g_pMemDb, "SELECT * FROM main.tmp", __sqlCbRemoveUnusedFile, NULL, NULL); - utstring_free(pSqlCmd); - - if(__cleanupUpgTbl() != 0) - { - sqlite3_exec(g_pMemDb, "ROLLBACK;", NULL, NULL, NULL); - } - else - { - sqlite3_exec(g_pMemDb, "END TRANSACTION;", NULL, NULL, NULL); - break; - } - } - } - } - - sleep(1); - } - - LOG_EX(LOG_Debug, "Sync Download Skins Finished\n"); - - uv_rwlock_wrlock(&g_SkinCacheRwLock); - HASH_ITER(hh, g_pSkinCackeTbl, pItem, pTemp) - { - free(pItem->pKeyName); - free(pItem->pResPath); - free(pItem); - pItem = NULL; - } - - g_pSkinCackeTbl = NULL; - uv_rwlock_wrunlock(&g_SkinCacheRwLock); - - g_isUpgradeRunning = FALSE; - pthread_detach(pthread_self()); -} - -static int __upgStatusTblCb(void *pData, int argc, char **argv, char **azColName) -{ - int rc = 0; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - DEBUG_SQL_CALLBACK_DATA(argc, argv, azColName); - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "UPDATE %s.upgrade SET resStatus = %d WHERE ID = %s", - SKIN_USER_DB, UPG_STATUS_DL_NEW, argv[2]); - - rc = sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, NULL, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "[%s] Query Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return -1; - } - - utstring_free(pSqlCmd); - - return 0; -} - -static int __saveUpgradeInfo(PSKIN_RES_INFO pInfo, int nItems) -{ - int isError = FALSE; - char* pErrMsg = NULL; - UT_string* pSqlCmd = NULL; - - if(pInfo == NULL || nItems <= 0) - { - return (-ERR_INPUT_PARAMS); - } - - utstring_new(pSqlCmd); - - sqlite3_exec(g_pMemDb, "BEGIN TRANSACTION;", NULL, NULL, NULL); - __cleanupUpgTbl(); - __cleanupTmpTbl(); - - for(int i = 0; i < nItems; i++) - { - UT_string* pPath = NULL; - PSKIN_RES_INFO pItem = &pInfo[i]; - - utstring_new(pPath); - utstring_renew(pSqlCmd); - -#if 0 - if(pItem->resType == VOICE_RES) - { - utstring_printf(pPath, "%svoice/%s", DOWNLOAD_RES_ROOT_PATH, basename_v2(pItem->pLocalPath)); - } - - else if(pItem->resType == IMAGE_RES) - { - utstring_printf(pPath, "%simage/%s", DOWNLOAD_RES_ROOT_PATH, basename_v2(pItem->pLocalPath)); - } - else if(pItem->resType == TEXT_RES) - { - utstring_printf(pPath, "%s", pItem->pLocalPath); - } - else - { - utstring_free(pPath); - continue; - } -#else - utstring_printf(pPath, "%svoice/%s", DOWNLOAD_RES_ROOT_PATH, basename_v2(pItem->pLocalPath)); -#endif - utstring_printf(pSqlCmd, "INSERT INTO %s.upgrade (keyName, resType, resVersion, localPath, serverURL, md5Chksum)" - " SELECT \'%s\', %d, \'%s\', \'%s\', \'%s\', \'%s\' ", - SKIN_USER_DB, pItem->pKeyName, pItem->resType, pItem->pResVer, utstring_body(pPath), - pItem->pLocalPath, pItem->pMD5Chksum); - -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - - if((sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, 0, &pErrMsg) != SQLITE_OK)) - { - LOG_EX(LOG_Error, "Add Skins[%d] %s To Database Error: %s\n", i, pItem->pKeyName, utstring_body(pSqlCmd)); - LOG_EX(LOG_Error, "SQL Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - isError = TRUE; - utstring_free(pPath); - break; - } - - utstring_free(pPath); - } - - utstring_renew(pSqlCmd); - - utstring_printf(pSqlCmd, "UPDATE %s.upgrade SET resStatus = %d " - "WHERE EXISTS (SELECT %s.keyName, upgrade.md5Chksum FROM main.%s, user_db.upgrade " - "WHERE upgrade.keyName = %s.keyName)", - SKIN_USER_DB, UPG_STATUS_DL_NEW, - SKIN_TBL_NAME, SKIN_TBL_NAME, - SKIN_TBL_NAME); - -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - - if(sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), __upgStatusTblCb, 0, &pErrMsg) != SQLITE_OK) - { - LOG_EX(LOG_Error, "Upgrade Status: %s\n", utstring_body(pSqlCmd)); - LOG_EX(LOG_Error, "SQL Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - isError = TRUE; - } - - utstring_renew(pSqlCmd); - - utstring_printf(pSqlCmd, "UPDATE %s.upgrade SET resStatus = %d " - "WHERE EXISTS (SELECT %s.md5Chksum FROM %s.%s " - "WHERE upgrade.md5Chksum = %s.md5Chksum);" - "UPDATE %s.upgrade SET resStatus = %d, localPath = " - "(SELECT %s.localPath FROM main.%s " - "WHERE upgrade.md5Chksum = %s.md5Chksum) " - "WHERE EXISTS (SELECT %s.md5Chksum FROM main.%s " - "WHERE upgrade.md5Chksum = %s.md5Chksum);", - SKIN_USER_DB, UPG_STATUS_DOWNLOADED, - RES_TBL_NAME, SKIN_USER_DB, RES_TBL_NAME, - RES_TBL_NAME, - SKIN_USER_DB, UPG_STATUS_DOWNLOADED, - RES_TBL_NAME, RES_TBL_NAME, RES_TBL_NAME, - RES_TBL_NAME, RES_TBL_NAME, RES_TBL_NAME); - -#if DEBUG_SQL_CMD - LOG_EX(LOG_Debug, "SQL CMD: [%s]\n", utstring_body(pSqlCmd)); -#endif - - if(sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), NULL, 0, &pErrMsg) != SQLITE_OK) - { - LOG_EX(LOG_Error, "Upgrade Status: %s\n", utstring_body(pSqlCmd)); - LOG_EX(LOG_Error, "SQL Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - isError = TRUE; - } - - utstring_free(pSqlCmd); - - - if(isError == TRUE) - { - sqlite3_exec(g_pMemDb, "ROLLBACK;", NULL, NULL, NULL); - return (-ERR_SQL_QUERY); - } - else - { - uv_thread_t uvSyncThread, uvDlThread; - sqlite3_exec(g_pMemDb, "END TRANSACTION;", NULL, NULL, NULL); - g_curDlItems = 0; - g_isDlFinished = FALSE; - uv_thread_create(&uvDlThread, __uvDownloadResThread, NULL); - uv_thread_create(&uvSyncThread, __uvSyncResThread, NULL); - } - - return 0; -} - -static int __loadPreUpgradeInfo(void) -{ - UT_string *pSqlCmd = NULL; - int rc = 0; - char* pErrMsg = NULL; - char **pResult = NULL; - int iRow = 0, iCol = 0; - int nItems = 0; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "SELECT COUNT(*) FROM %s.upgrade WHERE resStatus = %d AND dlStatus != %d", - SKIN_USER_DB, UPG_STATUS_DL_NEW, STATUS_DOWNLOADED); - - rc = sqlite3_get_table(g_pMemDb, utstring_body(pSqlCmd), &pResult, &iRow, &iCol, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Run SQL \n[%s]\n Error: %s\n", utstring_body(pSqlCmd), pErrMsg); - sqlite3_free(pErrMsg); - } - - utstring_free(pSqlCmd); - - nItems = strtol(pResult[1], NULL, 10); - sqlite3_free_table(pResult); - - if(nItems > 0) - { - uv_thread_t uvSyncThread, uvDlThread; - g_curDlItems = 0; - g_isDlFinished = FALSE; - g_isUpgradeRunning = TRUE; - uv_thread_create(&uvDlThread, __uvDownloadResThread, NULL); - uv_thread_create(&uvSyncThread, __uvSyncResThread, NULL); - LOG_EX(LOG_Debug, "Load Pre Download Items: %d\n", nItems); - } -} - -static int __getEnumResInfo(void) -{ - __saveUpgradeInfo((PSKIN_RES_INFO)g_emuUpgradeInfo, sizeof(g_emuUpgradeInfo) / sizeof(g_emuUpgradeInfo[0])); -} - -int SkinUpgrade(char* pUpdData) -{ - int nItems = 0; - PSKIN_RES_INFO pUpgResInfo; - cJSON *pRoot, *pList; - -#if 1 - FILE *pFile = fopen("/mnt/UDISK/skinupgrade.txt", "w+"); - fwrite(pUpdData, 1, strlen(pUpdData), pFile); - fclose(pFile); -#endif - - if (pUpdData == NULL || strlen(pUpdData) == 0) - { - LOG_EX(LOG_Error, "Input Params Error\n"); - return (-ERR_INPUT_PARAMS); - } - - LOG_EX(LOG_Debug, "Upgrade Cmd: [%s]\n", pUpdData); - //fprintf(stdout, "%s\n", pUpdData); - - pRoot = cJSON_Parse(pUpdData); - - if(pRoot == NULL) - { - LOG_EX(LOG_Error, "Parse Json Error\n"); - return (-ERR_NO_ITEMS); - } - - nItems = cJSON_GetArraySize(pRoot); - - if(nItems <= 0) - { - LOG_EX(LOG_Error, "Upgrade Item: %d\n", nItems); - cJSON_Delete(pRoot); - return (-ERR_NO_ITEMS); - } - - pUpgResInfo = (PSKIN_RES_INFO)malloc(sizeof(SKIN_RES_INFO) * nItems); - - if(pUpgResInfo == NULL) - { - LOG_EX(LOG_Error, "Malloc %d Memory Error\n", nItems); - cJSON_Delete(pRoot); - return (-ERR_MALLOC_MEMORY); - } - - LOG_EX(LOG_Debug, "Array Size = %d\n", nItems); - - memset(pUpgResInfo, 0, sizeof(SKIN_RES_INFO) * nItems); - pList = pRoot->child; - - for(int i = 0; i < nItems; i++) - { - pUpgResInfo[i].resType = VOICE_RES;//cJSON_GetObjectItem(pList, "resourceType")->valueint; - pUpgResInfo[i].pResVer = strdup(cJSON_GetObjectItem(pList, "resourceVersion")->valuestring); - pUpgResInfo[i].pKeyName = strdup(cJSON_GetObjectItem(pList, "resourceName")->valuestring); - pUpgResInfo[i].pLocalPath = strdup(cJSON_GetObjectItem(pList, "url")->valuestring); - pUpgResInfo[i].pMD5Chksum = strdup(cJSON_GetObjectItem(pList, "md5")->valuestring); - - pList = pList->next; - } - - cJSON_Delete(pRoot); - - g_isUpgradeRunning = TRUE; - __dumpSkinsInfo(pUpgResInfo, nItems); - __saveUpgradeInfo(pUpgResInfo, nItems); - - free(pUpgResInfo); -} - -char* SQLiteGetSkinsResource(char *pKeyName, int *pResType, int *pVersion, char **pComeFrom) -{ - char *pSkinPath = strdup(""); - int rc = 0; - char* pErrMsg = NULL; - UT_string *pSqlCmd = NULL; - char **pResult = NULL; - int iRow = 0, iCol = 0; - int queCol = 13; - int colPath = 21; - - utstring_new(pSqlCmd); - - if(g_isVerifyRes) - { - utstring_printf(pSqlCmd, "SELECT *, RANDOM() AS rnd, ResCheck(localPath, md5Chksum, resType) as resOK " - "FROM %s.%s, %s.%s\n" - "WHERE %s.keyName = \'%s\' AND resOK = 0 AND %s.resId = %s.ID\n" - "UNION\n" - "SELECT *, RANDOM() AS rnd, ResCheck(localPath, md5Chksum, resType) as resOK " - "FROM main.%s, main.%s\n" - "WHERE %s.keyName = \'%s\' AND resOK = 0 AND %s.resId = %s.ID\n" - "ORDER BY priority DESC, rnd " - "LIMIT 1", - SKIN_USER_DB, SKIN_TBL_NAME, SKIN_USER_DB, RES_TBL_NAME, - SKIN_TBL_NAME, pKeyName, SKIN_TBL_NAME, RES_TBL_NAME, - SKIN_TBL_NAME, RES_TBL_NAME, - SKIN_TBL_NAME, pKeyName, SKIN_TBL_NAME, RES_TBL_NAME); - } - else - { - utstring_printf(pSqlCmd, "SELECT *, RANDOM() AS rnd " - "FROM %s.%s, %s.%s\n" - "WHERE %s.keyName = \'%s\' AND %s.resId = %s.ID\n" - "UNION\n" - "SELECT *, RANDOM() AS rnd " - "FROM main.%s, main.%s\n" - "WHERE %s.keyName = \'%s\' AND %s.resId = %s.ID\n" - "ORDER BY priority DESC, rnd " - "LIMIT 1", - SKIN_USER_DB, SKIN_TBL_NAME, SKIN_USER_DB, RES_TBL_NAME, - SKIN_TBL_NAME, pKeyName, SKIN_TBL_NAME, RES_TBL_NAME, - SKIN_TBL_NAME, RES_TBL_NAME, - SKIN_TBL_NAME, pKeyName, SKIN_TBL_NAME, RES_TBL_NAME); - queCol = 12; - colPath = 20; - } - - rc = sqlite3_get_table(g_pMemDb, utstring_body(pSqlCmd), &pResult, &iRow, &iCol, &pErrMsg); - - if(rc != SQLITE_OK) - { - LOG_EX(LOG_Error, "Run SQL \n[%s]\n Error: %s(%d)\n", utstring_body(pSqlCmd), pErrMsg, rc); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return (pSkinPath); - } - - if(iRow == 1 && iCol == queCol) - { - - if(access(pResult[colPath], F_OK) != 0) - { - LOG_EX(LOG_Error, "Res %s --> %s not exists\n", pKeyName, pResult[colPath]); - } - else - { - free(pSkinPath); - pSkinPath = strdup(pResult[colPath]); - } -#if 0 - for(int i = 0; i <= 21; i++) - { - LOG_EX(LOG_Debug, "[%d]: {%s}\n", i, pResult[i]); - } -#endif - } - -#if 0 - int index = iCol; - for(int i = 0; i < iRow; i++) - { - fprintf(stdout, "----------%d[%d, %d]---------\n", i, iRow, iCol); - - for(int j = 0; j < iCol; j++) - { - int tblCol = i * iCol + j; - - fprintf(stdout, "%s(%d, %d) = [%s]\n", pResult[tblCol], j, index, pResult[index]); - index++; - } - } -#endif - - sqlite3_free_table(pResult); - utstring_free(pSqlCmd); - - if(strcmp(pSkinPath, "") == 0) - { - free(pSkinPath); - return TblGetSkinsResource(pKeyName, pResType, pVersion, pComeFrom); - } - else - { - return (pSkinPath); - } -} - -char* TblGetSkinsResource(char *pKeyName, int *pResType, int *pVersion, char **pComeFrom) -{ - - for(int i = 0; i < sizeof(g_SkinDefaultResTable) / sizeof(g_SkinDefaultResTable[0]); i++) - { - if(strcmp(g_SkinDefaultResTable[i].pKeyName, pKeyName) == 0) - { - return strdup(g_SkinDefaultResTable[i].pLocalPath); - } - } - - return strdup(""); -} - -char* GetSkinsResource(char *pKeyName, int *pResType, int *pVersion, char **pComeFrom) -{ - if(g_isUpgradeRunning) - { - return TblGetSkinsResource(pKeyName, pResType, pVersion, pComeFrom); - } - else - { - return SQLiteGetSkinsResource(pKeyName, pResType, pVersion, pComeFrom); - } -} - -unsigned int SkinsDefaultSize(void) -{ - return (g_tolDefaultSkins + 1); -} - -PSKIN_RES_INFO SkinsItemById(int iId) -{ - if(iId < 0 || iId > g_tolDefaultSkins) - { - return (NULL); - } - - return ((PSKIN_RES_INFO)&g_SkinDefaultResTable[iId - 1]); -} - -static int __verifyResValuableCb(void *pData, int argc, char **argv, char **azColName) -{ - char sqlCmd[256]; - char* pErrMsg = NULL; - - if(argc < 3) - { - return 0; - } - - sqlite3_exec(g_pMemDb, "BEGIN TRANSACTION;", NULL, NULL, NULL); - for(int i = 0; i < argc; i++) - { - if(strcmp(azColName[i], "ID") == 0) - { - LOG_EX(LOG_Error, "Cleanup Bad Resources: %s\n", argv[i + 1]); - - memset(sqlCmd, 0, 256); - sprintf(sqlCmd, "DELETE FROM %s.%s WHERE ID = %s", SKIN_USER_DB, RES_TBL_NAME, argv[i]); - - if(sqlite3_exec(g_pMemDb, sqlCmd, __verifyResValuableCb, 0, &pErrMsg) != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQL: %s\nError: %s\n", sqlCmd, pErrMsg); - sqlite3_free(pErrMsg); - sqlite3_exec(g_pMemDb, "ROLLBACK;", NULL, NULL, NULL); - return -1; - } - - unlink(argv[i + 1]); - } - } - sqlite3_exec(g_pMemDb, "END TRANSACTION;", NULL, NULL, NULL); - - return (0); -} - -static void VerifySkinResInfo(void) -{ - UT_string *pSqlCmd = NULL; - char* pErrMsg = NULL; - - utstring_new(pSqlCmd); - utstring_printf(pSqlCmd, "SELECT ID, localPath, md5Chksum FROM %s.%s " - "WHERE ResCheck(localPath, md5Chksum, 1) != 0", SKIN_USER_DB, RES_TBL_NAME); - - if(sqlite3_exec(g_pMemDb, utstring_body(pSqlCmd), __verifyResValuableCb, 0, &pErrMsg) != SQLITE_OK) - { - LOG_EX(LOG_Error, "SQL Error: %s\n", pErrMsg); - sqlite3_free(pErrMsg); - utstring_free(pSqlCmd); - return; - } - - utstring_free(pSqlCmd); -} - -void SkinIsVerifyRes(int isVerify) -{ - g_isVerifyRes = isVerify; -} - -int SkinInit(void) -{ - int rc = 0; - UT_string* pPath = NULL; -#if 0 - int tmUsed = 0; - struct timeval tmStart, tmEnd; - - if(DBusLibuvGetRuntime() == NULL) - { - int ret = 0; - uv_thread_t uvThread; - LOG_EX(LOG_Warn, "+++++++++++++++++++++++System Uninit\n"); - - DBusConnection* pBus = DBusWithLibuvInit(GetDBusDefaultLoop(), - g_pModInfoTable[MODULE_SKINS].modAliase, - __dusOnMsg, - NULL, - NULL, //KeyEventCb, - &ret); - - if(pBus == NULL) - { - LOG_EX(LOG_Error, "DBusWithLibuvInit Error: %d\n", ret); - return -ERR_UNINIT_ITEM; - } - - uv_thread_create(&uvThread, uvLoopProc, NULL); - } -#endif - - uv_rwlock_init(&g_uvSkinRwLock); - uv_rwlock_init(&g_uvDlRwLock); - uv_rwlock_init(&g_SkinCacheRwLock); - - utstring_new(pPath); - - utstring_printf(pPath, "mkdir -p %s/image > /dev/zero", DOWNLOAD_RES_ROOT_PATH); - rc = system(utstring_body(pPath)); - - utstring_renew(pPath); - utstring_printf(pPath, "mkdir -p %s/voice > /dev/zero", DOWNLOAD_RES_ROOT_PATH); - rc = system(utstring_body(pPath)); - - utstring_free(pPath); - - rc = __skinCreateCfgFile(SKINS_DB_PATH); - - if(rc != SQLITE_OK) - { - return rc; - } - - // gettimeofday(&tmStart, NULL); - VerifySkinResInfo(); -// gettimeofday(&tmEnd, NULL); - -// tmUsed = (tmEnd.tv_sec * 1000000 + tmEnd.tv_usec) - (tmStart.tv_sec * 1000000 + tmStart.tv_usec); -// LOG_EX(LOG_Debug, "VerifySkinResInfo used time: %d(us)\n", tmUsed); - - __loadPreUpgradeInfo(); - //__getEnumResInfo(); - //__getUpgradeInfo("./hotfix.txt"); - //__loadDownloadItems(); - - //__dumpSkinsInfo(); -} diff --git a/src/Framework/SvrManager/svr_manager.c b/src/Framework/SvrManager/svr_manager.c deleted file mode 100644 index 2a15b1c..0000000 --- a/src/Framework/SvrManager/svr_manager.c +++ /dev/null @@ -1,235 +0,0 @@ -#include "server_addr.h" -#include "log.h" -#include "config_engine.h" -#include "libuv_dbus.h" - -const char* g_ServerModuleStr[] = -{ - "YUNXIN_MODULE", - "VOICE_MODULE", - "VOICE_AI_MODULE", - "SERVER_MODULE", - "LOG_MODULE", - "MARK_POINT_MODULE", - "TTS_MODULE", - "DC_MODULE", - "UNKNOWN_MODULE" -}; - -const char* g_ServerModeStr[] = -{ - "DEV_MODE", - "TEST_MODE", - "PUBLISH_MODE", - "PUBLISH_PREBUILD", - "UNKNOWN_MODE" -}; - -const char* g_KeyMapStr[] = -{ - "VOICE_APP_KEY", - "VOICE_APP_SECRET", - "UNKNOWN_KEYMAP" -}; - -const char* SvrModuleStr(SERVER_MODULE_TYPE module) -{ - return g_ServerModuleStr[module]; -} - -const char* SvrModeStr(SERVER_MODE_TYPE mode) -{ - return g_ServerModeStr[mode]; -} - -const char* g_VoiceKeyMap[VOICE_MAX][MAX_MODE] = { - { // VOICE_APP_KEY - "vbox-dev", - "vbox-dev", - "vbox-online", - "vbox-online", - }, - { // VOICE_APP_SECRET - "b1ec33c03df80ea3035bc9ccaa4af09c", - "b1ec33c03df80ea3035bc9ccaa4af09c", - "8714d6de1c83f21dda5fc9a905a59ac1", - "8714d6de1c83f21dda5fc9a905a59ac1", - }, -}; - -const char* g_ServerAddr[MAX_MODULE][MAX_MODE] = { - { // YUNXIN_MODULE - "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", - }, -}; - -static unsigned int g_SvrMode = PUBLISH_MODE; - -void DumpCurServerAddr(const char* pTags) -{ - if(pTags && strlen(pTags) > 0) - { - LOG_EX2(LOG_Info, "%s\t Current Server Mode: %s\n", pTags, SvrModeStr(g_SvrMode)); - } - else - { - LOG_EX2(LOG_Info, "Current Server Mode: %s\n", SvrModeStr(g_SvrMode)); - } - -#if 0 - LOG_EX2(LOG_Info, "Voice Key = [%s], Secret = [%s]\n", - GetCurVoiceKeyValue(VOICE_APP_KEY), GetCurVoiceKeyValue(VOICE_APP_SECRET)); - - LOG_EX2(LOG_Info, "--------------------------------------------------------" - "----------------------------------------------\n"); - - LOG_EX2(LOG_Info, "| Module | " - "Server URL |\n"); - - LOG_EX2(LOG_Info, "--------------------------------------------------------" - "----------------------------------------------\n"); - - for(int i = 0; i < MAX_MODULE; i++) - { - LOG_EX2(LOG_Info, "| %-17s | %-78s |\n", SvrModuleStr(i), GetCurServerAddr(i)); - } - - LOG_EX2(LOG_Info, "--------------------------------------------------------" - "----------------------------------------------\n"); -#endif - -} - -SERVER_MODE_TYPE GetCurrentServerMode(void) -{ - return g_SvrMode; -} - -void SetCurrentServerMode(SERVER_MODE_TYPE mode) -{ - if(mode >= 0 && mode < MAX_MODE) - { - if(g_SvrMode != mode) - { - LOG_EX(LOG_Debug, "Change server mode from %s(%d) to %s(%d)\n", - g_ServerModeStr[g_SvrMode], g_SvrMode, g_ServerModeStr[mode], mode); - - g_SvrMode = mode; - - CfgSetIntValue("ServerMode", mode); - } - else - { - LOG_EX(LOG_Warn, "Current mode is %s(%d) yet\n", g_ServerModeStr[mode], mode); - } - } - else - { - LOG_EX(LOG_Error, "Unknown Mode: %d, current mode: %s(%d)\n", - mode, g_ServerModeStr[g_SvrMode], g_SvrMode); - } -} - -char* GetCurServerAddr(SERVER_MODULE_TYPE module) -{ - if(module >= 0 && module < MAX_MODULE) - { - return (char*)g_ServerAddr[module][g_SvrMode]; - } - else - { - LOG_EX(LOG_Error, "Unknown Module: %s(%d)\n", g_ServerModuleStr[module], module); - return ""; - } -} - -char* GetCurVoiceKeyValue(VOICE_KEYMAP_TYPE keyMap) -{ - if(keyMap >= 0 && keyMap < VOICE_MAX) - { - return (char*)g_VoiceKeyMap[keyMap][g_SvrMode]; - } - else - { - LOG_EX(LOG_Error, "Unknown KeyMap Type: %s(%d)\n", g_KeyMapStr[keyMap], keyMap); - return ""; - } -} - -static PDBUS_MSG_PACK __dusOnMsg(uv_loop_t* pLoop, DBusConnection* pConn, PDBUS_MSG_PACK pMsg) -{ - return NULL; -} - -static void uvLoopProc(void *pParams) -{ - RunUVLoop(GetDBusDefaultLoop()); - pthread_detach(pthread_self()); -} - -void ServerManagerInit(void) -{ - int ret = 0; - uv_thread_t uvThread; - LOG_EX(LOG_Warn, "+++++++++++++++++++++++System Uninit\n"); - - DBusConnection* pBus = DBusWithLibuvInit(GetDBusDefaultLoop(), - g_pModInfoTable[MODULE_SKINS].modAliase, - __dusOnMsg, - NULL, - NULL, //KeyEventCb, - &ret); - - if(pBus == NULL) - { - LOG_EX(LOG_Error, "DBusWithLibuvInit Error: %d\n", ret); - return; - } - - uv_thread_create(&uvThread, uvLoopProc, NULL); -}