himcu_lite是鸿蒙智联标准串口协议的MCU侧代码实现,集成在MCU侧以后,可以与使用了标准固件的通信模组进行交互,提供了控制、OTA等功能,支持无OS平台,资源占用:代码空间10k、静态RAM<1K
| Files | Last commit | Last update |
|---|---|---|
| 8 months ago | ||
| 5 months ago | ||
| 7 months ago | ||
| 8 months ago | ||
| 7 months ago | ||
| 8 months ago | ||
| 7 months ago | ||
| 8 months ago | ||
| 7 months ago | ||
| 7 months ago | ||
| 8 months ago | ||
| 8 months ago | ||
| 5 months ago | ||
| 7 months ago | ||
| 5 months ago | ||
| 7 months ago | ||
| 8 months ago |
himcu_lite
介绍
+-----------+ +-----------+
| Module | | MCU |
| TX ----->+--------------------------------->+ RX |
| RX <-----+<---------------------------------+ TX |
+-----------+ +-----------+
HiMCU_Lite是鸿蒙智联标准串口协议的MCU侧代码实现,集成在MCU侧以后,可以与使用了标准固件的通信模组进行交互,提供了控制、OTA等功能,支持无OS平台,资源占用:代码空间10k、静态RAM<1K
代码结构
目录
himcu
├── himcul_config.h # 配置宏
├── himcul_core.c # 核心代码实现
├── himcul_core.h # 核心代码头文件
├── himcul_framework.c # 框架代码实现
├── himcul_framework.h # 框架代码头文件
├── himcul_ota.c # OTA代码实现(需开发者适配)
├── himcul_ota.h # OTA头文件
├── himcul_product.c # 产品代码实现(需开发者适配)
├── himcul_product.h # 产品代码头文件
├── himcul_profile.c # 产品profile实现(需开发者适配)
└── himcul_profile.h # 产品profile头文件
调用关系
graph TD
main["main.c(业务入口)"] --> framework["himcul_framework.h/c(框架)"]
framework --> ota["himcul_ota.h/c(OTA)"]
framework --> product["himcul_product.h/c(产品信息)"]
framework --> config["himcul_config.h(配置)"]
framework --> core["himcul_core.h/c(核心)"]
product --> profile["himcul_profile.h/c(物模型)"]
ota --> core
product --> core
profile --> core
core --> config
ota --> config
product --> config
profile --> config
对外依赖
himcu_lite存在部分对外依赖,详见下表,如果当前软件平台无法提供对应依赖,则在集成阶段应按照移除依赖指导修改
| 接口 | 头文件 | 说明 | 移除依赖指导 |
|---|---|---|---|
| strlen | string.h | 计算字符串长度 | 实现himcul_product.h文件中的HIMCUL_PROD_Strlen |
| strnlen | string.h | 计算字符串长度,并限制最大值 | 实现himcul_product.h文件中HIMCUL_PROD_Strnlen |
| strtol | stdlib.h | 字符串转数字 | 实现himcul_product.h文件中HIMCUL_PROD_Strtol |
| memset_s | securec.h | 安全函数 | 安全函数移除参考集成适配-代码集成章节 |
| memcpy_s | securec.h | 安全函数 | |
| memmove_s | securec.h | 安全函数 | |
| strncpy_s | securec.h | 安全函数 | |
| sprintf_s | securec.h | 安全函数 | |
| vsprintf_s | securec.h | 安全函数 |
快速上手
产品信息适配
参考产品DP平台定义,修改himcul_product.c中的以下定义
static const char *g_prodId = "9LFJ"; # PID
static const char *g_model = "test3"; # 产品型号
static const char *g_devTypeId = "046"; # 设备类型ID
static const char *g_devTypeName = "TableLamp"; # 设备类型英文名
static const char *g_manuId = "002"; # 厂商ID
static const char *g_manuName = "HUAWEI"; # 厂商英文名
static const uint8_t g_netcfgMode = HIMCUL_NETCFG_TYPE_SOFTAP; # 配网模式
static const uint8_t g_acKey[48] = {xxx}; # 厂商ACKEY
串口收发包适配
实现himcul_product.c中的串口初始化函数HIMCUL_PROD_TransInit 与串口发送函数HIMCUL_PROD_TransSendHandler
int32_t HIMCUL_PROD_TransInit(void)
{
/* TODO: 实现串口初始化,波特率115200 */
return HIMCUL_OK;
}
int32_t HIMCUL_PROD_TransSendHandler(const uint8_t *data, uint32_t len)
{
HIMCUL_CHECK_RETURN_LOGW(data != NULL && len != 0, HIMCUL_ERR_PARAM_INVALID, "param invalid");
/* TODO: 实现串口发送,返回实际发送长度 */
return len;
}
串口接收可以支持两种模式
- 串口中断写入数据到唤醒缓冲区,需使能
himcul_config.h中的HIMCUL_CONF_UART_RING_BUFFER_SUPPORT宏 - 由框架主动调用读取函数读取串口数据,需将
himcul_config.h中的HIMCUL_CONF_UART_RING_BUFFER_SUPPORT宏置为0
中断模式
在串口中断函数调用himcul_framework.h中的接口HIMCUL_FWK_WriteDataRingBuffer,将串口数据写入缓冲区
主动读取模式
实现himcul_product.c中的与串口接收函数HIMCUL_PROD_TransRecvHandler
int32_t HIMCUL_PROD_TransRecvHandler(uint8_t *buf, uint32_t len, uint32_t timeoutMs)
{
HIMCUL_CHECK_RETURN_LOGW(buf != NULL && len != 0, HIMCUL_ERR_PARAM_INVALID, "param invalid");
/* TODO: 实现串口接收,返回实际接收长度,超时时间为timeoutMs */
return 0;
}
产品服务适配
参考产品在DP平台的物模型定义,完成适配
快速上手阶段可实现部分物模型,但不能实现产品在DP平台上不存在的物模型,否则可能导致设备频繁上下线或配网失败
完善物模型表
实现himcul_product.c中的物模型定义全局变量g_profile,示例如下
static const ProfileCharItem g_charSwitch[] = { # switch服务的属性列表
{
.cid = "on", # 属性,需与DP平台物模型一致
.ct = "bool", # 数据类型,int/bool/string/float/array/enum
.ciid = 0, # 属性序号,如果有多个属性则从0开始按顺序指定,不同服务独立
.putFunc = HIMCUL_PRF_SwitchOnPutHandler, # PUT处理函数,属性支持PUT则必须实现
.getFunc = HIMCUL_PRF_SwitchOnGetHandler, # GET处理函数,属性支持GET则必须实现
.rptFunc = HIMCUL_PRF_SwitchOnRptHandler, # REPORT处理函数,属性支持REPORT则必须实现
}
};
static const ProfileSvcItem g_profile[] = { # 物模型表
{
.sid = "switch", # 服务sid,需与DP平台物模型一致
.st = "switch", # 服务类型ServiceType,需与DP平台物模型一致
.siid = 0, # 服务序号,如果有多个服务则从0开始按顺序指定
.charNum = HIMCUL_ARRAY_SIZE(g_charSwitch), # 服务下的属性数量
.chars = g_charSwitch, # 服务下的属性列表
}
};
物模型回调实现
物模型表中对于每个属性,需按照物模型定义中的GET/REPORT/PUT权限,指定对应的回调函数,建议统一实现在himcul_profile.c文件中
PUT回调函数
PUT回调会在设备被控制时调用,用来修改属性值,其实现示例如下
/* 服务switch属性on PUT回调函数 */
int32_t HIMCUL_PRF_SwitchOnPutHandler(uint8_t siid, uint8_t ciid, const uint8_t *data,
uint32_t len, HIMCUL_Buffer *buf)
{
bool switchOnState = false;
/* 串口协议会使用ASCII编码传输属性数据,例如bool型为"0"或"1",int/enum型为"50",float型为"20.1",其他类型为字符串
* bool型属性,使用 HIMCUL_ProfileCharToBool 函数解析
* int或enum型属性,则需使用 HIMCUL_ProfileCharToInt 函数解析
* float型属性,需将数据拷贝到带有结束符的缓冲区后,使用 strtof 函数解析
*/
int32_t ret = HIMCUL_ProfileCharToBool(data, len, &switchOnState);
if (ret != 0) {
HIMCUL_LOGE("invalid bool data, ret=%d", ret);
return ret;
}
HIMCUL_LOGN("put switchOnState, state=%d=>%d", g_switchOnState, switchOnState);
/* 修改属性状态,使用全局变量模拟开关状态 */
g_switchOnState = switchOnState;
/* PUT函数需填充响应,在参数buf中使用HIMCUL_ProfileCharAddXXX 函数填充控制结果 */
return HIMCUL_ProfileCharAddBool(siid, ciid, g_switchOnState, buf);
}
GET回调函数
GET回调会在设备上线或者用户在H5界面时调用,用来查询属性值,其实现示例如下
/* 服务switch属性on GET回调函数 */
int32_t HIMCUL_PRF_SwitchOnGetHandler(uint8_t siid, uint8_t ciid, HIMCUL_Buffer *buf)
{
HIMCUL_LOGN("get switchOnState, state=%d", g_switchOnState);
/* GET函数仅需填充响应,在参数buf中使用HIMCUL_ProfileCharAddXXX 函数填充当前属性值 */
return HIMCUL_ProfileCharAddBool(siid, ciid, g_switchOnState, buf);
}
REPORT回调
REPORT回调会在MCU主动上报时调用,用来上报属性值,对于同时支持GET与REPORT的属性,其GET与REPORT回调可以实现为同一个函数,其实现示例如下
/* 服务switch属性on REPORT回调函数 */
int32_t HIMCUL_PRF_SwitchOnRptHandler(uint8_t siid, uint8_t ciid, HIMCUL_Buffer *buf)
{
HIMCUL_LOGN("report switchOnState, state=%d", g_switchOnState);
/* REPORT函数需在参数buf中使用HIMCUL_ProfileCharAddXXX 函数填充当前属性值 */
return HIMCUL_ProfileCharAddBool(siid, ciid, g_switchOnState, buf);
}
代码集成
产品需将himcu_lite的所有源文件集成进产品工程
himcu_lite依赖安全函数,可以从开源项目 third_party_bounds_checking_function 获取
对于资源有限无法集成安全函数的产品,可以参考以下宏将代码中使用的安全函数修改为非安全函数,但可能存在安全隐患,不建议
#include <string.h>
#include <stdio.h>
#define EOK 0
#define memset_s(dest, destsz, value, count) (memset((dest), (value), (count)), 0)
#define memcpy_s(dest, destsz, src, count) (memcpy((dest), (src), (count)), 0)
#define memmove_s(dest, destsz, src, count) (memmove((dest), (src), (count)), 0)
#define strncpy_s(dest, destsz, src, count) (strncpy((dest), (src), (count)), 0)
#define strcpy_s(dest, destsz, src) (strcpy((dest), (src)), 0)
#define sprintf_s(dest, destsz, fmt, ...) sprintf((dest), (fmt), __VA_ARGS__)
#define vsprintf_s(dest, destsz, fmt, args) vsprintf((dest), (fmt), (args))
调用代码入口
产品应在主任务中调用初始化接口与轮询接口,参考以下示例代码
#include "himcul_framework"
static uint32_t GetCurMs(void)
{
/* 获取系统毫秒级时间戳,对于无操作系统的设备,可以通过定时器累加变量的方式获取 */
return 0;
}
void mainEntry(void)
{
/* 初始化himcu_lite框架 */
int32_t ret = HIMCUL_FWK_Init();
if (ret != 0) {
printf("HIMCUL_FWK_Init init error, ret=%d\n", ret);
return;
}
while (1) {
/* 调用himcu_lite轮询入口,并传入当前系统运行的ms时间戳,请保证时间戳准确,否则会导致重传等功能异常 */
IMCUL_FWK_Loop(GetCurMs());
/* 为避免狗复位,应在轮询时喂狗或让出调度,建议调度间隔不超过100ms */
}
}
上板测试
正常启动后会通过HIMCUL_PROD_LogOutput函数输出以下日志(需使能himcul_config.h中的HIMCUL_CONF_LOG_SUPPORT)
HIMCUL:N:HIMCUL_FWK_Init:925 fwk init ok
HIMCUL_FWK_Init init ok
与模组连接后交互时会打印数据帧(需使能himcul_config.h中的HIMCUL_CONF_FRAME_DEBUG_DUMP,并确保HIMCUL_CONF_LOG_BUILD_LEVEL大于等于7)
HIMCUL:N:RecvFsmDispatchHandler:1120 [0x04|0x038b|0]recv frame
HIMCUL:D:TransMultiDataFrameDump:701 magic: 0xa55a
HIMCUL:D:TransMultiDataFrameDump:702 ver: 0x01
HIMCUL:D:TransMultiDataFrameDump:703 cmd: 0x04
HIMCUL:D:TransMultiDataFrameDump:704 id: 0x038b
HIMCUL:D:TransMultiDataFrameDump:705 len: 0x0000|0
HIMCUL:D:TransMultiDataFrameDump:706 data:
HIMCUL:D:TransMultiDataFrameDump:716 checkSum: 0x92|146
HIMCUL:N:BaseCmdHeartbeatReqHandler:231 [0x04|0x038b|0]recv heartbeat
智慧生活添加
使用智慧生活搜索,即可看到对应的设备,产品未通过认证时需要在沙箱环境搜索
高阶适配
OTA功能
himcu_lite提供一个边从云端下载边传输到MCU的OTA升级模式,需打开himcul_config.h中的HIMCUL_CONF_OTA_SUPPORT宏,并参考himcul_ota.h中的接口定义,实现himcul_ota.c中的所有函数
资源裁剪
修改himcul_config.h中的以下宏裁剪资源
#define HIMCUL_CONF_LOG_SUPPORT 0 # 关闭日志降低ROM,支持日志输出的产品不建议关闭
#define HIMCUL_CONF_LOG_BUILD_LEVEL 4 # 调低日志等级降低ROM,建议至少为4,详见HIMCUL_LOG_LEVEL_XXX定义
#define HIMCUL_CONF_FRAME_DEBUG_DUMP 0 # 关闭数据帧dump降低ROM
#define HIMCUL_CONF_TRANS_FRAME_MAX_SIZE 512 # 修改传输帧大小降低RAM,建议收集每次loop后的水线大小评估修改
#define HIMCUL_CONF_UART_RING_BUFFER_SUPPORT 0 # 关闭接收缓冲区降低RAM
Introduction
himcu_lite是鸿蒙智联标准串口协议的MCU侧代码实现,集成在MCU侧以后,可以与使用了标准固件的通信模组进行交互,提供了控制、OTA等功能,支持无OS平台,资源占用:代码空间10k、静态RAM<1K
Customize your domain