fujianhao 9 tuntia sitten
vanhempi
commit
89b76ff79b
2 muutettua tiedostoa jossa 55 lisäystä ja 72 poistoa
  1. BIN
      .DS_Store
  2. 55 72
      client/main.c

BIN
.DS_Store


+ 55 - 72
client/main.c

@@ -74,6 +74,7 @@ static GList *battery_proxies;
 static GList *admin_devices_proxies;
 
 static redis_client_t *redisClient;
+static int bluetooth_key_status_count = 0;
 
 // 存放当前正在处理的 proxy
 static GDBusProxy *g_current_proxy = NULL;
@@ -202,7 +203,6 @@ void redis_command_safe(const char *format, char *value) {
         redisReply *reply = redisCommand(redisClient->context, format, value);
         if (reply) {
             // 处理回复
-            freeReplyObject(reply);
         }
     }
 }
@@ -232,7 +232,7 @@ int GetCmdValue(char *file, char *key, char *value, int lenth)
         return FALSE ;
     }
 	strcpy(value, iniparser_getstring(ini, key, ""));
-	printf("keystr: \"%s\" ; value: %s.\n", key, value);
+	// printf("keystr: \"%s\" ; value: %s.\n", key, value);
 	iniparser_freedict(ini);
 	return TRUE;
 }
@@ -244,8 +244,8 @@ void control_cmd(int channel, char *cmd)
     switch (channel)
     {
         case BLUETOOTH_CHAN:
-            printf( "bluetooth-channel [%s]\n", cmd );
             redis_command_safe("LPUSH bluetooth-channel %s", cmd );
+			printf( "bluetooth-channel [%s]\n", cmd );
             break;
         default:
             redis_command_safe("LPUSH control-channel %s", cmd );
@@ -255,6 +255,8 @@ void control_cmd(int channel, char *cmd)
 
 int parse_bind_list_dynamic(const char *src, char ***out_list)
 {
+    if (!src || !out_list) return 0;
+
     int count = 0;
     const char *p = src;
     char **list = NULL;
@@ -262,17 +264,31 @@ int parse_bind_list_dynamic(const char *src, char ***out_list)
     while (*p) {
         if (*p == '"') {
             p++;
-            char *end = strchr(p, '"');
+            const char *end = strchr(p, '"');
             if (!end) break;
 
-            list = realloc(list, sizeof(char *) * (count + 1));
-            list[count] = malloc(18);
-            memset(list[count], 0, 18);
-
-            strncpy(list[count], p, 17);
-            list[count][17] = '\0';
-
-            count++;
+            // 计算实际长度,防止配置错误
+            int len = end - p;
+            if (len > 0) {
+                // 安全地重新分配指针数组
+                char **tmp = realloc(list, sizeof(char *) * (count + 1));
+                if (!tmp) {
+                    // 如果分配失败,清理已分配的内存并退出
+                    for(int i=0; i<count; i++) free(list[i]);
+                    free(list);
+                    *out_list = NULL;
+                    return 0;
+                }
+                list = tmp;
+
+                // 根据实际长度分配内存 (+1 用于存放 \0)
+                list[count] = malloc(len + 1);
+                if (list[count]) {
+                    memcpy(list[count], p, len);
+                    list[count][len] = '\0';
+                    count++;
+                }
+            }
             p = end + 1;
         } else {
             p++;
@@ -283,41 +299,6 @@ int parse_bind_list_dynamic(const char *src, char ***out_list)
     return count;
 }
 
-static dbus_bool_t is_device_in_bind_list(const char *current_mac)
-{
-    char value[512];
-    char **mac_list = NULL;
-    int mac_cnt = 0;
-    dbus_bool_t found = FALSE;
-
-    if (current_mac == NULL || strlen(current_mac) < 17)
-        return FALSE;
-
-    // 获取并解析绑定列表
-    if (GetCmdValue(BLUETOOTH_CONF, "bluetooth:bind_list", value, sizeof(value)) <= 0)
-        return FALSE;
-
-    mac_cnt = parse_bind_list_dynamic(value, &mac_list);
-    if (!mac_list)
-        return FALSE;
-
-    // 遍历比较
-    for (int i = 0; i < mac_cnt; i++) {
-        if (mac_list[i] != NULL && strcmp(current_mac, mac_list[i]) == 0) {
-            found = TRUE;
-            break;
-        }
-    }
-
-    // 释放内存
-    for (int i = 0; i < mac_cnt; i++) {
-        free(mac_list[i]);
-    }
-    free(mac_list);
-
-    return found;
-}
-
 /* Note2 */
 void ble_proc(unsigned char sigType, const unsigned char *buf, size_t len, int vals)
 {
@@ -328,7 +309,9 @@ void ble_proc(unsigned char sigType, const unsigned char *buf, size_t len, int v
 		if(len == 23){
 			//bt_shell_printf("=SOS key status=0x%02x\n", buf[22]);
 			if(buf[22] & 0x01){
-				sprintf(cmd,"{\"type\":\"Bluetooth\",\"key_action\":\"yes\"}");
+				bluetooth_key_status_count++;
+				if(bluetooth_key_status_count > 2) bluetooth_key_status_count = 1;
+				sprintf(cmd,"{\"type\":\"Bluetooth\",\"key_action\":\"yes\",\"key_status\":%d}", bluetooth_key_status_count);
 				control_cmd(BLUETOOTH_CHAN, cmd);
 				printf("got a sos and bvolt=%d\n", buf[22]>>2 );
 			  	/**********************************************************
@@ -417,42 +400,42 @@ static void print_iter(const char *label, const char *name,
 	DBusMessageIter subiter;
 	char *entry;
 
-	// char **bind_list = NULL;
-    // int bind_count = 0;
-    // char config_macs[512] = {0};
+	char **bind_list = NULL;
+    int bind_count = 0;
+    char config_macs[512] = {0};
     bool is_matched = false;
 	char *p;
 
-	char *des_mac[] = {"F0:C9:10:01:03:E2","F0:C9:10:01:03:E3"};
+	// char *des_mac[] = {"F0:C9:10:01:03:E2","F0:C9:10:01:03:E3"};
 	//char des_mac[] = "EA:C9:BA:DD:C0:D9";
 	if (iter == NULL) {
 		bt_shell_printf("%s%s is nil\n", label, name);
 		return;
 	}
 
-	// GetCmdValue(BLUETOOTH_CONF, "bluetooth:bind_list", config_macs, sizeof(config_macs));
-	// bind_count = parse_bind_list_dynamic(config_macs, &bind_list);
-
-	// for (int i = 0; i < bind_count; i++) {
-    //     // 检查 label 中是否包含该 MAC
-    //     if (strstr(label, bind_list[i])) {
-    //         is_matched = true;
-    //         break;
-    //     }
-	// }
-	// for (int i = 0; i < bind_count; i++) free(bind_list[i]);
-    // free(bind_list);
-	for (int i = 0; i < 2; i++) {
-        p = strstr(label, des_mac[i]);
-        if (p != NULL) {
-			is_matched = true;
-            // 只要匹配到了其中一个,就跳出循环
-            break; 
+	GetCmdValue(BLUETOOTH_CONF, "bluetooth:bind_list", config_macs, sizeof(config_macs));
+	bind_count = parse_bind_list_dynamic(config_macs, &bind_list);
+
+	if(bind_list != NULL){
+		for (int i = 0; i < bind_count; i++) {
+			// 检查 label 中是否包含该 MAC
+			p = strstr(label, bind_list[i]);
+			if (p != NULL) {
+				is_matched = true;
+				printf("MATCHED\n");
+				break;
+			}
+		}
+	}
+	// 释放内存
+	if (bind_list != NULL) {
+	for (int i = 0; i < bind_count; i++) {
+            if (bind_list[i]) free(bind_list[i]);
         }
+        free(bind_list);
+        bind_list = NULL; 
     }
 
-	// p = strstr(label, des_mac);
-
 	if( is_matched == true ){
 		switch (dbus_message_iter_get_arg_type(iter)) {
 		case DBUS_TYPE_INVALID: