Skip to content

Commit aa93fed

Browse files
committed
peripheral: Add initial code for btsensor application
1 parent 1c01a9f commit aa93fed

13 files changed

+1601
-1
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ tools/bnep-tester
112112
tools/btattach
113113
tools/btmgmt
114114
tools/btsnoop
115+
peripheral/btsensor
115116
monitor/btmon
116117
emulator/btvirt
117118
emulator/b1ee

Makefile.tools

+11-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ monitor_btmon_LDADD = lib/libbluetooth-internal.la \
3838
endif
3939

4040
if EXPERIMENTAL
41-
noinst_PROGRAMS += emulator/btvirt emulator/b1ee emulator/hfp tools/3dsp \
41+
noinst_PROGRAMS += emulator/btvirt emulator/b1ee emulator/hfp \
42+
peripheral/btsensor tools/3dsp \
4243
tools/mgmt-tester tools/gap-tester \
4344
tools/l2cap-tester tools/sco-tester \
4445
tools/smp-tester tools/hci-tester \
@@ -62,6 +63,15 @@ emulator_b1ee_LDADD = src/libshared-mainloop.la
6263
emulator_hfp_SOURCES = emulator/hfp.c
6364
emulator_hfp_LDADD = src/libshared-mainloop.la
6465

66+
peripheral_btsensor_SOURCES = peripheral/main.c \
67+
peripheral/efivars.h peripheral/efivars.c \
68+
peripheral/attach.h peripheral/attach.c \
69+
peripheral/log.h peripheral/log.c \
70+
peripheral/gap.h peripheral/gap.c \
71+
peripheral/gatt.h peripheral/gatt.c
72+
peripheral_btsensor_LDADD = src/libshared-mainloop.la \
73+
lib/libbluetooth-internal.la
74+
6575
tools_3dsp_SOURCES = tools/3dsp.c monitor/bt.h
6676
tools_3dsp_LDADD = src/libshared-mainloop.la
6777

peripheral/attach.c

+144
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
/*
2+
*
3+
* BlueZ - Bluetooth protocol stack for Linux
4+
*
5+
* Copyright (C) 2015 Intel Corporation. All rights reserved.
6+
*
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*
22+
*/
23+
24+
#ifdef HAVE_CONFIG_H
25+
#include <config.h>
26+
#endif
27+
28+
#include <fcntl.h>
29+
#include <unistd.h>
30+
#include <sys/ioctl.h>
31+
32+
#include "lib/bluetooth.h"
33+
#include "lib/hci.h"
34+
#include "lib/hci_lib.h"
35+
#include "tools/hciattach.h"
36+
#include "peripheral/attach.h"
37+
38+
static const char *serial_dev = "/dev/ttyS1";
39+
static int serial_fd = -1;
40+
41+
static int open_serial(const char *path)
42+
{
43+
struct termios ti;
44+
int fd, saved_ldisc, ldisc = N_HCI;
45+
46+
fd = open(path, O_RDWR | O_NOCTTY);
47+
if (fd < 0) {
48+
perror("Failed to open serial port");
49+
return -1;
50+
}
51+
52+
if (tcflush(fd, TCIOFLUSH) < 0) {
53+
perror("Failed to flush serial port");
54+
close(fd);
55+
return -1;
56+
}
57+
58+
if (ioctl(fd, TIOCGETD, &saved_ldisc) < 0) {
59+
perror("Failed get serial line discipline");
60+
close(fd);
61+
return -1;
62+
}
63+
64+
/* Switch TTY to raw mode */
65+
memset(&ti, 0, sizeof(ti));
66+
cfmakeraw(&ti);
67+
68+
ti.c_cflag |= (B115200 | CLOCAL | CREAD);
69+
70+
/* Set flow control */
71+
ti.c_cflag |= CRTSCTS;
72+
73+
if (tcsetattr(fd, TCSANOW, &ti) < 0) {
74+
perror("Failed to set serial port settings");
75+
close(fd);
76+
return -1;
77+
}
78+
79+
if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
80+
perror("Failed set serial line discipline");
81+
close(fd);
82+
return -1;
83+
}
84+
85+
printf("Switched line discipline from %d to %d\n", saved_ldisc, ldisc);
86+
87+
return fd;
88+
}
89+
90+
static int attach_proto(const char *path, unsigned int proto,
91+
unsigned int flags)
92+
{
93+
int fd, dev_id;
94+
95+
fd = open_serial(path);
96+
if (fd < 0)
97+
return -1;
98+
99+
if (ioctl(fd, HCIUARTSETFLAGS, flags) < 0) {
100+
perror("Failed to set flags");
101+
close(fd);
102+
return -1;
103+
}
104+
105+
if (ioctl(fd, HCIUARTSETPROTO, proto) < 0) {
106+
perror("Failed to set protocol");
107+
close(fd);
108+
return -1;
109+
}
110+
111+
dev_id = ioctl(fd, HCIUARTGETDEVICE);
112+
if (dev_id < 0) {
113+
perror("Failed to get device id");
114+
close(fd);
115+
return -1;
116+
}
117+
118+
printf("Device index %d attached\n", dev_id);
119+
120+
return fd;
121+
}
122+
123+
void attach_start(void)
124+
{
125+
unsigned long flags;
126+
127+
if (serial_fd >= 0)
128+
return;
129+
130+
printf("Attaching BR/EDR controller to %s\n", serial_dev);
131+
132+
flags = (1 << HCI_UART_RESET_ON_INIT);
133+
134+
serial_fd = attach_proto(serial_dev, HCI_UART_H4, flags);
135+
}
136+
137+
void attach_stop(void)
138+
{
139+
if (serial_fd < 0)
140+
return;
141+
142+
close(serial_fd);
143+
serial_fd = -1;
144+
}

peripheral/attach.h

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
*
3+
* BlueZ - Bluetooth protocol stack for Linux
4+
*
5+
* Copyright (C) 2015 Intel Corporation. All rights reserved.
6+
*
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*
22+
*/
23+
24+
void attach_start(void);
25+
void attach_stop(void);

peripheral/efivars.c

+132
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
/*
2+
*
3+
* BlueZ - Bluetooth protocol stack for Linux
4+
*
5+
* Copyright (C) 2015 Intel Corporation. All rights reserved.
6+
*
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*
22+
*/
23+
24+
#ifdef HAVE_CONFIG_H
25+
#include <config.h>
26+
#endif
27+
28+
#include <stdio.h>
29+
#include <errno.h>
30+
#include <fcntl.h>
31+
#include <unistd.h>
32+
#include <string.h>
33+
#include <stdlib.h>
34+
#include <stdint.h>
35+
#include <sys/stat.h>
36+
#include <sys/types.h>
37+
#include <sys/param.h>
38+
#include <sys/uio.h>
39+
40+
#include "peripheral/efivars.h"
41+
42+
#define SYSFS_EFIVARS "/sys/firmware/efi/efivars"
43+
44+
typedef struct {
45+
uint32_t data1;
46+
uint16_t data2;
47+
uint16_t data3;
48+
uint8_t data4[8];
49+
} efi_guid_t;
50+
51+
#define VENDOR_GUID \
52+
(efi_guid_t) { 0xd5f9d775, 0x1a09, 0x4e89, \
53+
{ 0x96, 0xcf, 0x1d, 0x19, 0x55, 0x4d, 0xa6, 0x67 } }
54+
55+
static void efivars_pathname(const char *name, char *pathname, size_t size)
56+
{
57+
static efi_guid_t guid = VENDOR_GUID;
58+
59+
snprintf(pathname, size - 1,
60+
"%s/%s-%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x",
61+
SYSFS_EFIVARS, name, guid.data1, guid.data2, guid.data3,
62+
guid.data4[0], guid.data4[1], guid.data4[2], guid.data4[3],
63+
guid.data4[4], guid.data4[5], guid.data4[6], guid.data4[7]);
64+
}
65+
66+
int efivars_read(const char *name, uint32_t *attributes,
67+
void *data, size_t size)
68+
{
69+
char pathname[PATH_MAX];
70+
struct iovec iov[2];
71+
uint32_t attr;
72+
ssize_t len;
73+
int fd;
74+
75+
efivars_pathname(name, pathname, PATH_MAX);
76+
77+
fd = open(pathname, O_RDONLY | O_CLOEXEC);
78+
if (fd < 0)
79+
return -EIO;
80+
81+
iov[0].iov_base = &attr;
82+
iov[0].iov_len = sizeof(attr);
83+
iov[1].iov_base = data;
84+
iov[1].iov_len = size;
85+
86+
len = readv(fd, iov, 2);
87+
88+
close(fd);
89+
90+
if (len < 0)
91+
return -EIO;
92+
93+
if (attributes)
94+
*attributes = attr;
95+
96+
return 0;
97+
}
98+
99+
int efivars_write(const char *name, uint32_t attributes,
100+
const void *data, size_t size)
101+
{
102+
char pathname[PATH_MAX];
103+
void *buf;
104+
ssize_t written;
105+
int fd;
106+
107+
efivars_pathname(name, pathname, PATH_MAX);
108+
109+
buf = malloc(size + sizeof(attributes));
110+
if (!buf)
111+
return -ENOMEM;
112+
113+
fd = open(pathname, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
114+
S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
115+
if (fd < 0) {
116+
free(buf);
117+
return -EIO;
118+
}
119+
120+
memcpy(buf, &attributes, sizeof(attributes));
121+
memcpy(buf + sizeof(attributes), data, size);
122+
123+
written = write(fd, buf, size + sizeof(attributes));
124+
125+
close(fd);
126+
free(buf);
127+
128+
if (written < 0)
129+
return -EIO;
130+
131+
return 0;
132+
}

peripheral/efivars.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
*
3+
* BlueZ - Bluetooth protocol stack for Linux
4+
*
5+
* Copyright (C) 2015 Intel Corporation. All rights reserved.
6+
*
7+
*
8+
* This library is free software; you can redistribute it and/or
9+
* modify it under the terms of the GNU Lesser General Public
10+
* License as published by the Free Software Foundation; either
11+
* version 2.1 of the License, or (at your option) any later version.
12+
*
13+
* This library is distributed in the hope that it will be useful,
14+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
15+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16+
* Lesser General Public License for more details.
17+
*
18+
* You should have received a copy of the GNU Lesser General Public
19+
* License along with this library; if not, write to the Free Software
20+
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21+
*
22+
*/
23+
24+
#define EFIVARS_NON_VOLATILE 0x00000001
25+
#define EFIVARS_BOOTSERVICE_ACCESS 0x00000002
26+
#define EFIVARS_RUNTIME_ACCESS 0x00000004
27+
#define EFIVARS_HARDWARE_ERROR_RECORD 0x00000008
28+
#define EFIVARS_AUTHENTICATED_WRITE_ACCESS 0x00000010
29+
30+
int efivars_read(const char *name, uint32_t *attributes,
31+
void *data, size_t size);
32+
int efivars_write(const char *name, uint32_t attributes,
33+
const void *data, size_t size);

0 commit comments

Comments
 (0)