Unverified Commit 09a2d645 authored by SAGAR PATEL's avatar SAGAR PATEL Committed by GitHub
Browse files

Add DevNonce Clear Functionality (#570)



* Add DevNonce Clear Functionality

Details:
- DevNonce clear functionality helps to remove older device
activation records from device_activation table in NS.
- Using this method we can clear older or already generated device activation
records from database to prevent "DevNonce already exits" error in OTAA method

*Note: Server keeps latest 20 records in device_activation table of NS database
for manage recent device activation.

Signed-off-by: default avatarSAGAR PATEL <sagar.a.patel@slscorp.com>

* Update chirpstack-api.

Co-authored-by: default avatarOrne Brocaar <info@brocaar.com>
parent a2d9a6d5
......@@ -1774,3 +1774,22 @@ func (n *NetworkServerAPI) GetADRAlgorithms(ctx context.Context, req *empty.Empt
return &resp, nil
}
// ClearDeviceNonces deletes the device activation records matching the given DevEUI.
func (n *NetworkServerAPI) ClearDeviceNonces(ctx context.Context, req *ns.ClearDeviceNoncesRequest) (*empty.Empty, error) {
var devEUI lorawan.EUI64
copy(devEUI[:], req.DevEui)
err := storage.Transaction(func(tx sqlx.Ext) error {
if err := storage.ClearDeviceNoncesForDevice(ctx, tx, devEUI); err != nil {
return errToRPCError(err)
}
return nil
})
if err != nil {
return nil, err
}
return &empty.Empty{}, nil
}
......@@ -286,3 +286,27 @@ func ValidateDevNonce(ctx context.Context, db sqlx.Queryer, joinEUI, devEUI lora
return nil
}
// ClearDeviceNoncesForDevice removes the device DevNonce from device-activation for the given
// DevEUI.
func ClearDeviceNoncesForDevice(ctx context.Context, db sqlx.Execer, devEUI lorawan.EUI64) error {
_, err := db.Exec(`
delete
from
device_activation
where
id IN (SELECT id from device_activation
where dev_eui = $1
ORDER BY id DESC
OFFSET 20)
`, devEUI[:])
if err != nil {
return handlePSQLError(err, "device devnonce delete error")
}
log.WithFields(log.Fields{
"dev_eui": devEUI,
"ctx_id": ctx.Value(logging.ContextIDKey),
}).Info("device dev-nonce deleted")
return nil
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment