discovery/usb: Add product ID (PID) 0x1020
[libjaylink.git] / libjaylink / emucom.c
blob035cb99534505d61a0ec60b3cc57de1f46a83f87
1 /*
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/>.
20 #include <stdint.h>
21 #include <stdbool.h>
23 #include "libjaylink.h"
24 #include "libjaylink-internal.h"
26 /**
27 * @file
29 * Emulator communication (EMUCOM).
32 /** @cond PRIVATE */
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
44 /**
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
55 /**
56 * Bitmask to extract the number of available bytes on a channel from an EMUCOM
57 * status code.
59 #define EMUCOM_AVAILABLE_BYTES_MASK 0x00ffffff
60 /** @endcond */
62 /**
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
75 * failure.
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
83 * device.
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.
91 * @since 0.1.0
93 JAYLINK_API int jaylink_emucom_read(struct jaylink_device_handle *devh,
94 uint32_t channel, uint8_t *buffer, uint32_t *length)
96 int ret;
97 struct jaylink_context *ctx;
98 uint8_t buf[10];
99 uint32_t tmp;
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));
110 return ret;
113 buf[0] = CMD_EMUCOM;
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));
124 return 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));
132 return 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.",
147 channel, tmp);
148 return JAYLINK_ERR_DEV;
151 if (tmp > *length) {
152 log_err(ctx, "Requested at most %u bytes but device "
153 "returned %u bytes.", *length, tmp);
154 return JAYLINK_ERR_PROTO;
157 *length = tmp;
159 if (!tmp)
160 return JAYLINK_OK;
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));
167 return 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));
175 return ret;
178 return JAYLINK_OK;
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
200 * device.
201 * @retval JAYLINK_ERR_DEV Unspecified device error.
202 * @retval JAYLINK_ERR Other error conditions.
204 * @since 0.1.0
206 JAYLINK_API int jaylink_emucom_write(struct jaylink_device_handle *devh,
207 uint32_t channel, const uint8_t *buffer, uint32_t *length)
209 int ret;
210 struct jaylink_context *ctx;
211 uint8_t buf[10];
212 uint32_t tmp;
214 if (!devh || !buffer || !length)
215 return JAYLINK_ERR_ARG;
217 if (!*length)
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));
226 return ret;
229 buf[0] = CMD_EMUCOM;
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));
240 return 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));
248 return 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));
256 return 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));
264 return 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.",
274 channel, tmp);
275 return JAYLINK_ERR_DEV;
278 if (tmp > *length) {
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;
284 *length = tmp;
286 return JAYLINK_OK;