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