]> sigrok.org Git - libsigrok.git/blob - src/hardware/mic-985xx/api.c
Introduce standard cleanup helper
[libsigrok.git] / src / hardware / mic-985xx / api.c
1 /*
2  * This file is part of the libsigrok project.
3  *
4  * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
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 2 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, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  */
20
21 #include <config.h>
22 #include "protocol.h"
23
24 static const uint32_t scanopts[] = {
25         SR_CONF_CONN,
26         SR_CONF_SERIALCOMM,
27 };
28
29 static const uint32_t drvopts_temp[] = {
30         SR_CONF_THERMOMETER,
31 };
32
33 static const uint32_t drvopts_temp_hum[] = {
34         SR_CONF_THERMOMETER,
35         SR_CONF_HYGROMETER,
36 };
37
38 static const uint32_t devopts[] = {
39         SR_CONF_CONTINUOUS | SR_CONF_SET,
40         SR_CONF_LIMIT_SAMPLES | SR_CONF_SET,
41         SR_CONF_LIMIT_MSEC | SR_CONF_SET,
42 };
43
44 SR_PRIV struct sr_dev_driver mic_98581_driver_info;
45 SR_PRIV struct sr_dev_driver mic_98583_driver_info;
46
47 SR_PRIV const struct mic_dev_info mic_devs[] = {
48         {
49                 "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
50                 packet_valid_temp,
51                 &mic_98581_driver_info, receive_data_MIC_98581,
52         },
53         {
54                 "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
55                 packet_valid_temp_hum,
56                 &mic_98583_driver_info, receive_data_MIC_98583,
57         },
58 };
59
60 static int dev_clear(int idx)
61 {
62         return std_dev_clear(mic_devs[idx].di, NULL);
63 }
64
65 static int init(struct sr_context *sr_ctx, int idx)
66 {
67         return std_init(sr_ctx, mic_devs[idx].di, LOG_PREFIX);
68 }
69
70 static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
71 {
72         struct sr_dev_inst *sdi;
73         struct drv_context *drvc;
74         struct dev_context *devc;
75         struct sr_serial_dev_inst *serial;
76         GSList *devices;
77
78         serial = sr_serial_dev_inst_new(conn, serialcomm);
79
80         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
81                 return NULL;
82
83         drvc = mic_devs[idx].di->context;
84         devices = NULL;
85         serial_flush(serial);
86
87         /* TODO: Query device type. */
88         // ret = mic_cmd_get_device_info(serial);
89
90         sr_info("Found device on port %s.", conn);
91
92         /* TODO: Fill in version from protocol response. */
93         sdi = g_malloc0(sizeof(struct sr_dev_inst));
94         sdi->status = SR_ST_INACTIVE;
95         sdi->vendor = g_strdup(mic_devs[idx].vendor);
96         sdi->model = g_strdup(mic_devs[idx].device);
97         devc = g_malloc0(sizeof(struct dev_context));
98         sdi->inst_type = SR_INST_SERIAL;
99         sdi->conn = serial;
100         sdi->priv = devc;
101         sdi->driver = mic_devs[idx].di;
102
103         sr_channel_new(sdi, 0, SR_CHANNEL_ANALOG, TRUE, "Temperature");
104
105         if (mic_devs[idx].has_humidity)
106                 sr_channel_new(sdi, 1, SR_CHANNEL_ANALOG, TRUE, "Humidity");
107
108         drvc->instances = g_slist_append(drvc->instances, sdi);
109         devices = g_slist_append(devices, sdi);
110
111         serial_close(serial);
112
113         return devices;
114 }
115
116 static GSList *scan(GSList *options, int idx)
117 {
118         struct sr_config *src;
119         GSList *l, *devices;
120         const char *conn, *serialcomm;
121
122         conn = serialcomm = NULL;
123         for (l = options; l; l = l->next) {
124                 src = l->data;
125                 switch (src->key) {
126                 case SR_CONF_CONN:
127                         conn = g_variant_get_string(src->data, NULL);
128                         break;
129                 case SR_CONF_SERIALCOMM:
130                         serialcomm = g_variant_get_string(src->data, NULL);
131                         break;
132                 }
133         }
134         if (!conn)
135                 return NULL;
136
137         if (serialcomm) {
138                 /* Use the provided comm specs. */
139                 devices = mic_scan(conn, serialcomm, idx);
140         } else {
141                 /* Try the default. */
142                 devices = mic_scan(conn, mic_devs[idx].conn, idx);
143         }
144
145         return devices;
146 }
147
148 static GSList *dev_list(int idx)
149 {
150         return ((struct drv_context *)(mic_devs[idx].di->context))->instances;
151 }
152
153 static int config_set(uint32_t key, GVariant *data, const struct sr_dev_inst *sdi,
154                 const struct sr_channel_group *cg)
155 {
156         struct dev_context *devc;
157
158         (void)cg;
159
160         if (sdi->status != SR_ST_ACTIVE)
161                 return SR_ERR_DEV_CLOSED;
162
163         devc = sdi->priv;
164
165         switch (key) {
166         case SR_CONF_LIMIT_SAMPLES:
167                 devc->limit_samples = g_variant_get_uint64(data);
168                 break;
169         case SR_CONF_LIMIT_MSEC:
170                 devc->limit_msec = g_variant_get_uint64(data);
171                 break;
172         default:
173                 return SR_ERR_NA;
174         }
175
176         return SR_OK;
177 }
178
179 static int config_list(uint32_t key, GVariant **data, const struct sr_dev_inst *sdi,
180                 const struct sr_channel_group *cg, int idx)
181 {
182         (void)cg;
183
184         switch (key) {
185         case SR_CONF_SCAN_OPTIONS:
186                 *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
187                                 scanopts, ARRAY_SIZE(scanopts), sizeof(uint32_t));
188                 break;
189         case SR_CONF_DEVICE_OPTIONS:
190                 if (!sdi && !mic_devs[idx].has_humidity) {
191                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
192                                 drvopts_temp, ARRAY_SIZE(drvopts_temp),
193                                 sizeof(uint32_t));
194                 } else if (!sdi && mic_devs[idx].has_humidity) {
195                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
196                                 drvopts_temp_hum, ARRAY_SIZE(drvopts_temp_hum),
197                                 sizeof(uint32_t));
198                 } else {
199                         *data = g_variant_new_fixed_array(G_VARIANT_TYPE_UINT32,
200                                 devopts, ARRAY_SIZE(devopts), sizeof(uint32_t));
201                 }
202                 break;
203         default:
204                 return SR_ERR_NA;
205         }
206
207         return SR_OK;
208 }
209
210 static int dev_acquisition_start(const struct sr_dev_inst *sdi, int idx)
211 {
212         struct dev_context *devc;
213         struct sr_serial_dev_inst *serial;
214
215         if (sdi->status != SR_ST_ACTIVE)
216                 return SR_ERR_DEV_CLOSED;
217
218         devc = sdi->priv;
219         devc->num_samples = 0;
220         devc->starttime = g_get_monotonic_time();
221
222         std_session_send_df_header(sdi, LOG_PREFIX);
223
224         /* Poll every 100ms, or whenever some data comes in. */
225         serial = sdi->conn;
226         serial_source_add(sdi->session, serial, G_IO_IN, 100,
227                       mic_devs[idx].receive_data, (void *)sdi);
228
229         return SR_OK;
230 }
231
232 static int dev_acquisition_stop(struct sr_dev_inst *sdi)
233 {
234         return std_serial_dev_acquisition_stop(sdi, std_serial_dev_close,
235                         sdi->conn, LOG_PREFIX);
236 }
237
238 /* Driver-specific API function wrappers */
239 #define HW_INIT(X) \
240 static int init_##X(struct sr_dev_driver *di, struct sr_context *sr_ctx) { \
241         (void)di; return init(sr_ctx, X); }
242 #define HW_SCAN(X) \
243 static GSList *scan_##X(struct sr_dev_driver *di, GSList *options) { \
244         (void)di; return scan(options, X); }
245 #define HW_DEV_LIST(X) \
246 static GSList *dev_list_##X(const struct sr_dev_driver *di) { \
247         (void)di; return dev_list(X); }
248 #define HW_DEV_CLEAR(X) \
249 static int dev_clear_##X(const struct sr_dev_driver *di) { \
250         (void)di; return dev_clear(X); }
251 #define HW_CONFIG_LIST(X) \
252 static int config_list_##X(uint32_t key, GVariant **data, \
253 const struct sr_dev_inst *sdi, const struct sr_channel_group *cg) { \
254 return config_list(key, data, sdi, cg, X); }
255 #define HW_DEV_ACQUISITION_START(X) \
256 static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi \
257 ) { return dev_acquisition_start(sdi, X); }
258
259 /* Driver structs and API function wrappers */
260 #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
261 HW_INIT(ID_UPPER) \
262 HW_SCAN(ID_UPPER) \
263 HW_DEV_LIST(ID_UPPER) \
264 HW_DEV_CLEAR(ID_UPPER) \
265 HW_CONFIG_LIST(ID_UPPER) \
266 HW_DEV_ACQUISITION_START(ID_UPPER) \
267 SR_PRIV struct sr_dev_driver ID##_driver_info = { \
268         .name = NAME, \
269         .longname = LONGNAME, \
270         .api_version = 1, \
271         .init = init_##ID_UPPER, \
272         .cleanup = std_cleanup, \
273         .scan = scan_##ID_UPPER, \
274         .dev_list = dev_list_##ID_UPPER, \
275         .dev_clear = dev_clear_##ID_UPPER, \
276         .config_get = NULL, \
277         .config_set = config_set, \
278         .config_list = config_list_##ID_UPPER, \
279         .dev_open = std_serial_dev_open, \
280         .dev_close = std_serial_dev_close, \
281         .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \
282         .dev_acquisition_stop = dev_acquisition_stop, \
283         .context = NULL, \
284 };
285
286 DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
287 DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")