forked from labapart/gattlib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
nordic_uart.c
141 lines (121 loc) · 4.11 KB
/
nordic_uart.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
*
* GattLib - GATT Library
*
* Copyright (C) 2016-2019 Olivier Martin <[email protected]>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include "gattlib.h"
#define MIN(a,b) ((a)<(b)?(a):(b))
#define NUS_CHARACTERISTIC_TX_UUID "6e400002-b5a3-f393-e0a9-e50e24dcca9e"
#define NUS_CHARACTERISTIC_RX_UUID "6e400003-b5a3-f393-e0a9-e50e24dcca9e"
gatt_connection_t* m_connection;
void notification_cb(const uuid_t* uuid, const uint8_t* data, size_t data_length, void* user_data) {
int i;
for(i = 0; i < data_length; i++) {
printf("%c", data[i]);
}
}
static void usage(char *argv[]) {
printf("%s <device_address>\n", argv[0]);
}
void int_handler(int dummy) {
gattlib_disconnect(m_connection);
exit(0);
}
int main(int argc, char *argv[]) {
char input[256];
char* input_ptr;
int i, ret, total_length, length = 0;
uuid_t nus_characteristic_tx_uuid;
uuid_t nus_characteristic_rx_uuid;
if (argc != 2) {
usage(argv);
return 1;
}
m_connection = gattlib_connect(NULL, argv[1],
GATTLIB_CONNECTION_OPTIONS_LEGACY_BDADDR_LE_RANDOM |
GATTLIB_CONNECTION_OPTIONS_LEGACY_BT_SEC_LOW);
if (m_connection == NULL) {
fprintf(stderr, "Fail to connect to the bluetooth device.\n");
return 1;
}
// Convert characteristics to their respective UUIDs
ret = gattlib_string_to_uuid(NUS_CHARACTERISTIC_TX_UUID, strlen(NUS_CHARACTERISTIC_TX_UUID) + 1, &nus_characteristic_tx_uuid);
if (ret) {
fprintf(stderr, "Fail to convert characteristic TX to UUID.\n");
return 1;
}
ret = gattlib_string_to_uuid(NUS_CHARACTERISTIC_RX_UUID, strlen(NUS_CHARACTERISTIC_RX_UUID) + 1, &nus_characteristic_rx_uuid);
if (ret) {
fprintf(stderr, "Fail to convert characteristic RX to UUID.\n");
return 1;
}
// Look for handle for NUS_CHARACTERISTIC_TX_UUID
gattlib_characteristic_t* characteristics;
int characteristic_count;
ret = gattlib_discover_char(m_connection, &characteristics, &characteristic_count);
if (ret) {
fprintf(stderr, "Fail to discover characteristic.\n");
return 1;
}
uint16_t tx_handle = 0, rx_handle = 0;
for (i = 0; i < characteristic_count; i++) {
if (gattlib_uuid_cmp(&characteristics[i].uuid, &nus_characteristic_tx_uuid) == 0) {
tx_handle = characteristics[i].value_handle;
} else if (gattlib_uuid_cmp(&characteristics[i].uuid, &nus_characteristic_rx_uuid) == 0) {
rx_handle = characteristics[i].value_handle;
}
}
if (tx_handle == 0) {
fprintf(stderr, "Fail to find NUS TX characteristic.\n");
return 1;
} else if (rx_handle == 0) {
fprintf(stderr, "Fail to find NUS RX characteristic.\n");
return 1;
}
free(characteristics);
// Register notification handler
gattlib_register_notification(m_connection, notification_cb, NULL);
ret = gattlib_notification_start(m_connection, &nus_characteristic_rx_uuid);
if (ret) {
fprintf(stderr, "Fail to start notification.\n");
return 2;
}
// Register handler to catch Ctrl+C
signal(SIGINT, int_handler);
while(1) {
fgets(input, sizeof(input), stdin);
// NUS TX can only receive 20 bytes at a time
input_ptr = input;
for (total_length = strlen(input) + 1; total_length > 0; total_length -= length) {
length = MIN(total_length, 20);
ret = gattlib_write_without_response_char_by_handle(m_connection, tx_handle, input_ptr, length);
if (ret) {
fprintf(stderr, "Fail to send data to NUS TX characteristic.\n");
return 1;
}
input_ptr += length;
}
}
return 0;
}