std.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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, write to the Free Software
  18. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  19. */
  20. /** @file
  21. * Standard API helper functions.
  22. * @internal
  23. */
  24. #include <glib.h>
  25. #include "libsigrok.h"
  26. #include "libsigrok-internal.h"
  27. #define LOG_PREFIX "std"
  28. /**
  29. * Standard sr_driver_init() API helper.
  30. *
  31. * This function can be used to simplify most driver's init() API callback.
  32. *
  33. * It creates a new 'struct drv_context' (drvc), assigns sr_ctx to it, and
  34. * then 'drvc' is assigned to the 'struct sr_dev_driver' (di) that is passed.
  35. *
  36. * @param sr_ctx The libsigrok context to assign.
  37. * @param di The driver instance to use.
  38. * @param[in] prefix A driver-specific prefix string used for log messages.
  39. *
  40. * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
  41. * SR_ERR_MALLOC upon memory allocation errors.
  42. */
  43. SR_PRIV int std_init(struct sr_context *sr_ctx, struct sr_dev_driver *di,
  44. const char *prefix)
  45. {
  46. struct drv_context *drvc;
  47. if (!di) {
  48. sr_err("%s: Invalid driver, cannot initialize.", prefix);
  49. return SR_ERR_ARG;
  50. }
  51. if (!(drvc = g_try_malloc(sizeof(struct drv_context)))) {
  52. sr_err("%s: Driver context malloc failed.", prefix);
  53. return SR_ERR_MALLOC;
  54. }
  55. drvc->sr_ctx = sr_ctx;
  56. drvc->instances = NULL;
  57. di->priv = drvc;
  58. return SR_OK;
  59. }
  60. /**
  61. * Standard API helper for sending an SR_DF_HEADER packet.
  62. *
  63. * This function can be used to simplify most driver's
  64. * dev_acquisition_start() API callback.
  65. *
  66. * @param sdi The device instance to use.
  67. * @param prefix A driver-specific prefix string used for log messages.
  68. * Must not be NULL. An empty string is allowed.
  69. *
  70. * @return SR_OK upon success, SR_ERR_ARG upon invalid arguments, or
  71. * SR_ERR upon other errors.
  72. */
  73. SR_PRIV int std_session_send_df_header(const struct sr_dev_inst *sdi,
  74. const char *prefix)
  75. {
  76. int ret;
  77. struct sr_datafeed_packet packet;
  78. struct sr_datafeed_header header;
  79. if (!prefix) {
  80. sr_err("Invalid prefix.");
  81. return SR_ERR_ARG;
  82. }
  83. sr_dbg("%s: Starting acquisition.", prefix);
  84. /* Send header packet to the session bus. */
  85. sr_dbg("%s: Sending SR_DF_HEADER packet.", prefix);
  86. packet.type = SR_DF_HEADER;
  87. packet.payload = (uint8_t *)&header;
  88. header.feed_version = 1;
  89. gettimeofday(&header.starttime, NULL);
  90. if ((ret = sr_session_send(sdi, &packet)) < 0) {
  91. sr_err("%s: Failed to send header packet: %d.", prefix, ret);
  92. return ret;
  93. }
  94. return SR_OK;
  95. }
  96. #ifdef HAVE_LIBSERIALPORT
  97. /**
  98. * Standard serial driver dev_open() helper.
  99. *
  100. * This function can be used to implement the dev_open() driver API
  101. * callback in drivers that use a serial port. The port is opened
  102. * with the SERIAL_RDWR and SERIAL_NONBLOCK flags.
  103. *
  104. * If the open succeeded, the status field of the given sdi is set
  105. * to SR_ST_ACTIVE.
  106. *
  107. * @retval SR_OK Success.
  108. * @retval SR_ERR Serial port open failed.
  109. */
  110. SR_PRIV int std_serial_dev_open(struct sr_dev_inst *sdi)
  111. {
  112. struct sr_serial_dev_inst *serial;
  113. serial = sdi->conn;
  114. if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
  115. return SR_ERR;
  116. sdi->status = SR_ST_ACTIVE;
  117. return SR_OK;
  118. }
  119. /**
  120. * Standard serial driver dev_close() helper.
  121. *
  122. * This function can be used to implement the dev_close() driver API
  123. * callback in drivers that use a serial port.
  124. *
  125. * After closing the port, the status field of the given sdi is set
  126. * to SR_ST_INACTIVE.
  127. *
  128. * @retval SR_OK Success.
  129. */
  130. SR_PRIV int std_serial_dev_close(struct sr_dev_inst *sdi)
  131. {
  132. struct sr_serial_dev_inst *serial;
  133. serial = sdi->conn;
  134. if (serial && sdi->status == SR_ST_ACTIVE) {
  135. serial_close(serial);
  136. sdi->status = SR_ST_INACTIVE;
  137. }
  138. return SR_OK;
  139. }
  140. /**
  141. * Standard sr_session_stop() API helper.
  142. *
  143. * This function can be used to simplify most (serial port based) driver's
  144. * dev_acquisition_stop() API callback.
  145. *
  146. * @param sdi The device instance for which acquisition should stop.
  147. * Must not be NULL.
  148. * @param cb_data Opaque 'cb_data' pointer. Must not be NULL.
  149. * @param dev_close_fn Function pointer to the driver's dev_close().
  150. * Must not be NULL.
  151. * @param serial The serial device instance (struct serial_dev_inst *).
  152. * Must not be NULL.
  153. * @param[in] prefix A driver-specific prefix string used for log messages.
  154. * Must not be NULL. An empty string is allowed.
  155. *
  156. * @retval SR_OK Success.
  157. * @retval SR_ERR_ARG Invalid arguments.
  158. * @retval SR_ERR_DEV_CLOSED Device is closed.
  159. * @retval SR_ERR Other errors.
  160. */
  161. SR_PRIV int std_serial_dev_acquisition_stop(struct sr_dev_inst *sdi,
  162. void *cb_data, dev_close_callback dev_close_fn,
  163. struct sr_serial_dev_inst *serial, const char *prefix)
  164. {
  165. int ret;
  166. struct sr_datafeed_packet packet;
  167. if (!prefix) {
  168. sr_err("Invalid prefix.");
  169. return SR_ERR_ARG;
  170. }
  171. if (sdi->status != SR_ST_ACTIVE) {
  172. sr_err("%s: Device inactive, can't stop acquisition.", prefix);
  173. return SR_ERR_DEV_CLOSED;
  174. }
  175. sr_dbg("%s: Stopping acquisition.", prefix);
  176. if ((ret = serial_source_remove(serial)) < 0) {
  177. sr_err("%s: Failed to remove source: %d.", prefix, ret);
  178. return ret;
  179. }
  180. if ((ret = dev_close_fn(sdi)) < 0) {
  181. sr_err("%s: Failed to close device: %d.", prefix, ret);
  182. return ret;
  183. }
  184. /* Send SR_DF_END packet to the session bus. */
  185. sr_dbg("%s: Sending SR_DF_END packet.", prefix);
  186. packet.type = SR_DF_END;
  187. packet.payload = NULL;
  188. if ((ret = sr_session_send(cb_data, &packet)) < 0) {
  189. sr_err("%s: Failed to send SR_DF_END packet: %d.", prefix, ret);
  190. return ret;
  191. }
  192. return SR_OK;
  193. }
  194. #endif
  195. /**
  196. * Standard driver dev_clear() helper.
  197. *
  198. * Clear driver, this means, close all instances.
  199. *
  200. * This function can be used to implement the dev_clear() driver API
  201. * callback. dev_close() is called before every sr_dev_inst is cleared.
  202. *
  203. * The only limitation is driver-specific device contexts (sdi->priv).
  204. * These are freed, but any dynamic allocation within structs stored
  205. * there cannot be freed.
  206. *
  207. * @param driver The driver which will have its instances released.
  208. * @param clear_private If not NULL, this points to a function called
  209. * with sdi->priv as argument. The function can then clear any device
  210. * instance-specific resources kept there. It must also clear the struct
  211. * pointed to by sdi->priv.
  212. *
  213. * @return SR_OK on success.
  214. */
  215. SR_PRIV int std_dev_clear(const struct sr_dev_driver *driver,
  216. std_dev_clear_callback clear_private)
  217. {
  218. struct drv_context *drvc;
  219. struct sr_dev_inst *sdi;
  220. GSList *l;
  221. int ret;
  222. if (!(drvc = driver->priv))
  223. /* Driver was never initialized, nothing to do. */
  224. return SR_OK;
  225. ret = SR_OK;
  226. for (l = drvc->instances; l; l = l->next) {
  227. if (!(sdi = l->data)) {
  228. ret = SR_ERR_BUG;
  229. continue;
  230. }
  231. if (driver->dev_close)
  232. driver->dev_close(sdi);
  233. if (sdi->conn) {
  234. #ifdef HAVE_LIBSERIALPORT
  235. if (sdi->inst_type == SR_INST_SERIAL)
  236. sr_serial_dev_inst_free(sdi->conn);
  237. #endif
  238. #ifdef HAVE_LIBUSB_1_0
  239. if (sdi->inst_type == SR_INST_USB)
  240. sr_usb_dev_inst_free(sdi->conn);
  241. #endif
  242. if (sdi->inst_type == SR_INST_SCPI)
  243. sr_scpi_free(sdi->conn);
  244. }
  245. if (clear_private)
  246. clear_private(sdi->priv);
  247. else
  248. g_free(sdi->priv);
  249. sr_dev_inst_free(sdi);
  250. }
  251. g_slist_free(drvc->instances);
  252. drvc->instances = NULL;
  253. return ret;
  254. }