9 #include <lib/bluetooth.h>
11 #include <lib/hci_lib.h>
15 #include "src/shared/util.h"
16 #include "attrib/att.h"
17 #include "btio/btio.h"
18 #include "attrib/gattrib.h"
19 #include "attrib/gatt.h"
21 GIOChannel *gatt_connect(const char *src, const char *dst,
22 const char *dst_type, const char *sec_level,
23 int psm, int mtu, BtIOConnect connect_cb,
26 static char *opt_src = NULL;
27 static char *opt_dst = NULL;
28 static char *opt_dst_type = NULL;
29 static int opt_mtu = 0;
30 static int opt_psm = 0;
31 static char *opt_sec_level = NULL;
33 static GMainLoop *event_loop;
35 static GOptionEntry options[] = {
36 { "adapter", 'i', 0, G_OPTION_ARG_STRING, &opt_src,
37 "Specify local adapter interface", "hciX" },
38 { "device", 'b', 0, G_OPTION_ARG_STRING, &opt_dst,
39 "Specify remote Bluetooth address", "MAC" },
40 { "addr-type", 't', 0, G_OPTION_ARG_STRING, &opt_dst_type,
41 "Set LE address type. Default: public", "[public | random]"},
42 { "mtu", 'm', 0, G_OPTION_ARG_INT, &opt_mtu,
43 "Specify the MTU size", "MTU" },
44 { "psm", 'p', 0, G_OPTION_ARG_INT, &opt_psm,
45 "Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
46 { "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
47 "Set security level. Default: low", "[low | medium | high]"},
51 static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
54 g_io_channel_shutdown(chan, FALSE, NULL);
55 g_io_channel_unref(chan);
56 g_main_loop_quit(event_loop);
57 g_print("channel_watcher cleared channel and exiting\n");
61 static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
63 GAttrib *attrib = user_data;
65 uint16_t handle, i, olen = 0;
72 (void)gmtime_r(&t, &tm);
73 (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
74 handle = bt_get_le16(&pdu[1]);
75 g_print("%s ev %02x hd 0x%04x value: ", tstr, pdu[0], handle);
76 for (i = 3; i < len; i++)
77 g_print("%02x ", pdu[i]);
81 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
89 g_printerr("%s\n", err->message);
90 g_main_loop_quit(event_loop);
92 bt_io_get(io, &gerr, BT_IO_OPT_IMTU, &mtu,
93 BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);
95 g_printerr("Can't detect MTU, using default: %s",
98 mtu = ATT_DEFAULT_LE_MTU;
101 mtu = ATT_DEFAULT_LE_MTU;
102 attrib = g_attrib_new(io, mtu, false);
103 g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
104 events_handler, attrib, NULL);
105 g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
106 events_handler, attrib, NULL);
107 g_print("connect_cb registered events_handler and exiting\n");
110 int main(int argc, char *argv[])
112 GOptionContext *context;
115 gboolean got_error = FALSE;
117 opt_dst_type = g_strdup("public");
118 opt_sec_level = g_strdup("low");
119 context = g_option_context_new(NULL);
120 g_option_context_add_main_entries(context, options, NULL);
121 if (!g_option_context_parse(context, &argc, &argv, &gerr)) {
122 g_printerr("%s\n", gerr->message);
123 g_clear_error(&gerr);
127 chan = gatt_connect(opt_src, opt_dst, opt_dst_type, opt_sec_level,
128 opt_psm, opt_mtu, connect_cb, &gerr);
130 g_printerr("%s\n", gerr->message);
131 g_clear_error(&gerr);
135 g_io_add_watch(chan, G_IO_HUP, channel_watcher, NULL);
138 event_loop = g_main_loop_new(NULL, FALSE);
139 g_main_loop_run(event_loop);
140 g_main_loop_unref(event_loop);
143 g_option_context_free(context);
146 g_free(opt_sec_level);