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