client implementation

This commit is contained in:
Wolfgang Hottgenroth 2020-11-12 23:11:13 +01:00
parent 472788c773
commit 11d4a046e1
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
2 changed files with 104 additions and 3 deletions

View File

@ -1,12 +1,102 @@
/* /*
platformAdaption.c - A simple client for MQTT in C platformAdaption.c - A simple client for MQTT in C
Derived from the PubSubClient from Nick O'Leary
Wolfgang Hottgenroth <woho@hottis.de> Wolfgang Hottgenroth <woho@hottis.de>
https://home.hottis.de/gitlab/wolutator/pubsubc https://home.hottis.de/gitlab/wolutator/pubsubc
*/ */
#include <platformAdaption.h> #include <platformAdaption.h>
#include <socket.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
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();
}

View File

@ -1,6 +1,5 @@
/* /*
platformAdaption.h - A simple client for MQTT in C platformAdaption.h - A simple client for MQTT in C
Derived from the PubSubClient from Nick O'Leary
Wolfgang Hottgenroth <woho@hottis.de> Wolfgang Hottgenroth <woho@hottis.de>
https://home.hottis.de/gitlab/wolutator/pubsubc https://home.hottis.de/gitlab/wolutator/pubsubc
*/ */
@ -14,11 +13,21 @@
#include <stdlib.h> #include <stdlib.h>
#include <stdbool.h> #include <stdbool.h>
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 { typedef struct {
uint8_t sockNum; uint8_t sockNum;
} client_t; } 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); int clientAvailable(client_t *client);
void clientStop(client_t *client); void clientStop(client_t *client);
int clientRead(client_t *client); int clientRead(client_t *client);
@ -29,5 +38,7 @@ bool clientConnected(client_t *client);
uint32_t millis(); uint32_t millis();
uint32_t HAL_GetTick(void);
int logMsg(const char *format, ...);
#endif // _PLATFORMADAPTION_H_ #endif // _PLATFORMADAPTION_H_