#ifndef PROTO_H #define PROTO_H #include /* 轻量 JSON 取值工具:按键名定位,仅适配平台下发的扁平/一层嵌套报文。 * 不依赖硬件,可独立测试。 */ /* 定位 "key" 并返回冒号后的值起点(跳过空白),未找到返回 NULL。 * 键名带引号完整匹配:"sw2" 不会误中 "sw21"。 */ const char *proto_locate(const char *json, const char *key); /* 取布尔值:在 key 的值区域内查找 true/false, * 支持 "key":true 与 "key":{"time":...,"value":true} 两种形式,也兼容 0/1。 * 成功返回 1 并写入 *val(1/0),失败返回 0。 */ int proto_get_bool(const char *json, const char *key, int *val); /* 取无符号整数:"key":123。成功返回 1,失败返回 0 */ int proto_get_u32(const char *json, const char *key, uint32_t *val); /* 取 16 位十六进制数:"key":"1A2B" 或不带引号。成功返回 1,失败返回 0 */ int proto_get_hex16(const char *json, const char *key, uint16_t *val); /* 取字符串:"key":"xxx"。成功返回 1(空串返回 0),输出以 '\0' 结尾 */ int proto_get_str(const char *json, const char *key, char *out, int cap); /* 取消息 id:兼容 "id":"abc123" 与 "id":123 两种格式。 * 引号形式取字母数字,非引号形式只取数字。成功返回 1,失败返回 0 */ int proto_get_id(const char *json, char *out, int cap); /* 取重量类数值,返回 0.1KG 单位: * 支持 "key":2.0、"key":"2.0"、"key":{"time":...,"value":2.0} 三种形式, * 最多取一位小数。成功返回 1,失败返回 0 */ int proto_get_kg100(const char *json, const char *key, uint16_t *val); #endif /* PROTO_H */