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