Selaa lähdekoodia

[bug]fix uart sample bug
[bug]fix get start handle bug
[bug]fix the reg_service bug
[add]add LED Button Service, Device Information Service
[bug]fix gnu option

区荣杰 4 vuotta sitten
vanhempi
sitoutus
2467ffd340

+ 29 - 2
SConscript

@@ -30,6 +30,24 @@ if GetDepend(['PKG_BSAL_SRV_NUS']):
 	src += Split("""
 		profiles/service/bsal_uart/bsal_srv_uart.c
 		""")
+
+if GetDepend(['PKG_BSAL_SRV_LBS']):
+	path += [cwd + '/profiles/service/bsal_lbs']
+	src += Split("""
+		profiles/service/bsal_lbs/bsal_srv_lbs.c
+		""")
+
+if GetDepend(['PKG_BSAL_SRV_DIS']):
+	path += [cwd + '/profiles/service/bsal_dis']
+	src += Split("""
+		profiles/service/bsal_dis/bsal_srv_dis.c
+		""")
+
+if GetDepend(['PKG_BSAL_SRV_HRS']):
+	path += [cwd + '/profiles/service/bsal_hrs']
+	src += Split("""
+		profiles/service/bsal_hrs/bsal_srv_hrs.c
+		""")
 		
 if GetDepend(['PKG_BSAL_SAMPLE_BAS_ONLY']):
 	src += Split("""
@@ -43,6 +61,14 @@ elif GetDepend(['PKG_BSAL_SAMPLE_NUS_ONLY']):
     src += Split("""
 		samples/ble_nus_app.c
 		""")
+elif GetDepend(['PKG_BSAL_SAMPLE_LBS_ONLY']):
+    src += Split("""
+		samples/ble_lbs_app.c
+		""")
+elif GetDepend(['PKG_BSAL_SAMPLE_HRS_ONLY']):
+    src += Split("""
+		samples/ble_hrs_app.c
+		""")
 
 		
 if GetDepend(['PKG_BSAL_NIMBLE_STACK']):
@@ -65,8 +91,9 @@ elif GetDepend(['PKG_BSAL_EMPTY_STACK']):
 		port/empty_stack/bsal_osal_empty.c
 		port/empty_stack/bsal_stack_empty.c
 		""")
-	
-LOCAL_CCFLAGS = ''
+
+if rtconfig.CROSS_TOOL == 'keil':
+    LOCAL_CCFLAGS = ' --gnu'
     
 group = DefineGroup('bsal', src, depend = ['PKG_USING_BSAL'], CPPPATH = path, LOCAL_CCFLAGS = LOCAL_CCFLAGS)
 

+ 18 - 0
inc/bsal_int.h

@@ -171,6 +171,24 @@ typedef enum
 */
 uint16_t bsal_profile_get_start_handle_by_16_uuid(void *stack_ptr, uint16_t uuid);
 
+/**
+* BSAL find the start handle by the uuid
+*
+* @param stack_ptr the point of the stack object
+* @param uuid the 32 bit of the uuid
+* @Note get the profile start handle by the uuid
+*/
+uint16_t bsal_profile_get_start_handle_by_32_uuid(void *stack_ptr, uint32_t uuid);
+
+/**
+* BSAL find the start handle by the uuid
+*
+* @param stack_ptr the point of the stack object
+* @param uuid the point of the 128 bit of the uuid
+* @Note get the profile start handle by the uuid
+*/
+uint16_t bsal_profile_get_start_handle_by_128_uuid(void *stack_ptr, uint8_t *uuid);
+
 /**
 * BSAL get the profile entry by the start handle
 *

+ 27 - 4
port/NIMBLE/bsal_nimble.c

@@ -275,14 +275,23 @@ static BSAL_STATUS bsal_int_srv_profile_reg_service(
             ble_uuid16_t *tmp_uuid = bsal_osif_malloc(sizeof(ble_uuid16_t));
             tmp_uuid->u.type = BLE_UUID_TYPE_16;
             tmp_uuid->value = tmp_srv->uuid->u16.value;
-            nimble_srvs->uuid = &tmp_uuid->u;
+            //nimble_srvs->uuid = &tmp_uuid->u;
+            nimble_srvs[srv_idx].uuid = &tmp_uuid->u;
         }
+//        else if (tmp_srv->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;
+//            memcpy(tmp_uuid->value, tmp_srv->uuid->u128.value,16);
+//            nimble_srvs->uuid = &tmp_uuid->u;
+//        }
         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;
             memcpy(tmp_uuid->value, tmp_srv->uuid->u128.value,16);
-            nimble_srvs->uuid = &tmp_uuid->u;
+            //nimble_srvs->uuid = &tmp_uuid->u;
+            nimble_srvs[srv_idx].uuid = &tmp_uuid->u;
         }
         write_index++;
         //add include=============================================================
@@ -370,13 +379,27 @@ static BSAL_STATUS bsal_int_srv_profile_reg_service(
                 write_index++;
             }
         }
+        srv_idx++;
     }
 
     for (i = 0; p_srv[i].type != 0; i++)
     {
         bsal_uuid_any_t srv_uuid;
-        srv_uuid.u_type = BSAL_UUID_TYPE_16BIT; //16bit
-        srv_uuid.u16.value = p_srv[i].uuid->u16.value;
+        switch (p_srv[i].uuid->u_type)
+        {
+            case BSAL_UUID_TYPE_16BIT:
+                srv_uuid.u_type = BSAL_UUID_TYPE_16BIT; //16bit
+                srv_uuid.u16.value = p_srv[i].uuid->u16.value;
+                break;
+            case BSAL_UUID_TYPE_32BIT:
+                srv_uuid.u_type = BSAL_UUID_TYPE_32BIT; //32bit
+                srv_uuid.u32.value = p_srv[i].uuid->u32.value;
+                break;
+            case BSAL_UUID_TYPE_128BIT:
+                srv_uuid.u_type = BSAL_UUID_TYPE_128BIT; //128bit
+                memcpy(srv_uuid.u128.value, p_srv[i].uuid->u128.value, 16);
+                break;
+        }
         tmp_srv = p_srv + i;
         p_bsal_stack->g_att_index++;
         bsal_profile_insert(bsal_get_local_stack_obj(), p_bsal_stack->g_att_index, p_func, srv_uuid);

+ 28 - 112
profiles/service/bsal_dis/bsal_srv_dis.c

@@ -1,103 +1,42 @@
 /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
  * Change Logs:
  * Date           Author       Notes
- * 2020-05-28     Supperthomas the first version
+ * 2021-09-27     WaterFishJ   the first version
  */
 
-
-
 #include <string.h>
 #include <stdint.h>
 #include <stdio.h>
 #include "bsal.h"
 #include "bsal_osif.h"
-#include "bsal_srv_blufi.h"
-
-
-#define GATT_UUID_BLUFI_PROFILE                   0xFFFF
-#define GATT_UUID_CHAR_BLUFI_V2_WRITE             0xFF01
-#define GATT_UUID_CHAR_BLUFI_V4_INDICATE          0xFF02
-
+#include "bsal_srv_dis.h"
 
-#define BLUEFI_BLE_SERVICE_CHAR_V2_WRITE_INDEX          0x02
-#define BLUEFI_BLE_SERVICE_CHAR_V3_INDICATE_INDEX         0x04
-#define BLUEFI_BLE_SERVICE_CHAR_INDICATE_CCCD_INDEX     (BLUEFI_BLE_SERVICE_CHAR_V3_INDICATE_INDEX + 1)
+unsigned char MANUFACTURER_NAME_STRING[] = "RT-THREAD";
+unsigned char PnP_ID[] = {0x02, 0x19, 0x15, 0xee, 0xee, 0x00, 0x01};
 
-#define BLUFI_RECV_BUFF_SIZE  200
 
-struct basl_blufi_recv_data
-{
-    uint8_t buf[BLUFI_RECV_BUFF_SIZE];
-    uint16_t alloc_len;
-    uint8_t recv_offset;
-};
-
-
-static struct basl_blufi_recv_data blufi_data;
 static P_SRV_GENERAL_CB pfn_bas_cb = NULL;
-uint8_t bsal_blufi_push_data(struct basl_blufi_recv_data *blufi_data, uint8_t length, uint8_t *data)
-{
-    if (blufi_data->recv_offset + length > blufi_data->alloc_len)
-    {
-        //error
-        return 1;
-    }
-    if (data[0] == '{')
-    {
-        //first entry
-        blufi_data->recv_offset = 0;
-        memset(blufi_data->buf, 0, blufi_data->alloc_len);
-    }
-    memcpy(&blufi_data->buf[blufi_data->recv_offset], data, length);
-    blufi_data->recv_offset += length;
-    //check the data
-    if (data[length - 1] == '}')
-    {
-        return 0xff;
-    }
-    return 0;
-}
 
-
-static void profile_callback(void *p)
+static void dis_profile_callback(void *p)
 {
     bsal_callbak_data_t *p_param = (bsal_callbak_data_t *)p;
     bool is_app_cb = false;
-    //BSAL_STATUS bsal_result = BSAL_RESULT_SUCCESS;
+
     if (p_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
     {
-        //NO DEAL
-    }
-    else if (p_param->msg_type == BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE)
-    {
-        //do nothing
-        if (BLUEFI_BLE_SERVICE_CHAR_V2_WRITE_INDEX == p_param->off_handle)
+        if (GATT_SVC_DIS_CHAR_MNS_INDEX == p_param->off_handle)
         {
-            uint8_t ret =  bsal_blufi_push_data(&blufi_data, p_param->length, p_param->data);
-            if (ret == 0xff)
-            {
-                //the data is ready
-                is_app_cb = true;
-                p_param->data = blufi_data.buf;
-                p_param->length = blufi_data.recv_offset;
-                //bsal_osif_printf("\r\n BLUFI: THE RECEIVE DATA IS :%s \r\n", p_param->data);
-            }
+            bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, sizeof(MANUFACTURER_NAME_STRING), MANUFACTURER_NAME_STRING);
         }
-    }
-    else if (p_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
-    {
-        if (BLUEFI_BLE_SERVICE_CHAR_INDICATE_CCCD_INDEX == p_param->off_handle)
+        else if (GATT_SVC_DIS_CHAR_PNPID_INDEX == p_param->off_handle)
         {
-            if (p_param->length == 2)
-            {
-                // uint16_t ccdbit = (uint32_t)p_param->data;
-                is_app_cb = true;
-            }
+            bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, sizeof(PnP_ID), PnP_ID);
         }
+        is_app_cb = true;
     }
     if (is_app_cb && (pfn_bas_cb != NULL))
     {
@@ -105,62 +44,39 @@ static void profile_callback(void *p)
     }
 }
 
-void bsal_le_blufi_svr_init(void *stack_ptr, void *app_callback)
+void bsal_le_dis_svr_init(void *stack_ptr, void *app_callback)
 {
-    struct bsal_gatt_app_srv_def ble_svc_blufi_defs[] =
+    struct bsal_gatt_app_srv_def ble_svc_hid_defs[] =
     {
         {
-            /*** Battery Service. */
+            /*** Device Information Service. */
             .type = BSAL_GATT_UUID_PRIMARY_SERVICE,
-            .uuid = BSAL_UUID16_DECLARE(GATT_UUID_BLUFI_PROFILE),
+            .uuid = BSAL_UUID16_DECLARE(GATT_UUID_DEVICE_INFORMATION),
             .characteristics = (bsal_gatt_chr_def_t[])
             {
                 {
-                    /*** Battery level characteristic */
-                    .uuid = BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BLUFI_V2_WRITE),
-                    .properties = BSAL_ATT_P_WRITE
-                    ,
-                    .permission = GATT_PERM_WRITE,
-                    .value_length = 1,
+                    /*** Manufacturer Name String characteristic */
+                    .uuid = BSAL_UUID16_DECLARE(GATT_UUID_CHAR_MANUFACTURER_NAME_STRING),
+                    .properties = BSAL_ATT_P_READ,
+                    .permission = BSAL_GATT_PERM_READ_NONE,
+                    .value_length = 10,
                 },
                 {
-                    /*** Battery level characteristic */
-                    .uuid = BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BLUFI_V4_INDICATE),
-                    .properties = BSAL_ATT_P_INDICATE
-                    ,
-                    .permission = GATT_PERM_READ | GATT_PERM_WRITE,
-                    .value_length = 1,
+                    /*** PnP ID characteristic */
+                    .uuid = BSAL_UUID16_DECLARE(GATT_UUID_CHAR_PNP_ID),
+                    .properties = BSAL_ATT_P_READ,
+                    .permission = BSAL_GATT_PERM_READ_NONE,
+                    .value_length = 7,
                 },
                 {
                     0, /* No more characteristics in this service. */
                 }
             },
         },
-
         {
             0, /* No more services. */
         },
     };
-    bsal_stack_le_srv_reg_func(stack_ptr, &ble_svc_blufi_defs, (P_SRV_GENERAL_CB *)profile_callback);
-
-    pfn_bas_cb = app_callback;
-    blufi_data.alloc_len = BLUFI_RECV_BUFF_SIZE;
-    blufi_data.recv_offset = 0;
+    bsal_stack_le_srv_reg_func(stack_ptr, &ble_svc_hid_defs, (P_SRV_GENERAL_CB *)dis_profile_callback);
+    pfn_bas_cb = (P_SRV_GENERAL_CB)app_callback;
 }
-
-void bsal_blufi_ble_send_v4_indicate(void *stack_ptr, uint16_t conn_id,  void *p_value, uint16_t length)
-{
-    bsal_uuid_any_t uuid_srv;
-    uuid_srv.u_type = BSAL_UUID_TYPE_16BIT;
-    uuid_srv.u16.value = GATT_UUID_BLUFI_PROFILE;
-    uint16_t  start_handle = bsal_srv_get_start_handle(stack_ptr, uuid_srv);
-    bsal_srv_send_notify_data(stack_ptr, conn_id, start_handle, BLUEFI_BLE_SERVICE_CHAR_INDICATE_CCCD_INDEX, length, p_value);
-}
-
-
-void bsal_le_blufi_svr_deinit(void *stack_ptr)
-{
-    //deinit
-}
-
-

+ 15 - 8
profiles/service/bsal_dis/bsal_srv_dis.h

@@ -1,18 +1,15 @@
 /*
- * Copyright (c) 2006-2020, RT-Thread Development Team
+ * Copyright (c) 2006-2021, RT-Thread Development Team
  *
  * SPDX-License-Identifier: Apache-2.0
  *
  * Change Logs:
  * Date           Author       Notes
- * 2020-05-28     Supperthomas the first version
+ * 2021-09-27     WaterFishJ   the first version
  */
 
-
-
-
-#ifndef __BSAL_SRV_BLUFI_H__
-#define __BSAL_SRV_BLUFI_H__
+#ifndef __BSAL_SRV_DIS_H__
+#define __BSAL_SRV_DIS_H__
 #ifdef __cplusplus
 extern "C" {
 #endif
@@ -20,7 +17,17 @@ extern "C" {
 #include "stdint.h"
 #include "stdbool.h"
 
-void bsal_le_blufi_svr_init(void *stack_ptr, void *app_callback);
+#define GATT_UUID_DEVICE_INFORMATION               BSAL_GATT_SERVICE_DEVICE_INFORMATION
+#define GATT_UUID_CHAR_MANUFACTURER_NAME_STRING    BSAL_UUID_CHAR_MANUFACTURER_NAME_STRING
+#define GATT_UUID_CHAR_PNP_ID                      BSAL_UUID_CHAR_PNP_ID
+
+#define GATT_SVC_DIS_CHAR_MNS_INDEX                2
+#define GATT_SVC_DIS_CHAR_PNPID_INDEX              4
+
+
+void bsal_le_dis_svr_init(void *stack_ptr, void *app_callback);
+    
+
 #ifdef __cplusplus
 }
 #endif

+ 109 - 0
profiles/service/bsal_hrs/bsal_srv_hrs.c

@@ -0,0 +1,109 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-09-010     WaterFishJ   the first version
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <rthw.h>
+
+#include <stdio.h>
+#include <stdint.h>
+#include "bsal.h"
+#include "bsal_osif.h"
+#include "bsal_srv_hrs.h"
+
+static uint16_t hrs_hrm_handle;
+
+/* Sensor location, set to "Chest" */
+static uint8_t body_sens_loc = BODY_SENSOR_LOCATION_CHEST;
+
+static P_SRV_GENERAL_CB pfn_bas_cb = NULL;
+static void hrs_profile_callback(void *p)
+{
+    bsal_callbak_data_t *p_param = (bsal_callbak_data_t *)p;
+    bool is_app_cb = false;
+
+    if (p_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
+    {
+        if (GATT_SVC_BODY_SENSOR_LOCATION_READ_INDEX == p_param->off_handle)
+        {
+            is_app_cb = true;
+            bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, sizeof(uint8_t), &body_sens_loc);
+        }
+    }
+    else if (p_param->msg_type == BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE)
+    {
+        is_app_cb = true;
+    }
+    else if (p_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
+    {
+        if (GATT_SVC_HRS_MEASUREMENT_CHAR_CCCD_INDEX == p_param->off_handle)
+        {
+            if (p_param->length == 2)
+            {
+                is_app_cb = true;
+            }
+        }
+    }
+    if (is_app_cb && (pfn_bas_cb != NULL))
+    {
+        pfn_bas_cb(p_param);
+    }
+}
+
+void bsal_le_hrs_svr_init(void *stack_ptr, void *app_callback)
+{
+    struct bsal_gatt_app_srv_def ble_svc_hrs_defs[] =
+    {
+        {
+            /*** Heart Rate Service. */
+            .type = BSAL_GATT_UUID_PRIMARY_SERVICE,
+            .uuid = BSAL_UUID16_DECLARE(GATT_UUID_HEART_RATE),
+            .characteristics = (bsal_gatt_chr_def_t[])
+            {
+                {
+                    /*** Heart Rate Measurement characteristic */
+                    .uuid = BSAL_UUID16_DECLARE(GATT_UUID_HRS_MEASUREMENT),
+                    .properties = BSAL_ATT_P_NOTIFY,
+                    .val_handle = &hrs_hrm_handle,
+                    .permission = BSAL_GATT_PERM_READ_NONE,
+                    .value_length = 1,
+                },
+                {
+                    /*** Body Sensor Location characteristic */
+                    .uuid = BSAL_UUID16_DECLARE(GATT_UUID_CHAR_BODY_SENSOR_LOCATION),
+                    .properties = BSAL_ATT_P_READ,
+                    .val_handle = &hrs_hrm_handle,
+                    .permission = BSAL_GATT_PERM_READ_NONE,
+                    .value_length = 1,
+                },
+                {
+                    0, /* No more characteristics in this service. */
+                }
+            },
+        },
+        {
+            0, /* No more services. */
+        },
+    };
+    bsal_stack_le_srv_reg_func(stack_ptr, &ble_svc_hrs_defs, (P_SRV_GENERAL_CB *)hrs_profile_callback);
+    pfn_bas_cb = (P_SRV_GENERAL_CB)app_callback;
+}
+
+void bsal_hrs_send_notify_level(void *stack_ptr, uint16_t conn_id,  uint8_t *hr_measurement)
+{
+    bsal_uuid_any_t uuid_srv;
+    uuid_srv.u_type = 16;
+    uuid_srv.u16.value = GATT_UUID_HEART_RATE;
+    uint16_t start_handle = bsal_srv_get_start_handle(stack_ptr, uuid_srv);
+    bsal_srv_send_notify_data(stack_ptr, conn_id, start_handle, GATT_SVC_HRS_MEASUREMENT_CHAR_INDEX, sizeof(hr_measurement), hr_measurement);
+}
+
+
+

+ 40 - 0
profiles/service/bsal_hrs/bsal_srv_hrs.h

@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-09-10     WaterFishJ   the first version
+ */
+
+#ifndef __BSAL_SRV_HRS_H__
+#define __BSAL_SRV_HRS_H__
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <stdint.h>
+#include <stdbool.h>
+#include "bsal.h"
+#define GATT_UUID_HEART_RATE                       BSAL_GATT_SERVICE_HEART_RATE
+#define GATT_UUID_HRS_MEASUREMENT                  BSAL_UUID_CHAR_HEART_RATE_MEASUREMENT
+#define GATT_UUID_CHAR_BODY_SENSOR_LOCATION        BSAL_UUID_CHAR_BODY_SENSOR_LOCATION
+#define GATT_UUID_SERVICE_DEVICE_INFORMATION       BSAL_GATT_SERVICE_DEVICE_INFORMATION
+#define GATT_UUID_CHAR_MANUFACTURER_NAME_STRING    BSAL_UUID_CHAR_MANUFACTURER_NAME_STRING
+#define GATT_UUID_CHAR_MODEL_NUMBER_STRING         BSAL_UUID_CHAR_MODEL_NUMBER_STRING
+
+
+#define GATT_SVC_BODY_SENSOR_LOCATION_READ_INDEX    5
+#define GATT_SVC_HRS_MEASUREMENT_CHAR_INDEX         2
+#define GATT_SVC_HRS_MEASUREMENT_CHAR_CCCD_INDEX    3
+
+#define BODY_SENSOR_LOCATION_CHEST                  0x01
+
+void bsal_le_hrs_svr_init(void *stack_ptr, void *app_callback);
+
+void bsal_hrs_send_notify_level(void *stack_ptr, uint16_t conn_id,  uint8_t *hr_measurement);
+
+#endif
+
+
+

+ 141 - 0
profiles/service/bsal_lbs/bsal_srv_lbs.c

@@ -0,0 +1,141 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-09-27     WaterFishJ   the first version
+ */
+
+#include <rtthread.h>
+#include <rtdevice.h>
+#include <rthw.h>
+
+#include <stdio.h>
+#include <stdint.h>
+#include "bsal.h"
+#include "bsal_osif.h"
+#include "bsal_srv_lbs.h"
+
+/* {00001523-1212-efde-1523-785feabcd123} */
+static const struct bsal_uuid128 gatt_svr_svc_lbs_uuid =
+    BSAL_UUID128_INIT(0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
+                      0xDE, 0xEF, 0x12, 0x12, 0x23, 0x15, 0x00, 0x00);
+
+/* {00001524-1212-efde-1523-785feabcd123} */
+static const struct bsal_uuid128 gatt_svr_chr_button_uuid =
+    BSAL_UUID128_INIT(0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
+                      0xDE, 0xEF, 0x12, 0x12, 0x24, 0x15, 0x00, 0x00);
+
+
+/* {00001525-1212-efde-1523-785feabcd123} */
+static const struct bsal_uuid128 gatt_svr_chr_led_uuid =
+    BSAL_UUID128_INIT(0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
+                      0xDE, 0xEF, 0x12, 0x12, 0x25, 0x15, 0x00, 0x00);
+
+static P_SRV_GENERAL_CB pfn_bas_cb = NULL;
+uint8_t *button_cccd_flag;
+uint8_t led_flag = 0;
+static void lbs_profile_callback(void *p)
+{
+    bsal_callbak_data_t *p_param = (bsal_callbak_data_t *)p;
+    bool is_app_cb = false;
+
+    if (p_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
+    {
+        if (GATT_SVC_LBS_BUTTON_INDEX == p_param->off_handle)
+        {
+            is_app_cb = true;
+            uint8_t button = !rt_pin_read(BUTTON_PIN);
+            bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, 1, &button);
+        }
+        else if (GATT_SVC_LBS_LED_INDEX == p_param->off_handle)
+        {
+            bsal_srv_write_data(p_param->stack_ptr, p_param->start_handle, p_param->off_handle, 1, &led_flag);
+        }
+    }
+    else if (p_param->msg_type == BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE)
+    {
+        if (GATT_SVC_LBS_LED_INDEX == p_param->off_handle)
+        {
+            is_app_cb = true;
+            led_flag = *(p_param->data);
+            rt_pin_write(LED_PIN, !led_flag);
+        }
+    }
+    else if (p_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
+    {
+        if (GATT_SVC_LBS_BUTTON_CCCD_INDEX == p_param->off_handle)
+        {
+            if (p_param->length == 2)
+            {
+                uint16_t  cccbits = p_param->value;
+                if (cccbits & BSAL_GATT_CCC_NOTIFY)
+                {
+                    *button_cccd_flag = 1;
+                }
+                else *button_cccd_flag = 0;
+                is_app_cb = true;
+            }
+        }
+    }
+    if (is_app_cb && (pfn_bas_cb != NULL))
+    {
+        pfn_bas_cb(p_param);
+    }
+}
+
+void bsal_le_lbs_svr_init(void *stack_ptr, void *app_callback, uint8_t *button_flag)
+{
+    struct bsal_gatt_app_srv_def ble_svc_lbs_defs[] =
+    {
+        {
+            /*** LBS Service. */
+            .type = BSAL_GATT_UUID_PRIMARY_SERVICE,
+            .uuid = BSAL_UUID128_DECLARE(0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
+                                         0xDE, 0xEF, 0x12, 0x12, 0x23, 0x15, 0x00, 0x00),
+            .characteristics = (bsal_gatt_chr_def_t[])
+            {
+                {
+                    /*** Button characteristic */
+                    .uuid = BSAL_UUID128_DECLARE(0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
+                                                 0xDE, 0xEF, 0x12, 0x12, 0x24, 0x15, 0x00, 0x00),
+                    .properties = BSAL_ATT_P_READ | BSAL_ATT_P_NOTIFY,
+                    .permission = BSAL_GATT_PERM_READ_NONE,
+                    .value_length = 1,
+                },
+                {
+                    /*** LED characteristic */
+                    .uuid = BSAL_UUID128_DECLARE(0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
+                                                 0xDE, 0xEF, 0x12, 0x12, 0x25, 0x15, 0x00, 0x00),
+                    .properties = BSAL_ATT_P_READ | BSAL_ATT_P_WRITE,
+                    .permission = BSAL_GATT_PERM_READ_NONE,
+                    .value_length = 1,
+                },
+                {
+                    0, /* No more characteristics in this service. */
+                }
+            },
+        },
+        {
+            0, /* No more services. */
+        },
+    };
+    bsal_stack_le_srv_reg_func(stack_ptr, &ble_svc_lbs_defs, (P_SRV_GENERAL_CB *)lbs_profile_callback);
+    pfn_bas_cb = (P_SRV_GENERAL_CB)app_callback;
+    button_cccd_flag = button_flag;
+}
+
+
+void bsal_lbs_send_notify_button(void *stack_ptr, uint16_t conn_id,  uint8_t flag)
+{
+    bsal_uuid_any_t uuid_srv;
+    uuid_srv.u_type = 128;
+    rt_memcpy(uuid_srv.u128.value, gatt_svr_svc_lbs_uuid.value, 16);
+    uint16_t start_handle = bsal_srv_get_start_handle(stack_ptr, uuid_srv);
+    bsal_srv_send_notify_data(stack_ptr, conn_id, start_handle, GATT_SVC_LBS_BUTTON_INDEX, sizeof(uint8_t), &flag);
+}
+
+
+

+ 40 - 0
profiles/service/bsal_lbs/bsal_srv_lbs.h

@@ -0,0 +1,40 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-09-27     WaterFishJ   the first version
+ */
+
+#ifndef __BSAL_SRV_LBS_H__
+#define __BSAL_SRV_LBS_H__
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+#include <stdint.h>
+#include <stdbool.h>
+#include <rtthread.h>
+#include <rtdevice.h>
+
+#include "bsal.h"
+
+#define BUTTON_PIN                                      11
+#define LED_PIN                                         15
+
+#define GATT_SVC_LBS_BUTTON_INDEX                       2
+#define GATT_SVC_LBS_BUTTON_CCCD_INDEX                  3
+#define GATT_SVC_LBS_LED_INDEX                          5
+
+void bsal_le_lbs_svr_init(void *stack_ptr, void *app_callback, uint8_t *button_flag);
+
+void bsal_lbs_send_notify_button(void *stack_ptr, uint16_t conn_id, uint8_t flag);
+
+#endif
+
+
+
+

+ 5 - 9
profiles/service/bsal_uart/bsal_srv_uart.c

@@ -6,6 +6,7 @@
  * Change Logs:
  * Date           Author       Notes
  * 2021-09-09     WaterFishJ   the first version
+ * 2021-09-22     WaterFishJ   fix the send bug
  */
 
 #include <rtthread.h>
@@ -58,9 +59,6 @@ static void profile_callback(void *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)
     {
         //NO DEAL had not finished
@@ -72,7 +70,6 @@ static void profile_callback(void *p)
         {
             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)
@@ -194,7 +191,7 @@ void bsal_bleuart_deinit(void)
     }
 }
 
-void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t conn_id)
+void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t *conn_id)
 {
     int off = 0;
     char ch;
@@ -202,7 +199,7 @@ void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t conn_id)
 
     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_svc_uart_uuid.value, 16);
     uint16_t start_handle = bsal_srv_get_start_handle(stack_ptr, uuid_srv);
 
     rt_kprintf("======== Welcome to enter bluetooth uart mode ========\n");
@@ -229,10 +226,9 @@ void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t conn_id)
                 continue;
             }
         }
-
         console_buf[off] = '\0';
         rt_kprintf("\n");
-        bsal_srv_send_notify_data(stack_ptr, conn_id, start_handle, GATT_SVC_NUS_READ_INDEX, sizeof(console_buf), console_buf);
+        bsal_srv_send_notify_data(stack_ptr, *conn_id, start_handle, GATT_SVC_NUS_READ_INDEX, off + 1, console_buf);
 
         off = 0;
     }
@@ -241,7 +237,7 @@ void bsal_bleuart_uart_proc(void *stack_ptr, uint16_t conn_id)
 }
 
 
-int bsal_bleuart_init(void *stack_ptr, uint16_t conn_id)
+int bsal_bleuart_init(void *stack_ptr, uint16_t *conn_id)
 {
     int rc;
     rt_base_t level;

+ 2 - 1
profiles/service/bsal_uart/bsal_srv_uart.h

@@ -6,6 +6,7 @@
  * Change Logs:
  * Date           Author       Notes
  * 2021-09-09     WaterFishJ   the first version
+ * 2021-09-22     WaterFishJ   fix the send bug
  */
 
 #ifndef __BSAL_SRV_UART_H__
@@ -22,7 +23,7 @@
 
 
 
-int bsal_bleuart_init(void *stack_ptr, uint16_t conn_id);
+int bsal_bleuart_init(void *stack_ptr, uint16_t *conn_id);
 
 void bsal_le_uart_svr_init(void *stack_ptr, void *app_callback);
 

+ 194 - 0
samples/ble_hrs_app.c

@@ -0,0 +1,194 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-09-10     WaterFishJ   the first version
+ */
+
+
+#include "bsal.h"
+#include <stdio.h>
+#include <string.h>
+#include "bsal_osif.h"
+#include "bsal_srv_hrs.h"
+
+#define BSAL_STACK_NAME PKG_BSAL_STACK_NAME
+
+static void *bsal_stack_ptr = NULL;
+static uint16_t bsal_app_conn_handle;
+static uint8_t heart_rate_flag = 0;
+static rt_uint8_t gap_conn_state = BSAL_GAP_CONN_STATE_CONNECTED;
+
+static 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);
+
+    char *adv_name = (char *)bsal_get_device_name(stack_ptr);
+    bsal_adv_data_add_name(tmp_data, strlen(adv_name), adv_name);
+    //bsal_adv_data_add_uuid16(tmp_data, BSAL_GATT_SERVICE_BATTERY_SERVICE);
+    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)
+{
+    T_BSAL_GAP_MSG_DATA  *bsal_gap_msg_data = (T_BSAL_GAP_MSG_DATA *)value;
+    uint8_t bd_addr[6];
+    switch (cb_layer)
+    {
+    case BSAL_CB_LAYER_GAP:
+        switch (cb_sub_event)
+        {
+        case BSAL_CB_STACK_READY:
+            //get mac address
+
+            bsal_osif_printf_info("============stack ready===========\r\n");
+            bsa_app_set_adv_data(stack_ptr);
+            bsal_stack_start_adv(stack_ptr);
+            break;
+        case BSAL_CB_CONNECT_STATUS:
+            bsal_osif_printf_info("============stack connect id %d===========\r\n", bsal_gap_msg_data->gap_conn_state_change.conn_id);
+            if (bsal_gap_msg_data->gap_conn_state_change.new_state == BSAL_GAP_CONN_STATE_CONNECTED)
+            {
+                bsal_app_conn_handle = bsal_gap_msg_data->gap_conn_state_change.conn_id;
+            }
+            else if (bsal_gap_msg_data->gap_conn_state_change.new_state == BSAL_GAP_CONN_STATE_DISCONNECTED)
+            {
+                bsal_stack_start_adv(stack_ptr);
+                heart_rate_flag = 0;
+            }
+            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);
+
+            break;
+        default:
+            break;
+        }
+
+        if (cb_sub_event == BSAL_CB_STACK_READY)
+        {
+            //stack ready
+        }
+
+        break;
+    case BSAL_CB_LAYER_GATT_PROFILE:
+        switch (cb_sub_event)
+        {
+            //save the service start_handle
+            //case uuid profile save start_handle
+            //case SRV_CALLBACK66
+            //save the identity
+        }
+        break;
+    case BSAL_CB_LAYER_SM:
+        break;
+    case BSAL_CB_LAYER_COMMON:
+        //connected save the connect id
+
+        break;
+    case BSAL_CB_LAYER_UNKNOWN:
+        break;
+    default:
+        break;
+    }
+
+}
+
+static void bsal_app_profile_callback(void *p)
+{
+    bsal_callbak_data_t *bsal_param = (bsal_callbak_data_t *)p;
+
+    if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
+    {
+        bsal_osif_printf_info("========callback read from %x====%x=======\r\n", bsal_param->off_handle, bsal_param->srv_uuid.u16.value);
+    }
+    else if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
+    {
+        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 (bsal_param->srv_uuid.u16.value == GATT_UUID_HEART_RATE)//heart_rate_uuid
+        {
+            if (cccbits & BSAL_GATT_CCC_NOTIFY)
+            {
+                bsal_osif_printf_info("=========NOTIFY ENABLE from %x===data cccd %x====%x=====\r\n", bsal_param->off_handle, cccbits, bsal_param->srv_uuid.u16.value);
+                heart_rate_flag = 1;
+            }
+            else
+            {
+                bsal_osif_printf_info("========NOTIFY DISABLE from %x===data cccd %x====%x=====\r\n", bsal_param->off_handle, cccbits, bsal_param->srv_uuid.u16.value);
+                heart_rate_flag = 0;
+            }
+        }
+    }
+    else if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE)
+    {
+        bsal_osif_printf_info("\r\n BSAL: THE DATA IS :%s\r\n", bsal_param->data);
+    }
+}
+
+static void bsal_ble_loop(void *p_param)
+{
+    static uint8_t hrm[2];
+    hrm[0] = 0x06;
+
+    uint8_t heart_rate = 90;
+    while (1)
+    {
+        bsal_osif_delay(1000);
+        bsal_osif_printf_info("====hello world===%d=\r\n", heart_rate_flag);
+        if (heart_rate_flag == 1)
+        {
+            if (heart_rate <= 120)
+            {
+                heart_rate++;
+            }
+            else
+            {
+                heart_rate = 90;
+            }
+            hrm[1] = heart_rate;
+            bsal_hrs_send_notify_level(bsal_stack_ptr, bsal_app_conn_handle, hrm);
+        }
+    }
+}
+
+int bsal_hrs_app(void)
+{
+    void *stack_ptr = bsal_find_stack_ptr(BSAL_STACK_NAME);
+    if (stack_ptr == NULL)
+    {
+        //print error;
+        return 1;
+    }
+    //set iocapability
+
+    bsal_stack_ptr  = stack_ptr;
+    //1. init stack
+    bsal_stack_init(stack_ptr, bsal_app_all_callback);  // init param not start stack
+    // set device name
+    char *device_name = "ble_rtt_hrs";
+    bsal_set_device_name(stack_ptr, strlen(device_name), (uint8_t *)device_name);
+    //2. bond type
+    bsal_set_device_le_bond_type(stack_ptr, false, BSAL_NO_INPUT, BSAL_NO_OUTPUT, BSAL_GAP_AUTHEN_BIT_NO_BONDING, false);
+    //set the bond flag:
+
+    //3. service begin
+    bsal_stack_le_srv_begin(stack_ptr, 1, bsal_app_profile_callback);  //will add 1 service
+    //4. bas_init
+    bsal_le_hrs_svr_init(stack_ptr, bsal_app_profile_callback); //add battery servcie
+
+    //5. srv_end
+    bsal_stack_le_srv_end(stack_ptr);    //end srv add
+
+    //start stack
+    bsal_stack_startup(stack_ptr);    //start she
+
+    bsal_ble_loop(stack_ptr);
+
+    return 0;
+}
+MSH_CMD_EXPORT_ALIAS(bsal_hrs_app, bsal_hrs_app, "bluetoooth heart rate sample");
+

+ 233 - 0
samples/ble_lbs_app.c

@@ -0,0 +1,233 @@
+/*
+ * Copyright (c) 2006-2021, RT-Thread Development Team
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ *
+ * Change Logs:
+ * Date           Author       Notes
+ * 2021-09-27     WaterFishJ   the first version
+ */
+
+#include "bsal.h"
+#include <stdio.h>
+#include <string.h>
+#include "bsal_osif.h"
+#include "bsal_srv_lbs.h"
+
+
+#define BSAL_STACK_NAME PKG_BSAL_STACK_NAME
+
+static void *bsal_stack_ptr = NULL;
+static uint16_t bsal_app_conn_handle;
+static rt_uint8_t gap_conn_state = BSAL_GAP_CONN_STATE_CONNECTED;
+static rt_uint8_t button_cccd_flag;
+
+static 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);
+
+    char *adv_name = (char *)bsal_get_device_name(stack_ptr);
+    bsal_adv_data_add_name(tmp_data, strlen(adv_name), adv_name);
+    //bsal_adv_data_add_uuid16(tmp_data, BSAL_GATT_SERVICE_BATTERY_SERVICE);
+    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)
+{
+    T_BSAL_GAP_MSG_DATA  *bsal_gap_msg_data = (T_BSAL_GAP_MSG_DATA *)value;
+    uint8_t bd_addr[6];
+    switch (cb_layer)
+    {
+    case BSAL_CB_LAYER_GAP:
+        switch (cb_sub_event)
+        {
+        case BSAL_CB_STACK_READY:
+            //get mac address
+
+            bsal_osif_printf_info("============stack ready===========\r\n");
+            bsa_app_set_adv_data(stack_ptr);
+            bsal_stack_start_adv(stack_ptr);
+            break;
+        case BSAL_CB_CONNECT_STATUS:
+            bsal_osif_printf_info("============stack connect id %d===========\r\n", bsal_gap_msg_data->gap_conn_state_change.conn_id);
+            if (bsal_gap_msg_data->gap_conn_state_change.new_state == BSAL_GAP_CONN_STATE_CONNECTED)
+            {
+                bsal_app_conn_handle = bsal_gap_msg_data->gap_conn_state_change.conn_id;
+            }
+            else if (bsal_gap_msg_data->gap_conn_state_change.new_state == BSAL_GAP_CONN_STATE_DISCONNECTED)
+            {
+                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);
+
+            break;
+        default:
+            break;
+        }
+
+        if (cb_sub_event == BSAL_CB_STACK_READY)
+        {
+            //stack ready
+        }
+
+        break;
+    case BSAL_CB_LAYER_GATT_PROFILE:
+        switch (cb_sub_event)
+        {
+            //save the service start_handle
+            //case uuid profile save start_handle
+            //case SRV_CALLBACK66
+            //save the identity
+        }
+        break;
+    case BSAL_CB_LAYER_SM:
+        break;
+    case BSAL_CB_LAYER_COMMON:
+        //connected save the connect id
+
+        break;
+    case BSAL_CB_LAYER_UNKNOWN:
+        break;
+    default:
+        break;
+    }
+
+}
+
+bool lbs_is_uuid(bsal_uuid_any_t *s, bsal_uuid_any_t *u)
+{
+    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;
+
+    if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
+    {
+        bsal_osif_printf_info("========callback read from %x====%x=======\r\n", bsal_param->off_handle, bsal_param->srv_uuid.u16.value);
+    }
+    else if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_INDIFICATION_NOTIFICATION)
+    {
+        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 (lbs_is_uuid(&(bsal_param->srv_uuid), BSAL_UUID128_DECLARE(0x23, 0xD1, 0xBC, 0xEA, 0x5F, 0x78, 0x23, 0x15,
+                      0xDE, 0xEF, 0x12, 0x12, 0x23, 0x15, 0x00, 0x00)))//lbs_uuid
+        {
+            if (cccbits & BSAL_GATT_CCC_NOTIFY)
+            {
+                bsal_osif_printf_info("=========NOTIFY ENABLE from %x===data cccd %x====%x=====\r\n", bsal_param->off_handle, cccbits, bsal_param->srv_uuid.u16.value);
+            }
+            else
+            {
+                bsal_osif_printf_info("========NOTIFY DISABLE from %x===data cccd %x====%x=====\r\n", bsal_param->off_handle, cccbits, bsal_param->srv_uuid.u16.value);
+            }
+        }
+    }
+    else if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_WRITE_CHAR_VALUE)
+    {
+        bsal_osif_printf_info("\r\n BSAL: THE DATA IS :%s\r\n", bsal_param->data);
+    }
+}
+
+rt_sem_t button_sem = 0;
+void button_irq(void *p)
+{
+    rt_sem_release(p);
+}
+
+void bsal_lbs_loop(void *p)
+{
+    button_sem = rt_sem_create("button", 0, RT_IPC_FLAG_FIFO);
+    
+    rt_pin_mode(BUTTON_PIN, PIN_MODE_INPUT_PULLUP);
+    rt_pin_attach_irq(BUTTON_PIN, PIN_IRQ_MODE_RISING_FALLING, button_irq, button_sem);
+    rt_pin_irq_enable(BUTTON_PIN, PIN_IRQ_ENABLE);
+    
+    rt_pin_mode(LED_PIN, PIN_MODE_OUTPUT);
+    rt_pin_write(LED_PIN, PIN_HIGH);
+    
+    uint8_t flag = 0;
+    
+    while (1)
+    {
+        rt_sem_take(button_sem, RT_WAITING_FOREVER);
+        if (button_cccd_flag)
+        {
+            flag = !rt_pin_read(BUTTON_PIN);
+            bsal_lbs_send_notify_button(p, bsal_app_conn_handle, flag);
+        }
+    }
+}
+
+int bsal_lbs_app(void)
+{   
+    void *bsal_test_app_task = RT_NULL;
+    
+    void *stack_ptr = bsal_find_stack_ptr(BSAL_STACK_NAME);
+    if (stack_ptr == NULL)
+    {
+        //print error;
+        return 1;
+    }
+    //set iocapability
+
+    bsal_stack_ptr = stack_ptr;
+    //1. init stack
+    bsal_stack_init(stack_ptr, bsal_app_all_callback);  // init param not start stack
+    // set device name
+    char *device_name = "ble_rtt_lbs";
+    bsal_set_device_name(stack_ptr, strlen(device_name), (uint8_t *)device_name);
+    //2. bond type
+    bsal_set_device_le_bond_type(stack_ptr, false, BSAL_NO_INPUT, BSAL_NO_OUTPUT, BSAL_GAP_AUTHEN_BIT_NO_BONDING, false);
+    //set the bond flag:
+
+    //3. service begin
+    bsal_stack_le_srv_begin(stack_ptr, 1, bsal_app_profile_callback);  //will add 1 service
+
+    //4. lbs init
+    bsal_le_lbs_svr_init(stack_ptr, bsal_app_profile_callback, &button_cccd_flag);
+
+    //5. srv_end
+    bsal_stack_le_srv_end(stack_ptr);    //end srv add
+
+    //6. start stack
+    bsal_stack_startup(stack_ptr);    //start she
+    
+    bsal_test_app_task = rt_thread_create("lbs_task", bsal_lbs_loop, stack_ptr, 2 * 256, 5, 10);
+    if (bsal_test_app_task != RT_NULL)
+    {
+        rt_thread_startup(bsal_test_app_task);
+    }
+    return 0;
+}
+MSH_CMD_EXPORT_ALIAS(bsal_lbs_app, bsal_lbs_app, "bluetoooth LED Button sample");
+
+
+
+
+
+
+
+
+

+ 3 - 7
samples/ble_nus_app.c

@@ -6,6 +6,7 @@
  * Change Logs:
  * Date           Author       Notes
  * 2021-09-09     WaterFishJ   the first version
+ * 2021-09-22     WaterFishJ   fix the send bug
  */
 
 
@@ -97,9 +98,6 @@ 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)
@@ -125,8 +123,6 @@ 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);
-
     if (bsal_param->msg_type == BSAL_CALLBACK_TYPE_READ_CHAR_VALUE)
     {
         bsal_osif_printf_info("========callback read from %x====%x=======\r\n", bsal_param->off_handle, bsal_param->srv_uuid.u16.value);
@@ -136,7 +132,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, 0x01, 0x00, 0x40, 0x6e)))//uart_uuid
         {
             if (cccbits & BSAL_GATT_CCC_NOTIFY)
             {
@@ -187,7 +183,7 @@ int bsal_nus_app(void)
     //6. start stack
     bsal_stack_startup(stack_ptr);    //start she
 
-    bsal_bleuart_init(stack_ptr, bsal_app_conn_handle);
+    bsal_bleuart_init(stack_ptr, &bsal_app_conn_handle);
 
     return 0;
 }

+ 49 - 2
src/bsal.c

@@ -361,9 +361,13 @@ uint16_t bsal_srv_get_start_handle(void *stack_ptr, bsal_uuid_any_t uuid)
     {
         return bsal_profile_get_start_handle_by_16_uuid(stack_ptr, uuid.u16.value);
     }
-    else
+    else if (uuid.u_type == 32)
+    {
+        return bsal_profile_get_start_handle_by_32_uuid(stack_ptr, uuid.u32.value);
+    }
+    else if (uuid.u_type == 128)
     {
-        /* TODO deal with the 128 bit*/
+        return bsal_profile_get_start_handle_by_128_uuid(stack_ptr, uuid.u128.value);
     }
     return 0;
 }
@@ -433,6 +437,49 @@ uint16_t bsal_profile_get_start_handle_by_16_uuid(void *stack_ptr, uint16_t uuid
     }
     return NULL;
 }
+
+
+uint16_t bsal_profile_get_start_handle_by_32_uuid(void *stack_ptr, uint32_t uuid)
+{
+    BSAL_ASSERT_PTR(stack_ptr);
+    uint8_t i = 0;
+    bsal_stack_obj_t *p_bsal_stack =  stack_ptr;
+    for (i = 0 ; i < p_bsal_stack->srv_num; i++)
+    {
+        if ((p_bsal_stack->bsal_srv_objs[i].srv_uuid.u_type == 32) && (p_bsal_stack->bsal_srv_objs[i].srv_uuid.u32.value == uuid))
+        {
+            return p_bsal_stack->bsal_srv_objs[i].start_handle;
+        }
+    }
+    return NULL;
+}
+
+
+uint16_t bsal_profile_get_start_handle_by_128_uuid(void *stack_ptr, uint8_t *uuid)
+{
+    BSAL_ASSERT_PTR(stack_ptr);
+    BSAL_ASSERT_PTR(uuid);
+    uint8_t i = 0, j = 0;
+    bsal_stack_obj_t *p_bsal_stack =  stack_ptr;
+    for (i = 0 ; i < p_bsal_stack->srv_num; i++)
+    {
+        if (p_bsal_stack->bsal_srv_objs[i].srv_uuid.u_type == 128)
+        {
+            for (j = 0; j < 16; j++)
+            {
+                if (p_bsal_stack->bsal_srv_objs[i].srv_uuid.u128.value[j] != uuid[j])
+                {
+                    break;
+                }
+            }
+            if (j == 16)
+            {
+                return p_bsal_stack->bsal_srv_objs[i].start_handle;
+            }
+        }
+    }
+    return NULL;
+}
 //bond about
 
 uint8_t io_capablity[3][2] =