From 89d3f352d313942e59b20de91117a910a300ccd8 Mon Sep 17 00:00:00 2001 From: hst <284164868@qq.com> Date: Tue, 20 Jun 2023 16:59:44 +0800 Subject: [PATCH] add bluetooth Peripherals Service --- ble_wificonfig/Server/src/gatt-service.c | 189 +++++++++++++++++++++++ 1 file changed, 189 insertions(+) diff --git a/ble_wificonfig/Server/src/gatt-service.c b/ble_wificonfig/Server/src/gatt-service.c index 2c64484..2c2bea8 100644 --- a/ble_wificonfig/Server/src/gatt-service.c +++ b/ble_wificonfig/Server/src/gatt-service.c @@ -51,6 +51,12 @@ #include #include +#include +#include +#include +#include +#include + #define GATT_MGR_IFACE "org.bluez.GattManager1" #define GATT_SERVICE_IFACE "org.bluez.GattService1" #define GATT_CHR_IFACE "org.bluez.GattCharacteristic1" @@ -72,6 +78,16 @@ #define WIFILIST_CHAR_UUID "8AC32D3f-5CB9-4D44-BEC2-EE689169F627" #define DEVICECONTEXT_CHAR_UUID "8AC32D3f-5CB9-4D44-BEC2-EE689169F628" +/* hst Bluetooth Service UUID */ +#define MOORGEN_INC_UUID "0x2A29" +#define TW8013_UUID "0x2A24" +#define FWVERSION_UUID "0x2A26" +#define HWVERSION_UUID "0x2A27" +#define QRCODE_UUID "fb3d0105-ea97-4769-9377-337c2f467236" +#define PRODUCTLD_UUID "fb3d0106-ea97-4769-9377-337c2f467236" +#define IP_UUID "fb3d0107-ea97-4769-9377-337c2f467236" +#define STATUSRESAULT_UUID "fb3d0108-ea97-4769-9377-337c2f467236" + #define BT_NAME "ROCKCHIP" static char reconnect_path[66]; @@ -142,12 +158,84 @@ struct descriptor { * Supported properties are defined at doc/gatt-api.txt. See "Flags" * property of the GattCharacteristic1. */ +static const char *alert_level_props[] = { "read", NULL }; +static const char *level_props[] = { "read", "notify", NULL }; +static const char *write_level_props[] = { "write", NULL }; static const char *ias_alert_level_props[] = { "read", "write", NULL }; static const char *chr_props[] = { "read", "write", "notify", NULL }; static const char *desc_props[] = { "read", "write", NULL }; static void chr_write(struct characteristic *chr, const uint8_t *value, int len); static void chr_iface_destroy(gpointer user_data); +/*hst################################################*/ +char on_board_PID [30]; +#define PID_FILE_PATH "/customer/tuya_key" +#define ETH_NAME "wlan0"//使用的网卡名 + +static void extract_product_id(const char* input_string) +{ + const char* start_delimiter = "\"productID\""; + const char* end_delimiter = "\","; + const char* start_position = strstr(input_string, start_delimiter); + if (start_position != NULL) { + start_position += strlen(start_delimiter) + 7; // 跳过冒号和空格 + const char* end_position = strstr(start_position, end_delimiter); + if (end_position != NULL) { + size_t length = end_position - start_position; + strncpy(on_board_PID, start_position, length); + on_board_PID[length] = '\0'; + } + } +} + +static int read_the_system_PID() +{ + int fd = 0; + int ret = 0; + char file_buff[150]; + fd=open(PID_FILE_PATH,O_RDONLY); + if(fd == -1) { + perror("open file failed!\n"); + goto open_error; + } + ret = read(fd,file_buff,sizeof(file_buff)); + if(ret ==-1) + { + perror("read file failed!\n"); + goto open_error; + } + extract_product_id(file_buff); + close(fd); + return 0; + open_error: + return 1; +} + +char* local_ip= NULL; + +char* get_local_ip() +{ + int sock; + struct sockaddr_in sin; + struct ifreq ifr; + sock = socket(AF_INET, SOCK_DGRAM, 0); + if (sock == -1) { + perror("socket"); + return NULL; + } + strncpy(ifr.ifr_name, ETH_NAME, IFNAMSIZ); + ifr.ifr_name[IFNAMSIZ - 1] = 0; + if (ioctl(sock, SIOCGIFADDR, &ifr) < 0) { + perror("ioctl"); + return NULL; + } + + memcpy(&sin, &ifr.ifr_addr, sizeof(sin)); + close(sock); + return inet_ntoa(sin.sin_addr); +} + + #ifdef DUEROS /********************SERVER API***************************/ @@ -1174,7 +1262,52 @@ static DBusMessage *chr_read_value(DBusConnection *conn, DBusMessage *msg, devicesn = NULL; } } + if (!strcmp(MOORGEN_INC_UUID, chr->uuid)){ + char *moorgen_inc = "moorgen Inc."; + chr_write(chr, moorgen_inc, strlen(moorgen_inc)); + // printf("moorgen lnc \n"); + } + if (!strcmp(TW8013_UUID, chr->uuid)){ + char *tw8013 = "tw8013"; + chr_write(chr, tw8013, strlen(tw8013)); + // printf("TW8013_UUID \n"); + } + + if (!strcmp(FWVERSION_UUID, chr->uuid)){ + char *fwversion = "fwversion"; + chr_write(chr, fwversion, strlen(fwversion)); + // printf("FWVERSION_UUID \n"); + } + + if (!strcmp(HWVERSION_UUID, chr->uuid)){ + char *hwversion = "hwversion"; + chr_write(chr, hwversion, strlen(hwversion)); + // printf("HWVERSION_UUID \n"); + } + + if (!strcmp(QRCODE_UUID, chr->uuid)){ + char *qrcode = "qrcode"; + chr_write(chr, qrcode , strlen(qrcode)); + // printf("QRCODE_UUID \n"); + } + + if (!strcmp(PRODUCTLD_UUID, chr->uuid)){ + char *productld = on_board_PID; + chr_write(chr, productld, strlen(productld)); + // printf("PRODUCTLD_UUID \n"); + } + + if (!strcmp(IP_UUID, chr->uuid)){ + if (local_ip == NULL) { + chr_write(chr, "IP IS NULL", strlen("IP IS NULL")); + // printf(" ip is null!\n"); + } + else { + chr_write(chr, local_ip, strlen(local_ip)); + // printf("IP_UUID \n"); + } + } chr_read(chr, &iter); strncpy(str, chr->value, chr->vlen); str[chr->vlen] = '\0'; @@ -1445,6 +1578,10 @@ static DBusMessage *chr_write_value(DBusConnection *conn, DBusMessage *msg, wifi_config_thread_create(); #endif } + if (!strcmp(STATUSRESAULT_UUID, chr->uuid)){ + strcpy(wifi_security, str); + printf("STATUSRESAULT_UUID %s\n", wifi_security); + } #endif return dbus_message_new_method_return(msg); @@ -1624,6 +1761,8 @@ static void create_wifi_services(void) { char *service_path; uint8_t level = ' '; + // read_the_system_PID(); + // local_ip = get_local_ip(); service_path = register_service(WIFI_SERVICES_UUID); if (!service_path) @@ -1647,6 +1786,56 @@ printf( "----------create wifi service -----------------\n" ); g_free(service_path); return; } + gboolean moorgen_lnc = register_characteristic(MOORGEN_INC_UUID, //Read + &level, sizeof(level), + alert_level_props, + NULL, + desc_props, + service_path); + gboolean tw8013 = register_characteristic(TW8013_UUID, //Read + &level, sizeof(level), + alert_level_props, + NULL, + desc_props, + service_path); + gboolean fwversion = register_characteristic(FWVERSION_UUID, //Read + &level, sizeof(level), + alert_level_props, + NULL, + desc_props, + service_path); + gboolean hwversion = register_characteristic(HWVERSION_UUID, //Read + &level, sizeof(level), + alert_level_props, + NULL, + desc_props, + service_path); + gboolean qrcode = register_characteristic(QRCODE_UUID, //Read + &level, sizeof(level), + alert_level_props, + NULL, + desc_props, + service_path); + read_the_system_PID(); + gboolean productld = register_characteristic(PRODUCTLD_UUID, //Read + &level, sizeof(level), + alert_level_props, + NULL, + desc_props, + service_path); + local_ip = get_local_ip(); + gboolean ip = register_characteristic(IP_UUID, //Read, Notify + &level, sizeof(level), + level_props, + NULL, + desc_props, + service_path); + gboolean statusresaut = register_characteristic(STATUSRESAULT_UUID, //Write + &level, sizeof(level), + write_level_props, + NULL, + desc_props, + service_path); #else gboolean mSecure = register_characteristic(SECURITY_CHAR_UUID, &level, sizeof(level), -- 2.34.1