aboutsummaryrefslogtreecommitdiffstats
path: root/src/org/broeuschmeul/android/gps/sirf/util/SirfUtils.java
blob: 65e6b443e742d7d89f95dd4e5afb1098cf76b35b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
/*
 * Copyright (C) 2010, 2011 Herbert von Broeuschmeul
 * Copyright (C) 2010, 2011 BluetoothGPS4Droid Project
 * 
 * This file is part of BluetoothGPS4Droid.
 *
 * BluetoothGPS4Droid is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * BluetoothGPS4Droid is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with BluetoothGPS4Droid. If not, see <http://www.gnu.org/licenses/>.
 */

package org.broeuschmeul.android.gps.sirf.util;

import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.util.Formatter;
import java.util.Locale;

/**
 * Utilities for generation of SIRF III binary command.
 * 
 * It can be used to generate a array of byte corresponding 
 * to a SIRF III binary command expressed in an hexadecimal string.  
 * 
 * The input can be a complete SIRF III command or just the payload.
 * In this last case the utility will add the start and end parts of the command 
 * (including the size and the checksum).
 * 
 * @author Herbert von Broeuschmeul
 *
 */
public class SirfUtils {
	
	private static final String start ="A0A2";
	private static final String end ="B0B3";

	public static byte[] genSirfCommand(String commandHexa){
		int length = commandHexa.length()/2;		
		ByteBuffer command = ByteBuffer.allocate(length);
		command.put(new BigInteger(commandHexa,16).toByteArray(), 1,length);
		return command.array();
	}
	
	public static byte[] genSirfCommandFromPayload(String payload){
		String commandHexa = createSirfCommandFromPayload(payload);
		return genSirfCommand(commandHexa);
	}
	
	public static String createSirfCommandFromPayload(String payload){
		int length = payload.length()/2;
		String res;
		
		byte[] command = new BigInteger(payload,16).toByteArray();
		
		int checkSum = 0;
		for (byte b : command){
			checkSum += (b & 0xff);
		}
		checkSum &= 0x7FFF;
		
		res = String.format((Locale)null, "%s%04X%s%04X%s", start, length, payload, checkSum, end);
		return res;
	}
	
	public static String showSirfCommandFromPayload(String payload){
		byte[] command = genSirfCommandFromPayload(payload);
		StringBuilder out = new StringBuilder(payload.length()+16);
		Formatter fmt = new Formatter(out, null);
		for (byte b : command){
			fmt.format("%02X", b);
		}
		return out.toString();
	}
}