Skip to content

Commit

Permalink
Enable clearing of sink scratchpad
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
sgunes-wirepas authored Sep 19, 2024
1 parent e17d41c commit a6c906c
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 3 deletions.
29 changes: 29 additions & 0 deletions python_transport/wirepas_gateway/dbus/sink_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 6 additions & 3 deletions python_transport/wirepas_gateway/transport_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand Down
32 changes: 32 additions & 0 deletions sink_service/source/otap.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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),

Expand Down

0 comments on commit a6c906c

Please sign in to comment.