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_DEV Unspecified device 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 Other error conditions.
92 JAYLINK_API
int jaylink_emucom_read(struct jaylink_device_handle
*devh
,
93 uint32_t channel
, uint8_t *buffer
, uint32_t *length
)
96 struct jaylink_context
*ctx
;
100 if (!devh
|| !buffer
|| !length
)
101 return JAYLINK_ERR_ARG
;
103 ctx
= devh
->dev
->ctx
;
104 ret
= transport_start_write_read(devh
, 10, 4, true);
106 if (ret
!= JAYLINK_OK
) {
107 log_err(ctx
, "transport_start_write_read() failed: %s.",
108 jaylink_strerror(ret
));
113 buf
[1] = EMUCOM_CMD_READ
;
115 buffer_set_u32(buf
, channel
, 2);
116 buffer_set_u32(buf
, *length
, 6);
118 ret
= transport_write(devh
, buf
, 10);
120 if (ret
!= JAYLINK_OK
) {
121 log_err(ctx
, "transport_write() failed: %s.",
122 jaylink_strerror(ret
));
126 ret
= transport_read(devh
, buf
, 4);
128 if (ret
!= JAYLINK_OK
) {
129 log_err(ctx
, "transport_read() failed: %s.",
130 jaylink_strerror(ret
));
134 tmp
= buffer_get_u32(buf
, 0);
136 if (tmp
== EMUCOM_ERR_NOT_SUPPORTED
)
137 return JAYLINK_ERR_DEV_NOT_SUPPORTED
;
139 if ((tmp
& ~EMUCOM_AVAILABLE_BYTES_MASK
) == EMUCOM_ERR_NOT_AVAILABLE
) {
140 *length
= tmp
& EMUCOM_AVAILABLE_BYTES_MASK
;
141 return JAYLINK_ERR_DEV_NOT_AVAILABLE
;
144 if (tmp
& EMUCOM_ERR
) {
145 log_err(ctx
, "Failed to read from channel 0x%x.", channel
);
146 return JAYLINK_ERR_DEV
;
150 log_err(ctx
, "Requested at most %u bytes but device "
151 "returned %u bytes.", *length
, tmp
);
152 return JAYLINK_ERR_PROTO
;
160 ret
= transport_start_read(devh
, tmp
);
162 if (ret
!= JAYLINK_OK
) {
163 log_err(ctx
, "transport_start_read() failed: %s.",
164 jaylink_strerror(ret
));
168 ret
= transport_read(devh
, buffer
, tmp
);
170 if (ret
!= JAYLINK_OK
) {
171 log_err(ctx
, "transport_read() failed: %s.",
172 jaylink_strerror(ret
));
180 * Write to an EMUCOM channel.
182 * @note This function must only be used if the device has the
183 * #JAYLINK_DEV_CAP_EMUCOM capability.
185 * @param[in,out] devh Device handle.
186 * @param[in] channel Channel to write data to.
187 * @param[in] buffer Buffer to write data from.
188 * @param[in,out] length Number of bytes to write. On success, the value gets
189 * updated with the actual number of bytes written. The
190 * value is undefined on failure.
192 * @retval JAYLINK_OK Success.
193 * @retval JAYLINK_ERR_ARG Invalid arguments.
194 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
195 * @retval JAYLINK_ERR_PROTO Protocol violation.
196 * @retval JAYLINK_ERR_DEV Unspecified device error.
197 * @retval JAYLINK_ERR_DEV_NOT_SUPPORTED Channel is not supported by the
199 * @retval JAYLINK_ERR Other error conditions.
203 JAYLINK_API
int jaylink_emucom_write(struct jaylink_device_handle
*devh
,
204 uint32_t channel
, const uint8_t *buffer
, uint32_t *length
)
207 struct jaylink_context
*ctx
;
211 if (!devh
|| !buffer
|| !length
)
212 return JAYLINK_ERR_ARG
;
215 return JAYLINK_ERR_ARG
;
217 ctx
= devh
->dev
->ctx
;
218 ret
= transport_start_write(devh
, 10, true);
220 if (ret
!= JAYLINK_OK
) {
221 log_err(ctx
, "transport_start_write() failed: %s.",
222 jaylink_strerror(ret
));
227 buf
[1] = EMUCOM_CMD_WRITE
;
229 buffer_set_u32(buf
, channel
, 2);
230 buffer_set_u32(buf
, *length
, 6);
232 ret
= transport_write(devh
, buf
, 10);
234 if (ret
!= JAYLINK_OK
) {
235 log_err(ctx
, "transport_write() failed: %s.",
236 jaylink_strerror(ret
));
240 ret
= transport_start_write_read(devh
, *length
, 4, false);
242 if (ret
!= JAYLINK_OK
) {
243 log_err(ctx
, "transport_start_write_read() failed: %s.",
244 jaylink_strerror(ret
));
248 ret
= transport_write(devh
, buffer
, *length
);
250 if (ret
!= JAYLINK_OK
) {
251 log_err(ctx
, "transport_write() failed: %s.",
252 jaylink_strerror(ret
));
256 ret
= transport_read(devh
, buf
, 4);
258 if (ret
!= JAYLINK_OK
) {
259 log_err(ctx
, "transport_read() failed: %s.",
260 jaylink_strerror(ret
));
264 tmp
= buffer_get_u32(buf
, 0);
266 if (tmp
== EMUCOM_ERR_NOT_SUPPORTED
)
267 return JAYLINK_ERR_DEV_NOT_SUPPORTED
;
269 if (tmp
& EMUCOM_ERR
) {
270 log_err(ctx
, "Failed to write to channel 0x%x.", channel
);
271 return JAYLINK_ERR_DEV
;
275 log_err(ctx
, "Only %u bytes were supposed to be written, but "
276 "the device reported %u written bytes.", *length
, tmp
);
277 return JAYLINK_ERR_PROTO
;