]>
Commit | Line | Data |
---|---|---|
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 | ||
24 | static 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 | ||
37 | SR_PRIV int mic_cmd_get_device_info(struct sr_serial_dev_inst *serial) | |
38 | { | |
39 | return mic_send(serial, "I\r"); | |
40 | } | |
41 | ||
42 | static 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 | ||
47 | SR_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 | ||
58 | SR_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 | ||
72 | static 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 | ||
94 | static 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 | devc->num_samples++; | |
146 | ||
147 | return SR_OK; | |
148 | } | |
149 | ||
150 | static 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 | ||
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 | int64_t t; | |
190 | static gboolean first_time = TRUE; | |
191 | struct sr_serial_dev_inst *serial; | |
192 | ||
193 | (void)fd; | |
194 | ||
195 | if (!(sdi = cb_data)) | |
196 | return TRUE; | |
197 | ||
198 | if (!(devc = sdi->priv)) | |
199 | return TRUE; | |
200 | ||
201 | serial = sdi->conn; | |
202 | ||
203 | if (revents == G_IO_IN) { | |
204 | /* New data arrived. */ | |
205 | handle_new_data(sdi, idx); | |
206 | } else { | |
207 | /* Timeout. */ | |
208 | if (first_time) { | |
209 | mic_cmd_set_realtime_mode(serial); | |
210 | first_time = FALSE; | |
211 | } | |
212 | } | |
213 | ||
214 | if (devc->limit_samples && devc->num_samples >= devc->limit_samples) { | |
215 | sr_info("Requested number of samples reached."); | |
216 | sdi->driver->dev_acquisition_stop(sdi); | |
217 | return TRUE; | |
218 | } | |
219 | ||
220 | if (devc->limit_msec) { | |
221 | t = (g_get_monotonic_time() - devc->starttime) / 1000; | |
222 | if (t > (int64_t)devc->limit_msec) { | |
223 | sr_info("Requested time limit reached."); | |
224 | sdi->driver->dev_acquisition_stop(sdi); | |
225 | return TRUE; | |
226 | } | |
227 | } | |
228 | ||
229 | return TRUE; | |
230 | } | |
231 | ||
232 | #define RECEIVE_DATA(ID_UPPER) \ | |
233 | SR_PRIV int receive_data_##ID_UPPER(int fd, int revents, void *cb_data) { \ | |
234 | return receive_data(fd, revents, ID_UPPER, cb_data); } | |
235 | ||
236 | /* Driver-specific receive_data() wrappers */ | |
237 | RECEIVE_DATA(MIC_98581) | |
238 | RECEIVE_DATA(MIC_98583) |