-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmartSerialHelper.cpp
More file actions
72 lines (56 loc) · 1.67 KB
/
Copy pathSmartSerialHelper.cpp
File metadata and controls
72 lines (56 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include "SmartSerialHelper.h"
#include "CallbackWrapperInterface.h"
#include "StreamInterface.h"
#include <string.h>
static const char COMMAND_END_SIGN = '?';
static const char COMMAND_SPLIT_SIGN = ' ';
const char* SmartSerialHelper::getNextParameter( const char* strBuffer )
{
while( strBuffer[ 0 ] != '\0' && strBuffer[ 0 ] != COMMAND_SPLIT_SIGN )
{
strBuffer++;
}
return strBuffer;
}
bool SmartSerialHelper::iterate( const char*& strCommand, const char*& strNextCommand, size_t uMaxKeywordLength )
{
while( *strNextCommand == COMMAND_SPLIT_SIGN )
{
strNextCommand++;
}
strCommand = strNextCommand;
strNextCommand = getNextParameter( strCommand );
if( strCommand == strNextCommand )
{
return false;
}
size_t uKeywordLength = strNextCommand - strCommand;
if( uKeywordLength > uMaxKeywordLength )
{
return false;
}
return true;
}
void SmartSerialHelper::copy( char* strBuffer, const char* strSource, size_t uLength )
{
strncpy( strBuffer, strSource, uLength );
strBuffer[ uLength ] = 0;
}
bool SmartSerialHelper::next( StreamInterface* pStream, char* strBuffer, size_t uLength )
{
if( pStream->available() )
{
size_t uBytesRead = pStream->readBytesUntil( COMMAND_END_SIGN, strBuffer, uLength );
strBuffer[ uBytesRead ] = 0;
return true;
}
return false;
}
void SmartSerialHelper::invoke( CallbackWrapperInterface* pCommand, const char** arrParams, size_t uCount )
{
pCommand->invoke( arrParams, uCount );
}
bool SmartSerialHelper::compare( const char* str1, const char* str2, size_t uLength )
{
return strncmp( str1, str2, uLength ) == 0;
}