README.md
1## coreMQTT Agent Library
2
3The coreMQTT Agent library is a high level API that adds thread safety to the [coreMQTT](https://github.com/FreeRTOS/coreMQTT) library. The library provides thread safe equivalents to the coreMQTT's APIs, greatly simplifying its use in multi-threaded environments. The coreMQTT Agent library manages the MQTT connection by serializing the access to the coreMQTT library and reducing implementation overhead (e.g., removing the need for the application to repeatedly call to `MQTT_ProcessLoop`). This allows your multi-threaded applications to share the same MQTT connection, and enables you to design an embedded application without having to worry about coreMQTT thread safety.
4
5This library has gone through code quality checks including verification that no function has a [GNU Complexity](https://www.gnu.org/software/complexity/manual/complexity.html) score over 8, and checks against deviations from mandatory rules in the [MISRA coding standard](https://www.misra.org.uk). Deviations from the MISRA C:2012 guidelines are documented under [MISRA Deviations](MISRA.md). This library has also undergone both static code analysis from [Coverity static analysis](https://scan.coverity.com/), and validation of memory safety through the [CBMC automated reasoning tool](https://www.cprover.org/cbmc/).
6
7See memory requirements for this library [here](./docs/doxygen/include/size_table.md).
8
9## Cloning this repository
10This repo uses [Git Submodules](https://git-scm.com/book/en/v2/Git-Tools-Submodules) to bring in dependent components.
11
12To clone using HTTPS:
13```
14git clone https://github.com/FreeRTOS/coreMQTT-Agent.git --recurse-submodules
15```
16Using SSH:
17```
18git clone git@github.com:FreeRTOS/coreMQTT-Agent.git --recurse-submodules
19```
20
21If you have downloaded the repo without using the `--recurse-submodules` argument, you need to run:
22```
23git submodule update --init --recursive
24```
25
26## coreMQTT Agent Library Configurations
27
28The MQTT Agent library uses the same `core_mqtt_config.h` configuration file as coreMQTT, with the addition of configuration constants listed at the top of [core_mqtt_agent.h](source/include/core_mqtt_agent.h) and [core_mqtt_agent_command_functions.h](source/include/core_mqtt_agent_command_functions.h). Documentation for these configurations can be found [here](https://freertos.org/Documentation/api-ref/coreMQTT-Agent/docs/doxygen/output/html/core_mqtt_agent_config.html).
29
30To provide values for these configuration values, they must be either:
31* Defined in `core_mqtt_config.h` used by coreMQTT
32**OR**
33* Passed as compile time preprocessor macros
34
35## Porting the coreMQTT Agent Library
36In order to use the MQTT Agent library on a platform, you need to supply thread safe functions for the agent's [messaging interface](source/include/core_mqtt_agent_message_interface.h).
37
38### Messaging Interface
39Each of the following functions must be thread safe.
40
41| Function Pointer | Description |
42| :-: | --- |
43| `MQTTAgentMessageSend_t` | A function that sends commands (as `MQTTAgentCommand_t *` pointers) to be received by `MQTTAgent_CommandLoop`. This can be implemented by pushing to a thread safe queue. |
44| `MQTTAgentMessageRecv_t` | A function used by `MQTTAgent_CommandLoop` to receive `MQTTAgentCommand_t *` pointers that were sent by API functions. This can be implemented by receiving from a thread safe queue. |
45| `MQTTAgentCommandGet_t` | A function that returns a pointer to an allocated `MQTTAgentCommand_t` structure, which is used to hold information and arguments for a command to be executed in `MQTTAgent_CommandLoop()`. If using dynamic memory, this can be implemented using `malloc()`. |
46| `MQTTAgentCommandRelease_t` | A function called to indicate that a command structure that had been allocated with the `MQTTAgentCommandGet_t` function pointer will no longer be used by the agent, so it may be freed or marked as not in use. If using dynamic memory, this can be implemented with `free()`. |
47
48Reference implementations for the interface functions can be found in the [reference examples](#reference-examples) below.
49### Additional Considerations
50
51#### Static Memory
52If only static allocation is used, then the `MQTTAgentCommandGet_t` and `MQTTAgentCommandRelease_t` could instead be implemented with a pool of `MQTTAgentCommand_t` structures, with a queue or semaphore used to control access and provide thread safety. The below [reference examples](#reference-examples) use static memory with a command pool.
53
54#### Subscription Management
55The MQTT Agent does not track subscriptions for MQTT topics. The receipt of any incoming PUBLISH packet will result in the invocation of a single `MQTTAgentIncomingPublishCallback_t` callback, which is passed to `MQTTAgent_Init()` for initialization. If it is desired for different handlers to be invoked for different incoming topics, then the publish callback will have to manage subscriptions and fan out messages. A platform independent subscription manager example is implemented in the [reference examples](#reference-examples) below.
56
57## Building the Library
58
59You can build the MQTT Agent source files that are in the [source](source/) directory, and add [source/include](source/include) to your compiler's include path. Additionally, the MQTT Agent library requires the coreMQTT library, whose files follow the same `source/` and `source/include` pattern as the agent library; its build instructions can be found [here](https://github.com/FreeRTOS/coreMQTT#building-the-library).
60
61If using CMake, the [mqttAgentFilePaths.cmake](mqttAgentFilePaths.cmake) file contains the above information of the source files and the header include path from this repository. The same information is found for coreMQTT from `mqttFilePaths.cmake` in the [coreMQTT submodule](source/dependency/).
62
63For a CMake example of building the MQTT Agent library with the `mqttAgentFilePaths.cmake` file, refer to the `coverity_analysis` library target in [test/CMakeLists.txt](test/CMakeLists.txt) file.
64
65## Building Unit Tests
66
67### Checkout CMock Submodule
68
69To build unit tests, the submodule dependency of CMock is required. Use the following command to clone the submodule:
70```
71git submodule update --checkout --init --recursive test/unit-test/CMock
72```
73
74### Unit Test Platform Prerequisites
75
76- For running unit tests
77 - **C90 compiler** like gcc
78 - **CMake 3.13.0 or later**
79 - **Ruby 2.0.0 or later** is additionally required for the CMock test framework (that we use).
80- For running the coverage target, **gcov** and **lcov** are additionally required.
81
82### Steps to build **Unit Tests**
83
841. Go to the root directory of this repository. (Make sure that the **CMock** submodule is cloned as described [above](#checkout-cmock-submodule))
85
861. Run the *cmake* command: `cmake -S test -B build`
87
881. Run this command to build the library and unit tests: `make -C build all`
89
901. The generated test executables will be present in `build/bin/tests` folder.
91
921. Run `cd build && ctest` to execute all tests and view the test run summary.
93
94## CBMC
95
96To learn more about CBMC and proofs specifically, review the training material [here](https://model-checking.github.io/cbmc-training).
97
98The `test/cbmc/proofs` directory contains CBMC proofs.
99
100In order to run these proofs you will need to install CBMC and other tools by following the instructions [here](https://model-checking.github.io/cbmc-training/installation.html).
101
102## Reference examples
103
104Please refer to the demos of the MQTT Agent library in the following locations for reference examples on FreeRTOS platforms:
105
106| Location |
107| :-: |
108| [coreMQTT Agent Demos](https://github.com/FreeRTOS/coreMQTT-Agent-Demos) |
109| [FreeRTOS/FreeRTOS](https://github.com/FreeRTOS/FreeRTOS/tree/main/FreeRTOS-Plus/Demo/coreMQTT_Windows_Simulator/MQTT_Multitask) |
110
111
112## Documentation
113
114The MQTT Agent API documentation can be found [here](https://freertos.org/Documentation/api-ref/coreMQTT-Agent/docs/doxygen/output/html/index.html).
115
116### Generating documentation
117
118The Doxygen references were created using Doxygen version 1.9.2. To generate the
119Doxygen pages yourself, please run the following command from the root of this repository:
120
121```shell
122doxygen docs/doxygen/config.doxyfile
123```
124
125## Getting help
126You can use your Github login to get support from both the FreeRTOS community and directly from the primary FreeRTOS developers on our [active support forum](https://forums.freertos.org). You can find a list of [frequently asked questions](https://www.freertos.org/FAQ.html) here.
127
128## Contributing
129
130See [CONTRIBUTING.md](./.github/CONTRIBUTING.md) for information on contributing.
131
132## License
133
134This library is licensed under the MIT License. See the [LICENSE](LICENSE) file.
135