Skip to content

Commit

Permalink
Enabled logging and reverted socket connection change
Browse files Browse the repository at this point in the history
  • Loading branch information
bhnaphade committed Apr 19, 2023
1 parent f97d99f commit 1c56c2e
Show file tree
Hide file tree
Showing 11 changed files with 54 additions and 179 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ extern "C" {
****************************************************************************/

/** Define this symbol to enable compilation of debug code. When this symbol is not defined, debug code is not present and the footprint is reduced. */
//#define EWF_DEBUG
#define EWF_DEBUG

/** Define this symbol to enable verbose logging */
//#define EWF_LOG_VERBOSE
Expand Down
213 changes: 44 additions & 169 deletions src/ewf_adapter_renesas_common_internet.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* @version Preview
* @copyright Copyright (c) Microsoft Corporation. All rights reserved.
* SPDX-License-Identifier: MIT
* brief The Embedded Wireless Framework Renesas common Internet(TCP+UDP) implementation
****************************************************************************/

Expand Down Expand Up @@ -522,97 +521,6 @@ static ewf_result _ewf_adapter_renesas_common_internet_socket_send(ewf_adapter*
return EWF_RESULT_OK;
}


struct _ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state
{
ewf_interface* interface_ptr;
bool prefix_matches;
bool parsed;
uint32_t read_actual_length;
uint32_t total_expected_length;
uint8_t* data_ptr;
};

static bool _ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function(const uint8_t* buffer_ptr, uint32_t buffer_length, const ewf_tokenizer_basic_pattern* pattern_ptr, bool* stop_ptr)
{
if (!buffer_ptr) return false;
if (!buffer_length) return false;
if (!pattern_ptr) return false;
if (!stop_ptr) return false;

struct _ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state* state_ptr =
(struct _ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state*)pattern_ptr->data_ptr;

/* Define the message prefix and calculate its length */
const uint8_t prefix_str[] = "\r\n+SQNSRECV: ";
const uint32_t prefix_length = sizeof(prefix_str) - 1;

/* If the buffer is smaller than the prefix, then it is not yet for us */
if (buffer_length < prefix_length)
{
return false;
}

/* If the buffer contains as many characters as the prefix, then look if it is for us */
if (buffer_length == prefix_length)
{
if (ewfl_buffer_equals_buffer(buffer_ptr, prefix_str, prefix_length))
{
state_ptr->prefix_matches = true;
return false;
}
}

/* At this point the buffer it is longer than the prefix */

/* We did not match the prefix in previous runs, just ignore the rest of the incoming characters */
if (!state_ptr->prefix_matches)
{
return false;
}
else
{
/* This is for us, stop parsing other tokens further down the list */
*stop_ptr = true;
}

/* At this point we have a matching prefix */

/* If the message parameters were not yet parsed */
if (!state_ptr->parsed)
{
/* and we have a whole line, then parse it now */
if (buffer_ptr[buffer_length - 2] == '\r' && buffer_ptr[buffer_length - 1] == '\n')
{
/* The message is complete, try to parse it */
char* p = NULL;
p = (char *)buffer_ptr + (sizeof(prefix_str) - 1) + 2; /* skip characters until actual receive length */
state_ptr->read_actual_length = ewfl_str_to_unsigned(p);

state_ptr->total_expected_length = buffer_length + state_ptr->read_actual_length + 2 + 6;
state_ptr->data_ptr = (uint8_t*)buffer_ptr + buffer_length;

state_ptr->parsed = true;
}

return false;
}

/* From this point we parsed data */

/* Is the message complete? */
if ((buffer_length) >= state_ptr->total_expected_length)
{
/* Signal the match */
return true;
}
else
{
/* Not yet matched */
return false;
}
}

static ewf_result _ewf_adapter_renesas_common_internet_socket_receive(
ewf_adapter* adapter_ptr,
ewf_adapter_renesas_common_internet_socket* internet_socket_ptr,
Expand Down Expand Up @@ -667,7 +575,7 @@ static ewf_result _ewf_adapter_renesas_common_internet_socket_receive(
uint32_t response_length = 0;

{
uint32_t t = (wait) ? (t = (interface_ptr->default_timeout)) : 1;
uint32_t t = (wait) ? (t = (60 * EWF_PLATFORM_TICKS_PER_SECOND)) : 1;

ewf_interface_poll(interface_ptr);

Expand All @@ -680,17 +588,11 @@ static ewf_result _ewf_adapter_renesas_common_internet_socket_receive(
uint32_t unread_length = 0;
uint32_t total_sent_not_acknowledged = 0;

result = ewf_result_failed(result = ewf_interface_send_commands(interface_ptr, "AT+SQNSI=", connection_id_cstr, "\r", NULL));
if (ewf_result_failed(result))
{
return result;
}

if (ewf_result_failed(result = ewf_interface_send_commands(interface_ptr, "AT+SQNSI=", connection_id_cstr, "\r", NULL))) return result;

result = ewf_interface_receive_response(interface_ptr, &response_ptr, &response_length, 100);
result = ewf_interface_receive_response(interface_ptr, &response_ptr, &response_length, 200);
if (ewf_result_succeeded(result))
{
unread_length = 0;
const char match_str[] = "\r\n+SQNSI: ";
if (ewfl_str_starts_with((char*)response_ptr, match_str))
{
Expand Down Expand Up @@ -737,7 +639,10 @@ static ewf_result _ewf_adapter_renesas_common_internet_socket_receive(
/* Did the operation timed-out? */
if (!t)
{
if (wait) EWF_LOG_ERROR("Timeout while waiting for receive!\n");
if (wait)
{
EWF_LOG_ERROR("Timeout while waiting for receive!\n");
}
return EWF_RESULT_NO_DATA_RECEIVED;
}
}
Expand All @@ -753,92 +658,60 @@ static ewf_result _ewf_adapter_renesas_common_internet_socket_receive(
char read_length_str[8] = {0};
char* read_length_cstr = ewfl_unsigned_to_str(read_length, read_length_str, sizeof(read_length_str));

result = ewf_interface_send_commands(
interface_ptr,
"AT+SQNSRECV=",
connection_id_cstr, ",",
read_length_cstr, "\r",
NULL);
if (ewf_result_failed(result)) return result;

struct _ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state = { 0 };
ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state.interface_ptr = interface_ptr;

{
ewf_tokenizer_basic_pattern ewf_adapter_renesas_common_sqnsrev_message_tokenizer_pattern =
{
NULL,
NULL,
0,
false,
_ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function,
&ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state
};
result = ewf_interface_receive_response(interface_ptr, &response_ptr, &response_length, 500);
if (ewf_result_failed(result)) return result;

ewf_tokenizer_basic_data* tokenizer_data_ptr = (ewf_tokenizer_basic_data*)interface_ptr->tokenizer_ptr->data_ptr;
/* Terminate the string to make parsing safer and faster */
response_ptr[response_length] = 0;

ewf_tokenizer_basic_pattern* tokenizer_basic_message_pattern_saved_ptr = NULL;
ewf_tokenizer_basic_pattern* tokenizer_basic_command_response_pattern_saved_ptr = NULL;
ewf_tokenizer_basic_pattern* tokenizer_basic_command_response_end_pattern_saved_ptr = NULL;
ewf_tokenizer_basic_pattern* tokenizer_basic_urc_pattern_saved_ptr = NULL;
/* Parse the response */
{
char data_read_response_str[] = "\r\n+SQNSRECV: ";
uint32_t read_actual_length = 0;

ewf_result ewf_result_send_command = EWF_RESULT_OK;
ewf_result ewf_result_receive_response = EWF_RESULT_OK;
char* read_actual_length_str = NULL;

if (ewf_result_failed(result = ewf_tokenizer_basic_message_pattern_get(tokenizer_data_ptr, &tokenizer_basic_message_pattern_saved_ptr))) return result;
if (ewf_result_failed(result = ewf_tokenizer_basic_command_response_pattern_get(tokenizer_data_ptr, &tokenizer_basic_command_response_pattern_saved_ptr))) return result;
if (ewf_result_failed(result = ewf_tokenizer_basic_command_response_end_pattern_get(tokenizer_data_ptr, &tokenizer_basic_command_response_end_pattern_saved_ptr))) return result;
if (ewf_result_failed(result = ewf_tokenizer_basic_urc_pattern_get(tokenizer_data_ptr, &tokenizer_basic_urc_pattern_saved_ptr))) return result;
char* p = NULL;

result = ewf_tokenizer_basic_message_pattern_set(tokenizer_data_ptr, &ewf_adapter_renesas_common_sqnsrev_message_tokenizer_pattern);
ewf_tokenizer_basic_command_response_pattern_set(tokenizer_data_ptr, NULL);
ewf_tokenizer_basic_command_response_end_pattern_set(tokenizer_data_ptr, NULL);
ewf_tokenizer_basic_urc_pattern_set(tokenizer_data_ptr, NULL);
if (ewf_result_failed(result))
if (!ewfl_buffer_equals_buffer(response_ptr, (uint8_t*)data_read_response_str, sizeof(data_read_response_str) - 1))
{
return result;
result = EWF_RESULT_UNEXPECTED_RESPONSE;
}
else
{
result = ewf_interface_send_commands(
interface_ptr,
"AT+SQNSRECV=",
connection_id_cstr, ",",
read_length_cstr, "\r",
NULL);
p = (char *) (response_ptr + (sizeof(data_read_response_str) - 1) + 2) /* skip characters until actual receive length */;
read_actual_length_str = p;
read_actual_length = ewfl_str_to_unsigned(read_actual_length_str);
}

if (ewf_result_failed(ewf_result_send_command))
{
// error handled below
}
if (ewf_result_succeeded(result))
{
p = ewfl_find_chars_with_terms(p, "\r", NULL);
if (!p) result = EWF_RESULT_UNEXPECTED_RESPONSE;
else
{
ewf_result_receive_response = ewf_interface_receive_response(interface_ptr, &response_ptr, &response_length, interface_ptr->default_timeout);
if (ewf_result_failed(ewf_result_receive_response))
{
// error handled below
}
p++;
if (*p != '\n') result = EWF_RESULT_UNEXPECTED_RESPONSE;
else { p++; }
}
ewf_result result_restore_message_pattern = ewf_tokenizer_basic_message_pattern_set(tokenizer_data_ptr, tokenizer_basic_message_pattern_saved_ptr);
ewf_result result_restore_command_response_pattern = ewf_tokenizer_basic_command_response_pattern_set(tokenizer_data_ptr, tokenizer_basic_command_response_pattern_saved_ptr);
ewf_result result_restore_command_response_end_pattern = ewf_tokenizer_basic_command_response_end_pattern_set(tokenizer_data_ptr, tokenizer_basic_command_response_end_pattern_saved_ptr);
ewf_result result_restore_urc_pattern = ewf_tokenizer_basic_urc_pattern_set(tokenizer_data_ptr, tokenizer_basic_urc_pattern_saved_ptr);

if (ewf_result_failed(result_restore_message_pattern)) return result_restore_message_pattern;
if (ewf_result_failed(result_restore_command_response_pattern)) return result_restore_command_response_pattern;
if (ewf_result_failed(result_restore_command_response_end_pattern)) return result_restore_command_response_end_pattern;
if (ewf_result_failed(result_restore_urc_pattern)) return result_restore_urc_pattern;
}

if (ewf_result_failed(ewf_result_send_command))
if (ewf_result_succeeded(result))
{
return ewf_result_send_command;
}

if (ewf_result_failed(ewf_result_receive_response))
{
return ewf_result_receive_response;
*buffer_length_ptr = (*buffer_length_ptr >= read_actual_length) ? read_actual_length : *buffer_length_ptr;
memcpy(buffer_ptr, p, *buffer_length_ptr);
}
}

/* Copy the buffer */
memcpy(buffer_ptr, ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state.data_ptr, ewf_adapter_renesas_common_sqnsrecv_message_tokenizer_pattern_match_function_state.read_actual_length);

/* Release the buffer */
result = ewf_interface_release(interface_ptr, response_ptr);
ewf_interface_release(interface_ptr, response_ptr);

if (ewf_result_failed(result))
{
Expand All @@ -848,7 +721,9 @@ static ewf_result _ewf_adapter_renesas_common_internet_socket_receive(

#ifdef EWF_DEBUG
if (ewf_result_failed(result = ewf_interface_send_commands(interface_ptr, "AT+SQNSI=", connection_id_cstr, "\r", NULL))) return result;
if (ewf_result_failed(result = ewf_interface_drop_response(interface_ptr))) return result;
result = ewf_interface_receive_response(interface_ptr, &response_ptr, &response_length, 200);
if (ewf_result_failed(result)) return result;
ewf_interface_release(interface_ptr, response_ptr);
#endif

return result;
Expand Down

0 comments on commit 1c56c2e

Please sign in to comment.