]> sigrok.org Git - libsigrok.git/blame - hardware/mic-985xx/protocol.c
Initial driver implementation for MIC 98583.
[libsigrok.git] / 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
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
0cd8e231
UH
23#define PACKET_SIZE 10
24
25static int mic_send(struct sr_serial_dev_inst *serial, const char *cmd)
7ec5b549 26{
0cd8e231
UH
27 int ret;
28
29 if ((ret = serial_write(serial, cmd, 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
47static gboolean packet_valid(const uint8_t *buf)
48{
49 if (buf[0] != 'v' || buf[1] != ' ' || buf[5] != ' ' || buf[9] != '\r')
50 return FALSE;
51
52 if (!isdigit(buf[2]) || !isdigit(buf[3]) || !isdigit(buf[4]))
53 return FALSE;
54
55 if (!isdigit(buf[6]) || !isdigit(buf[7]) || !isdigit(buf[8]))
56 return FALSE;
57
58 return TRUE;
59}
60
61static int packet_parse(const char *buf, float *temp, float *humidity)
62{
63 char tmp[4];
64
65 /* Packet format: "v ttt hhh\r". */
66
67 /* TODO: Sanity check on buf. For now we assume well-formed ASCII. */
68
69 tmp[3] = '\0';
70
71 strncpy((char *)&tmp, &buf[2], 3);
72 *temp = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
73
74 strncpy((char *)&tmp, &buf[6], 3);
75 *humidity = g_ascii_strtoull((const char *)&tmp, NULL, 10) / 10;
76
77 return SR_OK;
78}
79
80static int handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi, int idx)
81{
82 float temperature, humidity;
83 struct sr_datafeed_packet packet;
84 struct sr_datafeed_analog analog;
85 struct dev_context *devc;
86 GSList *l;
87 int ret;
7ec5b549 88
0cd8e231
UH
89 (void)idx;
90
91 devc = sdi->priv;
92
93 ret = packet_parse((const char *)buf, &temperature, &humidity);
94 if (ret < 0) {
95 sr_err("Failed to parse packet.");
96 return SR_ERR;
97 }
98
99 /* Common values for both probes. */
100 packet.type = SR_DF_ANALOG;
101 packet.payload = &analog;
102 analog.num_samples = 1;
103
104 /* Temperature. */
105 l = g_slist_copy(sdi->probes);
106 l = g_slist_remove_link(l, g_slist_nth(l, 1));
107 analog.probes = l;
108 analog.mq = SR_MQ_TEMPERATURE;
109 analog.unit = SR_UNIT_CELSIUS; /* TODO: Use C/F correctly. */
110 analog.data = &temperature;
111 sr_session_send(devc->cb_data, &packet);
112 g_slist_free(l);
113
114 /* Humidity. */
115 l = g_slist_copy(sdi->probes);
116 l = g_slist_remove_link(l, g_slist_nth(l, 0));
117 analog.probes = l;
118 analog.mq = SR_MQ_RELATIVE_HUMIDITY;
119 analog.unit = SR_UNIT_PERCENTAGE;
120 analog.data = &humidity;
121 sr_session_send(devc->cb_data, &packet);
122 g_slist_free(l);
123
124 devc->num_samples++;
125
126 return SR_OK;
127}
128
129static void handle_new_data(struct sr_dev_inst *sdi, int idx)
130{
7ec5b549 131 struct dev_context *devc;
0cd8e231
UH
132 int len, i, offset = 0;
133
134 devc = sdi->priv;
135
136 /* Try to get as much data as the buffer can hold. */
137 len = SERIAL_BUFSIZE - devc->buflen;
138 len = serial_read(devc->serial, devc->buf + devc->buflen, len);
139 if (len < 1) {
140 sr_err("Serial port read error: %d.", len);
141 return;
142 }
143
144 devc->buflen += len;
145
146 /* Now look for packets in that data. */
147 while ((devc->buflen - offset) >= PACKET_SIZE) {
148 if (packet_valid(devc->buf + offset)) {
149 handle_packet(devc->buf + offset, sdi, idx);
150 offset += PACKET_SIZE;
151 } else {
152 offset++;
153 }
154 }
155
156 /* If we have any data left, move it to the beginning of our buffer. */
157 for (i = 0; i < devc->buflen - offset; i++)
158 devc->buf[i] = devc->buf[offset + i];
159 devc->buflen -= offset;
160}
161
162static int receive_data(int fd, int revents, int idx, void *cb_data)
163{
164 struct sr_dev_inst *sdi;
165 struct dev_context *devc;
166 int64_t t;
167 static gboolean first_time = TRUE;
168
169 (void)fd;
7ec5b549
UH
170
171 if (!(sdi = cb_data))
172 return TRUE;
173
174 if (!(devc = sdi->priv))
175 return TRUE;
176
177 if (revents == G_IO_IN) {
0cd8e231
UH
178 /* New data arrived. */
179 handle_new_data(sdi, idx);
180 } else {
181 /* Timeout. */
182 if (first_time) {
183 mic_cmd_set_realtime_mode(devc->serial);
184 first_time = FALSE;
185 }
186 }
187
188 if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
189 sr_info("Requested number of samples reached.");
190 sdi->driver->dev_acquisition_stop(sdi, cb_data);
191 return TRUE;
192 }
193
194 if (devc->limit_msec) {
195 t = (g_get_monotonic_time() - devc->starttime) / 1000;
196 if (t > (int64_t)devc->limit_msec) {
197 sr_info("Requested time limit reached.");
198 sdi->driver->dev_acquisition_stop(sdi, cb_data);
199 return TRUE;
200 }
7ec5b549
UH
201 }
202
203 return TRUE;
204}
0cd8e231
UH
205
206#define RECEIVE_DATA(ID_UPPER) \
207SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \
208 return receive_data(fd, revents, ID_UPPER, cb_data); }
209
210/* Driver-specific receive_data() wrappers */
211RECEIVE_DATA(MIC_98583)