Skip to content

Commit

Permalink
Merge pull request #30 from OSGP/feature/FDP-2359-reboot-command
Browse files Browse the repository at this point in the history
FDP-2359: simulate device reboot
  • Loading branch information
smvdheijden authored Sep 10, 2024
2 parents ab07f96 + 710e51f commit 1ebb49d
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 9 deletions.
1 change: 1 addition & 0 deletions application/src/integrationTest/resources/application.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ simulator:
scheduled-message: "classpath:messages/kod-message.json"
success-message: "classpath:messages/psk-change-success-message.json"
failure-message: "classpath:messages/psk-change-failure-message.json"
reboot-success-message: "classpath:messages/kod-reboot-success-message.json"
# Simulator will produce valid/invalid CBOR messages
produce-valid-cbor: true
# Uri of the target coap server
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"ID": 867787050253370,
"MID": 1,
"IMSI": 460023210226023,
"EID": "89001012012341234012345678901224",
"ICCID": "89882280666074936745",
"TS": 1687522333,
"TSL": 1687522333,
"CON": "A",
"FW": 2100,
"TEL": 20416,
"cID": 7020155,
"PWR": 0,
"BAT": 3533,
"CSQ": 25,
"RSRQ": -210,
"RSRP": -99,
"SNR": 22,
"TRY": 1,
"MSI": 0,
"URC": [
"INIT","WDR",
{
"DL": "!CMD:REBOOT"
}
],
"A": [
3,
1,
0,
0,
0,
0,
0,
0
],
"MEM": 7,
"UPT": 32,
"RLY": 0,
"T1": [
265
],
"H1": [
402
],
"D": 8,
"P1": [
12
],
"P2": [
12
],
"FMC": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ class SimulatorProperties(
val scheduledMessage: Resource,
val successMessage: Resource,
val failureMessage: Resource,
val rebootSuccessMessage: Resource,
val produceValidCbor: Boolean,
val cipherSuites: List<CipherSuite>
)
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import org.eclipse.californium.core.coap.Request
import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
import org.gxf.crestdevicesimulator.simulator.CborFactory
import org.gxf.crestdevicesimulator.simulator.coap.CoapClientService
import org.gxf.crestdevicesimulator.simulator.response.CommandService
import org.gxf.crestdevicesimulator.simulator.response.PskExtractor
import org.gxf.crestdevicesimulator.simulator.response.command.PskService
import org.springframework.stereotype.Service
Expand All @@ -27,7 +28,8 @@ class MessageHandler(
private val coapClientService: CoapClientService,
private val simulatorProperties: SimulatorProperties,
private val pskService: PskService,
private val mapper: ObjectMapper
private val mapper: ObjectMapper,
private val commandService: CommandService
) {
private val logger = KotlinLogging.logger {}

Expand Down Expand Up @@ -70,10 +72,16 @@ class MessageHandler(
private fun handleResponse(response: CoapResponse) {
if (response.isSuccess) {
val payload = String(response.payload)
if (PskExtractor.hasPskSetCommand(payload)) {
handlePskSetCommand(payload)
} else if (pskService.isPendingKeyPresent()) {
pskService.changeActiveKey()
when {
PskExtractor.hasPskSetCommand(payload) -> {
handlePskSetCommand(payload)
}
pskService.isPendingKeyPresent() -> {
pskService.changeActiveKey()
}
commandService.hasRebootCommand(payload) -> {
sendRebootSuccesMessage(payload)
}
}
} else {
logger.error { "Received error response with ${response.code}" }
Expand All @@ -88,30 +96,37 @@ class MessageHandler(
try {
logger.info { "Device ${simulatorProperties.pskIdentity} needs key change" }
pskService.preparePendingKey(payload)
sendSuccessMessage(payload)
sendPskSetSuccessMessage(payload)
} catch (e: Exception) {
logger.error(e) { "PSK change error, send failure message and set pending key status to invalid" }
sendFailureMessage(payload)
sendPskSetFailureMessage(payload)
pskService.setPendingKeyAsInvalid()
}
}

private fun sendSuccessMessage(pskCommand: String) {
private fun sendPskSetSuccessMessage(pskCommand: String) {
logger.info { "Sending success message for command $pskCommand" }
val messageJsonNode =
mapper.readTree(simulatorProperties.successMessage.inputStream)
val message = updatePskCommandInMessage(messageJsonNode, URC_PSK_SUCCESS, pskCommand)
sendMessage(message)
}

private fun sendFailureMessage(pskCommand: String) {
private fun sendPskSetFailureMessage(pskCommand: String) {
logger.warn { "Sending failure message for command $pskCommand" }
val messageJsonNode =
mapper.readTree(simulatorProperties.failureMessage.inputStream)
val message = updatePskCommandInMessage(messageJsonNode, URC_PSK_ERROR, pskCommand)
sendMessage(message)
}

private fun sendRebootSuccesMessage(command: String) {
logger.info { "Sending success message for command $command" }
val message =
mapper.readTree(simulatorProperties.rebootSuccessMessage.inputStream)
sendMessage(message)
}

private fun updatePskCommandInMessage(
message: JsonNode,
urc: String,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-FileCopyrightText: Contributors to the GXF project
//
// SPDX-License-Identifier: Apache-2.0

package org.gxf.crestdevicesimulator.simulator.response

import org.springframework.stereotype.Service

@Service
class CommandService {
fun hasRebootCommand(command: String) =
command.contains("CMD:REBOOT")
}
1 change: 1 addition & 0 deletions application/src/main/resources/application-test-data.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ simulator:
scheduled-message: "classpath:messages/kod-alarm-message.json"
success-message: "classpath:messages/psk-change-success-message.json"
failure-message: "classpath:messages/psk-change-failure-message.json"
reboot-success-message: "classpath:messages/kod-reboot-success-message.json"
# Simulator will produce valid/invalid CBOR messages
produce-valid-cbor: true
# pre-shared key for coaps (dtls)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"ID": 867787050253370,
"MID": 1,
"IMSI": 460023210226023,
"EID": "89001012012341234012345678901224",
"ICCID": "89882280666074936745",
"TS": 1687522333,
"TSL": 1687522333,
"CON": "A",
"FW": 2100,
"TEL": 20416,
"cID": 7020155,
"PWR": 0,
"BAT": 3533,
"CSQ": 25,
"RSRQ": -210,
"RSRP": -99,
"SNR": 22,
"TRY": 1,
"MSI": 0,
"URC": [
"INIT","WDR",
{
"DL": "!CMD:REBOOT"
}
],
"A": [
3,
1,
0,
0,
0,
0,
0,
0
],
"MEM": 7,
"UPT": 32,
"RLY": 0,
"T1": [
265
],
"H1": [
402
],
"D": 8,
"P1": [
12
],
"P2": [
12
],
"FMC": 0
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import org.eclipse.californium.core.CoapClient
import org.gxf.crestdevicesimulator.configuration.SimulatorProperties
import org.gxf.crestdevicesimulator.simulator.CborFactory
import org.gxf.crestdevicesimulator.simulator.coap.CoapClientService
import org.gxf.crestdevicesimulator.simulator.response.CommandService
import org.gxf.crestdevicesimulator.simulator.response.command.PskService
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith
Expand Down Expand Up @@ -37,6 +38,9 @@ class MessageHandlerTests {
@Mock
private lateinit var pskService: PskService

@Mock
private lateinit var commandService: CommandService

@InjectMocks
private lateinit var messageHandler: MessageHandler

Expand Down

0 comments on commit 1ebb49d

Please sign in to comment.