api.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.com>
  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 3 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, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include "protocol.h"
  20. static const int32_t hwopts[] = {
  21. SR_CONF_CONN,
  22. SR_CONF_SERIALCOMM,
  23. };
  24. static const int32_t hwcaps[] = {
  25. SR_CONF_MULTIMETER,
  26. SR_CONF_LIMIT_SAMPLES,
  27. SR_CONF_CONTINUOUS,
  28. SR_CONF_LIMIT_MSEC,
  29. };
  30. SR_PRIV struct sr_dev_driver brymen_bm857_driver_info;
  31. static struct sr_dev_driver *di = &brymen_bm857_driver_info;
  32. static int init(struct sr_context *sr_ctx)
  33. {
  34. return std_init(sr_ctx, di, LOG_PREFIX);
  35. }
  36. static GSList *brymen_scan(const char *conn, const char *serialcomm)
  37. {
  38. struct sr_dev_inst *sdi;
  39. struct dev_context *devc;
  40. struct drv_context *drvc;
  41. struct sr_channel *ch;
  42. struct sr_serial_dev_inst *serial;
  43. GSList *devices;
  44. int ret;
  45. uint8_t buf[128];
  46. size_t len;
  47. if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
  48. return NULL;
  49. if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
  50. return NULL;
  51. sr_info("Probing port %s.", conn);
  52. devices = NULL;
  53. /* Request reading */
  54. if ((ret = brymen_packet_request(serial)) < 0) {
  55. sr_err("Unable to send command: %d.", ret);
  56. goto scan_cleanup;
  57. }
  58. len = 128;
  59. ret = brymen_stream_detect(serial, buf, &len, brymen_packet_length,
  60. brymen_packet_is_valid, 1000, 9600);
  61. if (ret != SR_OK)
  62. goto scan_cleanup;
  63. sr_info("Found device on port %s.", conn);
  64. if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Brymen", "BM85x", NULL)))
  65. goto scan_cleanup;
  66. if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
  67. sr_err("Device context malloc failed.");
  68. goto scan_cleanup;
  69. }
  70. sdi->inst_type = SR_INST_SERIAL;
  71. sdi->conn = serial;
  72. drvc = di->priv;
  73. sdi->priv = devc;
  74. sdi->driver = di;
  75. if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
  76. goto scan_cleanup;
  77. sdi->channels = g_slist_append(sdi->channels, ch);
  78. drvc->instances = g_slist_append(drvc->instances, sdi);
  79. devices = g_slist_append(devices, sdi);
  80. scan_cleanup:
  81. serial_close(serial);
  82. return devices;
  83. }
  84. static GSList *scan(GSList *options)
  85. {
  86. struct drv_context *drvc;
  87. struct sr_config *src;
  88. GSList *devices, *l;
  89. const char *conn, *serialcomm;
  90. devices = NULL;
  91. drvc = di->priv;
  92. drvc->instances = NULL;
  93. conn = serialcomm = NULL;
  94. for (l = options; l; l = l->next) {
  95. src = l->data;
  96. switch (src->key) {
  97. case SR_CONF_CONN:
  98. conn = g_variant_get_string(src->data, NULL);
  99. break;
  100. case SR_CONF_SERIALCOMM:
  101. serialcomm = g_variant_get_string(src->data, NULL);
  102. break;
  103. }
  104. }
  105. if (!conn)
  106. return NULL;
  107. if (serialcomm) {
  108. /* Use the provided comm specs. */
  109. devices = brymen_scan(conn, serialcomm);
  110. } else {
  111. /* But 9600/8n1 should work all of the time. */
  112. devices = brymen_scan(conn, "9600/8n1/dtr=1/rts=1");
  113. }
  114. return devices;
  115. }
  116. static GSList *dev_list(void)
  117. {
  118. return ((struct drv_context *)(di->priv))->instances;
  119. }
  120. static int cleanup(void)
  121. {
  122. return std_dev_clear(di, NULL);
  123. }
  124. static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
  125. const struct sr_channel_group *cg)
  126. {
  127. struct dev_context *devc;
  128. int ret;
  129. (void)cg;
  130. if (sdi->status != SR_ST_ACTIVE)
  131. return SR_ERR_DEV_CLOSED;
  132. if (!(devc = sdi->priv)) {
  133. sr_err("sdi->priv was NULL.");
  134. return SR_ERR_BUG;
  135. }
  136. ret = SR_OK;
  137. switch (id) {
  138. case SR_CONF_LIMIT_SAMPLES:
  139. devc->limit_samples = g_variant_get_uint64(data);
  140. break;
  141. case SR_CONF_LIMIT_MSEC:
  142. devc->limit_msec = g_variant_get_uint64(data);
  143. break;
  144. default:
  145. ret = SR_ERR_NA;
  146. }
  147. return ret;
  148. }
  149. static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
  150. const struct sr_channel_group *cg)
  151. {
  152. (void)sdi;
  153. (void)cg;
  154. switch (key) {
  155. case SR_CONF_SCAN_OPTIONS:
  156. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  157. hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
  158. break;
  159. case SR_CONF_DEVICE_OPTIONS:
  160. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  161. hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
  162. break;
  163. default:
  164. return SR_ERR_NA;
  165. }
  166. return SR_OK;
  167. }
  168. static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
  169. {
  170. struct dev_context *devc;
  171. struct sr_serial_dev_inst *serial;
  172. if (sdi->status != SR_ST_ACTIVE)
  173. return SR_ERR_DEV_CLOSED;
  174. if (!(devc = sdi->priv)) {
  175. sr_err("sdi->priv was NULL.");
  176. return SR_ERR_BUG;
  177. }
  178. devc->cb_data = cb_data;
  179. /*
  180. * Reset the number of samples to take. If we've already collected our
  181. * quota, but we start a new session, and don't reset this, we'll just
  182. * quit without acquiring any new samples.
  183. */
  184. devc->num_samples = 0;
  185. devc->starttime = g_get_monotonic_time();
  186. /* Send header packet to the session bus. */
  187. std_session_send_df_header(cb_data, LOG_PREFIX);
  188. /* Poll every 50ms, or whenever some data comes in. */
  189. serial = sdi->conn;
  190. serial_source_add(serial, G_IO_IN, 50,
  191. brymen_dmm_receive_data, (void *)sdi);
  192. return SR_OK;
  193. }
  194. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
  195. {
  196. return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
  197. sdi->conn, LOG_PREFIX);
  198. }
  199. SR_PRIV struct sr_dev_driver brymen_bm857_driver_info = {
  200. .name = "brymen-bm857",
  201. .longname = "Brymen BM857",
  202. .api_version = 1,
  203. .init = init,
  204. .cleanup = cleanup,
  205. .scan = scan,
  206. .dev_list = dev_list,
  207. .dev_clear = NULL,
  208. .config_get = NULL,
  209. .config_set = config_set,
  210. .config_list = config_list,
  211. .dev_open = std_serial_dev_open,
  212. .dev_close = std_serial_dev_close,
  213. .dev_acquisition_start = dev_acquisition_start,
  214. .dev_acquisition_stop = dev_acquisition_stop,
  215. .priv = NULL,
  216. };