protocol.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*
  2. * This file is part of the libsigrok project.
  3. *
  4. * Copyright (C) 2012 Alexandru Gagniuc <mr.nuke.me@gmail.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 "protocol.h"
  20. static void handle_packet(const uint8_t *buf, struct sr_dev_inst *sdi)
  21. {
  22. float floatval;
  23. struct dev_context *devc;
  24. struct sr_datafeed_packet packet;
  25. struct sr_datafeed_analog analog;
  26. devc = sdi->priv;
  27. analog.num_samples = 1;
  28. analog.mq = -1;
  29. if (brymen_parse(buf, &floatval, &analog, NULL) != SR_OK)
  30. return;
  31. analog.data = &floatval;
  32. analog.channels = sdi->channels;
  33. if (analog.mq != -1) {
  34. /* Got a measurement. */
  35. packet.type = SR_DF_ANALOG;
  36. packet.payload = &analog;
  37. sr_session_send(devc->cb_data, &packet);
  38. devc->num_samples++;
  39. }
  40. }
  41. static void handle_new_data(struct sr_dev_inst *sdi)
  42. {
  43. struct dev_context *devc;
  44. int len, status, offset = 0;
  45. struct sr_serial_dev_inst *serial;
  46. devc = sdi->priv;
  47. serial = sdi->conn;
  48. /* Try to get as much data as the buffer can hold. */
  49. len = DMM_BUFSIZE - devc->buflen;
  50. len = serial_read(serial, devc->buf + devc->buflen, len);
  51. if (len < 1) {
  52. sr_err("Serial port read error: %d.", len);
  53. return;
  54. }
  55. devc->buflen += len;
  56. status = PACKET_INVALID_HEADER;
  57. /* Now look for packets in that data. */
  58. while (status != PACKET_NEED_MORE_DATA) {
  59. /* We don't have a header, look for one. */
  60. if (devc->next_packet_len == 0) {
  61. len = devc->buflen - offset;
  62. status = brymen_packet_length(devc->buf + offset, &len);
  63. if (status == PACKET_HEADER_OK) {
  64. /* We know how large the packet will be. */
  65. devc->next_packet_len = len;
  66. } else if (status == PACKET_NEED_MORE_DATA) {
  67. /* We didn't yet receive the full header. */
  68. devc->next_packet_len = 0;
  69. break;
  70. } else {
  71. /* Invalid header. Move on. */
  72. devc->next_packet_len = 0;
  73. offset++;
  74. continue;
  75. }
  76. }
  77. /* We know how the packet size, but did we receive all of it? */
  78. if (devc->buflen - offset < devc->next_packet_len)
  79. break;
  80. /* We should have a full packet here, so we can check it. */
  81. if (brymen_packet_is_valid(devc->buf + offset)) {
  82. handle_packet(devc->buf + offset, sdi);
  83. offset += devc->next_packet_len;
  84. } else {
  85. offset++;
  86. }
  87. /* We are done with this packet. Look for a new one. */
  88. devc->next_packet_len = 0;
  89. }
  90. /* If we have any data left, move it to the beginning of our buffer. */
  91. memmove(devc->buf, devc->buf + offset, devc->buflen - offset);
  92. devc->buflen -= offset;
  93. }
  94. SR_PRIV int brymen_dmm_receive_data(int fd, int revents, void *cb_data)
  95. {
  96. struct sr_dev_inst *sdi;
  97. struct dev_context *devc;
  98. struct sr_serial_dev_inst *serial;
  99. int ret;
  100. int64_t time;
  101. (void)fd;
  102. if (!(sdi = cb_data))
  103. return TRUE;
  104. if (!(devc = sdi->priv))
  105. return TRUE;
  106. serial = sdi->conn;
  107. if (revents == G_IO_IN) {
  108. /* Serial data arrived. */
  109. handle_new_data(sdi);
  110. } else {
  111. /* Timeout, send another packet request. */
  112. if ((ret = brymen_packet_request(serial)) < 0) {
  113. sr_err("Failed to request packet: %d.", ret);
  114. return FALSE;
  115. }
  116. }
  117. if (devc->limit_samples && devc->num_samples >= devc->limit_samples) {
  118. sr_info("Requested number of samples reached, stopping.");
  119. sdi->driver->dev_acquisition_stop(sdi, cb_data);
  120. return TRUE;
  121. }
  122. if (devc->limit_msec) {
  123. time = (g_get_monotonic_time() - devc->starttime) / 1000;
  124. if (time > (int64_t)devc->limit_msec) {
  125. sr_info("Requested time limit reached, stopping.");
  126. sdi->driver->dev_acquisition_stop(sdi, cb_data);
  127. return TRUE;
  128. }
  129. }
  130. return TRUE;
  131. }
  132. /**
  133. * Try to find a valid packet in a serial data stream.
  134. *
  135. * @param serial Previously initialized serial port structure.
  136. * @param buf Buffer containing the bytes to write.
  137. * @param buflen Size of the buffer.
  138. * @param get_packet_size Callback that assesses the size of incoming packets.
  139. * @param is_valid Callback that assesses whether the packet is valid or not.
  140. * @param timeout_ms The timeout after which, if no packet is detected, to
  141. * abort scanning.
  142. * @param baudrate The baudrate of the serial port. This parameter is not
  143. * critical, but it helps fine tune the serial port polling
  144. * delay.
  145. *
  146. * @return SR_OK if a valid packet is found within the given timeout,
  147. * SR_ERR upon failure.
  148. */
  149. SR_PRIV int brymen_stream_detect(struct sr_serial_dev_inst *serial,
  150. uint8_t *buf, size_t *buflen,
  151. packet_length_t get_packet_size,
  152. packet_valid_callback is_valid,
  153. uint64_t timeout_ms, int baudrate)
  154. {
  155. int64_t start, time, byte_delay_us;
  156. size_t ibuf, i, maxlen;
  157. int status, len, packet_len, stream_len;
  158. maxlen = *buflen;
  159. sr_dbg("Detecting packets on %s (timeout = %" PRIu64
  160. "ms, baudrate = %d).", serial->port, timeout_ms, baudrate);
  161. /* Assume 8n1 transmission. That is 10 bits for every byte. */
  162. byte_delay_us = 10 * (1000000 / baudrate);
  163. start = g_get_monotonic_time();
  164. packet_len = i = ibuf = len = 0;
  165. while (ibuf < maxlen) {
  166. len = serial_read(serial, &buf[ibuf], maxlen - ibuf);
  167. if (len > 0) {
  168. ibuf += len;
  169. sr_spew("Read %d bytes.", len);
  170. }
  171. time = g_get_monotonic_time() - start;
  172. time /= 1000;
  173. stream_len = ibuf - i;
  174. if (stream_len > 0 && packet_len == 0) {
  175. /* How large of a packet are we expecting? */
  176. packet_len = stream_len;
  177. status = get_packet_size(&buf[i], &packet_len);
  178. switch(status) {
  179. case PACKET_HEADER_OK:
  180. /* We know how much data we need to wait for. */
  181. break;
  182. case PACKET_NEED_MORE_DATA:
  183. /* We did not receive the full header. */
  184. packet_len = 0;
  185. break;
  186. case PACKET_INVALID_HEADER:
  187. default:
  188. /*
  189. * We had enough data, but here was an error in
  190. * parsing the header. Restart parsing from the
  191. * next byte.
  192. */
  193. packet_len = 0;
  194. i++;
  195. break;
  196. }
  197. }
  198. if ((stream_len >= packet_len) && (packet_len != 0)) {
  199. /* We have at least a packet's worth of data. */
  200. if (is_valid(&buf[i])) {
  201. sr_spew("Found valid %d-byte packet after "
  202. "%" PRIu64 "ms.", packet_len, time);
  203. *buflen = ibuf;
  204. return SR_OK;
  205. } else {
  206. sr_spew("Got %d bytes, but not a valid "
  207. "packet.", packet_len);
  208. }
  209. /* Not a valid packet. Continue searching. */
  210. i++;
  211. packet_len = 0;
  212. }
  213. if (time >= (int64_t)timeout_ms) {
  214. /* Timeout */
  215. sr_dbg("Detection timed out after %dms.", time);
  216. break;
  217. }
  218. g_usleep(byte_delay_us);
  219. }
  220. *buflen = ibuf;
  221. sr_err("Didn't find a valid packet (read %d bytes).", ibuf);
  222. return SR_ERR;
  223. }