jolly has uploaded this change for review. ( https://gerrit.osmocom.org/c/osmo-e1d/+/35558?usp=email )
Change subject: Add functions to events from server to client ......................................................................
Add functions to events from server to client
The client may register a callback function to receive events.
Because there is no relation between the connected client and the interface, all events are broadcasted to all clients that are connected to the server.
Change-Id: I5ee3268f8349b611c3cf3fa0572dc5eab280ab2e --- M include/osmocom/e1d/proto_clnt.h M include/osmocom/e1d/proto_srv.h M src/e1d.h M src/osmo-e1d.c M src/osmo-e1gen.c M src/proto_clnt.c M src/proto_srv.c 7 files changed, 73 insertions(+), 2 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/osmo-e1d refs/changes/58/35558/1
diff --git a/include/osmocom/e1d/proto_clnt.h b/include/osmocom/e1d/proto_clnt.h index 53ccf89..26bd44e 100644 --- a/include/osmocom/e1d/proto_clnt.h +++ b/include/osmocom/e1d/proto_clnt.h @@ -49,3 +49,5 @@ int osmo_e1dp_client_ts_open_force(struct osmo_e1dp_client *clnt, uint8_t intf, uint8_t line, uint8_t ts, enum osmo_e1dp_ts_mode mode, uint16_t read_bufsize); +void osmo_e1dp_client_event_register(struct osmo_e1dp_client *clnt, + void (*cb)(enum osmo_e1dp_msg_type event, uint8_t intf, uint8_t line, uint8_t ts, uint8_t *data, int len)); diff --git a/include/osmocom/e1d/proto_srv.h b/include/osmocom/e1d/proto_srv.h index e794725..f95ae13 100644 --- a/include/osmocom/e1d/proto_srv.h +++ b/include/osmocom/e1d/proto_srv.h @@ -46,3 +46,5 @@ struct osmo_e1dp_server *osmo_e1dp_server_create(void *ctx, const char *path, struct osmo_e1dp_server_handler *handlers, void *handler_data); void osmo_e1dp_server_destroy(struct osmo_e1dp_server *srv); +void osmo_e1dp_server_event(struct osmo_e1dp_server *srv, enum osmo_e1dp_msg_type event, + uint8_t intf, uint8_t line, uint8_t ts, uint8_t *data, int len); diff --git a/src/e1d.h b/src/e1d.h index 45ecb82..ec264c7 100644 --- a/src/e1d.h +++ b/src/e1d.h @@ -196,6 +196,7 @@ struct e1_daemon { void *ctx; struct llist_head interfaces; + void *srv; };
extern const struct octoi_ops e1d_octoi_ops; diff --git a/src/osmo-e1d.c b/src/osmo-e1d.c index dc264d9..329f584 100644 --- a/src/osmo-e1d.c +++ b/src/osmo-e1d.c @@ -44,8 +44,8 @@ #include <osmocom/vty/misc.h> #include <osmocom/vty/cpu_sched_vty.h>
-#include <osmocom/e1d/proto_srv.h> #include <osmocom/e1d/proto.h> +#include <osmocom/e1d/proto_srv.h>
#include "e1d.h" #include <osmocom/octoi/octoi.h> @@ -221,6 +221,7 @@ /* server init */ srv = osmo_e1dp_server_create(g_e1d_ctx, E1DP_DEFAULT_SOCKET, e1d_ctl_handlers, e1d); OSMO_ASSERT(srv); + e1d->srv = srv;
/* main loop */ while (!g_shutdown) { diff --git a/src/osmo-e1gen.c b/src/osmo-e1gen.c index 008f083..81ef21a 100644 --- a/src/osmo-e1gen.c +++ b/src/osmo-e1gen.c @@ -436,3 +436,5 @@
return 0; } + +void osmo_e1dp_server_event(void) {} diff --git a/src/proto_clnt.c b/src/proto_clnt.c index 93853e9..d922e4a 100644 --- a/src/proto_clnt.c +++ b/src/proto_clnt.c @@ -69,13 +69,20 @@ struct osmo_e1dp_client { void *ctx; /*!< talloc context */ struct osmo_fd ctl_fd; /*!< osmo-fd wrapped unix domain (CTL) socket to @osmo-e1d@ */ + void (*event_cb)(enum osmo_e1dp_msg_type event, uint8_t intf, uint8_t line, uint8_t ts, uint8_t *data, int len); + /*!< callback function for incoming events */ };
static int _e1dp_client_event(struct osmo_e1dp_client *clnt, struct msgb *msgb) { - /* FIXME */ + struct osmo_e1dp_msg_hdr *hdr = msgb_l1(msgb); + + if (!clnt->event_cb) + return -EINVAL; + + clnt->event_cb(hdr->type, hdr->intf, hdr->line, hdr->ts, msgb_l1(msgb) + sizeof(*hdr), msgb_l1len(msgb) - sizeof(*hdr)); return 0; }
@@ -455,3 +462,12 @@ { return _client_ts_open(clnt, intf, line, ts, mode, read_bufsize, E1DP_TS_OPEN_F_FORCE); } + +/*! Register event handler for incoming event messages. + * \param[in] clnt Client previously returned from osmo_e1dp_client_create(). */ +void +osmo_e1dp_client_event_register(struct osmo_e1dp_client *clnt, + void (*cb)(enum osmo_e1dp_msg_type event, uint8_t intf, uint8_t line, uint8_t ts, uint8_t *data, int len)) +{ + clnt->event_cb = cb; +} diff --git a/src/proto_srv.c b/src/proto_srv.c index 2b85ac6..ab2fd30 100644 --- a/src/proto_srv.c +++ b/src/proto_srv.c @@ -260,3 +260,35 @@ osmo_fd_close(&srv->ctl_fd); talloc_free(srv); } + +void +osmo_e1dp_server_event(struct osmo_e1dp_server *srv, enum osmo_e1dp_msg_type event, + uint8_t intf, uint8_t line, uint8_t ts, uint8_t *data, int len) +{ + struct osmo_e1dp_msg_hdr *hdr; + struct msgb *msgb; + struct osmo_e1dp_server_conn *conn; + + /* Call handler */ + msgb = msgb_alloc(E1DP_MAX_LEN, "e1d proto tx (event) message"); + + msgb->l1h = msgb_put(msgb, sizeof(struct osmo_e1dp_msg_hdr)); + hdr = msgb_l1(msgb); + if (data && len) { + msgb->l2h = msgb_put(msgb, len); + memcpy(msgb->l2h, data, len); + } + hdr->magic = E1DP_MAGIC; + hdr->type = event; + hdr->len = msgb_length(msgb); + hdr->intf = intf; + hdr->line = line; + hdr->ts = ts; + + /* Send event */ + llist_for_each_entry(conn, &srv->conns, list) + osmo_e1dp_send(&conn->fd, msgb, -1); + + /* Done */ + msgb_free(msgb); +}