]> sigrok.org Git - libsigrok.git/blob - src/hardware/motech-lps-30x/api.c
Eliminate sr_dev_inst_new().
[libsigrok.git] / src / hardware / motech-lps-30x / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
5  * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> (code from atten-pps3xxx)
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 3 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 /** @file
22  *  <em>Motech LPS-30x series</em> power supply driver
23  *  @internal
24  */
25
26 #include <ctype.h>
27 #include <errno.h>
28 #include <math.h>
29 #include <string.h>
30
31 #include "protocol.h"
32
33 /* Forward declarations */
34 SR_PRIV struct sr_dev_driver motech_lps_301_driver_info;
35 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen);
36 SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_list args);
37 SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char* fmt, ...);
38 SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const char* fmt, ...);
39 SR_PRIV int lps_query_status(struct sr_dev_inst* sdi);
40
41 /* Serial communication parameters */
42 #define SERIALCOMM "2400/8n1/dtr=1/rts=1/flow=0"
43
44 #define VENDOR_MOTECH "Motech"
45
46 /** Driver capabilities generic. */
47 static const uint32_t drvopts[] = {
48         /* Device class */
49         SR_CONF_POWER_SUPPLY,
50 };
51
52 /** Driver scanning options. */
53 static const uint32_t scanopts[] = {
54         SR_CONF_CONN,
55         SR_CONF_SERIALCOMM,
56 };
57
58 /** Hardware capabilities generic. */
59 static const uint32_t devopts[] = {
60         /* Device class */
61         SR_CONF_POWER_SUPPLY,
62         /* Aquisition modes. */
63         SR_CONF_CONTINUOUS,
64         SR_CONF_LIMIT_SAMPLES | SR_CONF_GET | SR_CONF_SET,
65         SR_CONF_LIMIT_MSEC | SR_CONF_GET | SR_CONF_SET,
66         /* Device configuration */
67         SR_CONF_OUTPUT_CHANNEL_CONFIG | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
68 };
69
70 /** Hardware capabilities channel 1, 2. */
71 static const uint32_t devopts_ch12[] = {
72         SR_CONF_OUTPUT_VOLTAGE | SR_CONF_GET,
73         SR_CONF_OUTPUT_VOLTAGE_TARGET | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
74         SR_CONF_OUTPUT_CURRENT | SR_CONF_GET,
75         SR_CONF_OUTPUT_CURRENT_LIMIT | SR_CONF_GET | SR_CONF_SET | SR_CONF_LIST,
76         SR_CONF_OUTPUT_ENABLED | SR_CONF_GET | SR_CONF_SET,
77 };
78
79 /** Hardware capabilities channel 3. (LPS-304/305 only). */
80 static const uint32_t devopts_ch3[] = {
81         SR_CONF_OUTPUT_VOLTAGE | SR_CONF_GET,
82         SR_CONF_OUTPUT_ENABLED | SR_CONF_GET | SR_CONF_SET,
83 };
84
85 static const char *channel_modes[] = {
86         "Independent",
87         "Track1",
88         "Track2",
89 };
90
91 static struct lps_modelspec models[] = {
92         { LPS_UNKNOWN, "Dummy", 0,
93                 {
94
95                 }
96         },
97         { LPS_301, "LPS-301", 1,
98                 {
99                         /* Channel 1 */
100                         { { 0, 32, 0.01 }, { 0.005, 2, 0.001 } },
101                 },
102         },
103         { LPS_302, "LPS-302", 1,
104                 {
105                         /* Channel 1 */
106                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
107                 },
108         },
109         { LPS_303, "LPS-303", 1,
110                 {
111                         /* Channel 1 */
112                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
113                 },
114         },
115         { LPS_304, "LPS-304", 3,
116                 {
117                         /* Channel 1 */
118                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
119                         /* Channel 2 */
120                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
121                         /* Channel 3 */
122                         { { 5, 5, 0.0 }, { 0.005, 3, 0.001 } },
123                 },
124         },
125         { LPS_305, "LPS-305", 3,
126                 {
127                         /* Channel 1 */
128                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
129                         /* Channel 2 */
130                         { { 0, 32, 0.01 }, { 0.005, 3, 0.001 } },
131                         /* Channel 3 */
132                         { { 3.3, 5, 1.7 }, { 0.005, 3, 0.001 } },
133                 },
134         },
135 };
136
137 static int init_lps301(struct sr_context *sr_ctx)
138 {
139         return std_init(sr_ctx, &motech_lps_301_driver_info, LOG_PREFIX);
140 }
141
142 /** Send command to device with va_list.
143  */
144 SR_PRIV int lps_send_va(struct sr_serial_dev_inst *serial, const char* fmt, va_list args)
145 {
146         int retc;
147         char auxfmt[LINELEN_MAX];
148         char buf[LINELEN_MAX];
149
150         snprintf(auxfmt, sizeof(auxfmt), "%s\r\n", fmt);
151         vsnprintf(buf, sizeof(buf), auxfmt, args);
152
153         sr_spew("lps_send_va: \"%s\"", buf);
154
155         retc = serial_write_blocking(serial, buf, strlen(buf), 0);
156
157         if (retc < 0)
158                 return SR_ERR;
159
160         return SR_OK;
161 }
162
163 /** Send command to device.
164  */
165 SR_PRIV int lps_send_req(struct sr_serial_dev_inst *serial, const char* fmt, ...)
166 {
167         int retc;
168         va_list args;
169
170         va_start(args, fmt);
171         retc = lps_send_va(serial, fmt, args);
172         va_end(args);
173
174         return retc;
175 }
176
177 /** Send command and consume simple OK reply. */
178 SR_PRIV int lps_cmd_ok(struct sr_serial_dev_inst *serial, const char* fmt, ...)
179 {
180         int retc;
181         va_list args;
182         char buf[LINELEN_MAX];
183         char* bufptr;
184         int  buflen;
185
186         /* Send command */
187         va_start(args, fmt);
188         retc = lps_send_va(serial, fmt, args);
189         va_end(args);
190
191         if (retc != SR_OK)
192                 return SR_ERR;
193
194         /* Read reply */
195         buf[0] = '\0';
196         bufptr = buf;
197         buflen = sizeof(buf);
198         retc = lps_read_reply(serial, &bufptr, &buflen);
199         if ((retc == SR_OK) && (buflen == 0))
200                 return SR_OK;
201
202         return SR_ERR;
203 }
204
205 /** Send command and read reply string.
206  *  \param reply Pointer to buffer of size LINELEN_MAX. Will be NUL-terminated.
207  */
208 SR_PRIV int lps_cmd_reply(char* reply, struct sr_serial_dev_inst *serial, const char* fmt, ...)
209 {
210         int retc;
211         va_list args;
212         char buf[LINELEN_MAX];
213         char* bufptr;
214         int  buflen;
215
216         reply[0] = '\0';
217
218         /* Send command */
219         va_start(args, fmt);
220         retc = lps_send_va(serial, fmt, args);
221         va_end(args);
222
223         if (retc != SR_OK)
224                 return SR_ERR;
225
226         /* Read reply */
227         buf[0] = '\0';
228         bufptr = buf;
229         buflen = sizeof(buf);
230         retc = lps_read_reply(serial, &bufptr, &buflen);
231         if ((retc == SR_OK) && (buflen > 0)) {
232                 strcpy(reply, buf);
233                 return SR_OK;
234         }
235
236         return SR_ERR;
237 }
238
239 /** Process integer value returned by STATUS command. */
240 SR_PRIV int lps_process_status(struct sr_dev_inst* sdi, int stat)
241 {
242         struct dev_context* devc;
243         int tracking_mode;
244
245         devc = (struct dev_context*)sdi->priv;
246
247         sr_spew("Status: %d", stat);
248         devc->channel_status[0].cc_mode = (stat & 0x01) != 0;
249         sr_spew("Channel 1 %s mode", devc->channel_status[0].cc_mode?"CC":"CV");
250         if (devc->model->num_channels > 1) {
251                 devc->channel_status[1].cc_mode = (stat & 0x02) != 0;
252                 sr_spew("Channel 2 %s mode", devc->channel_status[1].cc_mode?"CC":"CV");
253
254                 tracking_mode = (stat & 0x0c) >> 2;
255                 switch (tracking_mode) {
256                 case 0: devc->tracking_mode = 0;
257                         break;
258                 case 2: devc->tracking_mode = 1;
259                         break;
260                 case 3: devc->tracking_mode = 2;
261                         break;
262                 default:
263                         sr_err("Illegal channel tracking mode %d!", tracking_mode);
264                         devc->tracking_mode = 0;
265                         break;
266                 }
267
268                 sr_spew("Channel tracking: %d", devc->tracking_mode);
269         }
270         devc->channel_status[0].output_enabled = devc->channel_status[1].output_enabled = stat&0x040?TRUE:FALSE;
271         sr_spew("Channel 1%s output: %s", devc->model->num_channels > 1?"+2":"", devc->channel_status[0].output_enabled?"ON":"OFF");
272         if (devc->model->num_channels > 2) {
273                 devc->channel_status[2].output_enabled = stat&0x010?TRUE:FALSE;
274                 devc->channel_status[2].output_voltage_last = stat&0x020?3.3:5;
275                 sr_spew("Channel 3 output: %s, U=%02f V, overload=%d",
276                         devc->channel_status[2].output_enabled?"ON":"OFF",
277                         devc->channel_status[2].output_voltage_last,
278                         stat&0x080?1:0);
279         }
280         sr_spew("Fan=%d, beep=%d, CC output compensated=%d", stat&0x0100?1:0, stat&0x0200?1:0, stat&0x0400?1:0);
281
282         return SR_OK;
283 }
284
285 /** Send STATUS commend and process status string. */
286 SR_PRIV int lps_query_status(struct sr_dev_inst* sdi)
287 {
288         char buf[LINELEN_MAX];
289         int stat;
290         struct dev_context* devc;
291
292         devc = (struct dev_context*)sdi->priv;
293
294         devc->req_sent_at = g_get_real_time();
295
296         if (lps_cmd_reply(buf, sdi->conn, "STATUS") < 0) {
297                 sr_err("%s: Failed to read status: %d %s", __func__, errno, strerror(errno));
298                 return SR_ERR;
299         }
300
301         if (sr_atoi(buf, &stat) != SR_OK)
302                 return SR_ERR;
303
304         return lps_process_status(sdi, stat);
305 }
306
307 static gint64 calc_timeout_ms(gint64 start_us)
308 {
309         gint64 result = REQ_TIMEOUT_MS - ((g_get_real_time() - start_us) / 1000);
310
311         if (result < 0)
312                 return 0;
313
314         return result;
315 }
316
317 /** Read message into buf until "OK" received.
318  *  \retval SR_OK Msg received; buf and buflen contain result, if any except OK.
319  *  \retval SR_ERR Error, including timeout.
320 */
321 SR_PRIV int lps_read_reply(struct sr_serial_dev_inst *serial, char **buf, int *buflen)
322 {
323         int retries;
324         char buf2[LINELEN_MAX];
325         char *buf2ptr;
326         int buf2len;
327         gint64 timeout_start;
328
329         *buf[0] = '\0';
330
331         /* Read one line. It is either a data message or "OK". */
332         timeout_start = g_get_real_time();
333         buf2len = *buflen;
334         /* Up to 5 tries because serial_readline() will consume only one CR or LF per
335          * call, but device sends up to 4 in a row. */
336         for (retries = 0; retries < 5; retries++) {
337                 *buflen = buf2len;
338                 if (serial_readline(serial, buf, buflen, calc_timeout_ms(timeout_start)) != SR_OK)
339                         return SR_ERR;
340                 if (!strcmp(*buf, "OK")) { /* We got an OK! */
341                         *buf[0] = '\0';
342                         *buflen = 0;
343                         return SR_OK;
344                 }
345                 if (*buflen > 0) /* We got a msg! */
346                         break;
347         }
348
349         /* A data msg is in buf (possibly ERROR), need to consume "OK". */
350         buf2[0] = '\0';
351         buf2ptr = buf2;
352         for (retries = 0; retries < 5; retries++) {
353                 buf2len = sizeof(buf2);
354                 if (serial_readline(serial, &buf2ptr, &buf2len, calc_timeout_ms(timeout_start)) != SR_OK)
355                         return SR_ERR;
356
357                 if (!strcmp(buf2ptr, "OK")) { /* We got an OK! */
358                         if (!strcmp(*buf, "ERROR")) { /* OK came after msg ERROR! */
359                                 sr_spew("ERROR found!");
360                                 *buf[0] = '\0';
361                                 *buflen = 0;
362                                 return SR_ERR;
363                         }
364                         return SR_OK;
365                 }
366         }
367
368         return SR_ERR; /* Timeout! */
369 }
370
371 /** Scan for LPS-300 series device.
372  */
373 static GSList *do_scan(lps_modelid modelid, struct sr_dev_driver *drv, GSList *options)
374 {
375         struct sr_dev_inst *sdi;
376         struct drv_context *drvc;
377         struct dev_context *devc;
378         struct sr_serial_dev_inst *serial;
379         struct sr_channel *ch;
380         struct sr_channel_group *cg;
381         GSList *devices;
382         const char *conn, *serialcomm;
383         int cnt;
384         gchar buf[LINELEN_MAX];
385         gchar channel[10];
386         char *verstr;
387
388         sdi = NULL;
389         devc = NULL;
390         conn = serialcomm = NULL;
391         devices = NULL;
392
393         drvc = drv->priv;
394         drvc->instances = NULL;
395
396         sr_spew("scan() called!");
397
398         /* Process and check options. */
399         if (sr_serial_extract_options(options, &conn, &serialcomm) != SR_OK)
400                 return NULL;
401         if (!serialcomm)
402                 serialcomm = SERIALCOMM;
403
404         /* Init serial port. */
405         if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
406                 return NULL;
407
408         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
409                 goto exit_err;
410
411         /* Query and verify model string. */
412         serial_flush(serial);
413         if (lps_cmd_reply(buf, serial, "MODEL") != SR_OK)
414                 return NULL;
415
416         /* Check model string. */
417         if (strncmp(buf, "LPS-", 4)) {
418                 sr_spew("Unknown model code \"%s\"!", buf);
419                 return NULL;
420         }
421
422         /* Bug in device FW 1.17, model number is empty, so this can't work with this FW! */
423         if (modelid == LPS_UNKNOWN) {
424                 g_strstrip(buf);
425                 for (cnt = LPS_301; cnt <= LPS_305; cnt++) {
426                         if (!strcmp(buf, models[cnt].modelstr)) {
427                                 modelid = cnt;
428                                 break;
429                         }
430                 }
431                 if (modelid == LPS_UNKNOWN) {
432                         sr_err("Unable to detect model from model string '%s'!", buf);
433                         return NULL;
434                 }
435         }
436
437         /* Query version */
438         verstr = NULL;
439         if (lps_cmd_reply(buf, serial, "VERSION") == SR_OK) {
440                 if (strncmp(buf, "Ver-", 4)) {
441                         sr_spew("Version string %s not recognized.", buf);
442                         goto exit_err;
443                 }
444
445
446                 g_strstrip(buf);
447                 verstr = buf + 4;
448         }
449         else  /* Bug in device FW 1.17: Quering version string fails while output is active.
450                 Therefore just print an error message, but do not exit with error. */
451                 sr_err("Failed to query for hardware version: %d %s", errno, strerror(errno));
452
453         sdi = g_malloc0(sizeof(struct sr_dev_inst));
454         sdi->status = SR_ST_INACTIVE;
455         sdi->vendor = g_strdup(VENDOR_MOTECH);
456         sdi->model = g_strdup(models[modelid].modelstr);
457         sdi->version = g_strdup(verstr);
458         sdi->driver = drv;
459         sdi->inst_type = SR_INST_SERIAL;
460         sdi->conn = serial;
461
462         devc = g_malloc0(sizeof(struct dev_context));
463         devc->model = &models[modelid];
464         devc->limit_samples = 0;
465         devc->limit_msec = 0;
466         devc->num_samples = 0;
467         devc->elapsed_msec = g_timer_new();
468
469         sdi->priv = devc;
470
471         /* Setup channels and channel groups. */
472         for (cnt = 0; cnt < models[modelid].num_channels; cnt++) {
473                 snprintf(channel, sizeof(channel), "CH%d", cnt + 1);
474                 ch = sr_channel_new(cnt, SR_CHANNEL_ANALOG, TRUE, channel);
475                 sdi->channels = g_slist_append(sdi->channels, ch);
476
477                 devc->channel_status[cnt].info = g_slist_append(NULL, ch);
478
479                 cg = g_malloc(sizeof(struct sr_channel_group));
480                 snprintf(channel, sizeof(channel), "CG%d", cnt+1);
481                 cg->name = g_strdup(channel);
482                 cg->priv = NULL;
483                 cg->channels = g_slist_append(NULL, ch);
484
485                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
486         }
487
488         drvc->instances = g_slist_append(drvc->instances, sdi);
489         devices = g_slist_append(devices, sdi);
490
491         /* Query status */
492         if (lps_query_status(sdi) != SR_OK)
493                 goto exit_err;
494
495         serial_close(serial);
496         if (!devices)
497                 sr_serial_dev_inst_free(serial);
498
499         return devices;
500
501 exit_err:
502         sr_info("%s: Error!", __func__);
503
504         if (serial) {
505                 serial_close(serial);
506                 sr_serial_dev_inst_free(serial);
507         }
508         if (devc)
509                 g_free(devc);
510         if (sdi)
511                 sr_dev_inst_free(sdi);
512
513         return NULL;
514 }
515
516 /** Scan for LPS-301 device. */
517 static GSList *scan_lps301(GSList *options)
518 {
519         return do_scan(LPS_301, &motech_lps_301_driver_info, options);
520 }
521
522 static GSList *doDevList(struct sr_dev_driver *drv)
523 {
524         return ((struct drv_context *)(drv->priv))->instances;
525 }
526
527 static GSList *dev_list_lps301(void)
528 {
529         return doDevList(&motech_lps_301_driver_info);
530 }
531
532 static void dev_clear_private(struct dev_context* devc)
533 {
534         int ch_idx;
535
536         /* Free channel_status.info (list only, data owned by sdi). */
537         for (ch_idx = 0; ch_idx < devc->model->num_channels; ch_idx++)
538                 g_slist_free(devc->channel_status[ch_idx].info);
539
540         g_timer_destroy(devc->elapsed_msec);
541 }
542
543 static int dev_clear_lps301(void)
544 {
545         return std_dev_clear(&motech_lps_301_driver_info, (std_dev_clear_callback)dev_clear_private);
546 }
547
548 static int cleanup(void)
549 {
550         return dev_clear_lps301();
551 }
552
553 static int config_get(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
554                 const struct sr_channel_group *cg)
555 {
556         struct dev_context *devc;
557         struct sr_channel *ch;
558         int ch_idx;
559
560         if (!sdi)
561                 return SR_ERR_ARG;
562
563         devc = sdi->priv;
564
565         if (!cg) {
566                 /* No channel group: global options. */
567                 switch (key) {
568                 case SR_CONF_LIMIT_SAMPLES:
569                         *data = g_variant_new_uint64(devc->limit_samples);
570                         break;
571                 case SR_CONF_LIMIT_MSEC:
572                         *data = g_variant_new_uint64(devc->limit_msec);
573                         break;
574                 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
575                         *data = g_variant_new_string(channel_modes[devc->tracking_mode]);
576                         break;
577                 default:
578                         return SR_ERR_NA;
579                 }
580         } else {
581                 /* We only ever have one channel per channel group in this driver. */
582                 ch = cg->channels->data;
583                 ch_idx = ch->index;
584                 switch (key) {
585                 case SR_CONF_OUTPUT_VOLTAGE:
586                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_last);
587                         break;
588                 case SR_CONF_OUTPUT_VOLTAGE_TARGET:
589                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_voltage_max);
590                         break;
591                 case SR_CONF_OUTPUT_CURRENT:
592                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_last);
593                         break;
594                 case SR_CONF_OUTPUT_CURRENT_LIMIT:
595                         *data = g_variant_new_double(devc->channel_status[ch_idx].output_current_max);
596                         break;
597                 case SR_CONF_OUTPUT_ENABLED:
598                         *data = g_variant_new_boolean(devc->channel_status[ch_idx].output_enabled);
599                         break;
600                 default:
601                         return SR_ERR_NA;
602                 }
603         }
604
605         return SR_OK;
606 }
607
608 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
609                 const struct sr_channel_group *cg)
610 {
611         struct dev_context *devc;
612         struct sr_channel *ch;
613         gdouble dval;
614         int ch_idx;
615         const char *sval;
616         gboolean bval;
617         int idx;
618         gboolean found;
619
620         if (sdi->status != SR_ST_ACTIVE)
621                 return SR_ERR_DEV_CLOSED;
622
623         devc = sdi->priv;
624
625         /* Cannot change settings while acquisition active, would cause a mess with commands.
626          * Changing this would be possible, but tricky. */
627         if (devc->acq_running)
628                 return SR_ERR_NA;
629
630         if (!cg) {
631                 /* No channel group: global options. */
632                 switch (key) {
633                 case SR_CONF_LIMIT_MSEC:
634                         if (g_variant_get_uint64(data) == 0) {
635                                 sr_err("LIMIT_MSEC can't be 0.");
636                                 return SR_ERR;
637                         }
638                         devc->limit_msec = g_variant_get_uint64(data);
639                         sr_dbg("Setting time limit to %" PRIu64 "ms.",
640                                 devc->limit_msec);
641                         break;
642                 case SR_CONF_LIMIT_SAMPLES:
643                         devc->limit_samples = g_variant_get_uint64(data);
644                         sr_dbg("Setting sample limit to %" PRIu64 ".",
645                                 devc->limit_samples);
646                         break;
647                 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
648                         sval = g_variant_get_string(data, NULL);
649                         found = FALSE;
650                         for (idx = 0; idx < (int)ARRAY_SIZE(channel_modes); idx++)
651                         {
652                                 if (!strcmp(sval, channel_modes[idx])) {
653                                         found = TRUE;
654                                         if (devc->tracking_mode == idx)
655                                                 break;  /* Nothing to do! */
656                                         devc->tracking_mode = idx;
657                                         if (devc->model->modelid >= LPS_304) /* No use to set anything in the smaller models. */
658                                                 return lps_cmd_ok(sdi->conn, "TRACK%1d", devc->tracking_mode);
659                                 }
660                                 if (devc->model->modelid <= LPS_303) /* Only first setting possible for smaller models. */
661                                         break;
662                         }
663                         if (!found) {
664                                 return SR_ERR_ARG;
665                         }
666                         break;
667                 default:
668                         return SR_ERR_NA;
669                 }
670         } else {
671                 /* Channel group specified: per-channel options. */
672                 /* We only ever have one channel per channel group in this driver. */
673                 ch = cg->channels->data;
674                 ch_idx = ch->index;
675
676                 switch (key) {
677                 case SR_CONF_OUTPUT_VOLTAGE_TARGET:
678                         dval = g_variant_get_double(data);
679                         if (dval < 0 || dval > devc->model->channels[ch_idx].voltage[1])
680                                 return SR_ERR_ARG;
681                         if (ch_idx == 2) {
682                                 if (devc->model->modelid < LPS_304)
683                                         return SR_ERR_ARG;
684
685                                 if (fabs(dval - 5.000) <= 0.001)
686                                         dval = 5.0;
687                                 else if ((devc->model->modelid >= LPS_305) && (fabs(dval - 3.300) <= 0.001))
688                                         dval = 3.3;
689                                 else return SR_ERR_ARG;
690                         }
691
692                         devc->channel_status[ch_idx].output_voltage_max = dval;
693                         if (ch_idx == 2)
694                                 return lps_cmd_ok(sdi->conn, "VDD%1.0f", trunc(dval));
695                         else
696                                 return lps_cmd_ok(sdi->conn, "VSET%d %05.3f", ch_idx+1, dval);
697                         break;
698                 case SR_CONF_OUTPUT_CURRENT_LIMIT:
699                         dval = g_variant_get_double(data);
700                         if (dval < 0 || dval > devc->model->channels[ch_idx].current[1])
701                                 return SR_ERR_ARG;
702                         if (ch_idx == 2) /* No current setting for CH3. */
703                                 return SR_ERR_NA;
704                         devc->channel_status[ch_idx].output_current_max = dval;
705                         return lps_cmd_ok(sdi->conn, "ISET%d %05.4f", ch_idx+1, dval);
706                         break;
707                 case SR_CONF_OUTPUT_ENABLED:
708                         bval = g_variant_get_boolean(data);
709                         if (bval == devc->channel_status[ch_idx].output_enabled) /* Nothing to do. */
710                                 break;
711                         devc->channel_status[ch_idx].output_enabled = bval;
712                         if (ch_idx != 2) { /* Channels 1,2 can be set only together. */
713                                 devc->channel_status[ch_idx^1].output_enabled = bval;
714                                 return lps_cmd_ok(sdi->conn, "OUT%1d", (int)bval);
715                         } else { /* Channel 3: No command to disable output, set voltage to 0 instead. */
716                                 if (bval)
717                                         return lps_cmd_ok(sdi->conn, "VDD%1.0f", devc->channel_status[ch_idx].output_voltage_max);
718                                 else
719                                         return lps_cmd_ok(sdi->conn, "VDD0");
720                         }
721                         break;
722                 default:
723                         return SR_ERR_NA;
724                 }
725         }
726
727         return SR_OK;
728 }
729
730 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
731                 const struct sr_channel_group *cg)
732 {
733         struct dev_context *devc;
734         struct sr_channel *ch;
735         int ch_idx, i;
736         GVariant *gvar;
737         GVariantBuilder gvb;
738
739         (void)data;
740
741         /* Driver options, no device instance necessary. */
742         switch (key) {
743         case SR_CONF_SCAN_OPTIONS:
744                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
745                                                   scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
746                 return SR_OK;
747         case SR_CONF_DEVICE_OPTIONS:
748                 if (sdi != NULL)
749                         break;
750                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
751                                 drvopts, ARRAY_SIZE(drvopts), sizeof(uint32_t));
752                 return SR_OK;
753         default:
754                 if (sdi == NULL)
755                         return SR_ERR_ARG;
756
757                 devc = sdi->priv;
758         }
759
760         /* Device options, independent from channel groups. */
761         if (cg == NULL) {
762                 switch (key) {
763                 case SR_CONF_DEVICE_OPTIONS:
764                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
765                                         devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
766                         return SR_OK;
767                 case SR_CONF_OUTPUT_CHANNEL_CONFIG:
768                         if (devc->model->modelid <= LPS_303) {
769                                 /* The 1-channel models. */
770                                 *data = g_variant_new_strv(channel_modes, 1);
771                         } else {
772                                 /* The other models support all modes. */
773                                 *data = g_variant_new_strv(channel_modes, ARRAY_SIZE(channel_modes));
774                         }
775                         return SR_OK;
776                         break;
777                 default:
778                         return SR_ERR_NA;
779                 }
780         }
781
782         /* Device options, depending on channel groups. */
783         ch = cg->channels->data;
784         ch_idx = ch->index;
785         switch (key) {
786         case SR_CONF_DEVICE_OPTIONS:
787                 if ((ch_idx == 0) || (ch_idx == 1)) /* CH1, CH2 */
788                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
789                                   devopts_ch12, ARRAY_SIZE(devopts_ch12), sizeof(uint32_t));
790                 else /* Must be CH3 */
791                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
792                                   devopts_ch3, ARRAY_SIZE(devopts_ch3), sizeof(uint32_t));
793                 break;
794         case SR_CONF_OUTPUT_VOLTAGE_TARGET:
795                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
796                 /* Min, max, step. */
797                 for (i = 0; i < 3; i++) {
798                         gvar = g_variant_new_double(devc->model->channels[ch_idx].voltage[i]);
799                         g_variant_builder_add_value(&gvb, gvar);
800                 }
801                 *data = g_variant_builder_end(&gvb);
802                 break;
803         case SR_CONF_OUTPUT_CURRENT_LIMIT:
804                 g_variant_builder_init(&gvb, G_VARIANT_TYPE_ARRAY);
805                 /* Min, max, step. */
806                 for (i = 0; i < 3; i++) {
807                         gvar = g_variant_new_double(devc->model->channels[ch_idx].current[i]);
808                         g_variant_builder_add_value(&gvb, gvar);
809                 }
810                 *data = g_variant_builder_end(&gvb);
811                 break;
812         default:
813                 return SR_ERR_NA;
814         }
815
816         return SR_OK;
817 }
818
819 static int dev_acquisition_start(const struct sr_dev_inst *sdi,
820                                     void *cb_data)
821 {
822         struct dev_context *devc;
823         struct sr_serial_dev_inst *serial;
824
825         if (sdi->status != SR_ST_ACTIVE)
826                 return SR_ERR_DEV_CLOSED;
827
828         devc = sdi->priv;
829
830         devc->acq_running = TRUE;
831
832         serial = sdi->conn;
833         serial_source_add(sdi->session, serial, G_IO_IN, 50,
834                         motech_lps_30x_receive_data, (void *)sdi);
835         std_session_send_df_header(cb_data, LOG_PREFIX);
836
837         /* Start timer, if required. */
838         if (devc->limit_msec)
839                 g_timer_start(devc->elapsed_msec);
840
841         devc->acq_req = AQ_NONE;
842         /* Do not start polling device here, the read function will do it in 50 ms. */
843
844         return SR_OK;
845 }
846
847 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
848 {
849         struct dev_context *devc;
850
851         /* Stop timer, if required. */
852         if (sdi && (devc = sdi->priv) && devc->limit_msec)
853                 g_timer_stop(devc->elapsed_msec);
854
855         return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
856                         sdi->conn, LOG_PREFIX);
857 }
858
859 SR_PRIV struct sr_dev_driver motech_lps_301_driver_info = {
860         .name = "motech-lps-301",
861         .longname = "Motech LPS-301",
862         .api_version = 1,
863         .init = init_lps301,
864         .cleanup = cleanup,
865         .scan = scan_lps301,
866         .dev_list = dev_list_lps301,
867         .dev_clear = dev_clear_lps301,
868         .config_get = config_get,
869         .config_set = config_set,
870         .config_list = config_list,
871         .dev_open = std_serial_dev_open,
872         .dev_close = std_serial_dev_close,
873         .dev_acquisition_start = dev_acquisition_start,
874         .dev_acquisition_stop = dev_acquisition_stop,
875         .priv = NULL,
876 };