lib.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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 <stdio.h>
  21. #include <string.h>
  22. #include <glib.h>
  23. #include <glib/gstdio.h>
  24. #include <check.h>
  25. #include "../libsigrok.h"
  26. #include "lib.h"
  27. /* Get a libsigrok driver by name. */
  28. struct sr_dev_driver *srtest_driver_get(const char *drivername)
  29. {
  30. struct sr_dev_driver **drivers, *driver = NULL;
  31. int i;
  32. drivers = sr_driver_list();
  33. fail_unless(drivers != NULL, "No drivers found.");
  34. for (i = 0; drivers[i]; i++) {
  35. if (strcmp(drivers[i]->name, drivername))
  36. continue;
  37. driver = drivers[i];
  38. }
  39. fail_unless(driver != NULL, "Driver '%s' not found.", drivername);
  40. return driver;
  41. }
  42. /* Get a libsigrok input format by ID. */
  43. struct sr_input_format *srtest_input_get(const char *id)
  44. {
  45. struct sr_input_format **inputs, *input = NULL;
  46. int i;
  47. inputs = sr_input_list();
  48. fail_unless(inputs != NULL, "No input modules found.");
  49. for (i = 0; inputs[i]; i++) {
  50. if (strcmp(inputs[i]->id, id))
  51. continue;
  52. input = inputs[i];
  53. }
  54. fail_unless(input != NULL, "Input module '%s' not found.", id);
  55. return input;
  56. }
  57. /* Get a libsigrok output format by ID. */
  58. struct sr_output_format *srtest_output_get(const char *id)
  59. {
  60. struct sr_output_format **outputs, *output = NULL;
  61. int i;
  62. outputs = sr_output_list();
  63. fail_unless(outputs != NULL, "No output modules found.");
  64. for (i = 0; outputs[i]; i++) {
  65. if (strcmp(outputs[i]->id, id))
  66. continue;
  67. output = outputs[i];
  68. }
  69. fail_unless(output != NULL, "Output module '%s' not found.", id);
  70. return output;
  71. }
  72. /* Initialize a libsigrok driver. */
  73. void srtest_driver_init(struct sr_context *sr_ctx, struct sr_dev_driver *driver)
  74. {
  75. int ret;
  76. ret = sr_driver_init(sr_ctx, driver);
  77. fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
  78. driver->name, ret);
  79. }
  80. /* Initialize all libsigrok drivers. */
  81. void srtest_driver_init_all(struct sr_context *sr_ctx)
  82. {
  83. struct sr_dev_driver **drivers, *driver;
  84. int i, ret;
  85. drivers = sr_driver_list();
  86. fail_unless(drivers != NULL, "No drivers found.");
  87. for (i = 0; drivers[i]; i++) {
  88. driver = drivers[i];
  89. ret = sr_driver_init(sr_ctx, driver);
  90. fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
  91. driver->name, ret);
  92. }
  93. }
  94. /* Initialize a libsigrok input module. */
  95. void srtest_input_init(struct sr_context *sr_ctx, struct sr_input_format *input)
  96. {
  97. int ret;
  98. struct sr_input *in;
  99. (void)sr_ctx;
  100. in = g_try_malloc0(sizeof(struct sr_input));
  101. fail_unless(in != NULL);
  102. in->format = input;
  103. in->param = NULL;
  104. ret = in->format->init(in, "nonexisting.dat");
  105. fail_unless(ret == SR_OK, "Failed to init '%s' input module: %d.",
  106. input->id, ret);
  107. g_free(in);
  108. }
  109. /* Initialize all libsigrok input modules. */
  110. void srtest_input_init_all(struct sr_context *sr_ctx)
  111. {
  112. struct sr_input_format **inputs;
  113. int i;
  114. inputs = sr_input_list();
  115. fail_unless(inputs != NULL, "No input modules found.");
  116. for (i = 0; inputs[i]; i++)
  117. srtest_input_init(sr_ctx, inputs[i]);
  118. }
  119. /* Set the samplerate for the respective driver to the specified value. */
  120. void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
  121. {
  122. int ret;
  123. struct sr_dev_inst *sdi;
  124. GVariant *gvar;
  125. sdi = g_slist_nth_data(driver->priv, 0);
  126. gvar = g_variant_new_uint64(samplerate);
  127. ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi, NULL);
  128. g_variant_unref(gvar);
  129. fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
  130. driver->name, ret);
  131. }
  132. /* Get the respective driver's current samplerate. */
  133. uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
  134. {
  135. int ret;
  136. uint64_t samplerate;
  137. struct sr_dev_inst *sdi;
  138. GVariant *gvar;
  139. sdi = g_slist_nth_data(driver->priv, 0);
  140. ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi, NULL);
  141. samplerate = g_variant_get_uint64(gvar);
  142. g_variant_unref(gvar);
  143. fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
  144. driver->name, ret);
  145. return samplerate;
  146. }
  147. /* Check whether the respective driver can set/get the correct samplerate. */
  148. void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
  149. uint64_t samplerate)
  150. {
  151. struct sr_dev_driver *driver;
  152. uint64_t s;
  153. driver = srtest_driver_get(drivername);
  154. srtest_driver_init(sr_ctx, driver);;
  155. srtest_set_samplerate(driver, samplerate);
  156. s = srtest_get_samplerate(driver);
  157. fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".",
  158. drivername, s);
  159. }
  160. void srtest_buf_to_file(const char *filename, const uint8_t *buf, uint64_t len)
  161. {
  162. FILE *f;
  163. GError *error = NULL;
  164. gboolean ret;
  165. f = g_fopen(filename, "wb");
  166. fail_unless(f != NULL);
  167. ret = g_file_set_contents(filename, (const gchar *)buf, len, &error);
  168. fail_unless(ret == TRUE);
  169. fclose(f);
  170. }
  171. GArray *srtest_get_enabled_logic_channels(const struct sr_dev_inst *sdi)
  172. {
  173. struct sr_channel *ch;
  174. GArray *channels;
  175. GSList *l;
  176. channels = g_array_new(FALSE, FALSE, sizeof(int));
  177. for (l = sdi->channels; l; l = l->next) {
  178. ch = l->data;
  179. if (ch->type != SR_CHANNEL_LOGIC)
  180. continue;
  181. if (ch->enabled != TRUE)
  182. continue;
  183. g_array_append_val(channels, ch->index);
  184. }
  185. return channels;
  186. }