Add devicename configure support

This commit is contained in:
HuangXin 2019-03-01 17:52:25 +08:00
parent d2ab1d96cf
commit 6eee1e38b9
7 changed files with 141 additions and 19 deletions

View File

@ -20,6 +20,8 @@ static PUSER_CONFIG g_pUserCfg = NULL;
static GLOGAL_CONFIG g_globalCfg;
static int CfgEncrypto(unsigned char* pDest, unsigned char* pSrc, int iSlen);
static void __dumpGlobalCfg(void)
{
int err;
@ -247,6 +249,7 @@ static void __dumpUserConfig(PUSER_CONFIG pCfg)
}
LOG_EX(LOG_Debug, "User Configure Information:\n%s\n", pStr);
free(pStr);
}
@ -261,6 +264,7 @@ PUSER_CONFIG CreateDefaultUserCfg(void)
usrCfg.UsedDefault = TRUE;
usrCfg.FactoryMode = 0;
usrCfg.DeviceType = 0;
usrCfg.DeviceName = "MC01";
usrCfg.ServerCfg.ProductKey = "46ab16f31749ac85076d50294efe2e03";
usrCfg.ServerCfg.ProductSecury = "eac46ba44c94";
usrCfg.ServerCfg.MQTTServer = "mqtt-dev.netease.im";
@ -305,6 +309,9 @@ int UserCfgSaveToFlash(void)
{
char* pStr;
int err = ERR_OK;
int out, oSize;
char *pBuf, *pCode;
SpiFlashOpResult ret = spi_flash_erase_sector(PARAM_SAVE_USERCFG);
if(ret != SPI_FLASH_RESULT_OK)
@ -321,43 +328,98 @@ int UserCfgSaveToFlash(void)
return -ERR_JSON_STR_FORMAT;
}
ret = spi_flash_write(PARAM_SAVE_USERCFG * SECTOR_SIZE, (unsigned int *)pStr, strlen(pStr));
pBuf = (char*)HAL_Malloc(SECTOR_SIZE);
pCode = (char*)HAL_Malloc(SECTOR_SIZE);
memset(pBuf, 0, SECTOR_SIZE);
memset(pCode, 0, SECTOR_SIZE);
oSize = CfgEncrypto(pBuf, pStr, strlen(pStr));
free(pStr);
mbedtls_base64_encode(pCode, SECTOR_SIZE, &out, pBuf, oSize);
free(pBuf);
ret = spi_flash_write(PARAM_SAVE_USERCFG * SECTOR_SIZE, (unsigned int *)pCode, strlen(pCode));
free(pCode);
if(ret != SPI_FLASH_RESULT_OK)
{
LOG_EX(LOG_Error, "Erase Sector error: %d\n", ret);
return -ERR_WRITE_FLASH;
}
}
free(pStr);
return ERR_OK;
}
static int CfgEncrypto(unsigned char* pDest, unsigned char* pSrc, int iSlen)
{
int isPad, outlen = 0, i = 0;
unsigned char* pRet = pDest;
mbedtls_aes_context aes_ctx;
char pad[16];
memset(pad, 0, 16);
isPad = iSlen % 16;
//密钥数值
const unsigned char key[16] = {'0', '1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
mbedtls_aes_init(&aes_ctx);
//设置密钥
mbedtls_aes_setkey_enc(&aes_ctx, key, 128);
//LOG_EX(LOG_Debug, "Input(%d):\n%s\n", iSlen, pSrc);
// AES ECB 加密
for(i = 0; i < iSlen / 16; i++)
{
mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, (const unsigned char*)pSrc, pDest);
pSrc += 16;
pDest += 16;
outlen += 16;
}
if(isPad)
{
memcpy(pad, pSrc, isPad);
mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, (const unsigned char*)pad, pDest);
outlen += 16;
}
return outlen;
}
static int CfgDecrypto(unsigned char* pDest, unsigned char* pSrc, int iSlen)
{
int i = 0;
unsigned char* pRet = pDest;
mbedtls_aes_context aes_ctx;
//密钥数值
//密钥数值
const unsigned char key[16] = {'0', '1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};
mbedtls_aes_init(&aes_ctx);
//设置解密密钥
//设置密钥
mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
//LOG_EX(LOG_Debug, "Input size: %d\n", iSlen);
//LOG_EX(LOG_Debug, "Input(%d):\n%s\n", iSlen, pSrc);
// AES ECB 解密
// AES ECB 解密
for(i = 0; i < iSlen / 16; i++)
{
mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_DECRYPT, (const unsigned char*)pSrc, pDest);
pSrc += 16;
pDest += 16;
}
// 去除pad字符
//LOG_EX(LOG_Debug, "AES Dec:\n%s\n", pRet);
// 去除pad字符
for(i = 0; i < iSlen; i++)
{
if((isprint(pRet[i]) == 0)
@ -369,6 +431,7 @@ static int CfgDecrypto(unsigned char* pDest, unsigned char* pSrc, int iSlen)
break;
}
}
//LOG_EX(LOG_Debug, "AES Dec Format:\n%s\n", pRet);
return i;
}
@ -393,11 +456,21 @@ int CfgReadUserCfgFromFlash(void)
if(ret != SPI_FLASH_RESULT_OK)
{
//LOG_EX(LOG_Error, "Read Sector error: %d\n", ret);
#if DEBUG_USER_CFG
LOG_EX(LOG_Error, "Read Sector error: %d\n", ret);
#endif
return -ERR_READ_FLASH;
}
slen = strlen(pUserCfg);
if(slen > SECTOR_SIZE)
{
#if DEBUG_USER_CFG
LOG_EX(LOG_Error, "Bad configure File: %d\n", slen);
#endif
return -ERR_BAD_CFG_FILE;
}
pBase64Buf = (unsigned char *)HAL_Malloc(slen);
if(pBase64Buf == NULL)
@ -416,14 +489,17 @@ int CfgReadUserCfgFromFlash(void)
ret = CfgDecrypto(pUserCfg, pBase64Buf, i);
#if DEBUG_USER_CFG
LOG_EX(LOG_Debug, "User Cfg Decode(%d): \n%s\n\n", ret, pUserCfg);
#endif
#endif
//IHW_LOG_BUF(LOG_Debug, "Json", (unsigned char*)pBase64Buf, strlen(pBase64Buf));
pCfgInfo = (PUSER_CONFIG)Json2Struct(pUserCfg, JE_USER_CFG, CRYPTO_NONE, &i);
if(i != ERR_OK || pCfgInfo == NULL)
{
//LOG_EX(LOG_Error, "Decode User Configure Error: %d\n", i);
#if DEBUG_USER_CFG
LOG_EX(LOG_Error, "Decode User Configure Error: %d\n", i);
#endif
if(pCfgInfo)
{

View File

@ -279,7 +279,7 @@ static int32 ne_general_local_process(uint8 *input, uint32 ilen)
GENERAL_LOG_EX(LOG_Info, "start apconfig\r\n");
/* get mcu product id - save to flash - set init flag */
set_platform_product_id(input + 1, 4);
// set_platform_product_id(input + 1, 4);
ne_cfg_set_init_type(NE_DEVICE_APCONFIG);
wifi_station_disconnect();
@ -432,6 +432,42 @@ static int32 ne_general_local_process(uint8 *input, uint32 ilen)
HAL_Free(pBuf);
}
break;
case NE_DEVICE_GENERAL_CMD_GET_DEVICENAME:
{
uint32 slen;
ne_general_rsp_data_t rsp_data;
const char* pId = USR_CFG_DEVICE_NAME();
unsigned char proBuf[36];
unsigned char* pBuf = (unsigned char*)HAL_Malloc(strlen(pId) + NE_GENERAL_MAX_SBUF_LEN);
if(pBuf) {
memset(proBuf, 0, 36);
proBuf[0] = NE_DEVICE_GENERAL_CMD_GET_DEVICENAME;
memcpy(proBuf + 1, pId, strlen(pId));
slen = ne_general_create_frame(NE_DEVICE_GENERAL_WIFI_PROCESS,
proBuf,
strlen(pId) + 1,
pBuf,
NE_GENERAL_MAX_SBUF_LEN);
} else {
rsp_data.msgtype = NE_DEVICE_GENERAL_CMD_GET_DEVICENAME;
rsp_data.result = NE_GENERAL_FAIL;
slen = ne_general_create_frame(NE_DEVICE_GENERAL_WIFI_PROCESS,
(uint8_t *)&rsp_data,
sizeof(rsp_data),
pBuf,
NE_GENERAL_MAX_SBUF_LEN);
}
if(slen < 0){
GENERAL_LOG_EX(LOG_Error, "create frame fail!\r\n");
}
ne_general_write(NULL, pBuf, slen);
HAL_Free(pBuf);
}
break;
default:

View File

@ -218,6 +218,7 @@ typedef struct
int Version;
int FactoryMode;
int DeviceType;
char* DeviceName;
SERVER_CFG ServerCfg;
SOFTAP_CONFIG SoftAP;
WIFIAP_CONFIG WifiAP;
@ -239,6 +240,7 @@ PUSER_CONFIG ne_cfg_get_user_cfg(void);
#define USR_CFG_FACTORY_MODE() (ne_cfg_get_user_cfg()->FactoryMode)
#define USR_CFG_DEVICE_TYPE() (ne_cfg_get_user_cfg()->DeviceType)
#define USR_CFG_DEVICE_NAME() (ne_cfg_get_user_cfg()->DeviceName)
#define USR_CFG_SERVER(key) (ne_cfg_get_user_cfg()->ServerCfg.key)
#define USR_CFG_WIFIAP(key) (ne_cfg_get_user_cfg()->WifiAP.key)
#define USR_CFG_SOFTAP(key) (ne_cfg_get_user_cfg()->SoftAP.key)

View File

@ -62,6 +62,7 @@ enum{
NE_DEVICE_GENERAL_CMD_RESET_WIFI,
NE_DEVICE_GENERAL_CMD_GET_VERSION,
NE_DEVICE_GENERAL_CMD_GET_CHIPID,
NE_DEVICE_GENERAL_CMD_GET_DEVICENAME,
};
#pragma pack (1)

View File

@ -55,6 +55,7 @@ typedef enum
ERR_ERASE_FLASH = 1500,
ERR_WRITE_FLASH,
ERR_READ_FLASH,
ERR_BAD_CFG_FILE,
ERR_GPIO_NOT_SUPPORT = 1600,
ERR_GPIO_NOT_CONFIG,

View File

@ -431,6 +431,8 @@ static const char* __userCfg2Json(void* pData)
cJSON_AddItemToObject(pRoot, "FactoryMode", cJSON_CreateNumber(pCfg->FactoryMode));
cJSON_AddItemToObject(pRoot, "DeviceType", cJSON_CreateNumber(pCfg->DeviceType));
cJSON_AddItemToObject(pRoot, "DeviceName", cJSON_CreateString(pCfg->DeviceName));
cJSON_AddItemToObject(pRoot, "ServerCfg", pSvrCfg);
cJSON_AddItemToObject(pRoot, "WIFIApConfig", pWifiApCfg);
cJSON_AddItemToObject(pRoot, "SoftAP", pSoftApCfg);
@ -498,7 +500,7 @@ static const char* __userCfg2Json(void* pData)
pJsonS = cJSON_Print(pRoot);
cJSON_Delete(pRoot);
return pJsonS;
}
@ -528,7 +530,8 @@ static void* __json2UserCfg(const char* pJsonS)
pCfg->Version = __getIntValue(pRoot, "Version", USER_CFG_VERSION);
pCfg->FactoryMode = __getIntValue(pRoot, "FactoryMode", 0);
pCfg->DeviceType = __getIntValue(pRoot, "DeviceType", 1);
pCfg->DeviceName = __getStringValue(pRoot, "DeviceName", "MC01");
pItem = cJSON_GetObjectItem(pRoot, "ServerCfg");
if(pItem)
@ -565,7 +568,7 @@ static void* __json2UserCfg(const char* pJsonS)
pCfg->SoftAP.softAPDHCP.DHCPEndIP = __getStringValue(pObj, "DHCPEndIP", "192.168.5.105");
}
}
pItem = cJSON_GetObjectItem(pRoot, "UART");
if(pItem)
@ -619,7 +622,7 @@ static void* __json2UserCfg(const char* pJsonS)
pList = pList->next;
}
}
cJSON_Delete(pRoot);
return pCfg;
}

View File

@ -243,9 +243,12 @@ int ProtocolOnMsg(PCLOUND_API pApi)
memset(&devId, 0, sizeof(RSP_GET_DEVID_PRO));
devId.cpuId = (char*)GetPlatformDevId();
devId.macAddr = (char *)GetSTAMacAddr();
devId.productSign = USR_CFG_DEVICE_NAME();
#if 0
if(ne_cfg_get_product_id(product_id, sizeof(product_id)) == ERR_OK){
devId.productSign = product_id;
}
#endif
pSubMsg = (char*)Struct2Json(&devId, JE_GETID, CRYPTO_NONE, &err);
if(err != ERR_OK || pSubMsg == NULL)