summaryrefslogtreecommitdiffstats
path: root/src/intf.c
blob: 8cb7e02f2fa4ccd6c21a771008ac6b0e26f8b3e1 (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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181

#include <asm/errno.h>

#include "intf.h"
#include "list.h"
#include "fsm.h"

int tsnif_debug = 0;

static struct tsnif_term *term_find(struct tsnif_handle *h, int type, int idx)
{
	struct tsnif_term *t;

	list_for_each_entry(t, &h->terms, list)
		if ((t->type == type) &&
		    (t->idx == idx))
			return t;

	return NULL;
}

static int process_notify(struct tsnif_handle *h, struct trans_msg *msg)
{
	struct tsnif_term term;

	if (msg->ack)
		return 0;

	if (!h->ops || !h->ops->cb_notify)
		return -1;

	memset(&term, 0x0, sizeof(term));
	term.type   = msg->type;
	term.idx    = msg->idx;
	term.handle = h;
	term.state  = TSNIF_INTF_STATE_NEW;

	return h->ops->cb_notify(&term, msg->cmd);
}

static int process_mgroup(struct tsnif_handle *h, struct trans_msg *msg)
{
	if (msg->ack)
		return 0;

	return trans_group(&h->trans, msg->group);
}

static int trans_cb(struct trans_handle *h, struct trans_msg *msg)
{
	struct tsnif_handle *handle;
	struct tsnif_term *term;

	TSNIF_DEBUG("got cmd %d, type %d, idx %d\n",
			msg->cmd, msg->type, msg->idx);

	handle = container_of(h, struct tsnif_handle, trans);

	/* then non term related bussines */
	switch(msg->cmd) {
	case TSNIF_CMD_TTY_CREATE:
	case TSNIF_CMD_TTY_RELEASE:
		return process_notify(handle, msg);

	case TSNIF_CMD_MGROUP:
		return process_mgroup(handle, msg);
	}

	/* and term it is now */
	term = term_find(handle, msg->type, msg->idx);
	if (!term)
		return -ENODEV;

	return fsm_process(handle, term, msg);
}

static int init_mgroup(struct tsnif_handle *h)
{
	struct trans_msg msg;

	msg.cmd = TSNIF_CMD_MGROUP;
	return trans_send(&h->trans, &msg);
}

int tsnif_init(struct tsnif_handle *h, struct tsnif_ops *ops)
{
	int err;

	if (!ops)
		return -EINVAL;

	memset(h, 0x0, sizeof(*h));
	h->ops = ops;
	INIT_LIST_HEAD(&h->terms);

	err = trans_init(&h->trans, trans_cb);
	if (err)
		return err;

	return init_mgroup(h);
}

int tsnif_close(struct tsnif_handle *h)
{
	return trans_close(&h->trans);
}

int tsnif_process(struct tsnif_handle *h)
{
	return trans_process(&h->trans);
}

int tsnif_fd(struct tsnif_handle *h)
{
	return trans_fd(&h->trans);
}

int tsnif_term_add(struct tsnif_handle *h, struct tsnif_term *term,
		   int type, int idx)
{
	struct tsnif_term *t;

	t = term_find(h, type, idx);
	if (t)
		return -EEXIST;

	memset(term, 0x0, sizeof(*term));
	term->type   = type;
	term->idx    = idx;
	term->handle = h;
	term->state  = TSNIF_INTF_STATE_NEW;

	list_add_tail(&term->list, &h->terms);
	return 0;
}

int tsnif_term_del(struct tsnif_handle *h, struct tsnif_term *term)
{
	list_del(&term->list);
	return 0;
}

int tsnif_attach(struct tsnif_term *term)
{
	struct trans_msg msg;
	struct tsnif_handle *h = term->handle;

	TSNIF_DEBUG("type %d, idx %d\n", term->type, term->idx);

	if (fsm_send(term, TSNIF_CMD_ATTACH)) {
		TSNIF_DEBUG("wrong state (%d) skiping detach\n",
			    term->state);
		return -1;
	}

	msg.type = term->type;
	msg.idx  = term->idx;
	msg.cmd  = TSNIF_CMD_ATTACH;

	return trans_send(&h->trans, &msg);
}

int tsnif_detach(struct tsnif_term *term)
{
	struct trans_msg msg;
	struct tsnif_handle *h = term->handle;

	TSNIF_DEBUG("type %d, idx %d\n", term->type, term->idx);

	if (fsm_send(term, TSNIF_CMD_DETACH)) {
		TSNIF_DEBUG("wrong state (%d) skiping detach\n",
			    term->state);
		return -1;
	}

	msg.type = term->type;
	msg.idx  = term->idx;
	msg.cmd  = TSNIF_CMD_DETACH;

	return trans_send(&h->trans, &msg);
}