api.c 7.4 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. #include "protocol.h"
  21. static const int32_t hwopts[] = {
  22. SR_CONF_CONN,
  23. SR_CONF_SERIALCOMM,
  24. };
  25. static const int32_t hwcaps[] = {
  26. SR_CONF_THERMOMETER,
  27. SR_CONF_HYGROMETER,
  28. SR_CONF_LIMIT_SAMPLES,
  29. SR_CONF_LIMIT_MSEC,
  30. SR_CONF_CONTINUOUS,
  31. };
  32. SR_PRIV struct sr_dev_driver mic_98581_driver_info;
  33. SR_PRIV struct sr_dev_driver mic_98583_driver_info;
  34. SR_PRIV const struct mic_dev_info mic_devs[] = {
  35. {
  36. "MIC", "98581", "38400/8n2", 32000, TRUE, FALSE, 6,
  37. packet_valid_temp,
  38. &mic_98581_driver_info, receive_data_MIC_98581,
  39. },
  40. {
  41. "MIC", "98583", "38400/8n2", 32000, TRUE, TRUE, 10,
  42. packet_valid_temp_hum,
  43. &mic_98583_driver_info, receive_data_MIC_98583,
  44. },
  45. };
  46. static int dev_clear(int idx)
  47. {
  48. return std_dev_clear(mic_devs[idx].di, NULL);
  49. }
  50. static int init(struct sr_context *sr_ctx, int idx)
  51. {
  52. sr_dbg("Selected '%s' subdriver.", mic_devs[idx].di->name);
  53. return std_init(sr_ctx, mic_devs[idx].di, LOG_PREFIX);
  54. }
  55. static GSList *mic_scan(const char *conn, const char *serialcomm, int idx)
  56. {
  57. struct sr_dev_inst *sdi;
  58. struct drv_context *drvc;
  59. struct dev_context *devc;
  60. struct sr_channel *ch;
  61. struct sr_serial_dev_inst *serial;
  62. GSList *devices;
  63. if (!(serial = sr_serial_dev_inst_new(conn, serialcomm)))
  64. return NULL;
  65. if (serial_open(serial, SERIAL_RDWR | SERIAL_NONBLOCK) != SR_OK)
  66. return NULL;
  67. drvc = mic_devs[idx].di->priv;
  68. devices = NULL;
  69. serial_flush(serial);
  70. /* TODO: Query device type. */
  71. // ret = mic_cmd_get_device_info(serial);
  72. sr_info("Found device on port %s.", conn);
  73. /* TODO: Fill in version from protocol response. */
  74. if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, mic_devs[idx].vendor,
  75. mic_devs[idx].device, NULL)))
  76. goto scan_cleanup;
  77. if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
  78. sr_err("Device context malloc failed.");
  79. goto scan_cleanup;
  80. }
  81. sdi->inst_type = SR_INST_SERIAL;
  82. sdi->conn = serial;
  83. sdi->priv = devc;
  84. sdi->driver = mic_devs[idx].di;
  85. if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "Temperature")))
  86. goto scan_cleanup;
  87. sdi->channels = g_slist_append(sdi->channels, ch);
  88. if (mic_devs[idx].has_humidity) {
  89. if (!(ch = sr_channel_new(1, SR_CHANNEL_ANALOG, TRUE, "Humidity")))
  90. goto scan_cleanup;
  91. sdi->channels = g_slist_append(sdi->channels, ch);
  92. }
  93. drvc->instances = g_slist_append(drvc->instances, sdi);
  94. devices = g_slist_append(devices, sdi);
  95. scan_cleanup:
  96. serial_close(serial);
  97. return devices;
  98. }
  99. static GSList *scan(GSList *options, int idx)
  100. {
  101. struct sr_config *src;
  102. GSList *l, *devices;
  103. const char *conn, *serialcomm;
  104. conn = serialcomm = NULL;
  105. for (l = options; l; l = l->next) {
  106. src = l->data;
  107. switch (src->key) {
  108. case SR_CONF_CONN:
  109. conn = g_variant_get_string(src->data, NULL);
  110. break;
  111. case SR_CONF_SERIALCOMM:
  112. serialcomm = g_variant_get_string(src->data, NULL);
  113. break;
  114. }
  115. }
  116. if (!conn)
  117. return NULL;
  118. if (serialcomm) {
  119. /* Use the provided comm specs. */
  120. devices = mic_scan(conn, serialcomm, idx);
  121. } else {
  122. /* Try the default. */
  123. devices = mic_scan(conn, mic_devs[idx].conn, idx);
  124. }
  125. return devices;
  126. }
  127. static GSList *dev_list(int idx)
  128. {
  129. return ((struct drv_context *)(mic_devs[idx].di->priv))->instances;
  130. }
  131. static int cleanup(int idx)
  132. {
  133. return dev_clear(idx);
  134. }
  135. static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
  136. const struct sr_channel_group *cg)
  137. {
  138. struct dev_context *devc;
  139. (void)cg;
  140. if (sdi->status != SR_ST_ACTIVE)
  141. return SR_ERR_DEV_CLOSED;
  142. devc = sdi->priv;
  143. switch (id) {
  144. case SR_CONF_LIMIT_SAMPLES:
  145. devc->limit_samples = g_variant_get_uint64(data);
  146. sr_dbg("Setting sample limit to %" PRIu64 ".",
  147. devc->limit_samples);
  148. break;
  149. case SR_CONF_LIMIT_MSEC:
  150. devc->limit_msec = g_variant_get_uint64(data);
  151. sr_dbg("Setting time limit to %" PRIu64 "ms.",
  152. devc->limit_msec);
  153. break;
  154. default:
  155. return SR_ERR_NA;
  156. }
  157. return SR_OK;
  158. }
  159. static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
  160. const struct sr_channel_group *cg)
  161. {
  162. (void)sdi;
  163. (void)cg;
  164. switch (key) {
  165. case SR_CONF_SCAN_OPTIONS:
  166. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  167. hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
  168. break;
  169. case SR_CONF_DEVICE_OPTIONS:
  170. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  171. hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
  172. break;
  173. default:
  174. return SR_ERR_NA;
  175. }
  176. return SR_OK;
  177. }
  178. static int dev_acquisition_start(const struct sr_dev_inst *sdi,
  179. void *cb_data, int idx)
  180. {
  181. struct dev_context *devc;
  182. struct sr_serial_dev_inst *serial;
  183. if (sdi->status != SR_ST_ACTIVE)
  184. return SR_ERR_DEV_CLOSED;
  185. devc = sdi->priv;
  186. devc->cb_data = cb_data;
  187. devc->num_samples = 0;
  188. devc->starttime = g_get_monotonic_time();
  189. /* Send header packet to the session bus. */
  190. std_session_send_df_header(cb_data, LOG_PREFIX);
  191. /* Poll every 100ms, or whenever some data comes in. */
  192. serial = sdi->conn;
  193. serial_source_add(serial, G_IO_IN, 100,
  194. mic_devs[idx].receive_data, (void *)sdi);
  195. return SR_OK;
  196. }
  197. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
  198. {
  199. return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
  200. sdi->conn, LOG_PREFIX);
  201. }
  202. /* Driver-specific API function wrappers */
  203. #define HW_INIT(X) \
  204. static int init_##X(struct sr_context *sr_ctx) { return init(sr_ctx, X); }
  205. #define HW_CLEANUP(X) \
  206. static int cleanup_##X(void) { return cleanup(X); }
  207. #define HW_SCAN(X) \
  208. static GSList *scan_##X(GSList *options) { return scan(options, X); }
  209. #define HW_DEV_LIST(X) \
  210. static GSList *dev_list_##X(void) { return dev_list(X); }
  211. #define HW_DEV_CLEAR(X) \
  212. static int dev_clear_##X(void) { return dev_clear(X); }
  213. #define HW_DEV_ACQUISITION_START(X) \
  214. static int dev_acquisition_start_##X(const struct sr_dev_inst *sdi, \
  215. void *cb_data) { return dev_acquisition_start(sdi, cb_data, X); }
  216. /* Driver structs and API function wrappers */
  217. #define DRV(ID, ID_UPPER, NAME, LONGNAME) \
  218. HW_INIT(ID_UPPER) \
  219. HW_CLEANUP(ID_UPPER) \
  220. HW_SCAN(ID_UPPER) \
  221. HW_DEV_LIST(ID_UPPER) \
  222. HW_DEV_CLEAR(ID_UPPER) \
  223. HW_DEV_ACQUISITION_START(ID_UPPER) \
  224. SR_PRIV struct sr_dev_driver ID##_driver_info = { \
  225. .name = NAME, \
  226. .longname = LONGNAME, \
  227. .api_version = 1, \
  228. .init = init_##ID_UPPER, \
  229. .cleanup = cleanup_##ID_UPPER, \
  230. .scan = scan_##ID_UPPER, \
  231. .dev_list = dev_list_##ID_UPPER, \
  232. .dev_clear = dev_clear_##ID_UPPER, \
  233. .config_get = NULL, \
  234. .config_set = config_set, \
  235. .config_list = config_list, \
  236. .dev_open = std_serial_dev_open, \
  237. .dev_close = std_serial_dev_close, \
  238. .dev_acquisition_start = dev_acquisition_start_##ID_UPPER, \
  239. .dev_acquisition_stop = dev_acquisition_stop, \
  240. .priv = NULL, \
  241. };
  242. DRV(mic_98581, MIC_98581, "mic-98581", "MIC 98581")
  243. DRV(mic_98583, MIC_98583, "mic-98583", "MIC 98583")