aboutsummaryrefslogtreecommitdiff
path: root/lib/link/seven/server.c
blob: a4b9666f2f26234ac4f3172c594f054f4e85b0ea (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
/* ****************************************************************************
 * link/usage/server/seven.c -- serving a Protocol 7.00 server.
 * Copyright (C) 2016-2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
 *
 * This file is part of libcasio.
 * libcasio is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3.0 of the License,
 * or (at your option) any later version.
 *
 * libcasio is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with libcasio; if not, see <http://www.gnu.org/licenses/>.
 * ************************************************************************* */
#include "../link.h"

/* ---
 * New-style server requests and responses.
 * --- */

/**
 *	casio_seven_start_server:
 *	Become passive when active.
 *
 *	@arg	handle		the link handle.
 *	@return				the error code (0 if ok).
 */

int CASIO_EXPORT casio_seven_start_server(casio_link_t *handle)
{
	int err;

	/* Make checks. */

	if (!handle)
		return (casio_error_init);
	if (handle->casio_link_flags & casio_linkflag_ended)
		return (casio_error_init);

	/* If active, we just want to send a swap and become passive. */

	if (handle->casio_link_flags & casio_linkflag_active) {
		if ((err = casio_seven_send_swp(handle)))
			return (err);
		return (0);
	}

	/* We are passive, so we want to wait for the initial check from
	 * the counterpart and ACK it. If the received packet is not an
	 * initial check, we don't respond, just like the calculator.
	 *
	 * TODO: and if the start packet is in the middle of a packet? */

	do {
		if ((err = casio_seven_receive(handle, 0)))
			return (err);
	} while (response.casio_seven_packet_type != casio_seven_type_chk
	 || !response.casio_seven_packet_initial);

	err = casio_seven_send_ack(handle, 1);
	if (err) return (err);

	return (0);
}

/**
 *	casio_seven_get_next_request:
 *	Get the next request if server.
 *
 *	@arg	handle		the link handle.
 *	@return				the error.
 */

int CASIO_EXPORT casio_seven_get_next_request(casio_link_t *handle)
{
	int err;

	/* TODO: check if the last action was receive, and if it is not
	 * the case, receive? */

	if (response.casio_seven_packet_type != casio_seven_type_cmd) {
		if (response.casio_seven_packet_type == casio_seven_type_end)
			return (casio_seven_send_ack(handle, 0));
		if (response.casio_seven_packet_type == casio_seven_type_swp)
			return (casio_error_iter);
		if ((err = casio_seven_send_err(handle, casio_seven_err_other)))
			return (err);
	}

	return (0);
}

/* ---
 * Old-style server using the new-style utilities.
 * --- */

/**
 *	casio_seven_serve:
 *	Take control of the execution thread to make a passive server.
 *
 *	@arg	handle		the link handle.
 *	@arg	callbacks	the callbacks.
 *	@arg	cookie		i suppose that's just the way that cookie crumbles.
 *	@return				the error code (0 if ok).
 */

int CASIO_EXPORT casio_seven_serve(casio_link_t *handle,
	casio_seven_server_func_t * const *callbacks, void *cookie)
{
	int err;

	if ((err = casio_seven_start_server(handle)) && err != casio_error_active)
		return (err);
	while (1) {
		/* Check if the next element is a command. */

		switch (casio_seven_get_next_request(handle)) {
		case 0:
			break;
		case casio_error_iter:
			return (0);
		default:
			return (err);
		}

		/* Check if the callback exists. */

		if (!callbacks[response.casio_seven_packet_code]) {
			err = casio_seven_send_err(handle, casio_seven_err_other);
			if (err)
				return (err);
			continue;
		}

		/* Call the callback. */

		err = (*callbacks[response.casio_seven_packet_code])(cookie, handle);
		if (err) {
			if (err != casio_error_unknown)
				return (err);

			err = casio_seven_send_err(handle, casio_seven_err_other);
			if (err)
				return (err);
			continue;
		}
	}

	return (0);
}