LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-12-2011, 09:28 AM   #1
HarryBoy
Member
 
Registered: Apr 2008
Distribution: MontaVista Linux Version 4.0.1, Professional Edition
Posts: 215

Rep: Reputation: 16
type-length-value question


Does anyoe have any example c/c++ code that is used to parse type-length-value messages.

The only decent article I can find on the web is:
http://en.wikipedia.org/wiki/Type-length-value

Bur it does not give any examples of parsing etc..
 
Old 02-12-2011, 10:16 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
I've never heard of the TLV, but I have worked with KLV (Key-Length-Value).

What I recommend that you do is define your own class to represent the data. Something like:
Code:
class Metadata
{
public:
   ...

   Type      type;
   ValLength length;
   Value     value;
};
Above, the Type and ValLength represent enumerated values that are used to describe the Value. For example, for stats concerning an airplane:
Code:
enum Type
{
   FUEL_REMAINING,
   AIR_SPEED,
   ALTITUDE,
   etc
};

enum ValLength
{
   BYTE, UBYTE, SHORT, USHORT, LONG, ULONG, LONGLONG, ULONGLONG, STRING
};
The "tricky" part is the data, or Value. It can be defined as a union of various types; for example:
Code:
struct Value
{
   typedef union
   {
      // signed types
      char     sbyte;
      short    sshort;
      int      slong;
      longlong slonglong;

      // add unsigned counterparts, if you need them.

      char     cstr[127];
   } Data;

   Data data;
};
In conclusion, with the simple code above, you can identify a value (measurement) using [Type, ValLength, Value]. Thus if you wanted to create a Metadata object using FUEL_REMAINING, SHORT, and 43000, you would set the appropriate fields of the Metadata object like:
Code:
type              = FUEL_REMAINING;
length            = SHORT;
value.data.sshort = 43000;
You could consider associating with the Metadata class functions for serializing and de-serializing the object, similar to what was described in the Wiki page, or you could consider making a binary stream of the object data, thus compacting it for better storage or transmission to a remote server.

There's lots more information that I could discuss, but for now I'll leave it for you to discover how "easy" TLV (or KLV) really is.
 
  


Reply

Tags
c/c++



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
packet queue length question vlyamtse Linux - Networking 4 10-08-2009 12:29 AM
Please help fix: sky2 error: rx length error: status 0x42c0500 length 600 trapix22 Linux - Wireless Networking 1 10-25-2008 07:50 AM
Quick perl question using length Seventh Programming 4 09-08-2004 04:29 PM
File type question exper Linux - Software 2 02-15-2004 08:12 PM
a question on type conversion in C++ ReverseLogic Programming 3 11-27-2001 11:06 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:30 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration