api.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2012 Bert Vermeulen <bert@biot.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 <glib.h>
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22. #include <fcntl.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include "libsigrok.h"
  26. #include "libsigrok-internal.h"
  27. #include "fluke-dmm.h"
  28. static const int32_t hwopts[] = {
  29. SR_CONF_CONN,
  30. SR_CONF_SERIALCOMM,
  31. };
  32. static const int32_t hwcaps[] = {
  33. SR_CONF_MULTIMETER,
  34. SR_CONF_LIMIT_SAMPLES,
  35. SR_CONF_LIMIT_MSEC,
  36. SR_CONF_CONTINUOUS,
  37. };
  38. SR_PRIV struct sr_dev_driver flukedmm_driver_info;
  39. static struct sr_dev_driver *di = &flukedmm_driver_info;
  40. static char *scan_conn[] = {
  41. /* 287/289 */
  42. "115200/8n1",
  43. /* 187/189 */
  44. "9600/8n1",
  45. /* Scopemeter 190 series */
  46. "1200/8n1",
  47. NULL
  48. };
  49. static const struct flukedmm_profile supported_flukedmm[] = {
  50. { FLUKE_187, "187", 100, 1000 },
  51. { FLUKE_189, "189", 100, 1000 },
  52. { FLUKE_287, "287", 100, 1000 },
  53. { FLUKE_190, "199B", 1000, 3500 },
  54. };
  55. static int init(struct sr_context *sr_ctx)
  56. {
  57. return std_init(sr_ctx, di, LOG_PREFIX);
  58. }
  59. static GSList *fluke_scan(const char *conn, const char *serialcomm)
  60. {
  61. struct sr_dev_inst *sdi;
  62. struct drv_context *drvc;
  63. struct dev_context *devc;
  64. struct sr_channel *ch;
  65. struct sr_serial_dev_inst *serial;
  66. GSList *devices;
  67. int retry, len, i, s;
  68. char buf[128], *b, **tokens;
  69. if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
  70. return NULL;
  71. if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
  72. return NULL;
  73. drvc = di->priv;
  74. b = buf;
  75. retry = 0;
  76. devices = NULL;
  77. /* We'll try the discovery sequence three times in case the device
  78. * is not in an idle state when we send ID. */
  79. while (!devices && retry < 3) {
  80. retry++;
  81. serial_flush(serial);
  82. if (serial_write(serial, "ID\r", 3) == -1) {
  83. sr_err("Unable to send ID string: %s.",
  84. strerror(errno));
  85. continue;
  86. }
  87. /* Response is first a CMD_ACK byte (ASCII '0' for OK,
  88. * or '1' to signify an error. */
  89. len = 128;
  90. serial_readline(serial, &b, &len, 150);
  91. if (len != 1)
  92. continue;
  93. if (buf[0] != '0')
  94. continue;
  95. /* If CMD_ACK was OK, ID string follows. */
  96. len = 128;
  97. serial_readline(serial, &b, &len, 850);
  98. if (len < 10)
  99. continue;
  100. if (strcspn(buf, ",") < 15)
  101. /* Looks like it's comma-separated. */
  102. tokens = g_strsplit(buf, ",", 3);
  103. else
  104. /* Fluke 199B, at least, uses semicolon. */
  105. tokens = g_strsplit(buf, ";", 3);
  106. if (!strncmp("FLUKE", tokens[0], 5)
  107. && tokens[1] && tokens[2]) {
  108. for (i = 0; supported_flukedmm[i].model; i++) {
  109. if (strcmp(supported_flukedmm[i].modelname, tokens[0] + 6))
  110. continue;
  111. /* Skip leading spaces in version number. */
  112. for (s = 0; tokens[1][s] == ' '; s++);
  113. if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Fluke",
  114. tokens[0] + 6, tokens[1] + s)))
  115. return NULL;
  116. if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
  117. sr_err("Device context malloc failed.");
  118. return NULL;
  119. }
  120. devc->profile = &supported_flukedmm[i];
  121. sdi->inst_type = SR_INST_SERIAL;
  122. sdi->conn = serial;
  123. sdi->priv = devc;
  124. sdi->driver = di;
  125. if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
  126. return NULL;
  127. sdi->channels = g_slist_append(sdi->channels, ch);
  128. drvc->instances = g_slist_append(drvc->instances, sdi);
  129. devices = g_slist_append(devices, sdi);
  130. break;
  131. }
  132. }
  133. g_strfreev(tokens);
  134. if (devices)
  135. /* Found one. */
  136. break;
  137. }
  138. serial_close(serial);
  139. if (!devices)
  140. sr_serial_dev_inst_free(serial);
  141. return devices;
  142. }
  143. static GSList *scan(GSList *options)
  144. {
  145. struct sr_config *src;
  146. GSList *l, *devices;
  147. int i;
  148. const char *conn, *serialcomm;
  149. conn = serialcomm = NULL;
  150. for (l = options; l; l = l->next) {
  151. src = l->data;
  152. switch (src->key) {
  153. case SR_CONF_CONN:
  154. conn = g_variant_get_string(src->data, NULL);
  155. break;
  156. case SR_CONF_SERIALCOMM:
  157. serialcomm = g_variant_get_string(src->data, NULL);
  158. break;
  159. }
  160. }
  161. if (!conn)
  162. return NULL;
  163. if (serialcomm) {
  164. /* Use the provided comm specs. */
  165. devices = fluke_scan(conn, serialcomm);
  166. } else {
  167. for (i = 0; scan_conn[i]; i++) {
  168. if ((devices = fluke_scan(conn, scan_conn[i])))
  169. break;
  170. /* The Scopemeter 199B, at least, requires this
  171. * after all the 115k/9.6k confusion. */
  172. g_usleep(5000);
  173. }
  174. }
  175. return devices;
  176. }
  177. static GSList *dev_list(void)
  178. {
  179. return ((struct drv_context *)(di->priv))->instances;
  180. }
  181. static int cleanup(void)
  182. {
  183. return std_dev_clear(di, NULL);
  184. }
  185. static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
  186. const struct sr_channel_group *cg)
  187. {
  188. struct dev_context *devc;
  189. (void)cg;
  190. if (sdi->status != SR_ST_ACTIVE)
  191. return SR_ERR_DEV_CLOSED;
  192. if (!(devc = sdi->priv)) {
  193. sr_err("sdi->priv was NULL.");
  194. return SR_ERR_BUG;
  195. }
  196. switch (id) {
  197. case SR_CONF_LIMIT_MSEC:
  198. /* TODO: not yet implemented */
  199. if (g_variant_get_uint64(data) == 0) {
  200. sr_err("LIMIT_MSEC can't be 0.");
  201. return SR_ERR;
  202. }
  203. devc->limit_msec = g_variant_get_uint64(data);
  204. sr_dbg("Setting time limit to %" PRIu64 "ms.",
  205. devc->limit_msec);
  206. break;
  207. case SR_CONF_LIMIT_SAMPLES:
  208. devc->limit_samples = g_variant_get_uint64(data);
  209. sr_dbg("Setting sample limit to %" PRIu64 ".",
  210. devc->limit_samples);
  211. break;
  212. default:
  213. return SR_ERR_NA;
  214. }
  215. return SR_OK;
  216. }
  217. static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
  218. const struct sr_channel_group *cg)
  219. {
  220. (void)sdi;
  221. (void)cg;
  222. switch (key) {
  223. case SR_CONF_SCAN_OPTIONS:
  224. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  225. hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
  226. break;
  227. case SR_CONF_DEVICE_OPTIONS:
  228. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  229. hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
  230. break;
  231. default:
  232. return SR_ERR_NA;
  233. }
  234. return SR_OK;
  235. }
  236. static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
  237. {
  238. struct dev_context *devc;
  239. struct sr_serial_dev_inst *serial;
  240. if (sdi->status != SR_ST_ACTIVE)
  241. return SR_ERR_DEV_CLOSED;
  242. if (!(devc = sdi->priv)) {
  243. sr_err("sdi->priv was NULL.");
  244. return SR_ERR_BUG;
  245. }
  246. devc->cb_data = cb_data;
  247. /* Send header packet to the session bus. */
  248. std_session_send_df_header(cb_data, LOG_PREFIX);
  249. /* Poll every 100ms, or whenever some data comes in. */
  250. serial = sdi->conn;
  251. serial_source_add(serial, G_IO_IN, 50, fluke_receive_data, (void *)sdi);
  252. if (serial_write(serial, "QM\r", 3) == -1) {
  253. sr_err("Unable to send QM: %s.", strerror(errno));
  254. return SR_ERR;
  255. }
  256. devc->cmd_sent_at = g_get_monotonic_time() / 1000;
  257. devc->expect_response = TRUE;
  258. return SR_OK;
  259. }
  260. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
  261. {
  262. return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
  263. sdi->conn, LOG_PREFIX);
  264. }
  265. SR_PRIV struct sr_dev_driver flukedmm_driver_info = {
  266. .name = "fluke-dmm",
  267. .longname = "Fluke 18x/28x series DMMs",
  268. .api_version = 1,
  269. .init = init,
  270. .cleanup = cleanup,
  271. .scan = scan,
  272. .dev_list = dev_list,
  273. .dev_clear = NULL,
  274. .config_get = NULL,
  275. .config_set = config_set,
  276. .config_list = config_list,
  277. .dev_open = std_serial_dev_open,
  278. .dev_close = std_serial_dev_close,
  279. .dev_acquisition_start = dev_acquisition_start,
  280. .dev_acquisition_stop = dev_acquisition_stop,
  281. .priv = NULL,
  282. };