Skip to content

Commit bf114d3

Browse files
committed
feat(modem): DTE to support sending and receiving raw data
MAJOR CHANGE: Enable DTE to redefine on_read() and write(cmd) directly
1 parent 574738b commit bf114d3

File tree

6 files changed

+63
-31
lines changed

6 files changed

+63
-31
lines changed

components/esp_modem/include/cxx_include/esp_modem_command_library.hpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,19 @@ namespace dce_commands {
2222
* @{
2323
*/
2424

25+
/**
26+
* @brief Generic AT command to be send with pass and fail phrases
27+
*
28+
* @param t Commandable object (anything that can accept commands)
29+
* @param command Command to be sent do the commandable object
30+
* @param pass_phrase String to be present in the reply to pass this command
31+
* @param fail_phrase String to be present in the reply to fail this command
32+
* @param timeout_ms Timeout in ms
33+
*/
34+
command_result generic_command(CommandableIf *t, const std::string &command,
35+
const std::string &pass_phrase,
36+
const std::string &fail_phrase, uint32_t timeout_ms);
37+
2538
/**
2639
* @brief Declaration of all commands is generated from esp_modem_command_declare.inc
2740
*/

components/esp_modem/include/cxx_include/esp_modem_dce_factory.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ class Creator {
6969
ESP_MODEM_THROW_IF_FALSE(netif != nullptr, "Null netif");
7070
}
7171

72+
explicit Creator(std::shared_ptr<DTE> dte): dte(std::move(dte)), device(nullptr), netif(nullptr)
73+
{
74+
}
75+
7276
~Creator()
7377
{
7478
if (device != nullptr) {

components/esp_modem/include/cxx_include/esp_modem_dte.hpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ class CMux;
2929
* @{
3030
*/
3131

32+
struct DTE_Command {
33+
DTE_Command(const std::string &cmd): data((uint8_t *)cmd.c_str()), len(cmd.length()) {}
34+
35+
uint8_t *data;
36+
size_t len;
37+
};
38+
3239
/**
3340
* DTE (Data Terminal Equipment) class
3441
*/
@@ -54,7 +61,9 @@ class DTE : public CommandableIf {
5461
* @param len Data len to write
5562
* @return number of bytes written
5663
*/
57-
int write(uint8_t *data, size_t len);
64+
int write(uint8_t *data, size_t len) override;
65+
66+
int write(DTE_Command command);
5867

5968
/**
6069
* @brief Reading from the underlying terminal
@@ -70,6 +79,8 @@ class DTE : public CommandableIf {
7079
*/
7180
void set_read_cb(std::function<bool(uint8_t *data, size_t len)> f);
7281

82+
void on_read(got_line_cb on_data) override;
83+
7384
/**
7485
* @brief Sets DTE error callback
7586
* @param f Function to be called on DTE error

components/esp_modem/include/cxx_include/esp_modem_types.hpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ class CommandableIf {
8080
*/
8181
virtual command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms, const char separator) = 0;
8282
virtual command_result command(const std::string &command, got_line_cb got_line, uint32_t time_ms) = 0;
83+
84+
virtual int write(uint8_t *data, size_t len) = 0;
85+
virtual void on_read(got_line_cb on_data) = 0;
8386
};
8487

8588
/**

components/esp_modem/include/generate/esp_modem_command_declare.inc

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright 2021 Espressif Systems (Shanghai) PTE LTD
1+
// Copyright 2021-2022 Espressif Systems (Shanghai) PTE LTD
22
//
33
// Licensed under the Apache License, Version 2.0 (the "License");
44
// you may not use this file except in compliance with the License.
@@ -11,36 +11,11 @@
1111
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
14+
#pragma once
1415

15-
#ifndef _ESP_MODEM_COMMAND_DECLARE_INC_
16-
#define _ESP_MODEM_COMMAND_DECLARE_INC_
16+
#include "esp_modem_command_declare_helper.inc"
1717

1818

19-
// Parameters
20-
// * handle different parameters for C++ and C API
21-
// * make parameter unique names, so they could be easily referenced and forwarded
22-
#define _ARG(param, name) param
23-
#define INT_IN(param, name) int _ARG(param, name)
24-
#ifdef __cplusplus
25-
#include <string>
26-
#define STRING_IN(param, name) const std::string& _ARG(param, name)
27-
#define STRING_OUT(param, name) std::string& _ARG(param, name)
28-
#define BOOL_IN(param, name) const bool _ARG(param, name)
29-
#define BOOL_OUT(param, name) bool& _ARG(param, name)
30-
#define INT_OUT(param, name) int& _ARG(param, name)
31-
#define INTEGER_LIST_IN(param, name) const int* _ARG(param, name)
32-
33-
#define STRUCT_OUT(struct_name, p1) struct_name& p1
34-
#else
35-
#define STRING_IN(param, name) const char* _ARG(param, name)
36-
#define STRING_OUT(param, name) char* _ARG(param, name)
37-
#define BOOL_IN(param, name) const bool _ARG(param, name)
38-
#define BOOL_OUT(param, name) bool* _ARG(param, name)
39-
#define INT_OUT(param, name) int* _ARG(param, name)
40-
#define INTEGER_LIST_IN(param, name) const int* _ARG(param, name)
41-
#define STRUCT_OUT(struct_name, p1) esp_modem_ ## struct_name ## _t* p1
42-
#endif
43-
4419
#define DECLARE_ALL_COMMAND_APIS(...) \
4520
/**
4621
* @brief Sends the initial AT sequence to sync up with the device
@@ -331,5 +306,3 @@ public:
331306
#endif
332307

333308
#endif
334-
335-
#endif // _ESP_MODEM_COMMAND_DECLARE_INC_

components/esp_modem/src/esp_modem_dte.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,34 @@ int DTE::write(uint8_t *data, size_t len)
202202
return secondary_term->write(data, len);
203203
}
204204

205+
int DTE::write(DTE_Command command)
206+
{
207+
return primary_term->write(command.data, command.len);
208+
}
209+
210+
void DTE::on_read(got_line_cb on_read_cb)
211+
{
212+
if (on_read_cb == nullptr) {
213+
primary_term->set_read_cb(nullptr);
214+
internal_lock.unlock();
215+
return;
216+
}
217+
internal_lock.lock();
218+
primary_term->set_read_cb([this, on_read_cb](uint8_t *data, size_t len) {
219+
if (!data) {
220+
data = buffer.get();
221+
len = primary_term->read(data, buffer.size);
222+
}
223+
auto res = on_read_cb(data, len);
224+
if (res == command_result::OK || res == command_result::FAIL) {
225+
primary_term->set_read_cb(nullptr);
226+
internal_lock.unlock();
227+
return true;
228+
}
229+
return false;
230+
});
231+
}
232+
205233
/**
206234
* Implemented here to keep all headers C++11 compliant
207235
*/

0 commit comments

Comments
 (0)