A
Angus
Hello
This is part coding and part protocol design. I am developing a very simple
protocol and working out how to parse out received data into messages. I
can process the stream of bytes just about but I think I need help in
design. My data is in tlv format and I might get 1 or many tlv's to parse.
I know the length of the entire byte stream.
This is what I have so far:
#include <stdio.h>
const char* getType(size_t type)
{
switch(type)
{
case 0: return "uint8_t";
case 1: return "byte array";
default: return "unknown type";
}
}
int main()
{
//message consists of 2 * tlv's - a 10 byte string and a 1 byte unsigned
char
//msg1=null terminated "123456789", msg2=8
unsigned char buffer[] =
{1,10,'1','2','3','4','5','6','7','8','9',0,0,1,8};
int len = sizeof(buffer) / sizeof(buffer[0]);
unsigned char* mybytes = buffer;
//parse buffer received into tlv messages
for(size_t i = 0; i < len; ++i)
{
size_t tlvtype = *mybytes++;
int tlvsize = *mybytes++;
switch(tlvtype)
{
case 0: //unsigned 1 byte
{
char c = *mybytes++;
printf("%s data type: %c, size: %i\n", getType(tlvtype), c,
tlvsize);
break;
}
case 1: //string of bytes
{
char* data = (char*)malloc(tlvsize);
for(int i = 0; i < tlvsize; ++i)
*data++ = *mybytes++;
data -= tlvsize;
printf("%s data type: %s, size: %i\n", getType(tlvtype), data,
tlvsize);
free(data);
break;
}
}
}
return 0;
}
But the for loop is totally wrong. I have no end marker but I don't think I
need one. I want to basically work through the byte stream until all
received bytes are processed. I could just keep a counter of how many bytes
processed? Any suggestions on how to do this sort of thing efficiently?
Angus
This is part coding and part protocol design. I am developing a very simple
protocol and working out how to parse out received data into messages. I
can process the stream of bytes just about but I think I need help in
design. My data is in tlv format and I might get 1 or many tlv's to parse.
I know the length of the entire byte stream.
This is what I have so far:
#include <stdio.h>
const char* getType(size_t type)
{
switch(type)
{
case 0: return "uint8_t";
case 1: return "byte array";
default: return "unknown type";
}
}
int main()
{
//message consists of 2 * tlv's - a 10 byte string and a 1 byte unsigned
char
//msg1=null terminated "123456789", msg2=8
unsigned char buffer[] =
{1,10,'1','2','3','4','5','6','7','8','9',0,0,1,8};
int len = sizeof(buffer) / sizeof(buffer[0]);
unsigned char* mybytes = buffer;
//parse buffer received into tlv messages
for(size_t i = 0; i < len; ++i)
{
size_t tlvtype = *mybytes++;
int tlvsize = *mybytes++;
switch(tlvtype)
{
case 0: //unsigned 1 byte
{
char c = *mybytes++;
printf("%s data type: %c, size: %i\n", getType(tlvtype), c,
tlvsize);
break;
}
case 1: //string of bytes
{
char* data = (char*)malloc(tlvsize);
for(int i = 0; i < tlvsize; ++i)
*data++ = *mybytes++;
data -= tlvsize;
printf("%s data type: %s, size: %i\n", getType(tlvtype), data,
tlvsize);
free(data);
break;
}
}
}
return 0;
}
But the for loop is totally wrong. I have no end marker but I don't think I
need one. I want to basically work through the byte stream until all
received bytes are processed. I could just keep a counter of how many bytes
processed? Any suggestions on how to do this sort of thing efficiently?
Angus