Skip to content

Commit

Permalink
examples/lte_hibernation: Change handling of context data buffer
Browse files Browse the repository at this point in the history
Changed handling of context data buffer so that the sample code
does not need to be changed even if the buffer size of context
data is increased.
  • Loading branch information
SPRESENSE committed Nov 29, 2024
1 parent 74777c6 commit 38168ca
Show file tree
Hide file tree
Showing 4 changed files with 188 additions and 80 deletions.
98 changes: 76 additions & 22 deletions examples/lte_hibernation/lte_hibernation_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ static char g_app_iobuffer[APP_IOBUFFER_LEN];
* Private Functions
****************************************************************************/

/****************************************************************************
* Name: get_file_size
*
* Description:
* Get the size of the specified file.
****************************************************************************/

static int get_file_size(char *filename)
{
struct stat tmp;

if (stat(filename, &tmp) == 0)
{
return tmp.st_size;
}

return -1;
}

/****************************************************************************
* Name: set_rtc_alarm
*
Expand Down Expand Up @@ -226,11 +245,11 @@ static int lte_enter_hibernation(void)

static int lte_resume_from_hibernation(void)
{
struct stat tmp;
uint8_t data[512] = {0};
int size = 0;
int ctx_size;
int ret = 0;
int fd = 0;
uint8_t data[64];

ret = lte_initialize();
if (ret < 0 && ret != -EALREADY)
Expand All @@ -239,22 +258,50 @@ static int lte_resume_from_hibernation(void)
return -1;
}

if (stat(LTE_HIBERNATION_CONTEXT_PATH, &tmp) == 0)
size = get_file_size(LTE_HIBERNATION_CONTEXT_PATH);
if (size > 0)
{
fd = open(LTE_HIBERNATION_CONTEXT_PATH, O_RDONLY);
size = read(fd, data, 512);
close(fd);
/* Get the context size required for resume. */

ctx_size = lte_hibernation_resume(NULL, 0);
if (ctx_size != size)
{
printf("File size does not match."
" file size = %d, context size = %d\n",
size, ctx_size);
ret = -1;
}
else
{
fd = open(LTE_HIBERNATION_CONTEXT_PATH, O_RDONLY);

do
{
size = read(fd, data, sizeof(data));
ret = lte_hibernation_resume(data, size);
if (ret < 0)
{
printf("lte_hibernation_resume failed.\n");
}
else
{
printf("remain size = %d\n", ret);
}
}
while (ret > 0);

close(fd);
}

unlink(LTE_HIBERNATION_CONTEXT_PATH);
}
else
{
return -1;
ret = -1;
}

ret = lte_hibernation_resume(data, size);
if (ret < 0)
if (ret != 0)
{
printf("lte_hibernation_resume failed.\n");
lte_finalize();
return -1;
}
Expand Down Expand Up @@ -484,22 +531,29 @@ int main(int argc, FAR char *argv[])
printf("Resume from LTE hibernation mode.\n");

ret = lte_resume_from_hibernation();
if (ret < 0)
if (ret == 0)
{
return -1;
/* Run wget */

ret = perform_wget();
if (ret < 0)
{
return -1;
}

printf("End of this sample\n");
return 0;
}
}
else
{
/* Normal boot */

printf("Turn On LTE.\n");
/* Normal boot */

ret = app_connect_to_lte(&apnsetting);
if (ret < 0)
{
return -1;
}
printf("Turn On LTE.\n");

ret = app_connect_to_lte(&apnsetting);
if (ret < 0)
{
return -1;
}

/* Run wget */
Expand Down Expand Up @@ -531,6 +585,6 @@ int main(int argc, FAR char *argv[])
/* Entering cold sleep */

boardctl(BOARDIOC_POWEROFF, 1);

return 0;
}
16 changes: 16 additions & 0 deletions examples/lte_hibernation_wake_socket/file_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,19 @@ bool app_file_exist(char *filename)

return false;
}

/****************************************************************************
* Name: app_file_size
****************************************************************************/

int app_file_size(char *filename)
{
struct stat tmp;

if (stat(filename, &tmp) == 0)
{
return tmp.st_size;
}

return 0;
}
1 change: 1 addition & 0 deletions examples/lte_hibernation_wake_socket/file_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,5 +43,6 @@
int app_save_file(FAR char *filename, FAR uint8_t *data, int size);
int app_read_file(FAR char *filename, FAR uint8_t *data, int size);
bool app_file_exist(char *filename);
int app_file_size(char *filename);

#endif /* __EXAMPLES_LTE_HIBERNATION_WAKE_SOCKET_FILE_UTILS_H */
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,10 @@ static int lte_enter_hibernation(void)
static int lte_resume_from_hibernation(void)
{
int size = 0;
int ctx_size;
int ret = 0;
int fd = 0;
uint8_t data[64];

if (strncmp(SD_MOUNT_POINT, SAVE_DIR, strlen(SD_MOUNT_POINT)) == 0)
{
Expand All @@ -706,23 +709,55 @@ static int lte_resume_from_hibernation(void)
return -1;
}

size = app_read_file(LTE_HIBERNATION_CONTEXT_PATH,
g_iobuffer, sizeof(g_iobuffer));
if (size < 0)
size = app_file_size(LTE_HIBERNATION_CONTEXT_PATH);
if (size > 0)
{
lte_finalize();
return -1;
/* Get the context size required for resume. */

ctx_size = lte_hibernation_resume(NULL, 0);
if (ctx_size != size)
{
printf("File size does not match."
" file size = %d, context size = %d\n",
size, ctx_size);
ret = -1;
}
else
{
fd = open(LTE_HIBERNATION_CONTEXT_PATH, O_RDONLY);

do
{
size = read(fd, data, sizeof(data));
ret = lte_hibernation_resume(data, size);
if (ret < 0)
{
printf("lte_hibernation_resume failed.\n");
}
else
{
printf("remain size = %d\n", ret);
}
}
while (ret > 0);

close(fd);
}

unlink(LTE_HIBERNATION_CONTEXT_PATH);
}
else
{
ret = -1;
}

ret = lte_hibernation_resume(g_iobuffer, size);
if (ret < 0)
if (ret != 0)
{
printf("lte_hibernation_resume failed.\n");
lte_finalize();
return -1;
}

return ret;
return 0;
}

/****************************************************************************
Expand Down Expand Up @@ -896,7 +931,7 @@ int main(int argc, FAR char *argv[])
ret = lte_resume_from_hibernation();
if (ret < 0)
{
return -1;
goto normal_boot;
}

/* Find out if there is a file that stores the context of the secure
Expand Down Expand Up @@ -1001,71 +1036,73 @@ int main(int argc, FAR char *argv[])

printf("End of this sample\n");
}

return 0;
}
else
{
/* Normal boot */

printf("Turn On LTE.\n");
normal_boot:

/* ------------------------
* 1. Connect LTE network
* ------------------------
*/
/* Normal boot */

ret = app_connect_to_lte(&apnsetting);
if (ret < 0)
{
return -1;
}
printf("Turn On LTE.\n");

/* --------------------------------------
* 2. send HTTP request (secure socket)
* --------------------------------------
*/
/* ------------------------
* 1. Connect LTE network
* ------------------------
*/

/* HTTP requests are sent using secure socket. HTTP response will
* arrive 10 seconds later. In the meantime, Spresense sleeps and
* waits for the HTTP response, and wakes up when triggered by
* the HTTP response.
*/
ret = app_connect_to_lte(&apnsetting);
if (ret < 0)
{
return -1;
}

ret = send_https_request(&g_tls_context, APP_HTTPS_WGET_URL);
if (ret < 0)
{
return -1;
}
/* --------------------------------------
* 2. send HTTP request (secure socket)
* --------------------------------------
*/

/* Save to file the secure socket context needed to receive a HTTP
* response after resuming from sleep.
*/
/* HTTP requests are sent using secure socket. HTTP response will
* arrive 10 seconds later. In the meantime, Spresense sleeps and
* waits for the HTTP response, and wakes up when triggered by
* the HTTP response.
*/

ret = save_tls_contexts(&g_tls_context);
if (ret < 0)
{
return -1;
}
ret = send_https_request(&g_tls_context, APP_HTTPS_WGET_URL);
if (ret < 0)
{
return -1;
}

/* ------------------------------------------------------
* 3. spresense cold sleep (Keep the connection to LTE)
* ------------------------------------------------------
*/
/* Save to file the secure socket context needed to receive a HTTP
* response after resuming from sleep.
*/

/* LTE module entering hibernation mode */
ret = save_tls_contexts(&g_tls_context);
if (ret < 0)
{
return -1;
}

printf("Entering LTE hibernation mode.\n");
/* ------------------------------------------------------
* 3. spresense cold sleep (Keep the connection to LTE)
* ------------------------------------------------------
*/

ret = lte_enter_hibernation();
if (ret < 0)
{
return -1;
}
/* LTE module entering hibernation mode */

/* Entering cold sleep */
printf("Entering LTE hibernation mode.\n");

boardctl(BOARDIOC_POWEROFF, 1);
ret = lte_enter_hibernation();
if (ret < 0)
{
return -1;
}

/* Entering cold sleep */

boardctl(BOARDIOC_POWEROFF, 1);

return 0;
}

0 comments on commit 38168ca

Please sign in to comment.