Skip to content

Commit f9d1e55

Browse files
committed
Merge branch 'OpenThread' of https://github.com/SuGlider/arduino-esp32 into OpenThread
2 parents 3507a9d + 198e6e8 commit f9d1e55

File tree

2 files changed

+126
-0
lines changed

2 files changed

+126
-0
lines changed

libraries/OpenThread/README.md

+68
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
# ESP32 Arduino OpenThreadCLI
2+
3+
The `OpenThreadCLI` class is an Arduino API for interacting with the OpenThread Command Line Interface (CLI). It allows you to manage and configure the Thread stack using a command-line interface. Below are the details of the class:
4+
The available OpenThread Commands are documented in the [OpenThread CLI Reference Page](https://openthread.io/reference/cli/commands)
5+
6+
There is one main class called `OpenThreadCLI` and a global object used to operate OpenThread CLI, called `OThreadCLI`
7+
Some [helper functions](helper_functions.md) were made available for working with the OpenThread CLI environment.
8+
9+
It is important to note that the current implementation can only be used with Espressif SoC that has support to IEEE 802.15.4, such as **ESP32-C6** and **ESP32-H2**.
10+
11+
## Class Definition
12+
13+
```cpp
14+
class OpenThreadCLI : public Stream {
15+
private:
16+
static size_t setBuffer(xQueueHandle &queue, size_t len);
17+
bool otStarted = false;
18+
19+
public:
20+
OpenThreadCLI();
21+
~OpenThreadCLI();
22+
operator bool() const;
23+
24+
// Starts a task to read/write otStream. Default prompt is "ot> ". Set it to NULL to make it invisible.
25+
void startOpenThreadConsole(Stream& otStream, bool echoback = true, const char* prompt = "ot> ");
26+
void stopOpenThreadConsole();
27+
void setPrompt(char* prompt); // Changes the console prompt. NULL is an empty prompt.
28+
void setEchoBack(bool echoback); // Changes the console echoback option
29+
void setStream(Stream& otStream); // Changes the console Stream object
30+
void onReceive(OnReceiveCb_t func); // Called on a complete line of output from OT CLI, as OT Response
31+
32+
void begin(bool OThreadAutoStart = true);
33+
void end();
34+
35+
// Default size is 256 bytes
36+
size_t setTxBufferSize(size_t tx_queue_len);
37+
// Default size is 1024 bytes
38+
size_t setRxBufferSize(size_t rx_queue_len);
39+
40+
size_t write(uint8_t);
41+
int available();
42+
int read();
43+
int peek();
44+
void flush();
45+
};
46+
47+
extern OpenThreadCLI OThreadCLI;
48+
```
49+
50+
## Class Overview
51+
- The `OpenThreadCLI` class inherits from the `Stream` class, making it compatible with Arduino's standard I/O functions.
52+
- It provides methods for managing the OpenThread CLI, including starting and stopping the console, setting prompts, and handling received data.
53+
- You can customize the console behavior by adjusting parameters such as echoback and buffer sizes.
54+
55+
## Public Methods
56+
- `startOpenThreadConsole(Stream& otStream, bool echoback = true, const char* prompt = "ot> ")`: Starts the OpenThread console with the specified stream, echoback option, and prompt.
57+
- `stopOpenThreadConsole()`: Stops the OpenThread console.
58+
- `setPrompt(char* prompt)`: Changes the console prompt (set to NULL for an empty prompt).
59+
- `setEchoBack(bool echoback)`: Changes the console echoback option.
60+
- `setStream(Stream& otStream)`: Changes the console Stream object.
61+
- `onReceive(OnReceiveCb_t func)`: Sets a callback function to handle complete lines of output from the OT CLI.
62+
- `begin(bool OThreadAutoStart = true)`: Initializes the OpenThread stack (optional auto-start).
63+
- `end()`: Deinitializes the OpenThread stack.
64+
- `setTxBufferSize(size_t tx_queue_len)`: Sets the transmit buffer size (default is 256 bytes).
65+
- `setRxBufferSize(size_t rx_queue_len)`: Sets the receive buffer size (default is 1024 bytes).
66+
- `write(uint8_t)`, `available()`, `read()`, `peek()`, `flush()`: Standard Stream methods implementation for OpenThread CLI object.
67+
68+
+58
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# OpenThread Helper Functions and Types
2+
3+
The following helper functions and types are designed to simplify writing Arduino sketches for OpenThread. They provide useful utilities for managing OpenThread stack behavior and interacting with the Thread network.
4+
5+
## Enumerated Type: `ot_device_role_t`
6+
7+
This enumeration defines the possible roles of a Thread device within the network:
8+
9+
- `OT_ROLE_DISABLED`: The Thread stack is disabled.
10+
- `OT_ROLE_DETACHED`: The device is not currently participating in a Thread network/partition.
11+
- `OT_ROLE_CHILD`: The device operates as a Thread Child.
12+
- `OT_ROLE_ROUTER`: The device operates as a Thread Router.
13+
- `OT_ROLE_LEADER`: The device operates as a Thread Leader.
14+
15+
## Struct: `ot_cmd_return_t`
16+
17+
This structure represents the return status of an OpenThread CLI command:
18+
19+
- `errorCode`: An integer representing the error code (if any).
20+
- `errorMessage`: A string containing an error message (if applicable).
21+
22+
## Function: `otGetDeviceRole()`
23+
24+
- Returns the current role of the device as an `ot_device_role_t` value.
25+
26+
## Function: `otGetStringDeviceRole()`
27+
28+
- Returns a human-readable string representation of the device role (e.g., "Child," "Router," etc.).
29+
30+
## Function: `otGetRespCmd(const char* cmd, char* resp = NULL, uint32_t respTimeout = 5000)`
31+
32+
- Executes an OpenThread CLI command and retrieves the response.
33+
- Parameters:
34+
- `cmd`: The OpenThread CLI command to execute.
35+
- `resp`: Optional buffer to store the response (if provided).
36+
- `respTimeout`: Timeout (in milliseconds) for waiting for the response.
37+
38+
## Function: `otExecCommand(const char* cmd, const char* arg, ot_cmd_return_t* returnCode = NULL)`
39+
40+
- Executes an OpenThread CLI command with an argument.
41+
- Parameters:
42+
- `cmd`: The OpenThread CLI command to execute.
43+
- `arg`: The argument for the command.
44+
- `returnCode`: Optional pointer to an `ot_cmd_return_t` structure to store the return status.
45+
46+
## Function: `otPrintRespCLI(const char* cmd, Stream& output, uint32_t respTimeout)`
47+
48+
- Executes an OpenThread CLI command and prints the response to the specified output stream.
49+
- Parameters:
50+
- `cmd`: The OpenThread CLI command to execute.
51+
- `output`: The output stream (e.g., Serial) to print the response.
52+
- `respTimeout`: Timeout (in milliseconds) for waiting for the response.
53+
54+
## Function: `otPrintNetworkInformation(Stream& output)`
55+
56+
- Prints information about the current Thread network to the specified output stream.
57+
- Parameters:
58+
- `output`: The output stream (e.g., Serial) to print the network information.

0 commit comments

Comments
 (0)