Fix copy-paste error in error messages.
[libjaylink.git] / libjaylink / emucom.c
blob605821cdca0d46082dd3195844648caf9eb7edb4
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2015 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>
22 #include "libjaylink.h"
23 #include "libjaylink-internal.h"
25 /**
26 * @file
28 * Emulator communication (EMUCOM).
31 /** @cond PRIVATE */
32 #define CMD_EMUCOM 0xee
34 #define EMUCOM_CMD_WRITE 0x01
36 /**
37 * Error code indicating that the EMUCOM channel is not supported by the
38 * device.
40 #define EMUCOM_ERR_NOT_SUPPORTED 0x80000001
41 /** @endcond */
43 /**
44 * Write to an EMUCOM channel.
46 * @note This function must only be used if the device has the
47 * #JAYLINK_DEV_CAP_EMUCOM capability.
49 * @param[in,out] devh Device handle.
50 * @param[in] channel Channel to write data to.
51 * @param[in] buffer Buffer to write data from.
52 * @param[in,out] length Number of bytes to write. On success, the value gets
53 * updated with the actual number of bytes written. The
54 * value is undefined on failure.
56 * @retval JAYLINK_OK Success.
57 * @retval JAYLINK_ERR_ARG Invalid arguments.
58 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
59 * @retval JAYLINK_ERR_PROTO Protocol violation.
60 * @retval JAYLINK_ERR_DEV Unspecified device error.
61 * @retval JAYLINK_ERR_DEV_NOT_SUPPORTED Channel is not supported by the device.
62 * @retval JAYLINK_ERR Other error conditions.
64 JAYLINK_API int jaylink_emucom_write(struct jaylink_device_handle *devh,
65 uint32_t channel, const uint8_t *buffer, uint32_t *length)
67 int ret;
68 struct jaylink_context *ctx;
69 uint8_t buf[10];
70 uint32_t tmp;
71 int32_t dummy;
73 if (!devh || !buffer || !length)
74 return JAYLINK_ERR_ARG;
76 if (!*length)
77 return JAYLINK_ERR_ARG;
79 ctx = devh->dev->ctx;
80 ret = transport_start_write(devh, 10, 1);
82 if (ret != JAYLINK_OK) {
83 log_err(ctx, "transport_start_write() failed: %i.", ret);
84 return ret;
87 buf[0] = CMD_EMUCOM;
88 buf[1] = EMUCOM_CMD_WRITE;
90 buffer_set_u32(buf, channel, 2);
91 buffer_set_u32(buf, *length, 6);
93 ret = transport_write(devh, buf, 10);
95 if (ret != JAYLINK_OK) {
96 log_err(ctx, "transport_write() failed: %i.", ret);
97 return ret;
100 ret = transport_start_write_read(devh, *length, 4, 0);
102 if (ret != JAYLINK_OK) {
103 log_err(ctx, "transport_start_write_read() failed: %i.", ret);
104 return ret;
107 ret = transport_write(devh, buffer, *length);
109 if (ret != JAYLINK_OK) {
110 log_err(ctx, "transport_write() failed: %i.", ret);
111 return ret;
114 ret = transport_read(devh, buf, 4);
116 if (ret != JAYLINK_OK) {
117 log_err(ctx, "transport_read() failed: %i.", ret);
118 return ret;
121 tmp = buffer_get_u32(buf, 0);
123 if (tmp == EMUCOM_ERR_NOT_SUPPORTED) {
124 log_err(ctx, "Channel 0x%x is not supported by the device.",
125 channel);
126 return JAYLINK_ERR_DEV_NOT_SUPPORTED;
129 dummy = tmp;
131 if (dummy < 0) {
132 log_err(ctx, "Failed to write to channel 0x%x.", channel);
133 return JAYLINK_ERR_DEV;
136 if (tmp > *length) {
137 log_err(ctx, "Only %u bytes were supposed to be written, but "
138 "the device reported %u written bytes.", *length, tmp);
139 return JAYLINK_ERR_PROTO;
142 *length = tmp;
144 return JAYLINK_OK;