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