]>
Commit | Line | Data |
---|---|---|
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 | 50 | SR_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 |
81 | SR_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 | ||
4afdfd46 UH |
96 | /** |
97 | * Standard API helper for sending an SR_DF_HEADER packet. | |
98 | * | |
12852b03 | 99 | * This function can be used to simplify most drivers' |
6078d2c9 | 100 | * dev_acquisition_start() API callback. |
4afdfd46 | 101 | * |
12852b03 | 102 | * @param[in] sdi The device instance to use. Must not be NULL. |
4afdfd46 | 103 | * |
12852b03 UH |
104 | * @retval SR_OK Success. |
105 | * @retval SR_ERR_ARG Invalid argument. | |
106 | * @retval other Other error. | |
4afdfd46 | 107 | */ |
bee2b016 | 108 | SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi) |
4afdfd46 | 109 | { |
12852b03 | 110 | const char *prefix; |
4afdfd46 UH |
111 | int ret; |
112 | struct sr_datafeed_packet packet; | |
113 | struct sr_datafeed_header header; | |
114 | ||
12852b03 UH |
115 | if (!sdi) { |
116 | sr_err("%s: Invalid argument.", __func__); | |
117 | return SR_ERR_ARG; | |
118 | } | |
119 | ||
120 | prefix = (sdi->driver) ? sdi->driver->name : "unknown"; | |
121 | ||
4afdfd46 | 122 | /* Send header packet to the session bus. */ |
ac2926b3 | 123 | sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix); |
4afdfd46 UH |
124 | packet.type = SR_DF_HEADER; |
125 | packet.payload = (uint8_t *)&header; | |
126 | header.feed_version = 1; | |
127 | gettimeofday(&header.starttime, NULL); | |
128 | ||
129 | if ((ret = sr_session_send(sdi, &packet)) < 0) { | |
12852b03 | 130 | sr_err("%s: Failed to send SR_DF_HEADER packet: %d.", prefix, ret); |
4afdfd46 UH |
131 | return ret; |
132 | } | |
133 | ||
134 | return SR_OK; | |
135 | } | |
cd2f0fe2 | 136 | |
3be42bc2 UH |
137 | /** |
138 | * Standard API helper for sending an SR_DF_END packet. | |
139 | * | |
12852b03 UH |
140 | * This function can be used to simplify most drivers' |
141 | * dev_acquisition_stop() API callback. | |
142 | * | |
143 | * @param[in] sdi The device instance to use. Must not be NULL. | |
3be42bc2 | 144 | * |
12852b03 UH |
145 | * @retval SR_OK Success. |
146 | * @retval SR_ERR_ARG Invalid argument. | |
147 | * @retval other Other error. | |
3be42bc2 | 148 | */ |
bee2b016 | 149 | SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi) |
3be42bc2 | 150 | { |
12852b03 | 151 | const char *prefix; |
3be42bc2 UH |
152 | int ret; |
153 | struct sr_datafeed_packet packet; | |
154 | ||
12852b03 UH |
155 | if (!sdi) { |
156 | sr_err("%s: Invalid argument.", __func__); | |
3be42bc2 | 157 | return SR_ERR_ARG; |
12852b03 UH |
158 | } |
159 | ||
160 | prefix = (sdi->driver) ? sdi->driver->name : "unknown"; | |
3be42bc2 UH |
161 | |
162 | sr_dbg("%s: Sending SR_DF_END packet.", prefix); | |
163 | ||
164 | packet.type = SR_DF_END; | |
165 | packet.payload = NULL; | |
166 | ||
167 | if ((ret = sr_session_send(sdi, &packet)) < 0) { | |
168 | sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret); | |
169 | return ret; | |
170 | } | |
171 | ||
172 | return SR_OK; | |
173 | } | |
174 | ||
c4f2dfd0 UH |
175 | #ifdef HAVE_LIBSERIALPORT |
176 | ||
813aab69 | 177 | /** |
12852b03 | 178 | * Standard serial driver dev_open() callback API helper. |
23dc6661 BV |
179 | * |
180 | * This function can be used to implement the dev_open() driver API | |
181 | * callback in drivers that use a serial port. The port is opened | |
6c592ece | 182 | * with the SERIAL_RDWR flag. |
23dc6661 | 183 | * |
12852b03 UH |
184 | * @param[in] sdi The device instance to use. Must not be NULL. |
185 | * | |
23dc6661 | 186 | * @retval SR_OK Success. |
12852b03 UH |
187 | * @retval SR_ERR_ARG Invalid argument. |
188 | * @retval other Serial port open failed. | |
23dc6661 BV |
189 | */ |
190 | SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi) | |
191 | { | |
192 | struct sr_serial_dev_inst *serial; | |
193 | ||
12852b03 UH |
194 | if (!sdi) { |
195 | sr_err("%s: Invalid argument.", __func__); | |
7e463623 | 196 | return SR_ERR_ARG; |
12852b03 | 197 | } |
23dc6661 | 198 | |
7e463623 | 199 | serial = sdi->conn; |
23dc6661 | 200 | |
7e463623 | 201 | return serial_open(serial, SERIAL_RDWR); |
23dc6661 BV |
202 | } |
203 | ||
813aab69 | 204 | /** |
12852b03 | 205 | * Standard serial driver dev_close() callback API helper. |
1e7134dc BV |
206 | * |
207 | * This function can be used to implement the dev_close() driver API | |
208 | * callback in drivers that use a serial port. | |
209 | * | |
12852b03 UH |
210 | * @param[in] sdi The device instance to use. Must not be NULL. |
211 | * | |
1e7134dc | 212 | * @retval SR_OK Success. |
12852b03 UH |
213 | * @retval SR_ERR_ARG Invalid argument. |
214 | * @retval other Serial port close failed. | |
1e7134dc BV |
215 | */ |
216 | SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi) | |
217 | { | |
218 | struct sr_serial_dev_inst *serial; | |
219 | ||
12852b03 UH |
220 | if (!sdi) { |
221 | sr_err("%s: Invalid argument.", __func__); | |
093e1cba | 222 | return SR_ERR_ARG; |
12852b03 | 223 | } |
1e7134dc | 224 | |
f1ba6b4b | 225 | serial = sdi->conn; |
093e1cba | 226 | |
f1ba6b4b | 227 | return serial_close(serial); |
1e7134dc BV |
228 | } |
229 | ||
813aab69 | 230 | /** |
12852b03 | 231 | * Standard serial driver dev_acquisition_stop() callback API helper. |
cd2f0fe2 | 232 | * |
12852b03 | 233 | * This function can be used to simplify most (serial port based) drivers' |
6078d2c9 | 234 | * dev_acquisition_stop() API callback. |
cd2f0fe2 | 235 | * |
12852b03 UH |
236 | * @param[in] sdi The device instance for which acquisition should stop. |
237 | * Must not be NULL. | |
cd2f0fe2 | 238 | * |
1477a9a6 | 239 | * @retval SR_OK Success. |
12852b03 UH |
240 | * @retval SR_ERR_ARG Invalid argument. |
241 | * @retval other Other error. | |
cd2f0fe2 | 242 | */ |
1b38775b | 243 | SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi) |
cd2f0fe2 | 244 | { |
12852b03 UH |
245 | struct sr_serial_dev_inst *serial; |
246 | const char *prefix; | |
cd2f0fe2 | 247 | int ret; |
cd2f0fe2 | 248 | |
12852b03 UH |
249 | if (!sdi) { |
250 | sr_err("%s: Invalid argument.", __func__); | |
251 | return SR_ERR_ARG; | |
252 | } | |
253 | ||
254 | serial = sdi->conn; | |
255 | prefix = sdi->driver->name; | |
256 | ||
102f1239 | 257 | if ((ret = serial_source_remove(sdi->session, serial)) < 0) { |
ac2926b3 | 258 | sr_err("%s: Failed to remove source: %d.", prefix, ret); |
cd2f0fe2 UH |
259 | return ret; |
260 | } | |
261 | ||
093e1cba | 262 | if ((ret = sr_dev_close(sdi)) < 0) { |
ac2926b3 | 263 | sr_err("%s: Failed to close device: %d.", prefix, ret); |
cd2f0fe2 UH |
264 | return ret; |
265 | } | |
266 | ||
12852b03 | 267 | return std_session_send_df_end(sdi); |
cd2f0fe2 | 268 | } |
49f00e13 | 269 | |
c4f2dfd0 UH |
270 | #endif |
271 | ||
813aab69 | 272 | /** |
12852b03 | 273 | * Standard driver dev_clear() callback API helper. |
49f00e13 | 274 | * |
813aab69 MH |
275 | * Clear driver, this means, close all instances. |
276 | * | |
49f00e13 BV |
277 | * This function can be used to implement the dev_clear() driver API |
278 | * callback. dev_close() is called before every sr_dev_inst is cleared. | |
279 | * | |
280 | * The only limitation is driver-specific device contexts (sdi->priv). | |
281 | * These are freed, but any dynamic allocation within structs stored | |
282 | * there cannot be freed. | |
283 | * | |
12852b03 UH |
284 | * @param[in] driver The driver which will have its instances released. |
285 | * Must not be NULL. | |
286 | * @param[in] clear_private If not NULL, this points to a function called | |
287 | * with sdi->priv as argument. The function can then clear | |
288 | * any device instance-specific resources kept there. | |
289 | * It must also clear the struct pointed to by sdi->priv. | |
49f00e13 | 290 | * |
12852b03 UH |
291 | * @retval SR_OK Success. |
292 | * @retval SR_ERR_ARG Invalid argument. | |
293 | * @retval SR_ERR_BUG Implementation bug. | |
294 | * @retval other Other error. | |
49f00e13 | 295 | */ |
ae5859ff | 296 | SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver, |
144f6660 | 297 | std_dev_clear_callback clear_private) |
49f00e13 | 298 | { |
49f00e13 | 299 | struct drv_context *drvc; |
12a33563 | 300 | struct sr_dev_inst *sdi; |
7aebe22d | 301 | GSList *l; |
49f00e13 BV |
302 | int ret; |
303 | ||
12852b03 UH |
304 | if (!driver) { |
305 | sr_err("%s: Invalid argument.", __func__); | |
306 | return SR_ERR_ARG; | |
307 | } | |
308 | ||
309 | drvc = driver->context; /* Caller checked for context != NULL. */ | |
3a277f3b | 310 | |
49f00e13 BV |
311 | ret = SR_OK; |
312 | for (l = drvc->instances; l; l = l->next) { | |
49f00e13 | 313 | if (!(sdi = l->data)) { |
12852b03 | 314 | sr_err("%s: Invalid device instance.", __func__); |
49f00e13 BV |
315 | ret = SR_ERR_BUG; |
316 | continue; | |
317 | } | |
49f00e13 BV |
318 | if (driver->dev_close) |
319 | driver->dev_close(sdi); | |
320 | ||
321 | if (sdi->conn) { | |
45357ce6 | 322 | #ifdef HAVE_LIBSERIALPORT |
c4f2dfd0 | 323 | if (sdi->inst_type == SR_INST_SERIAL) |
12a33563 | 324 | sr_serial_dev_inst_free(sdi->conn); |
c4f2dfd0 | 325 | #endif |
45357ce6 | 326 | #ifdef HAVE_LIBUSB_1_0 |
c4f2dfd0 | 327 | if (sdi->inst_type == SR_INST_USB) |
49f00e13 | 328 | sr_usb_dev_inst_free(sdi->conn); |
a0c7e23a | 329 | #endif |
23f43dff ML |
330 | if (sdi->inst_type == SR_INST_SCPI) |
331 | sr_scpi_free(sdi->conn); | |
daa39012 AJ |
332 | if (sdi->inst_type == SR_INST_MODBUS) |
333 | sr_modbus_free(sdi->conn); | |
49f00e13 | 334 | } |
ae5859ff | 335 | if (clear_private) |
886413b6 BV |
336 | /* The helper function is responsible for freeing |
337 | * its own sdi->priv! */ | |
ae5859ff | 338 | clear_private(sdi->priv); |
12a33563 BV |
339 | else |
340 | g_free(sdi->priv); | |
886413b6 | 341 | |
49f00e13 BV |
342 | sr_dev_inst_free(sdi); |
343 | } | |
344 | ||
345 | g_slist_free(drvc->instances); | |
346 | drvc->instances = NULL; | |
347 | ||
348 | return ret; | |
349 | } | |
c01bf34c LPC |
350 | |
351 | /** | |
12852b03 | 352 | * Standard driver dev_list() callback API helper. |
c01bf34c | 353 | * |
12852b03 | 354 | * This function can be used as the dev_list() callback by most drivers. |
c01bf34c | 355 | * |
12852b03 | 356 | * Return the devices contained in the driver context instances list. |
c01bf34c | 357 | * |
12852b03 UH |
358 | * @param[in] di The driver instance to use. Must not be NULL. |
359 | * | |
360 | * @retval NULL Error, or the list is empty. | |
361 | * @retval other The list of device instances of this driver. | |
c01bf34c LPC |
362 | */ |
363 | SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di) | |
364 | { | |
12852b03 UH |
365 | struct drv_context *drvc; |
366 | ||
367 | if (!di) { | |
368 | sr_err("%s: Invalid argument.", __func__); | |
369 | return NULL; | |
370 | } | |
371 | ||
372 | drvc = di->context; | |
c01bf34c LPC |
373 | |
374 | return drvc->instances; | |
375 | } | |
15a5bfe4 LPC |
376 | |
377 | /** | |
12852b03 | 378 | * Standard driver scan() callback API helper. |
15a5bfe4 LPC |
379 | * |
380 | * This function can be used to perform common tasks required by a driver's | |
381 | * scan() callback. It will initialize the driver for each device on the list | |
382 | * and add the devices on the list to the driver's device instance list. | |
383 | * Usually it should be used as the last step in the scan() callback, right | |
384 | * before returning. | |
385 | * | |
386 | * Note: This function can only be used if std_init() has been called | |
387 | * previously by the driver. | |
388 | * | |
389 | * Example: | |
390 | * @code{c} | |
391 | * static GSList *scan(struct sr_dev_driver *di, GSList *options) | |
392 | * { | |
393 | * struct GSList *device; | |
394 | * struct sr_dev_inst *sdi; | |
395 | * | |
396 | * sdi = g_new0(sr_dev_inst, 1); | |
397 | * sdi->vendor = ...; | |
398 | * ... | |
399 | * devices = g_slist_append(devices, sdi); | |
400 | * ... | |
401 | * return std_scan_complete(di, devices); | |
402 | * } | |
403 | * @endcode | |
404 | * | |
12852b03 UH |
405 | * @param[in] di The driver instance to use. Must not be NULL. |
406 | * @param[in] devices List of newly discovered devices (struct sr_dev_inst). | |
407 | * May be NULL. | |
15a5bfe4 LPC |
408 | * |
409 | * @return The @p devices list. | |
410 | */ | |
411 | SR_PRIV GSList *std_scan_complete(struct sr_dev_driver *di, GSList *devices) | |
412 | { | |
39fcfdc9 | 413 | struct drv_context *drvc; |
15a5bfe4 LPC |
414 | GSList *l; |
415 | ||
39fcfdc9 UH |
416 | if (!di) { |
417 | sr_err("Invalid driver instance (di), cannot complete scan."); | |
418 | return NULL; | |
419 | } | |
420 | ||
421 | drvc = di->context; | |
422 | ||
15a5bfe4 LPC |
423 | for (l = devices; l; l = l->next) { |
424 | struct sr_dev_inst *sdi = l->data; | |
39fcfdc9 | 425 | if (!sdi) { |
12852b03 | 426 | sr_err("Invalid device instance, cannot complete scan."); |
39fcfdc9 UH |
427 | return NULL; |
428 | } | |
15a5bfe4 LPC |
429 | sdi->driver = di; |
430 | } | |
431 | ||
432 | drvc->instances = g_slist_concat(drvc->instances, g_slist_copy(devices)); | |
433 | ||
434 | return devices; | |
435 | } |