LwM2M Firmware Update Fails Silently
DodaTech
Updated 2026-06-26
1 min read
In this tutorial, you'll learn about LwM2M Firmware Update Fails Silently. We cover key concepts, practical examples, and best practices.
The Problem
LwM2M firmware update execute returns success but device does not update.
Quick Fix
Wrong
// Firmware update handler does not validate or apply the firmware
static int update_exec(...) {
return 0; // Falsely reports success
}```
Server receives 2.04 Changed, but device still runs old firmware.
### Right
```cpp
#include <anjay/anjay.h>
#include <anjay/fw_update.h>
static int fw_update_handler(anjay_t *anjay) {
// Get firmware package from Firmware Update Object (ID 5)
const uint8_t *pkg;
size_t pkg_size;
anjay_fw_update_get_package(anjay, &pkg, &pkg_size);
// Validate firmware integrity (SHA256)
uint8_t hash[32];
anjay_fw_update_get_hash(anjay, hash);
// Apply firmware
if (pkg_size > 0 && pkg[0] == 0x1F && pkg[1] == 0x8B) {
// GZIP firmware — decompress and flash
flash_firmware(pkg, pkg_size);
return 0; // Success
}
// Report error
anjay_fw_update_set_result(anjay,
ANJAY_FW_UPDATE_RESULT_INVALID_PACKAGE);
return -1;
}
int main(void) {
anjay_fw_update_install(anjay, fw_update_handler);
}```
Firmware downloaded, validated, and flashed. Device reboots with new firmware.
## Prevention
LwM2M <a href="/embedded-systems/firmware-updates/">Firmware Update</a> (Object ID 5) supports OTA updates. Resources: Package (5/0/0) — the binary, Package URI (5/0/1) — download URL, Update (5/0/2) — execute trigger, State (5/0/3) — current state (Idle, Downloading, Downloaded, Updating), Result (5/0/5) — update result code. The client downloads the package (via CoAP or HTTP), validates, then applies on Execute.
DodaTech engineers apply these same patterns across Doda Browser, DodaZIP, and Durga Antivirus Pro for production IoT reliability.
## FAQ
<details style="margin-bottom:12px;border:1px solid #e2e8f0;border-radius:10px;overflow:hidden"><summary style="cursor:pointer;padding:14px 18px;font-weight:600;font-size:1.05rem;background:#f8fafc;border-bottom:1px solid #e2e8f0;color:#1e293b">### What firmware formats are supported?</summary><div style="padding:14px 18px;color:#475569;line-height:1.7;background:#fff"><p>Any binary format. Common: raw binary, GZIP, differential patches. Format depends on the device bootloader.</p>
<h3 id="what-is-package-vs-package-uri">What is Package vs Package URI?</h3><p>Package: direct CoAP upload of firmware binary (limited by CoAP block size). Package URI: HTTP URL for the device to download (supports larger files).</p>
<h3 id="what-state-machine-does-update-follow">What state machine does update follow?</h3><p>Idle → Downloading → Downloaded → Updating → Idle. Errors: check Result resource for failure reason.</p>
</div></details>
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro