Garmin Fleet Management Controller  2.19.0
MessageId.cpp
Go to the documentation of this file.
1 /*********************************************************************
2 *
3 * MODULE NAME:
4 * MessageId.cpp
5 *
6 * Copyright 2008-2009 by Garmin Ltd. or its subsidiaries.
7 *---------------------------------------------------------------------
8 * $NoKeywords$
9 *********************************************************************/
10 
11 #include "MessageId.h"
12 #include "util.h"
13 
14 //----------------------------------------------------------------------
17 //----------------------------------------------------------------------
18 MessageId::MessageId() : mIdSize(0)
19 {
20 }
21 
22 //----------------------------------------------------------------------
25 //----------------------------------------------------------------------
27  (
28  const MessageId & aRightSide
29  )
30 {
31  mIdSize = aRightSide.mIdSize;
32  memcpy( mId, aRightSide.mId, mIdSize );
33 }
34 
35 
36 //----------------------------------------------------------------------
41 //----------------------------------------------------------------------
43  (
44  const uint8 aIdSize,
45  const uint8 * aId
46  )
47 {
48  mIdSize = minval( aIdSize, sizeof( mId ) );
49  memcpy( mId, aId, mIdSize );
50 }
51 
52 //----------------------------------------------------------------------
59 //----------------------------------------------------------------------
61  (
62  const CString & aCString,
63  codepage_type aCodePage
64  )
65 {
66  char messageIdHex[35];
67  //assume in hex
68  WideCharToMultiByte( aCodePage, 0, aCString, -1, messageIdHex, 34, NULL, NULL );
69 
70  //check for '0x' in the message ID
71  if( strncmp( messageIdHex, "0x", 2 ) == 0 )
72  {
73  mIdSize = (uint8)UTIL_hex_to_uint8( messageIdHex + 2, mId, 16 );
74  }
75  else
76  {
77  mIdSize = (uint8)minval( 16, strlen( messageIdHex ) );
78  memmove( mId, messageIdHex, mIdSize );
79  }
80 }
81 
82 //----------------------------------------------------------------------
88 //----------------------------------------------------------------------
89 bool MessageId::operator<
90  (
91  const MessageId & aRightSide
92  ) const
93 {
94  if( mIdSize != aRightSide.mIdSize )
95  return mIdSize < aRightSide.mIdSize;
96  else
97  return memcmp( mId, aRightSide.mId, mIdSize ) < 0;
98 }
99 
100 //----------------------------------------------------------------------
103 //----------------------------------------------------------------------
104 const MessageId& MessageId::operator=
105  (
106  const MessageId & aRightSide
107  )
108 {
109  if( this != &aRightSide )
110  {
111  mIdSize = aRightSide.mIdSize;
112  memset( mId, 0, sizeof( mId ) );
113  memcpy( mId, aRightSide.mId, minval( mIdSize, sizeof( mId ) ) );
114  }
115  return *this;
116 }
117 
118 //----------------------------------------------------------------------
124 //----------------------------------------------------------------------
125 bool MessageId::operator==
126  (
127  const MessageId& aRightSide
128  ) const
129 {
130  if( this->mIdSize != aRightSide.mIdSize )
131  return false;
132 
133  if( 0 != memcmp( mId, aRightSide.mId, mIdSize ) )
134  return false;
135 
136  return true;
137 }
138 
139 //----------------------------------------------------------------------
143 //----------------------------------------------------------------------
144 const uint8 * MessageId::getId() const
145 {
146  return mId;
147 }
148 //----------------------------------------------------------------------
151 //----------------------------------------------------------------------
153 {
154  return mIdSize;
155 }
156 
157 //----------------------------------------------------------------------
165 //----------------------------------------------------------------------
166 CString MessageId::toCString
167  (
168  codepage_type aCodePage
169  ) const
170 {
171  TCHAR messageIdWide[35];
172  char messageId[35];
173  CString formattedId;
174 
175  memset( messageId, 0, 35 );
176  if( UTIL_data_is_printable( (const char *)mId, mIdSize ) )
177  {
178  strncpy( messageId, (const char *)mId, mIdSize );
179  }
180  else
181  {
182  strcpy( messageId, "0x" );
183  UTIL_uint8_to_hex( mId, &messageId[2], mIdSize );
184  }
185  MultiByteToWideChar( aCodePage, 0, messageId, -1, messageIdWide, 35 );
186  messageIdWide[34] = '\0';
187  formattedId.Format( _T(" %s"), messageIdWide );
188 
189  return formattedId;
190 }
MessageId()
Default constructor.
Definition: MessageId.cpp:18
codepage_type
The code page used for encoding of text fields sent to or received from the client.
Definition: fmi.h:189
CString toCString(codepage_type aCodePage) const
CString representation of the message ID.
Definition: MessageId.cpp:167
uint8 mId[16]
The message ID.
Definition: MessageId.h:70
#define minval(_x, _y)
The smaller of _x and _y.
Definition: util_macros.h:95
uint16 UTIL_hex_to_uint8(const char *aHexString, uint8 *aBinaryData, uint16 aMaxBytes)
Convert a hexadecimal ASCII string to an array of uint8.
Definition: util.cpp:323
void UTIL_uint8_to_hex(const uint8 *aData, char *aOutput, uint8 aNumBytes)
Convert from binary to a hexadecimal string.
Definition: util.cpp:376
bool UTIL_data_is_printable(const char *aData, int aLength)
Determine whether an array of characters consists only of printable ASCII.
Definition: util.cpp:471
uint8 mIdSize
Number of significant bytes of mId.
Definition: MessageId.h:71
const uint8 * getId() const
Return a reference to the bytes of the message ID.
Definition: MessageId.cpp:144
unsigned char uint8
8-bit unsigned integer
Definition: garmin_types.h:62
uint8 getIdSize() const
Return the size of the message ID.
Definition: MessageId.cpp:152
Encapsulation of a message ID.
Definition: MessageId.h:26