PV1_MakeProject/Modules/ConfigServer/config_server.c

211 lines
6.5 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/time.h>
#include <uv.h>
#include <dbus/dbus.h>
#include <errno.h>
#ifdef PLATFORM_R16
#include "log.h"
#include "libuv_dbus.h"
#include "crypto.h"
#include "json_struct.h"
#include "config_engine.h"
#else
#include <uvdbus/log.h>
#include <uvdbus/libuv_dbus.h>
#include <uvdbus/crypto.h>
#include <uvdbus/json_struct.h>
#include <uvdbus/config_engine.h>
#endif
static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t* pLoop, DBusConnection* pConn, PDBUS_MSG_PACK pMsg)
{
#if 0
int err;
int iValue;
double dValue;
PCFG_API_REQ pReq = NULL;
const char *pJsonStr = NULL;
CFG_API_RSP cfgRsp;
char *pEndPtr = NULL;
int overWrite = FALSE;
if(pMsg == NULL || pMsg->pMsg == NULL)
{
return (NULL);
}
pJsonStr = (const char *)pMsg->pMsg;
pReq = (PCFG_API_REQ)Json2Struct(pJsonStr, JSON_ENGINE_CFG_REQ, TRUE, &err);
memset(&cfgRsp, 0, sizeof(CFG_API_RSP));
strncpy(cfgRsp.keyName, pReq->keyName, MAX_CFG_KEY_NAME);
strncpy(cfgRsp.keyValue, pReq->keyValue, MAX_CFG_KEY_VALUE);
cfgRsp.keyType = pReq->keyType;
if(pReq == NULL || err != 0)
{
LOG_EX(LOG_Error, "Decode Json Error: %p, %d\n", pReq, err);
if(pReq)
{
free(pReq);
}
return (NULL);
}
switch(pMsg->busCmd)
{
case CMD_CFG_ADD:
case CMD_CFG_CHANGE:
if(pMsg->busCmd == CMD_CFG_CHANGE)
{
CFG_ITEM item;
PCFG_ITEM pItem = &item;
if(CfgGetKeyValue(pReq->keyName, &pItem) == -ERR_CFG_NOITEM)
{
cfgRsp.errNo = -ERR_CFG_NOITEM;
DBusJsonSendToCommand(NULL, g_pModInfoTable[pMsg->msgSrc].modAliase, CMD_CFG_UPG_NOTIFY, JSON_ENGINE_CFG_RSP, &cfgRsp, TRUE);
return (NULL);
}
overWrite = TRUE;
}
switch(pReq->keyType)
{
case CFG_TYPE_STRING:
cfgRsp.errNo = CfgAddGlobalStringItem(pReq->keyName, pReq->keyValue, overWrite);
break;
case CFG_TYPE_INT:
iValue = strtol(pReq->keyValue, NULL, 10);
if(errno != ERANGE && errno != EINVAL)
{
cfgRsp.errNo = CfgAddGlobalIntItem(pReq->keyName, iValue, overWrite);
if(cfgRsp.errNo != 0)
{
LOG_EX(LOG_Error, "Add Configure [%s](%d) --> [%s] Error: %d\n",
pReq->keyName, pReq->keyType, pReq->keyValue, err);
}
}
else
{
cfgRsp.errNo = -ERR_INPUT_PARAMS;
LOG_EX(LOG_Error, "Input Params Error: [%s](%d) --> [%s]\n",
pReq->keyName, pReq->keyType, pReq->keyValue);
}
break;
case CFG_TYPE_DOUBLE:
dValue = strtod(pReq->keyValue, &pEndPtr);
if((errno == ERANGE) || (dValue == 0 && (strcmp(pReq->keyName, pEndPtr) == 0)))
{
cfgRsp.errNo = -ERR_INPUT_PARAMS;
LOG_EX(LOG_Error, "Input Params Error: [%s](%d) --> [%s]\n",
pReq->keyName, pReq->keyType, pReq->keyValue);
}
else
{
cfgRsp.errNo = CfgAddGlobalIntItem(pReq->keyName, dValue, overWrite);
if(cfgRsp.errNo != 0)
{
LOG_EX(LOG_Error, "Add Configure [%s](%d) --> [%s] Error: %d\n",
pReq->keyName, pReq->keyType, pReq->keyValue, err);
}
}
break;
default:
cfgRsp.errNo = -ERR_UNKNOWN_TYPE;
break;
}
if(cfgRsp.errNo == 0)
{
DBusJsonBoardcastCommand(NULL, 0xFFFFFFFF, CMD_CFG_UPG_NOTIFY, JSON_ENGINE_CFG_RSP, &cfgRsp, TRUE);
}
else
{
DBusJsonSendToCommand(NULL, g_pModInfoTable[pMsg->msgSrc].modAliase, CMD_CFG_UPG_NOTIFY, JSON_ENGINE_CFG_RSP, &cfgRsp, TRUE);
}
break;
case CMD_CFG_GET:
{
PCFG_ITEM pItem = NULL;
if(CfgGetKeyValue(pReq->keyName, &pItem) == -ERR_CFG_NOITEM)
{
cfgRsp.errNo = -ERR_CFG_NOITEM;
DBusJsonSendToCommand(NULL, g_pModInfoTable[pMsg->msgSrc].modAliase, CMD_CFG_UPG_NOTIFY, JSON_ENGINE_CFG_RSP, &cfgRsp, TRUE);
return (NULL);
}
else
{
memset(cfgRsp.keyValue, 0, MAX_CFG_KEY_VALUE);
cfgRsp.keyType = pItem->keyType;
if(pItem->keyType == CFG_TYPE_STRING)
{
strncpy(cfgRsp.keyValue, pItem->pStrValue, MAX_CFG_KEY_VALUE);
}
else if(pItem->keyType == CFG_TYPE_INT)
{
sprintf(cfgRsp.keyValue, "%d", pItem->intValue);
}
else if(pItem->keyType == CFG_TYPE_DOUBLE)
{
sprintf(cfgRsp.keyValue, "%f", pItem->doubleValue);
}
DBusJsonSendToCommand(NULL, g_pModInfoTable[pMsg->msgSrc].modAliase, CMD_CFG_GET, JSON_ENGINE_CFG_RSP, &cfgRsp, TRUE);
}
}
break;
default:
//cfgRsp.errNo = -ERR_UNKNOWN_TYPE;
//DBusJsonSendToCommand(NULL, g_pModInfoTable[pMsg->msgSrc].modAliase, CMD_CFG_UPG_NOTIFY, JSON_ENGINE_CFG_RSP, &cfgRsp, TRUE);
break;
}
#endif
return NULL;
}
static void __dBusHeartLost(MODULE_NAME loster)
{
LOG_EX(LOG_Error, "Found %d Heart Lost\n", loster);
}
int main(int argc, char** argv)
{
int ret = 0;
uv_loop_t* pLoop = GetDBusDefaultLoop();
DBusConnection* pBus = DBusWithLibuvInit(pLoop, g_pModInfoTable[MODULE_CONFIGURE].modAliase,
__dBusOnMessage,
NULL,
NULL, &ret);
if(pBus == NULL)
{
fprintf(stderr, "DBusWithLibuvInit Error: %d\n", ret);
return 0;
}
//DBusWithLibuvCfgInit(__onCfgMsgCb);
//DBusWithLibuvCfgInit();
return uv_run(pLoop, UV_RUN_DEFAULT);
}