]> sigrok.org Git - libsigrok.git/blob - src/hardware/manson-hcs-3xxx/protocol.c
Build: Include <config.h> first in all source files
[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, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
20  */
21
22 /** @file
23   *  <em>Manson HCS-3xxx Series</em> power supply driver
24   *  @internal
25   */
26
27 #include <config.h>
28 #include "protocol.h"
29
30 #define REQ_TIMEOUT_MS 500
31
32 SR_PRIV int hcs_send_cmd(struct sr_serial_dev_inst *serial, const char *cmd, ...)
33 {
34         int ret;
35         char cmdbuf[50];
36         char *cmd_esc;
37         va_list args;
38
39         va_start(args, cmd);
40         vsnprintf(cmdbuf, sizeof(cmdbuf), cmd, args);
41         va_end(args);
42
43         cmd_esc = g_strescape(cmdbuf, NULL);
44         sr_dbg("Sending '%s'.", cmd_esc);
45         g_free(cmd_esc);
46
47         if ((ret = serial_write_blocking(serial, cmdbuf, strlen(cmdbuf), 0)) < 0) {
48                 sr_err("Error sending command: %d.", ret);
49                 return ret;
50         }
51
52         return ret;
53 }
54
55 /**
56  * Read data from interface into buffer blocking until @a lines number of \\r chars
57  * received.
58  * @param serial Previously initialized serial port structure.
59  * @param[in] lines Number of \\r-terminated lines to read (1-n).
60  * @param     buf Buffer for result. Contents is NUL-terminated on success.
61  * @param[in] buflen Buffer length (>0).
62  * @retval SR_OK Lines received and ending with "OK\r" (success).
63  * @retval SR_ERR Error.
64  * @retval SR_ERR_ARG Invalid argument.
65  */
66 SR_PRIV int hcs_read_reply(struct sr_serial_dev_inst *serial, int lines, char *buf, int buflen)
67 {
68         int l_recv = 0;
69         int bufpos = 0;
70         int retc;
71
72         if (!serial || (lines <= 0) || !buf || (buflen <= 0))
73                 return SR_ERR_ARG;
74
75         while ((l_recv < lines) && (bufpos < (buflen + 1))) {
76                 retc = serial_read_blocking(serial, &buf[bufpos], 1, 0);
77                 if (retc != 1)
78                         return SR_ERR;
79                 if (buf[bufpos] == '\r')
80                         l_recv++;
81                 bufpos++;
82         }
83         buf[bufpos] = '\0';
84
85         if ((l_recv == lines) && (g_str_has_suffix(buf, "OK\r")))
86                 return SR_OK;
87         else
88                 return SR_ERR;
89 }
90
91 /** Interpret result of GETD command. */
92 SR_PRIV int hcs_parse_volt_curr_mode(struct sr_dev_inst *sdi, char **tokens)
93 {
94         char *str;
95         double val;
96         struct dev_context *devc;
97
98         devc = sdi->priv;
99
100         /* Bytes 0-3: Voltage. */
101         str = g_strndup(tokens[0], 4);
102         val = g_ascii_strtod(str, NULL) / 100;
103         devc->voltage = val;
104         g_free(str);
105
106         /* Bytes 4-7: Current. */
107         str = g_strndup((tokens[0] + 4), 4);
108         val = g_ascii_strtod(str, NULL) / 100;
109         devc->current = val;
110         g_free(str);
111
112         /* Byte 8: Mode ('0' means CV, '1' means CC). */
113         devc->cc_mode = (tokens[0][8] == '1');
114
115         /* Output enabled? Works because voltage cannot be set to 0.0 directly. */
116         devc->output_enabled = devc->voltage != 0.0;
117
118         return SR_OK;
119 }
120
121 static void send_sample(struct sr_dev_inst *sdi)
122 {
123         struct dev_context *devc;
124         struct sr_datafeed_packet packet;
125         struct sr_datafeed_analog analog;
126
127         devc = sdi->priv;
128
129         packet.type = SR_DF_ANALOG;
130         packet.payload = &analog;
131         analog.channels = sdi->channels;
132         analog.num_samples = 1;
133
134         analog.mq = SR_MQ_VOLTAGE;
135         analog.unit = SR_UNIT_VOLT;
136         analog.mqflags = SR_MQFLAG_DC;
137         analog.data = &devc->voltage;
138         sr_session_send(sdi, &packet);
139
140         analog.mq = SR_MQ_CURRENT;
141         analog.unit = SR_UNIT_AMPERE;
142         analog.mqflags = 0;
143         analog.data = &devc->current;
144         sr_session_send(sdi, &packet);
145
146         devc->num_samples++;
147 }
148
149 static int parse_reply(struct sr_dev_inst *sdi)
150 {
151         struct dev_context *devc;
152         char *reply_esc, **tokens;
153         int retc;
154
155         devc = sdi->priv;
156
157         reply_esc = g_strescape(devc->buf, NULL);
158         sr_dbg("Received '%s'.", reply_esc);
159         g_free(reply_esc);
160
161         tokens = g_strsplit(devc->buf, "\r", 0);
162         retc = hcs_parse_volt_curr_mode(sdi, tokens);
163         g_strfreev(tokens);
164         if (retc < 0)
165                 return SR_ERR;
166
167         send_sample(sdi);
168
169         return SR_OK;
170 }
171
172 static int handle_new_data(struct sr_dev_inst *sdi)
173 {
174         int len;
175         struct dev_context *devc;
176         struct sr_serial_dev_inst *serial;
177
178         devc = sdi->priv;
179         serial = sdi->conn;
180
181         len = serial_read_blocking(serial, devc->buf + devc->buflen, 1, 0);
182         if (len < 1)
183                 return SR_ERR;
184
185         devc->buflen += len;
186         devc->buf[devc->buflen] = '\0';
187
188         /* Wait until we received an "OK\r" (among other bytes). */
189         if (!g_str_has_suffix(devc->buf, "OK\r"))
190                 return SR_OK;
191
192         parse_reply(sdi);
193
194         devc->buf[0] = '\0';
195         devc->buflen = 0;
196
197         devc->reply_pending = FALSE;
198
199         return SR_OK;
200 }
201
202 /** Driver/serial data reception function. */
203 SR_PRIV int hcs_receive_data(int fd, int revents, void *cb_data)
204 {
205         struct sr_dev_inst *sdi;
206         struct dev_context *devc;
207         struct sr_serial_dev_inst *serial;
208         int64_t t, elapsed_us;
209
210         (void)fd;
211
212         if (!(sdi = cb_data))
213                 return TRUE;
214
215         if (!(devc = sdi->priv))
216                 return TRUE;
217
218         serial = sdi->conn;
219
220         if (revents == G_IO_IN) {
221                 /* New data arrived. */
222                 handle_new_data(sdi);
223         } else {
224                 /* Timeout. */
225         }
226
227         if (devc->limit_samples && (devc->num_samples >= devc->limit_samples)) {
228                 sr_info("Requested number of samples reached.");
229                 sdi->driver->dev_acquisition_stop(sdi, cb_data);
230                 return TRUE;
231         }
232
233         if (devc->limit_msec) {
234                 t = (g_get_monotonic_time() - devc->starttime) / 1000;
235                 if (t > (int64_t)devc->limit_msec) {
236                         sr_info("Requested time limit reached.");
237                         sdi->driver->dev_acquisition_stop(sdi, cb_data);
238                         return TRUE;
239                 }
240         }
241
242         /* Request next packet, if required. */
243         if (sdi->status == SR_ST_ACTIVE) {
244                 if (devc->reply_pending) {
245                         elapsed_us = g_get_monotonic_time() - devc->req_sent_at;
246                         if (elapsed_us > (REQ_TIMEOUT_MS * 1000))
247                                 devc->reply_pending = FALSE;
248                         return TRUE;
249                 }
250
251                 /* Send command to get voltage, current, and mode (CC or CV). */
252                 if (hcs_send_cmd(serial, "GETD\r") < 0)
253                         return TRUE;
254
255                 devc->req_sent_at = g_get_monotonic_time();
256                 devc->reply_pending = TRUE;
257         }
258
259         return TRUE;
260 }