Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 1 | /* |
Uwe Hermann | 50985c2 | 2013-04-23 22:24:30 +0200 | [diff] [blame] | 2 | * This file is part of the libsigrok project. |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 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 | |
Uwe Hermann | 2eb84c9 | 2013-12-16 10:24:32 +0100 | [diff] [blame] | 21 | /** @file |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 22 | * Standard API helper functions. |
| 23 | * @internal |
| 24 | */ |
| 25 | |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 26 | #include <glib.h> |
| 27 | #include "libsigrok.h" |
| 28 | #include "libsigrok-internal.h" |
| 29 | |
Martin Ling | 3544f84 | 2013-12-23 03:38:35 +0000 | [diff] [blame] | 30 | #define LOG_PREFIX "std" |
| 31 | |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 32 | /** |
| 33 | * Standard sr_driver_init() API helper. |
| 34 | * |
Uwe Hermann | 6078d2c | 2013-05-10 19:37:54 +0200 | [diff] [blame] | 35 | * This function can be used to simplify most driver's init() API callback. |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 36 | * |
| 37 | * It creates a new 'struct drv_context' (drvc), assigns sr_ctx to it, and |
| 38 | * then 'drvc' is assigned to the 'struct sr_dev_driver' (di) that is passed. |
| 39 | * |
| 40 | * @param sr_ctx The libsigrok context to assign. |
| 41 | * @param di The driver instance to use. |
Matthias Heidbrink | 04cb915 | 2013-11-22 20:40:52 +0100 | [diff] [blame] | 42 | * @param[in] prefix A driver-specific prefix string used for log messages. |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 43 | * |
| 44 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 45 | * SR_ERR_MALLOC upon memory allocation errors. |
| 46 | */ |
Uwe Hermann | f6beaac | 2013-05-31 15:46:57 +0200 | [diff] [blame] | 47 | SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di, |
| 48 | const char *prefix) |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 49 | { |
| 50 | struct drv_context *drvc; |
| 51 | |
| 52 | if (!di) { |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 53 | sr_err("%s: Invalid driver, cannot initialize.", prefix); |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 54 | return SR_ERR_ARG; |
| 55 | } |
| 56 | |
Bert Vermeulen | c2523f2 | 2013-04-27 18:24:50 +0200 | [diff] [blame] | 57 | if (!(drvc = g_try_malloc(sizeof(struct drv_context)))) { |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 58 | sr_err("%s: Driver context malloc failed.", prefix); |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 59 | return SR_ERR_MALLOC; |
| 60 | } |
| 61 | |
| 62 | drvc->sr_ctx = sr_ctx; |
Bert Vermeulen | c2523f2 | 2013-04-27 18:24:50 +0200 | [diff] [blame] | 63 | drvc->instances = NULL; |
Uwe Hermann | 063e7ae | 2013-01-29 12:55:00 +0100 | [diff] [blame] | 64 | di->priv = drvc; |
| 65 | |
| 66 | return SR_OK; |
| 67 | } |
Uwe Hermann | 4afdfd4 | 2013-02-06 19:57:32 +0100 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * Standard API helper for sending an SR_DF_HEADER packet. |
| 71 | * |
| 72 | * This function can be used to simplify most driver's |
Uwe Hermann | 6078d2c | 2013-05-10 19:37:54 +0200 | [diff] [blame] | 73 | * dev_acquisition_start() API callback. |
Uwe Hermann | 4afdfd4 | 2013-02-06 19:57:32 +0100 | [diff] [blame] | 74 | * |
| 75 | * @param sdi The device instance to use. |
| 76 | * @param prefix A driver-specific prefix string used for log messages. |
| 77 | * Must not be NULL. An empty string is allowed. |
| 78 | * |
| 79 | * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or |
| 80 | * SR_ERR upon other errors. |
| 81 | */ |
| 82 | SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi, |
| 83 | const char *prefix) |
| 84 | { |
| 85 | int ret; |
| 86 | struct sr_datafeed_packet packet; |
| 87 | struct sr_datafeed_header header; |
| 88 | |
| 89 | if (!prefix) { |
| 90 | sr_err("Invalid prefix."); |
| 91 | return SR_ERR_ARG; |
| 92 | } |
| 93 | |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 94 | sr_dbg("%s: Starting acquisition.", prefix); |
Uwe Hermann | 4afdfd4 | 2013-02-06 19:57:32 +0100 | [diff] [blame] | 95 | |
| 96 | /* Send header packet to the session bus. */ |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 97 | sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix); |
Uwe Hermann | 4afdfd4 | 2013-02-06 19:57:32 +0100 | [diff] [blame] | 98 | packet.type = SR_DF_HEADER; |
| 99 | packet.payload = (uint8_t *)&header; |
| 100 | header.feed_version = 1; |
| 101 | gettimeofday(&header.starttime, NULL); |
| 102 | |
| 103 | if ((ret = sr_session_send(sdi, &packet)) < 0) { |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 104 | sr_err("%s: Failed to send header packet: %d.", prefix, ret); |
Uwe Hermann | 4afdfd4 | 2013-02-06 19:57:32 +0100 | [diff] [blame] | 105 | return ret; |
| 106 | } |
| 107 | |
| 108 | return SR_OK; |
| 109 | } |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 110 | |
Uwe Hermann | c4f2dfd | 2013-11-13 19:56:13 +0100 | [diff] [blame] | 111 | #ifdef HAVE_LIBSERIALPORT |
| 112 | |
Matthias Heidbrink | 813aab6 | 2014-02-05 14:32:21 +0100 | [diff] [blame] | 113 | /** |
Bert Vermeulen | 23dc666 | 2013-12-07 20:26:15 +0100 | [diff] [blame] | 114 | * Standard serial driver dev_open() helper. |
| 115 | * |
| 116 | * This function can be used to implement the dev_open() driver API |
| 117 | * callback in drivers that use a serial port. The port is opened |
Martin Ling | 6c592ec | 2014-09-21 19:30:24 +0100 | [diff] [blame] | 118 | * with the SERIAL_RDWR flag. |
Bert Vermeulen | 23dc666 | 2013-12-07 20:26:15 +0100 | [diff] [blame] | 119 | * |
| 120 | * If the open succeeded, the status field of the given sdi is set |
| 121 | * to SR_ST_ACTIVE. |
| 122 | * |
| 123 | * @retval SR_OK Success. |
| 124 | * @retval SR_ERR Serial port open failed. |
| 125 | */ |
| 126 | SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi) |
| 127 | { |
| 128 | struct sr_serial_dev_inst *serial; |
| 129 | |
| 130 | serial = sdi->conn; |
Martin Ling | 6c592ec | 2014-09-21 19:30:24 +0100 | [diff] [blame] | 131 | if (serial_open(serial, SERIAL_RDWR) != SR_OK) |
Bert Vermeulen | 23dc666 | 2013-12-07 20:26:15 +0100 | [diff] [blame] | 132 | return SR_ERR; |
| 133 | |
| 134 | sdi->status = SR_ST_ACTIVE; |
| 135 | |
| 136 | return SR_OK; |
| 137 | } |
| 138 | |
Matthias Heidbrink | 813aab6 | 2014-02-05 14:32:21 +0100 | [diff] [blame] | 139 | /** |
Bert Vermeulen | 1e7134d | 2013-12-11 00:33:11 +0100 | [diff] [blame] | 140 | * Standard serial driver dev_close() helper. |
| 141 | * |
| 142 | * This function can be used to implement the dev_close() driver API |
| 143 | * callback in drivers that use a serial port. |
| 144 | * |
| 145 | * After closing the port, the status field of the given sdi is set |
| 146 | * to SR_ST_INACTIVE. |
| 147 | * |
| 148 | * @retval SR_OK Success. |
| 149 | */ |
| 150 | SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi) |
| 151 | { |
| 152 | struct sr_serial_dev_inst *serial; |
| 153 | |
| 154 | serial = sdi->conn; |
| 155 | if (serial && sdi->status == SR_ST_ACTIVE) { |
| 156 | serial_close(serial); |
| 157 | sdi->status = SR_ST_INACTIVE; |
| 158 | } |
| 159 | |
| 160 | return SR_OK; |
| 161 | } |
| 162 | |
Matthias Heidbrink | 813aab6 | 2014-02-05 14:32:21 +0100 | [diff] [blame] | 163 | /** |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 164 | * Standard sr_session_stop() API helper. |
| 165 | * |
| 166 | * This function can be used to simplify most (serial port based) driver's |
Uwe Hermann | 6078d2c | 2013-05-10 19:37:54 +0200 | [diff] [blame] | 167 | * dev_acquisition_stop() API callback. |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 168 | * |
| 169 | * @param sdi The device instance for which acquisition should stop. |
| 170 | * Must not be NULL. |
| 171 | * @param cb_data Opaque 'cb_data' pointer. Must not be NULL. |
Uwe Hermann | 6078d2c | 2013-05-10 19:37:54 +0200 | [diff] [blame] | 172 | * @param dev_close_fn Function pointer to the driver's dev_close(). |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 173 | * Must not be NULL. |
| 174 | * @param serial The serial device instance (struct serial_dev_inst *). |
| 175 | * Must not be NULL. |
Matthias Heidbrink | 813aab6 | 2014-02-05 14:32:21 +0100 | [diff] [blame] | 176 | * @param[in] prefix A driver-specific prefix string used for log messages. |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 177 | * Must not be NULL. An empty string is allowed. |
| 178 | * |
Matthias Heidbrink | 1477a9a | 2013-10-17 10:57:16 +0200 | [diff] [blame] | 179 | * @retval SR_OK Success. |
| 180 | * @retval SR_ERR_ARG Invalid arguments. |
| 181 | * @retval SR_ERR_DEV_CLOSED Device is closed. |
| 182 | * @retval SR_ERR Other errors. |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 183 | */ |
Bert Vermeulen | d43b090 | 2013-12-07 20:39:55 +0100 | [diff] [blame] | 184 | SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi, |
Uwe Hermann | 144f666 | 2014-05-03 22:14:01 +0200 | [diff] [blame] | 185 | void *cb_data, dev_close_callback dev_close_fn, |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 186 | struct sr_serial_dev_inst *serial, const char *prefix) |
| 187 | { |
| 188 | int ret; |
| 189 | struct sr_datafeed_packet packet; |
| 190 | |
| 191 | if (!prefix) { |
| 192 | sr_err("Invalid prefix."); |
| 193 | return SR_ERR_ARG; |
| 194 | } |
| 195 | |
| 196 | if (sdi->status != SR_ST_ACTIVE) { |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 197 | sr_err("%s: Device inactive, can't stop acquisition.", prefix); |
Matthias Heidbrink | 1477a9a | 2013-10-17 10:57:16 +0200 | [diff] [blame] | 198 | return SR_ERR_DEV_CLOSED; |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 199 | } |
| 200 | |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 201 | sr_dbg("%s: Stopping acquisition.", prefix); |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 202 | |
Bert Vermeulen | 102f123 | 2014-07-21 14:35:27 +0200 | [diff] [blame] | 203 | if ((ret = serial_source_remove(sdi->session, serial)) < 0) { |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 204 | sr_err("%s: Failed to remove source: %d.", prefix, ret); |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 205 | return ret; |
| 206 | } |
| 207 | |
Uwe Hermann | 6078d2c | 2013-05-10 19:37:54 +0200 | [diff] [blame] | 208 | if ((ret = dev_close_fn(sdi)) < 0) { |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 209 | sr_err("%s: Failed to close device: %d.", prefix, ret); |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 210 | return ret; |
| 211 | } |
| 212 | |
| 213 | /* Send SR_DF_END packet to the session bus. */ |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 214 | sr_dbg("%s: Sending SR_DF_END packet.", prefix); |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 215 | packet.type = SR_DF_END; |
| 216 | packet.payload = NULL; |
| 217 | if ((ret = sr_session_send(cb_data, &packet)) < 0) { |
Bert Vermeulen | ac2926b | 2014-01-21 14:03:27 +0100 | [diff] [blame] | 218 | sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret); |
Uwe Hermann | cd2f0fe | 2013-02-01 23:45:32 +0100 | [diff] [blame] | 219 | return ret; |
| 220 | } |
| 221 | |
| 222 | return SR_OK; |
| 223 | } |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 224 | |
Uwe Hermann | c4f2dfd | 2013-11-13 19:56:13 +0100 | [diff] [blame] | 225 | #endif |
| 226 | |
Matthias Heidbrink | 813aab6 | 2014-02-05 14:32:21 +0100 | [diff] [blame] | 227 | /** |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 228 | * Standard driver dev_clear() helper. |
| 229 | * |
Matthias Heidbrink | 813aab6 | 2014-02-05 14:32:21 +0100 | [diff] [blame] | 230 | * Clear driver, this means, close all instances. |
| 231 | * |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 232 | * This function can be used to implement the dev_clear() driver API |
| 233 | * callback. dev_close() is called before every sr_dev_inst is cleared. |
| 234 | * |
| 235 | * The only limitation is driver-specific device contexts (sdi->priv). |
| 236 | * These are freed, but any dynamic allocation within structs stored |
| 237 | * there cannot be freed. |
| 238 | * |
| 239 | * @param driver The driver which will have its instances released. |
Bert Vermeulen | 12a3356 | 2013-05-06 00:36:50 +0200 | [diff] [blame] | 240 | * @param clear_private If not NULL, this points to a function called |
| 241 | * with sdi->priv as argument. The function can then clear any device |
| 242 | * instance-specific resources kept there. It must also clear the struct |
| 243 | * pointed to by sdi->priv. |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 244 | * |
| 245 | * @return SR_OK on success. |
| 246 | */ |
Bert Vermeulen | ae5859f | 2013-04-17 00:41:01 +0200 | [diff] [blame] | 247 | SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver, |
Uwe Hermann | 144f666 | 2014-05-03 22:14:01 +0200 | [diff] [blame] | 248 | std_dev_clear_callback clear_private) |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 249 | { |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 250 | struct drv_context *drvc; |
Bert Vermeulen | 12a3356 | 2013-05-06 00:36:50 +0200 | [diff] [blame] | 251 | struct sr_dev_inst *sdi; |
Bert Vermeulen | 886413b | 2014-07-24 21:01:08 +0200 | [diff] [blame] | 252 | struct sr_channel_group *cg; |
| 253 | GSList *l, *lcg; |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 254 | int ret; |
| 255 | |
Bert Vermeulen | 3a277f3 | 2013-05-01 14:54:44 +0200 | [diff] [blame] | 256 | if (!(drvc = driver->priv)) |
| 257 | /* Driver was never initialized, nothing to do. */ |
| 258 | return SR_OK; |
| 259 | |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 260 | ret = SR_OK; |
| 261 | for (l = drvc->instances; l; l = l->next) { |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 262 | if (!(sdi = l->data)) { |
| 263 | ret = SR_ERR_BUG; |
| 264 | continue; |
| 265 | } |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 266 | if (driver->dev_close) |
| 267 | driver->dev_close(sdi); |
| 268 | |
| 269 | if (sdi->conn) { |
Aurelien Jacobs | 45357ce | 2014-01-11 17:48:53 +0100 | [diff] [blame] | 270 | #ifdef HAVE_LIBSERIALPORT |
Uwe Hermann | c4f2dfd | 2013-11-13 19:56:13 +0100 | [diff] [blame] | 271 | if (sdi->inst_type == SR_INST_SERIAL) |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 272 | sr_serial_dev_inst_free(sdi->conn); |
Uwe Hermann | c4f2dfd | 2013-11-13 19:56:13 +0100 | [diff] [blame] | 273 | #endif |
Aurelien Jacobs | 45357ce | 2014-01-11 17:48:53 +0100 | [diff] [blame] | 274 | #ifdef HAVE_LIBUSB_1_0 |
Uwe Hermann | c4f2dfd | 2013-11-13 19:56:13 +0100 | [diff] [blame] | 275 | if (sdi->inst_type == SR_INST_USB) |
Bert Vermeulen | 12a3356 | 2013-05-06 00:36:50 +0200 | [diff] [blame] | 276 | sr_usb_dev_inst_free(sdi->conn); |
| 277 | #endif |
Martin Ling | 23f43df | 2013-12-03 20:40:19 +0000 | [diff] [blame] | 278 | if (sdi->inst_type == SR_INST_SCPI) |
| 279 | sr_scpi_free(sdi->conn); |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 280 | } |
Bert Vermeulen | ae5859f | 2013-04-17 00:41:01 +0200 | [diff] [blame] | 281 | if (clear_private) |
Bert Vermeulen | 886413b | 2014-07-24 21:01:08 +0200 | [diff] [blame] | 282 | /* The helper function is responsible for freeing |
| 283 | * its own sdi->priv! */ |
Bert Vermeulen | ae5859f | 2013-04-17 00:41:01 +0200 | [diff] [blame] | 284 | clear_private(sdi->priv); |
Bert Vermeulen | 12a3356 | 2013-05-06 00:36:50 +0200 | [diff] [blame] | 285 | else |
| 286 | g_free(sdi->priv); |
Bert Vermeulen | 886413b | 2014-07-24 21:01:08 +0200 | [diff] [blame] | 287 | |
| 288 | /* Channel groups */ |
| 289 | for (lcg = sdi->channel_groups; lcg; lcg = lcg->next) { |
| 290 | cg = lcg->data; |
| 291 | g_free(cg->name); |
| 292 | g_slist_free(cg->channels); |
| 293 | g_free(cg); |
| 294 | } |
Bert Vermeulen | 49f00e1 | 2013-04-16 17:53:21 +0200 | [diff] [blame] | 295 | sr_dev_inst_free(sdi); |
| 296 | } |
| 297 | |
| 298 | g_slist_free(drvc->instances); |
| 299 | drvc->instances = NULL; |
| 300 | |
| 301 | return ret; |
| 302 | } |