Просмотр исходного кода

[add]support 128bit and 32bit uuid

区荣杰 4 лет назад
Родитель
Сommit
c2c45319a8

+ 10 - 2
port/NIMBLE/bsal_nimble.c

@@ -279,7 +279,11 @@ static BSAL_STATUS bsal_int_srv_profile_reg_service(
         }
         else if (tmp_srv->characteristics[x].uuid->u_type == BSAL_UUID_TYPE_128BIT)
         {
-
+            ble_uuid128_t *tmp_uuid = bsal_osif_malloc(sizeof(ble_uuid128_t));
+            tmp_uuid->u.type = BLE_UUID_TYPE_128;
+            //tmp_uuid->value = tmp_srv->uuid->u16.value;
+            memcpy(tmp_uuid->value, tmp_srv->uuid->u128.value,16);
+            nimble_srvs->uuid = &tmp_uuid->u;
         }
         write_index++;
         //add include=============================================================
@@ -318,7 +322,11 @@ static BSAL_STATUS bsal_int_srv_profile_reg_service(
                 }
                 else if (tmp_srv->characteristics[x].uuid->u_type == BSAL_UUID_TYPE_128BIT)
                 {
-
+                    ble_uuid128_t *tmp_uuid = bsal_osif_malloc(sizeof(ble_uuid128_t));
+                    tmp_uuid->u.type = BLE_UUID_TYPE_128;
+                    //tmp_uuid->value = tmp_srv->uuid->u16.value;
+                    memcpy(tmp_uuid->value, tmp_srv->characteristics[x].uuid->u128.value,16);
+                    nimble_chrs[x].uuid = &tmp_uuid->u;
                 }
                 nimble_chrs[x].access_cb = bsal_ble_svc_bas_access;
                 nimble_chrs[x].arg = bsal_get_local_stack_obj();

+ 38 - 38
profiles/service/bsal_uart/bsal_srv_uart.c

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -7,7 +7,7 @@
  * Date           Author       Notes
  * 2021-09-09     WaterFishJ   the first version
  */
- 
+
 #include <rtthread.h>
 #include <rtdevice.h>
 #include <rthw.h>
@@ -27,18 +27,18 @@
 /* {6E400001-B5A3-F393-E0A9-E50E24DCCA9E} */
 static const struct bsal_uuid128 gatt_svr_svc_uart_uuid =
     BSAL_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
-                     0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e);
+                      0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e);
 
 /* {6E400002-B5A3-F393-E0A9-E50E24DCCA9E} */
 static const struct bsal_uuid128 gatt_svr_chr_uart_write_uuid =
     BSAL_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
-                     0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e);
+                      0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e);
 
 
 /* {6E400003-B5A3-F393-E0A9-E50E24DCCA9E} */
 static const struct bsal_uuid128 gatt_svr_chr_uart_read_uuid =
     BSAL_UUID128_INIT(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
-                     0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e);
+                      0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e);
 
 /* Pointer to a console buffer */
 static rt_uint8_t *console_buf;
@@ -55,30 +55,30 @@ static struct bleuart_console bleuart = {0};
 static P_SRV_GENERAL_CB pfn_bas_cb = NULL;
 static void profile_callback(void *p)
 {
-		bsal_callbak_data_t *p_param = (bsal_callbak_data_t *)p;
+    bsal_callbak_data_t *p_param = (bsal_callbak_data_t *)p;
     bool is_app_cb = false;
-		
-		rt_kprintf("type = %d\n", p_param->msg_type);
-		rt_kprintf("profile callback u_type: %d\n", p_param->srv_uuid.u_type);
-	
-		if (p_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
+
+    rt_kprintf("type = %d\n", p_param->msg_type);
+    rt_kprintf("profile callback u_type: %d\n", p_param->srv_uuid.u_type);
+
+    if (p_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
     {
         //NO DEAL had not finished
-//				rt_kprintf("read_index = %d\n", p_param->off_handle);
-				is_app_cb = true;
+//              rt_kprintf("read_index = %d\n", p_param->off_handle);
+        is_app_cb = true;
     }
     else if (p_param->msg_type == BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE)
     {
-				if (GATT_SVC_NUS_WRITE_INDEX == p_param->off_handle)
+        if (GATT_SVC_NUS_WRITE_INDEX == p_param->off_handle)
         {
-						is_app_cb = true;
-						rt_device_write(rt_console_get_device(), 0, (char *)p_param->data, p_param->length);
-						rt_device_write(rt_console_get_device(), 0, "\n", 1);
-				}
-		}
-		else if (p_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
+            is_app_cb = true;
+            rt_device_write(rt_console_get_device(), 0, (char *)p_param->data, p_param->length);
+            rt_device_write(rt_console_get_device(), 0, "\n", 1);
+        }
+    }
+    else if (p_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
     {
-//				rt_kprintf("CCCD off_handle = %d\n", p_param->off_handle);
+//              rt_kprintf("CCCD off_handle = %d\n", p_param->off_handle);
         if (GATT_SVC_NUS_CHAR_CCCD_INDEX == p_param->off_handle)
         {
             if (p_param->length == 2)
@@ -88,7 +88,7 @@ static void profile_callback(void *p)
             }
         }
     }
-		if (is_app_cb && (pfn_bas_cb != NULL))
+    if (is_app_cb && (pfn_bas_cb != NULL))
     {
         pfn_bas_cb(p_param);
     }
@@ -102,27 +102,27 @@ void bsal_le_uart_svr_init(void *stack_ptr, void *app_callback)
             /*** Uart Service. */
             .type = BSAL_GATT_UUID_PRIMARY_SERVICE,
             .uuid = BSAL_UUID128_DECLARE(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
-                     0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e),//(bsal_uuid_any_t *)&battery_srv,//BSAL_UUID16_DECLARE(GATT_UUID_BATTERY),
+                                         0x93, 0xf3, 0xa3, 0xb5, 0x01, 0x00, 0x40, 0x6e),//(bsal_uuid_any_t *)&battery_srv,//BSAL_UUID16_DECLARE(GATT_UUID_BATTERY),
             .characteristics = (bsal_gatt_chr_def_t[])
             {
                 {
                     /*** Uart read characteristic */
                     .uuid = BSAL_UUID128_DECLARE(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
-                     0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e),//(bsal_uuid_any_t *)&bas_char_bas_level,//BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BAS_LEVEL),
+                                                 0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e),//(bsal_uuid_any_t *)&bas_char_bas_level,//BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BAS_LEVEL),
                     .properties = BSAL_ATT_P_NOTIFY,
                     .permission = BSAL_GATT_PERM_READ_NONE,
                     .value_length = 1,
                 },
-								{
-										/*** Uart write characteristic */
-										.uuid = BSAL_UUID128_DECLARE(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
-                     0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e),//(bsal_uuid_any_t *)&bas_char_bas_level,//BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BAS_LEVEL),
+                {
+                    /*** Uart write characteristic */
+                    .uuid = BSAL_UUID128_DECLARE(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
+                                                 0x93, 0xf3, 0xa3, 0xb5, 0x02, 0x00, 0x40, 0x6e),//(bsal_uuid_any_t *)&bas_char_bas_level,//BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BAS_LEVEL),
                     .properties = BSAL_ATT_P_WRITE
                     | BSAL_ATT_P_WRITE_WITHOUT_RESPONSE
                     ,
                     .permission = BSAL_GATT_PERM_WRITE_NONE,
                     .value_length = 1,
-								},
+                },
                 {
                     0, /* No more characteristics in this service. */
                 }
@@ -202,10 +202,10 @@ void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t conn_id)
     int off = 0;
     char ch;
     struct os_mbuf *om;
-	
-		bsal_uuid_any_t uuid_srv;
+
+    bsal_uuid_any_t uuid_srv;
     uuid_srv.u_type = BSAL_UUID_TYPE_128BIT;
-		rt_memcpy(uuid_srv.u128.value, gatt_svr_chr_uart_read_uuid.value, 16);
+    rt_memcpy(uuid_srv.u128.value, gatt_svr_chr_uart_read_uuid.value, 16);
 //    uuid_srv.u128.value = ;
     uint16_t start_handle = bsal_srv_get_start_handle(stack_ptr, uuid_srv);
 
@@ -224,7 +224,7 @@ void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t conn_id)
                     console_buf[off--] = 0;
                     rt_kprintf("\b \b");
                 }
-								continue;
+                continue;
             }
             else
             {
@@ -232,14 +232,14 @@ void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t conn_id)
                 rt_kprintf("%c", ch);
                 continue;
             }
-        }    
+        }
 
         console_buf[off] = '\0';
         rt_kprintf("\n");
-//				rt_kprintf("send connid = %d\n", conn_id);
-//				rt_kprintf("send start_handle = %d\n", start_handle);
-//				bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, sizeof(console_buf), console_buf);
-				bsal_srv_send_notify_data(stack_ptr, conn_id, start_handle, GATT_SVC_NUS_READ_INDEX, sizeof(console_buf), console_buf);
+//              rt_kprintf("send connid = %d\n", conn_id);
+//              rt_kprintf("send start_handle = %d\n", start_handle);
+//              bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, sizeof(console_buf), console_buf);
+        bsal_srv_send_notify_data(stack_ptr, conn_id, start_handle, GATT_SVC_NUS_READ_INDEX, sizeof(console_buf), console_buf);
 //        om = ble_hs_mbuf_from_flat(console_buf, off);
 //        if (!om) {
 //            return;

+ 4 - 4
profiles/service/bsal_uart/bsal_srv_uart.h

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -16,9 +16,9 @@
 #include "bsal.h"
 
 
-#define GATT_SVC_NUS_READ_INDEX				2
-#define GATT_SVC_NUS_WRITE_INDEX			5
-#define GATT_SVC_NUS_CHAR_CCCD_INDEX	3
+#define GATT_SVC_NUS_READ_INDEX             2
+#define GATT_SVC_NUS_WRITE_INDEX            5
+#define GATT_SVC_NUS_CHAR_CCCD_INDEX    3
 
 
 

+ 1 - 1
samples/ble_bas_only_app.c

@@ -29,7 +29,7 @@ uint16_t battery_start_handle = 0;
 void bsa_app_set_adv_data(void *stack_ptr)
 {
     uint8_t tmp_data[32] = {0} ; //must be zero
-    bsal_le_adv_data_add_flag(tmp_data, BSAL_GAP_ADTYPE_FLAGS_LIMITED | BSAL_GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED);
+    bsal_le_adv_data_add_flag(tmp_data, BSAL_GAP_ADTYPE_FLAGS_BREDR_NOT_SUPPORTED);
 
     char *adv_name = (char *)bsal_get_device_name(stack_ptr);
     bsal_adv_data_add_name(tmp_data, strlen(adv_name), adv_name);

+ 39 - 39
samples/ble_nus_app.c

@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
@@ -32,7 +32,7 @@ static void bsa_app_set_adv_data(void *stack_ptr)
     bsal_set_le_adv_data_user(stack_ptr, tmp_data);
 }
 
-static void bsal_app_all_callback(void *stack_ptr, uint8_t cb_layer, uint16_t cb_sub_event, uint8_t value_length , void *value)
+static void bsal_app_all_callback(void *stack_ptr, uint8_t cb_layer, uint16_t cb_sub_event, uint8_t value_length, void *value)
 {
     T_BSAL_GAP_MSG_DATA  *bsal_gap_msg_data = (T_BSAL_GAP_MSG_DATA *)value;
     uint8_t bd_addr[6];
@@ -59,7 +59,7 @@ static void bsal_app_all_callback(void *stack_ptr, uint8_t cb_layer, uint16_t cb
                 bsal_stack_start_adv(stack_ptr);
             }
             bsal_osif_printf_info("BSAL: conn_id %d old_state %d new_state %d, disc_cause 0x%x",
-                                  bsal_gap_msg_data->gap_conn_state_change.conn_id , gap_conn_state, bsal_gap_msg_data->gap_conn_state_change.new_state, bsal_gap_msg_data->gap_conn_state_change.disc_cause);
+                                  bsal_gap_msg_data->gap_conn_state_change.conn_id, gap_conn_state, bsal_gap_msg_data->gap_conn_state_change.new_state, bsal_gap_msg_data->gap_conn_state_change.disc_cause);
 
             break;
         default:
@@ -99,35 +99,35 @@ static void bsal_app_all_callback(void *stack_ptr, uint8_t cb_layer, uint16_t cb
 
 bool nus_is_uuid(bsal_uuid_any_t *s, bsal_uuid_any_t *u)
 {
-		rt_kprintf("s_uuid_type: %d\n", s->u_type);
-		rt_kprintf("u_uuid_type: %d\n", u->u_type);
-		rt_kprintf("s_uuid_value: %d\n", s->u16.value);
-		if (s->u_type == u->u_type)
-		{
-				switch (s->u_type)
-				{
-						case BSAL_UUID_TYPE_128BIT:
-							for (rt_uint8_t i = 0; i < 16; i++)
-							{
-									if (s->u128.value[i] != u->u128.value[i])	return false;
-							}
-							return true;
-						case BSAL_UUID_TYPE_16BIT:
-								break;
-						case BSAL_UUID_TYPE_32BIT:
-								break;
-						default:
-								return false;
-				}	
-		}
-		else	return false;
+    rt_kprintf("s_uuid_type: %d\n", s->u_type);
+    rt_kprintf("u_uuid_type: %d\n", u->u_type);
+    rt_kprintf("s_uuid_value: %d\n", s->u16.value);
+    if (s->u_type == u->u_type)
+    {
+        switch (s->u_type)
+        {
+        case BSAL_UUID_TYPE_128BIT:
+            for (rt_uint8_t i = 0; i < 16; i++)
+            {
+                if (s->u128.value[i] != u->u128.value[i])   return false;
+            }
+            return true;
+        case BSAL_UUID_TYPE_16BIT:
+            break;
+        case BSAL_UUID_TYPE_32BIT:
+            break;
+        default:
+            return false;
+        }
+    }
+    else    return false;
 }
 
 static void bsal_app_profile_callback(void *p)
 {
     bsal_callbak_data_t *bsal_param = (bsal_callbak_data_t *)p;
-	
-		rt_kprintf("type = %d\n", bsal_param->msg_type);
+
+    rt_kprintf("type = %d\n", bsal_param->msg_type);
 
     if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
     {
@@ -138,7 +138,7 @@ static void bsal_app_profile_callback(void *p)
         uint16_t  cccbits = bsal_param->value;
         bsal_osif_printf_info("======callback notify from %x===data cccd %x====%x=====\r\n", bsal_param->off_handle, cccbits, bsal_param->srv_uuid.u16.value);
         if (nus_is_uuid(&(bsal_param->srv_uuid), BSAL_UUID128_DECLARE(0x9e, 0xca, 0xdc, 0x24, 0x0e, 0xe5, 0xa9, 0xe0,
-                     0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e)))//uart_read_uuid
+                        0x93, 0xf3, 0xa3, 0xb5, 0x03, 0x00, 0x40, 0x6e)))//uart_read_uuid
         {
             if (cccbits & BSAL_GATT_CCC_NOTIFY)
             {
@@ -179,19 +179,19 @@ int bsal_nus_app(void)
 
     //3. service begin
     bsal_stack_le_srv_begin(stack_ptr, 1, bsal_app_profile_callback);  //will add 1 service
-		
-		//4. uart init
-		bsal_le_uart_svr_init(stack_ptr, bsal_app_profile_callback);
-		
-		//5. srv_end
+
+    //4. uart init
+    bsal_le_uart_svr_init(stack_ptr, bsal_app_profile_callback);
+
+    //5. srv_end
     bsal_stack_le_srv_end(stack_ptr);    //end srv add
-		
-		//start stack
+
+    //start stack
     bsal_stack_startup(stack_ptr);    //start she
-		
-		bsal_bleuart_init(stack_ptr, bsal_app_conn_handle);
-		
-		return 0;
+
+    bsal_bleuart_init(stack_ptr, bsal_app_conn_handle);
+
+    return 0;
 }
 MSH_CMD_EXPORT_ALIAS(bsal_nus_app, bsal_nus_app, "bluetoooth uart sample");