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