├── .gitignore ├── inc ├── wifi_credentials.h ├── led.h ├── temperature.h ├── networkwrapper.h ├── stm32f10x_conf.h └── FreeRTOSConfig.h ├── .settings ├── ilg.gnumcueclipse.debug.gdbjtag.qemu.prefs └── language.settings.xml ├── .gitmodules ├── README.md ├── lib ├── FreeRTOS_Source │ ├── readme.txt │ ├── include │ │ ├── stdint.readme │ │ ├── projdefs.h │ │ ├── stack_macros.h │ │ ├── StackMacros.h │ │ ├── portable.h │ │ ├── deprecated_definitions.h │ │ ├── mpu_wrappers.h │ │ └── mpu_prototypes.h │ ├── portable │ │ ├── MemMang │ │ │ └── heap_1.c │ │ └── GCC │ │ │ └── ARM_CM3 │ │ │ └── portmacro.h │ └── list.c └── MQTTPacket │ ├── MQTTUnsubscribe.h │ ├── MQTTSubscribe.h │ ├── MQTTPublish.h │ ├── MQTTFormat.h │ ├── CMakeLists.txt │ ├── StackTrace.h │ ├── transport.h │ ├── MQTTPacket.h │ ├── transport.c │ ├── MQTTUnsubscribeServer.c │ ├── MQTTConnect.h │ ├── MQTTUnsubscribeClient.c │ ├── MQTTDeserializePublish.c │ ├── MQTTSubscribeServer.c │ ├── MQTTSubscribeClient.c │ ├── MQTTFormat.c │ ├── MQTTConnectServer.c │ ├── MQTTSerializePublish.c │ └── MQTTConnectClient.c ├── .project ├── LICENSE ├── src ├── led.c ├── temperature.c ├── main.c └── networkwrapper.c └── debug.launch /.gitignore: -------------------------------------------------------------------------------- 1 | Debug 2 | Release 3 | -------------------------------------------------------------------------------- /inc/wifi_credentials.h: -------------------------------------------------------------------------------- 1 | #define WIFI_AP_SSID "ssid" 2 | #define WIFI_AP_PASS "password" 3 | -------------------------------------------------------------------------------- /.settings/ilg.gnumcueclipse.debug.gdbjtag.qemu.prefs: -------------------------------------------------------------------------------- 1 | eclipse.preferences.version=1 2 | install.folder=C\:\\Program Files\\GNU ARM Eclipse\\QEMU\\2.8.0-201612271623-dev\\bin 3 | -------------------------------------------------------------------------------- /.gitmodules: -------------------------------------------------------------------------------- 1 | [submodule "lib/esp8266driver"] 2 | path = lib/esp8266driver 3 | url = https://github.com/atakansarioglu/esp8266-iot-driver.git 4 | [submodule "lib/c-circus"] 5 | path = lib/c-circus 6 | url = https://github.com/atakansarioglu/c-circus.git 7 | [submodule "lib/mculibs"] 8 | path = lib/mculibs 9 | url = https://github.com/atakansarioglu/mculibs_stm32f10x.git 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MQTT Temperature Logger via ESP8266 2 | Refer to: http://www.atakansarioglu.com/how-to-iot-mqtt-on-stm32-cortex-m-esp8266-wifi-temperature-freertos/ 3 | 4 | ## Clone 5 | git clone https://github.com/atakansarioglu/mqtt_temperature_logger_esp8266.git 6 | 7 | cd mqtt_temperature_logger_esp8266 8 | 9 | git submodule update --init --recursive 10 | 11 | ## Compile 12 | Refer to: http://www.atakansarioglu.com/embedded-mcu-software-development-eclipse-setup-debug/ 13 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/readme.txt: -------------------------------------------------------------------------------- 1 | Each real time kernel port consists of three files that contain the core kernel 2 | components and are common to every port, and one or more files that are 3 | specific to a particular microcontroller and or compiler. 4 | 5 | + The FreeRTOS/Source directory contains the three files that are common to 6 | every port - list.c, queue.c and tasks.c. The kernel is contained within these 7 | three files. croutine.c implements the optional co-routine functionality - which 8 | is normally only used on very memory limited systems. 9 | 10 | + The FreeRTOS/Source/Portable directory contains the files that are specific to 11 | a particular microcontroller and or compiler. 12 | 13 | + The FreeRTOS/Source/include directory contains the real time kernel header 14 | files. 15 | 16 | See the readme file in the FreeRTOS/Source/Portable directory for more 17 | information. -------------------------------------------------------------------------------- /.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | mqtt_temperature_logger_esp8266 4 | 5 | 6 | 7 | 8 | 9 | org.eclipse.cdt.managedbuilder.core.genmakebuilder 10 | clean,full,incremental, 11 | 12 | 13 | 14 | 15 | org.eclipse.cdt.managedbuilder.core.ScannerConfigBuilder 16 | full,incremental, 17 | 18 | 19 | 20 | 21 | 22 | org.eclipse.cdt.core.cnature 23 | org.eclipse.cdt.core.ccnature 24 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 25 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 26 | 27 | 28 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/stdint.readme: -------------------------------------------------------------------------------- 1 | 2 | #ifndef FREERTOS_STDINT 3 | #define FREERTOS_STDINT 4 | 5 | /******************************************************************************* 6 | * THIS IS NOT A FULL stdint.h IMPLEMENTATION - It only contains the definitions 7 | * necessary to build the FreeRTOS code. It is provided to allow FreeRTOS to be 8 | * built using compilers that do not provide their own stdint.h definition. 9 | * 10 | * To use this file: 11 | * 12 | * 1) Copy this file into the directory that contains your FreeRTOSConfig.h 13 | * header file, as that directory will already be in the compilers include 14 | * path. 15 | * 16 | * 2) Rename the copied file stdint.h. 17 | * 18 | */ 19 | 20 | typedef signed char int8_t; 21 | typedef unsigned char uint8_t; 22 | typedef short int16_t; 23 | typedef unsigned short uint16_t; 24 | typedef long int32_t; 25 | typedef unsigned long uint32_t; 26 | 27 | #endif /* FREERTOS_STDINT */ 28 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Atakan SARIOGLU 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /inc/led.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file led.h 3 | * @author Atakan S. 4 | * @date 01/06/2018 5 | * @version 1.0 6 | * @brief Simply controls the LED on stm32f103 Nucleo board. 7 | * 8 | * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | * DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | #ifndef _LED_H_ 30 | #define _LED_H_ 31 | 32 | // Prototypes. 33 | void vLedWrite(const unsigned char ledID, const unsigned char ledON); 34 | 35 | #endif 36 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTUnsubscribe.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTUNSUBSCRIBE_H_ 19 | #define MQTTUNSUBSCRIBE_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 29 | int count, MQTTString topicFilters[]); 30 | 31 | DLLExport int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int max_count, int* count, MQTTString topicFilters[], 32 | unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid); 35 | 36 | DLLExport int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int len); 37 | 38 | #endif /* MQTTUNSUBSCRIBE_H_ */ 39 | -------------------------------------------------------------------------------- /inc/temperature.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file temperature.h 3 | * @author Atakan S. 4 | * @date 01/06/2018 5 | * @version 1.0 6 | * @brief Simply reads temperature on stm32f10x. 7 | * 8 | * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | * DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | #ifndef _TEMPERATURE_H_ 30 | #define _TEMPERATURE_H_ 31 | 32 | // Include. 33 | #include "temperature.h" 34 | #include 35 | 36 | // Prototypes. 37 | void temperature_init(void); 38 | float temperature_read(void); 39 | 40 | #endif 41 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTSubscribe.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTSUBSCRIBE_H_ 19 | #define MQTTSUBSCRIBE_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 29 | int count, MQTTString topicFilters[], int requestedQoSs[]); 30 | 31 | DLLExport int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, 32 | int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs); 35 | 36 | DLLExport int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int len); 37 | 38 | 39 | #endif /* MQTTSUBSCRIBE_H_ */ 40 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTPublish.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTPUBLISH_H_ 19 | #define MQTTPUBLISH_H_ 20 | 21 | #if !defined(DLLImport) 22 | #define DLLImport 23 | #endif 24 | #if !defined(DLLExport) 25 | #define DLLExport 26 | #endif 27 | 28 | DLLExport int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 29 | MQTTString topicName, unsigned char* payload, int payloadlen); 30 | 31 | DLLExport int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 32 | unsigned char** payload, int* payloadlen, unsigned char* buf, int len); 33 | 34 | DLLExport int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid); 35 | DLLExport int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid); 36 | DLLExport int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid); 37 | 38 | #endif /* MQTTPUBLISH_H_ */ 39 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTFormat.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #if !defined(MQTTFORMAT_H) 18 | #define MQTTFORMAT_H 19 | 20 | #include "StackTrace.h" 21 | #include "MQTTPacket.h" 22 | 23 | const char* MQTTPacket_getName(unsigned short packetid); 24 | int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data); 25 | int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent); 26 | int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained, 27 | unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen); 28 | int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid); 29 | int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count, 30 | MQTTString topicFilters[], int requestedQoSs[]); 31 | int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs); 32 | int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, 33 | int count, MQTTString topicFilters[]); 34 | char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); 35 | char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen); 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /.settings/language.settings.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | -------------------------------------------------------------------------------- /src/led.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file led.c 3 | * @author Atakan S. 4 | * @date 01/06/2018 5 | * @version 1.0 6 | * @brief Simply controls the LED on stm32f103 Nucleo board. 7 | * 8 | * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | * DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | // Include. 30 | #include "led.h" 31 | #include 32 | #include 33 | 34 | // Settings. 35 | #define LED_COUNT 1 36 | 37 | /* 38 | * @brief Controls various LEDs. 39 | * @param ledID The id number of the LED to modify. 40 | * @param ledON If true, the LED is on, otherwise off. 41 | */ 42 | void vLedWrite(const unsigned char ledID, const unsigned char ledON) { 43 | GPIO_InitTypeDef GPIO_InitStructure; 44 | 45 | // Check the id. 46 | assert(ledID < LED_COUNT); 47 | 48 | // Pick the LED. 49 | switch(ledID){ 50 | case 0: 51 | // Enable peripheral clocks. 52 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); 53 | 54 | // Configure PC9 output pushpull mode. 55 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; 56 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 57 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; 58 | GPIO_Init(GPIOA, &GPIO_InitStructure); 59 | 60 | // Set state. 61 | GPIO_WriteBit(GPIOA, GPIO_Pin_5, ledON); 62 | break; 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /lib/MQTTPacket/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #******************************************************************************* 2 | # Copyright (c) 2017 IBM Corp. 3 | # 4 | # All rights reserved. This program and the accompanying materials 5 | # are made available under the terms of the Eclipse Public License v1.0 6 | # and Eclipse Distribution License v1.0 which accompany this distribution. 7 | # 8 | # The Eclipse Public License is available at 9 | # http://www.eclipse.org/legal/epl-v10.html 10 | # and the Eclipse Distribution License is available at 11 | # http://www.eclipse.org/org/documents/edl-v10.php. 12 | # 13 | # Contributors: 14 | # Ian Craggs - initial version 15 | #*******************************************************************************/ 16 | 17 | # MQTTPacket Library 18 | file(GLOB SOURCES "*.c") 19 | add_library(paho-embed-mqtt3c SHARED ${SOURCES}) 20 | install(TARGETS paho-embed-mqtt3c DESTINATION /usr/lib) 21 | target_compile_definitions(paho-embed-mqtt3c PRIVATE MQTT_SERVER MQTT_CLIENT) 22 | 23 | add_library(MQTTPacketClient SHARED MQTTFormat MQTTPacket 24 | MQTTSerializePublish MQTTDeserializePublish 25 | MQTTConnectClient MQTTSubscribeClient MQTTUnsubscribeClient) 26 | target_compile_definitions(MQTTPacketClient PRIVATE MQTT_CLIENT) 27 | 28 | add_library(MQTTPacketServer SHARED MQTTFormat MQTTPacket 29 | MQTTSerializePublish MQTTDeserializePublish 30 | MQTTConnectServer MQTTSubscribeServer MQTTUnsubscribeServer) 31 | target_compile_definitions(MQTTPacketServer PRIVATE MQTT_SERVER) 32 | 33 | file(GLOB SOURCES5 "*.c" "V5/*.c" ../samples/transport.c) 34 | 35 | add_library(paho-embed-mqtt5c SHARED ${SOURCES5}) 36 | install(TARGETS paho-embed-mqtt5c DESTINATION /usr/lib) 37 | target_compile_definitions(paho-embed-mqtt5c PRIVATE MQTT_SERVER MQTT_CLIENT MQTTV5) 38 | 39 | add_library(MQTTPacketClient5 SHARED MQTTFormat MQTTPacket 40 | MQTTSerializePublish MQTTDeserializePublish V5/MQTTProperties V5/MQTTV5Packet 41 | MQTTConnectClient MQTTSubscribeClient MQTTUnsubscribeClient) 42 | target_compile_definitions(MQTTPacketClient5 PRIVATE MQTT_CLIENT MQTTV5) 43 | 44 | add_library(MQTTPacketServer5 SHARED MQTTFormat MQTTPacket 45 | MQTTSerializePublish MQTTDeserializePublish V5/MQTTProperties V5/MQTTV5Packet 46 | MQTTConnectServer MQTTSubscribeServer MQTTUnsubscribeServer) 47 | target_compile_definitions(MQTTPacketServer5 PRIVATE MQTT_SERVER MQTTV5) 48 | -------------------------------------------------------------------------------- /inc/networkwrapper.h: -------------------------------------------------------------------------------- 1 | /** 2 | * @file networkwrapper.h 3 | * @author Atakan S. 4 | * @date 01/01/2019 5 | * @version 1.0 6 | * @brief Network wrapper for PAHO MQTT project based on ESP8266. 7 | * 8 | * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | * DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | #ifndef _NETWORKWRAPPER_H 30 | #define _NETWORKWRAPPER_H 31 | 32 | // Socket data type. 33 | #ifndef network_socket_t 34 | #define network_socket_t void* 35 | #endif 36 | 37 | /* 38 | * @brief Initialize network subsystem. 39 | */ 40 | void network_init(void); 41 | 42 | /* 43 | * @brief Close network connection. 44 | */ 45 | void network_close(void); 46 | 47 | /* 48 | * @brief Starts the connection or saves the parameters and defers the connection to the send/recv methods. 49 | * @param host Host name. 50 | * @param port Remote port number. 51 | * @param keepalive Seconds for keepalive function. 52 | * @param ssl If true, SSL connection type will be used, otherwise TCP. 53 | * @return Returns 0 on success and negative on error. 54 | */ 55 | int network_connect(const char * host, const unsigned short int port, const unsigned short int keepalive, const char ssl); 56 | 57 | /* 58 | * @brief NON-BLOCKING Sends data and mimics transparency. 59 | * @param address Pointer to the data to be sent. 60 | * @param bytes Number of bytes to be sent. 61 | * @return Returns the number of actual data bytes sent or negative on error. 62 | */ 63 | int network_send(unsigned char *address, unsigned int bytes); 64 | 65 | /* 66 | * @brief NON-BLOCKING Receives data and mimics transparency. 67 | * @param address Pointer to the memory into that the received data is stored. 68 | * @param maxbytes Number of maximum bytes to be received. 69 | * @return Returns the number of actual data bytes received or negative on error. 70 | */ 71 | int network_recv(unsigned char *address, unsigned int maxbytes); 72 | 73 | #endif 74 | -------------------------------------------------------------------------------- /src/temperature.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file temperature.c 3 | * @author Atakan S. 4 | * @date 01/06/2018 5 | * @version 1.0 6 | * @brief Simply reads temperature on stm32f10x. 7 | * 8 | * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | * DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | // Include. 30 | #include "temperature.h" 31 | #include 32 | 33 | // Settings. 34 | #define TEMPERATURE_VCC_MV 3300UL 35 | #define TEMPERATURE_V25_MV 1410UL 36 | #define TEMPERATURE_AVGSLOPE_MV 4.3f 37 | #define TEMPERATURE_FULLSCALE 4096UL 38 | 39 | /* 40 | * @brief Initializes the adc for temperature reading. 41 | */ 42 | void temperature_init(void) { 43 | ADC_InitTypeDef ADC_InitStructure; 44 | 45 | // Setup ADC clock. 46 | RCC_ADCCLKConfig(RCC_PCLK2_Div6); 47 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); 48 | 49 | // Configure. 50 | ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; 51 | ADC_InitStructure.ADC_ScanConvMode = DISABLE; 52 | ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; 53 | ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; 54 | ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; 55 | ADC_InitStructure.ADC_NbrOfChannel = 1; 56 | ADC_Init(ADC1, &ADC_InitStructure); 57 | 58 | // Configure the temperature sensor channel. 59 | ADC_TempSensorVrefintCmd(ENABLE); 60 | ADC_RegularChannelConfig(ADC1, ADC_Channel_TempSensor, 1, ADC_SampleTime_239Cycles5); 61 | 62 | // Enable. 63 | ADC_Cmd(ADC1, ENABLE); 64 | 65 | //ADC calibration procedure. 66 | ADC_ResetCalibration(ADC1); 67 | while(ADC_GetResetCalibrationStatus(ADC1)); 68 | ADC_StartCalibration(ADC1); 69 | while(ADC_GetCalibrationStatus(ADC1)); 70 | } 71 | 72 | /* 73 | * @brief Initializes the adc for temperature reading. 74 | * @return Temperature value in celcius. 75 | */ 76 | float temperature_read(void) { 77 | // Start the conversion. 78 | ADC_SoftwareStartConvCmd(ADC1, ENABLE); 79 | while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET); 80 | uint16_t adc_raw = ADC_GetConversionValue(ADC1); 81 | 82 | // Convert to celcius. 83 | float adc_celcius = ((((float)TEMPERATURE_V25_MV * TEMPERATURE_FULLSCALE / TEMPERATURE_VCC_MV) - adc_raw) / TEMPERATURE_AVGSLOPE_MV) + 25; 84 | 85 | // Return. 86 | return adc_celcius; 87 | } 88 | -------------------------------------------------------------------------------- /inc/stm32f10x_conf.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file USART/Interrupt/stm32f10x_conf.h 4 | * @author MCD Application Team 5 | * @version V3.5.0 6 | * @date 08-April-2011 7 | * @brief Library configuration file. 8 | ****************************************************************************** 9 | * @attention 10 | * 11 | * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS 12 | * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE 13 | * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY 14 | * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING 15 | * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE 16 | * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. 17 | * 18 | *

© COPYRIGHT 2011 STMicroelectronics

19 | ****************************************************************************** 20 | */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __STM32F10x_CONF_H 24 | #define __STM32F10x_CONF_H 25 | 26 | /* Includes ------------------------------------------------------------------*/ 27 | /* Uncomment/Comment the line below to enable/disable peripheral header file inclusion */ 28 | #include "stm32f10x_adc.h" 29 | #include "stm32f10x_bkp.h" 30 | #include "stm32f10x_can.h" 31 | #include "stm32f10x_cec.h" 32 | #include "stm32f10x_crc.h" 33 | #include "stm32f10x_dac.h" 34 | #include "stm32f10x_dbgmcu.h" 35 | #include "stm32f10x_dma.h" 36 | #include "stm32f10x_exti.h" 37 | #include "stm32f10x_flash.h" 38 | #include "stm32f10x_fsmc.h" 39 | #include "stm32f10x_gpio.h" 40 | #include "stm32f10x_i2c.h" 41 | #include "stm32f10x_iwdg.h" 42 | #include "stm32f10x_pwr.h" 43 | #include "stm32f10x_rcc.h" 44 | #include "stm32f10x_rtc.h" 45 | #include "stm32f10x_sdio.h" 46 | #include "stm32f10x_spi.h" 47 | #include "stm32f10x_tim.h" 48 | #include "stm32f10x_usart.h" 49 | #include "stm32f10x_wwdg.h" 50 | #include "misc.h" /* High level functions for NVIC and SysTick (add-on to CMSIS functions) */ 51 | 52 | /* Exported types ------------------------------------------------------------*/ 53 | /* Exported constants --------------------------------------------------------*/ 54 | /* Uncomment the line below to expanse the "assert_param" macro in the 55 | Standard Peripheral Library drivers code */ 56 | /* #define USE_FULL_ASSERT 1 */ 57 | 58 | /* Exported macro ------------------------------------------------------------*/ 59 | #ifdef USE_FULL_ASSERT 60 | /** 61 | * @brief The assert_param macro is used for function's parameters check. 62 | * @param expr: If expr is false, it calls assert_failed function which reports 63 | * the name of the source file and the source line number of the call 64 | * that failed. If expr is true, it returns no value. 65 | * @retval None 66 | */ 67 | #define assert_param(expr) ((expr) ? (void)0 : assert_failed((uint8_t *)__FILE__, __LINE__)) 68 | /* Exported functions ------------------------------------------------------- */ 69 | void assert_failed(uint8_t* file, uint32_t line); 70 | #else 71 | #define assert_param(expr) ((void)0) 72 | #endif /* USE_FULL_ASSERT */ 73 | 74 | #endif /* __STM32F10x_CONF_H */ 75 | 76 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 77 | -------------------------------------------------------------------------------- /lib/MQTTPacket/StackTrace.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - fix for bug #434081 16 | *******************************************************************************/ 17 | 18 | #ifndef STACKTRACE_H_ 19 | #define STACKTRACE_H_ 20 | 21 | #include 22 | #define NOSTACKTRACE 1 23 | 24 | #if defined(NOSTACKTRACE) 25 | #define FUNC_ENTRY 26 | #define FUNC_ENTRY_NOLOG 27 | #define FUNC_ENTRY_MED 28 | #define FUNC_ENTRY_MAX 29 | #define FUNC_EXIT 30 | #define FUNC_EXIT_NOLOG 31 | #define FUNC_EXIT_MED 32 | #define FUNC_EXIT_MAX 33 | #define FUNC_EXIT_RC(x) 34 | #define FUNC_EXIT_MED_RC(x) 35 | #define FUNC_EXIT_MAX_RC(x) 36 | 37 | #else 38 | 39 | #if defined(WIN32) 40 | #define inline __inline 41 | #define FUNC_ENTRY StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MINIMUM) 42 | #define FUNC_ENTRY_NOLOG StackTrace_entry(__FUNCTION__, __LINE__, -1) 43 | #define FUNC_ENTRY_MED StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MEDIUM) 44 | #define FUNC_ENTRY_MAX StackTrace_entry(__FUNCTION__, __LINE__, TRACE_MAXIMUM) 45 | #define FUNC_EXIT StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MINIMUM) 46 | #define FUNC_EXIT_NOLOG StackTrace_exit(__FUNCTION__, __LINE__, -1) 47 | #define FUNC_EXIT_MED StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MEDIUM) 48 | #define FUNC_EXIT_MAX StackTrace_exit(__FUNCTION__, __LINE__, NULL, TRACE_MAXIMUM) 49 | #define FUNC_EXIT_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MINIMUM) 50 | #define FUNC_EXIT_MED_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MEDIUM) 51 | #define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__FUNCTION__, __LINE__, &x, TRACE_MAXIMUM) 52 | #else 53 | #define FUNC_ENTRY StackTrace_entry(__func__, __LINE__, TRACE_MINIMUM) 54 | #define FUNC_ENTRY_NOLOG StackTrace_entry(__func__, __LINE__, -1) 55 | #define FUNC_ENTRY_MED StackTrace_entry(__func__, __LINE__, TRACE_MEDIUM) 56 | #define FUNC_ENTRY_MAX StackTrace_entry(__func__, __LINE__, TRACE_MAXIMUM) 57 | #define FUNC_EXIT StackTrace_exit(__func__, __LINE__, NULL, TRACE_MINIMUM) 58 | #define FUNC_EXIT_NOLOG StackTrace_exit(__func__, __LINE__, NULL, -1) 59 | #define FUNC_EXIT_MED StackTrace_exit(__func__, __LINE__, NULL, TRACE_MEDIUM) 60 | #define FUNC_EXIT_MAX StackTrace_exit(__func__, __LINE__, NULL, TRACE_MAXIMUM) 61 | #define FUNC_EXIT_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MINIMUM) 62 | #define FUNC_EXIT_MED_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MEDIUM) 63 | #define FUNC_EXIT_MAX_RC(x) StackTrace_exit(__func__, __LINE__, &x, TRACE_MAXIMUM) 64 | 65 | void StackTrace_entry(const char* name, int line, int trace); 66 | void StackTrace_exit(const char* name, int line, void* return_value, int trace); 67 | 68 | void StackTrace_printStack(FILE* dest); 69 | char* StackTrace_get(unsigned long); 70 | 71 | #endif 72 | 73 | #endif 74 | 75 | 76 | 77 | 78 | #endif /* STACKTRACE_H_ */ 79 | -------------------------------------------------------------------------------- /lib/MQTTPacket/transport.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Sergio R. Caprile - media specifics, nice api doc :^) 16 | *******************************************************************************/ 17 | 18 | typedef struct { 19 | int (*send)(unsigned char *address, unsigned int bytes); ///< pointer to function to send 'bytes' bytes, returns the actual number of bytes sent 20 | int (*recv)(unsigned char *address, unsigned int maxbytes); ///< pointer to function to receive upto 'maxbytes' bytes, returns the actual number of bytes copied 21 | } transport_iofunctions_t; 22 | 23 | #define TRANSPORT_DONE 1 24 | #define TRANSPORT_AGAIN 0 25 | #define TRANSPORT_ERROR -1 26 | /** 27 | @note Blocks until requested buflen is sent 28 | */ 29 | int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen); 30 | /** 31 | @note Blocks until requested count is received, as MQTTPacket_read() expects 32 | @warning This function is not supported (not implemented) 33 | @warning unless you provide a timeout, this function can block forever. Socket based systems do have 34 | a built in timeout, if your system can provide this, do modify this function, otherwise use getdatanb() instead 35 | @returns number of bytes read 36 | */ 37 | int transport_getdata(unsigned char* buf, int count); 38 | 39 | /** 40 | This is a bare metal implementation, so we must have non-blocking functions, 41 | the process of pumping to serial lines can result a bit slow and we don't want to busy wait. 42 | This function starts the process, you will call sendPacketBuffernb() until it reports success (or error) 43 | */ 44 | void transport_sendPacketBuffernb_start(int sock, unsigned char* buf, int buflen); 45 | /** 46 | This is a bare metal implementation, so we must have non-blocking functions, 47 | the process of pumping to serial lines can result a bit slow and we don't want to busy wait 48 | @returns TRANSPORT_DONE if finished, TRANSPORT_AGAIN for call again, or TRANSPORT_ERROR on error 49 | @note you will call again until it finishes (this is stream) 50 | */ 51 | int transport_sendPacketBuffernb(int sock); 52 | 53 | /** 54 | This is a bare metal implementation, so we must have non-blocking functions, 55 | the process of sucking from serial lines can result a bit slow and we don't want to busy wait 56 | @return the actual number of bytes read, 0 for none, or TRANSPORT_ERROR on error 57 | @note you will call again until total number of expected bytes is read (this is stream) 58 | */ 59 | int transport_getdatanb(void *sck, unsigned char* buf, int count); 60 | 61 | /** 62 | We assume whatever connection needs to be done, it is externally established by the specifics of the hardware 63 | E.g.: 64 | A cell modem: you will call AT+whatever and put the modem in transparent mode, OR, you will embed 65 | the AT+xSENDx / AT+xRECVx commands into the former sendPacketBuffer() and getdatanb() functions 66 | @param thisio pointer to a structure containing all necessary stuff to handle direct serial I/O 67 | @returns whatever indicator the system assigns to this link, if any. (a.k.a. : 'sock'), or TRANSPORT_ERROR for error 68 | */ 69 | int transport_open(transport_iofunctions_t *thisio); 70 | int transport_close(int sock); 71 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTPacket.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Xiang Rong - 442039 Add makefile to Embedded C client 16 | *******************************************************************************/ 17 | 18 | #ifndef MQTTPACKET_H_ 19 | #define MQTTPACKET_H_ 20 | 21 | #if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */ 22 | extern "C" { 23 | #endif 24 | 25 | #if defined(WIN32_DLL) || defined(WIN64_DLL) 26 | #define DLLImport __declspec(dllimport) 27 | #define DLLExport __declspec(dllexport) 28 | #elif defined(LINUX_SO) 29 | #define DLLImport extern 30 | #define DLLExport __attribute__ ((visibility ("default"))) 31 | #else 32 | #define DLLImport 33 | #define DLLExport 34 | #endif 35 | 36 | enum errors 37 | { 38 | MQTTPACKET_BUFFER_TOO_SHORT = -2, 39 | MQTTPACKET_READ_ERROR = -1, 40 | MQTTPACKET_READ_COMPLETE 41 | }; 42 | 43 | enum msgTypes 44 | { 45 | CONNECT = 1, CONNACK, PUBLISH, PUBACK, PUBREC, PUBREL, 46 | PUBCOMP, SUBSCRIBE, SUBACK, UNSUBSCRIBE, UNSUBACK, 47 | PINGREQ, PINGRESP, DISCONNECT 48 | #if defined(MQTTV5) 49 | , AUTH 50 | #endif 51 | }; 52 | 53 | /** 54 | * Bitfields for the MQTT header byte. 55 | */ 56 | typedef union 57 | { 58 | unsigned char byte; /**< the whole byte */ 59 | #if defined(REVERSED) 60 | struct 61 | { 62 | unsigned int type : 4; /**< message type nibble */ 63 | unsigned int dup : 1; /**< DUP flag bit */ 64 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 65 | unsigned int retain : 1; /**< retained flag bit */ 66 | } bits; 67 | #else 68 | struct 69 | { 70 | unsigned int retain : 1; /**< retained flag bit */ 71 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 72 | unsigned int dup : 1; /**< DUP flag bit */ 73 | unsigned int type : 4; /**< message type nibble */ 74 | } bits; 75 | #endif 76 | } MQTTHeader; 77 | 78 | typedef struct 79 | { 80 | int len; 81 | char* data; 82 | } MQTTLenString; 83 | 84 | typedef struct 85 | { 86 | char* cstring; 87 | MQTTLenString lenstring; 88 | } MQTTString; 89 | 90 | #define MQTTString_initializer {NULL, {0, NULL}} 91 | 92 | int MQTTstrlen(MQTTString mqttstring); 93 | 94 | #include "MQTTConnect.h" 95 | #include "MQTTPublish.h" 96 | #include "MQTTSubscribe.h" 97 | #include "MQTTUnsubscribe.h" 98 | #include "MQTTFormat.h" 99 | 100 | DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); 101 | DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); 102 | 103 | int MQTTPacket_VBIlen(int rem_len); 104 | int MQTTPacket_len(int rem_len); 105 | DLLExport int MQTTPacket_equals(MQTTString* a, char* b); 106 | 107 | DLLExport int MQTTPacket_encode(unsigned char* buf, int length); 108 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); 109 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); 110 | 111 | int readInt(unsigned char** pptr); 112 | char readChar(unsigned char** pptr); 113 | void writeChar(unsigned char** pptr, char c); 114 | void writeInt(unsigned char** pptr, int anInt); 115 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); 116 | void writeCString(unsigned char** pptr, const char* string); 117 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); 118 | 119 | DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); 120 | 121 | typedef struct { 122 | int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ 123 | void *sck; /* pointer to whatever the system may use to identify the transport */ 124 | int multiplier; 125 | int rem_len; 126 | int len; 127 | char state; 128 | }MQTTTransport; 129 | 130 | int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); 131 | 132 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 133 | } 134 | #endif 135 | 136 | 137 | #endif /* MQTTPACKET_H_ */ 138 | -------------------------------------------------------------------------------- /inc/FreeRTOSConfig.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef FREERTOS_CONFIG_H 29 | #define FREERTOS_CONFIG_H 30 | 31 | /*----------------------------------------------------------- 32 | * Application specific definitions. 33 | * 34 | * These definitions should be adjusted for your particular hardware and 35 | * application requirements. 36 | * 37 | * THESE PARAMETERS ARE DESCRIBED WITHIN THE 'CONFIGURATION' SECTION OF THE 38 | * FreeRTOS API DOCUMENTATION AVAILABLE ON THE FreeRTOS.org WEB SITE. 39 | * 40 | * See http://www.freertos.org/a00110.html 41 | *----------------------------------------------------------*/ 42 | 43 | #define configUSE_PREEMPTION 1 44 | #define configUSE_IDLE_HOOK 0 45 | #define configUSE_TICK_HOOK 0 46 | #define configCPU_CLOCK_HZ ( HSE_VALUE ) 47 | #define configTICK_RATE_HZ ( ( TickType_t ) 1000 ) 48 | #define configMAX_PRIORITIES ( 5 ) 49 | #define configMINIMAL_STACK_SIZE ( ( unsigned short ) 128 ) 50 | #define configTOTAL_HEAP_SIZE ( ( size_t ) ( 4 * 1024 ) ) 51 | #define configMAX_TASK_NAME_LEN ( 10 ) 52 | #define configUSE_TRACE_FACILITY 0 53 | #define configUSE_16_BIT_TICKS 0 54 | #define configIDLE_SHOULD_YIELD 1 55 | #define configUSE_MUTEXES 1 56 | #define configQUEUE_REGISTRY_SIZE 0 57 | #define configGENERATE_RUN_TIME_STATS 0 58 | #define configCHECK_FOR_STACK_OVERFLOW 0 59 | #define configUSE_RECURSIVE_MUTEXES 0 60 | #define configUSE_MALLOC_FAILED_HOOK 0 61 | #define configUSE_APPLICATION_TASK_TAG 0 62 | #define configUSE_COUNTING_SEMAPHORES 0 63 | #define configSUPPORT_STATIC_ALLOCATION 0 64 | 65 | /* Co-routine definitions. */ 66 | #define configUSE_CO_ROUTINES 0 67 | #define configMAX_CO_ROUTINE_PRIORITIES ( 2 ) 68 | 69 | /* Software timer definitions. */ 70 | #define configUSE_TIMERS 1 71 | #define configTIMER_TASK_PRIORITY ( 3 ) 72 | #define configTIMER_QUEUE_LENGTH 5 73 | #define configTIMER_TASK_STACK_DEPTH ( configMINIMAL_STACK_SIZE ) 74 | 75 | /* Set the following definitions to 1 to include the API function, or zero 76 | to exclude the API function. */ 77 | #define INCLUDE_vTaskPrioritySet 1 78 | #define INCLUDE_uxTaskPriorityGet 1 79 | #define INCLUDE_vTaskDelete 1 80 | #define INCLUDE_vTaskCleanUpResources 1 81 | #define INCLUDE_vTaskSuspend 1 82 | #define INCLUDE_vTaskDelayUntil 1 83 | #define INCLUDE_vTaskDelay 1 84 | 85 | /* Use the system definition, if there is one */ 86 | #ifdef __NVIC_PRIO_BITS 87 | #define configPRIO_BITS __NVIC_PRIO_BITS 88 | #else 89 | #define configPRIO_BITS 4 /* 15 priority levels */ 90 | #endif 91 | 92 | #define configLIBRARY_LOWEST_INTERRUPT_PRIORITY 15 93 | #define configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY 5 94 | 95 | /* The lowest priority. */ 96 | #define configKERNEL_INTERRUPT_PRIORITY ( configLIBRARY_LOWEST_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) ) 97 | /* Priority 5, or 95 as only the top four bits are implemented. */ 98 | /* !!!! configMAX_SYSCALL_INTERRUPT_PRIORITY must not be set to zero !!!! 99 | See http://www.FreeRTOS.org/RTOS-Cortex-M3-M4.html. */ 100 | #define configMAX_SYSCALL_INTERRUPT_PRIORITY ( configLIBRARY_MAX_SYSCALL_INTERRUPT_PRIORITY << (8 - configPRIO_BITS) ) 101 | 102 | #define configASSERT( x ) if( ( x ) == 0 ) { taskDISABLE_INTERRUPTS(); for( ;; ); } 103 | 104 | #define vPortSVCHandler SVC_Handler 105 | #define xPortPendSVHandler PendSV_Handler 106 | #define xPortSysTickHandler SysTick_Handler 107 | 108 | #endif /* FREERTOS_CONFIG_H */ 109 | 110 | -------------------------------------------------------------------------------- /lib/MQTTPacket/transport.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Sergio R. Caprile - port to the bare metal environment and serial media specifics 16 | *******************************************************************************/ 17 | 18 | /** By the way, this is a nice bare bones example, easier to expand to whatever non-OS 19 | media you might have */ 20 | 21 | #include 22 | #include 23 | #include 24 | #include 25 | #include 26 | #include "transport.h" 27 | 28 | /** 29 | This simple low-level implementation assumes a single connection for a single thread. Thus, single static 30 | variables are used for that connection. 31 | On other scenarios, you might want to put all these variables into a structure and index via the 'sock' 32 | parameter, as some functions show in the comments 33 | The blocking rx function is not supported. 34 | If you plan on writing one, take into account that the current implementation of 35 | MQTTPacket_read() has a function pointer for a function call to get the data to a buffer, but no provisions 36 | to know the caller or other indicator (the socket id): int (*getfn)(unsigned char*, int) 37 | */ 38 | static transport_iofunctions_t *io = NULL; 39 | static unsigned char *from = NULL; // to keep track of data sending 40 | static int howmany; // ditto 41 | 42 | 43 | void transport_sendPacketBuffernb_start(int sock, unsigned char* buf, int buflen) 44 | { 45 | from = buf; // from[sock] or mystruct[sock].from 46 | howmany = buflen; // myhowmany[sock] or mystruct[sock].howmany 47 | } 48 | 49 | int transport_sendPacketBuffernb(int sock) 50 | { 51 | transport_iofunctions_t *myio = io; // io[sock] or mystruct[sock].io 52 | int len; 53 | 54 | /* you should have called open() with a valid pointer to a valid struct and 55 | called sendPacketBuffernb_start with a valid buffer, before calling this */ 56 | assert((myio != NULL) && (myio->send != NULL) && (from != NULL)); 57 | if((len = myio->send(from, howmany)) > 0){ 58 | from += len; 59 | if((howmany -= len) <= 0){ 60 | return TRANSPORT_DONE; 61 | } 62 | } else if(len < 0){ 63 | return TRANSPORT_ERROR; 64 | } 65 | return TRANSPORT_AGAIN; 66 | } 67 | 68 | int transport_sendPacketBuffer(int sock, unsigned char* buf, int buflen) 69 | { 70 | int rc; 71 | 72 | transport_sendPacketBuffernb_start(sock, buf, buflen); 73 | while((rc=transport_sendPacketBuffernb(sock)) == TRANSPORT_AGAIN){ 74 | /* this is unlikely to loop forever unless there is a hardware problem */ 75 | } 76 | if(rc == TRANSPORT_DONE){ 77 | return buflen; 78 | } 79 | return TRANSPORT_ERROR; 80 | } 81 | 82 | 83 | int transport_getdata(unsigned char* buf, int count) 84 | { 85 | assert(0); /* This function is NOT supported, it is just here to tease you */ 86 | return TRANSPORT_ERROR; /* nah, it is here for similarity with other transport examples */ 87 | } 88 | 89 | int transport_getdatanb(void *sck, unsigned char* buf, int count) 90 | { 91 | //int sock = *((int *)sck); /* sck: pointer to whatever the system may use to identify the transport */ 92 | transport_iofunctions_t *myio = io; // io[sock] or mystruct[sock].io 93 | int len; 94 | 95 | /* you should have called open() with a valid pointer to a valid struct before calling this */ 96 | assert((myio != NULL) && (myio->recv != NULL)); 97 | /* this call will return immediately if no bytes, or return whatever outstanding bytes we have, 98 | upto count */ 99 | if((len = myio->recv(buf, count)) >= 0) 100 | return len; 101 | return TRANSPORT_ERROR; 102 | } 103 | 104 | /** 105 | return >=0 for a connection descriptor, <0 for an error code 106 | */ 107 | int transport_open(transport_iofunctions_t *thisio) 108 | { 109 | int idx=0; // for multiple connections, you might, basically turn myio into myio[MAX_CONNECTIONS], 110 | 111 | //if((idx=assignidx()) >= MAX_CONNECTIONS) // somehow assign an index, 112 | // return TRANSPORT_ERROR; 113 | io = thisio; // store myio[idx] = thisio, or mystruct[idx].io = thisio, 114 | return idx; // and return the index used 115 | } 116 | 117 | int transport_close(int sock) 118 | { 119 | int rc=TRANSPORT_DONE; 120 | 121 | return rc; 122 | } 123 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTUnsubscribeServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - MQTT V5 implementation 16 | *******************************************************************************/ 17 | 18 | #if defined(MQTTV5) 19 | #include "V5/MQTTV5Packet.h" 20 | #else 21 | #include "MQTTPacket.h" 22 | #endif 23 | 24 | #include "StackTrace.h" 25 | 26 | #include 27 | 28 | 29 | /** 30 | * Deserializes the supplied (wire) buffer into unsubscribe data 31 | * @param dup integer returned - the MQTT dup flag 32 | * @param packetid integer returned - the MQTT packet identifier 33 | * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays 34 | * @param count - number of members in the topicFilters and requestedQoSs arrays 35 | * @param topicFilters - array of topic filter names 36 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 37 | * @param buflen the length in bytes of the data in the supplied buffer 38 | * @return the length of the serialized data. <= 0 indicates error 39 | */ 40 | #if defined(MQTTV5) 41 | int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], 42 | unsigned char* buf, int len) 43 | { 44 | return MQTTV5Deserialize_unsubscribe(dup, packetid, NULL, maxcount, count, topicFilters, buf, len); 45 | } 46 | 47 | DLLExport int MQTTV5Deserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, MQTTProperties* properties, 48 | int maxcount, int* count, MQTTString topicFilters[], unsigned char* buf, int len) 49 | #else 50 | int MQTTDeserialize_unsubscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], 51 | unsigned char* buf, int len) 52 | #endif 53 | { 54 | MQTTHeader header = {0}; 55 | unsigned char* curdata = buf; 56 | unsigned char* enddata = NULL; 57 | int rc = 0; 58 | int mylen = 0; 59 | 60 | FUNC_ENTRY; 61 | header.byte = readChar(&curdata); 62 | if (header.bits.type != UNSUBSCRIBE) 63 | goto exit; 64 | *dup = header.bits.dup; 65 | 66 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 67 | enddata = curdata + mylen; 68 | 69 | *packetid = readInt(&curdata); 70 | 71 | #if defined(MQTTV5) 72 | if (properties) 73 | { 74 | if (enddata == curdata) 75 | properties->length = properties->count = 0; /* signal that no properties were received */ 76 | else if (!MQTTProperties_read(properties, &curdata, enddata)) 77 | goto exit; 78 | } 79 | #endif 80 | 81 | *count = 0; 82 | while (curdata < enddata) 83 | { 84 | if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) 85 | goto exit; 86 | (*count)++; 87 | } 88 | 89 | rc = 1; 90 | exit: 91 | FUNC_EXIT_RC(rc); 92 | return rc; 93 | } 94 | 95 | 96 | /** 97 | * Serializes the supplied unsuback data into the supplied buffer, ready for sending 98 | * @param buf the buffer into which the packet will be serialized 99 | * @param buflen the length in bytes of the supplied buffer 100 | * @param packetid integer - the MQTT packet identifier 101 | * @return the length of the serialized data. <= 0 indicates error 102 | */ 103 | #if defined(MQTTV5) 104 | int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid) 105 | { 106 | return MQTTV5Serialize_unsuback(buf, buflen, packetid, NULL, 0, NULL); 107 | } 108 | 109 | int MQTTV5Serialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid, 110 | MQTTProperties* properties, int count, int* reasonCodes) 111 | #else 112 | int MQTTSerialize_unsuback(unsigned char* buf, int buflen, unsigned short packetid) 113 | #endif 114 | { 115 | MQTTHeader header = {0}; 116 | int rc = 0; 117 | unsigned char *ptr = buf; 118 | int len = 2; 119 | #if defined(MQTTV5) 120 | int i = 0; 121 | #endif 122 | 123 | FUNC_ENTRY; 124 | #if defined(MQTTV5) 125 | if (properties) 126 | len += MQTTProperties_len(properties); 127 | len += count; 128 | #endif 129 | if (buflen < len) 130 | { 131 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 132 | goto exit; 133 | } 134 | header.byte = 0; 135 | header.bits.type = UNSUBACK; 136 | writeChar(&ptr, header.byte); /* write header */ 137 | 138 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 139 | 140 | writeInt(&ptr, packetid); 141 | 142 | #if defined(MQTTV5) 143 | if (properties && MQTTProperties_write(&ptr, properties) < 0) 144 | goto exit; 145 | 146 | if (reasonCodes) 147 | { 148 | for (i = 0; i < count; ++i) 149 | writeChar(&ptr, reasonCodes[i]); 150 | } 151 | #endif 152 | 153 | rc = ptr - buf; 154 | exit: 155 | FUNC_EXIT_RC(rc); 156 | return rc; 157 | } 158 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTConnect.h: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - add connack return code definitions 16 | * Xiang Rong - 442039 Add makefile to Embedded C client 17 | * Ian Craggs - fix for issue #64, bit order in connack response 18 | *******************************************************************************/ 19 | 20 | #ifndef MQTTCONNECT_H_ 21 | #define MQTTCONNECT_H_ 22 | 23 | enum connack_return_codes 24 | { 25 | MQTT_CONNECTION_ACCEPTED = 0, 26 | MQTT_UNNACCEPTABLE_PROTOCOL = 1, 27 | MQTT_CLIENTID_REJECTED = 2, 28 | MQTT_SERVER_UNAVAILABLE = 3, 29 | MQTT_BAD_USERNAME_OR_PASSWORD = 4, 30 | MQTT_NOT_AUTHORIZED = 5, 31 | }; 32 | 33 | #if !defined(DLLImport) 34 | #define DLLImport 35 | #endif 36 | #if !defined(DLLExport) 37 | #define DLLExport 38 | #endif 39 | 40 | 41 | typedef union 42 | { 43 | unsigned char all; /**< all connect flags */ 44 | #if defined(REVERSED) 45 | struct 46 | { 47 | unsigned int username : 1; /**< 3.1 user name */ 48 | unsigned int password : 1; /**< 3.1 password */ 49 | unsigned int willRetain : 1; /**< will retain setting */ 50 | unsigned int willQoS : 2; /**< will QoS value */ 51 | unsigned int will : 1; /**< will flag */ 52 | unsigned int cleansession : 1; /**< clean session flag */ 53 | unsigned int : 1; /**< unused */ 54 | } bits; 55 | #else 56 | struct 57 | { 58 | unsigned int : 1; /**< unused */ 59 | unsigned int cleansession : 1; /**< cleansession flag */ 60 | unsigned int will : 1; /**< will flag */ 61 | unsigned int willQoS : 2; /**< will QoS value */ 62 | unsigned int willRetain : 1; /**< will retain setting */ 63 | unsigned int password : 1; /**< 3.1 password */ 64 | unsigned int username : 1; /**< 3.1 user name */ 65 | } bits; 66 | #endif 67 | } MQTTConnectFlags; /**< connect flags byte */ 68 | 69 | 70 | 71 | /** 72 | * Defines the MQTT "Last Will and Testament" (LWT) settings for 73 | * the connect packet. 74 | */ 75 | typedef struct 76 | { 77 | /** The eyecatcher for this structure. must be MQTW. */ 78 | char struct_id[4]; 79 | /** The version number of this structure. Must be 0 */ 80 | int struct_version; 81 | /** The LWT topic to which the LWT message will be published. */ 82 | MQTTString topicName; 83 | /** The LWT payload. */ 84 | MQTTString message; 85 | /** 86 | * The retained flag for the LWT message (see MQTTAsync_message.retained). 87 | */ 88 | unsigned char retained; 89 | /** 90 | * The quality of service setting for the LWT message (see 91 | * MQTTAsync_message.qos and @ref qos). 92 | */ 93 | char qos; 94 | } MQTTPacket_willOptions; 95 | 96 | 97 | #define MQTTPacket_willOptions_initializer { {'M', 'Q', 'T', 'W'}, 0, {NULL, {0, NULL}}, {NULL, {0, NULL}}, 0, 0 } 98 | 99 | 100 | typedef struct 101 | { 102 | /** The eyecatcher for this structure. must be MQTC. */ 103 | char struct_id[4]; 104 | /** The version number of this structure. Must be 0 */ 105 | int struct_version; 106 | /** Version of MQTT to be used. 3 = 3.1 4 = 3.1.1 107 | */ 108 | unsigned char MQTTVersion; 109 | MQTTString clientID; 110 | unsigned short keepAliveInterval; 111 | unsigned char cleansession; 112 | unsigned char willFlag; 113 | MQTTPacket_willOptions will; 114 | MQTTString username; 115 | MQTTString password; 116 | } MQTTPacket_connectData; 117 | 118 | typedef union 119 | { 120 | unsigned char all; /**< all connack flags */ 121 | #if defined(REVERSED) 122 | struct 123 | { 124 | unsigned int reserved : 7; /**< unused */ 125 | unsigned int sessionpresent : 1; /**< session present flag */ 126 | } bits; 127 | #else 128 | struct 129 | { 130 | unsigned int sessionpresent : 1; /**< session present flag */ 131 | unsigned int reserved: 7; /**< unused */ 132 | } bits; 133 | #endif 134 | } MQTTConnackFlags; /**< connack flags byte */ 135 | 136 | #define MQTTPacket_connectData_initializer { {'M', 'Q', 'T', 'C'}, 0, 4, {NULL, {0, NULL}}, 60, 1, 0, \ 137 | MQTTPacket_willOptions_initializer, {NULL, {0, NULL}}, {NULL, {0, NULL}} } 138 | 139 | DLLExport int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options); 140 | DLLExport int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len); 141 | 142 | DLLExport int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent); 143 | DLLExport int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen); 144 | 145 | DLLExport int MQTTSerialize_disconnect(unsigned char* buf, int buflen); 146 | DLLExport int MQTTDeserialize_disconnect(unsigned char* buf, int buflen); 147 | DLLExport int MQTTSerialize_pingreq(unsigned char* buf, int buflen); 148 | 149 | #endif /* MQTTCONNECT_H_ */ 150 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/portable/MemMang/heap_1.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | 29 | /* 30 | * The simplest possible implementation of pvPortMalloc(). Note that this 31 | * implementation does NOT allow allocated memory to be freed again. 32 | * 33 | * See heap_2.c, heap_3.c and heap_4.c for alternative implementations, and the 34 | * memory management pages of http://www.FreeRTOS.org for more information. 35 | */ 36 | #include 37 | 38 | /* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining 39 | all the API functions to use the MPU wrappers. That should only be done when 40 | task.h is included from an application file. */ 41 | #define MPU_WRAPPERS_INCLUDED_FROM_API_FILE 42 | 43 | #include "FreeRTOS.h" 44 | #include "task.h" 45 | 46 | #undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE 47 | 48 | #if( configSUPPORT_DYNAMIC_ALLOCATION == 0 ) 49 | #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0 50 | #endif 51 | 52 | /* A few bytes might be lost to byte aligning the heap start address. */ 53 | #define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT ) 54 | 55 | /* Allocate the memory for the heap. */ 56 | #if( configAPPLICATION_ALLOCATED_HEAP == 1 ) 57 | /* The application writer has already defined the array used for the RTOS 58 | heap - probably so it can be placed in a special segment or address. */ 59 | extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ]; 60 | #else 61 | static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ]; 62 | #endif /* configAPPLICATION_ALLOCATED_HEAP */ 63 | 64 | /* Index into the ucHeap array. */ 65 | static size_t xNextFreeByte = ( size_t ) 0; 66 | 67 | /*-----------------------------------------------------------*/ 68 | 69 | void *pvPortMalloc( size_t xWantedSize ) 70 | { 71 | void *pvReturn = NULL; 72 | static uint8_t *pucAlignedHeap = NULL; 73 | 74 | /* Ensure that blocks are always aligned to the required number of bytes. */ 75 | #if( portBYTE_ALIGNMENT != 1 ) 76 | { 77 | if( xWantedSize & portBYTE_ALIGNMENT_MASK ) 78 | { 79 | /* Byte alignment required. */ 80 | xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) ); 81 | } 82 | } 83 | #endif 84 | 85 | vTaskSuspendAll(); 86 | { 87 | if( pucAlignedHeap == NULL ) 88 | { 89 | /* Ensure the heap starts on a correctly aligned boundary. */ 90 | pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) ); 91 | } 92 | 93 | /* Check there is enough room left for the allocation. */ 94 | if( ( ( xNextFreeByte + xWantedSize ) < configADJUSTED_HEAP_SIZE ) && 95 | ( ( xNextFreeByte + xWantedSize ) > xNextFreeByte ) )/* Check for overflow. */ 96 | { 97 | /* Return the next free byte then increment the index past this 98 | block. */ 99 | pvReturn = pucAlignedHeap + xNextFreeByte; 100 | xNextFreeByte += xWantedSize; 101 | } 102 | 103 | traceMALLOC( pvReturn, xWantedSize ); 104 | } 105 | ( void ) xTaskResumeAll(); 106 | 107 | #if( configUSE_MALLOC_FAILED_HOOK == 1 ) 108 | { 109 | if( pvReturn == NULL ) 110 | { 111 | extern void vApplicationMallocFailedHook( void ); 112 | vApplicationMallocFailedHook(); 113 | } 114 | } 115 | #endif 116 | 117 | return pvReturn; 118 | } 119 | /*-----------------------------------------------------------*/ 120 | 121 | void vPortFree( void *pv ) 122 | { 123 | /* Memory cannot be freed using this scheme. See heap_2.c, heap_3.c and 124 | heap_4.c for alternative implementations, and the memory management pages of 125 | http://www.FreeRTOS.org for more information. */ 126 | ( void ) pv; 127 | 128 | /* Force an assert as it is invalid to call this function. */ 129 | configASSERT( pv == NULL ); 130 | } 131 | /*-----------------------------------------------------------*/ 132 | 133 | void vPortInitialiseBlocks( void ) 134 | { 135 | /* Only required when static memory is not cleared. */ 136 | xNextFreeByte = ( size_t ) 0; 137 | } 138 | /*-----------------------------------------------------------*/ 139 | 140 | size_t xPortGetFreeHeapSize( void ) 141 | { 142 | return ( configADJUSTED_HEAP_SIZE - xNextFreeByte ); 143 | } 144 | 145 | 146 | 147 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTUnsubscribeClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - MQTT V5 implementation 16 | *******************************************************************************/ 17 | 18 | #if defined(MQTTV5) 19 | #include "V5/MQTTV5Packet.h" 20 | #else 21 | #include "MQTTPacket.h" 22 | #endif 23 | 24 | #include "StackTrace.h" 25 | 26 | #include 27 | 28 | /** 29 | * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters 30 | * @param count the number of topic filter strings in topicFilters 31 | * @param topicFilters the array of topic filter strings to be used in the publish 32 | * @return the length of buffer needed to contain the serialized version of the packet 33 | */ 34 | #if defined(MQTTV5) 35 | int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[], MQTTProperties* properties) 36 | #else 37 | int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) 38 | #endif 39 | { 40 | int i; 41 | int len = 2; /* packetid */ 42 | 43 | for (i = 0; i < count; ++i) 44 | len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/ 45 | #if defined(MQTTV5) 46 | if (properties) 47 | len += MQTTProperties_len(properties); 48 | #endif 49 | return len; 50 | } 51 | 52 | 53 | /** 54 | * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending 55 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 56 | * @param buflen the length in bytes of the data in the supplied buffer 57 | * @param dup integer - the MQTT dup flag 58 | * @param packetid integer - the MQTT packet identifier 59 | * @param count - number of members in the topicFilters array 60 | * @param topicFilters - array of topic filter names 61 | * @return the length of the serialized data. <= 0 indicates error 62 | */ 63 | #if defined(MQTTV5) 64 | int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 65 | int count, MQTTString topicFilters[]) 66 | { 67 | return MQTTV5Serialize_unsubscribe(buf, buflen, dup, packetid, NULL, count, topicFilters); 68 | } 69 | 70 | int MQTTV5Serialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 71 | MQTTProperties* properties, int count, MQTTString topicFilters[]) 72 | #else 73 | int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 74 | int count, MQTTString topicFilters[]) 75 | #endif 76 | { 77 | unsigned char *ptr = buf; 78 | MQTTHeader header = {0}; 79 | int rem_len = 0; 80 | int rc = -1; 81 | int i = 0; 82 | 83 | FUNC_ENTRY; 84 | #if defined(MQTTV5) 85 | if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters, properties)) > buflen) 86 | #else 87 | if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) 88 | #endif 89 | { 90 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 91 | goto exit; 92 | } 93 | 94 | header.byte = 0; 95 | header.bits.type = UNSUBSCRIBE; 96 | header.bits.dup = dup; 97 | header.bits.qos = 1; 98 | writeChar(&ptr, header.byte); /* write header */ 99 | 100 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 101 | 102 | writeInt(&ptr, packetid); 103 | 104 | #if defined(MQTTV5) 105 | if (properties && MQTTProperties_write(&ptr, properties) < 0) 106 | goto exit; 107 | #endif 108 | 109 | for (i = 0; i < count; ++i) 110 | writeMQTTString(&ptr, topicFilters[i]); 111 | 112 | rc = ptr - buf; 113 | exit: 114 | FUNC_EXIT_RC(rc); 115 | return rc; 116 | } 117 | 118 | 119 | /** 120 | * Deserializes the supplied (wire) buffer into unsuback data 121 | * @param packetid returned integer - the MQTT packet identifier 122 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 123 | * @param buflen the length in bytes of the data in the supplied buffer 124 | * @return error code. 1 is success, 0 is failure 125 | */ 126 | #if defined(MQTTV5) 127 | int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) 128 | { 129 | return MQTTV5Deserialize_unsuback(packetid, NULL, 0, 0, NULL, buf, buflen); 130 | } 131 | 132 | int MQTTV5Deserialize_unsuback(unsigned short* packetid, MQTTProperties* properties, 133 | int maxcount, int* count, int* reasonCodes, unsigned char* buf, int buflen) 134 | #else 135 | int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) 136 | #endif 137 | { 138 | #if !defined(MQTTV5) 139 | unsigned char type = 0; 140 | unsigned char dup = 0; 141 | #endif 142 | int rc = 0; 143 | 144 | FUNC_ENTRY; 145 | #if defined(MQTTV5) 146 | rc = MQTTV5Deserialize_subunsuback(UNSUBACK, packetid, properties, 147 | maxcount, count, reasonCodes, buf, buflen); 148 | #else 149 | rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen); 150 | if (type == UNSUBACK) 151 | rc = 1; 152 | #endif 153 | FUNC_EXIT_RC(rc); 154 | return rc; 155 | } 156 | -------------------------------------------------------------------------------- /debug.launch: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | -------------------------------------------------------------------------------- /src/main.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file main.c 3 | * @author Atakan S. 4 | * @date 01/01/2019 5 | * @version 1.0 6 | * @brief PAHO Embedded demo publishes temperature to MQTT via ESP8266. 7 | * 8 | * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | * DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | // Include 30 | #include "FreeRTOS.h" 31 | #include "task.h" 32 | #include "MQTTPacket.h" 33 | #include "transport.h" 34 | #include "networkwrapper.h" 35 | #include "temperature.h" 36 | #include "led.h" 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | // Settings. 45 | #define CONNECTION_KEEPALIVE_S 60UL 46 | 47 | /* 48 | * @brief Publishes temperature value to mqtt. 49 | * @param pvParameters Task call parameters. 50 | */ 51 | static void prvTemperaturePublisher(void *pvParameters) { 52 | unsigned char buffer[128]; 53 | MQTTTransport transporter; 54 | int result; 55 | int length; 56 | 57 | // Transport layer uses the esp8266 networkwrapper. 58 | static transport_iofunctions_t iof = {network_send, network_recv}; 59 | int transport_socket = transport_open(&iof); 60 | 61 | // State machine. 62 | int internalState = 0; 63 | while(true) { 64 | switch(internalState){ 65 | case 0: { 66 | // Turn the LED off. 67 | vLedWrite(0, false); 68 | 69 | // Initialize the temperature sensor. 70 | temperature_init(); 71 | 72 | // Initialize the network and connect to 73 | network_init(); 74 | if(network_connect("iot.eclipse.org", 1883, CONNECTION_KEEPALIVE_S, false) == 0){ 75 | // To the next state. 76 | internalState++; 77 | } 78 | } break; 79 | case 1: { 80 | // Populate the transporter. 81 | transporter.sck = &transport_socket; 82 | transporter.getfn = transport_getdatanb; 83 | transporter.state = 0; 84 | 85 | // Populate the connect struct. 86 | MQTTPacket_connectData connectData = MQTTPacket_connectData_initializer; 87 | connectData.MQTTVersion = 3; 88 | connectData.clientID.cstring = "TemperaturePublisher"; 89 | connectData.keepAliveInterval = CONNECTION_KEEPALIVE_S * 2; 90 | length = MQTTSerialize_connect(buffer, sizeof(buffer), &connectData); 91 | 92 | // Send CONNECT to the mqtt broker. 93 | if((result = transport_sendPacketBuffer(transport_socket, buffer, length)) == length){ 94 | // To the next state. 95 | internalState++; 96 | } else { 97 | // Start over. 98 | internalState = 0; 99 | } 100 | } break; 101 | case 2: { 102 | // Wait for CONNACK response from the mqtt broker. 103 | while(true) { 104 | // Wait until the transfer is done. 105 | if((result = MQTTPacket_readnb(buffer, sizeof(buffer), &transporter)) == CONNACK){ 106 | // Check if the connection was accepted. 107 | unsigned char sessionPresent, connack_rc; 108 | if ((MQTTDeserialize_connack(&sessionPresent, &connack_rc, buffer, sizeof(buffer)) != 1) || (connack_rc != 0)){ 109 | // Start over. 110 | internalState = 0; 111 | break; 112 | }else{ 113 | // To the next state. 114 | internalState++; 115 | break; 116 | } 117 | } else if (result == -1) { 118 | // Start over. 119 | internalState = 0; 120 | break; 121 | } 122 | } 123 | } break; 124 | case 3: { 125 | // Turn the LED on. 126 | vLedWrite(0, true); 127 | 128 | // Set delay timer. 129 | TickType_t wakeTime = xTaskGetTickCount(); 130 | 131 | // Populate the publish message. 132 | MQTTString topicString = MQTTString_initializer; 133 | topicString.cstring = "temperature/value"; 134 | unsigned char payload[16]; 135 | length = MQTTSerialize_publish(buffer, sizeof(buffer), 0, 0, 0, 0, topicString, payload, (length = sprintf(payload, "%d", (int)temperature_read()))); 136 | 137 | // Send PUBLISH to the mqtt broker. 138 | if((result = transport_sendPacketBuffer(transport_socket, buffer, length)) == length){ 139 | // Turn the LED off. 140 | vLedWrite(0, false); 141 | 142 | // Wait 5s. 143 | vTaskDelayUntil(&wakeTime, pdMS_TO_TICKS(5000)); 144 | } else { 145 | // Start over. 146 | internalState = 0; 147 | } 148 | } break; 149 | default: 150 | internalState = 0; 151 | } 152 | } 153 | } 154 | 155 | /* 156 | * @brief Time provider for the networkwrapper.. 157 | * @return Time in ms. 158 | */ 159 | long unsigned int network_gettime_ms(void) { 160 | return (xTaskGetTickCount() * portTICK_PERIOD_MS); 161 | } 162 | 163 | /* 164 | * @brief Main. 165 | * @return Non-zero on error. 166 | */ 167 | int main(void) { 168 | // Create the task. 169 | xTaskCreate(prvTemperaturePublisher, "TempPub", 512UL, NULL, (tskIDLE_PRIORITY + 1), NULL); 170 | 171 | // Start FreeRTOS. 172 | vTaskStartScheduler(); 173 | 174 | // Program should not reach here. 175 | return 1; 176 | } 177 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTDeserializePublish.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - MQTT V5 implementation 16 | *******************************************************************************/ 17 | 18 | #if defined(MQTTV5) 19 | #include "V5/MQTTV5Packet.h" 20 | #else 21 | #include "MQTTPacket.h" 22 | #endif 23 | #include "StackTrace.h" 24 | #include 25 | 26 | #define min(a, b) ((a < b) ? 1 : 0) 27 | 28 | /** 29 | * Deserializes the supplied (wire) buffer into publish data 30 | * @param dup returned integer - the MQTT dup flag 31 | * @param qos returned integer - the MQTT QoS value 32 | * @param retained returned integer - the MQTT retained flag 33 | * @param packetid returned integer - the MQTT packet identifier 34 | * @param topicName returned MQTTString - the MQTT topic in the publish 35 | * @param payload returned byte buffer - the MQTT publish payload 36 | * @param payloadlen returned integer - the length of the MQTT payload 37 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 38 | * @param buflen the length in bytes of the data in the supplied buffer 39 | * @return error code. 1 is success 40 | */ 41 | #if defined(MQTTV5) 42 | int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 43 | unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 44 | { 45 | return MQTTV5Deserialize_publish(dup, qos, retained, packetid, topicName, NULL, payload, payloadlen, buf, buflen); 46 | } 47 | 48 | int MQTTV5Deserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 49 | MQTTProperties* properties, unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 50 | #else 51 | int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 52 | unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 53 | #endif 54 | { 55 | MQTTHeader header = {0}; 56 | unsigned char* curdata = buf; 57 | unsigned char* enddata = NULL; 58 | int rc = 0; 59 | int mylen = 0; 60 | 61 | FUNC_ENTRY; 62 | header.byte = readChar(&curdata); 63 | if (header.bits.type != PUBLISH) 64 | goto exit; 65 | *dup = header.bits.dup; 66 | *qos = header.bits.qos; 67 | *retained = header.bits.retain; 68 | 69 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 70 | enddata = curdata + mylen; 71 | 72 | if (!readMQTTLenString(topicName, &curdata, enddata) || 73 | enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */ 74 | goto exit; 75 | 76 | if (*qos > 0) 77 | *packetid = readInt(&curdata); 78 | 79 | #if defined(MQTTV5) 80 | if (properties && !MQTTProperties_read(properties, &curdata, enddata)) 81 | goto exit; 82 | #endif 83 | 84 | *payloadlen = enddata - curdata; 85 | *payload = curdata; 86 | rc = 1; 87 | exit: 88 | FUNC_EXIT_RC(rc); 89 | return rc; 90 | } 91 | 92 | 93 | 94 | /** 95 | * Deserializes the supplied (wire) buffer into an ack 96 | * @param packettype returned integer - the MQTT packet type 97 | * @param dup returned integer - the MQTT dup flag 98 | * @param packetid returned integer - the MQTT packet identifier 99 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 100 | * @param buflen the length in bytes of the data in the supplied buffer 101 | * @return error code. 1 is success, 0 is failure 102 | */ 103 | #if defined(MQTTV5) 104 | int MQTTV5Deserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, 105 | int* reasonCode, MQTTProperties* properties, unsigned char* buf, int buflen); 106 | 107 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 108 | { 109 | return MQTTV5Deserialize_ack(packettype, dup, packetid, NULL, NULL, buf, buflen); 110 | } 111 | 112 | int MQTTV5Deserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, 113 | int *reasonCode, MQTTProperties* properties, unsigned char* buf, int buflen) 114 | #else 115 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 116 | #endif 117 | { 118 | MQTTHeader header = {0}; 119 | unsigned char* curdata = buf; 120 | unsigned char* enddata = NULL; 121 | int rc = 0; 122 | int mylen; 123 | 124 | FUNC_ENTRY; 125 | header.byte = readChar(&curdata); 126 | *dup = header.bits.dup; 127 | *packettype = header.bits.type; 128 | 129 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 130 | enddata = curdata + mylen; 131 | 132 | if (enddata - curdata < 2) 133 | goto exit; 134 | *packetid = readInt(&curdata); 135 | 136 | #if defined(MQTTV5) 137 | if (reasonCode) 138 | { 139 | if (enddata == curdata) /* no reason code, or properties */ 140 | *reasonCode = 0; 141 | else 142 | *reasonCode = readChar(&curdata); 143 | } 144 | 145 | if (properties) 146 | { 147 | if (enddata == curdata) 148 | properties->length = properties->count = 0; /* signal that no properties were received */ 149 | else if (!MQTTProperties_read(properties, &curdata, enddata)) 150 | goto exit; 151 | } 152 | #endif 153 | 154 | rc = 1; 155 | exit: 156 | FUNC_EXIT_RC(rc); 157 | return rc; 158 | } 159 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTSubscribeServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - MQTT V5.0 support 16 | *******************************************************************************/ 17 | 18 | #if defined(MQTTV5) 19 | #include "V5/MQTTV5Packet.h" 20 | #else 21 | #include "MQTTPacket.h" 22 | #endif 23 | #include "StackTrace.h" 24 | 25 | #include 26 | 27 | 28 | /** 29 | * Deserializes the supplied (wire) buffer into subscribe data 30 | * @param dup integer returned - the MQTT dup flag 31 | * @param packetid integer returned - the MQTT packet identifier 32 | * @param maxcount - the maximum number of members allowed in the topicFilters and requestedQoSs arrays 33 | * @param count - number of members in the topicFilters and requestedQoSs arrays 34 | * @param topicFilters - array of topic filter names 35 | * @param requestedQoSs - array of requested QoS 36 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 37 | * @param buflen the length in bytes of the data in the supplied buffer 38 | * @return the length of the serialized data. <= 0 indicates error 39 | */ 40 | #if defined(MQTTV5) 41 | int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, 42 | int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], 43 | unsigned char* buf, int buflen) 44 | { 45 | return MQTTV5Deserialize_subscribe(dup, packetid, NULL, 46 | maxcount, count, topicFilters, requestedQoSs, NULL, buf, buflen); 47 | } 48 | 49 | int MQTTV5Deserialize_subscribe(unsigned char* dup, unsigned short* packetid, MQTTProperties* properties, 50 | int maxcount, int* count, MQTTString topicFilters[], int requestedQoSs[], struct subscribeOptions options[], 51 | unsigned char* buf, int buflen) 52 | #else 53 | int MQTTDeserialize_subscribe(unsigned char* dup, unsigned short* packetid, int maxcount, int* count, MQTTString topicFilters[], 54 | int requestedQoSs[], unsigned char* buf, int buflen) 55 | #endif 56 | { 57 | MQTTHeader header = {0}; 58 | unsigned char* curdata = buf; 59 | unsigned char* enddata = NULL; 60 | int rc = -1; 61 | int mylen = 0; 62 | 63 | FUNC_ENTRY; 64 | header.byte = readChar(&curdata); 65 | if (header.bits.type != SUBSCRIBE) 66 | goto exit; 67 | *dup = header.bits.dup; 68 | 69 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 70 | enddata = curdata + mylen; 71 | 72 | *packetid = readInt(&curdata); 73 | 74 | #if defined(MQTTV5) 75 | if (properties && !MQTTProperties_read(properties, &curdata, enddata)) 76 | goto exit; 77 | #endif 78 | 79 | *count = 0; 80 | while (curdata < enddata) 81 | { 82 | if (!readMQTTLenString(&topicFilters[*count], &curdata, enddata)) 83 | goto exit; 84 | if (curdata >= enddata) /* do we have enough data to read the req_qos version byte? */ 85 | goto exit; 86 | requestedQoSs[*count] = readChar(&curdata); 87 | #if defined(MQTTV5) 88 | if (options) 89 | { 90 | options[*count].noLocal = (requestedQoSs[*count] >> 2) & 0x01; /* 1 bit */ 91 | options[*count].retainAsPublished = (requestedQoSs[*count] >> 3) & 0x01; /* 1 bit */ 92 | options[*count].retainHandling = (requestedQoSs[*count] >> 4) & 0x03; /* 2 bits */ 93 | } 94 | requestedQoSs[*count] &= 0x03; /* 0 all except qos bits */ 95 | #endif 96 | (*count)++; 97 | } 98 | 99 | rc = 1; 100 | exit: 101 | FUNC_EXIT_RC(rc); 102 | return rc; 103 | } 104 | 105 | 106 | /** 107 | * Serializes the supplied suback data into the supplied buffer, ready for sending 108 | * @param buf the buffer into which the packet will be serialized 109 | * @param buflen the length in bytes of the supplied buffer 110 | * @param packetid integer - the MQTT packet identifier 111 | * @param count - number of members in the grantedQoSs array 112 | * @param grantedQoSs - array of granted QoS 113 | * @return the length of the serialized data. <= 0 indicates error 114 | */ 115 | #if defined(MQTTV5) 116 | int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs) 117 | { 118 | return MQTTV5Serialize_suback(buf, buflen, packetid, NULL, count, grantedQoSs); 119 | } 120 | 121 | int MQTTV5Serialize_suback(unsigned char* buf, int buflen, unsigned short packetid, 122 | MQTTProperties* properties, int count, int* reasonCodes) 123 | #else 124 | int MQTTSerialize_suback(unsigned char* buf, int buflen, unsigned short packetid, int count, int* grantedQoSs) 125 | #endif 126 | { 127 | MQTTHeader header = {0}; 128 | int rc = -1; 129 | unsigned char *ptr = buf; 130 | int i; 131 | int len = 0; 132 | 133 | FUNC_ENTRY; 134 | #if defined(MQTTV5) 135 | if (properties) 136 | len += MQTTProperties_len(properties); 137 | #endif 138 | len += count + 2; 139 | if (buflen < len) 140 | { 141 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 142 | goto exit; 143 | } 144 | header.byte = 0; 145 | header.bits.type = SUBACK; 146 | writeChar(&ptr, header.byte); /* write header */ 147 | 148 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 149 | 150 | writeInt(&ptr, packetid); 151 | 152 | #if defined(MQTTV5) 153 | if (properties && MQTTProperties_write(&ptr, properties) < 0) 154 | goto exit; 155 | #endif 156 | 157 | for (i = 0; i < count; ++i) 158 | #if defined(MQTTV5) 159 | writeChar(&ptr, reasonCodes[i]); 160 | #else 161 | writeChar(&ptr, grantedQoSs[i]); 162 | #endif 163 | 164 | rc = ptr - buf; 165 | exit: 166 | FUNC_EXIT_RC(rc); 167 | return rc; 168 | } 169 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/projdefs.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef PROJDEFS_H 29 | #define PROJDEFS_H 30 | 31 | /* 32 | * Defines the prototype to which task functions must conform. Defined in this 33 | * file to ensure the type is known before portable.h is included. 34 | */ 35 | typedef void (*TaskFunction_t)( void * ); 36 | 37 | /* Converts a time in milliseconds to a time in ticks. This macro can be 38 | overridden by a macro of the same name defined in FreeRTOSConfig.h in case the 39 | definition here is not suitable for your application. */ 40 | #ifndef pdMS_TO_TICKS 41 | #define pdMS_TO_TICKS( xTimeInMs ) ( ( TickType_t ) ( ( ( TickType_t ) ( xTimeInMs ) * ( TickType_t ) configTICK_RATE_HZ ) / ( TickType_t ) 1000 ) ) 42 | #endif 43 | 44 | #define pdFALSE ( ( BaseType_t ) 0 ) 45 | #define pdTRUE ( ( BaseType_t ) 1 ) 46 | 47 | #define pdPASS ( pdTRUE ) 48 | #define pdFAIL ( pdFALSE ) 49 | #define errQUEUE_EMPTY ( ( BaseType_t ) 0 ) 50 | #define errQUEUE_FULL ( ( BaseType_t ) 0 ) 51 | 52 | /* FreeRTOS error definitions. */ 53 | #define errCOULD_NOT_ALLOCATE_REQUIRED_MEMORY ( -1 ) 54 | #define errQUEUE_BLOCKED ( -4 ) 55 | #define errQUEUE_YIELD ( -5 ) 56 | 57 | /* Macros used for basic data corruption checks. */ 58 | #ifndef configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES 59 | #define configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES 0 60 | #endif 61 | 62 | #if( configUSE_16_BIT_TICKS == 1 ) 63 | #define pdINTEGRITY_CHECK_VALUE 0x5a5a 64 | #else 65 | #define pdINTEGRITY_CHECK_VALUE 0x5a5a5a5aUL 66 | #endif 67 | 68 | /* The following errno values are used by FreeRTOS+ components, not FreeRTOS 69 | itself. */ 70 | #define pdFREERTOS_ERRNO_NONE 0 /* No errors */ 71 | #define pdFREERTOS_ERRNO_ENOENT 2 /* No such file or directory */ 72 | #define pdFREERTOS_ERRNO_EINTR 4 /* Interrupted system call */ 73 | #define pdFREERTOS_ERRNO_EIO 5 /* I/O error */ 74 | #define pdFREERTOS_ERRNO_ENXIO 6 /* No such device or address */ 75 | #define pdFREERTOS_ERRNO_EBADF 9 /* Bad file number */ 76 | #define pdFREERTOS_ERRNO_EAGAIN 11 /* No more processes */ 77 | #define pdFREERTOS_ERRNO_EWOULDBLOCK 11 /* Operation would block */ 78 | #define pdFREERTOS_ERRNO_ENOMEM 12 /* Not enough memory */ 79 | #define pdFREERTOS_ERRNO_EACCES 13 /* Permission denied */ 80 | #define pdFREERTOS_ERRNO_EFAULT 14 /* Bad address */ 81 | #define pdFREERTOS_ERRNO_EBUSY 16 /* Mount device busy */ 82 | #define pdFREERTOS_ERRNO_EEXIST 17 /* File exists */ 83 | #define pdFREERTOS_ERRNO_EXDEV 18 /* Cross-device link */ 84 | #define pdFREERTOS_ERRNO_ENODEV 19 /* No such device */ 85 | #define pdFREERTOS_ERRNO_ENOTDIR 20 /* Not a directory */ 86 | #define pdFREERTOS_ERRNO_EISDIR 21 /* Is a directory */ 87 | #define pdFREERTOS_ERRNO_EINVAL 22 /* Invalid argument */ 88 | #define pdFREERTOS_ERRNO_ENOSPC 28 /* No space left on device */ 89 | #define pdFREERTOS_ERRNO_ESPIPE 29 /* Illegal seek */ 90 | #define pdFREERTOS_ERRNO_EROFS 30 /* Read only file system */ 91 | #define pdFREERTOS_ERRNO_EUNATCH 42 /* Protocol driver not attached */ 92 | #define pdFREERTOS_ERRNO_EBADE 50 /* Invalid exchange */ 93 | #define pdFREERTOS_ERRNO_EFTYPE 79 /* Inappropriate file type or format */ 94 | #define pdFREERTOS_ERRNO_ENMFILE 89 /* No more files */ 95 | #define pdFREERTOS_ERRNO_ENOTEMPTY 90 /* Directory not empty */ 96 | #define pdFREERTOS_ERRNO_ENAMETOOLONG 91 /* File or path name too long */ 97 | #define pdFREERTOS_ERRNO_EOPNOTSUPP 95 /* Operation not supported on transport endpoint */ 98 | #define pdFREERTOS_ERRNO_ENOBUFS 105 /* No buffer space available */ 99 | #define pdFREERTOS_ERRNO_ENOPROTOOPT 109 /* Protocol not available */ 100 | #define pdFREERTOS_ERRNO_EADDRINUSE 112 /* Address already in use */ 101 | #define pdFREERTOS_ERRNO_ETIMEDOUT 116 /* Connection timed out */ 102 | #define pdFREERTOS_ERRNO_EINPROGRESS 119 /* Connection already in progress */ 103 | #define pdFREERTOS_ERRNO_EALREADY 120 /* Socket already connected */ 104 | #define pdFREERTOS_ERRNO_EADDRNOTAVAIL 125 /* Address not available */ 105 | #define pdFREERTOS_ERRNO_EISCONN 127 /* Socket is already connected */ 106 | #define pdFREERTOS_ERRNO_ENOTCONN 128 /* Socket is not connected */ 107 | #define pdFREERTOS_ERRNO_ENOMEDIUM 135 /* No medium inserted */ 108 | #define pdFREERTOS_ERRNO_EILSEQ 138 /* An invalid UTF-16 sequence was encountered. */ 109 | #define pdFREERTOS_ERRNO_ECANCELED 140 /* Operation canceled. */ 110 | 111 | /* The following endian values are used by FreeRTOS+ components, not FreeRTOS 112 | itself. */ 113 | #define pdFREERTOS_LITTLE_ENDIAN 0 114 | #define pdFREERTOS_BIG_ENDIAN 1 115 | 116 | /* Re-defining endian values for generic naming. */ 117 | #define pdLITTLE_ENDIAN pdFREERTOS_LITTLE_ENDIAN 118 | #define pdBIG_ENDIAN pdFREERTOS_BIG_ENDIAN 119 | 120 | 121 | #endif /* PROJDEFS_H */ 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/stack_macros.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef STACK_MACROS_H 29 | #define STACK_MACROS_H 30 | 31 | /* 32 | * Call the stack overflow hook function if the stack of the task being swapped 33 | * out is currently overflowed, or looks like it might have overflowed in the 34 | * past. 35 | * 36 | * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check 37 | * the current stack state only - comparing the current top of stack value to 38 | * the stack limit. Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1 39 | * will also cause the last few stack bytes to be checked to ensure the value 40 | * to which the bytes were set when the task was created have not been 41 | * overwritten. Note this second test does not guarantee that an overflowed 42 | * stack will always be recognised. 43 | */ 44 | 45 | /*-----------------------------------------------------------*/ 46 | 47 | #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) ) 48 | 49 | /* Only the current stack state is to be checked. */ 50 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 51 | { \ 52 | /* Is the currently saved stack pointer within the stack limit? */ \ 53 | if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack ) \ 54 | { \ 55 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 56 | } \ 57 | } 58 | 59 | #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */ 60 | /*-----------------------------------------------------------*/ 61 | 62 | #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) ) 63 | 64 | /* Only the current stack state is to be checked. */ 65 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 66 | { \ 67 | \ 68 | /* Is the currently saved stack pointer within the stack limit? */ \ 69 | if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack ) \ 70 | { \ 71 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 72 | } \ 73 | } 74 | 75 | #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */ 76 | /*-----------------------------------------------------------*/ 77 | 78 | #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) ) 79 | 80 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 81 | { \ 82 | const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \ 83 | const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \ 84 | \ 85 | if( ( pulStack[ 0 ] != ulCheckValue ) || \ 86 | ( pulStack[ 1 ] != ulCheckValue ) || \ 87 | ( pulStack[ 2 ] != ulCheckValue ) || \ 88 | ( pulStack[ 3 ] != ulCheckValue ) ) \ 89 | { \ 90 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 91 | } \ 92 | } 93 | 94 | #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */ 95 | /*-----------------------------------------------------------*/ 96 | 97 | #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) ) 98 | 99 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 100 | { \ 101 | int8_t *pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \ 102 | static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 103 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 104 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 105 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 106 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \ 107 | \ 108 | \ 109 | pcEndOfStack -= sizeof( ucExpectedStackBytes ); \ 110 | \ 111 | /* Has the extremity of the task stack ever been written over? */ \ 112 | if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \ 113 | { \ 114 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 115 | } \ 116 | } 117 | 118 | #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */ 119 | /*-----------------------------------------------------------*/ 120 | 121 | /* Remove stack overflow macro if not being used. */ 122 | #ifndef taskCHECK_FOR_STACK_OVERFLOW 123 | #define taskCHECK_FOR_STACK_OVERFLOW() 124 | #endif 125 | 126 | 127 | 128 | #endif /* STACK_MACROS_H */ 129 | 130 | -------------------------------------------------------------------------------- /src/networkwrapper.c: -------------------------------------------------------------------------------- 1 | /** 2 | * @file networkwrapper.c 3 | * @author Atakan S. 4 | * @date 01/01/2019 5 | * @version 1.0 6 | * @brief Network wrapper for PAHO MQTT project based on ESP8266. 7 | * 8 | * @copyright Copyright (c) 2018 Atakan SARIOGLU ~ www.atakansarioglu.com 9 | * 10 | * Permission is hereby granted, free of charge, to any person obtaining a 11 | * copy of this software and associated documentation files (the "Software"), 12 | * to deal in the Software without restriction, including without limitation 13 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, 14 | * and/or sell copies of the Software, and to permit persons to whom the 15 | * Software is furnished to do so, subject to the following conditions: 16 | * 17 | * The above copyright notice and this permission notice shall be included in 18 | * all copies or substantial portions of the Software. 19 | * 20 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 23 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 24 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 | * DEALINGS IN THE SOFTWARE. 27 | */ 28 | 29 | // Includes. 30 | #include "networkwrapper.h" 31 | #include "ESP8266Client.h" 32 | #include 33 | 34 | // Variables. 35 | static char network_host[32] = "iot.eclipse.org";///< HostName i.e. "test.mosquitto.org" 36 | static unsigned short int network_port = 1883;///< Remote port number. 37 | static unsigned short int network_keepalive = 20;///< Default keepalive time in seconds. 38 | static char network_ssl = false;///< SSL is disabled by default. 39 | static int network_send_state = 0;///< Internal state of send. 40 | static int network_recv_state = 0;///< Internal state of recv. 41 | 42 | // Global time provider. 43 | extern long unsigned int network_gettime_ms(void);///< Returns 32bit ms time value. 44 | 45 | void network_init(void){ } 46 | 47 | void network_close(void){ } 48 | 49 | int network_connect(const char * host, const unsigned short int port, const unsigned short int keepalive, const char ssl){ 50 | // Get connection info. 51 | strcpy(network_host, host); 52 | network_port = port; 53 | network_keepalive = keepalive; 54 | network_ssl = ssl; 55 | 56 | // Reset the internal states. 57 | network_send_state = 0; 58 | network_recv_state = 0; 59 | 60 | // Success. 61 | return 0; 62 | } 63 | 64 | int network_send(unsigned char *address, unsigned int bytes){ 65 | // State Machine. 66 | ESP82_Result_t espResult = ESP82_SUCCESS; 67 | switch(network_send_state) { 68 | case 0: 69 | // Init ESP8266 driver. 70 | ESP82_Init(115200, false, network_gettime_ms); 71 | 72 | // To the next state. 73 | network_send_state++; 74 | 75 | break; 76 | case 1: 77 | // Connect to wifi (restore to default first). 78 | #include "wifi_credentials.h"// Has the below 2 definitions only. 79 | espResult = ESP82_ConnectWifi(true, WIFI_AP_SSID, WIFI_AP_PASS); 80 | if(espResult == ESP82_SUCCESS){ 81 | // To the next state. 82 | network_send_state++; 83 | } 84 | break; 85 | case 2: 86 | // Wait 1sec. 87 | espResult = ESP82_Delay(1000); 88 | if(espResult == ESP82_SUCCESS){ 89 | // To the next state. 90 | network_send_state++; 91 | } 92 | break; 93 | case 3: 94 | // Check the wifi connection status. 95 | espResult = ESP82_IsConnectedWifi(); 96 | if(espResult == ESP82_SUCCESS){ 97 | // To the next state. 98 | network_send_state++; 99 | } 100 | break; 101 | case 4: 102 | // Start TCP connection. 103 | espResult = ESP82_StartTCP(network_host, network_port, network_keepalive, network_ssl); 104 | if(espResult == ESP82_SUCCESS){ 105 | // To the next state. 106 | network_send_state++; 107 | } 108 | break; 109 | case 5: 110 | // Send the data. 111 | espResult = ESP82_Send(address, bytes); 112 | if(espResult == ESP82_SUCCESS){ 113 | // Return the actual number of bytes. Stay in this state unless error occurs. 114 | return bytes; 115 | } 116 | break; 117 | default: 118 | // Reset the state machine. 119 | network_send_state = 0; 120 | } 121 | 122 | // Fall-back on error. 123 | if(espResult == ESP82_ERROR){ 124 | if(network_send_state < 4){ 125 | // If error occured before wifi connection, start over. 126 | network_send_state = 0; 127 | }else{ 128 | // Check wifi connection and try to send again. 129 | network_send_state = 2; 130 | } 131 | 132 | // Error. 133 | return -1; 134 | } 135 | 136 | // In progress. 137 | return 0; 138 | } 139 | 140 | int network_recv(unsigned char *address, unsigned int maxbytes){ 141 | static char receiveBuffer[128]; 142 | static int receiveBufferBack = 0; 143 | static int receiveBufferFront = 0; 144 | int actualLength; 145 | 146 | // State Machine. 147 | ESP82_Result_t espResult; 148 | switch(network_recv_state) { 149 | case 0: 150 | espResult = ESP82_Receive(receiveBuffer, 128); 151 | if(espResult > 0){ 152 | // Set the buffer pointers. 153 | receiveBufferBack = espResult; 154 | receiveBufferFront = 0; 155 | 156 | // To the next state. 157 | network_recv_state++; 158 | } 159 | break; 160 | case 1: 161 | // Extract to the out buffer. 162 | if(receiveBufferFront < receiveBufferBack) { 163 | // Get actual length. 164 | actualLength = (receiveBufferBack - receiveBufferFront); 165 | if(actualLength > maxbytes){ 166 | actualLength = maxbytes; 167 | } 168 | 169 | // Extract the actual bytes. 170 | memcpy(address, &receiveBuffer[receiveBufferFront], actualLength); 171 | receiveBufferFront += actualLength; 172 | 173 | // Buffer is empty. 174 | if(receiveBufferBack == receiveBufferFront) { 175 | network_recv_state = 0; 176 | } 177 | 178 | // Return the count. 179 | return actualLength; 180 | } 181 | break; 182 | default: 183 | // Reset the state machine. 184 | network_recv_state = 0; 185 | } 186 | 187 | // Fall-back on error. 188 | if(espResult == ESP82_ERROR){ 189 | // Reset the state machine. 190 | network_recv_state = 0; 191 | 192 | // Error. 193 | return -1; 194 | } 195 | 196 | // In progress. 197 | return 0; 198 | } 199 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/StackMacros.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef STACK_MACROS_H 29 | #define STACK_MACROS_H 30 | 31 | #ifndef _MSC_VER /* Visual Studio doesn't support #warning. */ 32 | #warning The name of this file has changed to stack_macros.h. Please update your code accordingly. This source file (which has the original name) will be removed in future released. 33 | #endif 34 | 35 | /* 36 | * Call the stack overflow hook function if the stack of the task being swapped 37 | * out is currently overflowed, or looks like it might have overflowed in the 38 | * past. 39 | * 40 | * Setting configCHECK_FOR_STACK_OVERFLOW to 1 will cause the macro to check 41 | * the current stack state only - comparing the current top of stack value to 42 | * the stack limit. Setting configCHECK_FOR_STACK_OVERFLOW to greater than 1 43 | * will also cause the last few stack bytes to be checked to ensure the value 44 | * to which the bytes were set when the task was created have not been 45 | * overwritten. Note this second test does not guarantee that an overflowed 46 | * stack will always be recognised. 47 | */ 48 | 49 | /*-----------------------------------------------------------*/ 50 | 51 | #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH < 0 ) ) 52 | 53 | /* Only the current stack state is to be checked. */ 54 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 55 | { \ 56 | /* Is the currently saved stack pointer within the stack limit? */ \ 57 | if( pxCurrentTCB->pxTopOfStack <= pxCurrentTCB->pxStack ) \ 58 | { \ 59 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 60 | } \ 61 | } 62 | 63 | #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */ 64 | /*-----------------------------------------------------------*/ 65 | 66 | #if( ( configCHECK_FOR_STACK_OVERFLOW == 1 ) && ( portSTACK_GROWTH > 0 ) ) 67 | 68 | /* Only the current stack state is to be checked. */ 69 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 70 | { \ 71 | \ 72 | /* Is the currently saved stack pointer within the stack limit? */ \ 73 | if( pxCurrentTCB->pxTopOfStack >= pxCurrentTCB->pxEndOfStack ) \ 74 | { \ 75 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 76 | } \ 77 | } 78 | 79 | #endif /* configCHECK_FOR_STACK_OVERFLOW == 1 */ 80 | /*-----------------------------------------------------------*/ 81 | 82 | #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH < 0 ) ) 83 | 84 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 85 | { \ 86 | const uint32_t * const pulStack = ( uint32_t * ) pxCurrentTCB->pxStack; \ 87 | const uint32_t ulCheckValue = ( uint32_t ) 0xa5a5a5a5; \ 88 | \ 89 | if( ( pulStack[ 0 ] != ulCheckValue ) || \ 90 | ( pulStack[ 1 ] != ulCheckValue ) || \ 91 | ( pulStack[ 2 ] != ulCheckValue ) || \ 92 | ( pulStack[ 3 ] != ulCheckValue ) ) \ 93 | { \ 94 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 95 | } \ 96 | } 97 | 98 | #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */ 99 | /*-----------------------------------------------------------*/ 100 | 101 | #if( ( configCHECK_FOR_STACK_OVERFLOW > 1 ) && ( portSTACK_GROWTH > 0 ) ) 102 | 103 | #define taskCHECK_FOR_STACK_OVERFLOW() \ 104 | { \ 105 | int8_t *pcEndOfStack = ( int8_t * ) pxCurrentTCB->pxEndOfStack; \ 106 | static const uint8_t ucExpectedStackBytes[] = { tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 107 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 108 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 109 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, \ 110 | tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE, tskSTACK_FILL_BYTE }; \ 111 | \ 112 | \ 113 | pcEndOfStack -= sizeof( ucExpectedStackBytes ); \ 114 | \ 115 | /* Has the extremity of the task stack ever been written over? */ \ 116 | if( memcmp( ( void * ) pcEndOfStack, ( void * ) ucExpectedStackBytes, sizeof( ucExpectedStackBytes ) ) != 0 ) \ 117 | { \ 118 | vApplicationStackOverflowHook( ( TaskHandle_t ) pxCurrentTCB, pxCurrentTCB->pcTaskName ); \ 119 | } \ 120 | } 121 | 122 | #endif /* #if( configCHECK_FOR_STACK_OVERFLOW > 1 ) */ 123 | /*-----------------------------------------------------------*/ 124 | 125 | /* Remove stack overflow macro if not being used. */ 126 | #ifndef taskCHECK_FOR_STACK_OVERFLOW 127 | #define taskCHECK_FOR_STACK_OVERFLOW() 128 | #endif 129 | 130 | 131 | 132 | #endif /* STACK_MACROS_H */ 133 | 134 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/portable.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | /*----------------------------------------------------------- 29 | * Portable layer API. Each function must be defined for each port. 30 | *----------------------------------------------------------*/ 31 | 32 | #ifndef PORTABLE_H 33 | #define PORTABLE_H 34 | 35 | /* Each FreeRTOS port has a unique portmacro.h header file. Originally a 36 | pre-processor definition was used to ensure the pre-processor found the correct 37 | portmacro.h file for the port being used. That scheme was deprecated in favour 38 | of setting the compiler's include path such that it found the correct 39 | portmacro.h file - removing the need for the constant and allowing the 40 | portmacro.h file to be located anywhere in relation to the port being used. 41 | Purely for reasons of backward compatibility the old method is still valid, but 42 | to make it clear that new projects should not use it, support for the port 43 | specific constants has been moved into the deprecated_definitions.h header 44 | file. */ 45 | #include "deprecated_definitions.h" 46 | 47 | /* If portENTER_CRITICAL is not defined then including deprecated_definitions.h 48 | did not result in a portmacro.h header file being included - and it should be 49 | included here. In this case the path to the correct portmacro.h header file 50 | must be set in the compiler's include path. */ 51 | #ifndef portENTER_CRITICAL 52 | #include "portmacro.h" 53 | #endif 54 | 55 | #if portBYTE_ALIGNMENT == 32 56 | #define portBYTE_ALIGNMENT_MASK ( 0x001f ) 57 | #endif 58 | 59 | #if portBYTE_ALIGNMENT == 16 60 | #define portBYTE_ALIGNMENT_MASK ( 0x000f ) 61 | #endif 62 | 63 | #if portBYTE_ALIGNMENT == 8 64 | #define portBYTE_ALIGNMENT_MASK ( 0x0007 ) 65 | #endif 66 | 67 | #if portBYTE_ALIGNMENT == 4 68 | #define portBYTE_ALIGNMENT_MASK ( 0x0003 ) 69 | #endif 70 | 71 | #if portBYTE_ALIGNMENT == 2 72 | #define portBYTE_ALIGNMENT_MASK ( 0x0001 ) 73 | #endif 74 | 75 | #if portBYTE_ALIGNMENT == 1 76 | #define portBYTE_ALIGNMENT_MASK ( 0x0000 ) 77 | #endif 78 | 79 | #ifndef portBYTE_ALIGNMENT_MASK 80 | #error "Invalid portBYTE_ALIGNMENT definition" 81 | #endif 82 | 83 | #ifndef portNUM_CONFIGURABLE_REGIONS 84 | #define portNUM_CONFIGURABLE_REGIONS 1 85 | #endif 86 | 87 | #ifdef __cplusplus 88 | extern "C" { 89 | #endif 90 | 91 | #include "mpu_wrappers.h" 92 | 93 | /* 94 | * Setup the stack of a new task so it is ready to be placed under the 95 | * scheduler control. The registers have to be placed on the stack in 96 | * the order that the port expects to find them. 97 | * 98 | */ 99 | #if( portUSING_MPU_WRAPPERS == 1 ) 100 | StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters, BaseType_t xRunPrivileged ) PRIVILEGED_FUNCTION; 101 | #else 102 | StackType_t *pxPortInitialiseStack( StackType_t *pxTopOfStack, TaskFunction_t pxCode, void *pvParameters ) PRIVILEGED_FUNCTION; 103 | #endif 104 | 105 | /* Used by heap_5.c. */ 106 | typedef struct HeapRegion 107 | { 108 | uint8_t *pucStartAddress; 109 | size_t xSizeInBytes; 110 | } HeapRegion_t; 111 | 112 | /* 113 | * Used to define multiple heap regions for use by heap_5.c. This function 114 | * must be called before any calls to pvPortMalloc() - not creating a task, 115 | * queue, semaphore, mutex, software timer, event group, etc. will result in 116 | * pvPortMalloc being called. 117 | * 118 | * pxHeapRegions passes in an array of HeapRegion_t structures - each of which 119 | * defines a region of memory that can be used as the heap. The array is 120 | * terminated by a HeapRegions_t structure that has a size of 0. The region 121 | * with the lowest start address must appear first in the array. 122 | */ 123 | void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION; 124 | 125 | 126 | /* 127 | * Map to the memory management routines required for the port. 128 | */ 129 | void *pvPortMalloc( size_t xSize ) PRIVILEGED_FUNCTION; 130 | void vPortFree( void *pv ) PRIVILEGED_FUNCTION; 131 | void vPortInitialiseBlocks( void ) PRIVILEGED_FUNCTION; 132 | size_t xPortGetFreeHeapSize( void ) PRIVILEGED_FUNCTION; 133 | size_t xPortGetMinimumEverFreeHeapSize( void ) PRIVILEGED_FUNCTION; 134 | 135 | /* 136 | * Setup the hardware ready for the scheduler to take control. This generally 137 | * sets up a tick interrupt and sets timers for the correct tick frequency. 138 | */ 139 | BaseType_t xPortStartScheduler( void ) PRIVILEGED_FUNCTION; 140 | 141 | /* 142 | * Undo any hardware/ISR setup that was performed by xPortStartScheduler() so 143 | * the hardware is left in its original condition after the scheduler stops 144 | * executing. 145 | */ 146 | void vPortEndScheduler( void ) PRIVILEGED_FUNCTION; 147 | 148 | /* 149 | * The structures and methods of manipulating the MPU are contained within the 150 | * port layer. 151 | * 152 | * Fills the xMPUSettings structure with the memory region information 153 | * contained in xRegions. 154 | */ 155 | #if( portUSING_MPU_WRAPPERS == 1 ) 156 | struct xMEMORY_REGION; 157 | void vPortStoreTaskMPUSettings( xMPU_SETTINGS *xMPUSettings, const struct xMEMORY_REGION * const xRegions, StackType_t *pxBottomOfStack, uint32_t ulStackDepth ) PRIVILEGED_FUNCTION; 158 | #endif 159 | 160 | #ifdef __cplusplus 161 | } 162 | #endif 163 | 164 | #endif /* PORTABLE_H */ 165 | 166 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTSubscribeClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - MQTT v5 implementation 16 | *******************************************************************************/ 17 | 18 | #if defined(MQTTV5) 19 | #include "V5/MQTTV5Packet.h" 20 | #else 21 | #include "MQTTPacket.h" 22 | #endif 23 | 24 | #include "StackTrace.h" 25 | 26 | #include 27 | 28 | /** 29 | * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters 30 | * @param count the number of topic filter strings in topicFilters 31 | * @param topicFilters the array of topic filter strings to be used in the publish 32 | * @return the length of buffer needed to contain the serialized version of the packet 33 | */ 34 | #if defined(MQTTV5) 35 | int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[], MQTTProperties* properties) 36 | #else 37 | int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) 38 | #endif 39 | { 40 | int i; 41 | int len = 2; /* packetid */ 42 | 43 | for (i = 0; i < count; ++i) 44 | len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ 45 | #if defined(MQTTV5) 46 | if (properties) 47 | len += MQTTProperties_len(properties); 48 | #endif 49 | return len; 50 | } 51 | 52 | 53 | /** 54 | * Serializes the supplied subscribe data into the supplied buffer, ready for sending 55 | * @param buf the buffer into which the packet will be serialized 56 | * @param buflen the length in bytes of the supplied bufferr 57 | * @param dup integer - the MQTT dup flag 58 | * @param packetid integer - the MQTT packet identifier 59 | * @param count - number of members in the topicFilters and reqQos arrays 60 | * @param topicFilters - array of topic filter names 61 | * @param requestedQoSs - array of requested QoS 62 | * @return the length of the serialized data. <= 0 indicates error 63 | */ 64 | #if defined(MQTTV5) 65 | int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, 66 | MQTTString topicFilters[], int requestedQoSs[]) 67 | { 68 | /* need to pack requestedQoSs into subscribeOptions */ 69 | return MQTTV5Serialize_subscribe(buf, buflen, dup, packetid, NULL, count, topicFilters, requestedQoSs, NULL); 70 | } 71 | 72 | int MQTTV5Serialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 73 | MQTTProperties* properties, int count, MQTTString topicFilters[], int requestedQoSs[], struct subscribeOptions options[]) 74 | #else 75 | int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, 76 | MQTTString topicFilters[], int requestedQoSs[]) 77 | #endif 78 | { 79 | unsigned char *ptr = buf; 80 | MQTTHeader header = {0}; 81 | int rem_len = 0; 82 | int rc = 0; 83 | int i = 0; 84 | 85 | FUNC_ENTRY; 86 | #if defined(MQTTV5) 87 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters, properties)) > buflen) 88 | #else 89 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) 90 | #endif 91 | { 92 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 93 | goto exit; 94 | } 95 | 96 | header.byte = 0; 97 | header.bits.type = SUBSCRIBE; 98 | header.bits.dup = dup; 99 | header.bits.qos = 1; 100 | writeChar(&ptr, header.byte); /* write header */ 101 | 102 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 103 | 104 | writeInt(&ptr, packetid); 105 | 106 | #if defined(MQTTV5) 107 | if (properties && MQTTProperties_write(&ptr, properties) < 0) 108 | goto exit; 109 | #endif 110 | 111 | for (i = 0; i < count; ++i) 112 | { 113 | unsigned char opts = requestedQoSs[i]; 114 | #if defined(MQTTV5) 115 | if (options) 116 | { 117 | opts |= (options[i].noLocal << 2); /* 1 bit */ 118 | opts |= (options[i].retainAsPublished << 3); /* 1 bit */ 119 | opts |= (options[i].retainHandling << 4); /* 2 bits */ 120 | } 121 | #endif 122 | writeMQTTString(&ptr, topicFilters[i]); 123 | writeChar(&ptr, opts); 124 | } 125 | 126 | rc = ptr - buf; 127 | exit: 128 | FUNC_EXIT_RC(rc); 129 | return rc; 130 | } 131 | 132 | 133 | 134 | /** 135 | * Deserializes the supplied (wire) buffer into suback data 136 | * @param packetid returned integer - the MQTT packet identifier 137 | * @param maxcount - the maximum number of members allowed in the grantedQoSs array 138 | * @param count returned integer - number of members in the grantedQoSs array 139 | * @param grantedQoSs returned array of integers - the granted qualities of service 140 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 141 | * @param buflen the length in bytes of the data in the supplied buffer 142 | * @return error code. 1 is success, 0 is failure 143 | */ 144 | #if defined(MQTTV5) 145 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], 146 | unsigned char* buf, int buflen) 147 | { 148 | return MQTTV5Deserialize_suback(packetid, NULL, maxcount, count, grantedQoSs, buf, buflen); 149 | } 150 | 151 | int MQTTV5Deserialize_suback(unsigned short* packetid, MQTTProperties* properties, 152 | int maxcount, int* count, int* reasonCodes, unsigned char* buf, int buflen) 153 | { 154 | return MQTTV5Deserialize_subunsuback(SUBACK, packetid, properties, 155 | maxcount, count, reasonCodes, buf, buflen); 156 | } 157 | 158 | int MQTTV5Deserialize_subunsuback(int type, unsigned short* packetid, MQTTProperties* properties, 159 | int maxcount, int* count, int* reasonCodes, unsigned char* buf, int buflen) 160 | #else 161 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], 162 | unsigned char* buf, int buflen) 163 | #endif 164 | { 165 | MQTTHeader header = {0}; 166 | unsigned char* curdata = buf; 167 | unsigned char* enddata = NULL; 168 | int rc = 0; 169 | int mylen; 170 | 171 | FUNC_ENTRY; 172 | header.byte = readChar(&curdata); 173 | #if defined(MQTTV5) 174 | if (header.bits.type != type) 175 | #else 176 | if (header.bits.type != SUBACK) 177 | #endif 178 | goto exit; 179 | 180 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 181 | enddata = curdata + mylen; 182 | if (enddata - curdata < 2) 183 | goto exit; 184 | 185 | *packetid = readInt(&curdata); 186 | 187 | #if defined(MQTTV5) 188 | if (properties && !MQTTProperties_read(properties, &curdata, enddata)) 189 | goto exit; 190 | #endif 191 | if (maxcount > 0) 192 | { 193 | *count = 0; 194 | while (curdata < enddata) 195 | { 196 | if (*count > maxcount) 197 | { 198 | rc = -1; 199 | goto exit; 200 | } 201 | #if defined(MQTTV5) 202 | reasonCodes[(*count)++] 203 | #else 204 | grantedQoSs[(*count)++] 205 | #endif 206 | = readChar(&curdata); 207 | } 208 | } 209 | rc = 1; 210 | exit: 211 | FUNC_EXIT_RC(rc); 212 | return rc; 213 | } 214 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/deprecated_definitions.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef DEPRECATED_DEFINITIONS_H 29 | #define DEPRECATED_DEFINITIONS_H 30 | 31 | 32 | /* Each FreeRTOS port has a unique portmacro.h header file. Originally a 33 | pre-processor definition was used to ensure the pre-processor found the correct 34 | portmacro.h file for the port being used. That scheme was deprecated in favour 35 | of setting the compiler's include path such that it found the correct 36 | portmacro.h file - removing the need for the constant and allowing the 37 | portmacro.h file to be located anywhere in relation to the port being used. The 38 | definitions below remain in the code for backward compatibility only. New 39 | projects should not use them. */ 40 | 41 | #ifdef OPEN_WATCOM_INDUSTRIAL_PC_PORT 42 | #include "..\..\Source\portable\owatcom\16bitdos\pc\portmacro.h" 43 | typedef void ( __interrupt __far *pxISR )(); 44 | #endif 45 | 46 | #ifdef OPEN_WATCOM_FLASH_LITE_186_PORT 47 | #include "..\..\Source\portable\owatcom\16bitdos\flsh186\portmacro.h" 48 | typedef void ( __interrupt __far *pxISR )(); 49 | #endif 50 | 51 | #ifdef GCC_MEGA_AVR 52 | #include "../portable/GCC/ATMega323/portmacro.h" 53 | #endif 54 | 55 | #ifdef IAR_MEGA_AVR 56 | #include "../portable/IAR/ATMega323/portmacro.h" 57 | #endif 58 | 59 | #ifdef MPLAB_PIC24_PORT 60 | #include "../../Source/portable/MPLAB/PIC24_dsPIC/portmacro.h" 61 | #endif 62 | 63 | #ifdef MPLAB_DSPIC_PORT 64 | #include "../../Source/portable/MPLAB/PIC24_dsPIC/portmacro.h" 65 | #endif 66 | 67 | #ifdef MPLAB_PIC18F_PORT 68 | #include "../../Source/portable/MPLAB/PIC18F/portmacro.h" 69 | #endif 70 | 71 | #ifdef MPLAB_PIC32MX_PORT 72 | #include "../../Source/portable/MPLAB/PIC32MX/portmacro.h" 73 | #endif 74 | 75 | #ifdef _FEDPICC 76 | #include "libFreeRTOS/Include/portmacro.h" 77 | #endif 78 | 79 | #ifdef SDCC_CYGNAL 80 | #include "../../Source/portable/SDCC/Cygnal/portmacro.h" 81 | #endif 82 | 83 | #ifdef GCC_ARM7 84 | #include "../../Source/portable/GCC/ARM7_LPC2000/portmacro.h" 85 | #endif 86 | 87 | #ifdef GCC_ARM7_ECLIPSE 88 | #include "portmacro.h" 89 | #endif 90 | 91 | #ifdef ROWLEY_LPC23xx 92 | #include "../../Source/portable/GCC/ARM7_LPC23xx/portmacro.h" 93 | #endif 94 | 95 | #ifdef IAR_MSP430 96 | #include "..\..\Source\portable\IAR\MSP430\portmacro.h" 97 | #endif 98 | 99 | #ifdef GCC_MSP430 100 | #include "../../Source/portable/GCC/MSP430F449/portmacro.h" 101 | #endif 102 | 103 | #ifdef ROWLEY_MSP430 104 | #include "../../Source/portable/Rowley/MSP430F449/portmacro.h" 105 | #endif 106 | 107 | #ifdef ARM7_LPC21xx_KEIL_RVDS 108 | #include "..\..\Source\portable\RVDS\ARM7_LPC21xx\portmacro.h" 109 | #endif 110 | 111 | #ifdef SAM7_GCC 112 | #include "../../Source/portable/GCC/ARM7_AT91SAM7S/portmacro.h" 113 | #endif 114 | 115 | #ifdef SAM7_IAR 116 | #include "..\..\Source\portable\IAR\AtmelSAM7S64\portmacro.h" 117 | #endif 118 | 119 | #ifdef SAM9XE_IAR 120 | #include "..\..\Source\portable\IAR\AtmelSAM9XE\portmacro.h" 121 | #endif 122 | 123 | #ifdef LPC2000_IAR 124 | #include "..\..\Source\portable\IAR\LPC2000\portmacro.h" 125 | #endif 126 | 127 | #ifdef STR71X_IAR 128 | #include "..\..\Source\portable\IAR\STR71x\portmacro.h" 129 | #endif 130 | 131 | #ifdef STR75X_IAR 132 | #include "..\..\Source\portable\IAR\STR75x\portmacro.h" 133 | #endif 134 | 135 | #ifdef STR75X_GCC 136 | #include "..\..\Source\portable\GCC\STR75x\portmacro.h" 137 | #endif 138 | 139 | #ifdef STR91X_IAR 140 | #include "..\..\Source\portable\IAR\STR91x\portmacro.h" 141 | #endif 142 | 143 | #ifdef GCC_H8S 144 | #include "../../Source/portable/GCC/H8S2329/portmacro.h" 145 | #endif 146 | 147 | #ifdef GCC_AT91FR40008 148 | #include "../../Source/portable/GCC/ARM7_AT91FR40008/portmacro.h" 149 | #endif 150 | 151 | #ifdef RVDS_ARMCM3_LM3S102 152 | #include "../../Source/portable/RVDS/ARM_CM3/portmacro.h" 153 | #endif 154 | 155 | #ifdef GCC_ARMCM3_LM3S102 156 | #include "../../Source/portable/GCC/ARM_CM3/portmacro.h" 157 | #endif 158 | 159 | #ifdef GCC_ARMCM3 160 | #include "../../Source/portable/GCC/ARM_CM3/portmacro.h" 161 | #endif 162 | 163 | #ifdef IAR_ARM_CM3 164 | #include "../../Source/portable/IAR/ARM_CM3/portmacro.h" 165 | #endif 166 | 167 | #ifdef IAR_ARMCM3_LM 168 | #include "../../Source/portable/IAR/ARM_CM3/portmacro.h" 169 | #endif 170 | 171 | #ifdef HCS12_CODE_WARRIOR 172 | #include "../../Source/portable/CodeWarrior/HCS12/portmacro.h" 173 | #endif 174 | 175 | #ifdef MICROBLAZE_GCC 176 | #include "../../Source/portable/GCC/MicroBlaze/portmacro.h" 177 | #endif 178 | 179 | #ifdef TERN_EE 180 | #include "..\..\Source\portable\Paradigm\Tern_EE\small\portmacro.h" 181 | #endif 182 | 183 | #ifdef GCC_HCS12 184 | #include "../../Source/portable/GCC/HCS12/portmacro.h" 185 | #endif 186 | 187 | #ifdef GCC_MCF5235 188 | #include "../../Source/portable/GCC/MCF5235/portmacro.h" 189 | #endif 190 | 191 | #ifdef COLDFIRE_V2_GCC 192 | #include "../../../Source/portable/GCC/ColdFire_V2/portmacro.h" 193 | #endif 194 | 195 | #ifdef COLDFIRE_V2_CODEWARRIOR 196 | #include "../../Source/portable/CodeWarrior/ColdFire_V2/portmacro.h" 197 | #endif 198 | 199 | #ifdef GCC_PPC405 200 | #include "../../Source/portable/GCC/PPC405_Xilinx/portmacro.h" 201 | #endif 202 | 203 | #ifdef GCC_PPC440 204 | #include "../../Source/portable/GCC/PPC440_Xilinx/portmacro.h" 205 | #endif 206 | 207 | #ifdef _16FX_SOFTUNE 208 | #include "..\..\Source\portable\Softune\MB96340\portmacro.h" 209 | #endif 210 | 211 | #ifdef BCC_INDUSTRIAL_PC_PORT 212 | /* A short file name has to be used in place of the normal 213 | FreeRTOSConfig.h when using the Borland compiler. */ 214 | #include "frconfig.h" 215 | #include "..\portable\BCC\16BitDOS\PC\prtmacro.h" 216 | typedef void ( __interrupt __far *pxISR )(); 217 | #endif 218 | 219 | #ifdef BCC_FLASH_LITE_186_PORT 220 | /* A short file name has to be used in place of the normal 221 | FreeRTOSConfig.h when using the Borland compiler. */ 222 | #include "frconfig.h" 223 | #include "..\portable\BCC\16BitDOS\flsh186\prtmacro.h" 224 | typedef void ( __interrupt __far *pxISR )(); 225 | #endif 226 | 227 | #ifdef __GNUC__ 228 | #ifdef __AVR32_AVR32A__ 229 | #include "portmacro.h" 230 | #endif 231 | #endif 232 | 233 | #ifdef __ICCAVR32__ 234 | #ifdef __CORE__ 235 | #if __CORE__ == __AVR32A__ 236 | #include "portmacro.h" 237 | #endif 238 | #endif 239 | #endif 240 | 241 | #ifdef __91467D 242 | #include "portmacro.h" 243 | #endif 244 | 245 | #ifdef __96340 246 | #include "portmacro.h" 247 | #endif 248 | 249 | 250 | #ifdef __IAR_V850ES_Fx3__ 251 | #include "../../Source/portable/IAR/V850ES/portmacro.h" 252 | #endif 253 | 254 | #ifdef __IAR_V850ES_Jx3__ 255 | #include "../../Source/portable/IAR/V850ES/portmacro.h" 256 | #endif 257 | 258 | #ifdef __IAR_V850ES_Jx3_L__ 259 | #include "../../Source/portable/IAR/V850ES/portmacro.h" 260 | #endif 261 | 262 | #ifdef __IAR_V850ES_Jx2__ 263 | #include "../../Source/portable/IAR/V850ES/portmacro.h" 264 | #endif 265 | 266 | #ifdef __IAR_V850ES_Hx2__ 267 | #include "../../Source/portable/IAR/V850ES/portmacro.h" 268 | #endif 269 | 270 | #ifdef __IAR_78K0R_Kx3__ 271 | #include "../../Source/portable/IAR/78K0R/portmacro.h" 272 | #endif 273 | 274 | #ifdef __IAR_78K0R_Kx3L__ 275 | #include "../../Source/portable/IAR/78K0R/portmacro.h" 276 | #endif 277 | 278 | #endif /* DEPRECATED_DEFINITIONS_H */ 279 | 280 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/list.c: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | 29 | #include 30 | #include "FreeRTOS.h" 31 | #include "list.h" 32 | 33 | /*----------------------------------------------------------- 34 | * PUBLIC LIST API documented in list.h 35 | *----------------------------------------------------------*/ 36 | 37 | void vListInitialise( List_t * const pxList ) 38 | { 39 | /* The list structure contains a list item which is used to mark the 40 | end of the list. To initialise the list the list end is inserted 41 | as the only list entry. */ 42 | pxList->pxIndex = ( ListItem_t * ) &( pxList->xListEnd ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */ 43 | 44 | /* The list end value is the highest possible value in the list to 45 | ensure it remains at the end of the list. */ 46 | pxList->xListEnd.xItemValue = portMAX_DELAY; 47 | 48 | /* The list end next and previous pointers point to itself so we know 49 | when the list is empty. */ 50 | pxList->xListEnd.pxNext = ( ListItem_t * ) &( pxList->xListEnd ); /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */ 51 | pxList->xListEnd.pxPrevious = ( ListItem_t * ) &( pxList->xListEnd );/*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. */ 52 | 53 | pxList->uxNumberOfItems = ( UBaseType_t ) 0U; 54 | 55 | /* Write known values into the list if 56 | configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */ 57 | listSET_LIST_INTEGRITY_CHECK_1_VALUE( pxList ); 58 | listSET_LIST_INTEGRITY_CHECK_2_VALUE( pxList ); 59 | } 60 | /*-----------------------------------------------------------*/ 61 | 62 | void vListInitialiseItem( ListItem_t * const pxItem ) 63 | { 64 | /* Make sure the list item is not recorded as being on a list. */ 65 | pxItem->pxContainer = NULL; 66 | 67 | /* Write known values into the list item if 68 | configUSE_LIST_DATA_INTEGRITY_CHECK_BYTES is set to 1. */ 69 | listSET_FIRST_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem ); 70 | listSET_SECOND_LIST_ITEM_INTEGRITY_CHECK_VALUE( pxItem ); 71 | } 72 | /*-----------------------------------------------------------*/ 73 | 74 | void vListInsertEnd( List_t * const pxList, ListItem_t * const pxNewListItem ) 75 | { 76 | ListItem_t * const pxIndex = pxList->pxIndex; 77 | 78 | /* Only effective when configASSERT() is also defined, these tests may catch 79 | the list data structures being overwritten in memory. They will not catch 80 | data errors caused by incorrect configuration or use of FreeRTOS. */ 81 | listTEST_LIST_INTEGRITY( pxList ); 82 | listTEST_LIST_ITEM_INTEGRITY( pxNewListItem ); 83 | 84 | /* Insert a new list item into pxList, but rather than sort the list, 85 | makes the new list item the last item to be removed by a call to 86 | listGET_OWNER_OF_NEXT_ENTRY(). */ 87 | pxNewListItem->pxNext = pxIndex; 88 | pxNewListItem->pxPrevious = pxIndex->pxPrevious; 89 | 90 | /* Only used during decision coverage testing. */ 91 | mtCOVERAGE_TEST_DELAY(); 92 | 93 | pxIndex->pxPrevious->pxNext = pxNewListItem; 94 | pxIndex->pxPrevious = pxNewListItem; 95 | 96 | /* Remember which list the item is in. */ 97 | pxNewListItem->pxContainer = pxList; 98 | 99 | ( pxList->uxNumberOfItems )++; 100 | } 101 | /*-----------------------------------------------------------*/ 102 | 103 | void vListInsert( List_t * const pxList, ListItem_t * const pxNewListItem ) 104 | { 105 | ListItem_t *pxIterator; 106 | const TickType_t xValueOfInsertion = pxNewListItem->xItemValue; 107 | 108 | /* Only effective when configASSERT() is also defined, these tests may catch 109 | the list data structures being overwritten in memory. They will not catch 110 | data errors caused by incorrect configuration or use of FreeRTOS. */ 111 | listTEST_LIST_INTEGRITY( pxList ); 112 | listTEST_LIST_ITEM_INTEGRITY( pxNewListItem ); 113 | 114 | /* Insert the new list item into the list, sorted in xItemValue order. 115 | 116 | If the list already contains a list item with the same item value then the 117 | new list item should be placed after it. This ensures that TCBs which are 118 | stored in ready lists (all of which have the same xItemValue value) get a 119 | share of the CPU. However, if the xItemValue is the same as the back marker 120 | the iteration loop below will not end. Therefore the value is checked 121 | first, and the algorithm slightly modified if necessary. */ 122 | if( xValueOfInsertion == portMAX_DELAY ) 123 | { 124 | pxIterator = pxList->xListEnd.pxPrevious; 125 | } 126 | else 127 | { 128 | /* *** NOTE *********************************************************** 129 | If you find your application is crashing here then likely causes are 130 | listed below. In addition see https://www.freertos.org/FAQHelp.html for 131 | more tips, and ensure configASSERT() is defined! 132 | https://www.freertos.org/a00110.html#configASSERT 133 | 134 | 1) Stack overflow - 135 | see https://www.freertos.org/Stacks-and-stack-overflow-checking.html 136 | 2) Incorrect interrupt priority assignment, especially on Cortex-M 137 | parts where numerically high priority values denote low actual 138 | interrupt priorities, which can seem counter intuitive. See 139 | https://www.freertos.org/RTOS-Cortex-M3-M4.html and the definition 140 | of configMAX_SYSCALL_INTERRUPT_PRIORITY on 141 | https://www.freertos.org/a00110.html 142 | 3) Calling an API function from within a critical section or when 143 | the scheduler is suspended, or calling an API function that does 144 | not end in "FromISR" from an interrupt. 145 | 4) Using a queue or semaphore before it has been initialised or 146 | before the scheduler has been started (are interrupts firing 147 | before vTaskStartScheduler() has been called?). 148 | **********************************************************************/ 149 | 150 | for( pxIterator = ( ListItem_t * ) &( pxList->xListEnd ); pxIterator->pxNext->xItemValue <= xValueOfInsertion; pxIterator = pxIterator->pxNext ) /*lint !e826 !e740 !e9087 The mini list structure is used as the list end to save RAM. This is checked and valid. *//*lint !e440 The iterator moves to a different value, not xValueOfInsertion. */ 151 | { 152 | /* There is nothing to do here, just iterating to the wanted 153 | insertion position. */ 154 | } 155 | } 156 | 157 | pxNewListItem->pxNext = pxIterator->pxNext; 158 | pxNewListItem->pxNext->pxPrevious = pxNewListItem; 159 | pxNewListItem->pxPrevious = pxIterator; 160 | pxIterator->pxNext = pxNewListItem; 161 | 162 | /* Remember which list the item is in. This allows fast removal of the 163 | item later. */ 164 | pxNewListItem->pxContainer = pxList; 165 | 166 | ( pxList->uxNumberOfItems )++; 167 | } 168 | /*-----------------------------------------------------------*/ 169 | 170 | UBaseType_t uxListRemove( ListItem_t * const pxItemToRemove ) 171 | { 172 | /* The list item knows which list it is in. Obtain the list from the list 173 | item. */ 174 | List_t * const pxList = pxItemToRemove->pxContainer; 175 | 176 | pxItemToRemove->pxNext->pxPrevious = pxItemToRemove->pxPrevious; 177 | pxItemToRemove->pxPrevious->pxNext = pxItemToRemove->pxNext; 178 | 179 | /* Only used during decision coverage testing. */ 180 | mtCOVERAGE_TEST_DELAY(); 181 | 182 | /* Make sure the index is left pointing to a valid item. */ 183 | if( pxList->pxIndex == pxItemToRemove ) 184 | { 185 | pxList->pxIndex = pxItemToRemove->pxPrevious; 186 | } 187 | else 188 | { 189 | mtCOVERAGE_TEST_MARKER(); 190 | } 191 | 192 | pxItemToRemove->pxContainer = NULL; 193 | ( pxList->uxNumberOfItems )--; 194 | 195 | return pxList->uxNumberOfItems; 196 | } 197 | /*-----------------------------------------------------------*/ 198 | 199 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/portable/GCC/ARM_CM3/portmacro.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | 29 | #ifndef PORTMACRO_H 30 | #define PORTMACRO_H 31 | 32 | #ifdef __cplusplus 33 | extern "C" { 34 | #endif 35 | 36 | /*----------------------------------------------------------- 37 | * Port specific definitions. 38 | * 39 | * The settings in this file configure FreeRTOS correctly for the 40 | * given hardware and compiler. 41 | * 42 | * These settings should not be altered. 43 | *----------------------------------------------------------- 44 | */ 45 | 46 | /* Type definitions. */ 47 | #define portCHAR char 48 | #define portFLOAT float 49 | #define portDOUBLE double 50 | #define portLONG long 51 | #define portSHORT short 52 | #define portSTACK_TYPE uint32_t 53 | #define portBASE_TYPE long 54 | 55 | typedef portSTACK_TYPE StackType_t; 56 | typedef long BaseType_t; 57 | typedef unsigned long UBaseType_t; 58 | 59 | #if( configUSE_16_BIT_TICKS == 1 ) 60 | typedef uint16_t TickType_t; 61 | #define portMAX_DELAY ( TickType_t ) 0xffff 62 | #else 63 | typedef uint32_t TickType_t; 64 | #define portMAX_DELAY ( TickType_t ) 0xffffffffUL 65 | 66 | /* 32-bit tick type on a 32-bit architecture, so reads of the tick count do 67 | not need to be guarded with a critical section. */ 68 | #define portTICK_TYPE_IS_ATOMIC 1 69 | #endif 70 | /*-----------------------------------------------------------*/ 71 | 72 | /* Architecture specifics. */ 73 | #define portSTACK_GROWTH ( -1 ) 74 | #define portTICK_PERIOD_MS ( ( TickType_t ) 1000 / configTICK_RATE_HZ ) 75 | #define portBYTE_ALIGNMENT 8 76 | /*-----------------------------------------------------------*/ 77 | 78 | /* Scheduler utilities. */ 79 | #define portYIELD() \ 80 | { \ 81 | /* Set a PendSV to request a context switch. */ \ 82 | portNVIC_INT_CTRL_REG = portNVIC_PENDSVSET_BIT; \ 83 | \ 84 | /* Barriers are normally not required but do ensure the code is completely \ 85 | within the specified behaviour for the architecture. */ \ 86 | __asm volatile( "dsb" ::: "memory" ); \ 87 | __asm volatile( "isb" ); \ 88 | } 89 | 90 | #define portNVIC_INT_CTRL_REG ( * ( ( volatile uint32_t * ) 0xe000ed04 ) ) 91 | #define portNVIC_PENDSVSET_BIT ( 1UL << 28UL ) 92 | #define portEND_SWITCHING_ISR( xSwitchRequired ) if( xSwitchRequired != pdFALSE ) portYIELD() 93 | #define portYIELD_FROM_ISR( x ) portEND_SWITCHING_ISR( x ) 94 | /*-----------------------------------------------------------*/ 95 | 96 | /* Critical section management. */ 97 | extern void vPortEnterCritical( void ); 98 | extern void vPortExitCritical( void ); 99 | #define portSET_INTERRUPT_MASK_FROM_ISR() ulPortRaiseBASEPRI() 100 | #define portCLEAR_INTERRUPT_MASK_FROM_ISR(x) vPortSetBASEPRI(x) 101 | #define portDISABLE_INTERRUPTS() vPortRaiseBASEPRI() 102 | #define portENABLE_INTERRUPTS() vPortSetBASEPRI(0) 103 | #define portENTER_CRITICAL() vPortEnterCritical() 104 | #define portEXIT_CRITICAL() vPortExitCritical() 105 | 106 | /*-----------------------------------------------------------*/ 107 | 108 | /* Task function macros as described on the FreeRTOS.org WEB site. These are 109 | not necessary for to use this port. They are defined so the common demo files 110 | (which build with all the ports) will build. */ 111 | #define portTASK_FUNCTION_PROTO( vFunction, pvParameters ) void vFunction( void *pvParameters ) 112 | #define portTASK_FUNCTION( vFunction, pvParameters ) void vFunction( void *pvParameters ) 113 | /*-----------------------------------------------------------*/ 114 | 115 | /* Tickless idle/low power functionality. */ 116 | #ifndef portSUPPRESS_TICKS_AND_SLEEP 117 | extern void vPortSuppressTicksAndSleep( TickType_t xExpectedIdleTime ); 118 | #define portSUPPRESS_TICKS_AND_SLEEP( xExpectedIdleTime ) vPortSuppressTicksAndSleep( xExpectedIdleTime ) 119 | #endif 120 | /*-----------------------------------------------------------*/ 121 | 122 | /* Architecture specific optimisations. */ 123 | #ifndef configUSE_PORT_OPTIMISED_TASK_SELECTION 124 | #define configUSE_PORT_OPTIMISED_TASK_SELECTION 1 125 | #endif 126 | 127 | #if configUSE_PORT_OPTIMISED_TASK_SELECTION == 1 128 | 129 | /* Generic helper function. */ 130 | __attribute__( ( always_inline ) ) static inline uint8_t ucPortCountLeadingZeros( uint32_t ulBitmap ) 131 | { 132 | uint8_t ucReturn; 133 | 134 | __asm volatile ( "clz %0, %1" : "=r" ( ucReturn ) : "r" ( ulBitmap ) : "memory" ); 135 | return ucReturn; 136 | } 137 | 138 | /* Check the configuration. */ 139 | #if( configMAX_PRIORITIES > 32 ) 140 | #error configUSE_PORT_OPTIMISED_TASK_SELECTION can only be set to 1 when configMAX_PRIORITIES is less than or equal to 32. It is very rare that a system requires more than 10 to 15 difference priorities as tasks that share a priority will time slice. 141 | #endif 142 | 143 | /* Store/clear the ready priorities in a bit map. */ 144 | #define portRECORD_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) |= ( 1UL << ( uxPriority ) ) 145 | #define portRESET_READY_PRIORITY( uxPriority, uxReadyPriorities ) ( uxReadyPriorities ) &= ~( 1UL << ( uxPriority ) ) 146 | 147 | /*-----------------------------------------------------------*/ 148 | 149 | #define portGET_HIGHEST_PRIORITY( uxTopPriority, uxReadyPriorities ) uxTopPriority = ( 31UL - ( uint32_t ) ucPortCountLeadingZeros( ( uxReadyPriorities ) ) ) 150 | 151 | #endif /* configUSE_PORT_OPTIMISED_TASK_SELECTION */ 152 | 153 | /*-----------------------------------------------------------*/ 154 | 155 | #ifdef configASSERT 156 | void vPortValidateInterruptPriority( void ); 157 | #define portASSERT_IF_INTERRUPT_PRIORITY_INVALID() vPortValidateInterruptPriority() 158 | #endif 159 | 160 | /* portNOP() is not required by this port. */ 161 | #define portNOP() 162 | 163 | #define portINLINE __inline 164 | 165 | #ifndef portFORCE_INLINE 166 | #define portFORCE_INLINE inline __attribute__(( always_inline)) 167 | #endif 168 | 169 | portFORCE_INLINE static BaseType_t xPortIsInsideInterrupt( void ) 170 | { 171 | uint32_t ulCurrentInterrupt; 172 | BaseType_t xReturn; 173 | 174 | /* Obtain the number of the currently executing interrupt. */ 175 | __asm volatile( "mrs %0, ipsr" : "=r"( ulCurrentInterrupt ) :: "memory" ); 176 | 177 | if( ulCurrentInterrupt == 0 ) 178 | { 179 | xReturn = pdFALSE; 180 | } 181 | else 182 | { 183 | xReturn = pdTRUE; 184 | } 185 | 186 | return xReturn; 187 | } 188 | 189 | /*-----------------------------------------------------------*/ 190 | 191 | portFORCE_INLINE static void vPortRaiseBASEPRI( void ) 192 | { 193 | uint32_t ulNewBASEPRI; 194 | 195 | __asm volatile 196 | ( 197 | " mov %0, %1 \n" \ 198 | " msr basepri, %0 \n" \ 199 | " isb \n" \ 200 | " dsb \n" \ 201 | :"=r" (ulNewBASEPRI) : "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory" 202 | ); 203 | } 204 | 205 | /*-----------------------------------------------------------*/ 206 | 207 | portFORCE_INLINE static uint32_t ulPortRaiseBASEPRI( void ) 208 | { 209 | uint32_t ulOriginalBASEPRI, ulNewBASEPRI; 210 | 211 | __asm volatile 212 | ( 213 | " mrs %0, basepri \n" \ 214 | " mov %1, %2 \n" \ 215 | " msr basepri, %1 \n" \ 216 | " isb \n" \ 217 | " dsb \n" \ 218 | :"=r" (ulOriginalBASEPRI), "=r" (ulNewBASEPRI) : "i" ( configMAX_SYSCALL_INTERRUPT_PRIORITY ) : "memory" 219 | ); 220 | 221 | /* This return will not be reached but is necessary to prevent compiler 222 | warnings. */ 223 | return ulOriginalBASEPRI; 224 | } 225 | /*-----------------------------------------------------------*/ 226 | 227 | portFORCE_INLINE static void vPortSetBASEPRI( uint32_t ulNewMaskValue ) 228 | { 229 | __asm volatile 230 | ( 231 | " msr basepri, %0 " :: "r" ( ulNewMaskValue ) : "memory" 232 | ); 233 | } 234 | /*-----------------------------------------------------------*/ 235 | 236 | 237 | #ifdef __cplusplus 238 | } 239 | #endif 240 | 241 | #endif /* PORTMACRO_H */ 242 | 243 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/mpu_wrappers.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | #ifndef MPU_WRAPPERS_H 29 | #define MPU_WRAPPERS_H 30 | 31 | /* This file redefines API functions to be called through a wrapper macro, but 32 | only for ports that are using the MPU. */ 33 | #ifdef portUSING_MPU_WRAPPERS 34 | 35 | /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE will be defined when this file is 36 | included from queue.c or task.c to prevent it from having an effect within 37 | those files. */ 38 | #ifndef MPU_WRAPPERS_INCLUDED_FROM_API_FILE 39 | 40 | /* 41 | * Map standard (non MPU) API functions to equivalents that start 42 | * "MPU_". This will cause the application code to call the MPU_ 43 | * version, which wraps the non-MPU version with privilege promoting 44 | * then demoting code, so the kernel code always runs will full 45 | * privileges. 46 | */ 47 | 48 | /* Map standard tasks.h API functions to the MPU equivalents. */ 49 | #define xTaskCreate MPU_xTaskCreate 50 | #define xTaskCreateStatic MPU_xTaskCreateStatic 51 | #define xTaskCreateRestricted MPU_xTaskCreateRestricted 52 | #define vTaskAllocateMPURegions MPU_vTaskAllocateMPURegions 53 | #define vTaskDelete MPU_vTaskDelete 54 | #define vTaskDelay MPU_vTaskDelay 55 | #define vTaskDelayUntil MPU_vTaskDelayUntil 56 | #define xTaskAbortDelay MPU_xTaskAbortDelay 57 | #define uxTaskPriorityGet MPU_uxTaskPriorityGet 58 | #define eTaskGetState MPU_eTaskGetState 59 | #define vTaskGetInfo MPU_vTaskGetInfo 60 | #define vTaskPrioritySet MPU_vTaskPrioritySet 61 | #define vTaskSuspend MPU_vTaskSuspend 62 | #define vTaskResume MPU_vTaskResume 63 | #define vTaskSuspendAll MPU_vTaskSuspendAll 64 | #define xTaskResumeAll MPU_xTaskResumeAll 65 | #define xTaskGetTickCount MPU_xTaskGetTickCount 66 | #define uxTaskGetNumberOfTasks MPU_uxTaskGetNumberOfTasks 67 | #define pcTaskGetName MPU_pcTaskGetName 68 | #define xTaskGetHandle MPU_xTaskGetHandle 69 | #define uxTaskGetStackHighWaterMark MPU_uxTaskGetStackHighWaterMark 70 | #define vTaskSetApplicationTaskTag MPU_vTaskSetApplicationTaskTag 71 | #define xTaskGetApplicationTaskTag MPU_xTaskGetApplicationTaskTag 72 | #define vTaskSetThreadLocalStoragePointer MPU_vTaskSetThreadLocalStoragePointer 73 | #define pvTaskGetThreadLocalStoragePointer MPU_pvTaskGetThreadLocalStoragePointer 74 | #define xTaskCallApplicationTaskHook MPU_xTaskCallApplicationTaskHook 75 | #define xTaskGetIdleTaskHandle MPU_xTaskGetIdleTaskHandle 76 | #define uxTaskGetSystemState MPU_uxTaskGetSystemState 77 | #define vTaskList MPU_vTaskList 78 | #define vTaskGetRunTimeStats MPU_vTaskGetRunTimeStats 79 | #define xTaskGenericNotify MPU_xTaskGenericNotify 80 | #define xTaskNotifyWait MPU_xTaskNotifyWait 81 | #define ulTaskNotifyTake MPU_ulTaskNotifyTake 82 | #define xTaskNotifyStateClear MPU_xTaskNotifyStateClear 83 | 84 | #define xTaskGetCurrentTaskHandle MPU_xTaskGetCurrentTaskHandle 85 | #define vTaskSetTimeOutState MPU_vTaskSetTimeOutState 86 | #define xTaskCheckForTimeOut MPU_xTaskCheckForTimeOut 87 | #define xTaskGetSchedulerState MPU_xTaskGetSchedulerState 88 | 89 | /* Map standard queue.h API functions to the MPU equivalents. */ 90 | #define xQueueGenericSend MPU_xQueueGenericSend 91 | #define xQueueReceive MPU_xQueueReceive 92 | #define xQueuePeek MPU_xQueuePeek 93 | #define xQueueSemaphoreTake MPU_xQueueSemaphoreTake 94 | #define uxQueueMessagesWaiting MPU_uxQueueMessagesWaiting 95 | #define uxQueueSpacesAvailable MPU_uxQueueSpacesAvailable 96 | #define vQueueDelete MPU_vQueueDelete 97 | #define xQueueCreateMutex MPU_xQueueCreateMutex 98 | #define xQueueCreateMutexStatic MPU_xQueueCreateMutexStatic 99 | #define xQueueCreateCountingSemaphore MPU_xQueueCreateCountingSemaphore 100 | #define xQueueCreateCountingSemaphoreStatic MPU_xQueueCreateCountingSemaphoreStatic 101 | #define xQueueGetMutexHolder MPU_xQueueGetMutexHolder 102 | #define xQueueTakeMutexRecursive MPU_xQueueTakeMutexRecursive 103 | #define xQueueGiveMutexRecursive MPU_xQueueGiveMutexRecursive 104 | #define xQueueGenericCreate MPU_xQueueGenericCreate 105 | #define xQueueGenericCreateStatic MPU_xQueueGenericCreateStatic 106 | #define xQueueCreateSet MPU_xQueueCreateSet 107 | #define xQueueAddToSet MPU_xQueueAddToSet 108 | #define xQueueRemoveFromSet MPU_xQueueRemoveFromSet 109 | #define xQueueSelectFromSet MPU_xQueueSelectFromSet 110 | #define xQueueGenericReset MPU_xQueueGenericReset 111 | 112 | #if( configQUEUE_REGISTRY_SIZE > 0 ) 113 | #define vQueueAddToRegistry MPU_vQueueAddToRegistry 114 | #define vQueueUnregisterQueue MPU_vQueueUnregisterQueue 115 | #define pcQueueGetName MPU_pcQueueGetName 116 | #endif 117 | 118 | /* Map standard timer.h API functions to the MPU equivalents. */ 119 | #define xTimerCreate MPU_xTimerCreate 120 | #define xTimerCreateStatic MPU_xTimerCreateStatic 121 | #define pvTimerGetTimerID MPU_pvTimerGetTimerID 122 | #define vTimerSetTimerID MPU_vTimerSetTimerID 123 | #define xTimerIsTimerActive MPU_xTimerIsTimerActive 124 | #define xTimerGetTimerDaemonTaskHandle MPU_xTimerGetTimerDaemonTaskHandle 125 | #define xTimerPendFunctionCall MPU_xTimerPendFunctionCall 126 | #define pcTimerGetName MPU_pcTimerGetName 127 | #define xTimerGetPeriod MPU_xTimerGetPeriod 128 | #define xTimerGetExpiryTime MPU_xTimerGetExpiryTime 129 | #define xTimerGenericCommand MPU_xTimerGenericCommand 130 | 131 | /* Map standard event_group.h API functions to the MPU equivalents. */ 132 | #define xEventGroupCreate MPU_xEventGroupCreate 133 | #define xEventGroupCreateStatic MPU_xEventGroupCreateStatic 134 | #define xEventGroupWaitBits MPU_xEventGroupWaitBits 135 | #define xEventGroupClearBits MPU_xEventGroupClearBits 136 | #define xEventGroupSetBits MPU_xEventGroupSetBits 137 | #define xEventGroupSync MPU_xEventGroupSync 138 | #define vEventGroupDelete MPU_vEventGroupDelete 139 | 140 | /* Map standard message/stream_buffer.h API functions to the MPU 141 | equivalents. */ 142 | #define xStreamBufferSend MPU_xStreamBufferSend 143 | #define xStreamBufferSendFromISR MPU_xStreamBufferSendFromISR 144 | #define xStreamBufferReceive MPU_xStreamBufferReceive 145 | #define xStreamBufferNextMessageLengthBytes MPU_xStreamBufferNextMessageLengthBytes 146 | #define xStreamBufferReceiveFromISR MPU_xStreamBufferReceiveFromISR 147 | #define vStreamBufferDelete MPU_vStreamBufferDelete 148 | #define xStreamBufferIsFull MPU_xStreamBufferIsFull 149 | #define xStreamBufferIsEmpty MPU_xStreamBufferIsEmpty 150 | #define xStreamBufferReset MPU_xStreamBufferReset 151 | #define xStreamBufferSpacesAvailable MPU_xStreamBufferSpacesAvailable 152 | #define xStreamBufferBytesAvailable MPU_xStreamBufferBytesAvailable 153 | #define xStreamBufferSetTriggerLevel MPU_xStreamBufferSetTriggerLevel 154 | #define xStreamBufferGenericCreate MPU_xStreamBufferGenericCreate 155 | #define xStreamBufferGenericCreateStatic MPU_xStreamBufferGenericCreateStatic 156 | 157 | 158 | /* Remove the privileged function macro, but keep the PRIVILEGED_DATA 159 | macro so applications can place data in privileged access sections 160 | (useful when using statically allocated objects). */ 161 | #define PRIVILEGED_FUNCTION 162 | #define PRIVILEGED_DATA __attribute__((section("privileged_data"))) 163 | 164 | #else /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE */ 165 | 166 | /* Ensure API functions go in the privileged execution section. */ 167 | #define PRIVILEGED_FUNCTION __attribute__((section("privileged_functions"))) 168 | #define PRIVILEGED_DATA __attribute__((section("privileged_data"))) 169 | 170 | #endif /* MPU_WRAPPERS_INCLUDED_FROM_API_FILE */ 171 | 172 | #else /* portUSING_MPU_WRAPPERS */ 173 | 174 | #define PRIVILEGED_FUNCTION 175 | #define PRIVILEGED_DATA 176 | #define portUSING_MPU_WRAPPERS 0 177 | 178 | #endif /* portUSING_MPU_WRAPPERS */ 179 | 180 | 181 | #endif /* MPU_WRAPPERS_H */ 182 | 183 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTFormat.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #include "StackTrace.h" 18 | #include "MQTTPacket.h" 19 | 20 | #include 21 | 22 | 23 | const char* MQTTPacket_names[] = 24 | { 25 | "RESERVED", "CONNECT", "CONNACK", "PUBLISH", "PUBACK", "PUBREC", "PUBREL", 26 | "PUBCOMP", "SUBSCRIBE", "SUBACK", "UNSUBSCRIBE", "UNSUBACK", 27 | "PINGREQ", "PINGRESP", "DISCONNECT" 28 | }; 29 | 30 | 31 | const char* MQTTPacket_getName(unsigned short packetid) 32 | { 33 | return MQTTPacket_names[packetid]; 34 | } 35 | 36 | 37 | int MQTTStringFormat_connect(char* strbuf, int strbuflen, MQTTPacket_connectData* data) 38 | { 39 | int strindex = 0; 40 | 41 | strindex = snprintf(strbuf, strbuflen, 42 | "CONNECT MQTT version %d, client id %.*s, clean session %d, keep alive %d", 43 | (int)data->MQTTVersion, data->clientID.lenstring.len, data->clientID.lenstring.data, 44 | (int)data->cleansession, data->keepAliveInterval); 45 | if (data->willFlag) 46 | strindex += snprintf(&strbuf[strindex], strbuflen - strindex, 47 | ", will QoS %d, will retain %d, will topic %.*s, will message %.*s", 48 | data->will.qos, data->will.retained, 49 | data->will.topicName.lenstring.len, data->will.topicName.lenstring.data, 50 | data->will.message.lenstring.len, data->will.message.lenstring.data); 51 | if (data->username.lenstring.data && data->username.lenstring.len > 0) 52 | strindex += snprintf(&strbuf[strindex], strbuflen - strindex, 53 | ", user name %.*s", data->username.lenstring.len, data->username.lenstring.data); 54 | if (data->password.lenstring.data && data->password.lenstring.len > 0) 55 | strindex += snprintf(&strbuf[strindex], strbuflen - strindex, 56 | ", password %.*s", data->password.lenstring.len, data->password.lenstring.data); 57 | return strindex; 58 | } 59 | 60 | 61 | int MQTTStringFormat_connack(char* strbuf, int strbuflen, unsigned char connack_rc, unsigned char sessionPresent) 62 | { 63 | int strindex = snprintf(strbuf, strbuflen, "CONNACK session present %d, rc %d", sessionPresent, connack_rc); 64 | return strindex; 65 | } 66 | 67 | 68 | int MQTTStringFormat_publish(char* strbuf, int strbuflen, unsigned char dup, int qos, unsigned char retained, 69 | unsigned short packetid, MQTTString topicName, unsigned char* payload, int payloadlen) 70 | { 71 | int strindex = snprintf(strbuf, strbuflen, 72 | "PUBLISH dup %d, QoS %d, retained %d, packet id %d, topic %.*s, payload length %d, payload %.*s", 73 | dup, qos, retained, packetid, 74 | (topicName.lenstring.len < 20) ? topicName.lenstring.len : 20, topicName.lenstring.data, 75 | payloadlen, (payloadlen < 20) ? payloadlen : 20, payload); 76 | return strindex; 77 | } 78 | 79 | 80 | int MQTTStringFormat_ack(char* strbuf, int strbuflen, unsigned char packettype, unsigned char dup, unsigned short packetid) 81 | { 82 | int strindex = snprintf(strbuf, strbuflen, "%s, packet id %d", MQTTPacket_names[packettype], packetid); 83 | if (dup) 84 | strindex += snprintf(strbuf + strindex, strbuflen - strindex, ", dup %d", dup); 85 | return strindex; 86 | } 87 | 88 | 89 | int MQTTStringFormat_subscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, int count, 90 | MQTTString topicFilters[], int requestedQoSs[]) 91 | { 92 | return snprintf(strbuf, strbuflen, 93 | "SUBSCRIBE dup %d, packet id %d count %d topic %.*s qos %d", 94 | dup, packetid, count, 95 | topicFilters[0].lenstring.len, topicFilters[0].lenstring.data, 96 | requestedQoSs[0]); 97 | } 98 | 99 | 100 | int MQTTStringFormat_suback(char* strbuf, int strbuflen, unsigned short packetid, int count, int* grantedQoSs) 101 | { 102 | return snprintf(strbuf, strbuflen, 103 | "SUBACK packet id %d count %d granted qos %d", packetid, count, grantedQoSs[0]); 104 | } 105 | 106 | 107 | int MQTTStringFormat_unsubscribe(char* strbuf, int strbuflen, unsigned char dup, unsigned short packetid, 108 | int count, MQTTString topicFilters[]) 109 | { 110 | return snprintf(strbuf, strbuflen, 111 | "UNSUBSCRIBE dup %d, packet id %d count %d topic %.*s", 112 | dup, packetid, count, 113 | topicFilters[0].lenstring.len, topicFilters[0].lenstring.data); 114 | } 115 | 116 | 117 | #if defined(MQTT_CLIENT) 118 | char* MQTTFormat_toClientString(char* strbuf, int strbuflen, unsigned char* buf, int buflen) 119 | { 120 | int index = 0; 121 | int rem_length = 0; 122 | MQTTHeader header = {0}; 123 | int strindex = 0; 124 | 125 | header.byte = buf[index++]; 126 | index += MQTTPacket_decodeBuf(&buf[index], &rem_length); 127 | 128 | switch (header.bits.type) 129 | { 130 | 131 | case CONNACK: 132 | { 133 | unsigned char sessionPresent, connack_rc; 134 | if (MQTTDeserialize_connack(&sessionPresent, &connack_rc, buf, buflen) == 1) 135 | strindex = MQTTStringFormat_connack(strbuf, strbuflen, connack_rc, sessionPresent); 136 | } 137 | break; 138 | case PUBLISH: 139 | { 140 | unsigned char dup, retained, *payload; 141 | unsigned short packetid; 142 | int qos, payloadlen; 143 | MQTTString topicName = MQTTString_initializer; 144 | if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName, 145 | &payload, &payloadlen, buf, buflen) == 1) 146 | strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid, 147 | topicName, payload, payloadlen); 148 | } 149 | break; 150 | case PUBACK: 151 | case PUBREC: 152 | case PUBREL: 153 | case PUBCOMP: 154 | { 155 | unsigned char packettype, dup; 156 | unsigned short packetid; 157 | if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1) 158 | strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid); 159 | } 160 | break; 161 | case SUBACK: 162 | { 163 | unsigned short packetid; 164 | int maxcount = 1, count = 0; 165 | int grantedQoSs[1]; 166 | if (MQTTDeserialize_suback(&packetid, maxcount, &count, grantedQoSs, buf, buflen) == 1) 167 | strindex = MQTTStringFormat_suback(strbuf, strbuflen, packetid, count, grantedQoSs); 168 | } 169 | break; 170 | case UNSUBACK: 171 | { 172 | unsigned short packetid; 173 | if (MQTTDeserialize_unsuback(&packetid, buf, buflen) == 1) 174 | strindex = MQTTStringFormat_ack(strbuf, strbuflen, UNSUBACK, 0, packetid); 175 | } 176 | break; 177 | case PINGREQ: 178 | case PINGRESP: 179 | case DISCONNECT: 180 | strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]); 181 | break; 182 | } 183 | return strbuf; 184 | } 185 | #endif 186 | 187 | #if defined(MQTT_SERVER) 188 | char* MQTTFormat_toServerString(char* strbuf, int strbuflen, unsigned char* buf, int buflen) 189 | { 190 | int index = 0; 191 | int rem_length = 0; 192 | MQTTHeader header = {0}; 193 | int strindex = 0; 194 | 195 | header.byte = buf[index++]; 196 | index += MQTTPacket_decodeBuf(&buf[index], &rem_length); 197 | 198 | switch (header.bits.type) 199 | { 200 | case CONNECT: 201 | { 202 | MQTTPacket_connectData data; 203 | int rc; 204 | if ((rc = MQTTDeserialize_connect(&data, buf, buflen)) == 1) 205 | strindex = MQTTStringFormat_connect(strbuf, strbuflen, &data); 206 | } 207 | break; 208 | case PUBLISH: 209 | { 210 | unsigned char dup, retained, *payload; 211 | unsigned short packetid; 212 | int qos, payloadlen; 213 | MQTTString topicName = MQTTString_initializer; 214 | if (MQTTDeserialize_publish(&dup, &qos, &retained, &packetid, &topicName, 215 | &payload, &payloadlen, buf, buflen) == 1) 216 | strindex = MQTTStringFormat_publish(strbuf, strbuflen, dup, qos, retained, packetid, 217 | topicName, payload, payloadlen); 218 | } 219 | break; 220 | case PUBACK: 221 | case PUBREC: 222 | case PUBREL: 223 | case PUBCOMP: 224 | { 225 | unsigned char packettype, dup; 226 | unsigned short packetid; 227 | if (MQTTDeserialize_ack(&packettype, &dup, &packetid, buf, buflen) == 1) 228 | strindex = MQTTStringFormat_ack(strbuf, strbuflen, packettype, dup, packetid); 229 | } 230 | break; 231 | case SUBSCRIBE: 232 | { 233 | unsigned char dup; 234 | unsigned short packetid; 235 | int maxcount = 1, count = 0; 236 | MQTTString topicFilters[1]; 237 | int requestedQoSs[1]; 238 | if (MQTTDeserialize_subscribe(&dup, &packetid, maxcount, &count, 239 | topicFilters, requestedQoSs, buf, buflen) == 1) 240 | strindex = MQTTStringFormat_subscribe(strbuf, strbuflen, dup, packetid, count, topicFilters, requestedQoSs);; 241 | } 242 | break; 243 | case UNSUBSCRIBE: 244 | { 245 | unsigned char dup; 246 | unsigned short packetid; 247 | int maxcount = 1, count = 0; 248 | MQTTString topicFilters[1]; 249 | if (MQTTDeserialize_unsubscribe(&dup, &packetid, maxcount, &count, topicFilters, buf, buflen) == 1) 250 | strindex = MQTTStringFormat_unsubscribe(strbuf, strbuflen, dup, packetid, count, topicFilters); 251 | } 252 | break; 253 | case PINGREQ: 254 | case PINGRESP: 255 | case DISCONNECT: 256 | strindex = snprintf(strbuf, strbuflen, "%s", MQTTPacket_names[header.bits.type]); 257 | break; 258 | } 259 | strbuf[strbuflen] = '\0'; 260 | return strbuf; 261 | } 262 | #endif 263 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTConnectServer.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - add MQTT v5 support 16 | *******************************************************************************/ 17 | 18 | #include "StackTrace.h" 19 | #if defined(MQTTV5) 20 | #include "V5/MQTTV5Packet.h" 21 | #else 22 | #include "MQTTPacket.h" 23 | #endif 24 | #include 25 | 26 | #define min(a, b) ((a < b) ? a : b) 27 | 28 | 29 | /** 30 | * Validates MQTT protocol name and version combinations 31 | * @param protocol the MQTT protocol name as an MQTTString 32 | * @param version the MQTT protocol version number, as in the connect packet 33 | * @return correct MQTT combination? 1 is true, 0 is false 34 | */ 35 | int MQTTPacket_checkVersion(MQTTString* protocol, int version) 36 | { 37 | int rc = 0; 38 | 39 | if (version == 3 && memcmp(protocol->lenstring.data, "MQIsdp", 40 | min(6, protocol->lenstring.len)) == 0) 41 | rc = 1; 42 | else if (version == 4 && memcmp(protocol->lenstring.data, "MQTT", 43 | min(4, protocol->lenstring.len)) == 0) 44 | rc = 1; 45 | #if defined(MQTTV5) 46 | else if (version == 5 && memcmp(protocol->lenstring.data, "MQTT", 47 | min(4, protocol->lenstring.len)) == 0) 48 | rc = 1; 49 | #endif 50 | return rc; 51 | } 52 | 53 | 54 | 55 | #if defined(MQTTV5) 56 | int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len) 57 | { 58 | return MQTTV5Deserialize_connect(NULL, NULL, data, buf, len); 59 | } 60 | 61 | /** 62 | * Deserializes the supplied (wire) buffer into connect data structure 63 | * @param willProperties the V5 properties to be applied to the will message, if it exists 64 | * @param connectProperties the V5 properties for the connect packet 65 | * @param data the connect data structure to be filled out 66 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 67 | * @param len the length in bytes of the data in the supplied buffer 68 | * @return error code. 1 is success, 0 is failure 69 | */ 70 | int MQTTV5Deserialize_connect(MQTTProperties* willProperties, MQTTProperties* connectProperties, 71 | MQTTPacket_connectData* data, unsigned char* buf, int len) 72 | #else 73 | /** 74 | * Deserializes the supplied (wire) buffer into connect data structure 75 | * @param data the connect data structure to be filled out 76 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 77 | * @param len the length in bytes of the data in the supplied buffer 78 | * @return error code. 1 is success, 0 is failure 79 | */ 80 | int MQTTDeserialize_connect(MQTTPacket_connectData* data, unsigned char* buf, int len) 81 | #endif 82 | { 83 | MQTTHeader header = {0}; 84 | MQTTConnectFlags flags = {0}; 85 | unsigned char* curdata = buf; 86 | unsigned char* enddata = &buf[len]; 87 | int rc = 0; 88 | MQTTString Protocol; 89 | int mylen = 0; 90 | 91 | FUNC_ENTRY; 92 | header.byte = readChar(&curdata); 93 | if (header.bits.type != CONNECT) 94 | goto exit; 95 | 96 | curdata += MQTTPacket_decodeBuf(curdata, &mylen); /* read remaining length */ 97 | 98 | if (!readMQTTLenString(&Protocol, &curdata, enddata) || 99 | enddata - curdata < 0) /* do we have enough data to read the protocol version byte? */ 100 | goto exit; 101 | 102 | data->MQTTVersion = (int)readChar(&curdata); /* Protocol version */ 103 | /* If we don't recognize the protocol version, we don't parse the connect packet on the 104 | * basis that we don't know what the format will be. 105 | */ 106 | if (MQTTPacket_checkVersion(&Protocol, data->MQTTVersion)) 107 | { 108 | flags.all = readChar(&curdata); 109 | data->cleansession = flags.bits.cleansession; 110 | data->keepAliveInterval = readInt(&curdata); 111 | #if defined(MQTTV5) 112 | if (data->MQTTVersion == 5) 113 | { 114 | if (!MQTTProperties_read(connectProperties, &curdata, enddata)) 115 | goto exit; 116 | } 117 | #endif 118 | if (!readMQTTLenString(&data->clientID, &curdata, enddata)) 119 | goto exit; 120 | data->willFlag = flags.bits.will; 121 | if (flags.bits.will) 122 | { 123 | #if defined(MQTTV5) 124 | if (data->MQTTVersion == 5) 125 | { 126 | if (!MQTTProperties_read(willProperties, &curdata, enddata)) 127 | goto exit; 128 | } 129 | #endif 130 | data->will.qos = flags.bits.willQoS; 131 | data->will.retained = flags.bits.willRetain; 132 | if (!readMQTTLenString(&data->will.topicName, &curdata, enddata) || 133 | !readMQTTLenString(&data->will.message, &curdata, enddata)) 134 | goto exit; 135 | } 136 | if (flags.bits.username) 137 | { 138 | if (enddata - curdata < 3 || !readMQTTLenString(&data->username, &curdata, enddata)) 139 | goto exit; /* username flag set, but no username supplied - invalid */ 140 | if (flags.bits.password && 141 | (enddata - curdata < 3 || !readMQTTLenString(&data->password, &curdata, enddata))) 142 | goto exit; /* password flag set, but no password supplied - invalid */ 143 | } 144 | else if (flags.bits.password) 145 | goto exit; /* password flag set without username - invalid */ 146 | rc = 1; 147 | } 148 | exit: 149 | FUNC_EXIT_RC(rc); 150 | return rc; 151 | } 152 | 153 | 154 | /** 155 | * Serializes the connack packet into the supplied buffer. 156 | * @param buf the buffer into which the packet will be serialized 157 | * @param buflen the length in bytes of the supplied buffer 158 | * @param connack_rc the integer connack return code to be used 159 | * @param sessionPresent the MQTT 3.1.1 sessionPresent flag 160 | * @param connackProperties MQTT v5 properties, if NULL, then MQTT 3.1.1 connack 161 | * @return serialized length, or error if 0 162 | */ 163 | #if defined(MQTTV5) 164 | int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent) 165 | { 166 | return MQTTV5Serialize_connack(buf, buflen, connack_rc, sessionPresent, NULL); 167 | } 168 | 169 | int MQTTV5Serialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent, 170 | MQTTProperties* connackProperties) 171 | #else 172 | int MQTTSerialize_connack(unsigned char* buf, int buflen, unsigned char connack_rc, unsigned char sessionPresent) 173 | #endif 174 | { 175 | MQTTHeader header = {0}; 176 | int rc = 0; 177 | unsigned char *ptr = buf; 178 | MQTTConnackFlags flags = {0}; 179 | int len = 0; 180 | 181 | FUNC_ENTRY; 182 | 183 | #if defined(MQTTV5) 184 | len = 2 + (connackProperties == NULL ? 0 : connackProperties->length); 185 | #else 186 | len = 2; 187 | #endif 188 | 189 | if (MQTTPacket_len(len) > buflen) 190 | { 191 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 192 | goto exit; 193 | } 194 | header.byte = 0; 195 | header.bits.type = CONNACK; 196 | writeChar(&ptr, header.byte); /* write header */ 197 | 198 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 199 | 200 | flags.all = 0; 201 | flags.bits.sessionpresent = sessionPresent; 202 | writeChar(&ptr, flags.all); 203 | writeChar(&ptr, connack_rc); 204 | 205 | #if defined(MQTTV5) 206 | if (connackProperties && MQTTProperties_write(&ptr, connackProperties) < 0) 207 | goto exit; 208 | #endif 209 | 210 | rc = ptr - buf; 211 | exit: 212 | FUNC_EXIT_RC(rc); 213 | return rc; 214 | } 215 | 216 | 217 | #if defined(MQTTV5) 218 | int MQTTV5Deserialize_zero(unsigned char packettype, MQTTProperties* properties, int* reasonCode, 219 | unsigned char* buf, int buflen) 220 | { 221 | MQTTHeader header = {0}; 222 | unsigned char* curdata = buf; 223 | unsigned char* enddata = NULL; 224 | int rc = 0; 225 | int mylen; 226 | 227 | FUNC_ENTRY; 228 | header.byte = readChar(&curdata); 229 | if (header.bits.type != packettype) 230 | goto exit; 231 | 232 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 233 | enddata = curdata + mylen; 234 | 235 | if (mylen > 0) 236 | { 237 | *reasonCode = readChar(&curdata); 238 | if (mylen > 1 && !MQTTProperties_read(properties, &curdata, enddata)) 239 | goto exit; 240 | } 241 | 242 | rc = 1; 243 | exit: 244 | FUNC_EXIT_RC(rc); 245 | return rc; 246 | } 247 | #endif 248 | 249 | /** 250 | * Deserializes the supplied (wire) buffer into connack data - return code 251 | * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) 252 | * @param connack_rc returned integer value of the connack return code 253 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 254 | * @param len the length in bytes of the data in the supplied buffer 255 | * @return error code. 1 is success, 0 is failure 256 | */ 257 | #if defined(MQTTV5) 258 | int MQTTV5Deserialize_disconnect(MQTTProperties* properties, int* reasonCode, 259 | unsigned char* buf, int buflen) 260 | { 261 | return MQTTV5Deserialize_zero(DISCONNECT, properties, reasonCode, buf, buflen); 262 | } 263 | 264 | int MQTTV5Deserialize_auth(MQTTProperties* properties, int* reasonCode, 265 | unsigned char* buf, int buflen) 266 | { 267 | return MQTTV5Deserialize_zero(AUTH, properties, reasonCode, buf, buflen); 268 | } 269 | #endif 270 | 271 | int MQTTDeserialize_disconnect(unsigned char* buf, int buflen) 272 | { 273 | unsigned char type = 0; 274 | unsigned char dup = 0; 275 | unsigned short packetid = 0; 276 | int rc = 0; 277 | 278 | FUNC_ENTRY; 279 | rc = MQTTDeserialize_ack(&type, &dup, &packetid, buf, buflen); 280 | if (type == DISCONNECT) 281 | rc = 1; 282 | FUNC_EXIT_RC(rc); 283 | return rc; 284 | } 285 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTSerializePublish.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144 16 | * Ian Craggs - MQTT v5 support 17 | *******************************************************************************/ 18 | 19 | #if defined(MQTTV5) 20 | #include "V5/MQTTV5Packet.h" 21 | #else 22 | #include "MQTTPacket.h" 23 | #endif 24 | 25 | #include "StackTrace.h" 26 | 27 | #include 28 | 29 | 30 | /** 31 | * Determines the length of the MQTT publish packet that would be produced using the supplied parameters 32 | * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0) 33 | * @param topicName the topic name to be used in the publish 34 | * @param payloadlen the length of the payload to be sent 35 | * @return the length of buffer needed to contain the serialized version of the packet 36 | */ 37 | #if defined(MQTTV5) 38 | int MQTTV5Serialize_publishLength(int qos, MQTTString topicName, int payloadlen, MQTTProperties* properties) 39 | #else 40 | int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen) 41 | #endif 42 | { 43 | int len = 0; 44 | 45 | len += 2 + MQTTstrlen(topicName) + payloadlen; 46 | if (qos > 0) 47 | len += 2; /* packetid */ 48 | #if defined(MQTTV5) 49 | if (properties) 50 | len += MQTTProperties_len(properties); 51 | #endif 52 | return len; 53 | } 54 | 55 | 56 | /** 57 | * Serializes the supplied publish data into the supplied buffer, ready for sending 58 | * @param buf the buffer into which the packet will be serialized 59 | * @param buflen the length in bytes of the supplied buffer 60 | * @param dup integer - the MQTT dup flag 61 | * @param qos integer - the MQTT QoS value 62 | * @param retained integer - the MQTT retained flag 63 | * @param packetid integer - the MQTT packet identifier 64 | * @param topicName MQTTString - the MQTT topic in the publish 65 | * @param payload byte buffer - the MQTT publish payload 66 | * @param payloadlen integer - the length of the MQTT payload 67 | * @return the length of the serialized data. <= 0 indicates error 68 | */ 69 | #if defined(MQTTV5) 70 | int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 71 | MQTTString topicName, unsigned char* payload, int payloadlen) 72 | { 73 | return MQTTV5Serialize_publish(buf, buflen, dup, qos, retained, packetid, topicName, NULL, payload, payloadlen); 74 | } 75 | 76 | int MQTTV5Serialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 77 | MQTTString topicName, MQTTProperties* properties, unsigned char* payload, int payloadlen) 78 | #else 79 | int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 80 | MQTTString topicName, unsigned char* payload, int payloadlen) 81 | #endif 82 | { 83 | unsigned char *ptr = buf; 84 | MQTTHeader header = {0}; 85 | int rem_len = 0; 86 | int rc = 0; 87 | 88 | FUNC_ENTRY; 89 | #if defined(MQTTV5) 90 | if (MQTTPacket_len(rem_len = MQTTV5Serialize_publishLength(qos, topicName, payloadlen, properties)) > buflen) 91 | #else 92 | if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen) 93 | #endif 94 | { 95 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 96 | goto exit; 97 | } 98 | 99 | header.bits.type = PUBLISH; 100 | header.bits.dup = dup; 101 | header.bits.qos = qos; 102 | header.bits.retain = retained; 103 | writeChar(&ptr, header.byte); /* write header */ 104 | 105 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 106 | 107 | writeMQTTString(&ptr, topicName); 108 | 109 | if (qos > 0) 110 | writeInt(&ptr, packetid); 111 | 112 | #if defined(MQTTV5) 113 | if (properties && MQTTProperties_write(&ptr, properties) < 0) 114 | goto exit; 115 | #endif 116 | 117 | memcpy(ptr, payload, payloadlen); 118 | ptr += payloadlen; 119 | 120 | rc = ptr - buf; 121 | 122 | exit: 123 | FUNC_EXIT_RC(rc); 124 | return rc; 125 | } 126 | 127 | 128 | 129 | /** 130 | * Serializes the ack packet into the supplied buffer. 131 | * @param buf the buffer into which the packet will be serialized 132 | * @param buflen the length in bytes of the supplied buffer 133 | * @param type the MQTT packet type 134 | * @param dup the MQTT dup flag 135 | * @param packetid the MQTT packet identifier 136 | * @return serialized length, or error if 0 137 | */ 138 | #if defined(MQTTV5) 139 | int MQTTV5Serialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid, 140 | int reasonCode, MQTTProperties* properties); 141 | 142 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) 143 | { 144 | return MQTTV5Serialize_ack(buf, buflen, packettype, dup, packetid, -1, NULL); 145 | } 146 | 147 | int MQTTV5Serialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid, 148 | int reasonCode, MQTTProperties* properties) 149 | #else 150 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) 151 | #endif 152 | { 153 | MQTTHeader header = {0}; 154 | int rc = 0; 155 | unsigned char *ptr = buf; 156 | int len = 2; 157 | 158 | FUNC_ENTRY; 159 | #if defined(MQTTV5) 160 | if (reasonCode >= 0) 161 | { 162 | len += 1; 163 | if (properties) 164 | len += MQTTProperties_len(properties); 165 | } 166 | #endif 167 | if (buflen < 4) 168 | { 169 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 170 | goto exit; 171 | } 172 | header.bits.type = packettype; 173 | header.bits.dup = dup; 174 | header.bits.qos = (packettype == PUBREL) ? 1 : 0; 175 | writeChar(&ptr, header.byte); /* write header */ 176 | 177 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 178 | writeInt(&ptr, packetid); 179 | 180 | #if defined(MQTTV5) 181 | if (reasonCode >= 0) 182 | { 183 | writeChar(&ptr, reasonCode); 184 | if (properties && MQTTProperties_write(&ptr, properties) < 0) 185 | goto exit; 186 | } 187 | #endif 188 | 189 | rc = ptr - buf; 190 | exit: 191 | FUNC_EXIT_RC(rc); 192 | return rc; 193 | } 194 | 195 | 196 | /** 197 | * Serializes a puback packet into the supplied buffer. 198 | * @param buf the buffer into which the packet will be serialized 199 | * @param buflen the length in bytes of the supplied buffer 200 | * @param packetid integer - the MQTT packet identifier 201 | * @return serialized length, or error if 0 202 | */ 203 | #if defined(MQTTV5) 204 | int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) 205 | { 206 | return MQTTV5Serialize_puback(buf, buflen, packetid, -1, NULL); 207 | } 208 | 209 | int MQTTV5Serialize_puback(unsigned char* buf, int buflen, unsigned short packetid, 210 | int reasonCode, MQTTProperties* properties) 211 | #else 212 | int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) 213 | #endif 214 | { 215 | #if defined(MQTTV5) 216 | return MQTTV5Serialize_ack(buf, buflen, PUBACK, 0, packetid, reasonCode, properties); 217 | #else 218 | return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid); 219 | #endif 220 | } 221 | 222 | /** 223 | * Serializes a pubrec packet into the supplied buffer. 224 | * @param buf the buffer into which the packet will be serialized 225 | * @param buflen the length in bytes of the supplied buffer 226 | * @param dup integer - the MQTT dup flag 227 | * @param packetid integer - the MQTT packet identifier 228 | * @return serialized length, or error if 0 229 | */ 230 | #if defined(MQTTV5) 231 | int MQTTSerialize_pubrec(unsigned char* buf, int buflen, unsigned short packetid) 232 | { 233 | return MQTTV5Serialize_pubrec(buf, buflen, packetid, -1, NULL); 234 | } 235 | 236 | int MQTTV5Serialize_pubrec(unsigned char* buf, int buflen, unsigned short packetid, 237 | int reasonCode, MQTTProperties* properties) 238 | #else 239 | int MQTTSerialize_pubrec(unsigned char* buf, int buflen, unsigned short packetid) 240 | #endif 241 | { 242 | #if defined(MQTTV5) 243 | return MQTTV5Serialize_ack(buf, buflen, PUBREC, 0, packetid, reasonCode, properties); 244 | #else 245 | return MQTTSerialize_ack(buf, buflen, PUBREC, 0, packetid); 246 | #endif 247 | } 248 | 249 | 250 | /** 251 | * Serializes a pubrel packet into the supplied buffer. 252 | * @param buf the buffer into which the packet will be serialized 253 | * @param buflen the length in bytes of the supplied buffer 254 | * @param dup integer - the MQTT dup flag 255 | * @param packetid integer - the MQTT packet identifier 256 | * @return serialized length, or error if 0 257 | */ 258 | #if defined(MQTTV5) 259 | int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid) 260 | { 261 | return MQTTV5Serialize_pubrel(buf, buflen, dup, packetid, -1, NULL); 262 | } 263 | 264 | int MQTTV5Serialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 265 | int reasonCode, MQTTProperties* properties) 266 | #else 267 | int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid) 268 | #endif 269 | { 270 | #if defined(MQTTV5) 271 | return MQTTV5Serialize_ack(buf, buflen, PUBREL, dup, packetid, reasonCode, properties); 272 | #else 273 | return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid); 274 | #endif 275 | } 276 | 277 | 278 | /** 279 | * Serializes a pubcomp packet into the supplied buffer. 280 | * @param buf the buffer into which the packet will be serialized 281 | * @param buflen the length in bytes of the supplied buffer 282 | * @param packetid integer - the MQTT packet identifier 283 | * @return serialized length, or error if 0 284 | */ 285 | #if defined(MQTTV5) 286 | int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid) 287 | { 288 | return MQTTV5Serialize_pubcomp(buf, buflen, packetid, -1, NULL); 289 | } 290 | 291 | int MQTTV5Serialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid, 292 | int reasonCode, MQTTProperties* properties) 293 | #else 294 | int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid) 295 | #endif 296 | { 297 | #if defined(MQTTV5) 298 | return MQTTV5Serialize_ack(buf, buflen, PUBCOMP, 0, packetid, reasonCode, properties); 299 | #else 300 | return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid); 301 | #endif 302 | } 303 | -------------------------------------------------------------------------------- /lib/MQTTPacket/MQTTConnectClient.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************* 2 | * Copyright (c) 2014, 2017 IBM Corp. 3 | * 4 | * All rights reserved. This program and the accompanying materials 5 | * are made available under the terms of the Eclipse Public License v1.0 6 | * and Eclipse Distribution License v1.0 which accompany this distribution. 7 | * 8 | * The Eclipse Public License is available at 9 | * http://www.eclipse.org/legal/epl-v10.html 10 | * and the Eclipse Distribution License is available at 11 | * http://www.eclipse.org/org/documents/edl-v10.php. 12 | * 13 | * Contributors: 14 | * Ian Craggs - initial API and implementation and/or initial documentation 15 | *******************************************************************************/ 16 | 17 | #if defined(MQTTV5) 18 | #include "V5/MQTTV5Packet.h" 19 | #else 20 | #include "MQTTPacket.h" 21 | #endif 22 | #include "StackTrace.h" 23 | 24 | #include 25 | 26 | /** 27 | * Determines the length of the MQTT connect packet that would be produced using the supplied connect options. 28 | * @param options the options to be used to build the connect packet 29 | * @return the length of buffer needed to contain the serialized version of the packet 30 | */ 31 | #if defined(MQTTV5) 32 | int MQTTSerialize_connectLength(MQTTPacket_connectData* options, MQTTProperties* connectProperties, 33 | MQTTProperties* willProperties) 34 | #else 35 | int MQTTSerialize_connectLength(MQTTPacket_connectData* options) 36 | #endif 37 | { 38 | int len = 0; 39 | 40 | FUNC_ENTRY; 41 | 42 | if (options->MQTTVersion == 3) 43 | len = 12; /* variable depending on MQTT or MQIsdp */ 44 | else if (options->MQTTVersion >= 4) 45 | len = 10; 46 | 47 | len += MQTTstrlen(options->clientID)+2; 48 | if (options->willFlag) 49 | len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2; 50 | if (options->username.cstring || options->username.lenstring.data) 51 | len += MQTTstrlen(options->username)+2; 52 | if (options->password.cstring || options->password.lenstring.data) 53 | len += MQTTstrlen(options->password)+2; 54 | #if defined(MQTTV5) 55 | if (options->MQTTVersion >= 5) 56 | { 57 | if (connectProperties) 58 | len += MQTTProperties_len(connectProperties); 59 | if (options->willFlag && willProperties) 60 | len += MQTTProperties_len(willProperties); 61 | } 62 | #endif 63 | 64 | FUNC_EXIT_RC(len); 65 | return len; 66 | } 67 | 68 | 69 | /** 70 | * Serializes the connect options into the buffer. 71 | * @param buf the buffer into which the packet will be serialized 72 | * @param len the length in bytes of the supplied buffer 73 | * @param options the options to be used to build the connect packet 74 | * @return serialized length, or error if 0 75 | */ 76 | #if defined(MQTTV5) 77 | int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) 78 | { 79 | return MQTTV5Serialize_connect(buf, buflen, options, NULL, NULL); 80 | } 81 | 82 | int MQTTV5Serialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options, 83 | MQTTProperties* connectProperties, MQTTProperties* willProperties) 84 | #else 85 | int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) 86 | #endif 87 | { 88 | unsigned char *ptr = buf; 89 | MQTTHeader header = {0}; 90 | MQTTConnectFlags flags = {0}; 91 | int len = 0; 92 | int rc = -1; 93 | 94 | FUNC_ENTRY; 95 | #if defined(MQTTV5) 96 | if (MQTTPacket_len(len = MQTTSerialize_connectLength(options, 97 | connectProperties, willProperties)) > buflen) 98 | #else 99 | if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) 100 | #endif 101 | { 102 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 103 | goto exit; 104 | } 105 | 106 | header.byte = 0; 107 | header.bits.type = CONNECT; 108 | writeChar(&ptr, header.byte); /* write header */ 109 | 110 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 111 | 112 | if (options->MQTTVersion == 5 || options->MQTTVersion == 4) 113 | writeCString(&ptr, "MQTT"); 114 | else if (options->MQTTVersion == 3) 115 | writeCString(&ptr, "MQIsdp"); 116 | else 117 | goto exit; 118 | writeChar(&ptr, (char)options->MQTTVersion); 119 | 120 | flags.all = 0; 121 | flags.bits.cleansession = options->cleansession; 122 | flags.bits.will = (options->willFlag) ? 1 : 0; 123 | if (flags.bits.will) 124 | { 125 | flags.bits.willQoS = options->will.qos; 126 | flags.bits.willRetain = options->will.retained; 127 | } 128 | 129 | if (options->username.cstring || options->username.lenstring.data) 130 | flags.bits.username = 1; 131 | if (options->password.cstring || options->password.lenstring.data) 132 | flags.bits.password = 1; 133 | 134 | writeChar(&ptr, flags.all); 135 | writeInt(&ptr, options->keepAliveInterval); 136 | #if defined(MQTTV5) 137 | if (options->MQTTVersion == 5) 138 | MQTTProperties_write(&ptr, connectProperties); 139 | #endif 140 | writeMQTTString(&ptr, options->clientID); 141 | if (options->willFlag) 142 | { 143 | #if defined(MQTTV5) 144 | /* write will properties */ 145 | if (options->MQTTVersion == 5 && willProperties) 146 | MQTTProperties_write(&ptr, willProperties); 147 | #endif 148 | writeMQTTString(&ptr, options->will.topicName); 149 | writeMQTTString(&ptr, options->will.message); 150 | } 151 | if (flags.bits.username) 152 | writeMQTTString(&ptr, options->username); 153 | if (flags.bits.password) 154 | writeMQTTString(&ptr, options->password); 155 | 156 | rc = ptr - buf; 157 | 158 | exit: FUNC_EXIT_RC(rc); 159 | return rc; 160 | } 161 | 162 | 163 | /** 164 | * Deserializes the supplied (wire) buffer into connack data - return code 165 | * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) 166 | * @param connack_rc returned integer value of the connack return code 167 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 168 | * @param len the length in bytes of the data in the supplied buffer 169 | * @return error code. 1 is success, 0 is failure 170 | */ 171 | #if defined(MQTTV5) 172 | int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) 173 | { 174 | return MQTTV5Deserialize_connack(NULL, sessionPresent, connack_rc, buf, buflen); 175 | } 176 | 177 | int MQTTV5Deserialize_connack(MQTTProperties* connackProperties, unsigned char* sessionPresent, unsigned char* connack_rc, 178 | unsigned char* buf, int buflen) 179 | #else 180 | int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) 181 | #endif 182 | { 183 | MQTTHeader header = {0}; 184 | unsigned char* curdata = buf; 185 | unsigned char* enddata = NULL; 186 | int rc = 0; 187 | int mylen; 188 | MQTTConnackFlags flags = {0}; 189 | 190 | FUNC_ENTRY; 191 | header.byte = readChar(&curdata); 192 | if (header.bits.type != CONNACK) 193 | goto exit; 194 | 195 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 196 | enddata = curdata + mylen; 197 | if (enddata - curdata < 2) 198 | goto exit; 199 | 200 | flags.all = readChar(&curdata); 201 | *sessionPresent = flags.bits.sessionpresent; 202 | *connack_rc = readChar(&curdata); 203 | 204 | #if defined(MQTTV5) 205 | if (connackProperties && !MQTTProperties_read(connackProperties, &curdata, enddata)) 206 | goto exit; 207 | #endif 208 | 209 | rc = 1; 210 | exit: 211 | FUNC_EXIT_RC(rc); 212 | return rc; 213 | } 214 | 215 | 216 | /** 217 | * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket 218 | * @param buf the buffer into which the packet will be serialized 219 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 220 | * @param packettype the message type 221 | * @return serialized length, or error if 0 222 | */ 223 | #if defined(MQTTV5) 224 | int MQTTV5Serialize_zero(unsigned char* buf, int buflen, unsigned char packettype, 225 | int reasonCode, MQTTProperties* properties); 226 | 227 | int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) 228 | { 229 | return MQTTV5Serialize_zero(buf, buflen, packettype, -1, NULL); 230 | } 231 | 232 | int MQTTV5Serialize_zero(unsigned char* buf, int buflen, unsigned char packettype, 233 | int reasonCode, MQTTProperties* properties) 234 | #else 235 | int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) 236 | #endif 237 | { 238 | MQTTHeader header = {0}; 239 | int rc = -1; 240 | unsigned char *ptr = buf; 241 | int len = 0; 242 | 243 | FUNC_ENTRY; 244 | #if defined(MQTTV5) 245 | if (reasonCode >= 0 && reasonCode <= 162) 246 | { 247 | len += 1; 248 | if (properties) 249 | len += MQTTProperties_len(properties); 250 | } 251 | #endif 252 | if (MQTTPacket_len(len) > buflen) 253 | { 254 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 255 | goto exit; 256 | } 257 | header.byte = 0; 258 | header.bits.type = packettype; 259 | writeChar(&ptr, header.byte); /* write header */ 260 | 261 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 262 | #if defined(MQTTV5) 263 | if (reasonCode >= 0 && reasonCode <= 162) 264 | { 265 | writeChar(&ptr, reasonCode); /* must have reasonCode before properties */ 266 | if (properties) 267 | MQTTProperties_write(&ptr, properties); 268 | } 269 | #endif 270 | rc = ptr - buf; 271 | exit: 272 | FUNC_EXIT_RC(rc); 273 | return rc; 274 | } 275 | 276 | 277 | /** 278 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 279 | * @param buf the buffer into which the packet will be serialized 280 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 281 | * @return serialized length, or error if 0 282 | */ 283 | #if defined(MQTTV5) 284 | int MQTTSerialize_disconnect(unsigned char* buf, int buflen) 285 | { 286 | return MQTTV5Serialize_disconnect(buf, buflen, -1, NULL); 287 | } 288 | 289 | int MQTTV5Serialize_disconnect(unsigned char* buf, int buflen, 290 | int reasonCode, MQTTProperties* properties) 291 | #else 292 | int MQTTSerialize_disconnect(unsigned char* buf, int buflen) 293 | #endif 294 | { 295 | #if defined(MQTTV5) 296 | return MQTTV5Serialize_zero(buf, buflen, DISCONNECT, reasonCode, properties); 297 | #else 298 | return MQTTSerialize_zero(buf, buflen, DISCONNECT); 299 | #endif 300 | } 301 | 302 | 303 | #if defined(MQTTV5) 304 | int MQTTV5Serialize_auth(unsigned char* buf, int buflen, 305 | int reasonCode, MQTTProperties* properties) 306 | { 307 | return MQTTV5Serialize_zero(buf, buflen, AUTH, reasonCode, properties); 308 | } 309 | #endif 310 | 311 | 312 | /** 313 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 314 | * @param buf the buffer into which the packet will be serialized 315 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 316 | * @return serialized length, or error if 0 317 | */ 318 | int MQTTSerialize_pingreq(unsigned char* buf, int buflen) 319 | { 320 | return MQTTSerialize_zero(buf, buflen, PINGREQ); 321 | } 322 | -------------------------------------------------------------------------------- /lib/FreeRTOS_Source/include/mpu_prototypes.h: -------------------------------------------------------------------------------- 1 | /* 2 | * FreeRTOS Kernel V10.1.1 3 | * Copyright (C) 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved. 4 | * 5 | * Permission is hereby granted, free of charge, to any person obtaining a copy of 6 | * this software and associated documentation files (the "Software"), to deal in 7 | * the Software without restriction, including without limitation the rights to 8 | * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of 9 | * the Software, and to permit persons to whom the Software is furnished to do so, 10 | * subject to the following conditions: 11 | * 12 | * The above copyright notice and this permission notice shall be included in all 13 | * copies or substantial portions of the Software. 14 | * 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS 17 | * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR 18 | * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER 19 | * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 20 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | * 22 | * http://www.FreeRTOS.org 23 | * http://aws.amazon.com/freertos 24 | * 25 | * 1 tab == 4 spaces! 26 | */ 27 | 28 | /* 29 | * When the MPU is used the standard (non MPU) API functions are mapped to 30 | * equivalents that start "MPU_", the prototypes for which are defined in this 31 | * header files. This will cause the application code to call the MPU_ version 32 | * which wraps the non-MPU version with privilege promoting then demoting code, 33 | * so the kernel code always runs will full privileges. 34 | */ 35 | 36 | 37 | #ifndef MPU_PROTOTYPES_H 38 | #define MPU_PROTOTYPES_H 39 | 40 | /* MPU versions of tasks.h API functions. */ 41 | BaseType_t MPU_xTaskCreate( TaskFunction_t pxTaskCode, const char * const pcName, const uint16_t usStackDepth, void * const pvParameters, UBaseType_t uxPriority, TaskHandle_t * const pxCreatedTask ); 42 | TaskHandle_t MPU_xTaskCreateStatic( TaskFunction_t pxTaskCode, const char * const pcName, const uint32_t ulStackDepth, void * const pvParameters, UBaseType_t uxPriority, StackType_t * const puxStackBuffer, StaticTask_t * const pxTaskBuffer ); 43 | BaseType_t MPU_xTaskCreateRestricted( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask ); 44 | BaseType_t MPU_xTaskCreateRestrictedStatic( const TaskParameters_t * const pxTaskDefinition, TaskHandle_t *pxCreatedTask ); 45 | void MPU_vTaskAllocateMPURegions( TaskHandle_t xTask, const MemoryRegion_t * const pxRegions ); 46 | void MPU_vTaskDelete( TaskHandle_t xTaskToDelete ); 47 | void MPU_vTaskDelay( const TickType_t xTicksToDelay ); 48 | void MPU_vTaskDelayUntil( TickType_t * const pxPreviousWakeTime, const TickType_t xTimeIncrement ); 49 | BaseType_t MPU_xTaskAbortDelay( TaskHandle_t xTask ); 50 | UBaseType_t MPU_uxTaskPriorityGet( const TaskHandle_t xTask ); 51 | eTaskState MPU_eTaskGetState( TaskHandle_t xTask ); 52 | void MPU_vTaskGetInfo( TaskHandle_t xTask, TaskStatus_t *pxTaskStatus, BaseType_t xGetFreeStackSpace, eTaskState eState ); 53 | void MPU_vTaskPrioritySet( TaskHandle_t xTask, UBaseType_t uxNewPriority ); 54 | void MPU_vTaskSuspend( TaskHandle_t xTaskToSuspend ); 55 | void MPU_vTaskResume( TaskHandle_t xTaskToResume ); 56 | void MPU_vTaskStartScheduler( void ); 57 | void MPU_vTaskSuspendAll( void ); 58 | BaseType_t MPU_xTaskResumeAll( void ); 59 | TickType_t MPU_xTaskGetTickCount( void ); 60 | UBaseType_t MPU_uxTaskGetNumberOfTasks( void ); 61 | char * MPU_pcTaskGetName( TaskHandle_t xTaskToQuery ); 62 | TaskHandle_t MPU_xTaskGetHandle( const char *pcNameToQuery ); 63 | UBaseType_t MPU_uxTaskGetStackHighWaterMark( TaskHandle_t xTask ); 64 | void MPU_vTaskSetApplicationTaskTag( TaskHandle_t xTask, TaskHookFunction_t pxHookFunction ); 65 | TaskHookFunction_t MPU_xTaskGetApplicationTaskTag( TaskHandle_t xTask ); 66 | void MPU_vTaskSetThreadLocalStoragePointer( TaskHandle_t xTaskToSet, BaseType_t xIndex, void *pvValue ); 67 | void * MPU_pvTaskGetThreadLocalStoragePointer( TaskHandle_t xTaskToQuery, BaseType_t xIndex ); 68 | BaseType_t MPU_xTaskCallApplicationTaskHook( TaskHandle_t xTask, void *pvParameter ); 69 | TaskHandle_t MPU_xTaskGetIdleTaskHandle( void ); 70 | UBaseType_t MPU_uxTaskGetSystemState( TaskStatus_t * const pxTaskStatusArray, const UBaseType_t uxArraySize, uint32_t * const pulTotalRunTime ); 71 | void MPU_vTaskList( char * pcWriteBuffer ); 72 | void MPU_vTaskGetRunTimeStats( char *pcWriteBuffer ); 73 | BaseType_t MPU_xTaskGenericNotify( TaskHandle_t xTaskToNotify, uint32_t ulValue, eNotifyAction eAction, uint32_t *pulPreviousNotificationValue ); 74 | BaseType_t MPU_xTaskNotifyWait( uint32_t ulBitsToClearOnEntry, uint32_t ulBitsToClearOnExit, uint32_t *pulNotificationValue, TickType_t xTicksToWait ); 75 | uint32_t MPU_ulTaskNotifyTake( BaseType_t xClearCountOnExit, TickType_t xTicksToWait ); 76 | BaseType_t MPU_xTaskNotifyStateClear( TaskHandle_t xTask ); 77 | BaseType_t MPU_xTaskIncrementTick( void ); 78 | TaskHandle_t MPU_xTaskGetCurrentTaskHandle( void ); 79 | void MPU_vTaskSetTimeOutState( TimeOut_t * const pxTimeOut ); 80 | BaseType_t MPU_xTaskCheckForTimeOut( TimeOut_t * const pxTimeOut, TickType_t * const pxTicksToWait ); 81 | void MPU_vTaskMissedYield( void ); 82 | BaseType_t MPU_xTaskGetSchedulerState( void ); 83 | 84 | /* MPU versions of queue.h API functions. */ 85 | BaseType_t MPU_xQueueGenericSend( QueueHandle_t xQueue, const void * const pvItemToQueue, TickType_t xTicksToWait, const BaseType_t xCopyPosition ); 86 | BaseType_t MPU_xQueueReceive( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait ); 87 | BaseType_t MPU_xQueuePeek( QueueHandle_t xQueue, void * const pvBuffer, TickType_t xTicksToWait ); 88 | BaseType_t MPU_xQueueSemaphoreTake( QueueHandle_t xQueue, TickType_t xTicksToWait ); 89 | UBaseType_t MPU_uxQueueMessagesWaiting( const QueueHandle_t xQueue ); 90 | UBaseType_t MPU_uxQueueSpacesAvailable( const QueueHandle_t xQueue ); 91 | void MPU_vQueueDelete( QueueHandle_t xQueue ); 92 | QueueHandle_t MPU_xQueueCreateMutex( const uint8_t ucQueueType ); 93 | QueueHandle_t MPU_xQueueCreateMutexStatic( const uint8_t ucQueueType, StaticQueue_t *pxStaticQueue ); 94 | QueueHandle_t MPU_xQueueCreateCountingSemaphore( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount ); 95 | QueueHandle_t MPU_xQueueCreateCountingSemaphoreStatic( const UBaseType_t uxMaxCount, const UBaseType_t uxInitialCount, StaticQueue_t *pxStaticQueue ); 96 | TaskHandle_t MPU_xQueueGetMutexHolder( QueueHandle_t xSemaphore ); 97 | BaseType_t MPU_xQueueTakeMutexRecursive( QueueHandle_t xMutex, TickType_t xTicksToWait ); 98 | BaseType_t MPU_xQueueGiveMutexRecursive( QueueHandle_t pxMutex ); 99 | void MPU_vQueueAddToRegistry( QueueHandle_t xQueue, const char *pcName ); 100 | void MPU_vQueueUnregisterQueue( QueueHandle_t xQueue ); 101 | const char * MPU_pcQueueGetName( QueueHandle_t xQueue ); 102 | QueueHandle_t MPU_xQueueGenericCreate( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, const uint8_t ucQueueType ); 103 | QueueHandle_t MPU_xQueueGenericCreateStatic( const UBaseType_t uxQueueLength, const UBaseType_t uxItemSize, uint8_t *pucQueueStorage, StaticQueue_t *pxStaticQueue, const uint8_t ucQueueType ); 104 | QueueSetHandle_t MPU_xQueueCreateSet( const UBaseType_t uxEventQueueLength ); 105 | BaseType_t MPU_xQueueAddToSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet ); 106 | BaseType_t MPU_xQueueRemoveFromSet( QueueSetMemberHandle_t xQueueOrSemaphore, QueueSetHandle_t xQueueSet ); 107 | QueueSetMemberHandle_t MPU_xQueueSelectFromSet( QueueSetHandle_t xQueueSet, const TickType_t xTicksToWait ); 108 | BaseType_t MPU_xQueueGenericReset( QueueHandle_t xQueue, BaseType_t xNewQueue ); 109 | void MPU_vQueueSetQueueNumber( QueueHandle_t xQueue, UBaseType_t uxQueueNumber ); 110 | UBaseType_t MPU_uxQueueGetQueueNumber( QueueHandle_t xQueue ); 111 | uint8_t MPU_ucQueueGetQueueType( QueueHandle_t xQueue ); 112 | 113 | /* MPU versions of timers.h API functions. */ 114 | TimerHandle_t MPU_xTimerCreate( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction ); 115 | TimerHandle_t MPU_xTimerCreateStatic( const char * const pcTimerName, const TickType_t xTimerPeriodInTicks, const UBaseType_t uxAutoReload, void * const pvTimerID, TimerCallbackFunction_t pxCallbackFunction, StaticTimer_t *pxTimerBuffer ); 116 | void * MPU_pvTimerGetTimerID( const TimerHandle_t xTimer ); 117 | void MPU_vTimerSetTimerID( TimerHandle_t xTimer, void *pvNewID ); 118 | BaseType_t MPU_xTimerIsTimerActive( TimerHandle_t xTimer ); 119 | TaskHandle_t MPU_xTimerGetTimerDaemonTaskHandle( void ); 120 | BaseType_t MPU_xTimerPendFunctionCall( PendedFunction_t xFunctionToPend, void *pvParameter1, uint32_t ulParameter2, TickType_t xTicksToWait ); 121 | const char * MPU_pcTimerGetName( TimerHandle_t xTimer ); 122 | TickType_t MPU_xTimerGetPeriod( TimerHandle_t xTimer ); 123 | TickType_t MPU_xTimerGetExpiryTime( TimerHandle_t xTimer ); 124 | BaseType_t MPU_xTimerCreateTimerTask( void ); 125 | BaseType_t MPU_xTimerGenericCommand( TimerHandle_t xTimer, const BaseType_t xCommandID, const TickType_t xOptionalValue, BaseType_t * const pxHigherPriorityTaskWoken, const TickType_t xTicksToWait ); 126 | 127 | /* MPU versions of event_group.h API functions. */ 128 | EventGroupHandle_t MPU_xEventGroupCreate( void ); 129 | EventGroupHandle_t MPU_xEventGroupCreateStatic( StaticEventGroup_t *pxEventGroupBuffer ); 130 | EventBits_t MPU_xEventGroupWaitBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToWaitFor, const BaseType_t xClearOnExit, const BaseType_t xWaitForAllBits, TickType_t xTicksToWait ); 131 | EventBits_t MPU_xEventGroupClearBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToClear ); 132 | EventBits_t MPU_xEventGroupSetBits( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet ); 133 | EventBits_t MPU_xEventGroupSync( EventGroupHandle_t xEventGroup, const EventBits_t uxBitsToSet, const EventBits_t uxBitsToWaitFor, TickType_t xTicksToWait ); 134 | void MPU_vEventGroupDelete( EventGroupHandle_t xEventGroup ); 135 | UBaseType_t MPU_uxEventGroupGetNumber( void* xEventGroup ); 136 | 137 | /* MPU versions of message/stream_buffer.h API functions. */ 138 | size_t MPU_xStreamBufferSend( StreamBufferHandle_t xStreamBuffer, const void *pvTxData, size_t xDataLengthBytes, TickType_t xTicksToWait ); 139 | size_t MPU_xStreamBufferSendFromISR( StreamBufferHandle_t xStreamBuffer, const void *pvTxData, size_t xDataLengthBytes, BaseType_t * const pxHigherPriorityTaskWoken ); 140 | size_t MPU_xStreamBufferReceive( StreamBufferHandle_t xStreamBuffer, void *pvRxData, size_t xBufferLengthBytes, TickType_t xTicksToWait ); 141 | size_t MPU_xStreamBufferNextMessageLengthBytes( StreamBufferHandle_t xStreamBuffer ); 142 | size_t MPU_xStreamBufferReceiveFromISR( StreamBufferHandle_t xStreamBuffer, void *pvRxData, size_t xBufferLengthBytes, BaseType_t * const pxHigherPriorityTaskWoken ); 143 | void MPU_vStreamBufferDelete( StreamBufferHandle_t xStreamBuffer ); 144 | BaseType_t MPU_xStreamBufferIsFull( StreamBufferHandle_t xStreamBuffer ); 145 | BaseType_t MPU_xStreamBufferIsEmpty( StreamBufferHandle_t xStreamBuffer ); 146 | BaseType_t MPU_xStreamBufferReset( StreamBufferHandle_t xStreamBuffer ); 147 | size_t MPU_xStreamBufferSpacesAvailable( StreamBufferHandle_t xStreamBuffer ); 148 | size_t MPU_xStreamBufferBytesAvailable( StreamBufferHandle_t xStreamBuffer ); 149 | BaseType_t MPU_xStreamBufferSetTriggerLevel( StreamBufferHandle_t xStreamBuffer, size_t xTriggerLevel ); 150 | StreamBufferHandle_t MPU_xStreamBufferGenericCreate( size_t xBufferSizeBytes, size_t xTriggerLevelBytes, BaseType_t xIsMessageBuffer ); 151 | StreamBufferHandle_t MPU_xStreamBufferGenericCreateStatic( size_t xBufferSizeBytes, size_t xTriggerLevelBytes, BaseType_t xIsMessageBuffer, uint8_t * const pucStreamBufferStorageArea, StaticStreamBuffer_t * const pxStaticStreamBuffer ); 152 | 153 | 154 | 155 | #endif /* MPU_PROTOTYPES_H */ 156 | 157 | --------------------------------------------------------------------------------