鸿蒙OpenHarmony源码分析之分布式软总线:trans_service/message.c文件分析

📌往期推文全新看点(文中附带最新·鸿蒙全栈学习笔记)

①📖 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?

②📖嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~

③📖 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?

④📖 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?

⑤📖 记录一场鸿蒙开发岗位面试经历~

⑥📖 持续更新中……


一、概述

trans_service模块基于系统内核提供的socket通信,向authmanager模块提供设备认证通道管理和设备认证数据的传输;向业务模块提供session管理和基于session的数据收发功能,并且通过GCM模块的加密功能提供收发报文的加解密保护。 本文主要是对message.c文件进行分析,该文件的主要功能是提供与cjson库相关的接口,这些接口用于设备间传输的json格式数据的封装或者解析。

二、源码分析

/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#include "message.h"
/*
函数功能:根据指定键获取int类型的json成员,然后将int类型的值保存在result中
函数参数:
    root:根json对象
    name:指定的键
    result:目标结果地址
函数返回值:
    成功:返回0
    失败:返回-1
*/
int GetJsonInt(const cJSON *root, const char *name, int *result)
{
    if (root == NULL || name == NULL || result == NULL) {//健壮性检查
        return -1;
    }
    cJSON *item = cJSON_GetObjectItem(root, name);//获取字段名为name的json成员
    if ((item == NULL) || (!cJSON_IsNumber(item))) {//判断item是否是number类型
        return -1;
    }
    *result = item->valueint;
    return 0;
}
/*
函数功能:根据指定键获取string类型的json成员,然后将string类型的值返回
函数参数:
    root:根json对象
    name:指定的键
函数返回值:
    成功:返回字符串value
    失败:返回NULL
*/
char* GetJsonString(const cJSON *root, const char *name)
{
    if (root == NULL || name == NULL) {//健壮性检查
        return NULL;
    }
    cJSON *item = cJSON_GetObjectItem(root, name);//获取字段名为name的json成员
    if ((item == NULL) || (!cJSON_IsString(item))) {//判断item是否是string类型
        return NULL;
    }
    return item->valuestring;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值