The Device OS API will send a status of “firmware_update_begin, firmware_update_progress, etc” depending on if a firmware update is coming through. This automatically happens when your device is part of a product. You should always get a status from this if a firmware .bin has been released and is marked to deploy to that device.
You can add an event handler to handle these system events.
System.on(firmware_update, firmware_update_handler);
In your handler, you can then use that info to switch your own state for your firmware so you can do whatever you want during each state. For instance, you can change a switch state based on these parameters that come through the system event. See the “status” variable below.
void firmware_update_handler(system_event_t event, int status) {
switch(status) {
This is the parameter from the Device OS API. When this changes, we log to the console and switch our gFirmwareUpdateStatus variable so we can use that outside of this handler.
switch(status) {
case firmware_update_begin:
gFirmwareUpdateStatus = FW_UPDATING;
appLog.info("Firmware update beginning event was triggered.");
break;
case firmware_update_progress:
gFirmwareUpdateStatus = FW_UPDATING;
appLog.info("Firmware update progress event was triggered.");
break;
case firmware_update_complete:
gFirmwareUpdateStatus = FW_DONE;
appLog.info("Firmware update complete event was triggered.");
break;
case firmware_update_failed:
gFirmwareUpdateStatus = FW_DONE;
appLog.error("Firmware update failed event was triggered.");
break;
default:
appLog.warn("Unknown firmware_update_handler status %i", status);
break;
}
For instance, outside of this handler, we want to have the device hang there and do a particle.process while the update is happening, so we add this while loop.
while (gFirmwareUpdateStatus == FW_UPDATING) {
Particle.process();
}
The other states don’t really matter to us as the device reboots automatically after an update. But, for instance, we could do something like flash an LED if our state switches to “firmware_update_failed” to let us know something bad happened.