Skip to content

Commit

Permalink
Merge pull request #101 from nathanntg/Issue89
Browse files Browse the repository at this point in the history
Refactor buffer parsing to serial packet descriptor
  • Loading branch information
armadsen committed Mar 14, 2016
2 parents 45307ef + 82b0f2f commit cbf8432
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 13 deletions.
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

0 comments on commit cbf8432

Please sign in to comment.