]> sigrok.org Git - libsigrok.git/blob - src/hardware/manson-hcs-3xxx/protocol.c
Add sr_dev_acquisition_stop(), factor out SR_ERR_DEV_CLOSED check.
[libsigrok.git] / src / hardware / manson-hcs-3xxx / protocol.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Uwe Hermann <uwe@hermann-uwe.de>
5  * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, see <http://www.gnu.org/licenses/>.
19  */
20
21 /**
22  * @file
23  *
24  * <em>Manson HCS-3xxx Series</em> power supply driver
25  *
26  * @internal
27  */
28
29 #include <config.h>
30 #include "protocol.h"
31
32 #define REQ_TIMEOUT_MS 500
33
34 SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...)
35 {
36         int ret;
37         char cmdbuf[50];
38         char *cmd_esc;
39         va_list args;
40
41         va_start(args, cmd);
42         vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args);
43         va_end(args);
44
45         cmd_esc = g_strescape(cmdbuf, NULL);
46         sr_dbg("Sending '%s'.", cmd_esc);
47         g_free(cmd_esc);
48
49         if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf),
50                         serial_timeout(serial, strlen(cmdbuf)))) < 0) {
51                 sr_err("Error sending command: %d.", ret);
52                 return ret;
53         }
54
55         return ret;
56 }
57
58 /**
59  * Read data from interface into buffer blocking until @a lines number of \\r chars
60  * received.
61  *
62  * @param serial Previously initialized serial port structure.
63  * @param[in] lines Number of \\r-terminated lines to read (1-n).
64  * @param buf Buffer for result. Contents is NUL-terminated on success.
65  * @param[in] buflen Buffer length (>0).
66  *
67  * @retval SR_OK Lines received and ending with "OK\r" (success).
68  * @retval SR_ERR Error.
69  * @retval SR_ERR_ARG Invalid argument.
70  */
71 SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char *buf, int buflen)
72 {
73         int l_recv = 0;
74         int bufpos = 0;
75         int retc;
76
77         if (!serial || (lines <= 0) || !buf || (buflen <= 0))
78                 return SR_ERR_ARG;
79
80         while ((l_recv < lines) && (bufpos < (buflen + 1))) {
81                 retc = serial_read_blocking(serial, &buf[bufpos], 1, 0);
82                 if (retc != 1)
83                         return SR_ERR;
84                 if (buf[bufpos] == '\r')
85                         l_recv++;
86                 bufpos++;
87         }
88         buf[bufpos] = '\0';
89
90         if ((l_recv == lines) && (g_str_has_suffix(buf, "OK\r")))
91                 return SR_OK;
92         else
93                 return SR_ERR;
94 }
95
96 /** Interpret result of GETD command. */
97 SR_PRIV int hcs_parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
98 {
99         char *str;
100         double val;
101         struct dev_context *devc;
102
103         devc = sdi->priv;
104
105         /* Bytes 0-3: Voltage. */
106         str = g_strndup(tokens[0], 4);
107         val = g_ascii_strtod(str, NULL) / 100;
108         devc->voltage = val;
109         g_free(str);
110
111         /* Bytes 4-7: Current. */
112         str = g_strndup((tokens[0] + 4), 4);
113         val = g_ascii_strtod(str, NULL) / 100;
114         devc->current = val;
115         g_free(str);
116
117         /* Byte 8: Mode ('0' means CV, '1' means CC). */
118         devc->cc_mode = (tokens[0][8] == '1');
119
120         /* Output enabled? Works because voltage cannot be set to 0.0 directly. */
121         devc->output_enabled = devc->voltage != 0.0;
122
123         return SR_OK;
124 }
125
126 static void send_sample(struct sr_dev_inst *sdi)
127 {
128         struct dev_context *devc;
129         struct sr_datafeed_packet packet;
130         struct sr_datafeed_analog analog;
131         struct sr_analog_encoding encoding;
132         struct sr_analog_meaning meaning;
133         struct sr_analog_spec spec;
134
135         devc = sdi->priv;
136
137         sr_analog_init(&analog, &encoding, &meaning, &spec, 2);
138
139         packet.type = SR_DF_ANALOG;
140         packet.payload = &analog;
141         analog.meaning->channels = sdi->channels;
142         analog.num_samples = 1;
143
144         analog.meaning->mq = SR_MQ_VOLTAGE;
145         analog.meaning->unit = SR_UNIT_VOLT;
146         analog.meaning->mqflags = SR_MQFLAG_DC;
147         analog.data = &devc->voltage;
148         sr_session_send(sdi, &packet);
149
150         analog.meaning->mq = SR_MQ_CURRENT;
151         analog.meaning->unit = SR_UNIT_AMPERE;
152         analog.meaning->mqflags = 0;
153         analog.data = &devc->current;
154         sr_session_send(sdi, &packet);
155
156
157         sr_sw_limits_update_samples_read(&devc->limits, 1);
158 }
159
160 static int parse_reply(struct sr_dev_inst *sdi)
161 {
162         struct dev_context *devc;
163         char *reply_esc, **tokens;
164         int retc;
165
166         devc = sdi->priv;
167
168         reply_esc = g_strescape(devc->buf, NULL);
169         sr_dbg("Received '%s'.", reply_esc);
170         g_free(reply_esc);
171
172         tokens = g_strsplit(devc->buf, "\r", 0);
173         retc = hcs_parse_volt_curr_mode(sdi, tokens);
174         g_strfreev(tokens);
175         if (retc < 0)
176                 return SR_ERR;
177
178         send_sample(sdi);
179
180         return SR_OK;
181 }
182
183 static int handle_new_data(struct sr_dev_inst *sdi)
184 {
185         int len;
186         struct dev_context *devc;
187         struct sr_serial_dev_inst *serial;
188
189         devc = sdi->priv;
190         serial = sdi->conn;
191
192         len = serial_read_blocking(serial, devc->buf + devc->buflen, 1, 0);
193         if (len < 1)
194                 return SR_ERR;
195
196         devc->buflen += len;
197         devc->buf[devc->buflen] = '\0';
198
199         /* Wait until we received an "OK\r" (among other bytes). */
200         if (!g_str_has_suffix(devc->buf, "OK\r"))
201                 return SR_OK;
202
203         parse_reply(sdi);
204
205         devc->buf[0] = '\0';
206         devc->buflen = 0;
207
208         devc->reply_pending = FALSE;
209
210         return SR_OK;
211 }
212
213 /** Driver/serial data reception function. */
214 SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
215 {
216         struct sr_dev_inst *sdi;
217         struct dev_context *devc;
218         struct sr_serial_dev_inst *serial;
219         uint64_t elapsed_us;
220
221         (void)fd;
222
223         if (!(sdi = cb_data))
224                 return TRUE;
225
226         if (!(devc = sdi->priv))
227                 return TRUE;
228
229         serial = sdi->conn;
230
231         if (revents == G_IO_IN) {
232                 /* New data arrived. */
233                 handle_new_data(sdi);
234         } else {
235                 /* Timeout. */
236         }
237
238         if (sr_sw_limits_check(&devc->limits)) {
239                 sr_dev_acquisition_stop(sdi);
240                 return TRUE;
241         }
242
243         /* Request next packet, if required. */
244         if (sdi->status == SR_ST_ACTIVE) {
245                 if (devc->reply_pending) {
246                         elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
247                         if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
248                                 devc->reply_pending = FALSE;
249                         return TRUE;
250                 }
251
252                 /* Send command to get voltage, current, and mode (CC or CV). */
253                 if (hcs_send_cmd(serial, "GETD\r") < 0)
254                         return TRUE;
255
256                 devc->req_sent_at = g_get_monotonic_time();
257                 devc->reply_pending = TRUE;
258         }
259
260         return TRUE;
261 }