#include "spotlight.h" #include "cJSON.h" // 服务处理函数定义 typedef int (*handle_put_func)(const char* svc_id, const char* payload, unsigned int len); typedef int (*handle_get_func)(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len); // 服务注册信息定义 typedef struct{ // service id char* sid; // HILINK_PutCharState cmd function handle_put_func putFunc; // handle HILINK_GetCharState cmd function handle_get_func getFunc; } HANDLE_SVC_INFO; #define CHECK_JSON_ITEM(condition) if(condition) { \ goto lab_end; \ } extern device_control_t g_device_control; // 场景模式预设值定义 typedef struct { int brightness; int cct; } scene_preset_t; static const scene_preset_t scene_presets[] = { {(INIT_STA__BRIGHTNESS), (INIT_STA__CCT)}, // 模式0:非情景模式 配网成功后的默认 {(50), (4000)}, // 模式1:休闲模式 - 较低亮度,暖色调 {(10), (3000)}, // 模式2:观影模式 - 低亮度,暖色调 {(100), (4000)}, // 模式3:用餐模式 - 中等亮度,自然光 {(80), (3500)}, // 模式4:回家模式 - 较高亮度,自然光 {(100), (CCT_MIN)}, // 模式5:冬天模式 - 中等亮度,暖色调 {(100), (CCT_MAX)}, // 模式6:夏天模式 - 较高亮度,冷色调 }; #define SCENE_MODE_COUNT (sizeof(scene_presets) / sizeof(scene_presets[0])) // 处理亮度控制 static int handle_put_brightness(const char* svc_id, const char* payload, unsigned int len) { int ret = -1; cJSON *json = cJSON_Parse(payload); if (json == NULL) { return -1; } cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_BRIGHTNESS); if (item == NULL || !cJSON_IsNumber(item)) { cJSON_Delete(json); return -1; } int brightness = item->valueint; if (brightness < BRIGHTNESS_MIN) brightness = BRIGHTNESS_MIN; if (brightness > BRIGHTNESS_MAX) brightness = BRIGHTNESS_MAX; // g_device_control.brightness_local = brightness; g_device_control.elightMode = LIGHT_MODE_CUSTOMER; ret = set_light(BRIGHTNESS_REMOTE2LOCAL(brightness), -1); // 只更新亮度,色温保持不变 cJSON_Delete(json); return ret; //异步上报 } // 处理色温控制 static int handle_put_cct(const char* svc_id, const char* payload, unsigned int len) { int ret = -1; cJSON *json = cJSON_Parse(payload); if (json == NULL) { return -1; } cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_CCT); if (item == NULL || !cJSON_IsNumber(item)) { cJSON_Delete(json); return -1; } int cct = item->valueint; if (cct < CCT_MIN) cct = CCT_MIN; if (cct > CCT_MAX) cct = CCT_MAX; // g_device_control.cct_local = cct; g_device_control.elightMode = LIGHT_MODE_CUSTOMER; ret = set_light(-1, CCT_REMOTE2LOCAL(cct)); // 只更新色温,亮度保持不变 cJSON_Delete(json); return ret ;//异步上报 } // 处理场景模式控制 static int handle_put_lightMode(const char* svc_id, const char* payload, unsigned int len) { int ret = -1; cJSON *json = cJSON_Parse(payload); if (json == NULL) { return -1; } cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_MODE); if (item == NULL || !cJSON_IsNumber(item)) { cJSON_Delete(json); return -1; } uint16_t mode = item->valueint; if (mode >= 0 && mode < SCENE_MODE_COUNT) { g_device_control.elightMode = mode; // 同时更新亮度和色温 ret = set_light(BRIGHTNESS_REMOTE2LOCAL(scene_presets[mode].brightness), CCT_REMOTE2LOCAL(scene_presets[mode].cct)); } cJSON_Delete(json); return ret; } // 处理开关控制 static int handle_put_switch(const char* svc_id, const char* payload, unsigned int len) { int ret = -1; cJSON *json = cJSON_Parse(payload); if (json == NULL) { return -1; } cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_ON); if (item == NULL || !cJSON_IsNumber(item)) { cJSON_Delete(json); return -1; } bool on = (item->valueint != 0); g_device_control.on = on; ret = set_switch(on); cJSON_Delete(json); return ret; } // 获取亮度状态 static int handle_get_brightness(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len) { *out_len = 32; *out = (char*)malloc(*out_len); if (NULL == *out) { return -1; } *out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_BRIGHTNESS, BRIGHTNESS_LOCAL2REMOTE(g_device_control.brightness_local)); return 0; } // 获取色温状态 static int handle_get_cct(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len) { *out_len = 32; *out = (char*)malloc(*out_len); if (NULL == *out) { return -1; } *out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_CCT, CCT_LOCAL2REMOTE(g_device_control.cct_local)); return 0; } // 获取场景模式状态 static int handle_get_lightMode(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len) { *out_len = 32; *out = (char*)malloc(*out_len); if (NULL == *out) { return -1; } *out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_MODE, g_device_control.elightMode); return 0; } // 获取开关状态 static int handle_get_switch(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len) { *out_len = 32; *out = (char*)malloc(*out_len); if (NULL == *out) { return -1; } *out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_ON, g_device_control.on); return 0; } // 处理渐变时长控制 static int handle_put_smooth_time(const char* svc_id, const char* payload, unsigned int len) { int ret = -1; e_printf("[handle_put_smooth_time] Received smooth time setting request: %s\n", payload); cJSON *json = cJSON_Parse(payload); if (json == NULL) { e_printf("[handle_put_smooth_time] JSON parsing failed\n"); return -1; } cJSON *item = cJSON_GetObjectItem(json, JSON_FIELD_FADE_TIME); if (item == NULL || !cJSON_IsNumber(item)) { e_printf("[handle_put_smooth_time] Invalid smooth time parameter\n"); cJSON_Delete(json); return -1; } int smooth_time = item->valueint; e_printf("[handle_put_smooth_time] Setting smooth time: %d s\n", smooth_time); ret = set_smooth_time(smooth_time); cJSON_Delete(json); // e_printf("[handle_put_smooth_time] Smooth time setting completed\n"); return ret; } // 获取渐变时长状态 static int handle_get_smooth_time(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len) { *out_len = 32; *out = (char*)malloc(*out_len); if (NULL == *out) { e_printf("[handle_get_smooth_time] Memory allocation failed\n"); return -1; } *out_len = sprintf_s(*out, *out_len, "{\"%s\":%d}", JSON_FIELD_FADE_TIME, g_device_control.fade_time); return 0; } // 服务处理信息注册 HANDLE_SVC_INFO g_device_profile[] = { {SVC_ID_SWITCH, handle_put_switch, handle_get_switch}, {SVC_ID_BRIGHTNESS, handle_put_brightness, handle_get_brightness}, {SVC_ID_CCT, handle_put_cct, handle_get_cct}, {SVC_ID_LIGHT_MODE, handle_put_lightMode, handle_get_lightMode}, {SVC_ID_FADE_TIME, handle_put_smooth_time, handle_get_smooth_time}, }; // 服务总数量 int g_device_profile_count = sizeof(g_device_profile) / sizeof(HANDLE_SVC_INFO); // 辅助函数,用于查找服务注册信息 static HANDLE_SVC_INFO* find_handle(const char* svc_id) { for(int i = 0; i < g_device_profile_count; i++) { HANDLE_SVC_INFO handle = g_device_profile[i]; if(strcmp(handle.sid, svc_id) == 0) { return &g_device_profile[i]; } } return NULL; } // 分发设备控制指令 int device_module_control(const char* svc_id, const char* payload, unsigned int len) { e_printf("[device_module_control] Received control command: svc_id=%s, payload=%s\n", svc_id, payload); HANDLE_SVC_INFO* handle = find_handle(svc_id); if(handle == NULL) { e_printf("[device_module_control] No service handler found\n"); return -1; } handle_put_func function = handle->putFunc; if(function == NULL) { e_printf("[device_module_control] Service handler function is NULL\n"); return -1; } int ret = function(svc_id, payload, len); e_printf("[device_module_control] Control command processing completed, return value: %d\n", ret); return ret; } // 分发获取状态指令 int device_module_get_state(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len) { HANDLE_SVC_INFO* handle = find_handle(svc_id); if(handle == NULL) { return -1; } handle_get_func function = handle->getFunc; if(function == NULL) { return -1; } return function(svc_id, in, in_len, out, out_len); }