altosuilib: Split cc1111 vs arm telefire/telemini for flashing
[fw/altos] / altosuilib / AltosFlashUI.java
1 /*
2  * Copyright © 2010 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 package org.altusmetrum.altosuilib_12;
20
21 import java.awt.*;
22 import java.awt.event.*;
23 import javax.swing.*;
24 import javax.swing.filechooser.FileNameExtensionFilter;
25 import java.io.*;
26 import java.util.concurrent.*;
27 import org.altusmetrum.altoslib_12.*;
28
29 public class AltosFlashUI
30         extends AltosUIDialog
31         implements ActionListener
32 {
33         Container       pane;
34         Box             box;
35         JLabel          serial_label;
36         JLabel          serial_value;
37         JLabel          file_label;
38         JLabel          file_value;
39         JProgressBar    pbar;
40         JButton         cancel;
41
42         AltosUIFrame    frame;
43
44         // Hex file with rom image
45         File            file;
46
47         // Debug connection
48         AltosDevice     device;
49
50         AltosLink       link;
51
52         // Desired Rom configuration
53         AltosRomconfig  rom_config;
54
55         // Flash controller
56         AltosProgrammer programmer;
57
58         private static final String[] pair_programmed_files = {
59                 "teleballoon",
60                 "telebt-v1",
61                 "teledongle-v0",
62                 "telefire-v0",
63                 "telemetrum-v0",
64                 "telemetrum-v1",
65                 "telemini-v1",
66                 "telenano",
67                 "teleshield",
68                 "teleterra"
69         };
70
71         private static final String[] pair_programmed_devices = {
72                 "TeleBalloon",
73                 "TeleBT-v1",
74                 "TeleDongle-v0",
75                 "TeleFire-v0",
76                 "TeleFire",
77                 "TeleMetrum-v0",
78                 "TeleMetrum-v1",
79                 "TeleMini-v1",
80                 "TeleNano",
81                 "TeleShield",
82                 "TeleTerra"
83         };
84
85         private boolean is_pair_programmed() {
86
87                 if (file != null) {
88                         String  name = file.getName();
89                         for (int i = 0; i < pair_programmed_files.length; i++) {
90                                 if (name.startsWith(pair_programmed_files[i]))
91                                         return true;
92                         }
93                 }
94                 if (device != null) {
95                         String  name = device.toString();
96                         for (int i = 0; i < pair_programmed_devices.length; i++) {
97                                 if (name.startsWith(pair_programmed_devices[i]))
98                                         return true;
99                         }
100                 }
101                 return false;
102         }
103
104         public void actionPerformed(ActionEvent e) {
105                 if (e.getSource() == cancel) {
106                         if (programmer != null)
107                                 programmer.abort();
108                         setVisible(false);
109                         dispose();
110                 } else {
111                         String  cmd = e.getActionCommand();
112                         if (e.getID() == -1) {
113                                 JOptionPane.showMessageDialog(frame,
114                                                               e.getActionCommand(),
115                                                               file.toString(),
116                                                               JOptionPane.ERROR_MESSAGE);
117                                 setVisible(false);
118                                 dispose();
119                         } else if (cmd.equals("done")) {
120                                 setVisible(false);
121                                 dispose();
122                         } else if (cmd.equals("start")) {
123                                 setVisible(true);
124                         } else {
125                                 pbar.setValue(e.getID());
126                                 pbar.setString(cmd);
127                         }
128                 }
129         }
130
131         public void build_dialog() {
132                 GridBagConstraints c;
133                 Insets il = new Insets(4,4,4,4);
134                 Insets ir = new Insets(4,4,4,4);
135
136                 pane = getContentPane();
137                 pane.setLayout(new GridBagLayout());
138
139                 c = new GridBagConstraints();
140                 c.gridx = 0; c.gridy = 0;
141                 c.fill = GridBagConstraints.NONE;
142                 c.anchor = GridBagConstraints.LINE_START;
143                 c.insets = il;
144                 serial_label = new JLabel("Serial:");
145                 pane.add(serial_label, c);
146
147                 c = new GridBagConstraints();
148                 c.gridx = 1; c.gridy = 0;
149                 c.fill = GridBagConstraints.HORIZONTAL;
150                 c.weightx = 1;
151                 c.anchor = GridBagConstraints.LINE_START;
152                 c.insets = ir;
153                 serial_value = new JLabel("");
154                 pane.add(serial_value, c);
155
156                 c = new GridBagConstraints();
157                 c.fill = GridBagConstraints.NONE;
158                 c.gridx = 0; c.gridy = 1;
159                 c.anchor = GridBagConstraints.LINE_START;
160                 c.insets = il;
161                 file_label = new JLabel("File:");
162                 pane.add(file_label, c);
163
164                 c = new GridBagConstraints();
165                 c.fill = GridBagConstraints.HORIZONTAL;
166                 c.weightx = 1;
167                 c.gridx = 1; c.gridy = 1;
168                 c.anchor = GridBagConstraints.LINE_START;
169                 c.insets = ir;
170                 file_value = new JLabel(file.toString());
171                 pane.add(file_value, c);
172
173                 pbar = new JProgressBar();
174                 pbar.setMinimum(0);
175                 pbar.setMaximum(100);
176                 pbar.setValue(0);
177                 pbar.setString("");
178                 pbar.setStringPainted(true);
179                 pbar.setPreferredSize(new Dimension(600, 20));
180                 c = new GridBagConstraints();
181                 c.fill = GridBagConstraints.HORIZONTAL;
182                 c.anchor = GridBagConstraints.CENTER;
183                 c.gridx = 0; c.gridy = 2;
184                 c.gridwidth = GridBagConstraints.REMAINDER;
185                 Insets ib = new Insets(4,4,4,4);
186                 c.insets = ib;
187                 pane.add(pbar, c);
188
189                 cancel = new JButton("Cancel");
190                 c = new GridBagConstraints();
191                 c.fill = GridBagConstraints.NONE;
192                 c.anchor = GridBagConstraints.CENTER;
193                 c.gridx = 0; c.gridy = 3;
194                 c.gridwidth = GridBagConstraints.REMAINDER;
195                 Insets ic = new Insets(4,4,4,4);
196                 c.insets = ic;
197                 pane.add(cancel, c);
198                 cancel.addActionListener(this);
199                 pack();
200                 setLocationRelativeTo(frame);
201         }
202
203         void set_serial(int serial_number) {
204                 serial_value.setText(String.format("%d", serial_number));
205         }
206
207         static class AltosHexfileFilter extends javax.swing.filechooser.FileFilter {
208                 int product;
209                 String head;
210                 String description;
211
212                 public AltosHexfileFilter(int product, String head, String description) {
213                         this.product = product;
214                         this.head = head;
215                         this.description = description;
216                 }
217
218                 public boolean accept(File file) {
219                         return !file.isFile() || (file.getName().startsWith(head) && file.getName().endsWith(".ihx"));
220                 }
221
222                 public String getDescription() {
223                         return description;
224                 }
225         }
226
227         static AltosHexfileFilter[] filters = {
228                 new AltosHexfileFilter(AltosLib.product_telemetrum, "telemetrum", "TeleMetrum Image"),
229                 new AltosHexfileFilter(AltosLib.product_teledongle, "teledongle", "TeleDongle Image"),
230                 new AltosHexfileFilter(AltosLib.product_telemega, "telemega", "TeleMega Image"),
231                 new AltosHexfileFilter(AltosLib.product_easymini, "easymini", "EasyMini Image"),
232                 new AltosHexfileFilter(AltosLib.product_easymega, "easymega", "EasyMega Image"),
233         };
234
235         boolean select_source_file() {
236                 JFileChooser    hexfile_chooser = new JFileChooser();
237
238                 File firmwaredir = AltosUIPreferences.firmwaredir();
239                 if (firmwaredir != null)
240                         hexfile_chooser.setCurrentDirectory(firmwaredir);
241
242                 hexfile_chooser.setDialogTitle("Select Flash Image");
243
244                 for (int i = 0; i < filters.length; i++) {
245                         hexfile_chooser.addChoosableFileFilter(filters[i]);
246                 }
247                 javax.swing.filechooser.FileFilter ihx_filter = new FileNameExtensionFilter("Flash Image", "ihx");
248                 hexfile_chooser.addChoosableFileFilter(ihx_filter);
249                 hexfile_chooser.setFileFilter(ihx_filter);
250
251                 if (!is_pair_programmed() && !device.matchProduct(AltosLib.product_altusmetrum)) {
252                         for (int i = 0; i < filters.length; i++) {
253                                 if (device != null && device.matchProduct(filters[i].product))
254                                         hexfile_chooser.setFileFilter(filters[i]);
255                         }
256                 }
257
258                 int returnVal = hexfile_chooser.showOpenDialog(frame);
259
260                 if (returnVal != JFileChooser.APPROVE_OPTION)
261                         return false;
262                 file = hexfile_chooser.getSelectedFile();
263                 if (file == null)
264                         return false;
265                 AltosUIPreferences.set_firmwaredir(file.getParentFile());
266
267                 return true;
268         }
269
270         boolean select_device() {
271                 int     product = AltosLib.product_any;
272
273                 device = AltosDeviceUIDialog.show(frame, AltosLib.product_any);
274
275                 if (device == null)
276                         return false;
277                 return true;
278         }
279
280         boolean rom_config_matches (AltosRomconfig a, AltosRomconfig b) {
281                 if (a.usb_id != null && b.usb_id != null &&
282                     (a.usb_id.vid != b.usb_id.vid ||
283                      a.usb_id.pid != b.usb_id.pid))
284                         return false;
285
286                 if (a.usb_product != null && b.usb_product != null &&
287                     !a.usb_product.equals(b.usb_product))
288                         return false;
289
290                 return true;
291         }
292
293         boolean update_rom_config_info(AltosRomconfig existing_config, AltosRomconfig image_config) {
294                 AltosRomconfig  new_config;
295
296                 if (!rom_config_matches(existing_config, image_config)) {
297                         int ret = JOptionPane.showConfirmDialog(this,
298                                                                 String.format("Device is %04x:%04x %s\nImage is %04x:%04x %s\nFlash anyways?",
299                                                                               existing_config.usb_id.vid,
300                                                                               existing_config.usb_id.pid,
301                                                                               existing_config.usb_product,
302                                                                               image_config.usb_id.vid,
303                                                                               image_config.usb_id.pid,
304                                                                               image_config.usb_product),
305                                                                 "Image doesn't match Device",
306                                                                 JOptionPane.YES_NO_OPTION);
307                         if (ret != JOptionPane.YES_OPTION)
308                                 return false;
309                 }
310
311                 new_config = AltosRomconfigUI.show(frame, existing_config);
312                 if (new_config == null)
313                         return false;
314                 rom_config = new_config;
315                 set_serial(rom_config.serial_number);
316                 setVisible(true);
317                 return true;
318         }
319
320         void exception (Exception e) {
321                 if (e instanceof FileNotFoundException) {
322                         JOptionPane.showMessageDialog(frame,
323                                                       ((FileNotFoundException) e).getMessage(),
324                                                       "Cannot open file",
325                                                       JOptionPane.ERROR_MESSAGE);
326                 } else if (e instanceof AltosSerialInUseException) {
327                         JOptionPane.showMessageDialog(frame,
328                                                       String.format("Device \"%s\" already in use",
329                                                                     device.toShortString()),
330                                                       "Device in use",
331                                                       JOptionPane.ERROR_MESSAGE);
332                 } else if (e instanceof IOException) {
333                         JOptionPane.showMessageDialog(frame,
334                                                       e.getMessage(),
335                                                       file.toString(),
336                                                       JOptionPane.ERROR_MESSAGE);
337                 }
338         }
339
340         class flash_task implements Runnable, AltosFlashListener {
341                 AltosFlashUI    ui;
342                 Thread          t;
343                 AltosProgrammer programmer;
344
345                 public void position(String in_s, int in_percent) {
346                         final String s = in_s;
347                         final int percent = in_percent;
348                         Runnable r = new Runnable() {
349                                         public void run() {
350                                                 try {
351                                                         ui.actionPerformed(new ActionEvent(this,
352                                                                                            percent,
353                                                                                            s));
354                                                 } catch (Exception ex) {
355                                                 }
356                                         }
357                                 };
358                         SwingUtilities.invokeLater(r);
359                 }
360
361                 public void run () {
362                         try {
363                                 if (ui.is_pair_programmed())
364                                         programmer = new AltosFlash(ui.file, link, this);
365                                 else
366                                         programmer = new AltosSelfFlash(ui.file, link, this);
367
368                                 final AltosRomconfig    current_config = programmer.target_romconfig();
369
370                                 final AltosRomconfig    image_config = programmer.image_romconfig();
371
372                                 final Semaphore await_rom_config = new Semaphore(0);
373                                 SwingUtilities.invokeLater(new Runnable() {
374                                                 public void run() {
375                                                         ui.programmer = programmer;
376                                                         ui.update_rom_config_info(current_config, image_config);
377                                                         await_rom_config.release();
378                                                 }
379                                         });
380                                 await_rom_config.acquire();
381
382                                 if (ui.rom_config != null) {
383                                         programmer.set_romconfig(ui.rom_config);
384                                         programmer.flash();
385                                 }
386                         } catch (InterruptedException ee) {
387                                 final Exception e = ee;
388                                 SwingUtilities.invokeLater(new Runnable() {
389                                                 public void run() {
390                                                         ui.exception(e);
391                                                 }
392                                         });
393                         } catch (IOException ee) {
394                                 final Exception e = ee;
395                                 SwingUtilities.invokeLater(new Runnable() {
396                                                 public void run() {
397                                                         ui.exception(e);
398                                                 }
399                                         });
400                         } finally {
401                                 if (programmer != null)
402                                         programmer.close();
403                         }
404                 }
405
406                 public flash_task(AltosFlashUI in_ui) {
407                         ui = in_ui;
408                         t = new Thread(this);
409                         t.start();
410                 }
411         }
412
413         flash_task      flasher;
414
415         private boolean open_device() throws InterruptedException {
416                 try {
417                         link = new AltosSerial(device);
418                         if (is_pair_programmed())
419                                 return true;
420
421                         if (link == null)
422                                 throw new IOException(String.format("%s: open failed", device.toShortString()));
423
424                         while (!link.is_loader()) {
425                                 link.to_loader();
426
427                                 java.util.List<AltosDevice> devices = null;
428
429                                 for (int tries = 0; tries < 10; tries++) {
430                                         Thread.sleep(100);
431                                         devices = AltosUSBDevice.list(AltosLib.product_altusmetrum);
432                                         if (devices.size() != 0)
433                                                 break;
434                                 }
435
436                                 if (devices.size() == 1)
437                                         device = devices.get(0);
438                                 else {
439                                         device = AltosDeviceUIDialog.show(frame, AltosLib.product_altusmetrum);
440                                         if (device == null)
441                                                 return false;
442                                 }
443                                 link = new AltosSerial(device);
444                         }
445                         return true;
446                 } catch (AltosSerialInUseException ee) {
447                         exception(ee);
448                 } catch (FileNotFoundException fe) {
449                         exception(fe);
450                 } catch (IOException ie) {
451                         exception (ie);
452                 }
453                 return false;
454         }
455
456         /*
457          * Execute the steps for flashing
458          * a device. Note that this returns immediately;
459          * this dialog is not modal
460          */
461         void showDialog() {
462                 if (!select_device())
463                         return;
464                 if (!select_source_file())
465                         return;
466                 try {
467                         if (!open_device())
468                                 return;
469                 } catch (InterruptedException ie) {
470                         return;
471                 }
472                 build_dialog();
473                 flash_task      f = new flash_task(this);
474         }
475
476         public static void show(AltosUIFrame frame) {
477                 AltosFlashUI    ui = new AltosFlashUI(frame);
478                 ui.showDialog();
479         }
480
481         public AltosFlashUI(AltosUIFrame in_frame) {
482                 super(in_frame, "Program Altusmetrum Device", false);
483
484                 frame = in_frame;
485         }
486 }