]> sigrok.org Git - libsigrok.git/blob - src/hardware/conrad-digi-35-cpu/api.c
Use g_malloc0() consistently, simplify error handling.
[libsigrok.git] / src / hardware / conrad-digi-35-cpu / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2014 Matthias Heidbrink <m-sigrok@heidbrink.biz>
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 3 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, see <http://www.gnu.org/licenses/>.
18  */
19
20 /** @file
21  *  <em>Conrad DIGI 35 CPU</em> power supply driver
22  *  @internal
23  */
24
25 #include "protocol.h"
26
27 #define SERIALCOMM "9600/8n1"
28
29 static const uint32_t scanopts[] = {
30         SR_CONF_CONN,
31         SR_CONF_SERIALCOMM,
32 };
33
34 static const uint32_t devopts[] = {
35         SR_CONF_POWER_SUPPLY,
36         SR_CONF_OUTPUT_VOLTAGE | SR_CONF_SET,
37         SR_CONF_OUTPUT_CURRENT | SR_CONF_SET,
38         SR_CONF_OVER_CURRENT_PROTECTION_ENABLED | SR_CONF_SET,
39 };
40
41 SR_PRIV struct sr_dev_driver conrad_digi_35_cpu_driver_info;
42 static struct sr_dev_driver *di = &conrad_digi_35_cpu_driver_info;
43
44 static int init(struct sr_context *sr_ctx)
45 {
46         return std_init(sr_ctx, di, LOG_PREFIX);
47 }
48
49 static GSList *scan(GSList *options)
50 {
51         struct sr_dev_inst *sdi;
52         struct drv_context *drvc;
53         struct sr_config *src;
54         struct sr_channel *ch;
55         struct sr_serial_dev_inst *serial;
56         GSList *l, *devices;
57         const char *conn, *serialcomm;
58
59         devices = NULL;
60         drvc = di->priv;
61         drvc->instances = NULL;
62         conn = serialcomm = NULL;
63
64         for (l = options; l; l = l->next) {
65                 src = l->data;
66                 switch (src->key) {
67                 case SR_CONF_CONN:
68                         conn = g_variant_get_string(src->data, NULL);
69                         break;
70                 case SR_CONF_SERIALCOMM:
71                         serialcomm = g_variant_get_string(src->data, NULL);
72                         break;
73                 }
74         }
75         if (!conn)
76                 return NULL;
77         if (!serialcomm)
78                 serialcomm = SERIALCOMM;
79
80         /*
81          * We cannot scan for this device because it is write-only.
82          * So just check that the port parameters are valid and assume that
83          * the device is there.
84          */
85
86         serial = sr_serial_dev_inst_new(conn, serialcomm);
87
88         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
89                 return NULL;
90
91         serial_flush(serial);
92         serial_close(serial);
93
94         sr_spew("Conrad DIGI 35 CPU assumed at %s.", conn);
95
96         sdi = g_malloc0(sizeof(struct sr_dev_inst));
97         sdi->status = SR_ST_ACTIVE;
98         sdi->vendor = g_strdup("Conrad");
99         sdi->model = g_strdup("DIGI 35 CPU");
100         sdi->conn = serial;
101         sdi->priv = NULL;
102         sdi->driver = di;
103         ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "CH1");
104         sdi->channels = g_slist_append(sdi->channels, ch);
105         drvc->instances = g_slist_append(drvc->instances, sdi);
106         devices = g_slist_append(devices, sdi);
107
108         return devices;
109 }
110
111 static GSList *dev_list(void)
112 {
113         return ((struct drv_context *)(di->priv))->instances;
114 }
115
116 static int cleanup(void)
117 {
118         return std_dev_clear(di, NULL);
119 }
120
121 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
122                 const struct sr_channel_group *cg)
123 {
124         int ret;
125         double dblval;
126
127         (void)cg;
128
129         if (sdi->status != SR_ST_ACTIVE)
130                 return SR_ERR_DEV_CLOSED;
131
132         ret = SR_OK;
133         switch (key) {
134         case SR_CONF_OUTPUT_VOLTAGE:
135                 dblval = g_variant_get_double(data);
136                 if ((dblval < 0.0) || (dblval > 35.0)) {
137                         sr_err("Voltage out of range (0 - 35.0)!");
138                         return SR_ERR_ARG;
139                 }
140                 ret = send_msg1(sdi, 'V', (int) (dblval * 10 + 0.5));
141                 break;
142         case SR_CONF_OUTPUT_CURRENT:
143                 dblval = g_variant_get_double(data);
144                 if ((dblval < 0.01) || (dblval > 2.55)) {
145                         sr_err("Current out of range (0 - 2.55)!");
146                         return SR_ERR_ARG;
147                 }
148                 ret = send_msg1(sdi, 'C', (int) (dblval * 100 + 0.5));
149                 break;
150         case SR_CONF_OVER_CURRENT_PROTECTION_ENABLED:
151                 if (g_variant_get_boolean(data))
152                         ret = send_msg1(sdi, 'V', 900);
153                 else /* Constant current mode */
154                         ret = send_msg1(sdi, 'V', 901);
155                 break;
156         default:
157                 ret = SR_ERR_NA;
158         }
159
160         return ret;
161 }
162
163 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
164                 const struct sr_channel_group *cg)
165 {
166         int ret;
167
168         (void)sdi;
169         (void)cg;
170
171         ret = SR_OK;
172         switch (key) {
173         case SR_CONF_SCAN_OPTIONS:
174                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
175                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
176                 break;
177         case SR_CONF_DEVICE_OPTIONS:
178                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
179                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
180                 break;
181         default:
182                 return SR_ERR_NA;
183         }
184
185         return ret;
186 }
187
188 static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
189 {
190         (void)cb_data;
191
192         if (sdi->status != SR_ST_ACTIVE)
193                 return SR_ERR_DEV_CLOSED;
194
195         return SR_OK;
196 }
197
198 static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
199 {
200         (void)cb_data;
201
202         if (sdi->status != SR_ST_ACTIVE)
203                 return SR_ERR_DEV_CLOSED;
204
205         return SR_OK;
206 }
207
208 SR_PRIV struct sr_dev_driver conrad_digi_35_cpu_driver_info = {
209         .name = "conrad-digi-35-cpu",
210         .longname = "Conrad DIGI 35 CPU",
211         .api_version = 1,
212         .init = init,
213         .cleanup = cleanup,
214         .scan = scan,
215         .dev_list = dev_list,
216         .dev_clear = NULL,
217         .config_get = NULL,
218         .config_set = config_set,
219         .config_list = config_list,
220         .dev_open = std_serial_dev_open,
221         .dev_close = std_serial_dev_close,
222         .dev_acquisition_start = dev_acquisition_start,
223         .dev_acquisition_stop = dev_acquisition_stop,
224         .priv = NULL,
225 };