2 * This file is part of the libjaylink project.
4 * Copyright (C) 2015-2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "libjaylink.h"
24 #include "libjaylink-internal.h"
29 * Emulator communication (EMUCOM).
33 #define CMD_EMUCOM 0xee
35 #define EMUCOM_CMD_READ 0x00
36 #define EMUCOM_CMD_WRITE 0x01
38 /** Bitmask for the error indication bit of an EMUCOM status code. */
39 #define EMUCOM_ERR 0x80000000
41 /** Error code indicating that the channel is not supported by the device. */
42 #define EMUCOM_ERR_NOT_SUPPORTED 0x80000001
45 * Error code indicating that the channel is not available for the requested
46 * number of bytes to be read.
48 * The number of bytes available on this channel is encoded in the lower
49 * 24 bits of the EMUCOM status code.
51 * @see EMUCOM_AVAILABLE_BYTES_MASK
53 #define EMUCOM_ERR_NOT_AVAILABLE 0x81000000
56 * Bitmask to extract the number of available bytes on a channel from an EMUCOM
59 #define EMUCOM_AVAILABLE_BYTES_MASK 0x00ffffff
63 * Read from an EMUCOM channel.
65 * @note This function must only be used if the device has the
66 * #JAYLINK_DEV_CAP_EMUCOM capability.
68 * @param[in,out] devh Device handle.
69 * @param[in] channel Channel to read data from.
70 * @param[out] buffer Buffer to store read data on success. Its content is
71 * undefined on failure.
72 * @param[in,out] length Number of bytes to read. On success, the value gets
73 * updated with the actual number of bytes read. Unless
74 * otherwise specified, the value is undefined on
77 * @retval JAYLINK_OK Success.
78 * @retval JAYLINK_ERR_ARG Invalid arguments.
79 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
80 * @retval JAYLINK_ERR_PROTO Protocol violation.
81 * @retval JAYLINK_ERR_IO Input/output error.
82 * @retval JAYLINK_ERR_DEV_NOT_SUPPORTED Channel is not supported by the
84 * @retval JAYLINK_ERR_DEV_NOT_AVAILABLE Channel is not available for the
85 * requested amount of data. @p length is
86 * updated with the number of bytes
87 * available on this channel.
88 * @retval JAYLINK_ERR_DEV Unspecified device error.
89 * @retval JAYLINK_ERR Other error conditions.
93 JAYLINK_API
int jaylink_emucom_read(struct jaylink_device_handle
*devh
,
94 uint32_t channel
, uint8_t *buffer
, uint32_t *length
)
97 struct jaylink_context
*ctx
;
101 if (!devh
|| !buffer
|| !length
)
102 return JAYLINK_ERR_ARG
;
104 ctx
= devh
->dev
->ctx
;
105 ret
= transport_start_write_read(devh
, 10, 4, true);
107 if (ret
!= JAYLINK_OK
) {
108 log_err(ctx
, "transport_start_write_read() failed: %s.",
109 jaylink_strerror(ret
));
114 buf
[1] = EMUCOM_CMD_READ
;
116 buffer_set_u32(buf
, channel
, 2);
117 buffer_set_u32(buf
, *length
, 6);
119 ret
= transport_write(devh
, buf
, 10);
121 if (ret
!= JAYLINK_OK
) {
122 log_err(ctx
, "transport_write() failed: %s.",
123 jaylink_strerror(ret
));
127 ret
= transport_read(devh
, buf
, 4);
129 if (ret
!= JAYLINK_OK
) {
130 log_err(ctx
, "transport_read() failed: %s.",
131 jaylink_strerror(ret
));
135 tmp
= buffer_get_u32(buf
, 0);
137 if (tmp
== EMUCOM_ERR_NOT_SUPPORTED
)
138 return JAYLINK_ERR_DEV_NOT_SUPPORTED
;
140 if ((tmp
& ~EMUCOM_AVAILABLE_BYTES_MASK
) == EMUCOM_ERR_NOT_AVAILABLE
) {
141 *length
= tmp
& EMUCOM_AVAILABLE_BYTES_MASK
;
142 return JAYLINK_ERR_DEV_NOT_AVAILABLE
;
145 if (tmp
& EMUCOM_ERR
) {
146 log_err(ctx
, "Failed to read from channel 0x%x: 0x%x.",
148 return JAYLINK_ERR_DEV
;
152 log_err(ctx
, "Requested at most %u bytes but device "
153 "returned %u bytes.", *length
, tmp
);
154 return JAYLINK_ERR_PROTO
;
162 ret
= transport_start_read(devh
, tmp
);
164 if (ret
!= JAYLINK_OK
) {
165 log_err(ctx
, "transport_start_read() failed: %s.",
166 jaylink_strerror(ret
));
170 ret
= transport_read(devh
, buffer
, tmp
);
172 if (ret
!= JAYLINK_OK
) {
173 log_err(ctx
, "transport_read() failed: %s.",
174 jaylink_strerror(ret
));
182 * Write to an EMUCOM channel.
184 * @note This function must only be used if the device has the
185 * #JAYLINK_DEV_CAP_EMUCOM capability.
187 * @param[in,out] devh Device handle.
188 * @param[in] channel Channel to write data to.
189 * @param[in] buffer Buffer to write data from.
190 * @param[in,out] length Number of bytes to write. On success, the value gets
191 * updated with the actual number of bytes written. The
192 * value is undefined on failure.
194 * @retval JAYLINK_OK Success.
195 * @retval JAYLINK_ERR_ARG Invalid arguments.
196 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
197 * @retval JAYLINK_ERR_PROTO Protocol violation.
198 * @retval JAYLINK_ERR_IO Input/output error.
199 * @retval JAYLINK_ERR_DEV_NOT_SUPPORTED Channel is not supported by the
201 * @retval JAYLINK_ERR_DEV Unspecified device error.
202 * @retval JAYLINK_ERR Other error conditions.
206 JAYLINK_API
int jaylink_emucom_write(struct jaylink_device_handle
*devh
,
207 uint32_t channel
, const uint8_t *buffer
, uint32_t *length
)
210 struct jaylink_context
*ctx
;
214 if (!devh
|| !buffer
|| !length
)
215 return JAYLINK_ERR_ARG
;
218 return JAYLINK_ERR_ARG
;
220 ctx
= devh
->dev
->ctx
;
221 ret
= transport_start_write(devh
, 10, true);
223 if (ret
!= JAYLINK_OK
) {
224 log_err(ctx
, "transport_start_write() failed: %s.",
225 jaylink_strerror(ret
));
230 buf
[1] = EMUCOM_CMD_WRITE
;
232 buffer_set_u32(buf
, channel
, 2);
233 buffer_set_u32(buf
, *length
, 6);
235 ret
= transport_write(devh
, buf
, 10);
237 if (ret
!= JAYLINK_OK
) {
238 log_err(ctx
, "transport_write() failed: %s.",
239 jaylink_strerror(ret
));
243 ret
= transport_start_write_read(devh
, *length
, 4, false);
245 if (ret
!= JAYLINK_OK
) {
246 log_err(ctx
, "transport_start_write_read() failed: %s.",
247 jaylink_strerror(ret
));
251 ret
= transport_write(devh
, buffer
, *length
);
253 if (ret
!= JAYLINK_OK
) {
254 log_err(ctx
, "transport_write() failed: %s.",
255 jaylink_strerror(ret
));
259 ret
= transport_read(devh
, buf
, 4);
261 if (ret
!= JAYLINK_OK
) {
262 log_err(ctx
, "transport_read() failed: %s.",
263 jaylink_strerror(ret
));
267 tmp
= buffer_get_u32(buf
, 0);
269 if (tmp
== EMUCOM_ERR_NOT_SUPPORTED
)
270 return JAYLINK_ERR_DEV_NOT_SUPPORTED
;
272 if (tmp
& EMUCOM_ERR
) {
273 log_err(ctx
, "Failed to write to channel 0x%x: 0x%x.",
275 return JAYLINK_ERR_DEV
;
279 log_err(ctx
, "Only %u bytes were supposed to be written, but "
280 "the device reported %u written bytes.", *length
, tmp
);
281 return JAYLINK_ERR_PROTO
;