blob: d1de79fbd94d7d97bd73c269518ebf80358f860e [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
Cyril Bur82eafec2015-10-30 19:19:11 +110078struct btbridged_context {
Cyril Bur1aa096a2015-10-28 15:24:52 +110079 struct pollfd fds[TOTAL_FDS];
80 struct sd_bus *bus;
81 struct bt_queue *bt_q;
82};
83
84static int running = 1;
85static int verbose;
Cyril Bur82eafec2015-10-30 19:19:11 +110086static int debug;
Cyril Bur1aa096a2015-10-28 15:24:52 +110087
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
Cyril Bur82eafec2015-10-30 19:19:11 +1100145 if (debug) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100146 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{
Chris Austenc0395682015-11-20 17:12:49 -0600241 int r;
242 struct btbridged_context *bt_fd = (struct btbridged_context *)userdata;
243
Cyril Bur1aa096a2015-10-28 15:24:52 +1100244 MSG_OUT("Sending SMS_ATN ioctl() to %s\n", BT_HOST_PATH);
Chris Austenc0395682015-11-20 17:12:49 -0600245
246
247 r = ioctl(bt_fd->fds[BT_FD].fd, BT_HOST_IOCTL_SMS_ATN);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100248 if (r == -1) {
249 r = errno;
Chris Austenc0395682015-11-20 17:12:49 -0600250 MSG_ERR("Couldn't ioctl() to 0x%x, %s: %s\n", bt_fd->fds[BT_FD].fd, BT_HOST_PATH, strerror(r));
Cyril Bur1aa096a2015-10-28 15:24:52 +1100251 return sd_bus_reply_method_errno(msg, errno, ret_error);
252 }
253
254 r = 0;
255 return sd_bus_reply_method_return(msg, "x", r);
256}
257
258static int method_send_message(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
259{
260 struct btbridged_context *context;
261 struct bt_queue *bt_msg;
262 uint8_t *data, *all_data;
263 size_t data_sz, all_data_sz;
264 uint8_t netfn, lun, seq, cmd, cc;
265 uint64_t cookie;
266 /*
267 * Doesn't say it anywhere explicitly but it looks like returning 0 or
268 * negative is BAD...
269 */
270 int r = 1;
271
272 context = (struct btbridged_context *)userdata;
273 if (!context) {
274 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Internal error");
275 r = 0;
276 goto out;
277 }
Cyril Bur99d545d2015-10-30 14:50:10 +1100278 r = sd_bus_message_read(msg, "yyyyy", &seq, &netfn, &lun, &cmd, &cc);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100279 if (r < 0) {
280 MSG_ERR("Couldn't parse leading bytes of message: %s\n", strerror(-r));
281 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message");
282 r = -EINVAL;
283 goto out;
284 }
285 r = sd_bus_message_read_array(msg, 'y', (const void **)&data, &data_sz);
286 if (r < 0) {
287 MSG_ERR("Couldn't parse data bytes of message: %s\n", strerror(-r));
288 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message data");
289 r = -EINVAL;
290 goto out;
291 }
292
293 bt_msg = bt_q_get_seq(context, seq);
294 if (!bt_msg) {
295 sd_bus_error_set_const(ret_error, "org.openbmc.error", "No matching request");
296 MSG_ERR("Failed to find matching request for dbus method with seq: 0x%02x\n", seq);
297 r = -EINVAL;
298 goto out;
299 }
Cyril Bur104200b2015-10-30 14:34:58 +1100300 MSG_OUT("Received a dbus response for msg with seq 0x%02x\n", seq);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100301 bt_msg->call = sd_bus_message_ref(msg);
302 bt_msg->rsp.netfn = netfn;
303 bt_msg->rsp.lun = lun;
304 bt_msg->rsp.seq = seq;
305 bt_msg->rsp.cmd = cmd;
306 bt_msg->rsp.cc = cc;
307 bt_msg->rsp.data_len = data_sz;
308 /* Because we've ref'ed the msg, I hope we don't need to memcpy data */
309 bt_msg->rsp.data = data;
310
311 /* Now that we have a response */
312 context->fds[BT_FD].events |= POLLOUT;
313
314out:
315 return r;
316}
317
318static int bt_host_write(struct btbridged_context *context, struct bt_queue *bt_msg)
319{
320 struct bt_queue *head;
321 uint8_t data[BT_MAX_MESSAGE] = { 0 };
322 sd_bus_message *msg = NULL;
323 int r, len;
324
325 assert(context);
326
327 if (!bt_msg)
328 return -EINVAL;
329
330 head = bt_q_get_head(context);
331 if (bt_msg == head) {
332 struct itimerspec ts;
333 /* Need to adjust the timer */
334 ts.it_interval.tv_sec = 0;
335 ts.it_interval.tv_nsec = 0;
336 if (head->next) {
337 ts.it_value = head->next->start;
338 ts.it_value.tv_sec += BT_HOST_TIMEOUT_SEC;
339 MSG_OUT("Adjusting timer for next element\n");
340 } else {
341 ts.it_value.tv_nsec = 0;
342 ts.it_value.tv_sec = 0;
343 MSG_OUT("Disabling timer, no elements remain in queue\n");
344 }
345 r = timerfd_settime(context->fds[TIMER_FD].fd, TFD_TIMER_ABSTIME, &ts, NULL);
346 if (r == -1)
347 MSG_ERR("Couldn't set timerfd\n");
348 }
349 data[1] = (bt_msg->rsp.netfn << 2) | (bt_msg->rsp.lun & 0x3);
350 data[2] = bt_msg->rsp.seq;
351 data[3] = bt_msg->rsp.cmd;
352 data[4] = bt_msg->rsp.cc;
353 if (bt_msg->rsp.data_len > sizeof(data) - 5) {
354 MSG_ERR("Response message size (%d) too big, truncating\n", bt_msg->rsp.data_len);
355 bt_msg->rsp.data_len = sizeof(data) - 5;
356 }
357 /* netfn/len + seq + cmd + cc = 4 */
358 data[0] = bt_msg->rsp.data_len + 4;
359 if (bt_msg->rsp.data_len)
360 memcpy(data + 5, bt_msg->rsp.data, bt_msg->rsp.data_len);
361 /* Count the data[0] byte */
362 len = write(context->fds[BT_FD].fd, data, data[0] + 1);
363 if (r == -1) {
364 r = errno;
365 MSG_ERR("Error writing to %s: %s\n", BT_HOST_PATH, strerror(errno));
366 if (bt_msg->call) {
367 r = sd_bus_message_new_method_errno(bt_msg->call, &msg, r, NULL);
368 if (r < 0)
369 MSG_ERR("Couldn't create response error\n");
370 }
371 goto out;
372 } else {
373 if (len != data[0] + 1)
374 MSG_ERR("Possible short write to %s, desired len: %d, written len: %d\n", BT_HOST_PATH, data[0] + 1, len);
375 else
376 MSG_OUT("Successfully wrote %d of %d bytes to %s\n", len, data[0] + 1, BT_HOST_PATH);
377
378 if (bt_msg->call) {
379 r = sd_bus_message_new_method_return(bt_msg->call, &msg);
380 if (r < 0) {
381 MSG_ERR("Couldn't create response message\n");
382 goto out;
383 }
384 r = 0; /* Just to be explicit about what we're sending back */
385 r = sd_bus_message_append(msg, "x", r);
386 if (r < 0) {
387 MSG_ERR("Couldn't append result to response\n");
388 goto out;
389 }
390
391 }
392 }
393
394out:
395 if (bt_msg->call) {
396 if (sd_bus_send(context->bus, msg, NULL) < 0)
397 MSG_ERR("Couldn't send response message\n");
398 sd_bus_message_unref(msg);
399 }
400 bt_q_drop(context, bt_msg);
401
402 /*
403 * There isn't another message ready to be sent so turn off POLLOUT
404 */
405 if (!bt_q_get_msg(context)) {
406 MSG_OUT("Turning off POLLOUT for the BT in poll()\n");
407 context->fds[BT_FD].events = POLLIN;
408 }
409
410 return r;
411}
412
413static int dispatch_timer(struct btbridged_context *context)
414{
415 int r = 0;
416 if (context->fds[TIMER_FD].revents & POLLIN) {
417 sd_bus_message *msg;
418 struct bt_queue *head;
419
420 head = bt_q_get_head(context);
421 if (!head) {
422 /* Odd, timer expired but we didn't have a message to timeout */
423 MSG_ERR("No message found to send timeout\n");
424 return 0;
425 }
426 if (head->call) {
427 r = sd_bus_message_new_method_errno(head->call, &msg, ETIMEDOUT, NULL);
428 if (r < 0) {
429 MSG_ERR("Couldn't create response error\n");
430 } else {
431 if (sd_bus_send(context->bus, msg, NULL) < 0)
432 MSG_ERR("Couldn't send response message\n");
433 sd_bus_message_unref(msg);
434 }
435 MSG_ERR("Message with seq 0x%02x is being timed out despite "
436 "appearing to have been responded to. Slow BT?\n", head->rsp.seq);
437 }
438
439 /* Set expiry */
440 head->expired = 1;
441 head->rsp.seq = head->req.seq;
442 head->rsp.netfn = head->req.netfn + 1;
443 head->rsp.lun = head->req.lun;
444 head->rsp.cc = 0xce; /* Command response could not be provided */
445 head->rsp.cmd = head->req.cmd;
446 /* These should already be zeroed but best to be sure */
447 head->rsp.data_len = 0;
448 head->rsp.data = NULL;
449 head->call = NULL;
450 MSG_OUT("Timeout on msg with seq: 0x%02x\n", head->rsp.seq);
451 /* Turn on POLLOUT so we'll write this one next */
452 context->fds[BT_FD].events |= POLLOUT;
453 }
454
455 return 0;
456}
457
458static int dispatch_sd_bus(struct btbridged_context *context)
459{
460 int r = 0;
461 if (context->fds[SD_BUS_FD].revents) {
462 r = sd_bus_process(context->bus, NULL);
463 if (r > 0)
464 MSG_OUT("Processed %d dbus events\n", r);
465 }
466
467 return r;
468}
469
470static int dispatch_bt(struct btbridged_context *context)
471{
472 int err = 0;
473 int r;
474
475 assert(context);
476
477 if (context->fds[BT_FD].revents & POLLIN) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100478 sd_bus_message *msg;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100479 int data_len;
480 struct bt_queue *new;
481 uint8_t data[BT_MAX_MESSAGE] = { 0 };
482
483 r = read(context->fds[BT_FD].fd, data, sizeof(data));
484 if (r < 0) {
485 MSG_ERR("Couldn't read from bt: %s\n", strerror(-r));
486 goto out1;
487 }
488 if (r < data[0] + 1) {
Cyril Bur8d057982015-10-30 18:48:08 +1100489 MSG_ERR("Short read from bt (%d vs %d)\n", r, data[0] + 1);
490 r = 0;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100491 goto out1;
492 }
493
Cyril Bur1aa096a2015-10-28 15:24:52 +1100494 new = bt_q_enqueue(context, data);
495 if (!new) {
496 r = -ENOMEM;
497 goto out1;
498 }
499 if (new == bt_q_get_head(context)) {
500 struct itimerspec ts;
501 /*
502 * Enqueued onto an empty list, setup a timer for sending a
503 * timeout
504 */
505 ts.it_interval.tv_sec = 0;
506 ts.it_interval.tv_nsec = 0;
507 ts.it_value.tv_nsec = 0;
508 ts.it_value.tv_sec = BT_HOST_TIMEOUT_SEC;
509 r = timerfd_settime(context->fds[TIMER_FD].fd, 0, &ts, NULL);
510 if (r == -1)
511 MSG_ERR("Couldn't set timerfd\n");
512 }
513
Cyril Burfbff9f52015-10-30 18:44:06 +1100514 r = sd_bus_message_new_signal(context->bus, &msg, OBJ_NAME, DBUS_NAME, "ReceivedMessage");
Cyril Bur1aa096a2015-10-28 15:24:52 +1100515 if (r < 0) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100516 MSG_ERR("Failed to create signal: %s\n", strerror(-r));
Cyril Bur1aa096a2015-10-28 15:24:52 +1100517 goto out1;
518 }
Cyril Burfbff9f52015-10-30 18:44:06 +1100519
520 r = sd_bus_message_append(msg, "yyyy", new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
521 if (r < 0) {
522 MSG_ERR("Couldn't append to signal: %s\n", strerror(-r));
523 goto out1_free;
524 }
525
526 r = sd_bus_message_append_array(msg, 'y', data + 4, new->req.data_len);
527 if (r < 0) {
528 MSG_ERR("Couldn't append array to signal\n", strerror(-r));
529 goto out1_free;
530 }
531
532 MSG_OUT("Sending dbus signal with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x\n",
533 new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
534
Cyril Bur82eafec2015-10-30 19:19:11 +1100535 if (debug) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100536 int i;
Cyril Bur82eafec2015-10-30 19:19:11 +1100537 /* If debug is on, so will verbose */
Cyril Burfbff9f52015-10-30 18:44:06 +1100538 for (i = 0; i < new->req.data_len; i++) {
Cyril Bur82eafec2015-10-30 19:19:11 +1100539 if (i % 8 == 0) {
540 if (i)
541 printf("\n");
542 MSG_OUT("\t");
543 }
544 printf("0x%02x ", data[i + 4]);
Cyril Burfbff9f52015-10-30 18:44:06 +1100545 }
Cyril Bur82eafec2015-10-30 19:19:11 +1100546 if (new->req.data_len)
547 printf("\n");
Cyril Burfbff9f52015-10-30 18:44:06 +1100548 }
549
550 /* Note we only actually keep the request data in the queue when debugging */
551 r = sd_bus_send(context->bus, msg, NULL);
552 if (r < 0) {
553 MSG_ERR("Couldn't emit dbus signal: %s\n", strerror(-r));
554 goto out1_free;
555 }
Cyril Bur1aa096a2015-10-28 15:24:52 +1100556 r = 0;
Cyril Burfbff9f52015-10-30 18:44:06 +1100557out1_free:
558 sd_bus_message_unref(msg);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100559out1:
560 err = r;
561 }
562
563 if (context->fds[BT_FD].revents & POLLOUT) {
564 struct bt_queue *bt_msg;
565 bt_msg = bt_q_get_msg(context);
566 if (!bt_msg) {
567 /* Odd, this shouldn't really happen */
568 MSG_ERR("Got a POLLOUT on %s but no message is ready to be written\n");
569 r = 0;
570 goto out;
571 }
572 r = bt_host_write(context, bt_msg);
573 if (r < 0)
Cyril Bur99d545d2015-10-30 14:50:10 +1100574 MSG_ERR("Problem putting request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n"
575 "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 +1100576 bt_msg->rsp.cmd, bt_msg->rsp.cc);
577 else
Cyril Bur99d545d2015-10-30 14:50:10 +1100578 MSG_OUT("Completed request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n",
579 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 +1100580 }
581out:
582 return err ? err : r;
583}
584
585static void usage(const char *name)
586{
587 fprintf(stderr, "Usage %s\n", name);
588 fprintf(stderr, "\t--verbose\t Be verbose\n\n");
589}
590
591static const sd_bus_vtable ipmid_vtable[] = {
592 SD_BUS_VTABLE_START(0),
593 SD_BUS_METHOD("sendMessage", "yyyyyay", "x", &method_send_message, SD_BUS_VTABLE_UNPRIVILEGED),
594 SD_BUS_METHOD("setAttention", "", "x", &method_send_sms_atn, SD_BUS_VTABLE_UNPRIVILEGED),
Cyril Bur104200b2015-10-30 14:34:58 +1100595 SD_BUS_SIGNAL("ReceivedMessage", "yyyyay", 0),
Cyril Bur1aa096a2015-10-28 15:24:52 +1100596 SD_BUS_VTABLE_END
597};
598
599int main(int argc, char *argv[]) {
600 struct btbridged_context context;
601 struct bt_queue *bt_q;
602 const char *path;
603 const char *name = argv[0];
604 int opt, polled, r, daemonise;
605 uint8_t buf[BT_MAX_MESSAGE];
606
607 static struct option long_options[] = {
Cyril Bur82eafec2015-10-30 19:19:11 +1100608 { "verbose", no_argument, &verbose, 1 },
609 { "debug", no_argument, &debug, 1 },
610 { 0, 0, 0, 0 }
Cyril Bur1aa096a2015-10-28 15:24:52 +1100611 };
612
Cyril Bur1aa096a2015-10-28 15:24:52 +1100613 context.bt_q = NULL;
614
615 while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
616 switch (opt) {
617 case 0:
Cyril Bur1aa096a2015-10-28 15:24:52 +1100618 break;
619 default:
620 usage(name);
621 exit(EXIT_FAILURE);
622 }
623 }
624
Cyril Bur82eafec2015-10-30 19:19:11 +1100625 if (verbose)
626 MSG_OUT("Found verbosity flag\n");
627
628 if (debug) {
629 if (!verbose)
630 MSG_OUT("Turning on verbosity because debug flag found\n");
631 else
632 MSG_OUT("Found debug flag\n");
633 }
634
Cyril Bur1aa096a2015-10-28 15:24:52 +1100635 MSG_OUT("Starting\n");
636 r = sd_bus_default_system(&context.bus);
637 if (r < 0) {
638 MSG_ERR("Failed to connect to system bus: %s\n", strerror(-r));
639 goto finish;
640 }
641
642 MSG_OUT("Registering dbus methods/signals\n");
643 r = sd_bus_add_object_vtable(context.bus,
644 NULL,
645 OBJ_NAME,
646 DBUS_NAME,
647 ipmid_vtable,
648 &context);
649 if (r < 0) {
650 MSG_ERR("Failed to issue method call: %s\n", strerror(-r));
651 goto finish;
652 }
653
654 MSG_OUT("Requesting dbus name: %s\n", DBUS_NAME);
655 r = sd_bus_request_name(context.bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING);
656 if (r < 0) {
657 MSG_ERR("Failed to acquire service name: %s\n", strerror(-r));
658 goto finish;
659 }
660
661 MSG_OUT("Getting dbus file descriptors\n");
662 context.fds[SD_BUS_FD].fd = sd_bus_get_fd(context.bus);
663 if (context.fds[SD_BUS_FD].fd < 0) {
664 r = -errno;
665 MSG_OUT("Couldn't get the bus file descriptor: %s\n", strerror(errno));
666 goto finish;
667 }
668
669 MSG_OUT("Opening %s\n", BT_HOST_PATH);
670 context.fds[BT_FD].fd = open(BT_HOST_PATH, O_RDWR | O_NONBLOCK);
671 if (context.fds[BT_FD].fd < 0) {
672 r = -errno;
673 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", BT_HOST_PATH, strerror(errno));
674 goto finish;
675 }
676
677 MSG_OUT("Creating timer fd\n");
678 context.fds[TIMER_FD].fd = timerfd_create(CLOCK_MONOTONIC, 0);
679 if (context.fds[TIMER_FD].fd < 0) {
680 r = -errno;
681 MSG_ERR("Couldn't create timer fd: %s\n", strerror(errno));
682 goto finish;
683 }
684 context.fds[SD_BUS_FD].events = POLLIN;
685 context.fds[BT_FD].events = POLLIN;
686 context.fds[TIMER_FD].events = POLLIN;
687
688 MSG_OUT("Entering polling loop\n");
689
690 while (running) {
691 polled = poll(context.fds, TOTAL_FDS, 1000);
692 if (polled == 0)
693 continue;
694 if (polled < 0) {
695 r = -errno;
696 MSG_ERR("Error from poll(): %s\n", strerror(errno));
697 goto finish;
698 }
699 r = dispatch_sd_bus(&context);
700 if (r < 0) {
701 MSG_ERR("Error handling dbus event: %s\n", strerror(-r));
702 goto finish;
703 }
704 r = dispatch_bt(&context);
705 if (r < 0) {
706 MSG_ERR("Error handling BT event: %s\n", strerror(-r));
707 goto finish;
708 }
709 r = dispatch_timer(&context);
710 if (r < 0) {
711 MSG_ERR("Error handling timer event: %s\n", strerror(-r));
712 goto finish;
713 }
714 }
715
716finish:
717 if (bt_q_get_head(&context)) {
718 MSG_ERR("Unresponded BT Message!\n");
719 while (bt_q_dequeue(&context));
720 }
721 close(context.fds[BT_FD].fd);
722 close(context.fds[TIMER_FD].fd);
723 sd_bus_unref(context.bus);
724
725 return r;
726}
727