summaryrefslogtreecommitdiff
path: root/package/tapi_sip/src/tapi_agent.c
blob: 93c73c24d8d57c23a9132b481914bbff93d01cbc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

#include <stdbool.h>
#include <stdio.h>
#include <tapi-port.h>

#include "session.h"
#include "agent.h"
#include "tapi_agent.h"

static int tapi_agent_invite(struct agent *agent, struct session *session)
{
	struct tapi_agent *tagent = agent_to_tapi_agent(agent);

	if (tagent->session)
		return -1;

	tagent->state = TAPI_AGENT_STATE_RINGING;
	tapi_port_set_ring(&tagent->port, true);

	tagent->session = session;

	return 0;
}

static int tapi_agent_accept(struct agent *agent, struct session *session)
{
	return 0;
}

static int tapi_agent_hangup(struct agent *agent, struct session *session)
{
	struct tapi_agent *tagent = agent_to_tapi_agent(agent);

	switch (tagent->state) {
	case TAPI_AGENT_STATE_RINGING:
		tapi_port_set_ring(&tagent->port, false);
		break;
	default:
		break;
	}

	tagent->state = TAPI_AGENT_STATE_IDLE;
	tagent->session = NULL;

	return 0;
}

static int tapi_agent_get_endpoint(struct agent *agent, struct session *session)
{
	struct tapi_agent *tagent = agent_to_tapi_agent(agent);
	return tapi_port_get_endpoint(&tagent->port);
}

static const struct agent_ops tapi_agent_ops = {
	.invite = tapi_agent_invite,
	.accept = tapi_agent_accept,
	.hangup = tapi_agent_hangup,
	.get_endpoint = tapi_agent_get_endpoint,
};

static void tapi_agent_event(struct tapi_port *port, struct tapi_event *event,
	void *data)
{
	struct tapi_agent *tagent = data;

	if (event->type != TAPI_EVENT_TYPE_HOOK)
		return;

	if (!tagent->session)
		return;

	if (event->hook.on) {
		session_hangup(tagent->session, &tagent->agent);
		tagent->state = TAPI_AGENT_STATE_IDLE;
		tagent->session = NULL;
	} else {
		session_accept(tagent->session, &tagent->agent);
		tagent->state = TAPI_AGENT_STATE_ACTIVE;
	}
}

void tapi_agent_init(struct tapi_device *tdev, int port, struct tapi_agent *tagent)
{
	int ret;

	tagent->agent.ops = &tapi_agent_ops;
	tagent->state = TAPI_AGENT_STATE_IDLE;
	tagent->session = NULL;

	ret = tapi_port_open(tdev, port, &tagent->port);
	if (ret) {
		printf("Failed to open tapi port %d: %d\n", port, ret);
		return;
	}

	tagent->event_listener.callback = tapi_agent_event;
	tagent->event_listener.data = tagent;

	tapi_port_register_event(&tagent->port, &tagent->event_listener);
}

void tapi_agent_free(struct tapi_agent *tagent)
{
	tapi_port_unregister_event(&tagent->port, &tagent->event_listener);
}