From 11d4a046e11b0afda3e32f46f9059b9191e9a5f3 Mon Sep 17 00:00:00 2001 From: Wolfgang Hottgenroth Date: Thu, 12 Nov 2020 23:11:13 +0100 Subject: [PATCH] client implementation --- src/platformAdaption.c | 92 +++++++++++++++++++++++++++++++++++++++++- src/platformAdaption.h | 15 ++++++- 2 files changed, 104 insertions(+), 3 deletions(-) diff --git a/src/platformAdaption.c b/src/platformAdaption.c index 638b249..3c67aef 100644 --- a/src/platformAdaption.c +++ b/src/platformAdaption.c @@ -1,12 +1,102 @@ /* platformAdaption.c - A simple client for MQTT in C - Derived from the PubSubClient from Nick O'Leary Wolfgang Hottgenroth https://home.hottis.de/gitlab/wolutator/pubsubc */ #include +#include + +#include +#include +#include +int clientConnect(client_t *client, uint8_t *host, uint16_t port) { + int8_t res = socket(client->sockNum, Sn_MR_TCP, port, SF_IO_NONBLOCK); + if (res != client->sockNum) { + close(client->sockNum); + return INVALID_RESPONSE; + } + logMsg("clientConnect: socket initialized"); + res = connect(client->sockNum, host, port); + if (res != SOCK_BUSY) { + close(client->sockNum); + return INVALID_RESPONSE; + } + uint32_t startTime = HAL_GetTick(); + while (startTime + TIMEOUT_MS > HAL_GetTick()) { + uint8_t sockState = getSn_SR(client->sockNum); + if (sockState == SOCK_ESTABLISHED) { + logMsg("clientConnect: connection established"); + return SUCCESS; + } + } + return TIMED_OUT; +} + +int clientAvailable(client_t *client) { + return getSn_RX_RSR(client->sockNum); +} + +void clientStop(client_t *client) { + int8_t res = disconnect(client->sockNum); + + if (res != SOCK_BUSY) { + close(client->sockNum); + logMsg("clientStop: disconnect returns 0x%02x, invalid response, ignore it", res); + } else { + bool successfullyClosed = false; + uint32_t startTime = HAL_GetTick(); + while (startTime + TIMEOUT_MS > HAL_GetTick()) { + uint8_t sockState = getSn_SR(client->sockNum); + if (sockState == SOCK_CLOSED) { + logMsg("clientStop: connection closed"); + successfullyClosed = true; + break; + } + } + if (successfullyClosed) { + logMsg("clientStop: done"); + close(client->sockNum); + } else { + logMsg("clientStop: timeout when closing, ignore"); + close(client->sockNum); + } + } +} + +int clientRead(client_t *client) { + int res = -1; + if (clientAvailable(client) >= 1) { + uint8_t buf; + int32_t res = recv(client->sockNum, &buf, 1); + if (res == 1) { + res = (int) buf; + } + } + return res; +} + +size_t clientWrite(client_t *client, const uint8_t *buf, size_t size) { + int32_t res = send(client->sockNum, (uint8_t*) buf, size); + return (res == size) ? size : 0; +} + +size_t clientWriteOne(client_t *client, uint8_t b) { + return clientWrite(client, &b, 1); +} + +void clientFlush(client_t *client) { + // does nothing +} + +bool clientConnected(client_t *client) { + return (getSn_SR(client->sockNum) == SOCK_ESTABLISHED) ? 1 : 0; +} + +uint32_t millis() { + return HAL_GetTick(); +} \ No newline at end of file diff --git a/src/platformAdaption.h b/src/platformAdaption.h index b5d0d21..241fdc4 100644 --- a/src/platformAdaption.h +++ b/src/platformAdaption.h @@ -1,6 +1,5 @@ /* platformAdaption.h - A simple client for MQTT in C - Derived from the PubSubClient from Nick O'Leary Wolfgang Hottgenroth https://home.hottis.de/gitlab/wolutator/pubsubc */ @@ -14,11 +13,21 @@ #include #include + +const int SUCCESS = 1; +const int TIMED_OUT = -1; +const int INVALID_SERVER = -2; +const int TRUNCATED = -3; +const int INVALID_RESPONSE = -4; + +const uint32_t TIMEOUT_MS = 1000; + + typedef struct { uint8_t sockNum; } client_t; -int clientConnect(client_t *client, const char *host, uint16_t port); +int clientConnect(client_t *client, uint8_t *host, uint16_t port); int clientAvailable(client_t *client); void clientStop(client_t *client); int clientRead(client_t *client); @@ -29,5 +38,7 @@ bool clientConnected(client_t *client); uint32_t millis(); +uint32_t HAL_GetTick(void); +int logMsg(const char *format, ...); #endif // _PLATFORMADAPTION_H_ \ No newline at end of file