Arduino abstraction layer continued
This commit is contained in:
parent
673f2815d5
commit
8b3a05fec2
1
.gitignore
vendored
1
.gitignore
vendored
@ -1,4 +1,5 @@
|
||||
build
|
||||
pubsub.a
|
||||
tests/bin
|
||||
.pioenvs
|
||||
.piolibdeps
|
||||
|
@ -2,8 +2,11 @@
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
uint32_t HAL_GetTick(void);
|
||||
|
||||
millis_t millis() {
|
||||
return HAL_GetTick();
|
||||
}
|
||||
|
||||
void yield() {
|
||||
// does nothing
|
||||
}
|
@ -7,11 +7,19 @@
|
||||
#include <Print.h>
|
||||
#include <IPAddress.h>
|
||||
#include <Stream.h>
|
||||
#include <Client.h>
|
||||
#include <stubs.h>
|
||||
|
||||
|
||||
typedef uint32_t millis_t;
|
||||
typedef bool boolean;
|
||||
|
||||
|
||||
millis_t millis();
|
||||
void yield();
|
||||
|
||||
|
||||
#define pgm_read_byte(addr) (*(const unsigned char *)(addr))
|
||||
#define pgm_read_byte_near(addr) pgm_read_byte(addr)
|
||||
|
||||
#endif // _ARDUINO_H_
|
122
AAL/Client.cpp
Normal file
122
AAL/Client.cpp
Normal file
@ -0,0 +1,122 @@
|
||||
#include <socket.h>
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <Client.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
|
||||
|
||||
|
||||
/*
|
||||
class Client {
|
||||
public:
|
||||
Client();
|
||||
int connect(const char *host, uint16_t port);
|
||||
int connect(IPAddress ip, uint16_t port);
|
||||
int available();
|
||||
void stop();
|
||||
int read();
|
||||
size_t write(const uint8_t *buf, size_t size);
|
||||
size_t write(uint8_t b);
|
||||
void flush();
|
||||
uint8_t connected();
|
||||
};
|
||||
*/
|
||||
|
||||
|
||||
Client::Client(uint8_t sockNum) : sockNum(sockNum) {
|
||||
|
||||
}
|
||||
|
||||
|
||||
int Client::connect(const char *host, uint16_t) {
|
||||
// DNS request required
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int Client::connect(IPAddress ip, uint16_t port) {
|
||||
int8_t res = socket(this->sockNum, Sn_MR_TCP, port, SF_IO_NONBLOCK);
|
||||
if (res != this->sockNum) {
|
||||
close(this->sockNum);
|
||||
return INVALID_RESPONSE;
|
||||
}
|
||||
logMsg("Client::connect: socket initialized");
|
||||
|
||||
res = ::connect(this->sockNum, ip.raw_address(), port);
|
||||
if (res != SOCK_BUSY) {
|
||||
close(this->sockNum);
|
||||
return INVALID_RESPONSE;
|
||||
}
|
||||
uint32_t startTime = HAL_GetTick();
|
||||
while (startTime + TIMEOUT_MS > HAL_GetTick()) {
|
||||
uint8_t sockState = getSn_SR(this->sockNum);
|
||||
if (sockState == SOCK_ESTABLISHED) {
|
||||
logMsg("Client::connect: connection established");
|
||||
return SUCCESS;
|
||||
}
|
||||
}
|
||||
return TIMED_OUT;
|
||||
}
|
||||
|
||||
int Client::available() {
|
||||
return getSn_RX_RSR(this->sockNum);
|
||||
}
|
||||
|
||||
void Client::stop() {
|
||||
int8_t res = disconnect(this->sockNum);
|
||||
|
||||
if (res != SOCK_BUSY) {
|
||||
close(this->sockNum);
|
||||
logMsg("Client::stop: 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(this->sockNum);
|
||||
if (sockState == SOCK_CLOSED) {
|
||||
logMsg("Client::stop: connection closed");
|
||||
successfullyClosed = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (successfullyClosed) {
|
||||
logMsg("Client::stop: done");
|
||||
close(this->sockNum);
|
||||
} else {
|
||||
logMsg("Client::stop: timeout when closing, ignore");
|
||||
close(this->sockNum);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int Client::read() {
|
||||
int res = -1;
|
||||
if (this->available() >= 1) {
|
||||
uint8_t buf;
|
||||
int32_t res = recv(this->sockNum, &buf, 1);
|
||||
if (res == 1) {
|
||||
res = (int) buf;
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
size_t Client::write(const uint8_t *buf, size_t size) {
|
||||
int32_t res = send(this->sockNum, (uint8_t*) buf, size);
|
||||
return (res == size) ? size : 0;
|
||||
}
|
||||
|
||||
size_t Client::write(uint8_t b) {
|
||||
return this->write(&b, 1);
|
||||
}
|
||||
|
||||
void Client::flush() {
|
||||
// does nothing
|
||||
}
|
||||
|
||||
uint8_t Client::connected() {
|
||||
return (getSn_SR(this->sockNum) == SOCK_ESTABLISHED) ? 1 : 0;
|
||||
}
|
||||
|
29
AAL/Client.h
29
AAL/Client.h
@ -1,6 +1,35 @@
|
||||
#ifndef _CLIENT_H_
|
||||
#define _CLIENT_H_
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <stdint.h>
|
||||
#include <Arduino.h>
|
||||
#include <IPAddress.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;
|
||||
|
||||
class Client {
|
||||
private:
|
||||
const uint8_t sockNum;
|
||||
public:
|
||||
Client(const uint8_t sockNum);
|
||||
int connect(const char *host, uint16_t port);
|
||||
int connect(IPAddress ip, uint16_t port);
|
||||
int available();
|
||||
void stop();
|
||||
int read();
|
||||
size_t write(const uint8_t *buf, size_t size);
|
||||
size_t write(uint8_t b);
|
||||
void flush();
|
||||
uint8_t connected();
|
||||
};
|
||||
|
||||
#endif // _CLIENT_H_
|
@ -1,7 +1,7 @@
|
||||
#include <IPAddress.h>
|
||||
|
||||
#include <stdint.h>
|
||||
|
||||
#include <string.h>
|
||||
|
||||
IPAddress::IPAddress() {
|
||||
memset(_address, 0, sizeof(_address));
|
||||
|
@ -10,6 +10,8 @@ class IPAddress {
|
||||
public:
|
||||
IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4);
|
||||
IPAddress();
|
||||
|
||||
friend class Client;
|
||||
};
|
||||
|
||||
|
||||
|
9
AAL/stubs.h
Normal file
9
AAL/stubs.h
Normal file
@ -0,0 +1,9 @@
|
||||
#ifndef _STUBS_H_
|
||||
#define _STUBS_H_
|
||||
|
||||
uint32_t HAL_GetTick(void);
|
||||
int logMsg(const char *format, ...);
|
||||
|
||||
|
||||
|
||||
#endif // _STUBS_H_
|
2
Makefile
2
Makefile
@ -9,7 +9,7 @@ CFLAGS+=-I../ioLibrary_Driver/Ethernet -Isrc -IAAL
|
||||
OBJDIR=build
|
||||
VPATH=src AAL
|
||||
|
||||
OBJS=$(addprefix $(OBJDIR)/,PubSubClient.o IPAddress.o Stream.o Arduino.o Print.o)
|
||||
OBJS=$(addprefix $(OBJDIR)/,PubSubClient.o IPAddress.o Stream.o Arduino.o Print.o Client.o)
|
||||
|
||||
all: $(OBJS)
|
||||
$(AR) rcs pubsub.a $^
|
||||
|
Loading…
x
Reference in New Issue
Block a user