1. 解决频繁频繁掉线的问题:原因是因为ble

sdk需要一开始就初始化,配网的时候只是去控制其广播是否开启。不然会导致一直掉线
This commit is contained in:
2025-05-29 13:08:40 +08:00
parent cb1ea6e364
commit 5ec8b9380c
19 changed files with 333 additions and 278 deletions

View File

@ -1,17 +1,6 @@
#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;
#include "hilink_device.h"
#define CHECK_JSON_ITEM(condition) if(condition) { \
goto lab_end; \
@ -38,7 +27,7 @@ static const scene_preset_t scene_presets[] = {
#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 myhandle_put_brightness(const char* svc_id, const char* payload, unsigned int len)
{
int ret = -1;
cJSON *json = cJSON_Parse(payload);
@ -65,7 +54,7 @@ static int handle_put_brightness(const char* svc_id, const char* payload, unsign
}
// 处理色温控制
static int handle_put_cct(const char* svc_id, const char* payload, unsigned int len)
int myhandle_put_cct(const char* svc_id, const char* payload, unsigned int len)
{
int ret = -1;
cJSON *json = cJSON_Parse(payload);
@ -92,7 +81,7 @@ static int handle_put_cct(const char* svc_id, const char* payload, unsigned int
}
// 处理场景模式控制
static int handle_put_lightMode(const char* svc_id, const char* payload, unsigned int len)
int myhandle_put_lightMode(const char* svc_id, const char* payload, unsigned int len)
{
int ret = -1;
cJSON *json = cJSON_Parse(payload);
@ -119,7 +108,7 @@ static int handle_put_lightMode(const char* svc_id, const char* payload, unsigne
}
// 处理开关控制
static int handle_put_switch(const char* svc_id, const char* payload, unsigned int len)
int myhandle_put_switch(const char* svc_id, const char* payload, unsigned int len)
{
int ret = -1;
cJSON *json = cJSON_Parse(payload);
@ -142,7 +131,7 @@ static int handle_put_switch(const char* svc_id, const char* payload, unsigned i
}
// 获取亮度状态
static int handle_get_brightness(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
int myhandle_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);
@ -154,7 +143,7 @@ static int handle_get_brightness(const char* svc_id, const char* in, unsigned in
}
// 获取色温状态
static int handle_get_cct(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
int myhandle_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);
@ -166,7 +155,7 @@ static int handle_get_cct(const char* svc_id, const char* in, unsigned int in_le
}
// 获取场景模式状态
static int handle_get_lightMode(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
int myhandle_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);
@ -178,7 +167,7 @@ static int handle_get_lightMode(const char* svc_id, const char* in, unsigned int
}
// 获取开关状态
static int handle_get_switch(const char* svc_id, const char* in, unsigned int in_len, char** out, unsigned int* out_len)
int myhandle_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);
@ -190,107 +179,41 @@ static int handle_get_switch(const char* svc_id, const char* in, unsigned int in
}
// 处理渐变时长控制
static int handle_put_smooth_time(const char* svc_id, const char* payload, unsigned int len)
int myhandle_put_progressSwitch(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");
e_printf("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");
e_printf("Invalid progressSwitch 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");
// e_printf("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)
int myhandle_get_progressSwitch(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");
e_printf("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);
}