Add functions for 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
This commit is contained in:
Andreas Eversberg 2024-01-14 14:29:00 +01:00
parent 69459b0a03
commit cd7d8a96a2
7 changed files with 61 additions and 2 deletions

View File

@ -49,3 +49,5 @@ int osmo_e1dp_client_ts_open(struct osmo_e1dp_client *clnt,
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));

View File

@ -46,3 +46,5 @@ struct osmo_e1dp_server_handler {
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);

View File

@ -193,9 +193,12 @@ struct e1_intf {
struct llist_head lines;
};
struct osmo_e1dp_server;
struct e1_daemon {
void *ctx;
struct llist_head interfaces;
struct osmo_e1dp_server *srv;
};
extern const struct octoi_ops e1d_octoi_ops;

View File

@ -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 @@ int main(int argc, char *argv[])
/* 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) {

View File

@ -436,3 +436,5 @@ int main(int argc, char **argv)
return 0;
}
void osmo_e1dp_server_event(void) {}

View File

@ -64,18 +64,26 @@
#include "log.h"
typedef void (*osmo_e1dp_event_cb_t)(enum osmo_e1dp_msg_type, uint8_t, uint8_t, uint8_t, uint8_t *, int);
/*! Internal representation of client program connected to the CTL socket */
struct osmo_e1dp_client {
void *ctx; /*!< talloc context */
struct osmo_fd ctl_fd; /*!< osmo-fd wrapped unix domain (CTL) socket to @osmo-e1d@ */
osmo_e1dp_event_cb_t event_cb;
/*!< 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_l2(msgb), msgb_l2len(msgb));
return 0;
}
@ -455,3 +463,12 @@ osmo_e1dp_client_ts_open_force(struct osmo_e1dp_client *clnt,
{
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;
}

View File

@ -260,3 +260,35 @@ osmo_e1dp_server_destroy(struct osmo_e1dp_server *srv)
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);
}