]> sigrok.org Git - libsigrok.git/blob - src/hardware/mic-985xx/protocol.c
scpi-pps: don't break SCPI devices when scanning for HP-IB devices
[libsigrok.git] / src / hardware / mic-985xx / 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 #include <config.h>
21 #include "protocol.h"
22
23 static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
24 {
25         int ret;
26
27         if ((ret = serial_write_blocking(serial, cmd, strlen(cmd),
28                         serial_timeout(serial, strlen(cmd)))) < 0) {
29                 sr_err("Error sending '%s' command: %d.", cmd, ret);
30                 return SR_ERR;
31         }
32
33         return SR_OK;
34 }
35
36 SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial)
37 {
38         return mic_send(serial, "I\r");
39 }
40
41 static int mic_cmd_set_realtime_mode(struct sr_serial_dev_inst *serial)
42 {
43         return mic_send(serial, "S 1 M 2 32 3\r");
44 }
45
46 SR_PRIV gboolean packet_valid_temp(const uint8_t *buf)
47 {
48         if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != '\r')
49                 return FALSE;
50
51         if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
52                 return FALSE;
53
54         return TRUE;
55 }
56
57 SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
58 {
59         if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != ' ' || buf[9] != '\r')
60                 return FALSE;
61
62         if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
63                 return FALSE;
64
65         if (!isdigit(buf[6]) || !isdigit(buf[7]) || !isdigit(buf[8]))
66                 return FALSE;
67
68         return TRUE;
69 }
70
71 static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
72 {
73         char tmp[4];
74
75         /* Packet format MIC98581: "v ttt\r". */
76         /* Packet format MIC98583: "v ttt hhh\r". */
77
78         /* TODO: Sanity check on buf. For now we assume well-formed ASCII. */
79
80         tmp[3] = '\0';
81
82         strncpy((char *)&tmp, &buf[2], 3);
83         *temp = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
84
85         if (mic_devs[idx].has_humidity) {
86                 strncpy((char *)&tmp, &buf[6], 3);
87                 *humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
88         }
89
90         return SR_OK;
91 }
92
93 static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
94 {
95         float temperature, humidity;
96         struct sr_datafeed_packet packet;
97         struct sr_datafeed_analog analog;
98         struct sr_analog_encoding encoding;
99         struct sr_analog_meaning meaning;
100         struct sr_analog_spec spec;
101         struct dev_context *devc;
102         GSList *l;
103         int ret;
104
105         (void)idx;
106
107         devc = sdi->priv;
108
109         ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
110         if (ret < 0) {
111                 sr_err("Failed to parse packet.");
112                 return SR_ERR;
113         }
114
115         sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
116
117         /* Common values for both channels. */
118         packet.type = SR_DF_ANALOG;
119         packet.payload = &analog;
120         analog.num_samples = 1;
121
122         /* Temperature. */
123         l = g_slist_copy(sdi->channels);
124         l = g_slist_remove_link(l, g_slist_nth(l, 1));
125         meaning.channels = l;
126         meaning.mq = SR_MQ_TEMPERATURE;
127         meaning.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
128         analog.data = &temperature;
129         sr_session_send(sdi, &packet);
130         g_slist_free(l);
131
132         /* Humidity. */
133         if (mic_devs[idx].has_humidity) {
134                 l = g_slist_copy(sdi->channels);
135                 l = g_slist_remove_link(l, g_slist_nth(l, 0));
136                 meaning.channels = l;
137                 meaning.mq = SR_MQ_RELATIVE_HUMIDITY;
138                 meaning.unit = SR_UNIT_PERCENTAGE;
139                 analog.data = &humidity;
140                 sr_session_send(sdi, &packet);
141                 g_slist_free(l);
142         }
143
144         sr_sw_limits_update_samples_read(&devc->limits, 1);
145
146         return SR_OK;
147 }
148
149 static void handle_new_data(struct sr_dev_inst *sdi, int idx)
150 {
151         struct dev_context *devc;
152         struct sr_serial_dev_inst *serial;
153         int len, offset;
154
155         devc = sdi->priv;
156         serial = sdi->conn;
157
158         /* Try to get as much data as the buffer can hold. */
159         len = SERIAL_BUFSIZE - devc->buflen;
160         len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
161         if (len < 1) {
162                 sr_err("Serial port read error: %d.", len);
163                 return;
164         }
165
166         devc->buflen += len;
167
168         /* Now look for packets in that data. */
169         offset = 0;
170         while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
171                 if (mic_devs[idx].packet_valid(devc->buf + offset)) {
172                         handle_packet(devc->buf + offset, sdi, idx);
173                         offset += mic_devs[idx].packet_size;
174                 } else {
175                         offset++;
176                 }
177         }
178
179         /* If we have any data left, move it to the beginning of our buffer. */
180         if (offset < devc->buflen)
181                 memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
182         devc->buflen -= offset;
183 }
184
185 static int receive_data(int fd, int revents, int idx, void *cb_data)
186 {
187         struct sr_dev_inst *sdi;
188         struct dev_context *devc;
189         static gboolean first_time = TRUE;
190         struct sr_serial_dev_inst *serial;
191
192         (void)fd;
193
194         if (!(sdi = cb_data))
195                 return TRUE;
196
197         if (!(devc = sdi->priv))
198                 return TRUE;
199
200         serial = sdi->conn;
201
202         if (revents == G_IO_IN) {
203                 /* New data arrived. */
204                 handle_new_data(sdi, idx);
205         } else {
206                 /* Timeout. */
207                 if (first_time) {
208                         mic_cmd_set_realtime_mode(serial);
209                         first_time = FALSE;
210                 }
211         }
212
213         if (sr_sw_limits_check(&devc->limits))
214                 sr_dev_acquisition_stop(sdi);
215
216         return TRUE;
217 }
218
219 #define RECEIVE_DATA(ID_UPPER) \
220 SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
221         return receive_data(fd, revents, ID_UPPER, cb_data); }
222
223 /* Driver-specific receive_data() wrappers */
224 RECEIVE_DATA(MIC_98581)
225 RECEIVE_DATA(MIC_98583)