2021-03-29 19:34:45 +02:00
|
|
|
/*
|
|
|
|
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD
|
2022-10-11 16:31:57 +02:00
|
|
|
*
|
2021-03-29 19:34:45 +02:00
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2021-03-07 19:43:45 +01:00
|
|
|
|
2021-05-26 15:57:25 +02:00
|
|
|
#pragma once
|
2021-03-07 19:43:45 +01:00
|
|
|
|
|
|
|
#include <memory>
|
|
|
|
#include <functional>
|
|
|
|
#include <exception>
|
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
#include <utility>
|
|
|
|
#include "esp_err.h"
|
|
|
|
#include "esp_modem_primitives.hpp"
|
|
|
|
|
2021-03-29 19:34:45 +02:00
|
|
|
namespace esp_modem {
|
2021-03-07 19:43:45 +01:00
|
|
|
|
2021-04-14 17:57:42 +02:00
|
|
|
/**
|
|
|
|
* @defgroup ESP_MODEM_TERMINAL
|
|
|
|
* @brief Definition of an abstract terminal to be attached to DTE class
|
|
|
|
*/
|
|
|
|
|
|
|
|
/** @addtogroup ESP_MODEM_TERMINAL
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Terminal errors
|
|
|
|
*/
|
2021-03-07 19:43:45 +01:00
|
|
|
enum class terminal_error {
|
|
|
|
BUFFER_OVERFLOW,
|
|
|
|
CHECKSUM_ERROR,
|
|
|
|
UNEXPECTED_CONTROL_FLOW,
|
2022-09-02 09:14:38 +02:00
|
|
|
DEVICE_GONE,
|
2021-03-07 19:43:45 +01:00
|
|
|
};
|
|
|
|
|
2021-04-14 17:57:42 +02:00
|
|
|
/**
|
2021-05-26 16:41:19 +08:00
|
|
|
* @brief Terminal interface. All communication interfaces must comply to this interface in order to be used as a DTE
|
2021-04-14 17:57:42 +02:00
|
|
|
*/
|
2021-03-07 19:43:45 +01:00
|
|
|
class Terminal {
|
|
|
|
public:
|
|
|
|
virtual ~Terminal() = default;
|
2021-03-29 19:34:45 +02:00
|
|
|
|
2021-06-01 10:21:51 +02:00
|
|
|
void set_error_cb(std::function<void(terminal_error)> f)
|
|
|
|
{
|
|
|
|
on_error = std::move(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void set_read_cb(std::function<bool(uint8_t *data, size_t len)> f)
|
|
|
|
{
|
|
|
|
on_read = std::move(f);
|
|
|
|
}
|
2021-03-29 19:34:45 +02:00
|
|
|
|
2021-04-14 17:57:42 +02:00
|
|
|
/**
|
|
|
|
* @brief Writes data to the terminal
|
|
|
|
* @param data Data pointer
|
|
|
|
* @param len Data len
|
|
|
|
* @return length of data written
|
|
|
|
*/
|
2021-03-07 19:43:45 +01:00
|
|
|
virtual int write(uint8_t *data, size_t len) = 0;
|
2021-03-29 19:34:45 +02:00
|
|
|
|
2021-04-14 17:57:42 +02:00
|
|
|
/**
|
|
|
|
* @brief Read from the terminal. This function doesn't block, but return all available data.
|
|
|
|
* @param data Data pointer to store the read payload
|
|
|
|
* @param len Maximum data len to read
|
|
|
|
* @return length of data actually read
|
|
|
|
*/
|
2021-03-07 19:43:45 +01:00
|
|
|
virtual int read(uint8_t *data, size_t len) = 0;
|
2021-03-29 19:34:45 +02:00
|
|
|
|
2021-03-07 19:43:45 +01:00
|
|
|
virtual void start() = 0;
|
2021-03-29 19:34:45 +02:00
|
|
|
|
2021-03-07 19:43:45 +01:00
|
|
|
virtual void stop() = 0;
|
2021-03-29 19:34:45 +02:00
|
|
|
|
2021-03-07 19:43:45 +01:00
|
|
|
protected:
|
2021-05-26 16:41:19 +08:00
|
|
|
std::function<bool(uint8_t *data, size_t len)> on_read;
|
2021-03-07 19:43:45 +01:00
|
|
|
std::function<void(terminal_error)> on_error;
|
|
|
|
};
|
|
|
|
|
2021-04-14 17:57:42 +02:00
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
2021-03-29 19:34:45 +02:00
|
|
|
} // namespace esp_modem
|