keep database config in a separate file
[pulsecounter.git] / linux / pulsecounter.c
1 #include <errno.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <stdbool.h>
5 #include <time.h>
6
7 #include <glib.h>
8
9 #include <lib/bluetooth.h>
10 #include <lib/hci.h>
11 #include <lib/hci_lib.h>
12 #include <lib/sdp.h>
13 #include "lib/uuid.h"
14
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"
20
21 #include "dbstore.h"
22
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,
26                         GError **gerr);
27
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;
34 static char *opt_dbconffile = NULL;
35
36 static GMainLoop *event_loop;
37
38 static GOptionEntry options[] = {
39         { "adapter", 'i', 0, G_OPTION_ARG_STRING, &opt_src,
40                 "Specify local adapter interface", "hciX" },
41         { "device", 'b', 0, G_OPTION_ARG_STRING, &opt_dst,
42                 "Specify remote Bluetooth address", "MAC" },
43         { "addr-type", 't', 0, G_OPTION_ARG_STRING, &opt_dst_type,
44                 "Set LE address type. Default: public", "[public | random]"},
45         { "mtu", 'm', 0, G_OPTION_ARG_INT, &opt_mtu,
46                 "Specify the MTU size", "MTU" },
47         { "psm", 'p', 0, G_OPTION_ARG_INT, &opt_psm,
48                 "Specify the PSM for GATT/ATT over BR/EDR", "PSM" },
49         { "sec-level", 'l', 0, G_OPTION_ARG_STRING, &opt_sec_level,
50                 "Set security level. Default: low", "[low | medium | high]"},
51         { "dbconfig", 'c', 0, G_OPTION_ARG_STRING, &opt_dbconffile,
52                 "Specify file name with database configuration", "cfile"},
53         { NULL },
54 };
55
56 static gboolean channel_watcher(GIOChannel *chan, GIOCondition cond,
57                                 gpointer user_data)
58 {
59         g_io_channel_shutdown(chan, FALSE, NULL);
60         g_io_channel_unref(chan);
61         g_main_loop_quit(event_loop);
62         g_info("channel_watcher cleared channel and exiting");
63         return FALSE;
64 }
65
66 static void events_handler(const uint8_t *pdu, uint16_t len, gpointer user_data)
67 {
68         GAttrib *attrib = user_data;
69         uint8_t *opdu;
70         uint8_t which;
71         uint16_t handle;
72
73         handle = bt_get_le16(&pdu[1]);
74         which = pdu[3];
75         if ((pdu[0] == 0x1b) && (handle == 0x0012) && (len == 9) &&
76             ((which == 1) || (which == 2))) {
77                 uint32_t val = bt_get_le32(&pdu[5]);
78                 g_debug("store: \"%hhu,%u\"\n", which, val);
79                 if (dbstore(which, val))
80                         g_warning("error storing \"%hhu,%u\"\n", which, val);
81         } else {
82                 time_t t;
83                 int i;
84                 struct tm tm;
85                 char buf[64];
86                 char tstr[64];
87
88                 t = time(NULL);
89                 (void)gmtime_r(&t, &tm);
90                 (void)strftime(tstr, sizeof(tstr), "%Y-%m-%d %H:%M:%S", &tm);
91                 for (i = 3; (i < len) && ((i-3) < (sizeof(buf)/3)); i++)
92                         sprintf(buf+strlen(buf), " %02x ", pdu[i]);
93                 g_warning("%s ev %02x hd 0x%04x value: %s",
94                         tstr, pdu[0], handle, buf);
95         }
96 }
97
98 static void connect_cb(GIOChannel *io, GError *err, gpointer user_data)
99 {
100         GAttrib *attrib;
101         uint16_t mtu;
102         uint16_t cid;
103         GError *gerr = NULL;
104
105         if (err) {
106                 g_warning("%s", err->message);
107                 g_main_loop_quit(event_loop);
108         }
109         bt_io_get(io, &gerr, BT_IO_OPT_IMTU, &mtu,
110                                 BT_IO_OPT_CID, &cid, BT_IO_OPT_INVALID);
111         if (gerr) {
112                 g_warning("Can't detect MTU, using default: %s",
113                                                         gerr->message);
114                 g_error_free(gerr);
115                 mtu = ATT_DEFAULT_LE_MTU;
116         }
117         if (cid == ATT_CID)
118                 mtu = ATT_DEFAULT_LE_MTU;
119         attrib = g_attrib_new(io, mtu, false);
120         g_attrib_register(attrib, ATT_OP_HANDLE_NOTIFY, GATTRIB_ALL_HANDLES,
121                                                 events_handler, attrib, NULL);
122         g_attrib_register(attrib, ATT_OP_HANDLE_IND, GATTRIB_ALL_HANDLES,
123                                                 events_handler, attrib, NULL);
124         g_info("connect_cb registered events_handler and exiting\n");
125 }
126
127 int main(int argc, char *argv[])
128 {
129         GOptionContext *context;
130         GError *gerr = NULL;
131         GIOChannel *chan;
132         gboolean got_error = FALSE;
133
134         g_log_set_handler(NULL, G_LOG_LEVEL_MASK | G_LOG_FLAG_FATAL
135                           | G_LOG_FLAG_RECURSION, g_log_default_handler, NULL);
136         opt_dst_type = g_strdup("public");
137         opt_sec_level = g_strdup("low");
138         opt_dbconffile = g_strdup("/etc/pulsecounter.db");
139         context = g_option_context_new(NULL);
140         g_option_context_add_main_entries(context, options, NULL);
141         if (!g_option_context_parse(context, &argc, &argv, &gerr)) {
142                 g_error("%s", gerr->message);
143                 g_clear_error(&gerr);
144                 got_error = TRUE;
145                 goto done;
146         }
147         if (!opt_dst) {
148                 g_error("Destination MAC address must be specified");
149                 got_error = TRUE;
150                 goto done;
151         }
152         if (dbconfig(opt_dbconffile)) {
153                 g_error("Could not parse database configuration file");
154                 got_error = TRUE;
155                 goto done;
156         }
157         while (1) {
158                 chan = gatt_connect(opt_src, opt_dst, opt_dst_type,
159                         opt_sec_level, opt_psm, opt_mtu, connect_cb, &gerr);
160                 if (chan) {
161                         g_io_add_watch(chan, G_IO_HUP, channel_watcher, NULL);
162                         event_loop = g_main_loop_new(NULL, FALSE);
163                         g_main_loop_run(event_loop);
164                         g_main_loop_unref(event_loop);
165                 } else {
166                         g_warning("%s", gerr->message);
167                         g_clear_error(&gerr);
168                         got_error = TRUE;
169                 }
170                 sleep(10);
171         }
172
173 done:
174         g_option_context_free(context);
175         g_free(opt_src);
176         g_free(opt_dst);
177         g_free(opt_sec_level);
178         g_free(opt_dbconffile);
179         if (got_error)
180                 exit(EXIT_FAILURE);
181         else
182                 exit(EXIT_SUCCESS);
183 }