📄 packet.java
字号:
// ----------------------------------------------------------------------------// Copyright 2006-2008, Martin D. Flynn// All rights reserved// ----------------------------------------------------------------------------//// Licensed under the Apache License, Version 2.0 (the "License");// you may not use this file except in compliance with the License.// You may obtain a copy of the License at// // http://www.apache.org/licenses/LICENSE-2.0// // Unless required by applicable law or agreed to in writing, software// distributed under the License is distributed on an "AS IS" BASIS,// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.// See the License for the specific language governing permissions and// limitations under the License.//// ----------------------------------------------------------------------------// Change History:// 2006/03/26 Martin D. Flynn// -Initial release// 2006/05/26 Martin D. Flynn// -Increases allowed UniqueID size to 20 bytes (however, the client doesn't// need to used that many bytes to represent a unique id)// 2007/02/08 Martin D. Flynn// -When encoding a packet and the preferred encoding is not specified, or// is invalid, use ENCODING_HEX as the default encoding.// 2007/02/25 Martin D. Flynn// -Removed custom defined payload template 'ClientCustomEvent_5F' (it wasn't// part of the standard DMTP protocol definition, and should only be used// in custom platform implementations).// -Fixed case where server error packets were sometimes created as client// error packets.// -Fixed "static int getPacketLength(byte[],int)" to return proper length of// binary encoded packets.// -Added 'createServerSetPropertyPacket' methods.// 2007/01/10 Martin D. Flynn// -Added 'createServerGetFilePacket' method// ----------------------------------------------------------------------------package org.opendmtp.server.base;import java.lang.*;import java.util.*;import java.io.*;import java.net.*;import java.sql.*;import org.opengts.util.*;import org.opendmtp.codes.*;import org.opendmtp.server.db.*;public class Packet{ // ------------------------------------------------------------------------ public static final int MIN_HEADER_LENGTH = 3; public static final int MAX_PAYLOAD_LENGTH = 255; public static final int HEADER_BASIC = 0xE0; // The value of 'HEADER_BASIC' must NOT be one of the following values: // 0x0A - This is the newline character and may be used to separate ASCII packets // 0x0D - This is the carriage-return character used to separate ASCII packets // 0x23 - This is reserved for encrypted ASCII encoded packets. // 0x24 - This is the start of an ASCII encoded packed ("$") // In general 0x20 through 0x7F are reserved for ASCII packet use and should not be used. // ------------------------------------------------------------------------ // Client originated packets // dialog packets public static final int PKT_CLIENT_EOB_DONE = 0x00; // End of block/transmission, "no more to say" public static final int PKT_CLIENT_EOB_MORE = 0x01; // End of block/transmission, "I have more to say" // identification packets public static final int PKT_CLIENT_UNIQUE_ID = 0x11; // Unique identifier public static final int PKT_CLIENT_ACCOUNT_ID = 0x12; // Account identifier public static final int PKT_CLIENT_DEVICE_ID = 0x13; // Device identifier // standard fixed format event packets public static final int PKT_CLIENT_FIXED_FMT_STD = 0x30; // Standard GPS public static final int PKT_CLIENT_FIXED_FMT_HIGH = 0x31; // High Resolution GPS // DMTP service provider format event packets public static final int PKT_CLIENT_DMTSP_FMT_0 = 0x50; // public static final int PKT_CLIENT_DMTSP_FMT_1 = 0x51; // public static final int PKT_CLIENT_DMTSP_FMT_2 = 0x52; // public static final int PKT_CLIENT_DMTSP_FMT_3 = 0x53; // public static final int PKT_CLIENT_DMTSP_FMT_4 = 0x54; // public static final int PKT_CLIENT_DMTSP_FMT_5 = 0x55; // public static final int PKT_CLIENT_DMTSP_FMT_6 = 0x56; // public static final int PKT_CLIENT_DMTSP_FMT_7 = 0x57; // public static final int PKT_CLIENT_DMTSP_FMT_8 = 0x58; // public static final int PKT_CLIENT_DMTSP_FMT_9 = 0x59; // public static final int PKT_CLIENT_DMTSP_FMT_A = 0x5A; // public static final int PKT_CLIENT_DMTSP_FMT_B = 0x5B; // public static final int PKT_CLIENT_DMTSP_FMT_C = 0x5C; // public static final int PKT_CLIENT_DMTSP_FMT_D = 0x5D; // public static final int PKT_CLIENT_DMTSP_FMT_E = 0x5E; // public static final int PKT_CLIENT_DMTSP_FMT_F = 0x5F; // // custom format event packets public static final int PKT_CLIENT_CUSTOM_FORMAT_0 = 0x70; // Custom format data #0 public static final int PKT_CLIENT_CUSTOM_FORMAT_1 = 0x71; // Custom format data #1 public static final int PKT_CLIENT_CUSTOM_FORMAT_2 = 0x72; // Custom format data #2 public static final int PKT_CLIENT_CUSTOM_FORMAT_3 = 0x73; // Custom format data #3 public static final int PKT_CLIENT_CUSTOM_FORMAT_4 = 0x74; // Custom format data #4 public static final int PKT_CLIENT_CUSTOM_FORMAT_5 = 0x75; // Custom format data #5 public static final int PKT_CLIENT_CUSTOM_FORMAT_6 = 0x76; // Custom format data #6 public static final int PKT_CLIENT_CUSTOM_FORMAT_7 = 0x77; // Custom format data #7 public static final int PKT_CLIENT_CUSTOM_FORMAT_8 = 0x78; // Custom format data #8 public static final int PKT_CLIENT_CUSTOM_FORMAT_9 = 0x79; // Custom format data #9 public static final int PKT_CLIENT_CUSTOM_FORMAT_A = 0x7A; // Custom format data #A public static final int PKT_CLIENT_CUSTOM_FORMAT_B = 0x7B; // Custom format data #B public static final int PKT_CLIENT_CUSTOM_FORMAT_C = 0x7C; // Custom format data #C public static final int PKT_CLIENT_CUSTOM_FORMAT_D = 0x7D; // Custom format data #D public static final int PKT_CLIENT_CUSTOM_FORMAT_E = 0x7E; // Custom format data #E public static final int PKT_CLIENT_CUSTOM_FORMAT_F = 0x7F; // Custom format data #F // Property packet public static final int PKT_CLIENT_PROPERTY_VALUE = 0xB0; // Property value // Custom format packet public static final int PKT_CLIENT_FORMAT_DEF_24 = 0xCF; // Custom format definition (24 bit field def) // Diagnostic/Error packets public static final int PKT_CLIENT_DIAGNOSTIC = 0xD0; // Diagnostic codes public static final int PKT_CLIENT_ERROR = 0xE0; // Error codes // ------------------------------------------------------------------------ // Server originated packets /* End-Of-Block packets */ public static final int PKT_SERVER_EOB_DONE = 0x00; // "" : End of transmission, query response public static final int PKT_SERVER_EOB_SPEAK_FREELY = 0x01; // "" : End of transmission, speak freely // Acknowledge packet public static final int PKT_SERVER_ACK = 0xA0; // "%*u" : Acknowledge // Property packets public static final int PKT_SERVER_GET_PROPERTY = 0xB0; // "%2u" : Get property public static final int PKT_SERVER_SET_PROPERTY = 0xB1; // "%2u%*b" : Set property // File upload packet public static final int PKT_SERVER_FILE_UPLOAD = 0xC0; // "%1x%3u%*b" : File upload // Error packets public static final int PKT_SERVER_ERROR = 0xE0; // "%2u" : NAK/Error codes // End-Of-Transmission public static final int PKT_SERVER_EOT = 0xFF; // "" : End transmission (socket will be closed) // ------------------------------------------------------------------------ // custom event payload templates /* standard fixed low-resolution event format */ private static PayloadTemplate ClientCustomEvent_30 = new PayloadTemplate( Packet.PKT_CLIENT_FIXED_FMT_STD, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), new PayloadTemplate.Field(PayloadTemplate.FIELD_TIMESTAMP , false, 0, 4), new PayloadTemplate.Field(PayloadTemplate.FIELD_GPS_POINT , false, 0, 6), new PayloadTemplate.Field(PayloadTemplate.FIELD_SPEED , false, 0, 1), new PayloadTemplate.Field(PayloadTemplate.FIELD_HEADING , false, 0, 1), new PayloadTemplate.Field(PayloadTemplate.FIELD_ALTITUDE , false, 0, 2), new PayloadTemplate.Field(PayloadTemplate.FIELD_DISTANCE , false, 0, 3), new PayloadTemplate.Field(PayloadTemplate.FIELD_SEQUENCE , false, 0, 1) } ); /* standard fixed high-resolution event format */ private static PayloadTemplate ClientCustomEvent_31 = new PayloadTemplate( Packet.PKT_CLIENT_FIXED_FMT_HIGH, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , true , 0, 2), new PayloadTemplate.Field(PayloadTemplate.FIELD_TIMESTAMP , true , 0, 4), new PayloadTemplate.Field(PayloadTemplate.FIELD_GPS_POINT , true , 0, 8), new PayloadTemplate.Field(PayloadTemplate.FIELD_SPEED , true , 0, 2), new PayloadTemplate.Field(PayloadTemplate.FIELD_HEADING , true , 0, 2), new PayloadTemplate.Field(PayloadTemplate.FIELD_ALTITUDE , true , 0, 3), new PayloadTemplate.Field(PayloadTemplate.FIELD_DISTANCE , true , 0, 3), new PayloadTemplate.Field(PayloadTemplate.FIELD_SEQUENCE , true , 0, 1) } ); // ------------------------------------------------------------------------ // packet payload templates // overloaded types: // FIELD_STATUS_CODE - numeric hex // FIELD_INDEX - numeric dec // FIELD_BINARY - binary (byte[]) // FIELD_STRING - string // FIELD_ENTITY - string private static PayloadTemplate ClientTemplate_EndOfBlock_Done = new PayloadTemplate( Packet.PKT_CLIENT_EOB_DONE, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), // checksum } ); private static PayloadTemplate ClientTemplate_EndOfBlock_More = new PayloadTemplate( Packet.PKT_CLIENT_EOB_MORE, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), // checksum } ); private static PayloadTemplate ClientTemplate_Unique_ID = new PayloadTemplate( Packet.PKT_CLIENT_UNIQUE_ID, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_BINARY , false, 0, 20), // unique-id } ); private static PayloadTemplate ClientTemplate_Account_ID = new PayloadTemplate( Packet.PKT_CLIENT_ACCOUNT_ID, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STRING , false, 0, 20), // account-id } ); private static PayloadTemplate ClientTemplate_Device_ID = new PayloadTemplate( Packet.PKT_CLIENT_DEVICE_ID, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STRING , false, 0, 20), // device-id } ); private static PayloadTemplate ClientTemplate_ProvertyValue = new PayloadTemplate( Packet.PKT_CLIENT_PROPERTY_VALUE, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), // key new PayloadTemplate.Field(PayloadTemplate.FIELD_BINARY , false, 0,253), // device-id } ); private static PayloadTemplate ClientTemplate_CustomDef = new PayloadTemplate( Packet.PKT_CLIENT_FORMAT_DEF_24, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 1), // key new PayloadTemplate.Field(PayloadTemplate.FIELD_INDEX , false, 0, 1), // count new PayloadTemplate.Field(PayloadTemplate.FIELD_BINARY , false, 0, 3), // field def }, true ); private static PayloadTemplate ClientTemplate_Diagnostic = new PayloadTemplate( Packet.PKT_CLIENT_DIAGNOSTIC, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), // code new PayloadTemplate.Field(PayloadTemplate.FIELD_BINARY , false, 0,253), // data } ); private static PayloadTemplate ClientTemplate_Error = new PayloadTemplate( Packet.PKT_CLIENT_ERROR, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), // code new PayloadTemplate.Field(PayloadTemplate.FIELD_BINARY , false, 0,253), // data } ); // ------------------------------------------------------------------------ // Client Payload template table private static PayloadTemplate ClientEventPayloadTemplate_table[] = { ClientCustomEvent_30, ClientCustomEvent_31, }; private static PayloadTemplate ClientStandardPayloadTemplate_table[] = { ClientTemplate_EndOfBlock_Done, ClientTemplate_EndOfBlock_More, ClientTemplate_Unique_ID, ClientTemplate_Account_ID, ClientTemplate_Device_ID, ClientTemplate_ProvertyValue, ClientTemplate_CustomDef, ClientTemplate_Diagnostic, ClientTemplate_Error }; public static PayloadTemplate GetClientPayloadTemplate(int type) { // These need to be in a hash table // first try events for (int i = 0; i < ClientEventPayloadTemplate_table.length; i++) { if (type == ClientEventPayloadTemplate_table[i].getPacketType()) { return ClientEventPayloadTemplate_table[i]; } } // then try the others for (int i = 0; i < ClientStandardPayloadTemplate_table.length; i++) { if (type == ClientStandardPayloadTemplate_table[i].getPacketType()) { return ClientStandardPayloadTemplate_table[i]; } } return null; } // ------------------------------------------------------------------------ // Server Payload template table private static PayloadTemplate ServerTemplate_EndOfBlock_Done = new PayloadTemplate( Packet.PKT_SERVER_EOB_DONE, new PayloadTemplate.Field[0] ); private static PayloadTemplate ServerTemplate_EndOfBlock_SpeakFreely = new PayloadTemplate( Packet.PKT_SERVER_EOB_SPEAK_FREELY, new PayloadTemplate.Field[0] ); private static PayloadTemplate ServerTemplate_Ack = new PayloadTemplate( Packet.PKT_SERVER_ACK, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 4), // sequence } ); private static PayloadTemplate ServerTemplate_GetProperty = new PayloadTemplate( Packet.PKT_SERVER_GET_PROPERTY, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 4), // property } ); private static PayloadTemplate ServerTemplate_SetProperty = new PayloadTemplate( Packet.PKT_SERVER_SET_PROPERTY, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), // property new PayloadTemplate.Field(PayloadTemplate.FIELD_BINARY , false, 0,251), // value } ); private static PayloadTemplate ServerTemplate_Error = new PayloadTemplate( Packet.PKT_SERVER_ERROR, new PayloadTemplate.Field[] { new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 2), // error new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 1), // header new PayloadTemplate.Field(PayloadTemplate.FIELD_STATUS_CODE , false, 0, 1), // type new PayloadTemplate.Field(PayloadTemplate.FIELD_BINARY , false, 0,251), // extra } ); private static PayloadTemplate ServerTemplate_EndOfTransmission = new PayloadTemplate( Packet.PKT_SERVER_EOT, new PayloadTemplate.Field[0] ); private static PayloadTemplate ServerStandardPayloadTemplate_table[] = { ServerTemplate_EndOfBlock_Done, ServerTemplate_Ack, ServerTemplate_GetProperty, ServerTemplate_SetProperty, ServerTemplate_Error, ServerTemplate_EndOfTransmission }; public static PayloadTemplate GetServerPayloadTemplate(int type) { // These need to be in a hash table for (int i = 0; i < ServerStandardPayloadTemplate_table.length; i++) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -