diff --git a/backend-apis b/backend-apis index 85a6ffd..56c734f 160000 --- a/backend-apis +++ b/backend-apis @@ -1 +1 @@ -Subproject commit 85a6ffd5168f42ac18561e25cc2cf334cd8ded17 +Subproject commit 56c734f1804b27f04d64da3f001321fc7472754e diff --git a/tests/default_value.py b/tests/default_value.py index 7c405cf..49cea2c 100644 --- a/tests/default_value.py +++ b/tests/default_value.py @@ -63,3 +63,29 @@ ("stored_scratchpad", SCRATCHPAD_INFO), ("configuration_data_content", TEST_CDC1) ]) + +NODE_CONFIG_FOR_SETTING = { + "sink_id": SINK_ID, + "node_role": 1, + "node_address": 1516, + "network_address": 879123, + "network_channel": 7, + "app_config_diag": 30, + "app_config_seq": 300, + "app_config_data": bytes.fromhex("90807060"), + "channel_map": 120, + "cipher_key": bytes.fromhex("102030"), + "authentication_key": bytes.fromhex("AABBCC"), + "started": True, + "network_keys": { + "cipher": bytes.fromhex("5000"), + "authentication": bytes.fromhex("6000"), + "sequence": 6767 + }, + "management_keys": { + "cipher": bytes.fromhex("10AA"), + "authentication": bytes.fromhex("20BB"), + "sequence": 520 + }, +} + diff --git a/tests/test_set_config.py b/tests/test_set_config.py index bb8d805..053be3e 100644 --- a/tests/test_set_config.py +++ b/tests/test_set_config.py @@ -43,3 +43,17 @@ def test_generate_parse_response_with_readonly_fields(): for k, v in request.__dict__.items(): assert v == request2.__dict__[k] + +def test_generate_parse_request_with_writeable_fields(): + request = wirepas_mesh_messaging.SetConfigRequest( + SINK_ID, NODE_CONFIG_FOR_SETTING, REQUEST_ID + ) + + request2 = wirepas_mesh_messaging.SetConfigRequest.from_payload( + request.payload + ) + + assert NODE_CONFIG_FOR_SETTING == request2.new_config + for k, v in request.__dict__.items(): + assert v == request2.__dict__[k] + diff --git a/wirepas_mesh_messaging/config_helper.py b/wirepas_mesh_messaging/config_helper.py index 2882f49..3f1e011 100644 --- a/wirepas_mesh_messaging/config_helper.py +++ b/wirepas_mesh_messaging/config_helper.py @@ -243,33 +243,63 @@ def set_config_otap(message_obj, dic): set_scratchpad_target(message_obj.target_and_action, dic["target_and_action"]) -def parse_config_keys(message_obj, dic): +def parse_config_wo_fields(message_obj, dic): """ - Parses network keys. + Parses the message_obj for write only (wo) fields and copies them into + dic. - The keys are only available on a SinkNewConfig message. + Write only fields are ones that are only available on a SinkNewConfig + message. Args: message_obj (proto): protocol buffer object - dic (dict): the dictionary where to copy the keys into + dic (dict): the dictionary where to copy the wo fields + """ parse_optional_field(message_obj.keys, "cipher", dic, "cipher_key") parse_optional_field(message_obj.keys, "authentication", dic, "authentication_key") + if message_obj.HasField("network_keys"): + dic["network_keys"] = { + "cipher": message_obj.network_keys.cipher, + "authentication": message_obj.network_keys.authentication, + "sequence": message_obj.network_keys.sequence + } + + if message_obj.HasField("management_keys"): + dic["management_keys"] = { + "cipher": message_obj.management_keys.cipher, + "authentication": message_obj.management_keys.authentication, + "sequence": message_obj.management_keys.sequence + } + -def set_config_keys(message_obj, dic): +def set_config_wo_fields(message_obj, dic): """ - Sets network keys. + Sets the message_obj with the write only (wo) fields present in dic. - The keys are only available on a SinkNewConfig message. + Write only fields are ones that are only available on a SinkNewConfig + message. Args: message_obj (proto): protocol buffer object - dic (dict): the dictionary where to copy the keys from + dic (dict): the dictionary where to copy the wo fields from + """ - set_optional_field(message_obj, "cipher", dic, "cipher_key") - set_optional_field(message_obj, "authentication", dic, "authentication_key") + + set_optional_field(message_obj.keys, "cipher", dic, "cipher_key") + set_optional_field(message_obj.keys, "authentication", dic, "authentication_key") + + if "network_keys" in dic: + message_obj.network_keys.cipher = dic["network_keys"]["cipher"] + message_obj.network_keys.authentication = dic["network_keys"]["authentication"] + message_obj.network_keys.sequence = dic["network_keys"]["sequence"] + + if "management_keys" in dic: + message_obj.management_keys.cipher = dic["management_keys"]["cipher"] + message_obj.management_keys.authentication = dic["management_keys"]["authentication"] + message_obj.management_keys.sequence = dic["management_keys"]["sequence"] def parse_config_ro(message_obj, dic): diff --git a/wirepas_mesh_messaging/gateway_feature.py b/wirepas_mesh_messaging/gateway_feature.py index cf6100d..98e269e 100644 --- a/wirepas_mesh_messaging/gateway_feature.py +++ b/wirepas_mesh_messaging/gateway_feature.py @@ -19,4 +19,5 @@ class GatewayFeature(enum.Enum): GW_FEATURE_UNKNOWN = GatewayFeature_pb.UNKNOWN GW_FEATURE_SCRATCHPAD_CHUNK_V1 = GatewayFeature_pb.SCRATCHPAD_CHUNK_V1 GW_FEATURE_CONFIGURATION_DATA_V1 = GatewayFeature_pb.CONFIGURATION_DATA_V1 + GW_FEATURE_SINK_KEY_MANAGEMENT_V1 = GatewayFeature_pb.SINK_KEY_MANAGEMENT_V1 diff --git a/wirepas_mesh_messaging/set_config.py b/wirepas_mesh_messaging/set_config.py index 7b650d7..4a5fb81 100644 --- a/wirepas_mesh_messaging/set_config.py +++ b/wirepas_mesh_messaging/set_config.py @@ -14,13 +14,13 @@ from .config_helper import ( parse_config_rw, - parse_config_keys, set_config_rw, - set_config_keys, parse_config_ro, parse_config_otap, set_config_otap, set_config_ro, + set_config_wo_fields, + parse_config_wo_fields, ) @@ -43,6 +43,14 @@ class SetConfigRequest(Request): cipher_key (bytearray) authentication_key (bytearray) started (bool) + network_keys (dict): with following required keys: + cipher (bytearray) + authentication (bytearray) + sequence (int) + management_keys (dict): with following required keys: + cipher (bytearray) + authentication (bytearray) + sequence (int) Note: app_config_data/app_config_seq/app_config_data must all be defined to change one of them only relevant keys for new config has to be defined @@ -71,7 +79,7 @@ def from_payload(cls, payload): new_config = {} new_config["sink_id"] = req.config.sink_id parse_config_rw(req.config, new_config) - parse_config_keys(req.config, new_config) + parse_config_wo_fields(req.config, new_config) return cls(req.config.sink_id, new_config, d["req_id"], time_ms_epoch=d["time_ms_epoch"]) @@ -84,7 +92,7 @@ def payload(self): set_config.config.sink_id = self.sink_id set_config_rw(set_config.config, self.new_config) - set_config_keys(set_config.config.keys, self.new_config) + set_config_wo_fields(set_config.config, self.new_config) return message.SerializeToString()