Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 1 | /* 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 Stanley | da6e94c | 2015-11-23 19:27:34 +1030 | [diff] [blame] | 16 | #include <assert.h> |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 17 | #include <errno.h> |
Joel Stanley | da6e94c | 2015-11-23 19:27:34 +1030 | [diff] [blame] | 18 | #include <fcntl.h> |
| 19 | #include <getopt.h> |
| 20 | #include <limits.h> |
| 21 | #include <poll.h> |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 22 | #include <stdint.h> |
| 23 | #include <stdio.h> |
| 24 | #include <stdlib.h> |
| 25 | #include <string.h> |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 26 | #include <syslog.h> |
Joel Stanley | da6e94c | 2015-11-23 19:27:34 +1030 | [diff] [blame] | 27 | #include <sys/mman.h> |
Joel Stanley | fbade63 | 2015-11-23 19:46:12 +1030 | [diff] [blame] | 28 | #include <sys/ioctl.h> |
Joel Stanley | da6e94c | 2015-11-23 19:27:34 +1030 | [diff] [blame] | 29 | #include <sys/stat.h> |
| 30 | #include <sys/timerfd.h> |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 31 | #include <time.h> |
Joel Stanley | da6e94c | 2015-11-23 19:27:34 +1030 | [diff] [blame] | 32 | #include <unistd.h> |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 33 | |
| 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 | |
| 60 | struct 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 | |
| 70 | struct 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 Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 79 | struct btbridged_context { |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 80 | struct pollfd fds[TOTAL_FDS]; |
| 81 | struct sd_bus *bus; |
| 82 | struct bt_queue *bt_q; |
| 83 | }; |
| 84 | |
| 85 | static int running = 1; |
| 86 | static int verbose; |
Cyril Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 87 | static int debug; |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 88 | |
| 89 | static struct bt_queue *bt_q_get_head(struct btbridged_context *context) |
| 90 | { |
| 91 | return context ? context->bt_q : NULL; |
| 92 | } |
| 93 | |
| 94 | static 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 | |
| 108 | static 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 | |
| 122 | static 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 Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 146 | if (debug) { |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 147 | 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 | |
| 176 | static 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 | |
| 189 | static 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 | |
| 220 | static 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 | |
| 240 | static int method_send_sms_atn(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) |
| 241 | { |
Chris Austen | c039568 | 2015-11-20 17:12:49 -0600 | [diff] [blame] | 242 | int r; |
| 243 | struct btbridged_context *bt_fd = (struct btbridged_context *)userdata; |
| 244 | |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 245 | MSG_OUT("Sending SMS_ATN ioctl() to %s\n", BT_HOST_PATH); |
Chris Austen | c039568 | 2015-11-20 17:12:49 -0600 | [diff] [blame] | 246 | |
| 247 | |
| 248 | r = ioctl(bt_fd->fds[BT_FD].fd, BT_HOST_IOCTL_SMS_ATN); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 249 | if (r == -1) { |
| 250 | r = errno; |
Chris Austen | c039568 | 2015-11-20 17:12:49 -0600 | [diff] [blame] | 251 | MSG_ERR("Couldn't ioctl() to 0x%x, %s: %s\n", bt_fd->fds[BT_FD].fd, BT_HOST_PATH, strerror(r)); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 252 | return sd_bus_reply_method_errno(msg, errno, ret_error); |
| 253 | } |
| 254 | |
| 255 | r = 0; |
| 256 | return sd_bus_reply_method_return(msg, "x", r); |
| 257 | } |
| 258 | |
| 259 | static int method_send_message(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error) |
| 260 | { |
| 261 | struct btbridged_context *context; |
| 262 | struct bt_queue *bt_msg; |
Joel Stanley | a720421 | 2015-11-23 19:47:10 +1030 | [diff] [blame] | 263 | uint8_t *data; |
| 264 | size_t data_sz; |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 265 | uint8_t netfn, lun, seq, cmd, cc; |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 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 Bur | 99d545d | 2015-10-30 14:50:10 +1100 | [diff] [blame] | 278 | r = sd_bus_message_read(msg, "yyyyy", &seq, &netfn, &lun, &cmd, &cc); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 279 | 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 Bur | 104200b | 2015-10-30 14:34:58 +1100 | [diff] [blame] | 300 | MSG_OUT("Received a dbus response for msg with seq 0x%02x\n", seq); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 301 | 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 | |
| 314 | out: |
| 315 | return r; |
| 316 | } |
| 317 | |
| 318 | static 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) { |
Joel Stanley | f84329e | 2015-11-23 19:48:43 +1030 | [diff] [blame^] | 354 | MSG_ERR("Response message size (%zu) too big, truncating\n", bt_msg->rsp.data_len); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 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 | |
| 394 | out: |
| 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 | |
| 413 | static 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 | |
| 458 | static 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 | |
| 470 | static 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 Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 478 | sd_bus_message *msg; |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 479 | struct bt_queue *new; |
| 480 | uint8_t data[BT_MAX_MESSAGE] = { 0 }; |
| 481 | |
| 482 | r = read(context->fds[BT_FD].fd, data, sizeof(data)); |
| 483 | if (r < 0) { |
| 484 | MSG_ERR("Couldn't read from bt: %s\n", strerror(-r)); |
| 485 | goto out1; |
| 486 | } |
| 487 | if (r < data[0] + 1) { |
Cyril Bur | 8d05798 | 2015-10-30 18:48:08 +1100 | [diff] [blame] | 488 | MSG_ERR("Short read from bt (%d vs %d)\n", r, data[0] + 1); |
| 489 | r = 0; |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 490 | goto out1; |
| 491 | } |
| 492 | |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 493 | new = bt_q_enqueue(context, data); |
| 494 | if (!new) { |
| 495 | r = -ENOMEM; |
| 496 | goto out1; |
| 497 | } |
| 498 | if (new == bt_q_get_head(context)) { |
| 499 | struct itimerspec ts; |
| 500 | /* |
| 501 | * Enqueued onto an empty list, setup a timer for sending a |
| 502 | * timeout |
| 503 | */ |
| 504 | ts.it_interval.tv_sec = 0; |
| 505 | ts.it_interval.tv_nsec = 0; |
| 506 | ts.it_value.tv_nsec = 0; |
| 507 | ts.it_value.tv_sec = BT_HOST_TIMEOUT_SEC; |
| 508 | r = timerfd_settime(context->fds[TIMER_FD].fd, 0, &ts, NULL); |
| 509 | if (r == -1) |
| 510 | MSG_ERR("Couldn't set timerfd\n"); |
| 511 | } |
| 512 | |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 513 | r = sd_bus_message_new_signal(context->bus, &msg, OBJ_NAME, DBUS_NAME, "ReceivedMessage"); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 514 | if (r < 0) { |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 515 | MSG_ERR("Failed to create signal: %s\n", strerror(-r)); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 516 | goto out1; |
| 517 | } |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 518 | |
| 519 | r = sd_bus_message_append(msg, "yyyy", new->req.seq, new->req.netfn, new->req.lun, new->req.cmd); |
| 520 | if (r < 0) { |
| 521 | MSG_ERR("Couldn't append to signal: %s\n", strerror(-r)); |
| 522 | goto out1_free; |
| 523 | } |
| 524 | |
| 525 | r = sd_bus_message_append_array(msg, 'y', data + 4, new->req.data_len); |
| 526 | if (r < 0) { |
Joel Stanley | f84329e | 2015-11-23 19:48:43 +1030 | [diff] [blame^] | 527 | MSG_ERR("Couldn't append array to signal: %s\n", strerror(-r)); |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 528 | goto out1_free; |
| 529 | } |
| 530 | |
| 531 | MSG_OUT("Sending dbus signal with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x\n", |
| 532 | new->req.seq, new->req.netfn, new->req.lun, new->req.cmd); |
| 533 | |
Cyril Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 534 | if (debug) { |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 535 | int i; |
Cyril Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 536 | /* If debug is on, so will verbose */ |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 537 | for (i = 0; i < new->req.data_len; i++) { |
Cyril Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 538 | if (i % 8 == 0) { |
| 539 | if (i) |
| 540 | printf("\n"); |
| 541 | MSG_OUT("\t"); |
| 542 | } |
| 543 | printf("0x%02x ", data[i + 4]); |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 544 | } |
Cyril Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 545 | if (new->req.data_len) |
| 546 | printf("\n"); |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | /* Note we only actually keep the request data in the queue when debugging */ |
| 550 | r = sd_bus_send(context->bus, msg, NULL); |
| 551 | if (r < 0) { |
| 552 | MSG_ERR("Couldn't emit dbus signal: %s\n", strerror(-r)); |
| 553 | goto out1_free; |
| 554 | } |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 555 | r = 0; |
Cyril Bur | fbff9f5 | 2015-10-30 18:44:06 +1100 | [diff] [blame] | 556 | out1_free: |
| 557 | sd_bus_message_unref(msg); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 558 | out1: |
| 559 | err = r; |
| 560 | } |
| 561 | |
| 562 | if (context->fds[BT_FD].revents & POLLOUT) { |
| 563 | struct bt_queue *bt_msg; |
| 564 | bt_msg = bt_q_get_msg(context); |
| 565 | if (!bt_msg) { |
| 566 | /* Odd, this shouldn't really happen */ |
Joel Stanley | f84329e | 2015-11-23 19:48:43 +1030 | [diff] [blame^] | 567 | MSG_ERR("Got a POLLOUT but no message is ready to be written\n"); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 568 | r = 0; |
| 569 | goto out; |
| 570 | } |
| 571 | r = bt_host_write(context, bt_msg); |
| 572 | if (r < 0) |
Cyril Bur | 99d545d | 2015-10-30 14:50:10 +1100 | [diff] [blame] | 573 | MSG_ERR("Problem putting request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n" |
Joel Stanley | f84329e | 2015-11-23 19:48:43 +1030 | [diff] [blame^] | 574 | "out to %s", bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun, |
| 575 | bt_msg->rsp.cmd, bt_msg->rsp.cc, BT_HOST_PATH); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 576 | else |
Cyril Bur | 99d545d | 2015-10-30 14:50:10 +1100 | [diff] [blame] | 577 | MSG_OUT("Completed request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n", |
| 578 | bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun, bt_msg->rsp.cmd, bt_msg->rsp.cc); |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 579 | } |
| 580 | out: |
| 581 | return err ? err : r; |
| 582 | } |
| 583 | |
| 584 | static void usage(const char *name) |
| 585 | { |
| 586 | fprintf(stderr, "Usage %s\n", name); |
| 587 | fprintf(stderr, "\t--verbose\t Be verbose\n\n"); |
| 588 | } |
| 589 | |
| 590 | static const sd_bus_vtable ipmid_vtable[] = { |
| 591 | SD_BUS_VTABLE_START(0), |
| 592 | SD_BUS_METHOD("sendMessage", "yyyyyay", "x", &method_send_message, SD_BUS_VTABLE_UNPRIVILEGED), |
| 593 | SD_BUS_METHOD("setAttention", "", "x", &method_send_sms_atn, SD_BUS_VTABLE_UNPRIVILEGED), |
Cyril Bur | 104200b | 2015-10-30 14:34:58 +1100 | [diff] [blame] | 594 | SD_BUS_SIGNAL("ReceivedMessage", "yyyyay", 0), |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 595 | SD_BUS_VTABLE_END |
| 596 | }; |
| 597 | |
| 598 | int main(int argc, char *argv[]) { |
| 599 | struct btbridged_context context; |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 600 | const char *name = argv[0]; |
Joel Stanley | a720421 | 2015-11-23 19:47:10 +1030 | [diff] [blame] | 601 | int opt, polled, r; |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 602 | |
| 603 | static struct option long_options[] = { |
Cyril Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 604 | { "verbose", no_argument, &verbose, 1 }, |
| 605 | { "debug", no_argument, &debug, 1 }, |
| 606 | { 0, 0, 0, 0 } |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 607 | }; |
| 608 | |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 609 | context.bt_q = NULL; |
| 610 | |
| 611 | while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) { |
| 612 | switch (opt) { |
| 613 | case 0: |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 614 | break; |
| 615 | default: |
| 616 | usage(name); |
| 617 | exit(EXIT_FAILURE); |
| 618 | } |
| 619 | } |
| 620 | |
Cyril Bur | 82eafec | 2015-10-30 19:19:11 +1100 | [diff] [blame] | 621 | if (verbose) |
| 622 | MSG_OUT("Found verbosity flag\n"); |
| 623 | |
| 624 | if (debug) { |
| 625 | if (!verbose) |
| 626 | MSG_OUT("Turning on verbosity because debug flag found\n"); |
| 627 | else |
| 628 | MSG_OUT("Found debug flag\n"); |
| 629 | } |
| 630 | |
Cyril Bur | 1aa096a | 2015-10-28 15:24:52 +1100 | [diff] [blame] | 631 | MSG_OUT("Starting\n"); |
| 632 | r = sd_bus_default_system(&context.bus); |
| 633 | if (r < 0) { |
| 634 | MSG_ERR("Failed to connect to system bus: %s\n", strerror(-r)); |
| 635 | goto finish; |
| 636 | } |
| 637 | |
| 638 | MSG_OUT("Registering dbus methods/signals\n"); |
| 639 | r = sd_bus_add_object_vtable(context.bus, |
| 640 | NULL, |
| 641 | OBJ_NAME, |
| 642 | DBUS_NAME, |
| 643 | ipmid_vtable, |
| 644 | &context); |
| 645 | if (r < 0) { |
| 646 | MSG_ERR("Failed to issue method call: %s\n", strerror(-r)); |
| 647 | goto finish; |
| 648 | } |
| 649 | |
| 650 | MSG_OUT("Requesting dbus name: %s\n", DBUS_NAME); |
| 651 | r = sd_bus_request_name(context.bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING); |
| 652 | if (r < 0) { |
| 653 | MSG_ERR("Failed to acquire service name: %s\n", strerror(-r)); |
| 654 | goto finish; |
| 655 | } |
| 656 | |
| 657 | MSG_OUT("Getting dbus file descriptors\n"); |
| 658 | context.fds[SD_BUS_FD].fd = sd_bus_get_fd(context.bus); |
| 659 | if (context.fds[SD_BUS_FD].fd < 0) { |
| 660 | r = -errno; |
| 661 | MSG_OUT("Couldn't get the bus file descriptor: %s\n", strerror(errno)); |
| 662 | goto finish; |
| 663 | } |
| 664 | |
| 665 | MSG_OUT("Opening %s\n", BT_HOST_PATH); |
| 666 | context.fds[BT_FD].fd = open(BT_HOST_PATH, O_RDWR | O_NONBLOCK); |
| 667 | if (context.fds[BT_FD].fd < 0) { |
| 668 | r = -errno; |
| 669 | MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", BT_HOST_PATH, strerror(errno)); |
| 670 | goto finish; |
| 671 | } |
| 672 | |
| 673 | MSG_OUT("Creating timer fd\n"); |
| 674 | context.fds[TIMER_FD].fd = timerfd_create(CLOCK_MONOTONIC, 0); |
| 675 | if (context.fds[TIMER_FD].fd < 0) { |
| 676 | r = -errno; |
| 677 | MSG_ERR("Couldn't create timer fd: %s\n", strerror(errno)); |
| 678 | goto finish; |
| 679 | } |
| 680 | context.fds[SD_BUS_FD].events = POLLIN; |
| 681 | context.fds[BT_FD].events = POLLIN; |
| 682 | context.fds[TIMER_FD].events = POLLIN; |
| 683 | |
| 684 | MSG_OUT("Entering polling loop\n"); |
| 685 | |
| 686 | while (running) { |
| 687 | polled = poll(context.fds, TOTAL_FDS, 1000); |
| 688 | if (polled == 0) |
| 689 | continue; |
| 690 | if (polled < 0) { |
| 691 | r = -errno; |
| 692 | MSG_ERR("Error from poll(): %s\n", strerror(errno)); |
| 693 | goto finish; |
| 694 | } |
| 695 | r = dispatch_sd_bus(&context); |
| 696 | if (r < 0) { |
| 697 | MSG_ERR("Error handling dbus event: %s\n", strerror(-r)); |
| 698 | goto finish; |
| 699 | } |
| 700 | r = dispatch_bt(&context); |
| 701 | if (r < 0) { |
| 702 | MSG_ERR("Error handling BT event: %s\n", strerror(-r)); |
| 703 | goto finish; |
| 704 | } |
| 705 | r = dispatch_timer(&context); |
| 706 | if (r < 0) { |
| 707 | MSG_ERR("Error handling timer event: %s\n", strerror(-r)); |
| 708 | goto finish; |
| 709 | } |
| 710 | } |
| 711 | |
| 712 | finish: |
| 713 | if (bt_q_get_head(&context)) { |
| 714 | MSG_ERR("Unresponded BT Message!\n"); |
| 715 | while (bt_q_dequeue(&context)); |
| 716 | } |
| 717 | close(context.fds[BT_FD].fd); |
| 718 | close(context.fds[TIMER_FD].fd); |
| 719 | sd_bus_unref(context.bus); |
| 720 | |
| 721 | return r; |
| 722 | } |
| 723 | |