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"
23 GIOChannel *gatt_connect(const char *src, const char *dst,
24 const char *dst_type, const char *sec_level,
25 int psm, int mtu, BtIOConnect connect_cb,
28 static char *opt_src = NULL;
29 static char *opt_dst = NULL;
30 static char *opt_dst_type = NULL;
31 static int opt_mtu = 0;
32 static int opt_psm = 0;
33 static char *opt_sec_level = NULL;
35 static GMainLoop *event_loop;
37 static GOptionEntry options[] = {
38 { "adapter", 'i', 0, G_OPTION_ARG_STRING, &opt_src,
39 "Specify local adapter interface", "hciX" },
40 { "device", 'b', 0, G_OPTION_ARG_STRING, &opt_dst,
41 "Specify remote Bluetooth address", "MAC" },
42 { "addr-type", 't', 0, G_OPTION_ARG_STRING, &opt_dst_type,
43 "Set LE address type. Default: public", "[public | random]"},
44 { "mtu", 'm', 0, G_OPTION_ARG_INT, &opt_mtu,
45 "Specify the MTU size", "MTU" },
46 { "psm", 'p', 0, G_OPTION_ARG_INT, &opt_psm,
47 "Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
48 { "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
49 "Set security level. Default: low", "[low | medium | high]"},
53 static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
56 g_io_channel_shutdown(chan, FALSE, NULL);
57 g_io_channel_unref(chan);
58 g_main_loop_quit(event_loop);
59 g_info("channel_watcher cleared channel and exiting");
63 static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
65 GAttrib *attrib = user_data;
70 handle = bt_get_le16(&pdu[1]);
72 if ((pdu[0] == 0x1b) && (handle == 0x0012) && (len == 9) &&
73 ((which == 1) || (which == 2))) {
74 uint32_t val = bt_get_le32(&pdu[5]);
75 g_debug("store: \"%hhu,%u\"\n", which, val);
76 if (dbstore(which, val))
77 g_warning("error storing \"%hhu,%u\"\n", which, val);
86 (void)gmtime_r(&t, &tm);
87 (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
88 for (i = 3; (i < len) && ((i-3) < (sizeof(buf)/3)); i++)
89 sprintf(buf+strlen(buf), " %02x ", pdu[i]);
90 g_warning("%s ev %02x hd 0x%04x value: %s",
91 tstr, pdu[0], handle, buf);
95 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
103 g_warning("%s", err->message);
104 g_main_loop_quit(event_loop);
106 bt_io_get(io, &gerr, BT_IO_OPT_IMTU, &mtu,
107 BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);
109 g_warning("Can't detect MTU, using default: %s",
112 mtu = ATT_DEFAULT_LE_MTU;
115 mtu = ATT_DEFAULT_LE_MTU;
116 attrib = g_attrib_new(io, mtu, false);
117 g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
118 events_handler, attrib, NULL);
119 g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
120 events_handler, attrib, NULL);
121 g_info("connect_cb registered events_handler and exiting\n");
124 int main(int argc, char *argv[])
126 GOptionContext *context;
129 gboolean got_error = FALSE;
131 g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
132 | G_LOG_FLAG_RECURSION, g_log_default_handler, NULL);
133 opt_dst_type = g_strdup("public");
134 opt_sec_level = g_strdup("low");
135 context = g_option_context_new(NULL);
136 g_option_context_add_main_entries(context, options, NULL);
137 if (!g_option_context_parse(context, &argc, &argv, &gerr)) {
138 g_error("%s", gerr->message);
139 g_clear_error(&gerr);
144 chan = gatt_connect(opt_src, opt_dst, opt_dst_type,
145 opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr);
147 g_io_add_watch(chan, G_IO_HUP, channel_watcher, NULL);
148 event_loop = g_main_loop_new(NULL, FALSE);
149 g_main_loop_run(event_loop);
150 g_main_loop_unref(event_loop);
152 g_warning("%s", gerr->message);
153 g_clear_error(&gerr);
160 g_option_context_free(context);
163 g_free(opt_sec_level);