From a6c906cbd59a8336a86c508a8c70c9123d82a0f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=9Ean=20G=C3=BCne=C5=9F?= <180301198+sgunes-wirepas@users.noreply.github.com> Date: Thu, 19 Sep 2024 11:58:12 +0300 Subject: [PATCH] Enable clearing of sink scratchpad According to the gateway to backend API, scratchpad should be cleared if the scratchpad bytes are not present in the upload scratchpad request. This change enables clearing of the scratchpad in order to make it easier to test OTAP functionality of the gateway. Additionally, in order to prevent accidental clearing of the scratchpad, the clearing is only done if the scratchpad bytes field is missing (i.e. it should fail if an empty file is uploaded). --- .../wirepas_gateway/dbus/sink_manager.py | 29 +++++++++++++++++ .../wirepas_gateway/transport_service.py | 9 ++++-- sink_service/source/otap.c | 32 +++++++++++++++++++ 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/python_transport/wirepas_gateway/dbus/sink_manager.py b/python_transport/wirepas_gateway/dbus/sink_manager.py index 7dc59fa1..bc240bdd 100644 --- a/python_transport/wirepas_gateway/dbus/sink_manager.py +++ b/python_transport/wirepas_gateway/dbus/sink_manager.py @@ -499,6 +499,35 @@ def upload_scratchpad(self, seq, file): return ret + def clear_local_scratchpad(self): + ret = wmm.GatewayResultCode.GW_RES_OK + restart = False + try: + # Stop the stack if not already stopped + if self.proxy.StackStatus == 0: + self.proxy.SetStackState(False) + restart = True + except GLib.Error: + logging.error("Sink in invalid state") + return wmm.GatewayResultCode.GW_RES_INVALID_SINK_STATE + + try: + self.proxy.ClearLocalScratchpad() + logging.info("Scratchpad cleared on sink %s", self.sink_id) + except GLib.Error as e: + ret = ReturnCode.error_from_dbus_exception(str(e)) + logging.error("Cannot clear local scratchpad: %s", ret.name) + + if restart: + try: + # Restart sink if we stopped it for this request + self.proxy.SetStackState(True) + except GLib.Error as e: + ret = ReturnCode.error_from_dbus_exception(str(e)) + logging.error("Could not restore sink's state: %s", ret.name) + + return ret + def set_target_scratchpad(self, action, target_seq, target_crc, param): ret = wmm.GatewayResultCode.GW_RES_OK diff --git a/python_transport/wirepas_gateway/transport_service.py b/python_transport/wirepas_gateway/transport_service.py index 05b7ed39..80b68650 100644 --- a/python_transport/wirepas_gateway/transport_service.py +++ b/python_transport/wirepas_gateway/transport_service.py @@ -288,8 +288,8 @@ def _set_status(self): # Publish only if something has changed if self._last_status_config is not None and \ self._last_status_config == configs: - logging.info("No new status to publish") - return + logging.info("No new status to publish") + return event_online = wmm.StatusEvent( self.gw_id, @@ -737,7 +737,10 @@ def _on_otap_upload_scratchpad_request_received(self, client, userdata, message) sink = self.sink_manager.get_sink(request.sink_id) if sink is not None: - res = sink.upload_scratchpad(request.seq, request.scratchpad) + if request.scratchpad is None: + res = sink.clear_local_scratchpad() + else: + res = sink.upload_scratchpad(request.seq, request.scratchpad) else: res = wmm.GatewayResultCode.GW_RES_INVALID_SINK_ID diff --git a/sink_service/source/otap.c b/sink_service/source/otap.c index fcea9493..e7e366fc 100644 --- a/sink_service/source/otap.c +++ b/sink_service/source/otap.c @@ -113,6 +113,37 @@ static int upload_scratchpad(sd_bus_message * m, void * userdata, sd_bus_error * return sd_bus_reply_method_return(m, ""); } +/** + * \brief Clear local scratchpad + * \param ... (from sd_bus function signature) + */ +static int clear_local_scratchpad(sd_bus_message * m, void * userdata, sd_bus_error * error) +{ + LOGD("Will clear local scratchpad\n"); + + const app_res_e res = WPC_clear_local_scratchpad(); + if (res != APP_RES_OK) + { + LOGE("Cannot clear local scratchpad\n"); + SET_WPC_ERROR(error, "WPC_clear_local_scratchpad", res); + return -EINVAL; + } + + /* Scratchpad cleared, update parameters values exposed on bus */ + initialize_unmodifiable_variables(); + + LOGD("Stored status:%08X, stored type:%08X\n", m_sink_otap.stored_status, m_sink_otap.stored_type); + + /* Do some sanity check: Do not generate error for that */ + if (m_sink_otap.stored_len != 0) + { + LOGE("Scratchpad is not cleared correctly (size not zero) %d", m_sink_otap.stored_len); + } + + /* Reply with the response */ + return sd_bus_reply_method_return(m, ""); +} + /** * \brief Update local scratchpad * \param ... (from sd_bus function signature) @@ -245,6 +276,7 @@ static const sd_bus_vtable otap_vtable[] = * ay -> byte array containing the scratchpad to upload */ SD_BUS_METHOD("UploadScratchpad","yay", "", upload_scratchpad, SD_BUS_VTABLE_UNPRIVILEGED), + SD_BUS_METHOD("ClearLocalScratchpad", "", "", clear_local_scratchpad, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("SetTargetScratchpad","yqyy", "b", set_target_scratchpad, SD_BUS_VTABLE_UNPRIVILEGED), SD_BUS_METHOD("GetTargetScratchpad", "", "yqyy", get_target_scratchpad, SD_BUS_VTABLE_UNPRIVILEGED),