]> sigrok.org Git - libsigrok.git/blob - src/std.c
sr_dev_close(): Factor out SR_ERR_DEV_CLOSED check.
[libsigrok.git] / src / std.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, see <http://www.gnu.org/licenses/>.
18  */
19
20 /**
21  * @file
22  *
23  * Standard API helper functions.
24  *
25  * @internal
26  */
27
28 #include <config.h>
29 #include <glib.h>
30 #include <libsigrok/libsigrok.h>
31 #include "libsigrok-internal.h"
32 #include "scpi.h"
33
34 #define LOG_PREFIX "std"
35
36 /**
37  * Standard sr_driver_init() API helper.
38  *
39  * This function can be used to simplify most driver's init() API callback.
40  *
41  * It creates a new 'struct drv_context' (drvc), assigns sr_ctx to it, and
42  * then 'drvc' is assigned to the 'struct sr_dev_driver' (di) that is passed.
43  *
44  * @param di The driver instance to use.
45  * @param sr_ctx The libsigrok context to assign.
46  *
47  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments.
48  */
49 SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
50 {
51         struct drv_context *drvc;
52
53         drvc = g_malloc0(sizeof(struct drv_context));
54         drvc->sr_ctx = sr_ctx;
55         drvc->instances = NULL;
56         di->context = drvc;
57
58         return SR_OK;
59 }
60
61 /**
62  * Standard driver cleanup() callback API helper
63  *
64  * @param di The driver instance to use.
65  *
66  * Frees all device instances by calling sr_dev_clear() and then releases any
67  * resources allocated by std_init().
68  *
69  * @retval SR_OK Success
70  * @retval SR_ERR_ARG Invalid driver
71  *
72 */
73 SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
74 {
75         int ret;
76
77         ret = sr_dev_clear(di);
78         g_free(di->context);
79
80         return ret;
81 }
82
83 /**
84  * Standard API helper for sending an SR_DF_HEADER packet.
85  *
86  * This function can be used to simplify most driver's
87  * dev_acquisition_start() API callback.
88  *
89  * @param sdi The device instance to use.
90  *
91  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
92  *         SR_ERR upon other errors.
93  */
94 SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
95 {
96         const char *prefix = (sdi->driver) ? sdi->driver->name : "unknown";
97         int ret;
98         struct sr_datafeed_packet packet;
99         struct sr_datafeed_header header;
100
101         /* Send header packet to the session bus. */
102         sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
103         packet.type = SR_DF_HEADER;
104         packet.payload = (uint8_t *)&header;
105         header.feed_version = 1;
106         gettimeofday(&header.starttime, NULL);
107
108         if ((ret = sr_session_send(sdi, &packet)) < 0) {
109                 sr_err("%s: Failed to send header packet: %d.", prefix, ret);
110                 return ret;
111         }
112
113         return SR_OK;
114 }
115
116 /**
117  * Standard API helper for sending an SR_DF_END packet.
118  *
119  * @param sdi The device instance to use. Must not be NULL.
120  *
121  * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
122  *         SR_ERR upon other errors.
123  */
124 SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
125 {
126         const char *prefix = (sdi->driver) ? sdi->driver->name : "unknown";
127         int ret;
128         struct sr_datafeed_packet packet;
129
130         if (!sdi)
131                 return SR_ERR_ARG;
132
133         sr_dbg("%s: Sending SR_DF_END packet.", prefix);
134
135         packet.type = SR_DF_END;
136         packet.payload = NULL;
137
138         if ((ret = sr_session_send(sdi, &packet)) < 0) {
139                 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
140                 return ret;
141         }
142
143         return SR_OK;
144 }
145
146 #ifdef HAVE_LIBSERIALPORT
147
148 /**
149  * Standard serial driver dev_open() helper.
150  *
151  * This function can be used to implement the dev_open() driver API
152  * callback in drivers that use a serial port. The port is opened
153  * with the SERIAL_RDWR flag.
154  *
155  * If the open succeeded, the status field of the given sdi is set
156  * to SR_ST_ACTIVE.
157  *
158  * @retval SR_OK Success.
159  * @retval SR_ERR Serial port open failed.
160  */
161 SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
162 {
163         struct sr_serial_dev_inst *serial;
164
165         serial = sdi->conn;
166         if (serial_open(serial, SERIAL_RDWR) != SR_OK)
167                 return SR_ERR;
168
169         sdi->status = SR_ST_ACTIVE;
170
171         return SR_OK;
172 }
173
174 /**
175  * Standard serial driver dev_close() helper.
176  *
177  * This function can be used to implement the dev_close() driver API
178  * callback in drivers that use a serial port.
179  *
180  * After closing the port, the status field of the given sdi is set
181  * to SR_ST_INACTIVE.
182  *
183  * @retval SR_OK Success.
184  * @retval SR_ERR_ARG Invalid arguments.
185  */
186 SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
187 {
188         struct sr_serial_dev_inst *serial;
189
190         sdi->status = SR_ST_INACTIVE;
191
192         serial = sdi->conn;
193         if (!serial) {
194                 sr_err("%s: Can't close invalid serial port.", sdi->driver->name);
195                 return SR_ERR_ARG;
196         }
197
198         serial_close(serial);
199
200         return SR_OK;
201 }
202
203 /**
204  * Standard sr_session_stop() API helper.
205  *
206  * This function can be used to simplify most (serial port based) driver's
207  * dev_acquisition_stop() API callback.
208  *
209  * @param sdi The device instance for which acquisition should stop.
210  *            Must not be NULL.
211  *
212  * @retval SR_OK Success.
213  * @retval SR_ERR_ARG Invalid arguments.
214  * @retval SR_ERR_DEV_CLOSED Device is closed.
215  * @retval SR_ERR Other errors.
216  */
217 SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
218 {
219         struct sr_serial_dev_inst *serial = sdi->conn;
220         const char *prefix = sdi->driver->name;
221         int ret;
222
223         if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
224                 sr_err("%s: Failed to remove source: %d.", prefix, ret);
225                 return ret;
226         }
227
228         if ((ret = sr_dev_close(sdi)) < 0) {
229                 sr_err("%s: Failed to close device: %d.", prefix, ret);
230                 return ret;
231         }
232
233         std_session_send_df_end(sdi);
234
235         return SR_OK;
236 }
237
238 #endif
239
240 /**
241  * Standard driver dev_clear() helper.
242  *
243  * Clear driver, this means, close all instances.
244  *
245  * This function can be used to implement the dev_clear() driver API
246  * callback. dev_close() is called before every sr_dev_inst is cleared.
247  *
248  * The only limitation is driver-specific device contexts (sdi->priv).
249  * These are freed, but any dynamic allocation within structs stored
250  * there cannot be freed.
251  *
252  * @param driver The driver which will have its instances released.
253  * @param clear_private If not NULL, this points to a function called
254  * with sdi->priv as argument. The function can then clear any device
255  * instance-specific resources kept there. It must also clear the struct
256  * pointed to by sdi->priv.
257  *
258  * @return SR_OK on success.
259  */
260 SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver,
261                 std_dev_clear_callback clear_private)
262 {
263         struct drv_context *drvc;
264         struct sr_dev_inst *sdi;
265         GSList *l;
266         int ret;
267
268         if (!(drvc = driver->context))
269                 /* Driver was never initialized, nothing to do. */
270                 return SR_OK;
271
272         ret = SR_OK;
273         for (l = drvc->instances; l; l = l->next) {
274                 if (!(sdi = l->data)) {
275                         ret = SR_ERR_BUG;
276                         continue;
277                 }
278                 if (driver->dev_close)
279                         driver->dev_close(sdi);
280
281                 if (sdi->conn) {
282 #ifdef HAVE_LIBSERIALPORT
283                         if (sdi->inst_type == SR_INST_SERIAL)
284                                 sr_serial_dev_inst_free(sdi->conn);
285 #endif
286 #ifdef HAVE_LIBUSB_1_0
287                         if (sdi->inst_type == SR_INST_USB)
288                                 sr_usb_dev_inst_free(sdi->conn);
289 #endif
290                         if (sdi->inst_type == SR_INST_SCPI)
291                                 sr_scpi_free(sdi->conn);
292                         if (sdi->inst_type == SR_INST_MODBUS)
293                                 sr_modbus_free(sdi->conn);
294                 }
295                 if (clear_private)
296                         /* The helper function is responsible for freeing
297                          * its own sdi->priv! */
298                         clear_private(sdi->priv);
299                 else
300                         g_free(sdi->priv);
301
302                 sr_dev_inst_free(sdi);
303         }
304
305         g_slist_free(drvc->instances);
306         drvc->instances = NULL;
307
308         return ret;
309 }
310
311 /**
312  * Standard implementation for the driver dev_list() callback
313  *
314  * This function can be used as the dev_list callback by most drivers that use
315  * the standard helper functions. It returns the devices contained in the driver
316  * context instances list.
317  *
318  * @param di The driver instance to use.
319  *
320  * @return The list of devices/instances of this driver, or NULL upon errors
321  *         or if the list is empty.
322  */
323 SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
324 {
325         struct drv_context *drvc = di->context;
326
327         return drvc->instances;
328 }
329
330 /**
331  * Standard scan() callback API helper.
332  *
333  * This function can be used to perform common tasks required by a driver's
334  * scan() callback. It will initialize the driver for each device on the list
335  * and add the devices on the list to the driver's device instance list.
336  * Usually it should be used as the last step in the scan() callback, right
337  * before returning.
338  *
339  * Note: This function can only be used if std_init() has been called
340  * previously by the driver.
341  *
342  * Example:
343  * @code{c}
344  * static GSList *scan(struct sr_dev_driver *di, GSList *options)
345  * {
346  *     struct GSList *device;
347  *     struct sr_dev_inst *sdi;
348  *
349  *     sdi = g_new0(sr_dev_inst, 1);
350  *     sdi->vendor = ...;
351  *     ...
352  *     devices = g_slist_append(devices, sdi);
353  *     ...
354  *     return std_scan_complete(di, devices);
355  * }
356  * @endcode
357  *
358  * @param di The driver instance to use. Must not be NULL.
359  * @param devices List of newly discovered devices (struct sr_dev_inst).
360  *
361  * @return The @p devices list.
362  */
363 SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
364 {
365         struct drv_context *drvc;
366         GSList *l;
367
368         if (!di) {
369                 sr_err("Invalid driver instance (di), cannot complete scan.");
370                 return NULL;
371         }
372
373         drvc = di->context;
374
375         for (l = devices; l; l = l->next) {
376                 struct sr_dev_inst *sdi = l->data;
377                 if (!sdi) {
378                         sr_err("Invalid driver instance, cannot complete scan.");
379                         return NULL;
380                 }
381                 sdi->driver = di;
382         }
383
384         drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
385
386         return devices;
387 }