Skip to content

Commit

Permalink
Target scratchpad: add support for read/write
Browse files Browse the repository at this point in the history
  • Loading branch information
GwendalRaoul committed Feb 1, 2021
1 parent 6ad4b34 commit ce17a47
Show file tree
Hide file tree
Showing 7 changed files with 248 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/api/wpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -728,6 +728,40 @@ app_res_e WPC_clear_local_scratchpad();
*/
app_res_e WPC_update_local_scratchpad();

/**
* \brief Request to write target scratchpad and action
* \param target_sequence
* The target sequence to set
* \param target_crc
* The target crc to set
* \param action
* The action to perform with this scratchpad
* \param param
* Parameter for the associated action (if relevant)
* \return Return code of the operation
*/
app_res_e WPC_write_target_scratchpad(uint8_t target_sequence,
uint16_t target_crc,
uint8_t action,
uint8_t param);

/**
* \brief Request to read target scratchpad and action
* \param target_sequence_p
* Pointer to store the target sequence to set
* \param target_crc_p
* Pointer to store the crc to set
* \param action_p
* Pointer to store the action to perform with this scratchpad
* \param param_p
* Pointer to store the parameter for the associated action (if relevant)
* \return Return code of the operation
*/
app_res_e WPC_read_target_scratchpad(uint8_t * target_sequence_p,
uint16_t * target_crc_p,
uint8_t * action_p,
uint8_t * param_p);

/**
* \brief Query scratchpad status for a remote node
* \param destination_address
Expand Down
53 changes: 53 additions & 0 deletions lib/wpc/include/msap.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ typedef struct __attribute__((__packed__))
uint8_t bytes[MAXIMUM_SCRATCHPAD_BLOCK_SIZE];
} msap_image_block_req_pl_t;

typedef struct __attribute__((__packed__))
{
uint8_t target_sequence;
uint16_t target_crc;
uint8_t action;
uint8_t param;
} msap_scratchpad_write_req_pl_t;

typedef struct __attribute__((__packed__))
{
uint32_t target;
Expand Down Expand Up @@ -171,6 +179,15 @@ typedef struct __attribute__((__packed__))
uint8_t firmware_dev_ver;
} msap_scratchpad_status_conf_pl_t;

typedef struct __attribute__((__packed__))
{
uint8_t result;
uint8_t target_sequence;
uint16_t target_crc;
uint8_t action;
uint8_t param;
} msap_scratchpad_read_conf_pl_t;

typedef struct __attribute__((__packed__))
{
uint8_t indication_status;
Expand Down Expand Up @@ -363,6 +380,42 @@ int msap_scratchpad_update_request();
*/
int msap_scratchpad_clear_request();

/**
* \brief Request to write target scratchpad and action
* \param target_sequence
* The target sequence to set
* \param target_crc
* The target crc to set
* \param action
* The action to perform with this scratchpad
* \param param
* Parameter for the associated action (if relevant)
* \return negative value if the request fails,
* a Mesh positive result otherwise
*/
int msap_scratchpad_target_write_request(uint8_t target_sequence,
uint16_t target_crc,
uint8_t action,
uint8_t param);

/**
* \brief Request to read target scratchpad and action
* \param target_sequence_p
* Pointer to store the target sequence to set
* \param target_crc_p
* Pointer to store the crc to set
* \param action_p
* Pointer to store the action to perform with this scratchpad
* \param param_p
* Pointer to store the parameter for the associated action (if relevant)
* \return negative value if the request fails,
* a Mesh positive result otherwise
*/
int msap_scratchpad_target_read_request(uint8_t * target_sequence_p,
uint16_t * target_crc_p,
uint8_t * action_p,
uint8_t * param_p);

/**
* \brief Get status of a remote node scratchpad status
* \param destination_address
Expand Down
4 changes: 4 additions & 0 deletions lib/wpc/include/wpc_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@
#define MSAP_SINK_COST_WRITE_CONFIRM (SAP_CONFIRM_OFFSET + MSAP_SINK_COST_WRITE_REQUEST)
#define MSAP_SINK_COST_READ_REQUEST 0x39
#define MSAP_SINK_COST_READ_CONFIRM (SAP_CONFIRM_OFFSET + MSAP_SINK_COST_READ_REQUEST)
#define MSAP_SCRATCH_TARGET_WRITE_REQUEST 0x26
#define MSAP_SCRATCH_TARGET_WRITE_CONFIRM (SAP_CONFIRM_OFFSET + MSAP_SCRATCH_TARGET_WRITE_REQUEST)
#define MSAP_SCRATCH_TARGET_READ_REQUEST 0x27
#define MSAP_SCRATCH_TARGET_READ_CONFIRM (SAP_CONFIRM_OFFSET + MSAP_SCRATCH_TARGET_READ_REQUEST)

/*
* Configuration Service Access Points
Expand Down
2 changes: 2 additions & 0 deletions lib/wpc/include/wpc_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ typedef struct __attribute__((__packed__))
attribute_write_req_pl_t attribute_write_request_payload;
attribute_read_req_pl_t attribute_read_request_payload;
csap_factory_reset_req_pl_t csap_factory_reset_request_payload;
msap_scratchpad_write_req_pl_t msap_scratchpad_target_write_request_payload;
// Indication
dsap_data_tx_ind_pl_t dsap_data_tx_indication_payload;
dsap_data_rx_ind_pl_t dsap_data_rx_indication_payload;
Expand All @@ -80,6 +81,7 @@ typedef struct __attribute__((__packed__))
msap_get_nbors_conf_pl_t msap_get_nbors_confirm_payload;
msap_scratchpad_status_conf_pl_t msap_scratchpad_status_confirm_payload;
attribute_read_conf_pl_t attribute_read_confirm_payload;
msap_scratchpad_read_conf_pl_t msap_scratchpad_target_read_confirm_payload;
// Response
sap_resp_pl_t sap_response_payload;
} payload;
Expand Down
65 changes: 65 additions & 0 deletions lib/wpc/msap.c
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,71 @@ int msap_scratchpad_clear_request()
return confirm.payload.sap_generic_confirm_payload.result;
}

int msap_scratchpad_target_write_request(uint8_t target_sequence,
uint16_t target_crc,
uint8_t action,
uint8_t param)
{
wpc_frame_t request, confirm;
int res;

request.primitive_id = MSAP_SCRATCH_TARGET_WRITE_REQUEST;
request.payload.msap_scratchpad_target_write_request_payload.target_sequence = target_sequence;
request.payload.msap_scratchpad_target_write_request_payload.target_crc = target_crc;
request.payload.msap_scratchpad_target_write_request_payload.action = action;
request.payload.msap_scratchpad_target_write_request_payload.param = param;

request.payload_length = sizeof(msap_scratchpad_write_req_pl_t);

res = WPC_Int_send_request(&request, &confirm);

if (res < 0)
return res;

LOGD(" Target scratchpad request result = 0x%02x\n",
confirm.payload.sap_generic_confirm_payload.result);
return confirm.payload.sap_generic_confirm_payload.result;
}


int msap_scratchpad_target_read_request(uint8_t * target_sequence_p,
uint16_t * target_crc_p,
uint8_t * action_p,
uint8_t * param_p)
{
wpc_frame_t request, confirm;
uint8_t result;
int res;

request.primitive_id = MSAP_SCRATCH_TARGET_READ_REQUEST;
request.payload_length = 0;

res = WPC_Int_send_request(&request, &confirm);

if (res < 0)
return res;

result = confirm.payload.msap_scratchpad_target_read_confirm_payload.result;
if (result == 0)
{
*target_sequence_p = confirm.payload.msap_scratchpad_target_read_confirm_payload.target_sequence;
*target_crc_p = confirm.payload.msap_scratchpad_target_read_confirm_payload.target_crc;
*action_p = confirm.payload.msap_scratchpad_target_read_confirm_payload.action;
*param_p = confirm.payload.msap_scratchpad_target_read_confirm_payload.param;
LOGD("Target scratchpad Read : seq=%d crc=0x%x action=%d param=%d\n",
*target_sequence_p,
*target_crc_p,
*action_p,
*param_p);
}
else
{
LOGE("Try to read target scratchpad but it failed %d\n", result);
}

return result;
}

int msap_scratchpad_remote_status(app_addr_t destination_address)
{
wpc_frame_t request, confirm;
Expand Down
37 changes: 37 additions & 0 deletions lib/wpc/wpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,43 @@ app_res_e WPC_remote_scratchpad_update(app_addr_t destination_address, uint8_t s
return convert_error_code(SCRATCHPAD_UPDATE_REMOTE_ERROR_CODE_LUT, res);
}

/* Error code LUT for target scratchpad write */
static const app_res_e TARGET_SCRATCHPAD_ERROR_CODE_LUT[] = {
APP_RES_OK, // 0
APP_RES_NODE_NOT_A_SINK, // 1
APP_RES_INVALID_VALUE, // 2
APP_RES_ACCESS_DENIED // 3
};

app_res_e WPC_write_target_scratchpad(uint8_t target_sequence,
uint16_t target_crc,
uint8_t action,
uint8_t param)
{
int res = msap_scratchpad_target_write_request(
target_sequence,
target_crc,
action,
param);

return convert_error_code(TARGET_SCRATCHPAD_ERROR_CODE_LUT, res);
}

app_res_e WPC_read_target_scratchpad(uint8_t * target_sequence_p,
uint16_t * target_crc_p,
uint8_t * action_p,
uint8_t * param_p)
{
int res = msap_scratchpad_target_read_request(
target_sequence_p,
target_crc_p,
action_p,
param_p);

// We should always be able to read it if implemented
return res == 0 ? APP_RES_OK : APP_RES_INTERNAL_ERROR;
}

/* Error code LUT for scan neighbors */
static const app_res_e SCAN_NEIGHBORS_ERROR_CODE_LUT[] = {
APP_RES_OK, // 0
Expand Down
53 changes: 53 additions & 0 deletions test/test.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,57 @@ static bool testAuthenticationKey()
return true;
}

static bool testScratchpadTarget()
{
uint8_t target_seq;
uint16_t target_crc;
uint8_t action;
uint8_t param;

// Read it a first time
if (WPC_read_target_scratchpad(&target_seq,
&target_crc,
&action,
&param) != APP_RES_OK)
{
LOGE("Cannot read target scratchpad\n");
return false;
}

LOGI("Target is: %d, 0x%x, %d, %d\n", target_seq, target_crc, action, param);

app_res_e res = WPC_write_target_scratchpad(12,
0x1234,
2,
13);
if (res != APP_RES_OK)
{
LOGE("Cannot write target scratchpad %d\n", res);
return false;
}

LOGI("Write new target\n");

if (WPC_read_target_scratchpad(&target_seq,
&target_crc,
&action,
&param))
{
LOGE("Cannot read target back scratchpad\n");
return false;
}

LOGI("Target read back is: %d, 0x%x, %d, %d\n", target_seq, target_crc, action, param);

if (target_seq != 12 || target_crc != 0x1234 || action != 2 || param != 13)
{
LOGE("Wrong read-back value\n");
return false;
}

return true;
}

void onDataSent(uint16_t pduid, uint32_t buffering_delay, uint8_t result)
{
LOGI("Indication received for %d, delay=%d, result=%d\n", pduid, buffering_delay, result);
Expand Down Expand Up @@ -789,6 +840,8 @@ int Test_runAll()

setInitialState(APP_ROLE_SINK, 1234, 0x654321, 5, false);

RUN_TEST(testScratchpadTarget, true);

RUN_TEST(dumpCSAPAttributes, true);

RUN_TEST(testAuthenticationKey, true);
Expand Down

0 comments on commit ce17a47

Please sign in to comment.