first sources

This commit is contained in:
Wolfgang Hottgenroth 2020-11-12 16:51:32 +01:00
parent f02838b6a2
commit 790ccfbfe9
Signed by: wn
GPG Key ID: 6C1E5E531E0D5D7F
7 changed files with 211 additions and 1 deletions

View File

@ -1,4 +1,4 @@
Copyright (c) 2008-2020 Nicholas O'Leary
Copyright (c) 2020-2020 Wolfgang Hottgenroth
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
@ -18,3 +18,4 @@ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

33
Makefile Normal file
View File

@ -0,0 +1,33 @@
# Makefile - A simple client for MQTT in C
# Derived from the PubSubClient from Nick O'Leary
# Wolfgang Hottgenroth <woho@hottis.de>
# https://home.hottis.de/gitlab/wolutator/pubsubc
CFLAGS?=-mcpu=cortex-m3 -mthumb -Og -fdata-sections -ffunction-sections -g -gdwarf-2
CC=arm-none-eabi-gcc
AR=arm-none-eabi-ar
CFLAGS+=-I../ioLibrary_Driver/Ethernet -Isrc
OBJDIR=build
VPATH=src
OBJS=$(addprefix $(OBJDIR)/,pubsubc.o client.o)
all: $(OBJS)
$(AR) rcs pubsub.a $^
$(OBJDIR)/%.o: %.c
$(CC) $(CFLAGS) -c $< -o $@
$(OBJS): | $(OBJDIR)
$(OBJDIR):
mkdir $(OBJDIR)
.PHONY: clean
clean:
-rm -rf $(OBJDIR)

5
README.md Normal file
View File

@ -0,0 +1,5 @@
This work is directly derived from the famous PubSubClient library
of Nick O'Leary, which can be found at https://pubsubclient.knolleary.net/
It is most or less a plain C rewrite.

12
src/client.c Normal file
View File

@ -0,0 +1,12 @@
/*
client.c - A simple client for MQTT in C
Derived from the PubSubClient from Nick O'Leary
Wolfgang Hottgenroth <woho@hottis.de>
https://home.hottis.de/gitlab/wolutator/pubsubc
*/
#include <client.h>

22
src/client.h Normal file
View File

@ -0,0 +1,22 @@
/*
client.h - A simple client for MQTT in C
Derived from the PubSubClient from Nick O'Leary
Wolfgang Hottgenroth <woho@hottis.de>
https://home.hottis.de/gitlab/wolutator/pubsubc
*/
#ifndef _CLIENT_H_
#define _CLIENT_H_
#include <stdint.h>
typedef struct {
uint8_t sockNum;
} client_t;
#endif // _CLIENT_H_

11
src/pubsubc.c Normal file
View File

@ -0,0 +1,11 @@
/*
pubsubc.c - A simple client for MQTT in C
Derived from the PubSubClient from Nick O'Leary
Wolfgang Hottgenroth <woho@hottis.de>
https://home.hottis.de/gitlab/wolutator/pubsubc
*/
#include <pubsubc.h>
#include <client.h>

126
src/pubsubc.h Normal file
View File

@ -0,0 +1,126 @@
/*
pubsubc.h - A simple client for MQTT in C
Derived from the PubSubClient from Nick O'Leary
Wolfgang Hottgenroth <woho@hottis.de>
https://home.hottis.de/gitlab/wolutator/pubsubc
*/
#ifndef _PUBSUBC_H_
#define _PUBSUBC_H_
#define MQTT_VERSION_3_1 3
#define MQTT_VERSION_3_1_1 4
// MQTT_VERSION : Pick the version
//#define MQTT_VERSION MQTT_VERSION_3_1
#ifndef MQTT_VERSION
#define MQTT_VERSION MQTT_VERSION_3_1_1
#endif
// MQTT_MAX_PACKET_SIZE : Maximum packet size. Override with setBufferSize().
#ifndef MQTT_MAX_PACKET_SIZE
#define MQTT_MAX_PACKET_SIZE 256
#endif
// MQTT_KEEPALIVE : keepAlive interval in Seconds. Override with setKeepAlive()
#ifndef MQTT_KEEPALIVE
#define MQTT_KEEPALIVE 15
#endif
// MQTT_SOCKET_TIMEOUT: socket timeout interval in Seconds. Override with setSocketTimeout()
#ifndef MQTT_SOCKET_TIMEOUT
#define MQTT_SOCKET_TIMEOUT 15
#endif
// MQTT_MAX_TRANSFER_SIZE : limit how much data is passed to the network client
// in each write call. Needed for the Arduino Wifi Shield. Leave undefined to
// pass the entire MQTT packet in each write call.
//#define MQTT_MAX_TRANSFER_SIZE 80
// Possible values for client.state()
#define MQTT_CONNECTION_TIMEOUT -4
#define MQTT_CONNECTION_LOST -3
#define MQTT_CONNECT_FAILED -2
#define MQTT_DISCONNECTED -1
#define MQTT_CONNECTED 0
#define MQTT_CONNECT_BAD_PROTOCOL 1
#define MQTT_CONNECT_BAD_CLIENT_ID 2
#define MQTT_CONNECT_UNAVAILABLE 3
#define MQTT_CONNECT_BAD_CREDENTIALS 4
#define MQTT_CONNECT_UNAUTHORIZED 5
#define MQTTCONNECT 1 << 4 // Client request to connect to Server
#define MQTTCONNACK 2 << 4 // Connect Acknowledgment
#define MQTTPUBLISH 3 << 4 // Publish message
#define MQTTPUBACK 4 << 4 // Publish Acknowledgment
#define MQTTPUBREC 5 << 4 // Publish Received (assured delivery part 1)
#define MQTTPUBREL 6 << 4 // Publish Release (assured delivery part 2)
#define MQTTPUBCOMP 7 << 4 // Publish Complete (assured delivery part 3)
#define MQTTSUBSCRIBE 8 << 4 // Client Subscribe request
#define MQTTSUBACK 9 << 4 // Subscribe Acknowledgment
#define MQTTUNSUBSCRIBE 10 << 4 // Client Unsubscribe request
#define MQTTUNSUBACK 11 << 4 // Unsubscribe Acknowledgment
#define MQTTPINGREQ 12 << 4 // PING Request
#define MQTTPINGRESP 13 << 4 // PING Response
#define MQTTDISCONNECT 14 << 4 // Client is Disconnecting
#define MQTTReserved 15 << 4 // Reserved
#define MQTTQOS0 (0 << 1)
#define MQTTQOS1 (1 << 1)
#define MQTTQOS2 (2 << 1)
// Maximum size of fixed header and variable length size header
#define MQTT_MAX_HEADER_SIZE 5
#include <stdint.h>
#include <stdbool.h>
#include <client.h>
typedef void (*callback_t)(char*, uint8_t*, unsigned int);
typedef struct {
client_t *client;
uint8_t *buffer;
uint16_t bufferSize;
uint16_t keepAlive;
uint16_t socketTimeout;
uint16_t nextMsgId;
uint32_t lastOutActivity;
uint32_t lastInActivity;
bool pingOutstanding;
callback_t *callback;
uint8_t brokerAddress[4];
uint16_t brokerPort;
int _state;
} mqttClient_t;
void mqttClientInit(mqttClient_t *mqttClient);
bool connect(mqttClient_t *mqttClient, const char *id,
const char *user, const char *pass,
const char *willTopic, uint8_t willQos,
bool willRetain, const char *willMessage,
bool cleanSession);
void disconnect();
bool publish(mqttClient_t *mqttClient,
const char *topic,
const uint8_t *payload, uint16_t pLength,
bool retained);
bool subscribe(const char *topic, uint8_t qos);
bool unsubscribe(const char* topic);
bool loop();
bool connected();
#endif // _PUBSUBC_H_