]> sigrok.org Git - libsigrok.git/blame - src/hardware/center-3xx/protocol.c
center-3xx: Fix incorrect values due to endianness issue.
[libsigrok.git] / src / hardware / center-3xx / protocol.c
CommitLineData
4433145f
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5 *
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.
10 *
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.
15 *
16 * You should have received a copy of the GNU General Public License
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
4433145f
UH
18 */
19
6ec6c43b 20#include <config.h>
4433145f
UH
21#include "protocol.h"
22
07ffa5b3
UH
23#define NUM_CHANNELS 4
24
4a8bbed7 25struct center_info {
07ffa5b3 26 float temp[NUM_CHANNELS];
0a2ffa3e 27 int digits[NUM_CHANNELS];
4a8bbed7
UH
28 gboolean rec, std, max, min, maxmin, t1t2, rel, hold, lowbat, celsius;
29 gboolean memfull, autooff;
30 gboolean mode_std, mode_rel, mode_max, mode_min, mode_maxmin;
31};
32
33static int center_send(struct sr_serial_dev_inst *serial, const char *cmd)
4433145f 34{
4a8bbed7
UH
35 int ret;
36
84d328ac
UH
37 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd),
38 serial_timeout(serial, strlen(cmd)))) < 0) {
4a8bbed7
UH
39 sr_err("Error sending '%s' command: %d.", cmd, ret);
40 return SR_ERR;
41 }
42
43 return SR_OK;
44}
45
46SR_PRIV gboolean center_3xx_packet_valid(const uint8_t *buf)
47{
48 return (buf[0] == 0x02 && buf[44] == 0x03);
49}
50
51static void log_packet(const uint8_t *buf, int idx)
52{
53 int i;
54 GString *s;
55
56 s = g_string_sized_new(100);
57 g_string_printf(s, "Packet: ");
58 for (i = 0; i < center_devs[idx].packet_size; i++)
59 g_string_append_printf(s, "%02x ", buf[i]);
60 sr_spew("%s", s->str);
61 g_string_free(s, TRUE);
62}
63
64static int packet_parse(const uint8_t *buf, int idx, struct center_info *info)
65{
66 int i;
56a1bf7b 67 int16_t temp_i16;
4a8bbed7
UH
68
69 log_packet(buf, idx);
70
71 /* Byte 0: Always 0x02. */
72
73 /* Byte 1: Various status bits. */
74 info->rec = (buf[1] & (1 << 0)) != 0;
75 info->mode_std = (((buf[1] >> 1) & 0x3) == 0);
76 info->mode_max = (((buf[1] >> 1) & 0x3) == 1);
77 info->mode_min = (((buf[1] >> 1) & 0x3) == 2);
78 info->mode_maxmin = (((buf[1] >> 1) & 0x3) == 3);
79 /* TODO: Rel. Not available on all models. */
80 info->t1t2 = (buf[1] & (1 << 3)) != 0;
81 info->rel = (buf[1] & (1 << 4)) != 0;
82 info->hold = (buf[1] & (1 << 5)) != 0;
83 info->lowbat = (buf[1] & (1 << 6)) != 0;
84 info->celsius = (buf[1] & (1 << 7)) != 0;
85
86 /* Byte 2: Further status bits. */
87 info->memfull = (buf[2] & (1 << 0)) != 0;
88 info->autooff = (buf[2] & (1 << 7)) != 0;
89
90 /* Byte 7+8/9+10/11+12/13+14: channel T1/T2/T3/T4 temperature. */
07ffa5b3 91 for (i = 0; i < NUM_CHANNELS; i++) {
f0aec556 92 temp_i16 = RB16S(&buf[7 + 2 * i]);
56a1bf7b 93 info->temp[i] = (float)temp_i16;
4a8bbed7
UH
94 }
95
96 /* Byte 43: Specifies whether we need to divide the value(s) by 10. */
07ffa5b3 97 for (i = 0; i < NUM_CHANNELS; i++) {
4a8bbed7 98 /* Bit = 0: Divide by 10. Bit = 1: Don't divide by 10. */
0a2ffa3e 99 if ((buf[43] & (1 << i)) == 0) {
4a8bbed7 100 info->temp[i] /= 10;
0a2ffa3e
AJ
101 info->digits[i] = 1;
102 } else {
103 info->digits[i] = 0;
104 }
4a8bbed7
UH
105 }
106
107 /* Bytes 39-42: Overflow/overlimit bits, depending on mode. */
07ffa5b3 108 for (i = 0; i < NUM_CHANNELS; i++) {
4a8bbed7
UH
109 if (info->mode_std && ((buf[39] & (1 << i)) != 0))
110 info->temp[i] = INFINITY;
111 /* TODO: Rel. Not available on all models. */
112 // if (info->mode_rel && ((buf[40] & (1 << i)) != 0))
113 // info->temp[i] = INFINITY;
114 if (info->mode_max && ((buf[41] & (1 << i)) != 0))
115 info->temp[i] = INFINITY;
116 if (info->mode_min && ((buf[42] & (1 << i)) != 0))
117 info->temp[i] = INFINITY;
118 /* TODO: Minmax? */
119 }
120
121 /* Byte 44: Always 0x03. */
122
123 return SR_OK;
124}
125
126static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
127{
128 struct sr_datafeed_packet packet;
d4f59ce8
UH
129 struct sr_datafeed_analog analog;
130 struct sr_analog_encoding encoding;
131 struct sr_analog_meaning meaning;
132 struct sr_analog_spec spec;
4a8bbed7
UH
133 struct dev_context *devc;
134 struct center_info info;
135 GSList *l;
136 int i, ret;
137
138 devc = sdi->priv;
4433145f 139
869c8375 140 /* Note: digits/spec_digits will be overridden later. */
d4f59ce8 141 sr_analog_init(&analog, &encoding, &meaning, &spec, 0);
4a8bbed7
UH
142 memset(&info, 0, sizeof(struct center_info));
143
144 ret = packet_parse(buf, idx, &info);
145 if (ret < 0) {
146 sr_err("Failed to parse packet.");
147 return SR_ERR;
148 }
149
ba7dd8bb 150 /* Common values for all 4 channels. */
d4f59ce8 151 packet.type = SR_DF_ANALOG;
4a8bbed7 152 packet.payload = &analog;
d4f59ce8
UH
153 analog.meaning->mq = SR_MQ_TEMPERATURE;
154 analog.meaning->unit = (info.celsius) ? SR_UNIT_CELSIUS : SR_UNIT_FAHRENHEIT;
4a8bbed7
UH
155 analog.num_samples = 1;
156
157 /* Send the values for T1 - T4. */
07ffa5b3 158 for (i = 0; i < NUM_CHANNELS; i++) {
4a8bbed7 159 l = NULL;
ba7dd8bb 160 l = g_slist_append(l, g_slist_nth_data(sdi->channels, i));
d4f59ce8 161 analog.meaning->channels = l;
0a2ffa3e
AJ
162 analog.encoding->digits = info.digits[i];
163 analog.spec->spec_digits = info.digits[i];
4a8bbed7 164 analog.data = &(info.temp[i]);
695dc859 165 sr_session_send(sdi, &packet);
4a8bbed7
UH
166 g_slist_free(l);
167 }
168
838f6906 169 sr_sw_limits_update_samples_read(&devc->sw_limits, 1);
4a8bbed7
UH
170
171 return SR_OK;
172}
173
174/* Return TRUE if a full packet was parsed, FALSE otherwise. */
175static gboolean handle_new_data(struct sr_dev_inst *sdi, int idx)
176{
177 struct dev_context *devc;
178 struct sr_serial_dev_inst *serial;
0f6ff97b 179 int len, offset, ret = FALSE;
4a8bbed7
UH
180
181 devc = sdi->priv;
182 serial = sdi->conn;
183
184 /* Try to get as much data as the buffer can hold. */
185 len = SERIAL_BUFSIZE - devc->buflen;
3582ce8a 186 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
4a8bbed7
UH
187 if (len < 1) {
188 sr_err("Serial port read error: %d.", len);
189 return FALSE;
190 }
191
192 devc->buflen += len;
193
194 /* Now look for packets in that data. */
0f6ff97b 195 offset = 0;
4a8bbed7
UH
196 while ((devc->buflen - offset) >= center_devs[idx].packet_size) {
197 if (center_devs[idx].packet_valid(devc->buf + offset)) {
198 handle_packet(devc->buf + offset, sdi, idx);
199 offset += center_devs[idx].packet_size;
200 ret = TRUE;
201 } else {
202 offset++;
203 }
204 }
205
206 /* If we have any data left, move it to the beginning of our buffer. */
0f6ff97b
GS
207 if (offset < devc->buflen)
208 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
4a8bbed7
UH
209 devc->buflen -= offset;
210
211 return ret;
212}
213
214static int receive_data(int fd, int revents, int idx, void *cb_data)
215{
216 struct sr_dev_inst *sdi;
4433145f 217 struct dev_context *devc;
4a8bbed7
UH
218 static gboolean request_new_packet = TRUE;
219 struct sr_serial_dev_inst *serial;
220
221 (void)fd;
4433145f
UH
222
223 if (!(sdi = cb_data))
224 return TRUE;
225
226 if (!(devc = sdi->priv))
227 return TRUE;
228
4a8bbed7
UH
229 serial = sdi->conn;
230
4433145f 231 if (revents == G_IO_IN) {
4a8bbed7
UH
232 /* New data arrived. */
233 request_new_packet = handle_new_data(sdi, idx);
234 } else {
235 /*
236 * Timeout. Send "A" to request a packet, but then don't send
237 * further "A" commands until we received a full packet first.
238 */
239 if (request_new_packet) {
240 center_send(serial, "A");
241 request_new_packet = FALSE;
242 }
243 }
244
838f6906 245 if (sr_sw_limits_check(&devc->sw_limits))
d2f7c417 246 sr_dev_acquisition_stop(sdi);
4433145f
UH
247
248 return TRUE;
249}
4a8bbed7
UH
250
251#define RECEIVE_DATA(ID_UPPER) \
252SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
253 return receive_data(fd, revents, ID_UPPER, cb_data); }
254
255/* Driver-specific receive_data() wrappers */
256RECEIVE_DATA(CENTER_309)
257RECEIVE_DATA(VOLTCRAFT_K204)