diff --git a/application/samples/wifi/ohos_connect/hilink_adapt/entry/hilink_ble_main.c b/application/samples/wifi/ohos_connect/hilink_adapt/entry/hilink_ble_main.c index 37486b3..8290689 100755 --- a/application/samples/wifi/ohos_connect/hilink_adapt/entry/hilink_ble_main.c +++ b/application/samples/wifi/ohos_connect/hilink_adapt/entry/hilink_ble_main.c @@ -1,6 +1,13 @@ /* * Copyright (c) Huawei Technologies Co., Ltd. 2020-2022. All rights reserved. * Description: 蓝牙SDK提供demo示例代码(此文件为DEMO,需集成方适配修改) + * + * BLE本地控制功能增强: + * - 增加完整的智能照明设备控制功能 + * - 支持亮度、色温、场景模式、渐变时长、色温模式控制 + * - 复用hilink_device.c中的fast_report统一上报机制 + * - 根据连接模式(BLE/云端)自动选择上报方式(BLE_SendCustomData/HILINK_ReportCharState) + * - 确保BLE和云端控制逻辑的完全一致性 */ #include #include @@ -39,8 +46,28 @@ #define CHECKSUM_REPORT "{\"data\":{\"checkSum\":\"%s\"},\"sid\":\"checkSum\"}" #endif +// 外部设备控制函数声明 - 引用device_module.c中的实际设备控制逻辑 +extern int myhandle_put_brightness(const char* svc_id, const char* payload, unsigned int len); +extern int myhandle_put_cct(const char* svc_id, const char* payload, unsigned int len); +extern int myhandle_put_lightMode(const char* svc_id, const char* payload, unsigned int len); +extern int myhandle_put_switch(const char* svc_id, const char* payload, unsigned int len); +extern int myhandle_put_progressSwitch(const char* svc_id, const char* payload, unsigned int len); +extern int myhandle_put_colourMode(const char* svc_id, const char* payload, unsigned int len); +extern int myhandle_get_brightness(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len); +extern int myhandle_get_cct(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len); +extern int myhandle_get_lightMode(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len); +extern int myhandle_get_switch(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len); +extern int myhandle_get_progressSwitch(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len); +extern int myhandle_get_colourMode(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len); + +// 引用统一的上报机制 +extern int fast_report(const char* svc_id); +extern void set_connection_mode(connection_mode_t mode); +extern connection_mode_t get_connection_mode(void); + + //static bool g_switch = 0; -int sid_switch; +// int sid_switch; // 注释掉,现在使用实际设备控制函数 static bool g_autoUpdate = 0; static int ble_adv_time = 0; static HILINK_BT_DevInfo g_btDevInfo = {0}; @@ -210,18 +237,11 @@ int getDeviceVersion(char* *firmwareVer, char* *softwareVer, char* *hardwareVer) //#if (HILINK_NETCFG_MODE != SOFTAP_MODE) -static void ReporSwitchStatus(void) -{ - char buff[128] = { 0 }; - int ret = snprintf_s(buff, sizeof(buff), sizeof(buff) - 1, SWITCH_REPORT, sid_switch); - if (ret <= 0) { - HILINK_SAL_NOTICE("ReporSwitchStatus snprintf_s ret:%d\r\n", ret); - return; - } - unsigned int buffLen = strlen(buff); - ret = BLE_SendCustomData(CUSTOM_SEC_DATA, (const unsigned char *)buff, buffLen); - HILINK_SAL_NOTICE("BLE_SendCustomData:%s len:%u,ret:%d\r\n", buff, buffLen, ret); -} +// static void ReporSwitchStatus(void) +// { +// set_connection_mode(CONN_MODE_BLE); // 设置为BLE模式 +// fast_report("switch"); +// } static void ReporAutoUpdateStatus(void) { @@ -295,8 +315,27 @@ static BLE_InitPara g_bleInitParam = { static int BleHandleCustomDataGet(const char *sid) { HILINK_SAL_NOTICE("get %s svc\r\n", sid); + + // 设置为BLE模式并使用统一的fast_report机制 + set_connection_mode(CONN_MODE_BLE); + if (strcmp(sid, "switch") == 0) { - ReporSwitchStatus(); + fast_report("switch"); + return 0; + } else if (strcmp(sid, "brightness") == 0) { + fast_report("brightness"); + return 0; + } else if (strcmp(sid, "cct") == 0) { + fast_report("cct"); + return 0; + } else if (strcmp(sid, "lightMode") == 0) { + fast_report("lightMode"); + return 0; + } else if (strcmp(sid, "progressSwitch") == 0) { + fast_report("progressSwitch"); + return 0; + } else if (strcmp(sid, "colourMode") == 0) { + fast_report("colourMode"); return 0; } else if (strcmp(sid, "update") == 0) { ReporAutoUpdateStatus(); @@ -311,23 +350,48 @@ static int BleHandleCustomDataGet(const char *sid) } // 参考链接 https://device.harmonyos.com/cn/docs/devicepartner/DevicePartner-Guides/device-development-ble-specifications-control-0000001482432154 +// 添加全量状态上报接口,供外部调用 +void BLE_ReportAllDeviceStatus(void) +{ + if (ble_sdk_running) { + // 设置为BLE模式 + set_connection_mode(CONN_MODE_BLE); + + // 使用统一的fast_report机制上报所有服务状态 + fast_report("switch"); + fast_report("brightness"); + fast_report("cct"); + fast_report("lightMode"); + fast_report("progressSwitch"); + fast_report("colourMode"); + + HILINK_SAL_NOTICE("BLE reported all device status using fast_report\r\n"); + } +} + static int BleHandleCustomData(const char *buff, unsigned int len) { if (strcmp((char *)buff, "{}") == 0) { /* 上报全量数据 */ - ReporSwitchStatus(); + set_connection_mode(CONN_MODE_BLE); // 设置为BLE模式 + fast_report("switch"); + fast_report("brightness"); + fast_report("cct"); + fast_report("lightMode"); + fast_report("progressSwitch"); + fast_report("colourMode"); #ifdef CONFIG_SUPPORT_HILINK_INDIE_UPGRADE ReporFwvCheckSum(); #endif return 0; } int ret = -1; - + cJSON *json = cJSON_Parse(buff); if (json == NULL) { return ret; } - + set_connection_mode(CONN_MODE_BLE); // 设置为BLE模式 do { cJSON *sidItem = cJSON_GetObjectItem(json, "sid"); cJSON *dataItem = cJSON_GetObjectItem(json, "data"); @@ -340,10 +404,15 @@ static int BleHandleCustomData(const char *buff, unsigned int len) break; } - if (strcmp(sidItem->valuestring, "allservices") == 0) { + if (strcmp(sidItem->valuestring, "allservices") == 0 || strcmp(sidItem->valuestring, "getStateinfo") == 0) { /* H5 连接上时会给设备发送allservices, 设备需要给H5同步全量状态 */ HILINK_SAL_NOTICE("sync dev status\r\n"); - ReporSwitchStatus(); + fast_report("switch"); + fast_report("brightness"); + fast_report("cct"); + fast_report("lightMode"); + fast_report("progressSwitch"); + fast_report("colourMode"); #ifdef CONFIG_SUPPORT_HILINK_INDIE_UPGRADE ReporFwvCheckSum(); #endif @@ -358,15 +427,76 @@ static int BleHandleCustomData(const char *buff, unsigned int len) ret = 0; break; } else if (strcmp(sidItem->valuestring, "switch") == 0) { - cJSON *item = cJSON_GetObjectItem(dataItem, "on"); - if (item == NULL) { - HILINK_SAL_NOTICE("on json null\r\n"); - break; + // 使用实际的设备控制函数 + char *data_str = cJSON_Print(dataItem); + if (data_str != NULL) { + ret = myhandle_put_switch("switch", data_str, strlen(data_str)); + if (ret == 0) { + fast_report("switch"); // 控制成功后上报状态 + } + ret = 0; + free(data_str); + } + break; + } else if (strcmp(sidItem->valuestring, "brightness") == 0) { + // 亮度控制 + char *data_str = cJSON_Print(dataItem); + if (data_str != NULL) { + ret = myhandle_put_brightness("brightness", data_str, strlen(data_str)); + if (ret == 0) { + fast_report("brightness"); // 控制成功后上报状态 + } + ret = 0; + free(data_str); + } + break; + } else if (strcmp(sidItem->valuestring, "cct") == 0) { + // 色温控制 + char *data_str = cJSON_Print(dataItem); + if (data_str != NULL) { + ret = myhandle_put_cct("cct", data_str, strlen(data_str)); + if (ret == 0) { + fast_report("cct"); // 控制成功后上报状态 + } + ret = 0; + free(data_str); + } + break; + } else if (strcmp(sidItem->valuestring, "lightMode") == 0) { + // 场景模式控制 + char *data_str = cJSON_Print(dataItem); + if (data_str != NULL) { + ret = myhandle_put_lightMode("lightMode", data_str, strlen(data_str)); + if (ret == 0) { + fast_report("lightMode"); // 控制成功后上报状态 + } + ret = 0; + free(data_str); + } + break; + } else if (strcmp(sidItem->valuestring, "progressSwitch") == 0) { + // 渐变时长控制 + char *data_str = cJSON_Print(dataItem); + if (data_str != NULL) { + ret = myhandle_put_progressSwitch("progressSwitch", data_str, strlen(data_str)); + if (ret == 0) { + fast_report("progressSwitch"); // 控制成功后上报状态 + } + ret = 0; + free(data_str); + } + break; + } else if (strcmp(sidItem->valuestring, "colourMode") == 0) { + // 色温模式控制 + char *data_str = cJSON_Print(dataItem); + if (data_str != NULL) { + ret = myhandle_put_colourMode("colourMode", data_str, strlen(data_str)); + if (ret == 0) { + fast_report("colourMode"); // 控制成功后上报状态 + } + ret = 0; + free(data_str); } - sid_switch = item->valueint; - HILINK_Printf("DEMO: Hilink receive a switch cmd, on = %d\r\n", sid_switch); - ReporSwitchStatus(); // 连上手机蓝牙时,发的第一个cjson数据。同步一次设备状态 - ret = 0; break; } else if (strcmp(sidItem->valuestring, "update") == 0) { cJSON *item = cJSON_GetObjectItem(dataItem, "autoUpdateOn"); @@ -395,11 +525,11 @@ static int BleRcvCustomData(unsigned char *buff, unsigned int len) HILINK_SAL_NOTICE("buff is NULL\r\n"); return -1; } - HILINK_SAL_NOTICE("custom data, len: %u, data: %s", len, buff); + HILINK_SAL_NOTICE("custom data, len: %u, data: %s\n", len, buff); /* 处理自定义数据 */ if (BleHandleCustomData((const char *)buff, len) != 0) { - HILINK_SAL_NOTICE("BleHandleCustomData fail"); + HILINK_SAL_NOTICE("BleHandleCustomData fail\n"); return -1; } diff --git a/application/samples/wifi/ohos_connect/hilink_adapt/product/device_profile.h b/application/samples/wifi/ohos_connect/hilink_adapt/product/device_profile.h index ff6eede..b18db4e 100755 --- a/application/samples/wifi/ohos_connect/hilink_adapt/product/device_profile.h +++ b/application/samples/wifi/ohos_connect/hilink_adapt/product/device_profile.h @@ -30,7 +30,7 @@ extern "C" { #define DEVICE_HIVERSION "1.0.0" /* 设备固件版本号 */ -#define FIRMWARE_VER "1.0.6" +#define FIRMWARE_VER "1.0.7" /* 设备硬件版本号 */ #define HARDWARE_VER "1.0.0" /* 设备软件版本号 */ diff --git a/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.c b/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.c index 819e43e..f231907 100755 --- a/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.c +++ b/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.c @@ -409,6 +409,24 @@ int handle_get_cmd(const char* svc_id, const char* in, unsigned int in_len, char return function(svc_id, in, in_len, out,out_len); } +// 全局连接模式变量 +static connection_mode_t g_connection_mode = CONN_MODE_CLOUD; + +// 设置连接模式的接口 +void set_connection_mode(connection_mode_t mode) +{ + if( mode != g_connection_mode) { + g_connection_mode = mode; + HILINK_Printf("Connection mode changed to: %s\r\n", (mode == CONN_MODE_BLE) ? "BLE" : "CLOUD"); + } +} + +// 获取当前连接模式 +connection_mode_t get_connection_mode(void) +{ + return g_connection_mode; +} + // 快速上报函数,用于上报服务状态信息 int fast_report(const char* svc_id) { @@ -416,11 +434,31 @@ int fast_report(const char* svc_id) int len; int err = handle_get_cmd(svc_id, NULL, 0, &payload, (unsigned int *)&len); if(err != 0) { - HILINK_Printf("get msg from %s failed \r\n", svc_id); - return err; + HILINK_Printf("get msg from %s failed \r\n", svc_id); + return err; + } + + // 根据当前连接模式选择上报方式 + if (g_connection_mode == CONN_MODE_BLE) { + // BLE模式:使用BLE_SendCustomData + char buff[256] = {0}; + int ret = snprintf_s(buff, sizeof(buff), sizeof(buff) - 1, "{\"data\":%s,\"sid\":\"%s\"}", payload, svc_id); + if (ret > 0) { + err = BLE_SendCustomData(CUSTOM_SEC_DATA, (const unsigned char *)buff, strlen(buff)); // 0x03 = CUSTOM_SEC_DATA + HILINK_Printf("BLE report %s result is %d, data:[%s] \r\n", svc_id, err, buff); + } else { + err = -1; + HILINK_Printf("BLE report %s snprintf failed\r\n", svc_id); + } + } else { + // 云端模式:使用HILINK_ReportCharState + err = HILINK_ReportCharState(svc_id, payload, len); + HILINK_Printf("CLOUD report %s result is %d, payload:%s \r\n", svc_id, err, payload); + } + + if (payload) { + free(payload); } - err = HILINK_ReportCharState(svc_id, payload, len); - HILINK_Printf("report %s result is %d, payload:%s \r\n", svc_id, err, payload); return err; } /* @@ -446,6 +484,9 @@ int HILINK_PutCharState(const char *svcId, const char *payload, unsigned int len } cJSON_Delete(json); + // 设置为云端连接模式 + set_connection_mode(CONN_MODE_CLOUD); + int err = handle_put_cmd(svcId, payload, len); e_printf("[HILINK_PutCharState] 控制指令处理完成,返回值: %d\r\n", err); return err; @@ -510,6 +551,10 @@ int HILINK_GetCharState(const char *svcId, const char *in, unsigned int inLen, c { return -1; } + + // 设置为云端连接模式 + set_connection_mode(CONN_MODE_CLOUD); + #ifdef CONFIG_SUPPORT_HILINK_INDIE_UPGRADE if (strcmp(svcId, "checkSum") == 0) { return GetFwvCheckSum(out, outLen); diff --git a/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.h b/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.h index cbfcd84..98677c2 100755 --- a/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.h +++ b/application/samples/wifi/ohos_connect/hilink_adapt/product/hilink_device.h @@ -186,6 +186,19 @@ void HILINK_NotifyDevStatus(int status); */ int HILINK_ProcessBeforeRestart(int flag); +// 连接模式枚举定义 +typedef enum { + CONN_MODE_CLOUD = 0, // 云端连接模式 + CONN_MODE_BLE = 1 // BLE本地连接模式 +} connection_mode_t; + +// 连接模式管理接口 +void set_connection_mode(connection_mode_t mode); +connection_mode_t get_connection_mode(void); + +// 快速上报接口 +int fast_report(const char* svc_id); + #ifdef __cplusplus #if __cplusplus } diff --git a/output/SR_light-LPT262_hilink-20250706-1.0.7.fwpkg b/output/SR_light-LPT262_hilink-20250706-1.0.7.fwpkg new file mode 100644 index 0000000..a018a4f Binary files /dev/null and b/output/SR_light-LPT262_hilink-20250706-1.0.7.fwpkg differ diff --git a/output/package(SR_light-LPT262_hilink-20250706-1.0.7).zip b/output/package(SR_light-LPT262_hilink-20250706-1.0.7).zip new file mode 100644 index 0000000..8c33d25 Binary files /dev/null and b/output/package(SR_light-LPT262_hilink-20250706-1.0.7).zip differ