diff --git a/application/src/integrationTest/resources/application.yaml b/application/src/integrationTest/resources/application.yaml index 0d99ba1..16b4b95 100644 --- a/application/src/integrationTest/resources/application.yaml +++ b/application/src/integrationTest/resources/application.yaml @@ -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 diff --git a/application/src/integrationTest/resources/messages/kod-reboot-success-message.json b/application/src/integrationTest/resources/messages/kod-reboot-success-message.json new file mode 100644 index 0000000..e79c922 --- /dev/null +++ b/application/src/integrationTest/resources/messages/kod-reboot-success-message.json @@ -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 +} diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt index f9ffa7e..3b32e09 100644 --- a/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/configuration/SimulatorProperties.kt @@ -20,6 +20,7 @@ class SimulatorProperties( val scheduledMessage: Resource, val successMessage: Resource, val failureMessage: Resource, + val rebootSuccessMessage: Resource, val produceValidCbor: Boolean, val cipherSuites: List ) diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt index 5e74943..0c9b43f 100644 --- a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandler.kt @@ -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 @@ -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 {} @@ -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}" } @@ -88,15 +96,15 @@ 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) @@ -104,7 +112,7 @@ class MessageHandler( 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) @@ -112,6 +120,13 @@ class MessageHandler( 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, diff --git a/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt new file mode 100644 index 0000000..328d393 --- /dev/null +++ b/application/src/main/kotlin/org/gxf/crestdevicesimulator/simulator/response/CommandService.kt @@ -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") +} diff --git a/application/src/main/resources/application-test-data.yaml b/application/src/main/resources/application-test-data.yaml index 00855d3..6c1af18 100644 --- a/application/src/main/resources/application-test-data.yaml +++ b/application/src/main/resources/application-test-data.yaml @@ -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) diff --git a/application/src/main/resources/messages/kod-reboot-success-message.json b/application/src/main/resources/messages/kod-reboot-success-message.json new file mode 100644 index 0000000..e79c922 --- /dev/null +++ b/application/src/main/resources/messages/kod-reboot-success-message.json @@ -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 +} diff --git a/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt b/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt index 7e4d6a5..a4cae83 100644 --- a/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt +++ b/application/src/test/kotlin/org/gxf/crestdevicesimulator/simulator/message/MessageHandlerTests.kt @@ -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 @@ -37,6 +38,9 @@ class MessageHandlerTests { @Mock private lateinit var pskService: PskService + @Mock + private lateinit var commandService: CommandService + @InjectMocks private lateinit var messageHandler: MessageHandler