PageRenderTime 47ms CodeModel.GetById 22ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 1ms

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