2017-11-14 03:16:20 +00:00
|
|
|
// Copyright 2015-2018 Espressif Systems (Shanghai) PTE LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "rom/queue.h"
|
|
|
|
#include "esp_log.h"
|
|
|
|
|
2018-09-25 08:34:04 +00:00
|
|
|
#include "esp_transport.h"
|
|
|
|
#include "esp_transport_utils.h"
|
2017-11-14 03:16:20 +00:00
|
|
|
|
|
|
|
static const char *TAG = "TRANSPORT";
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transport layer structure, which will provide functions, basic properties for transport types
|
|
|
|
*/
|
2018-09-26 09:56:47 +00:00
|
|
|
struct esp_transport_item_t {
|
2017-11-14 03:16:20 +00:00
|
|
|
int port;
|
|
|
|
int socket; /*!< Socket to use in this transport */
|
|
|
|
char *scheme; /*!< Tag name */
|
|
|
|
void *context; /*!< Context data */
|
|
|
|
void *data; /*!< Additional transport data */
|
|
|
|
connect_func _connect; /*!< Connect function of this transport */
|
|
|
|
io_read_func _read; /*!< Read */
|
|
|
|
io_func _write; /*!< Write */
|
|
|
|
trans_func _close; /*!< Close */
|
|
|
|
poll_func _poll_read; /*!< Poll and read */
|
|
|
|
poll_func _poll_write; /*!< Poll and write */
|
|
|
|
trans_func _destroy; /*!< Destroy and free transport */
|
2018-08-07 17:56:59 +00:00
|
|
|
connect_async_func _connect_async; /*!< non-blocking connect function of this transport */
|
2018-10-02 13:19:46 +00:00
|
|
|
payload_transfer_func _parent_transfer; /*!< Function returning underlying transport layer */
|
2018-07-24 14:59:03 +00:00
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
STAILQ_ENTRY(esp_transport_item_t) next;
|
2017-11-14 03:16:20 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This list will hold all transport available
|
|
|
|
*/
|
2018-09-26 09:56:47 +00:00
|
|
|
STAILQ_HEAD(esp_transport_list_t, esp_transport_item_t);
|
2017-11-14 03:16:20 +00:00
|
|
|
|
2018-10-02 13:19:46 +00:00
|
|
|
static esp_transport_handle_t esp_transport_get_default_parent(esp_transport_handle_t t)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* By default, the underlying transport layer handle is the handle itself
|
|
|
|
*/
|
|
|
|
return t;
|
|
|
|
}
|
2017-11-14 03:16:20 +00:00
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_list_handle_t esp_transport_list_init()
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_list_handle_t list = calloc(1, sizeof(struct esp_transport_list_t));
|
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, list, return NULL);
|
2017-11-14 03:16:20 +00:00
|
|
|
STAILQ_INIT(list);
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_err_t esp_transport_list_add(esp_transport_list_handle_t list, esp_transport_handle_t t, const char *scheme)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (list == NULL || t == NULL) {
|
|
|
|
return ESP_ERR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
t->scheme = calloc(1, strlen(scheme) + 1);
|
2018-09-26 09:56:47 +00:00
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, t->scheme, return ESP_ERR_NO_MEM);
|
2017-11-14 03:16:20 +00:00
|
|
|
strcpy(t->scheme, scheme);
|
|
|
|
STAILQ_INSERT_TAIL(list, t, next);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t esp_transport_list_get_transport(esp_transport_list_handle_t list, const char *scheme)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (!list) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (scheme == NULL) {
|
|
|
|
return STAILQ_FIRST(list);
|
|
|
|
}
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t item;
|
2017-11-14 03:16:20 +00:00
|
|
|
STAILQ_FOREACH(item, list, next) {
|
|
|
|
if (strcasecmp(item->scheme, scheme) == 0) {
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_err_t esp_transport_list_destroy(esp_transport_list_handle_t list)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_list_clean(list);
|
2017-11-14 03:16:20 +00:00
|
|
|
free(list);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_err_t esp_transport_list_clean(esp_transport_list_handle_t list)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t item = STAILQ_FIRST(list);
|
|
|
|
esp_transport_handle_t tmp;
|
2017-11-14 03:16:20 +00:00
|
|
|
while (item != NULL) {
|
|
|
|
tmp = STAILQ_NEXT(item, next);
|
|
|
|
if (item->_destroy) {
|
|
|
|
item->_destroy(item);
|
|
|
|
}
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_destroy(item);
|
2017-11-14 03:16:20 +00:00
|
|
|
item = tmp;
|
|
|
|
}
|
|
|
|
STAILQ_INIT(list);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t esp_transport_init()
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t t = calloc(1, sizeof(struct esp_transport_item_t));
|
|
|
|
ESP_TRANSPORT_MEM_CHECK(TAG, t, return NULL);
|
2017-11-14 03:16:20 +00:00
|
|
|
return t;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_transport_handle_t esp_transport_get_payload_transport_handle(esp_transport_handle_t t)
|
2018-07-24 14:59:03 +00:00
|
|
|
{
|
|
|
|
if (t && t->_read) {
|
2018-10-02 13:19:46 +00:00
|
|
|
return t->_parent_transfer(t);
|
2018-07-24 14:59:03 +00:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_err_t esp_transport_destroy(esp_transport_handle_t t)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t->scheme) {
|
|
|
|
free(t->scheme);
|
|
|
|
}
|
|
|
|
free(t);
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_connect(esp_transport_handle_t t, const char *host, int port, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
|
|
|
if (t && t->_connect) {
|
|
|
|
return t->_connect(t, host, port, timeout_ms);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_connect_async(esp_transport_handle_t t, const char *host, int port, int timeout_ms)
|
2018-08-07 17:56:59 +00:00
|
|
|
{
|
|
|
|
int ret = -1;
|
2018-10-24 07:32:47 +00:00
|
|
|
if (t && t->_connect_async) {
|
2018-08-07 17:56:59 +00:00
|
|
|
return t->_connect_async(t, host, port, timeout_ms);
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_read(esp_transport_handle_t t, char *buffer, int len, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t && t->_read) {
|
|
|
|
return t->_read(t, buffer, len, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_write(esp_transport_handle_t t, const char *buffer, int len, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t && t->_write) {
|
|
|
|
return t->_write(t, buffer, len, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_poll_read(esp_transport_handle_t t, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t && t->_poll_read) {
|
|
|
|
return t->_poll_read(t, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_poll_write(esp_transport_handle_t t, int timeout_ms)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t && t->_poll_write) {
|
|
|
|
return t->_poll_write(t, timeout_ms);
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_close(esp_transport_handle_t t)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t && t->_close) {
|
|
|
|
return t->_close(t);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
void *esp_transport_get_context_data(esp_transport_handle_t t)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t) {
|
|
|
|
return t->data;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_err_t esp_transport_set_context_data(esp_transport_handle_t t, void *data)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t) {
|
|
|
|
t->data = data;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_err_t esp_transport_set_func(esp_transport_handle_t t,
|
2017-11-14 03:16:20 +00:00
|
|
|
connect_func _connect,
|
|
|
|
io_read_func _read,
|
|
|
|
io_func _write,
|
|
|
|
trans_func _close,
|
|
|
|
poll_func _poll_read,
|
|
|
|
poll_func _poll_write,
|
2018-10-02 13:19:46 +00:00
|
|
|
trans_func _destroy)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
t->_connect = _connect;
|
|
|
|
t->_read = _read;
|
|
|
|
t->_write = _write;
|
|
|
|
t->_close = _close;
|
|
|
|
t->_poll_read = _poll_read;
|
|
|
|
t->_poll_write = _poll_write;
|
|
|
|
t->_destroy = _destroy;
|
2018-08-07 17:56:59 +00:00
|
|
|
t->_connect_async = NULL;
|
2018-10-02 13:19:46 +00:00
|
|
|
t->_parent_transfer = esp_transport_get_default_parent;
|
2017-11-14 03:16:20 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
int esp_transport_get_default_port(esp_transport_handle_t t)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return t->port;
|
|
|
|
}
|
|
|
|
|
2018-09-26 09:56:47 +00:00
|
|
|
esp_err_t esp_transport_set_default_port(esp_transport_handle_t t, int port)
|
2017-11-14 03:16:20 +00:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
t->port = port;
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
2018-07-24 14:59:03 +00:00
|
|
|
|
2018-10-02 13:19:46 +00:00
|
|
|
esp_err_t esp_transport_set_async_connect_func(esp_transport_handle_t t, connect_async_func _connect_async_func)
|
2018-07-24 14:59:03 +00:00
|
|
|
{
|
2018-10-02 13:19:46 +00:00
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
|
|
|
t->_connect_async = _connect_async_func;
|
|
|
|
return ESP_OK;
|
2018-08-07 17:56:59 +00:00
|
|
|
}
|
|
|
|
|
2018-10-02 13:19:46 +00:00
|
|
|
esp_err_t esp_transport_set_parent_transport_func(esp_transport_handle_t t, payload_transfer_func _parent_transport)
|
2018-08-07 17:56:59 +00:00
|
|
|
{
|
|
|
|
if (t == NULL) {
|
|
|
|
return ESP_FAIL;
|
|
|
|
}
|
2018-10-02 13:19:46 +00:00
|
|
|
t->_parent_transfer = _parent_transport;
|
2018-08-07 17:56:59 +00:00
|
|
|
return ESP_OK;
|
|
|
|
}
|