查看: 4334|回复: 0

CC254x-BLE协议栈添加服务与特征值

[复制链接]
  • TA的每日心情
    开心
    2019-10-11 13:43
  • 签到天数: 147 天

    连续签到: 1 天

    [LV.7]常住居民III

    发表于 2018-3-26 16:31:39 | 显示全部楼层 |阅读模式
    分享到:
    CC254x-BLE协议栈添加服务与特征值
    针对协议栈版本:CC254x-1.4.2.2
    使用的工程文件:Texas Instruments\BLE-CC254x-1.4.2.2\Projects\ble\SimpleBLEPeripheral\CC2541DBSimpleBLEPeripheral.eww

    实验过程:
    1.修改profile
    2.应用层处理
    3.使用SensorTag测试结果

    (后续有时间会写一篇TI协议栈OSAL任务调度的讲解)
    一.增加服务,增加两个特征值 的宏定义
    位置:simpleGATTprofile.h
    1. // Profile Parameters

    2. #define SIMPLEPROFILE_CHAR1                   0  // RW uint8 - Profile Characteristic 1 value

    3. #define SIMPLEPROFILE_CHAR2                   1  // RW uint8 - Profile Characteristic 2 value

    4. #define SIMPLEPROFILE_CHAR3                   2  // RW uint8 - Profile Characteristic 3 value

    5. #define SIMPLEPROFILE_CHAR4                   3  // RW uint8 - Profile Characteristic 4 value

    6. #define SIMPLEPROFILE_CHAR5                   4  // RW uint8 - Profile Characteristic 5 value

    7. #define SIMPLEPROFILE_CHAR6                   5  // RW uint8 - Profile Characteristic 6 value

    8. #define SIMPLEPROFILE_CHAR7                   6  // RW uint8 - Profile Characteristic 7 value



    9.   

    10. // Simple Profile Service UUID

    11. #define SIMPLEPROFILE_SERV_UUID               0xFFF0

    12. #define SIMPLEPROFILE_TEST_UUID               0xFFE0

    13.    

    14. // Key Pressed UUID

    15. #define SIMPLEPROFILE_CHAR1_UUID            0xFFF1

    16. #define SIMPLEPROFILE_CHAR2_UUID            0xFFF2

    17. #define SIMPLEPROFILE_CHAR3_UUID            0xFFF3

    18. #define SIMPLEPROFILE_CHAR4_UUID            0xFFF4

    19. #define SIMPLEPROFILE_CHAR5_UUID            0xFFF5

    20. #define SIMPLEPROFILE_CHAR6_UUID            0xFFE1

    21. #define SIMPLEPROFILE_CHAR7_UUID            0xFFE2
    复制代码

    1.png
    二.增加服务,增加两个特征值的UUID
    位置:simpleGATTprofile.c
    1. // Simple GATT Profile Test UUID: 0xFFE0

    2. CONST uint8 simpleProfileTestUUID[ATT_BT_UUID_SIZE] =

    3. {

    4.   LO_UINT16(SIMPLEPROFILE_TEST_UUID), HI_UINT16(SIMPLEPROFILE_TEST_UUID)

    5. };



    6. // Characteristic 6 UUID: 0xFFE1

    7. CONST uint8 simpleProfilechar6UUID[ATT_BT_UUID_SIZE] =

    8. {

    9.   LO_UINT16(SIMPLEPROFILE_CHAR6_UUID), HI_UINT16(SIMPLEPROFILE_CHAR6_UUID)

    10. };



    11. // Characteristic 7 UUID: 0xFFE2

    12. CONST uint8 simpleProfilechar7UUID[ATT_BT_UUID_SIZE] =

    13. {

    14.   LO_UINT16(SIMPLEPROFILE_CHAR7_UUID), HI_UINT16(SIMPLEPROFILE_CHAR7_UUID)

    15. };
    复制代码

    2.png
    3.png
    三.增加服务和特征值的属性
    位置:simpleGATTprofile.c
    1. // Simple Profile Service attribute

    2. static CONST gattAttrType_t simpleProfileService = { ATT_BT_UUID_SIZE, simpleProfileServUUID };



    3. // Simple Profile Test attribute

    4. static CONST gattAttrType_t simpleProfileTest = { ATT_BT_UUID_SIZE, simpleProfileTestUUID };
    复制代码
    4.png
    增加特征值6,属性为可读可写
    增加特征值7,属性为Notify(通知)
    1. // Simple Profile Characteristic 6 Properties

    2. static uint8 simpleProfileChar6Props = GATT_PROP_READ | GATT_PROP_WRITE;



    3. // Characteristic 6 Value

    4. static uint8 simpleProfileChar6 = 0;



    5. // Simple Profile Characteristic 6 User Description

    6. static uint8 simpleProfileChar6UserDesp[17] = "Characteristic 6";





    7. // Simple Profile Characteristic 7 Properties

    8. static uint8 simpleProfileChar7Props = GATT_PROP_NOTIFY;



    9. // Characteristic 7 Value

    10. static uint8 simpleProfileChar7 = 0;



    11. // Simple Profile Characteristic 7 Configuration Each client has its own

    12. // instantiation of the Client Characteristic Configuration. Reads of the

    13. // Client Characteristic Configuration only shows the configuration for

    14. // that client and writes only affect the configuration of that client.

    15. static gattCharCfg_t *simpleProfileChar7Config;

    16.                                        

    17. // Simple Profile Characteristic 7 User Description

    18. static uint8 simpleProfileChar7UserDesp[17] = "Characteristic 7";
    复制代码

    5.png
    四.增加注册服务列表
    位置:simpleGATTprofile.c
    1. static gattAttribute_t testProfileAttrTbl[8] =

    2. {

    3.   // Simple Profile Service

    4.   {

    5.     { ATT_BT_UUID_SIZE, primaryServiceUUID }, /* type */

    6.     GATT_PERMIT_READ,                         /* permissions */

    7.     0,                                        /* handle */

    8.     (uint8 *)&simpleProfileTest               /* pValue */

    9.   },



    10.     // Characteristic 6 Declaration

    11.     {

    12.       { ATT_BT_UUID_SIZE, characterUUID },

    13.       GATT_PERMIT_READ,

    14.       0,

    15.       &simpleProfileChar6Props

    16.     },



    17.       // Characteristic Value 6

    18.       {

    19.         { ATT_BT_UUID_SIZE, simpleProfilechar6UUID },

    20.         GATT_PERMIT_READ | GATT_PERMIT_WRITE,

    21.         0,

    22.         &simpleProfileChar6

    23.       },



    24.       // Characteristic 6 User Description

    25.       {

    26.         { ATT_BT_UUID_SIZE, charUserDescUUID },

    27.         GATT_PERMIT_READ,

    28.         0,

    29.         simpleProfileChar6UserDesp

    30.       },



    31.     // Characteristic 7 Declaration

    32.     {

    33.       { ATT_BT_UUID_SIZE, characterUUID },

    34.       GATT_PERMIT_READ,

    35.       0,

    36.       &simpleProfileChar7Props

    37.     },



    38.       // Characteristic Value 7

    39.       {

    40.         { ATT_BT_UUID_SIZE, simpleProfilechar7UUID },

    41.         0,

    42.         0,

    43.         &simpleProfileChar7

    44.       },



    45.       // Characteristic 7 configuration

    46.       {

    47.         { ATT_BT_UUID_SIZE, clientCharCfgUUID },

    48.         GATT_PERMIT_READ | GATT_PERMIT_WRITE,

    49.         0,

    50.         (uint8 *)&simpleProfileChar7Config

    51.       },

    52.       

    53.       // Characteristic 7 User Description

    54.       {

    55.         { ATT_BT_UUID_SIZE, charUserDescUUID },

    56.         GATT_PERMIT_READ,

    57.         0,

    58.         simpleProfileChar7UserDesp

    59.       },

    60. };
    复制代码

    6.png
    五.注册新的服务
    位置:SimpleProfile_AddService

    1. simpleProfileChar4Config = (gattCharCfg_t *)osal_mem_alloc( sizeof(gattCharCfg_t) *

    2.                                                               linkDBNumConns );

    3.   simpleProfileChar7Config = (gattCharCfg_t *)osal_mem_alloc( sizeof(gattCharCfg_t) *

    4.                                                               linkDBNumConns );

    5.   if ( simpleProfileChar4Config == NULL || simpleProfileChar7Config == NULL)

    6.   {     

    7.     return ( bleMemAllocError );

    8.   }

    9.   

    10.   // Initialize Client Characteristic Configuration attributes

    11.   GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar4Config );

    12.   GATTServApp_InitCharCfg( INVALID_CONNHANDLE, simpleProfileChar7Config );

    13.   

    14.   if ( services & SIMPLEPROFILE_SERVICE )

    15.   {

    16.     // Register GATT attribute list and CBs with GATT Server App

    17.     status = GATTServApp_RegisterService( simpleProfileAttrTbl,

    18.                                           GATT_NUM_ATTRS( simpleProfileAttrTbl ),

    19.                                           GATT_MAX_ENCRYPT_KEY_SIZE,

    20.                                           &simpleProfileCBs );

    21.    

    22.     status = GATTServApp_RegisterService( testProfileAttrTbl,

    23.                                           GATT_NUM_ATTRS( testProfileAttrTbl ),

    24.                                           GATT_MAX_ENCRYPT_KEY_SIZE,

    25.                                           &simpleProfileCBs );
    复制代码

    7.png
    六.增加特征值设置处理
    位置:SimpleProfile_SetParameter
    1. case SIMPLEPROFILE_CHAR6:

    2.       if ( len == sizeof ( uint8 ) )

    3.       {

    4.         simpleProfileChar6 = *((uint8*)value);

    5.       }

    6.       else

    7.       {

    8.         ret = bleInvalidRange;

    9.       }

    10.       break;



    11.     case SIMPLEPROFILE_CHAR7:

    12.       if ( len == sizeof ( uint8 ) )

    13.       {

    14.         simpleProfileChar7 = *((uint8*)value);

    15.         

    16.         // See if Notification has been enabled

    17.         GATTServApp_ProcessCharCfg( simpleProfileChar7Config, &simpleProfileChar7, FALSE,

    18.                                     testProfileAttrTbl, GATT_NUM_ATTRS( testProfileAttrTbl ),

    19.                                     INVALID_TASK_ID, simpleProfile_ReadAttrCB );

    20.       }
    复制代码

    8.png
    七.增加特征值获取处理
    位置:SimpleProfile_GetParameter

    1. case SIMPLEPROFILE_CHAR6:

    2.       *((uint8*)value) = simpleProfileChar6;

    3.       break;  







    4.     case SIMPLEPROFILE_CHAR7:

    5.       *((uint8*)value) = simpleProfileChar7;
    复制代码

    9.png
    八.增加特征值读取处理
    位置:simpleProfile_ReadAttrCB
    10.png
    九.增加特征值写入处理
    位置:simpleProfile_WriteAttrCB
    1. case SIMPLEPROFILE_CHAR1_UUID:

    2.       case SIMPLEPROFILE_CHAR3_UUID:

    3.       case SIMPLEPROFILE_CHAR6_UUID:



    4.         //Validate the value

    5.         // Make sure it's not a blob oper

    6.         if ( offset == 0 )

    7.         {

    8.           if ( len != 1 )

    9.           {

    10.             status = ATT_ERR_INVALID_VALUE_SIZE;

    11.           }

    12.         }

    13.         else

    14.         {

    15.           status = ATT_ERR_ATTR_NOT_LONG;

    16.         }

    17.         

    18.         //Write the value

    19.         if ( status == SUCCESS )

    20.         {

    21.           uint8 *pCurValue = (uint8 *)pAttr->pValue;        

    22.           *pCurValue = pValue[0];



    23.           if( pAttr->pValue == &simpleProfileChar1 )

    24.           {

    25.             notifyApp = SIMPLEPROFILE_CHAR1;        

    26.           }

    27.           else if( pAttr->pValue == &simpleProfileChar3 )

    28.           {

    29.             notifyApp = SIMPLEPROFILE_CHAR3;           

    30.           }

    31.           else

    32.           {

    33.             notifyApp = SIMPLEPROFILE_CHAR6;           

    34.           }

    35.         }

    36.             

    37.         break;
    复制代码

    11.png

    到此为止!!!Profile就修改完了。下面开始应用层的处理。

    十. 增加广播数据
    位置:simpleBLEPeripheral.c
    1. 0x03,   // length of this data

    2.   GAP_ADTYPE_16BIT_MORE,      // some of the UUID's, but not all

    3.   LO_UINT16( SIMPLEPROFILE_TEST_UUID ),

    4.   HI_UINT16( SIMPLEPROFILE_TEST_UUID ),
    复制代码

    12.png
    十一.初始化特征值
    位置:SimpleBLEPeripheral_Init
    1. uint8 charValue1 = 1;

    2.     uint8 charValue2 = 2;

    3.     uint8 charValue3 = 3;

    4.     uint8 charValue4 = 4;

    5.     uint8 charValue5[SIMPLEPROFILE_CHAR5_LEN] = { 1, 2, 3, 4, 5 };

    6.     uint8 charValue6 = 6;

    7.     uint8 charValue7 = 7;

    8.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR1, sizeof ( uint8 ), &charValue1 );

    9.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR2, sizeof ( uint8 ), &charValue2 );

    10.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR3, sizeof ( uint8 ), &charValue3 );

    11.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR4, sizeof ( uint8 ), &charValue4 );

    12.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR5, SIMPLEPROFILE_CHAR5_LEN, charValue5 );

    13.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR6, sizeof ( uint8 ), &charValue6 );

    14.     SimpleProfile_SetParameter( SIMPLEPROFILE_CHAR7, sizeof ( uint8 ), &charValue7 );
    复制代码

    13.png

    十二.增加特征值处理应用
    位置:SimpleBLEPeripheral_ProcessEvent
    时间处理函数中,在启动事件中创建周期性事件

    15.png

    周期性事件中调用处理函数performPeriodicTask

    16.png

    处理函数中读取char6的值,并写入char7
    这样当char7改变时,会自动通知serve端

    17.png
    测试结果:
    下载并运行程序,打开SensorTag这款TI官方APP。可以看到设备名。
    1.jpg
    点击设备并选择Service Explorer,可以看到我们添加的UUID为0xffe0的服务

    2.jpg

    点进去可以看到UUID为0xffe1的特征值其属性是读写,0xffe2属性为N(Notify)

    3.jpg



    点选0xffe2特征值,选择打开Notify开关on。


    稍微等待,可以接收到设备notify过来的值06。

    然后返回到UUID为0xffe1的特征值,修改其值为9




    返回0xffe2可以看到设备主动notify过来的值是9


    实验到此结束,你可以在开发板和手机之间传输 PWM等任何数据了。
    其过程只针对TI协议栈CC254x-1.4.2.2这个版本。
    后面有时间,开贴讲解协议栈中负责各层任务调度的OSAL系统。

    回复

    使用道具 举报

    您需要登录后才可以回帖 注册/登录

    本版积分规则

    关闭

    站长推荐上一条 /2 下一条

    手机版|小黑屋|与非网

    GMT+8, 2024-4-16 15:39 , Processed in 0.113504 second(s), 16 queries , MemCache On.

    ICP经营许可证 苏B2-20140176  苏ICP备14012660号-2   苏州灵动帧格网络科技有限公司 版权所有.

    苏公网安备 32059002001037号

    Powered by Discuz! X3.4

    Copyright © 2001-2020, Tencent Cloud.