PV1_MakeProject/Framework/libuvEngine/libcomm.c

520 lines
19 KiB
C
Raw Normal View History

2018-09-18 08:09:12 +00:00
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/time.h>
#include <linux/input.h>
#include <sys/shm.h>
#include <sys/msg.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/vfs.h>
#include "log.h"
#include "libuv_dbus.h"
2018-10-24 03:16:10 +00:00
int IsFileExists(const char* pPath)
{
struct stat st;
if (stat(pPath, &st) != 0)
{
return FALSE;
}
else
{
return TRUE;
}
}
2018-09-18 08:09:12 +00:00
int CopyFile(const char *pSrc, const char *pDest)
{
int fdSrc, fdDest;
struct stat st;
ssize_t sz;
if(stat(pSrc, &st) != 0)
{
LOG_EX(LOG_Error, "Get File %s Size Error\n", pSrc);
return (-ERR_GET_FILE_SIZE);
}
fdSrc = open(pSrc, O_RDONLY);
if(fdSrc < 0)
{
LOG_EX(LOG_Error, "Open File %s Error\n", pSrc);
return (-ERR_OPEN_FILE);
}
fdDest = open(pDest, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
if(fdDest < 0)
{
close(fdSrc);
LOG_EX(LOG_Error, "Open File %s Error\n", pDest);
return (-ERR_OPEN_FILE);
}
sz = sendfile(fdDest, fdSrc, NULL, st.st_size);
if(sz != st.st_size)
{
LOG_EX(LOG_Error, "Copy File Size Error: %d, %d\n", sz, st.st_size);
close(fdSrc);
close(fdDest);
return (-ERR_COPY_FILE);
}
fsync(fdDest);
close(fdSrc);
close(fdDest);
return (0);
}
int CopyFileWithSize(const char *pSrc, const char *pDest, int iSize)
{
int fdSrc, fdDest;
struct stat st;
ssize_t sz;
size_t cpSize = iSize;
if(iSize <= 0)
{
if(stat(pSrc, &st) != 0)
{
LOG_EX(LOG_Error, "Get File %s Size Error\n", pSrc);
return (-ERR_GET_FILE_SIZE);
}
cpSize = st.st_size;
}
fdSrc = open(pSrc, O_RDONLY);
if(fdSrc < 0)
{
LOG_EX(LOG_Error, "Open File %s Error\n", pSrc);
return (-ERR_OPEN_FILE);
}
fdDest = open(pDest, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH |S_IWOTH);
if(fdDest < 0)
{
close(fdSrc);
LOG_EX(LOG_Error, "Open File %s Error\n", pDest);
return (-ERR_OPEN_FILE);
}
sz = sendfile(fdDest, fdSrc, NULL, cpSize);
if(sz != cpSize)
{
LOG_EX(LOG_Error, "Copy File Size Error: %d, %d\n", sz, cpSize);
close(fdSrc);
close(fdDest);
return (-ERR_COPY_FILE);
}
close(fdSrc);
close(fdDest);
return (0);
}
int ReadFileToBuf(const char *pSrc, unsigned char *pBuf, int iSize)
{
int fdSrc;
struct stat st;
ssize_t sz;
size_t cpSize = iSize;
if(iSize < 0)
{
if(stat(pSrc, &st) != 0)
{
LOG_EX(LOG_Error, "Get File %s Size Error\n", pSrc);
return (-ERR_GET_FILE_SIZE);
}
cpSize = st.st_size;
}
fdSrc = open(pSrc, O_RDONLY);
if(fdSrc < 0)
{
LOG_EX(LOG_Error, "Open File %s Error\n", pSrc);
return (-ERR_OPEN_FILE);
}
sz = read(fdSrc, pBuf, cpSize);
if(sz != cpSize)
{
LOG_EX(LOG_Error, "Copy File Size Error: %d, %d\n", sz, cpSize);
close(fdSrc);
return (-ERR_COPY_FILE);
}
close(fdSrc);
return (sz);
}
int GetShellExecResult(const char *pCmd, char **pResult)
{
FILE *pFile = NULL;
unsigned int uRdSize = 0;
char *pCmdOut;
*pResult = NULL;
if(pCmd == NULL || strlen(pCmd) == 0)
{
return (-ERR_INPUT_PARAMS);
}
pFile = popen(pCmd, "r");
if(pFile == NULL)
{
return (-ERR_OPEN_FILE);
}
*pResult = (char *)malloc(4096);
pCmdOut = *pResult;
uRdSize = fread(pCmdOut, sizeof(char), 4096, pFile);
pCmdOut[uRdSize] = 0;
if(pCmdOut[strlen(pCmdOut) - 1] == '\n')
{
pCmdOut[strlen(pCmdOut) - 1] = 0;
}
pclose(pFile);
//fprintf(stdout, "%s --> [%s](%u)\n", pCmd, pCmdOut, uRdSize);
return (0);
}
void SystemSafeReboot(void)
{
int ret, reTry = 0;
#if 0
ret = system("sync");
ret = system("ubus call system watchdog \'{\"stop\" : true}\'");
reTry = 3;
do
{
sleep(1);
} while(reTry--);
reTry = 0;
LOG_EX(LOG_Debug, "Reboot System By Power Control Chips\n");
sleep(1);
ret = system("echo 3140 > /sys/bus/platform/devices/axp22_board/axp22_reg");
sleep(10);
#endif
while(TRUE)
{
LOG_EX(LOG_Debug, "Reboot System: %d times\n", reTry++);
sleep(1);
ret = system("reboot -f");
sleep(3);
}
}
char* GetCpuChipId(void)
{
char* pRet = NULL;
#ifdef PLATFORM_R16
char* pChipId = NULL;
GetShellExecResult("cat /proc/cpuinfo | grep Chipid | awk '{print $3}'", &pChipId);
if(pChipId == NULL)
{
return strdup("");
}
pRet = strdup(pChipId);
free(pChipId);
return pRet;
#else
return strdup("Unknown CPU Chip ID");
#endif
}
char* GetCpuSerial(void)
{
char* pRet = NULL;
#ifdef PLATFORM_R16
char* pSerial = NULL;
GetShellExecResult("cat /proc/cpuinfo | grep Serial | awk '{print $3}'", &pSerial);
if(pSerial == NULL)
{
return strdup("");
}
pRet = strdup(pSerial);
free(pSerial);
return pRet;
#else
return strdup("Unknown CPU Serial");
#endif
}
unsigned long long GetPartitionFreeSize(const char *pPartPath)
{
struct statfs myStatfs;
unsigned long long freeSize;
if(statfs(pPartPath, &myStatfs) == -1)
{
return 0;
}
freeSize = myStatfs.f_bsize * myStatfs.f_bfree;
//fprintf(stdout, "%s free = %llu bytes\n", pPartPath, freeSize);
return freeSize;
}
#ifdef CURRENT_VERSION
char* GetCurrentVersion(void)
{
return CURRENT_VERSION;
}
#else
char* GetCurrentVersion(void)
{
return "0.0.1";
}
#endif
const char* ErrcodeToString(int errCode)
{
switch(errCode)
{
case ERR_INPUT_PARAMS: return "ERR_INPUT_PARAMS";
case ERR_NO_ITEMS: return "ERR_NO_ITEMS";
case ERR_GET_BUS: return "ERR_GET_BUS";
case ERR_DBUS_CONNECTION: return "ERR_DBUS_CONNECTION";
case ERR_REQUEST_BUS_NAME: return "ERR_REQUEST_BUS_NAME";
case ERR_SET_WATCH_FUNCTION: return "ERR_SET_WATCH_FUNCTION";
case ERR_SET_TIMEOUT_FUNCTION: return "ERR_SET_TIMEOUT_FUNCTION";
case ERR_BUS_MATCH: return "ERR_BUS_MATCH";
case ERR_BUS_SET_MSG_CB: return "ERR_BUS_SET_MSG_CB";
case ERR_DBUS_CREATE_MSG: return "ERR_DBUS_CREATE_MSG";
case ERR_BUS_SEND_MSG: return "ERR_BUS_SEND_MSG";
case ERR_DBUS_MSG_TO_LARGE: return "ERR_DBUS_MSG_TO_LARGE";
case ERR_BUS_RCV_MSG: return "ERR_BUS_RCV_MSG";
case ERR_ADD_TASK: return "ERR_ADD_TASK";
case ERR_UNSUP_EVP_TYPE: return "ERR_UNSUP_EVP_TYPE";
case ERR_CREATE_MQ: return "ERR_CREATE_MQ";
case ERR_MQ_SENDMSG: return "ERR_MQ_SENDMSG";
case ERR_CREATE_SHM: return "ERR_CREATE_SHM";
case ERR_MAP_SHM: return "ERR_MAP_SHM";
case ERR_MALLOC_MEMORY: return "ERR_MALLOC_MEMORY";
case ERR_EVP_INIT_KEY: return "ERR_EVP_INIT_KEY";
case ERR_EVP_UPDATE: return "ERR_EVP_UPDATE";
case ERR_EVP_FINALE: return "ERR_EVP_FINALE";
case ERR_EVP_KEY_SIZE: return "ERR_EVP_KEY_SIZE";
case ERR_OPEN_FILE: return "ERR_OPEN_FILE";
case ERR_READ_FILE: return "ERR_READ_FILE";
case ERR_WRITE_FILE: return "ERR_WRITE_FILE";
case ERR_COPY_FILE: return "ERR_COPY_FILE";
case ERR_FILE_NOT_EXISTS: return "ERR_FILE_NOT_EXISTS";
case ERR_GET_FILE_SIZE: return "ERR_GET_FILE_SIZE";
case ERR_UNINIT_ITEM: return "ERR_UNINIT_ITEM";
case ERR_FILE_EMPTY: return "ERR_FILE_EMPTY";
case ERR_SEND_MAIL: return "ERR_SEND_MAIL";
case ERR_NETWORK_SEND: return "ERR_NETWORK_SEND";
case ERR_NETWORK_NOT_CONNECTED: return "ERR_NETWORK_NOT_CONNECTED";
case ERR_UNSUPPORT: return "ERR_UNSUPPORT";
case ERR_NO_INIT_IPL3: return "ERR_NO_INIT_IPL3";
case ERR_BAD_IPL3: return "ERR_BAD_IPL3";
case ERR_BAD_FILE_SIZE: return "ERR_BAD_FILE_SIZE";
case ERR_MD5_FILE: return "ERR_MD5_FILE";
case ERR_MD5_CHECK_SUM: return "ERR_MD5_CHECK_SUM";
case ERR_OTA_WRITE_BOOT: return "ERR_OTA_WRITE_BOOT";
case ERR_OTA_WRITE_ROOTFS: return "ERR_OTA_WRITE_ROOTFS";
case ERR_OTA_WRITE_IPL3: return "ERR_OTA_WRITE_IPL3";
case ERR_OTA_WRITE_PARAMS: return "ERR_OTA_WRITE_PARAMS";
case ERR_OTA_DOWNLOAD_FILE: return "ERR_OTA_DOWNLOAD_FILE";
case ERR_VERIFY_PARTITION_MD5: return "ERR_VERIFY_PARTITION_MD5";
case ERR_OTA_PRE_STATR: return "ERR_OTA_PRE_STATR";
case ERR_OTA_YET_CUR_VER: return "ERR_OTA_YET_CUR_VER";
case ERR_OTA_NOT_READY: return "ERR_OTA_NOT_READY";
case ERR_CREATE_CFG_FILE: return "ERR_CREATE_CFG_FILE";
case ERR_CREATE_SQLITE3_DB: return "ERR_CREATE_SQLITE3_DB";
case ERR_OPEN_SQLITE3_DB: return "ERR_OPEN_SQLITE3_DB";
case ERR_SQLITE3_CREATE_TABLE: return "ERR_SQLITE3_CREATE_TABLE";
case ERR_SYNC_DATABASE: return "ERR_SYNC_DATABASE";
case ERR_SQL_QUERY: return "ERR_SQL_QUERY";
case ERR_SQL_DELETE: return "ERR_SQL_DELETE";
case ERR_UNKNOWN_TYPE: return "ERR_UNKNOWN_TYPE";
case ERR_PERMISSION_DENIED: return "ERR_PERMISSION_DENIED";
case ERR_CFG_NOITEM: return "ERR_CFG_NOITEM";
case ERR_CFG_ITEM_EXIST: return "ERR_CFG_ITEM_EXIST";
case ERR_CFG_WAIT_RSP: return "ERR_CFG_WAIT_RSP";
case ERR_CFG_BUSY: return "ERR_CFG_BUSY";
case ERR_STR_CONVERT: return "ERR_STR_CONVERT";
case ERR_SQL_REG_MODULE: return "ERR_SQL_REG_MODULE";
default: return "Unknown Error";
}
}
const char* DBusCmdToString(DBUS_CMD cmd)
{
switch(cmd)
{
case CMD_MISC_PING: return "CMD_MISC_PING";
case CMD_MISC_OTA: return "CMD_MISC_OTA";
case CMD_MISC_WEATHER: return "CMD_MISC_WEATHER";
case CMD_MISC_NOWTIME: return "CMD_MISC_NOWTIME";
case CMD_MISC_UPGRADE: return "CMD_MISC_UPGRADE";
case CMD_SYSTEM_STANDBY: return "CMD_SYSTEM_STANDBY";
case CMD_MISC_QUERY_OTA_STATUS: return "CMD_MISC_QUERY_OTA_STATUS";
case CMD_MISC_QUERY_DL_STATUS: return "CMD_MISC_QUERY_DL_STATUS";
case CMD_CALL_DIAL: return "CMD_CALL_DIAL";
case CMD_CALL_ACCEPI: return "CMD_CALL_ACCEPI";
case CMD_CALL_HANGUP: return "CMD_CALL_HANGUP";
case CMD_CALL_MESSAGE: return "CMD_CALL_MESSAGE";
case CMD_PLAY_MODECHANGE: return "CMD_PLAY_MODECHANGE";
case CMD_PLAY_PLAY: return "CMD_PLAY_PLAY";
case CMD_PLAY_PAUSE: return "CMD_PLAY_PAUSE";
case CMD_PLAY_STOP: return "CMD_PLAY_STOP";
case CMD_PLAY_SEEKTO: return "CMD_PLAY_SEEKTO";
case CMD_PLAY_SHOWMODE: return "CMD_PLAY_SHOWMODE";
case CMD_PLAY_NEXT: return "CMD_PLAY_NEXT";
case CMD_PLAY_PRE: return "CMD_PLAY_PRE";
case CMD_PLAY_SHOWLIST: return "CMD_PLAY_SHOWLIST";
case CMD_PLAY_UPDATELIST: return "CMD_PLAY_UPDATELIST";
case CMD_PLAY_PREPARE_NEXT: return "CMD_PLAY_PREPARE_NEXT";
case CMD_PLAY_ADDTOLIST: return "CMD_PLAY_ADDTOLIST";
case CMD_PLAY_DELETEFROMLIST: return "CMD_PLAY_DELETEFROMLIST";
case CMD_PLAY_RESETLIST: return "CMD_PLAY_RESETLIST";
case CMD_PLAY_AUDIO_STOP: return "CMD_PLAY_AUDIO_STOP";
case CMD_PLAY_AUDIO_PLAY: return "CMD_PLAY_AUDIO_PLAY";
case CMD_SE_PLAY: return "CMD_SE_PLAY";
case CMD_PLAY_RET_STATUS: return "CMD_PLAY_RET_STATUS";
case CMD_CFG_ADD_REQ: return "CMD_CFG_ADD_REQ";
case CMD_CFG_ADD_RSP: return "CMD_CFG_ADD_RSP";
case CMD_CFG_CHANGE_REQ: return "CMD_CFG_CHANGE_REQ";
case CMD_CFG_CHANGE_RSP: return "CMD_CFG_CHANGE_RSP";
case CMD_CFG_GET_REQ: return "CMD_CFG_GET_REQ";
case CMD_CFG_GET_RSP: return "CMD_CFG_GET_RSP";
case CMD_CFG_UPG_NOTIFY: return "CMD_CFG_UPG_NOTIFY";
case CMD_MSC_MSG_CONTROLLER_RECOG_SUCCESS: return "CMD_MSC_MSG_CONTROLLER_RECOG_SUCCESS";
case CMD_MSC_MSG_CONTROLLER_RECOG_ERROR: return "CMD_MSC_MSG_CONTROLLER_RECOG_ERROR";
case CMD_MSC_MSG_CONTROLLER_WAKEUP: return "CMD_MSC_MSG_CONTROLLER_WAKEUP";
case CMD_MSC_MSG_CONTROLLER_RECOGING: return "CMD_MSC_MSG_CONTROLLER_RECOGING";
case CMD_CONTROLLER_REQMSG_INITARGS: return "CMD_CONTROLLER_REQMSG_INITARGS";
case CMD_CONTROLLER_RSPMSG_INITARGS: return "CMD_CONTROLLER_RSPMSG_INITARGS";
case CMD_CONTROLLER_REQMSG_PLAYERSTATUS: return "CMD_CONTROLLER_REQMSG_PLAYERSTATUS";
case CMD_CONTROLLER_RSPMSG_PLAYERSTATUS: return "CMD_CONTROLLER_RSPMSG_PLAYERSTATUS";
case CMD_MSC_REQMSG_MIC_CONTROL: return "CMD_MSC_REQMSG_MIC_CONTROL";
case CMD_MSC_RSPMSG_MIC_CONTROL: return "CMD_MSC_RSPMSG_MIC_CONTROL";
case CMD_YUNXIN_RECVMSG: return "CMD_YUNXIN_RECVMSG";
case CMD_YUNXIN_SENDMSG: return "CMD_YUNXIN_SENDMSG";
case CMD_YUNXIN_SENDMSG_BYPASS: return "CMD_YUNXIN_SENDMSG_BYPASS";
case CMD_YUNXIN_SENDMSGCB: return "CMD_YUNXIN_SENDMSGCB";
case CMD_CONTROLLER_MSG_YUNXIN: return "CMD_CONTROLLER_MSG_YUNXIN";
case CMD_YUNXIN_STATUS: return "CMD_YUNXIN_STATUS";
case CMD_YUNXIN_SYSMSG: return "CMD_YUNXIN_SYSMSG";
case CMD_WIFI_CONF: return "CMD_WIFI_CONF";
case CMD_WIFI_CONF_RESP: return "CMD_WIFI_CONF_RESP";
case CMD_WIFI_AUTO_CONN: return "CMD_WIFI_AUTO_CONN";
case CMD_WIFI_AUTO_CONN_RESP: return "CMD_WIFI_AUTO_CONN_RESP";
case CMD_WIFI_STATE_REQ: return "CMD_WIFI_STATE_REQ";
case CMD_WIFI_STATE_RESP: return "CMD_WIFI_STATE_RESP";
case CMD_WIFI_STATE_NTF: return "CMD_WIFI_STATE_NTF";
case CMD_BT_NAME_GET_REQ: return "CMD_BT_NAME_GET_REQ";
case CMD_BT_NAME_GET_RESP: return "CMD_BT_NAME_GET_RESP";
case CMD_BT_EVT_NTF: return "CMD_BT_EVT_NTF";
case CMD_KPLAYER_START: return "CMD_KPLAYER_START";
case CMD_KPLAYER_STOP: return "CMD_KPLAYER_STOP";
case CMD_KPLAYER_NOTIF_DUR: return "CMD_KPLAYER_NOTIF_DUR";
case CMD_KPLAYER_HOST_ACTION: return "CMD_KPLAYER_HOST_ACTION";
case CMD_KPLAYER_CTR_NTF_BASE: return "CMD_KPLAYER_CTR_NTF_BASE";
case CMD_KPLAYER_CTR_CREATED: return "CMD_KPLAYER_CTR_CREATED";
case CMD_KPLAYER_CTR_DELED: return "CMD_KPLAYER_CTR_DELED";
case CMD_KPLAYER_CTR_PLAY: return "CMD_KPLAYER_CTR_PLAY";
case CMD_KPLAYER_CTR_STOP: return "CMD_KPLAYER_CTR_STOP";
case CMD_KPLAYER_CTR_PAUSE: return "CMD_KPLAYER_CTR_PAUSE";
case CMD_KPLAYER_CTR_SEEK: return "CMD_KPLAYER_CTR_SEEK";
case CMD_KPLAYER_CTR_SET_URL: return "CMD_KPLAYER_CTR_SET_URL";
case CMD_KPLAYER_CTR_SET_VOLUME: return "CMD_KPLAYER_CTR_SET_VOLUME";
case CMD_KPLAYER_CTR_SET_MUTE: return "CMD_KPLAYER_CTR_SET_MUTE";
case CMD_KPLAYER_CTR_SET_NXT_URL: return "CMD_KPLAYER_CTR_SET_NXT_URL";
case CMD_KPLAYER_CTR_SET_NEXT: return "CMD_KPLAYER_CTR_SET_NEXT";
case CMD_KPLAYER_CTR_SET_PREV: return "CMD_KPLAYER_CTR_SET_PREV";
case CMD_ALARM_SYNC_REQ: return "CMD_ALARM_SYNC_REQ";
case CMD_ALARM_SYNC_RSP: return "CMD_ALARM_SYNC_RSP";
case CMD_ALARM_ADD: return "CMD_ALARM_ADD";
case CMD_ALARM_REMOVE: return "CMD_ALARM_REMOVE";
case CMD_ALARM_REMOVEALL: return "CMD_ALARM_REMOVEALL";
case CMD_REMAIND_SYNC_REQ: return "CMD_REMAIND_SYNC_REQ";
case CMD_REMAIND_SYNC_RSP: return "CMD_REMAIND_SYNC_RSP";
case CMD_REMAIND_ADD: return "CMD_REMAIND_ADD";
case CMD_REMAIND_REMOVE: return "CMD_REMAIND_REMOVE";
case CMD_REMAIND_REMOVEALL: return "CMD_REMAIND_REMOVEALL";
case CMD_ASSISTANT_STATUS: return "CMD_ASSISTANT_STATUS";
case CMD_ASSISTANT_RUNNING: return "CMD_ASSISTANT_RUNNING";
case CMD_ASSISTANT_NOTIFY: return "CMD_ASSISTANT_NOTIFY";
case CMD_SESSION_ALARM_SYNC: return "CMD_SESSION_ALARM_SYNC";
case CMD_WORKDAY_DB_REQ: return "CMD_WORKDAY_DB_REQ";
case CMD_WORKDAY_DB_RSP: return "CMD_WORKDAY_DB_RSP";
case CMD_OTA_NOTIFY: return "CMD_OTA_NOTIFY";
case CMD_OTA_STATUS: return "CMD_OTA_STATUS";
case CMD_OTA_RUNNOW: return "CMD_OTA_RUNNOW";
case CMD_LOG_CONFIG: return "CMD_LOG_CONFIG";
default: return "Unknown CMD";
}
}
const char* ModuleNameToString(MODULE_NAME modName)
{
switch(modName)
{
case MODULE_CONTROLLER: return "MODULE_CONTROLLER";
case MODULE_ALARM: return "MODULE_ALARM";
case MODULE_CALL: return "MODULE_CALL";
case MODULE_VOICEENGINE: return "MODULE_VOICEENGINE";
case MODULE_PLAYER: return "MODULE_PLAYER";
case MODULE_CONFIGURE: return "MODULE_CONFIGURE";
case MODULE_OTA: return "MODULE_OTA";
case MODULE_WIFI: return "MODULE_WIFI";
case MODULE_BT: return "MODULE_BT";
case MODULE_KPLAYER: return "MODULE_KPLAYER";
case MODULE_KPLAYER_TEST: return "MODULE_KPLAYER_TEST";
case MODULE_SPLAYER: return "MODULE_SPLAYER";
case MODULE_SPLAYER_TEST: return "MODULE_SPLAYER_TEST";
case MODULE_LIGHT_MCU: return "MODULE_LIGHT_MCU";
case MODULE_BLUEKC: return "MODULE_BLUEKC";
case MODULE_BLUEKC_TEST: return "MODULE_BLUEKC_TEST";
case MODULE_MANUFACTURE: return "MODULE_MANUFACTURE";
case MODULE_BT_DEMO: return "MODULE_BT_DEMO";
case MODULE_LOG_CTRL: return "MODULE_LOG_CTRL";
}
return "Unknown Module Name";
}