altoslib: Store saved state in version-independent format
[fw/altos] / altosdroid / src / org / altusmetrum / AltosDroid / TelemetryService.java
1 /*
2  * Copyright © 2012 Mike Beattie <mike@ethernal.org>
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; version 2 of the License.
7  *
8  * This program is distributed in the hope that it will be useful, but
9  * WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License along
14  * with this program; if not, write to the Free Software Foundation, Inc.,
15  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
16  */
17
18 package org.altusmetrum.AltosDroid;
19
20 import java.lang.ref.WeakReference;
21 import java.util.concurrent.TimeoutException;
22 import java.util.*;
23
24 import android.app.Notification;
25 //import android.app.NotificationManager;
26 import android.app.PendingIntent;
27 import android.app.Service;
28 import android.bluetooth.BluetoothDevice;
29 import android.bluetooth.BluetoothAdapter;
30 import android.hardware.usb.*;
31 import android.content.Intent;
32 import android.content.Context;
33 import android.os.Bundle;
34 import android.os.IBinder;
35 import android.os.Handler;
36 import android.os.Message;
37 import android.os.Messenger;
38 import android.os.RemoteException;
39 import android.os.Looper;
40 import android.widget.Toast;
41 import android.location.Criteria;
42
43 import org.altusmetrum.altoslib_11.*;
44
45 public class TelemetryService extends Service implements AltosIdleMonitorListener {
46
47         static final int MSG_REGISTER_CLIENT   = 1;
48         static final int MSG_UNREGISTER_CLIENT = 2;
49         static final int MSG_CONNECT           = 3;
50         static final int MSG_OPEN_USB          = 4;
51         static final int MSG_CONNECTED         = 5;
52         static final int MSG_CONNECT_FAILED    = 6;
53         static final int MSG_DISCONNECTED      = 7;
54         static final int MSG_TELEMETRY         = 8;
55         static final int MSG_SETFREQUENCY      = 9;
56         static final int MSG_CRC_ERROR         = 10;
57         static final int MSG_SETBAUD           = 11;
58         static final int MSG_DISCONNECT        = 12;
59         static final int MSG_DELETE_SERIAL     = 13;
60         static final int MSG_BLUETOOTH_ENABLED = 14;
61         static final int MSG_MONITOR_IDLE_START= 15;
62         static final int MSG_MONITOR_IDLE_STOP = 16;
63         static final int MSG_REBOOT            = 17;
64         static final int MSG_IGNITER_QUERY     = 18;
65         static final int MSG_IGNITER_FIRE      = 19;
66
67         // Unique Identification Number for the Notification.
68         // We use it on Notification start, and to cancel it.
69         private int NOTIFICATION = R.string.telemetry_service_label;
70         //private NotificationManager mNM;
71
72         ArrayList<Messenger> clients = new ArrayList<Messenger>(); // Keeps track of all current registered clients.
73         final Handler   handler   = new IncomingHandler(this);
74         final Messenger messenger = new Messenger(handler); // Target we publish for clients to send messages to IncomingHandler.
75
76         // Name of the connected device
77         DeviceAddress address;
78         private AltosDroidLink  altos_link  = null;
79         private TelemetryReader telemetry_reader = null;
80         private TelemetryLogger telemetry_logger = null;
81
82         // Local Bluetooth adapter
83         private BluetoothAdapter bluetooth_adapter = null;
84
85         // Last data seen; send to UI when it starts
86         private TelemetryState  telemetry_state;
87
88         // Idle monitor if active
89         AltosIdleMonitor idle_monitor = null;
90
91         // Igniter bits
92         AltosIgnite ignite = null;
93         boolean ignite_running;
94
95         // Handler of incoming messages from clients.
96         static class IncomingHandler extends Handler {
97                 private final WeakReference<TelemetryService> service;
98                 IncomingHandler(TelemetryService s) { service = new WeakReference<TelemetryService>(s); }
99
100                 @Override
101                 public void handleMessage(Message msg) {
102                         DeviceAddress address;
103
104                         TelemetryService s = service.get();
105                         AltosDroidLink bt = null;
106                         if (s == null)
107                                 return;
108
109                         switch (msg.what) {
110
111                                 /* Messages from application */
112                         case MSG_REGISTER_CLIENT:
113                                 s.add_client(msg.replyTo);
114                                 break;
115                         case MSG_UNREGISTER_CLIENT:
116                                 s.remove_client(msg.replyTo);
117                                 break;
118                         case MSG_CONNECT:
119                                 AltosDebug.debug("Connect command received");
120                                 address = (DeviceAddress) msg.obj;
121                                 AltosDroidPreferences.set_active_device(address);
122                                 s.start_altos_bluetooth(address, false);
123                                 break;
124                         case MSG_OPEN_USB:
125                                 AltosDebug.debug("Open USB command received");
126                                 UsbDevice device = (UsbDevice) msg.obj;
127                                 s.start_usb(device);
128                                 break;
129                         case MSG_DISCONNECT:
130                                 AltosDebug.debug("Disconnect command received");
131                                 s.address = null;
132                                 if (!(Boolean) msg.obj)
133                                         AltosDroidPreferences.set_active_device(null);
134                                 s.disconnect(true);
135                                 break;
136                         case MSG_DELETE_SERIAL:
137                                 AltosDebug.debug("Delete Serial command received");
138                                 s.delete_serial((Integer) msg.obj);
139                                 break;
140                         case MSG_SETFREQUENCY:
141                                 AltosDebug.debug("MSG_SETFREQUENCY");
142                                 s.telemetry_state.frequency = (Double) msg.obj;
143                                 if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
144                                         try {
145                                                 s.altos_link.set_radio_frequency(s.telemetry_state.frequency);
146                                                 s.altos_link.save_frequency();
147                                         } catch (InterruptedException e) {
148                                         } catch (TimeoutException e) {
149                                         }
150                                 }
151                                 s.send_to_clients();
152                                 break;
153                         case MSG_SETBAUD:
154                                 AltosDebug.debug("MSG_SETBAUD");
155                                 s.telemetry_state.telemetry_rate = (Integer) msg.obj;
156                                 if (s.telemetry_state.connect == TelemetryState.CONNECT_CONNECTED) {
157                                         s.altos_link.set_telemetry_rate(s.telemetry_state.telemetry_rate);
158                                         s.altos_link.save_telemetry_rate();
159                                 }
160                                 s.send_to_clients();
161                                 break;
162
163                                 /*
164                                  *Messages from AltosBluetooth
165                                  */
166                         case MSG_CONNECTED:
167                                 AltosDebug.debug("MSG_CONNECTED");
168                                 bt = (AltosDroidLink) msg.obj;
169
170                                 if (bt != s.altos_link) {
171                                         AltosDebug.debug("Stale message");
172                                         break;
173                                 }
174                                 AltosDebug.debug("Connected to device");
175                                 try {
176                                         s.connected();
177                                 } catch (InterruptedException ie) {
178                                 }
179                                 break;
180                         case MSG_CONNECT_FAILED:
181                                 AltosDebug.debug("MSG_CONNECT_FAILED");
182                                 bt = (AltosDroidLink) msg.obj;
183
184                                 if (bt != s.altos_link) {
185                                         AltosDebug.debug("Stale message");
186                                         break;
187                                 }
188                                 if (s.address != null) {
189                                         AltosDebug.debug("Connection failed... retrying");
190                                         s.start_altos_bluetooth(s.address, true);
191                                 } else {
192                                         s.disconnect(true);
193                                 }
194                                 break;
195                         case MSG_DISCONNECTED:
196
197                                 /* This can be sent by either AltosDroidLink or TelemetryReader */
198                                 AltosDebug.debug("MSG_DISCONNECTED");
199                                 bt = (AltosDroidLink) msg.obj;
200
201                                 if (bt != s.altos_link) {
202                                         AltosDebug.debug("Stale message");
203                                         break;
204                                 }
205                                 if (s.address != null) {
206                                         AltosDebug.debug("Connection lost... retrying");
207                                         s.start_altos_bluetooth(s.address, true);
208                                 } else {
209                                         s.disconnect(true);
210                                 }
211                                 break;
212
213                                 /*
214                                  * Messages from TelemetryReader
215                                  */
216                         case MSG_TELEMETRY:
217                                 s.telemetry((AltosTelemetry) msg.obj);
218                                 break;
219                         case MSG_CRC_ERROR:
220                                 // forward crc error messages
221                                 s.telemetry_state.crc_errors = (Integer) msg.obj;
222                                 s.send_to_clients();
223                                 break;
224                         case MSG_BLUETOOTH_ENABLED:
225                                 AltosDebug.debug("TelemetryService notes that BT is now enabled");
226                                 address = AltosDroidPreferences.active_device();
227                                 if (address != null && !address.address.startsWith("USB"))
228                                         s.start_altos_bluetooth(address, false);
229                                 break;
230                         case MSG_MONITOR_IDLE_START:
231                                 AltosDebug.debug("start monitor idle");
232                                 s.start_idle_monitor();
233                                 break;
234                         case MSG_MONITOR_IDLE_STOP:
235                                 AltosDebug.debug("stop monitor idle");
236                                 s.stop_idle_monitor();
237                                 break;
238                         case MSG_REBOOT:
239                                 AltosDebug.debug("reboot");
240                                 s.reboot_remote();
241                                 break;
242                         case MSG_IGNITER_QUERY:
243                                 AltosDebug.debug("igniter query");
244                                 s.igniter_query(msg.replyTo);
245                                 break;
246                         case MSG_IGNITER_FIRE:
247                                 AltosDebug.debug("igniter fire");
248                                 s.igniter_fire((String) msg.obj);
249                                 break;
250                         default:
251                                 super.handleMessage(msg);
252                         }
253                 }
254         }
255
256         /* Handle telemetry packet
257          */
258         private void telemetry(AltosTelemetry telem) {
259                 AltosState      state;
260
261                 if (telemetry_state.states.containsKey(telem.serial))
262                         state = telemetry_state.states.get(telem.serial).clone();
263                 else
264                         state = new AltosState();
265                 telem.update_state(state);
266                 telemetry_state.states.put(telem.serial, state);
267                 if (state != null) {
268                         AltosPreferences.set_state(state);
269                 }
270                 send_to_clients();
271         }
272
273         /* Construct the message to deliver to clients
274          */
275         private Message message() {
276                 if (telemetry_state == null)
277                         AltosDebug.debug("telemetry_state null!");
278                 if (telemetry_state.states == null)
279                         AltosDebug.debug("telemetry_state.states null!");
280                 return Message.obtain(null, AltosDroid.MSG_STATE, telemetry_state);
281         }
282
283         /* A new friend has connected
284          */
285         private void add_client(Messenger client) {
286
287                 clients.add(client);
288                 AltosDebug.debug("Client bound to service");
289
290                 /* On connect, send the current state to the new client
291                  */
292                 send_to_client(client);
293                 send_idle_mode_to_client(client);
294
295                 /* If we've got an address from a previous session, then
296                  * go ahead and try to reconnect to the device
297                  */
298                 if (address != null && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
299                         AltosDebug.debug("Reconnecting now...");
300                         start_altos_bluetooth(address, false);
301                 }
302         }
303
304         /* A client has disconnected, clean up
305          */
306         private void remove_client(Messenger client) {
307                 clients.remove(client);
308                 AltosDebug.debug("Client unbound from service");
309
310                 /* When the list of clients is empty, stop the service if
311                  * we have no current telemetry source
312                  */
313
314                  if (clients.isEmpty() && telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED) {
315                          AltosDebug.debug("No clients, no connection. Stopping\n");
316                          stopSelf();
317                  }
318         }
319
320         private void send_to_client(Messenger client) {
321                 Message m = message();
322                 try {
323                         client.send(m);
324                 } catch (RemoteException e) {
325                         AltosDebug.error("Client %s disappeared", client.toString());
326                         remove_client(client);
327                 }
328         }
329
330         private void send_to_clients() {
331                 for (Messenger client : clients)
332                         send_to_client(client);
333         }
334
335         private void send_idle_mode_to_client(Messenger client) {
336                 Message m = Message.obtain(null, AltosDroid.MSG_IDLE_MODE, idle_monitor != null);
337                 try {
338                         client.send(m);
339                 } catch (RemoteException e) {
340                         AltosDebug.error("Client %s disappeared", client.toString());
341                         remove_client(client);
342                 }
343         }
344
345         private void send_idle_mode_to_clients() {
346                 for (Messenger client : clients)
347                         send_idle_mode_to_client(client);
348         }
349
350         private void telemetry_start() {
351                 if (telemetry_reader == null && idle_monitor == null && !ignite_running) {
352                         telemetry_reader = new TelemetryReader(altos_link, handler);
353                         telemetry_reader.start();
354                 }
355         }
356
357         private void telemetry_stop() {
358                 if (telemetry_reader != null) {
359                         AltosDebug.debug("disconnect(): stopping TelemetryReader");
360                         telemetry_reader.interrupt();
361                         try {
362                                 telemetry_reader.join();
363                         } catch (InterruptedException e) {
364                         }
365                         telemetry_reader = null;
366                 }
367         }
368
369         private void disconnect(boolean notify) {
370                 AltosDebug.debug("disconnect(): begin");
371
372                 telemetry_state.connect = TelemetryState.CONNECT_DISCONNECTED;
373                 telemetry_state.address = null;
374
375                 if (idle_monitor != null)
376                         stop_idle_monitor();
377
378                 if (altos_link != null)
379                         altos_link.closing();
380
381                 stop_receiver_voltage_timer();
382
383                 telemetry_stop();
384                 if (telemetry_logger != null) {
385                         AltosDebug.debug("disconnect(): stopping TelemetryLogger");
386                         telemetry_logger.stop();
387                         telemetry_logger = null;
388                 }
389                 if (altos_link != null) {
390                         AltosDebug.debug("disconnect(): stopping AltosDroidLink");
391                         altos_link.close();
392                         altos_link = null;
393                         ignite = null;
394                 }
395                 telemetry_state.config = null;
396                 if (notify) {
397                         AltosDebug.debug("disconnect(): send message to clients");
398                         send_to_clients();
399                         if (clients.isEmpty()) {
400                                 AltosDebug.debug("disconnect(): no clients, terminating");
401                                 stopSelf();
402                         }
403                 }
404         }
405
406         private void start_usb(UsbDevice device) {
407                 AltosUsb        d = new AltosUsb(this, device, handler);
408
409                 if (d != null) {
410                         disconnect(false);
411                         altos_link = d;
412                         try {
413                                 connected();
414                         } catch (InterruptedException ie) {
415                         }
416                 }
417         }
418
419         private void delete_serial(int serial) {
420                 telemetry_state.states.remove((Integer) serial);
421                 AltosPreferences.remove_state(serial);
422                 send_to_clients();
423         }
424
425         private void start_altos_bluetooth(DeviceAddress address, boolean pause) {
426                 if (bluetooth_adapter == null || !bluetooth_adapter.isEnabled())
427                         return;
428
429                 disconnect(false);
430
431                 // Get the BluetoothDevice object
432                 BluetoothDevice device = bluetooth_adapter.getRemoteDevice(address.address);
433
434                 this.address = address;
435                 AltosDebug.debug("start_altos_bluetooth(): Connecting to %s (%s)", device.getName(), device.getAddress());
436                 altos_link = new AltosBluetooth(device, handler, pause);
437                 telemetry_state.connect = TelemetryState.CONNECT_CONNECTING;
438                 telemetry_state.address = address;
439                 send_to_clients();
440         }
441
442         private void start_idle_monitor() {
443                 if (altos_link != null && idle_monitor == null) {
444                         telemetry_stop();
445                         idle_monitor = new AltosIdleMonitor(this, altos_link, true, false);
446                         idle_monitor.set_callsign(AltosPreferences.callsign());
447                         idle_monitor.start();
448                         send_idle_mode_to_clients();
449                 }
450         }
451
452         private void stop_idle_monitor() {
453                 if (idle_monitor != null) {
454                         try {
455                                 idle_monitor.abort();
456                         } catch (InterruptedException ie) {
457                         }
458                         idle_monitor = null;
459                         telemetry_start();
460                         send_idle_mode_to_clients();
461                 }
462         }
463
464         private void reboot_remote() {
465                 if (altos_link != null) {
466                         stop_idle_monitor();
467                         try {
468                                 altos_link.start_remote();
469                                 altos_link.printf("r eboot\n");
470                                 altos_link.flush_output();
471                         } catch (TimeoutException te) {
472                         } catch (InterruptedException ie) {
473                         } finally {
474                                 try {
475                                         altos_link.stop_remote();
476                                 } catch (InterruptedException ie) {
477                                 }
478                         }
479                 }
480         }
481
482         private void ensure_ignite() {
483                 if (ignite == null)
484                         ignite = new AltosIgnite(altos_link, true, false);
485         }
486
487         private synchronized void igniter_query(Messenger client) {
488                 ensure_ignite();
489                 HashMap<String,Integer> status_map = null;
490                 ignite_running = true;
491                 try {
492                         stop_idle_monitor();
493                         try {
494                                 status_map = ignite.status();
495                         } catch (InterruptedException ie) {
496                                 AltosDebug.debug("ignite.status interrupted");
497                         } catch (TimeoutException te) {
498                                 AltosDebug.debug("ignite.status timeout");
499                         }
500                 } finally {
501                         ignite_running = false;
502                 }
503                 Message m = Message.obtain(null, AltosDroid.MSG_IGNITER_STATUS, status_map);
504                 try {
505                         client.send(m);
506                 } catch (RemoteException e) {
507                 }
508         }
509
510         private synchronized void igniter_fire(String igniter) {
511                 ensure_ignite();
512                 ignite_running = true;
513                 stop_idle_monitor();
514                 try {
515                         ignite.fire(igniter);
516                 } catch (InterruptedException ie) {
517                 } finally {
518                         ignite_running = false;
519                 }
520         }
521
522         // Timer for receiver battery voltage monitoring
523         Timer receiver_voltage_timer;
524
525         private void update_receiver_voltage() {
526                 if (altos_link != null && idle_monitor == null && !ignite_running) {
527                         try {
528                                 double  voltage = altos_link.monitor_battery();
529                                 telemetry_state.receiver_battery = voltage;
530                                 send_to_clients();
531                         } catch (InterruptedException ie) {
532                         }
533                 }
534         }
535
536         private void stop_receiver_voltage_timer() {
537                 if (receiver_voltage_timer != null) {
538                         receiver_voltage_timer.cancel();
539                         receiver_voltage_timer.purge();
540                         receiver_voltage_timer = null;
541                 }
542         }
543
544         private void start_receiver_voltage_timer() {
545                 if (receiver_voltage_timer == null && altos_link.has_monitor_battery()) {
546                         receiver_voltage_timer = new Timer();
547                         receiver_voltage_timer.scheduleAtFixedRate(new TimerTask() { public void run() {update_receiver_voltage();}}, 1000L, 10000L);
548                 }
549         }
550
551         private void connected() throws InterruptedException {
552                 AltosDebug.debug("connected top");
553                 AltosDebug.check_ui("connected\n");
554                 try {
555                         if (altos_link == null)
556                                 throw new InterruptedException("no bluetooth");
557                         telemetry_state.config = altos_link.config_data();
558                         altos_link.set_radio_frequency(telemetry_state.frequency);
559                         altos_link.set_telemetry_rate(telemetry_state.telemetry_rate);
560                 } catch (TimeoutException e) {
561                         // If this timed out, then we really want to retry it, but
562                         // probably safer to just retry the connection from scratch.
563                         AltosDebug.debug("connected timeout");
564                         if (address != null) {
565                                 AltosDebug.debug("connected timeout, retrying");
566                                 start_altos_bluetooth(address, true);
567                         } else {
568                                 handler.obtainMessage(MSG_CONNECT_FAILED).sendToTarget();
569                                 disconnect(true);
570                         }
571                         return;
572                 }
573
574                 AltosDebug.debug("connected bluetooth configured");
575                 telemetry_state.connect = TelemetryState.CONNECT_CONNECTED;
576                 telemetry_state.address = address;
577
578                 telemetry_start();
579
580                 AltosDebug.debug("connected TelemetryReader started");
581
582                 telemetry_logger = new TelemetryLogger(this, altos_link);
583
584                 start_receiver_voltage_timer();
585
586                 AltosDebug.debug("Notify UI of connection");
587
588                 send_to_clients();
589         }
590
591
592         @Override
593         public void onCreate() {
594
595                 AltosDebug.init(this);
596
597                 // Initialise preferences
598                 AltosDroidPreferences.init(this);
599
600                 // Get local Bluetooth adapter
601                 bluetooth_adapter = BluetoothAdapter.getDefaultAdapter();
602
603                 telemetry_state = new TelemetryState();
604
605                 // Create a reference to the NotificationManager so that we can update our notifcation text later
606                 //mNM = (NotificationManager)getSystemService(NOTIFICATION_SERVICE);
607
608                 telemetry_state.connect = TelemetryState.CONNECT_DISCONNECTED;
609                 telemetry_state.address = null;
610
611                 /* Pull the saved state information out of the preferences database
612                  */
613                 ArrayList<Integer> serials = AltosPreferences.list_states();
614
615                 telemetry_state.latest_serial = AltosPreferences.latest_state();
616
617                 AltosDebug.debug("latest serial %d\n", telemetry_state.latest_serial);
618
619                 for (int serial : serials) {
620                         AltosState saved_state = AltosPreferences.state(serial);
621                         if (saved_state != null) {
622                                 if (telemetry_state.latest_serial == 0)
623                                         telemetry_state.latest_serial = serial;
624
625                                 AltosDebug.debug("recovered old state serial %d flight %d",
626                                                  serial,
627                                                  saved_state.flight);
628                                 if (saved_state.gps != null)
629                                         AltosDebug.debug("\tposition %f,%f",
630                                                          saved_state.gps.lat,
631                                                          saved_state.gps.lon);
632                                 telemetry_state.states.put(serial, saved_state);
633                         } else {
634                                 AltosDebug.debug("Failed to recover state for %d", serial);
635                                 AltosPreferences.remove_state(serial);
636                         }
637                 }
638         }
639
640         @Override
641         public int onStartCommand(Intent intent, int flags, int startId) {
642                 AltosDebug.debug("Received start id %d: %s", startId, intent);
643
644                 CharSequence text = getText(R.string.telemetry_service_started);
645
646                 // Create notification to be displayed while the service runs
647                 Notification notification = new Notification(R.drawable.am_status_c, text, 0);
648
649                 // The PendingIntent to launch our activity if the user selects this notification
650                 PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
651                                 new Intent(this, AltosDroid.class), 0);
652
653                 // Set the info for the views that show in the notification panel.
654                 notification.setLatestEventInfo(this, getText(R.string.telemetry_service_label), text, contentIntent);
655
656                 // Set the notification to be in the "Ongoing" section.
657                 notification.flags |= Notification.FLAG_ONGOING_EVENT;
658
659                 // Move us into the foreground.
660                 startForeground(NOTIFICATION, notification);
661
662                 /* Start bluetooth if we don't have a connection already */
663                 if (intent != null &&
664                     (telemetry_state.connect == TelemetryState.CONNECT_NONE ||
665                      telemetry_state.connect == TelemetryState.CONNECT_DISCONNECTED))
666                 {
667                         String  action = intent.getAction();
668
669                         if (action.equals(AltosDroid.ACTION_BLUETOOTH)) {
670                                 DeviceAddress address = AltosDroidPreferences.active_device();
671                                 if (address != null && !address.address.startsWith("USB"))
672                                         start_altos_bluetooth(address, false);
673                         }
674                 }
675
676                 // We want this service to continue running until it is explicitly
677                 // stopped, so return sticky.
678                 return START_STICKY;
679         }
680
681         @Override
682         public void onDestroy() {
683
684                 // Stop the bluetooth Comms threads
685                 disconnect(true);
686
687                 // Demote us from the foreground, and cancel the persistent notification.
688                 stopForeground(true);
689
690                 // Tell the user we stopped.
691                 Toast.makeText(this, R.string.telemetry_service_stopped, Toast.LENGTH_SHORT).show();
692         }
693
694         @Override
695         public IBinder onBind(Intent intent) {
696                 return messenger.getBinder();
697         }
698
699         /* AltosIdleMonitorListener */
700         public void update(AltosState state, AltosListenerState listener_state) {
701                 telemetry_state.states.put(state.serial, state);
702                 telemetry_state.receiver_battery = listener_state.battery;
703                 send_to_clients();
704         }
705
706         public void failed() {
707         }
708
709         public void error(String reason) {
710                 stop_idle_monitor();
711         }
712 }