xref: /openbmc/btbridge/btbridged.c (revision 8ee19a4d70f7fd3aeb617728e3e65d3745ff281c)
11aa096a9SCyril Bur /* Copyright 2015 IBM
21aa096a9SCyril Bur  *
31aa096a9SCyril Bur  * Licensed under the Apache License, Version 2.0 (the "License");
41aa096a9SCyril Bur  * you may not use this file except in compliance with the License.
51aa096a9SCyril Bur  * You may obtain a copy of the License at
61aa096a9SCyril Bur  *
71aa096a9SCyril Bur  *    http://www.apache.org/licenses/LICENSE-2.0
81aa096a9SCyril Bur  *
91aa096a9SCyril Bur  *	Unless required by applicable law or agreed to in writing, software
101aa096a9SCyril Bur  *	distributed under the License is distributed on an "AS IS" BASIS,
111aa096a9SCyril Bur  *	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121aa096a9SCyril Bur  *	See the License for the specific language governing permissions and
131aa096a9SCyril Bur  *	limitations under the License.
141aa096a9SCyril Bur  */
151aa096a9SCyril Bur 
16da6e94cfSJoel Stanley #include <assert.h>
171aa096a9SCyril Bur #include <errno.h>
18da6e94cfSJoel Stanley #include <fcntl.h>
19da6e94cfSJoel Stanley #include <getopt.h>
20aa5511d2SAndrew Jeffery #include <inttypes.h>
21da6e94cfSJoel Stanley #include <limits.h>
22da6e94cfSJoel Stanley #include <poll.h>
231aa096a9SCyril Bur #include <stdint.h>
241aa096a9SCyril Bur #include <stdio.h>
251aa096a9SCyril Bur #include <stdlib.h>
261aa096a9SCyril Bur #include <string.h>
271aa096a9SCyril Bur #include <syslog.h>
28da6e94cfSJoel Stanley #include <sys/mman.h>
29fbade63dSJoel Stanley #include <sys/ioctl.h>
30da6e94cfSJoel Stanley #include <sys/stat.h>
31da6e94cfSJoel Stanley #include <sys/timerfd.h>
321aa096a9SCyril Bur #include <time.h>
33da6e94cfSJoel Stanley #include <unistd.h>
341aa096a9SCyril Bur 
35825022b4SCédric Le Goater #include <linux/bt-bmc.h>
361aa096a9SCyril Bur 
371aa096a9SCyril Bur #include <systemd/sd-bus.h>
381aa096a9SCyril Bur 
39825022b4SCédric Le Goater static const char *bt_bmc_device = "/dev/ipmi-bt-host";
4074e18137SCédric Le Goater 
41201c8d71SCyril Bur #define PREFIX "BTBRIDGED"
421aa096a9SCyril Bur 
43825022b4SCédric Le Goater #define BT_BMC_PATH bt_bmc_device
44ad247bd3SEmily Shaffer #define BT_BMC_TIMEOUT_SEC 5
451aa096a9SCyril Bur #define BT_MAX_MESSAGE 64
461aa096a9SCyril Bur 
471aa096a9SCyril Bur #define DBUS_NAME "org.openbmc.HostIpmi"
481aa096a9SCyril Bur #define OBJ_NAME "/org/openbmc/HostIpmi/1"
491aa096a9SCyril Bur 
501aa096a9SCyril Bur #define SD_BUS_FD 0
511aa096a9SCyril Bur #define BT_FD 1
521aa096a9SCyril Bur #define TIMER_FD 2
531aa096a9SCyril Bur #define TOTAL_FDS 3
541aa096a9SCyril Bur 
55a28444b6SCyril Bur #define MSG_OUT(f_, ...) do { if (verbosity != BT_LOG_NONE) { bt_log(LOG_INFO, f_, ##__VA_ARGS__); } } while(0)
56a28444b6SCyril Bur #define MSG_ERR(f_, ...) do { if (verbosity != BT_LOG_NONE) { bt_log(LOG_ERR, f_, ##__VA_ARGS__); } } while(0)
571aa096a9SCyril Bur 
581aa096a9SCyril Bur struct ipmi_msg {
591aa096a9SCyril Bur 	uint8_t netfn;
601aa096a9SCyril Bur 	uint8_t lun;
611aa096a9SCyril Bur 	uint8_t seq;
621aa096a9SCyril Bur 	uint8_t cmd;
631aa096a9SCyril Bur 	uint8_t cc; /* Only used on responses */
641aa096a9SCyril Bur 	uint8_t *data;
651aa096a9SCyril Bur 	size_t data_len;
661aa096a9SCyril Bur };
671aa096a9SCyril Bur 
681aa096a9SCyril Bur struct bt_queue {
691aa096a9SCyril Bur 	struct ipmi_msg req;
701aa096a9SCyril Bur 	struct ipmi_msg rsp;
711aa096a9SCyril Bur 	struct timespec start;
721aa096a9SCyril Bur 	int expired;
731aa096a9SCyril Bur 	sd_bus_message *call;
741aa096a9SCyril Bur 	struct bt_queue *next;
751aa096a9SCyril Bur };
761aa096a9SCyril Bur 
771aa096a9SCyril Bur struct btbridged_context {
781aa096a9SCyril Bur 	struct pollfd fds[TOTAL_FDS];
791aa096a9SCyril Bur 	struct sd_bus *bus;
801aa096a9SCyril Bur 	struct bt_queue *bt_q;
811aa096a9SCyril Bur };
821aa096a9SCyril Bur 
83201c8d71SCyril Bur static void (*bt_vlog)(int p, const char *fmt, va_list args);
841aa096a9SCyril Bur static int running = 1;
85a28444b6SCyril Bur static enum {
86a28444b6SCyril Bur    BT_LOG_NONE = 0,
87a28444b6SCyril Bur    BT_LOG_VERBOSE,
88a28444b6SCyril Bur    BT_LOG_DEBUG
89a28444b6SCyril Bur } verbosity;
901aa096a9SCyril Bur 
bt_log_console(int p,const char * fmt,va_list args)91201c8d71SCyril Bur static void bt_log_console(int p, const char *fmt, va_list args)
92201c8d71SCyril Bur {
93201c8d71SCyril Bur 	struct timespec time;
94201c8d71SCyril Bur 	FILE *s = (p < LOG_WARNING) ? stdout : stderr;
95201c8d71SCyril Bur 
96201c8d71SCyril Bur 	clock_gettime(CLOCK_REALTIME, &time);
97201c8d71SCyril Bur 
984aa83739SAndrew Geissler 	fprintf(s, "[%s %lld.%.9ld] ", PREFIX, (unsigned long long)time.tv_sec, time.tv_nsec);
99201c8d71SCyril Bur 
100201c8d71SCyril Bur 	vfprintf(s, fmt, args);
101201c8d71SCyril Bur }
102201c8d71SCyril Bur 
103201c8d71SCyril Bur __attribute__((format(printf, 2, 3)))
bt_log(int p,const char * fmt,...)104201c8d71SCyril Bur static void bt_log(int p, const char *fmt, ...)
105201c8d71SCyril Bur {
106201c8d71SCyril Bur 	va_list args;
107201c8d71SCyril Bur 
108201c8d71SCyril Bur 	va_start(args, fmt);
109201c8d71SCyril Bur 	bt_vlog(p, fmt, args);
110201c8d71SCyril Bur 	va_end(args);
111201c8d71SCyril Bur }
112201c8d71SCyril Bur 
bt_q_get_head(struct btbridged_context * context)1131aa096a9SCyril Bur static struct bt_queue *bt_q_get_head(struct btbridged_context *context)
1141aa096a9SCyril Bur {
1151aa096a9SCyril Bur 	return context ? context->bt_q : NULL;
1161aa096a9SCyril Bur }
1171aa096a9SCyril Bur 
bt_q_get_seq(struct btbridged_context * context,uint8_t seq)1181aa096a9SCyril Bur static struct bt_queue *bt_q_get_seq(struct btbridged_context *context, uint8_t seq)
1191aa096a9SCyril Bur {
1201aa096a9SCyril Bur 	struct bt_queue *t;
1211aa096a9SCyril Bur 
1221aa096a9SCyril Bur 	assert(context);
1231aa096a9SCyril Bur 
1241aa096a9SCyril Bur 	t = context->bt_q;
1251aa096a9SCyril Bur 
1261aa096a9SCyril Bur 	while (t && t->req.seq != seq)
1271aa096a9SCyril Bur 		t = t->next;
1281aa096a9SCyril Bur 
1291aa096a9SCyril Bur 	return t;
1301aa096a9SCyril Bur }
1311aa096a9SCyril Bur 
bt_q_get_msg(struct btbridged_context * context)1321aa096a9SCyril Bur static struct bt_queue *bt_q_get_msg(struct btbridged_context *context)
1331aa096a9SCyril Bur {
1341aa096a9SCyril Bur 	struct bt_queue *t;
1351aa096a9SCyril Bur 
1361aa096a9SCyril Bur 	assert(context);
1371aa096a9SCyril Bur 
1381aa096a9SCyril Bur 	t = context->bt_q;
1391aa096a9SCyril Bur 
1401aa096a9SCyril Bur 	while (t && (!t->call && !t->expired))
1411aa096a9SCyril Bur 		t = t->next;
1421aa096a9SCyril Bur 
1431aa096a9SCyril Bur 	return t;
1441aa096a9SCyril Bur }
1451aa096a9SCyril Bur 
bt_q_enqueue(struct btbridged_context * context,uint8_t * bt_data)1461aa096a9SCyril Bur static struct bt_queue *bt_q_enqueue(struct btbridged_context *context, uint8_t *bt_data)
1471aa096a9SCyril Bur {
1481aa096a9SCyril Bur 	struct bt_queue *n;
1491aa096a9SCyril Bur 	struct bt_queue *bt_q;
1501aa096a9SCyril Bur 	int len;
1511aa096a9SCyril Bur 
1521aa096a9SCyril Bur 	assert(context && bt_data);
1531aa096a9SCyril Bur 
1541aa096a9SCyril Bur 	/*
1551aa096a9SCyril Bur 	 * len here is the length of the array.
1561aa096a9SCyril Bur 	 * Helpfully BT doesn't count the length byte
1571aa096a9SCyril Bur 	 */
1581aa096a9SCyril Bur 	len = bt_data[0] + 1;
1591aa096a9SCyril Bur 	if (len < 4) {
1601aa096a9SCyril Bur 		MSG_ERR("Trying to queue a BT message with a short length (%d)\n", len);
1611aa096a9SCyril Bur 		return NULL;
1621aa096a9SCyril Bur 	}
1631aa096a9SCyril Bur 
1641aa096a9SCyril Bur 	bt_q = context->bt_q;
1651aa096a9SCyril Bur 
1661aa096a9SCyril Bur 	n = calloc(1, sizeof(struct bt_queue));
1671aa096a9SCyril Bur 	if (!n)
1681aa096a9SCyril Bur 		return NULL;
1691aa096a9SCyril Bur 
170a28444b6SCyril Bur 	if (verbosity == BT_LOG_DEBUG) {
1711aa096a9SCyril Bur 		n->req.data = malloc(len - 4);
1721aa096a9SCyril Bur 		if (n->req.data)
1731aa096a9SCyril Bur 			n->req.data = memcpy(n->req.data, bt_data + 4, len - 4);
1741aa096a9SCyril Bur 	}
1751aa096a9SCyril Bur 	n->req.data_len = len - 4;
1761aa096a9SCyril Bur 	/* Don't count the lenfn/ln, seq and command */
1771aa096a9SCyril Bur 	n->req.netfn = bt_data[1] >> 2;
1781aa096a9SCyril Bur 	n->req.lun = bt_data[1] & 0x3;
1791aa096a9SCyril Bur 	n->req.seq = bt_data[2];
1801aa096a9SCyril Bur 	n->req.cmd = bt_data[3];
1811aa096a9SCyril Bur 	if (clock_gettime(CLOCK_MONOTONIC, &n->start) == -1) {
1821aa096a9SCyril Bur 		MSG_ERR("Couldn't clock_gettime(): %s\n", strerror(errno));
1831aa096a9SCyril Bur 		free(n);
1841aa096a9SCyril Bur 		return NULL;
1851aa096a9SCyril Bur 	}
1861aa096a9SCyril Bur 	if (!bt_q) {
1871aa096a9SCyril Bur 		context->bt_q = n;
1881aa096a9SCyril Bur 	} else {
1891aa096a9SCyril Bur 		struct bt_queue *t = bt_q;
1901aa096a9SCyril Bur 
1911aa096a9SCyril Bur 		while (t->next)
1921aa096a9SCyril Bur 			t = t->next;
1931aa096a9SCyril Bur 
1941aa096a9SCyril Bur 		t->next = n;
1951aa096a9SCyril Bur 	}
1961aa096a9SCyril Bur 
1971aa096a9SCyril Bur 	return n;
1981aa096a9SCyril Bur }
1991aa096a9SCyril Bur 
bt_q_free(struct bt_queue * bt_q)2001aa096a9SCyril Bur static void bt_q_free(struct bt_queue *bt_q)
2011aa096a9SCyril Bur {
2021aa096a9SCyril Bur 	if (!bt_q)
2031aa096a9SCyril Bur 		return;
2041aa096a9SCyril Bur 
2051aa096a9SCyril Bur 	/* Unrefing sd_bus_message should free(rsp.data) */
2061aa096a9SCyril Bur 	if (bt_q->call)
2071aa096a9SCyril Bur 		sd_bus_message_unref(bt_q->call);
2081aa096a9SCyril Bur 
2091aa096a9SCyril Bur 	free(bt_q->req.data);
2101aa096a9SCyril Bur 	free(bt_q);
2111aa096a9SCyril Bur }
2121aa096a9SCyril Bur 
bt_q_drop(struct btbridged_context * context,struct bt_queue * element)2131aa096a9SCyril Bur static struct bt_queue *bt_q_drop(struct btbridged_context *context, struct bt_queue *element)
2141aa096a9SCyril Bur {
2151aa096a9SCyril Bur 	struct bt_queue *r;
2161aa096a9SCyril Bur 
2171aa096a9SCyril Bur 	assert(context);
2181aa096a9SCyril Bur 
2194c6eaa97SCyril Bur 	if (!element || !context || !context->bt_q)
2201aa096a9SCyril Bur 		return NULL;
2211aa096a9SCyril Bur 
2224c6eaa97SCyril Bur 	if (element == context->bt_q) {
2234c6eaa97SCyril Bur 		context->bt_q = context->bt_q->next;
2241aa096a9SCyril Bur 	} else {
2254c6eaa97SCyril Bur 		r = context->bt_q;
2261aa096a9SCyril Bur 		while (r && r->next != element)
2271aa096a9SCyril Bur 			r = r->next;
2281aa096a9SCyril Bur 
2291aa096a9SCyril Bur 		if (!r) {
230*8ee19a4dSEd Tanous 			MSG_ERR("Didn't find element %p in queue\n", (void*)element);
2311aa096a9SCyril Bur 			bt_q_free(element);
2321aa096a9SCyril Bur 			return NULL;
2331aa096a9SCyril Bur 		}
2341aa096a9SCyril Bur 		r->next = r->next->next;
2351aa096a9SCyril Bur 	}
2361aa096a9SCyril Bur 	bt_q_free(element);
2371aa096a9SCyril Bur 
2381aa096a9SCyril Bur 	return context->bt_q;
2391aa096a9SCyril Bur }
2401aa096a9SCyril Bur 
bt_q_dequeue(struct btbridged_context * context)2411aa096a9SCyril Bur static struct bt_queue *bt_q_dequeue(struct btbridged_context *context)
2421aa096a9SCyril Bur {
2431aa096a9SCyril Bur 	struct bt_queue *r;
2441aa096a9SCyril Bur 	struct bt_queue *bt_q;
2451aa096a9SCyril Bur 
2461aa096a9SCyril Bur 	assert(context);
2471aa096a9SCyril Bur 
2481aa096a9SCyril Bur 	bt_q = context->bt_q;
2491aa096a9SCyril Bur 	if (!bt_q) {
2501aa096a9SCyril Bur 		MSG_ERR("Dequeuing empty queue!\n");
2511aa096a9SCyril Bur 		return NULL;
2521aa096a9SCyril Bur 	}
2531aa096a9SCyril Bur 
2541aa096a9SCyril Bur 	r = bt_q->next;
2551aa096a9SCyril Bur 	bt_q_free(bt_q);
2561aa096a9SCyril Bur 	context->bt_q = r;
2571aa096a9SCyril Bur 
2581aa096a9SCyril Bur 	return r;
2591aa096a9SCyril Bur }
2601aa096a9SCyril Bur 
method_send_sms_atn(sd_bus_message * msg,void * userdata,sd_bus_error * ret_error)2619ae661cfSJoel Stanley static int method_send_sms_atn(sd_bus_message *msg, void *userdata,
2629ae661cfSJoel Stanley 			       sd_bus_error *ret_error)
2631aa096a9SCyril Bur {
264c0395684SChris Austen 	int r;
2659ae661cfSJoel Stanley 	struct btbridged_context *bt_fd = userdata;
266c0395684SChris Austen 
2679ae661cfSJoel Stanley 	MSG_OUT("Sending SMS_ATN ioctl (%d) to %s\n",
268825022b4SCédric Le Goater 			BT_BMC_IOCTL_SMS_ATN, BT_BMC_PATH);
269c0395684SChris Austen 
270825022b4SCédric Le Goater 	r = ioctl(bt_fd->fds[BT_FD].fd, BT_BMC_IOCTL_SMS_ATN);
2711aa096a9SCyril Bur 	if (r == -1) {
2721aa096a9SCyril Bur 		r = errno;
273825022b4SCédric Le Goater 		MSG_ERR("Couldn't ioctl() to 0x%x, %s: %s\n", bt_fd->fds[BT_FD].fd, BT_BMC_PATH, strerror(r));
2741aa096a9SCyril Bur 		return sd_bus_reply_method_errno(msg, errno, ret_error);
2751aa096a9SCyril Bur 	}
2761aa096a9SCyril Bur 
2771aa096a9SCyril Bur 	r = 0;
2781aa096a9SCyril Bur 	return sd_bus_reply_method_return(msg, "x", r);
2791aa096a9SCyril Bur }
2801aa096a9SCyril Bur 
method_send_message(sd_bus_message * msg,void * userdata,sd_bus_error * ret_error)2811aa096a9SCyril Bur static int method_send_message(sd_bus_message *msg, void *userdata, sd_bus_error *ret_error)
2821aa096a9SCyril Bur {
2831aa096a9SCyril Bur 	struct btbridged_context *context;
2841aa096a9SCyril Bur 	struct bt_queue *bt_msg;
285a720421cSJoel Stanley 	uint8_t *data;
286a720421cSJoel Stanley 	size_t data_sz;
2871aa096a9SCyril Bur 	uint8_t netfn, lun, seq, cmd, cc;
2881aa096a9SCyril Bur 	/*
2891aa096a9SCyril Bur 	 * Doesn't say it anywhere explicitly but it looks like returning 0 or
2901aa096a9SCyril Bur 	 * negative is BAD...
2911aa096a9SCyril Bur 	 */
2921aa096a9SCyril Bur 	int r = 1;
2931aa096a9SCyril Bur 
2941aa096a9SCyril Bur 	context = (struct btbridged_context *)userdata;
2951aa096a9SCyril Bur 	if (!context) {
2961aa096a9SCyril Bur 		sd_bus_error_set_const(ret_error, "org.openbmc.error", "Internal error");
2971aa096a9SCyril Bur 		r = 0;
2981aa096a9SCyril Bur 		goto out;
2991aa096a9SCyril Bur 	}
30099d545d3SCyril Bur 	r = sd_bus_message_read(msg, "yyyyy", &seq, &netfn, &lun, &cmd, &cc);
3011aa096a9SCyril Bur 	if (r < 0) {
3021aa096a9SCyril Bur 		MSG_ERR("Couldn't parse leading bytes of message: %s\n", strerror(-r));
3031aa096a9SCyril Bur 		sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message");
3041aa096a9SCyril Bur 		r = -EINVAL;
3051aa096a9SCyril Bur 		goto out;
3061aa096a9SCyril Bur 	}
3071aa096a9SCyril Bur 	r = sd_bus_message_read_array(msg, 'y', (const void **)&data, &data_sz);
3081aa096a9SCyril Bur 	if (r < 0) {
3091aa096a9SCyril Bur 		MSG_ERR("Couldn't parse data bytes of message: %s\n", strerror(-r));
3101aa096a9SCyril Bur 		sd_bus_error_set_const(ret_error, "org.openbmc.error", "Bad message data");
3111aa096a9SCyril Bur 		r = -EINVAL;
3121aa096a9SCyril Bur 		goto out;
3131aa096a9SCyril Bur 	}
3141aa096a9SCyril Bur 
3151aa096a9SCyril Bur 	bt_msg = bt_q_get_seq(context, seq);
3161aa096a9SCyril Bur 	if (!bt_msg) {
3171aa096a9SCyril Bur 		sd_bus_error_set_const(ret_error, "org.openbmc.error", "No matching request");
3181aa096a9SCyril Bur 		MSG_ERR("Failed to find matching request for dbus method with seq: 0x%02x\n", seq);
3191aa096a9SCyril Bur 		r = -EINVAL;
3201aa096a9SCyril Bur 		goto out;
3211aa096a9SCyril Bur 	}
322104200b2SCyril Bur 	MSG_OUT("Received a dbus response for msg with seq 0x%02x\n", seq);
3231aa096a9SCyril Bur 	bt_msg->call = sd_bus_message_ref(msg);
3241aa096a9SCyril Bur 	bt_msg->rsp.netfn = netfn;
3251aa096a9SCyril Bur 	bt_msg->rsp.lun = lun;
3261aa096a9SCyril Bur 	bt_msg->rsp.seq = seq;
3271aa096a9SCyril Bur 	bt_msg->rsp.cmd = cmd;
3281aa096a9SCyril Bur 	bt_msg->rsp.cc = cc;
3291aa096a9SCyril Bur 	bt_msg->rsp.data_len = data_sz;
3301aa096a9SCyril Bur 	/* Because we've ref'ed the msg, I hope we don't need to memcpy data */
3311aa096a9SCyril Bur 	bt_msg->rsp.data = data;
3321aa096a9SCyril Bur 
3331aa096a9SCyril Bur 	/* Now that we have a response */
3341aa096a9SCyril Bur 	context->fds[BT_FD].events |= POLLOUT;
3351aa096a9SCyril Bur 
3361aa096a9SCyril Bur out:
3371aa096a9SCyril Bur 	return r;
3381aa096a9SCyril Bur }
3391aa096a9SCyril Bur 
bt_host_write(struct btbridged_context * context,struct bt_queue * bt_msg)3401aa096a9SCyril Bur static int bt_host_write(struct btbridged_context *context, struct bt_queue *bt_msg)
3411aa096a9SCyril Bur {
3421aa096a9SCyril Bur 	struct bt_queue *head;
3431aa096a9SCyril Bur 	uint8_t data[BT_MAX_MESSAGE] = { 0 };
3441aa096a9SCyril Bur 	sd_bus_message *msg = NULL;
345c4bc8f08SMatthew Barth 	int r = 0, len;
3461aa096a9SCyril Bur 
3471aa096a9SCyril Bur 	assert(context);
3481aa096a9SCyril Bur 
3491aa096a9SCyril Bur 	if (!bt_msg)
3501aa096a9SCyril Bur 		return -EINVAL;
3511aa096a9SCyril Bur 
3521aa096a9SCyril Bur 	head = bt_q_get_head(context);
3531aa096a9SCyril Bur 	if (bt_msg == head) {
3541aa096a9SCyril Bur 		struct itimerspec ts;
3551aa096a9SCyril Bur 		/* Need to adjust the timer */
3561aa096a9SCyril Bur 		ts.it_interval.tv_sec = 0;
3571aa096a9SCyril Bur 		ts.it_interval.tv_nsec = 0;
3581aa096a9SCyril Bur 		if (head->next) {
3591aa096a9SCyril Bur 			ts.it_value = head->next->start;
360825022b4SCédric Le Goater 			ts.it_value.tv_sec += BT_BMC_TIMEOUT_SEC;
3611aa096a9SCyril Bur 			MSG_OUT("Adjusting timer for next element\n");
3621aa096a9SCyril Bur 		} else {
3631aa096a9SCyril Bur 			ts.it_value.tv_nsec = 0;
3641aa096a9SCyril Bur 			ts.it_value.tv_sec = 0;
3651aa096a9SCyril Bur 			MSG_OUT("Disabling timer, no elements remain in queue\n");
3661aa096a9SCyril Bur 		}
3671aa096a9SCyril Bur 		r = timerfd_settime(context->fds[TIMER_FD].fd, TFD_TIMER_ABSTIME, &ts, NULL);
3681aa096a9SCyril Bur 		if (r == -1)
3691aa096a9SCyril Bur 			MSG_ERR("Couldn't set timerfd\n");
3701aa096a9SCyril Bur 	}
3711aa096a9SCyril Bur 	data[1] = (bt_msg->rsp.netfn << 2) | (bt_msg->rsp.lun & 0x3);
3721aa096a9SCyril Bur 	data[2] = bt_msg->rsp.seq;
3731aa096a9SCyril Bur 	data[3] = bt_msg->rsp.cmd;
3741aa096a9SCyril Bur 	data[4] = bt_msg->rsp.cc;
3751aa096a9SCyril Bur 	if (bt_msg->rsp.data_len > sizeof(data) - 5) {
376f84329e4SJoel Stanley 		MSG_ERR("Response message size (%zu) too big, truncating\n", bt_msg->rsp.data_len);
3771aa096a9SCyril Bur 		bt_msg->rsp.data_len = sizeof(data) - 5;
3781aa096a9SCyril Bur 	}
3791aa096a9SCyril Bur 	/* netfn/len + seq + cmd + cc = 4 */
3801aa096a9SCyril Bur 	data[0] = bt_msg->rsp.data_len + 4;
3811aa096a9SCyril Bur 	if (bt_msg->rsp.data_len)
3821aa096a9SCyril Bur 		memcpy(data + 5, bt_msg->rsp.data, bt_msg->rsp.data_len);
3831aa096a9SCyril Bur 	/* Count the data[0] byte */
3841aa096a9SCyril Bur 	len = write(context->fds[BT_FD].fd, data, data[0] + 1);
385b92a933bSJoel Stanley 	if (len == -1) {
3861aa096a9SCyril Bur 		r = errno;
387825022b4SCédric Le Goater 		MSG_ERR("Error writing to %s: %s\n", BT_BMC_PATH, strerror(errno));
3881aa096a9SCyril Bur 		if (bt_msg->call) {
3891aa096a9SCyril Bur 			r = sd_bus_message_new_method_errno(bt_msg->call, &msg, r, NULL);
3901aa096a9SCyril Bur 			if (r < 0)
3911aa096a9SCyril Bur 				MSG_ERR("Couldn't create response error\n");
3921aa096a9SCyril Bur 		}
3931aa096a9SCyril Bur 		goto out;
3941aa096a9SCyril Bur 	} else {
3951aa096a9SCyril Bur 		if (len != data[0] + 1)
396825022b4SCédric Le Goater 			MSG_ERR("Possible short write to %s, desired len: %d, written len: %d\n", BT_BMC_PATH, data[0] + 1, len);
3971aa096a9SCyril Bur 		else
398825022b4SCédric Le Goater 			MSG_OUT("Successfully wrote %d of %d bytes to %s\n", len, data[0] + 1, BT_BMC_PATH);
3991aa096a9SCyril Bur 
4001aa096a9SCyril Bur 		if (bt_msg->call) {
4011aa096a9SCyril Bur 			r = sd_bus_message_new_method_return(bt_msg->call, &msg);
4021aa096a9SCyril Bur 			if (r < 0) {
4031aa096a9SCyril Bur 				MSG_ERR("Couldn't create response message\n");
4041aa096a9SCyril Bur 				goto out;
4051aa096a9SCyril Bur 			}
4061aa096a9SCyril Bur 			r = 0; /* Just to be explicit about what we're sending back */
4071aa096a9SCyril Bur 			r = sd_bus_message_append(msg, "x", r);
4081aa096a9SCyril Bur 			if (r < 0) {
4091aa096a9SCyril Bur 				MSG_ERR("Couldn't append result to response\n");
4101aa096a9SCyril Bur 				goto out;
4111aa096a9SCyril Bur 			}
4121aa096a9SCyril Bur 
4131aa096a9SCyril Bur 		}
4141aa096a9SCyril Bur 	}
4151aa096a9SCyril Bur 
4161aa096a9SCyril Bur out:
4171aa096a9SCyril Bur 	if (bt_msg->call) {
4181aa096a9SCyril Bur 		if (sd_bus_send(context->bus, msg, NULL) < 0)
4191aa096a9SCyril Bur 			MSG_ERR("Couldn't send response message\n");
4201aa096a9SCyril Bur 		sd_bus_message_unref(msg);
4211aa096a9SCyril Bur 	}
4221aa096a9SCyril Bur 	bt_q_drop(context, bt_msg);
4231aa096a9SCyril Bur 
4241aa096a9SCyril Bur 	/*
4251aa096a9SCyril Bur 	 * There isn't another message ready to be sent so turn off POLLOUT
4261aa096a9SCyril Bur 	 */
4271aa096a9SCyril Bur 	if (!bt_q_get_msg(context)) {
4281aa096a9SCyril Bur 		MSG_OUT("Turning off POLLOUT for the BT in poll()\n");
4291aa096a9SCyril Bur 		context->fds[BT_FD].events = POLLIN;
4301aa096a9SCyril Bur 	}
4311aa096a9SCyril Bur 
4321aa096a9SCyril Bur 	return r;
4331aa096a9SCyril Bur }
4341aa096a9SCyril Bur 
dispatch_timer(struct btbridged_context * context)4351aa096a9SCyril Bur static int dispatch_timer(struct btbridged_context *context)
4361aa096a9SCyril Bur {
4371aa096a9SCyril Bur 	int r = 0;
4381aa096a9SCyril Bur 	if (context->fds[TIMER_FD].revents & POLLIN) {
4391aa096a9SCyril Bur 		struct bt_queue *head;
440aa5511d2SAndrew Jeffery 		sd_bus_message *msg;
441aa5511d2SAndrew Jeffery 		uint64_t counter;
442aa5511d2SAndrew Jeffery 
443aa5511d2SAndrew Jeffery 		/* Empty out timerfd so we won't trigger POLLIN continuously */
444aa5511d2SAndrew Jeffery 		r = read(context->fds[TIMER_FD].fd, &counter, sizeof(counter));
445aa5511d2SAndrew Jeffery 		MSG_OUT("Timer fired %" PRIu64 " times\n", counter);
4461aa096a9SCyril Bur 
4471aa096a9SCyril Bur 		head = bt_q_get_head(context);
4481aa096a9SCyril Bur 		if (!head) {
4491aa096a9SCyril Bur 			/* Odd, timer expired but we didn't have a message to timeout */
4501aa096a9SCyril Bur 			MSG_ERR("No message found to send timeout\n");
4511aa096a9SCyril Bur 			return 0;
4521aa096a9SCyril Bur 		}
4531aa096a9SCyril Bur 		if (head->call) {
4541aa096a9SCyril Bur 			r = sd_bus_message_new_method_errno(head->call, &msg, ETIMEDOUT, NULL);
4551aa096a9SCyril Bur 			if (r < 0) {
4561aa096a9SCyril Bur 				MSG_ERR("Couldn't create response error\n");
4571aa096a9SCyril Bur 			} else {
4581aa096a9SCyril Bur 				if (sd_bus_send(context->bus, msg, NULL) < 0)
4591aa096a9SCyril Bur 					MSG_ERR("Couldn't send response message\n");
4601aa096a9SCyril Bur 				sd_bus_message_unref(msg);
4611aa096a9SCyril Bur 			}
4621aa096a9SCyril Bur 			MSG_ERR("Message with seq 0x%02x is being timed out despite "
4631aa096a9SCyril Bur 					"appearing to have been responded to. Slow BT?\n", head->rsp.seq);
4641aa096a9SCyril Bur 		}
4651aa096a9SCyril Bur 
4661aa096a9SCyril Bur 		/* Set expiry */
4671aa096a9SCyril Bur 		head->expired = 1;
4681aa096a9SCyril Bur 		head->rsp.seq = head->req.seq;
4691aa096a9SCyril Bur 		head->rsp.netfn = head->req.netfn + 1;
4701aa096a9SCyril Bur 		head->rsp.lun = head->req.lun;
4711aa096a9SCyril Bur 		head->rsp.cc = 0xce; /* Command response could not be provided */
4721aa096a9SCyril Bur 		head->rsp.cmd = head->req.cmd;
4731aa096a9SCyril Bur 		/* These should already be zeroed but best to be sure */
4741aa096a9SCyril Bur 		head->rsp.data_len = 0;
4751aa096a9SCyril Bur 		head->rsp.data = NULL;
4761aa096a9SCyril Bur 		head->call = NULL;
4771aa096a9SCyril Bur 		MSG_OUT("Timeout on msg with seq: 0x%02x\n", head->rsp.seq);
4781aa096a9SCyril Bur 		/* Turn on POLLOUT so we'll write this one next */
4791aa096a9SCyril Bur 		context->fds[BT_FD].events |= POLLOUT;
4801aa096a9SCyril Bur 	}
4811aa096a9SCyril Bur 
4821aa096a9SCyril Bur 	return 0;
4831aa096a9SCyril Bur }
4841aa096a9SCyril Bur 
dispatch_sd_bus(struct btbridged_context * context)4851aa096a9SCyril Bur static int dispatch_sd_bus(struct btbridged_context *context)
4861aa096a9SCyril Bur {
4871aa096a9SCyril Bur 	int r = 0;
4881aa096a9SCyril Bur 	if (context->fds[SD_BUS_FD].revents) {
4891aa096a9SCyril Bur 		r = sd_bus_process(context->bus, NULL);
4901aa096a9SCyril Bur 		if (r > 0)
4911aa096a9SCyril Bur 			MSG_OUT("Processed %d dbus events\n", r);
4921aa096a9SCyril Bur 	}
4931aa096a9SCyril Bur 
4941aa096a9SCyril Bur 	return r;
4951aa096a9SCyril Bur }
4961aa096a9SCyril Bur 
dispatch_bt(struct btbridged_context * context)4971aa096a9SCyril Bur static int dispatch_bt(struct btbridged_context *context)
4981aa096a9SCyril Bur {
4991aa096a9SCyril Bur 	int err = 0;
500c4bc8f08SMatthew Barth 	int r = 0;
5011aa096a9SCyril Bur 
5021aa096a9SCyril Bur 	assert(context);
5031aa096a9SCyril Bur 
5041aa096a9SCyril Bur 	if (context->fds[BT_FD].revents & POLLIN) {
505fbff9f55SCyril Bur 		sd_bus_message *msg;
5061aa096a9SCyril Bur 		struct bt_queue *new;
5071aa096a9SCyril Bur 		uint8_t data[BT_MAX_MESSAGE] = { 0 };
5081aa096a9SCyril Bur 
5091aa096a9SCyril Bur 		r = read(context->fds[BT_FD].fd, data, sizeof(data));
5101aa096a9SCyril Bur 		if (r < 0) {
5111aa096a9SCyril Bur 			MSG_ERR("Couldn't read from bt: %s\n", strerror(-r));
5121aa096a9SCyril Bur 			goto out1;
5131aa096a9SCyril Bur 		}
5141aa096a9SCyril Bur 		if (r < data[0] + 1) {
5158d057981SCyril Bur 			MSG_ERR("Short read from bt (%d vs %d)\n", r, data[0] + 1);
5168d057981SCyril Bur 			r = 0;
5171aa096a9SCyril Bur 			goto out1;
5181aa096a9SCyril Bur 		}
5191aa096a9SCyril Bur 
5201aa096a9SCyril Bur 		new = bt_q_enqueue(context, data);
5211aa096a9SCyril Bur 		if (!new) {
5221aa096a9SCyril Bur 			r = -ENOMEM;
5231aa096a9SCyril Bur 			goto out1;
5241aa096a9SCyril Bur 		}
5251aa096a9SCyril Bur 		if (new == bt_q_get_head(context)) {
5261aa096a9SCyril Bur 			struct itimerspec ts;
5271aa096a9SCyril Bur 			/*
5281aa096a9SCyril Bur 			 * Enqueued onto an empty list, setup a timer for sending a
5291aa096a9SCyril Bur 			 * timeout
5301aa096a9SCyril Bur 			 */
5311aa096a9SCyril Bur 			ts.it_interval.tv_sec = 0;
5321aa096a9SCyril Bur 			ts.it_interval.tv_nsec = 0;
5331aa096a9SCyril Bur 			ts.it_value.tv_nsec = 0;
534825022b4SCédric Le Goater 			ts.it_value.tv_sec = BT_BMC_TIMEOUT_SEC;
5351aa096a9SCyril Bur 			r = timerfd_settime(context->fds[TIMER_FD].fd, 0, &ts, NULL);
5361aa096a9SCyril Bur 			if (r == -1)
5371aa096a9SCyril Bur 				MSG_ERR("Couldn't set timerfd\n");
5381aa096a9SCyril Bur 		}
5391aa096a9SCyril Bur 
540fbff9f55SCyril Bur 		r = sd_bus_message_new_signal(context->bus, &msg, OBJ_NAME, DBUS_NAME, "ReceivedMessage");
5411aa096a9SCyril Bur 		if (r < 0) {
542fbff9f55SCyril Bur 			MSG_ERR("Failed to create signal: %s\n", strerror(-r));
5431aa096a9SCyril Bur 			goto out1;
5441aa096a9SCyril Bur 		}
545fbff9f55SCyril Bur 
546fbff9f55SCyril Bur 		r = sd_bus_message_append(msg, "yyyy", new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
547fbff9f55SCyril Bur 		if (r < 0) {
548fbff9f55SCyril Bur 			MSG_ERR("Couldn't append to signal: %s\n", strerror(-r));
549fbff9f55SCyril Bur 			goto out1_free;
550fbff9f55SCyril Bur 		}
551fbff9f55SCyril Bur 
552fbff9f55SCyril Bur 		r = sd_bus_message_append_array(msg, 'y', data + 4, new->req.data_len);
553fbff9f55SCyril Bur 		if (r < 0) {
554f84329e4SJoel Stanley 			MSG_ERR("Couldn't append array to signal: %s\n", strerror(-r));
555fbff9f55SCyril Bur 			goto out1_free;
556fbff9f55SCyril Bur 		}
557fbff9f55SCyril Bur 
558fbff9f55SCyril Bur 		MSG_OUT("Sending dbus signal with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x\n",
559fbff9f55SCyril Bur 				new->req.seq, new->req.netfn, new->req.lun, new->req.cmd);
560fbff9f55SCyril Bur 
561a28444b6SCyril Bur 		if (verbosity == BT_LOG_DEBUG) {
562*8ee19a4dSEd Tanous 			size_t i;
563fbff9f55SCyril Bur 			for (i = 0; i < new->req.data_len; i++) {
56482eafec8SCyril Bur 				if (i % 8 == 0) {
56582eafec8SCyril Bur 					if (i)
56682eafec8SCyril Bur 						printf("\n");
56782eafec8SCyril Bur 					MSG_OUT("\t");
568fbff9f55SCyril Bur 				}
56982eafec8SCyril Bur 				printf("0x%02x ", data[i + 4]);
57082eafec8SCyril Bur 			}
57182eafec8SCyril Bur 			if (new->req.data_len)
57282eafec8SCyril Bur 				printf("\n");
573fbff9f55SCyril Bur 		}
574fbff9f55SCyril Bur 
575fbff9f55SCyril Bur 		/* Note we only actually keep the request data in the queue when debugging */
576fbff9f55SCyril Bur 		r = sd_bus_send(context->bus, msg, NULL);
577fbff9f55SCyril Bur 		if (r < 0) {
578fbff9f55SCyril Bur 			MSG_ERR("Couldn't emit dbus signal: %s\n", strerror(-r));
579fbff9f55SCyril Bur 			goto out1_free;
580fbff9f55SCyril Bur 		}
5811aa096a9SCyril Bur 		r = 0;
582fbff9f55SCyril Bur out1_free:
583fbff9f55SCyril Bur 		sd_bus_message_unref(msg);
5841aa096a9SCyril Bur out1:
5851aa096a9SCyril Bur 		err = r;
5861aa096a9SCyril Bur 	}
5871aa096a9SCyril Bur 
5881aa096a9SCyril Bur 	if (context->fds[BT_FD].revents & POLLOUT) {
5891aa096a9SCyril Bur 		struct bt_queue *bt_msg;
5901aa096a9SCyril Bur 		bt_msg = bt_q_get_msg(context);
5911aa096a9SCyril Bur 		if (!bt_msg) {
5921aa096a9SCyril Bur 			/* Odd, this shouldn't really happen */
593f84329e4SJoel Stanley 			MSG_ERR("Got a POLLOUT but no message is ready to be written\n");
5941aa096a9SCyril Bur 			r = 0;
5951aa096a9SCyril Bur 			goto out;
5961aa096a9SCyril Bur 		}
5971aa096a9SCyril Bur 		r = bt_host_write(context, bt_msg);
5981aa096a9SCyril Bur 		if (r < 0)
59999d545d3SCyril Bur 			MSG_ERR("Problem putting request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n"
600f84329e4SJoel Stanley 					"out to %s", bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun,
601825022b4SCédric Le Goater 					bt_msg->rsp.cmd, bt_msg->rsp.cc, BT_BMC_PATH);
6021aa096a9SCyril Bur 		else
60399d545d3SCyril Bur 			MSG_OUT("Completed request with seq 0x%02x, netfn 0x%02x, lun 0x%02x, cmd 0x%02x, cc 0x%02x\n",
60499d545d3SCyril Bur 					bt_msg->rsp.seq, bt_msg->rsp.netfn, bt_msg->rsp.lun, bt_msg->rsp.cmd, bt_msg->rsp.cc);
6051aa096a9SCyril Bur 	}
6061aa096a9SCyril Bur out:
6071aa096a9SCyril Bur 	return err ? err : r;
6081aa096a9SCyril Bur }
6091aa096a9SCyril Bur 
usage(const char * name)6101aa096a9SCyril Bur static void usage(const char *name)
6111aa096a9SCyril Bur {
61274e18137SCédric Le Goater 	fprintf(stderr, "\
61374e18137SCédric Le Goater Usage %s [--v[v] | --syslog] [-d <DEVICE>]\n\
61474e18137SCédric Le Goater   --v                    Be verbose\n\
61574e18137SCédric Le Goater   --vv                   Be verbose and dump entire messages\n\
61674e18137SCédric Le Goater   -s, --syslog           Log output to syslog (pointless without --verbose)\n\
61774e18137SCédric Le Goater   -d, --device <DEVICE>  use <DEVICE> file. Default is '%s'\n\n",
61874e18137SCédric Le Goater 		name, bt_bmc_device);
6191aa096a9SCyril Bur }
6201aa096a9SCyril Bur 
6211aa096a9SCyril Bur static const sd_bus_vtable ipmid_vtable[] = {
6221aa096a9SCyril Bur 	SD_BUS_VTABLE_START(0),
6231aa096a9SCyril Bur 	SD_BUS_METHOD("sendMessage", "yyyyyay", "x", &method_send_message, SD_BUS_VTABLE_UNPRIVILEGED),
6241aa096a9SCyril Bur 	SD_BUS_METHOD("setAttention", "", "x", &method_send_sms_atn, SD_BUS_VTABLE_UNPRIVILEGED),
625104200b2SCyril Bur 	SD_BUS_SIGNAL("ReceivedMessage", "yyyyay", 0),
6261aa096a9SCyril Bur 	SD_BUS_VTABLE_END
6271aa096a9SCyril Bur };
6281aa096a9SCyril Bur 
main(int argc,char * argv[])6291aa096a9SCyril Bur int main(int argc, char *argv[]) {
63057ce2909SJoel Stanley 	struct btbridged_context *context;
6311aa096a9SCyril Bur 	const char *name = argv[0];
632a720421cSJoel Stanley 	int opt, polled, r;
6331aa096a9SCyril Bur 
63457ce2909SJoel Stanley 	static const struct option long_options[] = {
63574e18137SCédric Le Goater 		{ "device",  required_argument, NULL, 'd' },
636a28444b6SCyril Bur 		{ "v",       no_argument, (int *)&verbosity, BT_LOG_VERBOSE },
637a28444b6SCyril Bur 		{ "vv",      no_argument, (int *)&verbosity, BT_LOG_DEBUG   },
638201c8d71SCyril Bur 		{ "syslog",  no_argument, 0,          's'         },
6391aa096a9SCyril Bur 		{ 0,         0,           0,          0           }
6401aa096a9SCyril Bur 	};
6411aa096a9SCyril Bur 
64257ce2909SJoel Stanley 	context = calloc(1, sizeof(*context));
6431aa096a9SCyril Bur 
644201c8d71SCyril Bur 	bt_vlog = &bt_log_console;
6451aa096a9SCyril Bur 	while ((opt = getopt_long(argc, argv, "", long_options, NULL)) != -1) {
6461aa096a9SCyril Bur 		switch (opt) {
6471aa096a9SCyril Bur 			case 0:
6481aa096a9SCyril Bur 				break;
64974e18137SCédric Le Goater 			case 'd':
65074e18137SCédric Le Goater 				bt_bmc_device = optarg;
65174e18137SCédric Le Goater 				break;
652201c8d71SCyril Bur 			case 's':
653201c8d71SCyril Bur 				/* Avoid a double openlog() */
654201c8d71SCyril Bur 				if (bt_vlog != &vsyslog) {
655201c8d71SCyril Bur 					openlog(PREFIX, LOG_ODELAY, LOG_DAEMON);
656201c8d71SCyril Bur 					bt_vlog = &vsyslog;
657201c8d71SCyril Bur 				}
658201c8d71SCyril Bur 				break;
6591aa096a9SCyril Bur 			default:
6601aa096a9SCyril Bur 				usage(name);
6611aa096a9SCyril Bur 				exit(EXIT_FAILURE);
6621aa096a9SCyril Bur 		}
6631aa096a9SCyril Bur 	}
6641aa096a9SCyril Bur 
665a28444b6SCyril Bur 	if (verbosity == BT_LOG_VERBOSE)
666a28444b6SCyril Bur 		MSG_OUT("Verbose logging\n");
66782eafec8SCyril Bur 
668a28444b6SCyril Bur 	if (verbosity == BT_LOG_DEBUG)
669a28444b6SCyril Bur 		MSG_OUT("Debug logging\n");
67082eafec8SCyril Bur 
6711aa096a9SCyril Bur 	MSG_OUT("Starting\n");
67257ce2909SJoel Stanley 	r = sd_bus_default_system(&context->bus);
6731aa096a9SCyril Bur 	if (r < 0) {
6741aa096a9SCyril Bur 		MSG_ERR("Failed to connect to system bus: %s\n", strerror(-r));
6751aa096a9SCyril Bur 		goto finish;
6761aa096a9SCyril Bur 	}
6771aa096a9SCyril Bur 
6781aa096a9SCyril Bur 	MSG_OUT("Registering dbus methods/signals\n");
67957ce2909SJoel Stanley 	r = sd_bus_add_object_vtable(context->bus,
6801aa096a9SCyril Bur 	                             NULL,
6811aa096a9SCyril Bur 	                             OBJ_NAME,
6821aa096a9SCyril Bur 	                             DBUS_NAME,
6831aa096a9SCyril Bur 	                             ipmid_vtable,
68457ce2909SJoel Stanley 	                             context);
6851aa096a9SCyril Bur 	if (r < 0) {
6861aa096a9SCyril Bur 		MSG_ERR("Failed to issue method call: %s\n", strerror(-r));
6871aa096a9SCyril Bur 		goto finish;
6881aa096a9SCyril Bur 	}
6891aa096a9SCyril Bur 
6901aa096a9SCyril Bur 	MSG_OUT("Requesting dbus name: %s\n", DBUS_NAME);
69157ce2909SJoel Stanley 	r = sd_bus_request_name(context->bus, DBUS_NAME, SD_BUS_NAME_ALLOW_REPLACEMENT|SD_BUS_NAME_REPLACE_EXISTING);
6921aa096a9SCyril Bur 	if (r < 0) {
6931aa096a9SCyril Bur 		MSG_ERR("Failed to acquire service name: %s\n", strerror(-r));
6941aa096a9SCyril Bur 		goto finish;
6951aa096a9SCyril Bur 	}
6961aa096a9SCyril Bur 
6971aa096a9SCyril Bur 	MSG_OUT("Getting dbus file descriptors\n");
69857ce2909SJoel Stanley 	context->fds[SD_BUS_FD].fd = sd_bus_get_fd(context->bus);
69957ce2909SJoel Stanley 	if (context->fds[SD_BUS_FD].fd < 0) {
7001aa096a9SCyril Bur 		r = -errno;
7011aa096a9SCyril Bur 		MSG_OUT("Couldn't get the bus file descriptor: %s\n", strerror(errno));
7021aa096a9SCyril Bur 		goto finish;
7031aa096a9SCyril Bur 	}
7041aa096a9SCyril Bur 
705825022b4SCédric Le Goater 	MSG_OUT("Opening %s\n", BT_BMC_PATH);
706825022b4SCédric Le Goater 	context->fds[BT_FD].fd = open(BT_BMC_PATH, O_RDWR | O_NONBLOCK);
70757ce2909SJoel Stanley 	if (context->fds[BT_FD].fd < 0) {
7081aa096a9SCyril Bur 		r = -errno;
709825022b4SCédric Le Goater 		MSG_ERR("Couldn't open %s with flags O_RDWR: %s\n", BT_BMC_PATH, strerror(errno));
7101aa096a9SCyril Bur 		goto finish;
7111aa096a9SCyril Bur 	}
7121aa096a9SCyril Bur 
7131aa096a9SCyril Bur 	MSG_OUT("Creating timer fd\n");
71457ce2909SJoel Stanley 	context->fds[TIMER_FD].fd = timerfd_create(CLOCK_MONOTONIC, 0);
71557ce2909SJoel Stanley 	if (context->fds[TIMER_FD].fd < 0) {
7161aa096a9SCyril Bur 		r = -errno;
7171aa096a9SCyril Bur 		MSG_ERR("Couldn't create timer fd: %s\n", strerror(errno));
7181aa096a9SCyril Bur 		goto finish;
7191aa096a9SCyril Bur 	}
72057ce2909SJoel Stanley 	context->fds[SD_BUS_FD].events = POLLIN;
72157ce2909SJoel Stanley 	context->fds[BT_FD].events = POLLIN;
72257ce2909SJoel Stanley 	context->fds[TIMER_FD].events = POLLIN;
7231aa096a9SCyril Bur 
7241aa096a9SCyril Bur 	MSG_OUT("Entering polling loop\n");
7251aa096a9SCyril Bur 
7261aa096a9SCyril Bur 	while (running) {
72757ce2909SJoel Stanley 		polled = poll(context->fds, TOTAL_FDS, 1000);
7281aa096a9SCyril Bur 		if (polled == 0)
7291aa096a9SCyril Bur 			continue;
7301aa096a9SCyril Bur 		if (polled < 0) {
7311aa096a9SCyril Bur 			r = -errno;
7321aa096a9SCyril Bur 			MSG_ERR("Error from poll(): %s\n", strerror(errno));
7331aa096a9SCyril Bur 			goto finish;
7341aa096a9SCyril Bur 		}
73557ce2909SJoel Stanley 		r = dispatch_sd_bus(context);
7361aa096a9SCyril Bur 		if (r < 0) {
7371aa096a9SCyril Bur 			MSG_ERR("Error handling dbus event: %s\n", strerror(-r));
7381aa096a9SCyril Bur 			goto finish;
7391aa096a9SCyril Bur 		}
74057ce2909SJoel Stanley 		r = dispatch_bt(context);
7411aa096a9SCyril Bur 		if (r < 0) {
7421aa096a9SCyril Bur 			MSG_ERR("Error handling BT event: %s\n", strerror(-r));
7431aa096a9SCyril Bur 			goto finish;
7441aa096a9SCyril Bur 		}
74557ce2909SJoel Stanley 		r = dispatch_timer(context);
7461aa096a9SCyril Bur 		if (r < 0) {
7471aa096a9SCyril Bur 			MSG_ERR("Error handling timer event: %s\n", strerror(-r));
7481aa096a9SCyril Bur 			goto finish;
7491aa096a9SCyril Bur 		}
7501aa096a9SCyril Bur 	}
7511aa096a9SCyril Bur 
7521aa096a9SCyril Bur finish:
75357ce2909SJoel Stanley 	if (bt_q_get_head(context)) {
7541aa096a9SCyril Bur 		MSG_ERR("Unresponded BT Message!\n");
75557ce2909SJoel Stanley 		while (bt_q_dequeue(context));
7561aa096a9SCyril Bur 	}
75757ce2909SJoel Stanley 	close(context->fds[BT_FD].fd);
75857ce2909SJoel Stanley 	close(context->fds[TIMER_FD].fd);
75957ce2909SJoel Stanley 	sd_bus_unref(context->bus);
76057ce2909SJoel Stanley 	free(context);
7611aa096a9SCyril Bur 
7621aa096a9SCyril Bur 	return r;
7631aa096a9SCyril Bur }
7641aa096a9SCyril Bur 
765