]> sigrok.org Git - sigrok-util.git/blob - source/drv-api.c
new-driver: hw_init() now takes a context.
[sigrok-util.git] / source / drv-api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) ${year} ${author} <${email}>
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 <glib.h>
21 #include "libsigrok.h"
22 #include "libsigrok-internal.h"
23 #include "protocol.h"
24
25 SR_PRIV struct sr_dev_driver ${lib}_driver_info;
26 static struct sr_dev_driver *di = &${lib}_driver_info;
27
28 /* Properly close and free all devices. */
29 static int clear_instances(void)
30 {
31         struct sr_dev_inst *sdi;
32         struct drv_context *drvc;
33         struct dev_context *devc;
34         GSList *l;
35
36         if (!(drvc = di->priv))
37                 return SR_OK;
38
39         for (l = drvc->instances; l; l = l->next) {
40                 if (!(sdi = l->data))
41                         continue;
42                 if (!(devc = sdi->priv))
43                         continue;
44
45                 /* TODO */
46
47                 sr_dev_inst_free(sdi);
48         }
49
50         g_slist_free(drvc->instances);
51         drvc->instances = NULL;
52
53         return SR_OK;
54 }
55
56 static int hw_init(struct sr_context *sr_ctx)
57 {
58         struct drv_context *drvc;
59
60         if (!(drvc = g_try_malloc0(sizeof(struct drv_context)))) {
61                 sr_err("Driver context malloc failed.");
62                 return SR_ERR_MALLOC;
63         }
64
65         /* TODO */
66
67         drvc->sr_ctx = sr_ctx;
68         di->priv = drvc;
69
70         return SR_OK;
71 }
72
73 static GSList *hw_scan(GSList *options)
74 {
75         struct drv_context *drvc;
76         GSList *devices;
77
78         (void)options;
79
80         devices = NULL;
81         drvc = di->priv;
82         drvc->instances = NULL;
83
84         /* TODO */
85
86         return devices;
87 }
88
89 static GSList *hw_dev_list(void)
90 {
91         struct drv_context *drvc;
92
93         drvc = di->priv;
94
95         return drvc->instances;
96 }
97
98 static int hw_dev_open(struct sr_dev_inst *sdi)
99 {
100         /* TODO */
101
102         return SR_OK;
103 }
104
105 static int hw_dev_close(struct sr_dev_inst *sdi)
106 {
107         /* TODO */
108
109         return SR_OK;
110 }
111
112 static int hw_cleanup(void)
113 {
114         clear_instances();
115
116         /* TODO */
117
118         return SR_OK;
119 }
120
121 static int hw_info_get(int info_id, const void **data,
122                        const struct sr_dev_inst *sdi)
123 {
124         switch (info_id) {
125         /* TODO */
126         default:
127                 sr_err("Unknown info_id: %d.", info_id);
128                 return SR_ERR_ARG;
129         }
130
131         return SR_OK;
132 }
133
134 static int hw_dev_config_set(const struct sr_dev_inst *sdi, int hwcap,
135                              const void *value)
136 {
137         int ret;
138
139         if (sdi->status != SR_ST_ACTIVE) {
140                 sr_err("Device inactive, can't set config options.");
141                 return SR_ERR;
142         }
143
144         ret = SR_OK;
145         switch (hwcap) {
146         /* TODO */
147         default:
148                 sr_err("Unknown hardware capability: %d.", hwcap);
149                 ret = SR_ERR_ARG;
150         }
151
152         return ret;
153 }
154
155 static int hw_dev_acquisition_start(const struct sr_dev_inst *sdi,
156                                     void *cb_data)
157 {
158         /* TODO */
159
160         return SR_OK;
161 }
162
163 static int hw_dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
164 {
165         (void)cb_data;
166
167         if (sdi->status != SR_ST_ACTIVE) {
168                 sr_err("Device inactive, can't stop acquisition.");
169                 return SR_ERR;
170         }
171
172         /* TODO */
173
174         return SR_OK;
175 }
176
177 SR_PRIV struct sr_dev_driver ${lib}_driver_info = {
178         .name = "${short}",
179         .longname = "${name}",
180         .api_version = 1,
181         .init = hw_init,
182         .cleanup = hw_cleanup,
183         .scan = hw_scan,
184         .dev_list = hw_dev_list,
185         .dev_clear = clear_instances,
186         .dev_open = hw_dev_open,
187         .dev_close = hw_dev_close,
188         .info_get = hw_info_get,
189         .dev_config_set = hw_dev_config_set,
190         .dev_acquisition_start = hw_dev_acquisition_start,
191         .dev_acquisition_stop = hw_dev_acquisition_stop,
192         .priv = NULL,
193 };