/ghost-legacy/gpsprotocol.cpp

http://ghostcb.googlecode.com/ · C++ · 173 lines · 117 code · 25 blank · 31 comment · 6 complexity · 4f3d80467a9309375fe113b6548fcf6c MD5 · raw file

  1. /*
  2. Copyright 2010 Trevor Hogan
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. #include "ghost.h"
  14. #include "util.h"
  15. #include "gpsprotocol.h"
  16. //
  17. // CGPSProtocol
  18. //
  19. CGPSProtocol :: CGPSProtocol( )
  20. {
  21. }
  22. CGPSProtocol :: ~CGPSProtocol( )
  23. {
  24. }
  25. ///////////////////////
  26. // RECEIVE FUNCTIONS //
  27. ///////////////////////
  28. ////////////////////
  29. // SEND FUNCTIONS //
  30. ////////////////////
  31. BYTEARRAY CGPSProtocol :: SEND_GPSC_INIT( uint32_t version )
  32. {
  33. BYTEARRAY packet;
  34. packet.push_back( GPS_HEADER_CONSTANT );
  35. packet.push_back( GPS_INIT );
  36. packet.push_back( 0 );
  37. packet.push_back( 0 );
  38. UTIL_AppendByteArray( packet, version, false );
  39. AssignLength( packet );
  40. return packet;
  41. }
  42. BYTEARRAY CGPSProtocol :: SEND_GPSC_RECONNECT( unsigned char PID, uint32_t reconnectKey, uint32_t lastPacket )
  43. {
  44. BYTEARRAY packet;
  45. packet.push_back( GPS_HEADER_CONSTANT );
  46. packet.push_back( GPS_RECONNECT );
  47. packet.push_back( 0 );
  48. packet.push_back( 0 );
  49. packet.push_back( PID );
  50. UTIL_AppendByteArray( packet, reconnectKey, false );
  51. UTIL_AppendByteArray( packet, lastPacket, false );
  52. AssignLength( packet );
  53. return packet;
  54. }
  55. BYTEARRAY CGPSProtocol :: SEND_GPSC_ACK( uint32_t lastPacket )
  56. {
  57. BYTEARRAY packet;
  58. packet.push_back( GPS_HEADER_CONSTANT );
  59. packet.push_back( GPS_ACK );
  60. packet.push_back( 0 );
  61. packet.push_back( 0 );
  62. UTIL_AppendByteArray( packet, lastPacket, false );
  63. AssignLength( packet );
  64. return packet;
  65. }
  66. BYTEARRAY CGPSProtocol :: SEND_GPSS_INIT( uint16_t reconnectPort, unsigned char PID, uint32_t reconnectKey, unsigned char numEmptyActions )
  67. {
  68. BYTEARRAY packet;
  69. packet.push_back( GPS_HEADER_CONSTANT );
  70. packet.push_back( GPS_INIT );
  71. packet.push_back( 0 );
  72. packet.push_back( 0 );
  73. UTIL_AppendByteArray( packet, reconnectPort, false );
  74. packet.push_back( PID );
  75. UTIL_AppendByteArray( packet, reconnectKey, false );
  76. packet.push_back( numEmptyActions );
  77. AssignLength( packet );
  78. return packet;
  79. }
  80. BYTEARRAY CGPSProtocol :: SEND_GPSS_RECONNECT( uint32_t lastPacket )
  81. {
  82. BYTEARRAY packet;
  83. packet.push_back( GPS_HEADER_CONSTANT );
  84. packet.push_back( GPS_RECONNECT );
  85. packet.push_back( 0 );
  86. packet.push_back( 0 );
  87. UTIL_AppendByteArray( packet, lastPacket, false );
  88. AssignLength( packet );
  89. return packet;
  90. }
  91. BYTEARRAY CGPSProtocol :: SEND_GPSS_ACK( uint32_t lastPacket )
  92. {
  93. BYTEARRAY packet;
  94. packet.push_back( GPS_HEADER_CONSTANT );
  95. packet.push_back( GPS_ACK );
  96. packet.push_back( 0 );
  97. packet.push_back( 0 );
  98. UTIL_AppendByteArray( packet, lastPacket, false );
  99. AssignLength( packet );
  100. return packet;
  101. }
  102. BYTEARRAY CGPSProtocol :: SEND_GPSS_REJECT( uint32_t reason )
  103. {
  104. BYTEARRAY packet;
  105. packet.push_back( GPS_HEADER_CONSTANT );
  106. packet.push_back( GPS_REJECT );
  107. packet.push_back( 0 );
  108. packet.push_back( 0 );
  109. UTIL_AppendByteArray( packet, reason, false );
  110. AssignLength( packet );
  111. return packet;
  112. }
  113. /////////////////////
  114. // OTHER FUNCTIONS //
  115. /////////////////////
  116. bool CGPSProtocol :: AssignLength( BYTEARRAY &content )
  117. {
  118. // insert the actual length of the content array into bytes 3 and 4 (indices 2 and 3)
  119. BYTEARRAY LengthBytes;
  120. if( content.size( ) >= 4 && content.size( ) <= 65535 )
  121. {
  122. LengthBytes = UTIL_CreateByteArray( (uint16_t)content.size( ), false );
  123. content[2] = LengthBytes[0];
  124. content[3] = LengthBytes[1];
  125. return true;
  126. }
  127. return false;
  128. }
  129. bool CGPSProtocol :: ValidateLength( BYTEARRAY &content )
  130. {
  131. // verify that bytes 3 and 4 (indices 2 and 3) of the content array describe the length
  132. uint16_t Length;
  133. BYTEARRAY LengthBytes;
  134. if( content.size( ) >= 4 && content.size( ) <= 65535 )
  135. {
  136. LengthBytes.push_back( content[2] );
  137. LengthBytes.push_back( content[3] );
  138. Length = UTIL_ByteArrayToUInt16( LengthBytes, false );
  139. if( Length == content.size( ) )
  140. return true;
  141. }
  142. return false;
  143. }