根据Bluetooth HID协议,蓝牙设备在0x11和0x13这两个PSM上监听并接收从蓝牙主机发来的L2CAP连接请求。所以,从Socket连接的角度来看,蓝牙设备其实是L2CAP连接的Host端,而蓝牙主机则是L2CAP连接的Client端,这跟它们在Bluetooth HID协议中的角色正相反。
接下来我就要用Python来实践蓝牙设备端的Socket工作流。
当然,根据上一篇文章的介绍,我首先要用C语言实现一些友好的接口,让Python程序通过ctypes能够很容易地使用。
struct bthidd_t {
int serv_ctrl;
int serv_intr;
int sock_ctrl;
int sock_intr;
int b_shutdown;
};
struct bthidd_t * bthidd_init(void);
void bthidd_exit(struct bthidd_t *p_hidd);
void bthidd_shutdown(struct bthidd_t *p_hidd, int shutdown);
int bthidd_accept(struct bthidd_t *p_hidd);
int bthidd_ctrl_send(struct bthidd_t *p_hidd, char *data, int len);
int bthidd_intr_send(struct bthidd_t *p_hidd, char *data, int len);
由于Mouse和Keyboard设备只需要通过HID Interrupt通道向主机发送HID Report,因此暂时只设计了send接口。以下是这些接口的实现。
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <sys/select.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/l2cap.h>
#define PSMHIDCTL 0x11
#define PSMHIDINT 0x13
struct bthidd_t {
int serv_ctrl;
int serv_intr;
int sock_ctrl;
int sock_intr;
int b_shutdown;
};
// Wrapper for bind, caring for all the surrounding variables
static int bth_bind(int sockfd, unsigned short port) {
struct sockaddr_l2 l2a;
int i;
memset(&l2a, 0, sizeof(l2a));
l2a.l2_family = AF_BLUETOOTH;
bacpy(&l2a.l2_bdaddr, BDADDR_ANY);
l2a.l2_psm = htobs(port);
i = bind(sockfd, (struct sockaddr *)&l2a, sizeof(l2a));
if (0 > i) {
fprintf(stderr, "Bind error (PSM %d): %s.\n", port, strerror(errno));
}
return i;
}
struct bthidd_t * bthidd_init(void) {
struct bthidd_t * p_hidd;
int ret;
p_hidd = malloc(sizeof(struct bthidd_t));
if (!p_hidd) { goto _err; }
memset(p_hidd, 0, sizeof(struct bthidd_t));
// Open, bind and listen socket for HID-Control.
p_hidd->serv_ctrl = socket(AF_BLU