blob: d490c599fa4725992ab09f312b7a09fcf10b05e6 [file] [log] [blame]
Cyril Bur1aa096a2015-10-28 15:24:52 +11001/* Copyright 2015 IBM
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <errno.h>
17#include <stdint.h>
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <fcntl.h>
22#include <unistd.h>
23#include <sys/stat.h>
24#include <sys/mman.h>
25#include <sys/timerfd.h>
26#include <syslog.h>
27#include <poll.h>
28#include <limits.h>
29#include <getopt.h>
30#include <time.h>
31#include <assert.h>
32
33#include <linux/bt-host.h>
34
35#include <systemd/sd-bus.h>
36
37#define PREFIX "[BTBRIDGED] "
38
39#define BT_HOST_PATH "/dev/bt-host"
40#define BT_HOST_TIMEOUT_SEC 1
41#define BT_MAX_MESSAGE 64
42
43#define DBUS_NAME "org.openbmc.HostIpmi"
44#define OBJ_NAME "/org/openbmc/HostIpmi/1"
45
46#define SD_BUS_FD 0
47#define BT_FD 1
48#define TIMER_FD 2
49#define TOTAL_FDS 3
50
51#define MSG_OUT(f_, ...) do { if (verbose) { printf(PREFIX); printf((f_), ##__VA_ARGS__); } } while(0)
52#define MSG_ERR(f_, ...) do { \
53 if (verbose) { \
54 fprintf(stderr, PREFIX); \
55 fprintf(stderr, (f_), ##__VA_ARGS__); \
56 } \
57 } while(0)
58
59struct ipmi_msg {
60 uint8_t netfn;
61 uint8_t lun;
62 uint8_t seq;
63 uint8_t cmd;
64 uint8_t cc; /* Only used on responses */
65 uint8_t *data;
66 size_t data_len;
67};
68
69struct bt_queue {
70 struct ipmi_msg req;
71 struct ipmi_msg rsp;
72 struct timespec start;
73 int expired;
74 sd_bus_message *call;
75 struct bt_queue *next;
76};
77
78struct btbridged_context{
79 int debug;
Cyril Bur1aa096a2015-10-28 15:24:52 +110080 struct pollfd fds[TOTAL_FDS];
81 struct sd_bus *bus;
82 struct bt_queue *bt_q;
83};
84
85static int running = 1;
86static int verbose;
87
88static struct bt_queue *bt_q_get_head(struct btbridged_context *context)
89{
90 return context ? context->bt_q : NULL;
91}
92
93static struct bt_queue *bt_q_get_seq(struct btbridged_context *context, uint8_t seq)
94{
95 struct bt_queue *t;
96
97 assert(context);
98
99 t = context->bt_q;
100
101 while (t && t->req.seq != seq)
102 t = t->next;
103
104 return t;
105}
106
107static struct bt_queue *bt_q_get_msg(struct btbridged_context *context)
108{
109 struct bt_queue *t;
110
111 assert(context);
112
113 t = context->bt_q;
114
115 while (t && (!t->call && !t->expired))
116 t = t->next;
117
118 return t;
119}
120
121static struct bt_queue *bt_q_enqueue(struct btbridged_context *context, uint8_t *bt_data)
122{
123 struct bt_queue *n;
124 struct bt_queue *bt_q;
125 int len;
126
127 assert(context && bt_data);
128
129 /*
130 * len here is the length of the array.
131 * Helpfully BT doesn't count the length byte
132 */
133 len = bt_data[0] + 1;
134 if (len < 4) {
135 MSG_ERR("Trying to queue a BT message with a short length (%d)\n", len);
136 return NULL;
137 }
138
139 bt_q = context->bt_q;
140
141 n = calloc(1, sizeof(struct bt_queue));
142 if (!n)
143 return NULL;
144
145 if (context->debug) {
146 n->req.data = malloc(len - 4);
147 if (n->req.data)
148 n->req.data = memcpy(n->req.data, bt_data + 4, len - 4);
149 }
150 n->req.data_len = len - 4;
151 /* Don't count the lenfn/ln, seq and command */
152 n->req.netfn = bt_data[1] >> 2;
153 n->req.lun = bt_data[1] & 0x3;
154 n->req.seq = bt_data[2];
155 n->req.cmd = bt_data[3];
156 if (clock_gettime(CLOCK_MONOTONIC, &n->start) == -1) {
157 MSG_ERR("Couldn't clock_gettime(): %s\n", strerror(errno));
158 free(n);
159 return NULL;
160 }
161 if (!bt_q) {
162 context->bt_q = n;
163 } else {
164 struct bt_queue *t = bt_q;
165
166 while (t->next)
167 t = t->next;
168
169 t->next = n;
170 }
171
172 return n;
173}
174
175static void bt_q_free(struct bt_queue *bt_q)
176{
177 if (!bt_q)
178 return;
179
180 /* Unrefing sd_bus_message should free(rsp.data) */
181 if (bt_q->call)
182 sd_bus_message_unref(bt_q->call);
183
184 free(bt_q->req.data);
185 free(bt_q);
186}
187
188static struct bt_queue *bt_q_drop(struct btbridged_context *context, struct bt_queue *element)
189{
190 struct bt_queue *r;
191 struct bt_queue *bt_q;
192
193 assert(context);
194
195 bt_q = context->bt_q;
196 if (!element || !bt_q)
197 return NULL;
198
199
200 r = bt_q;
201 if (r == bt_q) {
202 context->bt_q = bt_q->next;
203 } else {
204 while (r && r->next != element)
205 r = r->next;
206
207 if (!r) {
208 MSG_ERR("Didn't find element %p in queue\n", element);
209 bt_q_free(element);
210 return NULL;
211 }
212 r->next = r->next->next;
213 }
214 bt_q_free(element);
215
216 return context->bt_q;
217}
218
219static struct bt_queue *bt_q_dequeue(struct btbridged_context *context)
220{
221 struct bt_queue *r;
222 struct bt_queue *bt_q;
223
224 assert(context);
225
226 bt_q = context->bt_q;
227 if (!bt_q) {
228 MSG_ERR("Dequeuing empty queue!\n");
229 return NULL;
230 }
231
232 r = bt_q->next;
233 bt_q_free(bt_q);
234 context->bt_q = r;
235
236 return r;
237}
238
239static int method_send_sms_atn(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
240{
241 int r, bt_fd = *(int *)userdata;
242 MSG_OUT("Sending SMS_ATN ioctl() to %s\n", BT_HOST_PATH);
243 r = ioctl(bt_fd, BT_HOST_IOCTL_SMS_ATN);
244 if (r == -1) {
245 r = errno;
246 MSG_ERR("Couldn't ioctl() to %s: %s\n", BT_HOST_PATH, strerror(r));
247 return sd_bus_reply_method_errno(msg, errno, ret_error);
248 }
249
250 r = 0;
251 return sd_bus_reply_method_return(msg, "x", r);
252}
253
254static int method_send_message(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
255{
256 struct btbridged_context *context;
257 struct bt_queue *bt_msg;
258 uint8_t *data, *all_data;
259 size_t data_sz, all_data_sz;
260 uint8_t netfn, lun, seq, cmd, cc;
261 uint64_t cookie;
262 /*
263 * Doesn't say it anywhere explicitly but it looks like returning 0 or
264 * negative is BAD...
265 */
266 int r = 1;
267
268 context = (struct btbridged_context *)userdata;
269 if (!context) {
270 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Internal error");
271 r = 0;
272 goto out;
273 }
Cyril Bur99d545d2015-10-30 14:50:10 +1100274 r = sd_bus_message_read(msg, "yyyyy", &seq, &netfn, &lun, &cmd, &cc);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100275 if (r < 0) {
276 MSG_ERR("Couldn't parse leading bytes of message: %s\n", strerror(-r));
277 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message");
278 r = -EINVAL;
279 goto out;
280 }
281 r = sd_bus_message_read_array(msg, 'y', (const void **)&data, &data_sz);
282 if (r < 0) {
283 MSG_ERR("Couldn't parse data bytes of message: %s\n", strerror(-r));
284 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message data");
285 r = -EINVAL;
286 goto out;
287 }
288
289 bt_msg = bt_q_get_seq(context, seq);
290 if (!bt_msg) {
291 sd_bus_error_set_const(ret_error, "org.openbmc.error", "No matching request");
292 MSG_ERR("Failed to find matching request for dbus method with seq: 0x%02x\n", seq);
293 r = -EINVAL;
294 goto out;
295 }
Cyril Bur104200b2015-10-30 14:34:58 +1100296 MSG_OUT("Received a dbus response for msg with seq 0x%02x\n", seq);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100297 bt_msg->call = sd_bus_message_ref(msg);
298 bt_msg->rsp.netfn = netfn;
299 bt_msg->rsp.lun = lun;
300 bt_msg->rsp.seq = seq;
301 bt_msg->rsp.cmd = cmd;
302 bt_msg->rsp.cc = cc;
303 bt_msg->rsp.data_len = data_sz;
304 /* Because we've ref'ed the msg, I hope we don't need to memcpy data */
305 bt_msg->rsp.data = data;
306
307 /* Now that we have a response */
308 context->fds[BT_FD].events |= POLLOUT;
309
310out:
311 return r;
312}
313
314static int bt_host_write(struct btbridged_context *context, struct bt_queue *bt_msg)
315{
316 struct bt_queue *head;
317 uint8_t data[BT_MAX_MESSAGE] = { 0 };
318 sd_bus_message *msg = NULL;
319 int r, len;
320
321 assert(context);
322
323 if (!bt_msg)
324 return -EINVAL;
325
326 head = bt_q_get_head(context);
327 if (bt_msg == head) {
328 struct itimerspec ts;
329 /* Need to adjust the timer */
330 ts.it_interval.tv_sec = 0;
331 ts.it_interval.tv_nsec = 0;
332 if (head->next) {
333 ts.it_value = head->next->start;
334 ts.it_value.tv_sec += BT_HOST_TIMEOUT_SEC;
335 MSG_OUT("Adjusting timer for next element\n");
336 } else {
337 ts.it_value.tv_nsec = 0;
338 ts.it_value.tv_sec = 0;
339 MSG_OUT("Disabling timer, no elements remain in queue\n");
340 }
341 r = timerfd_settime(context->fds[TIMER_FD].fd, TFD_TIMER_ABSTIME, &ts, NULL);
342 if (r == -1)
343 MSG_ERR("Couldn't set timerfd\n");
344 }
345 data[1] = (bt_msg->rsp.netfn << 2) | (bt_msg->rsp.lun & 0x3);
346 data[2] = bt_msg->rsp.seq;
347 data[3] = bt_msg->rsp.cmd;
348 data[4] = bt_msg->rsp.cc;
349 if (bt_msg->rsp.data_len > sizeof(data) - 5) {
350 MSG_ERR("Response message size (%d) too big, truncating\n", bt_msg->rsp.data_len);
351 bt_msg->rsp.data_len = sizeof(data) - 5;
352 }
353 /* netfn/len + seq + cmd + cc = 4 */
354 data[0] = bt_msg->rsp.data_len + 4;
355 if (bt_msg->rsp.data_len)
356 memcpy(data + 5, bt_msg->rsp.data, bt_msg->rsp.data_len);
357 /* Count the data[0] byte */
358 len = write(context->fds[BT_FD].fd, data, data[0] + 1);
359 if (r == -1) {
360 r = errno;
361 MSG_ERR("Error writing to %s: %s\n", BT_HOST_PATH, strerror(errno));
362 if (bt_msg->call) {
363 r = sd_bus_message_new_method_errno(bt_msg->call, &msg, r, NULL);
364 if (r < 0)
365 MSG_ERR("Couldn't create response error\n");
366 }
367 goto out;
368 } else {
369 if (len != data[0] + 1)
370 MSG_ERR("Possible short write to %s, desired len: %d, written len: %d\n", BT_HOST_PATH, data[0] + 1, len);
371 else
372 MSG_OUT("Successfully wrote %d of %d bytes to %s\n", len, data[0] + 1, BT_HOST_PATH);
373
374 if (bt_msg->call) {
375 r = sd_bus_message_new_method_return(bt_msg->call, &msg);
376 if (r < 0) {
377 MSG_ERR("Couldn't create response message\n");
378 goto out;
379 }
380 r = 0; /* Just to be explicit about what we're sending back */
381 r = sd_bus_message_append(msg, "x", r);
382 if (r < 0) {
383 MSG_ERR("Couldn't append result to response\n");
384 goto out;
385 }
386
387 }
388 }
389
390out:
391 if (bt_msg->call) {
392 if (sd_bus_send(context->bus, msg, NULL) < 0)
393 MSG_ERR("Couldn't send response message\n");
394 sd_bus_message_unref(msg);
395 }
396 bt_q_drop(context, bt_msg);
397
398 /*
399 * There isn't another message ready to be sent so turn off POLLOUT
400 */
401 if (!bt_q_get_msg(context)) {
402 MSG_OUT("Turning off POLLOUT for the BT in poll()\n");
403 context->fds[BT_FD].events = POLLIN;
404 }
405
406 return r;
407}
408
409static int dispatch_timer(struct btbridged_context *context)
410{
411 int r = 0;
412 if (context->fds[TIMER_FD].revents & POLLIN) {
413 sd_bus_message *msg;
414 struct bt_queue *head;
415
416 head = bt_q_get_head(context);
417 if (!head) {
418 /* Odd, timer expired but we didn't have a message to timeout */
419 MSG_ERR("No message found to send timeout\n");
420 return 0;
421 }
422 if (head->call) {
423 r = sd_bus_message_new_method_errno(head->call, &msg, ETIMEDOUT, NULL);
424 if (r < 0) {
425 MSG_ERR("Couldn't create response error\n");
426 } else {
427 if (sd_bus_send(context->bus, msg, NULL) < 0)
428 MSG_ERR("Couldn't send response message\n");
429 sd_bus_message_unref(msg);
430 }
431 MSG_ERR("Message with seq 0x%02x is being timed out despite "
432 "appearing to have been responded to. Slow BT?\n", head->rsp.seq);
433 }
434
435 /* Set expiry */
436 head->expired = 1;
437 head->rsp.seq = head->req.seq;
438 head->rsp.netfn = head->req.netfn + 1;
439 head->rsp.lun = head->req.lun;
440 head->rsp.cc = 0xce; /* Command response could not be provided */
441 head->rsp.cmd = head->req.cmd;
442 /* These should already be zeroed but best to be sure */
443 head->rsp.data_len = 0;
444 head->rsp.data = NULL;
445 head->call = NULL;
446 MSG_OUT("Timeout on msg with seq: 0x%02x\n", head->rsp.seq);
447 /* Turn on POLLOUT so we'll write this one next */
448 context->fds[BT_FD].events |= POLLOUT;
449 }
450
451 return 0;
452}
453
454static int dispatch_sd_bus(struct btbridged_context *context)
455{
456 int r = 0;
457 if (context->fds[SD_BUS_FD].revents) {
458 r = sd_bus_process(context->bus, NULL);
459 if (r > 0)
460 MSG_OUT("Processed %d dbus events\n", r);
461 }
462
463 return r;
464}
465
466static int dispatch_bt(struct btbridged_context *context)
467{
468 int err = 0;
469 int r;
470
471 assert(context);
472
473 if (context->fds[BT_FD].revents & POLLIN) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100474 sd_bus_message *msg;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100475 int data_len;
476 struct bt_queue *new;
477 uint8_t data[BT_MAX_MESSAGE] = { 0 };
478
479 r = read(context->fds[BT_FD].fd, data, sizeof(data));
480 if (r < 0) {
481 MSG_ERR("Couldn't read from bt: %s\n", strerror(-r));
482 goto out1;
483 }
484 if (r < data[0] + 1) {
Cyril Bur8d057982015-10-30 18:48:08 +1100485 MSG_ERR("Short read from bt (%d vs %d)\n", r, data[0] + 1);
486 r = 0;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100487 goto out1;
488 }
489
Cyril Bur1aa096a2015-10-28 15:24:52 +1100490 new = bt_q_enqueue(context, data);
491 if (!new) {
492 r = -ENOMEM;
493 goto out1;
494 }
495 if (new == bt_q_get_head(context)) {
496 struct itimerspec ts;
497 /*
498 * Enqueued onto an empty list, setup a timer for sending a
499 * timeout
500 */
501 ts.it_interval.tv_sec = 0;
502 ts.it_interval.tv_nsec = 0;
503 ts.it_value.tv_nsec = 0;
504 ts.it_value.tv_sec = BT_HOST_TIMEOUT_SEC;
505 r = timerfd_settime(context->fds[TIMER_FD].fd, 0, &ts, NULL);
506 if (r == -1)
507 MSG_ERR("Couldn't set timerfd\n");
508 }
509
Cyril Burfbff9f52015-10-30 18:44:06 +1100510 r = sd_bus_message_new_signal(context->bus, &msg, OBJ_NAME, DBUS_NAME, "ReceivedMessage");
Cyril Bur1aa096a2015-10-28 15:24:52 +1100511 if (r < 0) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100512 MSG_ERR("Failed to create signal: %s\n", strerror(-r));
Cyril Bur1aa096a2015-10-28 15:24:52 +1100513 goto out1;
514 }
Cyril Burfbff9f52015-10-30 18:44:06 +1100515
516 r = sd_bus_message_append(msg, "yyyy", new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
517 if (r < 0) {
518 MSG_ERR("Couldn't append to signal: %s\n", strerror(-r));
519 goto out1_free;
520 }
521
522 r = sd_bus_message_append_array(msg, 'y', data + 4, new->req.data_len);
523 if (r < 0) {
524 MSG_ERR("Couldn't append array to signal\n", strerror(-r));
525 goto out1_free;
526 }
527
528 MSG_OUT("Sending dbus signal with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x\n",
529 new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
530
531 if (context->debug) {
532 int i;
533
534 for (i = 0; i < new->req.data_len; i++) {
535 if (i && i % 8 == 0)
536 MSG_OUT("\n");
537 MSG_OUT("0x%02x ", data[i + 4]);
538 }
539 MSG_OUT("\n");
540 }
541
542 /* Note we only actually keep the request data in the queue when debugging */
543 r = sd_bus_send(context->bus, msg, NULL);
544 if (r < 0) {
545 MSG_ERR("Couldn't emit dbus signal: %s\n", strerror(-r));
546 goto out1_free;
547 }
Cyril Bur1aa096a2015-10-28 15:24:52 +1100548 r = 0;
Cyril Burfbff9f52015-10-30 18:44:06 +1100549out1_free:
550 sd_bus_message_unref(msg);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100551out1:
552 err = r;
553 }
554
555 if (context->fds[BT_FD].revents & POLLOUT) {
556 struct bt_queue *bt_msg;
557 bt_msg = bt_q_get_msg(context);
558 if (!bt_msg) {
559 /* Odd, this shouldn't really happen */
560 MSG_ERR("Got a POLLOUT on %s but no message is ready to be written\n");
561 r = 0;
562 goto out;
563 }
564 r = bt_host_write(context, bt_msg);
565 if (r < 0)
Cyril Bur99d545d2015-10-30 14:50:10 +1100566 MSG_ERR("Problem putting request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n"
567 "out to %s", BT_HOST_PATH, bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun,
Cyril Bur1aa096a2015-10-28 15:24:52 +1100568 bt_msg->rsp.cmd, bt_msg->rsp.cc);
569 else
Cyril Bur99d545d2015-10-30 14:50:10 +1100570 MSG_OUT("Completed request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n",
571 bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun, bt_msg->rsp.cmd, bt_msg->rsp.cc);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100572 }
573out:
574 return err ? err : r;
575}
576
577static void usage(const char *name)
578{
579 fprintf(stderr, "Usage %s\n", name);
580 fprintf(stderr, "\t--verbose\t Be verbose\n\n");
581}
582
583static const sd_bus_vtable ipmid_vtable[] = {
584 SD_BUS_VTABLE_START(0),
585 SD_BUS_METHOD("sendMessage", "yyyyyay", "x", &method_send_message, SD_BUS_VTABLE_UNPRIVILEGED),
586 SD_BUS_METHOD("setAttention", "", "x", &method_send_sms_atn, SD_BUS_VTABLE_UNPRIVILEGED),
Cyril Bur104200b2015-10-30 14:34:58 +1100587 SD_BUS_SIGNAL("ReceivedMessage", "yyyyay", 0),
Cyril Bur1aa096a2015-10-28 15:24:52 +1100588 SD_BUS_VTABLE_END
589};
590
591int main(int argc, char *argv[]) {
592 struct btbridged_context context;
593 struct bt_queue *bt_q;
594 const char *path;
595 const char *name = argv[0];
596 int opt, polled, r, daemonise;
597 uint8_t buf[BT_MAX_MESSAGE];
598
599 static struct option long_options[] = {
600 {"verbose", no_argument, &verbose, 1},
601 {0, 0, 0, 0}
602 };
603
Cyril Bur1aa096a2015-10-28 15:24:52 +1100604 context.debug = 0;
605 context.bt_q = NULL;
606
607 while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
608 switch (opt) {
609 case 0:
610 MSG_OUT("Found verbosity flag\n");
611 break;
612 default:
613 usage(name);
614 exit(EXIT_FAILURE);
615 }
616 }
617
618 MSG_OUT("Starting\n");
619 r = sd_bus_default_system(&context.bus);
620 if (r < 0) {
621 MSG_ERR("Failed to connect to system bus: %s\n", strerror(-r));
622 goto finish;
623 }
624
625 MSG_OUT("Registering dbus methods/signals\n");
626 r = sd_bus_add_object_vtable(context.bus,
627 NULL,
628 OBJ_NAME,
629 DBUS_NAME,
630 ipmid_vtable,
631 &context);
632 if (r < 0) {
633 MSG_ERR("Failed to issue method call: %s\n", strerror(-r));
634 goto finish;
635 }
636
637 MSG_OUT("Requesting dbus name: %s\n", DBUS_NAME);
638 r = sd_bus_request_name(context.bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING);
639 if (r < 0) {
640 MSG_ERR("Failed to acquire service name: %s\n", strerror(-r));
641 goto finish;
642 }
643
644 MSG_OUT("Getting dbus file descriptors\n");
645 context.fds[SD_BUS_FD].fd = sd_bus_get_fd(context.bus);
646 if (context.fds[SD_BUS_FD].fd < 0) {
647 r = -errno;
648 MSG_OUT("Couldn't get the bus file descriptor: %s\n", strerror(errno));
649 goto finish;
650 }
651
652 MSG_OUT("Opening %s\n", BT_HOST_PATH);
653 context.fds[BT_FD].fd = open(BT_HOST_PATH, O_RDWR | O_NONBLOCK);
654 if (context.fds[BT_FD].fd < 0) {
655 r = -errno;
656 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", BT_HOST_PATH, strerror(errno));
657 goto finish;
658 }
659
660 MSG_OUT("Creating timer fd\n");
661 context.fds[TIMER_FD].fd = timerfd_create(CLOCK_MONOTONIC, 0);
662 if (context.fds[TIMER_FD].fd < 0) {
663 r = -errno;
664 MSG_ERR("Couldn't create timer fd: %s\n", strerror(errno));
665 goto finish;
666 }
667 context.fds[SD_BUS_FD].events = POLLIN;
668 context.fds[BT_FD].events = POLLIN;
669 context.fds[TIMER_FD].events = POLLIN;
670
671 MSG_OUT("Entering polling loop\n");
672
673 while (running) {
674 polled = poll(context.fds, TOTAL_FDS, 1000);
675 if (polled == 0)
676 continue;
677 if (polled < 0) {
678 r = -errno;
679 MSG_ERR("Error from poll(): %s\n", strerror(errno));
680 goto finish;
681 }
682 r = dispatch_sd_bus(&context);
683 if (r < 0) {
684 MSG_ERR("Error handling dbus event: %s\n", strerror(-r));
685 goto finish;
686 }
687 r = dispatch_bt(&context);
688 if (r < 0) {
689 MSG_ERR("Error handling BT event: %s\n", strerror(-r));
690 goto finish;
691 }
692 r = dispatch_timer(&context);
693 if (r < 0) {
694 MSG_ERR("Error handling timer event: %s\n", strerror(-r));
695 goto finish;
696 }
697 }
698
699finish:
700 if (bt_q_get_head(&context)) {
701 MSG_ERR("Unresponded BT Message!\n");
702 while (bt_q_dequeue(&context));
703 }
704 close(context.fds[BT_FD].fd);
705 close(context.fds[TIMER_FD].fd);
706 sd_bus_unref(context.bus);
707
708 return r;
709}
710