9 #include <linux/input.h>
11 #include "tapi-ioctl.h"
12 #include "tapi-device.h"
13 #include "tapi-port.h"
18 static void tapi_port_event_dispatch(struct tapi_port *port,
19 struct tapi_event *event)
21 struct tapi_port_event_listener *l;
23 list_for_each_entry(l, &port->event_listeners, head) {
24 l->callback(port, event, l->data);
28 static bool tapi_port_input_event(int events, void *data)
30 struct tapi_port *port = data;
31 struct input_event event;
32 struct tapi_event tapi_event;
35 ret = read(port->input_fd, &event, sizeof(event));
37 fprintf(stderr, "Port %d failed to read from input device: %d\n",
46 case KEY_NUMERIC_0 ... KEY_NUMERIC_9:
47 tapi_event.type = TAPI_EVENT_TYPE_DTMF;
48 tapi_event.dtmf.code = event.code - KEY_NUMERIC_0;
50 case KEY_NUMERIC_POUND:
51 tapi_event.type = TAPI_EVENT_TYPE_DTMF;
52 tapi_event.dtmf.code = 10;
54 case KEY_NUMERIC_STAR:
55 tapi_event.type = TAPI_EVENT_TYPE_DTMF;
56 tapi_event.dtmf.code = 11;
59 tapi_event.type = TAPI_EVENT_TYPE_HOOK;
60 tapi_event.hook.on = true;
63 tapi_event.type = TAPI_EVENT_TYPE_HOOK;
64 tapi_event.hook.on = false;
70 if (tapi_event.type == TAPI_EVENT_TYPE_DTMF)
71 tapi_event.dtmf.time = event.time;
73 tapi_port_event_dispatch(port, &tapi_event);
78 int tapi_port_open(struct tapi_device *dev, unsigned int id, struct tapi_port *port)
85 snprintf(path, 100, "/dev/tapi%uP%u", dev->id, id);
86 port->fd = open(path, 0);
88 printf("Failed to open %s: %d\n", path, errno);
92 snprintf(path, 100, "/dev/event%u", id);
93 port->input_fd = open(path, O_RDONLY);
94 if (port->input_fd < 0) {
95 printf("Failed to open %s: %d\n", path, errno);
99 port->ep = ioctl(port->fd, TAPI_PORT_IOCTL_GET_ENDPOINT, 0);
101 INIT_LIST_HEAD(&port->event_listeners);
103 port->input_cb.callback = tapi_port_input_event;
104 port->input_cb.data = port;
106 return event_register(port->input_fd, EPOLLIN,
110 int tapi_port_set_ring(struct tapi_port *port, bool ring)
112 return ioctl(port->fd, TAPI_PORT_IOCTL_SET_RING, ring);
115 int tapi_port_register_event(struct tapi_port *port,
116 struct tapi_port_event_listener *cb)
118 list_add_tail(&cb->head, &port->event_listeners);
122 void tapi_port_unregister_event(struct tapi_port *port,
123 struct tapi_port_event_listener *cb)