Skip to content

Commit

Permalink
Remove the send timeout in network I/O and remove some platform headers
Browse files Browse the repository at this point in the history
  • Loading branch information
williamlai committed Sep 21, 2022
1 parent bcd3532 commit a4bf2da
Show file tree
Hide file tree
Showing 6 changed files with 4 additions and 98 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# AWS Polly C

AWS Polly C is a lightweight library of AWS Plly service for FreeRTOS/Embedded Linux.
AWS Polly C is a lightweight library of AWS Polly service for FreeRTOS/Embedded Linux. This library doesn't provide full features of AWS Polly but only supports some key features of it.

## What is AWS Polly?

Expand Down
1 change: 0 additions & 1 deletion samples/polly_mp3_download/polly_mp3_download.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,6 @@ static int prvInitPollyServiceParameter(PollyServiceParameter_t *pServPara)
pServPara->pService = AWS_POLLY_SERVICE_NAME;
snprintf(pPollyHostName, MAX_HOST_NAME_LEN, "%s.%s.amazonaws.com", AWS_POLLY_SERVICE_NAME, pServPara->pRegion);
pServPara->pHost = pPollyHostName;
pServPara->uSendTimeoutMs = 1000;
pServPara->uRecvTimeoutMs = 1000;

return 0;
Expand Down
1 change: 0 additions & 1 deletion src/include/polly/polly.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ typedef struct
const char *pHost;

unsigned int uRecvTimeoutMs;
unsigned int uSendTimeoutMs;
} PollyServiceParameter_t;

typedef struct
Expand Down
77 changes: 2 additions & 75 deletions src/source/netio.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@
#include <string.h>
#include <stdbool.h>

#include <sys/time.h>
#include <sys/socket.h>

/* Third party headers */
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/entropy.h"
Expand All @@ -48,7 +45,6 @@ typedef struct NetIo

/* Options */
uint32_t uRecvTimeoutMs;
uint32_t uSendTimeoutMs;
} NetIo_t;

static int prvCreateX509Cert(NetIo_t *pxNet)
Expand Down Expand Up @@ -96,7 +92,6 @@ static int prvInitConfig(NetIo_t *pxNet, const char *pcRootCA, const char *pcCer
{
mbedtls_ssl_conf_rng(&(pxNet->xConf), mbedtls_ctr_drbg_random, &(pxNet->xCtrDrbg));
mbedtls_ssl_conf_read_timeout(&(pxNet->xConf), pxNet->uRecvTimeoutMs);
NetIo_setSendTimeout(pxNet, pxNet->uSendTimeoutMs);

if (pcRootCA != NULL && pcCert != NULL && pcPrivKey != NULL)
{
Expand All @@ -119,7 +114,7 @@ static int prvInitConfig(NetIo_t *pxNet, const char *pcRootCA, const char *pcCer
}
else
{
mbedtls_ssl_conf_authmode(&(pxNet->xConf), MBEDTLS_SSL_VERIFY_OPTIONAL);
mbedtls_ssl_conf_authmode(&(pxNet->xConf), MBEDTLS_SSL_VERIFY_NONE);
}
}
}
Expand Down Expand Up @@ -195,7 +190,6 @@ NetIoHandle NetIo_create(void)
mbedtls_entropy_init(&(pxNet->xEntropy));

pxNet->uRecvTimeoutMs = DEFAULT_CONNECTION_TIMEOUT_MS;
pxNet->uSendTimeoutMs = DEFAULT_CONNECTION_TIMEOUT_MS;

if (mbedtls_ctr_drbg_seed(&(pxNet->xCtrDrbg), mbedtls_entropy_func, &(pxNet->xEntropy), NULL, 0) != 0)
{
Expand Down Expand Up @@ -328,38 +322,6 @@ int NetIo_recv(NetIoHandle xNetIoHandle, unsigned char *pBuffer, size_t uBufferS
return res;
}

bool NetIo_isDataAvailable(NetIoHandle xNetIoHandle)
{
NetIo_t *pxNet = (NetIo_t *)xNetIoHandle;
bool bDataAvailable = false;
struct timeval tv = {0};
fd_set read_fds = {0};
int fd = 0;

if (pxNet != NULL)
{
fd = pxNet->xFd.fd;
if (fd >= 0)
{
FD_ZERO(&read_fds);
FD_SET(fd, &read_fds);

tv.tv_sec = 0;
tv.tv_usec = 0;

if (select(fd + 1, &read_fds, NULL, NULL, &tv) >= 0)
{
if (FD_ISSET(fd, &read_fds))
{
bDataAvailable = true;
}
}
}
}

return bDataAvailable;
}

int NetIo_setRecvTimeout(NetIoHandle xNetIoHandle, unsigned int uRecvTimeoutMs)
{
int res = NETIO_ERRNO_NONE;
Expand All @@ -376,39 +338,4 @@ int NetIo_setRecvTimeout(NetIoHandle xNetIoHandle, unsigned int uRecvTimeoutMs)
}

return res;
}

int NetIo_setSendTimeout(NetIoHandle xNetIoHandle, unsigned int uSendTimeoutMs)
{
int res = NETIO_ERRNO_NONE;
NetIo_t *pxNet = (NetIo_t *)xNetIoHandle;
int fd = 0;
struct timeval tv = {0};

if (pxNet == NULL)
{
res = NETIO_ERRNO_INVALID_PARAMETER;
}
else
{
pxNet->uSendTimeoutMs = (uint32_t)uSendTimeoutMs;
fd = pxNet->xFd.fd;
tv.tv_sec = uSendTimeoutMs / 1000;
tv.tv_usec = (uSendTimeoutMs % 1000) * 1000;

if (fd < 0)
{
/* Do nothing when connection hasn't established. */
}
else if (setsockopt(fd, SOL_SOCKET, SO_SNDTIMEO, (void *)&tv, sizeof(tv)) != 0)
{
res = NETIO_ERRNO_UNABLE_TO_SET_SEND_TIMEOUT;
}
else
{
/* nop */
}
}

return res;
}
}
17 changes: 0 additions & 17 deletions src/source/netio.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,6 @@ int NetIo_send(NetIoHandle xNetIoHandle, const unsigned char *pBuffer, size_t uB
*/
int NetIo_recv(NetIoHandle xNetIoHandle, unsigned char *pBuffer, size_t uBufferSize, size_t *puBytesReceived);

/**
* @brief Check if any data available
*
* @param xNetIoHandle The network I/O handle
* @return true if data available, false otherwise
*/
bool NetIo_isDataAvailable(NetIoHandle xNetIoHandle);

/**
* @brief Configure receive timeout.
*
Expand All @@ -101,13 +93,4 @@ bool NetIo_isDataAvailable(NetIoHandle xNetIoHandle);
*/
int NetIo_setRecvTimeout(NetIoHandle xNetIoHandle, unsigned int uRecvTimeoutMs);

/**
* @brief Configure send timeout.
*
* @param xNetIoHandle The network I/O handle
* @param uSendTimeoutMs Send timeout in milliseconds
* @return 0 on success, non-zero value otherwise
*/
int NetIo_setSendTimeout(NetIoHandle xNetIoHandle, unsigned int uSendTimeoutMs);

#endif /* NETIO_H */
4 changes: 1 addition & 3 deletions src/source/polly.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <string.h>

#include <time.h>
#include <sys/time.h>

#include "polly/polly.h"

Expand Down Expand Up @@ -226,8 +225,7 @@ int Polly_synthesizeSpeech(PollyServiceParameter_t *pServPara, PollySynthesizeSp
{
res = POLLY_ERRNO_NET_CONNECT_FAILED;
}
else if (NetIo_setSendTimeout(xNetIo, pServPara->uSendTimeoutMs) != NETIO_ERRNO_NONE ||
NetIo_setRecvTimeout(xNetIo, pServPara->uRecvTimeoutMs) != NETIO_ERRNO_NONE)
else if (NetIo_setRecvTimeout(xNetIo, pServPara->uRecvTimeoutMs) != NETIO_ERRNO_NONE)
{
res = POLLY_ERRNO_NET_CONFIG_FAILED;
}
Expand Down

0 comments on commit a4bf2da

Please sign in to comment.