hp-59306a: Initial HP 59306A driver.
[libsigrok.git] / src / hardware / hp-59306a / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2021 Frank Stettner <frank-stettner@gmx.net>
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 #include <config.h>
21 #include "scpi.h"
22 #include "protocol.h"
23
24 static const uint32_t scanopts[] = {
25         SR_CONF_CONN,
26 };
27
28 static const uint32_t drvopts[] = {
29         SR_CONF_MULTIPLEXER,
30 };
31
32 static const uint32_t devopts[] = {
33         /*
34          * TODO Enable/disable multiple channel groups at once.
35          * SR_CONF_ENABLED | SR_CONF_SET,
36          */
37 };
38
39 static const uint32_t devopts_cg[] = {
40         SR_CONF_ENABLED | SR_CONF_SET,
41 };
42
43 static struct sr_dev_driver hp_59306a_driver_info;
44
45 static struct sr_dev_inst *probe_device(struct sr_scpi_dev_inst *scpi)
46 {
47         struct sr_dev_inst *sdi;
48         struct dev_context *devc;
49         struct channel_group_context *cgc;
50         size_t idx, nr;
51         struct sr_channel_group *cg;
52
53         sdi = g_malloc0(sizeof(*sdi));
54         sdi->vendor = g_strdup("Hewlett-Packard");
55         sdi->model = g_strdup("59306A");
56         sdi->conn = scpi;
57         sdi->driver = &hp_59306a_driver_info;
58         sdi->inst_type = SR_INST_SCPI;
59
60         devc = g_malloc0(sizeof(*devc));
61         sdi->priv = devc;
62
63         devc->channel_count = 6;
64         for (idx = 0; idx < devc->channel_count; idx++) {
65                 nr = idx + 1;
66
67                 cg = g_malloc0(sizeof(*cg));
68                 cg->name = g_strdup_printf("CH%zu", nr);
69
70                 cgc = g_malloc0(sizeof(*cgc));
71                 cgc->number = nr;
72                 cg->priv = cgc;
73
74                 sdi->channel_groups = g_slist_append(sdi->channel_groups, cg);
75         }
76
77         return sdi;
78 }
79
80 static GSList *scan(struct sr_dev_driver *di, GSList *options)
81 {
82         return sr_scpi_scan(di->context, options, probe_device);
83 }
84
85 static int dev_open(struct sr_dev_inst *sdi)
86 {
87         return sr_scpi_open(sdi->conn);
88 }
89
90 static int dev_close(struct sr_dev_inst *sdi)
91 {
92         return sr_scpi_close(sdi->conn);
93 }
94
95 static int config_set(uint32_t key, GVariant *data,
96         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
97 {
98         gboolean on;
99
100         if (!cg) {
101                 switch (key) {
102                 /* TODO: Enable/disbale multiple channel groups at once. */
103                 case SR_CONF_ENABLED:
104                 default:
105                         return SR_ERR_NA;
106                 }
107         } else {
108                 switch (key) {
109                 case SR_CONF_ENABLED:
110                         on = g_variant_get_boolean(data);
111                         return hp_59306a_switch_cg(sdi, cg, on);
112                 default:
113                         return SR_ERR_NA;
114                 }
115         }
116
117         return SR_OK;
118 }
119
120 static int config_list(uint32_t key, GVariant **data,
121         const struct sr_dev_inst *sdi, const struct sr_channel_group *cg)
122 {
123         if (!cg) {
124                 switch (key) {
125                 case SR_CONF_SCAN_OPTIONS:
126                 case SR_CONF_DEVICE_OPTIONS:
127                         return STD_CONFIG_LIST(key, data, sdi, cg,
128                                 scanopts, drvopts, devopts);
129                 default:
130                         return SR_ERR_NA;
131                 }
132         } else {
133                 switch (key) {
134                 case SR_CONF_DEVICE_OPTIONS:
135                         *data = std_gvar_array_u32(ARRAY_AND_SIZE(devopts_cg));
136                         break;
137                 default:
138                         return SR_ERR_NA;
139                 }
140         }
141
142         return SR_OK;
143 }
144
145 static struct sr_dev_driver hp_59306a_driver_info = {
146         .name = "hp-59306a",
147         .longname = "hp-59306a",
148         .api_version = 1,
149         .init = std_init,
150         .cleanup = std_cleanup,
151         .scan = scan,
152         .dev_list = std_dev_list,
153         .dev_clear = std_dev_clear,
154         .config_get = NULL,
155         .config_set = config_set,
156         .config_list = config_list,
157         .dev_open = dev_open,
158         .dev_close = dev_close,
159         .dev_acquisition_start = std_dummy_dev_acquisition_start,
160         .dev_acquisition_stop = std_dummy_dev_acquisition_stop,
161         .context = NULL,
162 };
163 SR_REGISTER_DEV_DRIVER(hp_59306a_driver_info);