Allow partial message reception
Handles cases where a read returns a partial packet by saving partial
pieces until a full packet is read.
diff --git a/src/vfs/ant_rx_chardev.c b/src/vfs/ant_rx_chardev.c
index 2af65a4..a596a88 100644
--- a/src/vfs/ant_rx_chardev.c
+++ b/src/vfs/ant_rx_chardev.c
@@ -232,14 +232,15 @@
int readChannelMsg(ant_channel_type eChannel, ant_channel_info_t *pstChnlInfo)
{
int iRet = -1;
- ANT_U8 aucRxBuffer[ANT_HCI_MAX_MSG_SIZE];
+ static ANT_U8 aucRxBuffer[ANT_HCI_MAX_MSG_SIZE];
+ static int iRxBufferLength = 0;
int iRxLenRead;
int iCurrentHciPacketOffset;
- int iHciPacketSize;
+ int iHciDataSize;
ANT_FUNC_START();
// Keep trying to read while there is an error, and that error is EAGAIN
- while (((iRxLenRead = read(pstChnlInfo->iFd, aucRxBuffer, sizeof(aucRxBuffer))) < 0)
+ while (((iRxLenRead = read(pstChnlInfo->iFd, &aucRxBuffer[iRxBufferLength], (sizeof(aucRxBuffer) - iRxBufferLength))) < 0)
&& errno == EAGAIN)
;
@@ -263,6 +264,15 @@
} else {
ANT_SERIAL(aucRxBuffer, iRxLenRead, 'R');
+ iRxLenRead += iRxBufferLength; // add existing data on
+
+ // if we didn't get a full packet, then just exit
+ if (iRxLenRead <= (aucRxBuffer[ANT_HCI_SIZE_OFFSET] + ANT_HCI_HEADER_SIZE + ANT_HCI_FOOTER_SIZE)) {
+ iRxBufferLength = iRxLenRead;
+ iRet = 0;
+ goto out;
+ }
+
#if ANT_HCI_OPCODE_SIZE == 1 // Check the different message types by opcode
ANT_U8 opcode = aucRxBuffer[ANT_HCI_OPCODE_OFFSET];
@@ -290,8 +300,17 @@
while(iCurrentHciPacketOffset < iRxLenRead) {
- iHciPacketSize = aucRxBuffer[iCurrentHciPacketOffset + ANT_HCI_SIZE_OFFSET];
+ // TODO Allow HCI Packet Size value to be larger than 1 byte
+ // This currently works as no size value is greater than 255, and little endian
+ iHciDataSize = aucRxBuffer[iCurrentHciPacketOffset + ANT_HCI_SIZE_OFFSET];
+ if ((iHciDataSize + ANT_HCI_HEADER_SIZE + ANT_HCI_FOOTER_SIZE + iCurrentHciPacketOffset) >=
+ iRxLenRead) {
+ // we don't have a whole packet
+ iRxBufferLength = iRxLenRead - iCurrentPacketOffset;
+ memcpy(aucRxBuffer, &aucRxBuffer[iCurrentHciPacketOffset], iRxBufferLength);
+ // the increment at the end should push us out of the while loop
+ } else
#ifdef ANT_MESG_FLOW_CONTROL
if (aucRxBuffer[iCurrentHciPacketOffset + ANT_HCI_DATA_OFFSET + ANT_MSG_ID_OFFSET] ==
ANT_MESG_FLOW_CONTROL) {
@@ -304,19 +323,16 @@
#endif // ANT_MESG_FLOW_CONTROL
{
if (pstChnlInfo->fnRxCallback != NULL) {
- // TODO Allow HCI Packet Size value to be larger than 1 byte
- // This currently works as no size value is greater than 255, and little endian
-
// Loop through read data until all HCI packets are written to callback
- pstChnlInfo->fnRxCallback(iHciPacketSize, \
+ pstChnlInfo->fnRxCallback(iHciDataSize, \
&aucRxBuffer[iCurrentHciPacketOffset + ANT_HCI_DATA_OFFSET]);
} else {
ANT_WARN("%s rx callback is null", pstChnlInfo->pcDevicePath);
}
}
- iCurrentHciPacketOffset = iCurrentHciPacketOffset + ANT_HCI_HEADER_SIZE + iHciPacketSize;
+ iCurrentHciPacketOffset = iCurrentHciPacketOffset + ANT_HCI_HEADER_SIZE + ANT_HCI_FOOTER_SIZE + iHciDataSize;
}
}
diff --git a/src/vfs/prerelease/ant_driver_defines.h b/src/vfs/prerelease/ant_driver_defines.h
index 7b6b58b..f2e843b 100644
--- a/src/vfs/prerelease/ant_driver_defines.h
+++ b/src/vfs/prerelease/ant_driver_defines.h
@@ -29,39 +29,70 @@
#ifndef __VFS_PRERELEASE_H
#define __VFS_PRERELEASE_H
-#define ANT_CHIP_NAME "TTY"
-
-#define ANT_COMMANDS_DEVICE_NAME "/dev/smd5"
-#define ANT_DATA_DEVICE_NAME "/dev/smd6"
-
-// Hard reset not supported, don't define ANT_IOCTL_RESET
-
// -----------------------------------------
// | Header | Data | Footer |
// |----------------------|-----------------|
// |Optional| Data | Opt. | ... | Optional |
// | Opcode | Size | Sync | | Checksum |
+
+// Data may include any number of ANT packets, with no sync byte or checksum.
+// A read from the driver may return any number of ANT HCI packets.
+
+// ---------------------- REQUIRED
+
+// Which chip is this library being built for:
+#define ANT_CHIP_NAME "TTY"
+
+// Set the file name the driver creates for the ANT device:
+// If chip uses separate command and data paths:
+#define ANT_COMMANDS_DEVICE_NAME "/dev/smd5"
+#define ANT_DATA_DEVICE_NAME "/dev/smd6"
+// OR
+// If chip uses one path:
+// #define ANT_DEVICE_NAME "/dev/Z"
+
+
+// Set to the number of bytes of header is for Opcode:
#define ANT_HCI_OPCODE_SIZE 0
+
+// Set to the number of bytes of header is for Data Size:
#define ANT_HCI_SIZE_SIZE 1
-
+
+// Set to the number of bytes of header is for Sync:
#define ANT_HCI_SYNC_SIZE 0
+
+// Set to the number of bytes of footer is for Checksum:
#define ANT_HCI_CHECKSUM_SIZE 0
+
+// ---------------------- OPTIONAL
+// If hard reset is supported, define ANT_IOCTL_RESET
+// #define ANT_IOCTL_RESET _IOW('U', 210, int)
+// #define ANT_IOCTL_RESET_PARAMETER (0)
+
+// If the chip sends flow control messages:
+// Define the Opcode for a Flow Control message:
#define ANT_MESG_FLOW_CONTROL ((ANT_U8)0xC9)
-
+// AND
+// define the message content:
+// That signals Flow Go:
#define ANT_FLOW_GO ((ANT_U8)0x00)
+
+// That signals Flow Stop:
+#define ANT_FLOW_STOP ((ANT_U8)0x80)
-// ---------------------- Not chip specific
+// ---------------------- NOT CHIP SPECIFIC
+// These should not need to be modified, but should be verified they are correct
#define ANT_HCI_HEADER_SIZE ((ANT_HCI_OPCODE_SIZE) + (ANT_HCI_SIZE_SIZE) + (ANT_HCI_SYNC_SIZE))
+#define ANT_HCI_FOOTER_SIZE (ANT_HCI_CHECKSUM_SIZE)
#define ANT_HCI_OPCODE_OFFSET 0
#define ANT_HCI_SIZE_OFFSET ((ANT_HCI_OPCODE_OFFSET) + (ANT_HCI_OPCODE_SIZE))
#define ANT_HCI_SYNC_OFFSET ((ANT_HCI_SIZE_OFFSET) + (ANT_HCI_SIZE_SIZE))
#define ANT_HCI_DATA_OFFSET (ANT_HCI_HEADER_SIZE)
-#define ANT_FLOW_STOP ((ANT_U8)0x80)
#define ANT_FLOW_GO_WAIT_TIMEOUT_SEC 10
#endif /* ifndef __VFS_PRERELEASE_H */