Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor buffer parsing to serial packet descriptor #101

Merged
merged 2 commits into from
Mar 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions Source/ORSSerialPacketDescriptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ typedef BOOL(^ORSSerialPacketEvaluator)(NSData * __nullable inputData);
*/
- (BOOL)dataIsValidPacket:(nullable NSData *)packetData;

/**
* Can be used to determine and extract a packet from a buffer, matching up to the end of the buffer.
*
* @param buffer Data received from serial port.
*
* @return Data corresponding to valid packet, or nil.
*/
- (nullable NSData *)packetMatchingAtEndOfBuffer:(nullable NSData *)buffer;

/**
* The fixed packetData for packets described by the receiver. Will be nil for packet
* descriptors not created using -initWithPacketData:userInfo:
Expand Down
10 changes: 10 additions & 0 deletions Source/ORSSerialPacketDescriptor.m
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,14 @@ - (BOOL)dataIsValidPacket:(NSData *)packetData
return self.responseEvaluator(packetData);
}

- (NSData *)packetMatchingAtEndOfBuffer:(NSData *)buffer
{
for (NSUInteger i=1; i<=[buffer length]; i++)
{
NSData *window = [buffer subdataWithRange:NSMakeRange([buffer length]-i, i)];
if ([self dataIsValidPacket:window]) return window;
}
return nil;
}

@end
15 changes: 2 additions & 13 deletions Source/ORSSerialPort.m
Original file line number Diff line number Diff line change
Expand Up @@ -474,17 +474,6 @@ - (void)stopListeningForPacketsMatchingDescriptor:(ORSSerialPacketDescriptor *)d

#pragma mark - Private Methods

// Must only be called on requestHandlingQueue (ie. wrap call to this method in dispatch())
- (NSData *)packetMatchingDescriptor:(ORSSerialPacketDescriptor *)descriptor atEndOfBuffer:(NSData *)buffer
{
for (NSUInteger i=1; i<=[buffer length]; i++)
{
NSData *window = [buffer subdataWithRange:NSMakeRange([buffer length]-i, i)];
if ([descriptor dataIsValidPacket:window]) return window;
}
return nil;
}

// Must only be called on requestHandlingQueue (ie. wrap call to this method in dispatch())
- (BOOL)reallySendRequest:(ORSSerialRequest *)request
{
Expand Down Expand Up @@ -558,7 +547,7 @@ - (void)checkResponseToPendingRequestAndContinueIfValidWithReceivedByte:(NSData
}

[self.requestResponseReceiveBuffer appendData:byte];
NSData *responseData = [self packetMatchingDescriptor:packetDescriptor atEndOfBuffer:self.requestResponseReceiveBuffer.data];
NSData *responseData = [packetDescriptor packetMatchingAtEndOfBuffer:self.requestResponseReceiveBuffer.data];
if (!responseData) return;

self.pendingRequestTimeoutTimer = nil;
Expand Down Expand Up @@ -600,7 +589,7 @@ - (void)receiveData:(NSData *)data;
[buffer appendData:byte];

// Check for complete packet
NSData *completePacket = [self packetMatchingDescriptor:descriptor atEndOfBuffer:buffer.data];
NSData *completePacket = [descriptor packetMatchingAtEndOfBuffer:buffer.data];
if (![completePacket length]) continue;

// Complete packet received, so notify delegate then clear buffer
Expand Down