* adds a rewrite of the tapi drivers + sip app. this is the result of lars' gsoc...
[openwrt/openwrt.git] / package / tapi_sip / src / tapi_agent.c
1
2 #include <stdbool.h>
3 #include <stdio.h>
4 #include <tapi-port.h>
5
6 #include "session.h"
7 #include "agent.h"
8 #include "tapi_agent.h"
9
10 static int tapi_agent_invite(struct agent *agent, struct session *session)
11 {
12 struct tapi_agent *tagent = agent_to_tapi_agent(agent);
13
14 if (tagent->session)
15 return -1;
16
17 tagent->state = TAPI_AGENT_STATE_RINGING;
18 tapi_port_set_ring(&tagent->port, true);
19
20 tagent->session = session;
21
22 return 0;
23 }
24
25 static int tapi_agent_accept(struct agent *agent, struct session *session)
26 {
27 return 0;
28 }
29
30 static int tapi_agent_hangup(struct agent *agent, struct session *session)
31 {
32 struct tapi_agent *tagent = agent_to_tapi_agent(agent);
33
34 switch (tagent->state) {
35 case TAPI_AGENT_STATE_RINGING:
36 tapi_port_set_ring(&tagent->port, false);
37 break;
38 default:
39 break;
40 }
41
42 tagent->state = TAPI_AGENT_STATE_IDLE;
43 tagent->session = NULL;
44
45 return 0;
46 }
47
48 static int tapi_agent_get_endpoint(struct agent *agent, struct session *session)
49 {
50 struct tapi_agent *tagent = agent_to_tapi_agent(agent);
51 return tapi_port_get_endpoint(&tagent->port);
52 }
53
54 static const struct agent_ops tapi_agent_ops = {
55 .invite = tapi_agent_invite,
56 .accept = tapi_agent_accept,
57 .hangup = tapi_agent_hangup,
58 .get_endpoint = tapi_agent_get_endpoint,
59 };
60
61 static void tapi_agent_event(struct tapi_port *port, struct tapi_event *event,
62 void *data)
63 {
64 struct tapi_agent *tagent = data;
65
66 if (event->type != TAPI_EVENT_TYPE_HOOK)
67 return;
68
69 if (!tagent->session)
70 return;
71
72 if (event->hook.on) {
73 session_hangup(tagent->session, &tagent->agent);
74 tagent->state = TAPI_AGENT_STATE_IDLE;
75 tagent->session = NULL;
76 } else {
77 session_accept(tagent->session, &tagent->agent);
78 tagent->state = TAPI_AGENT_STATE_ACTIVE;
79 }
80 }
81
82 void tapi_agent_init(struct tapi_device *tdev, int port, struct tapi_agent *tagent)
83 {
84 int ret;
85
86 tagent->agent.ops = &tapi_agent_ops;
87 tagent->state = TAPI_AGENT_STATE_IDLE;
88 tagent->session = NULL;
89
90 ret = tapi_port_open(tdev, port, &tagent->port);
91 if (ret) {
92 printf("Failed to open tapi port %d: %d\n", port, ret);
93 return;
94 }
95
96 tagent->event_listener.callback = tapi_agent_event;
97 tagent->event_listener.data = tagent;
98
99 tapi_port_register_event(&tagent->port, &tagent->event_listener);
100 }
101
102 void tapi_agent_free(struct tapi_agent *tagent)
103 {
104 tapi_port_unregister_event(&tagent->port, &tagent->event_listener);
105 }