* Protocol handling
* ---------------------------------------------------------------------- */
-int decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
+int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len)
{
unsigned short crc;
unsigned char msglen;
}
+int mw_feed_msg_buffer(mwdevice_t *mwdevice, unsigned char *buf, int len)
+{
+ char msgbuf[64];
+ int tlen;
+
+ if (len <= 0)
+ return -1;
+
+ memcpy((mwdevice->pbuf+mwdevice->pbuf_len), buf, len);
+ mwdevice->pbuf_len += len;
+
+ while (mwdevice->pbuf_len > 0) {
+ /* scan for MW_SOF */
+ while (mwdevice->pbuf[0] != MW_SOF) {
+ memmove(mwdevice->pbuf, (mwdevice->pbuf+1), --mwdevice->pbuf_len);
+ }
+ tlen = mwdevice->pbuf[1];
+ /* OK, there is an SOF but the message is too short */
+ if (tlen > mwdevice->pbuf_len) {
+ /* we have to wait for more data to come in */
+ break;
+ }
+ /* here we have a complete msg */
+ memcpy(msgbuf, mwdevice->pbuf, tlen);
+ mw_decode_frame(mwdevice, msgbuf, tlen);
+ memmove(mwdevice->pbuf, (mwdevice->pbuf+tlen), tlen);
+ mwdevice->pbuf_len -= tlen;
+ }
+ return 0;
+}
+
+
/* ----------------------------------------------------------------------
* General code usage
* ---------------------------------------------------------------------- */
{
memset(mwdevice, 0, sizeof(mwdevice_t));
mwdevice->mw_fd = mw_fd;
+ memset(mwdevice->pbuf, 0, MW_PBUF_LEN);
+ mwdevice->pbuf_len = 0;
/* figure out which device we run with */
mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);