-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbleuart.c
112 lines (92 loc) · 2.79 KB
/
bleuart.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <getopt.h>
#include "gattlib.h"
// serial service
uuid_t g_serial_uuid; // 0xFFE1
gatt_connection_t* connection;
void notification_handler(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data) {
int i;
for (i = 0; i < data_length; i++) {
putchar(data[i]);
}
fflush(stdout);
}
static gboolean stdin_callback(GIOChannel *io, GIOCondition condition, gpointer data) {
GError *error = NULL;
gchar in, cr = '\r';
switch (g_io_channel_read_chars(io, &in, 1, NULL, &error)) {
case G_IO_STATUS_NORMAL:
// Write to the BLE characteristic implementing serial TX
if (in == '\n') gattlib_write_char_by_uuid(connection, &g_serial_uuid, &cr, 1);
gattlib_write_char_by_uuid(connection, &g_serial_uuid, &in, 1);
return TRUE;
case G_IO_STATUS_ERROR:
case G_IO_STATUS_EOF:
case G_IO_STATUS_AGAIN:
break;
}
return FALSE;
}
void ctrlc_handler(int dummy) {
gattlib_disconnect(connection);
exit(0);
}
static void usage(char *argv[]) {
printf("%s -d <device_address> -c <characteristic>\n", argv[0]);
}
char baddr[18];
char cuuid[50];
int main(int argc, char *argv[]) {
int ret;
int op,u,test = 0;
while ((op = getopt(argc, argv, "d:c:")) != -1) {
switch (op) {
case 'd':
strncpy(baddr, optarg, 18);
test |= 0x01;
break;
case 'c':
strcpy(cuuid, optarg);
gattlib_string_to_uuid(optarg, strlen(optarg)+1, &g_serial_uuid);
test |= 0x02;
break;
default:
usage(argv);
exit(1);
break;
}
}
if (test != 3) {
usage(argv);
exit(1);
}
printf("Attempting to connect to %s\n", baddr);
connection = gattlib_connect(NULL, baddr, BDADDR_LE_PUBLIC, BT_SEC_LOW, 0, 0);
if (connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
return 1;
}
printf("Registering notification handler\n");
gattlib_register_notification(connection, notification_handler, NULL);
printf("Notification start on characteristic: %s\n", cuuid);
ret = gattlib_notification_start(connection, &g_serial_uuid);
if (ret) {
fprintf(stderr, "Fail to start notification\n.");
return 1;
}
signal(SIGINT, ctrlc_handler);
setbuf(stdin, NULL);
GIOChannel *io = NULL;
io = g_io_channel_unix_new(0); // STDIN_FILENO = 0
g_io_add_watch(io, G_IO_IN, stdin_callback, NULL);
g_io_channel_unref(io);
printf("Ready\n");
GMainLoop *loop = g_main_loop_new(NULL, 0);
g_main_loop_run(loop);
g_main_loop_unref(loop);
gattlib_disconnect(connection);
return 0;
}