├── .gitignore ├── CHANGELOG.md ├── LICENSE ├── README.md ├── examples └── MQTTSUB │ ├── .cproject │ ├── .gitignore │ ├── .mxproject │ ├── .project │ ├── .settings │ ├── language.settings.xml │ └── stm32cubeide.project.prefs │ ├── CMakeLists.txt │ ├── CMakeLists_template.txt │ ├── Core │ ├── Inc │ │ ├── MQTTConnect.h │ │ ├── MQTTFormat.h │ │ ├── MQTTPacket.h │ │ ├── MQTTPublish.h │ │ ├── MQTTSim800.h │ │ ├── MQTTSubscribe.h │ │ ├── MQTTUnsubscribe.h │ │ ├── StackTrace.h │ │ ├── gpio.h │ │ ├── main.h │ │ ├── stm32f4xx_hal_conf.h │ │ ├── stm32f4xx_it.h │ │ └── usart.h │ ├── Src │ │ ├── MQTTConnectClient.c │ │ ├── MQTTDeserializePublish.c │ │ ├── MQTTFormat.c │ │ ├── MQTTPacket.c │ │ ├── MQTTSerializePublish.c │ │ ├── MQTTSim800.c │ │ ├── MQTTSubscribeClient.c │ │ ├── MQTTUnsubscribeClient.c │ │ ├── gpio.c │ │ ├── main.c │ │ ├── stm32f4xx_hal_msp.c │ │ ├── stm32f4xx_it.c │ │ ├── syscalls.c │ │ ├── sysmem.c │ │ ├── system_stm32f4xx.c │ │ └── usart.c │ └── Startup │ │ └── startup_stm32f401ccux.s │ ├── Drivers │ ├── CMSIS │ │ ├── Device │ │ │ └── ST │ │ │ │ └── STM32F4xx │ │ │ │ └── Include │ │ │ │ ├── stm32f401xc.h │ │ │ │ ├── stm32f4xx.h │ │ │ │ └── system_stm32f4xx.h │ │ └── Include │ │ │ ├── cmsis_armcc.h │ │ │ ├── cmsis_armclang.h │ │ │ ├── cmsis_compiler.h │ │ │ ├── cmsis_gcc.h │ │ │ ├── cmsis_iccarm.h │ │ │ ├── cmsis_version.h │ │ │ ├── core_armv8mbl.h │ │ │ ├── core_armv8mml.h │ │ │ ├── core_cm0.h │ │ │ ├── core_cm0plus.h │ │ │ ├── core_cm1.h │ │ │ ├── core_cm23.h │ │ │ ├── core_cm3.h │ │ │ ├── core_cm33.h │ │ │ ├── core_cm4.h │ │ │ ├── core_cm7.h │ │ │ ├── core_sc000.h │ │ │ ├── core_sc300.h │ │ │ ├── mpu_armv7.h │ │ │ ├── mpu_armv8.h │ │ │ └── tz_context.h │ └── STM32F4xx_HAL_Driver │ │ ├── Inc │ │ ├── Legacy │ │ │ └── stm32_hal_legacy.h │ │ ├── stm32f4xx_hal.h │ │ ├── stm32f4xx_hal_cortex.h │ │ ├── stm32f4xx_hal_def.h │ │ ├── stm32f4xx_hal_dma.h │ │ ├── stm32f4xx_hal_dma_ex.h │ │ ├── stm32f4xx_hal_exti.h │ │ ├── stm32f4xx_hal_flash.h │ │ ├── stm32f4xx_hal_flash_ex.h │ │ ├── stm32f4xx_hal_flash_ramfunc.h │ │ ├── stm32f4xx_hal_gpio.h │ │ ├── stm32f4xx_hal_gpio_ex.h │ │ ├── stm32f4xx_hal_pwr.h │ │ ├── stm32f4xx_hal_pwr_ex.h │ │ ├── stm32f4xx_hal_rcc.h │ │ ├── stm32f4xx_hal_rcc_ex.h │ │ ├── stm32f4xx_hal_tim.h │ │ ├── stm32f4xx_hal_tim_ex.h │ │ └── stm32f4xx_hal_uart.h │ │ └── Src │ │ ├── stm32f4xx_hal.c │ │ ├── stm32f4xx_hal_cortex.c │ │ ├── stm32f4xx_hal_dma.c │ │ ├── stm32f4xx_hal_dma_ex.c │ │ ├── stm32f4xx_hal_exti.c │ │ ├── stm32f4xx_hal_flash.c │ │ ├── stm32f4xx_hal_flash_ex.c │ │ ├── stm32f4xx_hal_flash_ramfunc.c │ │ ├── stm32f4xx_hal_gpio.c │ │ ├── stm32f4xx_hal_pwr.c │ │ ├── stm32f4xx_hal_pwr_ex.c │ │ ├── stm32f4xx_hal_rcc.c │ │ ├── stm32f4xx_hal_rcc_ex.c │ │ ├── stm32f4xx_hal_tim.c │ │ ├── stm32f4xx_hal_tim_ex.c │ │ └── stm32f4xx_hal_uart.c │ ├── MQTTSUB Debug.launch │ ├── MQTTSUB.ioc │ ├── STM32F401CCUX_FLASH.ld │ └── stm32f4discovery(stlink).cfg ├── ext lib ├── MQTTConnect.h ├── MQTTConnectClient.c ├── MQTTDeserializePublish.c ├── MQTTFormat.c ├── MQTTFormat.h ├── MQTTPacket.c ├── MQTTPacket.h ├── MQTTPublish.h ├── MQTTSerializePublish.c ├── MQTTSubscribe.h ├── MQTTSubscribeClient.c ├── MQTTUnsubscribe.h ├── MQTTUnsubscribeClient.c └── StackTrace.h ├── img ├── ch_pair.png └── nvic.png └── src ├── MQTTSim800.c └── MQTTSim800.h /.gitignore: -------------------------------------------------------------------------------- 1 | # Clion files 2 | .idea/ -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | ## [2.1.1] - 2021-08-07 2 | - Add "DEACT" error; 3 | - Correct printf placeholder params for unsigned; 4 | 5 | ## [2.1.0] - 2021-05-06 6 | - Corrected delay on PUB SUB functions; 7 | - Added publishing functions (MQTT_PubUint8, MQTT_PubUint16, MQTT_PubDouble, etc ) for numeric types: 8 | - uint8_t; 9 | - uint16_t; 10 | - uint32_t; 11 | - float; 12 | - double; 13 | 14 | ## [2.0.0] - 2021-01-26 15 | - Added support for MQTT subscriptions. 16 | - work is performed through the transparent mode; 17 | - increased the buffer size to the maximum for SIM800 and now is 1460 bytes. 18 | - Small bugfixes by code. 19 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 2 | Version 2, December 2004 3 | 4 | Copyright (C) 2020 Bulanov Konstantin 5 | 6 | Everyone is permitted to copy and distribute verbatim or modified 7 | copies of this license document, and changing it is allowed as long 8 | as the name is changed. 9 | 10 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 11 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 12 | 13 | 0. You just DO WHAT THE FUCK YOU WANT TO. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # STM32 HAL library for SIM800 (GPRS) release MQTT client over AT command 2 | 3 | Simple C library (STM32 HAL) for working with the MQTT protocol through AT commands of the SIM800 (GPRS) module 4 | https://aliexpress.ru/item/32284560394.html?spm=a2g0s.9042311.0.0.34a833edF2cSsx 5 | 6 | Configure STM32CubeMX by setting "General peripheral Initalizion as a pair of '.c / .h' file per peripheral" in the project settings. 7 | ![photo](https://raw.githubusercontent.com/leech001/SIM800MQTT/master/img/ch_pair.png) 8 | Remember to enable global interrupts for your UART. 9 | ![photo](https://raw.githubusercontent.com/leech001/SIM800MQTT/master/img/nvic.png) 10 | Copy the library header and source file to the appropriate project directories (Inc, Src). 11 | Configure the UART port where your module is connected in the MQTTSim800.h file. 12 | ``` 13 | #define UART_SIM800 &huart2 14 | #define FREERTOS 0 15 | #define CMD_DELAY 2000 16 | ``` 17 | In the head file of your project (main.c), include the header file 18 | ``` 19 | / * USER CODE BEGIN Includes * / 20 | #include "MQTTSim800.h" 21 | / * USER CODE END Includes * / 22 | ``` 23 | In the head file of your project (main.c), create SIM800 structure 24 | ``` 25 | /* USER CODE BEGIN PV */ 26 | SIM800_t SIM800; 27 | /* USER CODE END PV */ 28 | ``` 29 | add function call Sim800_RxCallBack () to interrupt UART 30 | ``` 31 | / * USER CODE BEGIN 0 * / 32 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef * huart) { 33 |     if (huart == UART_SIM800) { 34 |         Sim800_RxCallBack(); 35 |     } 36 | } 37 | / * USER CODE END 0 * / 38 | ``` 39 | Fill structure in the main (void) function section 40 | ``` 41 | /* USER CODE BEGIN 2 */ 42 | SIM800.sim.apn = "internet"; 43 | SIM800.sim.apn_user = ""; 44 | SIM800.sim.apn_pass = ""; 45 | SIM800.mqttServer.host = "mqtt.mqtt.ru"; 46 | SIM800.mqttServer.port = 1883; 47 | SIM800.mqttClient.username = "user"; 48 | SIM800.mqttClient.pass = "pass"; 49 | SIM800.mqttClient.clientID = "TestSub"; 50 | SIM800.mqttClient.keepAliveInterval = 120; 51 | /* USER CODE END 2 */ 52 | ``` 53 | add the module initialization code in the main (void) function section 54 | ``` 55 | / * USER CODE BEGIN 2 * / 56 | MQTT_Init(); 57 | / * USER CODE END 2 * / 58 | ``` 59 | add Subscription status 60 | ``` 61 | / * USER CODE BEGIN 2 * / 62 | uint8_t sub = 0; 63 | / * USER CODE END 2 * / 64 | ``` 65 | add a send topic "STM32" and value "test" to the server in an infinite while (1) loop, for example every 1 seconds. 66 | add subscribe to topic "test" 67 | ``` 68 | /* Infinite loop */ 69 | /* USER CODE BEGIN WHILE */ 70 | while (1) { 71 | if (SIM800.mqttServer.connect == 0) { 72 | MQTT_Init(); 73 | sub = 0; 74 | } 75 | if (SIM800.mqttServer.connect == 1) { 76 | if(sub == 0){ 77 | MQTT_Sub("test"); 78 | sub = 1; 79 | } 80 | MQTT_Pub("STM32", "test"); 81 | 82 | if(SIM800.mqttReceive.newEvent) { 83 | unsigned char *topic = SIM800.mqttReceive.topic; 84 | int payload = atoi(SIM800.mqttReceive.payload); 85 | SIM800.mqttReceive.newEvent = 0; 86 | } 87 | } 88 | HAL_Delay(1000); 89 | /* USER CODE END WHILE */ 90 | ``` 91 | This completes the setup. 92 | Check in loop SIM800.mqttReceive.newEvent for new publish events. Set to 0 (zero) while use new data. 93 | 94 | The library also supports sending zero messages to the server to maintain a connection. 95 | ``` 96 | MQTT_PingReq(); 97 | ``` 98 | The library is implemented based on the code for generating MQTT packages of the project https://github.com/eclipse/paho.mqtt.embedded-c/tree/master/MQTTPacket 99 | 100 | The library testing with https://github.com/r2axz/bluepill-serial-monster -------------------------------------------------------------------------------- /examples/MQTTSUB/.gitignore: -------------------------------------------------------------------------------- 1 | /Debug/ 2 | -------------------------------------------------------------------------------- /examples/MQTTSUB/.mxproject: -------------------------------------------------------------------------------- 1 | [PreviousLibFiles] 2 | LibFiles=Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_tim_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_uart.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_rcc_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_gpio_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_pwr_ex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_cortex.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal.h;Drivers/STM32F4xx_HAL_Driver/Inc/Legacy/stm32_hal_legacy.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h;Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_exti.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f401xc.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;Drivers/CMSIS/Include/cmsis_armcc.h;Drivers/CMSIS/Include/cmsis_armclang.h;Drivers/CMSIS/Include/cmsis_compiler.h;Drivers/CMSIS/Include/cmsis_gcc.h;Drivers/CMSIS/Include/cmsis_iccarm.h;Drivers/CMSIS/Include/cmsis_version.h;Drivers/CMSIS/Include/core_armv8mbl.h;Drivers/CMSIS/Include/core_armv8mml.h;Drivers/CMSIS/Include/core_cm0.h;Drivers/CMSIS/Include/core_cm0plus.h;Drivers/CMSIS/Include/core_cm1.h;Drivers/CMSIS/Include/core_cm23.h;Drivers/CMSIS/Include/core_cm3.h;Drivers/CMSIS/Include/core_cm33.h;Drivers/CMSIS/Include/core_cm4.h;Drivers/CMSIS/Include/core_cm7.h;Drivers/CMSIS/Include/core_sc000.h;Drivers/CMSIS/Include/core_sc300.h;Drivers/CMSIS/Include/mpu_armv7.h;Drivers/CMSIS/Include/mpu_armv8.h;Drivers/CMSIS/Include/tz_context.h; 3 | 4 | [PreviousUsedCubeIDEFiles] 5 | SourceFiles=Core\Src\main.c;Core\Src\gpio.c;Core\Src\usart.c;Core\Src\stm32f4xx_it.c;Core\Src\stm32f4xx_hal_msp.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Core\Src/system_stm32f4xx.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_tim_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_uart.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_rcc_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_gpio.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_dma.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_pwr_ex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_cortex.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c;Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_exti.c;Core\Src/system_stm32f4xx.c;Drivers/CMSIS/Device/ST/STM32F4xx/Source/Templates/system_stm32f4xx.c;; 6 | HeaderPath=Drivers\STM32F4xx_HAL_Driver\Inc;Drivers\STM32F4xx_HAL_Driver\Inc\Legacy;Drivers\CMSIS\Device\ST\STM32F4xx\Include;Drivers\CMSIS\Include;Core\Inc; 7 | CDefines=USE_HAL_DRIVER;STM32F401xC;USE_HAL_DRIVER;USE_HAL_DRIVER; 8 | 9 | [PreviousGenFiles] 10 | AdvancedFolderStructure=true 11 | HeaderFileListSize=5 12 | HeaderFiles#0=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Inc/gpio.h 13 | HeaderFiles#1=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Inc/usart.h 14 | HeaderFiles#2=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Inc/stm32f4xx_it.h 15 | HeaderFiles#3=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Inc/stm32f4xx_hal_conf.h 16 | HeaderFiles#4=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Inc/main.h 17 | HeaderFolderListSize=1 18 | HeaderPath#0=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Inc 19 | HeaderFiles=; 20 | SourceFileListSize=5 21 | SourceFiles#0=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Src/gpio.c 22 | SourceFiles#1=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Src/usart.c 23 | SourceFiles#2=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Src/stm32f4xx_it.c 24 | SourceFiles#3=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Src/stm32f4xx_hal_msp.c 25 | SourceFiles#4=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Src/main.c 26 | SourceFolderListSize=1 27 | SourcePath#0=C:/Project/GIT/STM32/SIM800MQTT/examples/MQTTSUB/Core/Src 28 | SourceFiles=; 29 | 30 | -------------------------------------------------------------------------------- /examples/MQTTSUB/.project: -------------------------------------------------------------------------------- 1 | 2 | 3 | MQTTSUB 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 | com.st.stm32cube.ide.mcu.MCUProjectNature 23 | org.eclipse.cdt.core.cnature 24 | com.st.stm32cube.ide.mcu.MCUCubeIdeServicesRevAev2ProjectNature 25 | com.st.stm32cube.ide.mcu.MCUCubeProjectNature 26 | com.st.stm32cube.ide.mcu.MCUAdvancedStructureProjectNature 27 | com.st.stm32cube.ide.mcu.MCUEndUserDisabledTrustZoneProjectNature 28 | com.st.stm32cube.ide.mcu.MCUSingleCpuProjectNature 29 | com.st.stm32cube.ide.mcu.MCURootProjectNature 30 | org.eclipse.cdt.managedbuilder.core.managedBuildNature 31 | org.eclipse.cdt.managedbuilder.core.ScannerConfigNature 32 | 33 | 34 | -------------------------------------------------------------------------------- /examples/MQTTSUB/.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 | 27 | -------------------------------------------------------------------------------- /examples/MQTTSUB/.settings/stm32cubeide.project.prefs: -------------------------------------------------------------------------------- 1 | 8DF89ED150041C4CBC7CB9A9CAA90856=0C86D7ED0DEBF412462B3FA7A4E6C7D0 2 | DC22A860405A8BF2F2C095E5B6529F12=7B750BFCB298F2EA884A9B0F68E5D3E5 3 | eclipse.preferences.version=1 4 | -------------------------------------------------------------------------------- /examples/MQTTSUB/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #THIS FILE IS AUTO GENERATED FROM THE TEMPLATE! DO NOT CHANGE! 2 | set(CMAKE_SYSTEM_NAME Generic) 3 | set(CMAKE_SYSTEM_VERSION 1) 4 | cmake_minimum_required(VERSION 3.17) 5 | 6 | # specify cross compilers and tools 7 | set(CMAKE_C_COMPILER arm-none-eabi-gcc) 8 | set(CMAKE_CXX_COMPILER arm-none-eabi-g++) 9 | set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) 10 | set(CMAKE_AR arm-none-eabi-ar) 11 | set(CMAKE_OBJCOPY arm-none-eabi-objcopy) 12 | set(CMAKE_OBJDUMP arm-none-eabi-objdump) 13 | set(SIZE arm-none-eabi-size) 14 | set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) 15 | 16 | # project settings 17 | project(MQTTSUB C CXX ASM) 18 | set(CMAKE_CXX_STANDARD 17) 19 | set(CMAKE_C_STANDARD 11) 20 | 21 | #Uncomment for hardware floating point 22 | #add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) 23 | #add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) 24 | #add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) 25 | 26 | #Uncomment for software floating point 27 | #add_compile_options(-mfloat-abi=soft) 28 | 29 | add_compile_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork) 30 | add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) 31 | 32 | # uncomment to mitigate c++17 absolute addresses warnings 33 | #set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-register") 34 | 35 | if ("${CMAKE_BUILD_TYPE}" STREQUAL "Release") 36 | message(STATUS "Maximum optimization for speed") 37 | add_compile_options(-Ofast) 38 | elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") 39 | message(STATUS "Maximum optimization for speed, debug info included") 40 | add_compile_options(-Ofast -g) 41 | elseif ("${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") 42 | message(STATUS "Maximum optimization for size") 43 | add_compile_options(-Os) 44 | else () 45 | message(STATUS "Minimal optimization, debug info included") 46 | add_compile_options(-Og -g) 47 | endif () 48 | 49 | include_directories(Core/Inc Drivers/STM32F4xx_HAL_Driver/Inc Drivers/STM32F4xx_HAL_Driver/Inc/Legacy Drivers/CMSIS/Device/ST/STM32F4xx/Include Drivers/CMSIS/Include) 50 | 51 | add_definitions(-DUSE_HAL_DRIVER -DSTM32F401xC -DDEBUG) 52 | 53 | file(GLOB_RECURSE SOURCES "Core/*.*" "Drivers/*.*") 54 | 55 | set(LINKER_SCRIPT ${CMAKE_SOURCE_DIR}/STM32F401CCUX_FLASH.ld) 56 | 57 | add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=${PROJECT_BINARY_DIR}/${PROJECT_NAME}.map) 58 | add_link_options(-mcpu=cortex-m4 -mthumb -mthumb-interwork) 59 | add_link_options(-T ${LINKER_SCRIPT}) 60 | 61 | add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT}) 62 | 63 | set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex) 64 | set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin) 65 | 66 | add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD 67 | COMMAND ${CMAKE_OBJCOPY} -Oihex $ ${HEX_FILE} 68 | COMMAND ${CMAKE_OBJCOPY} -Obinary $ ${BIN_FILE} 69 | COMMENT "Building ${HEX_FILE} 70 | Building ${BIN_FILE}") 71 | -------------------------------------------------------------------------------- /examples/MQTTSUB/CMakeLists_template.txt: -------------------------------------------------------------------------------- 1 | #${templateWarning} 2 | set(CMAKE_SYSTEM_NAME Generic) 3 | set(CMAKE_SYSTEM_VERSION 1) 4 | ${cmakeRequiredVersion} 5 | # specify cross compilers and tools 6 | set(CMAKE_C_COMPILER arm-none-eabi-gcc) 7 | set(CMAKE_CXX_COMPILER arm-none-eabi-g++) 8 | set(CMAKE_ASM_COMPILER arm-none-eabi-gcc) 9 | set(CMAKE_AR arm-none-eabi-ar) 10 | set(CMAKE_OBJCOPY arm-none-eabi-objcopy) 11 | set(CMAKE_OBJDUMP arm-none-eabi-objdump) 12 | set(SIZE arm-none-eabi-size) 13 | set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY) 14 | 15 | # project settings 16 | project(${projectName} C CXX ASM) 17 | set(CMAKE_CXX_STANDARD 17) 18 | set(CMAKE_C_STANDARD 11) 19 | 20 | #Uncomment for hardware floating point 21 | #add_compile_definitions(ARM_MATH_CM4;ARM_MATH_MATRIX_CHECK;ARM_MATH_ROUNDING) 22 | #add_compile_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) 23 | #add_link_options(-mfloat-abi=hard -mfpu=fpv4-sp-d16) 24 | 25 | #Uncomment for software floating point 26 | #add_compile_options(-mfloat-abi=soft) 27 | 28 | add_compile_options(-mcpu=${mcpu} -mthumb -mthumb-interwork) 29 | add_compile_options(-ffunction-sections -fdata-sections -fno-common -fmessage-length=0) 30 | 31 | # uncomment to mitigate c++17 absolute addresses warnings 32 | #set(CMAKE_CXX_FLAGS "$${CMAKE_CXX_FLAGS} -Wno-register") 33 | 34 | if ("$${CMAKE_BUILD_TYPE}" STREQUAL "Release") 35 | message(STATUS "Maximum optimization for speed") 36 | add_compile_options(-Ofast) 37 | elseif ("$${CMAKE_BUILD_TYPE}" STREQUAL "RelWithDebInfo") 38 | message(STATUS "Maximum optimization for speed, debug info included") 39 | add_compile_options(-Ofast -g) 40 | elseif ("$${CMAKE_BUILD_TYPE}" STREQUAL "MinSizeRel") 41 | message(STATUS "Maximum optimization for size") 42 | add_compile_options(-Os) 43 | else () 44 | message(STATUS "Minimal optimization, debug info included") 45 | add_compile_options(-Og -g) 46 | endif () 47 | 48 | include_directories(${includes}) 49 | 50 | add_definitions(${defines}) 51 | 52 | file(GLOB_RECURSE SOURCES ${sources}) 53 | 54 | set(LINKER_SCRIPT $${CMAKE_SOURCE_DIR}/${linkerScript}) 55 | 56 | add_link_options(-Wl,-gc-sections,--print-memory-usage,-Map=$${PROJECT_BINARY_DIR}/$${PROJECT_NAME}.map) 57 | add_link_options(-mcpu=${mcpu} -mthumb -mthumb-interwork) 58 | add_link_options(-T $${LINKER_SCRIPT}) 59 | 60 | add_executable($${PROJECT_NAME}.elf $${SOURCES} $${LINKER_SCRIPT}) 61 | 62 | set(HEX_FILE $${PROJECT_BINARY_DIR}/$${PROJECT_NAME}.hex) 63 | set(BIN_FILE $${PROJECT_BINARY_DIR}/$${PROJECT_NAME}.bin) 64 | 65 | add_custom_command(TARGET $${PROJECT_NAME}.elf POST_BUILD 66 | COMMAND $${CMAKE_OBJCOPY} -Oihex $ $${HEX_FILE} 67 | COMMAND $${CMAKE_OBJCOPY} -Obinary $ $${BIN_FILE} 68 | COMMENT "Building $${HEX_FILE} 69 | Building $${BIN_FILE}") 70 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/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 MQTTSerialize_pingreq(unsigned char* buf, int buflen); 147 | 148 | #endif /* MQTTCONNECT_H_ */ 149 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/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 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/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 | }; 49 | 50 | /** 51 | * Bitfields for the MQTT header byte. 52 | */ 53 | typedef union 54 | { 55 | unsigned char byte; /**< the whole byte */ 56 | #if defined(REVERSED) 57 | struct 58 | { 59 | unsigned int type : 4; /**< message type nibble */ 60 | unsigned int dup : 1; /**< DUP flag bit */ 61 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 62 | unsigned int retain : 1; /**< retained flag bit */ 63 | } bits; 64 | #else 65 | struct 66 | { 67 | unsigned int retain : 1; /**< retained flag bit */ 68 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 69 | unsigned int dup : 1; /**< DUP flag bit */ 70 | unsigned int type : 4; /**< message type nibble */ 71 | } bits; 72 | #endif 73 | } MQTTHeader; 74 | 75 | typedef struct 76 | { 77 | int len; 78 | char* data; 79 | } MQTTLenString; 80 | 81 | typedef struct 82 | { 83 | char* cstring; 84 | MQTTLenString lenstring; 85 | } MQTTString; 86 | 87 | #define MQTTString_initializer {NULL, {0, NULL}} 88 | 89 | int MQTTstrlen(MQTTString mqttstring); 90 | 91 | #include "MQTTConnect.h" 92 | #include "MQTTPublish.h" 93 | #include "MQTTSubscribe.h" 94 | #include "MQTTUnsubscribe.h" 95 | #include "MQTTFormat.h" 96 | 97 | DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); 98 | DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); 99 | 100 | int MQTTPacket_len(int rem_len); 101 | DLLExport int MQTTPacket_equals(MQTTString* a, char* b); 102 | 103 | DLLExport int MQTTPacket_encode(unsigned char* buf, int length); 104 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); 105 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); 106 | 107 | int readInt(unsigned char** pptr); 108 | char readChar(unsigned char** pptr); 109 | void writeChar(unsigned char** pptr, char c); 110 | void writeInt(unsigned char** pptr, int anInt); 111 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); 112 | void writeCString(unsigned char** pptr, const char* string); 113 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); 114 | 115 | DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); 116 | 117 | typedef struct { 118 | int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ 119 | void *sck; /* pointer to whatever the system may use to identify the transport */ 120 | int multiplier; 121 | int rem_len; 122 | int len; 123 | char state; 124 | }MQTTTransport; 125 | 126 | int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); 127 | 128 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 129 | } 130 | #endif 131 | 132 | 133 | #endif /* MQTTPACKET_H_ */ 134 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/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 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/MQTTSim800.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MQTTSim800.h 3 | * 4 | * Created on: Jan 4, 2020 5 | * Author: Bulanov Konstantin 6 | * 7 | * Contact information 8 | * ------------------- 9 | * 10 | * e-mail : leech001@gmail.com 11 | * telegram : https://t.me/leech001 12 | * 13 | * 14 | */ 15 | 16 | /* 17 | * ----------------------------------------------------------------------------------------------------------------------------------------------- 18 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 19 | Version 2, December 2004 20 | 21 | Copyright (C) 2020 Bulanov Konstantin 22 | 23 | Everyone is permitted to copy and distribute verbatim or modified 24 | copies of this license document, and changing it is allowed as long 25 | as the name is changed. 26 | 27 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 28 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 29 | 30 | 0. You just DO WHAT THE FUCK YOU WANT TO. 31 | 32 | MQTT packet https://github.com/eclipse/paho.mqtt.embedded-c/tree/master/MQTTPacket 33 | * ------------------------------------------------------------------------------------------------------------------------------------------------ 34 | */ 35 | 36 | #include 37 | 38 | // === CONFIG === 39 | #define UART_SIM800 &huart2 40 | #define FREERTOS 0 41 | #define CMD_DELAY 2000 42 | // ============== 43 | 44 | typedef struct { 45 | char *apn; 46 | char *apn_user; 47 | char *apn_pass; 48 | } sim_t; 49 | 50 | typedef struct { 51 | char *host; 52 | uint16_t port; 53 | uint8_t connect; 54 | } mqttServer_t; 55 | 56 | typedef struct { 57 | char *username; 58 | char *pass; 59 | char *clientID; 60 | unsigned short keepAliveInterval; 61 | } mqttClient_t; 62 | 63 | typedef struct { 64 | uint8_t newEvent; 65 | unsigned char dup; 66 | int qos; 67 | unsigned char retained; 68 | unsigned short msgId; 69 | unsigned char payload[64]; 70 | int payloadLen; 71 | unsigned char topic[64]; 72 | int topicLen; 73 | } mqttReceive_t; 74 | 75 | typedef struct { 76 | sim_t sim; 77 | mqttServer_t mqttServer; 78 | mqttClient_t mqttClient; 79 | mqttReceive_t mqttReceive; 80 | } SIM800_t; 81 | 82 | void Sim800_RxCallBack(void); 83 | 84 | void clearRxBuffer(void); 85 | 86 | void clearMqttBuffer(void); 87 | 88 | int SIM800_SendCommand(char *command, char *reply, uint16_t delay); 89 | 90 | int MQTT_Init(void); 91 | 92 | void MQTT_Connect(void); 93 | 94 | void MQTT_Pub(char *topic, char *payload); 95 | 96 | void MQTT_PubUint8(char *topic, uint8_t data); 97 | 98 | void MQTT_PubUint16(char *topic, uint16_t data); 99 | 100 | void MQTT_PubUint32(char *topic, uint32_t data); 101 | 102 | void MQTT_PubFloat(char *topic, float payload); 103 | 104 | void MQTT_PubDouble(char *topic, double data); 105 | 106 | void MQTT_PingReq(void); 107 | 108 | void MQTT_Sub(char *topic); 109 | 110 | void MQTT_Receive(unsigned char *buf); 111 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/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 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/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 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/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 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/gpio.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file gpio.h 4 | * @brief This file contains all the function prototypes for 5 | * the gpio.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2021 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* Define to prevent recursive inclusion -------------------------------------*/ 20 | #ifndef __GPIO_H__ 21 | #define __GPIO_H__ 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* Includes ------------------------------------------------------------------*/ 28 | #include "main.h" 29 | 30 | /* USER CODE BEGIN Includes */ 31 | 32 | /* USER CODE END Includes */ 33 | 34 | /* USER CODE BEGIN Private defines */ 35 | 36 | /* USER CODE END Private defines */ 37 | 38 | void MX_GPIO_Init(void); 39 | 40 | /* USER CODE BEGIN Prototypes */ 41 | 42 | /* USER CODE END Prototypes */ 43 | 44 | #ifdef __cplusplus 45 | } 46 | #endif 47 | #endif /*__ GPIO_H__ */ 48 | 49 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 50 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/main.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : main.h 5 | * @brief : Header for main.c file. 6 | * This file contains the common defines of the application. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | *

© Copyright (c) 2020 STMicroelectronics. 11 | * All rights reserved.

12 | * 13 | * This software component is licensed by ST under BSD 3-Clause license, 14 | * the "License"; You may not use this file except in compliance with the 15 | * License. You may obtain a copy of the License at: 16 | * opensource.org/licenses/BSD-3-Clause 17 | * 18 | ****************************************************************************** 19 | */ 20 | /* USER CODE END Header */ 21 | 22 | /* Define to prevent recursive inclusion -------------------------------------*/ 23 | #ifndef __MAIN_H 24 | #define __MAIN_H 25 | 26 | #ifdef __cplusplus 27 | extern "C" { 28 | #endif 29 | 30 | /* Includes ------------------------------------------------------------------*/ 31 | #include "stm32f4xx_hal.h" 32 | 33 | /* Private includes ----------------------------------------------------------*/ 34 | /* USER CODE BEGIN Includes */ 35 | 36 | /* USER CODE END Includes */ 37 | 38 | /* Exported types ------------------------------------------------------------*/ 39 | /* USER CODE BEGIN ET */ 40 | 41 | /* USER CODE END ET */ 42 | 43 | /* Exported constants --------------------------------------------------------*/ 44 | /* USER CODE BEGIN EC */ 45 | 46 | /* USER CODE END EC */ 47 | 48 | /* Exported macro ------------------------------------------------------------*/ 49 | /* USER CODE BEGIN EM */ 50 | 51 | /* USER CODE END EM */ 52 | 53 | /* Exported functions prototypes ---------------------------------------------*/ 54 | void Error_Handler(void); 55 | 56 | /* USER CODE BEGIN EFP */ 57 | 58 | /* USER CODE END EFP */ 59 | 60 | /* Private defines -----------------------------------------------------------*/ 61 | #define SIM_PWR_Pin GPIO_PIN_0 62 | #define SIM_PWR_GPIO_Port GPIOB 63 | /* USER CODE BEGIN Private defines */ 64 | 65 | /* USER CODE END Private defines */ 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | 71 | #endif /* __MAIN_H */ 72 | 73 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 74 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/stm32f4xx_it.h: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f4xx_it.h 5 | * @brief This file contains the headers of the interrupt handlers. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2020 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __STM32F4xx_IT_H 23 | #define __STM32F4xx_IT_H 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Private includes ----------------------------------------------------------*/ 30 | /* USER CODE BEGIN Includes */ 31 | 32 | /* USER CODE END Includes */ 33 | 34 | /* Exported types ------------------------------------------------------------*/ 35 | /* USER CODE BEGIN ET */ 36 | 37 | /* USER CODE END ET */ 38 | 39 | /* Exported constants --------------------------------------------------------*/ 40 | /* USER CODE BEGIN EC */ 41 | 42 | /* USER CODE END EC */ 43 | 44 | /* Exported macro ------------------------------------------------------------*/ 45 | /* USER CODE BEGIN EM */ 46 | 47 | /* USER CODE END EM */ 48 | 49 | /* Exported functions prototypes ---------------------------------------------*/ 50 | void NMI_Handler(void); 51 | void HardFault_Handler(void); 52 | void MemManage_Handler(void); 53 | void BusFault_Handler(void); 54 | void UsageFault_Handler(void); 55 | void SVC_Handler(void); 56 | void DebugMon_Handler(void); 57 | void PendSV_Handler(void); 58 | void SysTick_Handler(void); 59 | void USART2_IRQHandler(void); 60 | /* USER CODE BEGIN EFP */ 61 | 62 | /* USER CODE END EFP */ 63 | 64 | #ifdef __cplusplus 65 | } 66 | #endif 67 | 68 | #endif /* __STM32F4xx_IT_H */ 69 | 70 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 71 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Inc/usart.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usart.h 4 | * @brief This file contains all the function prototypes for 5 | * the usart.c file 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2021 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* Define to prevent recursive inclusion -------------------------------------*/ 20 | #ifndef __USART_H__ 21 | #define __USART_H__ 22 | 23 | #ifdef __cplusplus 24 | extern "C" { 25 | #endif 26 | 27 | /* Includes ------------------------------------------------------------------*/ 28 | #include "main.h" 29 | 30 | /* USER CODE BEGIN Includes */ 31 | 32 | /* USER CODE END Includes */ 33 | 34 | extern UART_HandleTypeDef huart2; 35 | 36 | /* USER CODE BEGIN Private defines */ 37 | 38 | /* USER CODE END Private defines */ 39 | 40 | void MX_USART2_UART_Init(void); 41 | 42 | /* USER CODE BEGIN Prototypes */ 43 | 44 | /* USER CODE END Prototypes */ 45 | 46 | #ifdef __cplusplus 47 | } 48 | #endif 49 | 50 | #endif /* __USART_H__ */ 51 | 52 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 53 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/MQTTConnectClient.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 "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT connect packet that would be produced using the supplied connect options. 24 | * @param options the options to be used to build the connect packet 25 | * @return the length of buffer needed to contain the serialized version of the packet 26 | */ 27 | int MQTTSerialize_connectLength(MQTTPacket_connectData* options) 28 | { 29 | int len = 0; 30 | 31 | FUNC_ENTRY; 32 | 33 | if (options->MQTTVersion == 3) 34 | len = 12; /* variable depending on MQTT or MQIsdp */ 35 | else if (options->MQTTVersion == 4) 36 | len = 10; 37 | 38 | len += MQTTstrlen(options->clientID)+2; 39 | if (options->willFlag) 40 | len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2; 41 | if (options->username.cstring || options->username.lenstring.data) 42 | len += MQTTstrlen(options->username)+2; 43 | if (options->password.cstring || options->password.lenstring.data) 44 | len += MQTTstrlen(options->password)+2; 45 | 46 | FUNC_EXIT_RC(len); 47 | return len; 48 | } 49 | 50 | 51 | /** 52 | * Serializes the connect options into the buffer. 53 | * @param buf the buffer into which the packet will be serialized 54 | * @param len the length in bytes of the supplied buffer 55 | * @param options the options to be used to build the connect packet 56 | * @return serialized length, or error if 0 57 | */ 58 | int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) 59 | { 60 | unsigned char *ptr = buf; 61 | MQTTHeader header = {0}; 62 | MQTTConnectFlags flags = {0}; 63 | int len = 0; 64 | int rc = -1; 65 | 66 | FUNC_ENTRY; 67 | if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) 68 | { 69 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 70 | goto exit; 71 | } 72 | 73 | header.byte = 0; 74 | header.bits.type = CONNECT; 75 | writeChar(&ptr, header.byte); /* write header */ 76 | 77 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 78 | 79 | if (options->MQTTVersion == 4) 80 | { 81 | writeCString(&ptr, "MQTT"); 82 | writeChar(&ptr, (char) 4); 83 | } 84 | else 85 | { 86 | writeCString(&ptr, "MQIsdp"); 87 | writeChar(&ptr, (char) 3); 88 | } 89 | 90 | flags.all = 0; 91 | flags.bits.cleansession = options->cleansession; 92 | flags.bits.will = (options->willFlag) ? 1 : 0; 93 | if (flags.bits.will) 94 | { 95 | flags.bits.willQoS = options->will.qos; 96 | flags.bits.willRetain = options->will.retained; 97 | } 98 | 99 | if (options->username.cstring || options->username.lenstring.data) 100 | flags.bits.username = 1; 101 | if (options->password.cstring || options->password.lenstring.data) 102 | flags.bits.password = 1; 103 | 104 | writeChar(&ptr, flags.all); 105 | writeInt(&ptr, options->keepAliveInterval); 106 | writeMQTTString(&ptr, options->clientID); 107 | if (options->willFlag) 108 | { 109 | writeMQTTString(&ptr, options->will.topicName); 110 | writeMQTTString(&ptr, options->will.message); 111 | } 112 | if (flags.bits.username) 113 | writeMQTTString(&ptr, options->username); 114 | if (flags.bits.password) 115 | writeMQTTString(&ptr, options->password); 116 | 117 | rc = ptr - buf; 118 | 119 | exit: FUNC_EXIT_RC(rc); 120 | return rc; 121 | } 122 | 123 | 124 | /** 125 | * Deserializes the supplied (wire) buffer into connack data - return code 126 | * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) 127 | * @param connack_rc returned integer value of the connack return code 128 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 129 | * @param len the length in bytes of the data in the supplied buffer 130 | * @return error code. 1 is success, 0 is failure 131 | */ 132 | int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) 133 | { 134 | MQTTHeader header = {0}; 135 | unsigned char* curdata = buf; 136 | unsigned char* enddata = NULL; 137 | int rc = 0; 138 | int mylen; 139 | MQTTConnackFlags flags = {0}; 140 | 141 | FUNC_ENTRY; 142 | header.byte = readChar(&curdata); 143 | if (header.bits.type != CONNACK) 144 | goto exit; 145 | 146 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 147 | enddata = curdata + mylen; 148 | if (enddata - curdata < 2) 149 | goto exit; 150 | 151 | flags.all = readChar(&curdata); 152 | *sessionPresent = flags.bits.sessionpresent; 153 | *connack_rc = readChar(&curdata); 154 | 155 | rc = 1; 156 | exit: 157 | FUNC_EXIT_RC(rc); 158 | return rc; 159 | } 160 | 161 | 162 | /** 163 | * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket 164 | * @param buf the buffer into which the packet will be serialized 165 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 166 | * @param packettype the message type 167 | * @return serialized length, or error if 0 168 | */ 169 | int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) 170 | { 171 | MQTTHeader header = {0}; 172 | int rc = -1; 173 | unsigned char *ptr = buf; 174 | 175 | FUNC_ENTRY; 176 | if (buflen < 2) 177 | { 178 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 179 | goto exit; 180 | } 181 | header.byte = 0; 182 | header.bits.type = packettype; 183 | writeChar(&ptr, header.byte); /* write header */ 184 | 185 | ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */ 186 | rc = ptr - buf; 187 | exit: 188 | FUNC_EXIT_RC(rc); 189 | return rc; 190 | } 191 | 192 | 193 | /** 194 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 195 | * @param buf the buffer into which the packet will be serialized 196 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 197 | * @return serialized length, or error if 0 198 | */ 199 | int MQTTSerialize_disconnect(unsigned char* buf, int buflen) 200 | { 201 | return MQTTSerialize_zero(buf, buflen, DISCONNECT); 202 | } 203 | 204 | 205 | /** 206 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 207 | * @param buf the buffer into which the packet will be serialized 208 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 209 | * @return serialized length, or error if 0 210 | */ 211 | int MQTTSerialize_pingreq(unsigned char* buf, int buflen) 212 | { 213 | return MQTTSerialize_zero(buf, buflen, PINGREQ); 214 | } 215 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/MQTTDeserializePublish.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 | #include 20 | 21 | #define min(a, b) ((a < b) ? 1 : 0) 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into publish data 25 | * @param dup returned integer - the MQTT dup flag 26 | * @param qos returned integer - the MQTT QoS value 27 | * @param retained returned integer - the MQTT retained flag 28 | * @param packetid returned integer - the MQTT packet identifier 29 | * @param topicName returned MQTTString - the MQTT topic in the publish 30 | * @param payload returned byte buffer - the MQTT publish payload 31 | * @param payloadlen returned integer - the length of the MQTT payload 32 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 33 | * @param buflen the length in bytes of the data in the supplied buffer 34 | * @return error code. 1 is success 35 | */ 36 | int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 37 | unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 38 | { 39 | MQTTHeader header = {0}; 40 | unsigned char* curdata = buf; 41 | unsigned char* enddata = NULL; 42 | int rc = 0; 43 | int mylen = 0; 44 | 45 | FUNC_ENTRY; 46 | header.byte = readChar(&curdata); 47 | if (header.bits.type != PUBLISH) 48 | goto exit; 49 | *dup = header.bits.dup; 50 | *qos = header.bits.qos; 51 | *retained = header.bits.retain; 52 | 53 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); 54 | enddata = curdata + mylen; 55 | 56 | if (!readMQTTLenString(topicName, &curdata, enddata) || 57 | enddata - curdata < 0) 58 | goto exit; 59 | 60 | if (*qos > 0) 61 | *packetid = readInt(&curdata); 62 | 63 | *payloadlen = enddata - curdata; 64 | *payload = curdata; 65 | rc = 1; 66 | exit: 67 | FUNC_EXIT_RC(rc); 68 | return rc; 69 | } 70 | 71 | 72 | 73 | /** 74 | * Deserializes the supplied (wire) buffer into an ack 75 | * @param packettype returned integer - the MQTT packet type 76 | * @param dup returned integer - the MQTT dup flag 77 | * @param packetid returned integer - the MQTT packet identifier 78 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 79 | * @param buflen the length in bytes of the data in the supplied buffer 80 | * @return error code. 1 is success, 0 is failure 81 | */ 82 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 83 | { 84 | MQTTHeader header = {0}; 85 | unsigned char* curdata = buf; 86 | unsigned char* enddata = NULL; 87 | int rc = 0; 88 | int mylen; 89 | 90 | FUNC_ENTRY; 91 | header.byte = readChar(&curdata); 92 | *dup = header.bits.dup; 93 | *packettype = header.bits.type; 94 | 95 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 96 | enddata = curdata + mylen; 97 | 98 | if (enddata - curdata < 2) 99 | goto exit; 100 | *packetid = readInt(&curdata); 101 | 102 | rc = 1; 103 | exit: 104 | FUNC_EXIT_RC(rc); 105 | return rc; 106 | } 107 | 108 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/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 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/MQTTSerializePublish.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 | * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144 16 | *******************************************************************************/ 17 | 18 | #include "MQTTPacket.h" 19 | #include "StackTrace.h" 20 | 21 | #include 22 | 23 | 24 | /** 25 | * Determines the length of the MQTT publish packet that would be produced using the supplied parameters 26 | * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0) 27 | * @param topicName the topic name to be used in the publish 28 | * @param payloadlen the length of the payload to be sent 29 | * @return the length of buffer needed to contain the serialized version of the packet 30 | */ 31 | int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen) 32 | { 33 | int len = 0; 34 | 35 | len += 2 + MQTTstrlen(topicName) + payloadlen; 36 | if (qos > 0) 37 | len += 2; /* packetid */ 38 | return len; 39 | } 40 | 41 | 42 | /** 43 | * Serializes the supplied publish data into the supplied buffer, ready for sending 44 | * @param buf the buffer into which the packet will be serialized 45 | * @param buflen the length in bytes of the supplied buffer 46 | * @param dup integer - the MQTT dup flag 47 | * @param qos integer - the MQTT QoS value 48 | * @param retained integer - the MQTT retained flag 49 | * @param packetid integer - the MQTT packet identifier 50 | * @param topicName MQTTString - the MQTT topic in the publish 51 | * @param payload byte buffer - the MQTT publish payload 52 | * @param payloadlen integer - the length of the MQTT payload 53 | * @return the length of the serialized data. <= 0 indicates error 54 | */ 55 | int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 56 | MQTTString topicName, unsigned char* payload, int payloadlen) 57 | { 58 | unsigned char *ptr = buf; 59 | MQTTHeader header = {0}; 60 | int rem_len = 0; 61 | int rc = 0; 62 | 63 | FUNC_ENTRY; 64 | if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen) 65 | { 66 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 67 | goto exit; 68 | } 69 | 70 | header.bits.type = PUBLISH; 71 | header.bits.dup = dup; 72 | header.bits.qos = qos; 73 | header.bits.retain = retained; 74 | writeChar(&ptr, header.byte); /* write header */ 75 | 76 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 77 | 78 | writeMQTTString(&ptr, topicName); 79 | 80 | if (qos > 0) 81 | writeInt(&ptr, packetid); 82 | 83 | memcpy(ptr, payload, payloadlen); 84 | ptr += payloadlen; 85 | 86 | rc = ptr - buf; 87 | 88 | exit: 89 | FUNC_EXIT_RC(rc); 90 | return rc; 91 | } 92 | 93 | 94 | 95 | /** 96 | * Serializes the ack packet into the supplied buffer. 97 | * @param buf the buffer into which the packet will be serialized 98 | * @param buflen the length in bytes of the supplied buffer 99 | * @param type the MQTT packet type 100 | * @param dup the MQTT dup flag 101 | * @param packetid the MQTT packet identifier 102 | * @return serialized length, or error if 0 103 | */ 104 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) 105 | { 106 | MQTTHeader header = {0}; 107 | int rc = 0; 108 | unsigned char *ptr = buf; 109 | 110 | FUNC_ENTRY; 111 | if (buflen < 4) 112 | { 113 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 114 | goto exit; 115 | } 116 | header.bits.type = packettype; 117 | header.bits.dup = dup; 118 | header.bits.qos = (packettype == PUBREL) ? 1 : 0; 119 | writeChar(&ptr, header.byte); /* write header */ 120 | 121 | ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ 122 | writeInt(&ptr, packetid); 123 | rc = ptr - buf; 124 | exit: 125 | FUNC_EXIT_RC(rc); 126 | return rc; 127 | } 128 | 129 | 130 | /** 131 | * Serializes a puback packet into the supplied buffer. 132 | * @param buf the buffer into which the packet will be serialized 133 | * @param buflen the length in bytes of the supplied buffer 134 | * @param packetid integer - the MQTT packet identifier 135 | * @return serialized length, or error if 0 136 | */ 137 | int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) 138 | { 139 | return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid); 140 | } 141 | 142 | 143 | /** 144 | * Serializes a pubrel packet into the supplied buffer. 145 | * @param buf the buffer into which the packet will be serialized 146 | * @param buflen the length in bytes of the supplied buffer 147 | * @param dup integer - the MQTT dup flag 148 | * @param packetid integer - the MQTT packet identifier 149 | * @return serialized length, or error if 0 150 | */ 151 | int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid) 152 | { 153 | return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid); 154 | } 155 | 156 | 157 | /** 158 | * Serializes a pubrel packet into the supplied buffer. 159 | * @param buf the buffer into which the packet will be serialized 160 | * @param buflen the length in bytes of the supplied buffer 161 | * @param packetid integer - the MQTT packet identifier 162 | * @return serialized length, or error if 0 163 | */ 164 | int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid) 165 | { 166 | return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid); 167 | } 168 | 169 | 170 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/MQTTSubscribeClient.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 "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied subscribe data into the supplied buffer, ready for sending 41 | * @param buf the buffer into which the packet will be serialized 42 | * @param buflen the length in bytes of the supplied bufferr 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters and reqQos arrays 46 | * @param topicFilters - array of topic filter names 47 | * @param requestedQoSs - array of requested QoS 48 | * @return the length of the serialized data. <= 0 indicates error 49 | */ 50 | int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, 51 | MQTTString topicFilters[], int requestedQoSs[]) 52 | { 53 | unsigned char *ptr = buf; 54 | MQTTHeader header = {0}; 55 | int rem_len = 0; 56 | int rc = 0; 57 | int i = 0; 58 | 59 | FUNC_ENTRY; 60 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) 61 | { 62 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 63 | goto exit; 64 | } 65 | 66 | header.byte = 0; 67 | header.bits.type = SUBSCRIBE; 68 | header.bits.dup = dup; 69 | header.bits.qos = 1; 70 | writeChar(&ptr, header.byte); /* write header */ 71 | 72 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 73 | 74 | writeInt(&ptr, packetid); 75 | 76 | for (i = 0; i < count; ++i) 77 | { 78 | writeMQTTString(&ptr, topicFilters[i]); 79 | writeChar(&ptr, requestedQoSs[i]); 80 | } 81 | 82 | rc = ptr - buf; 83 | exit: 84 | FUNC_EXIT_RC(rc); 85 | return rc; 86 | } 87 | 88 | 89 | 90 | /** 91 | * Deserializes the supplied (wire) buffer into suback data 92 | * @param packetid returned integer - the MQTT packet identifier 93 | * @param maxcount - the maximum number of members allowed in the grantedQoSs array 94 | * @param count returned integer - number of members in the grantedQoSs array 95 | * @param grantedQoSs returned array of integers - the granted qualities of service 96 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 97 | * @param buflen the length in bytes of the data in the supplied buffer 98 | * @return error code. 1 is success, 0 is failure 99 | */ 100 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen) 101 | { 102 | MQTTHeader header = {0}; 103 | unsigned char* curdata = buf; 104 | unsigned char* enddata = NULL; 105 | int rc = 0; 106 | int mylen; 107 | 108 | FUNC_ENTRY; 109 | header.byte = readChar(&curdata); 110 | if (header.bits.type != SUBACK) 111 | goto exit; 112 | 113 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 114 | enddata = curdata + mylen; 115 | if (enddata - curdata < 2) 116 | goto exit; 117 | 118 | *packetid = readInt(&curdata); 119 | 120 | *count = 0; 121 | while (curdata < enddata) 122 | { 123 | if (*count > maxcount) 124 | { 125 | rc = -1; 126 | goto exit; 127 | } 128 | grantedQoSs[(*count)++] = readChar(&curdata); 129 | } 130 | 131 | rc = 1; 132 | exit: 133 | FUNC_EXIT_RC(rc); 134 | return rc; 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/MQTTUnsubscribeClient.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 "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending 41 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 42 | * @param buflen the length in bytes of the data in the supplied buffer 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters array 46 | * @param topicFilters - array of topic filter names 47 | * @return the length of the serialized data. <= 0 indicates error 48 | */ 49 | int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 50 | int count, MQTTString topicFilters[]) 51 | { 52 | unsigned char *ptr = buf; 53 | MQTTHeader header = {0}; 54 | int rem_len = 0; 55 | int rc = -1; 56 | int i = 0; 57 | 58 | FUNC_ENTRY; 59 | if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) 60 | { 61 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 62 | goto exit; 63 | } 64 | 65 | header.byte = 0; 66 | header.bits.type = UNSUBSCRIBE; 67 | header.bits.dup = dup; 68 | header.bits.qos = 1; 69 | writeChar(&ptr, header.byte); /* write header */ 70 | 71 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 72 | 73 | writeInt(&ptr, packetid); 74 | 75 | for (i = 0; i < count; ++i) 76 | writeMQTTString(&ptr, topicFilters[i]); 77 | 78 | rc = ptr - buf; 79 | exit: 80 | FUNC_EXIT_RC(rc); 81 | return rc; 82 | } 83 | 84 | 85 | /** 86 | * Deserializes the supplied (wire) buffer into unsuback data 87 | * @param packetid returned integer - the MQTT packet identifier 88 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 89 | * @param buflen the length in bytes of the data in the supplied buffer 90 | * @return error code. 1 is success, 0 is failure 91 | */ 92 | int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) 93 | { 94 | unsigned char type = 0; 95 | unsigned char dup = 0; 96 | int rc = 0; 97 | 98 | FUNC_ENTRY; 99 | rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen); 100 | if (type == UNSUBACK) 101 | rc = 1; 102 | FUNC_EXIT_RC(rc); 103 | return rc; 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/gpio.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file gpio.c 4 | * @brief This file provides code for the configuration 5 | * of all used GPIO pins. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2021 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "gpio.h" 22 | 23 | /* USER CODE BEGIN 0 */ 24 | 25 | /* USER CODE END 0 */ 26 | 27 | /*----------------------------------------------------------------------------*/ 28 | /* Configure GPIO */ 29 | /*----------------------------------------------------------------------------*/ 30 | /* USER CODE BEGIN 1 */ 31 | 32 | /* USER CODE END 1 */ 33 | 34 | /** Configure pins as 35 | * Analog 36 | * Input 37 | * Output 38 | * EVENT_OUT 39 | * EXTI 40 | */ 41 | void MX_GPIO_Init(void) 42 | { 43 | 44 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 45 | 46 | /* GPIO Ports Clock Enable */ 47 | __HAL_RCC_GPIOA_CLK_ENABLE(); 48 | __HAL_RCC_GPIOB_CLK_ENABLE(); 49 | 50 | /*Configure GPIO pin Output Level */ 51 | HAL_GPIO_WritePin(SIM_PWR_GPIO_Port, SIM_PWR_Pin, GPIO_PIN_RESET); 52 | 53 | /*Configure GPIO pin : PtPin */ 54 | GPIO_InitStruct.Pin = SIM_PWR_Pin; 55 | GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; 56 | GPIO_InitStruct.Pull = GPIO_NOPULL; 57 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; 58 | HAL_GPIO_Init(SIM_PWR_GPIO_Port, &GPIO_InitStruct); 59 | 60 | } 61 | 62 | /* USER CODE BEGIN 2 */ 63 | 64 | /* USER CODE END 2 */ 65 | 66 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 67 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/main.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file : main.c 5 | * @brief : Main program body 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2020 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "main.h" 22 | #include "usart.h" 23 | #include "gpio.h" 24 | 25 | /* Private includes ----------------------------------------------------------*/ 26 | /* USER CODE BEGIN Includes */ 27 | #include 28 | #include "MQTTSim800.h" 29 | /* USER CODE END Includes */ 30 | 31 | /* Private typedef -----------------------------------------------------------*/ 32 | /* USER CODE BEGIN PTD */ 33 | 34 | /* USER CODE END PTD */ 35 | 36 | /* Private define ------------------------------------------------------------*/ 37 | /* USER CODE BEGIN PD */ 38 | /* USER CODE END PD */ 39 | 40 | /* Private macro -------------------------------------------------------------*/ 41 | /* USER CODE BEGIN PM */ 42 | 43 | /* USER CODE END PM */ 44 | 45 | /* Private variables ---------------------------------------------------------*/ 46 | 47 | /* USER CODE BEGIN PV */ 48 | SIM800_t SIM800; 49 | /* USER CODE END PV */ 50 | 51 | /* Private function prototypes -----------------------------------------------*/ 52 | void SystemClock_Config(void); 53 | /* USER CODE BEGIN PFP */ 54 | 55 | /* USER CODE END PFP */ 56 | 57 | /* Private user code ---------------------------------------------------------*/ 58 | /* USER CODE BEGIN 0 */ 59 | void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart) { 60 | if (huart == UART_SIM800) { 61 | Sim800_RxCallBack(); 62 | } 63 | } 64 | 65 | /* USER CODE END 0 */ 66 | 67 | /** 68 | * @brief The application entry point. 69 | * @retval int 70 | */ 71 | int main(void) 72 | { 73 | /* USER CODE BEGIN 1 */ 74 | 75 | /* USER CODE END 1 */ 76 | 77 | /* MCU Configuration--------------------------------------------------------*/ 78 | 79 | /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ 80 | HAL_Init(); 81 | 82 | /* USER CODE BEGIN Init */ 83 | 84 | /* USER CODE END Init */ 85 | 86 | /* Configure the system clock */ 87 | SystemClock_Config(); 88 | 89 | /* USER CODE BEGIN SysInit */ 90 | 91 | /* USER CODE END SysInit */ 92 | 93 | /* Initialize all configured peripherals */ 94 | MX_GPIO_Init(); 95 | MX_USART2_UART_Init(); 96 | /* USER CODE BEGIN 2 */ 97 | 98 | // On SIM800 power if use 99 | HAL_GPIO_WritePin(SIM_PWR_GPIO_Port, SIM_PWR_Pin, GPIO_PIN_RESET); 100 | HAL_Delay(3000); 101 | HAL_GPIO_WritePin(SIM_PWR_GPIO_Port, SIM_PWR_Pin, GPIO_PIN_SET); 102 | HAL_Delay(10000); 103 | 104 | // MQQT settings 105 | SIM800.sim.apn = "internet"; 106 | SIM800.sim.apn_user = ""; 107 | SIM800.sim.apn_pass = ""; 108 | SIM800.mqttServer.host = "mqtt.mqtt.ru"; 109 | SIM800.mqttServer.port = 1883; 110 | SIM800.mqttClient.username = "user"; 111 | SIM800.mqttClient.pass = "pass"; 112 | SIM800.mqttClient.clientID = "TestSub"; 113 | SIM800.mqttClient.keepAliveInterval = 120; 114 | 115 | 116 | MQTT_Init(); 117 | 118 | uint8_t sub = 0; 119 | 120 | //Test data 121 | uint8_t pub_uint8 = 1; 122 | uint16_t pub_uint16 = 2; 123 | uint32_t pub_uint32 = 3; 124 | float pub_float = 1.1; 125 | double pub_double = 2.2; 126 | 127 | /* USER CODE END 2 */ 128 | 129 | /* Infinite loop */ 130 | /* USER CODE BEGIN WHILE */ 131 | while (1) { 132 | if (SIM800.mqttServer.connect == 0) { 133 | MQTT_Init(); 134 | sub = 0; 135 | } 136 | if (SIM800.mqttServer.connect == 1) { 137 | if(sub == 0){ 138 | MQTT_Sub("test"); 139 | sub = 1; 140 | } 141 | 142 | MQTT_Pub("STM32/string", "string"); 143 | MQTT_PubUint8("STM32/uint8", pub_uint8); 144 | MQTT_PubUint16("STM32/uint16", pub_uint16); 145 | MQTT_PubUint32("STM32/uint32", pub_uint32); 146 | MQTT_PubFloat("STM32/float", pub_float); 147 | MQTT_PubDouble("STM32/double", pub_double); 148 | 149 | if(SIM800.mqttReceive.newEvent) { 150 | unsigned char *topic = SIM800.mqttReceive.topic; 151 | int payload = atoi(SIM800.mqttReceive.payload); 152 | SIM800.mqttReceive.newEvent = 0; 153 | } 154 | } 155 | HAL_Delay(1000); 156 | /* USER CODE END WHILE */ 157 | 158 | /* USER CODE BEGIN 3 */ 159 | } 160 | /* USER CODE END 3 */ 161 | } 162 | 163 | /** 164 | * @brief System Clock Configuration 165 | * @retval None 166 | */ 167 | void SystemClock_Config(void) 168 | { 169 | RCC_OscInitTypeDef RCC_OscInitStruct = {0}; 170 | RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; 171 | 172 | /** Configure the main internal regulator output voltage 173 | */ 174 | __HAL_RCC_PWR_CLK_ENABLE(); 175 | __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE2); 176 | /** Initializes the RCC Oscillators according to the specified parameters 177 | * in the RCC_OscInitTypeDef structure. 178 | */ 179 | RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; 180 | RCC_OscInitStruct.HSIState = RCC_HSI_ON; 181 | RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; 182 | RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; 183 | if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) 184 | { 185 | Error_Handler(); 186 | } 187 | /** Initializes the CPU, AHB and APB buses clocks 188 | */ 189 | RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK 190 | |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; 191 | RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; 192 | RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; 193 | RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2; 194 | RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; 195 | 196 | if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) 197 | { 198 | Error_Handler(); 199 | } 200 | } 201 | 202 | /* USER CODE BEGIN 4 */ 203 | 204 | /* USER CODE END 4 */ 205 | 206 | /** 207 | * @brief This function is executed in case of error occurrence. 208 | * @retval None 209 | */ 210 | void Error_Handler(void) 211 | { 212 | /* USER CODE BEGIN Error_Handler_Debug */ 213 | /* User can add his own implementation to report the HAL error return state */ 214 | __disable_irq(); 215 | while (1) { 216 | } 217 | /* USER CODE END Error_Handler_Debug */ 218 | } 219 | 220 | #ifdef USE_FULL_ASSERT 221 | /** 222 | * @brief Reports the name of the source file and the source line number 223 | * where the assert_param error has occurred. 224 | * @param file: pointer to the source file name 225 | * @param line: assert_param error line source number 226 | * @retval None 227 | */ 228 | void assert_failed(uint8_t *file, uint32_t line) 229 | { 230 | /* USER CODE BEGIN 6 */ 231 | /* User can add his own implementation to report the file name and line number, 232 | ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ 233 | /* USER CODE END 6 */ 234 | } 235 | #endif /* USE_FULL_ASSERT */ 236 | 237 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 238 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/stm32f4xx_hal_msp.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f4xx_hal_msp.c 5 | * @brief This file provides code for the MSP Initialization 6 | * and de-Initialization codes. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | *

© Copyright (c) 2020 STMicroelectronics. 11 | * All rights reserved.

12 | * 13 | * This software component is licensed by ST under BSD 3-Clause license, 14 | * the "License"; You may not use this file except in compliance with the 15 | * License. You may obtain a copy of the License at: 16 | * opensource.org/licenses/BSD-3-Clause 17 | * 18 | ****************************************************************************** 19 | */ 20 | /* USER CODE END Header */ 21 | 22 | /* Includes ------------------------------------------------------------------*/ 23 | #include "main.h" 24 | /* USER CODE BEGIN Includes */ 25 | 26 | /* USER CODE END Includes */ 27 | 28 | /* Private typedef -----------------------------------------------------------*/ 29 | /* USER CODE BEGIN TD */ 30 | 31 | /* USER CODE END TD */ 32 | 33 | /* Private define ------------------------------------------------------------*/ 34 | /* USER CODE BEGIN Define */ 35 | 36 | /* USER CODE END Define */ 37 | 38 | /* Private macro -------------------------------------------------------------*/ 39 | /* USER CODE BEGIN Macro */ 40 | 41 | /* USER CODE END Macro */ 42 | 43 | /* Private variables ---------------------------------------------------------*/ 44 | /* USER CODE BEGIN PV */ 45 | 46 | /* USER CODE END PV */ 47 | 48 | /* Private function prototypes -----------------------------------------------*/ 49 | /* USER CODE BEGIN PFP */ 50 | 51 | /* USER CODE END PFP */ 52 | 53 | /* External functions --------------------------------------------------------*/ 54 | /* USER CODE BEGIN ExternalFunctions */ 55 | 56 | /* USER CODE END ExternalFunctions */ 57 | 58 | /* USER CODE BEGIN 0 */ 59 | 60 | /* USER CODE END 0 */ 61 | /** 62 | * Initializes the Global MSP. 63 | */ 64 | void HAL_MspInit(void) 65 | { 66 | /* USER CODE BEGIN MspInit 0 */ 67 | 68 | /* USER CODE END MspInit 0 */ 69 | 70 | __HAL_RCC_SYSCFG_CLK_ENABLE(); 71 | __HAL_RCC_PWR_CLK_ENABLE(); 72 | 73 | /* System interrupt init*/ 74 | 75 | /* USER CODE BEGIN MspInit 1 */ 76 | 77 | /* USER CODE END MspInit 1 */ 78 | } 79 | 80 | /* USER CODE BEGIN 1 */ 81 | 82 | /* USER CODE END 1 */ 83 | 84 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 85 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/stm32f4xx_it.c: -------------------------------------------------------------------------------- 1 | /* USER CODE BEGIN Header */ 2 | /** 3 | ****************************************************************************** 4 | * @file stm32f4xx_it.c 5 | * @brief Interrupt Service Routines. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2020 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | /* USER CODE END Header */ 20 | 21 | /* Includes ------------------------------------------------------------------*/ 22 | #include "main.h" 23 | #include "stm32f4xx_it.h" 24 | /* Private includes ----------------------------------------------------------*/ 25 | /* USER CODE BEGIN Includes */ 26 | /* USER CODE END Includes */ 27 | 28 | /* Private typedef -----------------------------------------------------------*/ 29 | /* USER CODE BEGIN TD */ 30 | 31 | /* USER CODE END TD */ 32 | 33 | /* Private define ------------------------------------------------------------*/ 34 | /* USER CODE BEGIN PD */ 35 | 36 | /* USER CODE END PD */ 37 | 38 | /* Private macro -------------------------------------------------------------*/ 39 | /* USER CODE BEGIN PM */ 40 | 41 | /* USER CODE END PM */ 42 | 43 | /* Private variables ---------------------------------------------------------*/ 44 | /* USER CODE BEGIN PV */ 45 | 46 | /* USER CODE END PV */ 47 | 48 | /* Private function prototypes -----------------------------------------------*/ 49 | /* USER CODE BEGIN PFP */ 50 | 51 | /* USER CODE END PFP */ 52 | 53 | /* Private user code ---------------------------------------------------------*/ 54 | /* USER CODE BEGIN 0 */ 55 | 56 | /* USER CODE END 0 */ 57 | 58 | /* External variables --------------------------------------------------------*/ 59 | extern UART_HandleTypeDef huart2; 60 | /* USER CODE BEGIN EV */ 61 | 62 | /* USER CODE END EV */ 63 | 64 | /******************************************************************************/ 65 | /* Cortex-M4 Processor Interruption and Exception Handlers */ 66 | /******************************************************************************/ 67 | /** 68 | * @brief This function handles Non maskable interrupt. 69 | */ 70 | void NMI_Handler(void) 71 | { 72 | /* USER CODE BEGIN NonMaskableInt_IRQn 0 */ 73 | 74 | /* USER CODE END NonMaskableInt_IRQn 0 */ 75 | /* USER CODE BEGIN NonMaskableInt_IRQn 1 */ 76 | while (1) 77 | { 78 | } 79 | /* USER CODE END NonMaskableInt_IRQn 1 */ 80 | } 81 | 82 | /** 83 | * @brief This function handles Hard fault interrupt. 84 | */ 85 | void HardFault_Handler(void) 86 | { 87 | /* USER CODE BEGIN HardFault_IRQn 0 */ 88 | 89 | /* USER CODE END HardFault_IRQn 0 */ 90 | while (1) 91 | { 92 | /* USER CODE BEGIN W1_HardFault_IRQn 0 */ 93 | /* USER CODE END W1_HardFault_IRQn 0 */ 94 | } 95 | } 96 | 97 | /** 98 | * @brief This function handles Memory management fault. 99 | */ 100 | void MemManage_Handler(void) 101 | { 102 | /* USER CODE BEGIN MemoryManagement_IRQn 0 */ 103 | 104 | /* USER CODE END MemoryManagement_IRQn 0 */ 105 | while (1) 106 | { 107 | /* USER CODE BEGIN W1_MemoryManagement_IRQn 0 */ 108 | /* USER CODE END W1_MemoryManagement_IRQn 0 */ 109 | } 110 | } 111 | 112 | /** 113 | * @brief This function handles Pre-fetch fault, memory access fault. 114 | */ 115 | void BusFault_Handler(void) 116 | { 117 | /* USER CODE BEGIN BusFault_IRQn 0 */ 118 | 119 | /* USER CODE END BusFault_IRQn 0 */ 120 | while (1) 121 | { 122 | /* USER CODE BEGIN W1_BusFault_IRQn 0 */ 123 | /* USER CODE END W1_BusFault_IRQn 0 */ 124 | } 125 | } 126 | 127 | /** 128 | * @brief This function handles Undefined instruction or illegal state. 129 | */ 130 | void UsageFault_Handler(void) 131 | { 132 | /* USER CODE BEGIN UsageFault_IRQn 0 */ 133 | 134 | /* USER CODE END UsageFault_IRQn 0 */ 135 | while (1) 136 | { 137 | /* USER CODE BEGIN W1_UsageFault_IRQn 0 */ 138 | /* USER CODE END W1_UsageFault_IRQn 0 */ 139 | } 140 | } 141 | 142 | /** 143 | * @brief This function handles System service call via SWI instruction. 144 | */ 145 | void SVC_Handler(void) 146 | { 147 | /* USER CODE BEGIN SVCall_IRQn 0 */ 148 | 149 | /* USER CODE END SVCall_IRQn 0 */ 150 | /* USER CODE BEGIN SVCall_IRQn 1 */ 151 | 152 | /* USER CODE END SVCall_IRQn 1 */ 153 | } 154 | 155 | /** 156 | * @brief This function handles Debug monitor. 157 | */ 158 | void DebugMon_Handler(void) 159 | { 160 | /* USER CODE BEGIN DebugMonitor_IRQn 0 */ 161 | 162 | /* USER CODE END DebugMonitor_IRQn 0 */ 163 | /* USER CODE BEGIN DebugMonitor_IRQn 1 */ 164 | 165 | /* USER CODE END DebugMonitor_IRQn 1 */ 166 | } 167 | 168 | /** 169 | * @brief This function handles Pendable request for system service. 170 | */ 171 | void PendSV_Handler(void) 172 | { 173 | /* USER CODE BEGIN PendSV_IRQn 0 */ 174 | 175 | /* USER CODE END PendSV_IRQn 0 */ 176 | /* USER CODE BEGIN PendSV_IRQn 1 */ 177 | 178 | /* USER CODE END PendSV_IRQn 1 */ 179 | } 180 | 181 | /** 182 | * @brief This function handles System tick timer. 183 | */ 184 | void SysTick_Handler(void) 185 | { 186 | /* USER CODE BEGIN SysTick_IRQn 0 */ 187 | 188 | /* USER CODE END SysTick_IRQn 0 */ 189 | HAL_IncTick(); 190 | /* USER CODE BEGIN SysTick_IRQn 1 */ 191 | 192 | /* USER CODE END SysTick_IRQn 1 */ 193 | } 194 | 195 | /******************************************************************************/ 196 | /* STM32F4xx Peripheral Interrupt Handlers */ 197 | /* Add here the Interrupt Handlers for the used peripherals. */ 198 | /* For the available peripheral interrupt handler names, */ 199 | /* please refer to the startup file (startup_stm32f4xx.s). */ 200 | /******************************************************************************/ 201 | 202 | /** 203 | * @brief This function handles USART2 global interrupt. 204 | */ 205 | void USART2_IRQHandler(void) 206 | { 207 | /* USER CODE BEGIN USART2_IRQn 0 */ 208 | 209 | /* USER CODE END USART2_IRQn 0 */ 210 | HAL_UART_IRQHandler(&huart2); 211 | /* USER CODE BEGIN USART2_IRQn 1 */ 212 | 213 | /* USER CODE END USART2_IRQn 1 */ 214 | } 215 | 216 | /* USER CODE BEGIN 1 */ 217 | 218 | /* USER CODE END 1 */ 219 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 220 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/syscalls.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file syscalls.c 4 | * @author Auto-generated by STM32CubeIDE 5 | * @brief STM32CubeIDE Minimal System calls file 6 | * 7 | * For more information about which c-functions 8 | * need which of these lowlevel functions 9 | * please consult the Newlib libc-manual 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | *

© Copyright (c) 2020 STMicroelectronics. 14 | * All rights reserved.

15 | * 16 | * This software component is licensed by ST under BSD 3-Clause license, 17 | * the "License"; You may not use this file except in compliance with the 18 | * License. You may obtain a copy of the License at: 19 | * opensource.org/licenses/BSD-3-Clause 20 | * 21 | ****************************************************************************** 22 | */ 23 | 24 | /* Includes */ 25 | #include 26 | #include 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | 34 | 35 | /* Variables */ 36 | //#undef errno 37 | extern int errno; 38 | extern int __io_putchar(int ch) __attribute__((weak)); 39 | extern int __io_getchar(void) __attribute__((weak)); 40 | 41 | register char * stack_ptr asm("sp"); 42 | 43 | char *__env[1] = { 0 }; 44 | char **environ = __env; 45 | 46 | 47 | /* Functions */ 48 | void initialise_monitor_handles() 49 | { 50 | } 51 | 52 | int _getpid(void) 53 | { 54 | return 1; 55 | } 56 | 57 | int _kill(int pid, int sig) 58 | { 59 | errno = EINVAL; 60 | return -1; 61 | } 62 | 63 | void _exit (int status) 64 | { 65 | _kill(status, -1); 66 | while (1) {} /* Make sure we hang here */ 67 | } 68 | 69 | __attribute__((weak)) int _read(int file, char *ptr, int len) 70 | { 71 | int DataIdx; 72 | 73 | for (DataIdx = 0; DataIdx < len; DataIdx++) 74 | { 75 | *ptr++ = __io_getchar(); 76 | } 77 | 78 | return len; 79 | } 80 | 81 | __attribute__((weak)) int _write(int file, char *ptr, int len) 82 | { 83 | int DataIdx; 84 | 85 | for (DataIdx = 0; DataIdx < len; DataIdx++) 86 | { 87 | __io_putchar(*ptr++); 88 | } 89 | return len; 90 | } 91 | 92 | int _close(int file) 93 | { 94 | return -1; 95 | } 96 | 97 | 98 | int _fstat(int file, struct stat *st) 99 | { 100 | st->st_mode = S_IFCHR; 101 | return 0; 102 | } 103 | 104 | int _isatty(int file) 105 | { 106 | return 1; 107 | } 108 | 109 | int _lseek(int file, int ptr, int dir) 110 | { 111 | return 0; 112 | } 113 | 114 | int _open(char *path, int flags, ...) 115 | { 116 | /* Pretend like we always fail */ 117 | return -1; 118 | } 119 | 120 | int _wait(int *status) 121 | { 122 | errno = ECHILD; 123 | return -1; 124 | } 125 | 126 | int _unlink(char *name) 127 | { 128 | errno = ENOENT; 129 | return -1; 130 | } 131 | 132 | int _times(struct tms *buf) 133 | { 134 | return -1; 135 | } 136 | 137 | int _stat(char *file, struct stat *st) 138 | { 139 | st->st_mode = S_IFCHR; 140 | return 0; 141 | } 142 | 143 | int _link(char *old, char *new) 144 | { 145 | errno = EMLINK; 146 | return -1; 147 | } 148 | 149 | int _fork(void) 150 | { 151 | errno = EAGAIN; 152 | return -1; 153 | } 154 | 155 | int _execve(char *name, char **argv, char **env) 156 | { 157 | errno = ENOMEM; 158 | return -1; 159 | } 160 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/sysmem.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file sysmem.c 4 | * @author Generated by STM32CubeIDE 5 | * @brief STM32CubeIDE System Memory calls file 6 | * 7 | * For more information about which C functions 8 | * need which of these lowlevel functions 9 | * please consult the newlib libc manual 10 | ****************************************************************************** 11 | * @attention 12 | * 13 | *

© Copyright (c) 2020 STMicroelectronics. 14 | * All rights reserved.

15 | * 16 | * This software component is licensed by ST under BSD 3-Clause license, 17 | * the "License"; You may not use this file except in compliance with the 18 | * License. You may obtain a copy of the License at: 19 | * opensource.org/licenses/BSD-3-Clause 20 | * 21 | ****************************************************************************** 22 | */ 23 | 24 | /* Includes */ 25 | #include 26 | #include 27 | 28 | /** 29 | * Pointer to the current high watermark of the heap usage 30 | */ 31 | static uint8_t *__sbrk_heap_end = NULL; 32 | 33 | /** 34 | * @brief _sbrk() allocates memory to the newlib heap and is used by malloc 35 | * and others from the C library 36 | * 37 | * @verbatim 38 | * ############################################################################ 39 | * # .data # .bss # newlib heap # MSP stack # 40 | * # # # # Reserved by _Min_Stack_Size # 41 | * ############################################################################ 42 | * ^-- RAM start ^-- _end _estack, RAM end --^ 43 | * @endverbatim 44 | * 45 | * This implementation starts allocating at the '_end' linker symbol 46 | * The '_Min_Stack_Size' linker symbol reserves a memory for the MSP stack 47 | * The implementation considers '_estack' linker symbol to be RAM end 48 | * NOTE: If the MSP stack, at any point during execution, grows larger than the 49 | * reserved size, please increase the '_Min_Stack_Size'. 50 | * 51 | * @param incr Memory size 52 | * @return Pointer to allocated memory 53 | */ 54 | void *_sbrk(ptrdiff_t incr) 55 | { 56 | extern uint8_t _end; /* Symbol defined in the linker script */ 57 | extern uint8_t _estack; /* Symbol defined in the linker script */ 58 | extern uint32_t _Min_Stack_Size; /* Symbol defined in the linker script */ 59 | const uint32_t stack_limit = (uint32_t)&_estack - (uint32_t)&_Min_Stack_Size; 60 | const uint8_t *max_heap = (uint8_t *)stack_limit; 61 | uint8_t *prev_heap_end; 62 | 63 | /* Initialize heap end at first call */ 64 | if (NULL == __sbrk_heap_end) 65 | { 66 | __sbrk_heap_end = &_end; 67 | } 68 | 69 | /* Protect heap from growing into the reserved MSP stack */ 70 | if (__sbrk_heap_end + incr > max_heap) 71 | { 72 | errno = ENOMEM; 73 | return (void *)-1; 74 | } 75 | 76 | prev_heap_end = __sbrk_heap_end; 77 | __sbrk_heap_end += incr; 78 | 79 | return (void *)prev_heap_end; 80 | } 81 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Core/Src/usart.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file usart.c 4 | * @brief This file provides code for the configuration 5 | * of the USART instances. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2021 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Includes ------------------------------------------------------------------*/ 21 | #include "usart.h" 22 | 23 | /* USER CODE BEGIN 0 */ 24 | 25 | /* USER CODE END 0 */ 26 | 27 | UART_HandleTypeDef huart2; 28 | 29 | /* USART2 init function */ 30 | 31 | void MX_USART2_UART_Init(void) 32 | { 33 | 34 | /* USER CODE BEGIN USART2_Init 0 */ 35 | 36 | /* USER CODE END USART2_Init 0 */ 37 | 38 | /* USER CODE BEGIN USART2_Init 1 */ 39 | 40 | /* USER CODE END USART2_Init 1 */ 41 | huart2.Instance = USART2; 42 | huart2.Init.BaudRate = 115200; 43 | huart2.Init.WordLength = UART_WORDLENGTH_8B; 44 | huart2.Init.StopBits = UART_STOPBITS_1; 45 | huart2.Init.Parity = UART_PARITY_NONE; 46 | huart2.Init.Mode = UART_MODE_TX_RX; 47 | huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE; 48 | huart2.Init.OverSampling = UART_OVERSAMPLING_16; 49 | if (HAL_UART_Init(&huart2) != HAL_OK) 50 | { 51 | Error_Handler(); 52 | } 53 | /* USER CODE BEGIN USART2_Init 2 */ 54 | 55 | /* USER CODE END USART2_Init 2 */ 56 | 57 | } 58 | 59 | void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle) 60 | { 61 | 62 | GPIO_InitTypeDef GPIO_InitStruct = {0}; 63 | if(uartHandle->Instance==USART2) 64 | { 65 | /* USER CODE BEGIN USART2_MspInit 0 */ 66 | 67 | /* USER CODE END USART2_MspInit 0 */ 68 | /* USART2 clock enable */ 69 | __HAL_RCC_USART2_CLK_ENABLE(); 70 | 71 | __HAL_RCC_GPIOA_CLK_ENABLE(); 72 | /**USART2 GPIO Configuration 73 | PA2 ------> USART2_TX 74 | PA3 ------> USART2_RX 75 | */ 76 | GPIO_InitStruct.Pin = GPIO_PIN_2|GPIO_PIN_3; 77 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; 78 | GPIO_InitStruct.Pull = GPIO_NOPULL; 79 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; 80 | GPIO_InitStruct.Alternate = GPIO_AF7_USART2; 81 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); 82 | 83 | /* USART2 interrupt Init */ 84 | HAL_NVIC_SetPriority(USART2_IRQn, 0, 0); 85 | HAL_NVIC_EnableIRQ(USART2_IRQn); 86 | /* USER CODE BEGIN USART2_MspInit 1 */ 87 | 88 | /* USER CODE END USART2_MspInit 1 */ 89 | } 90 | } 91 | 92 | void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle) 93 | { 94 | 95 | if(uartHandle->Instance==USART2) 96 | { 97 | /* USER CODE BEGIN USART2_MspDeInit 0 */ 98 | 99 | /* USER CODE END USART2_MspDeInit 0 */ 100 | /* Peripheral clock disable */ 101 | __HAL_RCC_USART2_CLK_DISABLE(); 102 | 103 | /**USART2 GPIO Configuration 104 | PA2 ------> USART2_TX 105 | PA3 ------> USART2_RX 106 | */ 107 | HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3); 108 | 109 | /* USART2 interrupt Deinit */ 110 | HAL_NVIC_DisableIRQ(USART2_IRQn); 111 | /* USER CODE BEGIN USART2_MspDeInit 1 */ 112 | 113 | /* USER CODE END USART2_MspDeInit 1 */ 114 | } 115 | } 116 | 117 | /* USER CODE BEGIN 1 */ 118 | 119 | /* USER CODE END 1 */ 120 | 121 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 122 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leech001/SIM800MQTT/70ccb723988f8a3e4699fd5c0e97d8ddebc4b98b/examples/MQTTSUB/Drivers/CMSIS/Device/ST/STM32F4xx/Include/stm32f4xx.h -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/CMSIS/Device/ST/STM32F4xx/Include/system_stm32f4xx.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file system_stm32f4xx.h 4 | * @author MCD Application Team 5 | * @brief CMSIS Cortex-M4 Device System Source File for STM32F4xx devices. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© COPYRIGHT(c) 2017 STMicroelectronics

10 | * 11 | * Redistribution and use in source and binary forms, with or without modification, 12 | * are permitted provided that the following conditions are met: 13 | * 1. Redistributions of source code must retain the above copyright notice, 14 | * this list of conditions and the following disclaimer. 15 | * 2. Redistributions in binary form must reproduce the above copyright notice, 16 | * this list of conditions and the following disclaimer in the documentation 17 | * and/or other materials provided with the distribution. 18 | * 3. Neither the name of STMicroelectronics nor the names of its contributors 19 | * may be used to endorse or promote products derived from this software 20 | * without specific prior written permission. 21 | * 22 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 23 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 24 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 25 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 26 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 27 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 28 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 29 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 30 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 | * 33 | ****************************************************************************** 34 | */ 35 | 36 | /** @addtogroup CMSIS 37 | * @{ 38 | */ 39 | 40 | /** @addtogroup stm32f4xx_system 41 | * @{ 42 | */ 43 | 44 | /** 45 | * @brief Define to prevent recursive inclusion 46 | */ 47 | #ifndef __SYSTEM_STM32F4XX_H 48 | #define __SYSTEM_STM32F4XX_H 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | /** @addtogroup STM32F4xx_System_Includes 55 | * @{ 56 | */ 57 | 58 | /** 59 | * @} 60 | */ 61 | 62 | 63 | /** @addtogroup STM32F4xx_System_Exported_types 64 | * @{ 65 | */ 66 | /* This variable is updated in three ways: 67 | 1) by calling CMSIS function SystemCoreClockUpdate() 68 | 2) by calling HAL API function HAL_RCC_GetSysClockFreq() 69 | 3) each time HAL_RCC_ClockConfig() is called to configure the system clock frequency 70 | Note: If you use this function to configure the system clock; then there 71 | is no need to call the 2 first functions listed above, since SystemCoreClock 72 | variable is updated automatically. 73 | */ 74 | extern uint32_t SystemCoreClock; /*!< System Clock Frequency (Core Clock) */ 75 | 76 | extern const uint8_t AHBPrescTable[16]; /*!< AHB prescalers table values */ 77 | extern const uint8_t APBPrescTable[8]; /*!< APB prescalers table values */ 78 | 79 | /** 80 | * @} 81 | */ 82 | 83 | /** @addtogroup STM32F4xx_System_Exported_Constants 84 | * @{ 85 | */ 86 | 87 | /** 88 | * @} 89 | */ 90 | 91 | /** @addtogroup STM32F4xx_System_Exported_Macros 92 | * @{ 93 | */ 94 | 95 | /** 96 | * @} 97 | */ 98 | 99 | /** @addtogroup STM32F4xx_System_Exported_Functions 100 | * @{ 101 | */ 102 | 103 | extern void SystemInit(void); 104 | extern void SystemCoreClockUpdate(void); 105 | /** 106 | * @} 107 | */ 108 | 109 | #ifdef __cplusplus 110 | } 111 | #endif 112 | 113 | #endif /*__SYSTEM_STM32F4XX_H */ 114 | 115 | /** 116 | * @} 117 | */ 118 | 119 | /** 120 | * @} 121 | */ 122 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 123 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/CMSIS/Include/cmsis_version.h: -------------------------------------------------------------------------------- 1 | /**************************************************************************//** 2 | * @file cmsis_version.h 3 | * @brief CMSIS Core(M) Version definitions 4 | * @version V5.0.2 5 | * @date 19. April 2017 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2009-2017 ARM Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef __CMSIS_VERSION_H 32 | #define __CMSIS_VERSION_H 33 | 34 | /* CMSIS Version definitions */ 35 | #define __CM_CMSIS_VERSION_MAIN ( 5U) /*!< [31:16] CMSIS Core(M) main version */ 36 | #define __CM_CMSIS_VERSION_SUB ( 1U) /*!< [15:0] CMSIS Core(M) sub version */ 37 | #define __CM_CMSIS_VERSION ((__CM_CMSIS_VERSION_MAIN << 16U) | \ 38 | __CM_CMSIS_VERSION_SUB ) /*!< CMSIS Core(M) version number */ 39 | #endif 40 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/CMSIS/Include/tz_context.h: -------------------------------------------------------------------------------- 1 | /****************************************************************************** 2 | * @file tz_context.h 3 | * @brief Context Management for Armv8-M TrustZone 4 | * @version V1.0.1 5 | * @date 10. January 2018 6 | ******************************************************************************/ 7 | /* 8 | * Copyright (c) 2017-2018 Arm Limited. All rights reserved. 9 | * 10 | * SPDX-License-Identifier: Apache-2.0 11 | * 12 | * Licensed under the Apache License, Version 2.0 (the License); you may 13 | * not use this file except in compliance with the License. 14 | * You may obtain a copy of the License at 15 | * 16 | * www.apache.org/licenses/LICENSE-2.0 17 | * 18 | * Unless required by applicable law or agreed to in writing, software 19 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT 20 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 21 | * See the License for the specific language governing permissions and 22 | * limitations under the License. 23 | */ 24 | 25 | #if defined ( __ICCARM__ ) 26 | #pragma system_include /* treat file as system include file for MISRA check */ 27 | #elif defined (__clang__) 28 | #pragma clang system_header /* treat file as system include file */ 29 | #endif 30 | 31 | #ifndef TZ_CONTEXT_H 32 | #define TZ_CONTEXT_H 33 | 34 | #include 35 | 36 | #ifndef TZ_MODULEID_T 37 | #define TZ_MODULEID_T 38 | /// \details Data type that identifies secure software modules called by a process. 39 | typedef uint32_t TZ_ModuleId_t; 40 | #endif 41 | 42 | /// \details TZ Memory ID identifies an allocated memory slot. 43 | typedef uint32_t TZ_MemoryId_t; 44 | 45 | /// Initialize secure context memory system 46 | /// \return execution status (1: success, 0: error) 47 | uint32_t TZ_InitContextSystem_S (void); 48 | 49 | /// Allocate context memory for calling secure software modules in TrustZone 50 | /// \param[in] module identifies software modules called from non-secure mode 51 | /// \return value != 0 id TrustZone memory slot identifier 52 | /// \return value 0 no memory available or internal error 53 | TZ_MemoryId_t TZ_AllocModuleContext_S (TZ_ModuleId_t module); 54 | 55 | /// Free context memory that was previously allocated with \ref TZ_AllocModuleContext_S 56 | /// \param[in] id TrustZone memory slot identifier 57 | /// \return execution status (1: success, 0: error) 58 | uint32_t TZ_FreeModuleContext_S (TZ_MemoryId_t id); 59 | 60 | /// Load secure context (called on RTOS thread context switch) 61 | /// \param[in] id TrustZone memory slot identifier 62 | /// \return execution status (1: success, 0: error) 63 | uint32_t TZ_LoadContext_S (TZ_MemoryId_t id); 64 | 65 | /// Store secure context (called on RTOS thread context switch) 66 | /// \param[in] id TrustZone memory slot identifier 67 | /// \return execution status (1: success, 0: error) 68 | uint32_t TZ_StoreContext_S (TZ_MemoryId_t id); 69 | 70 | #endif // TZ_CONTEXT_H 71 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_def.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f4xx_hal_def.h 4 | * @author MCD Application Team 5 | * @brief This file contains HAL common defines, enumeration, macros and 6 | * structures definitions. 7 | ****************************************************************************** 8 | * @attention 9 | * 10 | *

© Copyright (c) 2017 STMicroelectronics. 11 | * All rights reserved.

12 | * 13 | * This software component is licensed by ST under BSD 3-Clause license, 14 | * the "License"; You may not use this file except in compliance with the 15 | * License. You may obtain a copy of the License at: 16 | * opensource.org/licenses/BSD-3-Clause 17 | * 18 | ****************************************************************************** 19 | */ 20 | 21 | /* Define to prevent recursive inclusion -------------------------------------*/ 22 | #ifndef __STM32F4xx_HAL_DEF 23 | #define __STM32F4xx_HAL_DEF 24 | 25 | #ifdef __cplusplus 26 | extern "C" { 27 | #endif 28 | 29 | /* Includes ------------------------------------------------------------------*/ 30 | #include "stm32f4xx.h" 31 | #include "Legacy/stm32_hal_legacy.h" 32 | #include 33 | 34 | /* Exported types ------------------------------------------------------------*/ 35 | 36 | /** 37 | * @brief HAL Status structures definition 38 | */ 39 | typedef enum 40 | { 41 | HAL_OK = 0x00U, 42 | HAL_ERROR = 0x01U, 43 | HAL_BUSY = 0x02U, 44 | HAL_TIMEOUT = 0x03U 45 | } HAL_StatusTypeDef; 46 | 47 | /** 48 | * @brief HAL Lock structures definition 49 | */ 50 | typedef enum 51 | { 52 | HAL_UNLOCKED = 0x00U, 53 | HAL_LOCKED = 0x01U 54 | } HAL_LockTypeDef; 55 | 56 | /* Exported macro ------------------------------------------------------------*/ 57 | 58 | #define UNUSED(X) (void)X /* To avoid gcc/g++ warnings */ 59 | 60 | #define HAL_MAX_DELAY 0xFFFFFFFFU 61 | 62 | #define HAL_IS_BIT_SET(REG, BIT) (((REG) & (BIT)) == (BIT)) 63 | #define HAL_IS_BIT_CLR(REG, BIT) (((REG) & (BIT)) == 0U) 64 | 65 | #define __HAL_LINKDMA(__HANDLE__, __PPP_DMA_FIELD__, __DMA_HANDLE__) \ 66 | do{ \ 67 | (__HANDLE__)->__PPP_DMA_FIELD__ = &(__DMA_HANDLE__); \ 68 | (__DMA_HANDLE__).Parent = (__HANDLE__); \ 69 | } while(0U) 70 | 71 | /** @brief Reset the Handle's State field. 72 | * @param __HANDLE__ specifies the Peripheral Handle. 73 | * @note This macro can be used for the following purpose: 74 | * - When the Handle is declared as local variable; before passing it as parameter 75 | * to HAL_PPP_Init() for the first time, it is mandatory to use this macro 76 | * to set to 0 the Handle's "State" field. 77 | * Otherwise, "State" field may have any random value and the first time the function 78 | * HAL_PPP_Init() is called, the low level hardware initialization will be missed 79 | * (i.e. HAL_PPP_MspInit() will not be executed). 80 | * - When there is a need to reconfigure the low level hardware: instead of calling 81 | * HAL_PPP_DeInit() then HAL_PPP_Init(), user can make a call to this macro then HAL_PPP_Init(). 82 | * In this later function, when the Handle's "State" field is set to 0, it will execute the function 83 | * HAL_PPP_MspInit() which will reconfigure the low level hardware. 84 | * @retval None 85 | */ 86 | #define __HAL_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = 0U) 87 | 88 | #if (USE_RTOS == 1U) 89 | /* Reserved for future use */ 90 | #error "USE_RTOS should be 0 in the current HAL release" 91 | #else 92 | #define __HAL_LOCK(__HANDLE__) \ 93 | do{ \ 94 | if((__HANDLE__)->Lock == HAL_LOCKED) \ 95 | { \ 96 | return HAL_BUSY; \ 97 | } \ 98 | else \ 99 | { \ 100 | (__HANDLE__)->Lock = HAL_LOCKED; \ 101 | } \ 102 | }while (0U) 103 | 104 | #define __HAL_UNLOCK(__HANDLE__) \ 105 | do{ \ 106 | (__HANDLE__)->Lock = HAL_UNLOCKED; \ 107 | }while (0U) 108 | #endif /* USE_RTOS */ 109 | 110 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */ 111 | #ifndef __weak 112 | #define __weak __attribute__((weak)) 113 | #endif 114 | #ifndef __packed 115 | #define __packed __attribute__((packed)) 116 | #endif 117 | #elif defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */ 118 | #ifndef __weak 119 | #define __weak __attribute__((weak)) 120 | #endif /* __weak */ 121 | #ifndef __packed 122 | #define __packed __attribute__((__packed__)) 123 | #endif /* __packed */ 124 | #endif /* __GNUC__ */ 125 | 126 | 127 | /* Macro to get variable aligned on 4-bytes, for __ICCARM__ the directive "#pragma data_alignment=4" must be used instead */ 128 | #if defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) /* ARM Compiler V6 */ 129 | #ifndef __ALIGN_BEGIN 130 | #define __ALIGN_BEGIN 131 | #endif 132 | #ifndef __ALIGN_END 133 | #define __ALIGN_END __attribute__ ((aligned (4))) 134 | #endif 135 | #elif defined ( __GNUC__ ) && !defined (__CC_ARM) /* GNU Compiler */ 136 | #ifndef __ALIGN_END 137 | #define __ALIGN_END __attribute__ ((aligned (4))) 138 | #endif /* __ALIGN_END */ 139 | #ifndef __ALIGN_BEGIN 140 | #define __ALIGN_BEGIN 141 | #endif /* __ALIGN_BEGIN */ 142 | #else 143 | #ifndef __ALIGN_END 144 | #define __ALIGN_END 145 | #endif /* __ALIGN_END */ 146 | #ifndef __ALIGN_BEGIN 147 | #if defined (__CC_ARM) /* ARM Compiler V5*/ 148 | #define __ALIGN_BEGIN __align(4) 149 | #elif defined (__ICCARM__) /* IAR Compiler */ 150 | #define __ALIGN_BEGIN 151 | #endif /* __CC_ARM */ 152 | #endif /* __ALIGN_BEGIN */ 153 | #endif /* __GNUC__ */ 154 | 155 | 156 | /** 157 | * @brief __RAM_FUNC definition 158 | */ 159 | #if defined ( __CC_ARM ) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) 160 | /* ARM Compiler V4/V5 and V6 161 | -------------------------- 162 | RAM functions are defined using the toolchain options. 163 | Functions that are executed in RAM should reside in a separate source module. 164 | Using the 'Options for File' dialog you can simply change the 'Code / Const' 165 | area of a module to a memory space in physical RAM. 166 | Available memory areas are declared in the 'Target' tab of the 'Options for Target' 167 | dialog. 168 | */ 169 | #define __RAM_FUNC 170 | 171 | #elif defined ( __ICCARM__ ) 172 | /* ICCARM Compiler 173 | --------------- 174 | RAM functions are defined using a specific toolchain keyword "__ramfunc". 175 | */ 176 | #define __RAM_FUNC __ramfunc 177 | 178 | #elif defined ( __GNUC__ ) 179 | /* GNU Compiler 180 | ------------ 181 | RAM functions are defined using a specific toolchain attribute 182 | "__attribute__((section(".RamFunc")))". 183 | */ 184 | #define __RAM_FUNC __attribute__((section(".RamFunc"))) 185 | 186 | #endif 187 | 188 | /** 189 | * @brief __NOINLINE definition 190 | */ 191 | #if defined ( __CC_ARM ) || (defined (__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)) || defined ( __GNUC__ ) 192 | /* ARM V4/V5 and V6 & GNU Compiler 193 | ------------------------------- 194 | */ 195 | #define __NOINLINE __attribute__ ( (noinline) ) 196 | 197 | #elif defined ( __ICCARM__ ) 198 | /* ICCARM Compiler 199 | --------------- 200 | */ 201 | #define __NOINLINE _Pragma("optimize = no_inline") 202 | 203 | #endif 204 | 205 | #ifdef __cplusplus 206 | } 207 | #endif 208 | 209 | #endif /* ___STM32F4xx_HAL_DEF */ 210 | 211 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 212 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_dma_ex.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f4xx_hal_dma_ex.h 4 | * @author MCD Application Team 5 | * @brief Header file of DMA HAL extension module. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2017 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F4xx_HAL_DMA_EX_H 22 | #define __STM32F4xx_HAL_DMA_EX_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | 28 | /* Includes ------------------------------------------------------------------*/ 29 | #include "stm32f4xx_hal_def.h" 30 | 31 | /** @addtogroup STM32F4xx_HAL_Driver 32 | * @{ 33 | */ 34 | 35 | /** @addtogroup DMAEx 36 | * @{ 37 | */ 38 | 39 | /* Exported types ------------------------------------------------------------*/ 40 | /** @defgroup DMAEx_Exported_Types DMAEx Exported Types 41 | * @brief DMAEx Exported types 42 | * @{ 43 | */ 44 | 45 | /** 46 | * @brief HAL DMA Memory definition 47 | */ 48 | typedef enum 49 | { 50 | MEMORY0 = 0x00U, /*!< Memory 0 */ 51 | MEMORY1 = 0x01U /*!< Memory 1 */ 52 | }HAL_DMA_MemoryTypeDef; 53 | 54 | /** 55 | * @} 56 | */ 57 | 58 | /* Exported functions --------------------------------------------------------*/ 59 | /** @defgroup DMAEx_Exported_Functions DMAEx Exported Functions 60 | * @brief DMAEx Exported functions 61 | * @{ 62 | */ 63 | 64 | /** @defgroup DMAEx_Exported_Functions_Group1 Extended features functions 65 | * @brief Extended features functions 66 | * @{ 67 | */ 68 | 69 | /* IO operation functions *******************************************************/ 70 | HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength); 71 | HAL_StatusTypeDef HAL_DMAEx_MultiBufferStart_IT(DMA_HandleTypeDef *hdma, uint32_t SrcAddress, uint32_t DstAddress, uint32_t SecondMemAddress, uint32_t DataLength); 72 | HAL_StatusTypeDef HAL_DMAEx_ChangeMemory(DMA_HandleTypeDef *hdma, uint32_t Address, HAL_DMA_MemoryTypeDef memory); 73 | 74 | /** 75 | * @} 76 | */ 77 | /** 78 | * @} 79 | */ 80 | 81 | /* Private functions ---------------------------------------------------------*/ 82 | /** @defgroup DMAEx_Private_Functions DMAEx Private Functions 83 | * @brief DMAEx Private functions 84 | * @{ 85 | */ 86 | /** 87 | * @} 88 | */ 89 | 90 | /** 91 | * @} 92 | */ 93 | 94 | /** 95 | * @} 96 | */ 97 | 98 | #ifdef __cplusplus 99 | } 100 | #endif 101 | 102 | #endif /*__STM32F4xx_HAL_DMA_EX_H*/ 103 | 104 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 105 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/STM32F4xx_HAL_Driver/Inc/stm32f4xx_hal_flash_ramfunc.h: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f4xx_hal_flash_ramfunc.h 4 | * @author MCD Application Team 5 | * @brief Header file of FLASH RAMFUNC driver. 6 | ****************************************************************************** 7 | * @attention 8 | * 9 | *

© Copyright (c) 2017 STMicroelectronics. 10 | * All rights reserved.

11 | * 12 | * This software component is licensed by ST under BSD 3-Clause license, 13 | * the "License"; You may not use this file except in compliance with the 14 | * License. You may obtain a copy of the License at: 15 | * opensource.org/licenses/BSD-3-Clause 16 | * 17 | ****************************************************************************** 18 | */ 19 | 20 | /* Define to prevent recursive inclusion -------------------------------------*/ 21 | #ifndef __STM32F4xx_FLASH_RAMFUNC_H 22 | #define __STM32F4xx_FLASH_RAMFUNC_H 23 | 24 | #ifdef __cplusplus 25 | extern "C" { 26 | #endif 27 | #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) ||\ 28 | defined(STM32F412Vx) || defined(STM32F412Rx) || defined(STM32F412Cx) 29 | 30 | /* Includes ------------------------------------------------------------------*/ 31 | #include "stm32f4xx_hal_def.h" 32 | 33 | /** @addtogroup STM32F4xx_HAL_Driver 34 | * @{ 35 | */ 36 | 37 | /** @addtogroup FLASH_RAMFUNC 38 | * @{ 39 | */ 40 | 41 | /* Exported types ------------------------------------------------------------*/ 42 | /* Exported macro ------------------------------------------------------------*/ 43 | /* Exported functions --------------------------------------------------------*/ 44 | /** @addtogroup FLASH_RAMFUNC_Exported_Functions 45 | * @{ 46 | */ 47 | 48 | /** @addtogroup FLASH_RAMFUNC_Exported_Functions_Group1 49 | * @{ 50 | */ 51 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_StopFlashInterfaceClk(void); 52 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_StartFlashInterfaceClk(void); 53 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EnableFlashSleepMode(void); 54 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DisableFlashSleepMode(void); 55 | /** 56 | * @} 57 | */ 58 | 59 | /** 60 | * @} 61 | */ 62 | 63 | /** 64 | * @} 65 | */ 66 | 67 | /** 68 | * @} 69 | */ 70 | 71 | #endif /* STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx */ 72 | #ifdef __cplusplus 73 | } 74 | #endif 75 | 76 | 77 | #endif /* __STM32F4xx_FLASH_RAMFUNC_H */ 78 | 79 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 80 | -------------------------------------------------------------------------------- /examples/MQTTSUB/Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal_flash_ramfunc.c: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file stm32f4xx_hal_flash_ramfunc.c 4 | * @author MCD Application Team 5 | * @brief FLASH RAMFUNC module driver. 6 | * This file provides a FLASH firmware functions which should be 7 | * executed from internal SRAM 8 | * + Stop/Start the flash interface while System Run 9 | * + Enable/Disable the flash sleep while System Run 10 | @verbatim 11 | ============================================================================== 12 | ##### APIs executed from Internal RAM ##### 13 | ============================================================================== 14 | [..] 15 | *** ARM Compiler *** 16 | -------------------- 17 | [..] RAM functions are defined using the toolchain options. 18 | Functions that are be executed in RAM should reside in a separate 19 | source module. Using the 'Options for File' dialog you can simply change 20 | the 'Code / Const' area of a module to a memory space in physical RAM. 21 | Available memory areas are declared in the 'Target' tab of the 22 | Options for Target' dialog. 23 | 24 | *** ICCARM Compiler *** 25 | ----------------------- 26 | [..] RAM functions are defined using a specific toolchain keyword "__ramfunc". 27 | 28 | *** GNU Compiler *** 29 | -------------------- 30 | [..] RAM functions are defined using a specific toolchain attribute 31 | "__attribute__((section(".RamFunc")))". 32 | 33 | @endverbatim 34 | ****************************************************************************** 35 | * @attention 36 | * 37 | *

© Copyright (c) 2017 STMicroelectronics. 38 | * All rights reserved.

39 | * 40 | * This software component is licensed by ST under BSD 3-Clause license, 41 | * the "License"; You may not use this file except in compliance with the 42 | * License. You may obtain a copy of the License at: 43 | * opensource.org/licenses/BSD-3-Clause 44 | * 45 | ****************************************************************************** 46 | */ 47 | 48 | /* Includes ------------------------------------------------------------------*/ 49 | #include "stm32f4xx_hal.h" 50 | 51 | /** @addtogroup STM32F4xx_HAL_Driver 52 | * @{ 53 | */ 54 | 55 | /** @defgroup FLASH_RAMFUNC FLASH RAMFUNC 56 | * @brief FLASH functions executed from RAM 57 | * @{ 58 | */ 59 | #ifdef HAL_FLASH_MODULE_ENABLED 60 | #if defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F411xE) || defined(STM32F446xx) || defined(STM32F412Zx) || defined(STM32F412Vx) || \ 61 | defined(STM32F412Rx) || defined(STM32F412Cx) 62 | 63 | /* Private typedef -----------------------------------------------------------*/ 64 | /* Private define ------------------------------------------------------------*/ 65 | /* Private macro -------------------------------------------------------------*/ 66 | /* Private variables ---------------------------------------------------------*/ 67 | /* Private function prototypes -----------------------------------------------*/ 68 | /* Exported functions --------------------------------------------------------*/ 69 | /** @defgroup FLASH_RAMFUNC_Exported_Functions FLASH RAMFUNC Exported Functions 70 | * @{ 71 | */ 72 | 73 | /** @defgroup FLASH_RAMFUNC_Exported_Functions_Group1 Peripheral features functions executed from internal RAM 74 | * @brief Peripheral Extended features functions 75 | * 76 | @verbatim 77 | 78 | =============================================================================== 79 | ##### ramfunc functions ##### 80 | =============================================================================== 81 | [..] 82 | This subsection provides a set of functions that should be executed from RAM 83 | transfers. 84 | 85 | @endverbatim 86 | * @{ 87 | */ 88 | 89 | /** 90 | * @brief Stop the flash interface while System Run 91 | * @note This mode is only available for STM32F41xxx/STM32F446xx devices. 92 | * @note This mode couldn't be set while executing with the flash itself. 93 | * It should be done with specific routine executed from RAM. 94 | * @retval HAL status 95 | */ 96 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_StopFlashInterfaceClk(void) 97 | { 98 | /* Enable Power ctrl clock */ 99 | __HAL_RCC_PWR_CLK_ENABLE(); 100 | /* Stop the flash interface while System Run */ 101 | SET_BIT(PWR->CR, PWR_CR_FISSR); 102 | 103 | return HAL_OK; 104 | } 105 | 106 | /** 107 | * @brief Start the flash interface while System Run 108 | * @note This mode is only available for STM32F411xx/STM32F446xx devices. 109 | * @note This mode couldn't be set while executing with the flash itself. 110 | * It should be done with specific routine executed from RAM. 111 | * @retval HAL status 112 | */ 113 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_StartFlashInterfaceClk(void) 114 | { 115 | /* Enable Power ctrl clock */ 116 | __HAL_RCC_PWR_CLK_ENABLE(); 117 | /* Start the flash interface while System Run */ 118 | CLEAR_BIT(PWR->CR, PWR_CR_FISSR); 119 | 120 | return HAL_OK; 121 | } 122 | 123 | /** 124 | * @brief Enable the flash sleep while System Run 125 | * @note This mode is only available for STM32F41xxx/STM32F446xx devices. 126 | * @note This mode could n't be set while executing with the flash itself. 127 | * It should be done with specific routine executed from RAM. 128 | * @retval HAL status 129 | */ 130 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_EnableFlashSleepMode(void) 131 | { 132 | /* Enable Power ctrl clock */ 133 | __HAL_RCC_PWR_CLK_ENABLE(); 134 | /* Enable the flash sleep while System Run */ 135 | SET_BIT(PWR->CR, PWR_CR_FMSSR); 136 | 137 | return HAL_OK; 138 | } 139 | 140 | /** 141 | * @brief Disable the flash sleep while System Run 142 | * @note This mode is only available for STM32F41xxx/STM32F446xx devices. 143 | * @note This mode couldn't be set while executing with the flash itself. 144 | * It should be done with specific routine executed from RAM. 145 | * @retval HAL status 146 | */ 147 | __RAM_FUNC HAL_StatusTypeDef HAL_FLASHEx_DisableFlashSleepMode(void) 148 | { 149 | /* Enable Power ctrl clock */ 150 | __HAL_RCC_PWR_CLK_ENABLE(); 151 | /* Disable the flash sleep while System Run */ 152 | CLEAR_BIT(PWR->CR, PWR_CR_FMSSR); 153 | 154 | return HAL_OK; 155 | } 156 | 157 | /** 158 | * @} 159 | */ 160 | 161 | /** 162 | * @} 163 | */ 164 | 165 | #endif /* STM32F410xx || STM32F411xE || STM32F446xx || STM32F412Zx || STM32F412Vx || STM32F412Rx || STM32F412Cx */ 166 | #endif /* HAL_FLASH_MODULE_ENABLED */ 167 | /** 168 | * @} 169 | */ 170 | 171 | /** 172 | * @} 173 | */ 174 | 175 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ 176 | -------------------------------------------------------------------------------- /examples/MQTTSUB/MQTTSUB 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 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | -------------------------------------------------------------------------------- /examples/MQTTSUB/MQTTSUB.ioc: -------------------------------------------------------------------------------- 1 | #MicroXplorer Configuration settings - do not modify 2 | File.Version=6 3 | GPIO.groupedBy= 4 | KeepUserPlacement=false 5 | Mcu.Family=STM32F4 6 | Mcu.IP0=NVIC 7 | Mcu.IP1=RCC 8 | Mcu.IP2=SYS 9 | Mcu.IP3=USART2 10 | Mcu.IPNb=4 11 | Mcu.Name=STM32F401C(B-C)Ux 12 | Mcu.Package=UFQFPN48 13 | Mcu.Pin0=PA2 14 | Mcu.Pin1=PA3 15 | Mcu.Pin2=PB0 16 | Mcu.Pin3=PA13 17 | Mcu.Pin4=PA14 18 | Mcu.Pin5=VP_SYS_VS_Systick 19 | Mcu.PinsNb=6 20 | Mcu.ThirdPartyNb=0 21 | Mcu.UserConstants= 22 | Mcu.UserName=STM32F401CCUx 23 | MxCube.Version=6.2.1 24 | MxDb.Version=DB.6.0.21 25 | NVIC.BusFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false 26 | NVIC.DebugMonitor_IRQn=true\:0\:0\:false\:false\:true\:false\:false 27 | NVIC.ForceEnableDMAVector=true 28 | NVIC.HardFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false 29 | NVIC.MemoryManagement_IRQn=true\:0\:0\:false\:false\:true\:false\:false 30 | NVIC.NonMaskableInt_IRQn=true\:0\:0\:false\:false\:true\:false\:false 31 | NVIC.PendSV_IRQn=true\:0\:0\:false\:false\:true\:false\:false 32 | NVIC.PriorityGroup=NVIC_PRIORITYGROUP_4 33 | NVIC.SVCall_IRQn=true\:0\:0\:false\:false\:true\:false\:false 34 | NVIC.SysTick_IRQn=true\:0\:0\:false\:false\:true\:false\:true 35 | NVIC.USART2_IRQn=true\:0\:0\:false\:false\:true\:true\:true 36 | NVIC.UsageFault_IRQn=true\:0\:0\:false\:false\:true\:false\:false 37 | PA13.Mode=Serial_Wire 38 | PA13.Signal=SYS_JTMS-SWDIO 39 | PA14.Mode=Serial_Wire 40 | PA14.Signal=SYS_JTCK-SWCLK 41 | PA2.Mode=Asynchronous 42 | PA2.Signal=USART2_TX 43 | PA3.Mode=Asynchronous 44 | PA3.Signal=USART2_RX 45 | PB0.GPIOParameters=GPIO_Label 46 | PB0.GPIO_Label=SIM_PWR 47 | PB0.Locked=true 48 | PB0.Signal=GPIO_Output 49 | PinOutPanel.RotationAngle=0 50 | ProjectManager.AskForMigrate=true 51 | ProjectManager.BackupPrevious=false 52 | ProjectManager.CompilerOptimize=6 53 | ProjectManager.ComputerToolchain=false 54 | ProjectManager.CoupleFile=true 55 | ProjectManager.CustomerFirmwarePackage= 56 | ProjectManager.DefaultFWLocation=true 57 | ProjectManager.DeletePrevious=true 58 | ProjectManager.DeviceId=STM32F401CCUx 59 | ProjectManager.FirmwarePackage=STM32Cube FW_F4 V1.26.1 60 | ProjectManager.FreePins=false 61 | ProjectManager.HalAssertFull=false 62 | ProjectManager.HeapSize=0x200 63 | ProjectManager.KeepUserCode=true 64 | ProjectManager.LastFirmware=true 65 | ProjectManager.LibraryCopy=1 66 | ProjectManager.MainLocation=Core/Src 67 | ProjectManager.NoMain=false 68 | ProjectManager.PreviousToolchain=STM32CubeIDE 69 | ProjectManager.ProjectBuild=false 70 | ProjectManager.ProjectFileName=MQTTSUB.ioc 71 | ProjectManager.ProjectName=MQTTSUB 72 | ProjectManager.RegisterCallBack= 73 | ProjectManager.StackSize=0x400 74 | ProjectManager.TargetToolchain=STM32CubeIDE 75 | ProjectManager.ToolChainLocation= 76 | ProjectManager.UnderRoot=true 77 | ProjectManager.functionlistsort=1-MX_GPIO_Init-GPIO-false-HAL-true,2-SystemClock_Config-RCC-false-HAL-false,3-MX_USART2_UART_Init-USART2-false-HAL-true 78 | RCC.48MHZClocksFreq_Value=26880000 79 | RCC.AHBFreq_Value=16000000 80 | RCC.APB1CLKDivider=RCC_HCLK_DIV2 81 | RCC.APB1Freq_Value=8000000 82 | RCC.APB1TimFreq_Value=16000000 83 | RCC.APB2Freq_Value=16000000 84 | RCC.APB2TimFreq_Value=16000000 85 | RCC.CortexFreq_Value=16000000 86 | RCC.FCLKCortexFreq_Value=16000000 87 | RCC.HCLKFreq_Value=16000000 88 | RCC.HSE_VALUE=25000000 89 | RCC.HSI_VALUE=16000000 90 | RCC.I2SClocksFreq_Value=61440000 91 | RCC.IPParameters=48MHZClocksFreq_Value,AHBFreq_Value,APB1CLKDivider,APB1Freq_Value,APB1TimFreq_Value,APB2Freq_Value,APB2TimFreq_Value,CortexFreq_Value,FCLKCortexFreq_Value,HCLKFreq_Value,HSE_VALUE,HSI_VALUE,I2SClocksFreq_Value,LSE_VALUE,LSI_VALUE,PLLCLKFreq_Value,PLLM,PLLN,PLLQCLKFreq_Value,RTCFreq_Value,RTCHSEDivFreq_Value,SYSCLKFreq_VALUE,VCOI2SOutputFreq_Value,VCOInputFreq_Value,VCOOutputFreq_Value,VcooutputI2S 92 | RCC.LSE_VALUE=32768 93 | RCC.LSI_VALUE=32000 94 | RCC.PLLCLKFreq_Value=53760000 95 | RCC.PLLM=25 96 | RCC.PLLN=168 97 | RCC.PLLQCLKFreq_Value=26880000 98 | RCC.RTCFreq_Value=32000 99 | RCC.RTCHSEDivFreq_Value=12500000 100 | RCC.SYSCLKFreq_VALUE=16000000 101 | RCC.VCOI2SOutputFreq_Value=122880000 102 | RCC.VCOInputFreq_Value=640000 103 | RCC.VCOOutputFreq_Value=107520000 104 | RCC.VcooutputI2S=61440000 105 | USART2.IPParameters=VirtualMode 106 | USART2.VirtualMode=VM_ASYNC 107 | VP_SYS_VS_Systick.Mode=SysTick 108 | VP_SYS_VS_Systick.Signal=SYS_VS_Systick 109 | board=custom 110 | -------------------------------------------------------------------------------- /examples/MQTTSUB/STM32F401CCUX_FLASH.ld: -------------------------------------------------------------------------------- 1 | /** 2 | ****************************************************************************** 3 | * @file LinkerScript.ld 4 | * @author Auto-generated by STM32CubeIDE 5 | * @brief Linker script for STM32F401CCUx Device from STM32F4 series 6 | * 256Kbytes FLASH 7 | * 64Kbytes RAM 8 | * 9 | * Set heap size, stack size and stack location according 10 | * to application requirements. 11 | * 12 | * Set memory bank area and size if external memory is used 13 | ****************************************************************************** 14 | * @attention 15 | * 16 | *

© Copyright (c) 2020 STMicroelectronics. 17 | * All rights reserved.

18 | * 19 | * This software component is licensed by ST under BSD 3-Clause license, 20 | * the "License"; You may not use this file except in compliance with the 21 | * License. You may obtain a copy of the License at: 22 | * opensource.org/licenses/BSD-3-Clause 23 | * 24 | ****************************************************************************** 25 | */ 26 | 27 | /* Entry Point */ 28 | ENTRY(Reset_Handler) 29 | 30 | /* Highest address of the user mode stack */ 31 | _estack = ORIGIN(RAM) + LENGTH(RAM); /* end of "RAM" Ram type memory */ 32 | 33 | _Min_Heap_Size = 0x200 ; /* required amount of heap */ 34 | _Min_Stack_Size = 0x400 ; /* required amount of stack */ 35 | 36 | /* Memories definition */ 37 | MEMORY 38 | { 39 | RAM (xrw) : ORIGIN = 0x20000000, LENGTH = 64K 40 | FLASH (rx) : ORIGIN = 0x8000000, LENGTH = 256K 41 | } 42 | 43 | /* Sections */ 44 | SECTIONS 45 | { 46 | /* The startup code into "FLASH" Rom type memory */ 47 | .isr_vector : 48 | { 49 | . = ALIGN(4); 50 | KEEP(*(.isr_vector)) /* Startup code */ 51 | . = ALIGN(4); 52 | } >FLASH 53 | 54 | /* The program code and other data into "FLASH" Rom type memory */ 55 | .text : 56 | { 57 | . = ALIGN(4); 58 | *(.text) /* .text sections (code) */ 59 | *(.text*) /* .text* sections (code) */ 60 | *(.glue_7) /* glue arm to thumb code */ 61 | *(.glue_7t) /* glue thumb to arm code */ 62 | *(.eh_frame) 63 | 64 | KEEP (*(.init)) 65 | KEEP (*(.fini)) 66 | 67 | . = ALIGN(4); 68 | _etext = .; /* define a global symbols at end of code */ 69 | } >FLASH 70 | 71 | /* Constant data into "FLASH" Rom type memory */ 72 | .rodata : 73 | { 74 | . = ALIGN(4); 75 | *(.rodata) /* .rodata sections (constants, strings, etc.) */ 76 | *(.rodata*) /* .rodata* sections (constants, strings, etc.) */ 77 | . = ALIGN(4); 78 | } >FLASH 79 | 80 | .ARM.extab : { 81 | . = ALIGN(4); 82 | *(.ARM.extab* .gnu.linkonce.armextab.*) 83 | . = ALIGN(4); 84 | } >FLASH 85 | 86 | .ARM : { 87 | . = ALIGN(4); 88 | __exidx_start = .; 89 | *(.ARM.exidx*) 90 | __exidx_end = .; 91 | . = ALIGN(4); 92 | } >FLASH 93 | 94 | .preinit_array : 95 | { 96 | . = ALIGN(4); 97 | PROVIDE_HIDDEN (__preinit_array_start = .); 98 | KEEP (*(.preinit_array*)) 99 | PROVIDE_HIDDEN (__preinit_array_end = .); 100 | . = ALIGN(4); 101 | } >FLASH 102 | 103 | .init_array : 104 | { 105 | . = ALIGN(4); 106 | PROVIDE_HIDDEN (__init_array_start = .); 107 | KEEP (*(SORT(.init_array.*))) 108 | KEEP (*(.init_array*)) 109 | PROVIDE_HIDDEN (__init_array_end = .); 110 | . = ALIGN(4); 111 | } >FLASH 112 | 113 | .fini_array : 114 | { 115 | . = ALIGN(4); 116 | PROVIDE_HIDDEN (__fini_array_start = .); 117 | KEEP (*(SORT(.fini_array.*))) 118 | KEEP (*(.fini_array*)) 119 | PROVIDE_HIDDEN (__fini_array_end = .); 120 | . = ALIGN(4); 121 | } >FLASH 122 | 123 | /* Used by the startup to initialize data */ 124 | _sidata = LOADADDR(.data); 125 | 126 | /* Initialized data sections into "RAM" Ram type memory */ 127 | .data : 128 | { 129 | . = ALIGN(4); 130 | _sdata = .; /* create a global symbol at data start */ 131 | *(.data) /* .data sections */ 132 | *(.data*) /* .data* sections */ 133 | 134 | . = ALIGN(4); 135 | _edata = .; /* define a global symbol at data end */ 136 | 137 | } >RAM AT> FLASH 138 | 139 | /* Uninitialized data section into "RAM" Ram type memory */ 140 | . = ALIGN(4); 141 | .bss : 142 | { 143 | /* This is used by the startup in order to initialize the .bss section */ 144 | _sbss = .; /* define a global symbol at bss start */ 145 | __bss_start__ = _sbss; 146 | *(.bss) 147 | *(.bss*) 148 | *(COMMON) 149 | 150 | . = ALIGN(4); 151 | _ebss = .; /* define a global symbol at bss end */ 152 | __bss_end__ = _ebss; 153 | } >RAM 154 | 155 | /* User_heap_stack section, used to check that there is enough "RAM" Ram type memory left */ 156 | ._user_heap_stack : 157 | { 158 | . = ALIGN(8); 159 | PROVIDE ( end = . ); 160 | PROVIDE ( _end = . ); 161 | . = . + _Min_Heap_Size; 162 | . = . + _Min_Stack_Size; 163 | . = ALIGN(8); 164 | } >RAM 165 | 166 | /* Remove information from the compiler libraries */ 167 | /DISCARD/ : 168 | { 169 | libc.a ( * ) 170 | libm.a ( * ) 171 | libgcc.a ( * ) 172 | } 173 | 174 | .ARM.attributes 0 : { *(.ARM.attributes) } 175 | } 176 | -------------------------------------------------------------------------------- /examples/MQTTSUB/stm32f4discovery(stlink).cfg: -------------------------------------------------------------------------------- 1 | # This is an STM32F4 discovery board with a single STM32F407VGT6 chip. 2 | # http://www.st.com/internet/evalboard/product/252419.jsp 3 | 4 | source [find interface/stlink.cfg] 5 | 6 | transport select hla_swd 7 | 8 | # increase working area to 64KB 9 | set WORKAREASIZE 0x10000 10 | 11 | source [find target/stm32f4x.cfg] 12 | 13 | -------------------------------------------------------------------------------- /ext lib/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 MQTTSerialize_pingreq(unsigned char* buf, int buflen); 147 | 148 | #endif /* MQTTCONNECT_H_ */ 149 | -------------------------------------------------------------------------------- /ext lib/MQTTConnectClient.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 "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT connect packet that would be produced using the supplied connect options. 24 | * @param options the options to be used to build the connect packet 25 | * @return the length of buffer needed to contain the serialized version of the packet 26 | */ 27 | int MQTTSerialize_connectLength(MQTTPacket_connectData* options) 28 | { 29 | int len = 0; 30 | 31 | FUNC_ENTRY; 32 | 33 | if (options->MQTTVersion == 3) 34 | len = 12; /* variable depending on MQTT or MQIsdp */ 35 | else if (options->MQTTVersion == 4) 36 | len = 10; 37 | 38 | len += MQTTstrlen(options->clientID)+2; 39 | if (options->willFlag) 40 | len += MQTTstrlen(options->will.topicName)+2 + MQTTstrlen(options->will.message)+2; 41 | if (options->username.cstring || options->username.lenstring.data) 42 | len += MQTTstrlen(options->username)+2; 43 | if (options->password.cstring || options->password.lenstring.data) 44 | len += MQTTstrlen(options->password)+2; 45 | 46 | FUNC_EXIT_RC(len); 47 | return len; 48 | } 49 | 50 | 51 | /** 52 | * Serializes the connect options into the buffer. 53 | * @param buf the buffer into which the packet will be serialized 54 | * @param len the length in bytes of the supplied buffer 55 | * @param options the options to be used to build the connect packet 56 | * @return serialized length, or error if 0 57 | */ 58 | int MQTTSerialize_connect(unsigned char* buf, int buflen, MQTTPacket_connectData* options) 59 | { 60 | unsigned char *ptr = buf; 61 | MQTTHeader header = {0}; 62 | MQTTConnectFlags flags = {0}; 63 | int len = 0; 64 | int rc = -1; 65 | 66 | FUNC_ENTRY; 67 | if (MQTTPacket_len(len = MQTTSerialize_connectLength(options)) > buflen) 68 | { 69 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 70 | goto exit; 71 | } 72 | 73 | header.byte = 0; 74 | header.bits.type = CONNECT; 75 | writeChar(&ptr, header.byte); /* write header */ 76 | 77 | ptr += MQTTPacket_encode(ptr, len); /* write remaining length */ 78 | 79 | if (options->MQTTVersion == 4) 80 | { 81 | writeCString(&ptr, "MQTT"); 82 | writeChar(&ptr, (char) 4); 83 | } 84 | else 85 | { 86 | writeCString(&ptr, "MQIsdp"); 87 | writeChar(&ptr, (char) 3); 88 | } 89 | 90 | flags.all = 0; 91 | flags.bits.cleansession = options->cleansession; 92 | flags.bits.will = (options->willFlag) ? 1 : 0; 93 | if (flags.bits.will) 94 | { 95 | flags.bits.willQoS = options->will.qos; 96 | flags.bits.willRetain = options->will.retained; 97 | } 98 | 99 | if (options->username.cstring || options->username.lenstring.data) 100 | flags.bits.username = 1; 101 | if (options->password.cstring || options->password.lenstring.data) 102 | flags.bits.password = 1; 103 | 104 | writeChar(&ptr, flags.all); 105 | writeInt(&ptr, options->keepAliveInterval); 106 | writeMQTTString(&ptr, options->clientID); 107 | if (options->willFlag) 108 | { 109 | writeMQTTString(&ptr, options->will.topicName); 110 | writeMQTTString(&ptr, options->will.message); 111 | } 112 | if (flags.bits.username) 113 | writeMQTTString(&ptr, options->username); 114 | if (flags.bits.password) 115 | writeMQTTString(&ptr, options->password); 116 | 117 | rc = ptr - buf; 118 | 119 | exit: FUNC_EXIT_RC(rc); 120 | return rc; 121 | } 122 | 123 | 124 | /** 125 | * Deserializes the supplied (wire) buffer into connack data - return code 126 | * @param sessionPresent the session present flag returned (only for MQTT 3.1.1) 127 | * @param connack_rc returned integer value of the connack return code 128 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 129 | * @param len the length in bytes of the data in the supplied buffer 130 | * @return error code. 1 is success, 0 is failure 131 | */ 132 | int MQTTDeserialize_connack(unsigned char* sessionPresent, unsigned char* connack_rc, unsigned char* buf, int buflen) 133 | { 134 | MQTTHeader header = {0}; 135 | unsigned char* curdata = buf; 136 | unsigned char* enddata = NULL; 137 | int rc = 0; 138 | int mylen; 139 | MQTTConnackFlags flags = {0}; 140 | 141 | FUNC_ENTRY; 142 | header.byte = readChar(&curdata); 143 | if (header.bits.type != CONNACK) 144 | goto exit; 145 | 146 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 147 | enddata = curdata + mylen; 148 | if (enddata - curdata < 2) 149 | goto exit; 150 | 151 | flags.all = readChar(&curdata); 152 | *sessionPresent = flags.bits.sessionpresent; 153 | *connack_rc = readChar(&curdata); 154 | 155 | rc = 1; 156 | exit: 157 | FUNC_EXIT_RC(rc); 158 | return rc; 159 | } 160 | 161 | 162 | /** 163 | * Serializes a 0-length packet into the supplied buffer, ready for writing to a socket 164 | * @param buf the buffer into which the packet will be serialized 165 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 166 | * @param packettype the message type 167 | * @return serialized length, or error if 0 168 | */ 169 | int MQTTSerialize_zero(unsigned char* buf, int buflen, unsigned char packettype) 170 | { 171 | MQTTHeader header = {0}; 172 | int rc = -1; 173 | unsigned char *ptr = buf; 174 | 175 | FUNC_ENTRY; 176 | if (buflen < 2) 177 | { 178 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 179 | goto exit; 180 | } 181 | header.byte = 0; 182 | header.bits.type = packettype; 183 | writeChar(&ptr, header.byte); /* write header */ 184 | 185 | ptr += MQTTPacket_encode(ptr, 0); /* write remaining length */ 186 | rc = ptr - buf; 187 | exit: 188 | FUNC_EXIT_RC(rc); 189 | return rc; 190 | } 191 | 192 | 193 | /** 194 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 195 | * @param buf the buffer into which the packet will be serialized 196 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 197 | * @return serialized length, or error if 0 198 | */ 199 | int MQTTSerialize_disconnect(unsigned char* buf, int buflen) 200 | { 201 | return MQTTSerialize_zero(buf, buflen, DISCONNECT); 202 | } 203 | 204 | 205 | /** 206 | * Serializes a disconnect packet into the supplied buffer, ready for writing to a socket 207 | * @param buf the buffer into which the packet will be serialized 208 | * @param buflen the length in bytes of the supplied buffer, to avoid overruns 209 | * @return serialized length, or error if 0 210 | */ 211 | int MQTTSerialize_pingreq(unsigned char* buf, int buflen) 212 | { 213 | return MQTTSerialize_zero(buf, buflen, PINGREQ); 214 | } 215 | -------------------------------------------------------------------------------- /ext lib/MQTTDeserializePublish.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 | #include 20 | 21 | #define min(a, b) ((a < b) ? 1 : 0) 22 | 23 | /** 24 | * Deserializes the supplied (wire) buffer into publish data 25 | * @param dup returned integer - the MQTT dup flag 26 | * @param qos returned integer - the MQTT QoS value 27 | * @param retained returned integer - the MQTT retained flag 28 | * @param packetid returned integer - the MQTT packet identifier 29 | * @param topicName returned MQTTString - the MQTT topic in the publish 30 | * @param payload returned byte buffer - the MQTT publish payload 31 | * @param payloadlen returned integer - the length of the MQTT payload 32 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 33 | * @param buflen the length in bytes of the data in the supplied buffer 34 | * @return error code. 1 is success 35 | */ 36 | int MQTTDeserialize_publish(unsigned char* dup, int* qos, unsigned char* retained, unsigned short* packetid, MQTTString* topicName, 37 | unsigned char** payload, int* payloadlen, unsigned char* buf, int buflen) 38 | { 39 | MQTTHeader header = {0}; 40 | unsigned char* curdata = buf; 41 | unsigned char* enddata = NULL; 42 | int rc = 0; 43 | int mylen = 0; 44 | 45 | FUNC_ENTRY; 46 | header.byte = readChar(&curdata); 47 | if (header.bits.type != PUBLISH) 48 | goto exit; 49 | *dup = header.bits.dup; 50 | *qos = header.bits.qos; 51 | *retained = header.bits.retain; 52 | 53 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); 54 | enddata = curdata + mylen; 55 | 56 | if (!readMQTTLenString(topicName, &curdata, enddata) || 57 | enddata - curdata < 0) 58 | goto exit; 59 | 60 | if (*qos > 0) 61 | *packetid = readInt(&curdata); 62 | 63 | *payloadlen = enddata - curdata; 64 | *payload = curdata; 65 | rc = 1; 66 | exit: 67 | FUNC_EXIT_RC(rc); 68 | return rc; 69 | } 70 | 71 | 72 | 73 | /** 74 | * Deserializes the supplied (wire) buffer into an ack 75 | * @param packettype returned integer - the MQTT packet type 76 | * @param dup returned integer - the MQTT dup flag 77 | * @param packetid returned integer - the MQTT packet identifier 78 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 79 | * @param buflen the length in bytes of the data in the supplied buffer 80 | * @return error code. 1 is success, 0 is failure 81 | */ 82 | int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen) 83 | { 84 | MQTTHeader header = {0}; 85 | unsigned char* curdata = buf; 86 | unsigned char* enddata = NULL; 87 | int rc = 0; 88 | int mylen; 89 | 90 | FUNC_ENTRY; 91 | header.byte = readChar(&curdata); 92 | *dup = header.bits.dup; 93 | *packettype = header.bits.type; 94 | 95 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 96 | enddata = curdata + mylen; 97 | 98 | if (enddata - curdata < 2) 99 | goto exit; 100 | *packetid = readInt(&curdata); 101 | 102 | rc = 1; 103 | exit: 104 | FUNC_EXIT_RC(rc); 105 | return rc; 106 | } 107 | 108 | -------------------------------------------------------------------------------- /ext lib/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 | -------------------------------------------------------------------------------- /ext lib/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 | -------------------------------------------------------------------------------- /ext lib/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 | }; 49 | 50 | /** 51 | * Bitfields for the MQTT header byte. 52 | */ 53 | typedef union 54 | { 55 | unsigned char byte; /**< the whole byte */ 56 | #if defined(REVERSED) 57 | struct 58 | { 59 | unsigned int type : 4; /**< message type nibble */ 60 | unsigned int dup : 1; /**< DUP flag bit */ 61 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 62 | unsigned int retain : 1; /**< retained flag bit */ 63 | } bits; 64 | #else 65 | struct 66 | { 67 | unsigned int retain : 1; /**< retained flag bit */ 68 | unsigned int qos : 2; /**< QoS value, 0, 1 or 2 */ 69 | unsigned int dup : 1; /**< DUP flag bit */ 70 | unsigned int type : 4; /**< message type nibble */ 71 | } bits; 72 | #endif 73 | } MQTTHeader; 74 | 75 | typedef struct 76 | { 77 | int len; 78 | char* data; 79 | } MQTTLenString; 80 | 81 | typedef struct 82 | { 83 | char* cstring; 84 | MQTTLenString lenstring; 85 | } MQTTString; 86 | 87 | #define MQTTString_initializer {NULL, {0, NULL}} 88 | 89 | int MQTTstrlen(MQTTString mqttstring); 90 | 91 | #include "MQTTConnect.h" 92 | #include "MQTTPublish.h" 93 | #include "MQTTSubscribe.h" 94 | #include "MQTTUnsubscribe.h" 95 | #include "MQTTFormat.h" 96 | 97 | DLLExport int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char type, unsigned char dup, unsigned short packetid); 98 | DLLExport int MQTTDeserialize_ack(unsigned char* packettype, unsigned char* dup, unsigned short* packetid, unsigned char* buf, int buflen); 99 | 100 | int MQTTPacket_len(int rem_len); 101 | DLLExport int MQTTPacket_equals(MQTTString* a, char* b); 102 | 103 | DLLExport int MQTTPacket_encode(unsigned char* buf, int length); 104 | int MQTTPacket_decode(int (*getcharfn)(unsigned char*, int), int* value); 105 | int MQTTPacket_decodeBuf(unsigned char* buf, int* value); 106 | 107 | int readInt(unsigned char** pptr); 108 | char readChar(unsigned char** pptr); 109 | void writeChar(unsigned char** pptr, char c); 110 | void writeInt(unsigned char** pptr, int anInt); 111 | int readMQTTLenString(MQTTString* mqttstring, unsigned char** pptr, unsigned char* enddata); 112 | void writeCString(unsigned char** pptr, const char* string); 113 | void writeMQTTString(unsigned char** pptr, MQTTString mqttstring); 114 | 115 | DLLExport int MQTTPacket_read(unsigned char* buf, int buflen, int (*getfn)(unsigned char*, int)); 116 | 117 | typedef struct { 118 | int (*getfn)(void *, unsigned char*, int); /* must return -1 for error, 0 for call again, or the number of bytes read */ 119 | void *sck; /* pointer to whatever the system may use to identify the transport */ 120 | int multiplier; 121 | int rem_len; 122 | int len; 123 | char state; 124 | }MQTTTransport; 125 | 126 | int MQTTPacket_readnb(unsigned char* buf, int buflen, MQTTTransport *trp); 127 | 128 | #ifdef __cplusplus /* If this is a C++ compiler, use C linkage */ 129 | } 130 | #endif 131 | 132 | 133 | #endif /* MQTTPACKET_H_ */ 134 | -------------------------------------------------------------------------------- /ext lib/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 | -------------------------------------------------------------------------------- /ext lib/MQTTSerializePublish.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 | * Ian Craggs - fix for https://bugs.eclipse.org/bugs/show_bug.cgi?id=453144 16 | *******************************************************************************/ 17 | 18 | #include "MQTTPacket.h" 19 | #include "StackTrace.h" 20 | 21 | #include 22 | 23 | 24 | /** 25 | * Determines the length of the MQTT publish packet that would be produced using the supplied parameters 26 | * @param qos the MQTT QoS of the publish (packetid is omitted for QoS 0) 27 | * @param topicName the topic name to be used in the publish 28 | * @param payloadlen the length of the payload to be sent 29 | * @return the length of buffer needed to contain the serialized version of the packet 30 | */ 31 | int MQTTSerialize_publishLength(int qos, MQTTString topicName, int payloadlen) 32 | { 33 | int len = 0; 34 | 35 | len += 2 + MQTTstrlen(topicName) + payloadlen; 36 | if (qos > 0) 37 | len += 2; /* packetid */ 38 | return len; 39 | } 40 | 41 | 42 | /** 43 | * Serializes the supplied publish data into the supplied buffer, ready for sending 44 | * @param buf the buffer into which the packet will be serialized 45 | * @param buflen the length in bytes of the supplied buffer 46 | * @param dup integer - the MQTT dup flag 47 | * @param qos integer - the MQTT QoS value 48 | * @param retained integer - the MQTT retained flag 49 | * @param packetid integer - the MQTT packet identifier 50 | * @param topicName MQTTString - the MQTT topic in the publish 51 | * @param payload byte buffer - the MQTT publish payload 52 | * @param payloadlen integer - the length of the MQTT payload 53 | * @return the length of the serialized data. <= 0 indicates error 54 | */ 55 | int MQTTSerialize_publish(unsigned char* buf, int buflen, unsigned char dup, int qos, unsigned char retained, unsigned short packetid, 56 | MQTTString topicName, unsigned char* payload, int payloadlen) 57 | { 58 | unsigned char *ptr = buf; 59 | MQTTHeader header = {0}; 60 | int rem_len = 0; 61 | int rc = 0; 62 | 63 | FUNC_ENTRY; 64 | if (MQTTPacket_len(rem_len = MQTTSerialize_publishLength(qos, topicName, payloadlen)) > buflen) 65 | { 66 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 67 | goto exit; 68 | } 69 | 70 | header.bits.type = PUBLISH; 71 | header.bits.dup = dup; 72 | header.bits.qos = qos; 73 | header.bits.retain = retained; 74 | writeChar(&ptr, header.byte); /* write header */ 75 | 76 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 77 | 78 | writeMQTTString(&ptr, topicName); 79 | 80 | if (qos > 0) 81 | writeInt(&ptr, packetid); 82 | 83 | memcpy(ptr, payload, payloadlen); 84 | ptr += payloadlen; 85 | 86 | rc = ptr - buf; 87 | 88 | exit: 89 | FUNC_EXIT_RC(rc); 90 | return rc; 91 | } 92 | 93 | 94 | 95 | /** 96 | * Serializes the ack packet into the supplied buffer. 97 | * @param buf the buffer into which the packet will be serialized 98 | * @param buflen the length in bytes of the supplied buffer 99 | * @param type the MQTT packet type 100 | * @param dup the MQTT dup flag 101 | * @param packetid the MQTT packet identifier 102 | * @return serialized length, or error if 0 103 | */ 104 | int MQTTSerialize_ack(unsigned char* buf, int buflen, unsigned char packettype, unsigned char dup, unsigned short packetid) 105 | { 106 | MQTTHeader header = {0}; 107 | int rc = 0; 108 | unsigned char *ptr = buf; 109 | 110 | FUNC_ENTRY; 111 | if (buflen < 4) 112 | { 113 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 114 | goto exit; 115 | } 116 | header.bits.type = packettype; 117 | header.bits.dup = dup; 118 | header.bits.qos = (packettype == PUBREL) ? 1 : 0; 119 | writeChar(&ptr, header.byte); /* write header */ 120 | 121 | ptr += MQTTPacket_encode(ptr, 2); /* write remaining length */ 122 | writeInt(&ptr, packetid); 123 | rc = ptr - buf; 124 | exit: 125 | FUNC_EXIT_RC(rc); 126 | return rc; 127 | } 128 | 129 | 130 | /** 131 | * Serializes a puback packet into the supplied buffer. 132 | * @param buf the buffer into which the packet will be serialized 133 | * @param buflen the length in bytes of the supplied buffer 134 | * @param packetid integer - the MQTT packet identifier 135 | * @return serialized length, or error if 0 136 | */ 137 | int MQTTSerialize_puback(unsigned char* buf, int buflen, unsigned short packetid) 138 | { 139 | return MQTTSerialize_ack(buf, buflen, PUBACK, 0, packetid); 140 | } 141 | 142 | 143 | /** 144 | * Serializes a pubrel packet into the supplied buffer. 145 | * @param buf the buffer into which the packet will be serialized 146 | * @param buflen the length in bytes of the supplied buffer 147 | * @param dup integer - the MQTT dup flag 148 | * @param packetid integer - the MQTT packet identifier 149 | * @return serialized length, or error if 0 150 | */ 151 | int MQTTSerialize_pubrel(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid) 152 | { 153 | return MQTTSerialize_ack(buf, buflen, PUBREL, dup, packetid); 154 | } 155 | 156 | 157 | /** 158 | * Serializes a pubrel packet into the supplied buffer. 159 | * @param buf the buffer into which the packet will be serialized 160 | * @param buflen the length in bytes of the supplied buffer 161 | * @param packetid integer - the MQTT packet identifier 162 | * @return serialized length, or error if 0 163 | */ 164 | int MQTTSerialize_pubcomp(unsigned char* buf, int buflen, unsigned short packetid) 165 | { 166 | return MQTTSerialize_ack(buf, buflen, PUBCOMP, 0, packetid); 167 | } 168 | 169 | 170 | -------------------------------------------------------------------------------- /ext lib/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 | -------------------------------------------------------------------------------- /ext lib/MQTTSubscribeClient.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 "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT subscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_subscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]) + 1; /* length + topic + req_qos */ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied subscribe data into the supplied buffer, ready for sending 41 | * @param buf the buffer into which the packet will be serialized 42 | * @param buflen the length in bytes of the supplied bufferr 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters and reqQos arrays 46 | * @param topicFilters - array of topic filter names 47 | * @param requestedQoSs - array of requested QoS 48 | * @return the length of the serialized data. <= 0 indicates error 49 | */ 50 | int MQTTSerialize_subscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, int count, 51 | MQTTString topicFilters[], int requestedQoSs[]) 52 | { 53 | unsigned char *ptr = buf; 54 | MQTTHeader header = {0}; 55 | int rem_len = 0; 56 | int rc = 0; 57 | int i = 0; 58 | 59 | FUNC_ENTRY; 60 | if (MQTTPacket_len(rem_len = MQTTSerialize_subscribeLength(count, topicFilters)) > buflen) 61 | { 62 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 63 | goto exit; 64 | } 65 | 66 | header.byte = 0; 67 | header.bits.type = SUBSCRIBE; 68 | header.bits.dup = dup; 69 | header.bits.qos = 1; 70 | writeChar(&ptr, header.byte); /* write header */ 71 | 72 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 73 | 74 | writeInt(&ptr, packetid); 75 | 76 | for (i = 0; i < count; ++i) 77 | { 78 | writeMQTTString(&ptr, topicFilters[i]); 79 | writeChar(&ptr, requestedQoSs[i]); 80 | } 81 | 82 | rc = ptr - buf; 83 | exit: 84 | FUNC_EXIT_RC(rc); 85 | return rc; 86 | } 87 | 88 | 89 | 90 | /** 91 | * Deserializes the supplied (wire) buffer into suback data 92 | * @param packetid returned integer - the MQTT packet identifier 93 | * @param maxcount - the maximum number of members allowed in the grantedQoSs array 94 | * @param count returned integer - number of members in the grantedQoSs array 95 | * @param grantedQoSs returned array of integers - the granted qualities of service 96 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 97 | * @param buflen the length in bytes of the data in the supplied buffer 98 | * @return error code. 1 is success, 0 is failure 99 | */ 100 | int MQTTDeserialize_suback(unsigned short* packetid, int maxcount, int* count, int grantedQoSs[], unsigned char* buf, int buflen) 101 | { 102 | MQTTHeader header = {0}; 103 | unsigned char* curdata = buf; 104 | unsigned char* enddata = NULL; 105 | int rc = 0; 106 | int mylen; 107 | 108 | FUNC_ENTRY; 109 | header.byte = readChar(&curdata); 110 | if (header.bits.type != SUBACK) 111 | goto exit; 112 | 113 | curdata += (rc = MQTTPacket_decodeBuf(curdata, &mylen)); /* read remaining length */ 114 | enddata = curdata + mylen; 115 | if (enddata - curdata < 2) 116 | goto exit; 117 | 118 | *packetid = readInt(&curdata); 119 | 120 | *count = 0; 121 | while (curdata < enddata) 122 | { 123 | if (*count > maxcount) 124 | { 125 | rc = -1; 126 | goto exit; 127 | } 128 | grantedQoSs[(*count)++] = readChar(&curdata); 129 | } 130 | 131 | rc = 1; 132 | exit: 133 | FUNC_EXIT_RC(rc); 134 | return rc; 135 | } 136 | 137 | 138 | -------------------------------------------------------------------------------- /ext lib/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 | -------------------------------------------------------------------------------- /ext lib/MQTTUnsubscribeClient.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 "MQTTPacket.h" 18 | #include "StackTrace.h" 19 | 20 | #include 21 | 22 | /** 23 | * Determines the length of the MQTT unsubscribe packet that would be produced using the supplied parameters 24 | * @param count the number of topic filter strings in topicFilters 25 | * @param topicFilters the array of topic filter strings to be used in the publish 26 | * @return the length of buffer needed to contain the serialized version of the packet 27 | */ 28 | int MQTTSerialize_unsubscribeLength(int count, MQTTString topicFilters[]) 29 | { 30 | int i; 31 | int len = 2; /* packetid */ 32 | 33 | for (i = 0; i < count; ++i) 34 | len += 2 + MQTTstrlen(topicFilters[i]); /* length + topic*/ 35 | return len; 36 | } 37 | 38 | 39 | /** 40 | * Serializes the supplied unsubscribe data into the supplied buffer, ready for sending 41 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 42 | * @param buflen the length in bytes of the data in the supplied buffer 43 | * @param dup integer - the MQTT dup flag 44 | * @param packetid integer - the MQTT packet identifier 45 | * @param count - number of members in the topicFilters array 46 | * @param topicFilters - array of topic filter names 47 | * @return the length of the serialized data. <= 0 indicates error 48 | */ 49 | int MQTTSerialize_unsubscribe(unsigned char* buf, int buflen, unsigned char dup, unsigned short packetid, 50 | int count, MQTTString topicFilters[]) 51 | { 52 | unsigned char *ptr = buf; 53 | MQTTHeader header = {0}; 54 | int rem_len = 0; 55 | int rc = -1; 56 | int i = 0; 57 | 58 | FUNC_ENTRY; 59 | if (MQTTPacket_len(rem_len = MQTTSerialize_unsubscribeLength(count, topicFilters)) > buflen) 60 | { 61 | rc = MQTTPACKET_BUFFER_TOO_SHORT; 62 | goto exit; 63 | } 64 | 65 | header.byte = 0; 66 | header.bits.type = UNSUBSCRIBE; 67 | header.bits.dup = dup; 68 | header.bits.qos = 1; 69 | writeChar(&ptr, header.byte); /* write header */ 70 | 71 | ptr += MQTTPacket_encode(ptr, rem_len); /* write remaining length */; 72 | 73 | writeInt(&ptr, packetid); 74 | 75 | for (i = 0; i < count; ++i) 76 | writeMQTTString(&ptr, topicFilters[i]); 77 | 78 | rc = ptr - buf; 79 | exit: 80 | FUNC_EXIT_RC(rc); 81 | return rc; 82 | } 83 | 84 | 85 | /** 86 | * Deserializes the supplied (wire) buffer into unsuback data 87 | * @param packetid returned integer - the MQTT packet identifier 88 | * @param buf the raw buffer data, of the correct length determined by the remaining length field 89 | * @param buflen the length in bytes of the data in the supplied buffer 90 | * @return error code. 1 is success, 0 is failure 91 | */ 92 | int MQTTDeserialize_unsuback(unsigned short* packetid, unsigned char* buf, int buflen) 93 | { 94 | unsigned char type = 0; 95 | unsigned char dup = 0; 96 | int rc = 0; 97 | 98 | FUNC_ENTRY; 99 | rc = MQTTDeserialize_ack(&type, &dup, packetid, buf, buflen); 100 | if (type == UNSUBACK) 101 | rc = 1; 102 | FUNC_EXIT_RC(rc); 103 | return rc; 104 | } 105 | 106 | 107 | -------------------------------------------------------------------------------- /ext lib/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 | -------------------------------------------------------------------------------- /img/ch_pair.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leech001/SIM800MQTT/70ccb723988f8a3e4699fd5c0e97d8ddebc4b98b/img/ch_pair.png -------------------------------------------------------------------------------- /img/nvic.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/leech001/SIM800MQTT/70ccb723988f8a3e4699fd5c0e97d8ddebc4b98b/img/nvic.png -------------------------------------------------------------------------------- /src/MQTTSim800.h: -------------------------------------------------------------------------------- 1 | /* 2 | * MQTTSim800.h 3 | * 4 | * Created on: Jan 4, 2020 5 | * Author: Bulanov Konstantin 6 | * 7 | * Contact information 8 | * ------------------- 9 | * 10 | * e-mail : leech001@gmail.com 11 | * telegram : https://t.me/leech001 12 | * 13 | * 14 | */ 15 | 16 | /* 17 | * ----------------------------------------------------------------------------------------------------------------------------------------------- 18 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 19 | Version 2, December 2004 20 | 21 | Copyright (C) 2020 Bulanov Konstantin 22 | 23 | Everyone is permitted to copy and distribute verbatim or modified 24 | copies of this license document, and changing it is allowed as long 25 | as the name is changed. 26 | 27 | DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE 28 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 29 | 30 | 0. You just DO WHAT THE FUCK YOU WANT TO. 31 | 32 | MQTT packet https://github.com/eclipse/paho.mqtt.embedded-c/tree/master/MQTTPacket 33 | * ------------------------------------------------------------------------------------------------------------------------------------------------ 34 | */ 35 | 36 | #include 37 | 38 | // === CONFIG === 39 | #define UART_SIM800 &huart2 40 | #define FREERTOS 0 41 | #define CMD_DELAY 2000 42 | // ============== 43 | 44 | typedef struct { 45 | char *apn; 46 | char *apn_user; 47 | char *apn_pass; 48 | } sim_t; 49 | 50 | typedef struct { 51 | char *host; 52 | uint16_t port; 53 | uint8_t connect; 54 | } mqttServer_t; 55 | 56 | typedef struct { 57 | char *username; 58 | char *pass; 59 | char *clientID; 60 | unsigned short keepAliveInterval; 61 | } mqttClient_t; 62 | 63 | typedef struct { 64 | uint8_t newEvent; 65 | unsigned char dup; 66 | int qos; 67 | unsigned char retained; 68 | unsigned short msgId; 69 | unsigned char payload[64]; 70 | int payloadLen; 71 | unsigned char topic[64]; 72 | int topicLen; 73 | } mqttReceive_t; 74 | 75 | typedef struct { 76 | sim_t sim; 77 | mqttServer_t mqttServer; 78 | mqttClient_t mqttClient; 79 | mqttReceive_t mqttReceive; 80 | } SIM800_t; 81 | 82 | void Sim800_RxCallBack(void); 83 | 84 | void clearRxBuffer(void); 85 | 86 | void clearMqttBuffer(void); 87 | 88 | int SIM800_SendCommand(char *command, char *reply, uint16_t delay); 89 | 90 | int MQTT_Init(void); 91 | 92 | void MQTT_Connect(void); 93 | 94 | void MQTT_Pub(char *topic, char *payload); 95 | 96 | void MQTT_PubUint8(char *topic, uint8_t data); 97 | 98 | void MQTT_PubUint16(char *topic, uint16_t data); 99 | 100 | void MQTT_PubUint32(char *topic, uint32_t data); 101 | 102 | void MQTT_PubFloat(char *topic, float payload); 103 | 104 | void MQTT_PubDouble(char *topic, double data); 105 | 106 | void MQTT_PingReq(void); 107 | 108 | void MQTT_Sub(char *topic); 109 | 110 | void MQTT_Receive(unsigned char *buf); 111 | --------------------------------------------------------------------------------