Skip to content

Fix OTAA key mapping for LoRaWAN 1.1.x devices#298

Open
MortenGuldager wants to merge 1 commit into
OS2iot:masterfrom
MortenGuldager:fix/lorawan-1.1-otaa-key-mapping
Open

Fix OTAA key mapping for LoRaWAN 1.1.x devices#298
MortenGuldager wants to merge 1 commit into
OS2iot:masterfrom
MortenGuldager:fix/lorawan-1.1-otaa-key-mapping

Conversation

@MortenGuldager
Copy link
Copy Markdown

@MortenGuldager MortenGuldager commented May 26, 2026

Problem

For LoRaWAN 1.1.x devices havner brugerens AppKey i ChirpStacks nwk_key-felt, og det rigtige app_key-felt sættes aldrig. Symptomet er at devices ikke kan joine fordi network- og application-nøglerne reelt er byttet om i ChirpStack.

ChirpStacks DeviceKeys-protobuf har versionsafhængig semantik:

Felt LoRaWAN 1.0.x LoRaWAN 1.1.x
nwk_key AppKey (eneste root-nøgle) NwkKey (root network)
app_key Ubrugt AppKey (root app)

OS2IoT skrev altid OTAAapplicationKey ind i nwk_key uden at se på device-profilens macVersion (mapDeviceKeysToChirpstack, chirpstack-device.service.ts:207). Læsestien lavede den symmetriske fejl.

Løsning

  • OTAAnetworkKey tilføjet til DTO + entity + ny migration (kun udfyldt ved 1.1.x — frontend skjuler feltet ved 1.0.x).
  • mapDeviceKeysToChirpstack og activateDeviceWithOTAA tager nu macVersion ind og mapper version-afhængigt. 1.0.x-adfærd uændret.
  • doActivationByOTAA slår device-profilen op via DeviceProfileService og videregiver macVersion + begge nøgler.
  • mapActivationAndKeys udfylder både OTAAapplicationKey og OTAAnetworkKey for 1.1.x ved læsning.
  • CSV import/export schema udvidet med OTAAnetworkKey-kolonne.

Test (manuel, lokal docker-compose)

  1. Opret device-profil med mac_version = 5 (LoRaWAN 1.1.0) og en med mac_version = 3 (1.0.3).
  2. Opret application + to OTAA-devices, ét pr. profil.
  3. 1.1.x device: sæt OTAAapplicationKey = aaaa…aa og OTAAnetworkKey = bbbb…bb. Verificer i ChirpStack via gRPC:
    grpcurl … api.DeviceService/GetKeys
    → nwk_key = bbbb…bb, app_key = aaaa…aa  ✓
    
  4. 1.0.x device (regression): sæt kun OTAAapplicationKey = cccc…cc.
    → nwk_key = cccc…cc, app_key = 00…00  ✓ (uændret)
    
  5. GET device tilbage via API → korrekte nøgler i begge felter for 1.1.x.

Migration af eksisterende data

Migrationen tilføjer kun den nye kolonne. Eksisterende 1.1.x-rækker har brugerens "AppKey" gemt i ChirpStacks nwk_key, og OTAAnetworkKey på OS2IoT-siden er NULL. Næste gang en bruger åbner et 1.1.x device i frontend vil de se nøglen vist i App-feltet + et tomt NwkKey-felt — og kan reprovisionere korrekt. Bevidst valg at lade dette være en bruger-handling fremfor automatisk data-flytning; sig til hvis I foretrækker en data-migration.

Companion PR

ChirpStack's DeviceKeys protobuf has version-dependent semantics:

* LoRaWAN 1.0.x: nwk_key holds the single root AppKey, app_key is unused.
* LoRaWAN 1.1.x: nwk_key holds the root NwkKey, app_key holds the root
  AppKey.

The backend always wrote the user's OTAAapplicationKey into ChirpStack's
nwk_key field regardless of device-profile mac_version, so 1.1.x devices
ended up with their AppKey in the NwkKey slot and no AppKey set. The
read path made the symmetric mistake.

Changes:

* Add OTAAnetworkKey to CreateLoRaWANSettingsDto and LoRaWANDevice; add
  migration AddedOTAANetworkKey1779744000000.
* mapDeviceKeysToChirpstack and activateDeviceWithOTAA now take macVersion
  and route keys version-correctly. For 1.0.x, behaviour is unchanged.
* doActivationByOTAA looks up the device profile's macVersion and passes
  both keys through.
* mapActivationAndKeys reads both nwk_key and app_key for 1.1.x; for
  1.0.x, behaviour is unchanged.
* CSV import/export schema gets the new OTAAnetworkKey column.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant