From 4b0b4ab2c90c92d27ebde6fba1edbae7ac5918ce Mon Sep 17 00:00:00 2001 From: Jaakko Nahkala <74764860+jaakkoiot@users.noreply.github.com> Date: Wed, 26 Oct 2022 23:25:58 +0300 Subject: [PATCH] esp-vent-main: Mqtt classes ready (#39) * Relocated subreposiroty and added ESP-classes, problem with systick.h persists * added test to main * Clean directory by moving MQTT related classes to their own folders in /src /inc * Relocated subreposiroty and added ESP-classes, problem with systick.h persists * added test to main * rm MQTT test func Co-authored-by: Vasily Davydov --- .../info.properties | 8 +- esp-vent-main/.cproject | 55 +- esp-vent-main/.settings/language.settings.xml | 6 +- esp-vent-main/inc/MQTT/MQTTConnect.h | 148 ++++ esp-vent-main/inc/MQTT/MQTTFormat.h | 37 + esp-vent-main/inc/MQTT/MQTTPacket.h | 133 +++ esp-vent-main/inc/MQTT/MQTTPublish.h | 38 + esp-vent-main/inc/MQTT/MQTTSubscribe.h | 39 + esp-vent-main/inc/MQTT/MQTTUnsubscribe.h | 38 + esp-vent-main/inc/MQTT/MQTT_lpc1549.h | 66 ++ esp-vent-main/inc/MQTT/StackTrace.h | 78 ++ esp-vent-main/inc/MQTT/esp8266_socket.h | 20 + esp-vent-main/inc/serial_port.h | 29 + esp-vent-main/inc/systick.h | 27 + esp-vent-main/src/MQTT/MQTTConnectClient.c | 214 +++++ esp-vent-main/src/MQTT/MQTTConnectServer.c | 148 ++++ .../src/MQTT/MQTTDeserializePublish.c | 107 +++ esp-vent-main/src/MQTT/MQTTFormat.c | 261 ++++++ esp-vent-main/src/MQTT/MQTTPacket.c | 411 ++++++++++ esp-vent-main/src/MQTT/MQTTSerializePublish.c | 169 ++++ esp-vent-main/src/MQTT/MQTTSubscribeClient.c | 137 ++++ esp-vent-main/src/MQTT/MQTTSubscribeServer.c | 112 +++ .../src/MQTT/MQTTUnsubscribeClient.c | 106 +++ .../src/MQTT/MQTTUnsubscribeServer.c | 102 +++ esp-vent-main/src/MQTT/MQTT_lpc1549.c | 140 ++++ esp-vent-main/src/MQTT/esp8266_socket.c | 755 ++++++++++++++++++ esp-vent-main/src/esp-vent-main.cpp | 7 + esp-vent-main/src/serial_port.cpp | 49 ++ lpc_board_nxp_lpcxpresso_1549/.cproject | 63 +- .../.settings/language.settings.xml | 6 +- lpc_chip_15xx/.cproject | 57 +- lpc_chip_15xx/.settings/language.settings.xml | 6 +- periph_blinky/.cproject | 45 +- periph_blinky/.settings/language.settings.xml | 6 +- 34 files changed, 3443 insertions(+), 180 deletions(-) create mode 100644 esp-vent-main/inc/MQTT/MQTTConnect.h create mode 100644 esp-vent-main/inc/MQTT/MQTTFormat.h create mode 100644 esp-vent-main/inc/MQTT/MQTTPacket.h create mode 100644 esp-vent-main/inc/MQTT/MQTTPublish.h create mode 100644 esp-vent-main/inc/MQTT/MQTTSubscribe.h create mode 100644 esp-vent-main/inc/MQTT/MQTTUnsubscribe.h create mode 100644 esp-vent-main/inc/MQTT/MQTT_lpc1549.h create mode 100644 esp-vent-main/inc/MQTT/StackTrace.h create mode 100644 esp-vent-main/inc/MQTT/esp8266_socket.h create mode 100644 esp-vent-main/inc/serial_port.h create mode 100644 esp-vent-main/inc/systick.h create mode 100644 esp-vent-main/src/MQTT/MQTTConnectClient.c create mode 100644 esp-vent-main/src/MQTT/MQTTConnectServer.c create mode 100644 esp-vent-main/src/MQTT/MQTTDeserializePublish.c create mode 100644 esp-vent-main/src/MQTT/MQTTFormat.c create mode 100644 esp-vent-main/src/MQTT/MQTTPacket.c create mode 100644 esp-vent-main/src/MQTT/MQTTSerializePublish.c create mode 100644 esp-vent-main/src/MQTT/MQTTSubscribeClient.c create mode 100644 esp-vent-main/src/MQTT/MQTTSubscribeServer.c create mode 100644 esp-vent-main/src/MQTT/MQTTUnsubscribeClient.c create mode 100644 esp-vent-main/src/MQTT/MQTTUnsubscribeServer.c create mode 100644 esp-vent-main/src/MQTT/MQTT_lpc1549.c create mode 100644 esp-vent-main/src/MQTT/esp8266_socket.c create mode 100644 esp-vent-main/src/serial_port.cpp diff --git a/.mcuxpressoide_packages_support/info.properties b/.mcuxpressoide_packages_support/info.properties index 6e84e59..4362e6b 100644 --- a/.mcuxpressoide_packages_support/info.properties +++ b/.mcuxpressoide_packages_support/info.properties @@ -1,5 +1,5 @@ #MCUXpresso IDE -#Mon Oct 24 11:55:50 EEST 2022 -product.name=MCUXpresso IDE v11.5.0 [Build 7232] [2022-01-11] -product.version=11.5.0 -product.build=7232 +#Tue Oct 25 20:03:42 EEST 2022 +product.version=11.2.1 +product.build=4149 +product.name=MCUXpresso IDE v11.2.1 [Build 4149] [2020-10-07] diff --git a/esp-vent-main/.cproject b/esp-vent-main/.cproject index 99019c9..fa1a25c 100644 --- a/esp-vent-main/.cproject +++ b/esp-vent-main/.cproject @@ -39,6 +39,9 @@ @@ -314,31 +317,31 @@ - <?xml version="1.0" encoding="UTF-8"?> -<TargetConfig> -<Properties property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="100300"/> -<infoList vendor="NXP"> -<info chip="LPC1549" connectscript="LPC15RunBootRomConnect.scp" flash_driver="LPC15xx_256K.cfx" match_id="0x0" name="LPC1549" resetscript="LPC15RunBootRomReset.scp" stub="crt_emu_cm3_gen"> -<chip> -<name>LPC1549</name> -<family>LPC15xx</family> -<vendor>NXP (formerly Philips)</vendor> -<reset board="None" core="Real" sys="Real"/> -<clock changeable="TRUE" freq="12MHz" is_accurate="TRUE"/> -<memory can_program="true" id="Flash" is_ro="true" type="Flash"/> -<memory id="RAM" type="RAM"/> -<memory id="Periph" is_volatile="true" type="Peripheral"/> -<memoryInstance derived_from="Flash" id="MFlash256" location="0x0" size="0x40000"/> -<memoryInstance derived_from="RAM" id="Ram0_16" location="0x2000000" size="0x4000"/> -<memoryInstance derived_from="RAM" id="Ram1_16" location="0x2004000" size="0x4000"/> -<memoryInstance derived_from="RAM" id="Ram2_4" location="0x2008000" size="0x1000"/> -</chip> -<processor> -<name gcc_name="cortex-m3">Cortex-M3</name> -<family>Cortex-M</family> -</processor> -</info> -</infoList> + <?xml version="1.0" encoding="UTF-8"?> +<TargetConfig> +<Properties property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="100300"/> +<infoList vendor="NXP"> +<info chip="LPC1549" connectscript="LPC15RunBootRomConnect.scp" flash_driver="LPC15xx_256K.cfx" match_id="0x0" name="LPC1549" resetscript="LPC15RunBootRomReset.scp" stub="crt_emu_cm3_gen"> +<chip> +<name>LPC1549</name> +<family>LPC15xx</family> +<vendor>NXP (formerly Philips)</vendor> +<reset board="None" core="Real" sys="Real"/> +<clock changeable="TRUE" freq="12MHz" is_accurate="TRUE"/> +<memory can_program="true" id="Flash" is_ro="true" type="Flash"/> +<memory id="RAM" type="RAM"/> +<memory id="Periph" is_volatile="true" type="Peripheral"/> +<memoryInstance derived_from="Flash" id="MFlash256" location="0x0" size="0x40000"/> +<memoryInstance derived_from="RAM" id="Ram0_16" location="0x2000000" size="0x4000"/> +<memoryInstance derived_from="RAM" id="Ram1_16" location="0x2004000" size="0x4000"/> +<memoryInstance derived_from="RAM" id="Ram2_4" location="0x2008000" size="0x1000"/> +</chip> +<processor> +<name gcc_name="cortex-m3">Cortex-M3</name> +<family>Cortex-M</family> +</processor> +</info> +</infoList> </TargetConfig> @@ -346,4 +349,4 @@ - \ No newline at end of file + diff --git a/esp-vent-main/.settings/language.settings.xml b/esp-vent-main/.settings/language.settings.xml index 5645974..17b493c 100644 --- a/esp-vent-main/.settings/language.settings.xml +++ b/esp-vent-main/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + @@ -15,11 +15,11 @@ - + - \ No newline at end of file + diff --git a/esp-vent-main/inc/MQTT/MQTTConnect.h b/esp-vent-main/inc/MQTT/MQTTConnect.h new file mode 100644 index 0000000..98c2c16 --- /dev/null +++ b/esp-vent-main/inc/MQTT/MQTTConnect.h @@ -0,0 +1,148 @@ +/******************************************************************************* + * Copyright (c) 2014, 2017 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Ian Craggs - add connack return code definitions + * Xiang Rong - 442039 Add makefile to Embedded C client + * Ian Craggs - fix for issue #64, bit order in connack response + *******************************************************************************/ + +#ifndef MQTTCONNECT_H_ +#define MQTTCONNECT_H_ + +enum connack_return_codes +{ + MQTT_CONNECTION_ACCEPTED = 0, + MQTT_UNNACCEPTABLE_PROTOCOL = 1, + MQTT_CLIENTID_REJECTED = 2, + MQTT_SERVER_UNAVAILABLE = 3, + MQTT_BAD_USERNAME_OR_PASSWORD = 4, + MQTT_NOT_AUTHORIZED = 5, +}; + +#if !defined(DLLImport) + #define DLLImport +#endif +#if !defined(DLLExport) + #define DLLExport +#endif + + +typedef union +{ + unsigned char all; /**< all connect flags */ +#if defined(REVERSED) + struct + { + unsigned int username : 1; /**< 3.1 user name */ + unsigned int password : 1; /**< 3.1 password */ + unsigned int willRetain : 1; /**< will retain setting */ + unsigned int willQoS : 2; /**< will QoS value */ + unsigned int will : 1; /**< will flag */ + unsigned int cleansession : 1; /**< clean session flag */ + unsigned int : 1; /**< unused */ + } bits; +#else + struct + { + unsigned int : 1; /**< unused */ + unsigned int cleansession : 1; /**< cleansession flag */ + unsigned int will : 1; /**< will flag */ + unsigned int willQoS : 2; /**< will QoS value */ + unsigned int willRetain : 1; /**< will retain setting */ + unsigned int password : 1; /**< 3.1 password */ + unsigned int username : 1; /**< 3.1 user name */ + } bits; +#endif +} MQTTConnectFlags; /**< connect flags byte */ + + + +/** + * Defines the MQTT "Last Will and Testament" (LWT) settings for + * the connect packet. + */ +typedef struct +{ + /** The eyecatcher for this structure. must be MQTW. */ + char struct_id[4]; + /** The version number of this structure. Must be 0 */ + int struct_version; + /** The LWT topic to which the LWT message will be published. */ + MQTTString topicName; + /** The LWT payload. */ + MQTTString message; + /** + * The retained flag for the LWT message (see MQTTAsync_message.retained). + */ + unsigned char retained; + /** + * The quality of service setting for the LWT message (see + * MQTTAsync_message.qos and @ref qos). + */ + char qos; +} MQTTPacket_willOptions; + + +#define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 } + + +typedef struct +{ + /** The eyecatcher for this structure. must be MQTC. */ + char struct_id[4]; + /** The version number of this structure. Must be 0 */ + int struct_version; + /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1 + */ + unsigned char MQTTVersion; + MQTTString clientID; + unsigned short keepAliveInterval; + unsigned char cleansession; + unsigned char willFlag; + MQTTPacket_willOptions will; + MQTTString username; + MQTTString password; +} MQTTPacket_connectData; + +typedef union +{ + unsigned char all; /**< all connack flags */ +#if defined(REVERSED) + struct + { + unsigned int reserved : 7; /**< unused */ + unsigned int sessionpresent : 1; /**< session present flag */ + } bits; +#else + struct + { + unsigned int sessionpresent : 1; /**< session present flag */ + unsigned int reserved: 7; /**< unused */ + } bits; +#endif +} MQTTConnackFlags; /**< connack flags byte */ + +#define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \ + MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } + +DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options); +DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len); + +DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent); +DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen); + +DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen); +DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen); + +#endif /* MQTTCONNECT_H_ */ diff --git a/esp-vent-main/inc/MQTT/MQTTFormat.h b/esp-vent-main/inc/MQTT/MQTTFormat.h new file mode 100644 index 0000000..47b0c41 --- /dev/null +++ b/esp-vent-main/inc/MQTT/MQTTFormat.h @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#if !defined(MQTTFORMAT_H) +#define MQTTFORMAT_H + +#include "StackTrace.h" +#include "MQTTPacket.h" + +const char* MQTTPacket_getName(unsigned short packetid); +int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data); +int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent); +int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained, + unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen); +int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid); +int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count, + MQTTString topicFilters[], int requestedQoSs[]); +int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs); +int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, + int count, MQTTString topicFilters[]); +char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); +char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); + +#endif diff --git a/esp-vent-main/inc/MQTT/MQTTPacket.h b/esp-vent-main/inc/MQTT/MQTTPacket.h new file mode 100644 index 0000000..4e1daa0 --- /dev/null +++ b/esp-vent-main/inc/MQTT/MQTTPacket.h @@ -0,0 +1,133 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Xiang Rong - 442039 Add makefile to Embedded C client + *******************************************************************************/ + +#ifndef MQTTPACKET_H_ +#define MQTTPACKET_H_ + +#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ +extern "C" { +#endif + +#if defined(WIN32_DLL) || defined(WIN64_DLL) + #define DLLImport __declspec(dllimport) + #define DLLExport __declspec(dllexport) +#elif defined(LINUX_SO) + #define DLLImport extern + #define DLLExport __attribute__ ((visibility ("default"))) +#else + #define DLLImport + #define DLLExport +#endif + +enum errors +{ + MQTTPACKET_BUFFER_TOO_SHORT = -2, + MQTTPACKET_READ_ERROR = -1, + MQTTPACKET_READ_COMPLETE +}; + +enum msgTypes +{ + CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, + PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, + PINGREQ, PINGRESP, DISCONNECT +}; + +/** + * Bitfields for the MQTT header byte. + */ +typedef union +{ + unsigned char byte; /**< the whole byte */ +#if defined(REVERSED) + struct + { + unsigned int type : 4; /**< message type nibble */ + unsigned int dup : 1; /**< DUP flag bit */ + unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ + unsigned int retain : 1; /**< retained flag bit */ + } bits; +#else + struct + { + unsigned int retain : 1; /**< retained flag bit */ + unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ + unsigned int dup : 1; /**< DUP flag bit */ + unsigned int type : 4; /**< message type nibble */ + } bits; +#endif +} MQTTHeader; + +typedef struct +{ + int len; + char* data; +} MQTTLenString; + +typedef struct +{ + char* cstring; + MQTTLenString lenstring; +} MQTTString; + +#define MQTTString_initializer {NULL, {0, NULL}} + +int MQTTstrlen(MQTTString mqttstring); + +#include "MQTTConnect.h" +#include "MQTTPublish.h" +#include "MQTTSubscribe.h" +#include "MQTTUnsubscribe.h" +#include "../inc/MQTT/MQTTFormat.h" + +DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); +DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); + +int MQTTPacket_len(int rem_len); +DLLExport int MQTTPacket_equals(MQTTString* a, char* b); + +DLLExport int MQTTPacket_encode(unsigned char* buf, int length); +int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); +int MQTTPacket_decodeBuf(unsigned char* buf, int* value); + +int readInt(unsigned char** pptr); +char readChar(unsigned char** pptr); +void writeChar(unsigned char** pptr, char c); +void writeInt(unsigned char** pptr, int anInt); +int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); +void writeCString(unsigned char** pptr, const char* string); +void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); + +DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); + +typedef struct { + int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ + void *sck; /* pointer to whatever the system may use to identify the transport */ + int multiplier; + int rem_len; + int len; + char state; +}MQTTTransport; + +int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); + +#ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ +} +#endif + + +#endif /* MQTTPACKET_H_ */ diff --git a/esp-vent-main/inc/MQTT/MQTTPublish.h b/esp-vent-main/inc/MQTT/MQTTPublish.h new file mode 100644 index 0000000..ebe479d --- /dev/null +++ b/esp-vent-main/inc/MQTT/MQTTPublish.h @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Xiang Rong - 442039 Add makefile to Embedded C client + *******************************************************************************/ + +#ifndef MQTTPUBLISH_H_ +#define MQTTPUBLISH_H_ + +#if !defined(DLLImport) + #define DLLImport +#endif +#if !defined(DLLExport) + #define DLLExport +#endif + +DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, + MQTTString topicName, unsigned char* payload, int payloadlen); + +DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, + unsigned char** payload, int* payloadlen, unsigned char* buf, int len); + +DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid); +DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid); +DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid); + +#endif /* MQTTPUBLISH_H_ */ diff --git a/esp-vent-main/inc/MQTT/MQTTSubscribe.h b/esp-vent-main/inc/MQTT/MQTTSubscribe.h new file mode 100644 index 0000000..aa91826 --- /dev/null +++ b/esp-vent-main/inc/MQTT/MQTTSubscribe.h @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Xiang Rong - 442039 Add makefile to Embedded C client + *******************************************************************************/ + +#ifndef MQTTSUBSCRIBE_H_ +#define MQTTSUBSCRIBE_H_ + +#if !defined(DLLImport) + #define DLLImport +#endif +#if !defined(DLLExport) + #define DLLExport +#endif + +DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, + int count, MQTTString topicFilters[], int requestedQoSs[]); + +DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, + int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len); + +DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs); + +DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len); + + +#endif /* MQTTSUBSCRIBE_H_ */ diff --git a/esp-vent-main/inc/MQTT/MQTTUnsubscribe.h b/esp-vent-main/inc/MQTT/MQTTUnsubscribe.h new file mode 100644 index 0000000..355ca9a --- /dev/null +++ b/esp-vent-main/inc/MQTT/MQTTUnsubscribe.h @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Xiang Rong - 442039 Add makefile to Embedded C client + *******************************************************************************/ + +#ifndef MQTTUNSUBSCRIBE_H_ +#define MQTTUNSUBSCRIBE_H_ + +#if !defined(DLLImport) + #define DLLImport +#endif +#if !defined(DLLExport) + #define DLLExport +#endif + +DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, + int count, MQTTString topicFilters[]); + +DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[], + unsigned char* buf, int len); + +DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid); + +DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len); + +#endif /* MQTTUNSUBSCRIBE_H_ */ diff --git a/esp-vent-main/inc/MQTT/MQTT_lpc1549.h b/esp-vent-main/inc/MQTT/MQTT_lpc1549.h new file mode 100644 index 0000000..ab53688 --- /dev/null +++ b/esp-vent-main/inc/MQTT/MQTT_lpc1549.h @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright (c) 2014, 2015 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Allan Stockdill-Mander - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#if !defined(MQTT_lpc1549_H) +#define MQTT_lpc1549_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct Timer +{ + uint32_t TicksToWait; + uint32_t Start; +} Timer; + + +typedef struct Network Network; + +struct Network +{ + int my_socket; + int (*mqttread) (Network*, unsigned char*, int, int); + int (*mqttwrite) (Network*, unsigned char*, int, int); + void (*disconnect) (Network*); + char ssid[32]; + char password[32]; +}; + +void TimerInit(Timer*); +char TimerIsExpired(Timer*); +void TimerCountdownMS(Timer*, unsigned int); +void TimerCountdown(Timer*, unsigned int); +int TimerLeftMS(Timer*); + + + +int lpc1549_read(Network*, unsigned char*, int, int); +int lpc1549_write(Network*, unsigned char*, int, int); +void lpc1549_disconnect(Network*); + +void NetworkInit(Network *n, const char *ssid, const char *password); +int NetworkConnect(Network *n, char *address, int port); +void NetworkDisconnect(Network *n); + +#ifdef __cplusplus +} +#endif + + +#endif diff --git a/esp-vent-main/inc/MQTT/StackTrace.h b/esp-vent-main/inc/MQTT/StackTrace.h new file mode 100644 index 0000000..2808a0d --- /dev/null +++ b/esp-vent-main/inc/MQTT/StackTrace.h @@ -0,0 +1,78 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Ian Craggs - fix for bug #434081 + *******************************************************************************/ + +#ifndef STACKTRACE_H_ +#define STACKTRACE_H_ + +#include +#define NOSTACKTRACE 1 + +#if defined(NOSTACKTRACE) +#define FUNC_ENTRY +#define FUNC_ENTRY_NOLOG +#define FUNC_ENTRY_MED +#define FUNC_ENTRY_MAX +#define FUNC_EXIT +#define FUNC_EXIT_NOLOG +#define FUNC_EXIT_MED +#define FUNC_EXIT_MAX +#define FUNC_EXIT_RC(x) +#define FUNC_EXIT_MED_RC(x) +#define FUNC_EXIT_MAX_RC(x) + +#else + +#if defined(WIN32) +#define inline __inline +#define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM) +#define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1) +#define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM) +#define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM) +#define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM) +#define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1) +#define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM) +#define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM) +#define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM) +#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM) +#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM) +#else +#define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM) +#define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1) +#define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM) +#define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM) +#define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM) +#define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1) +#define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM) +#define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM) +#define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM) +#define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM) +#define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM) + +void StackTrace_entry(const char* name, int line, int trace); +void StackTrace_exit(const char* name, int line, void* return_value, int trace); + +void StackTrace_printStack(FILE* dest); +char* StackTrace_get(unsigned long); + +#endif + +#endif + + + + +#endif /* STACKTRACE_H_ */ diff --git a/esp-vent-main/inc/MQTT/esp8266_socket.h b/esp-vent-main/inc/MQTT/esp8266_socket.h new file mode 100644 index 0000000..786e5be --- /dev/null +++ b/esp-vent-main/inc/MQTT/esp8266_socket.h @@ -0,0 +1,20 @@ +#ifndef ESP8266_H_ +#define ESP8266_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + +int esp_socket(const char *ssid, const char *password) ; +int esp_connect(int sockfd, const char *addr, int port); +int esp_read(int sockfd, void *data, int length); +int esp_write(int sockfd, const void *data, int length); +int esp_close(int sockfd); +int esp_shutdown(int sockfd, int how); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/esp-vent-main/inc/serial_port.h b/esp-vent-main/inc/serial_port.h new file mode 100644 index 0000000..5e44632 --- /dev/null +++ b/esp-vent-main/inc/serial_port.h @@ -0,0 +1,29 @@ +/* + * serial_port.h + * + * Created on: 25.8.2021 + * Author: keijo + */ + +#ifndef SERIAL_PORT_H_ +#define SERIAL_PORT_H_ + +#ifdef __cplusplus +extern "C" { +#endif + + +void serial_init(void *ctx); +void serial_write_buf(void *ctx, const char *buf, int len); +void serial_write_str(void *ctx, const char *s); +int serial_get_char(void *ctx, char *p); +int serial_peek(void *ctx); + +#ifdef __cplusplus +} +#endif + + + + +#endif /* SERIAL_PORT_H_ */ diff --git a/esp-vent-main/inc/systick.h b/esp-vent-main/inc/systick.h new file mode 100644 index 0000000..b12cf5b --- /dev/null +++ b/esp-vent-main/inc/systick.h @@ -0,0 +1,27 @@ +/* + * systick.h + * + * Created on: 6.9.2021 + * Author: keijo + */ + +#ifndef SYSTICK_H_ +#define SYSTICK_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +uint32_t get_ticks(void); + +#ifdef __cplusplus +} +#endif + +void Sleep(int ms); + +/* this function is required by the modbus library */ +uint32_t millis(); + + +#endif /* SYSTICK_H_ */ diff --git a/esp-vent-main/src/MQTT/MQTTConnectClient.c b/esp-vent-main/src/MQTT/MQTTConnectClient.c new file mode 100644 index 0000000..1a3ece4 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTConnectClient.c @@ -0,0 +1,214 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include +#include "../inc/MQTT/StackTrace.h" + +#include + +/** + * Determines the length of the MQTT connect packet that would be produced using the supplied connect options. + * @param options the options to be used to build the connect packet + * @return the length of buffer needed to contain the serialized version of the packet + */ +int MQTTSerialize_connectLength(MQTTPacket_connectData* options) +{ + int len = 0; + + FUNC_ENTRY; + + if (options->MQTTVersion == 3) + len = 12; /* variable depending on MQTT or MQIsdp */ + else if (options->MQTTVersion == 4) + len = 10; + + len += MQTTstrlen(options->clientID)+2; + if (options->willFlag) + len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2; + if (options->username.cstring || options->username.lenstring.data) + len += MQTTstrlen(options->username)+2; + if (options->password.cstring || options->password.lenstring.data) + len += MQTTstrlen(options->password)+2; + + FUNC_EXIT_RC(len); + return len; +} + + +/** + * Serializes the connect options into the buffer. + * @param buf the buffer into which the packet will be serialized + * @param len the length in bytes of the supplied buffer + * @param options the options to be used to build the connect packet + * @return serialized length, or error if 0 + */ +int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) +{ + unsigned char *ptr = buf; + MQTTHeader header = {0}; + MQTTConnectFlags flags = {0}; + int len = 0; + int rc = -1; + + FUNC_ENTRY; + if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + + header.byte = 0; + header.bits.type = CONNECT; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ + + if (options->MQTTVersion == 4) + { + writeCString(&ptr, "MQTT"); + writeChar(&ptr, (char) 4); + } + else + { + writeCString(&ptr, "MQIsdp"); + writeChar(&ptr, (char) 3); + } + + flags.all = 0; + flags.bits.cleansession = options->cleansession; + flags.bits.will = (options->willFlag) ? 1 : 0; + if (flags.bits.will) + { + flags.bits.willQoS = options->will.qos; + flags.bits.willRetain = options->will.retained; + } + + if (options->username.cstring || options->username.lenstring.data) + flags.bits.username = 1; + if (options->password.cstring || options->password.lenstring.data) + flags.bits.password = 1; + + writeChar(&ptr, flags.all); + writeInt(&ptr, options->keepAliveInterval); + writeMQTTString(&ptr, options->clientID); + if (options->willFlag) + { + writeMQTTString(&ptr, options->will.topicName); + writeMQTTString(&ptr, options->will.message); + } + if (flags.bits.username) + writeMQTTString(&ptr, options->username); + if (flags.bits.password) + writeMQTTString(&ptr, options->password); + + rc = ptr - buf; + + exit: FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Deserializes the supplied (wire) buffer into connack data - return code + * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) + * @param connack_rc returned integer value of the connack return code + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param len the length in bytes of the data in the supplied buffer + * @return error code. 1 is success, 0 is failure + */ +int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) +{ + MQTTHeader header = {0}; + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen; + MQTTConnackFlags flags = {0}; + + FUNC_ENTRY; + header.byte = readChar(&curdata); + if (header.bits.type != CONNACK) + goto exit; + + curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ + enddata = curdata + mylen; + if (enddata - curdata < 2) + goto exit; + + flags.all = readChar(&curdata); + *sessionPresent = flags.bits.sessionpresent; + *connack_rc = readChar(&curdata); + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer, to avoid overruns + * @param packettype the message type + * @return serialized length, or error if 0 + */ +int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) +{ + MQTTHeader header = {0}; + int rc = -1; + unsigned char *ptr = buf; + + FUNC_ENTRY; + if (buflen < 2) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + header.byte = 0; + header.bits.type = packettype; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */ + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer, to avoid overruns + * @return serialized length, or error if 0 + */ +int MQTTSerialize_disconnect(unsigned char* buf, int buflen) +{ + return MQTTSerialize_zero(buf, buflen, DISCONNECT); +} + + +/** + * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer, to avoid overruns + * @return serialized length, or error if 0 + */ +int MQTTSerialize_pingreq(unsigned char* buf, int buflen) +{ + return MQTTSerialize_zero(buf, buflen, PINGREQ); +} diff --git a/esp-vent-main/src/MQTT/MQTTConnectServer.c b/esp-vent-main/src/MQTT/MQTTConnectServer.c new file mode 100644 index 0000000..95e0963 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTConnectServer.c @@ -0,0 +1,148 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include "../inc/MQTT/MQTTPacket.h" +#include "../inc/MQTT/StackTrace.h" +#include + +#define min(a, b) ((a < b) ? a : b) + + +/** + * Validates MQTT protocol name and version combinations + * @param protocol the MQTT protocol name as an MQTTString + * @param version the MQTT protocol version number, as in the connect packet + * @return correct MQTT combination? 1 is true, 0 is false + */ +int MQTTPacket_checkVersion(MQTTString* protocol, int version) +{ + int rc = 0; + + if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp", + min(6, protocol->lenstring.len)) == 0) + rc = 1; + else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT", + min(4, protocol->lenstring.len)) == 0) + rc = 1; + return rc; +} + + +/** + * Deserializes the supplied (wire) buffer into connect data structure + * @param data the connect data structure to be filled out + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param len the length in bytes of the data in the supplied buffer + * @return error code. 1 is success, 0 is failure + */ +int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len) +{ + MQTTHeader header = {0}; + MQTTConnectFlags flags = {0}; + unsigned char* curdata = buf; + unsigned char* enddata = &buf[len]; + int rc = 0; + MQTTString Protocol; + int version; + int mylen = 0; + + FUNC_ENTRY; + header.byte = readChar(&curdata); + if (header.bits.type != CONNECT) + goto exit; + + curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */ + + if (!readMQTTLenString(&Protocol, &curdata, enddata) || + enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */ + goto exit; + + version = (int)readChar(&curdata); /* Protocol version */ + /* If we don't recognize the protocol version, we don't parse the connect packet on the + * basis that we don't know what the format will be. + */ + if (MQTTPacket_checkVersion(&Protocol, version)) + { + flags.all = readChar(&curdata); + data->cleansession = flags.bits.cleansession; + data->keepAliveInterval = readInt(&curdata); + if (!readMQTTLenString(&data->clientID, &curdata, enddata)) + goto exit; + data->willFlag = flags.bits.will; + if (flags.bits.will) + { + data->will.qos = flags.bits.willQoS; + data->will.retained = flags.bits.willRetain; + if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) || + !readMQTTLenString(&data->will.message, &curdata, enddata)) + goto exit; + } + if (flags.bits.username) + { + if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata)) + goto exit; /* username flag set, but no username supplied - invalid */ + if (flags.bits.password && + (enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata))) + goto exit; /* password flag set, but no password supplied - invalid */ + } + else if (flags.bits.password) + goto exit; /* password flag set without username - invalid */ + rc = 1; + } +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Serializes the connack packet into the supplied buffer. + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param connack_rc the integer connack return code to be used + * @param sessionPresent the MQTT 3.1.1 sessionPresent flag + * @return serialized length, or error if 0 + */ +int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent) +{ + MQTTHeader header = {0}; + int rc = 0; + unsigned char *ptr = buf; + MQTTConnackFlags flags = {0}; + + FUNC_ENTRY; + if (buflen < 2) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + header.byte = 0; + header.bits.type = CONNACK; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ + + flags.all = 0; + flags.bits.sessionpresent = sessionPresent; + writeChar(&ptr, flags.all); + writeChar(&ptr, connack_rc); + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + diff --git a/esp-vent-main/src/MQTT/MQTTDeserializePublish.c b/esp-vent-main/src/MQTT/MQTTDeserializePublish.c new file mode 100644 index 0000000..d3a5e9e --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTDeserializePublish.c @@ -0,0 +1,107 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include "../inc/MQTT/StackTrace.h" +#include "../inc/MQTT/MQTTPacket.h" +#include + +#define min(a, b) ((a < b) ? 1 : 0) + +/** + * Deserializes the supplied (wire) buffer into publish data + * @param dup returned integer - the MQTT dup flag + * @param qos returned integer - the MQTT QoS value + * @param retained returned integer - the MQTT retained flag + * @param packetid returned integer - the MQTT packet identifier + * @param topicName returned MQTTString - the MQTT topic in the publish + * @param payload returned byte buffer - the MQTT publish payload + * @param payloadlen returned integer - the length of the MQTT payload + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success + */ +int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, + unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) +{ + MQTTHeader header = {0}; + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen = 0; + + FUNC_ENTRY; + header.byte = readChar(&curdata); + if (header.bits.type != PUBLISH) + goto exit; + *dup = header.bits.dup; + *qos = header.bits.qos; + *retained = header.bits.retain; + + curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ + enddata = curdata + mylen; + + if (!readMQTTLenString(topicName, &curdata, enddata) || + enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */ + goto exit; + + if (*qos > 0) + *packetid = readInt(&curdata); + + *payloadlen = enddata - curdata; + *payload = curdata; + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + + +/** + * Deserializes the supplied (wire) buffer into an ack + * @param packettype returned integer - the MQTT packet type + * @param dup returned integer - the MQTT dup flag + * @param packetid returned integer - the MQTT packet identifier + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success, 0 is failure + */ +int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) +{ + MQTTHeader header = {0}; + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen; + + FUNC_ENTRY; + header.byte = readChar(&curdata); + *dup = header.bits.dup; + *packettype = header.bits.type; + + curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ + enddata = curdata + mylen; + + if (enddata - curdata < 2) + goto exit; + *packetid = readInt(&curdata); + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + diff --git a/esp-vent-main/src/MQTT/MQTTFormat.c b/esp-vent-main/src/MQTT/MQTTFormat.c new file mode 100644 index 0000000..c555840 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTFormat.c @@ -0,0 +1,261 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include +#include "../inc/MQTT/StackTrace.h" +#include + + +const char* MQTTPacket_names[] = +{ + "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL", + "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", + "PINGREQ", "PINGRESP", "DISCONNECT" +}; + + +const char* MQTTPacket_getName(unsigned short packetid) +{ + return MQTTPacket_names[packetid]; +} + + +int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data) +{ + int strindex = 0; + + strindex = snprintf(strbuf, strbuflen, + "CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d", + (int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data, + (int)data->cleansession, data->keepAliveInterval); + if (data->willFlag) + strindex += snprintf(&strbuf[strindex], strbuflen - strindex, + ", will QoS %d, will retain %d, will topic %.*s, will message %.*s", + data->will.qos, data->will.retained, + data->will.topicName.lenstring.len, data->will.topicName.lenstring.data, + data->will.message.lenstring.len, data->will.message.lenstring.data); + if (data->username.lenstring.data && data->username.lenstring.len > 0) + strindex += snprintf(&strbuf[strindex], strbuflen - strindex, + ", user name %.*s", data->username.lenstring.len, data->username.lenstring.data); + if (data->password.lenstring.data && data->password.lenstring.len > 0) + strindex += snprintf(&strbuf[strindex], strbuflen - strindex, + ", password %.*s", data->password.lenstring.len, data->password.lenstring.data); + return strindex; +} + + +int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent) +{ + int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc); + return strindex; +} + + +int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained, + unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen) +{ + int strindex = snprintf(strbuf, strbuflen, + "PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s", + dup, qos, retained, packetid, + (topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data, + payloadlen, (payloadlen < 20) ? payloadlen : 20, payload); + return strindex; +} + + +int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid) +{ + int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid); + if (dup) + strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup); + return strindex; +} + + +int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count, + MQTTString topicFilters[], int requestedQoSs[]) +{ + return snprintf(strbuf, strbuflen, + "SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d", + dup, packetid, count, + topicFilters[0].lenstring.len, topicFilters[0].lenstring.data, + requestedQoSs[0]); +} + + +int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs) +{ + return snprintf(strbuf, strbuflen, + "SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]); +} + + +int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, + int count, MQTTString topicFilters[]) +{ + return snprintf(strbuf, strbuflen, + "UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s", + dup, packetid, count, + topicFilters[0].lenstring.len, topicFilters[0].lenstring.data); +} + + +#if defined(MQTT_CLIENT) +char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen) +{ + int index = 0; + int rem_length = 0; + MQTTHeader header = {0}; + int strindex = 0; + + header.byte = buf[index++]; + index += MQTTPacket_decodeBuf(&buf[index], &rem_length); + + switch (header.bits.type) + { + + case CONNACK: + { + unsigned char sessionPresent, connack_rc; + if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1) + strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent); + } + break; + case PUBLISH: + { + unsigned char dup, retained, *payload; + unsigned short packetid; + int qos, payloadlen; + MQTTString topicName = MQTTString_initializer; + if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName, + &payload, &payloadlen, buf, buflen) == 1) + strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid, + topicName, payload, payloadlen); + } + break; + case PUBACK: + case PUBREC: + case PUBREL: + case PUBCOMP: + { + unsigned char packettype, dup; + unsigned short packetid; + if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1) + strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid); + } + break; + case SUBACK: + { + unsigned short packetid; + int maxcount = 1, count = 0; + int grantedQoSs[1]; + if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1) + strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs); + } + break; + case UNSUBACK: + { + unsigned short packetid; + if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1) + strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid); + } + break; + case PINGREQ: + case PINGRESP: + case DISCONNECT: + strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]); + break; + } + return strbuf; +} +#endif + +#if defined(MQTT_SERVER) +char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen) +{ + int index = 0; + int rem_length = 0; + MQTTHeader header = {0}; + int strindex = 0; + + header.byte = buf[index++]; + index += MQTTPacket_decodeBuf(&buf[index], &rem_length); + + switch (header.bits.type) + { + case CONNECT: + { + MQTTPacket_connectData data; + int rc; + if ((rc = MQTTDeserialize_connect(&data, buf, buflen)) == 1) + strindex = MQTTStringFormat_connect(strbuf, strbuflen, &data); + } + break; + case PUBLISH: + { + unsigned char dup, retained, *payload; + unsigned short packetid; + int qos, payloadlen; + MQTTString topicName = MQTTString_initializer; + if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName, + &payload, &payloadlen, buf, buflen) == 1) + strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid, + topicName, payload, payloadlen); + } + break; + case PUBACK: + case PUBREC: + case PUBREL: + case PUBCOMP: + { + unsigned char packettype, dup; + unsigned short packetid; + if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1) + strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid); + } + break; + case SUBSCRIBE: + { + unsigned char dup; + unsigned short packetid; + int maxcount = 1, count = 0; + MQTTString topicFilters[1]; + int requestedQoSs[1]; + if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count, + topicFilters, requestedQoSs, buf, buflen) == 1) + strindex = MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);; + } + break; + case UNSUBSCRIBE: + { + unsigned char dup; + unsigned short packetid; + int maxcount = 1, count = 0; + MQTTString topicFilters[1]; + if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1) + strindex = MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters); + } + break; + case PINGREQ: + case PINGRESP: + case DISCONNECT: + strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]); + break; + } + strbuf[strbuflen] = '\0'; + return strbuf; +} +#endif diff --git a/esp-vent-main/src/MQTT/MQTTPacket.c b/esp-vent-main/src/MQTT/MQTTPacket.c new file mode 100644 index 0000000..d6708f2 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTPacket.c @@ -0,0 +1,411 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Sergio R. Caprile - non-blocking packet read functions for stream transport + *******************************************************************************/ + +#include +#include "../inc/MQTT/StackTrace.h" +#include + +/** + * Encodes the message length according to the MQTT algorithm + * @param buf the buffer into which the encoded data is written + * @param length the length to be encoded + * @return the number of bytes written to buffer + */ +int MQTTPacket_encode(unsigned char* buf, int length) +{ + int rc = 0; + + FUNC_ENTRY; + do + { + char d = length % 128; + length /= 128; + /* if there are more digits to encode, set the top bit of this digit */ + if (length > 0) + d |= 0x80; + buf[rc++] = d; + } while (length > 0); + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Decodes the message length according to the MQTT algorithm + * @param getcharfn pointer to function to read the next character from the data source + * @param value the decoded length returned + * @return the number of bytes read from the socket + */ +int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value) +{ + unsigned char c; + int multiplier = 1; + int len = 0; +#define MAX_NO_OF_REMAINING_LENGTH_BYTES 4 + + FUNC_ENTRY; + *value = 0; + do + { + int rc = MQTTPACKET_READ_ERROR; + + if (++len > MAX_NO_OF_REMAINING_LENGTH_BYTES) + { + rc = MQTTPACKET_READ_ERROR; /* bad data */ + goto exit; + } + rc = (*getcharfn)(&c, 1); + if (rc != 1) + goto exit; + *value += (c & 127) * multiplier; + multiplier *= 128; + } while ((c & 128) != 0); +exit: + FUNC_EXIT_RC(len); + return len; +} + + +int MQTTPacket_len(int rem_len) +{ + rem_len += 1; /* header byte */ + + /* now remaining_length field */ + if (rem_len < 128) + rem_len += 1; + else if (rem_len < 16384) + rem_len += 2; + else if (rem_len < 2097151) + rem_len += 3; + else + rem_len += 4; + return rem_len; +} + + +static unsigned char* bufptr; + +int bufchar(unsigned char* c, int count) +{ + int i; + + for (i = 0; i < count; ++i) + *c = *bufptr++; + return count; +} + + +int MQTTPacket_decodeBuf(unsigned char* buf, int* value) +{ + bufptr = buf; + return MQTTPacket_decode(bufchar, value); +} + + +/** + * Calculates an integer from two bytes read from the input buffer + * @param pptr pointer to the input buffer - incremented by the number of bytes used & returned + * @return the integer value calculated + */ +int readInt(unsigned char** pptr) +{ + unsigned char* ptr = *pptr; + int len = 256*(*ptr) + (*(ptr+1)); + *pptr += 2; + return len; +} + + +/** + * Reads one character from the input buffer. + * @param pptr pointer to the input buffer - incremented by the number of bytes used & returned + * @return the character read + */ +char readChar(unsigned char** pptr) +{ + char c = **pptr; + (*pptr)++; + return c; +} + + +/** + * Writes one character to an output buffer. + * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned + * @param c the character to write + */ +void writeChar(unsigned char** pptr, char c) +{ + **pptr = c; + (*pptr)++; +} + + +/** + * Writes an integer as 2 bytes to an output buffer. + * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned + * @param anInt the integer to write + */ +void writeInt(unsigned char** pptr, int anInt) +{ + **pptr = (unsigned char)(anInt / 256); + (*pptr)++; + **pptr = (unsigned char)(anInt % 256); + (*pptr)++; +} + + +/** + * Writes a "UTF" string to an output buffer. Converts C string to length-delimited. + * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned + * @param string the C string to write + */ +void writeCString(unsigned char** pptr, const char* string) +{ + int len = strlen(string); + writeInt(pptr, len); + memcpy(*pptr, string, len); + *pptr += len; +} + + +int getLenStringLen(char* ptr) +{ + int len = 256*((unsigned char)(*ptr)) + (unsigned char)(*(ptr+1)); + return len; +} + + +void writeMQTTString(unsigned char** pptr, MQTTString mqttstring) +{ + if (mqttstring.lenstring.len > 0) + { + writeInt(pptr, mqttstring.lenstring.len); + memcpy(*pptr, mqttstring.lenstring.data, mqttstring.lenstring.len); + *pptr += mqttstring.lenstring.len; + } + else if (mqttstring.cstring) + writeCString(pptr, mqttstring.cstring); + else + writeInt(pptr, 0); +} + + +/** + * @param mqttstring the MQTTString structure into which the data is to be read + * @param pptr pointer to the output buffer - incremented by the number of bytes used & returned + * @param enddata pointer to the end of the data: do not read beyond + * @return 1 if successful, 0 if not + */ +int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata) +{ + int rc = 0; + + FUNC_ENTRY; + /* the first two bytes are the length of the string */ + if (enddata - (*pptr) > 1) /* enough length to read the integer? */ + { + mqttstring->lenstring.len = readInt(pptr); /* increments pptr to point past length */ + if (&(*pptr)[mqttstring->lenstring.len] <= enddata) + { + mqttstring->lenstring.data = (char*)*pptr; + *pptr += mqttstring->lenstring.len; + rc = 1; + } + } + mqttstring->cstring = NULL; + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Return the length of the MQTTstring - C string if there is one, otherwise the length delimited string + * @param mqttstring the string to return the length of + * @return the length of the string + */ +int MQTTstrlen(MQTTString mqttstring) +{ + int rc = 0; + + if (mqttstring.cstring) + rc = strlen(mqttstring.cstring); + else + rc = mqttstring.lenstring.len; + return rc; +} + + +/** + * Compares an MQTTString to a C string + * @param a the MQTTString to compare + * @param bptr the C string to compare + * @return boolean - equal or not + */ +int MQTTPacket_equals(MQTTString* a, char* bptr) +{ + int alen = 0, + blen = 0; + char *aptr; + + if (a->cstring) + { + aptr = a->cstring; + alen = strlen(a->cstring); + } + else + { + aptr = a->lenstring.data; + alen = a->lenstring.len; + } + blen = strlen(bptr); + + return (alen == blen) && (strncmp(aptr, bptr, alen) == 0); +} + + +/** + * Helper function to read packet data from some source into a buffer + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param getfn pointer to a function which will read any number of bytes from the needed source + * @return integer MQTT packet type, or -1 on error + * @note the whole message must fit into the caller's buffer + */ +int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)) +{ + int rc = -1; + MQTTHeader header = {0}; + int len = 0; + int rem_len = 0; + + /* 1. read the header byte. This has the packet type in it */ + if ((*getfn)(buf, 1) != 1) + goto exit; + + len = 1; + /* 2. read the remaining length. This is variable in itself */ + MQTTPacket_decode(getfn, &rem_len); + len += MQTTPacket_encode(buf + 1, rem_len); /* put the original remaining length back into the buffer */ + + /* 3. read the rest of the buffer using a callback to supply the rest of the data */ + if((rem_len + len) > buflen) + goto exit; + if (rem_len && ((*getfn)(buf + len, rem_len) != rem_len)) + goto exit; + + header.byte = buf[0]; + rc = header.bits.type; +exit: + return rc; +} + +/** + * Decodes the message length according to the MQTT algorithm, non-blocking + * @param trp pointer to a transport structure holding what is needed to solve getting data from it + * @param value the decoded length returned + * @return integer the number of bytes read from the socket, 0 for call again, or -1 on error + */ +static int MQTTPacket_decodenb(MQTTTransport *trp) +{ + unsigned char c; + int rc = MQTTPACKET_READ_ERROR; + + FUNC_ENTRY; + if(trp->len == 0){ /* initialize on first call */ + trp->multiplier = 1; + trp->rem_len = 0; + } + do { + int frc; + if (trp->len >= MAX_NO_OF_REMAINING_LENGTH_BYTES) + goto exit; + if ((frc=(*trp->getfn)(trp->sck, &c, 1)) == -1) + goto exit; + if (frc == 0){ + rc = 0; + goto exit; + } + ++(trp->len); + trp->rem_len += (c & 127) * trp->multiplier; + trp->multiplier *= 128; + } while ((c & 128) != 0); + rc = trp->len; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + +/** + * Helper function to read packet data from some source into a buffer, non-blocking + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param trp pointer to a transport structure holding what is needed to solve getting data from it + * @return integer MQTT packet type, 0 for call again, or -1 on error + * @note the whole message must fit into the caller's buffer + */ +int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp) +{ + int rc = -1, frc; + MQTTHeader header = {0}; + + switch(trp->state){ + default: + trp->state = 0; + /*FALLTHROUGH*/ + case 0: + /* read the header byte. This has the packet type in it */ + if ((frc=(*trp->getfn)(trp->sck, buf, 1)) == -1) + goto exit; + if (frc == 0) + return 0; + trp->len = 0; + ++trp->state; + /*FALLTHROUGH*/ + /* read the remaining length. This is variable in itself */ + case 1: + if((frc=MQTTPacket_decodenb(trp)) == MQTTPACKET_READ_ERROR) + goto exit; + if(frc == 0) + return 0; + trp->len = 1 + MQTTPacket_encode(buf + 1, trp->rem_len); /* put the original remaining length back into the buffer */ + if((trp->rem_len + trp->len) > buflen) + goto exit; + ++trp->state; + /*FALLTHROUGH*/ + case 2: + if(trp->rem_len){ + /* read the rest of the buffer using a callback to supply the rest of the data */ + if ((frc=(*trp->getfn)(trp->sck, buf + trp->len, trp->rem_len)) == -1) + goto exit; + if (frc == 0) + return 0; + trp->rem_len -= frc; + trp->len += frc; + if(trp->rem_len) + return 0; + } + header.byte = buf[0]; + rc = header.bits.type; + break; + } + +exit: + trp->state = 0; + return rc; +} + diff --git a/esp-vent-main/src/MQTT/MQTTSerializePublish.c b/esp-vent-main/src/MQTT/MQTTSerializePublish.c new file mode 100644 index 0000000..bb1e9c8 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTSerializePublish.c @@ -0,0 +1,169 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144 + *******************************************************************************/ + +#include +#include "../inc/MQTT/StackTrace.h" + +#include + + +/** + * Determines the length of the MQTT publish packet that would be produced using the supplied parameters + * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0) + * @param topicName the topic name to be used in the publish + * @param payloadlen the length of the payload to be sent + * @return the length of buffer needed to contain the serialized version of the packet + */ +int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen) +{ + int len = 0; + + len += 2 + MQTTstrlen(topicName) + payloadlen; + if (qos > 0) + len += 2; /* packetid */ + return len; +} + + +/** + * Serializes the supplied publish data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param dup integer - the MQTT dup flag + * @param qos integer - the MQTT QoS value + * @param retained integer - the MQTT retained flag + * @param packetid integer - the MQTT packet identifier + * @param topicName MQTTString - the MQTT topic in the publish + * @param payload byte buffer - the MQTT publish payload + * @param payloadlen integer - the length of the MQTT payload + * @return the length of the serialized data. <= 0 indicates error + */ +int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, + MQTTString topicName, unsigned char* payload, int payloadlen) +{ + unsigned char *ptr = buf; + MQTTHeader header = {0}; + int rem_len = 0; + int rc = 0; + + FUNC_ENTRY; + if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + + header.bits.type = PUBLISH; + header.bits.dup = dup; + header.bits.qos = qos; + header.bits.retain = retained; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; + + writeMQTTString(&ptr, topicName); + + if (qos > 0) + writeInt(&ptr, packetid); + + memcpy(ptr, payload, payloadlen); + ptr += payloadlen; + + rc = ptr - buf; + +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + + +/** + * Serializes the ack packet into the supplied buffer. + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param type the MQTT packet type + * @param dup the MQTT dup flag + * @param packetid the MQTT packet identifier + * @return serialized length, or error if 0 + */ +int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) +{ + MQTTHeader header = {0}; + int rc = 0; + unsigned char *ptr = buf; + + FUNC_ENTRY; + if (buflen < 4) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + header.bits.type = packettype; + header.bits.dup = dup; + header.bits.qos = (packettype == PUBREL) ? 1 : 0; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ + writeInt(&ptr, packetid); + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Serializes a puback packet into the supplied buffer. + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param packetid integer - the MQTT packet identifier + * @return serialized length, or error if 0 + */ +int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) +{ + return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid); +} + + +/** + * Serializes a pubrel packet into the supplied buffer. + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param dup integer - the MQTT dup flag + * @param packetid integer - the MQTT packet identifier + * @return serialized length, or error if 0 + */ +int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid) +{ + return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid); +} + + +/** + * Serializes a pubrel packet into the supplied buffer. + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param packetid integer - the MQTT packet identifier + * @return serialized length, or error if 0 + */ +int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid) +{ + return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid); +} + + diff --git a/esp-vent-main/src/MQTT/MQTTSubscribeClient.c b/esp-vent-main/src/MQTT/MQTTSubscribeClient.c new file mode 100644 index 0000000..79c167b --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTSubscribeClient.c @@ -0,0 +1,137 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include +#include "../inc/MQTT/StackTrace.h" + +#include + +/** + * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters + * @param count the number of topic filter strings in topicFilters + * @param topicFilters the array of topic filter strings to be used in the publish + * @return the length of buffer needed to contain the serialized version of the packet + */ +int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) +{ + int i; + int len = 2; /* packetid */ + + for (i = 0; i < count; ++i) + len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ + return len; +} + + +/** + * Serializes the supplied subscribe data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied bufferr + * @param dup integer - the MQTT dup flag + * @param packetid integer - the MQTT packet identifier + * @param count - number of members in the topicFilters and reqQos arrays + * @param topicFilters - array of topic filter names + * @param requestedQoSs - array of requested QoS + * @return the length of the serialized data. <= 0 indicates error + */ +int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, + MQTTString topicFilters[], int requestedQoSs[]) +{ + unsigned char *ptr = buf; + MQTTHeader header = {0}; + int rem_len = 0; + int rc = 0; + int i = 0; + + FUNC_ENTRY; + if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + + header.byte = 0; + header.bits.type = SUBSCRIBE; + header.bits.dup = dup; + header.bits.qos = 1; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; + + writeInt(&ptr, packetid); + + for (i = 0; i < count; ++i) + { + writeMQTTString(&ptr, topicFilters[i]); + writeChar(&ptr, requestedQoSs[i]); + } + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + + +/** + * Deserializes the supplied (wire) buffer into suback data + * @param packetid returned integer - the MQTT packet identifier + * @param maxcount - the maximum number of members allowed in the grantedQoSs array + * @param count returned integer - number of members in the grantedQoSs array + * @param grantedQoSs returned array of integers - the granted qualities of service + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success, 0 is failure + */ +int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen) +{ + MQTTHeader header = {0}; + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen; + + FUNC_ENTRY; + header.byte = readChar(&curdata); + if (header.bits.type != SUBACK) + goto exit; + + curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ + enddata = curdata + mylen; + if (enddata - curdata < 2) + goto exit; + + *packetid = readInt(&curdata); + + *count = 0; + while (curdata < enddata) + { + if (*count > maxcount) + { + rc = -1; + goto exit; + } + grantedQoSs[(*count)++] = readChar(&curdata); + } + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + diff --git a/esp-vent-main/src/MQTT/MQTTSubscribeServer.c b/esp-vent-main/src/MQTT/MQTTSubscribeServer.c new file mode 100644 index 0000000..e4f42e7 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTSubscribeServer.c @@ -0,0 +1,112 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include "../inc/MQTT/MQTTPacket.h" +#include "../inc/MQTT/StackTrace.h" + +#include + + +/** + * Deserializes the supplied (wire) buffer into subscribe data + * @param dup integer returned - the MQTT dup flag + * @param packetid integer returned - the MQTT packet identifier + * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays + * @param count - number of members in the topicFilters and requestedQoSs arrays + * @param topicFilters - array of topic filter names + * @param requestedQoSs - array of requested QoS + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return the length of the serialized data. <= 0 indicates error + */ +int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], + int requestedQoSs[], unsigned char* buf, int buflen) +{ + MQTTHeader header = {0}; + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = -1; + int mylen = 0; + + FUNC_ENTRY; + header.byte = readChar(&curdata); + if (header.bits.type != SUBSCRIBE) + goto exit; + *dup = header.bits.dup; + + curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ + enddata = curdata + mylen; + + *packetid = readInt(&curdata); + + *count = 0; + while (curdata < enddata) + { + if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) + goto exit; + if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */ + goto exit; + requestedQoSs[*count] = readChar(&curdata); + (*count)++; + } + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Serializes the supplied suback data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param packetid integer - the MQTT packet identifier + * @param count - number of members in the grantedQoSs array + * @param grantedQoSs - array of granted QoS + * @return the length of the serialized data. <= 0 indicates error + */ +int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs) +{ + MQTTHeader header = {0}; + int rc = -1; + unsigned char *ptr = buf; + int i; + + FUNC_ENTRY; + if (buflen < 2 + count) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + header.byte = 0; + header.bits.type = SUBACK; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, 2 + count); /* write remaining length */ + + writeInt(&ptr, packetid); + + for (i = 0; i < count; ++i) + writeChar(&ptr, grantedQoSs[i]); + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + diff --git a/esp-vent-main/src/MQTT/MQTTUnsubscribeClient.c b/esp-vent-main/src/MQTT/MQTTUnsubscribeClient.c new file mode 100644 index 0000000..94bd206 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTUnsubscribeClient.c @@ -0,0 +1,106 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include "../inc/MQTT/MQTTPacket.h" +#include "../inc/MQTT/StackTrace.h" + +#include + +/** + * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters + * @param count the number of topic filter strings in topicFilters + * @param topicFilters the array of topic filter strings to be used in the publish + * @return the length of buffer needed to contain the serialized version of the packet + */ +int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) +{ + int i; + int len = 2; /* packetid */ + + for (i = 0; i < count; ++i) + len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/ + return len; +} + + +/** + * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @param dup integer - the MQTT dup flag + * @param packetid integer - the MQTT packet identifier + * @param count - number of members in the topicFilters array + * @param topicFilters - array of topic filter names + * @return the length of the serialized data. <= 0 indicates error + */ +int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, + int count, MQTTString topicFilters[]) +{ + unsigned char *ptr = buf; + MQTTHeader header = {0}; + int rem_len = 0; + int rc = -1; + int i = 0; + + FUNC_ENTRY; + if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + + header.byte = 0; + header.bits.type = UNSUBSCRIBE; + header.bits.dup = dup; + header.bits.qos = 1; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; + + writeInt(&ptr, packetid); + + for (i = 0; i < count; ++i) + writeMQTTString(&ptr, topicFilters[i]); + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Deserializes the supplied (wire) buffer into unsuback data + * @param packetid returned integer - the MQTT packet identifier + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return error code. 1 is success, 0 is failure + */ +int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) +{ + unsigned char type = 0; + unsigned char dup = 0; + int rc = 0; + + FUNC_ENTRY; + rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen); + if (type == UNSUBACK) + rc = 1; + FUNC_EXIT_RC(rc); + return rc; +} + + diff --git a/esp-vent-main/src/MQTT/MQTTUnsubscribeServer.c b/esp-vent-main/src/MQTT/MQTTUnsubscribeServer.c new file mode 100644 index 0000000..4e91ff1 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTTUnsubscribeServer.c @@ -0,0 +1,102 @@ +/******************************************************************************* + * Copyright (c) 2014 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Ian Craggs - initial API and implementation and/or initial documentation + *******************************************************************************/ + +#include +#include "../inc/MQTT/StackTrace.h" + +#include + + +/** + * Deserializes the supplied (wire) buffer into unsubscribe data + * @param dup integer returned - the MQTT dup flag + * @param packetid integer returned - the MQTT packet identifier + * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays + * @param count - number of members in the topicFilters and requestedQoSs arrays + * @param topicFilters - array of topic filter names + * @param buf the raw buffer data, of the correct length determined by the remaining length field + * @param buflen the length in bytes of the data in the supplied buffer + * @return the length of the serialized data. <= 0 indicates error + */ +int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], + unsigned char* buf, int len) +{ + MQTTHeader header = {0}; + unsigned char* curdata = buf; + unsigned char* enddata = NULL; + int rc = 0; + int mylen = 0; + + FUNC_ENTRY; + header.byte = readChar(&curdata); + if (header.bits.type != UNSUBSCRIBE) + goto exit; + *dup = header.bits.dup; + + curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ + enddata = curdata + mylen; + + *packetid = readInt(&curdata); + + *count = 0; + while (curdata < enddata) + { + if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) + goto exit; + (*count)++; + } + + rc = 1; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + +/** + * Serializes the supplied unsuback data into the supplied buffer, ready for sending + * @param buf the buffer into which the packet will be serialized + * @param buflen the length in bytes of the supplied buffer + * @param packetid integer - the MQTT packet identifier + * @return the length of the serialized data. <= 0 indicates error + */ +int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid) +{ + MQTTHeader header = {0}; + int rc = 0; + unsigned char *ptr = buf; + + FUNC_ENTRY; + if (buflen < 2) + { + rc = MQTTPACKET_BUFFER_TOO_SHORT; + goto exit; + } + header.byte = 0; + header.bits.type = UNSUBACK; + writeChar(&ptr, header.byte); /* write header */ + + ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ + + writeInt(&ptr, packetid); + + rc = ptr - buf; +exit: + FUNC_EXIT_RC(rc); + return rc; +} + + diff --git a/esp-vent-main/src/MQTT/MQTT_lpc1549.c b/esp-vent-main/src/MQTT/MQTT_lpc1549.c new file mode 100644 index 0000000..0e32197 --- /dev/null +++ b/esp-vent-main/src/MQTT/MQTT_lpc1549.c @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (c) 2014, 2015 IBM Corp. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v10.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.php. + * + * Contributors: + * Allan Stockdill-Mander - initial API and implementation and/or initial documentation + * Ian Craggs - convert to FreeRTOS + *******************************************************************************/ + +#include +#include +#include "systick.h" +#include "../inc/MQTT/esp8266_socket.h" + + + + +void TimerCountdownMS(Timer* timer, unsigned int timeout_ms) +{ + timer->TicksToWait = timeout_ms; + timer->Start = get_ticks(); +} + + +void TimerCountdown(Timer* timer, unsigned int timeout) +{ + TimerCountdownMS(timer, timeout * 1000); +} + + +int TimerLeftMS(Timer* timer) +{ + int32_t left = (int32_t)timer->TicksToWait - (int32_t)(get_ticks() - timer->Start); + return left < 0 ? 0 : left; +} + + +char TimerIsExpired(Timer* timer) +{ + return (get_ticks() - timer->Start) > timer->TicksToWait; +} + + +void TimerInit(Timer* timer) +{ + timer->TicksToWait = 0; + timer->Start = 0; +} + + +int lpc1549_read(Network* n, unsigned char* buffer, int len, int timeout_ms) +{ + int recvLen = 0; + Timer timer; + + TimerCountdownMS(&timer, timeout_ms); + + do + { + int rc = 0; + + rc = esp_read(n->my_socket, buffer + recvLen, len - recvLen); + if (rc > 0) + recvLen += rc; + else if (rc < 0) + { + recvLen = rc; + break; + } + } while (recvLen < len && !TimerIsExpired(&timer)); + + return recvLen; +} + + +int lpc1549_write(Network* n, unsigned char* buffer, int len, int timeout_ms) +{ + int sentLen = 0; + Timer timer; + + TimerCountdownMS(&timer, timeout_ms); + + + do + { + int rc = 0; + + rc = esp_write(n->my_socket, buffer + sentLen, len - sentLen); + if (rc > 0) + sentLen += rc; + else if (rc < 0) + { + sentLen = rc; + break; + } + } while (sentLen < len && !TimerIsExpired(&timer)); + +return sentLen; +} + + +void lpc1549_disconnect(Network* n) +{ + esp_close(n->my_socket); +} + + +void NetworkInit(Network *n, const char *ssid, const char *password) +{ + n->mqttread = lpc1549_read; + n->mqttwrite = lpc1549_write; + n->disconnect = lpc1549_disconnect; + + strncpy(n->ssid, ssid, 32); + strncpy(n->password, password, 32); + + n->my_socket = esp_socket(n->ssid, n->password); + + +} + + +int NetworkConnect(Network* n, char* address, int port) +{ + return esp_connect(n->my_socket, address, port); +} + +void NetworkDisconnect(Network *n) +{ + n->disconnect(n); +} + diff --git a/esp-vent-main/src/MQTT/esp8266_socket.c b/esp-vent-main/src/MQTT/esp8266_socket.c new file mode 100644 index 0000000..92d2688 --- /dev/null +++ b/esp-vent-main/src/MQTT/esp8266_socket.c @@ -0,0 +1,755 @@ +/* ======================================== + * + * Copyright YOUR COMPANY, THE YEAR + * All Rights Reserved + * UNPUBLISHED, LICENSED SOFTWARE. + * + * CONFIDENTIAL AND PROPRIETARY INFORMATION + * WHICH IS THE PROPERTY OF your company. + * + * ======================================== +*/ +#include +#include +#include +#include +#include + +#include "ring_buffer.h" + +#include "../inc/MQTT/esp8266_socket.h" + +#include "serial_port.h" + +typedef int EspSocket_t; + +uint32_t get_ticks(void); // defined externally + + +#define I_DONT_USE(x) (void) x +#define DEBUGP( ... ) printf( __VA_ARGS__ ) + +// macro for changing state. Correct operation requires that ctx is a pointer to state machine instance (see state template) +#define TRAN(st) ctx->next_state = st + +typedef enum eventTypes { eEnter, eExit, eTick, eReceive, eConnect, eDisconnect, eSend } EventType; + +typedef struct event_ { + EventType ev; // event type (= what happened) + // we could add additional data +} event; + +static const event evEnter = { eEnter }; +static const event evExit = { eExit }; + + +typedef struct smi_ smi; + +typedef void (*smf)(smi *, const event *); // prototype of state handler function pointer + +#define EVQ_SIZE 32 + +#define SMI_BUFSIZE 80 +#define RC_NOT_AVAILABLE -1 +#define RC_OK 0 +#define RC_ERROR 1 + +struct smi_ { + smf state; // current state (function pointer) + smf next_state; // next state (function pointer) + RINGBUFF_T EspEventQ; + event evq_buf[EVQ_SIZE]; + int timer; + int count; + int pos; + char buffer[SMI_BUFSIZE]; + char ssid[32]; // SSID + char pwd[32]; // password + char sa_data[32]; // ip address + char sa_port[14]; // port number (string) +}; + +static void stInit(smi *ctx, const event *e); +static void stEchoOff(smi *ctx, const event *e); +static void stStationModeCheck(smi *ctx, const event *e); +static void stStationModeSet(smi *ctx, const event *e); +static void stConnectAP(smi *ctx, const event *e); +static void stReady(smi *ctx, const event *e); +static void stConnectTCP(smi *ctx, const event *e); +static void stConnected(smi *ctx, const event *e); +static void stCloseTCP(smi *ctx, const event *e); +static void stPassthrough(smi *ctx, const event *e); +static void stPassthroughOK(smi *ctx, const event *e); +static void stAT(smi *ctx, const event *e); +static void stCommandMode(smi *ctx, const event *e); + +static void EspSocketRun(smi *ctx); + + + +smi EspSocketInstance; + +static void port2str(int i, char *str) +{ + int m = 100000; + i %= m; // limit integer size to max 5 digits. + while(i / m == 0) m/=10; + while(m > 0) { + *str++ = '0' + i / m; + i %= m; + m /= 10; + } + *str='\0'; +} + +void smi_init(smi *ctx) +{ + serial_init(ctx); + memset(ctx, 0, sizeof(smi)); + ctx->state = stInit; + ctx->next_state = stInit; + RingBuffer_Init(&ctx->EspEventQ, ctx->evq_buf, sizeof(event), EVQ_SIZE); + + ctx->state(ctx, &evEnter); // enter initial state +} + + +int esp_socket(const char *ssid, const char *password) +{ + smi_init(&EspSocketInstance); + + + strncpy(EspSocketInstance.ssid, ssid, 32); + strncpy(EspSocketInstance.pwd, password, 32); + + + while(EspSocketInstance.state != stReady) { + // run esp task and run ticks + EspSocketRun(&EspSocketInstance); + } + + return 0; +} + + + +int esp_connect(int sockfd, const char *addr, int port) +{ + I_DONT_USE(sockfd); + + strncpy(EspSocketInstance.sa_data,addr,sizeof(EspSocketInstance.sa_data)-1); + EspSocketInstance.sa_data[sizeof(EspSocketInstance.sa_data)-1] = '\0'; + port2str(port, EspSocketInstance.sa_port); + + const event e = { eConnect }; + + RingBuffer_Insert(&EspSocketInstance.EspEventQ,&e); + + int rc = 0; + + while(EspSocketInstance.state != stConnected) { + // run esp task and run ticks + EspSocketRun(&EspSocketInstance); + } + + return rc; +} + +int esp_read(int sockfd, void *data, int length) +{ + I_DONT_USE(sockfd); + int count = 0; + + if(EspSocketInstance.state == stConnected) { + // read data + while(count < length && serial_get_char(&EspSocketInstance, data)) { + ++count; + ++data; + } + } + + return count; +} + +int esp_write(int sockfd, const void *data, int length) +{ + I_DONT_USE(sockfd); + + if(EspSocketInstance.state == stConnected) { + // write data + serial_write_buf(&EspSocketInstance, data, length); + } + + return length; +} + +int esp_close(int sockfd) +{ + return esp_shutdown(sockfd, -1); +} + +int esp_shutdown(int sockfd, int how) +{ + I_DONT_USE(sockfd); + I_DONT_USE(how); + const event e = { eDisconnect }; + + RingBuffer_Insert(&EspSocketInstance.EspEventQ,&e); + while(EspSocketInstance.state != stReady) { + // run esp task and run ticks + EspSocketRun(&EspSocketInstance); + } + + return 0; +} + + + +#if 0 +/* this is state template */ +void stStateTemplate(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + break; + case eExit: + break; + case eTick: + break; + case eReceive: + break; + default: + break; + } +} +#endif + +void init_counters(smi *ctx) { + ctx->count = 0; + ctx->pos = 0; + ctx->timer = 0; +} + +/* Read and store characters upto specified length. + * Returns true when specified amount of characters have been accumulated. */ +void sm_flush(smi *ctx) +{ + //DEBUGP("flush: %d\n", (int)xSerialRxWaiting(ctx->ComPort)); + while(serial_get_char(ctx, ctx->buffer)); +} + + +/* Read and store characters upto specified length. + * Returns true when specified amount of characters have been accumulated. */ +bool sm_read_buffer(smi *ctx, int count) +{ + while(ctx->pos < (SMI_BUFSIZE - 1) && ctx->pos < count && serial_get_char(ctx, ctx->buffer + ctx->pos)) { + //putchar(ctx->buffer[ctx->pos]); // debugging + ++ctx->pos; + } + return (ctx->pos >= count); +} + + +/* Read an integer. + * Consumes characters until a non-nondigit is received. The nondigit is also consumed. */ +bool sm_read_int(smi *ctx, int *value) +{ + bool result = false; + while(ctx->pos < (SMI_BUFSIZE - 1) && serial_get_char(ctx, ctx->buffer + ctx->pos)) { + if(!isdigit((int)ctx->buffer[ctx->pos])) { + ctx->buffer[ctx->pos] = '\0'; + *value = atoi(ctx->buffer); + result = true; + break; + } + else { + ++ctx->pos; + } + } + return result; +} + + + +/* Read and store data until one of the specified strings is received. + * The matched string is also included in the data .*/ +int sm_read_until(smi *ctx, const char **p) +{ + int result = RC_NOT_AVAILABLE; + while(result < 0 && ctx->pos < (SMI_BUFSIZE - 1) && serial_get_char(ctx, ctx->buffer + ctx->pos)) { + ++ctx->pos; + ctx->buffer[ctx->pos] = '\0'; + for(int i = 0; result < 0 && p[i] != NULL; ++i) { + if(strstr(ctx->buffer, p[i]) != NULL) { + result = i; + } + } + } + return result; + +} + +/* read and store data until result is received */ +int sm_read_result(smi *ctx) +{ + static const char *result_list[] = { "OK\r\n", "ERROR\r\n", NULL }; + return sm_read_until(ctx, result_list); +} + +/* read and consume characters until specified string occurs */ +bool sm_wait_for(smi *ctx, const char *p) +{ + bool result = false; + int len = strlen(p); + + while(sm_read_buffer(ctx, len)) { + ctx->buffer[ctx->pos] = '\0'; + if(strstr(ctx->buffer, p) != NULL) { + result = true; + break; + } + else { + memmove(ctx->buffer, ctx->buffer + 1, ctx->pos); + --ctx->pos; + } + } + return result; +} + +static void stInit(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stInit\r\n"); + sm_flush(ctx); + init_counters(ctx); + serial_write_str(ctx, "AT\r\n"); + break; + case eExit: + break; + case eTick: + ++ctx->timer; + //if(ctx->timer == 5) DEBUGP("[%s]", ctx->buffer); + if(ctx->timer >= 10) { + ctx->timer = 0; + ++ctx->count; + if(ctx->count < 5) { + serial_write_str(ctx, "AT\r\n"); + } + else { + DEBUGP("Error: Module not responding\r\n"); + TRAN(stAT); + } + } + break; + case eReceive: + if(sm_wait_for(ctx, "OK\r\n")) { + TRAN(stEchoOff); + } + break; + default: + break; + } +} + +static void stAT(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stAT\r\n"); + init_counters(ctx); + break; + case eExit: + break; + case eTick: + ++ctx->timer; + if(ctx->timer == 10) serial_write_str(ctx, "+++"); + if(ctx->timer == 25) TRAN(stInit); + break; + case eReceive: + break; + default: + break; + } +} + +static void stEchoOff(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stEchoOff\r\n"); + sm_flush(ctx); + init_counters(ctx); + serial_write_str(ctx, "ATE0\r\n"); + break; + case eExit: + break; + case eTick: + ++ctx->timer; + if(ctx->timer >= 10) { + ++ctx->count; + if(ctx->count < 3) { + serial_write_str(ctx, "ATE0\r\n"); + } + else { + DEBUGP("Error: setting local echo off failed\r\n"); + TRAN(stInit); + } + } + break; + case eReceive: + if(sm_wait_for(ctx, "OK\r\n")) { + TRAN(stStationModeCheck); + } + break; + default: + break; + } +} + + +static void stStationModeCheck(smi *ctx, const event *e) +{ + int rc = -1; + switch(e->ev) { + case eEnter: + DEBUGP("stStationModeCheck\r\n"); + init_counters(ctx); + serial_write_str(ctx, "AT+CWMODE_CUR?\r\n"); + break; + case eExit: + break; + case eTick: + break; + case eReceive: + rc = sm_read_result(ctx); + if(rc == RC_OK) { + //DEBUGP("%d: %s", rc, ctx->buffer); + if(strstr(ctx->buffer, "+CWMODE_CUR:1\r\n") != NULL) { + TRAN(stConnectAP); + } + else { + TRAN(stStationModeSet); + } + } + else if(rc == RC_ERROR) { + } + break; + default: + break; + } +} + + +static void stStationModeSet(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stStationModeSet\r\n"); + init_counters(ctx); + serial_write_str(ctx, "AT+CWMODE_CUR=1\r\n"); + break; + case eExit: + break; + case eTick: + break; + case eReceive: + if(sm_wait_for(ctx, "OK\r\n")) { + TRAN(stStationModeCheck); + } + break; + default: + break; + } +} + +static void connect_ssid(smi *ctx) +{ + serial_write_str(ctx, "AT+CWJAP_CUR=\""); + serial_write_str(ctx, ctx->ssid); + serial_write_str(ctx, "\",\""); + serial_write_str(ctx, ctx->pwd); + serial_write_str(ctx, "\"\r\n"); +} + +static void stConnectAP(smi *ctx, const event *e) +{ + int rc; + switch(e->ev) { + case eEnter: + DEBUGP("stConnectAP\r\n"); + init_counters(ctx); + break; + case eExit: + break; + case eTick: + // may take upto 7 seconds. do we need a timeout? + ++ctx->timer; + if(ctx->timer == 1) { + connect_ssid(ctx); + } + if(ctx->timer >= 70) { + ctx->timer = 0; + } + break; + case eReceive: + rc = sm_read_result(ctx); + if(rc == RC_OK) { + //DEBUGP("%d: %s", rc, ctx->buffer); + TRAN(stReady); + } + else if(rc == RC_ERROR) { + // failed: what to do now? + } + break; + default: + break; + } +} + + +static void stReady(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stReady\r\n"); + init_counters(ctx); + break; + case eExit: + break; + case eTick: + break; + case eReceive: + break; + case eConnect: + TRAN(stConnectTCP); + break; + default: + break; + } + +} + + + +static void connect_tcp(smi *ctx) +{ + serial_write_str(ctx, "AT+CIPSTART=\"TCP\",\""); + serial_write_str(ctx, ctx->sa_data); + serial_write_str(ctx, "\","); + serial_write_str(ctx, ctx->sa_port); + serial_write_str(ctx, "\r\n"); +} + +static void stConnectTCP(smi *ctx, const event *e) +{ + int rc; + switch(e->ev) { + case eEnter: + DEBUGP("stConnectTCP\r\n"); + init_counters(ctx); + connect_tcp(ctx); + break; + case eExit: + break; + case eTick: + break; + case eReceive: + rc = sm_read_result(ctx); + if(rc == RC_OK) { + //DEBUGP("%d: %s", rc, ctx->buffer); + if(strstr(ctx->buffer, "CONNECT\r\n") != NULL) { + TRAN(stPassthrough); + } + else { + // what else can we get with OK?? + } + } + else if(rc == RC_ERROR) { + // failed: what to do now? + DEBUGP("Connect failed\r\n"); + connect_tcp(ctx); + } + break; + default: + break; + } +} + + +static void stPassthrough(smi *ctx, const event *e) +{ + int rc; + switch(e->ev) { + case eEnter: + DEBUGP("stPassthrough\r\n"); + init_counters(ctx); + serial_write_str(ctx, "AT+CIPMODE=1\r\n"); + break; + case eExit: + break; + case eTick: + break; + case eReceive: + rc = sm_read_result(ctx); + if(rc == RC_OK) { + TRAN(stPassthroughOK); + } + else if(rc == RC_ERROR) { + // failed: what to do now? + } + break; + default: + break; + } + +} + +static void stPassthroughOK(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stPassthroughOK\r\n"); + init_counters(ctx); + serial_write_str(ctx, "AT+CIPSEND\r\n"); + break; + case eExit: + break; + case eTick: + break; + case eReceive: + if(sm_wait_for(ctx, ">")) { + TRAN(stConnected); + } + break; + default: + break; + } + +} + + +static void stConnected(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stConnected\r\n"); + init_counters(ctx); + break; + case eExit: + break; + case eTick: + break; + case eReceive: + break; + case eDisconnect: + TRAN(stCommandMode); + break; + default: + break; + } + +} + + +static void stCommandMode(smi *ctx, const event *e) +{ + switch(e->ev) { + case eEnter: + DEBUGP("stCommandMode\r\n"); + init_counters(ctx); + break; + case eExit: + break; + case eTick: + ++ctx->timer; + if(ctx->timer == 10) serial_write_str(ctx, "+++"); + if(ctx->timer == 25) TRAN(stCloseTCP); + break; + case eReceive: + break; + default: + break; + } +} + + +static void stCloseTCP(smi *ctx, const event *e) +{ + int rc = -1; + switch(e->ev) { + case eEnter: + DEBUGP("stCloseTCP\r\n"); + init_counters(ctx); + serial_write_str(ctx, "AT+CIPMODE=0\r\n");; + break; + case eReceive: + rc = sm_read_result(ctx); + if(rc == RC_OK) { + if(strstr(ctx->buffer, "CLOSED") != NULL) { + TRAN(stReady); + } + else { + init_counters(ctx); + serial_write_str(ctx, "AT+CIPCLOSE\r\n");; + } + } + else if(rc == RC_ERROR) { + } + break; + case eExit: + break; + case eTick: + break; + default: + break; + } +} + + + +static void dispatch_event(smi *ctx, const event *e) +{ + ctx->state(ctx, e); // dispatch event to current state + if(ctx->state != ctx->next_state) { // check if state was changed + ctx->state(ctx, &evExit); // exit old state (cleanup) + ctx->state = ctx->next_state; // change state + ctx->state(ctx, &evEnter); // enter new state + } +} + + + +/** + * Receive events from queue and dispatch them to state machine + */ +static void EspSocketRun(smi *ctx) +{ + event e; + + static uint32_t old = 0 ; + uint32_t now = 0 ; + + now = get_ticks()/100; + if(now != old) { + const event tick = { eTick }; + old = now; + // send ESP tick + RingBuffer_Insert(&ctx->EspEventQ, &tick); + } + + if(serial_peek(ctx)) { + const event rcv = { eReceive }; + RingBuffer_Insert(&ctx->EspEventQ, &rcv); + } + + // read queue + while (RingBuffer_Pop(&ctx->EspEventQ,&e)) { + dispatch_event(ctx, &e); // dispatch event to current state + } +} + + + +/* [] END OF FILE */ diff --git a/esp-vent-main/src/esp-vent-main.cpp b/esp-vent-main/src/esp-vent-main.cpp index 63d570d..0988fce 100644 --- a/esp-vent-main/src/esp-vent-main.cpp +++ b/esp-vent-main/src/esp-vent-main.cpp @@ -15,11 +15,17 @@ #include "StateHandler/StateHandler.h" #include "SwitchController.h" #include "Timer.h" +#include #include "board.h" #include "chip.h" #include "common_control_values.h" #include +#define SSID "SmartIotMQTT" +#define PASSWORD "SmartIot" +#define BROKER_IP "192.168.1.254" +#define BROKER_PORT 1883 + int main (void) { @@ -85,3 +91,4 @@ main (void) return 0; } + diff --git a/esp-vent-main/src/serial_port.cpp b/esp-vent-main/src/serial_port.cpp new file mode 100644 index 0000000..3f14768 --- /dev/null +++ b/esp-vent-main/src/serial_port.cpp @@ -0,0 +1,49 @@ +/* + * serial_port.cpp + * + * Created on: 25.8.2021 + * Author: keijo + */ +#include "../inc/Modbus/Uart.h" +#include "serial_port.h" + +#ifdef __cplusplus +extern "C" { +#endif + +static LpcUart *EspUart; + +void serial_init(void *ctx) +{ + LpcPinMap none = {-1, -1}; // unused pin has negative values in it + LpcPinMap txpin_esp = { 0, 8 }; // transmit pin + LpcPinMap rxpin_esp = { 1, 6 }; // receive pin + LpcUartConfig cfg = { LPC_USART2, 115200, UART_CFG_DATALEN_8 | UART_CFG_PARITY_NONE | UART_CFG_STOPLEN_1, false, txpin_esp, rxpin_esp, none, none }; + + EspUart = new LpcUart(cfg); + +} + +void serial_write_buf(void *ctx, const char *buf, int len) +{ + EspUart->write(buf, len); +} + +void serial_write_str(void *ctx, const char *s) +{ + EspUart->write(s); +} + +int serial_get_char(void *ctx, char *p) +{ + return EspUart->read(*p); +} + +int serial_peek(void *ctx) +{ + return EspUart->peek(); +} + +#ifdef __cplusplus +} +#endif diff --git a/lpc_board_nxp_lpcxpresso_1549/.cproject b/lpc_board_nxp_lpcxpresso_1549/.cproject index 4b75152..da69100 100644 --- a/lpc_board_nxp_lpcxpresso_1549/.cproject +++ b/lpc_board_nxp_lpcxpresso_1549/.cproject @@ -11,13 +11,13 @@ + + - - @@ -28,18 +28,18 @@ - - @@ -74,13 +74,13 @@ + + - - @@ -139,7 +139,7 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="70200"/> +<Properties property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="100300"/> <infoList vendor="NXP"> <info chip="LPC1549" connectscript="LPC15RunBootRomConnect.scp" flash_driver="LPC15xx_256K.cfx" match_id="0x0" name="LPC1549" resetscript="LPC15RunBootRomReset.scp" stub="crt_emu_cm3_gen"> <chip> @@ -155,52 +155,15 @@ <memoryInstance derived_from="RAM" id="Ram0_16" location="0x2000000" size="0x4000"/> <memoryInstance derived_from="RAM" id="Ram1_16" location="0x2004000" size="0x4000"/> <memoryInstance derived_from="RAM" id="Ram2_4" location="0x2008000" size="0x1000"/> -<peripheralInstance derived_from="V7M_MPU" determined="infoFile" id="MPU" location="0xe000ed90"/> -<peripheralInstance derived_from="V7M_NVIC" determined="infoFile" id="NVIC" location="0xe000e000"/> -<peripheralInstance derived_from="V7M_DCR" determined="infoFile" id="DCR" location="0xe000edf0"/> -<peripheralInstance derived_from="V7M_ITM" determined="infoFile" id="ITM" location="0xe0000000"/> -<peripheralInstance derived_from="GPIO-PORT" determined="infoFile" id="GPIO-PORT" location="0x1c000000"/> -<peripheralInstance derived_from="DMA" determined="infoFile" id="DMA" location="0x1c004000"/> -<peripheralInstance derived_from="USB" determined="infoFile" id="USB" location="0x1c00c000"/> -<peripheralInstance derived_from="CRC" determined="infoFile" id="CRC" location="0x1c010000"/> -<peripheralInstance derived_from="SCT0" determined="infoFile" id="SCT0" location="0x1c018000"/> -<peripheralInstance derived_from="SCT1" determined="infoFile" id="SCT1" location="0x1c01c000"/> -<peripheralInstance derived_from="SCT2" determined="infoFile" id="SCT2" location="0x1c020000"/> -<peripheralInstance derived_from="SCT3" determined="infoFile" id="SCT3" location="0x1c024000"/> -<peripheralInstance derived_from="ADC0" determined="infoFile" id="ADC0" location="0x40000000"/> -<peripheralInstance derived_from="DAC" determined="infoFile" id="DAC" location="0x40004000"/> -<peripheralInstance derived_from="ACMP" determined="infoFile" id="ACMP" location="0x40008000"/> -<peripheralInstance derived_from="INMUX" determined="infoFile" id="INMUX" location="0x40014000"/> -<peripheralInstance derived_from="RTC" determined="infoFile" id="RTC" location="0x40028000"/> -<peripheralInstance derived_from="WWDT" determined="infoFile" id="WWDT" location="0x4002c000"/> -<peripheralInstance derived_from="SWM" determined="infoFile" id="SWM" location="0x40038000"/> -<peripheralInstance derived_from="PMU" determined="infoFile" id="PMU" location="0x4003c000"/> -<peripheralInstance derived_from="USART0" determined="infoFile" id="USART0" location="0x40040000"/> -<peripheralInstance derived_from="USART1" determined="infoFile" id="USART1" location="0x40044000"/> -<peripheralInstance derived_from="SPI0" determined="infoFile" id="SPI0" location="0x40048000"/> -<peripheralInstance derived_from="SPI1" determined="infoFile" id="SPI1" location="0x4004c000"/> -<peripheralInstance derived_from="I2C0" determined="infoFile" id="I2C0" location="0x40050000"/> -<peripheralInstance derived_from="QEI" determined="infoFile" id="QEI" location="0x40058000"/> -<peripheralInstance derived_from="SYSCON" determined="infoFile" id="SYSCON" location="0x40074000"/> -<peripheralInstance derived_from="ADC1" determined="infoFile" id="ADC1" location="0x40080000"/> -<peripheralInstance derived_from="MRT" determined="infoFile" id="MRT" location="0x400a0000"/> -<peripheralInstance derived_from="PINT" determined="infoFile" id="PINT" location="0x400a4000"/> -<peripheralInstance derived_from="GINT0" determined="infoFile" id="GINT0" location="0x400a8000"/> -<peripheralInstance derived_from="GINT1" determined="infoFile" id="GINT1" location="0x400ac000"/> -<peripheralInstance derived_from="RIT" determined="infoFile" id="RIT" location="0x400b4000"/> -<peripheralInstance derived_from="SCTIPU" determined="infoFile" id="SCTIPU" location="0x400b8000"/> -<peripheralInstance derived_from="FLASHCTRL" determined="infoFile" id="FLASHCTRL" location="0x400bc000"/> -<peripheralInstance derived_from="USART2" determined="infoFile" id="USART2" location="0x400c0000"/> -<peripheralInstance derived_from="C-CAN0" determined="infoFile" id="C-CAN0" location="0x400f0000"/> -<peripheralInstance derived_from="IOCON" determined="infoFile" id="IOCON" location="0x400f8000"/> </chip> <processor> <name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> -<link href="LPC15xx_peripheral.xme" show="embed" type="simple"/> </info> </infoList> </TargetConfig> + + diff --git a/lpc_board_nxp_lpcxpresso_1549/.settings/language.settings.xml b/lpc_board_nxp_lpcxpresso_1549/.settings/language.settings.xml index 48af2ca..8675f41 100644 --- a/lpc_board_nxp_lpcxpresso_1549/.settings/language.settings.xml +++ b/lpc_board_nxp_lpcxpresso_1549/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + @@ -15,11 +15,11 @@ - + - \ No newline at end of file + diff --git a/lpc_chip_15xx/.cproject b/lpc_chip_15xx/.cproject index 2281cdd..49988ad 100644 --- a/lpc_chip_15xx/.cproject +++ b/lpc_chip_15xx/.cproject @@ -28,20 +28,20 @@ - - - @@ -137,7 +137,7 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="70200"/> +<Properties property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="100300"/> <infoList vendor="NXP"> <info chip="LPC1549" connectscript="LPC15RunBootRomConnect.scp" flash_driver="LPC15xx_256K.cfx" match_id="0x0" name="LPC1549" resetscript="LPC15RunBootRomReset.scp" stub="crt_emu_cm3_gen"> <chip> @@ -153,53 +153,16 @@ <memoryInstance derived_from="RAM" id="Ram0_16" location="0x2000000" size="0x4000"/> <memoryInstance derived_from="RAM" id="Ram1_16" location="0x2004000" size="0x4000"/> <memoryInstance derived_from="RAM" id="Ram2_4" location="0x2008000" size="0x1000"/> -<peripheralInstance derived_from="V7M_MPU" determined="infoFile" id="MPU" location="0xe000ed90"/> -<peripheralInstance derived_from="V7M_NVIC" determined="infoFile" id="NVIC" location="0xe000e000"/> -<peripheralInstance derived_from="V7M_DCR" determined="infoFile" id="DCR" location="0xe000edf0"/> -<peripheralInstance derived_from="V7M_ITM" determined="infoFile" id="ITM" location="0xe0000000"/> -<peripheralInstance derived_from="GPIO-PORT" determined="infoFile" id="GPIO-PORT" location="0x1c000000"/> -<peripheralInstance derived_from="DMA" determined="infoFile" id="DMA" location="0x1c004000"/> -<peripheralInstance derived_from="USB" determined="infoFile" id="USB" location="0x1c00c000"/> -<peripheralInstance derived_from="CRC" determined="infoFile" id="CRC" location="0x1c010000"/> -<peripheralInstance derived_from="SCT0" determined="infoFile" id="SCT0" location="0x1c018000"/> -<peripheralInstance derived_from="SCT1" determined="infoFile" id="SCT1" location="0x1c01c000"/> -<peripheralInstance derived_from="SCT2" determined="infoFile" id="SCT2" location="0x1c020000"/> -<peripheralInstance derived_from="SCT3" determined="infoFile" id="SCT3" location="0x1c024000"/> -<peripheralInstance derived_from="ADC0" determined="infoFile" id="ADC0" location="0x40000000"/> -<peripheralInstance derived_from="DAC" determined="infoFile" id="DAC" location="0x40004000"/> -<peripheralInstance derived_from="ACMP" determined="infoFile" id="ACMP" location="0x40008000"/> -<peripheralInstance derived_from="INMUX" determined="infoFile" id="INMUX" location="0x40014000"/> -<peripheralInstance derived_from="RTC" determined="infoFile" id="RTC" location="0x40028000"/> -<peripheralInstance derived_from="WWDT" determined="infoFile" id="WWDT" location="0x4002c000"/> -<peripheralInstance derived_from="SWM" determined="infoFile" id="SWM" location="0x40038000"/> -<peripheralInstance derived_from="PMU" determined="infoFile" id="PMU" location="0x4003c000"/> -<peripheralInstance derived_from="USART0" determined="infoFile" id="USART0" location="0x40040000"/> -<peripheralInstance derived_from="USART1" determined="infoFile" id="USART1" location="0x40044000"/> -<peripheralInstance derived_from="SPI0" determined="infoFile" id="SPI0" location="0x40048000"/> -<peripheralInstance derived_from="SPI1" determined="infoFile" id="SPI1" location="0x4004c000"/> -<peripheralInstance derived_from="I2C0" determined="infoFile" id="I2C0" location="0x40050000"/> -<peripheralInstance derived_from="QEI" determined="infoFile" id="QEI" location="0x40058000"/> -<peripheralInstance derived_from="SYSCON" determined="infoFile" id="SYSCON" location="0x40074000"/> -<peripheralInstance derived_from="ADC1" determined="infoFile" id="ADC1" location="0x40080000"/> -<peripheralInstance derived_from="MRT" determined="infoFile" id="MRT" location="0x400a0000"/> -<peripheralInstance derived_from="PINT" determined="infoFile" id="PINT" location="0x400a4000"/> -<peripheralInstance derived_from="GINT0" determined="infoFile" id="GINT0" location="0x400a8000"/> -<peripheralInstance derived_from="GINT1" determined="infoFile" id="GINT1" location="0x400ac000"/> -<peripheralInstance derived_from="RIT" determined="infoFile" id="RIT" location="0x400b4000"/> -<peripheralInstance derived_from="SCTIPU" determined="infoFile" id="SCTIPU" location="0x400b8000"/> -<peripheralInstance derived_from="FLASHCTRL" determined="infoFile" id="FLASHCTRL" location="0x400bc000"/> -<peripheralInstance derived_from="USART2" determined="infoFile" id="USART2" location="0x400c0000"/> -<peripheralInstance derived_from="C-CAN0" determined="infoFile" id="C-CAN0" location="0x400f0000"/> -<peripheralInstance derived_from="IOCON" determined="infoFile" id="IOCON" location="0x400f8000"/> </chip> <processor> <name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> -<link href="LPC15xx_peripheral.xme" show="embed" type="simple"/> </info> </infoList> </TargetConfig> - \ No newline at end of file + + + diff --git a/lpc_chip_15xx/.settings/language.settings.xml b/lpc_chip_15xx/.settings/language.settings.xml index 168be13..6638d88 100644 --- a/lpc_chip_15xx/.settings/language.settings.xml +++ b/lpc_chip_15xx/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + @@ -15,11 +15,11 @@ - + - \ No newline at end of file + diff --git a/periph_blinky/.cproject b/periph_blinky/.cproject index fcf17ae..6008176 100644 --- a/periph_blinky/.cproject +++ b/periph_blinky/.cproject @@ -192,7 +192,7 @@ <?xml version="1.0" encoding="UTF-8"?> <TargetConfig> -<Properties property_0="" property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="70200"/> +<Properties property_2="LPC15xx_256K.cfx" property_3="NXP" property_4="LPC1549" property_count="5" version="100300"/> <infoList vendor="NXP"> <info chip="LPC1549" connectscript="LPC15RunBootRomConnect.scp" flash_driver="LPC15xx_256K.cfx" match_id="0x0" name="LPC1549" resetscript="LPC15RunBootRomReset.scp" stub="crt_emu_cm3_gen"> <chip> @@ -208,53 +208,16 @@ <memoryInstance derived_from="RAM" id="Ram0_16" location="0x2000000" size="0x4000"/> <memoryInstance derived_from="RAM" id="Ram1_16" location="0x2004000" size="0x4000"/> <memoryInstance derived_from="RAM" id="Ram2_4" location="0x2008000" size="0x1000"/> -<peripheralInstance derived_from="V7M_MPU" determined="infoFile" id="MPU" location="0xe000ed90"/> -<peripheralInstance derived_from="V7M_NVIC" determined="infoFile" id="NVIC" location="0xe000e000"/> -<peripheralInstance derived_from="V7M_DCR" determined="infoFile" id="DCR" location="0xe000edf0"/> -<peripheralInstance derived_from="V7M_ITM" determined="infoFile" id="ITM" location="0xe0000000"/> -<peripheralInstance derived_from="GPIO-PORT" determined="infoFile" id="GPIO-PORT" location="0x1c000000"/> -<peripheralInstance derived_from="DMA" determined="infoFile" id="DMA" location="0x1c004000"/> -<peripheralInstance derived_from="USB" determined="infoFile" id="USB" location="0x1c00c000"/> -<peripheralInstance derived_from="CRC" determined="infoFile" id="CRC" location="0x1c010000"/> -<peripheralInstance derived_from="SCT0" determined="infoFile" id="SCT0" location="0x1c018000"/> -<peripheralInstance derived_from="SCT1" determined="infoFile" id="SCT1" location="0x1c01c000"/> -<peripheralInstance derived_from="SCT2" determined="infoFile" id="SCT2" location="0x1c020000"/> -<peripheralInstance derived_from="SCT3" determined="infoFile" id="SCT3" location="0x1c024000"/> -<peripheralInstance derived_from="ADC0" determined="infoFile" id="ADC0" location="0x40000000"/> -<peripheralInstance derived_from="DAC" determined="infoFile" id="DAC" location="0x40004000"/> -<peripheralInstance derived_from="ACMP" determined="infoFile" id="ACMP" location="0x40008000"/> -<peripheralInstance derived_from="INMUX" determined="infoFile" id="INMUX" location="0x40014000"/> -<peripheralInstance derived_from="RTC" determined="infoFile" id="RTC" location="0x40028000"/> -<peripheralInstance derived_from="WWDT" determined="infoFile" id="WWDT" location="0x4002c000"/> -<peripheralInstance derived_from="SWM" determined="infoFile" id="SWM" location="0x40038000"/> -<peripheralInstance derived_from="PMU" determined="infoFile" id="PMU" location="0x4003c000"/> -<peripheralInstance derived_from="USART0" determined="infoFile" id="USART0" location="0x40040000"/> -<peripheralInstance derived_from="USART1" determined="infoFile" id="USART1" location="0x40044000"/> -<peripheralInstance derived_from="SPI0" determined="infoFile" id="SPI0" location="0x40048000"/> -<peripheralInstance derived_from="SPI1" determined="infoFile" id="SPI1" location="0x4004c000"/> -<peripheralInstance derived_from="I2C0" determined="infoFile" id="I2C0" location="0x40050000"/> -<peripheralInstance derived_from="QEI" determined="infoFile" id="QEI" location="0x40058000"/> -<peripheralInstance derived_from="SYSCON" determined="infoFile" id="SYSCON" location="0x40074000"/> -<peripheralInstance derived_from="ADC1" determined="infoFile" id="ADC1" location="0x40080000"/> -<peripheralInstance derived_from="MRT" determined="infoFile" id="MRT" location="0x400a0000"/> -<peripheralInstance derived_from="PINT" determined="infoFile" id="PINT" location="0x400a4000"/> -<peripheralInstance derived_from="GINT0" determined="infoFile" id="GINT0" location="0x400a8000"/> -<peripheralInstance derived_from="GINT1" determined="infoFile" id="GINT1" location="0x400ac000"/> -<peripheralInstance derived_from="RIT" determined="infoFile" id="RIT" location="0x400b4000"/> -<peripheralInstance derived_from="SCTIPU" determined="infoFile" id="SCTIPU" location="0x400b8000"/> -<peripheralInstance derived_from="FLASHCTRL" determined="infoFile" id="FLASHCTRL" location="0x400bc000"/> -<peripheralInstance derived_from="USART2" determined="infoFile" id="USART2" location="0x400c0000"/> -<peripheralInstance derived_from="C-CAN0" determined="infoFile" id="C-CAN0" location="0x400f0000"/> -<peripheralInstance derived_from="IOCON" determined="infoFile" id="IOCON" location="0x400f8000"/> </chip> <processor> <name gcc_name="cortex-m3">Cortex-M3</name> <family>Cortex-M</family> </processor> -<link href="LPC15xx_peripheral.xme" show="embed" type="simple"/> </info> </infoList> </TargetConfig> - \ No newline at end of file + + + diff --git a/periph_blinky/.settings/language.settings.xml b/periph_blinky/.settings/language.settings.xml index 5d2dd35..b1ccb28 100644 --- a/periph_blinky/.settings/language.settings.xml +++ b/periph_blinky/.settings/language.settings.xml @@ -4,7 +4,7 @@ - + @@ -15,11 +15,11 @@ - + - \ No newline at end of file +