protocol.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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 <stdlib.h>
  20. #include <glib.h>
  21. #include "libsigrok.h"
  22. #include "libsigrok-internal.h"
  23. #include "protocol.h"
  24. #include <errno.h>
  25. #include <string.h>
  26. static void process_packet(const struct sr_dev_inst *sdi)
  27. {
  28. struct dev_context *devc;
  29. struct sr_datafeed_packet packet;
  30. struct sr_datafeed_analog analog;
  31. GString *dbg;
  32. float fvalue;
  33. int checksum, mode, i;
  34. devc = sdi->priv;
  35. if (sr_log_loglevel_get() >= SR_LOG_SPEW) {
  36. dbg = g_string_sized_new(128);
  37. g_string_printf(dbg, "received packet:");
  38. for (i = 0; i < 10; i++)
  39. g_string_append_printf(dbg, " %.2x", (devc->buf)[i]);
  40. sr_spew("%s", dbg->str);
  41. g_string_free(dbg, TRUE);
  42. }
  43. if (devc->buf[0] != 0x08 || devc->buf[1] != 0x04) {
  44. sr_dbg("invalid packet header.");
  45. return;
  46. }
  47. if (devc->buf[8] != 0x01) {
  48. sr_dbg("invalid measurement.");
  49. return;
  50. }
  51. checksum = 0;
  52. for (i = 0; i < 9; i++)
  53. checksum += devc->buf[i];
  54. if ((checksum & 0xff) != devc->buf[9]) {
  55. sr_dbg("invalid packet checksum.");
  56. return;
  57. }
  58. fvalue = 0.0;
  59. for (i = 3; i < 8; i++) {
  60. if (devc->buf[i] > 0x09)
  61. continue;
  62. fvalue *= 10;
  63. fvalue += devc->buf[i];
  64. }
  65. fvalue /= 10;
  66. memset(&analog, 0, sizeof(struct sr_datafeed_analog));
  67. analog.mq = SR_MQ_SOUND_PRESSURE_LEVEL;
  68. analog.unit = SR_UNIT_DECIBEL_SPL;
  69. analog.channels = sdi->channels;
  70. analog.num_samples = 1;
  71. analog.data = &fvalue;
  72. /* High nibble should only have 0x01 or 0x02. */
  73. mode = (devc->buf[2] >> 4) & 0x0f;
  74. if (mode == 0x02)
  75. analog.mqflags |= SR_MQFLAG_HOLD;
  76. else if (mode != 0x01) {
  77. sr_dbg("unknown measurement mode 0x%.2x", mode);
  78. return;
  79. }
  80. /* Low nibble has 14 combinations of direct/long-term average,
  81. * time scale of that average, frequency weighting, and time
  82. * weighting. */
  83. mode = devc->buf[2] & 0x0f;
  84. switch (mode) {
  85. case 0x0:
  86. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  87. | SR_MQFLAG_SPL_TIME_WEIGHT_F;
  88. break;
  89. case 0x1:
  90. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  91. | SR_MQFLAG_SPL_TIME_WEIGHT_S;
  92. break;
  93. case 0x2:
  94. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
  95. | SR_MQFLAG_SPL_TIME_WEIGHT_F;
  96. break;
  97. case 0x3:
  98. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_C \
  99. | SR_MQFLAG_SPL_TIME_WEIGHT_S;
  100. break;
  101. case 0x4:
  102. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
  103. | SR_MQFLAG_SPL_TIME_WEIGHT_F;
  104. break;
  105. case 0x5:
  106. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT \
  107. | SR_MQFLAG_SPL_TIME_WEIGHT_S;
  108. break;
  109. case 0x6:
  110. analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
  111. | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  112. | SR_MQFLAG_SPL_TIME_WEIGHT_F;
  113. break;
  114. case 0x7:
  115. analog.mqflags |= SR_MQFLAG_SPL_PCT_OVER_ALARM \
  116. | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  117. | SR_MQFLAG_SPL_TIME_WEIGHT_S;
  118. break;
  119. case 0x8:
  120. /* 10-second mean, but we don't have MQ flags to express it. */
  121. analog.mqflags |= SR_MQFLAG_SPL_LAT \
  122. | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  123. | SR_MQFLAG_SPL_TIME_WEIGHT_F;
  124. break;
  125. case 0x9:
  126. /* Mean over a time period between 11 seconds and 24 hours.
  127. * Which is so silly that there's no point in expressing
  128. * either this or the previous case. */
  129. analog.mqflags |= SR_MQFLAG_SPL_LAT \
  130. | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  131. | SR_MQFLAG_SPL_TIME_WEIGHT_F;
  132. break;
  133. case 0xa:
  134. /* 10-second mean. */
  135. analog.mqflags |= SR_MQFLAG_SPL_LAT \
  136. | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  137. | SR_MQFLAG_SPL_TIME_WEIGHT_S;
  138. break;
  139. case 0xb:
  140. /* Mean over a time period between 11 seconds and 24 hours. */
  141. analog.mqflags |= SR_MQFLAG_SPL_LAT \
  142. | SR_MQFLAG_SPL_FREQ_WEIGHT_A \
  143. | SR_MQFLAG_SPL_TIME_WEIGHT_S;
  144. break;
  145. case 0xc:
  146. /* Internal calibration on 1kHz sine at 94dB, not useful
  147. * to anything but the device. */
  148. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
  149. break;
  150. case 0xd:
  151. /* Internal calibration on 1kHz sine at 94dB, not useful
  152. * to anything but the device. */
  153. analog.mqflags |= SR_MQFLAG_SPL_FREQ_WEIGHT_FLAT;
  154. break;
  155. default:
  156. sr_dbg("unknown configuration 0x%.2x", mode);
  157. return;
  158. break;
  159. }
  160. packet.type = SR_DF_ANALOG;
  161. packet.payload = &analog;
  162. sr_session_send(devc->cb_data, &packet);
  163. devc->num_samples++;
  164. }
  165. SR_PRIV int colead_slm_receive_data(int fd, int revents, void *cb_data)
  166. {
  167. const struct sr_dev_inst *sdi;
  168. struct dev_context *devc;
  169. struct sr_serial_dev_inst *serial;
  170. int len;
  171. char buf[128];
  172. (void)fd;
  173. if (!(sdi = cb_data))
  174. return TRUE;
  175. if (!(devc = sdi->priv))
  176. return TRUE;
  177. if (revents != G_IO_IN)
  178. /* Timeout event. */
  179. return TRUE;
  180. serial = sdi->conn;
  181. if (devc->state == IDLE) {
  182. if (serial_read(serial, buf, 128) != 1 || buf[0] != 0x10)
  183. /* Nothing there, or caught the tail end of a previous packet,
  184. * or some garbage. Unless it's a single "data ready" byte,
  185. * we don't want it. */
  186. return TRUE;
  187. /* Got 0x10, "measurement ready". */
  188. if (serial_write(serial, "\x20", 1) == -1)
  189. sr_err("unable to send command: %s", strerror(errno));
  190. else {
  191. devc->state = COMMAND_SENT;
  192. devc->buflen = 0;
  193. }
  194. } else {
  195. len = serial_read(serial, devc->buf + devc->buflen,
  196. 10 - devc->buflen);
  197. if (len < 1)
  198. return TRUE;
  199. devc->buflen += len;
  200. if (devc->buflen > 10) {
  201. sr_dbg("buffer overrun");
  202. devc->state = IDLE;
  203. return TRUE;
  204. }
  205. if (devc->buflen == 10) {
  206. /* If we got here, we're sure the device sent a "data ready"
  207. * notification, we asked for data, and got it. */
  208. process_packet(sdi);
  209. devc->state = IDLE;
  210. }
  211. }
  212. return TRUE;
  213. }