凌思微-LE5010蓝牙开发(五)

LE5010-凌思微电子蓝牙芯片的开发记录(五)

这篇文章主要介绍凌思微电子蓝牙模块的代码上怎么设置加密模式。

首先代码附上:(注意此例程是基于ble_hid这个demo进行修改的)

/**defgroup SEC Parameter**/
#define OOB_DATA_FLAG                     0x0                               /**< Indicate whether OOB is supported. */
#define AUTHREQ                           (AUTH_MITM | AUTH_BOND)           /**< Set the auth. */
#define KEY_SIZE                          0x10                              /**< Indicate the supported maximum LTK size (range: 7-16). */
#define INIT_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the initial key distribution. */
#define RESP_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the response key distribution. */
#define passkey_number                    {'1','2','3','4','5','6'}         /**< Set the passkey,size(6). */

#include "ls_ble.h"
//set the default security parameters.
struct pair_feature feat_param =
{
    .iocap = BLE_GAP_IO_CAPS_DISPLAY_ONLY,
    .oob = OOB_DATA_FLAG,
    .auth = AUTHREQ,
    .key_size = KEY_SIZE,
    .ikey_dist = INIT_KEY_DIST,
    .rkey_dist = RESP_KEY_DIST
};

struct gap_pin_str passkey =
{
    .pin = passkey_number,
    .str_pad = 0
};
static const struct svc_decl dis_server_svc =
{
    .uuid = dis_svc_uuid,
    .att = (struct att_decl*)dis_server_att_decl,
    .nb_att = DIS_SVC_ATT_NUM,
    .uuid_len = UUID_LEN_16BIT,
    .sec_lvl = 1,
};
static void gap_manager_callback(enum gap_evt_type type, union gap_evt_u *evt, uint8_t con_idx)
{
...
    switch (type)
    {    
    ...
    	case MASTER_PAIR_REQ:
    	{
        	gap_manager_slave_pair_response_send(con_idx, true, &feat_param);
        	gap_manager_passkey_input(con_idx,&passkey);
    	}
        break;
    ...
    }
...
}

上面的代码都是需要的,简单解释下:这个是作为从机的一个hid设备,我们在这个从设备的dis这个服务上设置一个安全级别(.sec_lvl = 1,),只有主设备在发现这个服务的时候便会发现自己的安全级别不够就会触发主设备发起配对加密这个流程,这个时候从机就会上一个MASTER_PAIR_REQ事件,这个时候我们就将发送自己的配对参数以及配对的密钥,这样只要主机输入的密钥正确就可以连接上这个设备了,但是需要注意的是,如果密钥输入错误就需要从机这边做一个断连处理,密钥错误后从机就会上一个ENCRYPT_FAIL事件,这个时候我们就可以使用断连的接口对设备进行断连,断连原因可以设置为HCI_AUTHENTICATION_FAILURE(0x05)。
如果想对蓝牙配对绑定想多一些了解建议查看这篇博客,看完这篇博客后基本上就对蓝牙绑定的流程全了解了:https://www.cnblogs.com/iini/p/12801242.html

/**蓝牙-HCI错误码列表**/
 1 /* Success code */
 2 #define HCI_SUCCESS                                                  0x00
 3 /* Possible error codes */
 4 #define HCI_UNKNOWN_HCI_COMMAND                                      0x01
 5 #define HCI_NO_CONNECTION                                            0x02
 6 #define HCI_HW_FAILURE                                               0x03
 7 #define HCI_PAGE_TIMEOUT                                             0x04
 8 #define HCI_AUTHENTICATION_FAILURE                                   0x05
 9 #define HCI_KEY_MISSING                                              0x06
10 #define HCI_MEMORY_FULL                                              0x07
11 #define HCI_CONN_TIMEOUT                                             0x08
12 #define HCI_MAX_NUMBER_OF_CONNECTIONS                                0x09
13 #define HCI_MAX_NUMBER_OF_SCO_CONNECTIONS_TO_DEVICE                  0x0A
14 #define HCI_ACL_CONNECTION_EXISTS                                    0x0B
15 #define HCI_COMMAND_DISSALLOWED                                      0x0C
16 #define HCI_HOST_REJECTED_DUE_TO_LIMITED_RESOURCES                   0x0D
17 #define HCI_HOST_REJECTED_DUE_TO_SECURITY_REASONS                    0x0E
18 #define HCI_HOST_REJECTED_DUE_TO_REMOTE_DEVICE_ONLY_PERSONAL_SERVICE 0x0F
19 #define HCI_HOST_TIMEOUT                                             0x10
20 #define HCI_UNSUPPORTED_FEATURE_OR_PARAMETER_VALUE                   0x11
21 #define HCI_INVALID_HCI_COMMAND_PARAMETERS                           0x12
22 #define HCI_OTHER_END_TERMINATED_CONN_USER_ENDED                     0x13
23 #define HCI_OTHER_END_TERMINATED_CONN_LOW_RESOURCES                  0x14
24 #define HCI_OTHER_END_TERMINATED_CONN_ABOUT_TO_POWER_OFF             0x15
25 #define HCI_CONN_TERMINATED_BY_LOCAL_HOST                            0x16
26 #define HCI_REPETED_ATTEMPTS                                         0x17
27 #define HCI_PAIRING_NOT_ALLOWED                                      0x18
28 #define HCI_UNKNOWN_LMP_PDU                                          0x19
29 #define HCI_UNSUPPORTED_REMOTE_FEATURE                               0x1A
30 #define HCI_SCO_OFFSET_REJECTED                                      0x1B
31 #define HCI_SCO_INTERVAL_REJECTED                                    0x1C
32 #define HCI_SCO_AIR_MODE_REJECTED                                    0x1D
33 #define HCI_INVALID_LMP_PARAMETERS                                   0x1E
34 #define HCI_UNSPECIFIED_ERROR                                        0x1F
35 #define HCI_UNSUPPORTED_LMP_PARAMETER_VALUE                          0x20
36 #define HCI_ROLE_CHANGE_NOT_ALLOWED                                  0x21
37 #define HCI_LMP_RESPONSE_TIMEOUT                                     0x22
38 #define HCI_LMP_ERROR_TRANSACTION_COLLISION                          0x23
39 #define HCI_LMP_PDU_NOT_ALLOWED                                      0x24
40 #define HCI_ENCRYPTION_MODE_NOT_ACCEPTABLE                           0x25
41 #define HCI_UNIT_KEY_USED                                            0x26
42 #define HCI_QOS_NOT_SUPPORTED                                        0x27
43 #define HCI_INSTANT_PASSED                                           0x28
44 #define HCI_PAIRING_UNIT_KEY_NOT_SUPPORTED                           0x29

代码摘自:lwBT Bluetooth stack
以上代码取自:https://www.cnblogs.com/utank/p/5095692.html

/********************************************** 分界线 **********************************************/

上面的都是主机主动发起配对请求的,下面的介绍下从机主动发起配对请求的这个命令。
话不多说直接上代码:
一些参数配置和上面是一样的。我这边就直接复制。

#define OOB_DATA_FLAG                     0x0                               /**< Indicate whether OOB is supported. */
#define AUTHREQ                           (AUTH_MITM | AUTH_BOND)           /**< Set the auth. */
#define KEY_SIZE                          0x10                              /**< Indicate the supported maximum LTK size (range: 7-16). */
#define INIT_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the initial key distribution. */
#define RESP_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the response key distribution. */
#define passkey_number                    {'1','2','3','4','5','6'}         /**< Set the passkey,size(6). */

#include "ls_ble.h"
//set the default security parameters.
struct pair_feature feat_param =
{
    .iocap = BLE_GAP_IO_CAPS_DISPLAY_ONLY,
    .oob = OOB_DATA_FLAG,
    .auth = AUTHREQ,
    .key_size = KEY_SIZE,
    .ikey_dist = INIT_KEY_DIST,
    .rkey_dist = RESP_KEY_DIST
};

struct gap_pin_str passkey =
{
    .pin = passkey_number,
    .str_pad = 0
};

static void gap_manager_callback(enum gap_evt_type type, union gap_evt_u *evt, uint8_t con_idx)
{
...
    switch (type)
    {    
    ...
    	case CONNECTED:
    	{
    		...
    		if(evt->connected.peer_id == 0xff{
    			gap_manager_slave_security_req(con_idx, true, AUTHREQ);
    		}
    		...
    	}
    	case MASTER_PAIR_REQ:
    	{
        	gap_manager_slave_pair_response_send(con_idx, true, &feat_param);
        	gap_manager_passkey_input(con_idx,&passkey);
    	}
        break;
    ...
    }
...
}

这里面有一个语句需要解释下:

	if(evt->connected.peer_id == 0xff)
	{
		gap_manager_slave_security_req(con_idx, true, AUTHREQ);
	}

这个位置的判断其实是为了配对成功后不再发起这个配对请求,这个peer_id是只有配对完成后才会改变,并且不同的连接设备都会分配一个peer_id,如果一个新的设备连接后,这个peer_id为0xff,否者不为0xff.

/********************************************** 分界线 **********************************************/

有些客户需要不输入密码的配对,手机直接出现一个配对按钮,点击配对便可进行连接,这种的只需要修改下.iocap便可,
直接上代码:

/**defgroup SEC Parameter**/
#define OOB_DATA_FLAG                     0x0                               /**< Indicate whether OOB is supported. */
#define AUTHREQ                           (AUTH_MITM | AUTH_BOND)           /**< Set the auth. */
#define KEY_SIZE                          0x10                              /**< Indicate the supported maximum LTK size (range: 7-16). */
#define INIT_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the initial key distribution. */
#define RESP_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the response key distribution. */

#include "ls_ble.h"
//set the default security parameters.
struct pair_feature feat_param =
{
    .iocap = BLE_GAP_IO_CAPS_NONE,
    .oob = OOB_DATA_FLAG,
    .auth = AUTHREQ,
    .key_size = KEY_SIZE,
    .ikey_dist = INIT_KEY_DIST,
    .rkey_dist = RESP_KEY_DIST
};
static const struct svc_decl dis_server_svc =
{
    .uuid = dis_svc_uuid,
    .att = (struct att_decl*)dis_server_att_decl,
    .nb_att = DIS_SVC_ATT_NUM,
    .uuid_len = UUID_LEN_16BIT,
    .sec_lvl = 1,
};
static void gap_manager_callback(enum gap_evt_type type, union gap_evt_u *evt, uint8_t con_idx)
{
...
    switch (type)
    {    
    ...
    	case MASTER_PAIR_REQ:
    	{
        	gap_manager_slave_pair_response_send(con_idx, true, &feat_param);
    	}
        break;
    ...
    }
...
}

或:

#define OOB_DATA_FLAG                     0x0                               /**< Indicate whether OOB is supported. */
#define AUTHREQ                           (AUTH_MITM | AUTH_BOND)           /**< Set the auth. */
#define KEY_SIZE                          0x10                              /**< Indicate the supported maximum LTK size (range: 7-16). */
#define INIT_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the initial key distribution. */
#define RESP_KEY_DIST                     (KDIST_ENCKEY | KDIST_IDKEY)      /**< Set the response key distribution. */

#include "ls_ble.h"
//set the default security parameters.
struct pair_feature feat_param =
{
    .iocap = BLE_GAP_IO_CAPS_NONE,
    .oob = OOB_DATA_FLAG,
    .auth = AUTHREQ,
    .key_size = KEY_SIZE,
    .ikey_dist = INIT_KEY_DIST,
    .rkey_dist = RESP_KEY_DIST
};

static void gap_manager_callback(enum gap_evt_type type, union gap_evt_u *evt, uint8_t con_idx)
{
...
    switch (type)
    {    
    ...
    	case CONNECTED:
    	{
    		...
    		if(evt->connected.peer_id == 0xff{
    			gap_manager_slave_security_req(con_idx, true, AUTHREQ);
    		}
    		...
    	}
    	case MASTER_PAIR_REQ:
    	{
        	gap_manager_slave_pair_response_send(con_idx, true, &feat_param);
    	}
        break;
    ...
    }
...
}

好了这里将le5010的加密配对的一些代码配置全部讲解完了,收工。

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值