【目标】
返回ErrorCode码,成功cnf_result返回0,失败返回其他非零数
【分析实现】
逻辑如下图:
/*
*ubus call solution.wlan app_modify_acl_list
*接收并解析msg
*格式如下
*
*{
* "response": {
* "setting_response": "ERROR"
* },
* "wireless": {
* "cnf_result": 0
* }
*}
*此结构形式要和wlan_service_ubus_api.c中的接口WLAN_API_ModifyMacfilterProc所发送的格式一致,否则数据解析就会出问题
*/
【定义数据表所需要的数据】
这个是用来构造表和成员的枚举数据
这个是用来存储解析后的数据,以备后用,其实可以下面定义:
typedef struct {
int ErrorCode;
};
注意:这里我不想再定义了,所以复用了下,AclInfoResp中的其他数据我不关注,所以我在下面的表格构建中,也没有增加对应的定义实现
【构建数据表格式】
这里我只关注ErrorCode数据值,所以只构建了一个这个
【函数实现】
/* 用解析出的信息构造回复报文 */
static void WEBSOCKET_CreateJsonObjAppWifiModifyAclConfig(AclInfoResp *app_acl_info, json_object *outResp)
{
if (app_acl_info == NULL || outResp == NULL) {
ZLINK_LOG_ERROR("WEBSOCKET_CreateJsonObjAppWifiSetAclConfig ERROR\n");
return;
}
/* 添加字段和值 */
ZLINK_LOG_DEBUG("ErrorCode = %u\n", app_acl_info->ErrorCode);
json_object_object_add(outResp, "ErrorCode", json_object_new_int(app_acl_info->ErrorCode));
ZLINK_LOG_DEBUG("WEBSOCKET_CreateJsonObjAppWifiModifyAclConfig OK\n");
return;
}
/* 解析 wlan_service ubus信息 */
static int WEBSOCKET_ParseUbusModifyAclConfig(AclInfoResp *app_wifi_info, struct blob_buf *wlanBuf)
{
int rc = ZLINK_FAILED;
struct blob_attr *tb[ARRAY_SIZE(ModifyTable)];//1.创建大表
struct blob_attr *subtba[ARRAY_SIZE(ModifyAclonfig_response)];//2.创建表内成员
struct blob_attr *subtb[ARRAY_SIZE(ModifyAclonfig_errorcode)];//2.创建表内成员
if (app_wifi_info == NULL || wlanBuf == NULL) {
ZLINK_LOG_DEBUG("app_wifi_info is NULL\n");
goto EXIT;;
}
//3.解析入参数据wlanBuf放入大表tb
rc = blobmsg_parse(ModifyTable, ARRAY_SIZE(ModifyTable), tb, blob_data(wlanBuf->head), blob_len(wlanBuf->head));
ZLINK_LOG_DEBUG("blobmsg_parse tb msg end, rc = %d\n", rc);
if (rc < 0) {
ZLINK_LOG_DEBUG("parse table msg failed, rc = %d\n", rc);
goto EXIT;
}
if (!tb[WEBSOCKET_MOD_TABLE_MAIN]) {
ZLINK_LOG_DEBUG("tb[WEBSOCKET_WLAN_TABLE] is null \n");
goto EXIT;
} else {/*解析值并放入结构app_wifi_info中*/
rc = ZLINK_BlobMsgParse(ModifyAclonfig_response, ARRAY_SIZE(ModifyAclonfig_response), subtba,
ZLINK_BlobMsgData(tb[WEBSOCKET_MOD_TABLE_MAIN]), ZLINK_BlobMsgDataLen(tb[WEBSOCKET_MOD_TABLE_MAIN]));
if (rc < 0) {
ZLINK_LOG_DEBUG("parse msg failed, rc = %d\n", rc);
goto EXIT;;
}
if (!subtba[WEBSOCKET_MOD_RESPONSE]) {
ZLINK_LOG_DEBUG("tb[WEBSOCKET_MOD_RESPONSE] is NULL\n");
rc = ZLINK_FAILED;
goto EXIT;;
}
if (subtba[WEBSOCKET_MOD_RESPONSE]) {
ZLINK_LOG_DEBUG("tb[WEBSOCKET_MOD_RESPONSE] is success\n");
}
rc = ZLINK_BlobMsgParse(ModifyAclonfig_errorcode, ARRAY_SIZE(ModifyAclonfig_errorcode), subtb,
ZLINK_BlobMsgData(tb[WEBSOCKET_MOD_TABLE_SECOND]), ZLINK_BlobMsgDataLen(tb[WEBSOCKET_MOD_TABLE_SECOND]));
if (rc < 0) {
ZLINK_LOG_DEBUG("parse msg failed, rc = %d\n", rc);
goto EXIT;;
}
if (!subtb[WEBSOCKET_MOD_ERRORCODE]) {
ZLINK_LOG_DEBUG("tb[WEBSOCKET_MOD_ERRORCODE] is NULL\n");
rc = ZLINK_FAILED;
goto EXIT;;
}
if (subtb[WEBSOCKET_MOD_ERRORCODE]) {
app_wifi_info->ErrorCode = blobmsg_get_u32(subtb[WEBSOCKET_MOD_ERRORCODE]);
ZLINK_LOG_DEBUG("tb[WEBSOCKET_STRENGTH_ERRORCODE] is success\n");
}
}
EXIT:
ZLINK_MemRelease((void**)(&wlanBuf->head));
return rc;
}
【调用】
static int WEBSOCKET_WIFI_ModifyAclList(char* inBuf, int inLen, struct json_object* outResp)
{
int ret = -1;
AclInfoResp app_acl_info = {0};
struct blob_buf reqBuf = {0};
char *methodName = "app_modify_acl_list";
int ubusCliFd = 0;
int timeout = WEBSOCKET_UBUS_TIMEOUT;
static ZLINK_BlobBuff* getBuf = NULL;
struct ModifyAclConfigmsg modifyacl_info = {0};
struct json_object *ModifyAclConfigInBuf = NULL;
if (inBuf == NULL || inLen == 0 || outResp == NULL) {
return ZLINK_FAILED;
}
ubusCliFd = WEBSOCKET_UnixCliSocketInit();
if (ubusCliFd <= 0) {
ZLINK_LOG_DEBUG("get ubusCliFd fail!");
return ZLINK_FAILED;
}
//parse inBuf jason to blob_buff ->reqBuf
ZLINK_BlobBuffInit(&reqBuf, 0);
memset_s(&modifyacl_info, sizeof(modifyacl_info), 0x00, sizeof(modifyacl_info));
ModifyAclConfigInBuf = json_tokener_parse(inBuf);
/*** app下发的:json转为blob***/
WEBSOCKET_ParseModifyAclConfigReqJson(ModifyAclConfigInBuf, &modifyacl_info);
WEBSOCKET_AddModifyAclConfigReqUbus(&reqBuf, &modifyacl_info);
/* 发送get strength ubus请求 */
ret = WEBSOCKET_SendUbus_RecvBlobBuf(WLAN_OBJ_NAME, methodName, &reqBuf, timeout, ubusCliFd);
if (ret != ZLINK_SUCCESS) {
ZLINK_LOG_ERROR("%s:WEBSOCKET_SendUbusReq failed\n", __func__);
}
getBuf = (ZLINK_BlobBuff *)WEBSOCKET_Get_RecvBlobBuf();
if (getBuf == NULL) {
ZLINK_LOG_DEBUG("getBuf pointer is NULL!");
goto RET;
}
/* 解析 wlan_service ubus信息 */
ret = WEBSOCKET_ParseUbusModifyAclConfig(&app_acl_info, getBuf);
if (ret != ZLINK_SUCCESS) {
ZLINK_LOG_ERROR("%s:WEBSOCKET_ParseUbuswifiServer failed, rc = %d\n", __func__, ret);
}
/* 用解析出的信息构造回复报文 */
WEBSOCKET_CreateJsonObjAppWifiModifyAclConfig(&app_acl_info, outResp);
ZLINK_LOG_DEBUG("json msg: %s\n", json_object_to_json_string(outResp));
ret = ZLINK_SUCCESS;
goto RET;
RET:
close(ubusCliFd);
ZLINK_MemRelease((void **)(&getBuf->head));
ZLINK_BlobBuffFree(&reqBuf);
return ret;
}
工况1:黑名单数量大于16时,添加失败并回复ErrorCode为3
工况2:黑名单数量小于16时,添加失败并回复ErrorCode为0