Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proto handler: move the serialization to main handler #76

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion example/linux/proto-api/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ int main(int argc, char * argv[])
}
else
{
LOGE("Cannot handle request: %d", res);
LOGE("Cannot handle request: %d\n", res);
}


Expand Down
98 changes: 52 additions & 46 deletions lib/wpc_proto/wpc_proto.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,9 @@ app_proto_res_e WPC_Proto_register_for_data_rx_event(onDataRxEvent_cb_f onDataRx
}

static app_proto_res_e handle_send_data_request(wp_SendPacketReq *req,
uint8_t * response_p,
size_t * response_size_p)
wp_SendPacketResp *resp)
{
bool status;
app_res_e res;
wp_GenericMessage message = wp_GenericMessage_init_zero;
wp_WirepasMessage message_wirepas = wp_WirepasMessage_init_zero;
wp_SendPacketResp sendResp;
pb_ostream_t stream;
message.wirepas = &message_wirepas;

// TODO: Add some sanity checks
res = WPC_send_data(req->payload.bytes,
Expand All @@ -208,34 +201,18 @@ static app_proto_res_e handle_send_data_request(wp_SendPacketReq *req,
LOGI("WPC_send_data res=%d\n", res);

// Fill the response
sendResp.header = (wp_ResponseHeader) {
resp->header = (wp_ResponseHeader) {
.req_id = req->header.req_id,
.has_sink_id = true,
.res = res, //TODO make a conversion!!!
.has_time_ms_epoch = true,
.time_ms_epoch = Platform_get_timestamp_ms_epoch(),
};

strcpy(sendResp.header.gw_id, m_gateway_id);
strcpy(sendResp.header.sink_id, m_sink_id);

message.wirepas->send_packet_resp = &sendResp;

stream = pb_ostream_from_buffer(response_p, *response_size_p);
strcpy(resp->header.gw_id, m_gateway_id);
strcpy(resp->header.sink_id, m_sink_id);

/* Now we are ready to encode the message! */
status = pb_encode(&stream, wp_GenericMessage_fields, &message);

if (!status) {
LOGE("Encoding failed: %s\n", PB_GET_ERROR(&stream));
return APP_RES_CANNOT_GENERATE_RESPONSE;
}
else
{
LOGI("Response generated %d\n", stream.bytes_written);
*response_size_p = stream.bytes_written;
return APP_RES_PROTO_OK;
}
return APP_RES_PROTO_OK;
}

app_proto_res_e WPC_Proto_handle_request(const uint8_t * request_p,
Expand All @@ -244,56 +221,65 @@ app_proto_res_e WPC_Proto_handle_request(const uint8_t * request_p,
size_t * response_size_p)
{
bool status;
app_proto_res_e res = APP_RES_PROTO_INVALID_REQUEST;
/* All messages are generic messages */
wp_GenericMessage message = wp_GenericMessage_init_zero;
wp_WirepasMessage * wp_message_p = NULL;
pb_istream_t stream = pb_istream_from_buffer(request_p, request_size);
wp_GenericMessage message_req = wp_GenericMessage_init_zero;
wp_WirepasMessage * wp_message_req_p = NULL;
pb_istream_t stream_in = pb_istream_from_buffer(request_p, request_size);
pb_ostream_t stream_out;

status = pb_decode(&stream, wp_GenericMessage_fields, &message);

// Prepare response
wp_GenericMessage message_resp = wp_GenericMessage_init_zero;
wp_WirepasMessage message_wirepas_resp = wp_WirepasMessage_init_zero;
message_resp.wirepas = &message_wirepas_resp;

status = pb_decode(&stream_in, wp_GenericMessage_fields, &message_req);

if (!status)
{
LOGE("Decoding failed: %s\n", PB_GET_ERROR(&stream));
LOGE("Decoding failed: %s\n", PB_GET_ERROR(&stream_in));
return APP_RES_PROTO_INVALID_REQUEST;
}

wp_message_p = message.wirepas;
wp_message_req_p = message_req.wirepas;
/* Do some sanity check on request header */
if (!wp_message_p)
if (!wp_message_req_p)
{
LOGW("Not a wirepas message\n");
return APP_RES_PROTO_INVALID_REQUEST;
}

/* Call the right request handler */
if (wp_message_p->get_configs_req)
if (wp_message_req_p->get_configs_req)
{
LOGI("Get config request\n");
}
else if (wp_message_p->set_config_req)
else if (wp_message_req_p->set_config_req)
{
LOGI("Set config request\n");
}
else if (wp_message_p->send_packet_req)
else if (wp_message_req_p->send_packet_req)
{
wp_SendPacketResp sendResp;
LOGI("Send packet request\n");
return handle_send_data_request(wp_message_p->send_packet_req,
response_p,
response_size_p);
message_resp.wirepas->send_packet_resp = &sendResp;
res = handle_send_data_request(wp_message_req_p->send_packet_req,
&sendResp);
}
else if (wp_message_p->get_scratchpad_status_req)
else if (wp_message_req_p->get_scratchpad_status_req)
{
LOGI("Get scratchpad status request\n");
}
else if (wp_message_p->upload_scratchpad_req)
else if (wp_message_req_p->upload_scratchpad_req)
{
LOGI("Upload scratchpad request\n");
}
else if (wp_message_p->process_scratchpad_req)
else if (wp_message_req_p->process_scratchpad_req)
{
LOGI("Process scratchpad request\n");
}
else if (wp_message_p->get_gateway_info_req)
else if (wp_message_req_p->get_gateway_info_req)
{
LOGI("Get gateway info request\n");
}
Expand All @@ -302,7 +288,27 @@ app_proto_res_e WPC_Proto_handle_request(const uint8_t * request_p,
LOGE("Not a supported request\n");
}

if (res == APP_RES_PROTO_OK)
{
//Serialize the answer
stream_out = pb_ostream_from_buffer(response_p, *response_size_p);

/* Now we are ready to encode the message! */
status = pb_encode(&stream_out, wp_GenericMessage_fields, &message_resp);

if (!status) {
LOGE("Encoding failed: %s\n", PB_GET_ERROR(&stream_out));
return APP_RES_CANNOT_GENERATE_RESPONSE;
}
else
{
LOGI("Response generated %d\n", stream_out.bytes_written);
*response_size_p = stream_out.bytes_written;
return APP_RES_PROTO_OK;
}
}

/** Set correctly the response_p and response_size */
return APP_RES_PROTO_OK;
return res;

}