api.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  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 "libsigrok.h"
  21. #include "libsigrok-internal.h"
  22. #include "protocol.h"
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <fcntl.h>
  26. #include <errno.h>
  27. #include <string.h>
  28. /* The Colead SL-5868P uses this. */
  29. #define SERIALCOMM "2400/8n1"
  30. static const int32_t hwopts[] = {
  31. SR_CONF_CONN,
  32. SR_CONF_SERIALCOMM,
  33. };
  34. static const int32_t hwcaps[] = {
  35. SR_CONF_SOUNDLEVELMETER,
  36. SR_CONF_LIMIT_SAMPLES,
  37. SR_CONF_LIMIT_MSEC,
  38. SR_CONF_CONTINUOUS,
  39. };
  40. SR_PRIV struct sr_dev_driver colead_slm_driver_info;
  41. static struct sr_dev_driver *di = &colead_slm_driver_info;
  42. static int init(struct sr_context *sr_ctx)
  43. {
  44. return std_init(sr_ctx, di, LOG_PREFIX);
  45. }
  46. static GSList *scan(GSList *options)
  47. {
  48. struct drv_context *drvc;
  49. struct dev_context *devc;
  50. struct sr_dev_inst *sdi;
  51. struct sr_config *src;
  52. struct sr_channel *ch;
  53. GSList *devices, *l;
  54. const char *conn, *serialcomm;
  55. drvc = di->priv;
  56. drvc->instances = NULL;
  57. devices = NULL;
  58. conn = serialcomm = NULL;
  59. for (l = options; l; l = l->next) {
  60. src = l->data;
  61. switch (src->key) {
  62. case SR_CONF_CONN:
  63. conn = g_variant_get_string(src->data, NULL);
  64. break;
  65. case SR_CONF_SERIALCOMM:
  66. serialcomm = g_variant_get_string(src->data, NULL);
  67. break;
  68. }
  69. }
  70. if (!conn)
  71. return NULL;
  72. if (!serialcomm)
  73. serialcomm = SERIALCOMM;
  74. if (!(sdi = sr_dev_inst_new(0, SR_ST_INACTIVE, "Colead",
  75. "SL-5868P", NULL)))
  76. return NULL;
  77. if (!(devc = g_try_malloc0(sizeof(struct dev_context)))) {
  78. sr_dbg("Device context malloc failed.");
  79. return NULL;
  80. }
  81. if (!(sdi->conn = sr_serial_dev_inst_new(conn, serialcomm)))
  82. return NULL;
  83. sdi->inst_type = SR_INST_SERIAL;
  84. sdi->priv = devc;
  85. sdi->driver = di;
  86. if (!(ch = sr_channel_new(0, SR_CHANNEL_ANALOG, TRUE, "P1")))
  87. return NULL;
  88. sdi->channels = g_slist_append(sdi->channels, ch);
  89. drvc->instances = g_slist_append(drvc->instances, sdi);
  90. devices = g_slist_append(devices, sdi);
  91. return devices;
  92. }
  93. static GSList *dev_list(void)
  94. {
  95. return ((struct drv_context *)(di->priv))->instances;
  96. }
  97. static int dev_open(struct sr_dev_inst *sdi)
  98. {
  99. struct sr_serial_dev_inst *serial;
  100. serial = sdi->conn;
  101. if (serial_open(serial, SERIAL_RDWR) != SR_OK)
  102. return SR_ERR;
  103. sdi->status = SR_ST_ACTIVE;
  104. return SR_OK;
  105. }
  106. static int cleanup(void)
  107. {
  108. return std_dev_clear(di, NULL);
  109. }
  110. static int config_set(int id, GVariant *data, const struct sr_dev_inst *sdi,
  111. const struct sr_channel_group *cg)
  112. {
  113. struct dev_context *devc;
  114. (void)cg;
  115. if (sdi->status != SR_ST_ACTIVE)
  116. return SR_ERR_DEV_CLOSED;
  117. if (!(devc = sdi->priv)) {
  118. sr_err("sdi->priv was NULL.");
  119. return SR_ERR_BUG;
  120. }
  121. switch (id) {
  122. case SR_CONF_LIMIT_MSEC:
  123. /* TODO: not yet implemented */
  124. if (g_variant_get_uint64(data) == 0) {
  125. sr_err("LIMIT_MSEC can't be 0.");
  126. return SR_ERR;
  127. }
  128. devc->limit_msec = g_variant_get_uint64(data);;
  129. sr_dbg("Setting time limit to %" PRIu64 "ms.",
  130. devc->limit_msec);
  131. break;
  132. case SR_CONF_LIMIT_SAMPLES:
  133. devc->limit_samples = g_variant_get_uint64(data);
  134. sr_dbg("Setting sample limit to %" PRIu64 ".",
  135. devc->limit_samples);
  136. break;
  137. default:
  138. return SR_ERR_NA;
  139. }
  140. return SR_OK;
  141. }
  142. static int config_list(int key, GVariant **data, const struct sr_dev_inst *sdi,
  143. const struct sr_channel_group *cg)
  144. {
  145. (void)sdi;
  146. (void)cg;
  147. switch (key) {
  148. case SR_CONF_SCAN_OPTIONS:
  149. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  150. hwopts, ARRAY_SIZE(hwopts), sizeof(int32_t));
  151. break;
  152. case SR_CONF_DEVICE_OPTIONS:
  153. *data = g_variant_new_fixed_array(G_VARIANT_TYPE_INT32,
  154. hwcaps, ARRAY_SIZE(hwcaps), sizeof(int32_t));
  155. break;
  156. default:
  157. return SR_ERR_NA;
  158. }
  159. return SR_OK;
  160. }
  161. static int dev_acquisition_start(const struct sr_dev_inst *sdi, void *cb_data)
  162. {
  163. struct dev_context *devc;
  164. struct sr_serial_dev_inst *serial;
  165. if (sdi->status != SR_ST_ACTIVE)
  166. return SR_ERR_DEV_CLOSED;
  167. if (!(devc = sdi->priv)) {
  168. sr_err("sdi->priv was NULL.");
  169. return SR_ERR_BUG;
  170. }
  171. devc->cb_data = cb_data;
  172. /* Send header packet to the session bus. */
  173. std_session_send_df_header(cb_data, LOG_PREFIX);
  174. /* Poll every 150ms, or whenever some data comes in. */
  175. serial = sdi->conn;
  176. serial_source_add(serial, G_IO_IN, 150, colead_slm_receive_data,
  177. (void *)sdi);
  178. return SR_OK;
  179. }
  180. static int dev_acquisition_stop(struct sr_dev_inst *sdi, void *cb_data)
  181. {
  182. return std_serial_dev_acquisition_stop(sdi, cb_data, std_serial_dev_close,
  183. sdi->conn, LOG_PREFIX);
  184. }
  185. SR_PRIV struct sr_dev_driver colead_slm_driver_info = {
  186. .name = "colead-slm",
  187. .longname = "Colead SLM",
  188. .api_version = 1,
  189. .init = init,
  190. .cleanup = cleanup,
  191. .scan = scan,
  192. .dev_list = dev_list,
  193. .dev_clear = NULL,
  194. .config_get = NULL,
  195. .config_set = config_set,
  196. .config_list = config_list,
  197. .dev_open = dev_open,
  198. .dev_close = std_serial_dev_close,
  199. .dev_acquisition_start = dev_acquisition_start,
  200. .dev_acquisition_stop = dev_acquisition_stop,
  201. .priv = NULL,
  202. };