Skip to content

Commit a20c0c9

Browse files
committed
feat(mosq): Add API docs, memory consideration and tests
1 parent 5c850cd commit a20c0c9

File tree

7 files changed

+146
-7
lines changed

7 files changed

+146
-7
lines changed

components/mosquitto/Doxyfile

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Set this to the include directory of your component
2+
INPUT = port/include
3+
STRIP_FROM_PATH = ./port/include
4+
5+
# Output goes into doxygen directory, which is added to gitignore
6+
OUTPUT_DIRECTORY = doxygen
7+
8+
# Warning-related settings, it's recommended to keep them enabled
9+
WARN_AS_ERROR = YES
10+
11+
# Other common settings
12+
RECURSIVE = YES
13+
FULL_PATH_NAMES = YES
14+
ENABLE_PREPROCESSING = YES
15+
MACRO_EXPANSION = YES
16+
EXPAND_ONLY_PREDEF = YES
17+
PREDEFINED = $(ENV_DOXYGEN_DEFINES)
18+
HAVE_DOT = NO
19+
GENERATE_XML = YES
20+
XML_OUTPUT = xml
21+
GENERATE_HTML = NO
22+
HAVE_DOT = NO
23+
GENERATE_LATEX = NO
24+
QUIET = YES
25+
MARKDOWN_SUPPORT = YES

components/mosquitto/README.md

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,32 @@
1-
# ESP32 Mosquitto port (mosq)
1+
# ESP32 Mosquitto Port
22

3-
This is a simplified port of the mosquitto broker running on ESP32. It support only a single listener and TCP transport only
3+
This is a lightweight port of the Mosquitto broker designed to run on the ESP32. It currently supports a single listener and TCP transport only.
4+
5+
## Supported Options
6+
7+
The Espressif port supports a limited set of options (with plans to add more in future releases). These options can be configured through a structure passed to the `mosq_broker_start()` function. For detailed information on available configuration options, refer to the [API documentation](api.md).
8+
9+
## API
10+
11+
### Starting the Broker
12+
13+
To start the broker, call the `mosq_broker_start()` function with a properly configured settings structure. The broker operates in the context of the calling task and does not create a separate task.
14+
15+
It's recommended to analyze the stack size needed for the task, but in general, the broker requires at least 4 kB of stack size.
16+
17+
```c
18+
mosq_broker_start(&config);
19+
```
20+
21+
## Memory Footprint Considerations
22+
23+
The broker primarily uses the heap for internal data, with minimal use of static/BSS memory. It consumes approximately 60 kB of program memory.
24+
25+
- **Initial Memory Usage**: ~2 kB of heap on startup
26+
- **Per Client Memory Usage**: ~4 kB of heap for each connected client
27+
28+
When using the broker with multiple clients, ensure sufficient heap space is available to handle scenarios where clients disconnect abruptly and reconnect. In cases of clean disconnections, the broker releases the memory immediately. However, when a client loses connection abruptly, the broker retains its connection information for some time before eventually freeing this memory. If the client reconnects during this period, an additional 4 kB of heap is allocated for the new connection while the memory for the previous connection is still being retained. Therefore, extra heap space is necessary to manage these abrupt reconnections smoothly.
29+
30+
## Testing
31+
32+
Extensive long-term tests have been conducted with the broker handling 5 clients simultaneously. Each client publishes messages every second to a different topic, while all clients subscribe to all topics. The test also included abrupt disconnections, where clients were suspended and reconnected after 10 seconds, to simulate real-world network instability.

components/mosquitto/api.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# API Reference
2+
3+
## Header files
4+
5+
- [mosq_broker.h](#file-mosq_brokerh)
6+
7+
## File mosq_broker.h
8+
9+
10+
11+
12+
13+
## Structures and Types
14+
15+
| Type | Name |
16+
| ---: | :--- |
17+
| struct | [**mosq\_broker\_config**](#struct-mosq_broker_config) <br>_Mosquitto configuration structure._ |
18+
19+
## Functions
20+
21+
| Type | Name |
22+
| ---: | :--- |
23+
| int | [**mosq\_broker\_start**](#function-mosq_broker_start) (struct [**mosq\_broker\_config**](#struct-mosq_broker_config) \*config) <br>_Start mosquitto broker._ |
24+
25+
26+
## Structures and Types Documentation
27+
28+
### struct `mosq_broker_config`
29+
30+
_Mosquitto configuration structure._
31+
32+
ESP port of mosquittto supports only the options in this configuration structure.
33+
34+
Variables:
35+
36+
- char \* host <br>Address on which the broker is listening for connections
37+
38+
- int port <br>Port number of the broker to listen to
39+
40+
41+
## Functions Documentation
42+
43+
### function `mosq_broker_start`
44+
45+
_Start mosquitto broker._
46+
```c
47+
int mosq_broker_start (
48+
struct mosq_broker_config *config
49+
)
50+
```
51+
52+
53+
This API runs the broker in the calling thread and blocks until the mosquitto exits.
54+
55+
56+
57+
**Parameters:**
58+
59+
60+
* `config` Mosquitto configuration structure
61+
62+
63+
**Returns:**
64+
65+
int Exit code (0 on success)

components/mosquitto/examples/broker/main/broker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,5 +85,5 @@ void app_main(void)
8585
mqtt_app_start(&config);
8686
#endif
8787
// broker continues to run in this task
88-
run_broker(&config);
88+
mosq_broker_start(&config);
8989
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#!/usr/bin/env bash
2+
3+
doxygen Doxyfile
4+
5+
esp-doxybook -i doxygen/xml -o ./api.md

components/mosquitto/port/broker.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static void listeners__stop(void)
9494
mosquitto__free(listensock);
9595
}
9696

97-
int run_broker(struct mosq_broker_config *broker_config)
97+
int mosq_broker_start(struct mosq_broker_config *broker_config)
9898
{
9999

100100
struct mosquitto__config config;

components/mosquitto/port/include/mosq_broker.h

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,24 @@
88

99
struct mosquitto__config;
1010

11+
/**
12+
* @brief Mosquitto configuration structure
13+
*
14+
* ESP port of mosquittto supports only the options in this configuration
15+
* structure.
16+
*/
1117
struct mosq_broker_config {
12-
char *host;
13-
int port;
18+
char *host; /*!< Address on which the broker is listening for connections */
19+
int port; /*!< Port number of the broker to listen to */
1420
};
1521

16-
int run_broker(struct mosq_broker_config *config);
22+
/**
23+
* @brief Start mosquitto broker
24+
*
25+
* This API runs the broker in the calling thread and blocks until
26+
* the mosquitto exits.
27+
*
28+
* @param config Mosquitto configuration structure
29+
* @return int Exit code (0 on success)
30+
*/
31+
int mosq_broker_start(struct mosq_broker_config *config);

0 commit comments

Comments
 (0)