]>
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 | |
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 | ||
2eb84c98 | 21 | /** @file |
04cb9157 MH |
22 | * Standard API helper functions. |
23 | * @internal | |
24 | */ | |
25 | ||
6ec6c43b | 26 | #include <config.h> |
063e7aef | 27 | #include <glib.h> |
c1aae900 | 28 | #include <libsigrok/libsigrok.h> |
063e7aef | 29 | #include "libsigrok-internal.h" |
5a1afc09 | 30 | #include "scpi.h" |
063e7aef | 31 | |
3544f848 ML |
32 | #define LOG_PREFIX "std" |
33 | ||
063e7aef UH |
34 | /** |
35 | * Standard sr_driver_init() API helper. | |
36 | * | |
6078d2c9 | 37 | * This function can be used to simplify most driver's init() API callback. |
063e7aef UH |
38 | * |
39 | * It creates a new 'struct drv_context' (drvc), assigns sr_ctx to it, and | |
40 | * then 'drvc' is assigned to the 'struct sr_dev_driver' (di) that is passed. | |
41 | * | |
063e7aef | 42 | * @param di The driver instance to use. |
1f8f5bc0 | 43 | * @param sr_ctx The libsigrok context to assign. |
063e7aef | 44 | * |
91219afc | 45 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments. |
063e7aef | 46 | */ |
c45c32ce | 47 | SR_PRIV int std_init(struct sr_dev_driver *di, struct sr_context *sr_ctx) |
063e7aef UH |
48 | { |
49 | struct drv_context *drvc; | |
50 | ||
91219afc | 51 | drvc = g_malloc0(sizeof(struct drv_context)); |
063e7aef | 52 | drvc->sr_ctx = sr_ctx; |
c2523f22 | 53 | drvc->instances = NULL; |
41812aca | 54 | di->context = drvc; |
063e7aef UH |
55 | |
56 | return SR_OK; | |
57 | } | |
4afdfd46 | 58 | |
700d6b64 LPC |
59 | /** |
60 | * Standard driver cleanup() callback API helper | |
61 | * | |
62 | * @param di The driver instance to use. | |
63 | * | |
64 | * Frees all device instances by calling sr_dev_clear() and then releases any | |
65 | * resources allocated by std_init(). | |
66 | * | |
67 | * @retval SR_OK Success | |
68 | * @retval SR_ERR_ARG Invalid driver | |
69 | * | |
70 | */ | |
71 | SR_PRIV int std_cleanup(const struct sr_dev_driver *di) | |
72 | { | |
73 | int ret; | |
74 | ||
75 | ret = sr_dev_clear(di); | |
76 | g_free(di->context); | |
77 | ||
78 | return ret; | |
79 | } | |
80 | ||
4afdfd46 UH |
81 | /** |
82 | * Standard API helper for sending an SR_DF_HEADER packet. | |
83 | * | |
84 | * This function can be used to simplify most driver's | |
6078d2c9 | 85 | * dev_acquisition_start() API callback. |
4afdfd46 UH |
86 | * |
87 | * @param sdi The device instance to use. | |
88 | * @param prefix A driver-specific prefix string used for log messages. | |
89 | * Must not be NULL. An empty string is allowed. | |
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 | const char *prefix) | |
96 | { | |
97 | int ret; | |
98 | struct sr_datafeed_packet packet; | |
99 | struct sr_datafeed_header header; | |
100 | ||
101 | if (!prefix) { | |
102 | sr_err("Invalid prefix."); | |
103 | return SR_ERR_ARG; | |
104 | } | |
105 | ||
ac2926b3 | 106 | sr_dbg("%s: Starting acquisition.", prefix); |
4afdfd46 UH |
107 | |
108 | /* Send header packet to the session bus. */ | |
ac2926b3 | 109 | sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix); |
4afdfd46 UH |
110 | packet.type = SR_DF_HEADER; |
111 | packet.payload = (uint8_t *)&header; | |
112 | header.feed_version = 1; | |
113 | gettimeofday(&header.starttime, NULL); | |
114 | ||
115 | if ((ret = sr_session_send(sdi, &packet)) < 0) { | |
ac2926b3 | 116 | sr_err("%s: Failed to send header packet: %d.", prefix, ret); |
4afdfd46 UH |
117 | return ret; |
118 | } | |
119 | ||
120 | return SR_OK; | |
121 | } | |
cd2f0fe2 | 122 | |
3be42bc2 UH |
123 | /** |
124 | * Standard API helper for sending an SR_DF_END packet. | |
125 | * | |
126 | * @param sdi The device instance to use. Must not be NULL. | |
127 | * @param prefix A driver-specific prefix string used for log messages. | |
128 | * Must not be NULL. An empty string is allowed. | |
129 | * | |
130 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or | |
131 | * SR_ERR upon other errors. | |
132 | */ | |
133 | SR_PRIV int std_session_send_df_end(const struct sr_dev_inst *sdi, | |
134 | const char *prefix) | |
135 | { | |
136 | int ret; | |
137 | struct sr_datafeed_packet packet; | |
138 | ||
139 | if (!sdi || !prefix) | |
140 | return SR_ERR_ARG; | |
141 | ||
142 | sr_dbg("%s: Sending SR_DF_END packet.", prefix); | |
143 | ||
144 | packet.type = SR_DF_END; | |
145 | packet.payload = NULL; | |
146 | ||
147 | if ((ret = sr_session_send(sdi, &packet)) < 0) { | |
148 | sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret); | |
149 | return ret; | |
150 | } | |
151 | ||
152 | return SR_OK; | |
153 | } | |
154 | ||
c4f2dfd0 UH |
155 | #ifdef HAVE_LIBSERIALPORT |
156 | ||
813aab69 | 157 | /** |
23dc6661 BV |
158 | * Standard serial driver dev_open() helper. |
159 | * | |
160 | * This function can be used to implement the dev_open() driver API | |
161 | * callback in drivers that use a serial port. The port is opened | |
6c592ece | 162 | * with the SERIAL_RDWR flag. |
23dc6661 BV |
163 | * |
164 | * If the open succeeded, the status field of the given sdi is set | |
165 | * to SR_ST_ACTIVE. | |
166 | * | |
167 | * @retval SR_OK Success. | |
168 | * @retval SR_ERR Serial port open failed. | |
169 | */ | |
170 | SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi) | |
171 | { | |
172 | struct sr_serial_dev_inst *serial; | |
173 | ||
174 | serial = sdi->conn; | |
6c592ece | 175 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
23dc6661 BV |
176 | return SR_ERR; |
177 | ||
178 | sdi->status = SR_ST_ACTIVE; | |
179 | ||
180 | return SR_OK; | |
181 | } | |
182 | ||
813aab69 | 183 | /** |
1e7134dc BV |
184 | * Standard serial driver dev_close() helper. |
185 | * | |
186 | * This function can be used to implement the dev_close() driver API | |
187 | * callback in drivers that use a serial port. | |
188 | * | |
189 | * After closing the port, the status field of the given sdi is set | |
190 | * to SR_ST_INACTIVE. | |
191 | * | |
192 | * @retval SR_OK Success. | |
193 | */ | |
194 | SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi) | |
195 | { | |
196 | struct sr_serial_dev_inst *serial; | |
197 | ||
198 | serial = sdi->conn; | |
199 | if (serial && sdi->status == SR_ST_ACTIVE) { | |
200 | serial_close(serial); | |
201 | sdi->status = SR_ST_INACTIVE; | |
202 | } | |
203 | ||
204 | return SR_OK; | |
205 | } | |
206 | ||
813aab69 | 207 | /** |
cd2f0fe2 UH |
208 | * Standard sr_session_stop() API helper. |
209 | * | |
210 | * This function can be used to simplify most (serial port based) driver's | |
6078d2c9 | 211 | * dev_acquisition_stop() API callback. |
cd2f0fe2 UH |
212 | * |
213 | * @param sdi The device instance for which acquisition should stop. | |
214 | * Must not be NULL. | |
215 | * @param cb_data Opaque 'cb_data' pointer. Must not be NULL. | |
6078d2c9 | 216 | * @param dev_close_fn Function pointer to the driver's dev_close(). |
cd2f0fe2 UH |
217 | * Must not be NULL. |
218 | * @param serial The serial device instance (struct serial_dev_inst *). | |
219 | * Must not be NULL. | |
813aab69 | 220 | * @param[in] prefix A driver-specific prefix string used for log messages. |
cd2f0fe2 UH |
221 | * Must not be NULL. An empty string is allowed. |
222 | * | |
1477a9a6 MH |
223 | * @retval SR_OK Success. |
224 | * @retval SR_ERR_ARG Invalid arguments. | |
225 | * @retval SR_ERR_DEV_CLOSED Device is closed. | |
226 | * @retval SR_ERR Other errors. | |
cd2f0fe2 | 227 | */ |
d43b0908 | 228 | SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi, |
6525d819 | 229 | dev_close_callback dev_close_fn, |
cd2f0fe2 UH |
230 | struct sr_serial_dev_inst *serial, const char *prefix) |
231 | { | |
232 | int ret; | |
cd2f0fe2 UH |
233 | |
234 | if (!prefix) { | |
235 | sr_err("Invalid prefix."); | |
236 | return SR_ERR_ARG; | |
237 | } | |
238 | ||
239 | if (sdi->status != SR_ST_ACTIVE) { | |
ac2926b3 | 240 | sr_err("%s: Device inactive, can't stop acquisition.", prefix); |
1477a9a6 | 241 | return SR_ERR_DEV_CLOSED; |
cd2f0fe2 UH |
242 | } |
243 | ||
ac2926b3 | 244 | sr_dbg("%s: Stopping acquisition.", prefix); |
cd2f0fe2 | 245 | |
102f1239 | 246 | if ((ret = serial_source_remove(sdi->session, serial)) < 0) { |
ac2926b3 | 247 | sr_err("%s: Failed to remove source: %d.", prefix, ret); |
cd2f0fe2 UH |
248 | return ret; |
249 | } | |
250 | ||
6078d2c9 | 251 | if ((ret = dev_close_fn(sdi)) < 0) { |
ac2926b3 | 252 | sr_err("%s: Failed to close device: %d.", prefix, ret); |
cd2f0fe2 UH |
253 | return ret; |
254 | } | |
255 | ||
6525d819 | 256 | std_session_send_df_end(sdi, prefix); |
cd2f0fe2 UH |
257 | |
258 | return SR_OK; | |
259 | } | |
49f00e13 | 260 | |
c4f2dfd0 UH |
261 | #endif |
262 | ||
813aab69 | 263 | /** |
49f00e13 BV |
264 | * Standard driver dev_clear() helper. |
265 | * | |
813aab69 MH |
266 | * Clear driver, this means, close all instances. |
267 | * | |
49f00e13 BV |
268 | * This function can be used to implement the dev_clear() driver API |
269 | * callback. dev_close() is called before every sr_dev_inst is cleared. | |
270 | * | |
271 | * The only limitation is driver-specific device contexts (sdi->priv). | |
272 | * These are freed, but any dynamic allocation within structs stored | |
273 | * there cannot be freed. | |
274 | * | |
275 | * @param driver The driver which will have its instances released. | |
12a33563 BV |
276 | * @param clear_private If not NULL, this points to a function called |
277 | * with sdi->priv as argument. The function can then clear any device | |
278 | * instance-specific resources kept there. It must also clear the struct | |
279 | * pointed to by sdi->priv. | |
49f00e13 BV |
280 | * |
281 | * @return SR_OK on success. | |
282 | */ | |
ae5859ff | 283 | SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver, |
144f6660 | 284 | std_dev_clear_callback clear_private) |
49f00e13 | 285 | { |
49f00e13 | 286 | struct drv_context *drvc; |
12a33563 | 287 | struct sr_dev_inst *sdi; |
7aebe22d | 288 | GSList *l; |
49f00e13 BV |
289 | int ret; |
290 | ||
41812aca | 291 | if (!(drvc = driver->context)) |
3a277f3b BV |
292 | /* Driver was never initialized, nothing to do. */ |
293 | return SR_OK; | |
294 | ||
49f00e13 BV |
295 | ret = SR_OK; |
296 | for (l = drvc->instances; l; l = l->next) { | |
49f00e13 BV |
297 | if (!(sdi = l->data)) { |
298 | ret = SR_ERR_BUG; | |
299 | continue; | |
300 | } | |
49f00e13 BV |
301 | if (driver->dev_close) |
302 | driver->dev_close(sdi); | |
303 | ||
304 | if (sdi->conn) { | |
45357ce6 | 305 | #ifdef HAVE_LIBSERIALPORT |
c4f2dfd0 | 306 | if (sdi->inst_type == SR_INST_SERIAL) |
12a33563 | 307 | sr_serial_dev_inst_free(sdi->conn); |
c4f2dfd0 | 308 | #endif |
45357ce6 | 309 | #ifdef HAVE_LIBUSB_1_0 |
c4f2dfd0 | 310 | if (sdi->inst_type == SR_INST_USB) |
49f00e13 | 311 | sr_usb_dev_inst_free(sdi->conn); |
a0c7e23a | 312 | #endif |
23f43dff ML |
313 | if (sdi->inst_type == SR_INST_SCPI) |
314 | sr_scpi_free(sdi->conn); | |
daa39012 AJ |
315 | if (sdi->inst_type == SR_INST_MODBUS) |
316 | sr_modbus_free(sdi->conn); | |
49f00e13 | 317 | } |
ae5859ff | 318 | if (clear_private) |
886413b6 BV |
319 | /* The helper function is responsible for freeing |
320 | * its own sdi->priv! */ | |
ae5859ff | 321 | clear_private(sdi->priv); |
12a33563 BV |
322 | else |
323 | g_free(sdi->priv); | |
886413b6 | 324 | |
49f00e13 BV |
325 | sr_dev_inst_free(sdi); |
326 | } | |
327 | ||
328 | g_slist_free(drvc->instances); | |
329 | drvc->instances = NULL; | |
330 | ||
331 | return ret; | |
332 | } | |
c01bf34c LPC |
333 | |
334 | /** | |
335 | * Standard implementation for the driver dev_list() callback | |
336 | * | |
337 | * This function can be used as the dev_list callback by most drivers that use | |
338 | * the standard helper functions. It returns the devices contained in the driver | |
339 | * context instances list. | |
340 | * | |
341 | * @param di The driver instance to use. | |
342 | * | |
343 | * @return The list of devices/instances of this driver, or NULL upon errors | |
344 | * or if the list is empty. | |
345 | */ | |
346 | SR_PRIV GSList *std_dev_list(const struct sr_dev_driver *di) | |
347 | { | |
348 | struct drv_context *drvc = di->context; | |
349 | ||
350 | return drvc->instances; | |
351 | } |