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