]> sigrok.org Git - libsigrok.git/blame - src/hardware/mic-985xx/protocol.c
Add sr_dev_acquisition_stop(), factor out SR_ERR_DEV_CLOSED check.
[libsigrok.git] / src / hardware / mic-985xx / protocol.c
CommitLineData
7ec5b549
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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
7ec5b549
UH
18 */
19
6ec6c43b 20#include <config.h>
7ec5b549
UH
21#include "protocol.h"
22
0cd8e231 23static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
7ec5b549 24{
0cd8e231
UH
25 int ret;
26
5360d6d7
UH
27 if ((ret = serial_write_blocking(serial, cmd, strlen(cmd),
28 serial_timeout(serial, strlen(cmd)))) < 0) {
0cd8e231
UH
29 sr_err("Error sending '%s' command: %d.", cmd, ret);
30 return SR_ERR;
31 }
32
33 return SR_OK;
34}
35
36SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial)
37{
38 return mic_send(serial, "I\r");
39}
40
41static 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
6f3e5335
UH
46SR_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
57SR_PRIV gboolean packet_valid_temp_hum(const uint8_t *buf)
0cd8e231
UH
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
6f3e5335 71static int packet_parse(const char *buf, int idx, float *temp, float *humidity)
0cd8e231
UH
72{
73 char tmp[4];
74
6f3e5335
UH
75 /* Packet format MIC98581: "v ttt\r". */
76 /* Packet format MIC98583: "v ttt hhh\r". */
0cd8e231
UH
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
6f3e5335
UH
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 }
0cd8e231
UH
89
90 return SR_OK;
91}
92
93static 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;
edb691fc 97 struct sr_datafeed_analog analog;
676877f6
UH
98 struct sr_analog_encoding encoding;
99 struct sr_analog_meaning meaning;
100 struct sr_analog_spec spec;
0cd8e231
UH
101 struct dev_context *devc;
102 GSList *l;
103 int ret;
7ec5b549 104
0cd8e231
UH
105 (void)idx;
106
107 devc = sdi->priv;
108
6f3e5335 109 ret = packet_parse((const char *)buf, idx, &temperature, &humidity);
0cd8e231
UH
110 if (ret < 0) {
111 sr_err("Failed to parse packet.");
112 return SR_ERR;
113 }
114
b66a0b5b 115 sr_analog_init(&analog, &encoding, &meaning, &spec, 1);
ff17e6ba 116
ba7dd8bb 117 /* Common values for both channels. */
edb691fc 118 packet.type = SR_DF_ANALOG;
0cd8e231
UH
119 packet.payload = &analog;
120 analog.num_samples = 1;
121
122 /* Temperature. */
ba7dd8bb 123 l = g_slist_copy(sdi->channels);
0cd8e231 124 l = g_slist_remove_link(l, g_slist_nth(l, 1));
676877f6
UH
125 meaning.channels = l;
126 meaning.mq = SR_MQ_TEMPERATURE;
127 meaning.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
0cd8e231 128 analog.data = &temperature;
695dc859 129 sr_session_send(sdi, &packet);
0cd8e231
UH
130 g_slist_free(l);
131
132 /* Humidity. */
6f3e5335 133 if (mic_devs[idx].has_humidity) {
ba7dd8bb 134 l = g_slist_copy(sdi->channels);
6f3e5335 135 l = g_slist_remove_link(l, g_slist_nth(l, 0));
676877f6
UH
136 meaning.channels = l;
137 meaning.mq = SR_MQ_RELATIVE_HUMIDITY;
138 meaning.unit = SR_UNIT_PERCENTAGE;
6f3e5335 139 analog.data = &humidity;
695dc859 140 sr_session_send(sdi, &packet);
6f3e5335
UH
141 g_slist_free(l);
142 }
0cd8e231 143
301090aa 144 sr_sw_limits_update_samples_read(&devc->limits, 1);
0cd8e231
UH
145
146 return SR_OK;
147}
148
149static void handle_new_data(struct sr_dev_inst *sdi, int idx)
150{
7ec5b549 151 struct dev_context *devc;
886bd5e0 152 struct sr_serial_dev_inst *serial;
0cd8e231
UH
153 int len, i, offset = 0;
154
155 devc = sdi->priv;
886bd5e0 156 serial = sdi->conn;
0cd8e231
UH
157
158 /* Try to get as much data as the buffer can hold. */
159 len = SERIAL_BUFSIZE - devc->buflen;
98842e53 160 len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
0cd8e231
UH
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. */
6f3e5335
UH
169 while ((devc->buflen - offset) >= mic_devs[idx].packet_size) {
170 if (mic_devs[idx].packet_valid(devc->buf + offset)) {
0cd8e231 171 handle_packet(devc->buf + offset, sdi, idx);
6f3e5335 172 offset += mic_devs[idx].packet_size;
0cd8e231
UH
173 } else {
174 offset++;
175 }
176 }
177
178 /* If we have any data left, move it to the beginning of our buffer. */
179 for (i = 0; i < devc->buflen - offset; i++)
180 devc->buf[i] = devc->buf[offset + i];
181 devc->buflen -= offset;
182}
183
184static int receive_data(int fd, int revents, int idx, void *cb_data)
185{
186 struct sr_dev_inst *sdi;
187 struct dev_context *devc;
0cd8e231 188 static gboolean first_time = TRUE;
886bd5e0 189 struct sr_serial_dev_inst *serial;
0cd8e231
UH
190
191 (void)fd;
7ec5b549
UH
192
193 if (!(sdi = cb_data))
194 return TRUE;
195
196 if (!(devc = sdi->priv))
197 return TRUE;
198
886bd5e0
UH
199 serial = sdi->conn;
200
7ec5b549 201 if (revents == G_IO_IN) {
0cd8e231
UH
202 /* New data arrived. */
203 handle_new_data(sdi, idx);
204 } else {
205 /* Timeout. */
206 if (first_time) {
886bd5e0 207 mic_cmd_set_realtime_mode(serial);
0cd8e231
UH
208 first_time = FALSE;
209 }
210 }
211
301090aa 212 if (sr_sw_limits_check(&devc->limits))
d2f7c417 213 sr_dev_acquisition_stop(sdi);
7ec5b549
UH
214
215 return TRUE;
216}
0cd8e231
UH
217
218#define RECEIVE_DATA(ID_UPPER) \
219SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
220 return receive_data(fd, revents, ID_UPPER, cb_data); }
221
222/* Driver-specific receive_data() wrappers */
6f3e5335 223RECEIVE_DATA(MIC_98581)
0cd8e231 224RECEIVE_DATA(MIC_98583)