2025-04-10 17:31:33 +08:00

246 lines
10 KiB
C

/****************************************************************************
*
* Copy right: 2017-, Copyrigths of EigenComm Ltd.
* File name: ctw_http.c
* Description: EC618 CTWING http access source file
* History: Rev1.0 2022-2-15
*
****************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "osasys.h"
#include "networkmgr.h"
//#include "ps_event_callback.h"
#include "ps_lib_api.h"
#include DEBUG_LOG_HEADER_FILE
#include "at_util.h"
#include "ctw_http.h"
#define HTTPSTA_RECV_BUF_SIZE (1501)
#define HTTPSTA_HEAD_BUF_SIZE (800)
#define HTTP_CONTTYPE "application/json"
static void ctwHttpCalcSign(char* outSign, uint32_t* outTimestamp, char* masterKey)
{
utc_timer_value_t* timeUtc = NULL;
char input[55] = {0};
uint8_t output[32] = {0};
timeUtc = OsaSystemTimeReadUtc();
if(timeUtc != NULL){
*outTimestamp = timeUtc->UTCsecs;
}
snprintf(input,55,"eigencomm%s%d000", masterKey, *outTimestamp);
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpCalcSign_1, P_INFO, "input str=%s", input);
atSha256((unsigned char *)input, 54, output);
cmsHexToLowHexStr(outSign, 65, output, 32);
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpCalcSign_2, P_INFO, "signature=%s", outSign);
}
void ctwHttpGetInvariPara(ctwHttpRegParam_t* pCtwHttpRegParam, MWNvmCfgCtwHttpParam* pCtwHttpParam)
{
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpGetInvariPara_1, P_INFO, "get imei and iccid");
OsaGetImeiNumSync(pCtwHttpRegParam->imei);
appGetIccidNumSync(pCtwHttpRegParam->iccid);
if((pCtwHttpParam->enableFlag & CTW_HTTP_ENABLE_FLAG_TLS) == 0){
strcpy(pCtwHttpRegParam->host, "http://http.ctwing.cn:8991");
}else{
strcpy(pCtwHttpRegParam->host, "https://http.ctwing.cn:8992");
}
}
void ctwHttpGetVariPara(ctwHttpRegParam_t* pCtwHttpRegParam, MWNvmCfgCtwHttpParam* pCtwHttpParam)
{
UeExtStatusInfo statusInfo;
CmsRetId ret = CMS_RET_SUCC;
ret = appGetUeExtStatusInfoSync(UE_EXT_STATUS_PHY, &statusInfo);
if(ret == CMS_RET_SUCC){
pCtwHttpRegParam->phyCellId = statusInfo.phyStatus.phyCellId;
pCtwHttpRegParam->rsrp = statusInfo.phyStatus.sRsrp/100;
pCtwHttpRegParam->snr = statusInfo.phyStatus.snr;
pCtwHttpRegParam->txPower = statusInfo.phyStatus.txPower;
}
ctwHttpCalcSign(pCtwHttpRegParam->signature, &(pCtwHttpRegParam->timestamp), pCtwHttpParam->masterKey);
}
uint8_t ctwHttpAuthReg(HttpClientContext* pHttpClient, MWNvmCfgCtwHttpParam* pCtwHttpParam, ctwHttpRegParam_t* pRegParam)
{
HTTPResult result = HTTP_INTERNAL;
HttpClientData clientData = {0};
UINT32 count = 0;
uint16_t headerLen = 0;
cJSON* cjson_resp = NULL;
cJSON* cjson_token = NULL;
char url[48] = {0};
MWNvmCfgCtwParamCfg ctwParamCfg = {0};
mwNvmCfgGetCtwParamConfig(&ctwParamCfg);
clientData.headerBuf = mallocEc(HTTPSTA_HEAD_BUF_SIZE);
clientData.headerBufLen = HTTPSTA_HEAD_BUF_SIZE;
clientData.respBuf = mallocEc(HTTPSTA_RECV_BUF_SIZE);
clientData.respBufLen = HTTPSTA_RECV_BUF_SIZE;
pHttpClient->custHeader = mallocEc(256);
memset(pHttpClient->custHeader, 0, 256);
snprintf(pHttpClient->custHeader, 256, "signKey:eigencomm\r\ntimestamp:%d000\r\nsignature:%s", pRegParam->timestamp, pRegParam->signature);
clientData.postContentType = HTTP_CONTTYPE;
clientData.postBuf = mallocEc(512);
memset(clientData.postBuf, 0, 512);
snprintf(clientData.postBuf, 512,
"{\"productId\":%d,\"deviceId\":\"%s\",\"password\":\"%s\",\"version\":\"1.0\",\"softversion\":\"%s\",\"Module\":\"%s\",\"chiptype\":\"%s\",\"iccid\":\"%s\",\"imsi\":\"%s\",\"imei\":\"%s\",\"rsrp\":%d,\"sinr\":%d,\"txpower\":%d,\"cellid\":%d}",
pCtwHttpParam->productId, pCtwHttpParam->deviceId, pCtwHttpParam->password, ctwParamCfg.softVersion, ctwParamCfg.module, ctwParamCfg.chipType, pRegParam->iccid, pRegParam->imsi, pRegParam->imei, pRegParam->rsrp, pRegParam->snr, pRegParam->txPower, pRegParam->phyCellId);
clientData.postBufLen = strlen(clientData.postBuf);
strncpy(url,pRegParam->host,42);
strcat(url, "/auth");
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_0_1, P_INFO, "url:%s", pRegParam->host);
result = httpSendRequest(pHttpClient, url, HTTP_POST, &clientData);
if (result != HTTP_OK){
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_0, P_INFO, "send request failed result=%d go exit", result);
goto exit;
}
do {
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_1, P_INFO, "recvResponse loop.");
memset(clientData.headerBuf, 0, clientData.headerBufLen);
memset(clientData.respBuf, 0, clientData.respBufLen);
result = httpRecvResponse(pHttpClient, &clientData);
if(result == HTTP_OK || result == HTTP_MOREDATA){
headerLen = strlen(clientData.headerBuf);
if(headerLen > 0)
{
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_2, P_INFO, "total content length=%d", clientData.recvContentLength);
}
if(clientData.blockContentLen > 0)
{
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_3, P_INFO, "response content:{%s}", (uint8_t*)clientData.respBuf);
cjson_resp = cJSON_Parse(clientData.respBuf);
if(cjson_resp == NULL){
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_4, P_INFO, "json parse fail");
result = HTTP_PRTCL;
goto exit;
}
cjson_token = cJSON_GetObjectItem(cjson_resp, "accessToken");
if(cjson_token->type == cJSON_String){
strcpy(pCtwHttpParam->token, cjson_token->valuestring);
pCtwHttpParam->enableFlag |= CTW_HTTP_ENABLE_FLAG_TOKEN; //bit:3 enable
mwNvmCfgSetAndSaveCtwHttpParam(pCtwHttpParam);
}else{
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_5, P_INFO, "token not string(%d) fail to get token", cjson_token->type);
result = HTTP_PRTCL;
}
cJSON_Delete(cjson_resp);
}
count += clientData.blockContentLen;
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_6, P_INFO, "has recv=%d", count);
}
} while (result == HTTP_MOREDATA || result == HTTP_CONN);
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpAuthReg_7, P_INFO, "result=%d", result);
exit:
freeEc(clientData.headerBuf);
freeEc(clientData.respBuf);
freeEc(clientData.postBuf);
freeEc(pHttpClient->custHeader);
return result;
}
uint8_t ctwHttpSendData(HttpClientContext* pHttpClient, MWNvmCfgCtwHttpParam* pCtwHttpParam, ctwHttpRegParam_t* pRegParam, char* topic, char* data)
{
HTTPResult result = HTTP_INTERNAL;
HttpClientData clientData = {0};
UINT32 count = 0;
uint16_t headerLen = 0;
cJSON* cjson_resp = NULL;
cJSON* cjson_code = NULL;
char url[48] = {0};
clientData.headerBuf = mallocEc(HTTPSTA_HEAD_BUF_SIZE);
clientData.headerBufLen = HTTPSTA_HEAD_BUF_SIZE;
clientData.respBuf = mallocEc(HTTPSTA_RECV_BUF_SIZE);
clientData.respBufLen = HTTPSTA_RECV_BUF_SIZE;
pHttpClient->custHeader = mallocEc(256);
memset(pHttpClient->custHeader, 0, 256);
snprintf(pHttpClient->custHeader, 256, "signKey:eigencomm\r\ntimestamp:%d000\r\nsignature:%s", pRegParam->timestamp, pRegParam->signature);
clientData.postContentType = HTTP_CONTTYPE;
clientData.postBuf = mallocEc(256);
memset(clientData.postBuf, 0, 256);
if(data == NULL){
snprintf(clientData.postBuf, 256, "{\"productId\":%d,\"deviceId\":\"%s\",\"accessToken\":\"%s\",\"topic\":\"%s\"}",
pCtwHttpParam->productId, pCtwHttpParam->deviceId, pCtwHttpParam->token, topic);
}else{
snprintf(clientData.postBuf, 256, "{\"productId\":%d,\"deviceId\":\"%s\",\"accessToken\":\"%s\",\"topic\":\"%s\",\"payload\":%s}",
pCtwHttpParam->productId, pCtwHttpParam->deviceId, pCtwHttpParam->token, topic, data);
}
clientData.postBufLen = strlen(clientData.postBuf);
strncpy(url,pRegParam->host,41);
strcat(url, "/topic");
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_0_1, P_INFO, "url:%s", pRegParam->host);
result = httpSendRequest(pHttpClient, url, HTTP_POST, &clientData);
if (result != HTTP_OK){
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_0, P_INFO, "send request failed result=%d go exit", result);
goto exit;
}
do {
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_1, P_INFO, "recvResponse loop.");
memset(clientData.headerBuf, 0, clientData.headerBufLen);
memset(clientData.respBuf, 0, clientData.respBufLen);
result = httpRecvResponse(pHttpClient, &clientData);
if(result == HTTP_OK || result == HTTP_MOREDATA){
headerLen = strlen(clientData.headerBuf);
if(headerLen > 0)
{
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_2, P_INFO, "total content length=%d", clientData.recvContentLength);
}
if(clientData.blockContentLen > 0)
{
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_3, P_INFO, "response content:{%s}", (uint8_t*)clientData.respBuf);
cjson_resp = cJSON_Parse(clientData.respBuf);
if(cjson_resp == NULL){
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_4, P_INFO, "json parse fail");
result = HTTP_PRTCL;
goto exit;
}
cjson_code = cJSON_GetObjectItem(cjson_resp, "code");
if(cjson_code->type == cJSON_Number && cjson_code->valueint == 0){
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_5, P_INFO, "send data success");
result = HTTP_OK;
}else{
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_6, P_INFO, "code is (%d) fail send data", cjson_code->valueint);
result = HTTP_PRTCL;
}
cJSON_Delete(cjson_resp);
}
count += clientData.blockContentLen;
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_7, P_INFO, "has recv=%d", count);
}
} while (result == HTTP_MOREDATA || result == HTTP_CONN);
ECPLAT_PRINTF(UNILOG_CTWING, ctwHttpSendData_8, P_INFO, "result=%d", result);
exit:
freeEc(clientData.headerBuf);
freeEc(clientData.respBuf);
freeEc(clientData.postBuf);
freeEc(pHttpClient->custHeader);
return result;
}