]> sigrok.org Git - libsigrok.git/blame - src/std.c
std: Rename std_dev_clear() to std_dev_clear_with_callback().
[libsigrok.git] / src / std.c
CommitLineData
063e7aef 1/*
50985c20 2 * This file is part of the libsigrok project.
063e7aef
UH
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
2ea1fdf1 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
063e7aef
UH
18 */
19
d9251a2c
UH
20/**
21 * @file
22 *
23 * Standard API helper functions.
24 *
25 * @internal
26 */
04cb9157 27
6ec6c43b 28#include <config.h>
063e7aef 29#include <glib.h>
c1aae900 30#include <libsigrok/libsigrok.h>
063e7aef 31#include "libsigrok-internal.h"
5a1afc09 32#include "scpi.h"
063e7aef 33
3544f848
ML
34#define LOG_PREFIX "std"
35
063e7aef 36/**
12852b03 37 * Standard driver init() callback API helper.
063e7aef 38 *
6078d2c9 39 * This function can be used to simplify most driver's init() API callback.
063e7aef 40 *
12852b03
UH
41 * Create a new 'struct drv_context' (drvc), assign sr_ctx to it, and
42 * then assign 'drvc' to the 'struct sr_dev_driver' (di) that is passed.
063e7aef 43 *
12852b03
UH
44 * @param[in] di The driver instance to use. Must not be NULL.
45 * @param[in] sr_ctx The libsigrok context to assign. May be NULL.
063e7aef 46 *
12852b03
UH
47 * @retval SR_OK Success.
48 * @retval SR_ERR_ARG Invalid argument.
063e7aef 49 */
c45c32ce 50SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx)
063e7aef
UH
51{
52 struct drv_context *drvc;
53
12852b03
UH
54 if (!di) {
55 sr_err("%s: Invalid argument.", __func__);
56 return SR_ERR_ARG;
57 }
58
91219afc 59 drvc = g_malloc0(sizeof(struct drv_context));
063e7aef 60 drvc->sr_ctx = sr_ctx;
c2523f22 61 drvc->instances = NULL;
41812aca 62 di->context = drvc;
063e7aef
UH
63
64 return SR_OK;
65}
4afdfd46 66
700d6b64 67/**
12852b03 68 * Standard driver cleanup() callback API helper.
700d6b64 69 *
12852b03 70 * This function can be used to simplify most driver's cleanup() API callback.
700d6b64 71 *
12852b03 72 * Free all device instances by calling sr_dev_clear() and then release any
700d6b64
LPC
73 * resources allocated by std_init().
74 *
12852b03 75 * @param[in] di The driver instance to use. Must not be NULL.
700d6b64 76 *
12852b03
UH
77 * @retval SR_OK Success.
78 * @retval SR_ERR_ARG Invalid argument.
79 * @retval other Other error.
80 */
700d6b64
LPC
81SR_PRIV int std_cleanup(const struct sr_dev_driver *di)
82{
83 int ret;
84
12852b03
UH
85 if (!di) {
86 sr_err("%s: Invalid argument.", __func__);
87 return SR_ERR_ARG;
88 }
89
700d6b64
LPC
90 ret = sr_dev_clear(di);
91 g_free(di->context);
92
93 return ret;
94}
95
4d67b9d9
UH
96/**
97 * Dummmy driver dev_open() callback API helper.
98 *
99 * @param[in] sdi The device instance to use. May be NULL (unused).
100 *
101 * @retval SR_OK Success.
102 */
103SR_PRIV int std_dummy_dev_open(struct sr_dev_inst *sdi)
104{
105 (void)sdi;
106
107 return SR_OK;
108}
109
110/**
111 * Dummmy driver dev_close() callback API helper.
112 *
113 * @param[in] sdi The device instance to use. May be NULL (unused).
114 *
115 * @retval SR_OK Success.
116 */
117SR_PRIV int std_dummy_dev_close(struct sr_dev_inst *sdi)
118{
119 (void)sdi;
120
121 return SR_OK;
122}
123
124/**
125 * Dummmy driver dev_acquisition_start() callback API helper.
126 *
127 * @param[in] sdi The device instance to use. May be NULL (unused).
128 *
129 * @retval SR_OK Success.
130 */
131SR_PRIV int std_dummy_dev_acquisition_start(const struct sr_dev_inst *sdi)
132{
133 (void)sdi;
134
135 return SR_OK;
136}
137
138/**
139 * Dummmy driver dev_acquisition_stop() callback API helper.
140 *
141 * @param[in] sdi The device instance to use. May be NULL (unused).
142 *
143 * @retval SR_OK Success.
144 */
145SR_PRIV int std_dummy_dev_acquisition_stop(struct sr_dev_inst *sdi)
146{
147 (void)sdi;
148
149 return SR_OK;
150}
151
4afdfd46
UH
152/**
153 * Standard API helper for sending an SR_DF_HEADER packet.
154 *
12852b03 155 * This function can be used to simplify most drivers'
6078d2c9 156 * dev_acquisition_start() API callback.
4afdfd46 157 *
12852b03 158 * @param[in] sdi The device instance to use. Must not be NULL.
4afdfd46 159 *
12852b03
UH
160 * @retval SR_OK Success.
161 * @retval SR_ERR_ARG Invalid argument.
162 * @retval other Other error.
4afdfd46 163 */
bee2b016 164SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi)
4afdfd46 165{
12852b03 166 const char *prefix;
4afdfd46
UH
167 int ret;
168 struct sr_datafeed_packet packet;
169 struct sr_datafeed_header header;
170
12852b03
UH
171 if (!sdi) {
172 sr_err("%s: Invalid argument.", __func__);
173 return SR_ERR_ARG;
174 }
175
176 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
177
4afdfd46 178 /* Send header packet to the session bus. */
ac2926b3 179 sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
4afdfd46
UH
180 packet.type = SR_DF_HEADER;
181 packet.payload = (uint8_t *)&header;
182 header.feed_version = 1;
183 gettimeofday(&header.starttime, NULL);
184
185 if ((ret = sr_session_send(sdi, &packet)) < 0) {
12852b03 186 sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret);
4afdfd46
UH
187 return ret;
188 }
189
190 return SR_OK;
191}
cd2f0fe2 192
3be42bc2
UH
193/**
194 * Standard API helper for sending an SR_DF_END packet.
195 *
12852b03
UH
196 * This function can be used to simplify most drivers'
197 * dev_acquisition_stop() API callback.
198 *
199 * @param[in] sdi The device instance to use. Must not be NULL.
3be42bc2 200 *
12852b03
UH
201 * @retval SR_OK Success.
202 * @retval SR_ERR_ARG Invalid argument.
203 * @retval other Other error.
3be42bc2 204 */
bee2b016 205SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi)
3be42bc2 206{
12852b03 207 const char *prefix;
3be42bc2
UH
208 int ret;
209 struct sr_datafeed_packet packet;
210
12852b03
UH
211 if (!sdi) {
212 sr_err("%s: Invalid argument.", __func__);
3be42bc2 213 return SR_ERR_ARG;
12852b03
UH
214 }
215
216 prefix = (sdi->driver) ? sdi->driver->name : "unknown";
3be42bc2
UH
217
218 sr_dbg("%s: Sending SR_DF_END packet.", prefix);
219
220 packet.type = SR_DF_END;
221 packet.payload = NULL;
222
223 if ((ret = sr_session_send(sdi, &packet)) < 0) {
224 sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
225 return ret;
226 }
227
228 return SR_OK;
229}
230
c4f2dfd0
UH
231#ifdef HAVE_LIBSERIALPORT
232
813aab69 233/**
12852b03 234 * Standard serial driver dev_open() callback API helper.
23dc6661
BV
235 *
236 * This function can be used to implement the dev_open() driver API
237 * callback in drivers that use a serial port. The port is opened
6c592ece 238 * with the SERIAL_RDWR flag.
23dc6661 239 *
12852b03
UH
240 * @param[in] sdi The device instance to use. Must not be NULL.
241 *
23dc6661 242 * @retval SR_OK Success.
12852b03
UH
243 * @retval SR_ERR_ARG Invalid argument.
244 * @retval other Serial port open failed.
23dc6661
BV
245 */
246SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
247{
248 struct sr_serial_dev_inst *serial;
249
12852b03
UH
250 if (!sdi) {
251 sr_err("%s: Invalid argument.", __func__);
7e463623 252 return SR_ERR_ARG;
12852b03 253 }
23dc6661 254
7e463623 255 serial = sdi->conn;
23dc6661 256
7e463623 257 return serial_open(serial, SERIAL_RDWR);
23dc6661
BV
258}
259
813aab69 260/**
12852b03 261 * Standard serial driver dev_close() callback API helper.
1e7134dc
BV
262 *
263 * This function can be used to implement the dev_close() driver API
264 * callback in drivers that use a serial port.
265 *
12852b03
UH
266 * @param[in] sdi The device instance to use. Must not be NULL.
267 *
1e7134dc 268 * @retval SR_OK Success.
12852b03
UH
269 * @retval SR_ERR_ARG Invalid argument.
270 * @retval other Serial port close failed.
1e7134dc
BV
271 */
272SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
273{
274 struct sr_serial_dev_inst *serial;
275
12852b03
UH
276 if (!sdi) {
277 sr_err("%s: Invalid argument.", __func__);
093e1cba 278 return SR_ERR_ARG;
12852b03 279 }
1e7134dc 280
f1ba6b4b 281 serial = sdi->conn;
093e1cba 282
f1ba6b4b 283 return serial_close(serial);
1e7134dc
BV
284}
285
813aab69 286/**
12852b03 287 * Standard serial driver dev_acquisition_stop() callback API helper.
cd2f0fe2 288 *
12852b03 289 * This function can be used to simplify most (serial port based) drivers'
6078d2c9 290 * dev_acquisition_stop() API callback.
cd2f0fe2 291 *
12852b03
UH
292 * @param[in] sdi The device instance for which acquisition should stop.
293 * Must not be NULL.
cd2f0fe2 294 *
1477a9a6 295 * @retval SR_OK Success.
12852b03
UH
296 * @retval SR_ERR_ARG Invalid argument.
297 * @retval other Other error.
cd2f0fe2 298 */
1b38775b 299SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi)
cd2f0fe2 300{
12852b03
UH
301 struct sr_serial_dev_inst *serial;
302 const char *prefix;
cd2f0fe2 303 int ret;
cd2f0fe2 304
12852b03
UH
305 if (!sdi) {
306 sr_err("%s: Invalid argument.", __func__);
307 return SR_ERR_ARG;
308 }
309
310 serial = sdi->conn;
311 prefix = sdi->driver->name;
312
102f1239 313 if ((ret = serial_source_remove(sdi->session, serial)) < 0) {
ac2926b3 314 sr_err("%s: Failed to remove source: %d.", prefix, ret);
cd2f0fe2
UH
315 return ret;
316 }
317
093e1cba 318 if ((ret = sr_dev_close(sdi)) < 0) {
ac2926b3 319 sr_err("%s: Failed to close device: %d.", prefix, ret);
cd2f0fe2
UH
320 return ret;
321 }
322
12852b03 323 return std_session_send_df_end(sdi);
cd2f0fe2 324}
49f00e13 325
c4f2dfd0
UH
326#endif
327
813aab69 328/**
12852b03 329 * Standard driver dev_clear() callback API helper.
49f00e13 330 *
813aab69
MH
331 * Clear driver, this means, close all instances.
332 *
49f00e13
BV
333 * This function can be used to implement the dev_clear() driver API
334 * callback. dev_close() is called before every sr_dev_inst is cleared.
335 *
336 * The only limitation is driver-specific device contexts (sdi->priv).
337 * These are freed, but any dynamic allocation within structs stored
338 * there cannot be freed.
339 *
12852b03
UH
340 * @param[in] driver The driver which will have its instances released.
341 * Must not be NULL.
342 * @param[in] clear_private If not NULL, this points to a function called
343 * with sdi->priv as argument. The function can then clear
344 * any device instance-specific resources kept there.
345 * It must also clear the struct pointed to by sdi->priv.
49f00e13 346 *
12852b03
UH
347 * @retval SR_OK Success.
348 * @retval SR_ERR_ARG Invalid argument.
349 * @retval SR_ERR_BUG Implementation bug.
350 * @retval other Other error.
49f00e13 351 */
6e43c3d5 352SR_PRIV int std_dev_clear_with_callback(const struct sr_dev_driver *driver,
144f6660 353 std_dev_clear_callback clear_private)
49f00e13 354{
49f00e13 355 struct drv_context *drvc;
12a33563 356 struct sr_dev_inst *sdi;
7aebe22d 357 GSList *l;
49f00e13
BV
358 int ret;
359
12852b03
UH
360 if (!driver) {
361 sr_err("%s: Invalid argument.", __func__);
362 return SR_ERR_ARG;
363 }
364
365 drvc = driver->context; /* Caller checked for context != NULL. */
3a277f3b 366
49f00e13
BV
367 ret = SR_OK;
368 for (l = drvc->instances; l; l = l->next) {
49f00e13 369 if (!(sdi = l->data)) {
12852b03 370 sr_err("%s: Invalid device instance.", __func__);
49f00e13
BV
371 ret = SR_ERR_BUG;
372 continue;
373 }
49f00e13
BV
374 if (driver->dev_close)
375 driver->dev_close(sdi);
376
377 if (sdi->conn) {
45357ce6 378#ifdef HAVE_LIBSERIALPORT
c4f2dfd0 379 if (sdi->inst_type == SR_INST_SERIAL)
12a33563 380 sr_serial_dev_inst_free(sdi->conn);
c4f2dfd0 381#endif
45357ce6 382#ifdef HAVE_LIBUSB_1_0
c4f2dfd0 383 if (sdi->inst_type == SR_INST_USB)
49f00e13 384 sr_usb_dev_inst_free(sdi->conn);
a0c7e23a 385#endif
23f43dff
ML
386 if (sdi->inst_type == SR_INST_SCPI)
387 sr_scpi_free(sdi->conn);
daa39012
AJ
388 if (sdi->inst_type == SR_INST_MODBUS)
389 sr_modbus_free(sdi->conn);
49f00e13 390 }
ae5859ff 391 if (clear_private)
886413b6
BV
392 /* The helper function is responsible for freeing
393 * its own sdi->priv! */
ae5859ff 394 clear_private(sdi->priv);
12a33563
BV
395 else
396 g_free(sdi->priv);
886413b6 397
49f00e13
BV
398 sr_dev_inst_free(sdi);
399 }
400
401 g_slist_free(drvc->instances);
402 drvc->instances = NULL;
403
404 return ret;
405}
c01bf34c
LPC
406
407/**
12852b03 408 * Standard driver dev_list() callback API helper.
c01bf34c 409 *
12852b03 410 * This function can be used as the dev_list() callback by most drivers.
c01bf34c 411 *
12852b03 412 * Return the devices contained in the driver context instances list.
c01bf34c 413 *
12852b03
UH
414 * @param[in] di The driver instance to use. Must not be NULL.
415 *
416 * @retval NULL Error, or the list is empty.
417 * @retval other The list of device instances of this driver.
c01bf34c
LPC
418 */
419SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di)
420{
12852b03
UH
421 struct drv_context *drvc;
422
423 if (!di) {
424 sr_err("%s: Invalid argument.", __func__);
425 return NULL;
426 }
427
428 drvc = di->context;
c01bf34c
LPC
429
430 return drvc->instances;
431}
15a5bfe4
LPC
432
433/**
12852b03 434 * Standard driver scan() callback API helper.
15a5bfe4
LPC
435 *
436 * This function can be used to perform common tasks required by a driver's
437 * scan() callback. It will initialize the driver for each device on the list
438 * and add the devices on the list to the driver's device instance list.
439 * Usually it should be used as the last step in the scan() callback, right
440 * before returning.
441 *
442 * Note: This function can only be used if std_init() has been called
443 * previously by the driver.
444 *
445 * Example:
446 * @code{c}
447 * static GSList *scan(struct sr_dev_driver *di, GSList *options)
448 * {
449 * struct GSList *device;
450 * struct sr_dev_inst *sdi;
451 *
452 * sdi = g_new0(sr_dev_inst, 1);
453 * sdi->vendor = ...;
454 * ...
455 * devices = g_slist_append(devices, sdi);
456 * ...
457 * return std_scan_complete(di, devices);
458 * }
459 * @endcode
460 *
12852b03
UH
461 * @param[in] di The driver instance to use. Must not be NULL.
462 * @param[in] devices List of newly discovered devices (struct sr_dev_inst).
463 * May be NULL.
15a5bfe4
LPC
464 *
465 * @return The @p devices list.
466 */
467SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices)
468{
39fcfdc9 469 struct drv_context *drvc;
15a5bfe4
LPC
470 GSList *l;
471
39fcfdc9
UH
472 if (!di) {
473 sr_err("Invalid driver instance (di), cannot complete scan.");
474 return NULL;
475 }
476
477 drvc = di->context;
478
15a5bfe4
LPC
479 for (l = devices; l; l = l->next) {
480 struct sr_dev_inst *sdi = l->data;
39fcfdc9 481 if (!sdi) {
12852b03 482 sr_err("Invalid device instance, cannot complete scan.");
39fcfdc9
UH
483 return NULL;
484 }
15a5bfe4
LPC
485 sdi->driver = di;
486 }
487
488 drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices));
489
490 return devices;
491}