]> sigrok.org Git - libsigrok.git/blame_incremental - src/hardware/mic-985xx/protocol.c
mic-985xx: Use software limit helpers
[libsigrok.git] / src / hardware / mic-985xx / protocol.c
... / ...
CommitLineData
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
24static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
25{
26 int ret;
27
28 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd),
29 serial_timeout(serial, strlen(cmd)))) < 0) {
30 sr_err("Error sending '%s' command: %d.", cmd, ret);
31 return SR_ERR;
32 }
33
34 return SR_OK;
35}
36
37SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial)
38{
39 return mic_send(serial, "I\r");
40}
41
42static int mic_cmd_set_realtime_mode(struct sr_serial_dev_inst *serial)
43{
44 return mic_send(serial, "S 1 M 2 32 3\r");
45}
46
47SR_PRIV gboolean packet_valid_temp(const uint8_t *buf)
48{
49 if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != '\r')
50 return FALSE;
51
52 if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
53 return FALSE;
54
55 return TRUE;
56}
57
58SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
59{
60 if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != ' ' || buf[9] != '\r')
61 return FALSE;
62
63 if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
64 return FALSE;
65
66 if (!isdigit(buf[6]) || !isdigit(buf[7]) || !isdigit(buf[8]))
67 return FALSE;
68
69 return TRUE;
70}
71
72static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
73{
74 char tmp[4];
75
76 /* Packet format MIC98581: "v ttt\r". */
77 /* Packet format MIC98583: "v ttt hhh\r". */
78
79 /* TODO: Sanity check on buf. For now we assume well-formed ASCII. */
80
81 tmp[3] = '\0';
82
83 strncpy((char *)&tmp, &buf[2], 3);
84 *temp = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
85
86 if (mic_devs[idx].has_humidity) {
87 strncpy((char *)&tmp, &buf[6], 3);
88 *humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
89 }
90
91 return SR_OK;
92}
93
94static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
95{
96 float temperature, humidity;
97 struct sr_datafeed_packet packet;
98 struct sr_datafeed_analog analog;
99 struct sr_analog_encoding encoding;
100 struct sr_analog_meaning meaning;
101 struct sr_analog_spec spec;
102 struct dev_context *devc;
103 GSList *l;
104 int ret;
105
106 (void)idx;
107
108 devc = sdi->priv;
109
110 ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
111 if (ret < 0) {
112 sr_err("Failed to parse packet.");
113 return SR_ERR;
114 }
115
116 sr_analog_init(&analog, &encoding, &meaning, &spec, 3);
117
118 /* Common values for both channels. */
119 packet.type = SR_DF_ANALOG;
120 packet.payload = &analog;
121 analog.num_samples = 1;
122
123 /* Temperature. */
124 l = g_slist_copy(sdi->channels);
125 l = g_slist_remove_link(l, g_slist_nth(l, 1));
126 meaning.channels = l;
127 meaning.mq = SR_MQ_TEMPERATURE;
128 meaning.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
129 analog.data = &temperature;
130 sr_session_send(sdi, &packet);
131 g_slist_free(l);
132
133 /* Humidity. */
134 if (mic_devs[idx].has_humidity) {
135 l = g_slist_copy(sdi->channels);
136 l = g_slist_remove_link(l, g_slist_nth(l, 0));
137 meaning.channels = l;
138 meaning.mq = SR_MQ_RELATIVE_HUMIDITY;
139 meaning.unit = SR_UNIT_PERCENTAGE;
140 analog.data = &humidity;
141 sr_session_send(sdi, &packet);
142 g_slist_free(l);
143 }
144
145 sr_sw_limits_update_samples_read(&devc->limits, 1);
146
147 return SR_OK;
148}
149
150static void handle_new_data(struct sr_dev_inst *sdi, int idx)
151{
152 struct dev_context *devc;
153 struct sr_serial_dev_inst *serial;
154 int len, i, offset = 0;
155
156 devc = sdi->priv;
157 serial = sdi->conn;
158
159 /* Try to get as much data as the buffer can hold. */
160 len = SERIAL_BUFSIZE - devc->buflen;
161 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
162 if (len < 1) {
163 sr_err("Serial port read error: %d.", len);
164 return;
165 }
166
167 devc->buflen += len;
168
169 /* Now look for packets in that data. */
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 for (i = 0; i < devc->buflen - offset; i++)
181 devc->buf[i] = devc->buf[offset + i];
182 devc->buflen -= offset;
183}
184
185static 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 sdi->driver->dev_acquisition_stop(sdi);
215
216 return TRUE;
217}
218
219#define RECEIVE_DATA(ID_UPPER) \
220SR_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 */
224RECEIVE_DATA(MIC_98581)
225RECEIVE_DATA(MIC_98583)