3 Commits
v2.8 ... master

13 changed files with 310 additions and 0 deletions

2
.gitignore vendored
View File

@ -1,3 +1,5 @@
build
pubsub.a
tests/bin
.pioenvs
.piolibdeps

12
AAL/Arduino.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <Arduino.h>
#include <stdint.h>
millis_t millis() {
return HAL_GetTick();
}
void yield() {
// does nothing
}

25
AAL/Arduino.h Normal file
View File

@ -0,0 +1,25 @@
#ifndef _ARDUINO_H_
#define _ARDUINO_H_
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
#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
View 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;
}

35
AAL/Client.h Normal file
View File

@ -0,0 +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_

16
AAL/IPAddress.cpp Normal file
View File

@ -0,0 +1,16 @@
#include <IPAddress.h>
#include <stdint.h>
#include <string.h>
IPAddress::IPAddress() {
memset(_address, 0, sizeof(_address));
}
IPAddress::IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4) {
_address[0] = o1;
_address[1] = o2;
_address[2] = o3;
_address[3] = o4;
}

18
AAL/IPAddress.h Normal file
View File

@ -0,0 +1,18 @@
#ifndef _IPADDRESS_H_
#define _IPADDRESS_H_
#include <stdint.h>
class IPAddress {
private:
uint8_t _address[4];
uint8_t *raw_address() { return _address; };
public:
IPAddress(uint8_t o1, uint8_t o2, uint8_t o3, uint8_t o4);
IPAddress();
friend class Client;
};
#endif // _IPADDRESS_H_

7
AAL/Print.cpp Normal file
View File

@ -0,0 +1,7 @@
#include <Arduino.h>
#include <Print.h>
Print::Print() {
}

9
AAL/Print.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _PRINT_H_
#define _PRINT_H_
class Print {
public:
Print();
};
#endif // _PRINT_H_

12
AAL/Stream.cpp Normal file
View File

@ -0,0 +1,12 @@
#include <Stream.h>
#include <stdint.h>
#include <stdlib.h>
Stream::Stream() {
}
size_t Stream::write(uint8_t c) {
return 0;
}

13
AAL/Stream.h Normal file
View File

@ -0,0 +1,13 @@
#ifndef _STREAM_H_
#define _STREAM_H_
#include <stdint.h>
#include <stdlib.h>
class Stream {
public:
Stream();
size_t write(uint8_t c);
};
#endif // _STREAM_H_

9
AAL/stubs.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef _STUBS_H_
#define _STUBS_H_
uint32_t HAL_GetTick(void);
int logMsg(const char *format, ...);
#endif // _STUBS_H_

30
Makefile Normal file
View File

@ -0,0 +1,30 @@
CFLAGS?=-mcpu=cortex-m3 -mthumb -Og -fdata-sections -ffunction-sections -g -gdwarf-2
CC=arm-none-eabi-gcc
CXX=arm-none-eabi-g++
AR=arm-none-eabi-ar
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 Client.o)
all: $(OBJS)
$(AR) rcs pubsub.a $^
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(OBJDIR)/%.o: %.cpp
$(CXX) $(CFLAGS) -c $< -o $@
$(OBJS): | $(OBJDIR)
$(OBJDIR):
mkdir $(OBJDIR)
.PHONY: clean
clean:
-rm -rf $(OBJDIR)