tp中Can't use function return value in write context报错问题

本文介绍在ThinkPHP框架中如何正确地使用isset()函数进行登录状态验证,并通过实例演示了如何避免函数返回值直接用于写上下文导致的错误。
public function index(){
    	$res = session('id');
    	if(!isset($res)){
    		$this->error('请先登录!','/admin/user/login');
    	}
    	$admin = M('admin');
    	$data = $admin->where(array('id'=>session('id')))->find();
    	$time = date('Y-m-d H:i:s');
    	$this->assign('time',$time);
    	$this->assign('data',$data);
        $this->display();
    }

在tp 中 使用 empty() isset() 是无法使用别的函数的返回值做参数的.


如果在上边的if语句中判断的是

if(!isset(session('id'))){
    		$this->error('请先登录!','/admin/user/login');
    	}

就会出现  Can't use function return value in write context 的报错.

解决方法就是  定义一个变量接收session()的值,然后再走判断语句.



之前百度搜索  关于此报错,    有说提升php版本,来解决,

但实际测试 此方法并不能解决...

/*! Copyright(c) 2008-2013 Shenzhen TP-LINK Technologies Co.Ltd. * *\file http_poeOut.c *\brief *\details * *\author Hu Haiming *\version *\date 2Sep21 * *\warning * *\history \arg */ /**************************************************************************************************/ /* CONFIGURATIONS */ /**************************************************************************************************/ /**************************************************************************************************/ /* INCLUDE_FILES */ /**************************************************************************************************/ #include <stdio.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/sysinfo.h> #include <net/if.h> #include <netinet/if_ether.h> #include <netinet/in.h> #include <arpa/inet.h> #include <fcntl.h> #include "http_file.h" #include "http_io.h" #include "http_data.h" #include "http_auth.h" #include "cJSON.h" #include "wrp_com_op.h" #include "dev_config.h" #include "http_poeOut.h" /**************************************************************************************************/ /* DEFINES */ /**************************************************************************************************/ /**************************************************************************************************/ /* TYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* EXTERN_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* VARIABLES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_FUNCTIONS */ /**************************************************************************************************/ static void _poeOutJsonWrite(HTTP_DATATEMP *pData, LAN_PORT_ENTRY *poeOutCfgData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; if (NULL == poeOutCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateObject(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); if (poeOutCfgData->poe_out_bitmap) { cJSON_AddStringToObject(poeOutInfo, "enable", "on"); } else { cJSON_AddStringToObject(poeOutInfo, "enable", "off"); } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS getPoeOutJson(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; LAN_PORT_ENTRY info; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); wrpRet = wrpOpDo(UCL_OPID_LAN_PORT_GET_INFO, NULL, 0, &info, sizeof(LAN_PORT_ENTRY), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _poeOutJsonWrite(pData, &info, status, OK); return RPM_DONE; } static STATUS _parsePoeCfgSet(HTTP_DATATEMP *pData, POE_GLOBAL_CFG_STRUCT_UC *poeOutCfgData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == poeOutCfgData) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { // printf("failed to get param str:%s", "data"); printf("####&&&& in [%s] failed to get param str:%s\n", __FUNCTION__, "data"); return ERROR; } printf("_parsePoeCfgSet data str:%s \n ", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*limit*/ pValue = cJSON_GetObjectItem(pRoot, "limit"); if (NULL == pValue || (pValue->type != cJSON_String) || 0 == strlen(pValue->valuestring)) { printf("module(%s) not present,use default \n", "0"); poeOutCfgData->sys_power_limit = 0; } else { poeOutCfgData->sys_power_limit = atof(pValue->valuestring) * 10; } return OK; } //ok static void _poeCfgJsonWrite(HTTP_DATATEMP *pData, POE_GLOBAL_CFG_STRUCT_UC *poeOutCfgData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; cJSON *poeOutInfo_Array = NULL; char *out = NULL; if (NULL == poeOutCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateObject(); poeOutInfo_Array = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo_Array); cJSON_AddNumberToObject(poeOutInfo, "id", 0); //UCL_OPID_MONITORAP_GET_DEVINFO cJSON_AddStringToObject(poeOutInfo, "device_name", "EAP650GP-Desktop"); //UCL_OPID_MONITORAP_GET_DEVINFO cJSON_AddNumberToObject(poeOutInfo, "min", 4); cJSON_AddNumberToObject(poeOutInfo, "max", (float)poeOutCfgData->sys_power_max/10.0); cJSON_AddNumberToObject(poeOutInfo, "limit", (float)poeOutCfgData->sys_power_limit/10.0); cJSON_AddNumberToObject(poeOutInfo, "consumption", (float)poeOutCfgData->sys_actual_cons/10.0); if(poeOutCfgData->sys_power_limit < poeOutCfgData->sys_actual_cons) { cJSON_AddNumberToObject(poeOutInfo, "remain", 0); } else { cJSON_AddNumberToObject(poeOutInfo, "remain", (float)(poeOutCfgData->sys_power_limit - poeOutCfgData->sys_actual_cons)/10.0); } cJSON_AddBoolToObject(poeOutInfo, "total_overload", poeOutCfgData->sys_overload == 1); cJSON_AddItemToArray(poeOutInfo_Array, poeOutInfo); out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS poe_cfg_get(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_GLOBAL_CFG_STRUCT_UC info_new; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_GET_ENABLE, NULL, 0, &info_new, sizeof(POE_GLOBAL_CFG_STRUCT_UC), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _poeCfgJsonWrite(pData, &info_new, status, OK); //ok return RPM_DONE; } HTTP_STATUS poe_cfg_set(HTTP_CONTEXT *pContext) { // STATUS status = OK; // WRP_RET wrpRet = WRP_OK; // LAN_PORT_ENTRY info; POE_GLOBAL_CFG_STRUCT_UC info_new; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, poe_cfg_get); //ok _parsePoeCfgSet(pData, &info_new); //half ok. only set limit now.need prd check wrpOpDo(UCL_OPID_POE_SET_ENABLE, &info_new, sizeof(POE_GLOBAL_CFG_STRUCT_UC), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); return poe_cfg_get(pContext); } //ok static STATUS _parsePortCfgSet(HTTP_DATATEMP *pData, POE_PORT_CFG_STRUCT_UC *portCfgData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == portCfgData) { return ERROR; } pStr = httpGetEnv(pData, "id"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("_parsePortCfgSet data str:%s \n", pStr); portCfgData->port_no = atoi(pStr); pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("_parsePortCfgSet data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*poe_profile_no*/ pValue = cJSON_GetObjectItem(pRoot, "poe_profile"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); } else if(pValue->valuestring[0] == &#39;\0&#39; || (0 == strcmp(pValue->valuestring, "default-none"))) { portCfgData->poe_profile_no = 0; } else { int index = 0; for(index = 0; index < MAX_POE_PROFILE_NUM; index++) { if( profileData[index].profile_id == atoi(pValue->valuestring)) { break; } if( index == MAX_POE_PROFILE_NUM -1) { printf("match profile failed \n"); return RPM_ERROR; } } portCfgData->poe_profile_no = atoi(pValue->valuestring); portCfgData->poe_enable = profileData[index].poe_enable; portCfgData->priority = profileData[index].priority; portCfgData->power_limit = profileData[index].power_limit; portCfgData->power_limit_class = profileData[index].power_limit_class; return OK; } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); portCfgData->poe_enable = POE_DISABLE; } else { if (0 == strcmp(pValue->valuestring, "on")) { portCfgData->poe_enable = POE_ENABLE; } else if(0 == strcmp(pValue->valuestring, "off")) { portCfgData->poe_enable = POE_DISABLE; } } /*priority*/ pValue = cJSON_GetObjectItem(pRoot, "priority"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "priority"); portCfgData->priority = POE_PRI_LOW_AD; } else { if (0 == strcmp(pValue->valuestring, "low")) { portCfgData->priority = POE_PRI_LOW_AD; } else if (0 == strcmp(pValue->valuestring, "middle")) { portCfgData->priority = POE_PRI_MIDDLE_AD; } else if (0 == strcmp(pValue->valuestring, "high")) { portCfgData->priority = POE_PRI_HIGH_AD; } else { printf("module(%s)2 not present ,use default", "priority"); portCfgData->priority = POE_PRI_LOW_AD; } } /*power_limit*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit"); portCfgData->power_limit = 0; } else { if (0 == strcmp(pValue->valuestring, "auto")) { portCfgData->power_limit = POE_CLASS_CLASS3_AD; portCfgData->power_limit_class = POE_CLASS_AUTO_AD; } else if(0 == strcmp(pValue->valuestring, "class1")) { portCfgData->power_limit = POE_CLASS_CLASS1_AD; portCfgData->power_limit_class = POE_CLASS1_AD; } else if(0 == strcmp(pValue->valuestring, "class2")) { portCfgData->power_limit = POE_CLASS_CLASS2_AD; portCfgData->power_limit_class = POE_CLASS2_AD; } else if(0 == strcmp(pValue->valuestring, "class3")) { portCfgData->power_limit = POE_CLASS_CLASS3_AD; portCfgData->power_limit_class = POE_CLASS3_AD; } // we cant set class4? // else if(0 == strcmp(pValue->valuestring, "class4")) // { // portCfgData->power_limit = POE_CLASS_CLASS4_AD; // } else if(0 == strcmp(pValue->valuestring, "manual")) { /*power_limit_value*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit_value"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit_value"); portCfgData->power_limit = POE_CLASS_CLASS3_AD; portCfgData->power_limit_class = POE_CLASS_MANUAL_AD; } else { portCfgData->power_limit = (int)(atof(pValue->valuestring)*10); portCfgData->power_limit_class = POE_CLASS_MANUAL_AD; } } } return OK; } static int _composePortcfg(const POE_PORT_CFG_STRUCT_UC *portCfgData,cJSON *pEntry,int idx) { /*index*/ cJSON_AddNumberToObject(pEntry, "id", portCfgData->port_no); /* port */ cJSON_AddNumberToObject(pEntry, "port", portCfgData->port_no); /* poe_profile */ if(portCfgData->poe_profile_no == 0) { cJSON_AddStringToObject(pEntry, "poe_profile", "default-none"); //todo } else { cJSON_AddNumberToObject(pEntry, "poe_profile", portCfgData->poe_profile_no); //todo } /*power_limit*/ cJSON_AddNumberToObject(pEntry, "power_limit_value", (float)(portCfgData->power_limit)/10.0); //@wxh 待修复 power limit 不能正常显示的问题, portCfgData->power_limit_class // if(portCfgData->power_limit == POE_CLASS_CLASS1_AD) // cJSON_AddStringToObject(pEntry, "power_limit", "class1"); // else if(portCfgData->power_limit == POE_CLASS_CLASS2_AD) // cJSON_AddStringToObject(pEntry, "power_limit", "class2"); // else if(portCfgData->power_limit == POE_CLASS_CLASS3_AD) // cJSON_AddStringToObject(pEntry, "power_limit", "class3"); // else if(portCfgData->power_limit == POE_CLASS_CLASS4_AD) // cJSON_AddStringToObject(pEntry, "power_limit", "class4"); // else // cJSON_AddStringToObject(pEntry, "power_limit", "manual"); //应该用 if(portCfgData->power_limit_class == POE_CLASS0_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class0"); else if(portCfgData->power_limit_class == POE_CLASS1_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class1"); else if(portCfgData->power_limit_class == POE_CLASS2_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class2"); else if(portCfgData->power_limit_class == POE_CLASS3_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class3"); else if(portCfgData->power_limit_class == POE_CLASS4_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class4"); // else if(portCfgData->power_limit_class == POE_CLASS_AUTO_AD) // cJSON_AddStringToObject(pEntry, "power_limit", "class3"); else if(portCfgData->power_limit_class == POE_CLASS_AUTO_AD) cJSON_AddStringToObject(pEntry, "power_limit", "auto"); else if(portCfgData->power_limit_class == POE_CLASS_MANUAL_AD) cJSON_AddStringToObject(pEntry, "power_limit", "manual"); else return ERROR; // /*poe_enable*/ /*status*/ if(portCfgData->poe_enable == POE_ENABLE) cJSON_AddStringToObject(pEntry, "status", "on"); else cJSON_AddStringToObject(pEntry, "status", "off"); /*priority*/ if(portCfgData->priority == POE_PRI_LOW_AD) cJSON_AddStringToObject(pEntry, "priority", "low"); else if(portCfgData->priority == POE_PRI_MIDDLE_AD) cJSON_AddStringToObject(pEntry, "priority", "middle"); else if(portCfgData->priority == POE_PRI_HIGH_AD) cJSON_AddStringToObject(pEntry, "priority", "high"); else cJSON_AddStringToObject(pEntry, "priority", "error"); //dbg //not support cJSON_AddStringToObject(pEntry, "time_range", "any"); cJSON_AddNumberToObject(pEntry, "current", (float)(portCfgData->actual_cur)); cJSON_AddNumberToObject(pEntry, "voltage", (float)(portCfgData->actual_vol)/10.0); cJSON_AddNumberToObject(pEntry, "power", (float)(portCfgData->actual_cons)/10.0); if(portCfgData->pd_class == POE_CLASS_CLASS1_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 1); else if(portCfgData->pd_class == POE_CLASS_CLASS2_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 2); else if(portCfgData->pd_class == POE_CLASS_CLASS3_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 3); else if(portCfgData->pd_class == POE_CLASS_CLASS4_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 4); else cJSON_AddNumberToObject(pEntry, "pd_class", 0); if(portCfgData->actual_cons > 0) { cJSON_AddStringToObject(pEntry, "power_status", "on"); } else { cJSON_AddStringToObject(pEntry, "power_status", "off"); } cJSON_AddBoolToObject(pEntry, "port_overload", portCfgData->port_overload == 1); return OK; } static void _portCfgJsonWrite(HTTP_DATATEMP *pData, POE_PORT_CFG_STRUCT_UC *portCfgData, POE_PROFILE_STRUCT_UC *profileData,int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; cJSON *othersInfo = NULL; unsigned int l_poeBitMap = poeBitMap; char *out = NULL; int idx = 0; if (NULL == portCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } othersInfo = cJSON_CreateObject(); if (NULL == othersInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); cJSON_AddItemToObject(root, "others", othersInfo); // for (idx = 0; portCfgData != NULL && idx < MAX_POE_PORT_NUM; idx ++) /* @wxh 解耦端口数量 */ // for (idx = 0; portCfgData != NULL && idx < configuredPortNum; idx ++) // { // cJSON *entry = cJSON_CreateObject(); // status = _composePortcfg(portCfgData,entry,idx); // if(status != OK) // { // return; // } // cJSON_AddItemToArray(poeOutInfo, entry); // portCfgData++; // } /* @wxh 解耦端口数量 */ while (l_poeBitMap) // poeBitMap 二进制中1所在位置的端口设置为开启(从右往左看,最低位的1表示端口1开启,依次类推) { idx++; if (l_poeBitMap & 1) { cJSON *entry = cJSON_CreateObject(); status = _composePortcfg(portCfgData,entry,idx); if(status != OK) { return; } cJSON_AddItemToArray(poeOutInfo, entry); } l_poeBitMap >>= 1; portCfgData++; } cJSON *profileOptions = cJSON_CreateArray(); for (idx = 0; profileData != NULL && profileData->profileName[0] != &#39;\0&#39; && idx < MAX_POE_PROFILE_NUM; idx ++) { cJSON *entry = cJSON_CreateObject(); cJSON_AddNumberToObject(entry, "profile_id", profileData->profile_id); cJSON_AddStringToObject(entry, "profile_name", (void*)profileData->profileName); cJSON_AddItemToArray(profileOptions, entry); profileData++; } cJSON_AddItemToObject(othersInfo, "profile_option", profileOptions); out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS port_cfg_get(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PORT_CFG_STRUCT_UC info_new[MAX_POE_PORT_NUM]; POE_PROFILE_STRUCT_UC profile_info[MAX_POE_PROFILE_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_GET_PORT_CONFIG, NULL, 0, info_new, sizeof(POE_PORT_CFG_STRUCT_UC) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } memset(profile_info, 0, sizeof(profile_info)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, profile_info, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _portCfgJsonWrite(pData, info_new, profile_info, status, OK); //ok return RPM_DONE; } HTTP_STATUS port_cfg_set(HTTP_CONTEXT *pContext) { WRP_RET wrpRet = WRP_OK; POE_PORT_CFG_STRUCT_UC info_new; POE_PROFILE_STRUCT_UC info_profile[MAX_POE_PROFILE_NUM]; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, port_cfg_get); //todo memset(info_profile, 0, sizeof(info_profile)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, info_profile, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { printf("UCL_OPID_POE_PROFILE_GET ERROR\n"); return RPM_ERROR; } _parsePortCfgSet(pData, &info_new, info_profile); //ok wrpRet = wrpOpDo(UCL_OPID_POE_SET_PORT_CONFIG, &info_new, sizeof(POE_PORT_CFG_STRUCT_UC), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) { printf("UCL_OPID_POE_SET_PORT_CONFIG ERROR\n"); return RPM_ERROR; } return port_cfg_get(pContext); } //ok static void _recoveryGlobalJsonWrite(HTTP_DATATEMP *pData, POE_RECOVERY_STATUS_UC *poeOutCfgData, int status, int err) { printf("####&&&& goto [%s]\n",__FUNCTION__); cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; if (NULL == poeOutCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateObject(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); /* typedef enum { POE_RECOVERY_DISABLE_UC = 0, POE_RECOVERY_ENABLE_UC }POE_RECOVERY_STATUS_UC; */ if( *poeOutCfgData == POE_RECOVERY_ENABLE_UC) { cJSON_AddStringToObject(poeOutInfo, "global", "on"); printf("####&&&& in [%s] cJSON_AddStringToObject(poeOutInfo, global, on); \n",__FUNCTION__); } else if( *poeOutCfgData == POE_RECOVERY_DISABLE_UC) { cJSON_AddStringToObject(poeOutInfo, "global", "off"); printf("####&&&& in [%s] cJSON_AddStringToObject(poeOutInfo, global, off); \n",__FUNCTION__); } else { printf("####&&&& http_poeOut (%s) read wrong param,use default\n", "_recoveryGlobalJsonWrite"); cJSON_AddStringToObject(poeOutInfo, "global", "off"); } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS recovery_global_get(HTTP_CONTEXT *pContext) { printf("####&&&& goto [%s] &&&&####\n",__FUNCTION__); STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_RECOVERY_STATUS_UC info; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_ENABLE, NULL, 0, &info, sizeof(POE_RECOVERY_STATUS_UC), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _recoveryGlobalJsonWrite(pData, &info, status, OK); //ok return RPM_DONE; } static STATUS _recoveryGlobalCfgSet(HTTP_DATATEMP *pData, POE_RECOVERY_STATUS_UC *poeOutCfgData) { printf("####&&&& goto [%s]\n",__FUNCTION__); char *pStr = NULL; // cJSON *pRoot = NULL; // cJSON *pValue = NULL; if (NULL == poeOutCfgData) { return ERROR; } // pStr = httpGetEnv(pData, "data"); // if (NULL == pStr) // { // // printf("failed to get param str:%s", "data"); // printf("####&&&& in [%s] failed to get param str:%s\n", __FUNCTION__, "data"); // return ERROR; // } // printf("_recoveryGlobalCfgSet data str:%s", pStr); // pRoot = cJSON_Parse(pStr); // if (NULL == pRoot) // { // printf("failed to parse json str:%s\n", pStr); // return ERROR; // } // /*global*/ // pValue = cJSON_GetObjectItem(pRoot, "global"); // if (NULL == pValue || (pValue->type != cJSON_String)) // { // printf("module(%s) not present,use default \n", "global"); // printf("####&&&& module(%s) not present,use default \n", "global"); // *poeOutCfgData = POE_RECOVERY_DISABLE_UC; // } // else // { // if (0 == strcmp(pValue->valuestring, "on")) // { // printf("####&&&& in [%s] pData: pValue->valuestring : on\n",__FUNCTION__); // *poeOutCfgData = POE_RECOVERY_ENABLE_UC; // } // else if(0 == strcmp(pValue->valuestring, "off")) // { // printf("####&&&& in [%s] pData: pValue->valuestring : off\n",__FUNCTION__); // *poeOutCfgData = POE_RECOVERY_DISABLE_UC; // } // else // { // printf("####&&&& module(%s)2 not present,use default", "global"); // printf("module(%s)2 not present,use default", "global"); // *poeOutCfgData = POE_RECOVERY_DISABLE_UC; // } // } /*global*/ pStr = httpGetEnv(pData, "global"); if (NULL == pStr) { printf("####&&&& in [%s] failed to get param str: %s\n", __FUNCTION__, "global"); return ERROR; } printf("####&&&& in [%s] global str:%s\n", __FUNCTION__, pStr); if (NULL == pStr) { printf("####&&&& module(%s) not present,use default \n", "global"); *poeOutCfgData = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pStr, "on")) { printf("####&&&& in [%s] pData: pValue->valuestring : on\n",__FUNCTION__); *poeOutCfgData = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pStr, "off")) { printf("####&&&& in [%s] pData: pValue->valuestring : off\n",__FUNCTION__); *poeOutCfgData = POE_RECOVERY_DISABLE_UC; } else { printf("####&&&& module(%s)2 not present,use default", "global"); printf("module(%s)2 not present,use default", "global"); *poeOutCfgData = POE_RECOVERY_DISABLE_UC; } } return OK; } HTTP_STATUS recovery_global_set(HTTP_CONTEXT *pContext) { printf("####&&&& goto [%s] &&&&####\n",__FUNCTION__); STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_RECOVERY_STATUS_UC info; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; printf("####&&&& in [%s]\n",__FUNCTION__); // RPM_POST_2_OTHER_OP(pContext, "load", NULL, recovery_global_get); //ok RPM_POST_2_OTHER_OP(pContext, "read", NULL, recovery_global_get); //ok printf("####&&&& in [%s] have done functionRPM_POST_2_OTHER_OP \n",__FUNCTION__); _recoveryGlobalCfgSet(pData, &info); //ok wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_SET_ENABLE, &info, sizeof(POE_RECOVERY_STATUS_UC), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) { status = ERROR; } else { status = OK; } wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_ENABLE, NULL, 0, &info, sizeof(POE_RECOVERY_STATUS_UC), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _recoveryGlobalJsonWrite(pData, &info, status, err); //ok return RPM_DONE; } static int _composeRecoveryPortcfg(const POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData,cJSON *pEntry, int idx) { // /* index */ // cJSON_AddNumberToObject(pEntry, "id", idx); // /*port_no*/ // cJSON_AddNumberToObject(pEntry, "port", recoveryPortCfgData->port_no); // /*status */ // if(POE_RECOVERY_ENABLE_UC == recoveryPortCfgData->status) // cJSON_AddStringToObject(pEntry, "status", "on"); // else // cJSON_AddStringToObject(pEntry, "status", "off"); // /*ip*/ // cJSON_AddStringToObject(pEntry, "ip", recoveryPortCfgData->ip); // /*startup*/ // cJSON_AddNumberToObject(pEntry, "startup", recoveryPortCfgData->startup); // /*interval*/ // cJSON_AddNumberToObject(pEntry, "interval", recoveryPortCfgData->interval); // /*failure_threshold*/ // cJSON_AddNumberToObject(pEntry, "failure_threshold", recoveryPortCfgData->retry); // /*break_time*/ // cJSON_AddNumberToObject(pEntry, "break_time", recoveryPortCfgData->reboot); // /*failures*/ // cJSON_AddNumberToObject(pEntry, "failures", recoveryPortCfgData->failure); // /*reboots*/ // cJSON_AddNumberToObject(pEntry, "reboots", recoveryPortCfgData->restart); // /*total*/ // cJSON_AddNumberToObject(pEntry, "total", recoveryPortCfgData->total); /* 转换成string 类型的响应*/ char buf_port[16], buf_startup[16], buf_interval[16], buf_retry[16]; char buf_reboot[16], buf_failure[16], buf_restart[16], buf_total[16]; /* index */ cJSON_AddNumberToObject(pEntry, "id", idx); /*port_no*/ snprintf(buf_port, sizeof(buf_port), "%d", recoveryPortCfgData->port_no); cJSON_AddStringToObject(pEntry, "port", buf_port); /*status */ if(POE_RECOVERY_ENABLE_UC == recoveryPortCfgData->status) cJSON_AddStringToObject(pEntry, "status", "on"); else cJSON_AddStringToObject(pEntry, "status", "off"); /*ip*/ cJSON_AddStringToObject(pEntry, "ip", recoveryPortCfgData->ip); /*startup*/ snprintf(buf_startup, sizeof(buf_startup), "%d", recoveryPortCfgData->startup); cJSON_AddStringToObject(pEntry, "startup", buf_startup); /*interval*/ snprintf(buf_interval, sizeof(buf_interval), "%d", recoveryPortCfgData->interval); cJSON_AddStringToObject(pEntry, "interval", buf_interval); /*failure_threshold*/ snprintf(buf_retry, sizeof(buf_retry), "%d", recoveryPortCfgData->retry); cJSON_AddStringToObject(pEntry, "failure_threshold", buf_retry); /*break_time*/ snprintf(buf_reboot, sizeof(buf_reboot), "%d", recoveryPortCfgData->reboot); cJSON_AddStringToObject(pEntry, "break_time", buf_reboot); /*failures*/ snprintf(buf_failure, sizeof(buf_failure), "%d", recoveryPortCfgData->failure); cJSON_AddStringToObject(pEntry, "failures", buf_failure); /*reboots*/ snprintf(buf_restart, sizeof(buf_restart), "%d", recoveryPortCfgData->restart); cJSON_AddStringToObject(pEntry, "reboots", buf_restart); /*total*/ snprintf(buf_total, sizeof(buf_total), "%d", recoveryPortCfgData->total); cJSON_AddStringToObject(pEntry, "total", buf_total); return OK; } static void _recoveryPortCfgJsonWrite(HTTP_DATATEMP *pData, POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; int idx = 0; unsigned int l_poeBitMap = poeBitMap; if (NULL == recoveryPortCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } /* @wxh 解耦端口数量 */ if(0 == l_poeBitMap) { printf("####&&&& [%s] error: l_poeBitMap = 0\n",__FUNCTION__); } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); // for (idx = 0; recoveryPortCfgData != NULL && idx < MAX_POE_PORT_NUM; idx ++) // idx: port 1 对应 idx 0, port 2 对应idx 1 // for (idx = 0; recoveryPortCfgData != NULL && idx < configuredPortNum; idx ++) // { // if(0 == recoveryPortCfgData->port_no || 0 == recoveryPortCfgData->startup || 0 == recoveryPortCfgData->interval) // 跳过端口号等于 0 的无数据的端口 // { // recoveryPortCfgData++; // continue; // } // cJSON *entry = cJSON_CreateObject(); // status = _composeRecoveryPortcfg(recoveryPortCfgData,entry,idx); //ok // if(status != OK) // { // return; // } // cJSON_AddItemToArray(poeOutInfo, entry); // recoveryPortCfgData++; // } /* @wxh 解耦端口数量 */ while (l_poeBitMap) // poeBitMap 二进制中1所在位置的端口设置为开启(从右往左看,最低位的1表示端口1开启,依次类推) { idx++; if (l_poeBitMap & 1) { if(0 == recoveryPortCfgData->port_no || 0 == recoveryPortCfgData->startup || 0 == recoveryPortCfgData->interval) // 跳过端口号等于 0 的无数据的端口 { // printf("[%s] 0 == recoveryPortCfgData->port_no || 0 == recoveryPortCfgData->startup || 0 == recoveryPortCfgData->interval \n",__FUNCTION__); } else { cJSON *entry = cJSON_CreateObject(); status = _composeRecoveryPortcfg(recoveryPortCfgData,entry,idx); //ok if(status != OK) { return; } cJSON_AddItemToArray(poeOutInfo, entry); } } l_poeBitMap >>= 1; recoveryPortCfgData++; } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS recovery_port_cfg_get(HTTP_CONTEXT *pContext) { // printf("####&&&& goto [%s] &&&&####\n",__FUNCTION__); STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PORTRECOVERYCFG_STRUCT info[MAX_POE_PORT_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, NULL, 0, info, sizeof(POE_PORTRECOVERYCFG_STRUCT) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _recoveryPortCfgJsonWrite(pData, info, status, OK); //ok /* @wxh 解耦端口数量 */ // POE_PORTRECOVERYCFG_STRUCT *info_used = malloc(configuredPortNum * sizeof(POE_PORTRECOVERYCFG_STRUCT)); // memcpy(info_used, info, configuredPortNum * sizeof(POE_PORTRECOVERYCFG_STRUCT)); // if (!info_used) // { // // 错误处理 // printf("Memory allocation failed for configuredPortNum = %d\n", configuredPortNum); // return RPM_ERROR; // } // else // { // printf(" _recoveryPortCfgJsonWrite(pData, info_used, status, OK); \n"); // _recoveryPortCfgJsonWrite(pData, info_used, status, OK); // } // free(info_used); // 释放避免泄漏 return RPM_DONE; } static STATUS _parseRecoveryPortCfgAdd(HTTP_DATATEMP *pData, POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == recoveryPortCfgData) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s\n", "new"); return ERROR; } printf("_parseRecoveryPortCfgAdd data str:%s\n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s\n", pStr); return ERROR; } /*port*/ pValue = cJSON_GetObjectItem(pRoot, "port"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR \n", "port"); return ERROR; } else { recoveryPortCfgData->port_no = atoi(pValue->valuestring); } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); recoveryPortCfgData->status = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { recoveryPortCfgData->status = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { recoveryPortCfgData->status = POE_RECOVERY_DISABLE_UC; } } /*ip*/ pValue = cJSON_GetObjectItem(pRoot, "ip"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR \n", "ip"); return ERROR; } else { snprintf(recoveryPortCfgData->ip,IP_STR_LEN,pValue->valuestring); } /*startup*/ pValue = cJSON_GetObjectItem(pRoot, "startup"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "startup"); return ERROR; } else { recoveryPortCfgData->startup = atoi(pValue->valuestring); } /*interval*/ pValue = cJSON_GetObjectItem(pRoot, "interval"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "interval"); return ERROR; } else { recoveryPortCfgData->interval = atoi(pValue->valuestring); } /*failure_threshold*/ pValue = cJSON_GetObjectItem(pRoot, "failure_threshold"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "failure_threshold"); return ERROR; } else { recoveryPortCfgData->retry = atoi(pValue->valuestring); } /*break_time*/ pValue = cJSON_GetObjectItem(pRoot, "break_time"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "break_time"); return ERROR; } else { recoveryPortCfgData->reboot = atoi(pValue->valuestring); } recoveryPortCfgData->failure = 0; recoveryPortCfgData->restart = 0; recoveryPortCfgData->total = 0; return OK; } static STATUS _parseRecoveryPortCfgSet(HTTP_DATATEMP *pData, POE_PORTRECOVERYCFG_STRUCT *new_recoveryPortCfgData, POE_PORTRECOVERYCFG_STRUCT *old_recoveryPortCfgData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; char *pStr2 = NULL; cJSON *pRoot2 = NULL; cJSON *pValue2 = NULL; if (NULL == new_recoveryPortCfgData) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s\n", "new"); return ERROR; } printf("_parseRecoveryPortCfgSet data str:%s\n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s\n", pStr); return ERROR; } /*port*/ pValue = cJSON_GetObjectItem(pRoot, "port"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR \n", "port"); return ERROR; } else { new_recoveryPortCfgData->port_no = atoi(pValue->valuestring); } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); new_recoveryPortCfgData->status = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { new_recoveryPortCfgData->status = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { new_recoveryPortCfgData->status = POE_RECOVERY_DISABLE_UC; } } /*ip*/ pValue = cJSON_GetObjectItem(pRoot, "ip"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR \n", "ip"); return ERROR; } else { snprintf(new_recoveryPortCfgData->ip,IP_STR_LEN,pValue->valuestring); } /*startup*/ pValue = cJSON_GetObjectItem(pRoot, "startup"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "startup"); return ERROR; } else { new_recoveryPortCfgData->startup = atoi(pValue->valuestring); } /*interval*/ pValue = cJSON_GetObjectItem(pRoot, "interval"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "interval"); return ERROR; } else { new_recoveryPortCfgData->interval = atoi(pValue->valuestring); } /*failure_threshold*/ pValue = cJSON_GetObjectItem(pRoot, "failure_threshold"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "failure_threshold"); return ERROR; } else { new_recoveryPortCfgData->retry = atoi(pValue->valuestring); } /*break_time*/ pValue = cJSON_GetObjectItem(pRoot, "break_time"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "break_time"); return ERROR; } else { new_recoveryPortCfgData->reboot = atoi(pValue->valuestring); } new_recoveryPortCfgData->failure = 0; new_recoveryPortCfgData->restart = 0; new_recoveryPortCfgData->total = 0; /* get old port Data */ printf("[%s] execute get old port Data\n", __FUNCTION__); if (NULL == old_recoveryPortCfgData) { return ERROR; } pStr2 = httpGetEnv(pData, "old"); if (NULL == pStr2) { printf("failed to get param str:%s\n", "old"); return ERROR; } printf("_parseRecoveryPortCfgSet data str:%s\n", pStr2); pRoot2 = cJSON_Parse(pStr2); if (NULL == pRoot2) { printf("failed to parse json str:%s\n", pStr2); return ERROR; } else { printf("sucessed to parse json str:%s\n", pStr2); } /*port*/ pValue2 = cJSON_GetObjectItem(pRoot2, "port"); if (NULL == pValue2 || (pValue2->type != cJSON_String)) { printf("input(%s) is ERROR \n", "port"); return ERROR; } else { // old_recoveryPortCfgData->port_no = atoi(pValue->valuestring); old_recoveryPortCfgData->port_no = atoi(pValue2->valuestring); printf("sucessed to cJSON_GetObjectItem:%s\n", "port"); printf("[%s] old_recoveryPortCfgData->port_no = %d\n", __FUNCTION__, old_recoveryPortCfgData->port_no); } return OK; } static STATUS _parseRecoveryPortCfgDel(HTTP_DATATEMP *pData, int *index) { // char *pStr = NULL; char *port = NULL; // cJSON *pRoot = NULL; // cJSON *pValue = NULL; if (NULL == index) { return ERROR; } /*index*/ // pIndex = httpGetEnv(pData, "index"); // pIndex = httpGetEnv(pData, "key"); port = httpGetEnv(pData, "port"); if (NULL == port) { printf("####&&&& in [%s] failed to get NULL param str: %s\n", __FUNCTION__, "port"); return ERROR; } *index = atoi(port); if (*index < 1 || *index > MAX_POE_PORT_NUM) { printf("####&&&& in [%s] the param str: %s\n is out of range", __FUNCTION__, "port"); return ERROR; } printf("####&&&& in [%s] *port = %d\n", __FUNCTION__, *index); return OK; } HTTP_STATUS recovery_port_cfg_set(HTTP_CONTEXT *pContext) { // printf("####&&&& goto [%s] &&&&####\n",__FUNCTION__); STATUS status = OK; WRP_RET wrpRet = WRP_OK; WRP_RET wrpRet1 = WRP_OK; WRP_RET wrpRet2 = WRP_OK; POE_PORTRECOVERYCFG_STRUCT info; POE_PORTRECOVERYCFG_STRUCT old_info; int info_delIndex = 0; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, recovery_port_cfg_get); char* pStr = httpGetEnv(pContext->pHttp_datatemp,"operation"); // if( 0 == strcmp(pStr, "add")) if( 0 == strcmp(pStr, "insert")) { _parseRecoveryPortCfgAdd(pData, &info); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_ADD_PORT_CONFIG, &info, sizeof(POE_PORTRECOVERYCFG_STRUCT), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } // else if( 0 == strcmp(pStr, "set")) else if (0 == strcmp(pStr, "update")) { _parseRecoveryPortCfgSet(pData, &info, &old_info); if (info.port_no != old_info.port_no) // port changed { printf("移除 old_info.port_no, 新增 info->port_no\n"); info_delIndex = old_info.port_no; printf("[%s] update: execute UCL_OPID_POE_RECOVRY_DEL_PORT_CONFIG wrpRet\n", __FUNCTION__); wrpRet1 = wrpOpDo(UCL_OPID_POE_RECOVRY_DEL_PORT_CONFIG, (void *)&info_delIndex, sizeof(int), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); printf("[%s] update: UCL_OPID_POE_RECOVRY_DEL_PORT_CONFIG wrpRet1 = %d\n", __FUNCTION__, wrpRet1); printf("[%s] update: execute UCL_OPID_POE_RECOVRY_ADD_PORT_CONFIG wrpRet\n", __FUNCTION__); wrpRet2 = wrpOpDo(UCL_OPID_POE_RECOVRY_ADD_PORT_CONFIG, &info, sizeof(POE_PORTRECOVERYCFG_STRUCT), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); printf("[%s] update: UCL_OPID_POE_RECOVRY_ADD_PORT_CONFIG wrpRet2 = %d\n", __FUNCTION__, wrpRet2); } else // ohter param changed { wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_SET_PORT_CONFIG, &info, sizeof(POE_PORTRECOVERYCFG_STRUCT), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); } if (WRP_OK != wrpRet) status = ERROR; else status = OK; } // else if( 0 == strcmp(pStr, "delete")) else if( 0 == strcmp(pStr, "remove")) { if(OK == _parseRecoveryPortCfgDel(pData, (void*)&info_delIndex)) { wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_DEL_PORT_CONFIG, (void*)&info_delIndex, sizeof(int), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); } else { printf("####&&&& in [%s] return error.\n", __FUNCTION__); } if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else { printf("recovery_port_cfg_set operation (%s) is ERROR", pStr); return ERROR; } wrpRet = status; return recovery_port_cfg_get(pContext); } static int _composeProfilecfg(const POE_PROFILE_STRUCT_UC *profileData,cJSON *pEntry,int idx) { /* index */ cJSON_AddNumberToObject(pEntry, "id", profileData->profile_id); /*profile_name*/ cJSON_AddStringToObject(pEntry, "profile_name", (void*)profileData->profileName); /*poe_enable*/ /*status*/ if(profileData->poe_enable == POE_ENABLE) cJSON_AddStringToObject(pEntry, "status", "on"); else cJSON_AddStringToObject(pEntry, "status", "off"); /*priority*/ if(profileData->priority == POE_PRI_LOW_AD) cJSON_AddStringToObject(pEntry, "priority", "low"); else if(profileData->priority == POE_PRI_MIDDLE_AD) cJSON_AddStringToObject(pEntry, "priority", "middle"); else if(profileData->priority == POE_PRI_HIGH_AD) cJSON_AddStringToObject(pEntry, "priority", "high"); else cJSON_AddStringToObject(pEntry, "priority", "error"); /*power_limit*/ cJSON_AddNumberToObject(pEntry, "power_limit_value", (float)profileData->power_limit/10.0); if(profileData->power_limit_class == POE_CLASS0_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class0"); else if(profileData->power_limit_class == POE_CLASS1_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class1"); else if(profileData->power_limit_class == POE_CLASS2_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class2"); else if(profileData->power_limit_class == POE_CLASS3_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class3"); else if(profileData->power_limit_class == POE_CLASS4_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class4"); else if(profileData->power_limit_class == POE_CLASS_AUTO_AD) cJSON_AddStringToObject(pEntry, "power_limit", "auto"); else if(profileData->power_limit_class == POE_CLASS_MANUAL_AD) cJSON_AddStringToObject(pEntry, "power_limit", "manual"); cJSON_AddBoolToObject(pEntry, "in_use", profileData->profile_in_use); return OK; } static void _profileJsonWrite(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; int idx = 0; if (NULL == profileData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); for (idx = 0; profileData != NULL && idx < MAX_POE_PROFILE_NUM; idx ++) { if(profileData->profileName[0] == &#39;\0&#39;) { profileData++; continue; } cJSON *entry = cJSON_CreateObject(); status = _composeProfilecfg(profileData,entry,idx); //ok if(status != OK) { return; } cJSON_AddItemToArray(poeOutInfo, entry); profileData++; } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS profile_get(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PROFILE_STRUCT_UC info_new[MAX_POE_PROFILE_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, info_new, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _profileJsonWrite(pData, info_new, status, OK); //ok return RPM_DONE; } HTTP_STATUS profile_get_afterDelFailed(HTTP_CONTEXT *pContext) { STATUS status = ERROR; WRP_RET wrpRet = WRP_OK; POE_PROFILE_STRUCT_UC info_new[MAX_POE_PROFILE_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, info_new, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _profileJsonWrite(pData, info_new, status, 4001); //ok return RPM_DONE; } static STATUS _parseProfileAdd(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == profileData) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } // doesnt have profile id when add new profile_id // /* profile_id */ // if (NULL == pValue || (pValue->type != cJSON_Number)) // { // printf("module(%s) not present,use default \n", "profile_id"); // // return ERROR; // } // else // { // profileData->profile_id = pValue->valueint; // } /*profile_name*/ pValue = cJSON_GetObjectItem(pRoot, "profile_name"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "profile_name"); return ERROR; } else { snprintf((char *)profileData->profileName,strlen(pValue->valuestring)+1,pValue->valuestring); } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { profileData->poe_enable = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } } /*priority*/ pValue = cJSON_GetObjectItem(pRoot, "priority"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "priority"); profileData->priority = POE_PRI_LOW_AD; } else { if (0 == strcmp(pValue->valuestring, "low")) { profileData->priority = POE_PRI_LOW_AD; } else if (0 == strcmp(pValue->valuestring, "middle")) { profileData->priority = POE_PRI_MIDDLE_AD; } else if (0 == strcmp(pValue->valuestring, "high")) { profileData->priority = POE_PRI_HIGH_AD; } else { printf("module(%s)2 not present ,use default", "priority"); profileData->priority = POE_PRI_LOW_AD; } } /*power_limit*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit"); profileData->power_limit = 0; } else { if (0 == strcmp(pValue->valuestring, "auto")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS_AUTO_AD; } else if(0 == strcmp(pValue->valuestring, "class1")) { profileData->power_limit = POE_CLASS_CLASS1_AD; profileData->power_limit_class = POE_CLASS1_AD; } else if(0 == strcmp(pValue->valuestring, "class2")) { profileData->power_limit = POE_CLASS_CLASS2_AD; profileData->power_limit_class = POE_CLASS2_AD; } else if(0 == strcmp(pValue->valuestring, "class3")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS3_AD; } //need check:can we set class4? // else if(0 == strcmp(pValue->valuestring, "class4")) // { // profileData->power_limit = POE_CLASS_CLASS4_AD; // profileData->power_limit_class = POE_CLASS4_AD; // } else if(0 == strcmp(pValue->valuestring, "manual")) { /*power_limit_value*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit_value"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit_value"); profileData->power_limit = 0; profileData->power_limit_class = POE_CLASS_MANUAL_AD; } else { profileData->power_limit = (int)(atof(pValue->valuestring)*10); profileData->power_limit_class = POE_CLASS_MANUAL_AD; } } } return OK; } static STATUS _parseProfileMod(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == profileData) { return ERROR; } pStr = httpGetEnv(pData, "old"); if (NULL == pStr) { printf("failed to get param str:%s", "old"); return ERROR; } printf("data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /* profile_id */ pValue = cJSON_GetObjectItem(pRoot, "id"); if (NULL == pValue || (pValue->type != cJSON_Number)) { printf("module(%s) not present,use default \n", "profile_id"); // return ERROR; } else { profileData->profile_id = pValue->valueint; } /*profile_name*/ pValue = cJSON_GetObjectItem(pRoot, "profile_name"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "profile_name"); return ERROR; } else { snprintf((char *)profileData->profileName,strlen(pValue->valuestring)+1,pValue->valuestring); } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { profileData->poe_enable = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } } /*priority*/ pValue = cJSON_GetObjectItem(pRoot, "priority"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "priority"); profileData->priority = POE_PRI_LOW_AD; } else { if (0 == strcmp(pValue->valuestring, "low")) { profileData->priority = POE_PRI_LOW_AD; } else if (0 == strcmp(pValue->valuestring, "middle")) { profileData->priority = POE_PRI_MIDDLE_AD; } else if (0 == strcmp(pValue->valuestring, "high")) { profileData->priority = POE_PRI_HIGH_AD; } else { printf("module(%s)2 not present ,use default", "priority"); profileData->priority = POE_PRI_LOW_AD; } } /*power_limit*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit"); profileData->power_limit = 0; } else { if (0 == strcmp(pValue->valuestring, "auto")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS_AUTO_AD; } else if(0 == strcmp(pValue->valuestring, "class1")) { profileData->power_limit = POE_CLASS_CLASS1_AD; profileData->power_limit_class = POE_CLASS1_AD; } else if(0 == strcmp(pValue->valuestring, "class2")) { profileData->power_limit = POE_CLASS_CLASS2_AD; profileData->power_limit_class = POE_CLASS2_AD; } else if(0 == strcmp(pValue->valuestring, "class3")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS3_AD; } //need check:can we set class4? // else if(0 == strcmp(pValue->valuestring, "class4")) // { // profileData->power_limit = POE_CLASS_CLASS4_AD; // profileData->power_limit_class = POE_CLASS4_AD; // } else if(0 == strcmp(pValue->valuestring, "manual")) { /*power_limit_value*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit_value"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit_value"); profileData->power_limit = 0; profileData->power_limit_class = POE_CLASS_MANUAL_AD; } else { profileData->power_limit = (int)(atof(pValue->valuestring)*10); profileData->power_limit_class = POE_CLASS_MANUAL_AD; } } } return OK; } static STATUS _parseProfileDel(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; if (NULL == profileData) { return ERROR; } /* profile_name */ pStr = httpGetEnv(pData, "profile_name"); if (NULL == pStr) { printf("failed to get param str:%s\n", "profile_name"); return ERROR; } printf("_parseProfileDel data str:%s \n", pStr); snprintf((char *)profileData->profileName,strlen(pStr)+1,pStr); /* profile_id */ pStr = httpGetEnv(pData, "id"); if (NULL == pStr) { printf("failed to get param str:%s \n", "profile_id"); return ERROR; } printf("_parseProfileDel data str:%s \n", pStr); profileData->profile_id = atoi(pStr); return OK; } // profile_set HTTP_STATUS profile_set(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PROFILE_STRUCT_UC info[MAX_POE_PORT_NUM]; // int info_delIndex = 0; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, profile_get); char* pStr = httpGetEnv(pContext->pHttp_datatemp,"operation"); if( 0 == strcmp(pStr, "insert")) { _parseProfileAdd(pData, info); //ok wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_ADD, info, sizeof(POE_PROFILE_STRUCT_UC), &err, sizeof(int), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else if( 0 == strcmp(pStr, "update")) { _parseProfileMod(pData, info); //ok wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_MOD, info, sizeof(POE_PROFILE_STRUCT_UC), &err, sizeof(int), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else if( 0 == strcmp(pStr, "remove")) { // _parseProfileDel(pData, (void*)&info_delIndex); _parseProfileDel(pData, info); //doing // wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_DEL, (void*)&info_delIndex, sizeof(int), // &err, sizeof(int), WRP_TRAN_TYPE_WRITE); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_DEL, info, sizeof(POE_PROFILE_STRUCT_UC), &err, sizeof(int), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; if(err!=UCL_OK){ return profile_get_afterDelFailed(pContext); } } else { printf("profile_set operation (%s) is ERROR \n", pStr); return ERROR; } wrpRet = status; return profile_get(pContext); } // todo // @wxh poe_port_status.json poe_port_status_get static void _recoveryPortStatusJsonWrite(HTTP_DATATEMP *pData, POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData, int status, int err) { printf("####&&&& goto [%s]\n",__FUNCTION__); cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; int i = 0; unsigned int l_poeBitMap = poeBitMap; if (NULL == recoveryPortCfgData) { printf("####&&&& [%s] goto NULL == recoveryPortCfgData then return\n",__FUNCTION__); // return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); /* @wxh 解耦端口数量 */ // printf("####&&&& in [%s] configuredPortNum = %d &&&&####\n",__FUNCTION__, configuredPortNum); // for (i = 0; i < configuredPortNum; i++) // { // cJSON *entry = cJSON_CreateObject(); // cJSON_AddStringToObject(entry, "state", "connected"); // cJSON_AddNumberToObject(entry, "port", i + 1); // cJSON_AddItemToArray(poeOutInfo, entry); // } /* @wxh 解耦端口数量 */ while (l_poeBitMap) // poeBitMap 二进制中1所在位置的端口设置为开启(从右往左看,最低位的1表示端口1开启,依次类推) { i++; if (l_poeBitMap & 1) { cJSON *entry = cJSON_CreateObject(); cJSON_AddStringToObject(entry, "state", "connected"); cJSON_AddNumberToObject(entry, "port", i); cJSON_AddItemToArray(poeOutInfo, entry); } l_poeBitMap >>= 1; recoveryPortCfgData++; } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS poe_port_status_get(HTTP_CONTEXT *pContext) { printf("####&&&& goto [%s] &&&&####\n",__FUNCTION__); STATUS status = OK; // WRP_RET wrpRet = WRP_OK; POE_PORTRECOVERYCFG_STRUCT info[MAX_POE_PORT_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); // wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, NULL, 0, info, sizeof(POE_PORTRECOVERYCFG_STRUCT) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); // if (WRP_OK != wrpRet) // { // status = ERROR; // } _recoveryPortStatusJsonWrite(pData, info, status, OK); //ok return RPM_DONE; } /**************************************************************************************************/ /* PUBLIC_FUNCTIONS */ /**************************************************************************************************/ void http_managementPoeOutHandlerInit() { if(!poeBitMap || !configuredPortNum) { poeBitMap = devcfg_downLinkPort_getPoeOutBitMap(); configuredPortNum = devcfg_calculate_poeBitMap(poeBitMap); printf(" [%s] ####&&&& poeBitMap = devcfg_downLinkPort_getPoeOutBitMap(); configuredPortNum = devcfg_calculate_poeBitMap(poeBitMap); &&&&#### \n", __FUNCTION__); printf(" ####&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& poeBitMap = %d, configuredPortNum = %d &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&#### \n",poeBitMap, configuredPortNum); } if(!poeBitMap) { printf(" [%s] ####&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& poeBitMap init ERROR, poeBitMap = 0 &&&&&&&&&&&&&&&&&&&&&&&&&&&&&&#### \n", __FUNCTION__); } http_midware_getReg("/data/poe_cfg.json", poe_cfg_get, http_auth_check, NULL); http_midware_postReg("/data/poe_cfg.json", poe_cfg_set, http_auth_check, NULL); http_midware_getReg("/data/port_cfg.json", port_cfg_get, http_auth_check, NULL); http_midware_postReg("/data/port_cfg.json", port_cfg_set, http_auth_check, NULL); http_midware_getReg("/data/poe_profile.json", profile_get, http_auth_check, NULL); http_midware_postReg("/data/poe_profile.json", profile_set, http_auth_check, NULL); //not support recovery //@wxh testing http_midware_getReg("/data/poe_auto_recovery_global.json", recovery_global_get, http_auth_check, NULL); http_midware_postReg("/data/poe_auto_recovery_global.json", recovery_global_set, http_auth_check, NULL); //not support recovery //@wxh testing http_midware_getReg("/data/poe_auto_recovery_cfg.json", recovery_port_cfg_get, http_auth_check, NULL); http_midware_postReg("/data/poe_auto_recovery_cfg.json", recovery_port_cfg_set, http_auth_check, NULL); //@wxh not support now // http_midware_getReg("/data/poe_port_status.json", poe_port_status_get, http_auth_check, NULL); //只需要响应? http_midware_postReg("/data/poe_port_status.json", poe_port_status_get, http_auth_check, NULL); } /**************************************************************************************************/ /* GLOBAL_FUNCTIONS */ /**************************************************************************************************/ 列出所有与 recovery 相关的函数的名字和入参 和他们的作用(简略)
10-30
/** * @file tuya_app_main.c * @author www.tuya.com * @brief tuya_app_main module is used to * @version 0.1 * @date 2022-10-28 * * @copyright Copyright (c) tuya.inc 2022 * */ #define ENABLE_FLASH_PROTECT 0 #include <stdlib.h> #include "tuya_cloud_types.h" #include "tuya_svc_netmgr.h" #if defined(ENABLE_WIFI_SERVICE) && (ENABLE_WIFI_SERVICE == 1) #include "tuya_iot_wifi_api.h" #endif #if defined(ENABLE_WIRED) && (ENABLE_WIRED == 1) #include "tuya_iot_base_api.h" #endif #include "tuya_iot_com_api.h" #include "tuya_ws_db.h" #include "tuya_wifi_netcfg.h" #include "tuya_wifi_status.h" #include "tuya_cloud_wifi_defs.h" #include "tkl_wakeup.h" #include "tkl_sleep.h" #include "tal_system.h" #include "tal_log.h" #include "base_event.h" #include "mf_test.h" #include "mqc_app.h" #if defined(ENABLE_LWIP) && (ENABLE_LWIP == 1) #include "lwip_init.h" #endif #include "tal_uart.h" #include "dp_process.h" #include "app_config.h" #include "app_key.h" #include "app_led.h" #include "app_product_test.h" #include "app_save.h" #include "app_battery.h" #include "app_save.h" #include "app_timer_wakeup.h" #include "app_ht_sensor.h" #include "tuya_error_code.h" /*********************************************************** ************************macro define************************ ***********************************************************/ #define PID "wwihdnnnbu2nhtll" // #define PID "uiiyltkjmkhwumga" // #define UUID "uuid8c979a2cea91955c" // #define AUTHKEY "8TxpnLMkuovxRtlh5TfICLipjzZKnc07" /* The registration code here does not work, you need to apply for a new one. * https://developer.tuya.com/cn/docs/iot/lisence-management?id=Kb4qlem97idl0 */ // #define UUID "f998xxxxxxxx2409" // #define AUTHKEY "WEHAxxxxxxxxxxxxxxxxxxxxxxxxVVkf" /* network button, LED pin */ /*********************************************************** ***********************typedef define*********************** ***********************************************************/ /*********************************************************** ********************function declaration******************** ***********************************************************/ extern void tuya_ble_enable_debug(bool enable); /*********************************************************** ***********************variable define********************** ***********************************************************/ /* app thread handle */ STATIC THREAD_HANDLE ty_app_thread = NULL; /*********************************************************** ***********************function define********************** ***********************************************************/ /** * @brief SOC device upgrade entry * * @param[in] fw: firmware info * * @return OPRT_OK on success. Others on error, please refer to "tuya_error_code.h". */ STATIC OPERATE_RET __soc_dev_rev_upgrade_info_cb(IN CONST FW_UG_S *fw) { TAL_PR_DEBUG("SOC Rev Upgrade Info"); TAL_PR_DEBUG("fw->tp:%d", fw->tp); TAL_PR_DEBUG("fw->fw_url:%s", fw->fw_url); TAL_PR_DEBUG("fw->fw_hmac:%s", fw->fw_hmac); TAL_PR_DEBUG("fw->sw_ver:%s", fw->sw_ver); TAL_PR_DEBUG("fw->file_size:%u", fw->file_size); return OPRT_OK; } /** * @brief SOC device cloud state change callback * * @param[in] status: current status * * @return none */ STATIC VOID_T __soc_dev_status_changed_cb(IN CONST GW_STATUS_E status) { TAL_PR_DEBUG("SOC TUYA-Cloud Status:%d", status); return; } /** * @brief SOC device DP query entry * * @param[in] dp_qry: DP query list * * @return none */ STATIC VOID_T __soc_dev_dp_query_cb(IN CONST TY_DP_QUERY_S *dp_qry) { UINT32_T index = 0; TAL_PR_DEBUG("SOC Rev DP Query Cmd"); if (dp_qry->cid != NULL) { TAL_PR_ERR("soc not have cid.%s", dp_qry->cid); } if (dp_qry->cnt == 0) { TAL_PR_DEBUG("soc rev all dp query"); respone_device_all_status(); } else { TAL_PR_DEBUG("soc rev dp query cnt:%d", dp_qry->cnt); for (index = 0; index < dp_qry->cnt; index++) { TAL_PR_DEBUG("rev dp query:%d", dp_qry->dpid[index]); // UserTODO } } return; } /** * @brief SOC device format command data delivery entry * * @param[in] dp: obj dp info * * @return none */ STATIC VOID_T __soc_dev_obj_dp_cmd_cb(IN CONST TY_RECV_OBJ_DP_S *dp) { TAL_PR_DEBUG("SOC Rev DP Obj Cmd t1:%d t2:%d CNT:%u", dp->cmd_tp, dp->dtt_tp, dp->dps_cnt); dp_obj_process(dp->dps, dp->dps_cnt); return; } /** * @brief SOC device transparently transmits command data delivery entry * * @param[in] dp: raw dp info * * @return none */ STATIC VOID_T __soc_dev_raw_dp_cmd_cb(IN CONST TY_RECV_RAW_DP_S *dp) { TAL_PR_DEBUG("SOC Rev DP Raw Cmd t1:%d t2:%d dpid:%d len:%u", dp->cmd_tp, dp->dtt_tp, dp->dpid, dp->len); dp_raw_process(dp->dpid, dp->data, dp->len); return; } /** * @brief app process when device reset * * @param[in] type: gateway reset type * * @return none */ STATIC VOID_T __soc_dev_reset_inform_cb(GW_RESET_TYPE_E type) { TAL_PR_DEBUG("reset type %d", type); if(type == GW_REMOTE_RESET_FACTORY || type == GW_REMOTE_RESET_FACTORY || type == GW_RESET_DATA_FACTORY || type == GW_REMOTE_RESET_DATA_FACTORY) { TAL_PR_NOTICE("clear local data"); app_save_delete(); } return; } /** * @brief SOC external network status change callback * * @param[in/out] data * @return STATIC */ STATIC OPERATE_RET __soc_dev_net_status_cb(VOID *data) { STATIC BOOL_T s_syn_all_status = FALSE; TAL_PR_DEBUG("network status changed!"); if (tuya_svc_netmgr_linkage_is_up(LINKAGE_TYPE_DEFAULT)) { TAL_PR_DEBUG("linkage status changed, current status is up"); if (get_mqc_conn_stat()) { TAL_PR_DEBUG("mqtt is connected!"); if (FALSE == s_syn_all_status) { upload_device_all_status(); s_syn_all_status = TRUE; } // UserTODO } } else { TAL_PR_DEBUG("linkage status changed, current status is down"); // UserTODO } return OPRT_OK; } /** * @brief mf uart init * * @param[in] baud: Baud rate * @param[in] bufsz: uart receive buffer size * * @return none */ VOID mf_uart_init_callback(UINT_T baud, UINT_T bufsz) { TAL_UART_CFG_T cfg; memset(&cfg, 0, sizeof(TAL_UART_CFG_T)); cfg.base_cfg.baudrate = baud; cfg.base_cfg.databits = TUYA_UART_DATA_LEN_8BIT; cfg.base_cfg.parity = TUYA_UART_PARITY_TYPE_NONE; cfg.base_cfg.stopbits = TUYA_UART_STOP_LEN_1BIT; cfg.rx_buffer_size = bufsz; tal_uart_init(TUYA_UART_NUM_0, &cfg); return; } /** * @brief mf uart free * * @param[in] none * * @return none */ VOID mf_uart_free_callback(VOID) { tal_uart_deinit(TUYA_UART_NUM_0); return; } /** * @brief mf uart send function * * @param[in] data: send data * @param[in] len: send data length * * @return none */ VOID mf_uart_send_callback(IN BYTE_T *data, IN CONST UINT_T len) { tal_uart_write(TUYA_UART_NUM_0, data, len); return; } /** * @brief mf uart receive function * * @param[in] buf: receive buffer * @param[in] len: receive buffer max length * * @return receive data length */ UINT_T mf_uart_recv_callback(OUT BYTE_T *buf, IN CONST UINT_T len) { return tal_uart_read(TUYA_UART_NUM_0, buf, len); } /** * @brief Product test callback function * * @param[in] cmd: Command * @param[in] data: data * @param[out] ret_data: Resulting data * @param[out] ret_len: Resulting data length * * @return OPRT_OK on success. Others on error, please refer to "tuya_error_code.h". */ OPERATE_RET mf_user_product_test_callback(USHORT_T cmd, UCHAR_T *data, UINT_T len, OUT UCHAR_T **ret_data, OUT USHORT_T *ret_len) { /* USER todo */ //gpio test refer to tuyaos_demo_examples -> src/examples/service_mf_test return OPRT_OK; } /** * @brief mf configure write callback functions * * @param[in] none * * @return none */ VOID mf_user_callback(VOID) { return ; } /** * @brief Callback function before entering the production test * * @param[in] none * * @return none */ VOID mf_user_enter_mf_callback(VOID) { return ; } typedef struct { uint8_t I2C_addr; CHT8315_CFG_REG_T cfg_reg; }CHT8315_CONTEXT_TT; int32_t CHT8315_ReadReg(uint8_t I2C_addr, uint8_t reg_addr, CHT8315_REG_DATA_T *pData); extern void* sg_CHT8315_handle; void read_limit() { uint8_t i2c = ((CHT8315_CONTEXT_TT*)sg_CHT8315_handle)->I2C_addr; uint16_t val = 0; int err = CHT8315_ReadReg(i2c, 5, &val); TAL_PR_DEBUG("REG: %x VAL: %x err: %d", 5, val, err); err = CHT8315_ReadReg(i2c, 6, &val); TAL_PR_DEBUG("REG: %x VAL: %x err: %d", 6, val, err); err = CHT8315_ReadReg(i2c, 7, &val); TAL_PR_DEBUG("REG: %x VAL: %x err: %d", 7, val, err); err = CHT8315_ReadReg(i2c, 8, &val); TAL_PR_DEBUG("REG: %x VAL: %x err: %d", 8, val, err); } VOID_T alt_irq_cb(VOID_T *args) { TAL_PR_DEBUG("ALT IRQ--------------"); } void alt_init() { /* init key gpio */ OPERATE_RET rt = OPRT_OK; TUYA_GPIO_BASE_CFG_T key_cfg = { .mode = TUYA_GPIO_PULLUP, .direct = TUYA_GPIO_INPUT, .level = TUYA_GPIO_LEVEL_LOW }; TUYA_CALL_ERR_LOG(tkl_gpio_init(TUYA_GPIO_NUM_16, &key_cfg)); } void app_task1(VOID* args) { tal_system_sleep(5000); double l = 25; double h = 30; OPERATE_RET rt = OPRT_OK; TAL_PR_DEBUG("TEST------------------- Limit: %f %f ", l, h); TUYA_WAKEUP_SOURCE_BASE_CFG_T get_cfg; memset(&get_cfg, 0, sizeof(TUYA_WAKEUP_SOURCE_BASE_CFG_T)); tkl_wakeup_source_type_get(&get_cfg); TAL_PR_DEBUG("WAKEUPSOURCE=%d",get_cfg.source); alt_init(); /* init key gpio */ int32_t ret = 0; app_ht_sensor_init(); CHT8315_CFG_REG_T cfg; CHT8315_Get_Cfg_Reg(sg_CHT8315_handle, &cfg); TAL_PR_DEBUG("CFG: %#04x", cfg.value); CHT8315_Set_TempLimit(sg_CHT8315_handle, 1, h); CHT8315_Set_TempLimit(sg_CHT8315_handle, 0, l); CHT8315_Set_HumLimit(sg_CHT8315_handle, 1, 127); CHT8315_Set_HumLimit(sg_CHT8315_handle, 0, 0); CHT8315_REG_DATA_T reg; CHT8315_Get_Status_Reg(sg_CHT8315_handle, &reg); TAL_PR_DEBUG("REG: %#04x", reg); TUYA_GPIO_LEVEL_E alt_val, tmp_val = TUYA_GPIO_LEVEL_HIGH; uint32_t tick = 0; while (1) { tkl_gpio_read(TUYA_GPIO_NUM_16, &tmp_val); if (tick++ > 50) { TAL_PR_DEBUG("DEEEEEEEPSleep"); TUYA_CALL_ERR_LOG(app_alter_wakeup_init()); tkl_cpu_sleep_mode_set(TRUE, TUYA_CPU_DEEP_SLEEP); } if (!tmp_val && alt_val) { tick = 0; double temp; uint8_t humi; CHT8315_Get_Temperature(sg_CHT8315_handle, &temp); CHT8315_Get_Humidity(sg_CHT8315_handle, &humi); TAL_PR_DEBUG("Temp: %f Humidity: %d", temp, humi); CHT8315_Set_HumLimit(sg_CHT8315_handle, 1, humi < 90 ? humi + 10 : 99); CHT8315_Set_HumLimit(sg_CHT8315_handle, 0, humi > 11 ? humi - 10 : 0); CHT8315_Set_TempLimit(sg_CHT8315_handle, 1, temp + 1); CHT8315_Set_TempLimit(sg_CHT8315_handle, 0, temp - 1); } alt_val = tmp_val; tal_system_sleep(100); } } uint32_t alt_check_limit() { CHT8315_REG_DATA_T val = 0xff; CHT8315_Get_Status_Reg(sg_CHT8315_handle, &val); TUYA_GPIO_LEVEL_E lvl = 0; tkl_gpio_read(TUYA_GPIO_NUM_16, &lvl); if (val && !lvl) { TAL_PR_ERR(">>>>>>>>>>> Sensor Status ERR: REG=%#04x IO=%d", val, lvl); return 1; } else { TAL_PR_DEBUG(">>>>>>>>>>> Sensor status OK."); return 0; } } uint32_t alt_update_limit_impl() { double temp = 0; uint8_t humi = 0; CHT8315_Get_Temperature(sg_CHT8315_handle, &temp); CHT8315_Get_Humidity(sg_CHT8315_handle, &humi); CHT8315_Set_HumLimit(sg_CHT8315_handle, 1, humi < 90 ? humi + 10 : 99); CHT8315_Set_HumLimit(sg_CHT8315_handle, 0, humi > 11 ? humi - 10 : 0); CHT8315_Set_TempLimit(sg_CHT8315_handle, 1, temp + 1); CHT8315_Set_TempLimit(sg_CHT8315_handle, 0, temp - 1); if (alt_check_limit() == 0) return 0; tal_system_sleep(500); if (alt_check_limit() == 0) return 0; tal_system_sleep(500); return alt_check_limit(); } uint32_t alt_update_limit() { if (alt_update_limit_impl() == 0) return 0; tal_system_sleep(100); if (alt_update_limit_impl() == 0) return 0; tal_system_sleep(100); return alt_update_limit_impl(); } void app_task2(VOID* args) { tal_system_sleep(10000); double l = 25; double h = 30; TAL_PR_DEBUG("TEST------------------- Limit: %f %f ", l, h); alt_init(); /* init key gpio */ int32_t ret = 0; // TUYA_WAKEUP_SOURCE_BASE_CFG_T wakeup_cfg = { // .source = TUYA_WAKEUP_SOURCE_GPIO, // .wakeup_para.gpio_param = { // .gpio_num = CHT8315_ALERT_GPIO_NUM, // .level = CHT8315_ALERT_LEVEL == TUYA_GPIO_LEVEL_LOW ? TUYA_GPIO_WAKEUP_RISE : TUYA_GPIO_WAKEUP_FALL // } // }; // ret = tkl_wakeup_source_set(&wakeup_cfg); // uint16_t val = CHT8315_Temperature_to_Reg(50, 0); app_ht_sensor_init(); read_limit(); CHT8315_Set_TempLimit(sg_CHT8315_handle, 1, h); CHT8315_Set_TempLimit(sg_CHT8315_handle, 0, l); CHT8315_Set_HumLimit(sg_CHT8315_handle, 1, 127); CHT8315_Set_HumLimit(sg_CHT8315_handle, 0, 0); read_limit(); CHT8315_CFG_REG_T cfg; CHT8315_Get_Cfg_Reg(sg_CHT8315_handle, &cfg); TAL_PR_DEBUG("CFG: %#04x", cfg.value); CHT8315_REG_DATA_T reg; CHT8315_Get_Status_Reg(sg_CHT8315_handle, &reg); TAL_PR_DEBUG("REG: %#04x", reg); while (1) { app_ht_sensor_get_temp_hum(); tal_system_sleep(500); CHT8315_Get_Status_Reg(sg_CHT8315_handle, &reg); TAL_PR_DEBUG("REG: %#04x", reg>>11); tal_system_sleep(500); } } #if (1) VOID app_task(VOID *args) { TAL_PR_NOTICE("APP_TASK CHECKPINT"); TUYA_WAKEUP_SOURCE_BASE_CFG_T get_cfg; memset(&get_cfg, 0, sizeof(TUYA_WAKEUP_SOURCE_BASE_CFG_T)); tkl_wakeup_source_type_get(&get_cfg); TAL_PR_DEBUG("WAKEUPSOURCE=%d",get_cfg.source); OPERATE_RET rt = OPRT_OK; UINT_T tick = 0, start_nw_cfg_tick = 0, nw_cfg_tick = 0, mqtt_disc_tick = 0,nw_tick=0; TIME_S start_sec = 0; TIME_MS start_ms = 0; TIME_S current_sec; TIME_MS current_ms; tal_time_get_system_time(&start_sec, &start_ms); // =============== 记录启动次数 =============== // 每次启动,启动次数加1 g_app_ht_sensor_context.save_data.boot_count++; TAL_PR_NOTICE("Device boot count: %u", g_app_ht_sensor_context.save_data.boot_count); // 如果需要,保存到Flash app_save_ht_sensor_later(0); // 立即保存 // app_product_test(0,NULL); // if(g_product_test_status == PRODUCT_TEST_STAT_ON) // { // TAL_PR_NOTICE("go product testmode"); // while (1) // { // tal_system_sleep(500); // } // } alt_init(); while(1) { // UINT8_T batPCT = 0; INT32_T raw_adc = 0; enable_battery_ADC(); tal_system_sleep(200); // rt = tkl_adc_read_data(BAT_ADC_NUM, &raw_adc, 1); rt = tkl_adc_read_voltage(BAT_ADC_NUM, &raw_adc, 1); TAL_PR_NOTICE("V=%d", raw_adc); // if(rt == OPRT_OK) // { // TAL_PR_NOTICE(" ADC:%d", raw_adc); // } else { // TAL_PR_ERR("ADC erro: %d", rt); // } // rt = get_battery_percentage(&batPCT); // get_battery_percentage(&batPCT); disable_battery_ADC(); ++tick; GW_WORK_STAT_T wk_stat = UNREGISTERED; GW_WIFI_NW_STAT_E nw_stat = STAT_LOW_POWER; wk_stat = get_gw_active(); rt = get_wf_gw_nw_status(&nw_stat); if(rt != OPRT_OK) { TAL_PR_ERR("[%s] get_wf_gw_nw_status error:%d", __FUNCTION__, rt); } TAL_PR_DEBUG("nw_stat is %d, wk_stat is %d", nw_stat, wk_stat); // if(nw_stat == 0 ||nw_stat == STAT_UNPROVISION) // { // TAL_PR_NOTICE("=== Device never provisioned ==="); // TAL_PR_NOTICE("=== Goto deepsleep immediately ==="); // // 配置按键唤醒(但不要启动按键线程) // app_key_wakeup_init(); // //给按键启动一点时间 // tal_system_sleep(4000); // // 立即深度睡眠(不记录启动次数) // tkl_cpu_sleep_mode_set(TRUE, TUYA_CPU_DEEP_SLEEP); // // 这里不会执行到 // return; // } if(nw_stat == STAT_UNPROVISION || nw_stat == STAT_AP_STA_UNCFG || nw_stat == STAT_UNPROVISION_AP_STA_UNCFG) //开始配网 { if(start_nw_cfg_tick++ >= WIFI_NETCONFIG_TIMEOUT) { // // 配网阶段,禁用深度睡眠 // tal_system_sleep(1000); // 短休眠,保持设备唤醒 // continue; // TAL_PR_DEBUG("goto deepsleep: start net-config timeout"); // goto DEEPSLEEP; TAL_PR_DEBUG("goto deepsleep: start net-config timeout"); goto DEEPSLEEP; // 超时→睡眠 }else { // 已配网/初始化完成,允许深度睡眠 // goto DEEPSLEEP; tal_system_sleep(1000); // 未超时→保持唤醒,继续配网 continue; } } else if(nw_stat == STAT_AP_STA_DISC || nw_stat == STAT_STA_DISC) //配网中 { if(nw_cfg_tick++ >= 60) { TAL_PR_DEBUG("goto deepsleep: net-config timeout"); goto DEEPSLEEP; } } else //配网成功/配网失败/未配网 { if(wk_stat == ACTIVATED) //已激活 { if(get_mqc_conn_stat()) //已连接MQTT { query_lp_dp_cache(); if(app_ht_sensor_get_temp_hum() != OPRT_OK) { TAL_PR_ERR("app_ht_sensor_get_temp_hum error: %d", rt); } app_ht_sensor_set_alarm(); TAL_PR_NOTICE("[ALARM_DEBUG] Before comparison:"); TAL_PR_NOTICE(" temp_alarm=%d, last_temp_alarm=%d, equal=%d", g_app_ht_sensor_context.temp_alarm, g_app_ht_sensor_context.save_data.last_temp_alarm, g_app_ht_sensor_context.temp_alarm == g_app_ht_sensor_context.save_data.last_temp_alarm); TY_OBJ_DP_S dp_data[4] = {0}; uint8_t dp_count = 2; // 温湿度2个是必报的 dp_data[0].dpid = DPID_TEMPERATURE; dp_data[0].type = PROP_VALUE; dp_data[0].value.dp_value = g_app_ht_sensor_context.temperature + g_app_ht_sensor_context.save_data.temp_calibration; dp_data[1].dpid = DPID_HUMIDITY; dp_data[1].type = PROP_VALUE; dp_data[1].value.dp_value = g_app_ht_sensor_context.humidity + g_app_ht_sensor_context.save_data.hum_calibration; TAL_PR_NOTICE("g_app_ht_sensor_context.temp_alarm = %d",g_app_ht_sensor_context.temp_alarm); TAL_PR_NOTICE("g_app_ht_sensor_context.hum_alarm = %d",g_app_ht_sensor_context.hum_alarm); TAL_PR_NOTICE("g_app_ht_sensor_context.save_data.last_temp_alarm = %d",g_app_ht_sensor_context.save_data.last_temp_alarm); TAL_PR_NOTICE("g_app_ht_sensor_context.save_data.last_hum_alarm = %d",g_app_ht_sensor_context.save_data.last_hum_alarm); bool alarm_changed = false; if(g_app_ht_sensor_context.temp_alarm != g_app_ht_sensor_context.save_data.last_temp_alarm) { dp_data[dp_count].dpid = DPID_TEMP_ALARM; dp_data[dp_count].type = PROP_ENUM; dp_data[dp_count].value.dp_value = g_app_ht_sensor_context.temp_alarm; dp_count++; alarm_changed = true; } if(g_app_ht_sensor_context.hum_alarm != g_app_ht_sensor_context.save_data.last_hum_alarm) { dp_data[dp_count].dpid = DPID_HUM_ALARM; dp_data[dp_count].type = PROP_ENUM; dp_data[dp_count].value.dp_value = g_app_ht_sensor_context.hum_alarm; dp_count++; alarm_changed = true; } OPERATE_RET ret = dev_report_dp_stat_sync(NULL, dp_data, dp_count, 1); if(ret == OPRT_OK&& alarm_changed) { g_app_ht_sensor_context.save_data.last_temp_alarm = g_app_ht_sensor_context.temp_alarm; g_app_ht_sensor_context.save_data.last_hum_alarm = g_app_ht_sensor_context.hum_alarm; app_save_ht_sensor_later(0); } TAL_PR_NOTICE("g_app_ht_sensor_context.save_data.temp = %d",g_app_ht_sensor_context.temperature); UINT8_T batPCT = 0; INT32_T raw_adc = 0; enable_battery_ADC(); tal_system_sleep(200); rt = tkl_adc_read_data(BAT_ADC_NUM, &raw_adc, 1); if(rt == OPRT_OK) { TAL_PR_NOTICE(" ADC:%d", raw_adc); } else { TAL_PR_ERR("ADC erro: %d", rt); } rt = get_battery_percentage(&batPCT); if(rt != OPRT_OK) { TAL_PR_ERR("[%s] get_battery_percentage error:%d", __FUNCTION__, rt); } else { TAL_PR_NOTICE("[%s] batPCT:%d", __FUNCTION__, batPCT); } disable_battery_ADC(); TAL_PR_NOTICE(" batPCT:%d", batPCT); upload_device_battery_percentage(batPCT); if(get_gw_ext_stat() == EXT_GW_UPGRD) { goto UPGRADE; } else { goto DEEPSLEEP; } } else //未连接MQTT { if(mqtt_disc_tick++ >= 30) { TAL_PR_WARN("goto deepsleep: connect timeout"); goto DEEPSLEEP; } } } else //未激活 { if(start_nw_cfg_tick == 0) //未配置网络 { if(tick >= 5) { TAL_PR_DEBUG("goto deepsleep: device not active"); goto DEEPSLEEP; } } else if(start_nw_cfg_tick++ >= WIFI_NETCONFIG_TIMEOUT + 5) //已配置网络,等待激活 { TAL_PR_DEBUG("goto deepsleep: device active timeout"); goto DEEPSLEEP; } } } CANNOT_SLEEP: tal_system_sleep(1000); } UPGRADE: current_sec = 0; current_ms = 0; tal_time_get_system_time(&current_sec, &current_ms); g_app_ht_sensor_context.save_data.boot_time += (UINT32_T)(current_sec - start_sec); TAL_PR_NOTICE("all time : %d s", g_app_ht_sensor_context.save_data.boot_time); // 保存到Flash app_save_ht_sensor_later(0); do { tal_system_sleep(500); } while(get_gw_ext_stat() == EXT_GW_UPGRD); DEEPSLEEP: current_sec = 0; current_ms = 0; tal_time_get_system_time(&current_sec, &current_ms); g_app_ht_sensor_context.save_data.boot_time += (UINT32_T)(current_sec - start_sec); // g_app_ht_sensor_context.save_data.boot_time=0; // g_app_ht_sensor_context.save_data.boot_count=0; TAL_PR_NOTICE("all time : %d s", g_app_ht_sensor_context.save_data.boot_time); // 保存到Flash app_save_ht_sensor_later(0); if(is_key_pressed()) { goto CANNOT_SLEEP; } else { app_led_deinit(); TUYA_CALL_ERR_LOG(app_timer_wakeup_init()); TUYA_CALL_ERR_LOG(app_key_wakeup_init()); TUYA_CALL_ERR_LOG(app_alter_wakeup_init()); // app_ht_sensor_get_temp_hum(); // TUYA_CALL_ERR_LOG(app_ht_sensor_wakeup_init()); TUYA_CALL_ERR_LOG(alt_update_limit()); tkl_cpu_sleep_mode_set(TRUE, TUYA_CPU_DEEP_SLEEP); TAL_PR_ERR("XXXXXXXXXXXXXXXXXXXXXXXXXXXXX SHOULD NOT PRINT XXXXXXXXXXXXXX"); app_save_ht_sensor_later(5); } } #else VOID app_task(VOID *args) { OPERATE_RET rt = OPRT_OK; TUYA_ADC_BASE_CFG_T bat_adc_base_cfg = {0}; bat_adc_base_cfg.ch_list.data = 1 << BAT_ADC_CHANNEL; bat_adc_base_cfg.ch_nums = 1; bat_adc_base_cfg.width = 12; bat_adc_base_cfg.mode = TUYA_ADC_SINGLE; bat_adc_base_cfg.type = TUYA_ADC_INNER_SAMPLE_VOL; bat_adc_base_cfg.conv_cnt = 1; tkl_adc_init(BAT_ADC_NUM, &bat_adc_base_cfg); INT_T voltaage = 0; TUYA_GPIO_BASE_CFG_T gpio_cfg = { .direct = TUYA_GPIO_OUTPUT, .mode = TUYA_GPIO_PUSH_PULL, .level = TUYA_GPIO_LEVEL_HIGH }; tkl_gpio_init(BAT_ADC_EN_GPIO_NUM, &gpio_cfg); while(1) { rt = tkl_adc_read_voltage(TUYA_ADC_NUM_0, &voltaage, 1); if(rt == OPRT_OK) { TAL_PR_DEBUG("[%s] bat_voltage:%d", __FUNCTION__, voltaage); } else { TAL_PR_ERR("[%s] tkl_adc_read_voltage error:%d", __FUNCTION__, rt); } tal_system_sleep(1000); } } #endif /** * @brief SOC device initialization * * @param[in] none * * @return OPRT_OK on success. Others on error, please refer to "tuya_error_code.h". */ OPERATE_RET __soc_device_init(VOID_T) { TAL_PR_NOTICE("__soc_device_init CHECKPOINT"); OPERATE_RET rt = OPRT_OK; ty_subscribe_event(EVENT_LINK_UP, "quickstart", __soc_dev_net_status_cb, SUBSCRIBE_TYPE_NORMAL); ty_subscribe_event(EVENT_LINK_DOWN, "quickstart", __soc_dev_net_status_cb, SUBSCRIBE_TYPE_NORMAL); ty_subscribe_event(EVENT_MQTT_CONNECTED, "quickstart", __soc_dev_net_status_cb, SUBSCRIBE_TYPE_NORMAL); #if (defined(UUID) && defined(AUTHKEY)) #ifndef ENABLE_KV_FILE ws_db_init_mf(); #endif /* Set authorization information * Note that if you use the default authorization information of the code, there may be problems of multiple users and conflicts, * so try to use all the authorizations purchased from the tuya iot platform. * Buying guide: https://developer.tuya.com/cn/docs/iot/lisence-management?id=Kb4qlem97idl0. * You can also apply for two authorization codes for free in the five-step hardware development stage of the Tuya IoT platform. * Authorization information can also be written through the production testing tool. * When the production testing function is started and the authorization is burned with the Tuya Cloud module tool, * please comment out this piece of code. */ #ifdef ENABLE_WIFI_SERVICE WF_GW_PROD_INFO_S prod_info = {UUID, AUTHKEY}; TUYA_CALL_ERR_RETURN(tuya_iot_set_wf_gw_prod_info(&prod_info)); #else GW_PROD_INFO_S prod_info = {UUID, AUTHKEY}; TUYA_CALL_ERR_RETURN(tuya_iot_set_gw_prod_info(&prod_info)); #endif #else /*authorization is burned with the Tuya Cloud module tool *If you want to get the specific details, such as GPIO TEST, *please refer to the tuyaos_demo_examples -> src/examples/service_mf_test.*/ MF_IMPORT_INTF_S intf = {0}; intf.uart_init = mf_uart_init_callback; intf.uart_free = mf_uart_free_callback; intf.uart_send = mf_uart_send_callback; intf.uart_recv = mf_uart_recv_callback; intf.mf_user_product_test = mf_user_product_test_callback; intf.user_callback = mf_user_callback; intf.user_enter_mf_callback = mf_user_enter_mf_callback; TUYA_CALL_ERR_RETURN(mf_init(&intf, APP_BIN_NAME, USER_SW_VER, TRUE)); // rt = mf_init(&intf, APP_BIN_NAME, USER_SW_VER, TRUE); // if(rt != OPRT_OK) // { // TARL_PR_ERR("mf_init failed: %d",rt); // return rt; // } TAL_PR_NOTICE("mf_init successfully"); #endif if(PRODUCT_TEST_STAT_OFF == g_product_test_status) { /* Initialize TuyaOS product information */ TY_IOT_CBS_S iot_cbs = {0}; iot_cbs.gw_status_cb = __soc_dev_status_changed_cb; iot_cbs.gw_ug_cb = __soc_dev_rev_upgrade_info_cb; iot_cbs.gw_reset_cb = __soc_dev_reset_inform_cb; iot_cbs.dev_obj_dp_cb = __soc_dev_obj_dp_cmd_cb; iot_cbs.dev_raw_dp_cb = __soc_dev_raw_dp_cmd_cb; iot_cbs.dev_dp_query_cb = __soc_dev_dp_query_cb; #if (defined(ENABLE_QRCODE_ACTIVE) && (ENABLE_QRCODE_ACTIVE == 1)) && (!(defined(ENABLE_WIFI_QRCODE) && (ENABLE_WIFI_QRCODE == 1))) iot_cbs.active_shorturl = __qrcode_active_shourturl_cb; #endif #ifdef ENABLE_WIFI_SERVICE TUYA_CALL_ERR_RETURN(tuya_iot_wf_soc_dev_init(GWCM_LOW_POWER, WF_START_AP_FIRST, &iot_cbs, PID, USER_SW_VER)); #ifdef ENABLE_WIRED // init wired linkage TUYA_CALL_ERR_RETURN(tuya_svc_wired_init()); #endif #else TUYA_CALL_ERR_RETURN(tuya_iot_soc_init(&iot_cbs, PID, USER_SW_VER)); #endif } #ifdef ENABLE_BT_SERVICE tuya_ble_enable_debug(false); #endif app_restore_ht_sensor(); app_save_init(); app_battery_init(); app_ht_sensor_init(); app_key_init(); app_led_init(); THREAD_HANDLE app_task_handle; THREAD_CFG_T thread_cfg = { .thrdname = "app_task", .priority = THREAD_PRIO_6, .stackDepth = 4096 }; TAL_PR_NOTICE("%d",__LINE__); TUYA_CALL_ERR_LOG(tal_thread_create_and_start(&app_task_handle, NULL, NULL, app_task, NULL, &thread_cfg)); return 0; } STATIC VOID_T user_main(VOID_T) { OPERATE_RET rt = OPRT_OK; // app_ht_sensor_wakeup_init(); // app_task(NULL); /* Initialization, because DB initialization takes a long time, * which affects the startup efficiency of some devices, * so special processing is performed during initialization to delay initialization of DB */ // #if OPERATING_SYSTEM != SYSTEM_LINUX // rt= system("mkdir -p ./tuya_db_files/"); // TUYA_CALL_ERR_LOG(tuya_iot_init_params("./tuya_db_files/", NULL)); // TY_INIT_PARAMS_S init_param = {0}; // init_param.init_db = TRUE; // strcpy(init_param.sys_env, TARGET_PLATFORM); // TUYA_CALL_ERR_LOG(tuya_iot_init_params(NULL, &init_param)); // #else // TY_INIT_PARAMS_S init_param = {0}; // init_param.init_db = TRUE; // strcpy(init_param.sys_env, TARGET_PLATFORM); // TAL_PR_ERR("Init_param.sys_env ------------------------- "TARGET_PLATFORM); // TUYA_CALL_ERR_LOG(tuya_iot_init_params(NULL, &init_param)); // TAL_PR_ERR("Init_param.sys_env -------------------------eof"); #if OPERATING_SYSTEM != SYSTEM_LINUX TY_INIT_PARAMS_S init_param = {0}; init_param.init_db = TRUE; // 开启DB初始化 strcpy(init_param.sys_env, TARGET_PLATFORM); // 修复后的正确赋值 // TUYA_CALL_ERR_LOG(tuya_iot_init_params("./tuya_db_files/", &init_param)); #else TY_INIT_PARAMS_S init_param = {0}; init_param.init_db = TRUE; strcpy(init_param.sys_env, TARGET_PLATFORM); TAL_PR_ERR("Init_param.sys_env ------------------------- "TARGET_PLATFORM); TUYA_CALL_ERR_LOG(tuya_iot_init_params(NULL, &init_param)); TAL_PR_ERR("Init_param.sys_env -------------------------eof"); #endif TAL_PR_ERR("Init_param.sys_env -------------------------"); set_wf_netcfg_timeout(WIFI_NETCONFIG_TIMEOUT); TAL_PR_NOTICE("sdk_infoX:%s", tuya_iot_get_sdk_info()); /* print SDK information */ TAL_PR_NOTICE("name:%s:%s", APP_BIN_NAME, USER_SW_VER); /* print the firmware name and version */ TAL_PR_NOTICE("firmware compiled at %s %s", __DATE__, __TIME__); /* print firmware compilation time */ TAL_PR_NOTICE("system reset reason:[%d]", tal_system_get_reset_reason(NULL)); /* print system reboot causes */ tal_log_set_manage_attr(PRINT_LOG_LEVEL); /* Initialization device */ TAL_PR_DEBUG("device_init in"); TUYA_CALL_ERR_LOG(__soc_device_init()); return; } /** * @brief task thread * * @param[in] arg:Parameters when creating a task * @return none */ STATIC VOID_T tuya_app_thread(VOID_T *arg) { /* Initialization LWIP first!!! */ #if defined(ENABLE_LWIP) && (ENABLE_LWIP == 1) TUYA_LwIP_Init(); #endif user_main(); tal_thread_delete(ty_app_thread); ty_app_thread = NULL; } /** * @brief user entry function * * @param[in] none: * * @return none */ #if OPERATING_SYSTEM == SYSTEM_LINUX INT_T main(INT_T argc, CHAR_T **argv) #else VOID_T tuya_app_main(VOID) #endif { THREAD_CFG_T thrd_param = {4096, 4, "tuya_app_main"}; tal_thread_create_and_start(&ty_app_thread, NULL, NULL, tuya_app_thread, NULL, &thrd_param); #if OPERATING_SYSTEM == SYSTEM_LINUX while (1) { tal_system_sleep(1000); } #endif } 这是刚刚那段日志的主函数代码,将上面日志和此处代码对比,分析,找出问题所在出。以及如何修改。尽量详细
最新发布
01-01
ECOVRY_MSG_TYPE_SET_GLOBAL_STATUS: #### msg.data.poe_r 上面是串口打印信息 /*! Copyright(c) 2008-2013 Shenzhen TP-LINK Technologies Co.Ltd. * *\file http_poeOut.c *\brief *\details * *\author Hu Haiming *\version *\date 2Sep21 * *\warning * *\history \arg */ /**************************************************************************************************/ /* CONFIGURATIONS */ /**************************************************************************************************/ /**************************************************************************************************/ /* INCLUDE_FILES */ /**************************************************************************************************/ #include <stdio.h> #include <unistd.h> #include <sys/ioctl.h> #include <sys/socket.h> #include <sys/sysinfo.h> #include <net/if.h> #include <netinet/if_ether.h> #include <netinet/in.h> #include <arpa/inet.h> #include <fcntl.h> #include "http_file.h" #include "http_io.h" #include "http_data.h" #include "http_auth.h" #include "cJSON.h" #include "wrp_com_op.h" #include "dev_config.h" #include "http_poeOut.h" /**************************************************************************************************/ /* DEFINES */ /**************************************************************************************************/ /**************************************************************************************************/ /* TYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* EXTERN_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_PROTOTYPES */ /**************************************************************************************************/ /**************************************************************************************************/ /* VARIABLES */ /**************************************************************************************************/ /**************************************************************************************************/ /* LOCAL_FUNCTIONS */ /**************************************************************************************************/ static void _poeOutJsonWrite(HTTP_DATATEMP *pData, LAN_PORT_ENTRY *poeOutCfgData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; if (NULL == poeOutCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateObject(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); if (poeOutCfgData->poe_out_bitmap) { cJSON_AddStringToObject(poeOutInfo, "enable", "on"); } else { cJSON_AddStringToObject(poeOutInfo, "enable", "off"); } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS getPoeOutJson(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; LAN_PORT_ENTRY info; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); wrpRet = wrpOpDo(UCL_OPID_LAN_PORT_GET_INFO, NULL, 0, &info, sizeof(LAN_PORT_ENTRY), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _poeOutJsonWrite(pData, &info, status, OK); return RPM_DONE; } static STATUS _parsePoeCfgSet(HTTP_DATATEMP *pData, POE_GLOBAL_CFG_STRUCT_UC *poeOutCfgData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == poeOutCfgData) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "data"); return ERROR; } printf("_parsePoeCfgSet data str:%s \n ", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*limit*/ pValue = cJSON_GetObjectItem(pRoot, "limit"); if (NULL == pValue || (pValue->type != cJSON_String) || 0 == strlen(pValue->valuestring)) { printf("module(%s) not present,use default \n", "0"); poeOutCfgData->sys_power_limit = 0; } else { poeOutCfgData->sys_power_limit = atof(pValue->valuestring) * 10; } return OK; } //ok static void _poeCfgJsonWrite(HTTP_DATATEMP *pData, POE_GLOBAL_CFG_STRUCT_UC *poeOutCfgData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; cJSON *poeOutInfo_Array = NULL; char *out = NULL; if (NULL == poeOutCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateObject(); poeOutInfo_Array = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo_Array); cJSON_AddNumberToObject(poeOutInfo, "id", 0); //UCL_OPID_MONITORAP_GET_DEVINFO cJSON_AddStringToObject(poeOutInfo, "device_name", "EAP650GP-Desktop"); //UCL_OPID_MONITORAP_GET_DEVINFO cJSON_AddNumberToObject(poeOutInfo, "min", 4); cJSON_AddNumberToObject(poeOutInfo, "max", (float)poeOutCfgData->sys_power_max/10.0); cJSON_AddNumberToObject(poeOutInfo, "limit", (float)poeOutCfgData->sys_power_limit/10.0); cJSON_AddNumberToObject(poeOutInfo, "consumption", (float)poeOutCfgData->sys_actual_cons/10.0); if(poeOutCfgData->sys_power_limit < poeOutCfgData->sys_actual_cons) { cJSON_AddNumberToObject(poeOutInfo, "remain", 0); } else { cJSON_AddNumberToObject(poeOutInfo, "remain", (float)(poeOutCfgData->sys_power_limit - poeOutCfgData->sys_actual_cons)/10.0); } cJSON_AddBoolToObject(poeOutInfo, "total_overload", poeOutCfgData->sys_overload == 1); cJSON_AddItemToArray(poeOutInfo_Array, poeOutInfo); out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS poe_cfg_get(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_GLOBAL_CFG_STRUCT_UC info_new; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_GET_ENABLE, NULL, 0, &info_new, sizeof(POE_GLOBAL_CFG_STRUCT_UC), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } #ifdef WXHTESTop_poeRecovery printf("####^^^^ in HTTP_STATUS poe_cfg_get() execute UCL_OPID_POE_RECOVRY_GET_ENABLE\n"); WRP_RET wrpRet2 = WRP_OK; POE_RECOVERY_STATUS_UC info_new2; memset(&info_new2, 0, sizeof(info_new2)); wrpRet2 = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_ENABLE, NULL, 0, &info_new2, sizeof(POE_RECOVERY_STATUS_UC), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet2) { printf("####^^^^ wrpOpDo(UCL_OPID_POE_RECOVRY_GET_ENABLE, ) failed\n"); } else { printf("####^^^^ wrpOpDo(UCL_OPID_POE_RECOVRY_GET_ENABLE, ) Done!!!!!!!!!!\n"); } #endif _poeCfgJsonWrite(pData, &info_new, status, OK); //ok return RPM_DONE; } HTTP_STATUS poe_cfg_set(HTTP_CONTEXT *pContext) { // STATUS status = OK; // WRP_RET wrpRet = WRP_OK; // LAN_PORT_ENTRY info; POE_GLOBAL_CFG_STRUCT_UC info_new; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, poe_cfg_get); //ok _parsePoeCfgSet(pData, &info_new); //half ok. only set limit now.need prd check wrpOpDo(UCL_OPID_POE_SET_ENABLE, &info_new, sizeof(POE_GLOBAL_CFG_STRUCT_UC), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); return poe_cfg_get(pContext); } //ok static STATUS _parsePortCfgSet(HTTP_DATATEMP *pData, POE_PORT_CFG_STRUCT_UC *portCfgData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == portCfgData) { return ERROR; } pStr = httpGetEnv(pData, "id"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("_parsePortCfgSet data str:%s \n", pStr); portCfgData->port_no = atoi(pStr); pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("_parsePortCfgSet data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*poe_profile_no*/ pValue = cJSON_GetObjectItem(pRoot, "poe_profile"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); } else if(pValue->valuestring[0] == &#39;\0&#39; || (0 == strcmp(pValue->valuestring, "default-none"))) { portCfgData->poe_profile_no = 0; } else { int index = 0; for(index = 0; index < MAX_POE_PROFILE_NUM; index++) { if( profileData[index].profile_id == atoi(pValue->valuestring)) { break; } if( index == MAX_POE_PROFILE_NUM -1) { printf("match profile failed \n"); return RPM_ERROR; } } portCfgData->poe_profile_no = atoi(pValue->valuestring); portCfgData->poe_enable = profileData[index].poe_enable; portCfgData->priority = profileData[index].priority; portCfgData->power_limit = profileData[index].power_limit; portCfgData->power_limit_class = profileData[index].power_limit_class; return OK; } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); portCfgData->poe_enable = POE_DISABLE; } else { if (0 == strcmp(pValue->valuestring, "on")) { portCfgData->poe_enable = POE_ENABLE; } else if(0 == strcmp(pValue->valuestring, "off")) { portCfgData->poe_enable = POE_DISABLE; } } /*priority*/ pValue = cJSON_GetObjectItem(pRoot, "priority"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "priority"); portCfgData->priority = POE_PRI_LOW_AD; } else { if (0 == strcmp(pValue->valuestring, "low")) { portCfgData->priority = POE_PRI_LOW_AD; } else if (0 == strcmp(pValue->valuestring, "middle")) { portCfgData->priority = POE_PRI_MIDDLE_AD; } else if (0 == strcmp(pValue->valuestring, "high")) { portCfgData->priority = POE_PRI_HIGH_AD; } else { printf("module(%s)2 not present ,use default", "priority"); portCfgData->priority = POE_PRI_LOW_AD; } } /*power_limit*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit"); portCfgData->power_limit = 0; } else { if (0 == strcmp(pValue->valuestring, "auto")) { portCfgData->power_limit = POE_CLASS_CLASS3_AD; portCfgData->power_limit_class = POE_CLASS_AUTO_AD; } else if(0 == strcmp(pValue->valuestring, "class1")) { portCfgData->power_limit = POE_CLASS_CLASS1_AD; portCfgData->power_limit_class = POE_CLASS1_AD; } else if(0 == strcmp(pValue->valuestring, "class2")) { portCfgData->power_limit = POE_CLASS_CLASS2_AD; portCfgData->power_limit_class = POE_CLASS2_AD; } else if(0 == strcmp(pValue->valuestring, "class3")) { portCfgData->power_limit = POE_CLASS_CLASS3_AD; portCfgData->power_limit_class = POE_CLASS3_AD; } // we cant set class4? // else if(0 == strcmp(pValue->valuestring, "class4")) // { // portCfgData->power_limit = POE_CLASS_CLASS4_AD; // } else if(0 == strcmp(pValue->valuestring, "manual")) { /*power_limit_value*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit_value"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit_value"); portCfgData->power_limit = POE_CLASS_CLASS3_AD; portCfgData->power_limit_class = POE_CLASS_MANUAL_AD; } else { portCfgData->power_limit = (int)(atof(pValue->valuestring)*10); portCfgData->power_limit_class = POE_CLASS_MANUAL_AD; } } } return OK; } static int _composePortcfg(const POE_PORT_CFG_STRUCT_UC *portCfgData,cJSON *pEntry,int idx) { /*index*/ cJSON_AddNumberToObject(pEntry, "id", portCfgData->port_no); /* port */ cJSON_AddNumberToObject(pEntry, "port", portCfgData->port_no); /* poe_profile */ if(portCfgData->poe_profile_no == 0) { cJSON_AddStringToObject(pEntry, "poe_profile", "default-none"); //todo } else { cJSON_AddNumberToObject(pEntry, "poe_profile", portCfgData->poe_profile_no); //todo } /*power_limit*/ cJSON_AddNumberToObject(pEntry, "power_limit_value", (float)(portCfgData->power_limit)/10.0); if(portCfgData->power_limit == POE_CLASS_CLASS1_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class1"); else if(portCfgData->power_limit == POE_CLASS_CLASS2_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class2"); else if(portCfgData->power_limit == POE_CLASS_CLASS3_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class3"); else if(portCfgData->power_limit == POE_CLASS_CLASS4_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class4"); else cJSON_AddStringToObject(pEntry, "power_limit", "manual"); /*poe_enable*/ /*status*/ if(portCfgData->poe_enable == POE_ENABLE) cJSON_AddStringToObject(pEntry, "status", "on"); else cJSON_AddStringToObject(pEntry, "status", "off"); /*priority*/ if(portCfgData->priority == POE_PRI_LOW_AD) cJSON_AddStringToObject(pEntry, "priority", "low"); else if(portCfgData->priority == POE_PRI_MIDDLE_AD) cJSON_AddStringToObject(pEntry, "priority", "middle"); else if(portCfgData->priority == POE_PRI_HIGH_AD) cJSON_AddStringToObject(pEntry, "priority", "high"); else cJSON_AddStringToObject(pEntry, "priority", "error"); //dbg //not support cJSON_AddStringToObject(pEntry, "time_range", "any"); cJSON_AddNumberToObject(pEntry, "current", (float)(portCfgData->actual_cur)); cJSON_AddNumberToObject(pEntry, "voltage", (float)(portCfgData->actual_vol)/10.0); cJSON_AddNumberToObject(pEntry, "power", (float)(portCfgData->actual_cons)/10.0); if(portCfgData->pd_class == POE_CLASS_CLASS1_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 1); else if(portCfgData->pd_class == POE_CLASS_CLASS2_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 2); else if(portCfgData->pd_class == POE_CLASS_CLASS3_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 3); else if(portCfgData->pd_class == POE_CLASS_CLASS4_AD) cJSON_AddNumberToObject(pEntry, "pd_class", 4); else cJSON_AddNumberToObject(pEntry, "pd_class", 0); if(portCfgData->actual_cons > 0) { cJSON_AddStringToObject(pEntry, "power_status", "on"); } else { cJSON_AddStringToObject(pEntry, "power_status", "off"); } cJSON_AddBoolToObject(pEntry, "port_overload", portCfgData->port_overload == 1); return OK; } static void _portCfgJsonWrite(HTTP_DATATEMP *pData, POE_PORT_CFG_STRUCT_UC *portCfgData, POE_PROFILE_STRUCT_UC *profileData,int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; cJSON *othersInfo = NULL; char *out = NULL; int idx = 0; if (NULL == portCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } othersInfo = cJSON_CreateObject(); if (NULL == othersInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); cJSON_AddItemToObject(root, "others", othersInfo); for (idx = 0; portCfgData != NULL && idx < MAX_POE_PORT_NUM; idx ++) { cJSON *entry = cJSON_CreateObject(); status = _composePortcfg(portCfgData,entry,idx); if(status != OK) { return; } cJSON_AddItemToArray(poeOutInfo, entry); portCfgData++; } cJSON *profileOptions = cJSON_CreateArray(); for (idx = 0; profileData != NULL && profileData->profileName[0] != &#39;\0&#39; && idx < MAX_POE_PROFILE_NUM; idx ++) { cJSON *entry = cJSON_CreateObject(); cJSON_AddNumberToObject(entry, "profile_id", profileData->profile_id); cJSON_AddStringToObject(entry, "profile_name", (void*)profileData->profileName); cJSON_AddItemToArray(profileOptions, entry); profileData++; } cJSON_AddItemToObject(othersInfo, "profile_option", profileOptions); out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS port_cfg_get(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PORT_CFG_STRUCT_UC info_new[MAX_POE_PORT_NUM]; POE_PROFILE_STRUCT_UC profile_info[MAX_POE_PROFILE_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_GET_PORT_CONFIG, NULL, 0, info_new, sizeof(POE_PORT_CFG_STRUCT_UC) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } // @wxh debug 测试 UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG #ifdef WXHTESTop_poeRecovery printf("####^^^^ in HTTP_STATUS port_cfg_get() execute UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG\n"); WRP_RET wrpRet2 = WRP_OK; POE_PORTRECOVERYCFG_STRUCT info_new2[MAX_POE_PORT_NUM]; memset(&info_new2, 0, sizeof(info_new2)); wrpRet2 = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, NULL, 0, info_new2, sizeof(POE_PORTRECOVERYCFG_STRUCT) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet2) { printf("####^^^^ wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, ) failed\n"); } else { printf("####^^^^ wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, ) Done!!!!!!!!!!\n"); } #endif memset(profile_info, 0, sizeof(profile_info)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, profile_info, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _portCfgJsonWrite(pData, info_new, profile_info, status, OK); //ok return RPM_DONE; } HTTP_STATUS port_cfg_set(HTTP_CONTEXT *pContext) { WRP_RET wrpRet = WRP_OK; POE_PORT_CFG_STRUCT_UC info_new; POE_PROFILE_STRUCT_UC info_profile[MAX_POE_PROFILE_NUM]; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, port_cfg_get); //todo memset(info_profile, 0, sizeof(info_profile)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, info_profile, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { printf("UCL_OPID_POE_PROFILE_GET ERROR\n"); return RPM_ERROR; } _parsePortCfgSet(pData, &info_new, info_profile); //ok wrpRet = wrpOpDo(UCL_OPID_POE_SET_PORT_CONFIG, &info_new, sizeof(POE_PORT_CFG_STRUCT_UC), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) { printf("UCL_OPID_POE_SET_PORT_CONFIG ERROR\n"); return RPM_ERROR; } return port_cfg_get(pContext); } //ok static void _recoveryGlobalJsonWrite(HTTP_DATATEMP *pData, POE_RECOVERY_STATUS_UC *poeOutCfgData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; if (NULL == poeOutCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateObject(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); /* typedef enum { POE_RECOVERY_DISABLE_UC = 0, POE_RECOVERY_ENABLE_UC }POE_RECOVERY_STATUS_UC; */ if( *poeOutCfgData == POE_RECOVERY_ENABLE_UC) { cJSON_AddStringToObject(poeOutInfo, "global", "on"); } else if( *poeOutCfgData == POE_RECOVERY_DISABLE_UC) { cJSON_AddStringToObject(poeOutInfo, "global", "off"); } else { printf("http_poeOut (%s) read wrong param,use default", "_recoveryGlobalJsonWrite"); cJSON_AddStringToObject(poeOutInfo, "global", "off"); } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS recovery_global_get(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_RECOVERY_STATUS_UC info; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_ENABLE, NULL, 0, &info, sizeof(POE_RECOVERY_STATUS_UC), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _recoveryGlobalJsonWrite(pData, &info, status, OK); //ok return RPM_DONE; } static STATUS _recoveryGlobalCfgSet(HTTP_DATATEMP *pData, POE_RECOVERY_STATUS_UC *poeOutCfgData) { printf("####&&&& goto [%s]\n",__FUNCTION__); char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == poeOutCfgData) { return ERROR; } pStr = httpGetEnv(pData, "data"); if (NULL == pStr) { printf("failed to get param str:%s", "data"); return ERROR; } printf("_recoveryGlobalCfgSet data str:%s", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*global*/ pValue = cJSON_GetObjectItem(pRoot, "global"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "global"); printf("####&&&& module(%s) not present,use default \n", "global"); *poeOutCfgData = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { printf("####&&&& in [%s] pData: pValue->valuestring : on\n",__FUNCTION__); *poeOutCfgData = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { printf("####&&&& in [%s] pData: pValue->valuestring : off\n",__FUNCTION__); *poeOutCfgData = POE_RECOVERY_DISABLE_UC; } else { printf("####&&&& module(%s)2 not present,use default", "global"); printf("module(%s)2 not present,use default", "global"); *poeOutCfgData = POE_RECOVERY_DISABLE_UC; } } return OK; } HTTP_STATUS recovery_global_set(HTTP_CONTEXT *pContext) { printf("####&&&& goto [%s]\n",__FUNCTION__); STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_RECOVERY_STATUS_UC info; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; printf("####&&&& in [%s]\n",__FUNCTION__); RPM_POST_2_OTHER_OP(pContext, "load", NULL, recovery_global_get); //ok printf("####&&&& in [%s] have done functionRPM_POST_2_OTHER_OP \n",__FUNCTION__); _recoveryGlobalCfgSet(pData, &info); //ok wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_SET_ENABLE, &info, sizeof(POE_RECOVERY_STATUS_UC), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) { status = ERROR; } else { status = OK; } wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_ENABLE, NULL, 0, &info, sizeof(POE_RECOVERY_STATUS_UC), WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _recoveryGlobalJsonWrite(pData, &info, status, err); //ok return RPM_DONE; } static int _composeRecoveryPortcfg(const POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData,cJSON *pEntry, int idx) { /* index */ cJSON_AddNumberToObject(pEntry, "id", idx); /*port_no*/ cJSON_AddNumberToObject(pEntry, "port", recoveryPortCfgData->port_no); /*status */ if(POE_RECOVERY_ENABLE_UC == recoveryPortCfgData->port_no) cJSON_AddStringToObject(pEntry, "status", "on"); else //(POE_RECOVERY_DISABLE_UC == recoveryPortCfgData->port_no) cJSON_AddStringToObject(pEntry, "status", "off"); /*ip*/ cJSON_AddStringToObject(pEntry, "ip", recoveryPortCfgData->ip); /*startup*/ cJSON_AddNumberToObject(pEntry, "startup", recoveryPortCfgData->startup); /*interval*/ cJSON_AddNumberToObject(pEntry, "interval", recoveryPortCfgData->interval); /*failure_threshold*/ cJSON_AddNumberToObject(pEntry, "failure_threshold", recoveryPortCfgData->failure); /*break_time*/ cJSON_AddNumberToObject(pEntry, "break_time", recoveryPortCfgData->reboot); /*failures*/ cJSON_AddNumberToObject(pEntry, "failures", recoveryPortCfgData->failure); /*reboots*/ cJSON_AddNumberToObject(pEntry, "reboots", recoveryPortCfgData->restart); /*total*/ cJSON_AddNumberToObject(pEntry, "total", recoveryPortCfgData->reboot); return OK; } static void _recoveryPortCfgJsonWrite(HTTP_DATATEMP *pData, POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; int idx = 0; if (NULL == recoveryPortCfgData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); for (idx = 0; recoveryPortCfgData != NULL && idx < MAX_POE_PORT_NUM; idx ++) { cJSON *entry = cJSON_CreateObject(); status = _composeRecoveryPortcfg(recoveryPortCfgData,entry,idx); //ok if(status != OK) { return; } cJSON_AddItemToArray(poeOutInfo, entry); recoveryPortCfgData++; } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS recovery_port_cfg_get(HTTP_CONTEXT *pContext) { printf("####&&&& goto [%s]\n",__FUNCTION__); STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PORTRECOVERYCFG_STRUCT info[MAX_POE_PORT_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, NULL, 0, info, sizeof(POE_PORTRECOVERYCFG_STRUCT) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _recoveryPortCfgJsonWrite(pData, info, status, OK); //ok return RPM_DONE; } static STATUS _parseRecoveryPortCfgSet(HTTP_DATATEMP *pData, POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == recoveryPortCfgData) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s\n", "new"); return ERROR; } printf("_parseRecoveryPortCfgSet data str:%s\n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s\n", pStr); return ERROR; } /*port*/ pValue = cJSON_GetObjectItem(pRoot, "port"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR \n", "port"); return ERROR; } else { recoveryPortCfgData->port_no = atoi(pValue->valuestring); } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); recoveryPortCfgData->status = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { recoveryPortCfgData->status = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { recoveryPortCfgData->status = POE_RECOVERY_DISABLE_UC; } } /*ip*/ pValue = cJSON_GetObjectItem(pRoot, "ip"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR \n", "ip"); return ERROR; } else { snprintf(recoveryPortCfgData->ip,IP_STR_LEN,pValue->valuestring); } /*startup*/ pValue = cJSON_GetObjectItem(pRoot, "startup"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "startup"); return ERROR; } else { recoveryPortCfgData->startup = atoi(pValue->valuestring); } /*interval*/ pValue = cJSON_GetObjectItem(pRoot, "interval"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "interval"); return ERROR; } else { recoveryPortCfgData->interval = atoi(pValue->valuestring); } /*failure_threshold*/ pValue = cJSON_GetObjectItem(pRoot, "failure_threshold"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "failure_threshold"); return ERROR; } else { recoveryPortCfgData->retry = atoi(pValue->valuestring); } /*break_time*/ pValue = cJSON_GetObjectItem(pRoot, "break_time"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR\n", "break_time"); return ERROR; } else { recoveryPortCfgData->reboot = atoi(pValue->valuestring); } return OK; } static STATUS _parseRecoveryPortCfgDel(HTTP_DATATEMP *pData, int *index) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == index) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("data str:%s", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*index*/ pValue = cJSON_GetObjectItem(pRoot, "index"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("input(%s) is ERROR", "index"); return ERROR; } else { *index = atoi(pValue->valuestring); } return OK; } HTTP_STATUS recovery_port_cfg_set(HTTP_CONTEXT *pContext) { printf("####&&&& goto [%s]\n",__FUNCTION__); STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PORTRECOVERYCFG_STRUCT info[MAX_POE_PORT_NUM]; int info_delIndex = 0; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, recovery_port_cfg_get); char* pStr = httpGetEnv(pContext->pHttp_datatemp,"operation"); if( 0 == strcmp(pStr, "add")) { _parseRecoveryPortCfgSet(pData, info); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_ADD_PORT_CONFIG, info, sizeof(POE_PORTRECOVERYCFG_STRUCT), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else if( 0 == strcmp(pStr, "set")) { _parseRecoveryPortCfgSet(pData, info); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_SET_PORT_CONFIG, info, sizeof(POE_PORTRECOVERYCFG_STRUCT), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else if( 0 == strcmp(pStr, "delete")) { _parseRecoveryPortCfgDel(pData, (void*)&info_delIndex); wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_DEL_PORT_CONFIG, (void*)&info_delIndex, sizeof(int), &err, sizeof(UINT32), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else { printf("recovery_port_cfg_set operation (%s) is ERROR", pStr); return ERROR; } wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, NULL, 0, info, sizeof(POE_PORTRECOVERYCFG_STRUCT) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _recoveryPortCfgJsonWrite(pData, info, status, OK); //ok return RPM_DONE; } static int _composeProfilecfg(const POE_PROFILE_STRUCT_UC *profileData,cJSON *pEntry,int idx) { /* index */ cJSON_AddNumberToObject(pEntry, "id", profileData->profile_id); /*profile_name*/ cJSON_AddStringToObject(pEntry, "profile_name", (void*)profileData->profileName); /*poe_enable*/ /*status*/ if(profileData->poe_enable == POE_ENABLE) cJSON_AddStringToObject(pEntry, "status", "on"); else cJSON_AddStringToObject(pEntry, "status", "off"); /*priority*/ if(profileData->priority == POE_PRI_LOW_AD) cJSON_AddStringToObject(pEntry, "priority", "low"); else if(profileData->priority == POE_PRI_MIDDLE_AD) cJSON_AddStringToObject(pEntry, "priority", "middle"); else if(profileData->priority == POE_PRI_HIGH_AD) cJSON_AddStringToObject(pEntry, "priority", "high"); else cJSON_AddStringToObject(pEntry, "priority", "error"); /*power_limit*/ cJSON_AddNumberToObject(pEntry, "power_limit_value", (float)profileData->power_limit/10.0); if(profileData->power_limit_class == POE_CLASS0_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class0"); else if(profileData->power_limit_class == POE_CLASS1_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class1"); else if(profileData->power_limit_class == POE_CLASS2_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class2"); else if(profileData->power_limit_class == POE_CLASS3_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class3"); else if(profileData->power_limit_class == POE_CLASS4_AD) cJSON_AddStringToObject(pEntry, "power_limit", "class4"); else if(profileData->power_limit_class == POE_CLASS_AUTO_AD) cJSON_AddStringToObject(pEntry, "power_limit", "auto"); else if(profileData->power_limit_class == POE_CLASS_MANUAL_AD) cJSON_AddStringToObject(pEntry, "power_limit", "manual"); cJSON_AddBoolToObject(pEntry, "in_use", profileData->profile_in_use); return OK; } static void _profileJsonWrite(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData, int status, int err) { cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; int idx = 0; if (NULL == profileData) { return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); for (idx = 0; profileData != NULL && idx < MAX_POE_PROFILE_NUM; idx ++) { if(profileData->profileName[0] == &#39;\0&#39;) { profileData++; continue; } cJSON *entry = cJSON_CreateObject(); status = _composeProfilecfg(profileData,entry,idx); //ok if(status != OK) { return; } cJSON_AddItemToArray(poeOutInfo, entry); profileData++; } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS profile_get(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PROFILE_STRUCT_UC info_new[MAX_POE_PROFILE_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, info_new, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _profileJsonWrite(pData, info_new, status, OK); //ok return RPM_DONE; } HTTP_STATUS profile_get_afterDelFailed(HTTP_CONTEXT *pContext) { STATUS status = ERROR; WRP_RET wrpRet = WRP_OK; POE_PROFILE_STRUCT_UC info_new[MAX_POE_PROFILE_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(info_new, 0, sizeof(info_new)); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_GET, NULL, 0, info_new, sizeof(POE_PROFILE_STRUCT_UC) * MAX_POE_PROFILE_NUM, WRP_TRAN_TYPE_READ); if (WRP_OK != wrpRet) { status = ERROR; } _profileJsonWrite(pData, info_new, status, 4001); //ok return RPM_DONE; } static STATUS _parseProfileAdd(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == profileData) { return ERROR; } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } // doesnt have profile id when add new profile_id // /* profile_id */ // if (NULL == pValue || (pValue->type != cJSON_Number)) // { // printf("module(%s) not present,use default \n", "profile_id"); // // return ERROR; // } // else // { // profileData->profile_id = pValue->valueint; // } /*profile_name*/ pValue = cJSON_GetObjectItem(pRoot, "profile_name"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "profile_name"); return ERROR; } else { snprintf((char *)profileData->profileName,strlen(pValue->valuestring)+1,pValue->valuestring); } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { profileData->poe_enable = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } } /*priority*/ pValue = cJSON_GetObjectItem(pRoot, "priority"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "priority"); profileData->priority = POE_PRI_LOW_AD; } else { if (0 == strcmp(pValue->valuestring, "low")) { profileData->priority = POE_PRI_LOW_AD; } else if (0 == strcmp(pValue->valuestring, "middle")) { profileData->priority = POE_PRI_MIDDLE_AD; } else if (0 == strcmp(pValue->valuestring, "high")) { profileData->priority = POE_PRI_HIGH_AD; } else { printf("module(%s)2 not present ,use default", "priority"); profileData->priority = POE_PRI_LOW_AD; } } /*power_limit*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit"); profileData->power_limit = 0; } else { if (0 == strcmp(pValue->valuestring, "auto")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS_AUTO_AD; } else if(0 == strcmp(pValue->valuestring, "class1")) { profileData->power_limit = POE_CLASS_CLASS1_AD; profileData->power_limit_class = POE_CLASS1_AD; } else if(0 == strcmp(pValue->valuestring, "class2")) { profileData->power_limit = POE_CLASS_CLASS2_AD; profileData->power_limit_class = POE_CLASS2_AD; } else if(0 == strcmp(pValue->valuestring, "class3")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS3_AD; } //need check:can we set class4? // else if(0 == strcmp(pValue->valuestring, "class4")) // { // profileData->power_limit = POE_CLASS_CLASS4_AD; // profileData->power_limit_class = POE_CLASS4_AD; // } else if(0 == strcmp(pValue->valuestring, "manual")) { /*power_limit_value*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit_value"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit_value"); profileData->power_limit = 0; profileData->power_limit_class = POE_CLASS_MANUAL_AD; } else { profileData->power_limit = (int)(atof(pValue->valuestring)*10); profileData->power_limit_class = POE_CLASS_MANUAL_AD; } } } return OK; } static STATUS _parseProfileMod(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; cJSON *pRoot = NULL; cJSON *pValue = NULL; if (NULL == profileData) { return ERROR; } pStr = httpGetEnv(pData, "old"); if (NULL == pStr) { printf("failed to get param str:%s", "old"); return ERROR; } printf("data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /* profile_id */ pValue = cJSON_GetObjectItem(pRoot, "id"); if (NULL == pValue || (pValue->type != cJSON_Number)) { printf("module(%s) not present,use default \n", "profile_id"); // return ERROR; } else { profileData->profile_id = pValue->valueint; } /*profile_name*/ pValue = cJSON_GetObjectItem(pRoot, "profile_name"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "profile_name"); return ERROR; } else { snprintf((char *)profileData->profileName,strlen(pValue->valuestring)+1,pValue->valuestring); } pStr = httpGetEnv(pData, "new"); if (NULL == pStr) { printf("failed to get param str:%s", "new"); return ERROR; } printf("data str:%s \n", pStr); pRoot = cJSON_Parse(pStr); if (NULL == pRoot) { printf("failed to parse json str:%s", pStr); return ERROR; } /*status*/ pValue = cJSON_GetObjectItem(pRoot, "status"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "status"); profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } else { if (0 == strcmp(pValue->valuestring, "on")) { profileData->poe_enable = POE_RECOVERY_ENABLE_UC; } else if(0 == strcmp(pValue->valuestring, "off")) { profileData->poe_enable = POE_RECOVERY_DISABLE_UC; } } /*priority*/ pValue = cJSON_GetObjectItem(pRoot, "priority"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "priority"); profileData->priority = POE_PRI_LOW_AD; } else { if (0 == strcmp(pValue->valuestring, "low")) { profileData->priority = POE_PRI_LOW_AD; } else if (0 == strcmp(pValue->valuestring, "middle")) { profileData->priority = POE_PRI_MIDDLE_AD; } else if (0 == strcmp(pValue->valuestring, "high")) { profileData->priority = POE_PRI_HIGH_AD; } else { printf("module(%s)2 not present ,use default", "priority"); profileData->priority = POE_PRI_LOW_AD; } } /*power_limit*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit"); profileData->power_limit = 0; } else { if (0 == strcmp(pValue->valuestring, "auto")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS_AUTO_AD; } else if(0 == strcmp(pValue->valuestring, "class1")) { profileData->power_limit = POE_CLASS_CLASS1_AD; profileData->power_limit_class = POE_CLASS1_AD; } else if(0 == strcmp(pValue->valuestring, "class2")) { profileData->power_limit = POE_CLASS_CLASS2_AD; profileData->power_limit_class = POE_CLASS2_AD; } else if(0 == strcmp(pValue->valuestring, "class3")) { profileData->power_limit = POE_CLASS_CLASS3_AD; profileData->power_limit_class = POE_CLASS3_AD; } //need check:can we set class4? // else if(0 == strcmp(pValue->valuestring, "class4")) // { // profileData->power_limit = POE_CLASS_CLASS4_AD; // profileData->power_limit_class = POE_CLASS4_AD; // } else if(0 == strcmp(pValue->valuestring, "manual")) { /*power_limit_value*/ pValue = cJSON_GetObjectItem(pRoot, "power_limit_value"); if (NULL == pValue || (pValue->type != cJSON_String)) { printf("module(%s) not present,use default \n", "power_limit_value"); profileData->power_limit = 0; profileData->power_limit_class = POE_CLASS_MANUAL_AD; } else { profileData->power_limit = (int)(atof(pValue->valuestring)*10); profileData->power_limit_class = POE_CLASS_MANUAL_AD; } } } return OK; } static STATUS _parseProfileDel(HTTP_DATATEMP *pData, POE_PROFILE_STRUCT_UC *profileData) { char *pStr = NULL; if (NULL == profileData) { return ERROR; } /* profile_name */ pStr = httpGetEnv(pData, "profile_name"); if (NULL == pStr) { printf("failed to get param str:%s\n", "profile_name"); return ERROR; } printf("_parseProfileDel data str:%s \n", pStr); snprintf((char *)profileData->profileName,strlen(pStr)+1,pStr); /* profile_id */ pStr = httpGetEnv(pData, "id"); if (NULL == pStr) { printf("failed to get param str:%s \n", "profile_id"); return ERROR; } printf("_parseProfileDel data str:%s \n", pStr); profileData->profile_id = atoi(pStr); return OK; } // profile_set HTTP_STATUS profile_set(HTTP_CONTEXT *pContext) { STATUS status = OK; WRP_RET wrpRet = WRP_OK; POE_PROFILE_STRUCT_UC info[MAX_POE_PORT_NUM]; // int info_delIndex = 0; UINT32 err = 0; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; RPM_POST_2_OTHER_OP(pContext, "load", NULL, profile_get); char* pStr = httpGetEnv(pContext->pHttp_datatemp,"operation"); if( 0 == strcmp(pStr, "insert")) { _parseProfileAdd(pData, info); //ok wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_ADD, info, sizeof(POE_PROFILE_STRUCT_UC), &err, sizeof(int), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else if( 0 == strcmp(pStr, "update")) { _parseProfileMod(pData, info); //ok wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_MOD, info, sizeof(POE_PROFILE_STRUCT_UC), &err, sizeof(int), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; } else if( 0 == strcmp(pStr, "remove")) { // _parseProfileDel(pData, (void*)&info_delIndex); _parseProfileDel(pData, info); //doing // wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_DEL, (void*)&info_delIndex, sizeof(int), // &err, sizeof(int), WRP_TRAN_TYPE_WRITE); wrpRet = wrpOpDo(UCL_OPID_POE_PROFILE_DEL, info, sizeof(POE_PROFILE_STRUCT_UC), &err, sizeof(int), WRP_TRAN_TYPE_WRITE); if (WRP_OK != wrpRet) status = ERROR; else status = OK; if(err!=UCL_OK){ return profile_get_afterDelFailed(pContext); } } else { printf("profile_set operation (%s) is ERROR \n", pStr); return ERROR; } wrpRet = status; return profile_get(pContext); } // todo // @wxh poe_port_status.json poe_port_status_get static void _recoveryPortStatusJsonWrite(HTTP_DATATEMP *pData, POE_PORTRECOVERYCFG_STRUCT *recoveryPortCfgData, int status, int err) { printf("####&&&& goto [%s]\n",__FUNCTION__); cJSON *root = NULL; cJSON *poeOutInfo = NULL; char *out = NULL; int i = 0; if (NULL == recoveryPortCfgData) { printf("####&&&& [%s] goto NULL == recoveryPortCfgData then return\n",__FUNCTION__); // return; } root = cJSON_CreateObject(); if (NULL == root) { return; } poeOutInfo = cJSON_CreateArray(); if (NULL == poeOutInfo) { cJSON_Delete(root); return; } cJSON_AddItemToObject(root, "success", cJSON_CreateBool(OK == status)); cJSON_AddNumberToObject(root, "error", err); cJSON_AddItemToObject(root, "data", poeOutInfo); for (i = 0; i < 2; i++) { cJSON *entry = cJSON_CreateObject(); cJSON_AddStringToObject(entry, "state", "connected"); cJSON_AddNumberToObject(entry, "port", i + 1); cJSON_AddItemToArray(poeOutInfo, entry); } out = cJSON_Print(root); if (NULL != out) { pData->contentLength = strlen(out); http_parser_makeHeader(pData, HTTP_OK); http_io_fwrite(pData->pDataPersist, out, strlen(out)); pData->outputState = HTTP_NONEED_RETURN; } cJSON_Delete(root); if (NULL != out) { free(out); } return; } HTTP_STATUS poe_port_status_get(HTTP_CONTEXT *pContext) { printf("####&&&& goto [%s]\n",__FUNCTION__); STATUS status = OK; // WRP_RET wrpRet = WRP_OK; POE_PORTRECOVERYCFG_STRUCT info[MAX_POE_PORT_NUM]; HTTP_DATATEMP *pData = NULL; if (pContext == NULL) return RPM_ERROR; pData = pContext->pHttp_datatemp; pData->outputState = HTTP_NEED_RETURN200; memset(&info, 0, sizeof(info)); // wrpRet = wrpOpDo(UCL_OPID_POE_RECOVRY_GET_PORT_CONFIG, NULL, 0, info, sizeof(POE_PORTRECOVERYCFG_STRUCT) * MAX_POE_PORT_NUM, WRP_TRAN_TYPE_READ); // if (WRP_OK != wrpRet) // { // status = ERROR; // } // _recoveryPortCfgJsonWrite(pData, info, status, OK); //ok _recoveryPortStatusJsonWrite(pData, info, status, OK); //ok return RPM_DONE; } /**************************************************************************************************/ /* PUBLIC_FUNCTIONS */ /**************************************************************************************************/ void http_managementPoeOutHandlerInit() { http_midware_getReg("/data/poe_cfg.json", poe_cfg_get, http_auth_check, NULL); http_midware_postReg("/data/poe_cfg.json", poe_cfg_set, http_auth_check, NULL); http_midware_getReg("/data/port_cfg.json", port_cfg_get, http_auth_check, NULL); http_midware_postReg("/data/port_cfg.json", port_cfg_set, http_auth_check, NULL); http_midware_getReg("/data/poe_profile.json", profile_get, http_auth_check, NULL); http_midware_postReg("/data/poe_profile.json", profile_set, http_auth_check, NULL); //not support recovery //@wxh testing http_midware_getReg("/data/poe_auto_recovery_global.json", recovery_global_get, http_auth_check, NULL); http_midware_postReg("/data/poe_auto_recovery_global.json", recovery_global_set, http_auth_check, NULL); //not support recovery //@wxh testing http_midware_getReg("/data/poe_auto_recovery_cfg.json", recovery_port_cfg_get, http_auth_check, NULL); http_midware_postReg("/data/poe_auto_recovery_cfg.json", recovery_port_cfg_set, http_auth_check, NULL); //@wxh not support now // http_midware_getReg("/data/poe_port_status.json", poe_port_status_get, http_auth_check, NULL); //只需要响应? http_midware_postReg("/data/poe_port_status.json", poe_port_status_get, http_auth_check, NULL); } 我在网页上勾选了poe auto recovery enable 然后save,这是哪里有问题
10-14
/****************************************************************************** * Copyright (c) 2018-2018 TP-Link Systems Inc. * * Filename: httpd_utils.c * Version: 1.0 * Description: httpd 模块功能函数 * Author: liyijie<liyijie@tp-link.com.cn> * Date: 2018-9-10 ******************************************************************************/ #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <string.h> #include <fcntl.h> #include <errno.h> #include <nvmp_common.h> #include <sys/stat.h> #include <sys/types.h> #include <sys/time.h> #include <time.h> #ifdef MAKEROOM_BEFORE_UPGRADE #include <sys/reboot.h> #endif #include "http_utils.h" #include "http_parser.h" #include "http_special_request.h" #include "httpd.h" #define NONCE_LIFETIME 30 #define HEADER_SIZE 2 * 1024 #define HTTP_SEND_RETRY_INTERVAL 20000 #define HTTP_RECV_RETRY_INTERVAL 20000 #define HTTP_IMG_CATCH_TIME 120 #define HTTP_UPGRADE_TIMEOUT 30 #define HTTP_MAX_VIRTUAL_FILE 10 #define HTTP_UNAUTHORIZED 401 #define ERROR_CRYPTO_FAIL -1 #define ERROR_BUFFER_OVERFLOW -2 #define ESC_CHAR &#39;%&#39; #define ESC_FIRST_CHAR &#39;2&#39; #define ESC_SPACE &#39;0&#39; #define ESC_PERCENT &#39;5&#39; #define ESC_DOUBLE_QUOTE &#39;2&#39; #define ESC_LEN 3 #define BEGIN_NUM &#39;0&#39; #define END_NUM &#39;9&#39; #define BEGIN_LOWERCASE &#39;a&#39; #define END_LOWERCASE &#39;z&#39; #define BEGIN_CAPITAL &#39;A&#39; #define END_CAPITAL &#39;Z&#39; #define END_LOWER_HEX &#39;f&#39; #define END_UPPER_HEX &#39;F&#39; #define TO_HEX_FIRST(x) ((x) / 16) > 9 ? (((x) / 16) + 55) : (((x) / 16) + 48); #define TO_HEX_SECOND(x) ((x) % 16) > 9 ? (((x) % 16) + 55) : (((x) % 16) + 48); LOCAL HTTP_ERROR_CODE g_http_err_code[] = { {HTTP_REQ_OK, "OK"}, {HTTP_MOVE_TEMPORARILY, "Moved Temporarily"}, {HTTP_NOT_MODIFIED, "Not Modified"}, {HTTP_BAD_REQUEST, "Bad Request"}, {HTTP_UNAUTHORIZED, "Unauthorized"}, {HTTP_FORBIDDEN, "Forbidden"}, {HTTP_NOT_FOUND, "Not Found"}, {HTTP_METHOD_NA, "Method Not Allowed"}, {HTTP_LENGTH_REQIRED, "Length Required"}, {HTTP_PRECOND_FAIL, "Precondition Failed"}, {HTTP_ENTITY_TOO_LARGE, "Request Entity Too Large"}, {HTTP_URI_TOO_LONG, "Request-URI Too Long"}, {HTTP_INTERNAL_ERROR, "Internal Server Error"}, {HTTP_NOT_IMPLEMENTED, "Not Implemented"}, {HTTP_SERV_UNAVAILABLE, "Service Unavailable"}, {HTTP_VERSION_NOT_SUPP, "HTTP Version not supported"}, {0, NULL} }; LOCAL char g_http_media_txt[HTTP_MEDIA_END][32] = { "text/html", "x-bin/octet-stream", "text/plain", "text/html", "text/html", /* HTTP_MEDIA_LOG */ "text/html", "text/xml", "image/gif", "image/jpeg", "text/css", "application/javascript", "application/x-zip-compressed", "application/json" }; extern UHTTPD_MAIN g_uhttp_main; char *http_get_err_str(U32 code) { U32 index = 0; while (NULL != g_http_err_code[index].value) { if (code == g_http_err_code[index].code) { return g_http_err_code[index].value; } index++; } return NULL; } LOCAL char *http_get_media_txt(CONTEXT *context) { if (NULL == context) { return NULL; } return g_http_media_txt[context->media_type]; } /* 发送数据在某些浏览器,如IE6.0~IE8.0中偶尔会出现发送不成功不成功的现象,在此处理 */ S32 http_send_block(void *ctx, const char *buffer, S32 send_len, S32 fail_max_time) { S32 length = 0; S32 fail_times = 0; S32 error = 0; CONTEXT *context = (CONTEXT *)ctx; while(fail_times < fail_max_time) { if (context->is_https == TRUE) { HTTPD_ERROR("Use HTTPS"); length = tpssl_writeSSL(context->ssl_ctx, buffer, send_len); if (length <= 0){ int err = tpssl_get_err(context->ssl_ctx, length); if ( err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE){ HTTPD_ERROR("tpssl_write error: %d\n", err); return -1; }else{ HTTPD_ERROR("no err, ret:%d\n", length); usleep(HTTP_SEND_RETRY_INTERVAL); fail_times++; continue; } } } else { HTTPD_ERROR("Use HTTP"); length = send(context->sock, buffer, send_len, 0); } if (length > 0) { break; } error = errno; if (error == EAGAIN || error == EINTR) { usleep(HTTP_SEND_RETRY_INTERVAL); fail_times++; } else { return HTTP_SEND_BLOCK_ERR; } } if (fail_times == fail_max_time) { HTTPD_ERROR("Http send timeout."); } return length; } /* 数据接收在某些浏览器,如IE6.0~IE8.0中偶尔会出现第一次接收不成功的现象,在此处理 */ S32 http_recv_block(void *ctx, char *buffer, int recv_len) { S32 length = 0; CONTEXT *context = (CONTEXT *)ctx; struct timeval last, now; /* 数据接收在IE6.0~IE8.0中偶尔会出现第一次接收不成功的现象,在此处理 */ if (context->is_https == TRUE) { if (context->is_upgrade == 1) { gettimeofday(&last, NULL); } readssl_again: length = tpssl_readSSL(context->ssl_ctx, buffer, recv_len); if (length <= 0) { int err = tpssl_get_err(context->ssl_ctx, length); if ( err != SSL_ERROR_WANT_READ && err != SSL_ERROR_WANT_WRITE) { HTTPD_ERROR("tpssl_read error: %d\n", err); return -1; } else { HTTPD_DEBUG("no err, tpssl_get_err:%d, ret:%d\n", err, length); #ifdef MAKEROOM_BEFORE_UPGRADE if (context->is_upgrade == 1) { gettimeofday(&now, NULL); if (now.tv_sec - last.tv_sec > HTTP_UPGRADE_TIMEOUT) { HTTPD_ERROR("timeout! return error."); return ERROR; } usleep(10); goto readssl_again; /* 固件升级保持阻塞方式读取 */ } else #endif { return RESERVE; } } } else { return length; } } else { length = recv(context->sock, buffer, recv_len, 0); } if (length == 0) { HTTPD_ERROR("recv length: %d\n", length); return ERROR; } if (length < 0) /* 接收数据失败。 */ { HTTPD_ERROR("recv length %d, errno %d\n", length, errno); if (errno != EAGAIN && errno != EINTR && errno != EWOULDBLOCK) { return ERROR; } else { return RESERVE; } } return length; } /****************************************************************************** * FUNCTION : * DESCRIPTION : HTTP某个请求会话中负责发送响应数据的函数。 * INPUT : * OUTPUT : N/A * RETURN : * HISTORY : * 把原发送缓存部分提出来单独成发送函数(即增加了一层发送处理函数), * 以便异步发送时复用。 * Create by luhan add 2015.12.23 ******************************************************************************/ S32 http_send_buffer(CONTEXT *context, const char *buffer, S32 send_len) { const char *send_leave_buf = buffer; /* 剩余要发送的缓存。*/ S32 send_leave_buf_len = send_len; /* 剩余要发送的缓存长度。*/ S32 tmp_send_buf_len = 0; /*S32 tmp_buf_len = NULL;*/ BOOL is_need_asyn_send_flag = FALSE; S32 fail_max_time = HTTP_FAILED_MAXTIMES; if (NULL == context || NULL == buffer || send_len < 1) { return ERROR; } /* 是静态页面? 发送失败的话则使用异步发送机制。*/ if ((HTTP_REQ_OK == context->code) && (context->media_type >= HTTP_MEDIA_INDEX && context->media_type <= HTTP_MEDIA_ZIP) && (strlen(context->path) > 0) /* && (context->virt_file_index >= HTTP_MAX_VIRTUAL_FILE) */) { is_need_asyn_send_flag = TRUE; fail_max_time = HTTP_FAILED_MAXTIMES - 5; /* 静态页面文件由于有了异步发送机制,则不用阻塞太久。*/ } /* 发送数据部分,如果有的话。 */ while (send_leave_buf_len > 0) { /* 计算一轮要发送的长度,最多4K。*/ tmp_send_buf_len = HTTP_SECTOR_SIZE; if (send_leave_buf_len < HTTP_SECTOR_SIZE) { tmp_send_buf_len = send_leave_buf_len; } tmp_send_buf_len = http_send_block(context, send_leave_buf, tmp_send_buf_len, fail_max_time); if (tmp_send_buf_len <= 0) { /* tcp reset 后将 timeout 设置为 0,在 context_check_timer 处释放 context */ if (tmp_send_buf_len == HTTP_SEND_BLOCK_ERR) { context->timeout = 0; return ERROR; } if (TRUE == is_need_asyn_send_flag) { if (context->async_buffer_len > send_leave_buf_len) { context->timeout = HTTP_CONTEXT_TIMEOUT; /* 有发送成功过,延长时间。*/ } /* 走到这里说明现在发送有失败了,存储未完成部分的静态文件的长度,稍后异步再继续发送。*/ context->async_buffer_len = send_leave_buf_len; return ERROR; } else { break; /* 非静态页面,发送失败的话直接不处理了。*/ } } send_leave_buf_len -= tmp_send_buf_len; send_leave_buf += tmp_send_buf_len; } context->async_buffer_len = 0; /* 发送完成,把需要发送的剩余文件长度置为0。*/ return OK; } /****************************************************************************** * FUNCTION : * DESCRIPTION : HTTP异步发送资源文件。 * INPUT : * OUTPUT : N/A * RETURN : * HISTORY : * Create by luhan add 2015.12.28 ******************************************************************************/ S32 http_send_async_buffer(CONTEXT *context) { char *send_buffer = NULL; char *file_buffer = NULL; S32 fd = -1; S32 malloc_size = 0; S32 file_len = 0; if (context->async_buffer_len < 1) { return ERROR; } if ((context->rsp_file_stat.st_mode & S_IFREG) && ((fd = open(context->rsp_file_path, O_RDONLY)) > 0)) { file_len = context->rsp_file_stat.st_size; } /* 如果文件打不开,可能是找不到文件,也可能是文件解压失败,直接断开连接 */ else { return ERROR; } if (file_len < context->async_buffer_len) { return ERROR; } malloc_size = file_len + HTTP_CHUNK_BOU_LEN; /* 首先申请一段缓存。这里复用HTTP解析专用缓存。*/ file_buffer = (char*)NSD_MALLOC(malloc_size); if (NULL == file_buffer) { /* 一时申请不到内存,并不急于马上断开会话,先返回OK */ HTTPD_ERROR("FAIL(async):alloc content error, no memory."); return OK; } memset(file_buffer, 0, malloc_size); read(fd, file_buffer, malloc_size); /* 定位到要继续发送的位置。*/ send_buffer = file_buffer + (file_len - context->async_buffer_len); /* 继续发送出去。*/ http_send_buffer(context, send_buffer, context->async_buffer_len); /* 释放临时申请的内存块。*/ NSD_FREE(file_buffer); if (fd > 0) { close(fd); } return OK; } /****************************************************************************** * fn nvmp_gen_rand_bytes * brief Read a stream of bytes from /dev/urandom * param[in] hex the address where the byte stream is saved * param[in] len the number of bytes to be read * return the actual number of bytes read ******************************************************************************/ int nvmp_gen_rand_bytes(unsigned char *hex, int len) { if (!hex || (len <= 0)) { return 0; } int total_bytes = 0; /* Number of bytes read */ struct stat b; static int nvmp_rand_fd = -2; /* init rand fd */ if (-2 == nvmp_rand_fd) { for (int i = 0; i < 5; i++) { if (!stat("/dev/urandom", &b) && (b.st_mode & S_IFCHR)) { nvmp_rand_fd = open("/dev/urandom", O_RDONLY); } if (nvmp_rand_fd >= 0) { break; } NVMP_PRINT("/dev/urandom is NOT READY!\n"); sleep(1); } if (nvmp_rand_fd < 0) { nvmp_rand_fd = -1; } } /* read from /dev/urandom */ if (nvmp_rand_fd >= 0) { while (total_bytes < len) { int n = read(nvmp_rand_fd, hex + total_bytes, len - total_bytes); if (n <= 0) { break; } total_bytes += n; } if (total_bytes == len) { return len; } } /* use rand to pad when not fully read */ struct timeval tv = {0}; gettimeofday(&tv, NULL); srand(tv.tv_sec * tv.tv_usec + (unsigned int)(hex)); for (; total_bytes < len; ++total_bytes) { hex[total_bytes] = (char)(rand() & 0xFF); } return len; } S32 generate_digest_nonce(CONTEXT *context) { /* 数据源:时间戳+随机数 */ struct { time_t timestamp; /* 精确到秒的时间戳 */ time_t expiration_time; /* 过期时间 */ unsigned char rand_bytes[16]; /* 强随机数 */ } nonce_seed; /* 取当前时间戳 */ nonce_seed.timestamp = time(NULL); nonce_seed.expiration_time = nonce_seed.timestamp + NONCE_LIFETIME; /* 过期时间 */ /* 生成密码学强随机数 (使用RAND_bytes) */ if (0 == nvmp_gen_rand_bytes(nonce_seed.rand_bytes, sizeof(nonce_seed.rand_bytes))) { return ERROR_CRYPTO_FAIL; /* 随机数生成失败 */ } /* 转为十六进制字符串格式 */ char hex_buffer[2 * sizeof(nonce_seed) + 1]; const unsigned char *ptr = (const unsigned char *)&nonce_seed; for (size_t i = 0; i < sizeof(nonce_seed); i++) { snprintf(hex_buffer + 2*i, 3, "%02x", ptr[i]); } hex_buffer[2 * sizeof(nonce_seed)] = &#39;\0&#39;; /* 组合realm和hex_buffer生成nonce:realm:hex_buffer" */ int len = snprintf(context->digest_nonce, LEN_DIGEST_NONCE, "%s:%s", context->digest_realm, hex_buffer); if (len < 0 || len >= LEN_DIGEST_NONCE) { return ERROR_BUFFER_OVERFLOW; } return OK; } S32 http_send_rsp_header(CONTEXT *context) { S32 length = 0; S32 send_len = 0; char *file_name = NULL; char *http_err_str = NULL; U16 local_port = 0; NSD_ASSERT(NULL != context); /* 先封装头部。 */ http_err_str = http_get_err_str(context->code); if (NULL == http_err_str) { return ERROR; } char send_head_buf[HEADER_SIZE] = {0}; /*不能复用接受报文时的缓冲区*/ context->head_end = send_head_buf; context->head_buf_end = send_head_buf + sizeof(send_head_buf); /* Status-Line */ length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "HTTP/%s %03i %s\r\n", (context->version == HTTP_VERSION_1_0)?"1.0":"1.1", context->code, http_err_str); context->head_end+= length; context->header_len += length; /* ========== 新增Digest认证头 ========== */ if (HTTP_UNAUTHORIZED == context->code) { /* realm、opaque:自定义常量 */ strncpy(context->digest_realm, "TP-Link IP-Camera", sizeof(context->digest_realm) - 1); strncpy(context->digest_opaque, "64943214654649846565646421", sizeof(context->digest_opaque) - 1); /* 生成带时效的nonce */ if (generate_digest_nonce(context) != OK) { HTTPD_ERROR("generate_digest_nonce failed"); return ERROR; }else{ HTTPD_DEBUG("context->digest_nonce: %s", context->digest_nonce); } length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "WWW-Authenticate: Digest realm=\"%s\",algorithm=\"MD5\",qop=\"auth\",nonce=\"%s\",opaque=\"%s\"\r\n", context->digest_realm, context->digest_nonce, context->digest_opaque); /* 结束头部 */ context->head_end += length; context->header_len += length; } /* Connection */ #ifdef FAC_MODEL_ENABLE if(context->keepalive) { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Connection: %s\r\n", "keep-alive"); } else #endif { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Connection: %s\r\n", "close"); } context->head_end += length; context->header_len += length; /* ETag */ if (context->etag[0] != &#39;\0&#39;) { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "ETag: %s\r\n", context->etag); context->head_end+= length; context->header_len += length; } /* Last-Modified */ if (context->last_modified[0] != &#39;\0&#39;) { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Last-Modified: %s\r\n", context->last_modified); context->head_end += length; context->header_len += length; } /* Cache-control */ if ((HTTP_MEDIA_GIF == context->media_type) || (HTTP_MEDIA_JPEG == context->media_type)) { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Cache-control: max-age=%d\r\n", HTTP_IMG_CATCH_TIME); context->head_end += length; context->header_len += length; } else { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Cache-Control: no-cache\r\n"); context->head_end += length; context->header_len += length; } #ifdef INCLUDE_UPNP_SERVER /* UPnP扩展头部。 */ if (OK != http_make_extend_head(context)) { return ERROR; } #endif /* Location */ if (HTTP_MOVE_TEMPORARILY == context->code) { local_port = get_group_default_port(); length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Location: http://%s:%d\r\n", context->host, local_port); context->head_end += length; context->header_len += length; } /* Access-Control-Allow-Origin */ if (context->origin == TRUE) { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Access-Control-Allow-Origin: *\r\n"); context->head_end += length; context->header_len += length; } /* if have content */ if (context->content_len != 0) { /* Content-Type,filename 初始化为响应文件的路径 */ file_name = context->rsp_file_path; /* 修复在safari浏览器上保存的配置和日志都被保存为.html文件的问题 */ if ((TRUE == context->disposition) && (NULL != file_name) && /* (context->virt_file_index < HTTP_MAX_VIRTUAL_FILE) && */ ((0 == strcmp(file_name, CONFIG_FILE_PATH)) || (0 == strcmp(file_name, SYSLOG_FILE_PATH)) || (0 == strcmp(file_name, PLUGIN_DIR)))) { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Content-Type: application/octet-stream\r\n"); } else { length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Content-Type: %s;charset=UTF-8\r\n", http_get_media_txt(context)); } context->head_end += length; context->header_len += length; /* Content-disposition */ //if ((TRUE == context->disposition) && // (context->virt_file_index < HTTP_MAX_VIRTUAL_FILE)) if (TRUE == context->disposition && NULL != file_name) { /* 去除文件的路径 */ file_name = strrchr(file_name, &#39;/&#39;) + 1; length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Content-disposition: attachment;filename=\"%s\"\r\n", file_name); context->head_end += length; context->header_len += length; } /* 发送文件时,不使用chunked模块 */ if (TRUE == context->chunked && FALSE == context->rsp_file) { /* chunked */ length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Transfer-Encoding: chunked\r\n"); } else { /* Content-Length */ length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "Content-Length: %d\r\n", context->content_len); } context->head_end += length; context->header_len += length; } /* close header */ length = snprintf(context->head_end, (context->head_buf_end - context->head_end), "\r\n"); context->head_end += length; context->header_len += length; /* 发送头部。 */ send_len = http_send_block(context, send_head_buf, context->header_len, HTTP_FAILED_MAXTIMES); if (send_len <= 0) { /* tcp re set 后将 timeout 设置为 0,在 context_check_timer 处释放 context */ if (send_len == HTTP_SEND_BLOCK_ERR) { context->timeout = 0; } return ERROR; } return OK; } S32 http_send_rsp_content(CONTEXT *context) { U32 len = 0; U32 send_len = 0; char *ptr = NULL; NSD_ASSERT(NULL != context); if (NULL == context->content_buf || 0 == context->content_len) { return OK; } ptr = context->content_buf; while (context->content_len > 0) { send_len = min(context->content_len, HTTP_SECTOR_SIZE); len = http_send_block(context, ptr, send_len, HTTP_FAILED_MAXTIMES); if (len <= 0) { break; } context->content_len -= len; ptr += len; } return OK; } LOCAL BOOL is_num(char ch) { if (ch >= BEGIN_NUM && ch <= END_NUM) { return TRUE; } return FALSE; } LOCAL BOOL is_lowercase(char ch) { if (ch >= BEGIN_LOWERCASE && ch <= END_LOWERCASE) { return TRUE; } return FALSE; } LOCAL BOOL is_capital(char ch) { if (ch >= BEGIN_CAPITAL && ch <= END_CAPITAL) { return TRUE; } return FALSE; } LOCAL BOOL is_normal_char(char ch) { if (is_num(ch) == TRUE || is_lowercase(ch) == TRUE || is_capital(ch) == TRUE) { return TRUE; } return FALSE; } LOCAL BOOL is_hex_char(char ch) { if (is_num(ch) == TRUE || (ch >= BEGIN_CAPITAL && ch <= END_UPPER_HEX) || (ch >= BEGIN_LOWERCASE && ch <= END_LOWER_HEX)) { return TRUE; } return FALSE; } S32 model_encode_sc(const char* s_in, U32 s_in_len, char* s_out, U32 s_out_len) { U32 i = 0; U32 index = 0; if (NULL == s_in || NULL == s_out) { return ERROR; } for (i = 0; i < s_in_len; i++) { if(is_normal_char(s_in[i]) == TRUE && s_out_len > index + 1) { s_out[index++] = s_in[i]; } else if(s_out_len > index + ESC_LEN) { s_out[index++] = ESC_CHAR; s_out[index++] = TO_HEX_FIRST((U8)s_in[i]); s_out[index++] = TO_HEX_SECOND((U8)s_in[i]); } else { s_out[index] = &#39;\0&#39;; HTTPD_WARNING("output string don&#39;t have enough space!"); return ERROR; } } s_out[index] = &#39;\0&#39;; return OK; } S32 model_decode_sc(char *ptr_arg) { char *s_in = NULL; char *s_out = NULL; char tmp_buf[3] = {0}; if (NULL == ptr_arg) { return ERROR; } s_in = ptr_arg; s_out = ptr_arg; while (*s_in != &#39;\0&#39;) { /*不是%的字符不需要进行转换*/ if(*s_in != &#39;%&#39;) { *s_out++ = *s_in; s_in++; continue; } tmp_buf[0] = *(s_in + 1); if (!is_hex_char(tmp_buf[0])) { *s_out++ = *s_in; s_in++; continue; } tmp_buf[1] = *(s_in + 2); if (!is_hex_char(tmp_buf[1])) { *s_out++ = *s_in; *s_out++ = *(s_in + 1); s_in += 2; continue; } tmp_buf[2] = &#39;\0&#39;; *s_out++ = (char)strtol(tmp_buf, NULL, 16); s_in += 3; } *s_out = &#39;\0&#39;; return OK; } #ifdef MAKEROOM_BEFORE_UPGRADE /* 面向httpd的升级后延时重启 */ #define HTTPD_REBOOT_DELAY_SECOND 1 LOCAL void httpd_system_reboot_delay(S32 data) { int ret = reboot(RB_AUTOBOOT); if (ret) { ret = system("reboot -f"); if (ret) { HTTPD_WARNING("reboot -f fail!"); ret = system("echo b 2>/dev/null > /proc/sysrq-trigger"); if (ret) { HTTPD_WARNING("echo b 2>/dev/null > /proc/sysrq-trigger fail!"); } } } } S32 httpd_reboot_delay() { if (inet_add_timer(httpd_system_reboot_delay, 0, HTTPD_REBOOT_DELAY_SECOND, EXECUTE_SINGLE) == -1) { return ERROR; } return OK; } #endif
10-23
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值