Rev 242 | Rev 264 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 242 | Rev 252 | ||
---|---|---|---|
1 | /******************************************************************************************************************************** |
1 | /******************************************************************************************************************************** |
2 | * |
2 | * |
3 | * Abstaction Layer to Communicate via J2ME and Bluetooth with the FlightCtrl of the MikroKopter Project (www.mikrokopter.de ) |
3 | * Abstaction Layer to Communicate via J2ME and Bluetooth with the FlightCtrl of the MikroKopter Project (www.mikrokopter.de ) |
4 | * |
4 | * |
5 | * Author: Marcus -LiGi- Bueschleb |
5 | * Author: Marcus -LiGi- Bueschleb |
6 | * |
6 | * |
7 | * see README for further Infos |
7 | * see README for further Infos |
8 | * |
8 | * |
9 | * |
9 | * |
10 | *******************************************************************************************************************************/ |
10 | *******************************************************************************************************************************/ |
11 | 11 | ||
12 | 12 | ||
13 | package org.ligi.ufo; |
13 | package org.ligi.ufo; |
14 | 14 | ||
15 | 15 | ||
16 | //#ifdef j2me |
16 | //#ifdef j2me |
17 | //# import javax.microedition.io.*; |
17 | //# import javax.microedition.io.*; |
18 | //#endif |
18 | //#endif |
19 | 19 | ||
20 | //#ifdef android |
20 | //#ifdef android |
21 | import android.util.Log; |
21 | import android.util.Log; |
22 | //#endif |
22 | //#endif |
23 | 23 | ||
24 | 24 | ||
25 | 25 | ||
26 | import java.io.*; |
26 | import java.io.*; |
27 | 27 | ||
28 | 28 | ||
29 | public class MKCommunicator |
29 | public class MKCommunicator |
30 | implements Runnable,DUBwiseDefinitions |
30 | implements Runnable,DUBwiseDefinitions |
31 | { |
31 | { |
32 | 32 | ||
33 | public int angle_nick=-4242; |
33 | public int angle_nick=-4242; |
34 | public int angle_roll=-4242; |
34 | public int angle_roll=-4242; |
35 | public byte bl_retrys=0; |
35 | public byte bl_retrys=0; |
36 | public boolean init_bootloader=false; |
36 | public boolean init_bootloader=false; |
37 | 37 | ||
38 | public byte lib_version_major=0; |
38 | public byte lib_version_major=0; |
39 | public byte lib_version_minor=4; |
39 | public byte lib_version_minor=5; |
40 | 40 | ||
41 | public String lib_version_str() |
41 | public String lib_version_str() |
42 | { |
42 | { |
43 | return "V"+lib_version_major+"."+lib_version_minor; |
43 | return "V"+lib_version_major+"."+lib_version_minor; |
44 | } |
44 | } |
45 | 45 | ||
46 | 46 | ||
47 | /***************** Section: public Attributes **********************************************/ |
47 | /***************** Section: public Attributes **********************************************/ |
48 | public boolean connected=false; // flag for the connection state |
48 | public boolean connected=false; // flag for the connection state |
49 | 49 | ||
50 | public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes |
50 | public String mk_url=""; // buffer the url which is given in the constuctor for reconnectin purposes |
51 | 51 | ||
52 | public final static int DATA_BUFF_LEN = 20; // in lines |
52 | public final static int DATA_BUFF_LEN = 20; // in lines |
53 | 53 | ||
54 | public String[] data_buff; |
54 | public String[] data_buff; |
55 | 55 | ||
56 | // boolean do_log=false; |
56 | // boolean do_log=false; |
57 | boolean do_log=true; |
57 | boolean do_log=true; |
58 | 58 | ||
59 | int data_buff_pos=0; |
59 | int data_buff_pos=0; |
60 | 60 | ||
61 | public byte user_intent=0; |
61 | public byte user_intent=0; |
62 | public final static int[] crc16_table = { |
62 | public final static int[] crc16_table = { |
63 | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, |
63 | 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7, |
64 | 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, |
64 | 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF, |
65 | 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, |
65 | 0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6, |
66 | 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, |
66 | 0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE, |
67 | 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, |
67 | 0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485, |
68 | 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, |
68 | 0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D, |
69 | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, |
69 | 0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4, |
70 | 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, |
70 | 0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC, |
71 | 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, |
71 | 0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823, |
72 | 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, |
72 | 0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B, |
73 | 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, |
73 | 0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12, |
74 | 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, |
74 | 0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A, |
75 | 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, |
75 | 0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41, |
76 | 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, |
76 | 0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49, |
77 | 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, |
77 | 0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70, |
78 | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, |
78 | 0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78, |
79 | 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, |
79 | 0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F, |
80 | 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, |
80 | 0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067, |
81 | 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, |
81 | 0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E, |
82 | 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, |
82 | 0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256, |
83 | 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, |
83 | 0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D, |
84 | 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
84 | 0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405, |
85 | 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, |
85 | 0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C, |
86 | 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, |
86 | 0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634, |
87 | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, |
87 | 0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB, |
88 | 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, |
88 | 0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3, |
89 | 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, |
89 | 0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A, |
90 | 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, |
90 | 0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92, |
91 | 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, |
91 | 0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9, |
92 | 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, |
92 | 0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1, |
93 | 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, |
93 | 0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8, |
94 | 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 |
94 | 0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0 |
95 | }; |
95 | }; |
96 | 96 | ||
97 | public void log(String str) |
97 | public void log(String str) |
98 | { |
98 | { |
99 | //#ifdef android |
99 | //#ifdef android |
100 | if (do_log) Log.d("MK-Comm",str); |
100 | if (do_log) Log.d("MK-Comm",str); |
101 | //#endif |
101 | //#endif |
102 | System.out.println(str); |
102 | System.out.println(str); |
103 | } |
103 | } |
104 | 104 | ||
105 | public int CRC16(int ch, int crc) |
105 | public int CRC16(int ch, int crc) |
106 | { |
106 | { |
107 | return crc16_table[((crc >> 8) ^ (ch)) & 0xFF] ^ (crc << 8); |
107 | return crc16_table[((crc >> 8) ^ (ch)) & 0xFF] ^ (crc << 8); |
108 | } |
108 | } |
109 | 109 | ||
110 | 110 | ||
111 | public int conn_time_in_s() |
111 | public int conn_time_in_s() |
112 | { |
112 | { |
113 | if (connected) |
113 | if (connected) |
114 | return (int)((System.currentTimeMillis()-connection_start_time)/1000); |
114 | return (int)((System.currentTimeMillis()-connection_start_time)/1000); |
115 | else |
115 | else |
116 | return 0; |
116 | return 0; |
117 | } |
117 | } |
118 | public final static byte BOOTLOADER_STAGE_NONE=0; |
118 | public final static byte BOOTLOADER_STAGE_NONE=0; |
119 | public final static byte BOOTLOADER_STAGE_GOT_MKBL=1; |
119 | public final static byte BOOTLOADER_STAGE_GOT_MKBL=1; |
120 | 120 | ||
121 | byte bootloader_stage= BOOTLOADER_STAGE_NONE; |
121 | byte bootloader_stage= BOOTLOADER_STAGE_NONE; |
122 | 122 | ||
123 | public MKLCD LCD; |
123 | public MKLCD LCD; |
124 | public MKVersion version; |
124 | public MKVersion version; |
125 | public MKDebugData debug_data; |
125 | public MKDebugData debug_data; |
126 | 126 | ||
127 | public int[] extern_control; |
127 | public int[] extern_control; |
128 | 128 | ||
129 | public MKGPSPosition gps_position; |
129 | public MKGPSPosition gps_position; |
130 | 130 | ||
131 | public MKStickData stick_data; |
131 | public MKStickData stick_data; |
132 | public MKParamsParser params; |
132 | public MKParamsParser params; |
133 | public MKWatchDog watchdog; |
133 | public MKWatchDog watchdog; |
134 | public MKProxy proxy=null; |
134 | public MKProxy proxy=null; |
135 | public MKStatistics stats ; |
135 | public MKStatistics stats ; |
136 | // public DUBwiseDebug debug; |
136 | // public DUBwiseDebug debug; |
137 | 137 | ||
138 | public UFOProber ufo_prober; |
138 | public UFOProber ufo_prober; |
139 | public long connection_start_time=-1; |
139 | public long connection_start_time=-1; |
140 | 140 | ||
141 | 141 | ||
142 | public String error_str = null; |
142 | public String error_str = null; |
143 | 143 | ||
144 | 144 | ||
145 | public final static int FC_SLAVE_ADDR = 'a'+1; |
145 | public final static int FC_SLAVE_ADDR = 'a'+1; |
146 | public final static int NAVI_SLAVE_ADDR = 'a'+2; |
146 | public final static int NAVI_SLAVE_ADDR = 'a'+2; |
147 | public final static int MK3MAG_SLAVE_ADDR = 'a'+3; |
147 | public final static int MK3MAG_SLAVE_ADDR = 'a'+3; |
148 | 148 | ||
149 | 149 | ||
150 | 150 | ||
151 | 151 | ||
152 | /****************** Section: private Attributes **********************************************/ |
152 | /****************** Section: private Attributes **********************************************/ |
153 | //#ifdef j2me |
153 | //#ifdef j2me |
154 | //# private javax.microedition.io.StreamConnection connection; |
154 | //# private javax.microedition.io.StreamConnection connection; |
155 | //#endif |
155 | //#endif |
156 | 156 | ||
157 | //#ifdef android |
157 | //#ifdef android |
158 | // java.net.Socket connection; |
158 | // java.net.Socket connection; |
159 | java.net.Socket connection; |
159 | java.net.Socket connection; |
160 | //#endif |
160 | //#endif |
161 | 161 | ||
162 | 162 | ||
163 | private java.io.InputStream reader; |
163 | private java.io.InputStream reader; |
164 | private java.io.OutputStream writer; |
164 | private java.io.OutputStream writer; |
165 | 165 | ||
166 | 166 | ||
167 | 167 | ||
168 | public String name; |
168 | public String name; |
169 | // DUBwise root; |
169 | // DUBwise root; |
170 | 170 | ||
171 | 171 | ||
172 | private boolean sending=false; |
172 | private boolean sending=false; |
173 | private boolean recieving=false; |
173 | private boolean recieving=false; |
174 | 174 | ||
175 | 175 | ||
176 | 176 | ||
177 | /****************** Section: public Methods ************************************************/ |
177 | /****************** Section: public Methods ************************************************/ |
178 | public MKCommunicator() |
178 | public MKCommunicator() |
179 | { |
179 | { |
180 | 180 | ||
181 | data_buff=new String[DATA_BUFF_LEN]; |
181 | data_buff=new String[DATA_BUFF_LEN]; |
182 | for (int i=0;i<DATA_BUFF_LEN;i++) |
182 | for (int i=0;i<DATA_BUFF_LEN;i++) |
183 | data_buff[i]=""; |
183 | data_buff[i]=""; |
184 | // debug=debug_; |
184 | // debug=debug_; |
185 | // root=root_; |
185 | // root=root_; |
186 | version=new MKVersion(); |
186 | version=new MKVersion(); |
187 | debug_data=new MKDebugData(); |
187 | debug_data=new MKDebugData(); |
188 | stick_data=new MKStickData(); |
188 | stick_data=new MKStickData(); |
189 | params=new MKParamsParser(); |
189 | params=new MKParamsParser(); |
190 | extern_control=new int[EXTERN_CONTROL_LENGTH]; |
190 | extern_control=new int[EXTERN_CONTROL_LENGTH]; |
191 | extern_control[EXTERN_CONTROL_CONFIG]=1; |
191 | extern_control[EXTERN_CONTROL_CONFIG]=1; |
192 | extern_control[EXTERN_CONTROL_FRAME]=1; |
192 | extern_control[EXTERN_CONTROL_FRAME]=1; |
193 | 193 | ||
194 | LCD= new MKLCD(this); |
194 | LCD= new MKLCD(this); |
195 | watchdog=new MKWatchDog(this); |
195 | watchdog=new MKWatchDog(this); |
196 | gps_position=new MKGPSPosition(); |
196 | gps_position=new MKGPSPosition(); |
197 | stats = new MKStatistics(); |
197 | stats = new MKStatistics(); |
198 | proxy =new MKProxy(this); |
198 | proxy =new MKProxy(this); |
199 | ufo_prober=new UFOProber(); |
199 | ufo_prober=new UFOProber(); |
200 | new Thread( this ).start(); // fire up main Thread |
200 | new Thread( this ).start(); // fire up main Thread |
201 | } |
201 | } |
202 | 202 | ||
203 | 203 | ||
204 | 204 | ||
205 | public void write_raw(byte[] _data) |
205 | public void write_raw(byte[] _data) |
206 | { |
206 | { |
207 | wait4send(); |
207 | wait4send(); |
208 | sending=true; |
208 | sending=true; |
209 | try { |
209 | try { |
210 | writer.write(_data,0,_data.length); |
210 | writer.write(_data,0,_data.length); |
211 | writer.flush(); |
211 | writer.flush(); |
212 | 212 | ||
213 | stats.bytes_out+=_data.length; |
213 | stats.bytes_out+=_data.length; |
214 | } |
214 | } |
215 | catch ( Exception e){} |
215 | catch ( Exception e){} |
216 | sending=false; |
216 | sending=false; |
217 | } |
217 | } |
218 | 218 | ||
219 | public void do_proxy(String proxy_url) |
219 | public void do_proxy(String proxy_url) |
220 | { |
220 | { |
221 | proxy.connect(proxy_url); |
221 | proxy.connect(proxy_url); |
222 | } |
222 | } |
223 | 223 | ||
224 | // int port; |
224 | // int port; |
225 | 225 | ||
226 | // URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control |
226 | // URL string: "btspp://XXXXXXXXXXXX:1" - the X-Part is the MAC-Adress of the Bluetooth-Device connected to the Fligth-Control |
227 | public void connect_to(String _url,String _name) |
227 | public void connect_to(String _url,String _name) |
228 | { |
228 | { |
229 | // port=_port; |
229 | // port=_port; |
230 | mk_url=_url; // remember URL for connecting / reconnecting later |
230 | mk_url=_url; // remember URL for connecting / reconnecting later |
231 | name=_name; |
231 | name=_name; |
232 | force_disconnect=false; |
232 | force_disconnect=false; |
233 | connected=false; |
233 | connected=false; |
234 | } |
234 | } |
235 | 235 | ||
236 | public boolean ready() |
236 | public boolean ready() |
237 | { |
237 | { |
238 | return (connected&&(version.major!=-1)); |
238 | return (connected&&(version.major!=-1)); |
239 | } |
239 | } |
240 | 240 | ||
241 | 241 | ||
242 | public String get_buff(int age) |
242 | public String get_buff(int age) |
243 | { |
243 | { |
244 | 244 | ||
245 | age%=DATA_BUFF_LEN; |
245 | age%=DATA_BUFF_LEN; |
246 | 246 | ||
247 | if (age<=data_buff_pos) |
247 | if (age<=data_buff_pos) |
248 | return ""+data_buff[data_buff_pos-age]; |
248 | return ""+data_buff[data_buff_pos-age]; |
249 | else |
249 | else |
250 | return ""+data_buff[DATA_BUFF_LEN+data_buff_pos-age]; |
250 | return ""+data_buff[DATA_BUFF_LEN+data_buff_pos-age]; |
251 | 251 | ||
252 | 252 | ||
253 | } |
253 | } |
254 | /****************** Section: private Methods ************************************************/ |
254 | /****************** Section: private Methods ************************************************/ |
255 | private void connect() |
255 | private void connect() |
256 | { |
256 | { |
257 | log("trying to connect to" + mk_url); |
257 | log("trying to connect to" + mk_url); |
258 | try{ |
258 | try{ |
259 | 259 | ||
260 | // old call |
260 | // old call |
261 | // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE); |
261 | // connection = (StreamConnection) Connector.open(mk_url, Connector.READ_WRITE); |
262 | 262 | ||
263 | //#ifdef android |
263 | //#ifdef android |
264 | connection = (new java.net.Socket(mk_url.split(":")[0],Integer.parseInt(mk_url.split(":")[1]))); |
264 | connection = (new java.net.Socket(mk_url.split(":")[0],Integer.parseInt(mk_url.split(":")[1]))); |
265 | //.Socket |
265 | //.Socket |
266 | 266 | ||
267 | reader=connection.getInputStream(); |
267 | reader=connection.getInputStream(); |
268 | writer=connection.getOutputStream(); |
268 | writer=connection.getOutputStream(); |
269 | 269 | ||
270 | String magic="conn:foo bar\r\n"; |
270 | String magic="conn:foo bar\r\n"; |
271 | writer.write(magic.getBytes()); |
271 | writer.write(magic.getBytes()); |
272 | writer.flush(); |
272 | writer.flush(); |
273 | 273 | ||
274 | //#else |
274 | //#else |
275 | 275 | ||
276 | //# connection = (StreamConnection) Connector.open(mk_url); |
276 | //# connection = (StreamConnection) Connector.open(mk_url); |
277 | 277 | ||
278 | //# reader=connection.openInputStream(); |
278 | //# reader=connection.openInputStream(); |
279 | //# writer=connection.openOutputStream(); |
279 | //# writer=connection.openOutputStream(); |
280 | 280 | ||
281 | //#endif |
281 | //#endif |
282 | connection_start_time=System.currentTimeMillis(); |
282 | connection_start_time=System.currentTimeMillis(); |
283 | connected=true; // if we get here everything seems to be OK |
283 | connected=true; // if we get here everything seems to be OK |
284 | 284 | ||
285 | stats.reset(); |
285 | stats.reset(); |
286 | 286 | ||
287 | log("connecting OK"); |
287 | log("connecting OK"); |
288 | } |
288 | } |
289 | catch (Exception ex) |
289 | catch (Exception ex) |
290 | { |
290 | { |
291 | // TODO difference fatal errors from those which will lead to reconnection |
291 | // TODO difference fatal errors from those which will lead to reconnection |
292 | log("Problem connecting" + "\n" + ex); |
292 | log("Problem connecting" + "\n" + ex); |
293 | } |
293 | } |
294 | } |
294 | } |
295 | 295 | ||
296 | public int[] Decode64(byte[] in_arr, int offset,int len) |
296 | public int[] Decode64(byte[] in_arr, int offset,int len) |
297 | { |
297 | { |
298 | int ptrIn=offset; |
298 | int ptrIn=offset; |
299 | int a,b,c,d,x,y,z; |
299 | int a,b,c,d,x,y,z; |
300 | int ptr=0; |
300 | int ptr=0; |
301 | 301 | ||
302 | int[] out_arr=new int[len]; |
302 | int[] out_arr=new int[len]; |
303 | 303 | ||
304 | while(len!=0) |
304 | while(len!=0) |
305 | { |
305 | { |
306 | a=0; |
306 | a=0; |
307 | b=0; |
307 | b=0; |
308 | c=0; |
308 | c=0; |
309 | d=0; |
309 | d=0; |
310 | try { |
310 | try { |
311 | a = in_arr[ptrIn++] - '='; |
311 | a = in_arr[ptrIn++] - '='; |
312 | b = in_arr[ptrIn++] - '='; |
312 | b = in_arr[ptrIn++] - '='; |
313 | c = in_arr[ptrIn++] - '='; |
313 | c = in_arr[ptrIn++] - '='; |
314 | d = in_arr[ptrIn++] - '='; |
314 | d = in_arr[ptrIn++] - '='; |
315 | } |
315 | } |
316 | catch (Exception e) {} |
316 | catch (Exception e) {} |
317 | //if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
317 | //if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
318 | 318 | ||
319 | x = (a << 2) | (b >> 4); |
319 | x = (a << 2) | (b >> 4); |
320 | y = ((b & 0x0f) << 4) | (c >> 2); |
320 | y = ((b & 0x0f) << 4) | (c >> 2); |
321 | z = ((c & 0x03) << 6) | d; |
321 | z = ((c & 0x03) << 6) | d; |
322 | 322 | ||
323 | if((len--)!=0) out_arr[ptr++] = x; else break; |
323 | if((len--)!=0) out_arr[ptr++] = x; else break; |
324 | if((len--)!=0) out_arr[ptr++] = y; else break; |
324 | if((len--)!=0) out_arr[ptr++] = y; else break; |
325 | if((len--)!=0) out_arr[ptr++] = z; else break; |
325 | if((len--)!=0) out_arr[ptr++] = z; else break; |
326 | } |
326 | } |
327 | 327 | ||
328 | return out_arr; |
328 | return out_arr; |
329 | 329 | ||
330 | } |
330 | } |
331 | 331 | ||
332 | public void wait4send() |
332 | public void wait4send() |
333 | { |
333 | { |
334 | while(sending) //||recieving) |
334 | while(sending) //||recieving) |
335 | sleep(50); |
335 | sleep(50); |
336 | } |
336 | } |
337 | 337 | ||
338 | 338 | ||
339 | public void sleep(int time) |
339 | public void sleep(int time) |
340 | { |
340 | { |
341 | try { Thread.sleep(time); } |
341 | try { Thread.sleep(time); } |
342 | catch (Exception e) { } |
342 | catch (Exception e) { } |
343 | } |
343 | } |
344 | 344 | ||
345 | // FC - Function Mappers |
345 | // FC - Function Mappers |
346 | 346 | ||
347 | // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives |
347 | // send a version Request to the FC - the reply to this request will be processed in process_data when it arrives |
348 | public void get_version() |
348 | public void get_version() |
349 | { |
349 | { |
350 | stats.version_data_request_count++; |
350 | stats.version_data_request_count++; |
351 | send_command(0,'v'); |
351 | send_command(0,'v'); |
352 | } |
352 | } |
353 | 353 | ||
354 | public void set_gps_target(int longitude,int latitude) |
354 | public void set_gps_target(int longitude,int latitude) |
355 | { |
355 | { |
356 | int[] target=new int[8]; |
356 | int[] target=new int[8]; |
357 | target[0]= (0xFF)&(longitude<<24); |
357 | target[0]= (0xFF)&(longitude<<24); |
358 | target[1]= (0xFF)&(longitude<<16); |
358 | target[1]= (0xFF)&(longitude<<16); |
359 | target[2]= (0xFF)&(longitude<<8); |
359 | target[2]= (0xFF)&(longitude<<8); |
360 | target[3]= (0xFF)&(longitude); |
360 | target[3]= (0xFF)&(longitude); |
361 | // send_command(0,'s',target); |
361 | // send_command(0,'s',target); |
362 | } |
362 | } |
363 | 363 | ||
364 | // send a MotorTest request - params are the speed for each Motor |
364 | // send a MotorTest request - params are the speed for each Motor |
365 | public void motor_test(int[] params) |
365 | public void motor_test(int[] params) |
366 | { |
366 | { |
367 | stats.motortest_request_count++; |
367 | stats.motortest_request_count++; |
368 | send_command(FC_SLAVE_ADDR,'t',params); |
368 | send_command(FC_SLAVE_ADDR,'t',params); |
369 | } |
369 | } |
370 | 370 | ||
371 | 371 | ||
372 | public void send_extern_control() |
372 | public void send_extern_control() |
373 | { |
373 | { |
374 | 374 | ||
375 | stats.external_control_request_count++; |
375 | stats.external_control_request_count++; |
376 | send_command(FC_SLAVE_ADDR,'b',extern_control); |
376 | send_command(FC_SLAVE_ADDR,'b',extern_control); |
377 | } |
377 | } |
378 | 378 | ||
379 | /* public void send_keys(int[] params) |
379 | /* public void send_keys(int[] params) |
380 | { |
380 | { |
381 | send_command(FC_SLAVE_ADDR,'k',params); |
381 | send_command(FC_SLAVE_ADDR,'k',params); |
382 | }*/ |
382 | }*/ |
383 | 383 | ||
384 | // get params |
384 | // get params |
385 | public void get_params(int id) |
385 | public void get_params(int id) |
386 | { |
386 | { |
387 | wait4send(); |
387 | wait4send(); |
388 | send_command(FC_SLAVE_ADDR,'q',id+1); |
388 | send_command(FC_SLAVE_ADDR,'q',id+1); |
389 | stats.params_data_request_count++; |
389 | stats.params_data_request_count++; |
390 | } |
390 | } |
391 | 391 | ||
392 | public void get_debug_name(int id) |
392 | public void get_debug_name(int id) |
393 | { |
393 | { |
394 | 394 | ||
395 | wait4send(); |
395 | wait4send(); |
396 | send_command(0,'a',id); |
396 | send_command(0,'a',id); |
397 | } |
397 | } |
398 | 398 | ||
399 | 399 | ||
400 | public void trigger_LCD_by_page(int page) |
400 | public void trigger_LCD_by_page(int page) |
401 | { |
401 | { |
402 | wait4send(); |
402 | wait4send(); |
403 | send_command(0,'l',page); |
403 | send_command(0,'l',page); |
404 | stats.lcd_data_request_count++; |
404 | stats.lcd_data_request_count++; |
405 | } |
405 | } |
406 | 406 | ||
407 | public void trigger_debug() |
407 | public void trigger_debug() |
408 | { |
408 | { |
409 | if (sending||recieving) return; // its not that important - can be dropped |
409 | if (sending||recieving) return; // its not that important - can be dropped |
410 | send_command(0,'c'); |
410 | send_command(0,'c'); |
411 | } |
411 | } |
412 | 412 | ||
413 | 413 | ||
414 | public void switch_todo() |
414 | public void switch_todo() |
415 | { |
415 | { |
416 | sleep(50); |
416 | sleep(50); |
417 | version=new MKVersion(); |
417 | version=new MKVersion(); |
418 | LCD= new MKLCD(this); |
418 | LCD= new MKLCD(this); |
419 | debug_data=new MKDebugData(); |
419 | debug_data=new MKDebugData(); |
420 | 420 | ||
421 | } |
421 | } |
422 | 422 | ||
423 | public void switch_to_fc() |
423 | public void switch_to_fc() |
424 | { |
424 | { |
425 | wait4send(); |
425 | wait4send(); |
426 | send_command(NAVI_SLAVE_ADDR,'u',0); |
426 | send_command(NAVI_SLAVE_ADDR,'u',0); |
427 | switch_todo(); |
427 | switch_todo(); |
428 | 428 | ||
429 | } |
429 | } |
430 | 430 | ||
431 | 431 | ||
432 | public void switch_to_mk3mag() |
432 | public void switch_to_mk3mag() |
433 | { |
433 | { |
434 | wait4send(); |
434 | wait4send(); |
435 | send_command(NAVI_SLAVE_ADDR ,'u',1); |
435 | send_command(NAVI_SLAVE_ADDR ,'u',1); |
436 | switch_todo(); |
436 | switch_todo(); |
437 | } |
437 | } |
438 | 438 | ||
439 | public final static byte[] navi_switch_magic={27,27,0x55,(byte)0xAA,0,(byte)'\r'}; |
439 | public final static byte[] navi_switch_magic={27,27,0x55,(byte)0xAA,0,(byte)'\r'}; |
440 | public void switch_to_navi() |
440 | public void switch_to_navi() |
441 | { |
441 | { |
442 | wait4send(); |
442 | wait4send(); |
443 | sending=true; |
443 | sending=true; |
444 | try |
444 | try |
445 | { |
445 | { |
446 | writer.write(navi_switch_magic); |
446 | writer.write(navi_switch_magic); |
447 | stats.bytes_out+=6; |
447 | stats.bytes_out+=6; |
448 | writer.flush(); |
448 | writer.flush(); |
449 | } |
449 | } |
450 | catch (Exception e) { } |
450 | catch (Exception e) { } |
451 | sending=false; |
451 | sending=false; |
452 | 452 | ||
453 | switch_todo(); |
453 | switch_todo(); |
454 | 454 | ||
455 | } |
455 | } |
456 | 456 | ||
457 | public String[] flash_msgs; |
457 | public String[] flash_msgs; |
458 | int msg_pos=0; |
458 | int msg_pos=0; |
459 | 459 | ||
460 | 460 | ||
461 | public boolean bootloader_intension_flash=false; |
461 | public boolean bootloader_intension_flash=false; |
462 | 462 | ||
463 | public boolean bootloader_finish_ok=false; |
463 | public boolean bootloader_finish_ok=false; |
464 | 464 | ||
465 | public void jump_bootloader() |
465 | public void jump_bootloader() |
466 | { |
466 | { |
467 | 467 | ||
468 | bootloader_finish_ok=false; |
468 | bootloader_finish_ok=false; |
469 | msg_pos=0; |
469 | msg_pos=0; |
470 | bootloader_stage= BOOTLOADER_STAGE_NONE; |
470 | bootloader_stage= BOOTLOADER_STAGE_NONE; |
471 | flash_msgs=new String[100]; |
471 | flash_msgs=new String[100]; |
472 | flash_msgs[msg_pos++]="Initiializing Bootloader"; |
472 | flash_msgs[msg_pos++]="Initiializing Bootloader"; |
473 | wait4send(); |
473 | wait4send(); |
474 | sending=true; |
474 | sending=true; |
475 | 475 | ||
476 | try |
476 | try |
477 | { |
477 | { |
478 | int attempt=0; |
478 | int attempt=0; |
479 | 479 | ||
480 | while(bootloader_stage!= BOOTLOADER_STAGE_GOT_MKBL) |
480 | while(bootloader_stage!= BOOTLOADER_STAGE_GOT_MKBL) |
481 | { |
481 | { |
482 | flash_msgs[msg_pos]="attempt "+attempt; |
482 | flash_msgs[msg_pos]="attempt "+attempt; |
483 | attempt++; |
483 | attempt++; |
484 | send_command_nocheck((byte)FC_SLAVE_ADDR,'R',new int[0]); |
484 | send_command_nocheck((byte)FC_SLAVE_ADDR,'R',new int[0]); |
485 | 485 | ||
486 | try{ |
486 | try{ |
487 | writer.write( 27); |
487 | writer.write( 27); |
488 | writer.flush(); |
488 | writer.flush(); |
489 | 489 | ||
490 | sleep(20); |
490 | sleep(20); |
491 | 491 | ||
492 | writer.write( 0xAA); |
492 | writer.write( 0xAA); |
493 | writer.flush(); |
493 | writer.flush(); |
494 | } |
494 | } |
495 | catch (Exception e) { } |
495 | catch (Exception e) { } |
496 | sleep((attempt%2==0)?80:800); //800 |
496 | sleep((attempt%2==0)?80:800); //800 |
497 | } |
497 | } |
498 | msg_pos++; |
498 | msg_pos++; |
499 | } |
499 | } |
500 | 500 | ||
501 | catch (Exception e) { |
501 | catch (Exception e) { |
502 | flash_msgs[msg_pos++]="Exception:" +e.getMessage() ; |
502 | flash_msgs[msg_pos++]="Exception:" +e.getMessage() ; |
503 | flash_msgs[msg_pos++]=e.toString() ; |
503 | flash_msgs[msg_pos++]=e.toString() ; |
504 | } |
504 | } |
505 | 505 | ||
506 | new Thread( this ).start(); // fire up main Thread |
506 | new Thread( this ).start(); // fire up main Thread |
507 | } |
507 | } |
508 | 508 | ||
509 | 509 | ||
510 | public void get_error_str() |
510 | public void get_error_str() |
511 | { |
511 | { |
512 | send_command(NAVI_SLAVE_ADDR,'e'); |
512 | send_command(NAVI_SLAVE_ADDR,'e'); |
513 | } |
513 | } |
514 | 514 | ||
515 | public void trigger_rcdata() |
515 | public void trigger_rcdata() |
516 | { |
516 | { |
517 | send_command(FC_SLAVE_ADDR,'p'); |
517 | send_command(FC_SLAVE_ADDR,'p'); |
518 | } |
518 | } |
519 | 519 | ||
520 | 520 | ||
521 | public void write_params(int to) |
521 | public void write_params(int to) |
522 | { |
522 | { |
523 | params.update_backup(to); |
523 | params.update_backup(to); |
524 | write_params_(to) ; |
524 | write_params_(to) ; |
525 | } |
525 | } |
526 | 526 | ||
527 | public void write_params_(int to) |
527 | public void write_params_(int to) |
528 | { |
528 | { |
529 | wait4send(); |
529 | wait4send(); |
530 | params.active_paramset=to; |
530 | params.active_paramset=to; |
531 | send_command(FC_SLAVE_ADDR,'s',params.field_bak[to]); |
531 | send_command(FC_SLAVE_ADDR,'s',params.field_bak[to]); |
532 | } |
532 | } |
533 | 533 | ||
534 | public void send_command(int modul,char cmd) |
534 | public void send_command(int modul,char cmd) |
535 | { |
535 | { |
536 | send_command(modul,cmd,new int[0]); |
536 | send_command(modul,cmd,new int[0]); |
537 | } |
537 | } |
538 | 538 | ||
539 | public void send_command(int modul,char cmd,int param) |
539 | public void send_command(int modul,char cmd,int param) |
540 | { |
540 | { |
541 | int[] params=new int[1]; |
541 | int[] params=new int[1]; |
542 | params[0]=param; |
542 | params[0]=param; |
543 | send_command(modul,cmd,params); |
543 | send_command(modul,cmd,params); |
544 | } |
544 | } |
545 | 545 | ||
546 | public void send_command_nocheck(byte modul,char cmd,int[] params) |
546 | public void send_command_nocheck(byte modul,char cmd,int[] params) |
547 | { |
547 | { |
548 | // char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
548 | // char[] send_buff=new char[5 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
549 | 549 | ||
550 | byte[] send_buff=new byte[3 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
550 | byte[] send_buff=new byte[3 + (params.length/3 + (params.length%3==0?0:1) )*4]; // 5=1*start_char+1*addr+1*cmd+2*crc |
551 | send_buff[0]='#'; |
551 | send_buff[0]='#'; |
552 | send_buff[1]=modul; |
552 | send_buff[1]=modul; |
553 | send_buff[2]=(byte)cmd; |
553 | send_buff[2]=(byte)cmd; |
554 | 554 | ||
555 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
555 | for(int param_pos=0;param_pos<(params.length/3 + (params.length%3==0?0:1)) ;param_pos++) |
556 | { |
556 | { |
557 | int a = (param_pos*3<params.length)?params[param_pos*3]:0; |
557 | int a = (param_pos*3<params.length)?params[param_pos*3]:0; |
558 | int b = ((param_pos*3+1)<params.length)?params[param_pos*3+1]:0; |
558 | int b = ((param_pos*3+1)<params.length)?params[param_pos*3+1]:0; |
559 | int c = ((param_pos*3+2)<params.length)?params[param_pos*3+2]:0; |
559 | int c = ((param_pos*3+2)<params.length)?params[param_pos*3+2]:0; |
560 | 560 | ||
561 | send_buff[3+param_pos*4] = (byte)((a >> 2)+'=' ); |
561 | send_buff[3+param_pos*4] = (byte)((a >> 2)+'=' ); |
562 | send_buff[3+param_pos*4+1] = (byte)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
562 | send_buff[3+param_pos*4+1] = (byte)('=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4))); |
563 | send_buff[3+param_pos*4+2] = (byte)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
563 | send_buff[3+param_pos*4+2] = (byte)('=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6))); |
564 | send_buff[3+param_pos*4+3] = (byte)('=' + ( c & 0x3f)); |
564 | send_buff[3+param_pos*4+3] = (byte)('=' + ( c & 0x3f)); |
565 | 565 | ||
566 | //send_buff[3+foo]='='; |
566 | //send_buff[3+foo]='='; |
567 | } |
567 | } |
568 | 568 | ||
569 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
569 | /* for(int foo=0;foo<(params.length/3 + (params.length%3==0?0:1) )*4;foo++) |
570 | { |
570 | { |
571 | int a = (foo<params.length) params[foo]; |
571 | int a = (foo<params.length) params[foo]; |
572 | int a = params[foo]; |
572 | int a = params[foo]; |
573 | |
573 | |
574 | //send_buff[3+foo]='='; |
574 | //send_buff[3+foo]='='; |
575 | } |
575 | } |
576 | */ |
576 | */ |
577 | try |
577 | try |
578 | { |
578 | { |
579 | int tmp_crc=0; |
579 | int tmp_crc=0; |
580 | for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++) |
580 | for ( int tmp_i=0; tmp_i<send_buff.length;tmp_i++) |
581 | tmp_crc+=(int)send_buff[tmp_i]; |
581 | tmp_crc+=(int)send_buff[tmp_i]; |
582 | 582 | ||
583 | writer.write(send_buff,0,send_buff.length); |
583 | writer.write(send_buff,0,send_buff.length); |
584 | tmp_crc%=4096; |
584 | tmp_crc%=4096; |
585 | 585 | ||
586 | writer.write( (char)(tmp_crc/64 + '=')); |
586 | writer.write( (char)(tmp_crc/64 + '=')); |
587 | writer.write( (char)(tmp_crc%64 + '=')); |
587 | writer.write( (char)(tmp_crc%64 + '=')); |
588 | writer.write('\r'); |
588 | writer.write('\r'); |
589 | stats.bytes_out+=send_buff.length+3; |
589 | stats.bytes_out+=send_buff.length+3; |
590 | writer.flush(); |
590 | writer.flush(); |
591 | } |
591 | } |
592 | catch (Exception e) |
592 | catch (Exception e) |
593 | { // problem sending data to FC |
593 | { // problem sending data to FC |
594 | } |
594 | } |
595 | 595 | ||
596 | } |
596 | } |
597 | // send command to FC ( add crc and pack into pseudo Base64 |
597 | // send command to FC ( add crc and pack into pseudo Base64 |
598 | public void send_command(int modul,char cmd,int[] params) |
598 | public void send_command(int modul,char cmd,int[] params) |
599 | { |
599 | { |
600 | // if (modul==0) return; |
600 | // if (modul==0) return; |
601 | sending=true; |
601 | sending=true; |
602 | send_command_nocheck((byte)modul,cmd,params); |
602 | send_command_nocheck((byte)modul,cmd,params); |
603 | sending=false; |
603 | sending=false; |
604 | } |
604 | } |
605 | 605 | ||
606 | 606 | ||
607 | public int slave_addr=-1; |
607 | public int slave_addr=-1; |
608 | 608 | ||
609 | 609 | ||
610 | public int UBatt() |
610 | public int UBatt() |
611 | { |
611 | { |
612 | if (ufo_prober.is_mk()) |
612 | if (ufo_prober.is_mk()) |
613 | return debug_data.UBatt(); |
613 | return debug_data.UBatt(); |
614 | else if (ufo_prober.is_navi()) |
614 | else if (ufo_prober.is_navi()) |
615 | return gps_position.UBatt; |
615 | return gps_position.UBatt; |
616 | 616 | ||
617 | return -1; |
617 | return -1; |
618 | 618 | ||
619 | } |
619 | } |
620 | 620 | ||
621 | 621 | ||
622 | 622 | ||
623 | public int SenderOkay() |
623 | public int SenderOkay() |
624 | { |
624 | { |
625 | if (ufo_prober.is_mk()) |
625 | if (ufo_prober.is_mk()) |
626 | return debug_data.SenderOkay(); |
626 | return debug_data.SenderOkay(); |
627 | else if (ufo_prober.is_navi()) |
627 | else if (ufo_prober.is_navi()) |
628 | return gps_position.SenderOkay; |
628 | return gps_position.SenderOkay; |
629 | 629 | ||
630 | return -1; |
630 | return -1; |
631 | 631 | ||
632 | } |
632 | } |
633 | 633 | ||
634 | 634 | ||
635 | public void process_data(byte[] data,int len) |
635 | public void process_data(byte[] data,int len) |
636 | { |
636 | { |
637 | 637 | ||
638 | 638 | ||
639 | log("command " +(char)data[2] ); |
639 | log("command " +(char)data[2] ); |
640 | switch((char)data[2]) |
640 | switch((char)data[2]) |
641 | { |
641 | { |
642 | 642 | ||
643 | case 'A': // debug Data Names |
643 | case 'A': // debug Data Names |
644 | stats.debug_names_count++; |
644 | stats.debug_names_count++; |
645 | debug_data.set_names_by_mk_data(Decode64(data,3,len-3)); |
645 | debug_data.set_names_by_mk_data(Decode64(data,3,len-3)); |
646 | break; |
646 | break; |
647 | 647 | ||
648 | case 'B': // external_control confirm frames |
648 | case 'B': // external_control confirm frames |
649 | stats.external_control_confirm_frame_count++; |
649 | stats.external_control_confirm_frame_count++; |
650 | break; |
650 | break; |
651 | 651 | ||
652 | case 'L': // LCD Data |
652 | case 'L': // LCD Data |
653 | stats.lcd_data_count++; |
653 | stats.lcd_data_count++; |
654 | LCD.handle_lcd_data(Decode64(data,3,len-3)); |
654 | LCD.handle_lcd_data(Decode64(data,3,len-3)); |
655 | 655 | ||
656 | break; |
656 | break; |
657 | 657 | ||
658 | case 'D': // debug Data |
658 | case 'D': // debug Data |
659 | log("got debug data"); |
659 | log("got debug data"); |
660 | stats.debug_data_count++; |
660 | stats.debug_data_count++; |
661 | debug_data.set_by_mk_data(Decode64(data,3,len-3),version); |
661 | debug_data.set_by_mk_data(Decode64(data,3,len-3),version); |
662 | log("processed debug data"); |
662 | log("processed debug data"); |
663 | break; |
663 | break; |
664 | 664 | ||
665 | case 'V': // Version Info |
665 | case 'V': // Version Info |
666 | stats.version_data_count++; |
666 | stats.version_data_count++; |
667 | slave_addr=data[1]; |
667 | slave_addr=data[1]; |
668 | 668 | ||
669 | switch(slave_addr) |
669 | switch(slave_addr) |
670 | { |
670 | { |
671 | case FC_SLAVE_ADDR: |
671 | case FC_SLAVE_ADDR: |
672 | ufo_prober.set_to_mk(); |
672 | ufo_prober.set_to_mk(); |
673 | break; |
673 | break; |
674 | 674 | ||
675 | case NAVI_SLAVE_ADDR: |
675 | case NAVI_SLAVE_ADDR: |
676 | ufo_prober.set_to_navi(); |
676 | ufo_prober.set_to_navi(); |
677 | break; |
677 | break; |
678 | 678 | ||
679 | case MK3MAG_SLAVE_ADDR: |
679 | case MK3MAG_SLAVE_ADDR: |
680 | // ufo_prober.set_to_mk(); |
680 | // ufo_prober.set_to_mk(); |
681 | ufo_prober.set_to_mk3mag(); |
681 | ufo_prober.set_to_mk3mag(); |
682 | break; |
682 | break; |
683 | 683 | ||
684 | default: |
684 | default: |
685 | ufo_prober.set_to_incompatible(); |
685 | ufo_prober.set_to_incompatible(); |
686 | break; |
686 | break; |
687 | } |
687 | } |
688 | 688 | ||
689 | 689 | ||
690 | version.set_by_mk_data(Decode64(data,3,len-3)); |
690 | version.set_by_mk_data(Decode64(data,3,len-3)); |
691 | break; |
691 | break; |
692 | 692 | ||
693 | case 'w': |
693 | case 'w': |
694 | int[] dec=Decode64(data,3,len-3); |
694 | int[] dec=Decode64(data,3,len-3); |
695 | angle_nick=MKHelper.parse_signed_int_2(dec[0],dec[1]); |
695 | angle_nick=MKHelper.parse_signed_int_2(dec[0],dec[1]); |
696 | angle_roll=MKHelper.parse_signed_int_2(dec[2],dec[3]); |
696 | angle_roll=MKHelper.parse_signed_int_2(dec[2],dec[3]); |
697 | stats.angle_data_count++; |
697 | stats.angle_data_count++; |
698 | 698 | ||
699 | break; |
699 | break; |
700 | 700 | ||
701 | 701 | ||
702 | case 'Q': |
702 | case 'Q': |
703 | if (ufo_prober.is_mk()) |
703 | if (ufo_prober.is_mk()) |
704 | { |
704 | { |
705 | stats.params_data_count++; |
705 | stats.params_data_count++; |
706 | params.set_by_mk_data(Decode64(data,3,len-3)); |
706 | params.set_by_mk_data(Decode64(data,3,len-3)); |
707 | } |
707 | } |
708 | break; |
708 | break; |
709 | 709 | ||
710 | case 'P': |
710 | case 'P': |
711 | stats.stick_data_count++; |
711 | stats.stick_data_count++; |
712 | stick_data.set_by_mk_data(Decode64(data,3,20)); |
712 | stick_data.set_by_mk_data(Decode64(data,3,20)); |
713 | break; |
713 | break; |
714 | 714 | ||
715 | 715 | ||
716 | case 'E': |
716 | case 'E': |
717 | int[] dec_data=Decode64(data,3,len-3); |
717 | int[] dec_data=Decode64(data,3,len-3); |
718 | error_str=""; |
718 | error_str=""; |
719 | for(int foo=0;foo<20;foo++) |
719 | for(int foo=0;foo<20;foo++) |
720 | if (dec_data[foo]!=0) |
720 | if (dec_data[foo]!=0) |
721 | error_str+=(char)dec_data[foo]; |
721 | error_str+=(char)dec_data[foo]; |
722 | break; |
722 | break; |
723 | 723 | ||
724 | 724 | ||
725 | case 'O': |
725 | case 'O': |
726 | stats.navi_data_count++; |
726 | stats.navi_data_count++; |
727 | log("got navi data(" + len +"):"); |
727 | log("got navi data(" + len +"):"); |
728 | 728 | ||
729 | gps_position.set_by_mk_data(Decode64(data,3,len-3),version); |
729 | gps_position.set_by_mk_data(Decode64(data,3,len-3),version); |
730 | 730 | ||
731 | log("long:" + gps_position.Longitude); |
731 | log("long:" + gps_position.Longitude); |
732 | log("lat:" + gps_position.Latitude); |
732 | log("lat:" + gps_position.Latitude); |
733 | 733 | ||
734 | break; |
734 | break; |
735 | 735 | ||
736 | 736 | ||
737 | // Error from Navi |
737 | // Error from Navi |
738 | 738 | ||
739 | 739 | ||
740 | default: |
740 | default: |
741 | stats.other_data_count++; |
741 | stats.other_data_count++; |
742 | break; |
742 | break; |
743 | 743 | ||
744 | } |
744 | } |
745 | 745 | ||
746 | } |
746 | } |
747 | 747 | ||
748 | public boolean force_disconnect=true; |
748 | public boolean force_disconnect=true; |
749 | 749 | ||
750 | public void close_connections(boolean force) |
750 | public void close_connections(boolean force) |
751 | { |
751 | { |
752 | // if ((!force)&&root.canvas.do_vibra) root.vibrate(500); |
752 | // if ((!force)&&root.canvas.do_vibra) root.vibrate(500); |
753 | force_disconnect=force; |
753 | force_disconnect=force; |
754 | try{ reader.close(); } |
754 | try{ reader.close(); } |
755 | catch (Exception inner_ex) { } |
755 | catch (Exception inner_ex) { } |
756 | 756 | ||
757 | try{ writer.close(); } |
757 | try{ writer.close(); } |
758 | catch (Exception inner_ex) { } |
758 | catch (Exception inner_ex) { } |
759 | 759 | ||
760 | //#ifdef j2me |
760 | //#ifdef j2me |
761 | //# try{ connection.close(); } |
761 | //# try{ connection.close(); } |
762 | //# catch (Exception inner_ex) { } |
762 | //# catch (Exception inner_ex) { } |
763 | //#endif |
763 | //#endif |
764 | ufo_prober.set_to_none(); |
764 | ufo_prober.set_to_none(); |
765 | stats.reset(); |
765 | stats.reset(); |
766 | connected=false; |
766 | connected=false; |
767 | version=new MKVersion(); |
767 | version=new MKVersion(); |
768 | } |
768 | } |
769 | 769 | ||
770 | // Thread to recieve data from Connection |
770 | // Thread to recieve data from Connection |
771 | public void run() |
771 | public void run() |
772 | { |
772 | { |
773 | boolean sigfail=false; |
773 | boolean sigfail=false; |
774 | if (bootloader_stage==BOOTLOADER_STAGE_GOT_MKBL) |
774 | if (bootloader_stage==BOOTLOADER_STAGE_GOT_MKBL) |
775 | { |
775 | { |
776 | try { |
776 | try { |
777 | flash_msgs[msg_pos++]="reading avr_sig"; |
777 | flash_msgs[msg_pos++]="reading avr_sig"; |
778 | 778 | ||
779 | writer.write( 't'); |
779 | writer.write( 't'); |
780 | writer.flush(); |
780 | writer.flush(); |
781 | 781 | ||
782 | int avr_sig=reader.read(); |
782 | int avr_sig=reader.read(); |
783 | 783 | ||
784 | //while (avr_sig==63) |
784 | //while (avr_sig==63) |
785 | // avr_sig=reader.read(); |
785 | // avr_sig=reader.read(); |
786 | 786 | ||
787 | flash_msgs[msg_pos++]="got avr sig " + avr_sig; |
787 | flash_msgs[msg_pos++]="got avr sig " + avr_sig; |
788 | 788 | ||
789 | int avrsig_suff=reader.read(); |
789 | int avrsig_suff=reader.read(); |
790 | if (avrsig_suff!=0) |
790 | if (avrsig_suff!=0) |
791 | throw new Exception("val after avrsig is" +avrsig_suff +"should b 0"); |
791 | throw new Exception("val after avrsig is" +avrsig_suff +"should b 0"); |
792 | 792 | ||
793 | if ((avr_sig!=0x74)&&(avr_sig!=224)&&(avr_sig!=120)) |
793 | if ((avr_sig!=0x74)&&(avr_sig!=224)&&(avr_sig!=120)) |
794 | { |
794 | { |
795 | sigfail=true; |
795 | sigfail=true; |
796 | throw new Exception("avr sig" + avr_sig + " unknown"); |
796 | throw new Exception("avr sig" + avr_sig + " unknown"); |
797 | } |
797 | } |
798 | 798 | ||
799 | writer.write('T'); |
799 | writer.write('T'); |
800 | // writer.flush(); |
800 | // writer.flush(); |
801 | writer.write(avr_sig); // set devicetyp = 0x74 oder 0x76 |
801 | writer.write(avr_sig); // set devicetyp = 0x74 oder 0x76 |
802 | writer.flush(); |
802 | writer.flush(); |
803 | 803 | ||
804 | if (reader.read()!=0x0d) |
804 | if (reader.read()!=0x0d) |
805 | throw new Exception("cant get buffer size"); |
805 | throw new Exception("cant get buffer size"); |
806 | 806 | ||
807 | writer.write('V'); |
807 | writer.write('V'); |
808 | writer.flush(); |
808 | writer.flush(); |
809 | 809 | ||
810 | int bl_version_major=reader.read(); |
810 | int bl_version_major=reader.read(); |
811 | int bl_version_minor=reader.read(); |
811 | int bl_version_minor=reader.read(); |
812 | 812 | ||
813 | flash_msgs[msg_pos++]="BL Version " + bl_version_major+"."+bl_version_minor; |
813 | flash_msgs[msg_pos++]="BL Version " + bl_version_major+"."+bl_version_minor; |
814 | 814 | ||
815 | 815 | ||
816 | writer.write('b'); |
816 | writer.write('b'); |
817 | writer.flush(); |
817 | writer.flush(); |
818 | 818 | ||
819 | if (reader.read()!='Y') |
819 | if (reader.read()!='Y') |
820 | throw new Exception("cant get buffer size"); |
820 | throw new Exception("cant get buffer size"); |
821 | 821 | ||
822 | int send_buff_size1=reader.read(); |
822 | int send_buff_size1=reader.read(); |
823 | int send_buff_size2=reader.read(); |
823 | int send_buff_size2=reader.read(); |
824 | int send_buff_size=send_buff_size1*0x100+send_buff_size2; |
824 | int send_buff_size=send_buff_size1*0x100+send_buff_size2; |
825 | 825 | ||
826 | flash_msgs[msg_pos++]="BUFF Size:" + send_buff_size; |
826 | flash_msgs[msg_pos++]="BUFF Size:" + send_buff_size; |
827 | // if (send_buff_size>128) |
827 | // if (send_buff_size>128) |
828 | // send_buff_size=128; |
828 | // send_buff_size=128; |
829 | 829 | ||
830 | // if (!bootloader_intension |
830 | // if (!bootloader_intension |
831 | if (bootloader_intension_flash) |
831 | if (bootloader_intension_flash) |
832 | { |
832 | { |
833 | 833 | ||
834 | byte[] flash_buff =new byte[send_buff_size]; ///!! |
834 | byte[] flash_buff =new byte[send_buff_size]; ///!! |
835 | 835 | ||
836 | String firmware_filename=(avr_sig==224)?"/navi.bin":((avr_sig==120)?"/mk3.bin":"/fc.bin"); |
836 | String firmware_filename=(avr_sig==224)?"/navi.bin":((avr_sig==120)?"/mk3.bin":"/fc.bin"); |
837 | flash_msgs[msg_pos++]="Opening firmware " + firmware_filename + ".."; |
837 | flash_msgs[msg_pos++]="Opening firmware " + firmware_filename + ".."; |
838 | 838 | ||
839 | 839 | ||
840 | InputStream in; |
840 | InputStream in; |
841 | try { |
841 | try { |
842 | in=this.getClass().getResourceAsStream(firmware_filename); |
842 | in=this.getClass().getResourceAsStream(firmware_filename); |
843 | } |
843 | } |
844 | 844 | ||
845 | catch (Exception e) { throw new Exception(" .. cant open firmware"); } |
845 | catch (Exception e) { throw new Exception(" .. cant open firmware"); } |
846 | int firmware_size=-1; |
846 | int firmware_size=-1; |
847 | try { |
847 | try { |
848 | 848 | ||
849 | firmware_size= ((int)in.read()<<24) |((int)in.read()<<16) | ((int)in.read()<<8) | ((int)in.read()&0xff) ; |
849 | firmware_size= ((int)in.read()<<24) |((int)in.read()<<16) | ((int)in.read()<<8) | ((int)in.read()&0xff) ; |
850 | } |
850 | } |
851 | catch (Exception e) { throw new Exception(" .. cant read size"); } |
851 | catch (Exception e) { throw new Exception(" .. cant read size"); } |
852 | 852 | ||
853 | 853 | ||
854 | 854 | ||
855 | int blocks2write=((firmware_size/send_buff_size)); |
855 | int blocks2write=((firmware_size/send_buff_size)); |
856 | flash_msgs[msg_pos++]=".. open("+blocks2write+" blocks," + firmware_size + "bytes)"; |
856 | flash_msgs[msg_pos++]=".. open("+blocks2write+" blocks," + firmware_size + "bytes)"; |
857 | 857 | ||
858 | 858 | ||
859 | 859 | ||
860 | // if (true) throw new Exception("before erasing"); |
860 | // if (true) throw new Exception("before erasing"); |
861 | 861 | ||
862 | // if (true) throw new Exception("before erasing" ); |
862 | // if (true) throw new Exception("before erasing" ); |
863 | 863 | ||
864 | flash_msgs[msg_pos++]="Erasing Flash .."; |
864 | flash_msgs[msg_pos++]="Erasing Flash .."; |
865 | writer.write('e'); |
865 | writer.write('e'); |
866 | writer.flush(); |
866 | writer.flush(); |
867 | 867 | ||
868 | if (reader.read()!=0x0d) |
868 | if (reader.read()!=0x0d) |
869 | throw new Exception("cant erase flash"); |
869 | throw new Exception("cant erase flash"); |
870 | 870 | ||
871 | flash_msgs[msg_pos]+="OK"; |
871 | flash_msgs[msg_pos]+="OK"; |
872 | 872 | ||
873 | 873 | ||
874 | writer.write('A'); |
874 | writer.write('A'); |
875 | writer.write(0); |
875 | writer.write(0); |
876 | writer.write(0); |
876 | writer.write(0); |
877 | writer.flush(); |
877 | writer.flush(); |
878 | 878 | ||
879 | if (reader.read()!=0x0d) |
879 | if (reader.read()!=0x0d) |
880 | throw new Exception("cant set addr"); |
880 | throw new Exception("cant set addr"); |
881 | 881 | ||
882 | flash_msgs[msg_pos++]="addr set"; |
882 | flash_msgs[msg_pos++]="addr set"; |
883 | 883 | ||
884 | 884 | ||
885 | // int blocks2write=((firmware_size/send_buff_size)); |
885 | // int blocks2write=((firmware_size/send_buff_size)); |
886 | if ((firmware_size%send_buff_size)>0) |
886 | if ((firmware_size%send_buff_size)>0) |
887 | blocks2write++; |
887 | blocks2write++; |
888 | 888 | ||
889 | for ( int block=0; block<blocks2write; block ++) |
889 | for ( int block=0; block<blocks2write; block ++) |
890 | { |
890 | { |
891 | int hex_bytes_read=in.read(flash_buff,0,send_buff_size); |
891 | int hex_bytes_read=in.read(flash_buff,0,send_buff_size); |
892 | 892 | ||
893 | flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
893 | flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
894 | 894 | ||
895 | 895 | ||
896 | writer.write('B'); |
896 | writer.write('B'); |
897 | writer.write((hex_bytes_read>>8)& 0xFF); |
897 | writer.write((hex_bytes_read>>8)& 0xFF); |
898 | writer.write((hex_bytes_read)& 0xFF); |
898 | writer.write((hex_bytes_read)& 0xFF); |
899 | writer.write('F'); |
899 | writer.write('F'); |
900 | writer.flush(); |
900 | writer.flush(); |
901 | 901 | ||
902 | 902 | ||
903 | writer.write(flash_buff,0,hex_bytes_read); |
903 | writer.write(flash_buff,0,hex_bytes_read); |
904 | writer.flush(); |
904 | writer.flush(); |
905 | 905 | ||
906 | 906 | ||
907 | if (avr_sig==224) |
907 | if (avr_sig==224) |
908 | { |
908 | { |
909 | int crc=0xFFFF; |
909 | int crc=0xFFFF; |
910 | for (int crc_pos=0;crc_pos<hex_bytes_read;crc_pos++) |
910 | for (int crc_pos=0;crc_pos<hex_bytes_read;crc_pos++) |
911 | crc=CRC16(flash_buff[crc_pos],crc); |
911 | crc=CRC16(flash_buff[crc_pos],crc); |
912 | writer.write(crc>>8); |
912 | writer.write(crc>>8); |
913 | writer.write(crc&0xff); |
913 | writer.write(crc&0xff); |
914 | writer.flush(); |
914 | writer.flush(); |
915 | } |
915 | } |
916 | // flash_msgs[msg_pos]+="ok"; |
916 | // flash_msgs[msg_pos]+="ok"; |
917 | // writer.flush(); |
917 | // writer.flush(); |
918 | 918 | ||
919 | 919 | ||
920 | 920 | ||
921 | if (reader.read()!=0x0d) |
921 | if (reader.read()!=0x0d) |
922 | throw new Exception("abort write at block"+block); |
922 | throw new Exception("abort write at block"+block); |
923 | 923 | ||
924 | 924 | ||
925 | 925 | ||
926 | // sleep(1000); |
926 | // sleep(1000); |
927 | } |
927 | } |
928 | // flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
928 | // flash_msgs[msg_pos]="bl:" + block + "/" + blocks2write + " si:"+hex_bytes_read ; |
929 | /* |
929 | /* |
930 | |
930 | |
931 | int inp=0; |
931 | int inp=0; |
932 | int block=0; |
932 | int block=0; |
933 | while (inp!=-1) |
933 | while (inp!=-1) |
934 | { |
934 | { |
935 | int flash_buff_pos=0; |
935 | int flash_buff_pos=0; |
936 | int crc=0xFFFF; |
936 | int crc=0xFFFF; |
937 | |
937 | |
938 | while ((flash_buff_pos<send_buff_size)&&(inp!=-1)) |
938 | while ((flash_buff_pos<send_buff_size)&&(inp!=-1)) |
939 | { |
939 | { |
940 | inp=in.read(); |
940 | inp=in.read(); |
941 | if (inp!=-1) |
941 | if (inp!=-1) |
942 | { |
942 | { |
943 | crc=CRC16(inp,crc); |
943 | crc=CRC16(inp,crc); |
944 | flash_buff[flash_buff_pos++]=(byte)inp; |
944 | flash_buff[flash_buff_pos++]=(byte)inp; |
945 | } |
945 | } |
946 | } |
946 | } |
947 | // flash_msgs[msg_pos]="block" + block + "size:"+flash_buff_pos; |
947 | // flash_msgs[msg_pos]="block" + block + "size:"+flash_buff_pos; |
948 | |
948 | |
949 | block++; |
949 | block++; |
950 | |
950 | |
951 | boolean block_fin=false; |
951 | boolean block_fin=false; |
952 | 952 | ||
953 | 953 | ||
954 | while(!block_fin) |
954 | while(!block_fin) |
955 | { |
955 | { |
956 | |
956 | |
957 | writer.write('B'); |
957 | writer.write('B'); |
958 | writer.write((flash_buff_pos>>8)& 0xFF); |
958 | writer.write((flash_buff_pos>>8)& 0xFF); |
959 | writer.write((flash_buff_pos)& 0xFF); |
959 | writer.write((flash_buff_pos)& 0xFF); |
960 | writer.write('F'); |
960 | writer.write('F'); |
961 | writer.flush(); |
961 | writer.flush(); |
962 | 962 | ||
963 | // int ret_v=-1; |
963 | // int ret_v=-1; |
964 | |
964 | |
965 | writer.write(flash_buff,0,flash_buff_pos); |
965 | writer.write(flash_buff,0,flash_buff_pos); |
966 | flash_msgs[msg_pos]="bl:" + block + "si:"+flash_buff_pos ; |
966 | flash_msgs[msg_pos]="bl:" + block + "si:"+flash_buff_pos ; |
967 | |
967 | |
968 | writer.flush(); |
968 | writer.flush(); |
969 | // flash_msgs[msg_pos]+="wtc"; |
969 | // flash_msgs[msg_pos]+="wtc"; |
970 | |
970 | |
971 | |
971 | |
972 | // append crc if navi |
972 | // append crc if navi |
973 | if (avr_sig==224) |
973 | if (avr_sig==224) |
974 | { |
974 | { |
975 | writer.write(crc>>8); |
975 | writer.write(crc>>8); |
976 | writer.write(crc&0xff); |
976 | writer.write(crc&0xff); |
977 | writer.flush(); |
977 | writer.flush(); |
978 | } |
978 | } |
979 | // flash_msgs[msg_pos]+="ok"; |
979 | // flash_msgs[msg_pos]+="ok"; |
980 | // writer.flush(); |
980 | // writer.flush(); |
981 | // if (reader.read()!=0x0d) |
981 | // if (reader.read()!=0x0d) |
982 | // throw new Exception("abort write at block"+block); |
982 | // throw new Exception("abort write at block"+block); |
983 | |
983 | |
984 | |
984 | |
985 | //ret_v=reader.read(); |
985 | //ret_v=reader.read(); |
986 | // flash_msgs[msg_pos]="ret"+ret_v + "crc"+crc; |
986 | // flash_msgs[msg_pos]="ret"+ret_v + "crc"+crc; |
987 | |
987 | |
988 | if (reader.read()==0x0d) |
988 | if (reader.read()==0x0d) |
989 | block_fin=true; |
989 | block_fin=true; |
990 | |
990 | |
991 | } |
991 | } |
992 | 992 | ||
993 | } |
993 | } |
994 | */ |
994 | */ |
995 | flash_msgs[++msg_pos]="written last block "; |
995 | flash_msgs[++msg_pos]="written last block "; |
996 | msg_pos++; |
996 | msg_pos++; |
997 | flash_buff=null; |
997 | flash_buff=null; |
998 | 998 | ||
999 | ufo_prober.set_to_none(); |
999 | ufo_prober.set_to_none(); |
1000 | stats.reset(); |
1000 | stats.reset(); |
1001 | version=new MKVersion(); |
1001 | version=new MKVersion(); |
1002 | System.gc(); |
1002 | System.gc(); |
1003 | } |
1003 | } |
1004 | else // bootloader intension clear settings |
1004 | else // bootloader intension clear settings |
1005 | { |
1005 | { |
1006 | 1006 | ||
1007 | flash_msgs[msg_pos]="reset params .."; |
1007 | flash_msgs[msg_pos]="reset params .."; |
1008 | writer.write('B'); |
1008 | writer.write('B'); |
1009 | writer.write(0); |
1009 | writer.write(0); |
1010 | writer.write(4); |
1010 | writer.write(4); |
1011 | writer.write('E'); |
1011 | writer.write('E'); |
1012 | writer.flush(); |
1012 | writer.flush(); |
1013 | 1013 | ||
1014 | writer.write(0xFF); |
1014 | writer.write(0xFF); |
1015 | writer.write(0xFF); |
1015 | writer.write(0xFF); |
1016 | writer.write(0xFF); |
1016 | writer.write(0xFF); |
1017 | writer.write(0xFF); |
1017 | writer.write(0xFF); |
1018 | writer.flush(); |
1018 | writer.flush(); |
1019 | flash_msgs[msg_pos++]+=" done"; |
1019 | flash_msgs[msg_pos++]+=" done"; |
1020 | } |
1020 | } |
1021 | 1021 | ||
1022 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
1022 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
1023 | params=new MKParamsParser(); |
1023 | params=new MKParamsParser(); |
1024 | try{ |
1024 | try{ |
1025 | writer.write('E'); |
1025 | writer.write('E'); |
1026 | writer.flush(); |
1026 | writer.flush(); |
1027 | } |
1027 | } |
1028 | catch (Exception e) { |
1028 | catch (Exception e) { |
1029 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
1029 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
1030 | } |
1030 | } |
1031 | flash_msgs[msg_pos++]="Exit BL done" ; |
1031 | flash_msgs[msg_pos++]="Exit BL done" ; |
1032 | 1032 | ||
1033 | bootloader_finish_ok=true; |
1033 | bootloader_finish_ok=true; |
1034 | } |
1034 | } |
1035 | 1035 | ||
1036 | catch (Exception e) { |
1036 | catch (Exception e) { |
1037 | flash_msgs[msg_pos++]="Fail:" +e.getMessage() ; |
1037 | flash_msgs[msg_pos++]="Fail:" +e.getMessage() ; |
1038 | 1038 | ||
1039 | 1039 | ||
1040 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
1040 | flash_msgs[msg_pos++]="Exiting Bootloader" ; |
1041 | params=new MKParamsParser(); |
1041 | params=new MKParamsParser(); |
1042 | try{ |
1042 | try{ |
1043 | writer.write('E'); |
1043 | writer.write('E'); |
1044 | writer.flush(); |
1044 | writer.flush(); |
1045 | } |
1045 | } |
1046 | catch (Exception e2) { |
1046 | catch (Exception e2) { |
1047 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
1047 | flash_msgs[msg_pos++]="cant exit bootloader" ; |
1048 | } |
1048 | } |
1049 | flash_msgs[msg_pos++]="Exit BL done" ; |
1049 | flash_msgs[msg_pos++]="Exit BL done" ; |
1050 | if (sigfail&&(bl_retrys<3)) |
1050 | if (sigfail&&(bl_retrys<3)) |
1051 | { |
1051 | { |
1052 | bl_retrys++; |
1052 | bl_retrys++; |
1053 | init_bootloader=true; |
1053 | init_bootloader=true; |
1054 | } |
1054 | } |
1055 | close_connections(false); |
1055 | close_connections(false); |
1056 | } |
1056 | } |
1057 | 1057 | ||
1058 | 1058 | ||
1059 | sending=false; |
1059 | sending=false; |
1060 | } |
1060 | } |
1061 | 1061 | ||
1062 | 1062 | ||
1063 | byte[] data_set=new byte[1024]; |
1063 | byte[] data_set=new byte[1024]; |
1064 | int data_set_pos=0; |
1064 | int data_set_pos=0; |
1065 | byte[] data_in_buff=new byte[2048]; |
1065 | byte[] data_in_buff=new byte[2048]; |
1066 | 1066 | ||
1067 | int input; |
1067 | int input; |
1068 | int pos=0; |
1068 | int pos=0; |
1069 | 1069 | ||
1070 | 1070 | ||
1071 | 1071 | ||
1072 | 1072 | ||
1073 | 1073 | ||
1074 | log("Thread started"); |
1074 | log("Thread started"); |
1075 | while(true) |
1075 | while(true) |
1076 | { |
1076 | { |
1077 | 1077 | ||
1078 | if (!connected) |
1078 | if (!connected) |
1079 | { |
1079 | { |
1080 | if (!force_disconnect) connect(); |
1080 | if (!force_disconnect) connect(); |
1081 | sleep(100); |
1081 | sleep(100); |
1082 | } |
1082 | } |
1083 | else |
1083 | else |
1084 | try{ |
1084 | try{ |
1085 | 1085 | ||
1086 | /* |
1086 | /* |
1087 | while(sending) |
1087 | while(sending) |
1088 | {try { Thread.sleep(50); } |
1088 | {try { Thread.sleep(50); } |
1089 | catch (Exception e) { } |
1089 | catch (Exception e) { } |
1090 | } |
1090 | } |
1091 | */ |
1091 | */ |
1092 | 1092 | ||
1093 | recieving=true; |
1093 | recieving=true; |
1094 | int read_count =reader.read(data_in_buff,0,reader.available()); |
1094 | int read_count =reader.read(data_in_buff,0,reader.available()); |
1095 | log("Connected - reading data " + read_count); |
1095 | log("Connected - reading data " + read_count); |
1096 | // pos=0; |
1096 | // pos=0; |
1097 | input=0; |
1097 | input=0; |
1098 | //data_buff[data_buff_pos]=""; |
1098 | //data_buff[data_buff_pos]=""; |
1099 | // recieve data-set |
1099 | // recieve data-set |
1100 | if (read_count==0) sleep(50); |
1100 | if (read_count==0) sleep(50); |
1101 | 1101 | ||
1102 | // int read_count =reader.read(data_in_buff,0,reader.available()); |
1102 | // int read_count =reader.read(data_in_buff,0,reader.available()); |
1103 | stats.bytes_in+=read_count; |
1103 | stats.bytes_in+=read_count; |
1104 | if (read_count>0) |
1104 | if (read_count>0) |
1105 | { |
1105 | { |
1106 | log("read" + read_count + " ds_pos" + data_set_pos); |
1106 | log("read" + read_count + " ds_pos" + data_set_pos); |
1107 | 1107 | ||
1108 | for ( pos=0;pos<read_count;pos++) |
1108 | for ( pos=0;pos<read_count;pos++) |
1109 | { |
1109 | { |
1110 | if (data_in_buff[pos]==13) |
1110 | if (data_in_buff[pos]==13) |
1111 | { |
1111 | { |
1112 | data_buff[data_buff_pos]=new String(data_set, 0, data_set_pos); |
1112 | data_buff[data_buff_pos]=new String(data_set, 0, data_set_pos); |
1113 | data_buff_pos++; |
1113 | data_buff_pos++; |
1114 | data_buff_pos%=DATA_BUFF_LEN; |
1114 | data_buff_pos%=DATA_BUFF_LEN; |
1115 | 1115 | ||
1116 | 1116 | ||
1117 | try{process_data(data_set,data_set_pos); } |
1117 | try{process_data(data_set,data_set_pos); } |
1118 | catch (Exception e) |
1118 | catch (Exception e) |
1119 | { |
1119 | { |
1120 | log(".. problem processing"); |
1120 | log(".. problem processing"); |
1121 | log(e.toString()); |
1121 | log(e.toString()); |
1122 | } |
1122 | } |
1123 | 1123 | ||
1124 | 1124 | ||
1125 | 1125 | ||
1126 | 1126 | ||
1127 | proxy.write(data_set,0,data_set_pos); |
1127 | proxy.write(data_set,0,data_set_pos); |
1128 | // proxy.writer.write('\r'); |
1128 | // proxy.writer.write('\r'); |
1129 | //proxy.writer.write('\n'); |
1129 | //proxy.writer.write('\n'); |
1130 | //proxy.writer.flush(); |
1130 | //proxy.writer.flush(); |
1131 | /* |
1131 | /* |
1132 | if (proxy!=null) |
1132 | if (proxy!=null) |
1133 | { |
1133 | { |
1134 | |
1134 | |
1135 | 1135 | ||
1136 | 1136 | ||
1137 | } |
1137 | } |
1138 | */ |
1138 | */ |
1139 | data_set_pos=0; |
1139 | data_set_pos=0; |
1140 | 1140 | ||
1141 | } |
1141 | } |
1142 | else |
1142 | else |
1143 | { |
1143 | { |
1144 | data_set[data_set_pos++]=data_in_buff[pos]; |
1144 | data_set[data_set_pos++]=data_in_buff[pos]; |
1145 | 1145 | ||
1146 | 1146 | ||
1147 | 1147 | ||
1148 | if ( (data_set_pos>4) && (data_set[data_set_pos-4]==(byte)'M') && (data_set[data_set_pos-3]==(byte)'K') && (data_set[data_set_pos-2]==(byte)'B') && (data_set[data_set_pos-1]==(byte)'L')) |
1148 | if ( (data_set_pos>4) && (data_set[data_set_pos-4]==(byte)'M') && (data_set[data_set_pos-3]==(byte)'K') && (data_set[data_set_pos-2]==(byte)'B') && (data_set[data_set_pos-1]==(byte)'L')) |
1149 | { |
1149 | { |
1150 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
1150 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
1151 | return; |
1151 | return; |
1152 | } |
1152 | } |
1153 | 1153 | ||
1154 | } |
1154 | } |
1155 | 1155 | ||
1156 | } |
1156 | } |
1157 | 1157 | ||
1158 | 1158 | ||
1159 | } |
1159 | } |
1160 | else |
1160 | else |
1161 | sleep(50); |
1161 | sleep(50); |
1162 | /* |
1162 | /* |
1163 | while ((input != 13)) //&&(input!=-1)) |
1163 | while ((input != 13)) //&&(input!=-1)) |
1164 | { |
1164 | { |
1165 | { |
1165 | { |
1166 | //log("pre read"); |
1166 | //log("pre read"); |
1167 | log(""+reader.available()); |
1167 | log(""+reader.available()); |
1168 | input = reader.read() ; |
1168 | input = reader.read() ; |
1169 | log("Byte rcv" + input +"pos"+ pos); |
1169 | log("Byte rcv" + input +"pos"+ pos); |
1170 | |
1170 | |
1171 | proxy.write(input); |
1171 | proxy.write(input); |
1172 | |
1172 | |
1173 | data_buff[data_buff_pos]+=(char)input; |
1173 | data_buff[data_buff_pos]+=(char)input; |
1174 | |
1174 | |
1175 | if ((data_buff[data_buff_pos].length()>3)&&(data_buff[data_buff_pos].substring(data_buff[data_buff_pos].length()-4,data_buff[data_buff_pos].length()).equals("MKBL"))) |
1175 | if ((data_buff[data_buff_pos].length()>3)&&(data_buff[data_buff_pos].substring(data_buff[data_buff_pos].length()-4,data_buff[data_buff_pos].length()).equals("MKBL"))) |
1176 | { |
1176 | { |
1177 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
1177 | bootloader_stage= BOOTLOADER_STAGE_GOT_MKBL; |
1178 | return; |
1178 | return; |
1179 | } |
1179 | } |
1180 | if (input==-1) throw new Exception("disconnect"); |
1180 | if (input==-1) throw new Exception("disconnect"); |
1181 | else |
1181 | else |
1182 | { |
1182 | { |
1183 | stats.bytes_in++; |
1183 | stats.bytes_in++; |
1184 | data_set[pos]=input; |
1184 | data_set[pos]=input; |
1185 | pos++; |
1185 | pos++; |
1186 | } |
1186 | } |
1187 | } |
1187 | } |
1188 | |
1188 | |
1189 | } |
1189 | } |
1190 | |
1190 | |
1191 | |
1191 | |
1192 | 1192 | ||
1193 | data_buff_pos++; |
1193 | data_buff_pos++; |
1194 | data_buff_pos%=DATA_BUFF_LEN; |
1194 | data_buff_pos%=DATA_BUFF_LEN; |
1195 | recieving=false; |
1195 | recieving=false; |
1196 | log("Data recieved (" + pos + "Bytes)"); |
1196 | log("Data recieved (" + pos + "Bytes)"); |
1197 | log("processing .."); |
1197 | log("processing .."); |
1198 | */ |
1198 | */ |
1199 | 1199 | ||
1200 | /* |
1200 | /* |
1201 | if (proxy!=null) |
1201 | if (proxy!=null) |
1202 | { |
1202 | { |
1203 | proxy.writer.write('\r'); |
1203 | proxy.writer.write('\r'); |
1204 | proxy.writer.write('\n'); |
1204 | proxy.writer.write('\n'); |
1205 | proxy.writer.flush(); |
1205 | proxy.writer.flush(); |
1206 | } |
1206 | } |
1207 | */ |
1207 | */ |
1208 | /*if (pos>5) |
1208 | /*if (pos>5) |
1209 | { |
1209 | { |
1210 | try{process_data(data_set,pos); } |
1210 | try{process_data(data_set,pos); } |
1211 | catch (Exception e) |
1211 | catch (Exception e) |
1212 | { |
1212 | { |
1213 | log(".. problem processing"); |
1213 | log(".. problem processing"); |
1214 | log(e.toString()); |
1214 | log(e.toString()); |
1215 | } |
1215 | } |
1216 | |
1216 | |
1217 | log(".. processing done"); |
1217 | log(".. processing done"); |
1218 | } |
1218 | } |
1219 | */ |
1219 | */ |
1220 | } |
1220 | } |
1221 | catch (Exception ex) |
1221 | catch (Exception ex) |
1222 | { |
1222 | { |
1223 | log("Problem reading from MK -> closing conn"); |
1223 | log("Problem reading from MK -> closing conn"); |
1224 | log(ex.toString()); |
1224 | log(ex.toString()); |
1225 | // close the connection |
1225 | // close the connection |
1226 | close_connections(false); |
1226 | close_connections(false); |
1227 | } |
1227 | } |
1228 | 1228 | ||
1229 | // sleep a bit to get someting more done |
1229 | // sleep a bit to get someting more done |
1230 | // sleep(5); //50 |
1230 | // sleep(5); //50 |
1231 | 1231 | ||
1232 | } // while |
1232 | } // while |
1233 | // log("Leaving Communicator thread"); |
1233 | // log("Leaving Communicator thread"); |
1234 | } // run() |
1234 | } // run() |
1235 | } |
1235 | } |
1236 | 1236 |