blob: c2e00485c339d75607bb8a1d002b7bb5784563d8 [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
Cyril Bur201c8d72015-12-02 16:54:04 +110038#define PREFIX "BTBRIDGED"
Cyril Bur1aa096a2015-10-28 15:24:52 +110039
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
Cyril Bur201c8d72015-12-02 16:54:04 +110052#define MSG_OUT(f_, ...) do { if (verbose) { bt_log(LOG_INFO, f_, ##__VA_ARGS__); } } while(0)
53#define MSG_ERR(f_, ...) do { if (verbose) { bt_log(LOG_ERR, f_, ##__VA_ARGS__); } } while(0)
Cyril Bur1aa096a2015-10-28 15:24:52 +110054
55struct ipmi_msg {
56 uint8_t netfn;
57 uint8_t lun;
58 uint8_t seq;
59 uint8_t cmd;
60 uint8_t cc; /* Only used on responses */
61 uint8_t *data;
62 size_t data_len;
63};
64
65struct bt_queue {
66 struct ipmi_msg req;
67 struct ipmi_msg rsp;
68 struct timespec start;
69 int expired;
70 sd_bus_message *call;
71 struct bt_queue *next;
72};
73
Cyril Bur82eafec2015-10-30 19:19:11 +110074struct btbridged_context {
Cyril Bur1aa096a2015-10-28 15:24:52 +110075 struct pollfd fds[TOTAL_FDS];
76 struct sd_bus *bus;
77 struct bt_queue *bt_q;
78};
79
Cyril Bur201c8d72015-12-02 16:54:04 +110080static void (*bt_vlog)(int p, const char *fmt, va_list args);
Cyril Bur1aa096a2015-10-28 15:24:52 +110081static int running = 1;
82static int verbose;
Cyril Bur82eafec2015-10-30 19:19:11 +110083static int debug;
Cyril Bur1aa096a2015-10-28 15:24:52 +110084
Cyril Bur201c8d72015-12-02 16:54:04 +110085static void bt_log_console(int p, const char *fmt, va_list args)
86{
87 struct timespec time;
88 FILE *s = (p < LOG_WARNING) ? stdout : stderr;
89
90 clock_gettime(CLOCK_REALTIME, &time);
91
92 fprintf(s, "[%s %ld.%.9ld] ", PREFIX, time.tv_sec, time.tv_nsec);
93
94 vfprintf(s, fmt, args);
95}
96
97__attribute__((format(printf, 2, 3)))
98static void bt_log(int p, const char *fmt, ...)
99{
100 va_list args;
101
102 va_start(args, fmt);
103 bt_vlog(p, fmt, args);
104 va_end(args);
105}
106
Cyril Bur1aa096a2015-10-28 15:24:52 +1100107static struct bt_queue *bt_q_get_head(struct btbridged_context *context)
108{
109 return context ? context->bt_q : NULL;
110}
111
112static struct bt_queue *bt_q_get_seq(struct btbridged_context *context, uint8_t seq)
113{
114 struct bt_queue *t;
115
116 assert(context);
117
118 t = context->bt_q;
119
120 while (t && t->req.seq != seq)
121 t = t->next;
122
123 return t;
124}
125
126static struct bt_queue *bt_q_get_msg(struct btbridged_context *context)
127{
128 struct bt_queue *t;
129
130 assert(context);
131
132 t = context->bt_q;
133
134 while (t && (!t->call && !t->expired))
135 t = t->next;
136
137 return t;
138}
139
140static struct bt_queue *bt_q_enqueue(struct btbridged_context *context, uint8_t *bt_data)
141{
142 struct bt_queue *n;
143 struct bt_queue *bt_q;
144 int len;
145
146 assert(context && bt_data);
147
148 /*
149 * len here is the length of the array.
150 * Helpfully BT doesn't count the length byte
151 */
152 len = bt_data[0] + 1;
153 if (len < 4) {
154 MSG_ERR("Trying to queue a BT message with a short length (%d)\n", len);
155 return NULL;
156 }
157
158 bt_q = context->bt_q;
159
160 n = calloc(1, sizeof(struct bt_queue));
161 if (!n)
162 return NULL;
163
Cyril Bur82eafec2015-10-30 19:19:11 +1100164 if (debug) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100165 n->req.data = malloc(len - 4);
166 if (n->req.data)
167 n->req.data = memcpy(n->req.data, bt_data + 4, len - 4);
168 }
169 n->req.data_len = len - 4;
170 /* Don't count the lenfn/ln, seq and command */
171 n->req.netfn = bt_data[1] >> 2;
172 n->req.lun = bt_data[1] & 0x3;
173 n->req.seq = bt_data[2];
174 n->req.cmd = bt_data[3];
175 if (clock_gettime(CLOCK_MONOTONIC, &n->start) == -1) {
176 MSG_ERR("Couldn't clock_gettime(): %s\n", strerror(errno));
177 free(n);
178 return NULL;
179 }
180 if (!bt_q) {
181 context->bt_q = n;
182 } else {
183 struct bt_queue *t = bt_q;
184
185 while (t->next)
186 t = t->next;
187
188 t->next = n;
189 }
190
191 return n;
192}
193
194static void bt_q_free(struct bt_queue *bt_q)
195{
196 if (!bt_q)
197 return;
198
199 /* Unrefing sd_bus_message should free(rsp.data) */
200 if (bt_q->call)
201 sd_bus_message_unref(bt_q->call);
202
203 free(bt_q->req.data);
204 free(bt_q);
205}
206
207static struct bt_queue *bt_q_drop(struct btbridged_context *context, struct bt_queue *element)
208{
209 struct bt_queue *r;
210 struct bt_queue *bt_q;
211
212 assert(context);
213
214 bt_q = context->bt_q;
215 if (!element || !bt_q)
216 return NULL;
217
218
219 r = bt_q;
220 if (r == bt_q) {
221 context->bt_q = bt_q->next;
222 } else {
223 while (r && r->next != element)
224 r = r->next;
225
226 if (!r) {
227 MSG_ERR("Didn't find element %p in queue\n", element);
228 bt_q_free(element);
229 return NULL;
230 }
231 r->next = r->next->next;
232 }
233 bt_q_free(element);
234
235 return context->bt_q;
236}
237
238static struct bt_queue *bt_q_dequeue(struct btbridged_context *context)
239{
240 struct bt_queue *r;
241 struct bt_queue *bt_q;
242
243 assert(context);
244
245 bt_q = context->bt_q;
246 if (!bt_q) {
247 MSG_ERR("Dequeuing empty queue!\n");
248 return NULL;
249 }
250
251 r = bt_q->next;
252 bt_q_free(bt_q);
253 context->bt_q = r;
254
255 return r;
256}
257
Joel Stanley9ae661c2015-11-23 19:49:29 +1030258static int method_send_sms_atn(sd_bus_message *msg, void *userdata,
259 sd_bus_error *ret_error)
Cyril Bur1aa096a2015-10-28 15:24:52 +1100260{
Chris Austenc0395682015-11-20 17:12:49 -0600261 int r;
Joel Stanley9ae661c2015-11-23 19:49:29 +1030262 struct btbridged_context *bt_fd = userdata;
Chris Austenc0395682015-11-20 17:12:49 -0600263
Joel Stanley9ae661c2015-11-23 19:49:29 +1030264 MSG_OUT("Sending SMS_ATN ioctl (%d) to %s\n",
265 BT_HOST_IOCTL_SMS_ATN, BT_HOST_PATH);
Chris Austenc0395682015-11-20 17:12:49 -0600266
267 r = ioctl(bt_fd->fds[BT_FD].fd, BT_HOST_IOCTL_SMS_ATN);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100268 if (r == -1) {
269 r = errno;
Chris Austenc0395682015-11-20 17:12:49 -0600270 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 +1100271 return sd_bus_reply_method_errno(msg, errno, ret_error);
272 }
273
274 r = 0;
275 return sd_bus_reply_method_return(msg, "x", r);
276}
277
278static int method_send_message(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
279{
280 struct btbridged_context *context;
281 struct bt_queue *bt_msg;
Joel Stanleya7204212015-11-23 19:47:10 +1030282 uint8_t *data;
283 size_t data_sz;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100284 uint8_t netfn, lun, seq, cmd, cc;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100285 /*
286 * Doesn't say it anywhere explicitly but it looks like returning 0 or
287 * negative is BAD...
288 */
289 int r = 1;
290
291 context = (struct btbridged_context *)userdata;
292 if (!context) {
293 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Internal error");
294 r = 0;
295 goto out;
296 }
Cyril Bur99d545d2015-10-30 14:50:10 +1100297 r = sd_bus_message_read(msg, "yyyyy", &seq, &netfn, &lun, &cmd, &cc);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100298 if (r < 0) {
299 MSG_ERR("Couldn't parse leading bytes of message: %s\n", strerror(-r));
300 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message");
301 r = -EINVAL;
302 goto out;
303 }
304 r = sd_bus_message_read_array(msg, 'y', (const void **)&data, &data_sz);
305 if (r < 0) {
306 MSG_ERR("Couldn't parse data bytes of message: %s\n", strerror(-r));
307 sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message data");
308 r = -EINVAL;
309 goto out;
310 }
311
312 bt_msg = bt_q_get_seq(context, seq);
313 if (!bt_msg) {
314 sd_bus_error_set_const(ret_error, "org.openbmc.error", "No matching request");
315 MSG_ERR("Failed to find matching request for dbus method with seq: 0x%02x\n", seq);
316 r = -EINVAL;
317 goto out;
318 }
Cyril Bur104200b2015-10-30 14:34:58 +1100319 MSG_OUT("Received a dbus response for msg with seq 0x%02x\n", seq);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100320 bt_msg->call = sd_bus_message_ref(msg);
321 bt_msg->rsp.netfn = netfn;
322 bt_msg->rsp.lun = lun;
323 bt_msg->rsp.seq = seq;
324 bt_msg->rsp.cmd = cmd;
325 bt_msg->rsp.cc = cc;
326 bt_msg->rsp.data_len = data_sz;
327 /* Because we've ref'ed the msg, I hope we don't need to memcpy data */
328 bt_msg->rsp.data = data;
329
330 /* Now that we have a response */
331 context->fds[BT_FD].events |= POLLOUT;
332
333out:
334 return r;
335}
336
337static int bt_host_write(struct btbridged_context *context, struct bt_queue *bt_msg)
338{
339 struct bt_queue *head;
340 uint8_t data[BT_MAX_MESSAGE] = { 0 };
341 sd_bus_message *msg = NULL;
342 int r, len;
343
344 assert(context);
345
346 if (!bt_msg)
347 return -EINVAL;
348
349 head = bt_q_get_head(context);
350 if (bt_msg == head) {
351 struct itimerspec ts;
352 /* Need to adjust the timer */
353 ts.it_interval.tv_sec = 0;
354 ts.it_interval.tv_nsec = 0;
355 if (head->next) {
356 ts.it_value = head->next->start;
357 ts.it_value.tv_sec += BT_HOST_TIMEOUT_SEC;
358 MSG_OUT("Adjusting timer for next element\n");
359 } else {
360 ts.it_value.tv_nsec = 0;
361 ts.it_value.tv_sec = 0;
362 MSG_OUT("Disabling timer, no elements remain in queue\n");
363 }
364 r = timerfd_settime(context->fds[TIMER_FD].fd, TFD_TIMER_ABSTIME, &ts, NULL);
365 if (r == -1)
366 MSG_ERR("Couldn't set timerfd\n");
367 }
368 data[1] = (bt_msg->rsp.netfn << 2) | (bt_msg->rsp.lun & 0x3);
369 data[2] = bt_msg->rsp.seq;
370 data[3] = bt_msg->rsp.cmd;
371 data[4] = bt_msg->rsp.cc;
372 if (bt_msg->rsp.data_len > sizeof(data) - 5) {
Joel Stanleyf84329e2015-11-23 19:48:43 +1030373 MSG_ERR("Response message size (%zu) too big, truncating\n", bt_msg->rsp.data_len);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100374 bt_msg->rsp.data_len = sizeof(data) - 5;
375 }
376 /* netfn/len + seq + cmd + cc = 4 */
377 data[0] = bt_msg->rsp.data_len + 4;
378 if (bt_msg->rsp.data_len)
379 memcpy(data + 5, bt_msg->rsp.data, bt_msg->rsp.data_len);
380 /* Count the data[0] byte */
381 len = write(context->fds[BT_FD].fd, data, data[0] + 1);
Joel Stanleyb92a9332015-11-23 20:02:30 +1030382 if (len == -1) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100383 r = errno;
384 MSG_ERR("Error writing to %s: %s\n", BT_HOST_PATH, strerror(errno));
385 if (bt_msg->call) {
386 r = sd_bus_message_new_method_errno(bt_msg->call, &msg, r, NULL);
387 if (r < 0)
388 MSG_ERR("Couldn't create response error\n");
389 }
390 goto out;
391 } else {
392 if (len != data[0] + 1)
393 MSG_ERR("Possible short write to %s, desired len: %d, written len: %d\n", BT_HOST_PATH, data[0] + 1, len);
394 else
395 MSG_OUT("Successfully wrote %d of %d bytes to %s\n", len, data[0] + 1, BT_HOST_PATH);
396
397 if (bt_msg->call) {
398 r = sd_bus_message_new_method_return(bt_msg->call, &msg);
399 if (r < 0) {
400 MSG_ERR("Couldn't create response message\n");
401 goto out;
402 }
403 r = 0; /* Just to be explicit about what we're sending back */
404 r = sd_bus_message_append(msg, "x", r);
405 if (r < 0) {
406 MSG_ERR("Couldn't append result to response\n");
407 goto out;
408 }
409
410 }
411 }
412
413out:
414 if (bt_msg->call) {
415 if (sd_bus_send(context->bus, msg, NULL) < 0)
416 MSG_ERR("Couldn't send response message\n");
417 sd_bus_message_unref(msg);
418 }
419 bt_q_drop(context, bt_msg);
420
421 /*
422 * There isn't another message ready to be sent so turn off POLLOUT
423 */
424 if (!bt_q_get_msg(context)) {
425 MSG_OUT("Turning off POLLOUT for the BT in poll()\n");
426 context->fds[BT_FD].events = POLLIN;
427 }
428
429 return r;
430}
431
432static int dispatch_timer(struct btbridged_context *context)
433{
434 int r = 0;
435 if (context->fds[TIMER_FD].revents & POLLIN) {
436 sd_bus_message *msg;
437 struct bt_queue *head;
438
439 head = bt_q_get_head(context);
440 if (!head) {
441 /* Odd, timer expired but we didn't have a message to timeout */
442 MSG_ERR("No message found to send timeout\n");
443 return 0;
444 }
445 if (head->call) {
446 r = sd_bus_message_new_method_errno(head->call, &msg, ETIMEDOUT, NULL);
447 if (r < 0) {
448 MSG_ERR("Couldn't create response error\n");
449 } else {
450 if (sd_bus_send(context->bus, msg, NULL) < 0)
451 MSG_ERR("Couldn't send response message\n");
452 sd_bus_message_unref(msg);
453 }
454 MSG_ERR("Message with seq 0x%02x is being timed out despite "
455 "appearing to have been responded to. Slow BT?\n", head->rsp.seq);
456 }
457
458 /* Set expiry */
459 head->expired = 1;
460 head->rsp.seq = head->req.seq;
461 head->rsp.netfn = head->req.netfn + 1;
462 head->rsp.lun = head->req.lun;
463 head->rsp.cc = 0xce; /* Command response could not be provided */
464 head->rsp.cmd = head->req.cmd;
465 /* These should already be zeroed but best to be sure */
466 head->rsp.data_len = 0;
467 head->rsp.data = NULL;
468 head->call = NULL;
469 MSG_OUT("Timeout on msg with seq: 0x%02x\n", head->rsp.seq);
470 /* Turn on POLLOUT so we'll write this one next */
471 context->fds[BT_FD].events |= POLLOUT;
472 }
473
474 return 0;
475}
476
477static int dispatch_sd_bus(struct btbridged_context *context)
478{
479 int r = 0;
480 if (context->fds[SD_BUS_FD].revents) {
481 r = sd_bus_process(context->bus, NULL);
482 if (r > 0)
483 MSG_OUT("Processed %d dbus events\n", r);
484 }
485
486 return r;
487}
488
489static int dispatch_bt(struct btbridged_context *context)
490{
491 int err = 0;
492 int r;
493
494 assert(context);
495
496 if (context->fds[BT_FD].revents & POLLIN) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100497 sd_bus_message *msg;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100498 struct bt_queue *new;
499 uint8_t data[BT_MAX_MESSAGE] = { 0 };
500
501 r = read(context->fds[BT_FD].fd, data, sizeof(data));
502 if (r < 0) {
503 MSG_ERR("Couldn't read from bt: %s\n", strerror(-r));
504 goto out1;
505 }
506 if (r < data[0] + 1) {
Cyril Bur8d057982015-10-30 18:48:08 +1100507 MSG_ERR("Short read from bt (%d vs %d)\n", r, data[0] + 1);
508 r = 0;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100509 goto out1;
510 }
511
Cyril Bur1aa096a2015-10-28 15:24:52 +1100512 new = bt_q_enqueue(context, data);
513 if (!new) {
514 r = -ENOMEM;
515 goto out1;
516 }
517 if (new == bt_q_get_head(context)) {
518 struct itimerspec ts;
519 /*
520 * Enqueued onto an empty list, setup a timer for sending a
521 * timeout
522 */
523 ts.it_interval.tv_sec = 0;
524 ts.it_interval.tv_nsec = 0;
525 ts.it_value.tv_nsec = 0;
526 ts.it_value.tv_sec = BT_HOST_TIMEOUT_SEC;
527 r = timerfd_settime(context->fds[TIMER_FD].fd, 0, &ts, NULL);
528 if (r == -1)
529 MSG_ERR("Couldn't set timerfd\n");
530 }
531
Cyril Burfbff9f52015-10-30 18:44:06 +1100532 r = sd_bus_message_new_signal(context->bus, &msg, OBJ_NAME, DBUS_NAME, "ReceivedMessage");
Cyril Bur1aa096a2015-10-28 15:24:52 +1100533 if (r < 0) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100534 MSG_ERR("Failed to create signal: %s\n", strerror(-r));
Cyril Bur1aa096a2015-10-28 15:24:52 +1100535 goto out1;
536 }
Cyril Burfbff9f52015-10-30 18:44:06 +1100537
538 r = sd_bus_message_append(msg, "yyyy", new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
539 if (r < 0) {
540 MSG_ERR("Couldn't append to signal: %s\n", strerror(-r));
541 goto out1_free;
542 }
543
544 r = sd_bus_message_append_array(msg, 'y', data + 4, new->req.data_len);
545 if (r < 0) {
Joel Stanleyf84329e2015-11-23 19:48:43 +1030546 MSG_ERR("Couldn't append array to signal: %s\n", strerror(-r));
Cyril Burfbff9f52015-10-30 18:44:06 +1100547 goto out1_free;
548 }
549
550 MSG_OUT("Sending dbus signal with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x\n",
551 new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
552
Cyril Bur82eafec2015-10-30 19:19:11 +1100553 if (debug) {
Cyril Burfbff9f52015-10-30 18:44:06 +1100554 int i;
Cyril Bur82eafec2015-10-30 19:19:11 +1100555 /* If debug is on, so will verbose */
Cyril Burfbff9f52015-10-30 18:44:06 +1100556 for (i = 0; i < new->req.data_len; i++) {
Cyril Bur82eafec2015-10-30 19:19:11 +1100557 if (i % 8 == 0) {
558 if (i)
559 printf("\n");
560 MSG_OUT("\t");
561 }
562 printf("0x%02x ", data[i + 4]);
Cyril Burfbff9f52015-10-30 18:44:06 +1100563 }
Cyril Bur82eafec2015-10-30 19:19:11 +1100564 if (new->req.data_len)
565 printf("\n");
Cyril Burfbff9f52015-10-30 18:44:06 +1100566 }
567
568 /* Note we only actually keep the request data in the queue when debugging */
569 r = sd_bus_send(context->bus, msg, NULL);
570 if (r < 0) {
571 MSG_ERR("Couldn't emit dbus signal: %s\n", strerror(-r));
572 goto out1_free;
573 }
Cyril Bur1aa096a2015-10-28 15:24:52 +1100574 r = 0;
Cyril Burfbff9f52015-10-30 18:44:06 +1100575out1_free:
576 sd_bus_message_unref(msg);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100577out1:
578 err = r;
579 }
580
581 if (context->fds[BT_FD].revents & POLLOUT) {
582 struct bt_queue *bt_msg;
583 bt_msg = bt_q_get_msg(context);
584 if (!bt_msg) {
585 /* Odd, this shouldn't really happen */
Joel Stanleyf84329e2015-11-23 19:48:43 +1030586 MSG_ERR("Got a POLLOUT but no message is ready to be written\n");
Cyril Bur1aa096a2015-10-28 15:24:52 +1100587 r = 0;
588 goto out;
589 }
590 r = bt_host_write(context, bt_msg);
591 if (r < 0)
Cyril Bur99d545d2015-10-30 14:50:10 +1100592 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 +1030593 "out to %s", bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun,
594 bt_msg->rsp.cmd, bt_msg->rsp.cc, BT_HOST_PATH);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100595 else
Cyril Bur99d545d2015-10-30 14:50:10 +1100596 MSG_OUT("Completed request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n",
597 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 +1100598 }
599out:
600 return err ? err : r;
601}
602
603static void usage(const char *name)
604{
Cyril Bur201c8d72015-12-02 16:54:04 +1100605 fprintf(stderr, "Usage %s [ --debug | --verbose | --syslog ]\n", name);
Cyril Burdca92572015-12-09 12:03:28 +1100606 fprintf(stderr, "\t--debug\t Implies --verbose\n\t Dumps entire message contents to console\n");
Cyril Bur1aa096a2015-10-28 15:24:52 +1100607 fprintf(stderr, "\t--verbose\t Be verbose\n\n");
Cyril Bur201c8d72015-12-02 16:54:04 +1100608 fprintf(stderr, "\t--syslog\t Log output to syslog (pointless without --verbose)\n");
Cyril Bur1aa096a2015-10-28 15:24:52 +1100609}
610
611static const sd_bus_vtable ipmid_vtable[] = {
612 SD_BUS_VTABLE_START(0),
613 SD_BUS_METHOD("sendMessage", "yyyyyay", "x", &method_send_message, SD_BUS_VTABLE_UNPRIVILEGED),
614 SD_BUS_METHOD("setAttention", "", "x", &method_send_sms_atn, SD_BUS_VTABLE_UNPRIVILEGED),
Cyril Bur104200b2015-10-30 14:34:58 +1100615 SD_BUS_SIGNAL("ReceivedMessage", "yyyyay", 0),
Cyril Bur1aa096a2015-10-28 15:24:52 +1100616 SD_BUS_VTABLE_END
617};
618
619int main(int argc, char *argv[]) {
Joel Stanley57ce2902015-11-23 21:48:21 +1030620 struct btbridged_context *context;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100621 const char *name = argv[0];
Joel Stanleya7204212015-11-23 19:47:10 +1030622 int opt, polled, r;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100623
Joel Stanley57ce2902015-11-23 21:48:21 +1030624 static const struct option long_options[] = {
Cyril Bur201c8d72015-12-02 16:54:04 +1100625 { "debug", no_argument, &debug, 1 },
626 { "verbose", no_argument, &verbose, 1 },
627 { "syslog", no_argument, 0, 's' },
628 { 0, 0, 0, 0 }
Cyril Bur1aa096a2015-10-28 15:24:52 +1100629 };
630
Joel Stanley57ce2902015-11-23 21:48:21 +1030631 context = calloc(1, sizeof(*context));
Cyril Bur1aa096a2015-10-28 15:24:52 +1100632
Cyril Bur201c8d72015-12-02 16:54:04 +1100633 bt_vlog = &bt_log_console;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100634 while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
635 switch (opt) {
636 case 0:
Cyril Bur1aa096a2015-10-28 15:24:52 +1100637 break;
Cyril Bur201c8d72015-12-02 16:54:04 +1100638 case 's':
639 /* Avoid a double openlog() */
640 if (bt_vlog != &vsyslog) {
641 openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
642 bt_vlog = &vsyslog;
643 }
644 break;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100645 default:
646 usage(name);
647 exit(EXIT_FAILURE);
648 }
649 }
650
Cyril Bur82eafec2015-10-30 19:19:11 +1100651 if (verbose)
652 MSG_OUT("Found verbosity flag\n");
653
654 if (debug) {
Cyril Burcc569dc2015-12-09 12:05:05 +1100655 if (!verbose) {
656 verbose = 1;
Cyril Bur82eafec2015-10-30 19:19:11 +1100657 MSG_OUT("Turning on verbosity because debug flag found\n");
Cyril Burcc569dc2015-12-09 12:05:05 +1100658 } else {
Cyril Bur82eafec2015-10-30 19:19:11 +1100659 MSG_OUT("Found debug flag\n");
Cyril Burcc569dc2015-12-09 12:05:05 +1100660 }
Cyril Bur82eafec2015-10-30 19:19:11 +1100661 }
662
Cyril Bur1aa096a2015-10-28 15:24:52 +1100663 MSG_OUT("Starting\n");
Joel Stanley57ce2902015-11-23 21:48:21 +1030664 r = sd_bus_default_system(&context->bus);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100665 if (r < 0) {
666 MSG_ERR("Failed to connect to system bus: %s\n", strerror(-r));
667 goto finish;
668 }
669
670 MSG_OUT("Registering dbus methods/signals\n");
Joel Stanley57ce2902015-11-23 21:48:21 +1030671 r = sd_bus_add_object_vtable(context->bus,
Cyril Bur1aa096a2015-10-28 15:24:52 +1100672 NULL,
673 OBJ_NAME,
674 DBUS_NAME,
675 ipmid_vtable,
Joel Stanley57ce2902015-11-23 21:48:21 +1030676 context);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100677 if (r < 0) {
678 MSG_ERR("Failed to issue method call: %s\n", strerror(-r));
679 goto finish;
680 }
681
682 MSG_OUT("Requesting dbus name: %s\n", DBUS_NAME);
Joel Stanley57ce2902015-11-23 21:48:21 +1030683 r = sd_bus_request_name(context->bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100684 if (r < 0) {
685 MSG_ERR("Failed to acquire service name: %s\n", strerror(-r));
686 goto finish;
687 }
688
689 MSG_OUT("Getting dbus file descriptors\n");
Joel Stanley57ce2902015-11-23 21:48:21 +1030690 context->fds[SD_BUS_FD].fd = sd_bus_get_fd(context->bus);
691 if (context->fds[SD_BUS_FD].fd < 0) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100692 r = -errno;
693 MSG_OUT("Couldn't get the bus file descriptor: %s\n", strerror(errno));
694 goto finish;
695 }
696
697 MSG_OUT("Opening %s\n", BT_HOST_PATH);
Joel Stanley57ce2902015-11-23 21:48:21 +1030698 context->fds[BT_FD].fd = open(BT_HOST_PATH, O_RDWR | O_NONBLOCK);
699 if (context->fds[BT_FD].fd < 0) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100700 r = -errno;
701 MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", BT_HOST_PATH, strerror(errno));
702 goto finish;
703 }
704
705 MSG_OUT("Creating timer fd\n");
Joel Stanley57ce2902015-11-23 21:48:21 +1030706 context->fds[TIMER_FD].fd = timerfd_create(CLOCK_MONOTONIC, 0);
707 if (context->fds[TIMER_FD].fd < 0) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100708 r = -errno;
709 MSG_ERR("Couldn't create timer fd: %s\n", strerror(errno));
710 goto finish;
711 }
Joel Stanley57ce2902015-11-23 21:48:21 +1030712 context->fds[SD_BUS_FD].events = POLLIN;
713 context->fds[BT_FD].events = POLLIN;
714 context->fds[TIMER_FD].events = POLLIN;
Cyril Bur1aa096a2015-10-28 15:24:52 +1100715
716 MSG_OUT("Entering polling loop\n");
717
718 while (running) {
Joel Stanley57ce2902015-11-23 21:48:21 +1030719 polled = poll(context->fds, TOTAL_FDS, 1000);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100720 if (polled == 0)
721 continue;
722 if (polled < 0) {
723 r = -errno;
724 MSG_ERR("Error from poll(): %s\n", strerror(errno));
725 goto finish;
726 }
Joel Stanley57ce2902015-11-23 21:48:21 +1030727 r = dispatch_sd_bus(context);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100728 if (r < 0) {
729 MSG_ERR("Error handling dbus event: %s\n", strerror(-r));
730 goto finish;
731 }
Joel Stanley57ce2902015-11-23 21:48:21 +1030732 r = dispatch_bt(context);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100733 if (r < 0) {
734 MSG_ERR("Error handling BT event: %s\n", strerror(-r));
735 goto finish;
736 }
Joel Stanley57ce2902015-11-23 21:48:21 +1030737 r = dispatch_timer(context);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100738 if (r < 0) {
739 MSG_ERR("Error handling timer event: %s\n", strerror(-r));
740 goto finish;
741 }
742 }
743
744finish:
Joel Stanley57ce2902015-11-23 21:48:21 +1030745 if (bt_q_get_head(context)) {
Cyril Bur1aa096a2015-10-28 15:24:52 +1100746 MSG_ERR("Unresponded BT Message!\n");
Joel Stanley57ce2902015-11-23 21:48:21 +1030747 while (bt_q_dequeue(context));
Cyril Bur1aa096a2015-10-28 15:24:52 +1100748 }
Joel Stanley57ce2902015-11-23 21:48:21 +1030749 close(context->fds[BT_FD].fd);
750 close(context->fds[TIMER_FD].fd);
751 sd_bus_unref(context->bus);
752 free(context);
Cyril Bur1aa096a2015-10-28 15:24:52 +1100753
754 return r;
755}
756