blob: 1c7babc1ecccac37b02c39164f48a459d748ff3f [file] [log] [blame]
#include "apparms.h"
#include "apparms_json.h"
#include "apparms_log.h"
static unsigned char* arms_json_construct_final_buf(int* pResult, char* pType, json_object **pRsp, unsigned char* pOutBuf, int* pOutLen)
{
json_object *final_json = NULL;
const char *resp = NULL;
int nDataLen;
unsigned char* pRetBuf = NULL;
final_json = json_object_new_object();
CHECK_NEW_JSON_WITH_FREE_REQ(final_json, *pRsp);
//resp = json_object_to_json_string(*pRsp);
//CHECK_JSON_STRING_FREE_REQ_RSP(resp, final_json, *pRsp);
//my_printf(LOG_MODE_LEVEL_5, "%s(%d) rsp Json string content = %s\n",__FUNCTION__,__LINE__,resp);
json_object_object_add(final_json, (const char *)pType, *pRsp);
//resp = NULL;
resp = json_object_to_json_string(final_json);
CHECK_JSON_STRING_FREE_REQ_RSP(resp, final_json, *pRsp);
ARMS_LOG_DEBUG("%s(%d) final json to string content=%s\n",__FUNCTION__,__LINE__, resp);
nDataLen = strlen(resp);
/* dynamic buffer is needed*/
if (nDataLen >= *pOutLen)
{
pRetBuf = malloc(nDataLen + 1);
CHECK_JSON_STRING_FREE_REQ_RSP(pRetBuf, final_json, *pRsp);
/*memset(pRetBuf, 0, nDataLen+1);*/
memcpy(pRetBuf, resp, nDataLen);
pRetBuf[nDataLen] = '\0';
ARMS_LOG_INFO("%s(%d) Final New Rsp Buffer = %s\n",__FUNCTION__,__LINE__,pRetBuf);
}
else
{
memcpy(pOutBuf, resp, nDataLen);
ARMS_LOG_INFO("%s(%d) Final OUT Buffer = %s\n",__FUNCTION__,__LINE__,pOutBuf);
}
json_object_put(final_json);
//json_object_put(*pRsp);
ARMS_LOG_DEBUG("%s(%d) ConstructFinalBuffer Finished\n",__FUNCTION__,__LINE__);
*pOutLen = nDataLen;
return pRetBuf;
}
static unsigned char* arms_json_handle_devlog(int nRemote,int *pResult, json_object *pJsonReq, unsigned char* pOutBuf, int* pOutLen, int *pContExe)
{
json_object *rsp_json = NULL;
const char *pOperCont = NULL;
*pResult = JSON_FALSE;
rsp_json = json_object_new_object();
if (rsp_json == NULL)
return NULL;
json_object_object_foreach(pJsonReq, key, val)
{
ARMS_LOG_INFO("%s(%d) key=%s, val=%s\n",__FUNCTION__,__LINE__,key, json_object_get_string(val));
if (strcmp(key, APPARMS_JSON_COMMON_OPER_STR) == 0)
{
pOperCont = json_object_get_string(val);
}
}
#ifdef ARMS_SUPPORT_FOTA
if ((pOperCont != NULL) && (strcmp(pOperCont, "ReportToSrv") == 0))
{
PAPPARMS pApp = GetARMSAppPointer();
arms_fota_add_msg_event(&pApp->stARMSFOTA, ARMS_SIGN_LOG_START, NULL, 0);
*pResult = JSON_TRUE;
*pContExe = JSON_FALSE;
}
#endif
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_OPER_STR, json_object_new_string(pOperCont));
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_RESULT_STR, json_object_new_string(((*pResult != JSON_FALSE) ? APPARMS_JSON_COMMON_OK_STR : APPARMS_JSON_COMMON_FAIL_STR)));
json_object_object_add(rsp_json, APPARMS_JSON_COMM_MSG_CODE, json_object_new_string(APPARMS_JSON_COMM_MSG_0000));
return arms_json_construct_final_buf(pResult, APPARMS_JSON_DEVLOG_RSP, &rsp_json, pOutBuf, pOutLen);
}
static unsigned char* arms_json_handle_armslog_cfg(int nRemote,int *pResult, json_object *pJsonReq, unsigned char* pOutBuf, int* pOutLen, int *pContExe)
{
json_object *rsp_json = NULL;
const char *pOperCont = NULL;
int nLevel = -1;
*pResult = JSON_FALSE;
rsp_json = json_object_new_object();
if (rsp_json == NULL)
return NULL;
json_object_object_foreach(pJsonReq, key, val)
{
ARMS_LOG_INFO("%s(%d) key=%s, val=%s\n",__FUNCTION__,__LINE__,key, json_object_get_string(val));
if (strcmp(key, APPARMS_JSON_COMMON_OPER_STR) == 0)
{
pOperCont = json_object_get_string(val);
}
else if (strcmp(key, "Level") == 0)
{
nLevel = json_object_get_int(val);
}
}
if ((pOperCont != NULL) && (strcmp(pOperCont, "Set") == 0))
{
if(nLevel >= ARMS_LOG_LEVEL_ERROR && nLevel <= ARMS_LOG_LEVEL_ALL)
{
arms_log_set_level(nLevel);
*pResult = JSON_TRUE;
}
}
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_OPER_STR, json_object_new_string(pOperCont));
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_RESULT_STR, json_object_new_string(((*pResult != JSON_FALSE) ? APPARMS_JSON_COMMON_OK_STR : APPARMS_JSON_COMMON_FAIL_STR)));
json_object_object_add(rsp_json, APPARMS_JSON_COMM_MSG_CODE, json_object_new_string(APPARMS_JSON_COMM_MSG_0000));
return arms_json_construct_final_buf(pResult, APPARMS_JSON_ARMSLOG_CFG_RSP, &rsp_json, pOutBuf, pOutLen);
}
static unsigned char* arms_json_handle_fota(int nRemote,int *pResult, json_object *pJsonReq, unsigned char* pOutBuf, int* pOutLen, int *pContExe)
{
json_object *rsp_json = NULL;
const char *pOperCont = NULL;
*pResult = JSON_FALSE;
rsp_json = json_object_new_object();
if (rsp_json == NULL)
return NULL;
json_object_object_foreach(pJsonReq, key, val)
{
ARMS_LOG_INFO("%s(%d) key=%s, val=%s\n",__FUNCTION__,__LINE__,key, json_object_get_string(val));
if (strcmp(key, APPARMS_JSON_COMMON_OPER_STR) == 0)
{
pOperCont = json_object_get_string(val);
}
}
#ifdef ARMS_SUPPORT_FOTA
if ((pOperCont != NULL) && ((strcmp(pOperCont, "Upgrade") == 0) || strcmp(pOperCont, "Check") == 0))
{
PAPPARMS pApp = GetARMSAppPointer();
arms_fota_add_msg_event(&pApp->stARMSFOTA, ARMS_SIGN_FOTA_START, NULL, 0);
*pResult = JSON_TRUE;
*pContExe = JSON_FALSE;
}
#endif
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_OPER_STR, json_object_new_string(pOperCont));
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_RESULT_STR, json_object_new_string(((*pResult != JSON_FALSE) ? APPARMS_JSON_COMMON_OK_STR : APPARMS_JSON_COMMON_FAIL_STR)));
json_object_object_add(rsp_json, APPARMS_JSON_COMM_MSG_CODE, json_object_new_string(APPARMS_JSON_COMM_MSG_0000));
return arms_json_construct_final_buf(pResult, APPARMS_JSON_FOTA_RSP, &rsp_json, pOutBuf, pOutLen);
}
#ifdef ARMS_SUPPORT_DM
#ifdef ARMS_DM_ATRACS
static unsigned char* arms_json_handle_reoprt(int nRemote,int *pResult, json_object *pJsonReq, unsigned char* pOutBuf, int* pOutLen, int *pContExe)
{
json_object *rsp_json = NULL;
const char *pOper = NULL;
const char *pEvtType = NULL;
const char *pEvtText = NULL;
const char *pWarnText = NULL;
int nEvtTypeLen = 0, nEvtTextLen = 0, nEvtWarnLen = 0;
*pResult = JSON_FALSE;
rsp_json = json_object_new_object();
if (rsp_json == NULL)
return NULL;
json_object_object_foreach(pJsonReq, key, val)
{
ARMS_LOG_INFO("%s(%d) key=%s, val=%s\n",__FUNCTION__,__LINE__,key, json_object_get_string(val));
if (strcmp(key, APPARMS_JSON_COMMON_OPER_STR) == 0)
{
pOper = json_object_get_string(val);
}
else if (strcmp(key, APPARMS_JSON_COMMON_EVENT_TYPE) == 0)
{
pEvtType = json_object_get_string(val);
if (pEvtType != NULL)
nEvtTypeLen = strlen(pEvtType);
}
else if (strcmp(key, APPARMS_JSON_COMMON_EVENT_TEXT) == 0)
{
pEvtText = json_object_get_string(val);
if (pEvtText != NULL)
nEvtTextLen = strlen(pEvtText);
}
else if (strcmp(key, APPARMS_JSON_COMMON_WARNING_TEXT) == 0)
{
pWarnText = json_object_get_string(val);
if (pWarnText != NULL)
nEvtWarnLen = strlen(pWarnText);
}
}
if (pOper != NULL)
{
if(strcmp(pOper, "ReportTelemetry") == 0)
{
PAPPARMS pApp = GetARMSAppPointer();
arms_dm_add_report_telemetry_msg_event(&pApp->stARMSDM);
*pResult = JSON_TRUE;
}
else if(strcmp(pOper, "ReportFota") == 0)
{
if ((nEvtTypeLen > 0) && (nEvtTextLen > 0) && (nEvtWarnLen > 0))
{
#ifdef ARMS_SUPPORT_EC2
PAPPARMS pApp = GetARMSAppPointer();
EC2EVTMSGST stEC2Msg;
memset(&stEC2Msg, 0, sizeof(EC2EVTMSGST));
stEC2Msg.evt_type = atoi(pEvtType);
strcpy((char *)stEC2Msg.evt_content, pEvtText);
stEC2Msg.evt_len = nEvtTextLen;
strcpy((char *)stEC2Msg.warn_text, pWarnText);
stEC2Msg.warn_len = nEvtWarnLen;
arms_ec2_add_msg_event(&pApp->stARMSEC2, ARMS_SIGN_FOTA_STATUS_REPORT, (unsigned char*)&stEC2Msg, sizeof(EC2EVTMSGST));
#endif
*pResult = JSON_TRUE;
}
}
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_OPER_STR, json_object_new_string(pOper));
}
json_object_object_add(rsp_json, APPARMS_JSON_COMMON_RESULT_STR, json_object_new_string(((*pResult != JSON_FALSE) ? APPARMS_JSON_COMMON_OK_STR : APPARMS_JSON_COMMON_FAIL_STR)));
json_object_object_add(rsp_json, APPARMS_JSON_COMM_MSG_CODE, json_object_new_string(APPARMS_JSON_COMM_MSG_0000));
return arms_json_construct_final_buf(pResult, APPARMS_JSON_REPORT_RSP, &rsp_json, pOutBuf, pOutLen);
}
#endif
#endif
static int arms_json_parse(int nRemote, char *cmdBuf, int cmdBuf_len, unsigned char *rspBuf, int *pRspBuf_len, int *pContExe)
{
int nRet = 0, nHandRet = JSON_FALSE;
json_object *req_json = NULL;
unsigned char *pRetNewBuf = NULL;
int bFound = 0;
if ((cmdBuf == NULL) || (rspBuf == NULL) || (pRspBuf_len == NULL) || (pContExe == NULL))
{
ARMS_LOG_ERROR(" %s(%d) poll struct pointer is null\n",__FUNCTION__,__LINE__);
return ARMS_JSON_POINT_NULL_ERROR;
}
if ((cmdBuf_len <= 0) || (*pRspBuf_len <= 0))
{
ARMS_LOG_ERROR(" %s(%d) poll struct pointer is null\n",__FUNCTION__,__LINE__);
return ARMS_JSON_BUFFER_LEN_ERROR;
}
req_json = json_tokener_parse((const char *)cmdBuf);
/*Invalid json format, let's send the common Response */
if (req_json != NULL)
{
enum json_type json_result = json_object_get_type(req_json);
ARMS_LOG_INFO(" %s(%d) json_type =[%d]\n",__FUNCTION__,__LINE__, json_result);
if (json_result == json_type_object)
{
json_object_object_foreach(req_json, key, val)
{
ARMS_LOG_INFO("%s(%d) key=%s, val=%s\n",__FUNCTION__,__LINE__,key, json_object_get_string(val));
if (strcmp(key, APPARMS_JSON_DEVLOG_REQ) == 0)
{
pRetNewBuf = arms_json_handle_devlog(nRemote, &nHandRet, val, rspBuf, pRspBuf_len, pContExe);
bFound = 1;
}
else if (strcmp(key, APPARMS_JSON_ARMSLOG_CFG_REQ) == 0)
{
pRetNewBuf = arms_json_handle_armslog_cfg(nRemote, &nHandRet, val, rspBuf, pRspBuf_len, pContExe);
bFound = 1;
}
else if (strcmp(key, APPARMS_JSON_FOTA_REQ) == 0)
{
pRetNewBuf = arms_json_handle_fota(nRemote, &nHandRet, val, rspBuf, pRspBuf_len, pContExe);
bFound = 1;
}
#ifdef ARMS_SUPPORT_DM
#ifdef ARMS_DM_ATRACS
else if (strcmp(key, APPARMS_JSON_REPORT_REQ) == 0)
{
pRetNewBuf = arms_json_handle_reoprt(nRemote, &nHandRet, val, rspBuf, pRspBuf_len, pContExe);
bFound = 1;
}
#endif
#endif
if (bFound == 1)
break;
}
}
/*free the request json object*/
json_object_put(req_json);
}
//Unkwown Command or Invalid format
if (bFound == 0)
{
if (nRemote == 0)
{
ARMS_LOG_ERROR("%s(%d) invalid json format=%d\n",__FUNCTION__,__LINE__,nRet);
}
return ARMS_JSON_UNSUPPORTED_ERROR;
}
/*Handle json response failed*/
if (nHandRet == JSON_FALSE)
{
ARMS_LOG_ERROR("%s(%d) Handle json response failed\n",__FUNCTION__,__LINE__);
return ARMS_JSON_HANDLE_ERROR;
}
if (pRetNewBuf != NULL)
{
free(pRetNewBuf);
return ARMS_JSON_RSP_BUFLEN_ERROR;
}
return 0;
}
int arms_json_handle_buffer(int nRemote, char *cmdBuf, int cmdBuf_len, unsigned char *rspBuf, int *pRspBuf_len)
{
int nRet, nContExe = JSON_TRUE;
nRet = arms_json_parse(nRemote, cmdBuf, cmdBuf_len, rspBuf, pRspBuf_len, &nContExe);
if ((nRet < 0) && (nRemote == 1) && (nContExe == JSON_FALSE))
{
nRet = 0;
}
else if ((nRemote == 0) && (nRet == ARMS_JSON_UNSUPPORTED_ERROR))
{
const char *pSupport = "{\"UnknownCmd\":{\"RESULT\":\"FAIL\"}}";
strcpy((char *)rspBuf, pSupport);
*pRspBuf_len = strlen(pSupport);
}
return nRet;
}