blob: c57843d3dbadf9bb96e7e7ac05c0e2ee3dda3f96 [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
Joel Stanleyda6e94c2015-11-23 19:27:34 +103016#include <assert.h>
Cyril Bur1aa096a2015-10-28 15:24:52 +110017#include <errno.h>
Joel Stanleyda6e94c2015-11-23 19:27:34 +103018#include <fcntl.h>
19#include <getopt.h>
20#include <limits.h>
21#include <poll.h>
Cyril Bur1aa096a2015-10-28 15:24:52 +110022#include <stdint.h>
23#include <stdio.h>
24#include <stdlib.h>
25#include <string.h>
Cyril Bur1aa096a2015-10-28 15:24:52 +110026#include <syslog.h>
Joel Stanleyda6e94c2015-11-23 19:27:34 +103027#include <sys/mman.h>
Joel Stanleyfbade632015-11-23 19:46:12 +103028#include <sys/ioctl.h>
Joel Stanleyda6e94c2015-11-23 19:27:34 +103029#include <sys/stat.h>
30#include <sys/timerfd.h>
Cyril Bur1aa096a2015-10-28 15:24:52 +110031#include <time.h>
Joel Stanleyda6e94c2015-11-23 19:27:34 +103032#include <unistd.h>
Cyril Bur1aa096a2015-10-28 15:24:52 +110033
34#include <linux/bt-host.h>
35
36#include <systemd/sd-bus.h>
37
38#define PREFIX "[BTBRIDGED] "
39
40#define BT_HOST_PATH "/dev/bt-host"
41#define BT_HOST_TIMEOUT_SEC 1
42#define BT_MAX_MESSAGE 64
43
44#define DBUS_NAME "org.openbmc.HostIpmi"
45#define OBJ_NAME "/org/openbmc/HostIpmi/1"
46
47#define SD_BUS_FD 0
48#define BT_FD 1
49#define TIMER_FD 2
50#define TOTAL_FDS 3
51
52#define MSG_OUT(f_, ...) do { if (verbose) { printf(PREFIX); printf((f_), ##__VA_ARGS__); } } while(0)
53#define MSG_ERR(f_, ...) do { \
54 if (verbose) { \
55 fprintf(stderr, PREFIX); \
56 fprintf(stderr, (f_), ##__VA_ARGS__); \
57 } \
58 } while(0)
59
60struct ipmi_msg {
61 uint8_t netfn;
62 uint8_t lun;
63 uint8_t seq;
64 uint8_t cmd;
65 uint8_t cc; /* Only used on responses */
66 uint8_t *data;
67 size_t data_len;
68};
69
70struct bt_queue {
71 struct ipmi_msg req;
72 struct ipmi_msg rsp;
73 struct timespec start;
74 int expired;
75 sd_bus_message *call;
76 struct bt_queue *next;
77};
78
Cyril Bur82eafec2015-10-30 19:19:11 +110079struct btbridged_context {
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;
Cyril Bur82eafec2015-10-30 19:19:11 +110087static int debug;
Cyril Bur1aa096a2015-10-28 15:24:52 +110088
89static struct bt_queue *bt_q_get_head(struct btbridged_context *context)
90{
91 return context ? context->bt_q : NULL;
92}
93
94static struct bt_queue *bt_q_get_seq(struct btbridged_context *context, uint8_t seq)
95{
96 struct bt_queue *t;
97
98 assert(context);
99
100 t = context->bt_q;
101
102 while (t && t->req.seq != seq)
103 t = t->next;
104
105 return t;
106}
107
108static struct bt_queue *bt_q_get_msg(struct btbridged_context *context)
109{
110 struct bt_queue *t;
111
112 assert(context);
113
114 t = context->bt_q;
115
116 while (t && (!t->call && !t->expired))
117 t = t->next;
118
119 return t;
120}
121
122static struct bt_queue *bt_q_enqueue(struct btbridged_context *context, uint8_t *bt_data)
123{
124 struct bt_queue *n;
125 struct bt_queue *bt_q;
126 int len;
127
128 assert(context && bt_data);
129
130 /*
131 * len here is the length of the array.
132 * Helpfully BT doesn't count the length byte
133 */
134 len = bt_data[0] + 1;
135 if (len < 4) {
136 MSG_ERR("Trying to queue a BT message with a short length (%d)\n", len);
137 return NULL;
138 }
139
140 bt_q = context->bt_q;
141
142 n = calloc(1, sizeof(struct bt_queue));
143 if (!n)
144 return NULL;
145
Cyril Bur82eafec2015-10-30 19:19:11 +1100146 if (debug) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100147 n->req.data = malloc(len - 4);
148 if (n->req.data)
149 n->req.data = memcpy(n->req.data, bt_data + 4, len - 4);
150 }
151 n->req.data_len = len - 4;
152 /* Don't count the lenfn/ln, seq and command */
153 n->req.netfn = bt_data[1] >> 2;
154 n->req.lun = bt_data[1] & 0x3;
155 n->req.seq = bt_data[2];
156 n->req.cmd = bt_data[3];
157 if (clock_gettime(CLOCK_MONOTONIC, &n->start) == -1) {
158 MSG_ERR("Couldn't clock_gettime(): %s\n", strerror(errno));
159 free(n);
160 return NULL;
161 }
162 if (!bt_q) {
163 context->bt_q = n;
164 } else {
165 struct bt_queue *t = bt_q;
166
167 while (t->next)
168 t = t->next;
169
170 t->next = n;
171 }
172
173 return n;
174}
175
176static void bt_q_free(struct bt_queue *bt_q)
177{
178 if (!bt_q)
179 return;
180
181 /* Unrefing sd_bus_message should free(rsp.data) */
182 if (bt_q->call)
183 sd_bus_message_unref(bt_q->call);
184
185 free(bt_q->req.data);
186 free(bt_q);
187}
188
189static struct bt_queue *bt_q_drop(struct btbridged_context *context, struct bt_queue *element)
190{
191 struct bt_queue *r;
192 struct bt_queue *bt_q;
193
194 assert(context);
195
196 bt_q = context->bt_q;
197 if (!element || !bt_q)
198 return NULL;
199
200
201 r = bt_q;
202 if (r == bt_q) {
203 context->bt_q = bt_q->next;
204 } else {
205 while (r && r->next != element)
206 r = r->next;
207
208 if (!r) {
209 MSG_ERR("Didn't find element %p in queue\n", element);
210 bt_q_free(element);
211 return NULL;
212 }
213 r->next = r->next->next;
214 }
215 bt_q_free(element);
216
217 return context->bt_q;
218}
219
220static struct bt_queue *bt_q_dequeue(struct btbridged_context *context)
221{
222 struct bt_queue *r;
223 struct bt_queue *bt_q;
224
225 assert(context);
226
227 bt_q = context->bt_q;
228 if (!bt_q) {
229 MSG_ERR("Dequeuing empty queue!\n");
230 return NULL;
231 }
232
233 r = bt_q->next;
234 bt_q_free(bt_q);
235 context->bt_q = r;
236
237 return r;
238}
239
Joel Stanley9ae661c2015-11-23 19:49:29 +1030240static int method_send_sms_atn(sd_bus_message *msg, void *userdata,
241 sd_bus_error *ret_error)
Cyril Bur1aa096a2015-10-28 15:24:52 +1100242{
Chris Austenc0395682015-11-20 17:12:49 -0600243 int r;
Joel Stanley9ae661c2015-11-23 19:49:29 +1030244 struct btbridged_context *bt_fd = userdata;
Chris Austenc0395682015-11-20 17:12:49 -0600245
Joel Stanley9ae661c2015-11-23 19:49:29 +1030246 MSG_OUT("Sending SMS_ATN ioctl (%d) to %s\n",
247 BT_HOST_IOCTL_SMS_ATN, BT_HOST_PATH);
Chris Austenc0395682015-11-20 17:12:49 -0600248
249 r = ioctl(bt_fd->fds[BT_FD].fd, BT_HOST_IOCTL_SMS_ATN);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100250 if (r == -1) {
251 r = errno;
Chris Austenc0395682015-11-20 17:12:49 -0600252 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 +1100253 return sd_bus_reply_method_errno(msg, errno, ret_error);
254 }
255
256 r = 0;
257 return sd_bus_reply_method_return(msg, "x", r);
258}
259
260static int method_send_message(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
261{
262 struct btbridged_context *context;
263 struct bt_queue *bt_msg;
Joel Stanleya7204212015-11-23 19:47:10 +1030264 uint8_t *data;
265 size_t data_sz;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100266 uint8_t netfn, lun, seq, cmd, cc;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100267 /*
268 * Doesn't say it anywhere explicitly but it looks like returning 0 or
269 * negative is BAD...
270 */
271 int r = 1;
272
273 context = (struct btbridged_context *)userdata;
274 if (!context) {
275 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Internal error");
276 r = 0;
277 goto out;
278 }
Cyril Bur99d545d2015-10-30 14:50:10 +1100279 r = sd_bus_message_read(msg, "yyyyy", &seq, &netfn, &lun, &cmd, &cc);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100280 if (r < 0) {
281 MSG_ERR("Couldn't parse leading bytes of message: %s\n", strerror(-r));
282 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message");
283 r = -EINVAL;
284 goto out;
285 }
286 r = sd_bus_message_read_array(msg, 'y', (const void **)&data, &data_sz);
287 if (r < 0) {
288 MSG_ERR("Couldn't parse data bytes of message: %s\n", strerror(-r));
289 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message data");
290 r = -EINVAL;
291 goto out;
292 }
293
294 bt_msg = bt_q_get_seq(context, seq);
295 if (!bt_msg) {
296 sd_bus_error_set_const(ret_error, "org.openbmc.error", "No matching request");
297 MSG_ERR("Failed to find matching request for dbus method with seq: 0x%02x\n", seq);
298 r = -EINVAL;
299 goto out;
300 }
Cyril Bur104200b2015-10-30 14:34:58 +1100301 MSG_OUT("Received a dbus response for msg with seq 0x%02x\n", seq);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100302 bt_msg->call = sd_bus_message_ref(msg);
303 bt_msg->rsp.netfn = netfn;
304 bt_msg->rsp.lun = lun;
305 bt_msg->rsp.seq = seq;
306 bt_msg->rsp.cmd = cmd;
307 bt_msg->rsp.cc = cc;
308 bt_msg->rsp.data_len = data_sz;
309 /* Because we've ref'ed the msg, I hope we don't need to memcpy data */
310 bt_msg->rsp.data = data;
311
312 /* Now that we have a response */
313 context->fds[BT_FD].events |= POLLOUT;
314
315out:
316 return r;
317}
318
319static int bt_host_write(struct btbridged_context *context, struct bt_queue *bt_msg)
320{
321 struct bt_queue *head;
322 uint8_t data[BT_MAX_MESSAGE] = { 0 };
323 sd_bus_message *msg = NULL;
324 int r, len;
325
326 assert(context);
327
328 if (!bt_msg)
329 return -EINVAL;
330
331 head = bt_q_get_head(context);
332 if (bt_msg == head) {
333 struct itimerspec ts;
334 /* Need to adjust the timer */
335 ts.it_interval.tv_sec = 0;
336 ts.it_interval.tv_nsec = 0;
337 if (head->next) {
338 ts.it_value = head->next->start;
339 ts.it_value.tv_sec += BT_HOST_TIMEOUT_SEC;
340 MSG_OUT("Adjusting timer for next element\n");
341 } else {
342 ts.it_value.tv_nsec = 0;
343 ts.it_value.tv_sec = 0;
344 MSG_OUT("Disabling timer, no elements remain in queue\n");
345 }
346 r = timerfd_settime(context->fds[TIMER_FD].fd, TFD_TIMER_ABSTIME, &ts, NULL);
347 if (r == -1)
348 MSG_ERR("Couldn't set timerfd\n");
349 }
350 data[1] = (bt_msg->rsp.netfn << 2) | (bt_msg->rsp.lun & 0x3);
351 data[2] = bt_msg->rsp.seq;
352 data[3] = bt_msg->rsp.cmd;
353 data[4] = bt_msg->rsp.cc;
354 if (bt_msg->rsp.data_len > sizeof(data) - 5) {
Joel Stanleyf84329e2015-11-23 19:48:43 +1030355 MSG_ERR("Response message size (%zu) too big, truncating\n", bt_msg->rsp.data_len);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100356 bt_msg->rsp.data_len = sizeof(data) - 5;
357 }
358 /* netfn/len + seq + cmd + cc = 4 */
359 data[0] = bt_msg->rsp.data_len + 4;
360 if (bt_msg->rsp.data_len)
361 memcpy(data + 5, bt_msg->rsp.data, bt_msg->rsp.data_len);
362 /* Count the data[0] byte */
363 len = write(context->fds[BT_FD].fd, data, data[0] + 1);
Joel Stanleyb92a9332015-11-23 20:02:30 +1030364 if (len == -1) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100365 r = errno;
366 MSG_ERR("Error writing to %s: %s\n", BT_HOST_PATH, strerror(errno));
367 if (bt_msg->call) {
368 r = sd_bus_message_new_method_errno(bt_msg->call, &msg, r, NULL);
369 if (r < 0)
370 MSG_ERR("Couldn't create response error\n");
371 }
372 goto out;
373 } else {
374 if (len != data[0] + 1)
375 MSG_ERR("Possible short write to %s, desired len: %d, written len: %d\n", BT_HOST_PATH, data[0] + 1, len);
376 else
377 MSG_OUT("Successfully wrote %d of %d bytes to %s\n", len, data[0] + 1, BT_HOST_PATH);
378
379 if (bt_msg->call) {
380 r = sd_bus_message_new_method_return(bt_msg->call, &msg);
381 if (r < 0) {
382 MSG_ERR("Couldn't create response message\n");
383 goto out;
384 }
385 r = 0; /* Just to be explicit about what we're sending back */
386 r = sd_bus_message_append(msg, "x", r);
387 if (r < 0) {
388 MSG_ERR("Couldn't append result to response\n");
389 goto out;
390 }
391
392 }
393 }
394
395out:
396 if (bt_msg->call) {
397 if (sd_bus_send(context->bus, msg, NULL) < 0)
398 MSG_ERR("Couldn't send response message\n");
399 sd_bus_message_unref(msg);
400 }
401 bt_q_drop(context, bt_msg);
402
403 /*
404 * There isn't another message ready to be sent so turn off POLLOUT
405 */
406 if (!bt_q_get_msg(context)) {
407 MSG_OUT("Turning off POLLOUT for the BT in poll()\n");
408 context->fds[BT_FD].events = POLLIN;
409 }
410
411 return r;
412}
413
414static int dispatch_timer(struct btbridged_context *context)
415{
416 int r = 0;
417 if (context->fds[TIMER_FD].revents & POLLIN) {
418 sd_bus_message *msg;
419 struct bt_queue *head;
420
421 head = bt_q_get_head(context);
422 if (!head) {
423 /* Odd, timer expired but we didn't have a message to timeout */
424 MSG_ERR("No message found to send timeout\n");
425 return 0;
426 }
427 if (head->call) {
428 r = sd_bus_message_new_method_errno(head->call, &msg, ETIMEDOUT, NULL);
429 if (r < 0) {
430 MSG_ERR("Couldn't create response error\n");
431 } else {
432 if (sd_bus_send(context->bus, msg, NULL) < 0)
433 MSG_ERR("Couldn't send response message\n");
434 sd_bus_message_unref(msg);
435 }
436 MSG_ERR("Message with seq 0x%02x is being timed out despite "
437 "appearing to have been responded to. Slow BT?\n", head->rsp.seq);
438 }
439
440 /* Set expiry */
441 head->expired = 1;
442 head->rsp.seq = head->req.seq;
443 head->rsp.netfn = head->req.netfn + 1;
444 head->rsp.lun = head->req.lun;
445 head->rsp.cc = 0xce; /* Command response could not be provided */
446 head->rsp.cmd = head->req.cmd;
447 /* These should already be zeroed but best to be sure */
448 head->rsp.data_len = 0;
449 head->rsp.data = NULL;
450 head->call = NULL;
451 MSG_OUT("Timeout on msg with seq: 0x%02x\n", head->rsp.seq);
452 /* Turn on POLLOUT so we'll write this one next */
453 context->fds[BT_FD].events |= POLLOUT;
454 }
455
456 return 0;
457}
458
459static int dispatch_sd_bus(struct btbridged_context *context)
460{
461 int r = 0;
462 if (context->fds[SD_BUS_FD].revents) {
463 r = sd_bus_process(context->bus, NULL);
464 if (r > 0)
465 MSG_OUT("Processed %d dbus events\n", r);
466 }
467
468 return r;
469}
470
471static int dispatch_bt(struct btbridged_context *context)
472{
473 int err = 0;
474 int r;
475
476 assert(context);
477
478 if (context->fds[BT_FD].revents & POLLIN) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100479 sd_bus_message *msg;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100480 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) {
Joel Stanleyf84329e2015-11-23 19:48:43 +1030528 MSG_ERR("Couldn't append array to signal: %s\n", strerror(-r));
Cyril Burfbff9f52015-10-30 18:44:06 +1100529 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 */
Joel Stanleyf84329e2015-11-23 19:48:43 +1030568 MSG_ERR("Got a POLLOUT but no message is ready to be written\n");
Cyril Bur1aa096a2015-10-28 15:24:52 +1100569 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"
Joel Stanleyf84329e2015-11-23 19:48:43 +1030575 "out to %s", bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun,
576 bt_msg->rsp.cmd, bt_msg->rsp.cc, BT_HOST_PATH);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100577 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;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100601 const char *name = argv[0];
Joel Stanleya7204212015-11-23 19:47:10 +1030602 int opt, polled, r;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100603
604 static struct option long_options[] = {
Cyril Bur82eafec2015-10-30 19:19:11 +1100605 { "verbose", no_argument, &verbose, 1 },
606 { "debug", no_argument, &debug, 1 },
607 { 0, 0, 0, 0 }
Cyril Bur1aa096a2015-10-28 15:24:52 +1100608 };
609
Cyril Bur1aa096a2015-10-28 15:24:52 +1100610 context.bt_q = NULL;
611
612 while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
613 switch (opt) {
614 case 0:
Cyril Bur1aa096a2015-10-28 15:24:52 +1100615 break;
616 default:
617 usage(name);
618 exit(EXIT_FAILURE);
619 }
620 }
621
Cyril Bur82eafec2015-10-30 19:19:11 +1100622 if (verbose)
623 MSG_OUT("Found verbosity flag\n");
624
625 if (debug) {
626 if (!verbose)
627 MSG_OUT("Turning on verbosity because debug flag found\n");
628 else
629 MSG_OUT("Found debug flag\n");
630 }
631
Cyril Bur1aa096a2015-10-28 15:24:52 +1100632 MSG_OUT("Starting\n");
633 r = sd_bus_default_system(&context.bus);
634 if (r < 0) {
635 MSG_ERR("Failed to connect to system bus: %s\n", strerror(-r));
636 goto finish;
637 }
638
639 MSG_OUT("Registering dbus methods/signals\n");
640 r = sd_bus_add_object_vtable(context.bus,
641 NULL,
642 OBJ_NAME,
643 DBUS_NAME,
644 ipmid_vtable,
645 &context);
646 if (r < 0) {
647 MSG_ERR("Failed to issue method call: %s\n", strerror(-r));
648 goto finish;
649 }
650
651 MSG_OUT("Requesting dbus name: %s\n", DBUS_NAME);
652 r = sd_bus_request_name(context.bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING);
653 if (r < 0) {
654 MSG_ERR("Failed to acquire service name: %s\n", strerror(-r));
655 goto finish;
656 }
657
658 MSG_OUT("Getting dbus file descriptors\n");
659 context.fds[SD_BUS_FD].fd = sd_bus_get_fd(context.bus);
660 if (context.fds[SD_BUS_FD].fd < 0) {
661 r = -errno;
662 MSG_OUT("Couldn't get the bus file descriptor: %s\n", strerror(errno));
663 goto finish;
664 }
665
666 MSG_OUT("Opening %s\n", BT_HOST_PATH);
667 context.fds[BT_FD].fd = open(BT_HOST_PATH, O_RDWR | O_NONBLOCK);
668 if (context.fds[BT_FD].fd < 0) {
669 r = -errno;
670 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", BT_HOST_PATH, strerror(errno));
671 goto finish;
672 }
673
674 MSG_OUT("Creating timer fd\n");
675 context.fds[TIMER_FD].fd = timerfd_create(CLOCK_MONOTONIC, 0);
676 if (context.fds[TIMER_FD].fd < 0) {
677 r = -errno;
678 MSG_ERR("Couldn't create timer fd: %s\n", strerror(errno));
679 goto finish;
680 }
681 context.fds[SD_BUS_FD].events = POLLIN;
682 context.fds[BT_FD].events = POLLIN;
683 context.fds[TIMER_FD].events = POLLIN;
684
685 MSG_OUT("Entering polling loop\n");
686
687 while (running) {
688 polled = poll(context.fds, TOTAL_FDS, 1000);
689 if (polled == 0)
690 continue;
691 if (polled < 0) {
692 r = -errno;
693 MSG_ERR("Error from poll(): %s\n", strerror(errno));
694 goto finish;
695 }
696 r = dispatch_sd_bus(&context);
697 if (r < 0) {
698 MSG_ERR("Error handling dbus event: %s\n", strerror(-r));
699 goto finish;
700 }
701 r = dispatch_bt(&context);
702 if (r < 0) {
703 MSG_ERR("Error handling BT event: %s\n", strerror(-r));
704 goto finish;
705 }
706 r = dispatch_timer(&context);
707 if (r < 0) {
708 MSG_ERR("Error handling timer event: %s\n", strerror(-r));
709 goto finish;
710 }
711 }
712
713finish:
714 if (bt_q_get_head(&context)) {
715 MSG_ERR("Unresponded BT Message!\n");
716 while (bt_q_dequeue(&context));
717 }
718 close(context.fds[BT_FD].fd);
719 close(context.fds[TIMER_FD].fd);
720 sd_bus_unref(context.bus);
721
722 return r;
723}
724