IDF master 3e370c4296

* Fix build compilation due to changes in the HW_TIMER's structs

* Fix compilation warnings and errors with USB

* Update USBCDC.cpp

* Update CMakeLists.txt

* Update HWCDC.cpp
This commit is contained in:
Me No Dev
2021-10-01 17:52:29 +03:00
committed by GitHub
parent 381e88ec75
commit 00214d5c2a
1475 changed files with 88153 additions and 49503 deletions

View File

@ -244,6 +244,14 @@ const esp_partition_t* esp_ota_get_next_update_partition(const esp_partition_t *
*/
esp_err_t esp_ota_get_partition_description(const esp_partition_t *partition, esp_app_desc_t *app_desc);
/**
* @brief Returns number of ota partitions provided in partition table.
*
* @return
* - Number of OTA partitions
*/
uint8_t esp_ota_get_app_partition_count(void);
/**
* @brief This function is called to indicate that the running app is working well.
*

View File

@ -119,6 +119,15 @@ bool bootloader_common_label_search(const char *list, char *label);
*/
void bootloader_configure_spi_pins(int drv);
/**
* @brief Get flash CS IO
*
* Can be determined by eFuse values, or the default value
*
* @return Flash CS IO
*/
uint8_t bootloader_flash_get_cs_io(void);
/**
* @brief Calculates a sha-256 for a given partition or returns a appended digest.
*

View File

@ -10,6 +10,18 @@
#include "sdkconfig.h"
#include "soc/soc_caps.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Read flash ID by sending RDID command (0x9F)
* @return flash raw ID
* mfg_id = (ID >> 16) & 0xFF;
flash_id = ID & 0xffff;
*/
uint32_t bootloader_read_flash_id(void);
#if SOC_CACHE_SUPPORT_WRAP
/**
* @brief Set the burst mode setting command for specified wrap mode.
@ -19,3 +31,22 @@
*/
esp_err_t bootloader_flash_wrap_set(spi_flash_wrap_mode_t mode);
#endif
/**
* @brief Unlock Flash write protect.
* Please do not call this function in SDK.
*
* @note This can be overridden because it's attribute weak.
*/
esp_err_t bootloader_flash_unlock(void);
/**
* @brief Startup flow recommended by XMC. Call at startup before any erase/write operation.
*
* @return ESP_OK When startup successfully, otherwise ESP_FAIL (indiciating you should reboot before erase/write).
*/
esp_err_t bootloader_flash_xmc_startup(void);
#ifdef __cplusplus
}
#endif

View File

@ -77,4 +77,8 @@ period_ms_t osi_alarm_get_remaining_ms(const osi_alarm_t *alarm);
uint32_t osi_time_get_os_boottime_ms(void);
// This function returns whether the given |alarm| is active or not.
// Return true if active, false otherwise.
bool osi_alarm_is_active(osi_alarm_t *alarm);
#endif /*_ALARM_H_*/

View File

@ -467,7 +467,7 @@ typedef struct
{
uint16_t rx_len; /*!< pkt rx data length value */
uint16_t tx_len; /*!< pkt tx data length value */
}esp_ble_pkt_data_length_params_t;
} esp_ble_pkt_data_length_params_t;
/**
* @brief BLE encryption keys
@ -648,7 +648,7 @@ typedef enum {
typedef enum{
ESP_BLE_WHITELIST_REMOVE = 0X00, /*!< remove mac from whitelist */
ESP_BLE_WHITELIST_ADD = 0X01, /*!< add address to whitelist */
}esp_ble_wl_opration_t;
} esp_ble_wl_opration_t;
#if (BLE_42_FEATURE_SUPPORT == TRUE)
typedef enum {
ESP_BLE_DUPLICATE_EXCEPTIONAL_LIST_ADD = 0, /*!< Add device info into duplicate scan exceptional list */
@ -998,7 +998,7 @@ typedef union {
uint16_t conn_int; /*!< Current connection interval */
uint16_t timeout; /*!< Supervision timeout for the LE Link. Range: 0x000A to 0x0C80.
Mandatory Range: 0x000A to 0x0C80 Time = N * 10 msec */
}update_conn_params; /*!< Event parameter of ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT */
} update_conn_params; /*!< Event parameter of ESP_GAP_BLE_UPDATE_CONN_PARAMS_EVT */
/**
* @brief ESP_GAP_BLE_SET_PKT_LENGTH_COMPLETE_EVT
*/
@ -1018,13 +1018,13 @@ typedef union {
struct ble_remove_bond_dev_cmpl_evt_param {
esp_bt_status_t status; /*!< Indicate the remove bond device operation success status */
esp_bd_addr_t bd_addr; /*!< The device address which has been remove from the bond list */
}remove_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT */
} remove_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_REMOVE_BOND_DEV_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT
*/
struct ble_clear_bond_dev_cmpl_evt_param {
esp_bt_status_t status; /*!< Indicate the clear bond device operation success status */
}clear_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT */
} clear_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_CLEAR_BOND_DEV_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT
*/
@ -1032,7 +1032,7 @@ typedef union {
esp_bt_status_t status; /*!< Indicate the get bond device operation success status */
uint8_t dev_num; /*!< Indicate the get number device in the bond list */
esp_ble_bond_dev_t *bond_dev; /*!< the pointer to the bond device Structure */
}get_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT */
} get_bond_dev_cmpl; /*!< Event parameter of ESP_GAP_BLE_GET_BOND_DEV_COMPLETE_EVT */
/**
* @brief ESP_GAP_BLE_READ_RSSI_COMPLETE_EVT
*/

View File

@ -380,7 +380,7 @@ esp_err_t esp_ble_gattc_search_service(esp_gatt_if_t gattc_if, uint16_t conn_id,
/**
* @brief Find all the service with the given service uuid in the gattc cache, if the svc_uuid is NULL, find all the service.
* Note: It just get service from local cache, won't get from remote devices. If want to get it from remote device, need
* to used the esp_ble_gattc_search_service.
* to used the esp_ble_gattc_cache_refresh, then call esp_ble_gattc_get_service again.
*
* @param[in] gattc_if: Gatt client access interface.
* @param[in] conn_id: connection ID which identify the server.

View File

@ -0,0 +1,379 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
// Copyright 2019 Blake Felt
//
// 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.
#ifndef __ESP_HIDD_API_H__
#define __ESP_HIDD_API_H__
#include "esp_bt_defs.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/* sub_class of hid device */
#define ESP_HID_CLASS_UNKNOWN (0x00<<2)
#define ESP_HID_CLASS_JOS (0x01<<2) /* joy stick */
#define ESP_HID_CLASS_GPD (0x02<<2) /* game pad */
#define ESP_HID_CLASS_RMC (0x03<<2) /* remote control */
#define ESP_HID_CLASS_SED (0x04<<2) /* sensing device */
#define ESP_HID_CLASS_DGT (0x05<<2) /* Digitizer tablet */
#define ESP_HID_CLASS_CDR (0x06<<2) /* card reader */
#define ESP_HID_CLASS_KBD (0x10<<2) /* keyboard */
#define ESP_HID_CLASS_MIC (0x20<<2) /* pointing device */
#define ESP_HID_CLASS_COM (0x30<<2) /* Combo keyboard/pointing */
/**
* @brief HIDD handshake error
*/
typedef enum {
ESP_HID_PAR_HANDSHAKE_RSP_SUCCESS = 0,
ESP_HID_PAR_HANDSHAKE_RSP_NOT_READY = 1,
ESP_HID_PAR_HANDSHAKE_RSP_ERR_INVALID_REP_ID = 2,
ESP_HID_PAR_HANDSHAKE_RSP_ERR_UNSUPPORTED_REQ = 3,
ESP_HID_PAR_HANDSHAKE_RSP_ERR_INVALID_PARAM = 4,
ESP_HID_PAR_HANDSHAKE_RSP_ERR_UNKNOWN = 14,
ESP_HID_PAR_HANDSHAKE_RSP_ERR_FATAL = 15
} esp_hidd_handshake_error_t;
/**
* @brief HIDD report types
*/
typedef enum {
ESP_HIDD_REPORT_TYPE_OTHER = 0,
ESP_HIDD_REPORT_TYPE_INPUT,
ESP_HIDD_REPORT_TYPE_OUTPUT,
ESP_HIDD_REPORT_TYPE_FEATURE,
// special value for reports to be sent on INTR(INPUT is assumed)
ESP_HIDD_REPORT_TYPE_INTRDATA
} esp_hidd_report_type_t;
/**
* @brief HIDD connection state
*/
typedef enum {
ESP_HIDD_CONN_STATE_CONNECTED,
ESP_HIDD_CONN_STATE_CONNECTING,
ESP_HIDD_CONN_STATE_DISCONNECTED,
ESP_HIDD_CONN_STATE_DISCONNECTING,
ESP_HIDD_CONN_STATE_UNKNOWN
} esp_hidd_connection_state_t;
/**
* @brief HID device protocol modes
*/
typedef enum {
ESP_HIDD_REPORT_MODE = 0x00,
ESP_HIDD_BOOT_MODE = 0x01,
ESP_HIDD_UNSUPPORTED_MODE = 0xff
} esp_hidd_protocol_mode_t;
/**
* @brief HIDD characteristics for SDP report
*/
typedef struct {
const char *name;
const char *description;
const char *provider;
uint8_t subclass;
uint8_t *desc_list;
int desc_list_len;
} esp_hidd_app_param_t;
/**
* @brief HIDD Quality of Service parameters
*/
typedef struct {
uint8_t service_type;
uint32_t token_rate;
uint32_t token_bucket_size;
uint32_t peak_bandwidth;
uint32_t access_latency;
uint32_t delay_variation;
} esp_hidd_qos_param_t;
/**
* @brief HID device callback function events
*/
typedef enum {
ESP_HIDD_INIT_EVT = 0, /*!< When HID device is inited, the event comes */
ESP_HIDD_DEINIT_EVT, /*!< When HID device is deinited, the event comes */
ESP_HIDD_REGISTER_APP_EVT, /*!< When HID device application registered, the event comes */
ESP_HIDD_UNREGISTER_APP_EVT, /*!< When HID device application unregistered, the event comes */
ESP_HIDD_OPEN_EVT, /*!< When HID device connection to host opened, the event comes */
ESP_HIDD_CLOSE_EVT, /*!< When HID device connection to host closed, the event comes */
ESP_HIDD_SEND_REPORT_EVT, /*!< When HID device send report to lower layer, the event comes */
ESP_HIDD_REPORT_ERR_EVT, /*!< When HID device report handshanke error to lower layer, the event comes */
ESP_HIDD_GET_REPORT_EVT, /*!< When HID device receives GET_REPORT request from host, the event comes */
ESP_HIDD_SET_REPORT_EVT, /*!< When HID device receives SET_REPORT request from host, the event comes */
ESP_HIDD_SET_PROTOCOL_EVT, /*!< When HID device receives SET_PROTOCOL request from host, the event comes */
ESP_HIDD_INTR_DATA_EVT, /*!< When HID device receives DATA from host on intr, the event comes */
ESP_HIDD_VC_UNPLUG_EVT, /*!< When HID device initiates Virtual Cable Unplug, the event comes */
ESP_HIDD_API_ERR_EVT /*!< When HID device has API error, the event comes */
} esp_hidd_cb_event_t;
typedef enum {
ESP_HIDD_SUCCESS,
ESP_HIDD_ERROR, /*!< general ESP HD error */
ESP_HIDD_NO_RES, /*!< out of system resources */
ESP_HIDD_BUSY, /*!< Temporarily can not handle this request. */
ESP_HIDD_NO_DATA, /*!< No data. */
ESP_HIDD_NEED_INIT, /*!< HIDD module shall init first */
ESP_HIDD_NEED_DEINIT, /*!< HIDD module shall deinit first */
ESP_HIDD_NEED_REG, /*!< HIDD module shall register first */
ESP_HIDD_NEED_DEREG, /*!< HIDD module shall deregister first */
ESP_HIDD_NO_CONNECTION, /*!< connection may have been closed */
} esp_hidd_status_t;
/**
* @brief HID device callback parameters union
*/
typedef union {
/**
* @brief ESP_HIDD_INIT_EVT
*/
struct hidd_init_evt_param {
esp_hidd_status_t status; /*!< operation status */
} init; /*!< HIDD callback param of ESP_HIDD_INIT_EVT */
/**
* @brief ESP_HIDD_DEINIT_EVT
*/
struct hidd_deinit_evt_param {
esp_hidd_status_t status; /*!< operation status */
} deinit; /*!< HIDD callback param of ESP_HIDD_DEINIT_EVT */
/**
* @brief ESP_HIDD_REGISTER_APP_EVT
*/
struct hidd_register_app_evt_param {
esp_hidd_status_t status; /*!< operation status */
bool in_use; /*!< indicate whether use virtual cable plug host address */
esp_bd_addr_t bd_addr; /*!< host address */
} register_app; /*!< HIDD callback param of ESP_HIDD_REGISTER_APP_EVT */
/**
* @brief ESP_HIDD_UNREGISTER_APP_EVT
*/
struct hidd_unregister_app_evt_param {
esp_hidd_status_t status; /*!< operation status */
} unregister_app; /*!< HIDD callback param of ESP_HIDD_UNREGISTER_APP_EVT */
/**
* @brief ESP_HIDD_OPEN_EVT
*/
struct hidd_open_evt_param {
esp_hidd_status_t status; /*!< operation status */
esp_hidd_connection_state_t conn_status; /*!< connection status */
esp_bd_addr_t bd_addr; /*!< host address */
} open; /*!< HIDD callback param of ESP_HIDD_OPEN_EVT */
/**
* @brief ESP_HIDD_CLOSE_EVT
*/
struct hidd_close_evt_param {
esp_hidd_status_t status; /*!< operation status */
esp_hidd_connection_state_t conn_status; /*!< connection status */
} close; /*!< HIDD callback param of ESP_HIDD_CLOSE_EVT */
/**
* @brief ESP_HIDD_SEND_REPORT_EVT
*/
struct hidd_send_report_evt_param {
esp_hidd_status_t status; /*!< operation status */
uint8_t reason; /*!< lower layer failed reason(ref hiddefs.h) */
esp_hidd_report_type_t report_type; /*!< report type */
uint8_t report_id; /*!< report id */
} send_report; /*!< HIDD callback param of ESP_HIDD_SEND_REPORT_EVT */
/**
* @brief ESP_HIDD_REPORT_ERR_EVT
*/
struct hidd_report_err_evt_param {
esp_hidd_status_t status; /*!< operation status */
uint8_t reason; /*!< lower layer failed reason(ref hiddefs.h) */
} report_err; /*!< HIDD callback param of ESP_HIDD_REPORT_ERR_EVT */
/**
* @brief ESP_HIDD_GET_REPORT_EVT
*/
struct hidd_get_report_evt_param {
esp_hidd_report_type_t report_type; /*!< report type */
uint8_t report_id; /*!< report id */
uint16_t buffer_size; /*!< buffer size */
} get_report; /*!< HIDD callback param of ESP_HIDD_GET_REPORT_EVT */
/**
* @brief ESP_HIDD_SET_REPORT_EVT
*/
struct hidd_set_report_evt_param {
esp_hidd_report_type_t report_type; /*!< report type */
uint8_t report_id; /*!< report id */
uint16_t len; /*!< set_report data length */
uint8_t *data; /*!< set_report data pointer */
} set_report; /*!< HIDD callback param of ESP_HIDD_SET_REPORT_EVT */
/**
* @brief ESP_HIDD_SET_PROTOCOL_EVT
*/
struct hidd_set_protocol_evt_param {
esp_hidd_protocol_mode_t protocol_mode; /*!< protocol mode */
} set_protocol; /*!< HIDD callback param of ESP_HIDD_SET_PROTOCOL_EVT */
/**
* @brief ESP_HIDD_INTR_DATA_EVT
*/
struct hidd_intr_data_evt_param {
uint8_t report_id; /*!< interrupt channel report id */
uint16_t len; /*!< interrupt channel report data length */
uint8_t *data; /*!< interrupt channel report data pointer */
} intr_data; /*!< HIDD callback param of ESP_HIDD_INTR_DATA_EVT */
/**
* @brief ESP_HIDD_VC_UNPLUG_EVT
*/
struct hidd_vc_unplug_param {
esp_hidd_status_t status; /*!< operation status */
esp_hidd_connection_state_t conn_status; /*!< connection status */
} vc_unplug; /*!< HIDD callback param of ESP_HIDD_VC_UNPLUG_EVT */
} esp_hidd_cb_param_t;
/**
* @brief HID device callback function type.
* @param event: Event type
* @param param: Point to callback parameter, currently is union type
*/
typedef void (esp_hd_cb_t)(esp_hidd_cb_event_t event, esp_hidd_cb_param_t *param);
/**
* @brief This function is called to init callbacks with HID device module.
*
* @param[in] callback: pointer to the init callback function.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_register_callback(esp_hd_cb_t callback);
/**
* @brief This function initializes HIDD. This function should be called after esp_bluedroid_enable and
* esp_blueroid_init success, and should be called after esp_bt_hid_device_register_callback.
* When the operation is complete the callback function will be called with ESP_HIDD_INIT_EVT.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_init(void);
/**
* @brief This function de-initializes HIDD interface. This function should be called after esp_bluedroid_enable() and
* esp_blueroid_init() success, and should be called after esp_bt_hid_device_init(). When the operation is complete the callback
* function will be called with ESP_HIDD_DEINIT_EVT.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_deinit(void);
/**
* @brief Registers HIDD parameters with SDP and sets l2cap Quality of Service. This function should be called after
* esp_bluedroid_enable and esp_blueroid_init success, and must be done after esp_bt_hid_device_init. When the operation is complete the callback
* function will be called with ESP_HIDD_REGISTER_APP_EVT.
*
* @param[in] app_param: HIDD parameters
* @param[in] in_qos: incoming QoS parameters
* @param[in] out_qos: outgoing QoS parameters
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_register_app(esp_hidd_app_param_t *app_param, esp_hidd_qos_param_t *in_qos,
esp_hidd_qos_param_t *out_qos);
/**
* @brief Removes HIDD parameters from SDP and resets l2cap Quality of Service. This function should be called after esp_bluedroid_enable and
* esp_blueroid_init success, and should be called after esp_bt_hid_device_init. When the operation is complete the callback
* function will be called with ESP_HIDD_UNREGISTER_APP_EVT.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_unregister_app(void);
/**
* @brief This function connects HIDD interface to connected bluetooth device, if not done already. When the operation is complete the callback
* function will be called with ESP_HIDD_OPEN_EVT.
*
* @param[in] bd_addr: Remote host bluetooth device address.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_connect(esp_bd_addr_t bd_addr);
/**
* @brief This function disconnects HIDD interface. When the operation is complete the callback
* function will be called with ESP_HIDD_CLOSE_EVT.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_disconnect(void);
/**
* @brief Send HIDD report. When the operation is complete the callback
* function will be called with ESP_HIDD_SEND_REPORT_EVT.
*
* @param[in] type: type of report
* @param[in] id: report id as defined by descriptor
* @param[in] len: length of report
* @param[in] data: report data
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_send_report(esp_hidd_report_type_t type, uint8_t id, uint16_t len, uint8_t *data);
/**
* @brief Sends HIDD handshake with error info for invalid set_report. When the operation is complete the callback
* function will be called with ESP_HIDD_REPORT_ERR_EVT.
*
* @param[in] error: type of error
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_report_error(esp_hidd_handshake_error_t error);
/**
* @brief Unplug virtual cable of HIDD. When the operation is complete the callback
* function will be called with ESP_HIDD_VC_UNPLUG_EVT.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_device_virtual_cable_unplug(void);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -0,0 +1,465 @@
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
// Copyright 2019 Blake Felt
//
// 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.
#ifndef __ESP_HIDH_API_H__
#define __ESP_HIDH_API_H__
#include "esp_bt_defs.h"
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
#define BTHH_MAX_DSC_LEN 884
/**
* @brief HID host connection state
*/
typedef enum {
ESP_HIDH_CONN_STATE_CONNECTED = 0, /*!< connected state */
ESP_HIDH_CONN_STATE_CONNECTING, /*!< connecting state */
ESP_HIDH_CONN_STATE_DISCONNECTED, /*!< disconnected state */
ESP_HIDH_CONN_STATE_DISCONNECTING, /*!< disconnecting state */
ESP_HIDH_CONN_STATE_UNKNOWN /*!< unknown state(initial state) */
} esp_hidh_connection_state_t;
typedef enum {
ESP_HIDH_OK,
ESP_HIDH_HS_HID_NOT_READY, /*!< handshake error : device not ready */
ESP_HIDH_HS_INVALID_RPT_ID, /*!< handshake error : invalid report ID */
ESP_HIDH_HS_TRANS_NOT_SPT, /*!< handshake error : transaction not spt */
ESP_HIDH_HS_INVALID_PARAM, /*!< handshake error : invalid paremter */
ESP_HIDH_HS_ERROR, /*!< handshake error : unspecified HS error */
ESP_HIDH_ERR, /*!< general ESP HH error */
ESP_HIDH_ERR_SDP, /*!< SDP error */
ESP_HIDH_ERR_PROTO, /*!< SET_Protocol error,
only used in ESP_HIDH_OPEN_EVT callback */
ESP_HIDH_ERR_DB_FULL, /*!< device database full error, used in
ESP_HIDH_OPEN_EVT/ESP_HIDH_ADD_DEV_EVT */
ESP_HIDH_ERR_TOD_UNSPT, /*!< type of device not supported */
ESP_HIDH_ERR_NO_RES, /*!< out of system resources */
ESP_HIDH_ERR_AUTH_FAILED, /*!< authentication fail */
ESP_HIDH_ERR_HDL, /*!< connection handle error */
ESP_HIDH_ERR_SEC, /*!< encryption error */
// self_defined
ESP_HIDH_BUSY, /*!< Temporarily can not handle this request. */
ESP_HIDH_NO_DATA, /*!< No data. */
ESP_HIDH_NEED_INIT, /*!< HIDH module shall init first */
ESP_HIDH_NEED_DEINIT, /*!< HIDH module shall deinit first */
ESP_HIDH_NO_CONNECTION, /*!< connection may have been closed */
} esp_hidh_status_t;
/**
* @brief HID host protocol modes
*/
typedef enum {
ESP_HIDH_BOOT_MODE = 0x00, /*!< boot protocol mode */
ESP_HIDH_REPORT_MODE = 0x01, /*!< report protocol mode */
ESP_HIDH_UNSUPPORTED_MODE = 0xff /*!< unsupported protocol mode */
} esp_hidh_protocol_mode_t;
/**
* @brief HID host report types
*/
typedef enum {
ESP_HIDH_REPORT_TYPE_OTHER = 0, /*!< unsupported report type */
ESP_HIDH_REPORT_TYPE_INPUT, /*!< input report type */
ESP_HIDH_REPORT_TYPE_OUTPUT, /*!< output report type */
ESP_HIDH_REPORT_TYPE_FEATURE, /*!< feature report type */
} esp_hidh_report_type_t;
/**
* @brief HID host callback function events
*/
typedef enum {
ESP_HIDH_INIT_EVT = 0, /*!< When HID host is inited, the event comes */
ESP_HIDH_DEINIT_EVT, /*!< When HID host is deinited, the event comes */
ESP_HIDH_OPEN_EVT, /*!< When HID host connection opened, the event comes */
ESP_HIDH_CLOSE_EVT, /*!< When HID host connection closed, the event comes */
ESP_HIDH_GET_RPT_EVT, /*!< When Get_Report command is called, the event comes */
ESP_HIDH_SET_RPT_EVT, /*!< When Set_Report command is called, the event comes */
ESP_HIDH_GET_PROTO_EVT, /*!< When Get_Protocol command is called, the event comes */
ESP_HIDH_SET_PROTO_EVT, /*!< When Set_Protocol command is called, the event comes */
ESP_HIDH_GET_IDLE_EVT, /*!< When Get_Idle command is called, the event comes */
ESP_HIDH_SET_IDLE_EVT, /*!< When Set_Idle command is called, the event comes */
ESP_HIDH_GET_DSCP_EVT, /*!< When HIDH is inited, the event comes */
ESP_HIDH_ADD_DEV_EVT, /*!< When a device is added, the event comes */
ESP_HIDH_RMV_DEV_EVT, /*!< When a device is removed, the event comes */
ESP_HIDH_VC_UNPLUG_EVT, /*!< When virtually unplugged, the event comes */
ESP_HIDH_DATA_EVT, /*!< When send data on interrupt channel, the event comes */
ESP_HIDH_DATA_IND_EVT, /*!< When receive data on interrupt channel, the event comes */
ESP_HIDH_SET_INFO_EVT /*!< When set the HID device descriptor, the event comes */
} esp_hidh_cb_event_t;
typedef struct {
int attr_mask;
uint8_t sub_class;
uint8_t app_id;
int vendor_id;
int product_id;
int version;
uint8_t ctry_code;
int dl_len;
uint8_t dsc_list[BTHH_MAX_DSC_LEN];
} esp_hidh_hid_info_t;
/**
* @brief HID host callback parameters union
*/
typedef union {
/**
* @brief ESP_HIDH_INIT_EVT
*/
struct hidh_init_evt_param {
esp_hidh_status_t status; /*!< status */
} init; /*!< HIDH callback param of ESP_HIDH_INIT_EVT */
/**
* @brief ESP_HIDH_DEINIT_EVT
*/
struct hidh_uninit_evt_param {
esp_hidh_status_t status; /*!< status */
} deinit; /*!< HIDH callback param of ESP_HIDH_DEINIT_EVT */
/**
* @brief ESP_HIDH_OPEN_EVT
*/
struct hidh_open_evt_param {
esp_hidh_status_t status; /*!< operation status */
esp_hidh_connection_state_t conn_status; /*!< connection status */
bool is_orig; /*!< indicate if host intiate the connection */
uint8_t handle; /*!< device handle */
esp_bd_addr_t bd_addr; /*!< device address */
} open; /*!< HIDH callback param of ESP_HIDH_OPEN_EVT */
/**
* @brief ESP_HIDH_CLOSE_EVT
*/
struct hidh_close_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t reason; /*!< lower layer failed reason(ref hiddefs.h) */
esp_hidh_connection_state_t conn_status; /*!< connection status */
uint8_t handle; /*!< device handle */
} close; /*!< HIDH callback param of ESP_HIDH_CLOSE_EVT */
/**
* @brief ESP_HIDH_VC_UNPLUG_EVT
*/
struct hidh_unplug_evt_param {
esp_hidh_status_t status; /*!< operation status */
esp_hidh_connection_state_t conn_status; /*!< connection status */
uint8_t handle; /*!< device handle */
} unplug; /*!< HIDH callback param of ESP_HIDH_VC_UNPLUG_EVT */
/**
* @brief ESP_HIDH_GET_PROTO_EVT
*/
struct hidh_get_proto_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
esp_hidh_protocol_mode_t proto_mode; /*!< protocol mode */
} get_proto; /*!< HIDH callback param of ESP_HIDH_GET_PROTO_EVT */
/**
* @brief ESP_HIDH_SET_PROTO_EVT
*/
struct hidh_set_proto_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
} set_proto; /*!< HIDH callback param of ESP_HIDH_SET_PROTO_EVT */
/**
* @brief ESP_HIDH_GET_RPT_EVT
*/
struct hidh_get_rpt_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
uint16_t len; /*!< data length */
uint8_t *data; /*!< data pointer */
} get_rpt; /*!< HIDH callback param of ESP_HIDH_GET_RPT_EVT */
/**
* @brief ESP_HIDH_SET_RPT_EVT
*/
struct hidh_set_rpt_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
} set_rpt; /*!< HIDH callback param of ESP_HIDH_SET_RPT_EVT */
/**
* @brief ESP_HIDH_DATA_EVT
*/
struct hidh_send_data_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
uint8_t reason; /*!< lower layer failed reason(ref hiddefs.h) */
} send_data; /*!< HIDH callback param of ESP_HIDH_DATA_EVT */
/**
* @brief ESP_HIDH_GET_IDLE_EVT
*/
struct hidh_get_idle_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
uint8_t idle_rate; /*!< idle rate */
} get_idle; /*!< HIDH callback param of ESP_HIDH_GET_IDLE_EVT */
/**
* @brief ESP_HIDH_SET_IDLE_EVT
*/
struct hidh_set_idle_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
} set_idle; /*!< HIDH callback param of ESP_HIDH_SET_IDLE_EVT */
/**
* @brief ESP_HIDH_DATA_IND_EVT
*/
struct hidh_data_ind_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
esp_hidh_protocol_mode_t proto_mode; /*!< protocol mode */
uint16_t len; /*!< data length */
uint8_t *data; /*!< data pointer */
} data_ind; /*!< HIDH callback param of ESP_HIDH_DATA_IND_EVT */
/**
* @brief ESP_HIDH_ADD_DEV_EVT
*/
struct hidh_add_dev_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
esp_bd_addr_t bd_addr; /*!< device address */
} add_dev; /*!< HIDH callback param of ESP_HIDH_ADD_DEV_EVT */
/**
* @brief ESP_HIDH_RMV_DEV_EVT
*/
struct hidh_rmv_dev_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
esp_bd_addr_t bd_addr; /*!< device address */
} rmv_dev; /*!< HIDH callback param of ESP_HIDH_RMV_DEV_EVT */
/**
* @brief ESP_HIDH_GET_DSCP_EVT
*/
struct hidh_get_dscp_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
bool added; /*!< Indicate if added */
uint16_t vendor_id; /*!< Vendor ID */
uint16_t product_id; /*!< Product ID */
uint16_t version; /*!< Version */
uint16_t ssr_max_latency; /*!< SSR max latency */
uint16_t ssr_min_tout; /*!< SSR min timeout */
uint8_t ctry_code; /*!< Country Code */
uint16_t dl_len; /*!< Device descriptor length */
uint8_t *dsc_list; /*!< Device descriptor pointer */
} dscp; /*!< HIDH callback param of ESP_HIDH_GET_DSCP_EVT */
/**
* @brief ESP_HIDH_SET_INFO_EVT
*/
struct hidh_set_info_evt_param {
esp_hidh_status_t status; /*!< operation status */
uint8_t handle; /*!< device handle */
esp_bd_addr_t bd_addr; /*!< device address */
} set_info; /*!< HIDH callback param of ESP_HIDH_SET_INFO_EVT */
} esp_hidh_cb_param_t;
/**
* @brief HID host callback function type
* @param event: Event type
* @param param: Point to callback parameter, currently is union type
*/
typedef void (esp_hh_cb_t)(esp_hidh_cb_event_t event, esp_hidh_cb_param_t *param);
/**
* @brief This function is called to init callbacks with HID host module.
*
* @param[in] callback: pointer to the init callback function.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_register_callback(esp_hh_cb_t callback);
/**
* @brief This function initializes HID host. This function should be called after esp_bluedroid_enable() and
* esp_blueroid_init() success, and should be called after esp_bt_hid_host_register_callback().
* When the operation is complete the callback function will be called with ESP_HIDH_INIT_EVT.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_init(void);
/**
* @brief Closes the interface. This function should be called after esp_bluedroid_enable() and
* esp_blueroid_init() success, and should be called after esp_bt_hid_host_init().
* When the operation is complete the callback function will be called with ESP_HIDH_DEINIT_EVT.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_deinit(void);
/**
* @brief Connect to hid device. When the operation is complete the callback
* function will be called with ESP_HIDH_OPEN_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_connect(esp_bd_addr_t bd_addr);
/**
* @brief Disconnect from hid device. When the operation is complete the callback
* function will be called with ESP_HIDH_CLOSE_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_disconnect(esp_bd_addr_t bd_addr);
/**
* @brief Virtual UnPlug (VUP) the specified HID device. When the operation is complete the callback
* function will be called with ESP_HIDH_VC_UNPLUG_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_virtual_cable_unplug(esp_bd_addr_t bd_addr);
/**
* @brief Set the HID device descriptor for the specified HID device. When the operation is complete the callback
* function will be called with ESP_HIDH_SET_INFO_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
* @param[in] hid_info: HID device descriptor structure.
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_set_info(esp_bd_addr_t bd_addr, esp_hidh_hid_info_t *hid_info);
/**
* @brief Get the HID proto mode. When the operation is complete the callback
* function will be called with ESP_HIDH_GET_PROTO_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_get_protocol(esp_bd_addr_t bd_addr);
/**
* @brief Set the HID proto mode. When the operation is complete the callback
* function will be called with ESP_HIDH_SET_PROTO_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
* @param[in] protocol_mode: Protocol mode type.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_set_protocol(esp_bd_addr_t bd_addr, esp_hidh_protocol_mode_t protocol_mode);
/**
* @brief Get the HID Idle Time. When the operation is complete the callback
* function will be called with ESP_HIDH_GET_IDLE_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
*
* @return
* - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_get_idle(esp_bd_addr_t bd_addr);
/**
* @brief Set the HID Idle Time. When the operation is complete the callback
* function will be called with ESP_HIDH_SET_IDLE_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
* @param[in] idle_time: Idle time rate
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_set_idle(esp_bd_addr_t bd_addr, uint16_t idle_time);
/**
* @brief Send a GET_REPORT to HID device. When the operation is complete the callback
* function will be called with ESP_HIDH_GET_RPT_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
* @param[in] report_type: Report type
* @param[in] report_id: Report id
* @param[in] buffer_size: Buffer size
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_get_report(esp_bd_addr_t bd_addr, esp_hidh_report_type_t report_type, uint8_t report_id,
int buffer_size);
/**
* @brief Send a SET_REPORT to HID device. When the operation is complete the callback
* function will be called with ESP_HIDH_SET_RPT_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
* @param[in] report_type: Report type
* @param[in] report: Report data pointer
* @param[in] len: Report data length
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_set_report(esp_bd_addr_t bd_addr, esp_hidh_report_type_t report_type, uint8_t *report,
size_t len);
/**
* @brief Send data to HID device. When the operation is complete the callback
* function will be called with ESP_HIDH_DATA_EVT.
*
* @param[in] bd_addr: Remote device bluetooth device address.
* @param[in] data: Data pointer
* @param[in] len: Data length
*
* @return - ESP_OK: success
* - other: failed
*/
esp_err_t esp_bt_hid_host_send_data(esp_bd_addr_t bd_addr, uint8_t *data, size_t len);
#ifdef __cplusplus
}
#endif
#endif

View File

@ -527,12 +527,6 @@ void esp_bt_controller_wakeup_request(void);
*/
int esp_bt_h4tl_eif_io_event_notify(int event);
/**
* @brief Get BT MAC address.
* @return Array pointer of length 6 storing MAC address value.
*/
uint8_t* esp_bt_get_mac(void);
#ifdef __cplusplus
}
#endif

View File

@ -1,148 +0,0 @@
/*
* async.h -- state management for asynchronous messages
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file async.h
* @brief State management for asynchronous messages
*/
#ifndef COAP_ASYNC_H_
#define COAP_ASYNC_H_
#include "net.h"
#ifndef WITHOUT_ASYNC
/**
* @defgroup coap_async Asynchronous Messaging
* @{
* Structure for managing asynchronous state of CoAP resources. A
* coap_resource_t object holds a list of coap_async_state_t objects that can be
* used to generate a separate response in case a result of an operation cannot
* be delivered in time, or the resource has been explicitly subscribed to with
* the option @c observe.
*/
typedef struct coap_async_state_t {
unsigned char flags; /**< holds the flags to control behaviour */
/**
* Holds the internal time when the object was registered with a
* resource. This field will be updated whenever
* coap_register_async() is called for a specific resource.
*/
coap_tick_t created;
/**
* This field can be used to register opaque application data with the
* asynchronous state object.
*/
void *appdata;
coap_session_t *session; /**< transaction session */
coap_tid_t id; /**< transaction id */
struct coap_async_state_t *next; /**< internally used for linking */
size_t tokenlen; /**< length of the token */
uint8_t token[8]; /**< the token to use in a response */
} coap_async_state_t;
/* Definitions for Async Status Flags These flags can be used to control the
* behaviour of asynchronous response generation.
*/
#define COAP_ASYNC_CONFIRM 0x01 /**< send confirmable response */
#define COAP_ASYNC_SEPARATE 0x02 /**< send separate response */
#define COAP_ASYNC_OBSERVED 0x04 /**< the resource is being observed */
/** release application data on destruction */
#define COAP_ASYNC_RELEASE_DATA 0x08
/**
* Allocates a new coap_async_state_t object and fills its fields according to
* the given @p request. The @p flags are used to control generation of empty
* ACK responses to stop retransmissions and to release registered @p data when
* the resource is deleted by coap_free_async(). This function returns a pointer
* to the registered coap_async_t object or @c NULL on error. Note that this
* function will return @c NULL in case that an object with the same identifier
* is already registered.
*
* @param context The context to use.
* @param session The session that is used for asynchronous transmissions.
* @param request The request that is handled asynchronously.
* @param flags Flags to control state management.
* @param data Opaque application data to register. Note that the
* storage occupied by @p data is released on destruction
* only if flag COAP_ASYNC_RELEASE_DATA is set.
*
* @return A pointer to the registered coap_async_state_t object or @c
* NULL in case of an error.
*/
coap_async_state_t *
coap_register_async(coap_context_t *context,
coap_session_t *session,
coap_pdu_t *request,
unsigned char flags,
void *data);
/**
* Removes the state object identified by @p id from @p context. The removed
* object is returned in @p s, if found. Otherwise, @p s is undefined. This
* function returns @c 1 if the object was removed, @c 0 otherwise. Note that
* the storage allocated for the stored object is not released by this
* functions. You will have to call coap_free_async() to do so.
*
* @param context The context where the async object is registered.
* @param session The session that is used for asynchronous transmissions.
* @param id The identifier of the asynchronous transaction.
* @param s Will be set to the object identified by @p id after removal.
*
* @return @c 1 if object was removed and @p s updated, or @c 0 if no
* object was found with the given id. @p s is valid only if the
* return value is @c 1.
*/
int coap_remove_async(coap_context_t *context,
coap_session_t *session,
coap_tid_t id,
coap_async_state_t **s);
/**
* Releases the memory that was allocated by coap_async_state_init() for the
* object @p s. The registered application data will be released automatically
* if COAP_ASYNC_RELEASE_DATA is set.
*
* @param state The object to delete.
*/
void
coap_free_async(coap_async_state_t *state);
/**
* Retrieves the object identified by @p id from the list of asynchronous
* transactions that are registered with @p context. This function returns a
* pointer to that object or @c NULL if not found.
*
* @param context The context where the asynchronous objects are registered
* with.
* @param session The session that is used for asynchronous transmissions.
* @param id The id of the object to retrieve.
*
* @return A pointer to the object identified by @p id or @c NULL if
* not found.
*/
coap_async_state_t *coap_find_async(coap_context_t *context, coap_session_t *session, coap_tid_t id);
/**
* Updates the time stamp of @p s.
*
* @param s The state object to update.
*/
COAP_STATIC_INLINE void
coap_touch_async(coap_async_state_t *s) { coap_ticks(&s->created); }
/** @} */
#endif /* WITHOUT_ASYNC */
#endif /* COAP_ASYNC_H_ */

View File

@ -1,78 +0,0 @@
/*
* bits.h -- bit vector manipulation
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file bits.h
* @brief Bit vector manipulation
*/
#ifndef COAP_BITS_H_
#define COAP_BITS_H_
#include <stdint.h>
/**
* Sets the bit @p bit in bit-vector @p vec. This function returns @c 1 if bit
* was set or @c -1 on error (i.e. when the given bit does not fit in the
* vector).
*
* @param vec The bit-vector to change.
* @param size The size of @p vec in bytes.
* @param bit The bit to set in @p vec.
*
* @return @c -1 if @p bit does not fit into @p vec, @c 1 otherwise.
*/
COAP_STATIC_INLINE int
bits_setb(uint8_t *vec, size_t size, uint8_t bit) {
if (size <= ((size_t)bit >> 3))
return -1;
*(vec + (bit >> 3)) |= (uint8_t)(1 << (bit & 0x07));
return 1;
}
/**
* Clears the bit @p bit from bit-vector @p vec. This function returns @c 1 if
* bit was cleared or @c -1 on error (i.e. when the given bit does not fit in
* the vector).
*
* @param vec The bit-vector to change.
* @param size The size of @p vec in bytes.
* @param bit The bit to clear from @p vec.
*
* @return @c -1 if @p bit does not fit into @p vec, @c 1 otherwise.
*/
COAP_STATIC_INLINE int
bits_clrb(uint8_t *vec, size_t size, uint8_t bit) {
if (size <= ((size_t)bit >> 3))
return -1;
*(vec + (bit >> 3)) &= (uint8_t)(~(1 << (bit & 0x07)));
return 1;
}
/**
* Gets the status of bit @p bit from bit-vector @p vec. This function returns
* @c 1 if the bit is set, @c 0 otherwise (even in case of an error).
*
* @param vec The bit-vector to read from.
* @param size The size of @p vec in bytes.
* @param bit The bit to get from @p vec.
*
* @return @c 1 if the bit is set, @c 0 otherwise.
*/
COAP_STATIC_INLINE int
bits_getb(const uint8_t *vec, size_t size, uint8_t bit) {
if (size <= ((size_t)bit >> 3))
return -1;
return (*(vec + (bit >> 3)) & (1 << (bit & 0x07))) != 0;
}
#endif /* COAP_BITS_H_ */

View File

@ -1,173 +0,0 @@
/*
* block.h -- block transfer
*
* Copyright (C) 2010-2012,2014-2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_BLOCK_H_
#define COAP_BLOCK_H_
#include "encode.h"
#include "option.h"
#include "pdu.h"
struct coap_resource_t;
struct coap_session_t;
/**
* @defgroup block Block Transfer
* API functions for handling PDUs using CoAP BLOCK options
* @{
*/
#ifndef COAP_MAX_BLOCK_SZX
/**
* The largest value for the SZX component in a Block option.
*/
#define COAP_MAX_BLOCK_SZX 6
#endif /* COAP_MAX_BLOCK_SZX */
/**
* Structure of Block options.
*/
typedef struct {
unsigned int num; /**< block number */
unsigned int m:1; /**< 1 if more blocks follow, 0 otherwise */
unsigned int szx:3; /**< block size */
} coap_block_t;
/**
* Returns the value of the least significant byte of a Block option @p opt.
* For zero-length options (i.e. num == m == szx == 0), COAP_OPT_BLOCK_LAST
* returns @c NULL.
*/
#define COAP_OPT_BLOCK_LAST(opt) \
(coap_opt_length(opt) ? (coap_opt_value(opt) + (coap_opt_length(opt)-1)) : 0)
/** Returns the value of the More-bit of a Block option @p opt. */
#define COAP_OPT_BLOCK_MORE(opt) \
(coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
/** Returns the value of the SZX-field of a Block option @p opt. */
#define COAP_OPT_BLOCK_SZX(opt) \
(coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
/**
* Returns the value of field @c num in the given block option @p block_opt.
*/
unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
/**
* Checks if more than @p num blocks are required to deliver @p data_len
* bytes of data for a block size of 1 << (@p szx + 4).
*/
COAP_STATIC_INLINE int
coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx) {
return ((num+1) << (szx + 4)) < data_len;
}
#if 0
/** Sets the More-bit in @p block_opt */
COAP_STATIC_INLINE void
coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
if (m)
*(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) |= 0x08;
else
*(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) &= ~0x08;
}
#endif
/**
* Initializes @p block from @p pdu. @p type must be either COAP_OPTION_BLOCK1
* or COAP_OPTION_BLOCK2. When option @p type was found in @p pdu, @p block is
* initialized with values from this option and the function returns the value
* @c 1. Otherwise, @c 0 is returned.
*
* @param pdu The pdu to search for option @p type.
* @param type The option to search for (must be COAP_OPTION_BLOCK1 or
* COAP_OPTION_BLOCK2).
* @param block The block structure to initilize.
*
* @return @c 1 on success, @c 0 otherwise.
*/
int coap_get_block(coap_pdu_t *pdu, uint16_t type, coap_block_t *block);
/**
* Writes a block option of type @p type to message @p pdu. If the requested
* block size is too large to fit in @p pdu, it is reduced accordingly. An
* exception is made for the final block when less space is required. The actual
* length of the resource is specified in @p data_length.
*
* This function may change *block to reflect the values written to @p pdu. As
* the function takes into consideration the remaining space @p pdu, no more
* options should be added after coap_write_block_opt() has returned.
*
* @param block The block structure to use. On return, this object is
* updated according to the values that have been written to
* @p pdu.
* @param type COAP_OPTION_BLOCK1 or COAP_OPTION_BLOCK2.
* @param pdu The message where the block option should be written.
* @param data_length The length of the actual data that will be added the @p
* pdu by calling coap_add_block().
*
* @return @c 1 on success, or a negative value on error.
*/
int coap_write_block_opt(coap_block_t *block,
uint16_t type,
coap_pdu_t *pdu,
size_t data_length);
/**
* Adds the @p block_num block of size 1 << (@p block_szx + 4) from source @p
* data to @p pdu.
*
* @param pdu The message to add the block.
* @param len The length of @p data.
* @param data The source data to fill the block with.
* @param block_num The actual block number.
* @param block_szx Encoded size of block @p block_number.
*
* @return @c 1 on success, @c 0 otherwise.
*/
int coap_add_block(coap_pdu_t *pdu,
unsigned int len,
const uint8_t *data,
unsigned int block_num,
unsigned char block_szx);
/**
* Adds the appropriate part of @p data to the @p response pdu. If blocks are
* required, then the appropriate block will be added to the PDU and sent.
* Adds a ETAG option that is the hash of the entire data if the data is to be
* split into blocks
* Used by a GET request handler.
*
* @param resource The resource the data is associated with.
* @param session The coap session.
* @param request The requesting pdu.
* @param response The response pdu.
* @param token The token taken from the (original) requesting pdu.
* @param media_type The format of the data.
* @param maxage The maxmimum life of the data. If @c -1, then there
* is no maxage.
* @param length The total length of the data.
* @param data The entire data block to transmit.
*
*/
void
coap_add_data_blocked_response(struct coap_resource_t *resource,
struct coap_session_t *session,
coap_pdu_t *request,
coap_pdu_t *response,
const coap_binary_t *token,
uint16_t media_type,
int maxage,
size_t length,
const uint8_t* data);
/**@}*/
#endif /* COAP_BLOCK_H_ */

View File

@ -1,611 +0,0 @@
/*
* coap_dtls.h -- (Datagram) Transport Layer Support for libcoap
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2017 Jean-Claude Michelou <jcm@spinetix.com>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_DTLS_H_
#define COAP_DTLS_H_
#include "coap_time.h"
struct coap_context_t;
struct coap_session_t;
struct coap_dtls_pki_t;
/**
* @defgroup dtls DTLS Support
* API functions for interfacing with DTLS libraries.
* @{
*/
/**
* Check whether DTLS is available.
*
* @return @c 1 if support for DTLS is enabled, or @c 0 otherwise.
*/
int coap_dtls_is_supported(void);
/**
* Check whether TLS is available.
*
* @return @c 1 if support for TLS is enabled, or @c 0 otherwise.
*/
int coap_tls_is_supported(void);
#define COAP_TLS_LIBRARY_NOTLS 0 /**< No DTLS library */
#define COAP_TLS_LIBRARY_TINYDTLS 1 /**< Using TinyDTLS library */
#define COAP_TLS_LIBRARY_OPENSSL 2 /**< Using OpenSSL library */
#define COAP_TLS_LIBRARY_GNUTLS 3 /**< Using GnuTLS library */
/**
* The structure used for returning the underlying (D)TLS library
* information.
*/
typedef struct coap_tls_version_t {
uint64_t version; /**< (D)TLS runtime Library Version */
int type; /**< Library type. One of COAP_TLS_LIBRARY_* */
uint64_t built_version; /**< (D)TLS Built against Library Version */
} coap_tls_version_t;
/**
* Determine the type and version of the underlying (D)TLS library.
*
* @return The version and type of library libcoap was compiled against.
*/
coap_tls_version_t *coap_get_tls_library_version(void);
/**
* Additional Security setup handler that can be set up by
* coap_context_set_pki().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to do some additional checks/changes/updates.
*
* @param tls_session The security session definition - e.g. SSL * for OpenSSL.
* NULL if server call-back.
* This will be dependent on the underlying TLS library -
* see coap_get_tls_library_version()
* @param setup_data A structure containing setup data originally passed into
* coap_context_set_pki() or coap_new_client_session_pki().
*
* @return @c 1 if successful, else @c 0.
*/
typedef int (*coap_dtls_security_setup_t)(void* tls_session,
struct coap_dtls_pki_t *setup_data);
/**
* CN Validation call-back that can be set up by coap_context_set_pki().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to check that the CN is allowed.
* CN is the SubjectAltName in the cert, if not present, then the leftmost
* Common Name (CN) component of the subject name.
*
* @param cn The determined CN from the certificate
* @param asn1_public_cert The ASN.1 DER encoded X.509 certificate
* @param asn1_length The ASN.1 length
* @param coap_session The CoAP session associated with the certificate update
* @param depth Depth in cert chain. If 0, then client cert, else a CA
* @param validated TLS layer can find no issues if 1
* @param arg The same as was passed into coap_context_set_pki()
* in setup_data->cn_call_back_arg
*
* @return @c 1 if accepted, else @c 0 if to be rejected.
*/
typedef int (*coap_dtls_cn_callback_t)(const char *cn,
const uint8_t *asn1_public_cert,
size_t asn1_length,
struct coap_session_t *coap_session,
unsigned depth,
int validated,
void *arg);
/**
* The enum used for determining the provided PKI ASN.1 (DER) Private Key
* formats.
*/
typedef enum coap_asn1_privatekey_type_t {
COAP_ASN1_PKEY_NONE, /**< NONE */
COAP_ASN1_PKEY_RSA, /**< RSA type */
COAP_ASN1_PKEY_RSA2, /**< RSA2 type */
COAP_ASN1_PKEY_DSA, /**< DSA type */
COAP_ASN1_PKEY_DSA1, /**< DSA1 type */
COAP_ASN1_PKEY_DSA2, /**< DSA2 type */
COAP_ASN1_PKEY_DSA3, /**< DSA3 type */
COAP_ASN1_PKEY_DSA4, /**< DSA4 type */
COAP_ASN1_PKEY_DH, /**< DH type */
COAP_ASN1_PKEY_DHX, /**< DHX type */
COAP_ASN1_PKEY_EC, /**< EC type */
COAP_ASN1_PKEY_HMAC, /**< HMAC type */
COAP_ASN1_PKEY_CMAC, /**< CMAC type */
COAP_ASN1_PKEY_TLS1_PRF, /**< TLS1_PRF type */
COAP_ASN1_PKEY_HKDF /**< HKDF type */
} coap_asn1_privatekey_type_t;
/**
* The enum used for determining the PKI key formats.
*/
typedef enum coap_pki_key_t {
COAP_PKI_KEY_PEM = 0, /**< The PKI key type is PEM */
COAP_PKI_KEY_ASN1, /**< The PKI key type is ASN.1 (DER) */
} coap_pki_key_t;
/**
* The structure that holds the PKI PEM definitions.
*/
typedef struct coap_pki_key_pem_t {
const char *ca_file; /**< File location of Common CA in PEM format */
const char *public_cert; /**< File location of Public Cert in PEM format */
const char *private_key; /**< File location of Private Key in PEM format */
} coap_pki_key_pem_t;
/**
* The structure that holds the PKI ASN.1 (DER) definitions.
*/
typedef struct coap_pki_key_asn1_t {
const uint8_t *ca_cert; /**< ASN1 (DER) Common CA Cert */
const uint8_t *public_cert; /**< ASN1 (DER) Public Cert */
const uint8_t *private_key; /**< ASN1 (DER) Private Key */
size_t ca_cert_len; /**< ASN1 CA Cert length */
size_t public_cert_len; /**< ASN1 Public Cert length */
size_t private_key_len; /**< ASN1 Private Key length */
coap_asn1_privatekey_type_t private_key_type; /**< Private Key Type */
} coap_pki_key_asn1_t;
/**
* The structure that holds the PKI key information.
*/
typedef struct coap_dtls_key_t {
coap_pki_key_t key_type; /**< key format type */
union {
coap_pki_key_pem_t pem; /**< for PEM keys */
coap_pki_key_asn1_t asn1; /**< for ASN.1 (DER) keys */
} key;
} coap_dtls_key_t;
/**
* Server Name Indication (SNI) Validation call-back that can be set up by
* coap_context_set_pki().
* Invoked if the SNI is not previously seen and prior to sending a certificate
* set back to the client so that the appropriate certificate set can be used
* based on the requesting SNI.
*
* @param sni The requested SNI
* @param arg The same as was passed into coap_context_set_pki()
* in setup_data->sni_call_back_arg
*
* @return New set of certificates to use, or @c NULL if SNI is to be rejected.
*/
typedef coap_dtls_key_t *(*coap_dtls_sni_callback_t)(const char *sni,
void* arg);
#define COAP_DTLS_PKI_SETUP_VERSION 1 /**< Latest PKI setup version */
/**
* The structure used for defining the PKI setup data to be used.
*/
typedef struct coap_dtls_pki_t {
uint8_t version; /** Set to 1 to support this version of the struct */
/* Options to enable different TLS functionality in libcoap */
uint8_t verify_peer_cert; /**< 1 if peer cert is to be verified */
uint8_t require_peer_cert; /**< 1 if peer cert is required */
uint8_t allow_self_signed; /**< 1 if self signed certs are allowed */
uint8_t allow_expired_certs; /**< 1 if expired certs are allowed */
uint8_t cert_chain_validation; /**< 1 if to check cert_chain_verify_depth */
uint8_t cert_chain_verify_depth; /**< recommended depth is 3 */
uint8_t check_cert_revocation; /**< 1 if revocation checks wanted */
uint8_t allow_no_crl; /**< 1 ignore if CRL not there */
uint8_t allow_expired_crl; /**< 1 if expired crl is allowed */
uint8_t reserved[6]; /**< Reserved - must be set to 0 for
future compatibility */
/* Size of 6 chosen to align to next
* parameter, so if newly defined option
* it can use one of the reserverd slot so
* no need to change
* COAP_DTLS_PKI_SETUP_VERSION and just
* decrement the reserved[] count.
*/
/** CN check call-back function.
* If not NULL, is called when the TLS connection has passed the configured
* TLS options above for the application to verify if the CN is valid.
*/
coap_dtls_cn_callback_t validate_cn_call_back;
void *cn_call_back_arg; /**< Passed in to the CN call-back function */
/** SNI check call-back function.
* If not @p NULL, called if the SNI is not previously seen and prior to
* sending a certificate set back to the client so that the appropriate
* certificate set can be used based on the requesting SNI.
*/
coap_dtls_sni_callback_t validate_sni_call_back;
void *sni_call_back_arg; /**< Passed in to the sni call-back function */
/** Additional Security call-back handler that is invoked when libcoap has
* done the standerd, defined validation checks at the TLS level,
* If not @p NULL, called from within the TLS Client Hello connection
* setup.
*/
coap_dtls_security_setup_t additional_tls_setup_call_back;
char* client_sni; /**< If not NULL, SNI to use in client TLS setup.
Owned by the client app and must remain valid
during the call to coap_new_client_session_pki() */
coap_dtls_key_t pki_key; /**< PKI key definition */
} coap_dtls_pki_t;
/** @} */
/**
* @defgroup dtls_internal DTLS Support (Internal)
* Internal API functions for interfacing with DTLS libraries.
* @{
*/
/**
* Creates a new DTLS context for the given @p coap_context. This function
* returns a pointer to a new DTLS context object or @c NULL on error.
*
* Internal function.
*
* @param coap_context The CoAP context where the DTLS object shall be used.
*
* @return A DTLS context object or @c NULL on error.
*/
void *
coap_dtls_new_context(struct coap_context_t *coap_context);
typedef enum coap_dtls_role_t {
COAP_DTLS_ROLE_CLIENT, /**< Internal function invoked for client */
COAP_DTLS_ROLE_SERVER /**< Internal function invoked for server */
} coap_dtls_role_t;
/**
* Set the DTLS context's default PSK information.
* This does the PSK specifics following coap_dtls_new_context().
* If @p COAP_DTLS_ROLE_SERVER, then identity hint will also get set.
* If @p COAP_DTLS_ROLE_SERVER, then the information will get put into the
* TLS library's context (from which sessions are derived).
* If @p COAP_DTLS_ROLE_CLIENT, then the information will get put into the
* TLS library's session.
*
* Internal function.
*
* @param coap_context The CoAP context.
* @param identity_hint The default PSK server identity hint sent to a client.
* Required parameter. If @p NULL, will be set to "".
* Empty string is a valid hint.
* This parameter is ignored if COAP_DTLS_ROLE_CLIENT
* @param role One of @p COAP_DTLS_ROLE_CLIENT or @p COAP_DTLS_ROLE_SERVER
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_psk(struct coap_context_t *coap_context,
const char *identity_hint,
coap_dtls_role_t role);
/**
* Set the DTLS context's default server PKI information.
* This does the PKI specifics following coap_dtls_new_context().
* If @p COAP_DTLS_ROLE_SERVER, then the information will get put into the
* TLS library's context (from which sessions are derived).
* If @p COAP_DTLS_ROLE_CLIENT, then the information will get put into the
* TLS library's session.
*
* Internal function.
*
* @param coap_context The CoAP context.
* @param setup_data Setup information defining how PKI is to be setup.
* Required parameter. If @p NULL, PKI will not be
* set up.
* @param role One of @p COAP_DTLS_ROLE_CLIENT or @p COAP_DTLS_ROLE_SERVER
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_pki(struct coap_context_t *coap_context,
coap_dtls_pki_t *setup_data,
coap_dtls_role_t role);
/**
* Set the dtls context's default Root CA information for a client or server.
*
* Internal function.
*
* @param coap_context The current coap_context_t object.
* @param ca_file If not @p NULL, is the full path name of a PEM encoded
* file containing all the Root CAs to be used.
* @param ca_dir If not @p NULL, points to a directory containing PEM
* encoded files containing all the Root CAs to be used.
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_pki_root_cas(struct coap_context_t *coap_context,
const char *ca_file,
const char *ca_dir);
/**
* Check whether one of the coap_dtls_context_set_{psk|pki}() functions have
* been called.
*
* Internal function.
*
* @param coap_context The current coap_context_t object.
*
* @return @c 1 if coap_dtls_context_set_{psk|pki}() called, else @c 0.
*/
int coap_dtls_context_check_keys_enabled(struct coap_context_t *coap_context);
/**
* Releases the storage allocated for @p dtls_context.
*
* Internal function.
*
* @param dtls_context The DTLS context as returned by coap_dtls_new_context().
*/
void coap_dtls_free_context(void *dtls_context);
/**
* Create a new client-side session. This should send a HELLO to the server.
*
* Internal function.
*
* @param coap_session The CoAP session.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_dtls_new_client_session(struct coap_session_t *coap_session);
/**
* Create a new DTLS server-side session.
* Called after coap_dtls_hello() has returned @c 1, signalling that a validated
* HELLO was received from a client.
* This should send a HELLO to the server.
*
* Internal function.
*
* @param coap_session The CoAP session.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the DTLS session.
*/
void *coap_dtls_new_server_session(struct coap_session_t *coap_session);
/**
* Terminates the DTLS session (may send an ALERT if necessary) then frees the
* underlying TLS library object containing security parameters for the session.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_free_session(struct coap_session_t *coap_session);
/**
* Notify of a change in the CoAP session's MTU, for example after
* a PMTU update.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_session_update_mtu(struct coap_session_t *coap_session);
/**
* Send data to a DTLS peer.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data pointer to data.
* @param data_len Number of bytes to send.
*
* @return @c 0 if this would be blocking, @c -1 if there is an error or the
* number of cleartext bytes sent.
*/
int coap_dtls_send(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Check if timeout is handled per CoAP session or per CoAP context.
*
* Internal function.
*
* @return @c 1 of timeout and retransmit is per context, @c 0 if it is
* per session.
*/
int coap_dtls_is_context_timeout(void);
/**
* Do all pending retransmits and get next timeout
*
* Internal function.
*
* @param dtls_context The DTLS context.
*
* @return @c 0 if no event is pending or date of the next retransmit.
*/
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context);
/**
* Get next timeout for this session.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param now The current time in ticks.
*
* @return @c 0 If no event is pending or ticks time of the next retransmit.
*/
coap_tick_t coap_dtls_get_timeout(struct coap_session_t *coap_session,
coap_tick_t now);
/**
* Handle a DTLS timeout expiration.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_handle_timeout(struct coap_session_t *coap_session);
/**
* Handling incoming data from a DTLS peer.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Encrypted datagram.
* @param data_len Encrypted datagram size.
*
* @return Result of coap_handle_dgram on the decrypted CoAP PDU
* or @c -1 for error.
*/
int coap_dtls_receive(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Handling client HELLO messages from a new candiate peer.
* Note that session->tls is empty.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Encrypted datagram.
* @param data_len Encrypted datagram size.
*
* @return @c 0 if a cookie verification message has been sent, @c 1 if the
* HELLO contains a valid cookie and a server session should be created,
* @c -1 if the message is invalid.
*/
int coap_dtls_hello(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Get DTLS overhead over cleartext PDUs.
*
* Internal function.
*
* @param coap_session The CoAP session.
*
* @return Maximum number of bytes added by DTLS layer.
*/
unsigned int coap_dtls_get_overhead(struct coap_session_t *coap_session);
/**
* Create a new TLS client-side session.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param connected Updated with whether the connection is connected yet or not.
* @c 0 is not connected, @c 1 is connected.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_tls_new_client_session(struct coap_session_t *coap_session, int *connected);
/**
* Create a TLS new server-side session.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param connected Updated with whether the connection is connected yet or not.
* @c 0 is not connected, @c 1 is connected.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_tls_new_server_session(struct coap_session_t *coap_session, int *connected);
/**
* Terminates the TLS session (may send an ALERT if necessary) then frees the
* underlying TLS library object containing security parameters for the session.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_tls_free_session( struct coap_session_t *coap_session );
/**
* Send data to a TLS peer, with implicit flush.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Pointer to data.
* @param data_len Number of bytes to send.
*
* @return @c 0 if this should be retried, @c -1 if there is an error
* or the number of cleartext bytes sent.
*/
ssize_t coap_tls_write(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len
);
/**
* Read some data from a TLS peer.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Pointer to data.
* @param data_len Maximum number of bytes to read.
*
* @return @c 0 if this should be retried, @c -1 if there is an error
* or the number of cleartext bytes read.
*/
ssize_t coap_tls_read(struct coap_session_t *coap_session,
uint8_t *data,
size_t data_len
);
/**
* Initialize the underlying (D)TLS Library layer.
*
* Internal function.
*
*/
void coap_dtls_startup(void);
/** @} */
/**
* @ingroup logging
* Sets the (D)TLS logging level to the specified @p level.
* Note: coap_log_level() will influence output if at a specified level.
*
* @param level The logging level to use - LOG_*
*/
void coap_dtls_set_log_level(int level);
/**
* @ingroup logging
* Get the current (D)TLS logging.
*
* @return The current log level (one of LOG_*).
*/
int coap_dtls_get_log_level(void);
#endif /* COAP_DTLS_H */

View File

@ -1,492 +0,0 @@
/* coap_session.h -- Session management for libcoap
*
* Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#ifndef COAP_SESSION_H_
#define COAP_SESSION_H_
#include "coap_io.h"
#include "coap_time.h"
#include "pdu.h"
struct coap_endpoint_t;
struct coap_context_t;
struct coap_queue_t;
/**
* Abstraction of a fixed point number that can be used where necessary instead
* of a float. 1,000 fractional bits equals one integer
*/
typedef struct coap_fixed_point_t {
uint16_t integer_part; /**< Integer part of fixed point variable */
uint16_t fractional_part; /**< Fractional part of fixed point variable
1/1000 (3 points) precision */
} coap_fixed_point_t;
#define COAP_DEFAULT_SESSION_TIMEOUT 300
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
#define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
#define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
typedef uint8_t coap_session_type_t;
/**
* coap_session_type_t values
*/
#define COAP_SESSION_TYPE_CLIENT 1 /**< client-side */
#define COAP_SESSION_TYPE_SERVER 2 /**< server-side */
#define COAP_SESSION_TYPE_HELLO 3 /**< server-side ephemeral session for responding to a client hello */
typedef uint8_t coap_session_state_t;
/**
* coap_session_state_t values
*/
#define COAP_SESSION_STATE_NONE 0
#define COAP_SESSION_STATE_CONNECTING 1
#define COAP_SESSION_STATE_HANDSHAKE 2
#define COAP_SESSION_STATE_CSM 3
#define COAP_SESSION_STATE_ESTABLISHED 4
typedef struct coap_session_t {
struct coap_session_t *next;
coap_proto_t proto; /**< protocol used */
coap_session_type_t type; /**< client or server side socket */
coap_session_state_t state; /**< current state of relationaship with peer */
unsigned ref; /**< reference count from queues */
unsigned tls_overhead; /**< overhead of TLS layer */
unsigned mtu; /**< path or CSM mtu */
coap_address_t local_if; /**< optional local interface address */
coap_address_t remote_addr; /**< remote address and port */
coap_address_t local_addr; /**< local address and port */
int ifindex; /**< interface index */
coap_socket_t sock; /**< socket object for the session, if any */
struct coap_endpoint_t *endpoint; /**< session's endpoint */
struct coap_context_t *context; /**< session's context */
void *tls; /**< security parameters */
uint16_t tx_mid; /**< the last message id that was used in this session */
uint8_t con_active; /**< Active CON request sent */
struct coap_queue_t *delayqueue; /**< list of delayed messages waiting to be sent */
size_t partial_write; /**< if > 0 indicates number of bytes already written from the pdu at the head of sendqueue */
uint8_t read_header[8]; /**< storage space for header of incoming message header */
size_t partial_read; /**< if > 0 indicates number of bytes already read for an incoming message */
coap_pdu_t *partial_pdu; /**< incomplete incoming pdu */
coap_tick_t last_rx_tx;
coap_tick_t last_tx_rst;
coap_tick_t last_ping;
coap_tick_t last_pong;
coap_tick_t csm_tx;
uint8_t *psk_identity;
size_t psk_identity_len;
uint8_t *psk_key;
size_t psk_key_len;
void *app; /**< application-specific data */
unsigned int max_retransmit; /**< maximum re-transmit count (default 4) */
coap_fixed_point_t ack_timeout; /**< timeout waiting for ack (default 2 secs) */
coap_fixed_point_t ack_random_factor; /**< ack random factor backoff (default 1.5) */
unsigned int dtls_timeout_count; /**< dtls setup retry counter */
int dtls_event; /**< Tracking any (D)TLS events on this sesison */
} coap_session_t;
/**
* Increment reference counter on a session.
*
* @param session The CoAP session.
* @return same as session
*/
coap_session_t *coap_session_reference(coap_session_t *session);
/**
* Decrement reference counter on a session.
* Note that the session may be deleted as a result and should not be used
* after this call.
*
* @param session The CoAP session.
*/
void coap_session_release(coap_session_t *session);
/**
* Stores @p data with the given session. This function overwrites any value
* that has previously been stored with @p session.
*/
void coap_session_set_app_data(coap_session_t *session, void *data);
/**
* Returns any application-specific data that has been stored with @p
* session using the function coap_session_set_app_data(). This function will
* return @c NULL if no data has been stored.
*/
void *coap_session_get_app_data(const coap_session_t *session);
/**
* Notify session that it has failed.
*
* @param session The CoAP session.
* @param reason The reason why the session was disconnected.
*/
void coap_session_disconnected(coap_session_t *session, coap_nack_reason_t reason);
/**
* Notify session transport has just connected and CSM exchange can now start.
*
* @param session The CoAP session.
*/
void coap_session_send_csm(coap_session_t *session);
/**
* Notify session that it has just connected or reconnected.
*
* @param session The CoAP session.
*/
void coap_session_connected(coap_session_t *session);
/**
* Set the session MTU. This is the maximum message size that can be sent,
* excluding IP and UDP overhead.
*
* @param session The CoAP session.
* @param mtu maximum message size
*/
void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
/**
* Get maximum acceptable PDU size
*
* @param session The CoAP session.
* @return maximum PDU size, not including header (but including token).
*/
size_t coap_session_max_pdu_size(const coap_session_t *session);
/**
* Creates a new client session to the designated server.
* @param ctx The CoAP context.
* @param local_if Address of local interface. It is recommended to use NULL to let the operating system choose a suitable local interface. If an address is specified, the port number should be zero, which means that a free port is automatically selected.
* @param server The server's address. If the port number is zero, the default port for the protocol will be used.
* @param proto Protocol.
*
* @return A new CoAP session or NULL if failed. Call coap_session_release to free.
*/
coap_session_t *coap_new_client_session(
struct coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto
);
/**
* Creates a new client session to the designated server with PSK credentials
* @param ctx The CoAP context.
* @param local_if Address of local interface. It is recommended to use NULL to let the operating system choose a suitable local interface. If an address is specified, the port number should be zero, which means that a free port is automatically selected.
* @param server The server's address. If the port number is zero, the default port for the protocol will be used.
* @param proto Protocol.
* @param identity PSK client identity
* @param key PSK shared key
* @param key_len PSK shared key length
*
* @return A new CoAP session or NULL if failed. Call coap_session_release to free.
*/
coap_session_t *coap_new_client_session_psk(
struct coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
const char *identity,
const uint8_t *key,
unsigned key_len
);
struct coap_dtls_pki_t;
/**
* Creates a new client session to the designated server with PKI credentials
* @param ctx The CoAP context.
* @param local_if Address of local interface. It is recommended to use NULL to
* let the operating system choose a suitable local interface.
* If an address is specified, the port number should be zero,
* which means that a free port is automatically selected.
* @param server The server's address. If the port number is zero, the default
* port for the protocol will be used.
* @param proto CoAP Protocol.
* @param setup_data PKI parameters.
*
* @return A new CoAP session or NULL if failed. Call coap_session_release()
* to free.
*/
coap_session_t *coap_new_client_session_pki(
struct coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
struct coap_dtls_pki_t *setup_data
);
/**
* Creates a new server session for the specified endpoint.
* @param ctx The CoAP context.
* @param ep An endpoint where an incoming connection request is pending.
*
* @return A new CoAP session or NULL if failed. Call coap_session_release to free.
*/
coap_session_t *coap_new_server_session(
struct coap_context_t *ctx,
struct coap_endpoint_t *ep
);
/**
* Function interface for datagram data transmission. This function returns
* the number of bytes that have been transmitted, or a value less than zero
* on error.
*
* @param session Session to send data on.
* @param data The data to send.
* @param datalen The actual length of @p data.
*
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_session_send(coap_session_t *session,
const uint8_t *data, size_t datalen);
/**
* Function interface for stream data transmission. This function returns
* the number of bytes that have been transmitted, or a value less than zero
* on error. The number of bytes written may be less than datalen because of
* congestion control.
*
* @param session Session to send data on.
* @param data The data to send.
* @param datalen The actual length of @p data.
*
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_session_write(coap_session_t *session,
const uint8_t *data, size_t datalen);
/**
* Send a pdu according to the session's protocol. This function returns
* the number of bytes that have been transmitted, or a value less than zero
* on error.
*
* @param session Session to send pdu on.
* @param pdu The pdu to send.
*
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu);
/**
* @ingroup logging
* Get session description.
*
* @param session The CoAP session.
* @return description string.
*/
const char *coap_session_str(const coap_session_t *session);
ssize_t
coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
struct coap_queue_t *node);
/**
* Abstraction of virtual endpoint that can be attached to coap_context_t. The
* tuple (handle, addr) must uniquely identify this endpoint.
*/
typedef struct coap_endpoint_t {
struct coap_endpoint_t *next;
struct coap_context_t *context; /**< endpoint's context */
coap_proto_t proto; /**< protocol used on this interface */
uint16_t default_mtu; /**< default mtu for this interface */
coap_socket_t sock; /**< socket object for the interface, if any */
coap_address_t bind_addr; /**< local interface address */
coap_session_t *sessions; /**< list of active sessions */
} coap_endpoint_t;
/**
* Create a new endpoint for communicating with peers.
*
* @param context The coap context that will own the new endpoint
* @param listen_addr Address the endpoint will listen for incoming requests on or originate outgoing requests from. Use NULL to specify that no incoming request will be accepted and use a random endpoint.
* @param proto Protocol used on this endpoint
*/
coap_endpoint_t *coap_new_endpoint(struct coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
/**
* Set the endpoint's default MTU. This is the maximum message size that can be
* sent, excluding IP and UDP overhead.
*
* @param endpoint The CoAP endpoint.
* @param mtu maximum message size
*/
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu);
void coap_free_endpoint(coap_endpoint_t *ep);
/**
* @ingroup logging
* Get endpoint description.
*
* @param endpoint The CoAP endpoint.
* @return description string.
*/
const char *coap_endpoint_str(const coap_endpoint_t *endpoint);
/**
* Lookup the server session for the packet received on an endpoint, or create
* a new one.
*
* @param endpoint Active endpoint the packet was received on.
* @param packet Received packet.
* @param now The current time in ticks.
* @return The CoAP session or @c NULL if error.
*/
coap_session_t *coap_endpoint_get_session(coap_endpoint_t *endpoint,
const struct coap_packet_t *packet, coap_tick_t now);
/**
* Create a new DTLS session for the @p session.
* Note: the @p session is released if no DTLS server session can be created.
*
* @ingroup dtls_internal
*
* @param session Session to add DTLS session to
* @param now The current time in ticks.
*
* @return CoAP session or @c NULL if error.
*/
coap_session_t *coap_session_new_dtls_session(coap_session_t *session,
coap_tick_t now);
coap_session_t *coap_session_get_by_peer(struct coap_context_t *ctx,
const struct coap_address_t *remote_addr, int ifindex);
void coap_session_free(coap_session_t *session);
void coap_session_mfree(coap_session_t *session);
/**
* @defgroup cc Rate Control
* The transmission parameters for CoAP rate control ("Congestion
* Control" in stream-oriented protocols) are defined in
* https://tools.ietf.org/html/rfc7252#section-4.8
* @{
*/
/**
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
* RFC 7252, Section 4.8 Default value of ACK_TIMEOUT is 2
*/
#define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
/**
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
* RFC 7252, Section 4.8 Default value of ACK_RANDOM_FACTOR is 1.5
*/
#define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
/**
* Number of message retransmissions before message sending is stopped
* RFC 7252, Section 4.8 Default value of MAX_RETRANSMIT is 4
*/
#define COAP_DEFAULT_MAX_RETRANSMIT 4
/**
* The number of simultaneous outstanding interactions that a client
* maintains to a given server.
* RFC 7252, Section 4.8 Default value of NSTART is 1
*/
#define COAP_DEFAULT_NSTART 1
/** @} */
/**
* Set the CoAP maximum retransmit count before failure
*
* Number of message retransmissions before message sending is stopped
*
* @param session The CoAP session.
* @param value The value to set to. The default is 4 and should not normally
* get changed.
*/
void coap_session_set_max_retransmit(coap_session_t *session,
unsigned int value);
/**
* Set the CoAP initial ack response timeout before the next re-transmit
*
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
*
* @param session The CoAP session.
* @param value The value to set to. The default is 2 and should not normally
* get changed.
*/
void coap_session_set_ack_timeout(coap_session_t *session,
coap_fixed_point_t value);
/**
* Set the CoAP ack randomize factor
*
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
*
* @param session The CoAP session.
* @param value The value to set to. The default is 1.5 and should not normally
* get changed.
*/
void coap_session_set_ack_random_factor(coap_session_t *session,
coap_fixed_point_t value);
/**
* Get the CoAP maximum retransmit before failure
*
* Number of message retransmissions before message sending is stopped
*
* @param session The CoAP session.
*
* @return Current maximum retransmit value
*/
unsigned int coap_session_get_max_transmit(coap_session_t *session);
/**
* Get the CoAP initial ack response timeout before the next re-transmit
*
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
*
* @param session The CoAP session.
*
* @return Current ack response timeout value
*/
coap_fixed_point_t coap_session_get_ack_timeout(coap_session_t *session);
/**
* Get the CoAP ack randomize factor
*
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
*
* @param session The CoAP session.
*
* @return Current ack randomize value
*/
coap_fixed_point_t coap_session_get_ack_random_factor(coap_session_t *session);
/**
* Send a ping message for the session.
* @param session The CoAP session.
*
* @return COAP_INVALID_TID if there is an error
*/
coap_tid_t coap_session_send_ping(coap_session_t *session);
#endif /* COAP_SESSION_H */

View File

@ -1,746 +0,0 @@
/*
* net.h -- CoAP network interface
*
* Copyright (C) 2010-2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_NET_H_
#define COAP_NET_H_
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <time.h>
#ifdef WITH_LWIP
#include <lwip/ip_addr.h>
#endif
#include "coap_io.h"
#include "coap_dtls.h"
#include "coap_event.h"
#include "coap_time.h"
#include "option.h"
#include "pdu.h"
#include "prng.h"
#include "coap_session.h"
struct coap_queue_t;
/**
* Queue entry
*/
typedef struct coap_queue_t {
struct coap_queue_t *next;
coap_tick_t t; /**< when to send PDU for the next time */
unsigned char retransmit_cnt; /**< retransmission counter, will be removed
* when zero */
unsigned int timeout; /**< the randomized timeout value */
coap_session_t *session; /**< the CoAP session */
coap_tid_t id; /**< CoAP transaction id */
coap_pdu_t *pdu; /**< the CoAP PDU to send */
} coap_queue_t;
/**
* Adds @p node to given @p queue, ordered by variable t in @p node.
*
* @param queue Queue to add to.
* @param node Node entry to add to Queue.
*
* @return @c 1 added to queue, @c 0 failure.
*/
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
/**
* Destroys specified @p node.
*
* @param node Node entry to remove.
*
* @return @c 1 node deleted from queue, @c 0 failure.
*/
int coap_delete_node(coap_queue_t *node);
/**
* Removes all items from given @p queue and frees the allocated storage.
*
* @param queue The queue to delete.
*/
void coap_delete_all(coap_queue_t *queue);
/**
* Creates a new node suitable for adding to the CoAP sendqueue.
*
* @return New node entry, or @c NULL if failure.
*/
coap_queue_t *coap_new_node(void);
struct coap_resource_t;
struct coap_context_t;
#ifndef WITHOUT_ASYNC
struct coap_async_state_t;
#endif
/**
* Response handler that is used as call-back in coap_context_t.
*
* @param context CoAP session.
* @param session CoAP session.
* @param sent The PDU that was transmitted.
* @param received The PDU that was received.
* @param id CoAP transaction ID.
*/
typedef void (*coap_response_handler_t)(struct coap_context_t *context,
coap_session_t *session,
coap_pdu_t *sent,
coap_pdu_t *received,
const coap_tid_t id);
/**
* Negative Acknowedge handler that is used as call-back in coap_context_t.
*
* @param context CoAP session.
* @param session CoAP session.
* @param sent The PDU that was transmitted.
* @param reason The reason for the NACK.
* @param id CoAP transaction ID.
*/
typedef void (*coap_nack_handler_t)(struct coap_context_t *context,
coap_session_t *session,
coap_pdu_t *sent,
coap_nack_reason_t reason,
const coap_tid_t id);
/**
* Recieved Ping handler that is used as call-back in coap_context_t.
*
* @param context CoAP session.
* @param session CoAP session.
* @param received The PDU that was received.
* @param id CoAP transaction ID.
*/
typedef void (*coap_ping_handler_t)(struct coap_context_t *context,
coap_session_t *session,
coap_pdu_t *received,
const coap_tid_t id);
/**
* Recieved Pong handler that is used as call-back in coap_context_t.
*
* @param context CoAP session.
* @param session CoAP session.
* @param received The PDU that was received.
* @param id CoAP transaction ID.
*/
typedef void (*coap_pong_handler_t)(struct coap_context_t *context,
coap_session_t *session,
coap_pdu_t *received,
const coap_tid_t id);
/**
* The CoAP stack's global state is stored in a coap_context_t object.
*/
typedef struct coap_context_t {
coap_opt_filter_t known_options;
struct coap_resource_t *resources; /**< hash table or list of known
resources */
struct coap_resource_t *unknown_resource; /**< can be used for handling
unknown resources */
#ifndef WITHOUT_ASYNC
/**
* list of asynchronous transactions */
struct coap_async_state_t *async_state;
#endif /* WITHOUT_ASYNC */
/**
* The time stamp in the first element of the sendqeue is relative
* to sendqueue_basetime. */
coap_tick_t sendqueue_basetime;
coap_queue_t *sendqueue;
coap_endpoint_t *endpoint; /**< the endpoints used for listening */
coap_session_t *sessions; /**< client sessions */
#ifdef WITH_CONTIKI
struct uip_udp_conn *conn; /**< uIP connection object */
struct etimer retransmit_timer; /**< fires when the next packet must be sent */
struct etimer notify_timer; /**< used to check resources periodically */
#endif /* WITH_CONTIKI */
#ifdef WITH_LWIP
uint8_t timer_configured; /**< Set to 1 when a retransmission is
* scheduled using lwIP timers for this
* context, otherwise 0. */
#endif /* WITH_LWIP */
coap_response_handler_t response_handler;
coap_nack_handler_t nack_handler;
coap_ping_handler_t ping_handler;
coap_pong_handler_t pong_handler;
/**
* Callback function that is used to signal events to the
* application. This field is set by coap_set_event_handler().
*/
coap_event_handler_t handle_event;
ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen);
ssize_t (*network_read)(coap_socket_t *sock, struct coap_packet_t *packet);
size_t(*get_client_psk)(const coap_session_t *session, const uint8_t *hint, size_t hint_len, uint8_t *identity, size_t *identity_len, size_t max_identity_len, uint8_t *psk, size_t max_psk_len);
size_t(*get_server_psk)(const coap_session_t *session, const uint8_t *identity, size_t identity_len, uint8_t *psk, size_t max_psk_len);
size_t(*get_server_hint)(const coap_session_t *session, uint8_t *hint, size_t max_hint_len);
void *dtls_context;
uint8_t *psk_hint;
size_t psk_hint_len;
uint8_t *psk_key;
size_t psk_key_len;
unsigned int session_timeout; /**< Number of seconds of inactivity after which an unused session will be closed. 0 means use default. */
unsigned int max_idle_sessions; /**< Maximum number of simultaneous unused sessions per endpoint. 0 means no maximum. */
unsigned int max_handshake_sessions; /**< Maximum number of simultaneous negotating sessions per endpoint. 0 means use default. */
unsigned int ping_timeout; /**< Minimum inactivity time before sending a ping message. 0 means disabled. */
unsigned int csm_timeout; /**< Timeout for waiting for a CSM from the remote side. 0 means disabled. */
void *app; /**< application-specific data */
} coap_context_t;
/**
* Registers a new message handler that is called whenever a response was
* received that matches an ongoing transaction.
*
* @param context The context to register the handler for.
* @param handler The response handler to register.
*/
COAP_STATIC_INLINE void
coap_register_response_handler(coap_context_t *context,
coap_response_handler_t handler) {
context->response_handler = handler;
}
/**
* Registers a new message handler that is called whenever a confirmable
* message (request or response) is dropped after all retries have been
* exhausted, or a rst message was received, or a network or TLS level
* event was received that indicates delivering the message is not possible.
*
* @param context The context to register the handler for.
* @param handler The nack handler to register.
*/
COAP_STATIC_INLINE void
coap_register_nack_handler(coap_context_t *context,
coap_nack_handler_t handler) {
context->nack_handler = handler;
}
/**
* Registers a new message handler that is called whenever a CoAP Ping
* message is received.
*
* @param context The context to register the handler for.
* @param handler The ping handler to register.
*/
COAP_STATIC_INLINE void
coap_register_ping_handler(coap_context_t *context,
coap_ping_handler_t handler) {
context->ping_handler = handler;
}
/**
* Registers a new message handler that is called whenever a CoAP Pong
* message is received.
*
* @param context The context to register the handler for.
* @param handler The pong handler to register.
*/
COAP_STATIC_INLINE void
coap_register_pong_handler(coap_context_t *context,
coap_pong_handler_t handler) {
context->pong_handler = handler;
}
/**
* Registers the option type @p type with the given context object @p ctx.
*
* @param ctx The context to use.
* @param type The option type to register.
*/
COAP_STATIC_INLINE void
coap_register_option(coap_context_t *ctx, uint16_t type) {
coap_option_setb(ctx->known_options, type);
}
/**
* Set sendqueue_basetime in the given context object @p ctx to @p now. This
* function returns the number of elements in the queue head that have timed
* out.
*/
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
/**
* Returns the next pdu to send without removing from sendqeue.
*/
coap_queue_t *coap_peek_next( coap_context_t *context );
/**
* Returns the next pdu to send and removes it from the sendqeue.
*/
coap_queue_t *coap_pop_next( coap_context_t *context );
/**
* Creates a new coap_context_t object that will hold the CoAP stack status.
*/
coap_context_t *coap_new_context(const coap_address_t *listen_addr);
/**
* Set the context's default PSK hint and/or key for a server.
*
* @param context The current coap_context_t object.
* @param hint The default PSK server hint sent to a client. If @p NULL, PSK
* authentication is disabled. Empty string is a valid hint.
* @param key The default PSK key. If @p NULL, PSK authentication will fail.
* @param key_len The default PSK key's length. If @p 0, PSK authentication will
* fail.
*
* @return @c 1 if successful, else @c 0.
*/
int coap_context_set_psk( coap_context_t *context, const char *hint,
const uint8_t *key, size_t key_len );
/**
* Set the context's default PKI information for a server.
*
* @param context The current coap_context_t object.
* @param setup_data If @p NULL, PKI authentication will fail. Certificate
* information required.
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_context_set_pki(coap_context_t *context,
coap_dtls_pki_t *setup_data);
/**
* Set the context's default Root CA information for a client or server.
*
* @param context The current coap_context_t object.
* @param ca_file If not @p NULL, is the full path name of a PEM encoded
* file containing all the Root CAs to be used.
* @param ca_dir If not @p NULL, points to a directory containing PEM
* encoded files containing all the Root CAs to be used.
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_context_set_pki_root_cas(coap_context_t *context,
const char *ca_file,
const char *ca_dir);
/**
* Set the context keepalive timer for sessions.
* A keepalive message will be sent after if a session has been inactive,
* i.e. no packet sent or received, for the given number of seconds.
* For reliable protocols, a PING message will be sent. If a PONG has not
* been received before the next PING is due to be sent, the session will
* considered as disconnected.
*
* @param context The coap_context_t object.
* @param seconds Number of seconds for the inactivity timer, or zero
* to disable CoAP-level keepalive messages.
*
* @return 1 if successful, else 0
*/
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
/**
* Returns a new message id and updates @p session->tx_mid accordingly. The
* message id is returned in network byte order to make it easier to read in
* tracing tools.
*
* @param session The current coap_session_t object.
*
* @return Incremented message id in network byte order.
*/
COAP_STATIC_INLINE uint16_t
coap_new_message_id(coap_session_t *session) {
return ++session->tx_mid;
}
/**
* CoAP stack context must be released with coap_free_context(). This function
* clears all entries from the receive queue and send queue and deletes the
* resources that have been registered with @p context, and frees the attached
* endpoints.
*
* @param context The current coap_context_t object to free off.
*/
void coap_free_context(coap_context_t *context);
/**
* Stores @p data with the given CoAP context. This function
* overwrites any value that has previously been stored with @p
* context.
*
* @param context The CoAP context.
* @param data The data to store with wih the context. Note that this data
* must be valid during the lifetime of @p context.
*/
void coap_set_app_data(coap_context_t *context, void *data);
/**
* Returns any application-specific data that has been stored with @p
* context using the function coap_set_app_data(). This function will
* return @c NULL if no data has been stored.
*
* @param context The CoAP context.
*
* @return The data previously stored or @c NULL if not data stored.
*/
void *coap_get_app_data(const coap_context_t *context);
/**
* Creates a new ACK PDU with specified error @p code. The options specified by
* the filter expression @p opts will be copied from the original request
* contained in @p request. Unless @c SHORT_ERROR_RESPONSE was defined at build
* time, the textual reason phrase for @p code will be added as payload, with
* Content-Type @c 0.
* This function returns a pointer to the new response message, or @c NULL on
* error. The storage allocated for the new message must be relased with
* coap_free().
*
* @param request Specification of the received (confirmable) request.
* @param code The error code to set.
* @param opts An option filter that specifies which options to copy from
* the original request in @p node.
*
* @return A pointer to the new message or @c NULL on error.
*/
coap_pdu_t *coap_new_error_response(coap_pdu_t *request,
unsigned char code,
coap_opt_filter_t opts);
/**
* Sends an error response with code @p code for request @p request to @p dst.
* @p opts will be passed to coap_new_error_response() to copy marked options
* from the request. This function returns the transaction id if the message was
* sent, or @c COAP_INVALID_TID otherwise.
*
* @param session The CoAP session.
* @param request The original request to respond to.
* @param code The response code.
* @param opts A filter that specifies the options to copy from the
* @p request.
*
* @return The transaction id if the message was sent, or @c
* COAP_INVALID_TID otherwise.
*/
coap_tid_t coap_send_error(coap_session_t *session,
coap_pdu_t *request,
unsigned char code,
coap_opt_filter_t opts);
/**
* Helper funktion to create and send a message with @p type (usually ACK or
* RST). This function returns @c COAP_INVALID_TID when the message was not
* sent, a valid transaction id otherwise.
*
* @param session The CoAP session.
* @param request The request that should be responded to.
* @param type Which type to set.
* @return transaction id on success or @c COAP_INVALID_TID
* otherwise.
*/
coap_tid_t
coap_send_message_type(coap_session_t *session, coap_pdu_t *request, unsigned char type);
/**
* Sends an ACK message with code @c 0 for the specified @p request to @p dst.
* This function returns the corresponding transaction id if the message was
* sent or @c COAP_INVALID_TID on error.
*
* @param session The CoAP session.
* @param request The request to be acknowledged.
*
* @return The transaction id if ACK was sent or @c
* COAP_INVALID_TID on error.
*/
coap_tid_t coap_send_ack(coap_session_t *session, coap_pdu_t *request);
/**
* Sends an RST message with code @c 0 for the specified @p request to @p dst.
* This function returns the corresponding transaction id if the message was
* sent or @c COAP_INVALID_TID on error.
*
* @param session The CoAP session.
* @param request The request to be reset.
*
* @return The transaction id if RST was sent or @c
* COAP_INVALID_TID on error.
*/
COAP_STATIC_INLINE coap_tid_t
coap_send_rst(coap_session_t *session, coap_pdu_t *request) {
return coap_send_message_type(session, request, COAP_MESSAGE_RST);
}
/**
* Sends a CoAP message to given peer. The memory that is
* allocated by pdu will be released by coap_send().
* The caller must not use the pdu after calling coap_send().
*
* @param session The CoAP session.
* @param pdu The CoAP PDU to send.
*
* @return The message id of the sent message or @c
* COAP_INVALID_TID on error.
*/
coap_tid_t coap_send( coap_session_t *session, coap_pdu_t *pdu );
/**
* Handles retransmissions of confirmable messages
*
* @param context The CoAP context.
* @param node The node to retransmit.
*
* @return The message id of the sent message or @c
* COAP_INVALID_TID on error.
*/
coap_tid_t coap_retransmit(coap_context_t *context, coap_queue_t *node);
/**
* For applications with their own message loop, send all pending retransmits and
* return the list of sockets with events to wait for and the next timeout
* The application should call coap_read, then coap_write again when any condition below is true:
* - data is available on any of the sockets with the COAP_SOCKET_WANT_READ
* - an incoming connection is pending in the listen queue and the COAP_SOCKET_WANT_ACCEPT flag is set
* - at least some data can be written without blocking on any of the sockets with the COAP_SOCKET_WANT_WRITE flag set
* - a connection event occured (success or failure) and the COAP_SOCKET_WANT_CONNECT flag is set
* - the timeout has expired
* Before calling coap_read or coap_write again, the application should position COAP_SOCKET_CAN_READ and COAP_SOCKET_CAN_WRITE flags as applicable.
*
* @param ctx The CoAP context
* @param sockets array of socket descriptors, filled on output
* @param max_sockets size of socket array.
* @param num_sockets pointer to the number of valid entries in the socket arrays on output
* @param now Current time.
*
* @return timeout as maxmimum number of milliseconds that the application should wait for network events or 0 if the application should wait forever.
*/
unsigned int
coap_write(coap_context_t *ctx,
coap_socket_t *sockets[],
unsigned int max_sockets,
unsigned int *num_sockets,
coap_tick_t now
);
/**
* For applications with their own message loop, reads all data from the network.
*
* @param ctx The CoAP context
* @param now Current time
*/
void coap_read(coap_context_t *ctx, coap_tick_t now);
/**
* The main message processing loop.
*
* @param ctx The CoAP context
* @param timeout_ms Minimum number of milliseconds to wait for new messages before returning. If zero the call will block until at least one packet is sent or received.
*
* @return number of milliseconds spent or @c -1 if there was an error
*/
int coap_run_once( coap_context_t *ctx, unsigned int timeout_ms );
/**
* Parses and interprets a CoAP datagram with context @p ctx. This function
* returns @c 0 if the datagram was handled, or a value less than zero on
* error.
*
* @param ctx The current CoAP context.
* @param session The current CoAP session.
* @param data The received packet'd data.
* @param data_len The received packet'd data length.
*
* @return @c 0 if message was handled successfully, or less than zero on
* error.
*/
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len);
/**
* Invokes the event handler of @p context for the given @p event and
* @p data.
*
* @param context The CoAP context whose event handler is to be called.
* @param event The event to deliver.
* @param session The session related to @p event.
* @return The result from the associated event handler or 0 if none was
* registered.
*/
int coap_handle_event(coap_context_t *context,
coap_event_t event,
coap_session_t *session);
/**
* This function removes the element with given @p id from the list given list.
* If @p id was found, @p node is updated to point to the removed element. Note
* that the storage allocated by @p node is @b not released. The caller must do
* this manually using coap_delete_node(). This function returns @c 1 if the
* element with id @p id was found, @c 0 otherwise. For a return value of @c 0,
* the contents of @p node is undefined.
*
* @param queue The queue to search for @p id.
* @param session The session to look for.
* @param id The transaction id to look for.
* @param node If found, @p node is updated to point to the removed node. You
* must release the storage pointed to by @p node manually.
*
* @return @c 1 if @p id was found, @c 0 otherwise.
*/
int coap_remove_from_queue(coap_queue_t **queue,
coap_session_t *session,
coap_tid_t id,
coap_queue_t **node);
coap_tid_t
coap_wait_ack( coap_context_t *context, coap_session_t *session,
coap_queue_t *node);
/**
* Retrieves transaction from the queue.
*
* @param queue The transaction queue to be searched.
* @param session The session to find.
* @param id The transaction id to find.
*
* @return A pointer to the transaction object or @c NULL if not found.
*/
coap_queue_t *coap_find_transaction(coap_queue_t *queue, coap_session_t *session, coap_tid_t id);
/**
* Cancels all outstanding messages for session @p session that have the specified
* token.
*
* @param context The context in use.
* @param session Session of the messages to remove.
* @param token Message token.
* @param token_length Actual length of @p token.
*/
void coap_cancel_all_messages(coap_context_t *context,
coap_session_t *session,
const uint8_t *token,
size_t token_length);
/**
* Cancels all outstanding messages for session @p session.
*
* @param context The context in use.
* @param session Session of the messages to remove.
* @param reason The reasion for the session cancellation
*/
void
coap_cancel_session_messages(coap_context_t *context,
coap_session_t *session,
coap_nack_reason_t reason);
/**
* Dispatches the PDUs from the receive queue in given context.
*/
void coap_dispatch(coap_context_t *context, coap_session_t *session,
coap_pdu_t *pdu);
/**
* Returns 1 if there are no messages to send or to dispatch in the context's
* queues. */
int coap_can_exit(coap_context_t *context);
/**
* Returns the current value of an internal tick counter. The counter counts \c
* COAP_TICKS_PER_SECOND ticks every second.
*/
void coap_ticks(coap_tick_t *);
/**
* Verifies that @p pdu contains no unknown critical options. Options must be
* registered at @p ctx, using the function coap_register_option(). A basic set
* of options is registered automatically by coap_new_context(). This function
* returns @c 1 if @p pdu is ok, @c 0 otherwise. The given filter object @p
* unknown will be updated with the unknown options. As only @c COAP_MAX_OPT
* options can be signalled this way, remaining options must be examined
* manually.
*
* @code
coap_opt_filter_t f = COAP_OPT_NONE;
coap_opt_iterator_t opt_iter;
if (coap_option_check_critical(ctx, pdu, f) == 0) {
coap_option_iterator_init(pdu, &opt_iter, f);
while (coap_option_next(&opt_iter)) {
if (opt_iter.type & 0x01) {
... handle unknown critical option in opt_iter ...
}
}
}
@endcode
*
* @param ctx The context where all known options are registered.
* @param pdu The PDU to check.
* @param unknown The output filter that will be updated to indicate the
* unknown critical options found in @p pdu.
*
* @return @c 1 if everything was ok, @c 0 otherwise.
*/
int coap_option_check_critical(coap_context_t *ctx,
coap_pdu_t *pdu,
coap_opt_filter_t unknown);
/**
* Creates a new response for given @p request with the contents of @c
* .well-known/core. The result is NULL on error or a newly allocated PDU that
* must be either sent with coap_sent() or released by coap_delete_pdu().
*
* @param context The current coap context to use.
* @param session The CoAP session.
* @param request The request for @c .well-known/core .
*
* @return A new 2.05 response for @c .well-known/core or NULL on error.
*/
coap_pdu_t *coap_wellknown_response(coap_context_t *context,
coap_session_t *session,
coap_pdu_t *request);
/**
* Calculates the initial timeout based on the session CoAP transmission
* parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND.
* The calculation requires 'ack_timeout' and 'ack_random_factor' to be in
* Qx.FRAC_BITS fixed point notation, whereas the passed parameter @p r
* is interpreted as the fractional part of a Q0.MAX_BITS random value.
*
* @param session session timeout is associated with
* @param r random value as fractional part of a Q0.MAX_BITS fixed point
* value
* @return COAP_TICKS_PER_SECOND * 'ack_timeout' *
* (1 + ('ack_random_factor' - 1) * r)
*/
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
/**
* Function interface for joining a multicast group for listening
*
* @param ctx The current context
* @param groupname The name of the group that is to be joined for listening
*
* @return 0 on success, -1 on error
*/
int
coap_join_mcast_group(coap_context_t *ctx, const char *groupname);
#endif /* COAP_NET_H_ */

View File

@ -1,543 +0,0 @@
/*
* pdu.h -- CoAP message structure
*
* Copyright (C) 2010-2014 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file pdu.h
* @brief Pre-defined constants that reflect defaults for CoAP
*/
#ifndef COAP_PDU_H_
#define COAP_PDU_H_
#include "uri.h"
struct coap_session_t;
#ifdef WITH_LWIP
#include <lwip/pbuf.h>
#endif
#include <stdint.h>
#define COAP_DEFAULT_PORT 5683 /* CoAP default UDP/TCP port */
#define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP/TCP port for secure transmission */
#define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
#ifndef COAP_DEFAULT_MTU
#define COAP_DEFAULT_MTU 1152
#endif /* COAP_DEFAULT_MTU */
/* TCP Message format constants, do not modify */
#define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
#define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
#define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
/* Derived message size limits */
#define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
#define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
#define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
#define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
#ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4)
#else
/* 8 MiB max-message-size plus some space for options */
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (8*1024*1024+256)
#endif
#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
#ifndef COAP_DEBUG_BUF_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEBUG_BUF_SIZE 128
#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
/* 1024 derived from RFC7252 4.6. Message Size max payload */
#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
#endif /* COAP_DEBUG_BUF_SIZE */
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
#define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
/** well-known resources URI */
#define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
/* CoAP message types */
#define COAP_MESSAGE_CON 0 /* confirmable message (requires ACK/RST) */
#define COAP_MESSAGE_NON 1 /* non-confirmable message (one-shot message) */
#define COAP_MESSAGE_ACK 2 /* used to acknowledge confirmable messages */
#define COAP_MESSAGE_RST 3 /* indicates error in received messages */
/* CoAP request methods */
#define COAP_REQUEST_GET 1
#define COAP_REQUEST_POST 2
#define COAP_REQUEST_PUT 3
#define COAP_REQUEST_DELETE 4
#define COAP_REQUEST_FETCH 5 /* RFC 8132 */
#define COAP_REQUEST_PATCH 6 /* RFC 8132 */
#define COAP_REQUEST_IPATCH 7 /* RFC 8132 */
/*
* CoAP option types (be sure to update coap_option_check_critical() when
* adding options
*/
#define COAP_OPTION_IF_MATCH 1 /* C, opaque, 0-8 B, (none) */
#define COAP_OPTION_URI_HOST 3 /* C, String, 1-255 B, destination address */
#define COAP_OPTION_ETAG 4 /* E, opaque, 1-8 B, (none) */
#define COAP_OPTION_IF_NONE_MATCH 5 /* empty, 0 B, (none) */
#define COAP_OPTION_URI_PORT 7 /* C, uint, 0-2 B, destination port */
#define COAP_OPTION_LOCATION_PATH 8 /* E, String, 0-255 B, - */
#define COAP_OPTION_URI_PATH 11 /* C, String, 0-255 B, (none) */
#define COAP_OPTION_CONTENT_FORMAT 12 /* E, uint, 0-2 B, (none) */
#define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
#define COAP_OPTION_MAXAGE 14 /* E, uint, 0--4 B, 60 Seconds */
#define COAP_OPTION_URI_QUERY 15 /* C, String, 1-255 B, (none) */
#define COAP_OPTION_ACCEPT 17 /* C, uint, 0-2 B, (none) */
#define COAP_OPTION_LOCATION_QUERY 20 /* E, String, 0-255 B, (none) */
#define COAP_OPTION_SIZE2 28 /* E, uint, 0-4 B, (none) */
#define COAP_OPTION_PROXY_URI 35 /* C, String, 1-1034 B, (none) */
#define COAP_OPTION_PROXY_SCHEME 39 /* C, String, 1-255 B, (none) */
#define COAP_OPTION_SIZE1 60 /* E, uint, 0-4 B, (none) */
/* option types from RFC 7641 */
#define COAP_OPTION_OBSERVE 6 /* E, empty/uint, 0 B/0-3 B, (none) */
#define COAP_OPTION_SUBSCRIPTION COAP_OPTION_OBSERVE
/* selected option types from RFC 7959 */
#define COAP_OPTION_BLOCK2 23 /* C, uint, 0--3 B, (none) */
#define COAP_OPTION_BLOCK1 27 /* C, uint, 0--3 B, (none) */
/* selected option types from RFC 7967 */
#define COAP_OPTION_NORESPONSE 258 /* N, uint, 0--1 B, 0 */
#define COAP_MAX_OPT 65535 /**< the highest option number we know */
/* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */
/* As of draft-ietf-core-coap-04, response codes are encoded to base
* 32, i.e. the three upper bits determine the response class while
* the remaining five fine-grained information specific to that class.
*/
#define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
/* Determines the class of response code C */
#define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
#ifndef SHORT_ERROR_RESPONSE
/**
* Returns a human-readable response phrase for the specified CoAP response @p
* code. This function returns @c NULL if not found.
*
* @param code The response code for which the literal phrase should be
* retrieved.
*
* @return A zero-terminated string describing the error, or @c NULL if not
* found.
*/
const char *coap_response_phrase(unsigned char code);
#define COAP_ERROR_PHRASE_LENGTH 32 /**< maximum length of error phrase */
#else
#define coap_response_phrase(x) ((char *)NULL)
#define COAP_ERROR_PHRASE_LENGTH 0 /**< maximum length of error phrase */
#endif /* SHORT_ERROR_RESPONSE */
/* The following definitions exist for backwards compatibility */
#if 0 /* this does not exist any more */
#define COAP_RESPONSE_100 40 /* 100 Continue */
#endif
#define COAP_RESPONSE_200 COAP_RESPONSE_CODE(200) /* 2.00 OK */
#define COAP_RESPONSE_201 COAP_RESPONSE_CODE(201) /* 2.01 Created */
#define COAP_RESPONSE_304 COAP_RESPONSE_CODE(203) /* 2.03 Valid */
#define COAP_RESPONSE_400 COAP_RESPONSE_CODE(400) /* 4.00 Bad Request */
#define COAP_RESPONSE_404 COAP_RESPONSE_CODE(404) /* 4.04 Not Found */
#define COAP_RESPONSE_405 COAP_RESPONSE_CODE(405) /* 4.05 Method Not Allowed */
#define COAP_RESPONSE_415 COAP_RESPONSE_CODE(415) /* 4.15 Unsupported Media Type */
#define COAP_RESPONSE_500 COAP_RESPONSE_CODE(500) /* 5.00 Internal Server Error */
#define COAP_RESPONSE_501 COAP_RESPONSE_CODE(501) /* 5.01 Not Implemented */
#define COAP_RESPONSE_503 COAP_RESPONSE_CODE(503) /* 5.03 Service Unavailable */
#define COAP_RESPONSE_504 COAP_RESPONSE_CODE(504) /* 5.04 Gateway Timeout */
#if 0 /* these response codes do not have a valid code any more */
# define COAP_RESPONSE_X_240 240 /* Token Option required by server */
# define COAP_RESPONSE_X_241 241 /* Uri-Authority Option required by server */
#endif
#define COAP_RESPONSE_X_242 COAP_RESPONSE_CODE(402) /* Critical Option not supported */
#define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
#define COAP_SIGNALING_CSM COAP_SIGNALING_CODE(701)
#define COAP_SIGNALING_PING COAP_SIGNALING_CODE(702)
#define COAP_SIGNALING_PONG COAP_SIGNALING_CODE(703)
#define COAP_SIGNALING_RELEASE COAP_SIGNALING_CODE(704)
#define COAP_SIGNALING_ABORT COAP_SIGNALING_CODE(705)
/* Applies to COAP_SIGNALING_CSM */
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4
/* Applies to COAP_SIGNALING_PING / COAP_SIGNALING_PONG */
#define COAP_SIGNALING_OPTION_CUSTODY 2
/* Applies to COAP_SIGNALING_RELEASE */
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2
#define COAP_SIGNALING_OPTION_HOLD_OFF 4
/* Applies to COAP_SIGNALING_ABORT */
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2
/* CoAP media type encoding */
#define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */
#define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */
#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */
#define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */
#define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */
#define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */
#define COAP_MEDIATYPE_APPLICATION_CBOR 60 /* application/cbor */
/* Content formats from RFC 8152 */
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98 /* application/cose; cose-type="cose-sign" */
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18 /* application/cose; cose-type="cose-sign1" */
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96 /* application/cose; cose-type="cose-encrypt" */
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16 /* application/cose; cose-type="cose-encrypt0" */
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97 /* application/cose; cose-type="cose-mac" */
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17 /* application/cose; cose-type="cose-mac0" */
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101 /* application/cose-key */
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102 /* application/cose-key-set */
/* Content formats from RFC 8428 */
#define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110 /* application/senml+json */
#define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111 /* application/sensml+json */
#define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112 /* application/senml+cbor */
#define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113 /* application/sensml+cbor */
#define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114 /* application/senml-exi */
#define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115 /* application/sensml-exi */
#define COAP_MEDIATYPE_APPLICATION_SENML_XML 310 /* application/senml+xml */
#define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311 /* application/sensml+xml */
/* Note that identifiers for registered media types are in the range 0-65535. We
* use an unallocated type here and hope for the best. */
#define COAP_MEDIATYPE_ANY 0xff /* any media type */
/**
* coap_tid_t is used to store CoAP transaction id, i.e. a hash value
* built from the remote transport address and the message id of a
* CoAP PDU. Valid transaction ids are greater or equal zero.
*/
typedef int coap_tid_t;
/** Indicates an invalid transaction id. */
#define COAP_INVALID_TID -1
/**
* Indicates that a response is suppressed. This will occur for error
* responses if the request was received via IP multicast.
*/
#define COAP_DROPPED_RESPONSE -2
#define COAP_PDU_DELAYED -3
#define COAP_OPT_LONG 0x0F /* OC == 0b1111 indicates that the option list
* in a CoAP message is limited by 0b11110000
* marker */
#define COAP_OPT_END 0xF0 /* end marker */
#define COAP_PAYLOAD_START 0xFF /* payload marker */
/**
* @deprecated Use coap_optlist_t instead.
*
* Structures for more convenient handling of options. (To be used with ordered
* coap_list_t.) The option's data will be added to the end of the coap_option
* structure (see macro COAP_OPTION_DATA).
*/
COAP_DEPRECATED typedef struct {
uint16_t key; /* the option key (no delta coding) */
unsigned int length;
} coap_option;
#define COAP_OPTION_KEY(option) (option).key
#define COAP_OPTION_LENGTH(option) (option).length
#define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
/**
* structure for CoAP PDUs
* token, if any, follows the fixed size header, then options until
* payload marker (0xff), then the payload if stored inline.
* Memory layout is:
* <---header--->|<---token---><---options--->0xff<---payload--->
* header is addressed with a negative offset to token, its maximum size is
* max_hdr_size.
* options starts at token + token_length
* payload starts at data, its length is used_size - (data - token)
*/
typedef struct coap_pdu_t {
uint8_t type; /**< message type */
uint8_t code; /**< request method (value 1--10) or response code (value 40-255) */
uint8_t max_hdr_size; /**< space reserved for protocol-specific header */
uint8_t hdr_size; /**< actaul size used for protocol-specific header */
uint8_t token_length; /**< length of Token */
uint16_t tid; /**< transaction id, if any, in regular host byte order */
uint16_t max_delta; /**< highest option number */
size_t alloc_size; /**< allocated storage for token, options and payload */
size_t used_size; /**< used bytes of storage for token, options and payload */
size_t max_size; /**< maximum size for token, options and payload, or zero for variable size pdu */
uint8_t *token; /**< first byte of token, if any, or options */
uint8_t *data; /**< first byte of payload, if any */
#ifdef WITH_LWIP
struct pbuf *pbuf; /**< lwIP PBUF. The package data will always reside
* inside the pbuf's payload, but this pointer
* has to be kept because no exact offset can be
* given. This field must not be accessed from
* outside, because the pbuf's reference count
* is checked to be 1 when the pbuf is assigned
* to the pdu, and the pbuf stays exclusive to
* this pdu. */
#endif
} coap_pdu_t;
#define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
#define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
#define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224)
#define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
#define COAP_PDU_MAX_UDP_HEADER_SIZE 4
#define COAP_PDU_MAX_TCP_HEADER_SIZE 6
#ifdef WITH_LWIP
/**
* Creates a CoAP PDU from an lwIP @p pbuf, whose reference is passed on to this
* function.
*
* The pbuf is checked for being contiguous, and for having only one reference.
* The reference is stored in the PDU and will be freed when the PDU is freed.
*
* (For now, these are fatal errors; in future, a new pbuf might be allocated,
* the data copied and the passed pbuf freed).
*
* This behaves like coap_pdu_init(0, 0, 0, pbuf->tot_len), and afterwards
* copying the contents of the pbuf to the pdu.
*
* @return A pointer to the new PDU object or @c NULL on error.
*/
coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf);
#endif
typedef uint8_t coap_proto_t;
/**
* coap_proto_t values
*/
#define COAP_PROTO_NONE 0
#define COAP_PROTO_UDP 1
#define COAP_PROTO_DTLS 2
#define COAP_PROTO_TCP 3
#define COAP_PROTO_TLS 4
/**
* Creates a new CoAP PDU with at least enough storage space for the given
* @p size maximum message size. The function returns a pointer to the
* node coap_pdu_t object on success, or @c NULL on error. The storage allocated
* for the result must be released with coap_delete_pdu() if coap_send() is not
* called.
*
* @param type The type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON,
* COAP_MESSAGE_ACK, COAP_MESSAGE_RST).
* @param code The message code.
* @param tid The transcation id to set or 0 if unknown / not applicable.
* @param size The maximum allowed number of byte for the message.
* @return A pointer to the new PDU object or @c NULL on error.
*/
coap_pdu_t *
coap_pdu_init(uint8_t type, uint8_t code, uint16_t tid, size_t size);
/**
* Dynamically grows the size of @p pdu to @p new_size. The new size
* must not exceed the PDU's configure maximum size. On success, this
* function returns 1, otherwise 0.
*
* @param pdu The PDU to resize.
* @param new_size The new size in bytes.
* @return 1 if the operation succeeded, 0 otherwise.
*/
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
/**
* Clears any contents from @p pdu and resets @c used_size,
* and @c data pointers. @c max_size is set to @p size, any
* other field is set to @c 0. Note that @p pdu must be a valid
* pointer to a coap_pdu_t object created e.g. by coap_pdu_init().
*/
void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
/**
* Creates a new CoAP PDU.
*/
coap_pdu_t *coap_new_pdu(const struct coap_session_t *session);
/**
* Dispose of an CoAP PDU and frees associated storage.
* Not that in general you should not call this function directly.
* When a PDU is sent with coap_send(), coap_delete_pdu() will be
* called automatically for you.
*/
void coap_delete_pdu(coap_pdu_t *);
/**
* Interprets @p data to determine the number of bytes in the header.
* This function returns @c 0 on error or a number greater than zero on success.
*
* @param proto Session's protocol
* @param data The first byte of raw data to parse as CoAP PDU.
*
* @return A value greater than zero on success or @c 0 on error.
*/
size_t coap_pdu_parse_header_size(coap_proto_t proto,
const uint8_t *data);
/**
* Parses @p data to extract the message size.
* @p length must be at least coap_pdu_parse_header_size(proto, data).
* This function returns @c 0 on error or a number greater than zero on success.
*
* @param proto Session's protocol
* @param data The raw data to parse as CoAP PDU.
* @param length The actual size of @p data.
*
* @return A value greater than zero on success or @c 0 on error.
*/
size_t coap_pdu_parse_size(coap_proto_t proto,
const uint8_t *data,
size_t length);
/**
* Decode the protocol specific header for the specified PDU.
* @param pdu A newly received PDU.
* @param proto The target wire protocol.
* @return 1 for success or 0 on error.
*/
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto);
/**
* Verify consistency in the given CoAP PDU structure and locate the data.
* This function returns @c 0 on error or a number greater than zero on
* success.
* This function only parses the token and options, up to the payload start
* marker.
*
* @param pdu The PDU structure to.
*
* @return 1 on success or @c 0 on error.
*/
int coap_pdu_parse_opt(coap_pdu_t *pdu);
/**
* Parses @p data into the CoAP PDU structure given in @p result.
* The target pdu must be large enough to
* This function returns @c 0 on error or a number greater than zero on success.
*
* @param proto Session's protocol
* @param data The raw data to parse as CoAP PDU.
* @param length The actual size of @p data.
* @param pdu The PDU structure to fill. Note that the structure must
* provide space to hold at least the token and options
* part of the message.
*
* @return 1 on success or @c 0 on error.
*/
int coap_pdu_parse(coap_proto_t proto,
const uint8_t *data,
size_t length,
coap_pdu_t *pdu);
/**
* Adds token of length @p len to @p pdu.
* Adding the token destroys any following contents of the pdu. Hence options
* and data must be added after coap_add_token() has been called. In @p pdu,
* length is set to @p len + @c 4, and max_delta is set to @c 0. This function
* returns @c 0 on error or a value greater than zero on success.
*
* @param pdu The PDU where the token is to be added.
* @param len The length of the new token.
* @param data The token to add.
*
* @return A value greater than zero on success, or @c 0 on error.
*/
int coap_add_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
/**
* Adds option of given type to pdu that is passed as first
* parameter.
* coap_add_option() destroys the PDU's data, so coap_add_data() must be called
* after all options have been added. As coap_add_token() destroys the options
* following the token, the token must be added before coap_add_option() is
* called. This function returns the number of bytes written or @c 0 on error.
*/
size_t coap_add_option(coap_pdu_t *pdu,
uint16_t type,
size_t len,
const uint8_t *data);
/**
* Adds option of given type to pdu that is passed as first parameter, but does
* not write a value. It works like coap_add_option with respect to calling
* sequence (i.e. after token and before data). This function returns a memory
* address to which the option data has to be written before the PDU can be
* sent, or @c NULL on error.
*/
uint8_t *coap_add_option_later(coap_pdu_t *pdu,
uint16_t type,
size_t len);
/**
* Adds given data to the pdu that is passed as first parameter. Note that the
* PDU's data is destroyed by coap_add_option(). coap_add_data() must be called
* only once per PDU, otherwise the result is undefined.
*/
int coap_add_data(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
/**
* Adds given data to the pdu that is passed as first parameter but does not
* copyt it. Note that the PDU's data is destroyed by coap_add_option().
* coap_add_data() must be have been called once for this PDU, otherwise the
* result is undefined.
* The actual data must be copied at the returned location.
*/
uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len);
/**
* Retrieves the length and data pointer of specified PDU. Returns 0 on error or
* 1 if *len and *data have correct values. Note that these values are destroyed
* with the pdu.
*/
int coap_get_data(const coap_pdu_t *pdu,
size_t *len,
uint8_t **data);
/**
* Compose the protocol specific header for the specified PDU.
* @param pdu A newly composed PDU.
* @param proto The target wire protocol.
* @return Number of header bytes prepended before pdu->token or 0 on error.
*/
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto);
#endif /* COAP_PDU_H_ */

View File

@ -1,127 +0,0 @@
/*
* prng.h -- Pseudo Random Numbers
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file prng.h
* @brief Pseudo Random Numbers
*/
#ifndef COAP_PRNG_H_
#define COAP_PRNG_H_
/**
* @defgroup prng Pseudo Random Numbers
* API functions for gerating pseudo random numbers
* @{
*/
#if defined(WITH_CONTIKI)
#include <string.h>
/**
* Fills \p buf with \p len random bytes. This is the default implementation for
* prng(). You might want to change prng() to use a better PRNG on your specific
* platform.
*/
COAP_STATIC_INLINE int
contiki_prng_impl(unsigned char *buf, size_t len) {
uint16_t v = random_rand();
while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
len -= sizeof(v);
buf += sizeof(v);
v = random_rand();
}
memcpy(buf, &v, len);
return 1;
}
#define prng(Buf,Length) contiki_prng_impl((Buf), (Length))
#define prng_init(Value) random_init((uint16_t)(Value))
#elif defined(WITH_LWIP) && defined(LWIP_RAND)
COAP_STATIC_INLINE int
lwip_prng_impl(unsigned char *buf, size_t len) {
u32_t v = LWIP_RAND();
while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
len -= sizeof(v);
buf += sizeof(v);
v = LWIP_RAND();
}
memcpy(buf, &v, len);
return 1;
}
#define prng(Buf,Length) lwip_prng_impl((Buf), (Length))
#define prng_init(Value)
#elif defined(_WIN32)
#define prng_init(Value)
errno_t __cdecl rand_s( _Out_ unsigned int* _RandomValue );
/**
* Fills \p buf with \p len random bytes. This is the default implementation for
* prng(). You might want to change prng() to use a better PRNG on your specific
* platform.
*/
COAP_STATIC_INLINE int
coap_prng_impl( unsigned char *buf, size_t len ) {
while ( len != 0 ) {
uint32_t r = 0;
size_t i;
if ( rand_s( &r ) != 0 )
return 0;
for ( i = 0; i < len && i < 4; i++ ) {
*buf++ = (uint8_t)r;
r >>= 8;
}
len -= i;
}
return 1;
}
#else
#include <stdlib.h>
/**
* Fills \p buf with \p len random bytes. This is the default implementation for
* prng(). You might want to change prng() to use a better PRNG on your specific
* platform.
*/
COAP_STATIC_INLINE int
coap_prng_impl( unsigned char *buf, size_t len ) {
while ( len-- )
*buf++ = rand() & 0xFF;
return 1;
}
#endif
#ifndef prng
/**
* Fills \p Buf with \p Length bytes of random data.
*
* @hideinitializer
*/
#define prng(Buf,Length) coap_prng_impl((Buf), (Length))
#endif
#ifndef prng_init
/**
* Called to set the PRNG seed. You may want to re-define this to allow for a
* better PRNG.
*
* @hideinitializer
*/
#define prng_init(Value) srand((unsigned long)(Value))
#endif
/** @} */
#endif /* COAP_PRNG_H_ */

View File

@ -1,121 +0,0 @@
/*
* str.h -- strings to be used in the CoAP library
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_STR_H_
#define COAP_STR_H_
#include <string.h>
/**
* @defgroup string String handling support
* API functions for handling strings
* @{
*/
/**
* Coap string data definition
*/
typedef struct coap_string_t {
size_t length; /**< length of string */
uint8_t *s; /**< string data */
} coap_string_t;
/**
* Coap string data definition with const data
*/
typedef struct coap_str_const_t {
size_t length; /**< length of string */
const uint8_t *s; /**< string data */
} coap_str_const_t;
#define COAP_SET_STR(st,l,v) { (st)->length = (l), (st)->s = (v); }
/**
* Coap binary data definition
*/
typedef struct coap_binary_t {
size_t length; /**< length of binary data */
uint8_t *s; /**< binary data */
} coap_binary_t;
/**
* Returns a new string object with at least size+1 bytes storage allocated.
* The string must be released using coap_delete_string().
*
* @param size The size to allocate for the binary string data.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_string_t *coap_new_string(size_t size);
/**
* Deletes the given string and releases any memory allocated.
*
* @param string The string to free off.
*/
void coap_delete_string(coap_string_t *string);
/**
* Returns a new const string object with at least size+1 bytes storage
* allocated, and the provided data copied into the string object.
* The string must be released using coap_delete_str_const().
*
* @param data The data to put in the new string object.
* @param size The size to allocate for the binary string data.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size);
/**
* Deletes the given const string and releases any memory allocated.
*
* @param string The string to free off.
*/
void coap_delete_str_const(coap_str_const_t *string);
/**
* Take the specified byte array (text) and create a coap_str_const_t *
*
* WARNING: The byte array must be in the local scope and not a
* parameter in the function call as sizeof() will return the size of the
* pointer, not the size of the byte array, leading to unxepected results.
*
* @param string The const byte array to convert to a coap_str_const_t *
*/
#ifdef __cplusplus
namespace libcoap {
struct CoAPStrConst : coap_str_const_t {
operator coap_str_const_t *() { return this; }
};
}
#define coap_make_str_const(CStr) \
libcoap::CoAPStrConst{sizeof(CStr)-1, reinterpret_cast<const uint8_t *>(CStr)}
#else /* __cplusplus */
#define coap_make_str_const(string) \
(&(coap_str_const_t){sizeof(string)-1,(const uint8_t *)(string)})
#endif /* __cplusplus */
/**
* Compares the two strings for equality
*
* @param string1 The first string.
* @param string2 The second string.
*
* @return @c 1 if the strings are equal
* @c 0 otherwise.
*/
#define coap_string_equal(string1,string2) \
((string1)->length == (string2)->length && ((string1)->length == 0 || \
memcmp((string1)->s, (string2)->s, (string1)->length) == 0))
/** @} */
#endif /* COAP_STR_H_ */

View File

@ -1,77 +0,0 @@
/*
* subscribe.h -- subscription handling for CoAP
* see RFC7641
*
* Copyright (C) 2010-2012,2014-2015 Olaf Bergmann <bergmann@tzi.org>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_SUBSCRIBE_H_
#define COAP_SUBSCRIBE_H_
#include "address.h"
#include "coap_io.h"
#include "block.h"
/**
* @defgroup observe Resource observation
* API functions for interfacing with the observe handling (RFC7641)
* @{
*/
/**
* The value COAP_OBSERVE_ESTABLISH in a GET request indicates a new observe
* relationship for (sender address, token) is requested.
*/
#define COAP_OBSERVE_ESTABLISH 0
/**
* The value COAP_OBSERVE_CANCEL in a GET request indicates that the observe
* relationship for (sender address, token) must be cancelled.
*/
#define COAP_OBSERVE_CANCEL 1
#ifndef COAP_OBS_MAX_NON
/**
* Number of notifications that may be sent non-confirmable before a confirmable
* message is sent to detect if observers are alive. The maximum allowed value
* here is @c 15.
*/
#define COAP_OBS_MAX_NON 5
#endif /* COAP_OBS_MAX_NON */
#ifndef COAP_OBS_MAX_FAIL
/**
* Number of confirmable notifications that may fail (i.e. time out without
* being ACKed) before an observer is removed. The maximum value for
* COAP_OBS_MAX_FAIL is @c 3.
*/
#define COAP_OBS_MAX_FAIL 3
#endif /* COAP_OBS_MAX_FAIL */
/** Subscriber information */
typedef struct coap_subscription_t {
struct coap_subscription_t *next; /**< next element in linked list */
coap_session_t *session; /**< subscriber session */
unsigned int non_cnt:4; /**< up to 15 non-confirmable notifies allowed */
unsigned int fail_cnt:2; /**< up to 3 confirmable notifies can fail */
unsigned int dirty:1; /**< set if the notification temporarily could not be
* sent (in that case, the resource's partially
* dirty flag is set too) */
unsigned int has_block2:1; /**< GET request had Block2 definition */
uint16_t tid; /**< transaction id, if any, in regular host byte order */
coap_block_t block2; /**< GET request Block2 definition */
size_t token_length; /**< actual length of token */
unsigned char token[8]; /**< token used for subscription */
coap_string_t *query; /**< query string used for subscription, if any */
} coap_subscription_t;
void coap_subscription_init(coap_subscription_t *);
/** @} */
#endif /* COAP_SUBSCRIBE_H_ */

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2010-2011,2015-2016 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -30,6 +32,22 @@ typedef struct coap_address_t {
ip_addr_t addr;
} coap_address_t;
/**
* Returns the port from @p addr in host byte order.
*/
COAP_STATIC_INLINE uint16_t
coap_address_get_port(const coap_address_t *addr) {
return ntohs(addr->port);
}
/**
* Sets the port field of @p addr to @p port (in host byte order).
*/
COAP_STATIC_INLINE void
coap_address_set_port(coap_address_t *addr, uint16_t port) {
addr->port = htons(port);
}
#define _coap_address_equals_impl(A, B) \
((A)->port == (B)->port \
&& (!!ip_addr_cmp(&(A)->addr,&(B)->addr)))
@ -47,6 +65,22 @@ typedef struct coap_address_t {
uint16_t port;
} coap_address_t;
/**
* Returns the port from @p addr in host byte order.
*/
COAP_STATIC_INLINE uint16_t
coap_address_get_port(const coap_address_t *addr) {
return uip_ntohs(addr->port);
}
/**
* Sets the port field of @p addr to @p port (in host byte order).
*/
COAP_STATIC_INLINE void
coap_address_set_port(coap_address_t *addr, uint16_t port) {
addr->port = uip_htons(port);
}
#define _coap_address_equals_impl(A,B) \
((A)->port == (B)->port \
&& uip_ipaddr_cmp(&((A)->addr),&((B)->addr)))
@ -68,6 +102,16 @@ typedef struct coap_address_t {
} addr;
} coap_address_t;
/**
* Returns the port from @p addr in host byte order.
*/
uint16_t coap_address_get_port(const coap_address_t *addr);
/**
* Set the port field of @p addr to @p port (in host byte order).
*/
void coap_address_set_port(coap_address_t *addr, uint16_t port);
/**
* Compares given address objects @p a and @p b. This function returns @c 1 if
* addresses are equal, @c 0 otherwise. The parameters @p a and @p b must not be
@ -100,15 +144,7 @@ _coap_address_isany_impl(const coap_address_t *a) {
*
* @param addr The coap_address_t object to initialize.
*/
COAP_STATIC_INLINE void
coap_address_init(coap_address_t *addr) {
assert(addr);
memset(addr, 0, sizeof(coap_address_t));
#if !defined(WITH_LWIP) && !defined(WITH_CONTIKI)
/* lwip and Contiki have constant address sizes and doesn't need the .size part */
addr->size = sizeof(addr->addr);
#endif
}
void coap_address_init(coap_address_t *addr);
/* Convenience function to copy IPv6 addresses without garbage. */

View File

@ -0,0 +1,116 @@
/*
* async.h -- state management for asynchronous messages
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file async.h
* @brief State management for asynchronous messages
*/
#ifndef COAP_ASYNC_H_
#define COAP_ASYNC_H_
#include "net.h"
/**
* @defgroup coap_async Asynchronous Messaging
* @{
* API functions for Async "separate" messages.
* A coap_context_t object holds a list of coap_async_t objects that can
* be used to generate a separate response in the case a result of a request
* cannot be delivered immediately.
*/
/**
* Returns @c 1 if libcoap was built with separate messages enabled,
* @c 0 otherwise.
*/
int coap_async_is_supported(void);
/**
* Allocates a new coap_async_t object and fills its fields according to
* the given @p request. This function returns a pointer to the registered
* coap_async_t object or @c NULL on error. Note that this function will
* return @c NULL in case that an object with the same identifier is already
* registered.
*
* When the delay expires, a copy of the @p request will get sent to the
* appropriate request handler.
*
* @param session The session that is used for asynchronous transmissions.
* @param request The request that is handled asynchronously.
* @param delay The amount of time to delay before sending response, 0 means
* wait forever.
*
* @return A pointer to the registered coap_async_t object or @c
* NULL in case of an error.
*/
coap_async_t *
coap_register_async(coap_session_t *session,
const coap_pdu_t *request,
coap_tick_t delay);
/**
* Update the delay timeout, so changing when the registered @p async triggers.
*
* When the new delay expires, a copy of the original request will get sent to
* the appropriate request handler.
*
* @param async The object to update.
* @param delay The amount of time to delay before sending response, 0 means
* wait forever.
*/
void
coap_async_set_delay(coap_async_t *async, coap_tick_t delay);
/**
* Releases the memory that was allocated by coap_register_async() for the
* object @p async.
*
* @param session The session to use.
* @param async The object to delete.
*/
void
coap_free_async(coap_session_t *session, coap_async_t *async);
/**
* Retrieves the object identified by @p token from the list of asynchronous
* transactions that are registered with @p context. This function returns a
* pointer to that object or @c NULL if not found.
*
* @param session The session that is used for asynchronous transmissions.
* @param token The PDU's token of the object to retrieve.
*
* @return A pointer to the object identified by @p token or @c NULL if
* not found.
*/
coap_async_t *coap_find_async(coap_session_t *session, coap_bin_const_t token);
/**
* Set the application data pointer held in @p async. This overwrites any
* existing data pointer.
*
* @param async The async state object.
* @param app_data The pointer to the data.
*/
void coap_async_set_app_data(coap_async_t *async, void *app_data);
/**
* Gets the application data pointer held in @p async.
*
* @param async The async state object.
*
* @return The applicaton data pointer.
*/
void *coap_async_get_app_data(const coap_async_t *async);
/** @} */
#endif /* COAP_ASYNC_H_ */

View File

@ -0,0 +1,348 @@
/*
* block.h -- block transfer
*
* Copyright (C) 2010-2012,2014-2015 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_BLOCK_H_
#define COAP_BLOCK_H_
#include "encode.h"
#include "option.h"
#include "pdu.h"
/**
* @defgroup block Block Transfer
* API functions for handling PDUs using CoAP BLOCK options
* @{
*/
#ifndef COAP_MAX_BLOCK_SZX
/**
* The largest value for the SZX component in a Block option.
*/
#define COAP_MAX_BLOCK_SZX 6
#endif /* COAP_MAX_BLOCK_SZX */
/**
* Structure of Block options.
*/
typedef struct {
unsigned int num; /**< block number */
unsigned int m:1; /**< 1 if more blocks follow, 0 otherwise */
unsigned int szx:3; /**< block size */
} coap_block_t;
#define COAP_BLOCK_USE_LIBCOAP 0x01 /* Use libcoap to do block requests */
#define COAP_BLOCK_SINGLE_BODY 0x02 /* Deliver the data as a single body */
/**
* Returns the value of the least significant byte of a Block option @p opt.
* For zero-length options (i.e. num == m == szx == 0), COAP_OPT_BLOCK_LAST
* returns @c NULL.
*/
#define COAP_OPT_BLOCK_LAST(opt) \
(coap_opt_length(opt) ? (coap_opt_value(opt) + (coap_opt_length(opt)-1)) : 0)
/** Returns the value of the More-bit of a Block option @p opt. */
#define COAP_OPT_BLOCK_MORE(opt) \
(coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x08) : 0)
/** Returns the value of the SZX-field of a Block option @p opt. */
#define COAP_OPT_BLOCK_SZX(opt) \
(coap_opt_length(opt) ? (*COAP_OPT_BLOCK_LAST(opt) & 0x07) : 0)
/**
* Returns the value of field @c num in the given block option @p block_opt.
*/
unsigned int coap_opt_block_num(const coap_opt_t *block_opt);
/**
* Checks if more than @p num blocks are required to deliver @p data_len
* bytes of data for a block size of 1 << (@p szx + 4).
*/
COAP_STATIC_INLINE int
coap_more_blocks(size_t data_len, unsigned int num, uint16_t szx) {
return ((num+1) << (szx + 4)) < data_len;
}
#if 0
/** Sets the More-bit in @p block_opt */
COAP_STATIC_INLINE void
coap_opt_block_set_m(coap_opt_t *block_opt, int m) {
if (m)
*(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) |= 0x08;
else
*(coap_opt_value(block_opt) + (coap_opt_length(block_opt) - 1)) &= ~0x08;
}
#endif
/**
* Initializes @p block from @p pdu. @p number must be either COAP_OPTION_BLOCK1
* or COAP_OPTION_BLOCK2. When option @p number was found in @p pdu, @p block is
* initialized with values from this option and the function returns the value
* @c 1. Otherwise, @c 0 is returned.
*
* @param pdu The pdu to search for option @p number.
* @param number The option number to search for (must be COAP_OPTION_BLOCK1 or
* COAP_OPTION_BLOCK2).
* @param block The block structure to initilize.
*
* @return @c 1 on success, @c 0 otherwise.
*/
int coap_get_block(const coap_pdu_t *pdu, coap_option_num_t number,
coap_block_t *block);
/**
* Writes a block option of type @p number to message @p pdu. If the requested
* block size is too large to fit in @p pdu, it is reduced accordingly. An
* exception is made for the final block when less space is required. The actual
* length of the resource is specified in @p data_length.
*
* This function may change *block to reflect the values written to @p pdu. As
* the function takes into consideration the remaining space @p pdu, no more
* options should be added after coap_write_block_opt() has returned.
*
* @param block The block structure to use. On return, this object is
* updated according to the values that have been written to
* @p pdu.
* @param number COAP_OPTION_BLOCK1 or COAP_OPTION_BLOCK2.
* @param pdu The message where the block option should be written.
* @param data_length The length of the actual data that will be added the @p
* pdu by calling coap_add_block().
*
* @return @c 1 on success, or a negative value on error.
*/
int coap_write_block_opt(coap_block_t *block,
coap_option_num_t number,
coap_pdu_t *pdu,
size_t data_length);
/**
* Adds the @p block_num block of size 1 << (@p block_szx + 4) from source @p
* data to @p pdu.
*
* @param pdu The message to add the block.
* @param len The length of @p data.
* @param data The source data to fill the block with.
* @param block_num The actual block number.
* @param block_szx Encoded size of block @p block_number.
*
* @return @c 1 on success, @c 0 otherwise.
*/
int coap_add_block(coap_pdu_t *pdu,
size_t len,
const uint8_t *data,
unsigned int block_num,
unsigned char block_szx);
/**
* Re-assemble payloads into a body
*
* @param body_data The pointer to the data for the body holding the
* representation so far or NULL if the first time.
* @param length The length of @p data.
* @param data The payload data to update the body with.
* @param offset The offset of the @p data into the body.
* @param total The estimated total size of the body.
*
* @return The current representation of the body or @c NULL if error.
* If NULL, @p body_data will have been de-allocated.
*/
coap_binary_t *
coap_block_build_body(coap_binary_t *body_data, size_t length,
const uint8_t *data, size_t offset, size_t total);
/**
* Adds the appropriate part of @p data to the @p response pdu. If blocks are
* required, then the appropriate block will be added to the PDU and sent.
* Adds a ETAG option that is the hash of the entire data if the data is to be
* split into blocks
* Used by a request handler.
*
* Note: The application will get called for every packet of a large body to
* process. Consider using coap_add_data_response_large() instead.
*
* @param request The requesting pdu.
* @param response The response pdu.
* @param media_type The format of the data.
* @param maxage The maxmimum life of the data. If @c -1, then there
* is no maxage.
* @param length The total length of the data.
* @param data The entire data block to transmit.
*
*/
void
coap_add_data_blocked_response(const coap_pdu_t *request,
coap_pdu_t *response,
uint16_t media_type,
int maxage,
size_t length,
const uint8_t* data);
/**
* Callback handler for de-allocating the data based on @p app_ptr provided to
* coap_add_data_large_*() functions following transmission of the supplied
* data.
*
* @param session The session that this data is associated with
* @param app_ptr The application provided pointer provided to the
* coap_add_data_large_* functions.
*/
typedef void (*coap_release_large_data_t)(coap_session_t *session,
void *app_ptr);
/**
* Associates given data with the @p pdu that is passed as second parameter.
*
* If all the data can be transmitted in a single PDU, this is functionally
* the same as coap_add_data() except @p release_func (if not NULL) will get
* invoked after data transmission.
*
* Used for a client request.
*
* If the data spans multiple PDUs, then the data will get transmitted using
* BLOCK1 option with the addition of the SIZE1 option.
* The underlying library will handle the transmission of the individual blocks.
* Once the body of data has been transmitted (or a failure occurred), then
* @p release_func (if not NULL) will get called so the application can
* de-allocate the @p data based on @p app_data. It is the responsibility of
* the application not to change the contents of @p data until the data
* transfer has completed.
*
* There is no need for the application to include the BLOCK1 option in the
* @p pdu.
*
* coap_add_data_large_request() (or the alternative coap_add_data_large_*()
* functions) must be called only once per PDU and must be the last PDU update
* before the PDU is transmitted. The (potentially) initial data will get
* transmitted when coap_send() is invoked.
*
* Note: COAP_BLOCK_USE_LIBCOAP must be set by coap_context_set_block_mode()
* for libcoap to work correctly when using this function.
*
* @param session The session to associate the data with.
* @param pdu The PDU to associate the data with.
* @param length The length of data to transmit.
* @param data The data to transmit.
* @param release_func The function to call to de-allocate @p data or @c NULL
* if the function is not required.
* @param app_ptr A Pointer that the application can provide for when
* release_func() is called.
*
* @return @c 1 if addition is successful, else @c 0.
*/
int coap_add_data_large_request(coap_session_t *session,
coap_pdu_t *pdu,
size_t length,
const uint8_t *data,
coap_release_large_data_t release_func,
void *app_ptr);
/**
* Associates given data with the @p response pdu that is passed as fourth
* parameter.
*
* If all the data can be transmitted in a single PDU, this is functionally
* the same as coap_add_data() except @p release_func (if not NULL) will get
* invoked after data transmission. The MEDIA_TYPE, MAXAGE and ETAG options may
* be added in as appropriate.
*
* Used by a server request handler to create the response.
*
* If the data spans multiple PDUs, then the data will get transmitted using
* BLOCK2 (response) option with the addition of the SIZE2 and ETAG
* options. The underlying library will handle the transmission of the
* individual blocks. Once the body of data has been transmitted (or a
* failure occurred), then @p release_func (if not NULL) will get called so the
* application can de-allocate the @p data based on @p app_data. It is the
* responsibility of the application not to change the contents of @p data
* until the data transfer has completed.
*
* There is no need for the application to include the BLOCK2 option in the
* @p pdu.
*
* coap_add_data_large_response() (or the alternative coap_add_data_large*()
* functions) must be called only once per PDU and must be the last PDU update
* before returning from the request handler function.
*
* Note: COAP_BLOCK_USE_LIBCOAP must be set by coap_context_set_block_mode()
* for libcoap to work correctly when using this function.
*
* @param resource The resource the data is associated with.
* @param session The coap session.
* @param request The requesting pdu.
* @param response The response pdu.
* @param query The query taken from the (original) requesting pdu.
* @param media_type The format of the data.
* @param maxage The maxmimum life of the data. If @c -1, then there
* is no maxage.
* @param etag ETag to use if not 0.
* @param length The total length of the data.
* @param data The entire data block to transmit.
* @param release_func The function to call to de-allocate @p data or NULL if
* the function is not required.
* @param app_ptr A Pointer that the application can provide for when
* release_func() is called.
*
* @return @c 1 if addition is successful, else @c 0.
*/
int
coap_add_data_large_response(coap_resource_t *resource,
coap_session_t *session,
const coap_pdu_t *request,
coap_pdu_t *response,
const coap_string_t *query,
uint16_t media_type,
int maxage,
uint64_t etag,
size_t length,
const uint8_t *data,
coap_release_large_data_t release_func,
void *app_ptr);
/**
* Set the context level CoAP block handling bits for handling RFC7959.
* These bits flow down to a session when a session is created and if the peer
* does not support something, an appropriate bit may get disabled in the
* session block_mode.
* The session block_mode then flows down into coap_crcv_t or coap_srcv_t where
* again an appropriate bit may get disabled.
*
* Note: This function must be called before the session is set up.
*
* Note: COAP_BLOCK_USE_LIBCOAP must be set if libcoap is to do all the
* block tracking and requesting, otherwise the application will have to do
* all of this work (the default if coap_context_set_block_mode() is not
* called).
*
* @param context The coap_context_t object.
* @param block_mode Zero or more COAP_BLOCK_ or'd options
*/
void coap_context_set_block_mode(coap_context_t *context,
uint8_t block_mode);
/**
* Cancel an observe that is being tracked by the client large receive logic.
* (coap_context_set_block_mode() has to be called)
* This will trigger the sending of an observe cancel pdu to the server.
*
* @param session The session that is being used for the observe.
* @param token The original token used to initiate the observation.
* @param message_type The COAP_MESSAGE_ type (NON or CON) to send the observe
* cancel pdu as.
*
* @return @c 1 if observe cancel transmission initiation is successful,
* else @c 0.
*/
int coap_cancel_observe(coap_session_t *session, coap_binary_t *token,
coap_pdu_type_t message_type);
/**@}*/
#endif /* COAP_BLOCK_H_ */

View File

@ -0,0 +1,89 @@
/*
* coap_asn1_internal.h -- ASN.1 functions for libcoap
*
* Copyright (C) 2020 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_asn1_internal.h
* @brief COAP ASN.1 internal information
*/
#ifndef COAP_ASN1_INTERNAL_H_
#define COAP_ASN1_INTERNAL_H_
/**
* @defgroup asn1 ASN.1 Support (Internal)
* CoAP ASN.1 Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
typedef enum {
COAP_ASN1_NONE = 0,
COAP_ASN1_INTEGER = 2,
COAP_ASN1_BITSTRING = 3,
COAP_ASN1_OCTETSTRING = 4,
COAP_ASN1_IDENTIFIER = 6,
} coap_asn1_tag_t;
/**
* Callback to validate the asn1 tag and data.
*
* Internal function.
*
* @param data The start of the tag and data
* @param size The size of the tag and data
*
* @return @c 1 if pass, else @c 0 if fail
*/
typedef int (*asn1_validate)(const uint8_t *data, size_t size);
/**
* Get the asn1 length from the current @p ptr.
*
* Internal function.
*
* @param ptr The current asn.1 object length pointer
*
* @return The length of the asn.1 object. @p ptr is updated to be after the length.
*/
size_t asn1_len(const uint8_t **ptr);
/**
* Get the asn1 tag from the current @p ptr.
*
* Internal function.
*
* @param ptr The current asn.1 object tag pointer
* @param constructed 1 if current tag is constructed
* @param class The current class of the tag
*
* @return The tag value.@p ptr is updated to be after the tag.
*/
coap_asn1_tag_t asn1_tag_c(const uint8_t **ptr, int *constructed, int *class);
/**
* Get the asn1 tag and data from the current @p ptr.
*
* Internal function.
*
* @param ltag The tag to look for
* @param ptr The current asn.1 object pointer
* @param tlen The remaining size oof the asn.1 data
* @param validate Call validate to verify tag data or @c NULL
*
* @return The asn.1 tag and data or @c NULL if not found
*/
coap_binary_t *get_asn1_tag(coap_asn1_tag_t ltag, const uint8_t *ptr,
size_t tlen, asn1_validate validate);
/** @} */
#endif /* COAP_ASN1_INTERNAL_H_ */

View File

@ -0,0 +1,67 @@
/*
* coap_async_internal.h -- state management for asynchronous messages
*
* Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_async_internal.h
* @brief CoAP async internal information
*/
#ifndef COAP_ASYNC_INTERNAL_H_
#define COAP_ASYNC_INTERNAL_H_
#include "coap3/net.h"
#ifndef WITHOUT_ASYNC
/**
* @defgroup coap_async_internal Asynchronous Messaging (Internal)
* @{
* CoAP Async Structures, Enums and Functions that are not exposed to
* applications.
* A coap_context_t object holds a list of coap_async_t objects that can be
* used to generate a separate response in the case a result of a request cannot
* be delivered immediately.
*/
struct coap_async_t {
struct coap_async_t *next; /**< internally used for linking */
coap_tick_t delay; /**< When to delay to before triggering the response
0 indicates never trigger */
coap_session_t *session; /**< transaction session */
coap_pdu_t *pdu; /**< copy of request pdu */
void* appdata; /** User definable data pointer */
};
/**
* Checks if there are any pending Async requests - if so, send them off.
* Otherewise return the time remaining for the next Async to be triggered
* or 0 if nothing to do.
*
* @param context The current context.
* @param now The current time in ticks.
*
* @return The tick time before the next Async needs to go, else 0 if
* nothing to do.
*/
coap_tick_t coap_check_async(coap_context_t *context, coap_tick_t now);
/**
* Removes and frees off all of the async entries for the given context.
*
* @param context The context to remove all async entries from.
*/
void
coap_delete_all_async(coap_context_t *context);
/** @} */
#endif /* WITHOUT_ASYNC */
#endif /* COAP_ASYNC_INTERNAL_H_ */

View File

@ -0,0 +1,239 @@
/*
* coap_block_internal.h -- Structures, Enums & Functions that are not
* exposed to application programming
*
* Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2021 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_block_internal.h
* @brief COAP block internal information
*/
#ifndef COAP_BLOCK_INTERNAL_H_
#define COAP_BLOCK_INTERNAL_H_
#include "coap_pdu_internal.h"
#include "resource.h"
/**
* @defgroup block_internal Block (Internal)
* Structures, Enums and Functions that are not exposed to applications
* @{
*/
typedef enum {
COAP_RECURSE_OK,
COAP_RECURSE_NO
} coap_recurse_t;
struct coap_lg_range {
uint32_t begin;
uint32_t end;
};
#define COAP_RBLOCK_CNT 4
/**
* Structure to keep track of received blocks
*/
typedef struct coap_rblock_t {
uint32_t used;
uint32_t retry;
struct coap_lg_range range[COAP_RBLOCK_CNT];
coap_tick_t last_seen;
} coap_rblock_t;
/**
* Structure to keep track of block1 specific information
* (Requests)
*/
typedef struct coap_l_block1_t {
coap_binary_t *app_token; /**< original PDU token */
uint8_t token[8]; /**< last used token */
size_t token_length; /**< length of token */
uint32_t count; /**< the number of packets sent for payload */
} coap_l_block1_t;
/**
* Structure to keep track of block2 specific information
* (Responses)
*/
typedef struct coap_l_block2_t {
coap_resource_t *resource; /**< associated resource */
coap_string_t *query; /**< Associated query for the resource */
uint64_t etag; /**< ETag value */
coap_time_t maxage_expire; /**< When this entry expires */
} coap_l_block2_t;
/**
* Structure to hold large body (many blocks) transmission information
*/
struct coap_lg_xmit_t {
struct coap_lg_xmit_t *next;
uint8_t blk_size; /**< large block transmission size */
uint16_t option; /**< large block transmisson CoAP option */
int last_block; /**< last acknowledged block number */
const uint8_t *data; /**< large data ptr */
size_t length; /**< large data length */
size_t offset; /**< large data next offset to transmit */
union {
coap_l_block1_t b1;
coap_l_block2_t b2;
} b;
coap_pdu_t pdu; /**< skeletal PDU */
coap_tick_t last_payload; /**< Last time MAX_PAYLOAD was sent or 0 */
coap_tick_t last_used; /**< Last time all data sent or 0 */
coap_release_large_data_t release_func; /**< large data de-alloc function */
void *app_ptr; /**< applicaton provided ptr for de-alloc function */
};
/**
* Structure to hold large body (many blocks) client receive information
*/
struct coap_lg_crcv_t {
struct coap_lg_crcv_t *next;
uint8_t observe[3]; /**< Observe data (if set) (only 24 bits) */
uint8_t observe_length;/**< Length of observe data */
uint8_t observe_set; /**< Set if this is an observe receive PDU */
uint8_t etag_set; /**< Set if ETag is in receive PDU */
uint8_t etag_length; /**< ETag length */
uint8_t etag[8]; /**< ETag for block checking */
uint16_t content_format; /**< Content format for the set of blocks */
uint8_t last_type; /**< Last request type (CON/NON) */
uint8_t initial; /**< If set, has not been used yet */
uint8_t szx; /**< size of individual blocks */
size_t total_len; /**< Length as indicated by SIZE2 option */
coap_binary_t *body_data; /**< Used for re-assembling entire body */
coap_binary_t *app_token; /**< app requesting PDU token */
uint8_t base_token[8]; /**< established base PDU token */
size_t base_token_length; /**< length of token */
uint8_t token[8]; /**< last used token */
size_t token_length; /**< length of token */
coap_pdu_t pdu; /**< skeletal PDU */
coap_rblock_t rec_blocks; /** < list of received blocks */
coap_tick_t last_used; /**< Last time all data sent or 0 */
uint16_t block_option; /**< Block option in use */
};
/**
* Structure to hold large body (many blocks) server receive information
*/
struct coap_lg_srcv_t {
struct coap_lg_srcv_t *next;
uint8_t observe[3]; /**< Observe data (if set) (only 24 bits) */
uint8_t observe_length;/**< Length of observe data */
uint8_t observe_set; /**< Set if this is an observe receive PDU */
uint8_t rtag_set; /**< Set if RTag is in receive PDU */
uint8_t rtag_length; /**< RTag length */
uint8_t rtag[8]; /**< RTag for block checking */
uint16_t content_format; /**< Content format for the set of blocks */
uint8_t last_type; /**< Last request type (CON/NON) */
uint8_t szx; /**< size of individual blocks */
size_t total_len; /**< Length as indicated by SIZE1 option */
coap_binary_t *body_data; /**< Used for re-assembling entire body */
size_t amount_so_far; /**< Amount of data seen so far */
coap_resource_t *resource; /**< associated resource */
coap_str_const_t *uri_path; /** set to uri_path if unknown resource */
coap_rblock_t rec_blocks; /** < list of received blocks */
uint8_t last_token[8]; /**< last used token */
size_t last_token_length; /**< length of token */
coap_mid_t last_mid; /**< Last received mid for this set of packets */
coap_tick_t last_used; /**< Last time data sent or 0 */
uint16_t block_option; /**< Block option in use */
};
coap_lg_crcv_t * coap_block_new_lg_crcv(coap_session_t *session,
coap_pdu_t *pdu);
void coap_block_delete_lg_crcv(coap_session_t *session,
coap_lg_crcv_t *lg_crcv);
coap_tick_t coap_block_check_lg_crcv_timeouts(coap_session_t *session,
coap_tick_t now);
void coap_block_delete_lg_srcv(coap_session_t *session,
coap_lg_srcv_t *lg_srcv);
coap_tick_t coap_block_check_lg_srcv_timeouts(coap_session_t *session,
coap_tick_t now);
int coap_handle_request_send_block(coap_session_t *session,
coap_pdu_t *pdu,
coap_pdu_t *response,
coap_resource_t *resource,
coap_string_t *query);
int coap_handle_request_put_block(coap_context_t *context,
coap_session_t *session,
coap_pdu_t *pdu,
coap_pdu_t *response,
coap_resource_t *resource,
coap_string_t *uri_path,
coap_opt_t *observe,
coap_string_t *query,
coap_method_handler_t h,
int *added_block);
int coap_handle_response_send_block(coap_session_t *session, coap_pdu_t *rcvd);
int coap_handle_response_get_block(coap_context_t *context,
coap_session_t *session,
coap_pdu_t *sent,
coap_pdu_t *rcvd,
coap_recurse_t recursive);
void coap_block_delete_lg_xmit(coap_session_t *session,
coap_lg_xmit_t *lg_xmit);
/**
* The function that does all the work for the coap_add_data_large*()
* functions.
*
* @param session The session to associate the data with.
* @param pdu The PDU to associate the data with.
* @param resource The resource to associate the data with (BLOCK2).
* @param query The query to associate the data with (BLOCK2).
* @param maxage The maxmimum life of the data. If @c -1, then there
* is no maxage (BLOCK2).
* @param etag ETag to use if not 0 (BLOCK2).
* @param length The length of data to transmit.
* @param data The data to transmit.
* @param release_func The function to call to de-allocate @p data or NULL if
* the function is not required.
* @param app_ptr A Pointer that the application can provide for when
* release_func() is called.
*
* @return @c 1 if transmission initiation is successful, else @c 0.
*/
int coap_add_data_large_internal(coap_session_t *session,
coap_pdu_t *pdu,
coap_resource_t *resource,
const coap_string_t *query,
int maxage,
uint64_t etag,
size_t length,
const uint8_t *data,
coap_release_large_data_t release_func,
void *app_ptr);
/**
* The function checks that the code in a newly formed lg_xmit created by
* coap_add_data_large_response() is updated.
*
* @param session The session
* @param response The response PDU to to check
* @param resource The requested resource
* @param query The requested query
*/
void coap_check_code_lg_xmit(coap_session_t *session, coap_pdu_t *response,
coap_resource_t *resource, coap_string_t *query);
/** @} */
#endif /* COAP_BLOCK_INTERNAL_H_ */

View File

@ -0,0 +1,232 @@
/* coap_cache.h -- Caching of CoAP requests
*
* Copyright (C) 2020 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
/**
* @file coap_cache.h
* @brief Provides a simple cache request storage for CoAP requests
*/
#ifndef COAP_CACHE_H_
#define COAP_CACHE_H_
#include "coap_forward_decls.h"
/**
* @defgroup cache Cache Support
* API functions for CoAP Caching
* @{
*/
/**
* Callback to free off the app data when the cache-entry is
* being deleted / freed off.
*
* @param data The app data to be freed off.
*/
typedef void (*coap_cache_app_data_free_callback_t)(void *data);
typedef enum coap_cache_session_based_t {
COAP_CACHE_NOT_SESSION_BASED,
COAP_CACHE_IS_SESSION_BASED
} coap_cache_session_based_t;
typedef enum coap_cache_record_pdu_t {
COAP_CACHE_NOT_RECORD_PDU,
COAP_CACHE_RECORD_PDU
} coap_cache_record_pdu_t;
/**
* Calculates a cache-key for the given CoAP PDU. See
* https://tools.ietf.org/html/rfc7252#section-5.6
* for an explanation of CoAP cache keys.
*
* Specific CoAP options can be removed from the cache-key. Examples of
* this are the BLOCK1 and BLOCK2 options - which make no real sense including
* them in a client or server environment, but should be included in a proxy
* caching environment where things are cached on a per block basis.
* This is done globally by calling the coap_cache_ignore_options()
* function.
*
* NOTE: The returned cache-key needs to be freed off by the caller by
* calling coap_cache_delete_key().
*
* @param session The session to add into cache-key if @p session_based
* is set.
* @param pdu The CoAP PDU for which a cache-key is to be
* calculated.
* @param session_based COAP_CACHE_IS_SESSION_BASED if session based
* cache-key, else COAP_CACHE_NOT_SESSION_BASED.
*
* @return The returned cache-key or @c NULL if failure.
*/
coap_cache_key_t *coap_cache_derive_key(const coap_session_t *session,
const coap_pdu_t *pdu,
coap_cache_session_based_t session_based);
/**
* Calculates a cache-key for the given CoAP PDU. See
* https://tools.ietf.org/html/rfc7252#section-5.6
* for an explanation of CoAP cache keys.
*
* Specific CoAP options can be removed from the cache-key. Examples of
* this are the BLOCK1 and BLOCK2 options - which make no real sense including
* them in a client or server environment, but should be included in a proxy
* caching environment where things are cached on a per block basis.
* This is done individually by specifying @p cache_ignore_count and
* @p cache_ignore_options .
*
* NOTE: The returned cache-key needs to be freed off by the caller by
* calling coap_cache_delete_key().
*
* @param session The session to add into cache-key if @p session_based
* is set.
* @param pdu The CoAP PDU for which a cache-key is to be
* calculated.
* @param session_based COAP_CACHE_IS_SESSION_BASED if session based
* cache-key, else COAP_CACHE_NOT_SESSION_BASED.
* @param ignore_options The array of options to ignore.
* @param ignore_count The number of options to ignore.
*
* @return The returned cache-key or @c NULL if failure.
*/
coap_cache_key_t *coap_cache_derive_key_w_ignore(const coap_session_t *session,
const coap_pdu_t *pdu,
coap_cache_session_based_t session_based,
const uint16_t *ignore_options,
size_t ignore_count);
/**
* Delete the cache-key.
*
* @param cache_key The cache-key to delete.
*/
void coap_delete_cache_key(coap_cache_key_t *cache_key);
/**
* Define the CoAP options that are not to be included when calculating
* the cache-key. Options that are defined as Non-Cache and the Observe
* option are always ignored.
*
* @param context The context to save the ignored options information in.
* @param options The array of options to ignore.
* @param count The number of options to ignore. Use 0 to reset the
* options matching.
*
* @return @return @c 1 if successful, else @c 0.
*/
int coap_cache_ignore_options(coap_context_t *context,
const uint16_t *options, size_t count);
/**
* Create a new cache-entry hash keyed by cache-key derived from the PDU.
*
* If @p session_based is set, then this cache-entry will get deleted when
* the session is freed off.
* If @p record_pdu is set, then the copied PDU will get freed off when
* this cache-entry is deleted.
*
* The cache-entry is maintained on a context hash list.
*
* @param session The session to use to derive the context from.
* @param pdu The pdu to use to generate the cache-key.
* @param record_pdu COAP_CACHE_RECORD_PDU if to take a copy of the PDU for
* later use, else COAP_CACHE_NOT_RECORD_PDU.
* @param session_based COAP_CACHE_IS_SESSION_BASED if to associate this
* cache-entry with the the session (which is embedded
* in the cache-entry), else COAP_CACHE_NOT_SESSION_BASED.
* @param idle_time Idle time in seconds before cache-entry is expired.
* If set to 0, it does not expire (but will get
* deleted if the session is deleted and it is session_based).
*
* @return The returned cache-key or @c NULL if failure.
*/
coap_cache_entry_t *coap_new_cache_entry(coap_session_t *session,
const coap_pdu_t *pdu,
coap_cache_record_pdu_t record_pdu,
coap_cache_session_based_t session_based,
unsigned int idle_time);
/**
* Remove a cache-entry from the hash list and free off all the appropriate
* contents apart from app_data.
*
* @param context The context to use.
* @param cache_entry The cache-entry to remove.
*/
void coap_delete_cache_entry(coap_context_t *context,
coap_cache_entry_t *cache_entry);
/**
* Searches for a cache-entry identified by @p cache_key. This
* function returns the corresponding cache-entry or @c NULL
* if not found.
*
* @param context The context to use.
* @param cache_key The cache-key to get the hashed coap-entry.
*
* @return The cache-entry for @p cache_key or @c NULL if not found.
*/
coap_cache_entry_t *coap_cache_get_by_key(coap_context_t *context,
const coap_cache_key_t *cache_key);
/**
* Searches for a cache-entry corresponding to @p pdu. This
* function returns the corresponding cache-entry or @c NULL if not
* found.
*
* @param session The session to use.
* @param pdu The CoAP request to search for.
* @param session_based COAP_CACHE_IS_SESSION_BASED if session based
* cache-key to be used, else COAP_CACHE_NOT_SESSION_BASED.
*
* @return The cache-entry for @p request or @c NULL if not found.
*/
coap_cache_entry_t *coap_cache_get_by_pdu(coap_session_t *session,
const coap_pdu_t *pdu,
coap_cache_session_based_t session_based);
/**
* Returns the PDU information stored in the @p coap_cache entry.
*
* @param cache_entry The CoAP cache entry.
*
* @return The PDU information stored in the cache_entry or NULL
* if the PDU was not initially copied.
*/
const coap_pdu_t *coap_cache_get_pdu(const coap_cache_entry_t *cache_entry);
/**
* Stores @p data with the given cache entry. This function
* overwrites any value that has previously been stored with @p
* cache_entry.
*
* @param cache_entry The CoAP cache entry.
* @param data The data pointer to store with wih the cache entry. Note that
* this data must be valid during the lifetime of @p cache_entry.
* @param callback The callback to call to free off this data when the
* cache-entry is deleted, or @c NULL if not required.
*/
void coap_cache_set_app_data(coap_cache_entry_t *cache_entry, void *data,
coap_cache_app_data_free_callback_t callback);
/**
* Returns any application-specific data that has been stored with @p
* cache_entry using the function coap_cache_set_app_data(). This function will
* return @c NULL if no data has been stored.
*
* @param cache_entry The CoAP cache entry.
*
* @return The data pointer previously stored or @c NULL if no data stored.
*/
void *coap_cache_get_app_data(const coap_cache_entry_t *cache_entry);
/** @} */
#endif /* COAP_CACHE_H */

View File

@ -0,0 +1,111 @@
/*
* coap_cache_internal.h -- Cache functions for libcoap
*
* Copyright (C) 2019--2020 Olaf Bergmann <bergmann@tzi.org> and others
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_cache_internal.h
* @brief COAP cache internal information
*/
#ifndef COAP_CACHE_INTERNAL_H_
#define COAP_CACHE_INTERNAL_H_
#include "coap_io.h"
/**
* @defgroup cache_internal Cache Support (Internal)
* CoAP Cache Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
/* Holds a digest in binary typically sha256 except for notls */
typedef struct coap_digest_t {
uint8_t key[32];
} coap_digest_t;
struct coap_cache_key_t {
uint8_t key[32];
};
struct coap_cache_entry_t {
UT_hash_handle hh;
coap_cache_key_t *cache_key;
coap_session_t *session;
coap_pdu_t *pdu;
void* app_data;
coap_tick_t expire_ticks;
unsigned int idle_timeout;
coap_cache_app_data_free_callback_t callback;
};
/**
* Expire coap_cache_entry_t entries
*
* Internal function.
*
* @param context The context holding the coap-entries to exire
*/
void coap_expire_cache_entries(coap_context_t *context);
typedef void coap_digest_ctx_t;
/**
* Initialize a coap_digest
*
* Internal function.
*
* @return The digest context or @c NULL if failure.
*/
coap_digest_ctx_t *coap_digest_setup(void);
/**
* Free off coap_digest_ctx_t. Always done by
* coap_digest_final()
*
* Internal function.
*
* @param digest_ctx The coap_digest context.
*/
void coap_digest_free(coap_digest_ctx_t *digest_ctx);
/**
* Update the coap_digest information with the next chunk of data
*
* Internal function.
*
* @param digest_ctx The coap_digest context.
* @param data Pointer to data.
* @param data_len Number of bytes.
*
* @return @c 1 success, @c 0 failure.
*/
int coap_digest_update(coap_digest_ctx_t *digest_ctx,
const uint8_t *data,
size_t data_len
);
/**
* Finalize the coap_digest information into the provided
* @p digest_buffer.
*
* Internal function.
*
* @param digest_ctx The coap_digest context.
* @param digest_buffer Pointer to digest buffer to update
*
* @return @c 1 success, @c 0 failure.
*/
int coap_digest_final(coap_digest_ctx_t *digest_ctx,
coap_digest_t *digest_buffer);
/** @} */
#endif /* COAP_CACHE_INTERNAL_H_ */

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2010-2011,2014 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -36,8 +38,16 @@
* Logging type. One of LOG_* from @b syslog.
*/
typedef short coap_log_t;
#else
/** Pre-defined log levels akin to what is used in \b syslog. */
/*
LOG_DEBUG+2 gives ciphers in GnuTLS
Use COAP_LOG_CIPHERS to output Cipher Info in OpenSSL etc.
*/
#define COAP_LOG_CIPHERS (LOG_DEBUG+2)
#else /* !HAVE_SYSLOG_H */
/** Pre-defined log levels akin to what is used in \b syslog
with LOG_CIPHERS added. */
#if !defined(RIOT_VERSION)
typedef enum {
LOG_EMERG=0, /**< Emergency */
LOG_ALERT, /**< Alert */
@ -46,9 +56,29 @@ typedef enum {
LOG_WARNING, /**< Warning */
LOG_NOTICE, /**< Notice */
LOG_INFO, /**< Information */
LOG_DEBUG /**< Debug */
LOG_DEBUG, /**< Debug */
COAP_LOG_CIPHERS=LOG_DEBUG+2 /**< CipherInfo */
} coap_log_t;
#endif
#else /* RIOT_VERSION */
/* RIOT defines a subset of the syslog levels in log.h with different
* numeric values. The remaining levels are defined here. Note that
* output granularity differs from what would be expected when
* adhering to the syslog levels.
*/
#include <log.h>
typedef short coap_log_t;
#define LOG_EMERG (0)
#define LOG_ALERT (1)
#define LOG_CRIT (2)
#define LOG_ERR (3)
/* LOG_WARNING (4) */
#define LOG_NOTICE (5)
/* LOG_INFO (6) */
/* LOG_DEBUG (7) */
#define COAP_LOG_CIPHERS (9)
#endif /* RIOT_VERSION */
#endif /* !HAVE_SYSLOG_H */
/**
* Get the current logging level.
@ -65,7 +95,7 @@ coap_log_t coap_get_log_level(void);
void coap_set_log_level(coap_log_t level);
/**
* Logging call-back handler definition.
* Logging callback handler definition.
*
* @param level One of the LOG_* values.
* @param message Zero-terminated string message to log.
@ -163,7 +193,15 @@ void coap_show_tls_version(coap_log_t level);
*/
char *coap_string_tls_version(char *buffer, size_t bufsize);
struct coap_address_t;
/**
* Build a string containing the current (D)TLS library support
*
* @param buffer The buffer to put the string into.
* @param bufsize The size of the buffer to put the string into.
*
* @return A pointer to the provided buffer.
*/
char *coap_string_tls_support(char *buffer, size_t bufsize);
/**
* Print the address into the defined buffer.
@ -176,7 +214,7 @@ struct coap_address_t;
*
* @return The amount written into the buffer.
*/
size_t coap_print_addr(const struct coap_address_t *address,
size_t coap_print_addr(const coap_address_t *address,
unsigned char *buffer, size_t size);
/** @} */

View File

@ -0,0 +1,479 @@
/*
* coap_dtls.h -- (Datagram) Transport Layer Support for libcoap
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2017 Jean-Claude Michelou <jcm@spinetix.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_DTLS_H_
#define COAP_DTLS_H_
#include "coap_time.h"
#include "str.h"
/**
* @defgroup dtls DTLS Support
* API functions for interfacing with DTLS libraries.
* @{
*/
typedef struct coap_dtls_pki_t coap_dtls_pki_t;
#ifndef COAP_DTLS_HINT_LENGTH
#define COAP_DTLS_HINT_LENGTH 128
#endif
typedef enum coap_dtls_role_t {
COAP_DTLS_ROLE_CLIENT, /**< Internal function invoked for client */
COAP_DTLS_ROLE_SERVER /**< Internal function invoked for server */
} coap_dtls_role_t;
#define COAP_DTLS_RPK_CERT_CN "RPK"
/**
* Check whether DTLS is available.
*
* @return @c 1 if support for DTLS is enabled, or @c 0 otherwise.
*/
int coap_dtls_is_supported(void);
/**
* Check whether TLS is available.
*
* @return @c 1 if support for TLS is enabled, or @c 0 otherwise.
*/
int coap_tls_is_supported(void);
typedef enum coap_tls_library_t {
COAP_TLS_LIBRARY_NOTLS = 0, /**< No DTLS library */
COAP_TLS_LIBRARY_TINYDTLS, /**< Using TinyDTLS library */
COAP_TLS_LIBRARY_OPENSSL, /**< Using OpenSSL library */
COAP_TLS_LIBRARY_GNUTLS, /**< Using GnuTLS library */
COAP_TLS_LIBRARY_MBEDTLS, /**< Using Mbed TLS library */
} coap_tls_library_t;
/**
* The structure used for returning the underlying (D)TLS library
* information.
*/
typedef struct coap_tls_version_t {
uint64_t version; /**< (D)TLS runtime Library Version */
coap_tls_library_t type; /**< Library type. One of COAP_TLS_LIBRARY_* */
uint64_t built_version; /**< (D)TLS Built against Library Version */
} coap_tls_version_t;
/**
* Determine the type and version of the underlying (D)TLS library.
*
* @return The version and type of library libcoap was compiled against.
*/
coap_tls_version_t *coap_get_tls_library_version(void);
/**
* Additional Security setup handler that can be set up by
* coap_context_set_pki().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to do some additional checks/changes/updates.
*
* @param tls_session The security session definition - e.g. SSL * for OpenSSL.
* NULL if server callback.
* This will be dependent on the underlying TLS library -
* see coap_get_tls_library_version()
* @param setup_data A structure containing setup data originally passed into
* coap_context_set_pki() or coap_new_client_session_pki().
*
* @return @c 1 if successful, else @c 0.
*/
typedef int (*coap_dtls_security_setup_t)(void* tls_session,
coap_dtls_pki_t *setup_data);
/**
* CN Validation callback that can be set up by coap_context_set_pki().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to check that the CN is allowed.
* CN is the SubjectAltName in the cert, if not present, then the leftmost
* Common Name (CN) component of the subject name.
* NOTE: If using RPK, then the Public Key does not contain a CN, but the
* content of COAP_DTLS_RPK_CERT_CN is presented for the @p cn parameter.
*
* @param cn The determined CN from the certificate
* @param asn1_public_cert The ASN.1 DER encoded X.509 certificate
* @param asn1_length The ASN.1 length
* @param coap_session The CoAP session associated with the certificate update
* @param depth Depth in cert chain. If 0, then client cert, else a CA
* @param validated TLS layer can find no issues if 1
* @param arg The same as was passed into coap_context_set_pki()
* in setup_data->cn_call_back_arg
*
* @return @c 1 if accepted, else @c 0 if to be rejected.
*/
typedef int (*coap_dtls_cn_callback_t)(const char *cn,
const uint8_t *asn1_public_cert,
size_t asn1_length,
coap_session_t *coap_session,
unsigned int depth,
int validated,
void *arg);
/**
* The enum used for determining the provided PKI ASN.1 (DER) Private Key
* formats.
*/
typedef enum coap_asn1_privatekey_type_t {
COAP_ASN1_PKEY_NONE, /**< NONE */
COAP_ASN1_PKEY_RSA, /**< RSA type */
COAP_ASN1_PKEY_RSA2, /**< RSA2 type */
COAP_ASN1_PKEY_DSA, /**< DSA type */
COAP_ASN1_PKEY_DSA1, /**< DSA1 type */
COAP_ASN1_PKEY_DSA2, /**< DSA2 type */
COAP_ASN1_PKEY_DSA3, /**< DSA3 type */
COAP_ASN1_PKEY_DSA4, /**< DSA4 type */
COAP_ASN1_PKEY_DH, /**< DH type */
COAP_ASN1_PKEY_DHX, /**< DHX type */
COAP_ASN1_PKEY_EC, /**< EC type */
COAP_ASN1_PKEY_HMAC, /**< HMAC type */
COAP_ASN1_PKEY_CMAC, /**< CMAC type */
COAP_ASN1_PKEY_TLS1_PRF, /**< TLS1_PRF type */
COAP_ASN1_PKEY_HKDF /**< HKDF type */
} coap_asn1_privatekey_type_t;
/**
* The enum used for determining the PKI key formats.
*/
typedef enum coap_pki_key_t {
COAP_PKI_KEY_PEM = 0, /**< The PKI key type is PEM file */
COAP_PKI_KEY_ASN1, /**< The PKI key type is ASN.1 (DER) buffer */
COAP_PKI_KEY_PEM_BUF, /**< The PKI key type is PEM buffer */
COAP_PKI_KEY_PKCS11, /**< The PKI key type is PKCS11 (DER) */
} coap_pki_key_t;
/**
* The structure that holds the PKI PEM definitions.
*/
typedef struct coap_pki_key_pem_t {
const char *ca_file; /**< File location of Common CA in PEM format */
const char *public_cert; /**< File location of Public Cert */
const char *private_key; /**< File location of Private Key in PEM format */
} coap_pki_key_pem_t;
/**
* The structure that holds the PKI PEM buffer definitions.
* The certificates and private key data must be in PEM format.
*
* Note: The Certs and Key should be NULL terminated strings for
* performance reasons (to save a potential buffer copy) and the length include
* this NULL terminator. It is not a requirement to have the NULL terminator
* though and the length must then reflect the actual data size.
*/
typedef struct coap_pki_key_pem_buf_t {
const uint8_t *ca_cert; /**< PEM buffer Common CA Cert */
const uint8_t *public_cert; /**< PEM buffer Public Cert, or Public Key if RPK */
const uint8_t *private_key; /**< PEM buffer Private Key
If RPK and 'EC PRIVATE KEY' this can be used
for both the public_cert and private_key */
size_t ca_cert_len; /**< PEM buffer CA Cert length */
size_t public_cert_len; /**< PEM buffer Public Cert length */
size_t private_key_len; /**< PEM buffer Private Key length */
} coap_pki_key_pem_buf_t;
/**
* The structure that holds the PKI ASN.1 (DER) definitions.
*/
typedef struct coap_pki_key_asn1_t {
const uint8_t *ca_cert; /**< ASN1 (DER) Common CA Cert */
const uint8_t *public_cert; /**< ASN1 (DER) Public Cert, or Public Key if RPK */
const uint8_t *private_key; /**< ASN1 (DER) Private Key */
size_t ca_cert_len; /**< ASN1 CA Cert length */
size_t public_cert_len; /**< ASN1 Public Cert length */
size_t private_key_len; /**< ASN1 Private Key length */
coap_asn1_privatekey_type_t private_key_type; /**< Private Key Type */
} coap_pki_key_asn1_t;
/**
* The structure that holds the PKI PKCS11 definitions.
*/
typedef struct coap_pki_key_pkcs11_t {
const char *ca; /**< pkcs11: URI for Common CA Certificate */
const char *public_cert; /**< pkcs11: URI for Public Cert */
const char *private_key; /**< pkcs11: URI for Private Key */
const char *user_pin; /**< User pin to access PKCS11. If NULL, then
pin-value= parameter must be set in
pkcs11: URI as a query. */
} coap_pki_key_pkcs11_t;
/**
* The structure that holds the PKI key information.
*/
typedef struct coap_dtls_key_t {
coap_pki_key_t key_type; /**< key format type */
union {
coap_pki_key_pem_t pem; /**< for PEM file keys */
coap_pki_key_pem_buf_t pem_buf; /**< for PEM memory keys */
coap_pki_key_asn1_t asn1; /**< for ASN.1 (DER) memory keys */
coap_pki_key_pkcs11_t pkcs11; /**< for PKCS11 keys */
} key;
} coap_dtls_key_t;
/**
* Server Name Indication (SNI) Validation callback that can be set up by
* coap_context_set_pki().
* Invoked if the SNI is not previously seen and prior to sending a certificate
* set back to the client so that the appropriate certificate set can be used
* based on the requesting SNI.
*
* @param sni The requested SNI
* @param arg The same as was passed into coap_context_set_pki()
* in setup_data->sni_call_back_arg
*
* @return New set of certificates to use, or @c NULL if SNI is to be rejected.
*/
typedef coap_dtls_key_t *(*coap_dtls_pki_sni_callback_t)(const char *sni,
void* arg);
#define COAP_DTLS_PKI_SETUP_VERSION 1 /**< Latest PKI setup version */
/**
* The structure used for defining the PKI setup data to be used.
*/
struct coap_dtls_pki_t {
uint8_t version; /** Set to COAP_DTLS_PKI_SETUP_VERSION
to support this version of the struct */
/* Options to enable different TLS functionality in libcoap */
uint8_t verify_peer_cert; /**< 1 if peer cert is to be verified */
uint8_t check_common_ca; /**< 1 if peer cert is to be signed by
* the same CA as the local cert */
uint8_t allow_self_signed; /**< 1 if self-signed certs are allowed.
* Ignored if check_common_ca set */
uint8_t allow_expired_certs; /**< 1 if expired certs are allowed */
uint8_t cert_chain_validation; /**< 1 if to check cert_chain_verify_depth */
uint8_t cert_chain_verify_depth; /**< recommended depth is 3 */
uint8_t check_cert_revocation; /**< 1 if revocation checks wanted */
uint8_t allow_no_crl; /**< 1 ignore if CRL not there */
uint8_t allow_expired_crl; /**< 1 if expired crl is allowed */
uint8_t allow_bad_md_hash; /**< 1 if unsupported MD hashes are allowed */
uint8_t allow_short_rsa_length; /**< 1 if small RSA keysizes are allowed */
uint8_t is_rpk_not_cert; /**< 1 is RPK instead of Public Certificate.
* If set, PKI key format type cannot be
* COAP_PKI_KEY_PEM */
uint8_t reserved[3]; /**< Reserved - must be set to 0 for
future compatibility */
/* Size of 3 chosen to align to next
* parameter, so if newly defined option
* it can use one of the reserverd slot so
* no need to change
* COAP_DTLS_PKI_SETUP_VERSION and just
* decrement the reserved[] count.
*/
/** CN check callback function.
* If not NULL, is called when the TLS connection has passed the configured
* TLS options above for the application to verify if the CN is valid.
*/
coap_dtls_cn_callback_t validate_cn_call_back;
void *cn_call_back_arg; /**< Passed in to the CN callback function */
/** SNI check callback function.
* If not @p NULL, called if the SNI is not previously seen and prior to
* sending a certificate set back to the client so that the appropriate
* certificate set can be used based on the requesting SNI.
*/
coap_dtls_pki_sni_callback_t validate_sni_call_back;
void *sni_call_back_arg; /**< Passed in to the sni callback function */
/** Additional Security callback handler that is invoked when libcoap has
* done the standard, defined validation checks at the TLS level,
* If not @p NULL, called from within the TLS Client Hello connection
* setup.
*/
coap_dtls_security_setup_t additional_tls_setup_call_back;
char* client_sni; /**< If not NULL, SNI to use in client TLS setup.
Owned by the client app and must remain valid
during the call to coap_new_client_session_pki() */
coap_dtls_key_t pki_key; /**< PKI key definition */
};
/**
* The structure that holds the Client PSK information.
*/
typedef struct coap_dtls_cpsk_info_t {
coap_bin_const_t identity;
coap_bin_const_t key;
} coap_dtls_cpsk_info_t;
/**
* Identity Hint Validation callback that can be set up by
* coap_new_client_session_psk2().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to check that the Identity Hint is allowed,
* and thus needs to use the appropriate PSK information for the Identity
* Hint for the (D)TLS session.
* Note: Identity Hint is not supported in (D)TLS1.3.
*
* @param hint The server provided Identity Hint
* @param coap_session The CoAP session associated with the Identity Hint
* @param arg The same as was passed into coap_new_client_session_psk2()
* in setup_data->ih_call_back_arg
*
* @return New coap_dtls_cpsk_info_t object or @c NULL on error.
*/
typedef const coap_dtls_cpsk_info_t *(*coap_dtls_ih_callback_t)(
coap_str_const_t *hint,
coap_session_t *coap_session,
void *arg);
#define COAP_DTLS_CPSK_SETUP_VERSION 1 /**< Latest CPSK setup version */
/**
* The structure used for defining the Client PSK setup data to be used.
*/
typedef struct coap_dtls_cpsk_t {
uint8_t version; /** Set to COAP_DTLS_CPSK_SETUP_VERSION
to support this version of the struct */
/* Options to enable different TLS functionality in libcoap */
uint8_t reserved[7]; /**< Reserved - must be set to 0 for
future compatibility */
/* Size of 7 chosen to align to next
* parameter, so if newly defined option
* it can use one of the reserverd slot so
* no need to change
* COAP_DTLS_CPSK_SETUP_VERSION and just
* decrement the reserved[] count.
*/
/** Identity Hint check callback function.
* If not NULL, is called when the Identity Hint (TLS1.2 or earlier) is
* provided by the server.
* The appropriate Identity and Pre-shared Key to use can then be returned.
*/
coap_dtls_ih_callback_t validate_ih_call_back;
void *ih_call_back_arg; /**< Passed in to the Identity Hint callback
function */
char* client_sni; /**< If not NULL, SNI to use in client TLS setup.
Owned by the client app and must remain valid
during the call to coap_new_client_session_psk2()
Note: Not supported by TinyDTLS. */
coap_dtls_cpsk_info_t psk_info; /**< Client PSK definition */
} coap_dtls_cpsk_t;
/**
* The structure that holds the Server Pre-Shared Key and Identity
* Hint information.
*/
typedef struct coap_dtls_spsk_info_t {
coap_bin_const_t hint;
coap_bin_const_t key;
} coap_dtls_spsk_info_t;
/**
* Identity Validation callback that can be set up by
* coap_context_set_psk2().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to check that the Identity is allowed,
* and needs to use the appropriate Pre-Shared Key for the (D)TLS session.
*
* @param identity The client provided Identity
* @param coap_session The CoAP session associated with the Identity Hint
* @param arg The value as passed into coap_context_set_psk2()
* in setup_data->id_call_back_arg
*
* @return New coap_bin_const_t object containing the Pre-Shared Key or
@c NULL on error.
* Note: This information will be duplicated into an internal
* structure.
*/
typedef const coap_bin_const_t *(*coap_dtls_id_callback_t)(
coap_bin_const_t *identity,
coap_session_t *coap_session,
void *arg);
/**
* PSK SNI callback that can be set up by coap_context_set_psk2().
* Invoked when libcoap has done the validation checks at the TLS level
* and the application needs to:-
* a) check that the SNI is allowed
* b) provide the appropriate PSK information for the (D)TLS session.
*
* @param sni The client provided SNI
* @param coap_session The CoAP session associated with the SNI
* @param arg The same as was passed into coap_context_set_psk2()
* in setup_data->sni_call_back_arg
*
* @return New coap_dtls_spsk_info_t object or @c NULL on error.
*/
typedef const coap_dtls_spsk_info_t *(*coap_dtls_psk_sni_callback_t)(
const char *sni,
coap_session_t *coap_session,
void *arg);
#define COAP_DTLS_SPSK_SETUP_VERSION 1 /**< Latest SPSK setup version */
/**
* The structure used for defining the Server PSK setup data to be used.
*/
typedef struct coap_dtls_spsk_t {
uint8_t version; /** Set to COAP_DTLS_SPSK_SETUP_VERSION
to support this version of the struct */
/* Options to enable different TLS functionality in libcoap */
uint8_t reserved[7]; /**< Reserved - must be set to 0 for
future compatibility */
/* Size of 7 chosen to align to next
* parameter, so if newly defined option
* it can use one of the reserverd slot so
* no need to change
* COAP_DTLS_SPSK_SETUP_VERSION and just
* decrement the reserved[] count.
*/
/** Identity check callback function.
* If not @p NULL, is called when the Identity is provided by the client.
* The appropriate Pre-Shared Key to use can then be returned.
*/
coap_dtls_id_callback_t validate_id_call_back;
void *id_call_back_arg; /**< Passed in to the Identity callback function */
/** SNI check callback function.
* If not @p NULL, called if the SNI is not previously seen and prior to
* sending PSK information back to the client so that the appropriate
* PSK information can be used based on the requesting SNI.
*/
coap_dtls_psk_sni_callback_t validate_sni_call_back;
void *sni_call_back_arg; /**< Passed in to the SNI callback function */
coap_dtls_spsk_info_t psk_info; /**< Server PSK definition */
} coap_dtls_spsk_t;
/** @} */
/**
* @ingroup logging
* Sets the (D)TLS logging level to the specified @p level.
* Note: coap_log_level() will influence output if at a specified level.
*
* @param level The logging level to use - LOG_*
*/
void coap_dtls_set_log_level(int level);
/**
* @ingroup logging
* Get the current (D)TLS logging.
*
* @return The current log level (one of LOG_*).
*/
int coap_dtls_get_log_level(void);
#endif /* COAP_DTLS_H */

View File

@ -0,0 +1,345 @@
/*
* coap_dtls_internal.h -- (Datagram) Transport Layer Support for libcoap
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2017 Jean-Claude Michelou <jcm@spinetix.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_DTLS_INTERNAL_H_
#define COAP_DTLS_INTERNAL_H_
/**
* @defgroup dtls_internal DTLS Support (Internal)
* CoAP DTLS Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
/* https://tools.ietf.org/html/rfc6347#section-4.2.4.1 */
#ifndef COAP_DTLS_RETRANSMIT_MS
#define COAP_DTLS_RETRANSMIT_MS 1000
#endif
#ifndef COAP_DTLS_RETRANSMIT_TOTAL_MS
#define COAP_DTLS_RETRANSMIT_TOTAL_MS 60000
#endif
#define COAP_DTLS_RETRANSMIT_COAP_TICKS (COAP_DTLS_RETRANSMIT_MS * COAP_TICKS_PER_SECOND / 1000)
/**
* Creates a new DTLS context for the given @p coap_context. This function
* returns a pointer to a new DTLS context object or @c NULL on error.
*
* @param coap_context The CoAP context where the DTLS object shall be used.
*
* @return A DTLS context object or @c NULL on error.
*/
void *
coap_dtls_new_context(coap_context_t *coap_context);
/**
* Set the DTLS context's default server PSK information.
* This does the PSK specifics following coap_dtls_new_context().
*
* @param coap_context The CoAP context.
* @param setup_data A structure containing setup data originally passed into
* coap_context_set_psk2().
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_spsk(coap_context_t *coap_context,
coap_dtls_spsk_t *setup_data);
/**
* Set the DTLS context's default client PSK information.
* This does the PSK specifics following coap_dtls_new_context().
*
* @param coap_context The CoAP context.
* @param setup_data A structure containing setup data originally passed into
* coap_new_client_session_psk2().
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_cpsk(coap_context_t *coap_context,
coap_dtls_cpsk_t *setup_data);
/**
* Set the DTLS context's default server PKI information.
* This does the PKI specifics following coap_dtls_new_context().
* If @p COAP_DTLS_ROLE_SERVER, then the information will get put into the
* TLS library's context (from which sessions are derived).
* If @p COAP_DTLS_ROLE_CLIENT, then the information will get put into the
* TLS library's session.
*
* @param coap_context The CoAP context.
* @param setup_data Setup information defining how PKI is to be setup.
* Required parameter. If @p NULL, PKI will not be
* set up.
* @param role One of @p COAP_DTLS_ROLE_CLIENT or @p COAP_DTLS_ROLE_SERVER
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_pki(coap_context_t *coap_context,
const coap_dtls_pki_t *setup_data,
const coap_dtls_role_t role);
/**
* Set the dtls context's default Root CA information for a client or server.
*
* @param coap_context The current coap_context_t object.
* @param ca_file If not @p NULL, is the full path name of a PEM encoded
* file containing all the Root CAs to be used.
* @param ca_dir If not @p NULL, points to a directory containing PEM
* encoded files containing all the Root CAs to be used.
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_pki_root_cas(coap_context_t *coap_context,
const char *ca_file,
const char *ca_dir);
/**
* Check whether one of the coap_dtls_context_set_{psk|pki}() functions have
* been called.
*
* @param coap_context The current coap_context_t object.
*
* @return @c 1 if coap_dtls_context_set_{psk|pki}() called, else @c 0.
*/
int coap_dtls_context_check_keys_enabled(coap_context_t *coap_context);
/**
* Releases the storage allocated for @p dtls_context.
*
* @param dtls_context The DTLS context as returned by coap_dtls_new_context().
*/
void coap_dtls_free_context(void *dtls_context);
/**
* Create a new client-side session. This should send a HELLO to the server.
*
* @param coap_session The CoAP session.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_dtls_new_client_session(coap_session_t *coap_session);
/**
* Create a new DTLS server-side session.
* Called after coap_dtls_hello() has returned @c 1, signalling that a validated
* HELLO was received from a client.
* This should send a HELLO to the server.
*
* @param coap_session The CoAP session.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the DTLS session.
*/
void *coap_dtls_new_server_session(coap_session_t *coap_session);
/**
* Terminates the DTLS session (may send an ALERT if necessary) then frees the
* underlying TLS library object containing security parameters for the session.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_free_session(coap_session_t *coap_session);
/**
* Notify of a change in the CoAP session's MTU, for example after
* a PMTU update.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_session_update_mtu(coap_session_t *coap_session);
/**
* Send data to a DTLS peer.
*
* @param coap_session The CoAP session.
* @param data pointer to data.
* @param data_len Number of bytes to send.
*
* @return @c 0 if this would be blocking, @c -1 if there is an error or the
* number of cleartext bytes sent.
*/
int coap_dtls_send(coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Check if timeout is handled per CoAP session or per CoAP context.
*
* @return @c 1 of timeout and retransmit is per context, @c 0 if it is
* per session.
*/
int coap_dtls_is_context_timeout(void);
/**
* Do all pending retransmits and get next timeout
*
* @param dtls_context The DTLS context.
*
* @return @c 0 if no event is pending or date of the next retransmit.
*/
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context);
/**
* Get next timeout for this session.
*
* @param coap_session The CoAP session.
* @param now The current time in ticks.
*
* @return @c 0 If no event is pending or ticks time of the next retransmit.
*/
coap_tick_t coap_dtls_get_timeout(coap_session_t *coap_session,
coap_tick_t now);
/**
* Handle a DTLS timeout expiration.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_handle_timeout(coap_session_t *coap_session);
/**
* Handling incoming data from a DTLS peer.
*
* @param coap_session The CoAP session.
* @param data Encrypted datagram.
* @param data_len Encrypted datagram size.
*
* @return Result of coap_handle_dgram on the decrypted CoAP PDU
* or @c -1 for error.
*/
int coap_dtls_receive(coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Handling client HELLO messages from a new candiate peer.
* Note that session->tls is empty.
*
* @param coap_session The CoAP session.
* @param data Encrypted datagram.
* @param data_len Encrypted datagram size.
*
* @return @c 0 if a cookie verification message has been sent, @c 1 if the
* HELLO contains a valid cookie and a server session should be created,
* @c -1 if the message is invalid.
*/
int coap_dtls_hello(coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Get DTLS overhead over cleartext PDUs.
*
* @param coap_session The CoAP session.
*
* @return Maximum number of bytes added by DTLS layer.
*/
unsigned int coap_dtls_get_overhead(coap_session_t *coap_session);
/**
* Create a new TLS client-side session.
*
* @param coap_session The CoAP session.
* @param connected Updated with whether the connection is connected yet or not.
* @c 0 is not connected, @c 1 is connected.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_tls_new_client_session(coap_session_t *coap_session, int *connected);
/**
* Create a TLS new server-side session.
*
* @param coap_session The CoAP session.
* @param connected Updated with whether the connection is connected yet or not.
* @c 0 is not connected, @c 1 is connected.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_tls_new_server_session(coap_session_t *coap_session, int *connected);
/**
* Terminates the TLS session (may send an ALERT if necessary) then frees the
* underlying TLS library object containing security parameters for the session.
*
* @param coap_session The CoAP session.
*/
void coap_tls_free_session( coap_session_t *coap_session );
/**
* Send data to a TLS peer, with implicit flush.
*
* @param coap_session The CoAP session.
* @param data Pointer to data.
* @param data_len Number of bytes to send.
*
* @return @c 0 if this should be retried, @c -1 if there is an error
* or the number of cleartext bytes sent.
*/
ssize_t coap_tls_write(coap_session_t *coap_session,
const uint8_t *data,
size_t data_len
);
/**
* Read some data from a TLS peer.
*
* @param coap_session The CoAP session.
* @param data Pointer to data.
* @param data_len Maximum number of bytes to read.
*
* @return @c 0 if this should be retried, @c -1 if there is an error
* or the number of cleartext bytes read.
*/
ssize_t coap_tls_read(coap_session_t *coap_session,
uint8_t *data,
size_t data_len
);
/**
* Initialize the underlying (D)TLS Library layer.
*
*/
void coap_dtls_startup(void);
/**
* Close down the underlying (D)TLS Library layer.
*
*/
void coap_dtls_shutdown(void);
/**
* Get the actual (D)TLS object for the session.
*
* @param session The session.
* @param tls_lib Updated with the library type.
*
* @return The TLS information.
*/
void *coap_dtls_get_tls(const coap_session_t *session,
coap_tls_library_t *tls_lib);
/** @} */
#endif /* COAP_DTLS_INTERNAL_H */

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -12,9 +14,6 @@
#include "libcoap.h"
struct coap_context_t;
struct coap_session_t;
/**
* @defgroup events Event API
* API functions for event delivery from lower-layer library functions.
@ -49,17 +48,20 @@ struct coap_session_t;
#define COAP_EVENT_SESSION_CLOSED 0x2002
#define COAP_EVENT_SESSION_FAILED 0x2003
/**
* BLOCK2 receive errors
*/
#define COAP_EVENT_PARTIAL_BLOCK 0x3001
/**
* Type for event handler functions that can be registered with a CoAP
* context using the unction coap_set_event_handler(). When called by
* the library, the first argument will be the coap_context_t object
* where the handler function has been registered. The second argument
* is the event type that may be complemented by event-specific data
* passed as the third argument.
* the library, the first argument will be the current coap_session_t object
* which is associated with the original CoAP context. The second parameter
* is the event type.
*/
typedef int (*coap_event_handler_t)(struct coap_context_t *,
coap_event_t event,
struct coap_session_t *session);
typedef int (*coap_event_handler_t)(coap_session_t *session,
const coap_event_t event);
/**
* Registers the function @p hnd as callback for events from the given
@ -70,9 +72,11 @@ typedef int (*coap_event_handler_t)(struct coap_context_t *,
* @param hnd The event handler to be registered. @c NULL if to be
* de-registered.
*/
void coap_register_event_handler(struct coap_context_t *context,
void coap_register_event_handler(coap_context_t *context,
coap_event_handler_t hnd);
/** @} */
/**
* Registers the function @p hnd as callback for events from the given
* CoAP context @p context. Any event handler that has previously been
@ -84,7 +88,7 @@ void coap_register_event_handler(struct coap_context_t *context,
* @param hnd The event handler to be registered.
*/
COAP_DEPRECATED
void coap_set_event_handler(struct coap_context_t *context,
void coap_set_event_handler(coap_context_t *context,
coap_event_handler_t hnd);
/**
@ -95,8 +99,6 @@ void coap_set_event_handler(struct coap_context_t *context,
* @param context The CoAP context whose event handler is to be removed.
*/
COAP_DEPRECATED
void coap_clear_event_handler(struct coap_context_t *context);
/** @} */
void coap_clear_event_handler(coap_context_t *context);
#endif /* COAP_EVENT_H */

View File

@ -0,0 +1,107 @@
/*
* coap_forward_decls.h -- Forward declarations of structures that are
* opaque to application programming that use libcoap.
*
* Copyright (C) 2019-2021 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_forward_decls.h
* @brief COAP forward definitions
*/
#ifndef COAP_FORWARD_DECLS_H_
#define COAP_FORWARD_DECLS_H_
/*
* Define the forward declations for the structures (even non-opaque)
* so that applications (using coap.h) as well as libcoap builds
* can reference them (and makes .h file dependencies a lot simpler).
*/
struct coap_address_t;
struct coap_bin_const_t;
struct coap_dtls_pki_t;
struct coap_str_const_t;
struct coap_string_t;
/*
* typedef all the opaque structures that are defined in coap_*_internal.h
*/
/* ************* coap_async_internal.h ***************** */
/**
* Async Entry information.
*/
typedef struct coap_async_t coap_async_t;
/* ************* coap_block_internal.h ***************** */
/*
* Block handling information.
*/
typedef struct coap_lg_xmit_t coap_lg_xmit_t;
typedef struct coap_lg_crcv_t coap_lg_crcv_t;
typedef struct coap_lg_srcv_t coap_lg_srcv_t;
/* ************* coap_cache_internal.h ***************** */
/*
* Cache Entry information.
*/
typedef struct coap_cache_entry_t coap_cache_entry_t;
typedef struct coap_cache_key_t coap_cache_key_t;
/* ************* coap_io_internal.h ***************** */
/**
* coap_socket_t and coap_packet_t information.
*/
typedef struct coap_packet_t coap_packet_t;
typedef struct coap_socket_t coap_socket_t;
/* ************* coap_net_internal.h ***************** */
/*
* Net information.
*/
typedef struct coap_context_t coap_context_t;
typedef struct coap_queue_t coap_queue_t;
/* ************* coap_pdu_internal.h ***************** */
/**
* PDU information.
*/
typedef struct coap_pdu_t coap_pdu_t;
/* ************* coap_resource_internal.h ***************** */
/*
* Resource information.
*/
typedef struct coap_attr_t coap_attr_t;
typedef struct coap_resource_t coap_resource_t;
/* ************* coap_session_internal.h ***************** */
/*
* Session information.
*/
typedef struct coap_addr_hash_t coap_addr_hash_t;
typedef struct coap_endpoint_t coap_endpoint_t;
typedef struct coap_session_t coap_session_t;
/* ************* coap_subscribe_internal.h ***************** */
/*
* Observe subscriber information.
*/
typedef struct coap_subscription_t coap_subscription_t;
#endif /* COAP_FORWARD_DECLS_H_ */

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -31,7 +33,7 @@ typedef unsigned char coap_key_t[4];
* @param len The length of @p s.
* @param h The result buffer to store the calculated hash key.
*/
void coap_hash_impl(const unsigned char *s, unsigned int len, coap_key_t h);
void coap_hash_impl(const unsigned char *s, size_t len, coap_key_t h);
#define coap_hash(String,Length,Result) \
coap_hash_impl((String),(Length),(Result))

View File

@ -0,0 +1,65 @@
/*
* coap_internal.h -- Structures, Enums & Functions that are not exposed to
* application programming
*
* Copyright (C) 2019-2021 Jon Shallow <supjps-libcoap@jpshallow.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/*
* All libcoap library files should include this file which then pulls in all
* of the other appropriate header files.
*
* Note: This file should never be included in application code (with the
* possible exception of internal test suites).
*/
/**
* @file coap_internal.h
* @brief Pulls together all the internal only header files
*/
#ifndef COAP_INTERNAL_H_
#define COAP_INTERNAL_H_
#include "coap_config.h"
/*
* Correctly set up assert() based on NDEBUG for libcoap
*/
#if defined(HAVE_ASSERT_H) && !defined(assert)
# include <assert.h>
#endif
#include "coap3/coap.h"
/*
* Include all the header files that are for internal use only.
*/
/* Not defined in coap.h - internal usage .h files */
#include "utlist.h"
#include "uthash.h"
#include "coap_hashkey.h"
#include "coap_mutex.h"
/* Specifically defined internal .h files */
#include "coap_asn1_internal.h"
#include "coap_async_internal.h"
#include "coap_block_internal.h"
#include "coap_cache_internal.h"
#include "coap_dtls_internal.h"
#include "coap_io_internal.h"
#include "coap_net_internal.h"
#include "coap_pdu_internal.h"
#include "coap_session_internal.h"
#include "coap_resource_internal.h"
#include "coap_session_internal.h"
#include "coap_subscribe_internal.h"
#include "coap_tcp_internal.h"
#endif /* COAP_INTERNAL_H_ */

View File

@ -0,0 +1,72 @@
/*
* coap_io.h -- Default network I/O functions for libcoap
*
* Copyright (C) 2012-2013 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_IO_H_
#define COAP_IO_H_
#include <sys/types.h>
#include "address.h"
#ifdef RIOT_VERSION
#include "net/gnrc.h"
#endif /* RIOT_VERSION */
#ifndef COAP_RXBUFFER_SIZE
#define COAP_RXBUFFER_SIZE 1472
#endif /* COAP_RXBUFFER_SIZE */
/*
* It may may make sense to define this larger on busy systems
* (lots of sessions, large number of which are active), by using
* -DCOAP_MAX_EPOLL_EVENTS=nn at compile time.
*/
#ifndef COAP_MAX_EPOLL_EVENTS
#define COAP_MAX_EPOLL_EVENTS 10
#endif /* COAP_MAX_EPOLL_EVENTS */
#ifdef _WIN32
typedef SOCKET coap_fd_t;
#define coap_closesocket closesocket
#define COAP_SOCKET_ERROR SOCKET_ERROR
#define COAP_INVALID_SOCKET INVALID_SOCKET
#else
typedef int coap_fd_t;
#define coap_closesocket close
#define COAP_SOCKET_ERROR (-1)
#define COAP_INVALID_SOCKET (-1)
#endif
typedef uint16_t coap_socket_flags_t;
typedef struct coap_addr_tuple_t {
coap_address_t remote; /**< remote address and port */
coap_address_t local; /**< local address and port */
} coap_addr_tuple_t;
const char *coap_socket_strerror( void );
/**
* Check whether TCP is available.
*
* @return @c 1 if support for TCP is enabled, or @c 0 otherwise.
*/
int coap_tcp_is_supported(void);
typedef enum {
COAP_NACK_TOO_MANY_RETRIES,
COAP_NACK_NOT_DELIVERABLE,
COAP_NACK_RST,
COAP_NACK_TLS_FAILED,
COAP_NACK_ICMP_ISSUE
} coap_nack_reason_t;
#endif /* COAP_IO_H_ */

View File

@ -3,41 +3,24 @@
*
* Copyright (C) 2012-2013 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_IO_H_
#define COAP_IO_H_
#ifndef COAP_IO_INTERNAL_H_
#define COAP_IO_INTERNAL_H_
#include <assert.h>
#include <sys/types.h>
#include "address.h"
#ifndef COAP_RXBUFFER_SIZE
#define COAP_RXBUFFER_SIZE 1472
#endif /* COAP_RXBUFFER_SIZE */
#ifdef RIOT_VERSION
#include "net/gnrc.h"
#endif /* RIOT_VERSION */
#ifdef _WIN32
typedef SOCKET coap_fd_t;
#define coap_closesocket closesocket
#define COAP_SOCKET_ERROR SOCKET_ERROR
#define COAP_INVALID_SOCKET INVALID_SOCKET
#else
typedef int coap_fd_t;
#define coap_closesocket close
#define COAP_SOCKET_ERROR (-1)
#define COAP_INVALID_SOCKET (-1)
#endif
struct coap_packet_t;
struct coap_session_t;
struct coap_pdu_t;
typedef uint16_t coap_socket_flags_t;
typedef struct coap_socket_t {
struct coap_socket_t {
#if defined(WITH_LWIP)
struct udp_pcb *pcb;
#elif defined(WITH_CONTIKI)
@ -45,8 +28,14 @@ typedef struct coap_socket_t {
#else
coap_fd_t fd;
#endif /* WITH_LWIP */
#if defined(RIOT_VERSION)
gnrc_pktsnip_t *pkt; /* pointer to received packet for processing */
#endif /* RIOT_VERSION */
coap_socket_flags_t flags;
} coap_socket_t;
coap_session_t *session; /* Used by the epoll logic for an active session. */
coap_endpoint_t *endpoint; /* Used by the epoll logic for a listening
endpoint. */
};
/**
* coap_socket_flags_t values
@ -65,8 +54,10 @@ typedef struct coap_socket_t {
#define COAP_SOCKET_CAN_CONNECT 0x0800 /**< non blocking client socket can now connect without blocking */
#define COAP_SOCKET_MULTICAST 0x1000 /**< socket is used for multicast communication */
struct coap_endpoint_t *coap_malloc_endpoint( void );
void coap_mfree_endpoint( struct coap_endpoint_t *ep );
coap_endpoint_t *coap_malloc_endpoint( void );
void coap_mfree_endpoint( coap_endpoint_t *ep );
const char *coap_socket_format_errno(int error);
int
coap_socket_connect_udp(coap_socket_t *sock,
@ -81,34 +72,10 @@ coap_socket_bind_udp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr );
int
coap_socket_connect_tcp1(coap_socket_t *sock,
const coap_address_t *local_if,
const coap_address_t *server,
int default_port,
coap_address_t *local_addr,
coap_address_t *remote_addr);
int
coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *local_addr,
coap_address_t *remote_addr);
int
coap_socket_bind_tcp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr);
int
coap_socket_accept_tcp(coap_socket_t *server,
coap_socket_t *new_client,
coap_address_t *local_addr,
coap_address_t *remote_addr);
void coap_socket_close(coap_socket_t *sock);
ssize_t
coap_socket_send( coap_socket_t *sock, struct coap_session_t *session,
coap_socket_send( coap_socket_t *sock, coap_session_t *session,
const uint8_t *data, size_t data_len );
ssize_t
@ -117,14 +84,15 @@ coap_socket_write(coap_socket_t *sock, const uint8_t *data, size_t data_len);
ssize_t
coap_socket_read(coap_socket_t *sock, uint8_t *data, size_t data_len);
void
coap_epoll_ctl_mod(coap_socket_t *sock, uint32_t events, const char *func);
#ifdef WITH_LWIP
ssize_t
coap_socket_send_pdu( coap_socket_t *sock, struct coap_session_t *session,
struct coap_pdu_t *pdu );
coap_socket_send_pdu( coap_socket_t *sock, coap_session_t *session,
coap_pdu_t *pdu );
#endif
const char *coap_socket_strerror( void );
/**
* Function interface for data transmission. This function returns the number of
* bytes that have been transmitted, or a value less than zero on error.
@ -137,7 +105,7 @@ const char *coap_socket_strerror( void );
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *session, const uint8_t *data, size_t datalen );
ssize_t coap_network_send( coap_socket_t *sock, const coap_session_t *session, const uint8_t *data, size_t datalen );
/**
* Function interface for reading data. This function returns the number of
@ -150,7 +118,7 @@ ssize_t coap_network_send( coap_socket_t *sock, const struct coap_session_t *ses
* @return The number of bytes received on success, or a value less than
* zero on error.
*/
ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
ssize_t coap_network_read( coap_socket_t *sock, coap_packet_t *packet );
#ifndef coap_mcast_interface
# define coap_mcast_interface(Local) 0
@ -160,7 +128,7 @@ ssize_t coap_network_read( coap_socket_t *sock, struct coap_packet_t *packet );
* Given a packet, set msg and msg_len to an address and length of the packet's
* data in memory.
* */
void coap_packet_get_memmapped(struct coap_packet_t *packet,
void coap_packet_get_memmapped(coap_packet_t *packet,
unsigned char **address,
size_t *length);
@ -169,7 +137,7 @@ void coap_packet_get_memmapped(struct coap_packet_t *packet,
* Get the pbuf of a packet. The caller takes over responsibility for freeing
* the pbuf.
*/
struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
struct pbuf *coap_packet_extract_pbuf(coap_packet_t *packet);
#endif
#if defined(WITH_LWIP)
@ -183,28 +151,18 @@ struct pbuf *coap_packet_extract_pbuf(struct coap_packet_t *packet);
*/
struct coap_packet_t {
struct pbuf *pbuf;
const struct coap_endpoint_t *local_interface;
coap_address_t src; /**< the packet's source address */
coap_address_t dst; /**< the packet's destination address */
const coap_endpoint_t *local_interface;
coap_addr_tuple_t addr_info; /**< local and remote addresses */
int ifindex; /**< the interface index */
// uint16_t srcport;
};
#else
struct coap_packet_t {
coap_address_t src; /**< the packet's source address */
coap_address_t dst; /**< the packet's destination address */
coap_addr_tuple_t addr_info; /**< local and remote addresses */
int ifindex; /**< the interface index */
size_t length; /**< length of payload */
unsigned char payload[COAP_RXBUFFER_SIZE]; /**< payload */
};
#endif
typedef struct coap_packet_t coap_packet_t;
typedef enum {
COAP_NACK_TOO_MANY_RETRIES,
COAP_NACK_NOT_DELIVERABLE,
COAP_NACK_RST,
COAP_NACK_TLS_FAILED
} coap_nack_reason_t;
#endif /* COAP_IO_H_ */
#endif /* COAP_IO_INTERNAL_H_ */

View File

@ -2,6 +2,9 @@
* coap_mutex.h -- mutex utilities
*
* Copyright (C) 2019 Jon Shallow <supjps-libcoap@jpshallow.com>
* 2019 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
@ -15,28 +18,14 @@
#ifndef COAP_MUTEX_H_
#define COAP_MUTEX_H_
#if defined(RIOT_VERSION)
#include <mutex.h>
typedef mutex_t coap_mutex_t;
#define COAP_MUTEX_INITIALIZER MUTEX_INIT
#define coap_mutex_lock(a) mutex_lock(a)
#define coap_mutex_trylock(a) mutex_trylock(a)
#define coap_mutex_unlock(a) mutex_unlock(a)
#elif defined(WITH_CONTIKI)
/* CONTIKI does not support mutex */
typedef int coap_mutex_t;
#define COAP_MUTEX_INITIALIZER 0
#define coap_mutex_lock(a) *(a) = 1
#define coap_mutex_trylock(a) *(a) = 1
#define coap_mutex_unlock(a) *(a) = 0
#else /* ! RIOT_VERSION && ! WITH_CONTIKI */
/*
* Mutexes are currently only used if there is a constrained stack,
* and large static variables (instead of the large variable being on
* the stack) need to be protected.
*/
#if COAP_CONSTRAINED_STACK
#if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
#include <pthread.h>
typedef pthread_mutex_t coap_mutex_t;
@ -45,6 +34,26 @@ typedef pthread_mutex_t coap_mutex_t;
#define coap_mutex_trylock(a) pthread_mutex_trylock(a)
#define coap_mutex_unlock(a) pthread_mutex_unlock(a)
#endif /* ! RIOT_VERSION && ! WITH_CONTIKI */
#elif defined(RIOT_VERSION)
/* use RIOT's mutex API */
#include <mutex.h>
typedef mutex_t coap_mutex_t;
#define COAP_MUTEX_INITIALIZER MUTEX_INIT
#define coap_mutex_lock(a) mutex_lock(a)
#define coap_mutex_trylock(a) mutex_trylock(a)
#define coap_mutex_unlock(a) mutex_unlock(a)
#else
/* define stub mutex functions */
typedef int coap_mutex_t;
#define COAP_MUTEX_INITIALIZER 0
#define coap_mutex_lock(a) *(a) = 1
#define coap_mutex_trylock(a) *(a) = 1
#define coap_mutex_unlock(a) *(a) = 0
#endif /* !RIOT_VERSION && !HAVE_PTHREAD_H && !HAVE_PTHREAD_MUTEX_LOCK */
#endif /* COAP_CONSTRAINED_STACK */
#endif /* COAP_MUTEX_H_ */

View File

@ -0,0 +1,366 @@
/*
* coap_context_internal.h -- Structures, Enums & Functions that are not
* exposed to application programming
*
* Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_net_internal.h
* @brief COAP net internal information
*/
#ifndef COAP_NET_INTERNAL_H_
#define COAP_NET_INTERNAL_H_
/**
* @defgroup context_internal Context Handling (Internal)
* CoAP Context Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
/**
* Queue entry
*/
struct coap_queue_t {
struct coap_queue_t *next;
coap_tick_t t; /**< when to send PDU for the next time */
unsigned char retransmit_cnt; /**< retransmission counter, will be removed
* when zero */
unsigned int timeout; /**< the randomized timeout value */
coap_session_t *session; /**< the CoAP session */
coap_mid_t id; /**< CoAP message id */
coap_pdu_t *pdu; /**< the CoAP PDU to send */
};
/**
* The CoAP stack's global state is stored in a coap_context_t object.
*/
struct coap_context_t {
coap_opt_filter_t known_options;
coap_resource_t *resources; /**< hash table or list of known
resources */
coap_resource_t *unknown_resource; /**< can be used for handling
unknown resources */
coap_resource_t *proxy_uri_resource; /**< can be used for handling
proxy URI resources */
coap_resource_release_userdata_handler_t release_userdata;
/**< function to release user_data
when resource is deleted */
#ifndef WITHOUT_ASYNC
/**
* list of asynchronous message ids */
coap_async_t *async_state;
#endif /* WITHOUT_ASYNC */
/**
* The time stamp in the first element of the sendqeue is relative
* to sendqueue_basetime. */
coap_tick_t sendqueue_basetime;
coap_queue_t *sendqueue;
coap_endpoint_t *endpoint; /**< the endpoints used for listening */
coap_session_t *sessions; /**< client sessions */
#ifdef WITH_CONTIKI
struct uip_udp_conn *conn; /**< uIP connection object */
struct etimer retransmit_timer; /**< fires when the next packet must be
sent */
struct etimer notify_timer; /**< used to check resources periodically */
#endif /* WITH_CONTIKI */
#ifdef WITH_LWIP
uint8_t timer_configured; /**< Set to 1 when a retransmission is
* scheduled using lwIP timers for this
* context, otherwise 0. */
#endif /* WITH_LWIP */
coap_response_handler_t response_handler;
coap_nack_handler_t nack_handler;
coap_ping_handler_t ping_handler;
coap_pong_handler_t pong_handler;
/**
* Callback function that is used to signal events to the
* application. This field is set by coap_set_event_handler().
*/
coap_event_handler_t handle_event;
ssize_t (*network_send)(coap_socket_t *sock, const coap_session_t *session,
const uint8_t *data, size_t datalen);
ssize_t (*network_read)(coap_socket_t *sock, coap_packet_t *packet);
size_t(*get_client_psk)(const coap_session_t *session, const uint8_t *hint,
size_t hint_len, uint8_t *identity,
size_t *identity_len, size_t max_identity_len,
uint8_t *psk, size_t max_psk_len);
size_t(*get_server_psk)(const coap_session_t *session,
const uint8_t *identity, size_t identity_len,
uint8_t *psk, size_t max_psk_len);
size_t(*get_server_hint)(const coap_session_t *session, uint8_t *hint,
size_t max_hint_len);
void *dtls_context;
coap_dtls_spsk_t spsk_setup_data; /**< Contains the initial PSK server setup
data */
unsigned int session_timeout; /**< Number of seconds of inactivity after
which an unused session will be closed.
0 means use default. */
unsigned int max_idle_sessions; /**< Maximum number of simultaneous unused
sessions per endpoint. 0 means no
maximum. */
unsigned int max_handshake_sessions; /**< Maximum number of simultaneous
negotating sessions per endpoint. 0
means use default. */
unsigned int ping_timeout; /**< Minimum inactivity time before
sending a ping message. 0 means
disabled. */
unsigned int csm_timeout; /**< Timeout for waiting for a CSM from
the remote side. 0 means disabled. */
uint8_t observe_pending; /**< Observe response pending */
uint8_t block_mode; /**< Zero or more COAP_BLOCK_ or'd options */
uint64_t etag; /**< Next ETag to use */
coap_cache_entry_t *cache; /**< CoAP cache-entry cache */
uint16_t *cache_ignore_options; /**< CoAP options to ignore when creating a
cache-key */
size_t cache_ignore_count; /**< The number of CoAP options to ignore
when creating a cache-key */
void *app; /**< application-specific data */
#ifdef COAP_EPOLL_SUPPORT
int epfd; /**< External FD for epoll */
int eptimerfd; /**< Internal FD for timeout */
coap_tick_t next_timeout; /**< When the next timeout is to occur */
#endif /* COAP_EPOLL_SUPPORT */
};
/**
* Adds @p node to given @p queue, ordered by variable t in @p node.
*
* @param queue Queue to add to.
* @param node Node entry to add to Queue.
*
* @return @c 1 added to queue, @c 0 failure.
*/
int coap_insert_node(coap_queue_t **queue, coap_queue_t *node);
/**
* Destroys specified @p node.
*
* @param node Node entry to remove.
*
* @return @c 1 node deleted from queue, @c 0 failure.
*/
int coap_delete_node(coap_queue_t *node);
/**
* Removes all items from given @p queue and frees the allocated storage.
*
* Internal function.
*
* @param queue The queue to delete.
*/
void coap_delete_all(coap_queue_t *queue);
/**
* Creates a new node suitable for adding to the CoAP sendqueue.
*
* @return New node entry, or @c NULL if failure.
*/
coap_queue_t *coap_new_node(void);
/**
* Set sendqueue_basetime in the given context object @p ctx to @p now. This
* function returns the number of elements in the queue head that have timed
* out.
*/
unsigned int coap_adjust_basetime(coap_context_t *ctx, coap_tick_t now);
/**
* Returns the next pdu to send without removing from sendqeue.
*/
coap_queue_t *coap_peek_next( coap_context_t *context );
/**
* Returns the next pdu to send and removes it from the sendqeue.
*/
coap_queue_t *coap_pop_next( coap_context_t *context );
/**
* Handles retransmissions of confirmable messages
*
* @param context The CoAP context.
* @param node The node to retransmit.
*
* @return The message id of the sent message or @c
* COAP_INVALID_MID on error.
*/
coap_mid_t coap_retransmit(coap_context_t *context, coap_queue_t *node);
/**
* Parses and interprets a CoAP datagram with context @p ctx. This function
* returns @c 0 if the datagram was handled, or a value less than zero on
* error.
*
* @param ctx The current CoAP context.
* @param session The current CoAP session.
* @param data The received packet'd data.
* @param data_len The received packet'd data length.
*
* @return @c 0 if message was handled successfully, or less than zero on
* error.
*/
int coap_handle_dgram(coap_context_t *ctx, coap_session_t *session, uint8_t *data, size_t data_len);
/**
* This function removes the element with given @p id from the list given list.
* If @p id was found, @p node is updated to point to the removed element. Note
* that the storage allocated by @p node is @b not released. The caller must do
* this manually using coap_delete_node(). This function returns @c 1 if the
* element with id @p id was found, @c 0 otherwise. For a return value of @c 0,
* the contents of @p node is undefined.
*
* @param queue The queue to search for @p id.
* @param session The session to look for.
* @param id The message id to look for.
* @param node If found, @p node is updated to point to the removed node. You
* must release the storage pointed to by @p node manually.
*
* @return @c 1 if @p id was found, @c 0 otherwise.
*/
int coap_remove_from_queue(coap_queue_t **queue,
coap_session_t *session,
coap_mid_t id,
coap_queue_t **node);
coap_mid_t
coap_wait_ack( coap_context_t *context, coap_session_t *session,
coap_queue_t *node);
/**
* Cancels all outstanding messages for session @p session that have the specified
* token.
*
* @param context The context in use.
* @param session Session of the messages to remove.
* @param token Message token.
* @param token_length Actual length of @p token.
*/
void coap_cancel_all_messages(coap_context_t *context,
coap_session_t *session,
const uint8_t *token,
size_t token_length);
/**
* Cancels all outstanding messages for session @p session.
*
* @param context The context in use.
* @param session Session of the messages to remove.
* @param reason The reasion for the session cancellation
*/
void
coap_cancel_session_messages(coap_context_t *context,
coap_session_t *session,
coap_nack_reason_t reason);
/**
* Dispatches the PDUs from the receive queue in given context.
*/
void coap_dispatch(coap_context_t *context, coap_session_t *session,
coap_pdu_t *pdu);
/**
* Verifies that @p pdu contains no unknown critical options. Options must be
* registered at @p ctx, using the function coap_register_option(). A basic set
* of options is registered automatically by coap_new_context(). This function
* returns @c 1 if @p pdu is ok, @c 0 otherwise. The given filter object @p
* unknown will be updated with the unknown options. As only @c COAP_MAX_OPT
* options can be signalled this way, remaining options must be examined
* manually.
*
* @code
coap_opt_filter_t f = COAP_OPT_NONE;
coap_opt_iterator_t opt_iter;
if (coap_option_check_critical(ctx, pdu, f) == 0) {
coap_option_iterator_init(pdu, &opt_iter, f);
while (coap_option_next(&opt_iter)) {
if (opt_iter.type & 0x01) {
... handle unknown critical option in opt_iter ...
}
}
}
@endcode
*
* @param ctx The context where all known options are registered.
* @param pdu The PDU to check.
* @param unknown The output filter that will be updated to indicate the
* unknown critical options found in @p pdu.
*
* @return @c 1 if everything was ok, @c 0 otherwise.
*/
int coap_option_check_critical(coap_context_t *ctx,
coap_pdu_t *pdu,
coap_opt_filter_t *unknown);
/**
* Creates a new response for given @p request with the contents of @c
* .well-known/core. The result is NULL on error or a newly allocated PDU that
* must be either sent with coap_sent() or released by coap_delete_pdu().
*
* @param context The current coap context to use.
* @param session The CoAP session.
* @param request The request for @c .well-known/core .
*
* @return A new 2.05 response for @c .well-known/core or NULL on error.
*/
coap_pdu_t *coap_wellknown_response(coap_context_t *context,
coap_session_t *session,
coap_pdu_t *request);
/**
* Calculates the initial timeout based on the session CoAP transmission
* parameters 'ack_timeout', 'ack_random_factor', and COAP_TICKS_PER_SECOND.
* The calculation requires 'ack_timeout' and 'ack_random_factor' to be in
* Qx.FRAC_BITS fixed point notation, whereas the passed parameter @p r
* is interpreted as the fractional part of a Q0.MAX_BITS random value.
*
* @param session session timeout is associated with
* @param r random value as fractional part of a Q0.MAX_BITS fixed point
* value
* @return COAP_TICKS_PER_SECOND * 'ack_timeout' *
* (1 + ('ack_random_factor' - 1) * r)
*/
unsigned int coap_calc_timeout(coap_session_t *session, unsigned char r);
/**
* Sends a CoAP message to given peer. The memory that is
* allocated for the pdu will be released by coap_send_internal().
* The caller must not use the pdu after calling coap_send_internal().
*
* If the response body is split into multiple payloads using blocks, libcoap
* will handle asking for the subsequent blocks and any necessary recovery
* needed.
*
* @param session The CoAP session.
* @param pdu The CoAP PDU to send.
*
* @return The message id of the sent message or @c
* COAP_INVALID_MID on error.
*/
coap_mid_t coap_send_internal(coap_session_t *session, coap_pdu_t *pdu);
/** @} */
#endif /* COAP_NET_INTERNAL_H_ */

View File

@ -0,0 +1,300 @@
/*
* coap_pdu_internal.h -- CoAP PDU structure
*
* Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_pdu_internal.h
* @brief CoAP PDU internal information
*/
#ifndef COAP_COAP_PDU_INTERNAL_H_
#define COAP_COAP_PDU_INTERNAL_H_
#ifdef WITH_LWIP
#include <lwip/pbuf.h>
#endif
#include <stdint.h>
/**
* @defgroup pdu_internal PDU (Internal)
* CoAP PDU Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
#define COAP_DEFAULT_VERSION 1 /* version of CoAP supported */
/* TCP Message format constants, do not modify */
#define COAP_MESSAGE_SIZE_OFFSET_TCP8 13
#define COAP_MESSAGE_SIZE_OFFSET_TCP16 269 /* 13 + 256 */
#define COAP_MESSAGE_SIZE_OFFSET_TCP32 65805 /* 269 + 65536 */
/* Derived message size limits */
#define COAP_MAX_MESSAGE_SIZE_TCP0 (COAP_MESSAGE_SIZE_OFFSET_TCP8-1) /* 12 */
#define COAP_MAX_MESSAGE_SIZE_TCP8 (COAP_MESSAGE_SIZE_OFFSET_TCP16-1) /* 268 */
#define COAP_MAX_MESSAGE_SIZE_TCP16 (COAP_MESSAGE_SIZE_OFFSET_TCP32-1) /* 65804 */
#define COAP_MAX_MESSAGE_SIZE_TCP32 (COAP_MESSAGE_SIZE_OFFSET_TCP32+0xFFFFFFFF)
#ifndef COAP_DEBUG_BUF_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEBUG_BUF_SIZE 128
#else /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
/* 1024 derived from RFC7252 4.6. Message Size max payload */
#define COAP_DEBUG_BUF_SIZE (8 + 1024 * 2)
#endif /* defined(WITH_CONTIKI) || defined(WITH_LWIP) */
#endif /* COAP_DEBUG_BUF_SIZE */
#ifndef COAP_DEFAULT_MAX_PDU_RX_SIZE
#if defined(WITH_CONTIKI) || defined(WITH_LWIP)
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (COAP_MAX_MESSAGE_SIZE_TCP16+4UL)
#else
/* 8 MiB max-message-size plus some space for options */
#define COAP_DEFAULT_MAX_PDU_RX_SIZE (8UL*1024*1024+256)
#endif
#endif /* COAP_DEFAULT_MAX_PDU_RX_SIZE */
/**
* Indicates that a response is suppressed. This will occur for error
* responses if the request was received via IP multicast.
*/
#define COAP_DROPPED_RESPONSE -2
#define COAP_PDU_DELAYED -3
#define COAP_PAYLOAD_START 0xFF /* payload marker */
#define COAP_PDU_IS_EMPTY(pdu) ((pdu)->code == 0)
#define COAP_PDU_IS_REQUEST(pdu) (!COAP_PDU_IS_EMPTY(pdu) && (pdu)->code < 32)
#define COAP_PDU_IS_RESPONSE(pdu) ((pdu)->code >= 64 && (pdu)->code < 224)
#define COAP_PDU_IS_SIGNALING(pdu) ((pdu)->code >= 224)
#define COAP_PDU_MAX_UDP_HEADER_SIZE 4
#define COAP_PDU_MAX_TCP_HEADER_SIZE 6
/**
* structure for CoAP PDUs
* token, if any, follows the fixed size header, then options until
* payload marker (0xff), then the payload if stored inline.
* Memory layout is:
* <---header--->|<---token---><---options--->0xff<---payload--->
* header is addressed with a negative offset to token, its maximum size is
* max_hdr_size.
* options starts at token + token_length
* payload starts at data, its length is used_size - (data - token)
*/
struct coap_pdu_t {
coap_pdu_type_t type; /**< message type */
coap_pdu_code_t code; /**< request method (value 1--31) or response code
(value 64-255) */
coap_mid_t mid; /**< message id, if any, in regular host byte
order */
uint8_t max_hdr_size; /**< space reserved for protocol-specific header */
uint8_t hdr_size; /**< actual size used for protocol-specific
header */
uint8_t token_length; /**< length of Token */
uint16_t max_opt; /**< highest option number in PDU */
size_t alloc_size; /**< allocated storage for token, options and
payload */
size_t used_size; /**< used bytes of storage for token, options and
payload */
size_t max_size; /**< maximum size for token, options and payload,
or zero for variable size pdu */
uint8_t *token; /**< first byte of token, if any, or options */
uint8_t *data; /**< first byte of payload, if any */
#ifdef WITH_LWIP
struct pbuf *pbuf; /**< lwIP PBUF. The package data will always reside
* inside the pbuf's payload, but this pointer
* has to be kept because no exact offset can be
* given. This field must not be accessed from
* outside, because the pbuf's reference count
* is checked to be 1 when the pbuf is assigned
* to the pdu, and the pbuf stays exclusive to
* this pdu. */
#endif
const uint8_t *body_data; /**< Holds ptr to re-assembled data or NULL */
size_t body_length; /**< Holds body data length */
size_t body_offset; /**< Holds body data offset */
size_t body_total; /**< Holds body data total size */
coap_lg_xmit_t *lg_xmit; /**< Holds ptr to lg_xmit if sending a set of
blocks */
};
/**
* Dynamically grows the size of @p pdu to @p new_size. The new size
* must not exceed the PDU's configure maximum size. On success, this
* function returns 1, otherwise 0.
*
* @param pdu The PDU to resize.
* @param new_size The new size in bytes.
* @return 1 if the operation succeeded, 0 otherwise.
*/
int coap_pdu_resize(coap_pdu_t *pdu, size_t new_size);
/**
* Dynamically grows the size of @p pdu to @p new_size if needed. The new size
* must not exceed the PDU's configured maximum size. On success, this
* function returns 1, otherwise 0.
*
* @param pdu The PDU to resize.
* @param new_size The new size in bytes.
* @return 1 if the operation succeeded, 0 otherwise.
*/
int coap_pdu_check_resize(coap_pdu_t *pdu, size_t new_size);
/**
* Interprets @p data to determine the number of bytes in the header.
* This function returns @c 0 on error or a number greater than zero on success.
*
* @param proto Session's protocol
* @param data The first byte of raw data to parse as CoAP PDU.
*
* @return A value greater than zero on success or @c 0 on error.
*/
size_t coap_pdu_parse_header_size(coap_proto_t proto,
const uint8_t *data);
/**
* Parses @p data to extract the message size.
* @p length must be at least coap_pdu_parse_header_size(proto, data).
* This function returns @c 0 on error or a number greater than zero on success.
*
* @param proto Session's protocol
* @param data The raw data to parse as CoAP PDU.
* @param length The actual size of @p data.
*
* @return A value greater than zero on success or @c 0 on error.
*/
size_t coap_pdu_parse_size(coap_proto_t proto,
const uint8_t *data,
size_t length);
/**
* Decode the protocol specific header for the specified PDU.
* @param pdu A newly received PDU.
* @param proto The target wire protocol.
* @return 1 for success or 0 on error.
*/
int coap_pdu_parse_header(coap_pdu_t *pdu, coap_proto_t proto);
/**
* Verify consistency in the given CoAP PDU structure and locate the data.
* This function returns @c 0 on error or a number greater than zero on
* success.
* This function only parses the token and options, up to the payload start
* marker.
*
* @param pdu The PDU structure to check.
*
* @return 1 on success or @c 0 on error.
*/
int coap_pdu_parse_opt(coap_pdu_t *pdu);
/**
* Parses @p data into the CoAP PDU structure given in @p result.
* The target pdu must be large enough to
* This function returns @c 0 on error or a number greater than zero on success.
*
* @param proto Session's protocol
* @param data The raw data to parse as CoAP PDU.
* @param length The actual size of @p data.
* @param pdu The PDU structure to fill. Note that the structure must
* provide space to hold at least the token and options
* part of the message.
*
* @return 1 on success or @c 0 on error.
*/
int coap_pdu_parse(coap_proto_t proto,
const uint8_t *data,
size_t length,
coap_pdu_t *pdu);
/**
* Clears any contents from @p pdu and resets @c used_size,
* and @c data pointers. @c max_size is set to @p size, any
* other field is set to @c 0. Note that @p pdu must be a valid
* pointer to a coap_pdu_t object created e.g. by coap_pdu_init().
*
* @param pdu The PDU to clear.
* @param size The maximum size of the PDU.
*/
void coap_pdu_clear(coap_pdu_t *pdu, size_t size);
/**
* Removes (first) option of given number from the @p pdu.
*
* @param pdu The PDU to remove the option from.
* @param number The number of the CoAP option to remove (first only removed).
*
* @return @c 1 if success else @c 0 if error.
*/
int coap_remove_option(coap_pdu_t *pdu, coap_option_num_t number);
/**
* Inserts option of given number in the @p pdu with the appropriate data.
* The option will be inserted in the appropriate place in the options in
* the pdu.
*
* @param pdu The PDU where the option is to be inserted.
* @param number The number of the new option.
* @param len The length of the new option.
* @param data The data of the new option.
*
* @return The overall length of the option or @c 0 on failure.
*/
size_t coap_insert_option(coap_pdu_t *pdu, coap_option_num_t number,
size_t len, const uint8_t *data);
/**
* Updates existing first option of given number in the @p pdu with the new
* data.
*
* @param pdu The PDU where the option is to be updated.
* @param number The number of the option to update (first only updated).
* @param len The length of the updated option.
* @param data The data of the updated option.
*
* @return The overall length of the updated option or @c 0 on failure.
*/
size_t coap_update_option(coap_pdu_t *pdu,
coap_option_num_t number,
size_t len,
const uint8_t *data);
/**
* Compose the protocol specific header for the specified PDU.
*
* @param pdu A newly composed PDU.
* @param proto The target wire protocol.
*
* @return Number of header bytes prepended before pdu->token or 0 on error.
*/
size_t coap_pdu_encode_header(coap_pdu_t *pdu, coap_proto_t proto);
/**
* Updates token in @p pdu with length @p len and @p data.
* This function returns @c 0 on error or a value greater than zero on success.
*
* @param pdu The PDU where the token is to be updated.
* @param len The length of the new token.
* @param data The token to add.
*
* @return A value greater than zero on success, or @c 0 on error.
*/
int coap_update_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
/** @} */
#endif /* COAP_COAP_PDU_INTERNAL_H_ */

View File

@ -0,0 +1,113 @@
/*
* coap_prng.h -- Pseudo Random Numbers
*
* Copyright (C) 2010-2020 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_prng.h
* @brief Pseudo Random Numbers
*/
#ifndef COAP_PRNG_H_
#define COAP_PRNG_H_
/**
* @defgroup coap_prng Pseudo Random Numbers
* API functions for gerating pseudo random numbers
* @{
*/
#if defined(WITH_CONTIKI)
#include <string.h>
/**
* Fills \p buf with \p len random bytes. This is the default implementation for
* coap_prng(). You might want to change contiki_prng_impl() to use a better
* PRNG on your specific platform.
*/
COAP_STATIC_INLINE int
contiki_prng_impl(unsigned char *buf, size_t len) {
uint16_t v = random_rand();
while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
len -= sizeof(v);
buf += sizeof(v);
v = random_rand();
}
memcpy(buf, &v, len);
return 1;
}
#define coap_prng(Buf,Length) contiki_prng_impl((Buf), (Length))
#define coap_prng_init(Value) random_init((uint16_t)(Value))
#elif defined(WITH_LWIP) && defined(LWIP_RAND)
COAP_STATIC_INLINE int
lwip_prng_impl(unsigned char *buf, size_t len) {
u32_t v = LWIP_RAND();
while (len > sizeof(v)) {
memcpy(buf, &v, sizeof(v));
len -= sizeof(v);
buf += sizeof(v);
v = LWIP_RAND();
}
memcpy(buf, &v, len);
return 1;
}
#define coap_prng(Buf,Length) lwip_prng_impl((Buf), (Length))
#define coap_prng_init(Value) (void)Value
#else
/**
* Data type for random number generator function. The function must
* fill @p len bytes of random data into the buffer starting at @p
* out. On success, the function should return 1, zero otherwise.
*/
typedef int (*coap_rand_func_t)(void *out, size_t len);
/**
* Replaces the current random number generation function with the
* default function @p rng.
*
* @param rng The random number generation function to use.
*/
void coap_set_prng(coap_rand_func_t rng);
/**
* Seeds the default random number generation function with the given
* @p seed. The default random number generation function will use
* getrandom() if available, ignoring the seed.
*
* @param seed The seed for the pseudo random number generator.
*/
void coap_prng_init(unsigned int seed);
/**
* Fills @p buf with @p len random bytes using the default pseudo
* random number generator. The default PRNG can be changed with
* coap_set_prng(). This function returns 1 when @p len random bytes
* have been written to @p buf, zero otherwise.
*
* @param buf The buffer to fill with random bytes.
* @param len The number of random bytes to write into @p buf.
*
* @return 1 on success, 0 otherwise.
*/
int coap_prng(void *buf, size_t len);
#endif /* POSIX */
/** @} */
#endif /* COAP_PRNG_H_ */

View File

@ -0,0 +1,141 @@
/*
* coap_resource_internal.h -- generic resource handling
*
* Copyright (C) 2010,2011,2014-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_resource_internal.h
* @brief Generic resource internal handling
*/
#ifndef COAP_RESOURCE_INTERNAL_H_
#define COAP_RESOURCE_INTERNAL_H_
#include "uthash.h"
/**
* @defgroup coap_resource_internal Resources (Internal)
* Structures, Enums and Functions that are not exposed to applications
* @{
*/
/**
* Abstraction of attribute associated with a resource.
*/
struct coap_attr_t {
struct coap_attr_t *next; /**< Pointer to next in chain or NULL */
coap_str_const_t *name; /**< Name of the attribute */
coap_str_const_t *value; /**< Value of the attribute (can be NULL) */
int flags;
};
/**
* Abstraction of resource that can be attached to coap_context_t.
* The key is uri_path.
*/
struct coap_resource_t {
unsigned int dirty:1; /**< set to 1 if resource has changed */
unsigned int partiallydirty:1; /**< set to 1 if some subscribers have not yet
* been notified of the last change */
unsigned int observable:1; /**< can be observed */
unsigned int cacheable:1; /**< can be cached */
unsigned int is_unknown:1; /**< resource created for unknown handler */
unsigned int is_proxy_uri:1; /**< resource created for proxy URI handler */
/**
* Used to store handlers for the seven coap methods @c GET, @c POST, @c PUT,
* @c DELETE, @c FETCH, @c PATCH and @c IPATCH.
* coap_dispatch() will pass incoming requests to handle_request() and then
* to the handler that corresponds to its request method or generate a 4.05
* response if no handler is available.
*/
coap_method_handler_t handler[7];
UT_hash_handle hh;
coap_attr_t *link_attr; /**< attributes to be included with the link format */
coap_subscription_t *subscribers; /**< list of observers for this resource */
/**
* Request URI Path for this resource. This field will point into static
* or allocated memory which must remain there for the duration of the
* resource.
*/
coap_str_const_t *uri_path; /**< the key used for hash lookup for this
resource */
int flags; /**< zero or more COAP_RESOURCE_FLAGS_* or'd together */
/**
* The next value for the Observe option. This field must be increased each
* time the resource changes. Only the lower 24 bits are sent.
*/
unsigned int observe;
/**
* Pointer back to the context that 'owns' this resource.
*/
coap_context_t *context;
/**
* Count of valid names this host is known by (proxy support)
*/
size_t proxy_name_count;
/**
* Array valid names this host is known by (proxy support)
*/
coap_str_const_t ** proxy_name_list;
/**
* This pointer is under user control. It can be used to store context for
* the coap handler.
*/
void *user_data;
};
/**
* Deletes all resources from given @p context and frees their storage.
*
* @param context The CoAP context with the resources to be deleted.
*/
void coap_delete_all_resources(coap_context_t *context);
#define RESOURCES_ADD(r, obj) \
HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj))
#define RESOURCES_DELETE(r, obj) \
HASH_DELETE(hh, (r), (obj))
#define RESOURCES_ITER(r,tmp) \
coap_resource_t *tmp, *rtmp; \
HASH_ITER(hh, (r), tmp, rtmp)
#define RESOURCES_FIND(r, k, res) { \
HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \
}
/**
* Deletes an attribute.
* Note: This is for internal use only, as it is not deleted from its chain.
*
* @param attr Pointer to a previously created attribute.
*
*/
void coap_delete_attr(coap_attr_t *attr);
coap_print_status_t coap_print_wellknown(coap_context_t *,
unsigned char *,
size_t *, size_t,
coap_opt_t *);
/** @} */
#endif /* COAP_RESOURCE_INTERNAL_H_ */

View File

@ -0,0 +1,34 @@
/* coap_riot.h -- RIOT-specific definitions for libcoap
*
* Copyright (C) 2019 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
#ifndef COAP_RIOT_H_
#define COAP_RIOT_H_
#ifndef LIBCOAP_MSG_QUEUE_SIZE
/**
* Size of the queue for passing messages between the network
* interface and the coap stack. */
#define LIBCOAP_MSG_QUEUE_SIZE (32U)
#endif /* LIBCOAP_MSG_QUEUE_SIZE */
#ifndef LIBCOAP_MAX_SOCKETS
/**
* Maximum number of sockets that are simultaneously considered for
* reading or writing. */
#define LIBCOAP_MAX_SOCKETS (16U)
#endif /* LIBCOAP_MAX_SOCKETS */
/**
* This function must be called in the RIOT CoAP thread for
* RIOT-specific initialization.
*/
void coap_riot_startup(void);
#endif /* COAP_RIOT_H_ */

View File

@ -0,0 +1,591 @@
/* coap_session.h -- Session management for libcoap
*
* Copyright (C) 2017 Jean-Claue Michelou <jcm@spinetix.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see
* README for terms of use.
*/
/**
* @file coap_session.h
* @brief Defines the application visible session information
*/
#ifndef COAP_SESSION_H_
#define COAP_SESSION_H_
/**
* @defgroup session Sessions
* API functions for CoAP Sessions
* @{
*/
/**
* Abstraction of a fixed point number that can be used where necessary instead
* of a float. 1,000 fractional bits equals one integer
*/
typedef struct coap_fixed_point_t {
uint16_t integer_part; /**< Integer part of fixed point variable */
uint16_t fractional_part; /**< Fractional part of fixed point variable
1/1000 (3 points) precision */
} coap_fixed_point_t;
#define COAP_PROTO_NOT_RELIABLE(p) ((p)==COAP_PROTO_UDP || (p)==COAP_PROTO_DTLS)
#define COAP_PROTO_RELIABLE(p) ((p)==COAP_PROTO_TCP || (p)==COAP_PROTO_TLS)
/**
* coap_session_type_t values
*/
typedef enum coap_session_type_t {
COAP_SESSION_TYPE_NONE = 0, /**< Not defined */
COAP_SESSION_TYPE_CLIENT, /**< client-side */
COAP_SESSION_TYPE_SERVER, /**< server-side */
COAP_SESSION_TYPE_HELLO, /**< server-side ephemeral session for
responding to a client hello */
} coap_session_type_t;
/**
* coap_session_state_t values
*/
typedef enum coap_session_state_t {
COAP_SESSION_STATE_NONE = 0,
COAP_SESSION_STATE_CONNECTING,
COAP_SESSION_STATE_HANDSHAKE,
COAP_SESSION_STATE_CSM,
COAP_SESSION_STATE_ESTABLISHED,
} coap_session_state_t;
/**
* Increment reference counter on a session.
*
* @param session The CoAP session.
* @return same as session
*/
coap_session_t *coap_session_reference(coap_session_t *session);
/**
* Decrement reference counter on a session.
* Note that the session may be deleted as a result and should not be used
* after this call.
*
* @param session The CoAP session.
*/
void coap_session_release(coap_session_t *session);
/**
* Notify session that it has failed. This cleans up any outstanding / queued
* transmissions, observations etc..
*
* @param session The CoAP session.
* @param reason The reason why the session was disconnected.
*/
void coap_session_disconnected(coap_session_t *session,
coap_nack_reason_t reason);
/**
* Stores @p data with the given session. This function overwrites any value
* that has previously been stored with @p session.
*
* @param session The CoAP session.
* @param data The pointer to the data to store.
*/
void coap_session_set_app_data(coap_session_t *session, void *data);
/**
* Returns any application-specific data that has been stored with @p
* session using the function coap_session_set_app_data(). This function will
* return @c NULL if no data has been stored.
*
* @param session The CoAP session.
*
* @return Pointer to the stored data or @c NULL.
*/
void *coap_session_get_app_data(const coap_session_t *session);
/**
* Get the remote IP address from the session.
*
* @param session The CoAP session.
*
* @return The session's remote address or @c NULL on failure.
*/
const coap_address_t *coap_session_get_addr_remote(
const coap_session_t *session);
/**
* Get the local IP address from the session.
*
* @param session The CoAP session.
*
* @return The session's local address or @c NULL on failure.
*/
const coap_address_t *coap_session_get_addr_local(
const coap_session_t *session);
/**
* Get the session protocol type
*
* @param session The CoAP session.
*
* @return The session's protocol type
*/
coap_proto_t coap_session_get_proto(const coap_session_t *session);
/**
* Get the session type
*
* @param session The CoAP session.
*
* @return The session's type
*/
coap_session_type_t coap_session_get_type(const coap_session_t *session);
/**
* Get the session state
*
* @param session The CoAP session.
*
* @return The session's state
*/
coap_session_state_t coap_session_get_state(const coap_session_t *session);
/**
* Get the session if index
*
* @param session The CoAP session.
*
* @return The session's if index, or @c -1 on error.
*/
int coap_session_get_ifindex(const coap_session_t *session);
/**
* Get the session TLS security ptr (TLS type dependent)
*
* OpenSSL: SSL*
* GnuTLS: gnutls_session_t (implicit *)
* Mbed TLS: mbedtls_ssl_context*
* TinyDTLS: struct dtls_context*
*
* @param session The CoAP session.
* @param tls_lib Updated with the library type.
*
* @return The session TLS ptr or @c NULL if not set up
*/
void *coap_session_get_tls(const coap_session_t *session,
coap_tls_library_t *tls_lib);
/**
* Get the session context
*
* @param session The CoAP session.
*
* @return The session's context
*/
coap_context_t *coap_session_get_context(const coap_session_t *session);
/**
* Set the session type to client. Typically used in a call-home server.
* The session needs to be of type COAP_SESSION_TYPE_SERVER.
* Note: If this function is successful, the session reference count is
* incremented and a subsequent coap_session_release() taking the
* reference count to 0 will cause the session to be freed off.
*
* @param session The CoAP session.
*
* @return @c 1 if updated, @c 0 on failure.
*/
int coap_session_set_type_client(coap_session_t *session);
/**
* Set the session MTU. This is the maximum message size that can be sent,
* excluding IP and UDP overhead.
*
* @param session The CoAP session.
* @param mtu maximum message size
*/
void coap_session_set_mtu(coap_session_t *session, unsigned mtu);
/**
* Get maximum acceptable PDU size
*
* @param session The CoAP session.
* @return maximum PDU size, not including header (but including token).
*/
size_t coap_session_max_pdu_size(const coap_session_t *session);
/**
* Creates a new client session to the designated server.
* @param ctx The CoAP context.
* @param local_if Address of local interface. It is recommended to use NULL to let the operating system choose a suitable local interface. If an address is specified, the port number should be zero, which means that a free port is automatically selected.
* @param server The server's address. If the port number is zero, the default port for the protocol will be used.
* @param proto Protocol.
*
* @return A new CoAP session or NULL if failed. Call coap_session_release to free.
*/
coap_session_t *coap_new_client_session(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto
);
/**
* Creates a new client session to the designated server with PSK credentials
* @param ctx The CoAP context.
* @param local_if Address of local interface. It is recommended to use NULL to let the operating system choose a suitable local interface. If an address is specified, the port number should be zero, which means that a free port is automatically selected.
* @param server The server's address. If the port number is zero, the default port for the protocol will be used.
* @param proto Protocol.
* @param identity PSK client identity
* @param key PSK shared key
* @param key_len PSK shared key length
*
* @return A new CoAP session or NULL if failed. Call coap_session_release to free.
*/
coap_session_t *coap_new_client_session_psk(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
const char *identity,
const uint8_t *key,
unsigned key_len
);
/**
* Creates a new client session to the designated server with PSK credentials
* @param ctx The CoAP context.
* @param local_if Address of local interface. It is recommended to use NULL to
* let the operating system choose a suitable local interface.
* If an address is specified, the port number should be zero,
* which means that a free port is automatically selected.
* @param server The server's address. If the port number is zero, the default
* port for the protocol will be used.
* @param proto CoAP Protocol.
* @param setup_data PSK parameters.
*
* @return A new CoAP session or NULL if failed. Call coap_session_release()
* to free.
*/
coap_session_t *coap_new_client_session_psk2(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_cpsk_t *setup_data
);
/**
* Get the server session's current Identity Hint (PSK).
*
* @param session The current coap_session_t object.
*
* @return @c hint if successful, else @c NULL.
*/
const coap_bin_const_t * coap_session_get_psk_hint(
const coap_session_t *session);
/**
* Get the session's current pre-shared key (PSK).
*
* @param session The current coap_session_t object.
*
* @return @c psk_key if successful, else @c NULL.
*/
const coap_bin_const_t * coap_session_get_psk_key(
const coap_session_t *session);
/**
* Creates a new client session to the designated server with PKI credentials
* @param ctx The CoAP context.
* @param local_if Address of local interface. It is recommended to use NULL to
* let the operating system choose a suitable local interface.
* If an address is specified, the port number should be zero,
* which means that a free port is automatically selected.
* @param server The server's address. If the port number is zero, the default
* port for the protocol will be used.
* @param proto CoAP Protocol.
* @param setup_data PKI parameters.
*
* @return A new CoAP session or NULL if failed. Call coap_session_release()
* to free.
*/
coap_session_t *coap_new_client_session_pki(
coap_context_t *ctx,
const coap_address_t *local_if,
const coap_address_t *server,
coap_proto_t proto,
coap_dtls_pki_t *setup_data
);
/**
* Initializes the token value to use as a starting point.
*
* @param session The current coap_session_t object.
* @param length The length of the token (0 - 8 bytes).
* @param token The token data.
*
*/
void coap_session_init_token(coap_session_t *session, size_t length,
const uint8_t *token);
/**
* Creates a new token for use.
*
* @param session The current coap_session_t object.
* @param length Updated with the length of the new token.
* @param token Updated with the new token data (must be 8 bytes long).
*
*/
void coap_session_new_token(coap_session_t *session, size_t *length,
uint8_t *token);
/**
* @ingroup logging
* Get session description.
*
* @param session The CoAP session.
* @return description string.
*/
const char *coap_session_str(const coap_session_t *session);
/**
* Create a new endpoint for communicating with peers.
*
* @param context The coap context that will own the new endpoint
* @param listen_addr Address the endpoint will listen for incoming requests on or originate outgoing requests from. Use NULL to specify that no incoming request will be accepted and use a random endpoint.
* @param proto Protocol used on this endpoint
*/
coap_endpoint_t *coap_new_endpoint(coap_context_t *context, const coap_address_t *listen_addr, coap_proto_t proto);
/**
* Set the endpoint's default MTU. This is the maximum message size that can be
* sent, excluding IP and UDP overhead.
*
* @param endpoint The CoAP endpoint.
* @param mtu maximum message size
*/
void coap_endpoint_set_default_mtu(coap_endpoint_t *endpoint, unsigned mtu);
void coap_free_endpoint(coap_endpoint_t *ep);
/** @} */
/**
* @ingroup logging
* Get endpoint description.
*
* @param endpoint The CoAP endpoint.
* @return description string.
*/
const char *coap_endpoint_str(const coap_endpoint_t *endpoint);
coap_session_t *coap_session_get_by_peer(const coap_context_t *ctx,
const coap_address_t *remote_addr, int ifindex);
/**
* @defgroup cc Rate Control
* The transmission parameters for CoAP rate control ("Congestion
* Control" in stream-oriented protocols) are defined in
* https://tools.ietf.org/html/rfc7252#section-4.8
* @{
*/
/**
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
* RFC 7252, Section 4.8 Default value of ACK_TIMEOUT is 2
*
* Configurable using coap_session_set_ack_timeout()
*/
#define COAP_DEFAULT_ACK_TIMEOUT ((coap_fixed_point_t){2,0})
/**
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
* RFC 7252, Section 4.8 Default value of ACK_RANDOM_FACTOR is 1.5
*
* Configurable using coap_session_set_ack_random_factor()
*/
#define COAP_DEFAULT_ACK_RANDOM_FACTOR ((coap_fixed_point_t){1,500})
/**
* Number of message retransmissions before message sending is stopped
* RFC 7252, Section 4.8 Default value of MAX_RETRANSMIT is 4
*
* Configurable using coap_session_set_max_retransmit()
*/
#define COAP_DEFAULT_MAX_RETRANSMIT 4
/**
* The number of simultaneous outstanding interactions that a client
* maintains to a given server.
* RFC 7252, Section 4.8 Default value of NSTART is 1
*/
#define COAP_DEFAULT_NSTART 1
/**
* The maximum number of seconds before sending back a response to a
* multicast request.
* RFC 7252, Section 4.8 DEFAULT_LEISURE is 5.
*/
#ifndef COAP_DEFAULT_LEISURE
#define COAP_DEFAULT_LEISURE (5U)
#endif /* COAP_DEFAULT_LEISURE */
/**
* The MAX_TRANSMIT_SPAN definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of MAX_TRAMSMIT_SPAN
* ACK_TIMEOUT * ((2 ** (MAX_RETRANSMIT)) - 1) * ACK_RANDOM_FACTOR
*/
#define COAP_MAX_TRANSMIT_SPAN(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
((1 << (s->max_retransmit)) -1) * \
(s->ack_random_factor.integer_part * 1000 + \
s->ack_random_factor.fractional_part) \
/ 1000000)
/**
* The MAX_TRANSMIT_WAIT definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of MAX_TRAMSMIT_WAIT
* ACK_TIMEOUT * ((2 ** (MAX_RETRANSMIT + 1)) - 1) * ACK_RANDOM_FACTOR
*/
#define COAP_MAX_TRANSMIT_WAIT(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part) * \
((1 << (s->max_retransmit + 1)) -1) * \
(s->ack_random_factor.integer_part * 1000 + \
s->ack_random_factor.fractional_part) \
/ 1000000)
/**
* The MAX_LATENCY definition.
* RFC 7252, Section 4.8.2 MAX_LATENCY is 100.
*/
#define COAP_MAX_LATENCY 100
/**
* The PROCESSING_DELAY definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of PROCESSING_DELAY
* PROCESSING_DELAY set to ACK_TIMEOUT
*/
#define COAP_PROCESSING_DELAY(s) \
((s->ack_timeout.integer_part * 1000 + s->ack_timeout.fractional_part + 500) \
/ 1000)
/**
* The MAX_RTT definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of MAX_RTT
* (2 * MAX_LATENCY) + PROCESSING_DELAY
*/
#define COAP_MAX_RTT(s) \
((2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
/**
* The EXCHANGE_LIFETIME definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of EXCHANGE_LIFETIME
* MAX_TRANSMIT_SPAN + (2 * MAX_LATENCY) + PROCESSING_DELAY
*/
#define COAP_EXCHANGE_LIFETIME(s) \
(COAP_MAX_TRANSMIT_SPAN(s) + (2 * COAP_MAX_LATENCY) + COAP_PROCESSING_DELAY(s))
/**
* The NON_LIFETIME definition for the session (s).
*
* RFC 7252, Section 4.8.2 Calculation of NON_LIFETIME
* MAX_TRANSMIT_SPAN + MAX_LATENCY
*/
#define COAP_NON_LIFETIME(s) \
(COAP_MAX_TRANSMIT_SPAN(s) + COAP_MAX_LATENCY)
/** @} */
/**
* Set the CoAP maximum retransmit count before failure
*
* Number of message retransmissions before message sending is stopped
*
* @param session The CoAP session.
* @param value The value to set to. The default is 4 and should not normally
* get changed.
*/
void coap_session_set_max_retransmit(coap_session_t *session,
unsigned int value);
/**
* Set the CoAP initial ack response timeout before the next re-transmit
*
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
*
* @param session The CoAP session.
* @param value The value to set to. The default is 2 and should not normally
* get changed.
*/
void coap_session_set_ack_timeout(coap_session_t *session,
coap_fixed_point_t value);
/**
* Set the CoAP ack randomize factor
*
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
*
* @param session The CoAP session.
* @param value The value to set to. The default is 1.5 and should not normally
* get changed.
*/
void coap_session_set_ack_random_factor(coap_session_t *session,
coap_fixed_point_t value);
/**
* Get the CoAP maximum retransmit before failure
*
* Number of message retransmissions before message sending is stopped
*
* @param session The CoAP session.
*
* @return Current maximum retransmit value
*/
unsigned int coap_session_get_max_retransmit(const coap_session_t *session);
/**
* Get the CoAP initial ack response timeout before the next re-transmit
*
* Number of seconds when to expect an ACK or a response to an
* outstanding CON message.
*
* @param session The CoAP session.
*
* @return Current ack response timeout value
*/
coap_fixed_point_t coap_session_get_ack_timeout(const coap_session_t *session);
/**
* Get the CoAP ack randomize factor
*
* A factor that is used to randomize the wait time before a message
* is retransmitted to prevent synchronization effects.
*
* @param session The CoAP session.
*
* @return Current ack randomize value
*/
coap_fixed_point_t coap_session_get_ack_random_factor(
const coap_session_t *session);
/**
* Send a ping message for the session.
* @param session The CoAP session.
*
* @return COAP_INVALID_MID if there is an error
*/
coap_mid_t coap_session_send_ping(coap_session_t *session);
#endif /* COAP_SESSION_H */

View File

@ -0,0 +1,295 @@
/*
* coap_session_internal.h -- Structures, Enums & Functions that are not
* exposed to application programming
*
* Copyright (C) 2010-2019 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_session_internal.h
* @brief COAP session internal information
*/
#ifndef COAP_SESSION_INTERNAL_H_
#define COAP_SESSION_INTERNAL_H_
#include "coap_io_internal.h"
#define COAP_DEFAULT_SESSION_TIMEOUT 300
#define COAP_PARTIAL_SESSION_TIMEOUT_TICKS (30 * COAP_TICKS_PER_SECOND)
#define COAP_DEFAULT_MAX_HANDSHAKE_SESSIONS 100
/**
* @defgroup session_internal Sessions (Internal)
* CoAP Session Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
/**
* Only used for servers for hashing incoming packets. Cannot have local IP
* address as this may be an initial multicast and subsequent unicast address
*/
struct coap_addr_hash_t {
coap_address_t remote; /**< remote address and port */
uint16_t lport; /**< local port */
coap_proto_t proto; /**< CoAP protocol */
};
/**
* Abstraction of virtual session that can be attached to coap_context_t
* (client) or coap_endpoint_t (server).
*/
struct coap_session_t {
coap_proto_t proto; /**< protocol used */
coap_session_type_t type; /**< client or server side socket */
coap_session_state_t state; /**< current state of relationaship with
peer */
unsigned ref; /**< reference count from queues */
size_t tls_overhead; /**< overhead of TLS layer */
size_t mtu; /**< path or CSM mtu */
coap_addr_hash_t addr_hash; /**< Address hash for server incoming packets */
UT_hash_handle hh;
coap_addr_tuple_t addr_info; /**< key: remote/local address info */
int ifindex; /**< interface index */
coap_socket_t sock; /**< socket object for the session, if
any */
coap_endpoint_t *endpoint; /**< session's endpoint */
coap_context_t *context; /**< session's context */
void *tls; /**< security parameters */
uint16_t tx_mid; /**< the last message id that was used in
this session */
uint8_t con_active; /**< Active CON request sent */
uint8_t csm_block_supported; /**< CSM TCP blocks supported */
coap_mid_t last_ping_mid; /**< the last keepalive message id that was
used in this session */
coap_queue_t *delayqueue; /**< list of delayed messages waiting to
be sent */
coap_lg_xmit_t *lg_xmit; /**< list of large transmissions */
coap_lg_crcv_t *lg_crcv; /**< Client list of expected large receives */
coap_lg_srcv_t *lg_srcv; /**< Server list of expected large receives */
size_t partial_write; /**< if > 0 indicates number of bytes
already written from the pdu at the
head of sendqueue */
uint8_t read_header[8]; /**< storage space for header of incoming
message header */
size_t partial_read; /**< if > 0 indicates number of bytes
already read for an incoming message */
coap_pdu_t *partial_pdu; /**< incomplete incoming pdu */
coap_tick_t last_rx_tx;
coap_tick_t last_tx_rst;
coap_tick_t last_ping;
coap_tick_t last_pong;
coap_tick_t csm_tx;
coap_dtls_cpsk_t cpsk_setup_data; /**< client provided PSK initial setup
data */
coap_bin_const_t *psk_identity; /**< If client, this field contains the
current identity for server; When this
field is NULL, the current identity is
contained in cpsk_setup_data
If server, this field contains the client
provided identity.
Value maintained internally */
coap_bin_const_t *psk_key; /**< If client, this field contains the
current pre-shared key for server;
When this field is NULL, the current
key is contained in cpsk_setup_data
If server, this field contains the
client's current key.
Value maintained internally */
coap_bin_const_t *psk_hint; /**< If client, this field contains the
server provided identity hint.
If server, this field contains the
current hint for the client; When this
field is NULL, the current hint is
contained in context->spsk_setup_data
Value maintained internally */
void *app; /**< application-specific data */
unsigned int max_retransmit; /**< maximum re-transmit count (default
4) */
coap_fixed_point_t ack_timeout; /**< timeout waiting for ack (default 2
secs) */
coap_fixed_point_t ack_random_factor; /**< ack random factor backoff (default
1.5) */
unsigned int dtls_timeout_count; /**< dtls setup retry counter */
int dtls_event; /**< Tracking any (D)TLS events on this
sesison */
uint8_t block_mode; /**< Zero or more COAP_BLOCK_ or'd options */
uint64_t tx_token; /**< Next token number to use */
};
/**
* Abstraction of virtual endpoint that can be attached to coap_context_t. The
* keys (port, bind_addr) must uniquely identify this endpoint.
*/
struct coap_endpoint_t {
struct coap_endpoint_t *next;
coap_context_t *context; /**< endpoint's context */
coap_proto_t proto; /**< protocol used on this interface */
uint16_t default_mtu; /**< default mtu for this interface */
coap_socket_t sock; /**< socket object for the interface, if
any */
coap_address_t bind_addr; /**< local interface address */
coap_session_t *sessions; /**< hash table or list of active sessions */
};
/**
* Notify session transport has just connected and CSM exchange can now start.
*
* @param session The CoAP session.
*/
void coap_session_send_csm(coap_session_t *session);
/**
* Notify session that it has just connected or reconnected.
*
* @param session The CoAP session.
*/
void coap_session_connected(coap_session_t *session);
/**
* Refresh the session's current Identity Hint (PSK).
* Note: A copy of @p psk_hint is maintained in the session by libcoap.
*
* @param session The current coap_session_t object.
* @param psk_hint If NULL, the Identity Hint will revert to the
* initial Identity Hint used at session setup.
*
* @return @c 1 if successful, else @c 0.
*/
int coap_session_refresh_psk_hint(coap_session_t *session,
const coap_bin_const_t *psk_hint);
/**
* Refresh the session's current pre-shared key (PSK).
* Note: A copy of @p psk_key is maintained in the session by libcoap.
*
* @param session The current coap_session_t object.
* @param psk_key If NULL, the pre-shared key will revert to the
* initial pre-shared key used as session setup.
*
* @return @c 1 if successful, else @c 0.
*/
int coap_session_refresh_psk_key(coap_session_t *session,
const coap_bin_const_t *psk_key);
/**
* Creates a new server session for the specified endpoint.
* @param ctx The CoAP context.
* @param ep An endpoint where an incoming connection request is pending.
*
* @return A new CoAP session or NULL if failed. Call coap_session_release to
* add to unused queue.
*/
coap_session_t *coap_new_server_session(
coap_context_t *ctx,
coap_endpoint_t *ep
);
/**
* Function interface for datagram data transmission. This function returns
* the number of bytes that have been transmitted, or a value less than zero
* on error.
*
* @param session Session to send data on.
* @param data The data to send.
* @param datalen The actual length of @p data.
*
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_session_send(coap_session_t *session,
const uint8_t *data, size_t datalen);
/**
* Function interface for stream data transmission. This function returns
* the number of bytes that have been transmitted, or a value less than zero
* on error. The number of bytes written may be less than datalen because of
* congestion control.
*
* @param session Session to send data on.
* @param data The data to send.
* @param datalen The actual length of @p data.
*
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_session_write(coap_session_t *session,
const uint8_t *data, size_t datalen);
/**
* Send a pdu according to the session's protocol. This function returns
* the number of bytes that have been transmitted, or a value less than zero
* on error.
*
* @param session Session to send pdu on.
* @param pdu The pdu to send.
*
* @return The number of bytes written on success, or a value
* less than zero on error.
*/
ssize_t coap_session_send_pdu(coap_session_t *session, coap_pdu_t *pdu);
ssize_t
coap_session_delay_pdu(coap_session_t *session, coap_pdu_t *pdu,
coap_queue_t *node);
/**
* Lookup the server session for the packet received on an endpoint, or create
* a new one.
*
* @param endpoint Active endpoint the packet was received on.
* @param packet Received packet.
* @param now The current time in ticks.
* @return The CoAP session or @c NULL if error.
*/
coap_session_t *coap_endpoint_get_session(coap_endpoint_t *endpoint,
const coap_packet_t *packet, coap_tick_t now);
/**
* Create a new DTLS session for the @p session.
* Note: the @p session is released if no DTLS server session can be created.
*
* @ingroup dtls_internal
*
* @param session Session to add DTLS session to
* @param now The current time in ticks.
*
* @return CoAP session or @c NULL if error.
*/
coap_session_t *coap_session_new_dtls_session(coap_session_t *session,
coap_tick_t now);
void coap_session_free(coap_session_t *session);
void coap_session_mfree(coap_session_t *session);
/** @} */
#define SESSIONS_ADD(e, obj) \
HASH_ADD(hh, (e), addr_hash, sizeof((obj)->addr_hash), (obj))
#define SESSIONS_DELETE(e, obj) \
HASH_DELETE(hh, (e), (obj))
#define SESSIONS_ITER(e, el, rtmp) \
HASH_ITER(hh, (e), el, rtmp)
#define SESSIONS_ITER_SAFE(e, el, rtmp) \
for ((el) = (e); (el) && ((rtmp) = (el)->hh.next, 1); (el) = (rtmp))
#define SESSIONS_FIND(e, k, res) { \
HASH_FIND(hh, (e), &(k), sizeof(k), (res)); \
}
#endif /* COAP_SESSION_INTERNAL_H_ */

View File

@ -0,0 +1,151 @@
/*
* coap_subscribe_internal.h -- Structures, Enums & Functions that are not
* exposed to application programming
*
* Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_subscribe_internal.h
* @brief COAP subscribe internal information
*/
#ifndef COAP_SUBSCRIBE_INTERNAL_H_
#define COAP_SUBSCRIBE_INTERNAL_H_
/**
* @defgroup subscribe_internal Observe Subscription (Internal)
* CoAP Observe Subscription Structures, Enums and Functions that are not
* exposed to applications
* @{
*/
#ifndef COAP_OBS_MAX_NON
/**
* Number of notifications that may be sent non-confirmable before a confirmable
* message is sent to detect if observers are alive. The maximum allowed value
* here is @c 15.
*/
#define COAP_OBS_MAX_NON 5
#endif /* COAP_OBS_MAX_NON */
#ifndef COAP_OBS_MAX_FAIL
/**
* Number of confirmable notifications that may fail (i.e. time out without
* being ACKed) before an observer is removed. The maximum value for
* COAP_OBS_MAX_FAIL is @c 3.
*/
#define COAP_OBS_MAX_FAIL 3
#endif /* COAP_OBS_MAX_FAIL */
/** Subscriber information */
struct coap_subscription_t {
struct coap_subscription_t *next; /**< next element in linked list */
struct coap_session_t *session; /**< subscriber session */
unsigned int non_cnt:4; /**< up to 15 non-confirmable notifies allowed */
unsigned int fail_cnt:2; /**< up to 3 confirmable notifies can fail */
unsigned int dirty:1; /**< set if the notification temporarily could not be
* sent (in that case, the resource's partially
* dirty flag is set too) */
coap_cache_key_t *cache_key; /** cache_key to identify requester */
coap_pdu_t *pdu; /**< PDU to use for additional requests */
};
void coap_subscription_init(coap_subscription_t *);
/**
* Handles a failed observe notify.
*
* @param context The context holding the resource.
* @param session The session that the observe notify failed on.
* @param token The token used when the observe notify failed.
*/
void
coap_handle_failed_notify(coap_context_t *context,
coap_session_t *session,
const coap_binary_t *token);
/**
* Checks all known resources to see if they are dirty and then notifies
* subscribed observers.
*
* @param context The context to check for dirty resources.
*/
void coap_check_notify(coap_context_t *context);
/**
* Adds the specified peer as observer for @p resource. The subscription is
* identified by the given @p token. This function returns the registered
* subscription information if the @p observer has been added, or @c NULL on
* error.
*
* @param resource The observed resource.
* @param session The observer's session
* @param token The token that identifies this subscription.
* @param pdu The requesting pdu.
*
* @return A pointer to the added/updated subscription
* information or @c NULL on error.
*/
coap_subscription_t *coap_add_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token,
const coap_pdu_t *pdu);
/**
* Returns a subscription object for given @p peer.
*
* @param resource The observed resource.
* @param session The observer's session
* @param token The token that identifies this subscription or @c NULL for
* any token.
* @return A valid subscription if exists or @c NULL otherwise.
*/
coap_subscription_t *coap_find_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token);
/**
* Flags that data is ready to be sent to observers.
*
* @param context The CoAP context to use.
* @param session The observer's session
* @param token The corresponding token that has been used for the
* subscription.
*/
void coap_touch_observer(coap_context_t *context,
coap_session_t *session,
const coap_binary_t *token);
/**
* Removes any subscription for @p observer from @p resource and releases the
* allocated storage. The result is @c 1 if an observation relationship with @p
* observer and @p token existed, @c 0 otherwise.
*
* @param resource The observed resource.
* @param session The observer's session.
* @param token The token that identifies this subscription or @c NULL for
* any token.
* @return @c 1 if the observer has been deleted, @c 0 otherwise.
*/
int coap_delete_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token);
/**
* Removes any subscription for @p session and releases the allocated storage.
*
* @param context The CoAP context to use.
* @param session The observer's session.
*/
void coap_delete_observers(coap_context_t *context, coap_session_t *session);
/** @} */
#endif /* COAP_SUBSCRIBE_INTERNAL_H_ */

View File

@ -0,0 +1,111 @@
/*
* coap_tcp_internal.h -- TCP functions for libcoap
*
* Copyright (C) 2019--2020 Olaf Bergmann <bergmann@tzi.org> and others
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file coap_tcp_internal.h
* @brief COAP tcp internal information
*/
#ifndef COAP_TCP_INTERNAL_H_
#define COAP_TCP_INTERNAL_H_
#include "coap_io.h"
/**
* @defgroup tcp TCP Support (Internal)
* CoAP TCP Structures, Enums and Functions that are not exposed to
* applications
* @{
*/
#if !COAP_DISABLE_TCP
/**
* Create a new TCP socket and initiate the connection
*
* Internal function.
*
* @param sock Where socket information is to be filled in
* @param local_if The local address to use or NULL
* @param server The address to connect to
* @param default_port The port to use if not set in @p server
* @param local_addr Filled in after connection initiation with
* the local address
* @param remote_addr Filled in after connection initiation with
* the remote address
*
* @return @c 1 if succesful, @c 0 if failure of some sort
*/
int
coap_socket_connect_tcp1(coap_socket_t *sock,
const coap_address_t *local_if,
const coap_address_t *server,
int default_port,
coap_address_t *local_addr,
coap_address_t *remote_addr);
/**
* Complete the TCP Connection
*
* Internal function.
*
* @param sock The socket information to use
* @param local_addr Filled in with the final local address
* @param remote_addr Filled in with the final remote address
*
* @return @c 1 if succesful, @c 0 if failure of some sort
*/
int
coap_socket_connect_tcp2(coap_socket_t *sock,
coap_address_t *local_addr,
coap_address_t *remote_addr);
/**
* Create a new TCP socket and then listen for new incoming TCP sessions
*
* Internal function.
*
* @param sock Where socket information is to be filled in
* @param listen_addr The address to be listening for new incoming sessions
* @param bound_addr Filled in with the address that the TCP layer
* is listening on for new incoming TCP sessions
*
* @return @c 1 if succesful, @c 0 if failure of some sort
*/
int
coap_socket_bind_tcp(coap_socket_t *sock,
const coap_address_t *listen_addr,
coap_address_t *bound_addr);
/**
* Accept a new incoming TCP session
*
* Internal function.
*
* @param server The socket information to use to accept the TCP connection
* @param new_client Filled in socket information with the new incoming
* session information
* @param local_addr Filled in with the local address
* @param remote_addr Filled in with the remote address
*
* @return @c 1 if succesful, @c 0 if failure of some sort
*/
int
coap_socket_accept_tcp(coap_socket_t *server,
coap_socket_t *new_client,
coap_address_t *local_addr,
coap_address_t *remote_addr);
#endif /* !COAP_DISABLE_TCP */
/** @} */
#endif /* COAP_TCP_INTERNAL_H_ */

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2010-2019 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -47,6 +49,10 @@ COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) {
return t / COAP_TICKS_PER_SECOND;
}
COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
}
#elif defined(WITH_CONTIKI)
#include "clock.h"
@ -75,7 +81,38 @@ COAP_STATIC_INLINE coap_time_t coap_ticks_to_rt(coap_tick_t t) {
return t / COAP_TICKS_PER_SECOND;
}
#else
COAP_STATIC_INLINE uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
return (uint64_t)t * 1000000 / COAP_TICKS_PER_SECOND;
}
#elif defined(RIOT_VERSION)
#include <xtimer.h>
#define COAP_TICKS_PER_SECOND (XTIMER_HZ)
typedef uint64_t coap_tick_t;
typedef int64_t coap_tick_diff_t;
typedef uint32_t coap_time_t;
static inline void coap_clock_init(void) {}
static inline void coap_ticks(coap_tick_t *t) {
*t = xtimer_now_usec64();
}
static inline coap_time_t coap_ticks_to_rt(coap_tick_t t) {
return t / 1000000UL;
}
static inline uint64_t coap_ticks_to_rt_us(coap_tick_t t) {
return t;
}
static inline coap_tick_t coap_ticks_from_rt_us(uint64_t t) {
return t / 1000000UL;
}
#else /* !WITH_LWIP && !WITH_CONTIKI && !RIOT_VERSION */
#include <stdint.h>
/**

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2010-2012 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -18,15 +20,6 @@
#include <stdint.h>
#define Nn 8 /* duplicate definition of N if built on sky motes */
#define ENCODE_HEADER_SIZE 4
#define HIBIT (1 << (Nn - 1))
#define EMASK ((1 << ENCODE_HEADER_SIZE) - 1)
#define MMASK ((1 << Nn) - 1 - EMASK)
#define MAX_VALUE ( (1 << Nn) - (1 << ENCODE_HEADER_SIZE) ) * (1 << ((1 << ENCODE_HEADER_SIZE) - 1))
#define COAP_PSEUDOFP_DECODE_8_4(r) (r < HIBIT ? r : (r & MMASK) << (r & EMASK))
#ifndef HAVE_FLS
/* include this only if fls() is not available */
extern int coap_fls(unsigned int i);
@ -41,28 +34,41 @@ extern int coap_flsll(long long i);
#define coap_flsll(i) flsll(i)
#endif
/* ls and s must be integer variables */
#define COAP_PSEUDOFP_ENCODE_8_4_DOWN(v,ls) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (v >> ls) & MMASK) + ls)
#define COAP_PSEUDOFP_ENCODE_8_4_UP(v,ls,s) (v < HIBIT ? v : (ls = coap_fls(v) - Nn, (s = (((v + ((1<<ENCODE_HEADER_SIZE<<ls)-1)) >> ls) & MMASK)), s == 0 ? HIBIT + ls + 1 : s + ls))
/**
* @defgroup encode Encode / Decode API
* API functions for endoding/decoding CoAP options.
* @{
*/
/**
* Decodes multiple-length byte sequences. @p buf points to an input byte
* sequence of length @p length. Returns the decoded value.
* sequence of length @p length. Returns the up to 4 byte decoded value.
*
* @param buf The input byte sequence to decode from
* @param length The length of the input byte sequence
*
* @return The decoded value
*/
unsigned int coap_decode_var_bytes(const uint8_t *buf, unsigned int length);
unsigned int coap_decode_var_bytes(const uint8_t *buf, size_t length);
/**
* Decodes multiple-length byte sequences. @p buf points to an input byte
* sequence of length @p length. Returns the up to 8 byte decoded value.
*
* @param buf The input byte sequence to decode from
* @param length The length of the input byte sequence
*
* @return The decoded value
*/
uint64_t coap_decode_var_bytes8(const uint8_t *buf, size_t length);
/**
* Encodes multiple-length byte sequences. @p buf points to an output buffer of
* sufficient length to store the encoded bytes. @p value is the value to
* encode.
* sufficient length to store the encoded bytes. @p value is the 4 byte value
* to encode.
* Returns the number of bytes used to encode @p value or 0 on error.
*
* @param buf The output buffer to decode into
* @param buf The output buffer to encode into
* @param length The output buffer size to encode into (must be sufficient)
* @param value The value to encode into the buffer
*
@ -72,6 +78,24 @@ unsigned int coap_encode_var_safe(uint8_t *buf,
size_t length,
unsigned int value);
/**
* Encodes multiple-length byte sequences. @p buf points to an output buffer of
* sufficient length to store the encoded bytes. @p value is the 8 byte value
* to encode.
* Returns the number of bytes used to encode @p value or 0 on error.
*
* @param buf The output buffer to encode into
* @param length The output buffer size to encode into (must be sufficient)
* @param value The value to encode into the buffer
*
* @return The number of bytes used to encode @p value or @c 0 on error.
*/
unsigned int coap_encode_var_safe8(uint8_t *buf,
size_t length,
uint64_t value);
/** @} */
/**
* @deprecated Use coap_encode_var_safe() instead.
* Provided for backward compatibility. As @p value has a

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2015 Carsten Schoenert <c.schoenert@t-online.de>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -46,6 +48,13 @@ typedef USHORT in_port_t;
# define COAP_DEPRECATED __attribute__ ((deprecated))
# endif
#endif
#ifndef COAP_UNUSED
# ifdef __GNUC__
# define COAP_UNUSED __attribute__((unused))
# else /* __GNUC__ */
# define COAP_UNUSED
# endif /* __GNUC__ */
#endif /* COAP_UNUSED */
void coap_startup(void);

View File

@ -1,4 +1,6 @@
/*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -9,10 +11,10 @@
* include this or include something more generic which includes this), and
* MEMP_USE_CUSTOM_POOLS has to be set in lwipopts.h. */
#include "coap_config.h"
#include <net.h>
#include <resource.h>
#include <subscribe.h>
#include "coap_internal.h"
#include "net.h"
#include "resource.h"
#include "subscribe.h"
#ifndef MEMP_NUM_COAPCONTEXT
#define MEMP_NUM_COAPCONTEXT 1
@ -67,6 +69,34 @@
#define MEMP_LEN_COAPSTRING 32
#endif
#ifndef MEMP_NUM_COAPCACHE_KEYS
#define MEMP_NUM_COAPCACHE_KEYS (2U)
#endif /* MEMP_NUM_COAPCACHE_KEYS */
#ifndef MEMP_NUM_COAPCACHE_ENTRIES
#define MEMP_NUM_COAPCACHE_ENTRIES (2U)
#endif /* MEMP_NUM_COAPCACHE_ENTRIES */
#ifndef MEMP_NUM_COAPPDUBUF
#define MEMP_NUM_COAPPDUBUF 2
#endif
#ifndef MEMP_LEN_COAPPDUBUF
#define MEMP_LEN_COAPPDUBUF 32
#endif
#ifndef MEMP_NUM_COAPLGXMIT
#define MEMP_NUM_COAPLGXMIT 2
#endif
#ifndef MEMP_NUM_COAPLGCRCV
#define MEMP_NUM_COAPLGCRCV 2
#endif
#ifndef MEMP_NUM_COAPLGSRCV
#define MEMP_NUM_COAPLGSRCV 2
#endif
LWIP_MEMPOOL(COAP_CONTEXT, MEMP_NUM_COAPCONTEXT, sizeof(coap_context_t), "COAP_CONTEXT")
LWIP_MEMPOOL(COAP_ENDPOINT, MEMP_NUM_COAPENDPOINT, sizeof(coap_endpoint_t), "COAP_ENDPOINT")
LWIP_MEMPOOL(COAP_PACKET, MEMP_NUM_COAPPACKET, sizeof(coap_packet_t), "COAP_PACKET")
@ -78,4 +108,10 @@ LWIP_MEMPOOL(COAP_RESOURCE, MEMP_NUM_COAPRESOURCE, sizeof(coap_resource_t), "COA
LWIP_MEMPOOL(COAP_RESOURCEATTR, MEMP_NUM_COAPRESOURCEATTR, sizeof(coap_attr_t), "COAP_RESOURCEATTR")
LWIP_MEMPOOL(COAP_OPTLIST, MEMP_NUM_COAPOPTLIST, sizeof(coap_optlist_t)+MEMP_LEN_COAPOPTLIST, "COAP_OPTLIST")
LWIP_MEMPOOL(COAP_STRING, MEMP_NUM_COAPSTRING, sizeof(coap_string_t)+MEMP_LEN_COAPSTRING, "COAP_STRING")
LWIP_MEMPOOL(COAP_CACHE_KEY, MEMP_NUM_COAPCACHE_KEYS, sizeof(coap_cache_key_t), "COAP_CACHE_KEY")
LWIP_MEMPOOL(COAP_CACHE_ENTRY, MEMP_NUM_COAPCACHE_ENTRIES, sizeof(coap_cache_entry_t), "COAP_CACHE_ENTRY")
LWIP_MEMPOOL(COAP_PDU_BUF, MEMP_NUM_COAPPDUBUF, MEMP_LEN_COAPPDUBUF, "COAP_PDU_BUF")
LWIP_MEMPOOL(COAP_LG_XMIT, MEMP_NUM_COAPLGXMIT, sizeof(coap_lg_xmit_t), "COAP_LG_XMIT")
LWIP_MEMPOOL(COAP_LG_CRCV, MEMP_NUM_COAPLGCRCV, sizeof(coap_lg_crcv_t), "COAP_LG_CRCV")
LWIP_MEMPOOL(COAP_LG_SRCV, MEMP_NUM_COAPLGSRCV, sizeof(coap_lg_srcv_t), "COAP_LG_SRCV")

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2010-2011,2014-2015 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -43,6 +45,11 @@ typedef enum {
#endif
COAP_SESSION,
COAP_OPTLIST,
COAP_CACHE_KEY,
COAP_CACHE_ENTRY,
COAP_LG_XMIT,
COAP_LG_CRCV,
COAP_LG_SRCV,
} coap_memory_tag_t;
#ifndef WITH_LWIP
@ -59,6 +66,22 @@ typedef enum {
*/
void *coap_malloc_type(coap_memory_tag_t type, size_t size);
/**
* Reallocates a chunk @p p of bytes created by coap_malloc_type() or
* coap_realloc_type() and returns a pointer to the newly allocated memory of
* @p size.
* Only COAP_STRING type is supported.
*
* Note: If there is an error, @p p will separately need to be released by
* coap_free_type().
*
* @param type The type of object to be stored.
* @param p A pointer to memory that was allocated by coap_malloc_type().
* @param size The number of bytes requested.
* @return A pointer to the allocated storage or @c NULL on error.
*/
void *coap_realloc_type(coap_memory_tag_t type, void *p, size_t size);
/**
* Releases the memory that was allocated by coap_malloc_type(). The type tag @p
* type must be the same that was used for allocating the object pointed to by

View File

@ -0,0 +1,779 @@
/*
* net.h -- CoAP network interface
*
* Copyright (C) 2010-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_NET_H_
#define COAP_NET_H_
#include <stdlib.h>
#include <string.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#include <time.h>
#ifdef WITH_LWIP
#include <lwip/ip_addr.h>
#endif
#include "coap_io.h"
#include "coap_dtls.h"
#include "coap_event.h"
#include "pdu.h"
#include "coap_session.h"
/**
* @defgroup context Context Handling
* API functions for handling PDUs using CoAP Contexts
* @{
*/
typedef enum coap_response_t {
COAP_RESPONSE_FAIL, /**< Response not liked - send CoAP RST packet */
COAP_RESPONSE_OK /**< Response is fine */
} coap_response_t;
/**
* Response handler that is used as callback in coap_context_t.
*
* @param session CoAP session.
* @param sent The PDU that was transmitted.
* @param received The PDU that was received.
* @param mid CoAP transaction ID.
* @return @c COAP_RESPONSE_OK if successful, else @c COAP_RESPONSE_FAIL which
* triggers sending a RST packet.
*/
typedef coap_response_t (*coap_response_handler_t)(coap_session_t *session,
const coap_pdu_t *sent,
const coap_pdu_t *received,
const coap_mid_t mid);
/**
* Negative Acknowedge handler that is used as callback in coap_context_t.
*
* @param session CoAP session.
* @param sent The PDU that was transmitted.
* @param reason The reason for the NACK.
* @param mid CoAP message ID.
*/
typedef void (*coap_nack_handler_t)(coap_session_t *session,
const coap_pdu_t *sent,
const coap_nack_reason_t reason,
const coap_mid_t mid);
/**
* Received Ping handler that is used as callback in coap_context_t.
*
* @param session CoAP session.
* @param received The PDU that was received.
* @param mid CoAP message ID.
*/
typedef void (*coap_ping_handler_t)(coap_session_t *session,
const coap_pdu_t *received,
const coap_mid_t mid);
/**
* Received Pong handler that is used as callback in coap_context_t.
*
* @param session CoAP session.
* @param received The PDU that was received.
* @param mid CoAP message ID.
*/
typedef void (*coap_pong_handler_t)(coap_session_t *session,
const coap_pdu_t *received,
const coap_mid_t mid);
/**
* Registers a new message handler that is called whenever a response is
* received.
*
* @param context The context to register the handler for.
* @param handler The response handler to register.
*/
void
coap_register_response_handler(coap_context_t *context,
coap_response_handler_t handler);
/**
* Registers a new message handler that is called whenever a confirmable
* message (request or response) is dropped after all retries have been
* exhausted, or a rst message was received, or a network or TLS level
* event was received that indicates delivering the message is not possible.
*
* @param context The context to register the handler for.
* @param handler The nack handler to register.
*/
void
coap_register_nack_handler(coap_context_t *context,
coap_nack_handler_t handler);
/**
* Registers a new message handler that is called whenever a CoAP Ping
* message is received.
*
* @param context The context to register the handler for.
* @param handler The ping handler to register.
*/
void
coap_register_ping_handler(coap_context_t *context,
coap_ping_handler_t handler);
/**
* Registers a new message handler that is called whenever a CoAP Pong
* message is received.
*
* @param context The context to register the handler for.
* @param handler The pong handler to register.
*/
void
coap_register_pong_handler(coap_context_t *context,
coap_pong_handler_t handler);
/**
* Registers the option type @p type with the given context object @p ctx.
*
* @param ctx The context to use.
* @param type The option type to register.
*/
void
coap_register_option(coap_context_t *ctx, uint16_t type);
/**
* Creates a new coap_context_t object that will hold the CoAP stack status.
*/
coap_context_t *coap_new_context(const coap_address_t *listen_addr);
/**
* Set the context's default PSK hint and/or key for a server.
*
* @param context The current coap_context_t object.
* @param hint The default PSK server hint sent to a client. If NULL, PSK
* authentication is disabled. Empty string is a valid hint.
* @param key The default PSK key. If NULL, PSK authentication will fail.
* @param key_len The default PSK key's length. If @p 0, PSK authentication will
* fail.
*
* @return @c 1 if successful, else @c 0.
*/
int coap_context_set_psk( coap_context_t *context, const char *hint,
const uint8_t *key, size_t key_len );
/**
* Set the context's default PSK hint and/or key for a server.
*
* @param context The current coap_context_t object.
* @param setup_data If NULL, PSK authentication will fail. PSK
* information required.
*
* @return @c 1 if successful, else @c 0.
*/
int coap_context_set_psk2(coap_context_t *context,
coap_dtls_spsk_t *setup_data);
/**
* Set the context's default PKI information for a server.
*
* @param context The current coap_context_t object.
* @param setup_data If NULL, PKI authentication will fail. Certificate
* information required.
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_context_set_pki(coap_context_t *context,
const coap_dtls_pki_t *setup_data);
/**
* Set the context's default Root CA information for a client or server.
*
* @param context The current coap_context_t object.
* @param ca_file If not NULL, is the full path name of a PEM encoded
* file containing all the Root CAs to be used.
* @param ca_dir If not NULL, points to a directory containing PEM
* encoded files containing all the Root CAs to be used.
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_context_set_pki_root_cas(coap_context_t *context,
const char *ca_file,
const char *ca_dir);
/**
* Set the context keepalive timer for sessions.
* A keepalive message will be sent after if a session has been inactive,
* i.e. no packet sent or received, for the given number of seconds.
* For unreliable protocols, a CoAP Empty message will be sent. If a
* CoAP RST is not received, the CoAP Empty messages will get resent based
* on the Confirmable retry parameters until there is a failure timeout,
* at which point the session will be considered as disconnected.
* For reliable protocols, a CoAP PING message will be sent. If a CoAP PONG
* has not been received before the next PING is due to be sent, the session
* will be considered as disconnected.
*
* @param context The coap_context_t object.
* @param seconds Number of seconds for the inactivity timer, or zero
* to disable CoAP-level keepalive messages.
*/
void coap_context_set_keepalive(coap_context_t *context, unsigned int seconds);
/**
* Get the libcoap internal file descriptor for using in an application's
* select() or returned as an event in an application's epoll_wait() call.
*
* @param context The coap_context_t object.
*
* @return The libcoap file descriptor or @c -1 if epoll is not available.
*/
int coap_context_get_coap_fd(const coap_context_t *context);
/**
* Set the maximum idle sessions count. The number of server sessions that
* are currently not in use. If this number is exceeded, the least recently
* used server session is completely removed.
* 0 (the default) means that the number is not monitored.
*
* @param context The coap_context_t object.
* @param max_idle_sessions The maximum idle session count.
*/
void
coap_context_set_max_idle_sessions(coap_context_t *context,
unsigned int max_idle_sessions);
/**
* Get the maximum idle sessions count.
*
* @param context The coap_context_t object.
*
* @return The count of max idle sessions.
*/
unsigned int
coap_context_get_max_idle_sessions(const coap_context_t *context);
/**
* Set the session timeout value. The number of seconds of inactivity after
* which an unused server session will be closed.
* 0 means use default (300 secs).
*
* @param context The coap_context_t object.
* @param session_timeout The session timeout value.
*/
void
coap_context_set_session_timeout(coap_context_t *context,
unsigned int session_timeout);
/**
* Get the session timeout value
*
* @param context The coap_context_t object.
*
* @return The session timeout value.
*/
unsigned int
coap_context_get_session_timeout(const coap_context_t *context);
/**
* Set the CSM timeout value. The number of seconds to wait for a (TCP) CSM
* negotiation response from the peer.
* 0 (the default) means use wait forever.
*
* @param context The coap_context_t object.
* @param csm_tmeout The CSM timeout value.
*/
void
coap_context_set_csm_timeout(coap_context_t *context,
unsigned int csm_tmeout);
/**
* Get the CSM timeout value
*
* @param context The coap_context_t object.
*
* @return The CSM timeout value.
*/
unsigned int
coap_context_get_csm_timeout(const coap_context_t *context);
/**
* Set the maximum number of sessions in (D)TLS handshake value. If this number
* is exceeded, the least recently used server session in handshake is
* completely removed.
* 0 (the default) means that the number is not monitored.
*
* @param context The coap_context_t object.
* @param max_handshake_sessions The maximum number of sessions in handshake.
*/
void
coap_context_set_max_handshake_sessions(coap_context_t *context,
unsigned int max_handshake_sessions);
/**
* Get the session timeout value
*
* @param context The coap_context_t object.
*
* @return The maximim number of sessions in (D)TLS handshake value.
*/
unsigned int
coap_context_get_max_handshake_sessions(const coap_context_t *context);
/**
* Returns a new message id and updates @p session->tx_mid accordingly. The
* message id is returned in network byte order to make it easier to read in
* tracing tools.
*
* @param session The current coap_session_t object.
*
* @return Incremented message id in network byte order.
*/
uint16_t coap_new_message_id(coap_session_t *session);
/**
* CoAP stack context must be released with coap_free_context(). This function
* clears all entries from the receive queue and send queue and deletes the
* resources that have been registered with @p context, and frees the attached
* endpoints.
*
* @param context The current coap_context_t object to free off.
*/
void coap_free_context(coap_context_t *context);
/**
* Stores @p data with the given CoAP context. This function
* overwrites any value that has previously been stored with @p
* context.
*
* @param context The CoAP context.
* @param data The data to store with wih the context. Note that this data
* must be valid during the lifetime of @p context.
*/
void coap_set_app_data(coap_context_t *context, void *data);
/**
* Returns any application-specific data that has been stored with @p
* context using the function coap_set_app_data(). This function will
* return @c NULL if no data has been stored.
*
* @param context The CoAP context.
*
* @return The data previously stored or @c NULL if not data stored.
*/
void *coap_get_app_data(const coap_context_t *context);
/**
* Creates a new ACK PDU with specified error @p code. The options specified by
* the filter expression @p opts will be copied from the original request
* contained in @p request. Unless @c SHORT_ERROR_RESPONSE was defined at build
* time, the textual reason phrase for @p code will be added as payload, with
* Content-Type @c 0.
* This function returns a pointer to the new response message, or @c NULL on
* error. The storage allocated for the new message must be released with
* coap_free().
*
* @param request Specification of the received (confirmable) request.
* @param code The error code to set.
* @param opts An option filter that specifies which options to copy from
* the original request in @p node.
*
* @return A pointer to the new message or @c NULL on error.
*/
coap_pdu_t *coap_new_error_response(const coap_pdu_t *request,
coap_pdu_code_t code,
coap_opt_filter_t *opts);
/**
* Sends an error response with code @p code for request @p request to @p dst.
* @p opts will be passed to coap_new_error_response() to copy marked options
* from the request. This function returns the message id if the message was
* sent, or @c COAP_INVALID_MID otherwise.
*
* @param session The CoAP session.
* @param request The original request to respond to.
* @param code The response code.
* @param opts A filter that specifies the options to copy from the
* @p request.
*
* @return The message id if the message was sent, or @c
* COAP_INVALID_MID otherwise.
*/
coap_mid_t coap_send_error(coap_session_t *session,
const coap_pdu_t *request,
coap_pdu_code_t code,
coap_opt_filter_t *opts);
/**
* Helper function to create and send a message with @p type (usually ACK or
* RST). This function returns @c COAP_INVALID_MID when the message was not
* sent, a valid transaction id otherwise.
*
* @param session The CoAP session.
* @param request The request that should be responded to.
* @param type Which type to set.
* @return message id on success or @c COAP_INVALID_MID
* otherwise.
*/
coap_mid_t
coap_send_message_type(coap_session_t *session, const coap_pdu_t *request,
coap_pdu_type_t type);
/**
* Sends an ACK message with code @c 0 for the specified @p request to @p dst.
* This function returns the corresponding message id if the message was
* sent or @c COAP_INVALID_MID on error.
*
* @param session The CoAP session.
* @param request The request to be acknowledged.
*
* @return The message id if ACK was sent or @c
* COAP_INVALID_MID on error.
*/
coap_mid_t coap_send_ack(coap_session_t *session, const coap_pdu_t *request);
/**
* Sends an RST message with code @c 0 for the specified @p request to @p dst.
* This function returns the corresponding message id if the message was
* sent or @c COAP_INVALID_MID on error.
*
* @param session The CoAP session.
* @param request The request to be reset.
*
* @return The message id if RST was sent or @c
* COAP_INVALID_MID on error.
*/
COAP_STATIC_INLINE coap_mid_t
coap_send_rst(coap_session_t *session, const coap_pdu_t *request) {
return coap_send_message_type(session, request, COAP_MESSAGE_RST);
}
/**
* Sends a CoAP message to given peer. The memory that is
* allocated for the pdu will be released by coap_send().
* The caller must not use the pdu after calling coap_send().
*
* @param session The CoAP session.
* @param pdu The CoAP PDU to send.
*
* @return The message id of the sent message or @c
* COAP_INVALID_MID on error.
*/
coap_mid_t coap_send( coap_session_t *session, coap_pdu_t *pdu );
#define coap_send_large(session, pdu) coap_send(session, pdu)
/**
* Invokes the event handler of @p context for the given @p event and
* @p data.
*
* @param context The CoAP context whose event handler is to be called.
* @param event The event to deliver.
* @param session The session related to @p event.
* @return The result from the associated event handler or 0 if none was
* registered.
*/
int coap_handle_event(coap_context_t *context,
coap_event_t event,
coap_session_t *session);
/**
* Returns 1 if there are no messages to send or to dispatch in the context's
* queues. */
int coap_can_exit(coap_context_t *context);
/**
* Returns the current value of an internal tick counter. The counter counts \c
* COAP_TICKS_PER_SECOND ticks every second.
*/
void coap_ticks(coap_tick_t *);
/**
* Function interface for joining a multicast group for listening for the
* currently defined endpoints that are UDP.
*
* @param ctx The current context.
* @param groupname The name of the group that is to be joined for listening.
* @param ifname Network interface to join the group on, or NULL if first
* appropriate interface is to be chosen by the O/S.
*
* @return 0 on success, -1 on error
*/
int
coap_join_mcast_group_intf(coap_context_t *ctx, const char *groupname,
const char *ifname);
#define coap_join_mcast_group(ctx, groupname) \
(coap_join_mcast_group_intf(ctx, groupname, NULL))
/**
* Function interface for defining the hop count (ttl) for sending
* multicast traffic
*
* @param session The current contexsion.
* @param hops The number of hops (ttl) to use before the multicast
* packet expires.
*
* @return 1 on success, 0 on error
*/
int
coap_mcast_set_hops(coap_session_t *session, size_t hops);
/**@}*/
/**
* @defgroup app_io Application I/O Handling
* API functions for Application Input / Output
* @{
*/
#define COAP_IO_WAIT 0
#define COAP_IO_NO_WAIT ((uint32_t)-1)
/**
* The main I/O processing function. All pending network I/O is completed,
* and then optionally waits for the next input packet.
*
* This internally calls coap_io_prepare_io(), then select() for the appropriate
* sockets, updates COAP_SOCKET_CAN_xxx where appropriate and then calls
* coap_io_do_io() before returning with the time spent in the function.
*
* Alternatively, if libcoap is compiled with epoll support, this internally
* calls coap_io_prepare_epoll(), then epoll_wait() for waiting for any file
* descriptors that have (internally) been set up with epoll_ctl() and
* finally coap_io_do_epoll() before returning with the time spent in the
* function.
*
* @param ctx The CoAP context
* @param timeout_ms Minimum number of milliseconds to wait for new packets
* before returning after doing any processing.
* If COAP_IO_WAIT, the call will block until the next
* internal action (e.g. packet retransmit) if any, or block
* until the next packet is received whichever is the sooner
* and do the necessary processing.
* If COAP_IO_NO_WAIT, the function will return immediately
* after processing without waiting for any new input
* packets to arrive.
*
* @return Number of milliseconds spent in function or @c -1 if there was
* an error
*/
int coap_io_process(coap_context_t *ctx, uint32_t timeout_ms);
#ifndef RIOT_VERSION
/**
* The main message processing loop with additional fds for internal select.
*
* @param ctx The CoAP context
* @param timeout_ms Minimum number of milliseconds to wait for new packets
* before returning after doing any processing.
* If COAP_IO_WAIT, the call will block until the next
* internal action (e.g. packet retransmit) if any, or block
* until the next packet is received whichever is the sooner
* and do the necessary processing.
* If COAP_IO_NO_WAIT, the function will return immediately
* after processing without waiting for any new input
* packets to arrive.
* @param nfds The maximum FD set in readfds, writefds or exceptfds
* plus one,
* @param readfds Read FDs to additionally check for in internal select()
* or NULL if not required.
* @param writefds Write FDs to additionally check for in internal select()
* or NULL if not required.
* @param exceptfds Except FDs to additionally check for in internal select()
* or NULL if not required.
*
*
* @return Number of milliseconds spent in coap_io_process_with_fds, or @c -1
* if there was an error. If defined, readfds, writefds, exceptfds
* are updated as returned by the internal select() call.
*/
int coap_io_process_with_fds(coap_context_t *ctx, uint32_t timeout_ms,
int nfds, fd_set *readfds, fd_set *writefds,
fd_set *exceptfds);
#endif /* !RIOT_VERSION */
/**@}*/
/**
* @defgroup app_io_internal Application I/O Handling (Internal)
* Internal API functions for Application Input / Output
* @{
*/
/**
* Iterates through all the coap_socket_t structures embedded in endpoints or
* sessions associated with the @p ctx to determine which are wanting any
* read, write, accept or connect I/O (COAP_SOCKET_WANT_xxx is set). If set,
* the coap_socket_t is added to the @p sockets.
*
* Any now timed out delayed packet is transmitted, along with any packets
* associated with requested observable response.
*
* In addition, it returns when the next expected I/O is expected to take place
* (e.g. a packet retransmit).
*
* Prior to calling coap_io_do_io(), the @p sockets must be tested to see
* if any of the COAP_SOCKET_WANT_xxx have the appropriate information and if
* so, COAP_SOCKET_CAN_xxx is set. This typically will be done after using a
* select() call.
*
* Note: If epoll support is compiled into libcoap, coap_io_prepare_epoll() must
* be used instead of coap_io_prepare_io().
*
* Internal function.
*
* @param ctx The CoAP context
* @param sockets Array of socket descriptors, filled on output
* @param max_sockets Size of socket array.
* @param num_sockets Pointer to the number of valid entries in the socket
* arrays on output.
* @param now Current time.
*
* @return timeout Maxmimum number of milliseconds that can be used by a
* select() to wait for network events or 0 if wait should be
* forever.
*/
unsigned int
coap_io_prepare_io(coap_context_t *ctx,
coap_socket_t *sockets[],
unsigned int max_sockets,
unsigned int *num_sockets,
coap_tick_t now
);
/**
* Processes any outstanding read, write, accept or connect I/O as indicated
* in the coap_socket_t structures (COAP_SOCKET_CAN_xxx set) embedded in
* endpoints or sessions associated with @p ctx.
*
* Note: If epoll support is compiled into libcoap, coap_io_do_epoll() must
* be used instead of coap_io_do_io().
*
* Internal function.
*
* @param ctx The CoAP context
* @param now Current time
*/
void coap_io_do_io(coap_context_t *ctx, coap_tick_t now);
/**
* Any now timed out delayed packet is transmitted, along with any packets
* associated with requested observable response.
*
* In addition, it returns when the next expected I/O is expected to take place
* (e.g. a packet retransmit).
*
* Note: If epoll support is compiled into libcoap, coap_io_prepare_epoll() must
* be used instead of coap_io_prepare_io().
*
* Internal function.
*
* @param ctx The CoAP context
* @param now Current time.
*
* @return timeout Maxmimum number of milliseconds that can be used by a
* epoll_wait() to wait for network events or 0 if wait should be
* forever.
*/
unsigned int
coap_io_prepare_epoll(coap_context_t *ctx, coap_tick_t now);
struct epoll_event;
/**
* Process all the epoll events
*
* Note: If epoll support is compiled into libcoap, coap_io_do_epoll() must
* be used instead of coap_io_do_io().
*
* Internal function
*
* @param ctx The current CoAP context.
* @param events The list of events returned from an epoll_wait() call.
* @param nevents The number of events.
*
*/
void coap_io_do_epoll(coap_context_t *ctx, struct epoll_event* events,
size_t nevents);
/**@}*/
/**
* @deprecated Use coap_io_process() instead.
*
* This function just calls coap_io_process().
*
* @param ctx The CoAP context
* @param timeout_ms Minimum number of milliseconds to wait for new packets
* before returning after doing any processing.
* If COAP_IO_WAIT, the call will block until the next
* internal action (e.g. packet retransmit) if any, or block
* until the next packet is received whichever is the sooner
* and do the necessary processing.
* If COAP_IO_NO_WAIT, the function will return immediately
* after processing without waiting for any new input
* packets to arrive.
*
* @return Number of milliseconds spent in function or @c -1 if there was
* an error
*/
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_run_once(coap_context_t *ctx, uint32_t timeout_ms)
{
return coap_io_process(ctx, timeout_ms);
}
/**
* @deprecated Use coap_io_prepare_io() instead.
*
* This function just calls coap_io_prepare_io().
*
* Internal function.
*
* @param ctx The CoAP context
* @param sockets Array of socket descriptors, filled on output
* @param max_sockets Size of socket array.
* @param num_sockets Pointer to the number of valid entries in the socket
* arrays on output.
* @param now Current time.
*
* @return timeout Maxmimum number of milliseconds that can be used by a
* select() to wait for network events or 0 if wait should be
* forever.
*/
COAP_STATIC_INLINE COAP_DEPRECATED unsigned int
coap_write(coap_context_t *ctx,
coap_socket_t *sockets[],
unsigned int max_sockets,
unsigned int *num_sockets,
coap_tick_t now
) {
return coap_io_prepare_io(ctx, sockets, max_sockets, num_sockets, now);
}
/**
* @deprecated Use coap_io_do_io() instead.
*
* This function just calls coap_io_do_io().
*
* Internal function.
*
* @param ctx The CoAP context
* @param now Current time
*/
COAP_STATIC_INLINE COAP_DEPRECATED void
coap_read(coap_context_t *ctx, coap_tick_t now
) {
coap_io_do_io(ctx, now);
}
/* Old definitions which may be hanging around in old code - be helpful! */
#define COAP_RUN_NONBLOCK COAP_RUN_NONBLOCK_deprecated_use_COAP_IO_NO_WAIT
#define COAP_RUN_BLOCK COAP_RUN_BLOCK_deprecated_use_COAP_IO_WAIT
#endif /* COAP_NET_H_ */

View File

@ -3,6 +3,8 @@
*
* Copyright (C) 2010-2013 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
@ -15,8 +17,7 @@
#ifndef COAP_OPTION_H_
#define COAP_OPTION_H_
#include "bits.h"
#include "pdu.h"
typedef uint16_t coap_option_num_t;
/**
* Use byte-oriented access methods here because sliding a complex struct
@ -89,134 +90,66 @@ size_t coap_opt_size(const coap_opt_t *opt);
#error COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG must be less or equal 16
#endif /* (COAP_OPT_FILTER_SHORT + COAP_OPT_FILTER_LONG > 16) */
/** The number of elements in coap_opt_filter_t. */
#define COAP_OPT_FILTER_SIZE \
(((COAP_OPT_FILTER_SHORT + 1) >> 1) + COAP_OPT_FILTER_LONG) +1
/**
* Fixed-size vector we use for option filtering. It is large enough
* to hold COAP_OPT_FILTER_SHORT entries with an option number between
* 0 and 255, and COAP_OPT_FILTER_LONG entries with an option number
* between 256 and 65535. Its internal structure is
*
* @code
struct {
/*
* mask contains a bit vector that indicates which fields in the long_opts[]
* and subsequent short_opts[] are used. The first COAP_OPT_FILTER_LONG bits
* correspond to the long option types that are stored in long_opts[]
* elements. The next COAP_OPT_FILTER_SHORT bits correspond to the short
* option types that are stored in short_opts[].
*/
typedef struct coap_opt_filter_t {
uint16_t mask;
uint16_t long_opts[COAP_OPT_FILTER_LONG];
uint8_t short_opts[COAP_OPT_FILTER_SHORT];
}
* @endcode
*
* The first element contains a bit vector that indicates which fields
* in the remaining array are used. The first COAP_OPT_FILTER_LONG
* bits correspond to the long option types that are stored in the
* elements from index 1 to COAP_OPT_FILTER_LONG. The next
* COAP_OPT_FILTER_SHORT bits correspond to the short option types
* that are stored in the elements from index COAP_OPT_FILTER_LONG + 1
* to COAP_OPT_FILTER_LONG + COAP_OPT_FILTER_SHORT. The latter
* elements are treated as bytes.
*/
typedef uint16_t coap_opt_filter_t[COAP_OPT_FILTER_SIZE];
} coap_opt_filter_t;
/** Pre-defined filter that includes all options. */
#define COAP_OPT_ALL NULL
/**
* Clears filter @p f.
* Clears filter @p filter.
*
* @param f The filter to clear.
* @param filter The filter to clear.
*/
COAP_STATIC_INLINE void
coap_option_filter_clear(coap_opt_filter_t f) {
memset(f, 0, sizeof(coap_opt_filter_t));
}
void
coap_option_filter_clear(coap_opt_filter_t *filter);
/**
* Sets the corresponding entry for @p type in @p filter. This
* Sets the corresponding entry for @p number in @p filter. This
* function returns @c 1 if bit was set or @c 0 on error (i.e. when
* the given type does not fit in the filter).
* the given number does not fit in the filter).
*
* @param filter The filter object to change.
* @param type The type for which the bit should be set.
* @param number The option number for which the bit should be set.
*
* @return @c 1 if bit was set, @c 0 otherwise.
*/
int coap_option_filter_set(coap_opt_filter_t filter, uint16_t type);
int coap_option_filter_set(coap_opt_filter_t *filter, coap_option_num_t number);
/**
* Clears the corresponding entry for @p type in @p filter. This
* Clears the corresponding entry for @p number in @p filter. This
* function returns @c 1 if bit was set or @c 0 on error (i.e. when
* the given type does not fit in the filter).
* the given number does not fit in the filter).
*
* @param filter The filter object to change.
* @param type The type that should be cleared from the filter.
* @param number The option number that should be cleared from the filter.
*
* @return @c 1 if bit was set, @c 0 otherwise.
*/
int coap_option_filter_unset(coap_opt_filter_t filter, uint16_t type);
int coap_option_filter_unset(coap_opt_filter_t *filter,
coap_option_num_t number);
/**
* Checks if @p type is contained in @p filter. This function returns
* Checks if @p number is contained in @p filter. This function returns
* @c 1 if found, @c 0 if not, or @c -1 on error (i.e. when the given
* type does not fit in the filter).
* number does not fit in the filter).
*
* @param filter The filter object to search.
* @param type The type to search for.
* @param number The option number to search for.
*
* @return @c 1 if @p type was found, @c 0 otherwise, or @c -1 on error.
* @return @c 1 if @p number was found, @c 0 otherwise, or @c -1 on error.
*/
int coap_option_filter_get(coap_opt_filter_t filter, uint16_t type);
/**
* Sets the corresponding bit for @p type in @p filter. This function returns @c
* 1 if bit was set or @c -1 on error (i.e. when the given type does not fit in
* the filter).
*
* @deprecated Use coap_option_filter_set() instead.
*
* @param filter The filter object to change.
* @param type The type for which the bit should be set.
*
* @return @c 1 if bit was set, @c -1 otherwise.
*/
COAP_STATIC_INLINE int
coap_option_setb(coap_opt_filter_t filter, uint16_t type) {
return coap_option_filter_set(filter, type) ? 1 : -1;
}
/**
* Clears the corresponding bit for @p type in @p filter. This function returns
* @c 1 if bit was cleared or @c -1 on error (i.e. when the given type does not
* fit in the filter).
*
* @deprecated Use coap_option_filter_unset() instead.
*
* @param filter The filter object to change.
* @param type The type for which the bit should be cleared.
*
* @return @c 1 if bit was set, @c -1 otherwise.
*/
COAP_STATIC_INLINE int
coap_option_clrb(coap_opt_filter_t filter, uint16_t type) {
return coap_option_filter_unset(filter, type) ? 1 : -1;
}
/**
* Gets the corresponding bit for @p type in @p filter. This function returns @c
* 1 if the bit is set @c 0 if not, or @c -1 on error (i.e. when the given type
* does not fit in the filter).
*
* @deprecated Use coap_option_filter_get() instead.
*
* @param filter The filter object to read bit from.
* @param type The type for which the bit should be read.
*
* @return @c 1 if bit was set, @c 0 if not, @c -1 on error.
*/
COAP_STATIC_INLINE int
coap_option_getb(coap_opt_filter_t filter, uint16_t type) {
return coap_option_filter_get(filter, type);
}
int coap_option_filter_get(coap_opt_filter_t *filter, coap_option_num_t number);
/**
* Iterator to run through PDU options. This object must be
@ -236,7 +169,7 @@ coap_option_getb(coap_opt_filter_t filter, uint16_t type) {
*/
typedef struct {
size_t length; /**< remaining length of PDU */
uint16_t type; /**< decoded option type */
coap_option_num_t number; /**< decoded option number */
unsigned int bad:1; /**< iterator object is ok if not set */
unsigned int filtered:1; /**< denotes whether or not filter is used */
coap_opt_t *next_option; /**< pointer to the unparsed next option */
@ -251,8 +184,8 @@ typedef struct {
*
* @param pdu The PDU the options of which should be walked through.
* @param oi An iterator object that will be initilized.
* @param filter An optional option type filter.
* With @p type != @c COAP_OPT_ALL, coap_option_next()
* @param filter An optional option number filter.
* With @p number != @c COAP_OPT_ALL, coap_option_next()
* will return only options matching this bitmask.
* Fence-post options @c 14, @c 28, @c 42, ... are always
* skipped.
@ -261,18 +194,18 @@ typedef struct {
*/
coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *pdu,
coap_opt_iterator_t *oi,
const coap_opt_filter_t filter);
const coap_opt_filter_t *filter);
/**
* Updates the iterator @p oi to point to the next option. This function returns
* a pointer to that option or @c NULL if no more options exist. The contents of
* @p oi will be updated. In particular, @c oi->n specifies the current option's
* ordinal number (counted from @c 1), @c oi->type is the option's type code,
* and @c oi->option points to the beginning of the current option itself. When
* advanced past the last option, @c oi->option will be @c NULL.
* ordinal number (counted from @c 1), @c oi->number is the option's number
* value, and @c oi->option points to the beginning of the current option
* itself. When * advanced past the last option, @c oi->option will be @c NULL.
*
* Note that options are skipped whose corresponding bits in the filter
* specified with coap_option_iterator_init() are @c 0. Options with type codes
* specified with coap_option_iterator_init() are @c 0. Options with numbers
* that do not fit in this filter hence will always be returned.
*
* @param oi The option iterator to update.
@ -282,20 +215,20 @@ coap_opt_iterator_t *coap_option_iterator_init(const coap_pdu_t *pdu,
coap_opt_t *coap_option_next(coap_opt_iterator_t *oi);
/**
* Retrieves the first option of type @p type from @p pdu. @p oi must point to a
* coap_opt_iterator_t object that will be initialized by this function to
* filter only options with code @p type. This function returns the first option
* with this type, or @c NULL if not found.
* Retrieves the first option of number @p number from @p pdu. @p oi must
* point to a coap_opt_iterator_t object that will be initialized by this
* function to filter only options with number @p number. This function returns
* the first option with this number, or @c NULL if not found.
*
* @param pdu The PDU to parse for options.
* @param type The option type code to search for.
* @param number The option number to search for.
* @param oi An iterator object to use.
*
* @return A pointer to the first option of type @p type, or @c NULL if
* @return A pointer to the first option of number @p number, or @c NULL if
* not found.
*/
coap_opt_t *coap_check_option(coap_pdu_t *pdu,
uint16_t type,
coap_opt_t *coap_check_option(const coap_pdu_t *pdu,
coap_option_num_t number,
coap_opt_iterator_t *oi);
/**
@ -349,18 +282,6 @@ size_t coap_opt_encode(coap_opt_t *opt,
const uint8_t *val,
size_t length);
/**
* Decodes the delta value of the next option. This function returns the number
* of bytes read or @c 0 on error. The caller of this function must ensure that
* it does not read over the boundaries of @p opt (e.g. by calling
* coap_opt_check_delta().
*
* @param opt The option to examine.
*
* @return The number of bytes read or @c 0 on error.
*/
uint16_t coap_opt_delta(const coap_opt_t *opt);
/**
* Returns the length of the given option. @p opt must point to an option jump
* or the beginning of the option. This function returns @c 0 when @p opt is not
@ -374,7 +295,7 @@ uint16_t coap_opt_delta(const coap_opt_t *opt);
*
* @return The option's length or @c 0 when undefined.
*/
uint16_t coap_opt_length(const coap_opt_t *opt);
uint32_t coap_opt_length(const coap_opt_t *opt);
/**
* Returns a pointer to the value of the given option. @p opt must point to an
@ -387,8 +308,6 @@ uint16_t coap_opt_length(const coap_opt_t *opt);
*/
const uint8_t *coap_opt_value(const coap_opt_t *opt);
/** @} */
/**
* Representation of chained list of CoAP options to install.
*
@ -415,6 +334,12 @@ typedef struct coap_optlist_t {
/**
* Create a new optlist entry.
*
* Note: Where possible, the option data needs to be stripped of leading zeros
* (big endian) to reduce the amount of data needed in the PDU, as well as in
* some cases the maximum data size of an opton can be exceeded if not stripped
* and hence be illegal. This is done by using coap_encode_var_safe() or
* coap_encode_var_safe8().
*
* @param number The option number (COAP_OPTION_*)
* @param length The option length
* @param data The option value data
@ -458,4 +383,57 @@ int coap_insert_optlist(coap_optlist_t **optlist_chain,
*/
void coap_delete_optlist(coap_optlist_t *optlist_chain);
/** @} */
/**
* Sets the corresponding bit for @p type in @p filter. This function returns @c
* 1 if bit was set or @c -1 on error (i.e. when the given type does not fit in
* the filter).
*
* @deprecated Use coap_option_filter_set() instead.
*
* @param filter The filter object to change.
* @param type The type for which the bit should be set.
*
* @return @c 1 if bit was set, @c -1 otherwise.
*/
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_option_setb(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_set(filter, type) ? 1 : -1;
}
/**
* Clears the corresponding bit for @p type in @p filter. This function returns
* @c 1 if bit was cleared or @c -1 on error (i.e. when the given type does not
* fit in the filter).
*
* @deprecated Use coap_option_filter_unset() instead.
*
* @param filter The filter object to change.
* @param type The type for which the bit should be cleared.
*
* @return @c 1 if bit was set, @c -1 otherwise.
*/
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_option_clrb(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_unset(filter, type) ? 1 : -1;
}
/**
* Gets the corresponding bit for @p type in @p filter. This function returns @c
* 1 if the bit is set @c 0 if not, or @c -1 on error (i.e. when the given type
* does not fit in the filter).
*
* @deprecated Use coap_option_filter_get() instead.
*
* @param filter The filter object to read bit from.
* @param type The type for which the bit should be read.
*
* @return @c 1 if bit was set, @c 0 if not, @c -1 on error.
*/
COAP_STATIC_INLINE COAP_DEPRECATED int
coap_option_getb(coap_opt_filter_t *filter, uint16_t type) {
return coap_option_filter_get(filter, type);
}
#endif /* COAP_OPTION_H_ */

View File

@ -0,0 +1,572 @@
/*
* pdu.h -- CoAP message structure
*
* Copyright (C) 2010-2014 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file pdu.h
* @brief Pre-defined constants that reflect defaults for CoAP
*/
#ifndef COAP_PDU_H_
#define COAP_PDU_H_
#include "uri.h"
#include "option.h"
#ifdef WITH_LWIP
#include <lwip/pbuf.h>
#endif
#include <stdint.h>
/**
* @defgroup pdu PDU
* API functions for PDUs
* @{
*/
#define COAP_DEFAULT_PORT 5683 /* CoAP default UDP/TCP port */
#define COAPS_DEFAULT_PORT 5684 /* CoAP default UDP/TCP port for secure transmission */
#define COAP_DEFAULT_MAX_AGE 60 /* default maximum object lifetime in seconds */
#ifndef COAP_DEFAULT_MTU
#define COAP_DEFAULT_MTU 1152
#endif /* COAP_DEFAULT_MTU */
#ifndef COAP_DEFAULT_HOP_LIMIT
#define COAP_DEFAULT_HOP_LIMIT 16
#endif /* COAP_DEFAULT_HOP_LIMIT */
#define COAP_DEFAULT_SCHEME "coap" /* the default scheme for CoAP URIs */
/** well-known resources URI */
#define COAP_DEFAULT_URI_WELLKNOWN ".well-known/core"
/* CoAP message types */
/**
* CoAP PDU message type definitions
*/
typedef enum coap_pdu_type_t {
COAP_MESSAGE_CON, /* 0 confirmable message (requires ACK/RST) */
COAP_MESSAGE_NON, /* 1 non-confirmable message (one-shot message) */
COAP_MESSAGE_ACK, /* 2 used to acknowledge confirmable messages */
COAP_MESSAGE_RST /* 3 indicates error in received messages */
} coap_pdu_type_t;
/**
* CoAP PDU Request methods
*/
typedef enum coap_request_t {
COAP_REQUEST_GET = 1,
COAP_REQUEST_POST, /* 2 */
COAP_REQUEST_PUT, /* 3 */
COAP_REQUEST_DELETE, /* 4 */
COAP_REQUEST_FETCH, /* 5 RFC 8132 */
COAP_REQUEST_PATCH, /* 6 RFC 8132 */
COAP_REQUEST_IPATCH, /* 7 RFC 8132 */
} coap_request_t;
/*
* CoAP option numbers (be sure to update coap_option_check_critical() and
* coap_add_option() when adding options
*/
/*
* The C, U, and N flags indicate the properties
* Critical, Unsafe, and NoCacheKey, respectively.
* If U is set, then N has no meaning as per
* https://tools.ietf.org/html/rfc7252#section-5.10
* and is set to a -.
*
* Separately, R is for the options that can be repeated
*
* The least significant byte of the option is set as followed
* as per https://tools.ietf.org/html/rfc7252#section-5.4.6
*
* 0 1 2 3 4 5 6 7
* --+---+---+---+---+---+---+---+
* | NoCacheKey| U | C |
* --+---+---+---+---+---+---+---+
*
* https://tools.ietf.org/html/rfc8613#section-4 goes on to define E, I and U
* properties Encrypted and Integrity Protected, Integrity Protected Only, and
* Unprotected respectively. Integrity Protected Only is not currently used.
*
* An Option is tagged with CUNREIU with any of the letters replaced with _ if
* not set, or - for N if U is set (see above) for aiding understanding of the
* Option.
*/
#define COAP_OPTION_IF_MATCH 1 /* C__RE__, opaque, 0-8 B, RFC7252 */
#define COAP_OPTION_URI_HOST 3 /* CU-___U, String, 1-255 B, RFC7252 */
#define COAP_OPTION_ETAG 4 /* ___RE__, opaque, 1-8 B, RFC7252 */
#define COAP_OPTION_IF_NONE_MATCH 5 /* C___E__, empty, 0 B, RFC7252 */
#define COAP_OPTION_OBSERVE 6 /* _U-_E_U, empty/uint,0/0-3 B, RFC7641 */
#define COAP_OPTION_URI_PORT 7 /* CU-___U, uint, 0-2 B, RFC7252 */
#define COAP_OPTION_LOCATION_PATH 8 /* ___RE__, String, 0-255 B, RFC7252 */
#define COAP_OPTION_OSCORE 9 /* C_____U, *, 0-255 B, RFC8613 */
#define COAP_OPTION_URI_PATH 11 /* CU-RE__, String, 0-255 B, RFC7252 */
#define COAP_OPTION_CONTENT_FORMAT 12 /* ____E__, uint, 0-2 B, RFC7252 */
#define COAP_OPTION_CONTENT_TYPE COAP_OPTION_CONTENT_FORMAT
/* COAP_OPTION_MAXAGE default 60 seconds if not set */
#define COAP_OPTION_MAXAGE 14 /* _U-_E_U, uint, 0-4 B, RFC7252 */
#define COAP_OPTION_URI_QUERY 15 /* CU-RE__, String, 1-255 B, RFC7252 */
#define COAP_OPTION_HOP_LIMIT 16 /* ______U, uint, 1 B, RFC8768 */
#define COAP_OPTION_ACCEPT 17 /* C___E__, uint, 0-2 B, RFC7252 */
#define COAP_OPTION_LOCATION_QUERY 20 /* ___RE__, String, 0-255 B, RFC7252 */
#define COAP_OPTION_BLOCK2 23 /* CU-_E_U, uint, 0-3 B, RFC7959 */
#define COAP_OPTION_BLOCK1 27 /* CU-_E_U, uint, 0-3 B, RFC7959 */
#define COAP_OPTION_SIZE2 28 /* __N_E_U, uint, 0-4 B, RFC7959 */
#define COAP_OPTION_PROXY_URI 35 /* CU-___U, String, 1-1034 B, RFC7252 */
#define COAP_OPTION_PROXY_SCHEME 39 /* CU-___U, String, 1-255 B, RFC7252 */
#define COAP_OPTION_SIZE1 60 /* __N_E_U, uint, 0-4 B, RFC7252 */
#define COAP_OPTION_NORESPONSE 258 /* _U-_E_U, uint, 0-1 B, RFC7967 */
#define COAP_MAX_OPT 65535 /**< the highest option number we know */
/* CoAP result codes (HTTP-Code / 100 * 40 + HTTP-Code % 100) */
/* As of draft-ietf-core-coap-04, response codes are encoded to base
* 32, i.e. the three upper bits determine the response class while
* the remaining five fine-grained information specific to that class.
*/
#define COAP_RESPONSE_CODE(N) (((N)/100 << 5) | (N)%100)
/* Determines the class of response code C */
#define COAP_RESPONSE_CLASS(C) (((C) >> 5) & 0xFF)
#ifndef SHORT_ERROR_RESPONSE
/**
* Returns a human-readable response phrase for the specified CoAP response @p
* code. This function returns @c NULL if not found.
*
* @param code The response code for which the literal phrase should be
* retrieved.
*
* @return A zero-terminated string describing the error, or @c NULL if not
* found.
*/
const char *coap_response_phrase(unsigned char code);
#define COAP_ERROR_PHRASE_LENGTH 32 /**< maximum length of error phrase */
#else
#define coap_response_phrase(x) ((char *)NULL)
#define COAP_ERROR_PHRASE_LENGTH 0 /**< maximum length of error phrase */
#endif /* SHORT_ERROR_RESPONSE */
#define COAP_SIGNALING_CODE(N) (((N)/100 << 5) | (N)%100)
typedef enum coap_pdu_signaling_proto_t {
COAP_SIGNALING_CSM = COAP_SIGNALING_CODE(701),
COAP_SIGNALING_PING = COAP_SIGNALING_CODE(702),
COAP_SIGNALING_PONG = COAP_SIGNALING_CODE(703),
COAP_SIGNALING_RELEASE = COAP_SIGNALING_CODE(704),
COAP_SIGNALING_ABORT = COAP_SIGNALING_CODE(705),
} coap_pdu_signaling_proto_t;
/* Applies to COAP_SIGNALING_CSM */
#define COAP_SIGNALING_OPTION_MAX_MESSAGE_SIZE 2
#define COAP_SIGNALING_OPTION_BLOCK_WISE_TRANSFER 4
/* Applies to COAP_SIGNALING_PING / COAP_SIGNALING_PONG */
#define COAP_SIGNALING_OPTION_CUSTODY 2
/* Applies to COAP_SIGNALING_RELEASE */
#define COAP_SIGNALING_OPTION_ALTERNATIVE_ADDRESS 2
#define COAP_SIGNALING_OPTION_HOLD_OFF 4
/* Applies to COAP_SIGNALING_ABORT */
#define COAP_SIGNALING_OPTION_BAD_CSM_OPTION 2
/* CoAP media type encoding */
#define COAP_MEDIATYPE_TEXT_PLAIN 0 /* text/plain (UTF-8) */
#define COAP_MEDIATYPE_APPLICATION_LINK_FORMAT 40 /* application/link-format */
#define COAP_MEDIATYPE_APPLICATION_XML 41 /* application/xml */
#define COAP_MEDIATYPE_APPLICATION_OCTET_STREAM 42 /* application/octet-stream */
#define COAP_MEDIATYPE_APPLICATION_RDF_XML 43 /* application/rdf+xml */
#define COAP_MEDIATYPE_APPLICATION_EXI 47 /* application/exi */
#define COAP_MEDIATYPE_APPLICATION_JSON 50 /* application/json */
#define COAP_MEDIATYPE_APPLICATION_CBOR 60 /* application/cbor */
#define COAP_MEDIATYPE_APPLICATION_CWT 61 /* application/cwt, RFC 8392 */
/* Content formats from RFC 8152 */
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN 98 /* application/cose; cose-type="cose-sign" */
#define COAP_MEDIATYPE_APPLICATION_COSE_SIGN1 18 /* application/cose; cose-type="cose-sign1" */
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT 96 /* application/cose; cose-type="cose-encrypt" */
#define COAP_MEDIATYPE_APPLICATION_COSE_ENCRYPT0 16 /* application/cose; cose-type="cose-encrypt0" */
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC 97 /* application/cose; cose-type="cose-mac" */
#define COAP_MEDIATYPE_APPLICATION_COSE_MAC0 17 /* application/cose; cose-type="cose-mac0" */
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY 101 /* application/cose-key */
#define COAP_MEDIATYPE_APPLICATION_COSE_KEY_SET 102 /* application/cose-key-set */
/* Content formats from RFC 8428 */
#define COAP_MEDIATYPE_APPLICATION_SENML_JSON 110 /* application/senml+json */
#define COAP_MEDIATYPE_APPLICATION_SENSML_JSON 111 /* application/sensml+json */
#define COAP_MEDIATYPE_APPLICATION_SENML_CBOR 112 /* application/senml+cbor */
#define COAP_MEDIATYPE_APPLICATION_SENSML_CBOR 113 /* application/sensml+cbor */
#define COAP_MEDIATYPE_APPLICATION_SENML_EXI 114 /* application/senml-exi */
#define COAP_MEDIATYPE_APPLICATION_SENSML_EXI 115 /* application/sensml-exi */
#define COAP_MEDIATYPE_APPLICATION_SENML_XML 310 /* application/senml+xml */
#define COAP_MEDIATYPE_APPLICATION_SENSML_XML 311 /* application/sensml+xml */
/* Content formats from RFC 8782 */
#define COAP_MEDIATYPE_APPLICATION_DOTS_CBOR 271 /* application/dots+cbor */
/* Note that identifiers for registered media types are in the range 0-65535. We
* use an unallocated type here and hope for the best. */
#define COAP_MEDIATYPE_ANY 0xff /* any media type */
/**
* coap_mid_t is used to store the CoAP Message ID of a CoAP PDU.
* Valid message ids are 0 to 2^16. Negative values are error codes.
*/
typedef int coap_mid_t;
/** Indicates an invalid message id. */
#define COAP_INVALID_MID -1
/**
* Indicates an invalid message id.
* @deprecated Use COAP_INVALID_MID instead.
*/
#define COAP_INVALID_TID COAP_INVALID_MID
/**
* @deprecated Use coap_optlist_t instead.
*
* Structures for more convenient handling of options. (To be used with ordered
* coap_list_t.) The option's data will be added to the end of the coap_option
* structure (see macro COAP_OPTION_DATA).
*/
COAP_DEPRECATED typedef struct {
uint16_t key; /* the option key (no delta coding) */
unsigned int length;
} coap_option;
#define COAP_OPTION_KEY(option) (option).key
#define COAP_OPTION_LENGTH(option) (option).length
#define COAP_OPTION_DATA(option) ((unsigned char *)&(option) + sizeof(coap_option))
#ifdef WITH_LWIP
/**
* Creates a CoAP PDU from an lwIP @p pbuf, whose reference is passed on to this
* function.
*
* The pbuf is checked for being contiguous, and for having only one reference.
* The reference is stored in the PDU and will be freed when the PDU is freed.
*
* (For now, these are fatal errors; in future, a new pbuf might be allocated,
* the data copied and the passed pbuf freed).
*
* This behaves like coap_pdu_init(0, 0, 0, pbuf->tot_len), and afterwards
* copying the contents of the pbuf to the pdu.
*
* @return A pointer to the new PDU object or @c NULL on error.
*/
coap_pdu_t * coap_pdu_from_pbuf(struct pbuf *pbuf);
#endif
/**
* CoAP protocol types
*/
typedef enum coap_proto_t {
COAP_PROTO_NONE = 0,
COAP_PROTO_UDP,
COAP_PROTO_DTLS,
COAP_PROTO_TCP,
COAP_PROTO_TLS,
} coap_proto_t;
/**
* Set of codes available for a PDU.
*/
typedef enum coap_pdu_code_t {
COAP_EMPTY_CODE = 0,
COAP_REQUEST_CODE_GET = COAP_REQUEST_GET,
COAP_REQUEST_CODE_POST = COAP_REQUEST_POST,
COAP_REQUEST_CODE_PUT = COAP_REQUEST_PUT,
COAP_REQUEST_CODE_DELETE = COAP_REQUEST_DELETE,
COAP_REQUEST_CODE_FETCH = COAP_REQUEST_FETCH,
COAP_REQUEST_CODE_PATCH = COAP_REQUEST_PATCH,
COAP_REQUEST_CODE_IPATCH = COAP_REQUEST_IPATCH,
COAP_RESPONSE_CODE_OK = COAP_RESPONSE_CODE(200),
COAP_RESPONSE_CODE_CREATED = COAP_RESPONSE_CODE(201),
COAP_RESPONSE_CODE_DELETED = COAP_RESPONSE_CODE(202),
COAP_RESPONSE_CODE_VALID = COAP_RESPONSE_CODE(203),
COAP_RESPONSE_CODE_CHANGED = COAP_RESPONSE_CODE(204),
COAP_RESPONSE_CODE_CONTENT = COAP_RESPONSE_CODE(205),
COAP_RESPONSE_CODE_CONTINUE = COAP_RESPONSE_CODE(231),
COAP_RESPONSE_CODE_BAD_REQUEST = COAP_RESPONSE_CODE(400),
COAP_RESPONSE_CODE_UNAUTHORIZED = COAP_RESPONSE_CODE(401),
COAP_RESPONSE_CODE_BAD_OPTION = COAP_RESPONSE_CODE(402),
COAP_RESPONSE_CODE_FORBIDDEN = COAP_RESPONSE_CODE(403),
COAP_RESPONSE_CODE_NOT_FOUND = COAP_RESPONSE_CODE(404),
COAP_RESPONSE_CODE_NOT_ALLOWED = COAP_RESPONSE_CODE(405),
COAP_RESPONSE_CODE_NOT_ACCEPTABLE = COAP_RESPONSE_CODE(406),
COAP_RESPONSE_CODE_INCOMPLETE = COAP_RESPONSE_CODE(408),
COAP_RESPONSE_CODE_CONFLICT = COAP_RESPONSE_CODE(409),
COAP_RESPONSE_CODE_PRECONDITION_FAILED = COAP_RESPONSE_CODE(412),
COAP_RESPONSE_CODE_REQUEST_TOO_LARGE = COAP_RESPONSE_CODE(413),
COAP_RESPONSE_CODE_UNSUPPORTED_CONTENT_FORMAT = COAP_RESPONSE_CODE(415),
COAP_RESPONSE_CODE_UNPROCESSABLE = COAP_RESPONSE_CODE(422),
COAP_RESPONSE_CODE_TOO_MANY_REQUESTS = COAP_RESPONSE_CODE(429),
COAP_RESPONSE_CODE_INTERNAL_ERROR = COAP_RESPONSE_CODE(500),
COAP_RESPONSE_CODE_NOT_IMPLEMENTED = COAP_RESPONSE_CODE(501),
COAP_RESPONSE_CODE_BAD_GATEWAY = COAP_RESPONSE_CODE(502),
COAP_RESPONSE_CODE_SERVICE_UNAVAILABLE = COAP_RESPONSE_CODE(503),
COAP_RESPONSE_CODE_GATEWAY_TIMEOUT = COAP_RESPONSE_CODE(504),
COAP_RESPONSE_CODE_PROXYING_NOT_SUPPORTED = COAP_RESPONSE_CODE(505),
COAP_RESPONSE_CODE_HOP_LIMIT_REACHED = COAP_RESPONSE_CODE(508),
COAP_SIGNALING_CODE_CSM = COAP_SIGNALING_CSM,
COAP_SIGNALING_CODE_PING = COAP_SIGNALING_PING,
COAP_SIGNALING_CODE_PONG = COAP_SIGNALING_PONG,
COAP_SIGNALING_CODE_RELEASE = COAP_SIGNALING_RELEASE,
COAP_SIGNALING_CODE_ABORT = COAP_SIGNALING_ABORT
} coap_pdu_code_t;
/**
* Creates a new CoAP PDU with at least enough storage space for the given
* @p size maximum message size. The function returns a pointer to the
* node coap_pdu_t object on success, or @c NULL on error. The storage allocated
* for the result must be released with coap_delete_pdu() if coap_send()
* is not called.
*
* @param type The type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON,
* COAP_MESSAGE_ACK, COAP_MESSAGE_RST).
* @param code The message code of the PDU.
* @param mid The message id to set or 0 if unknown / not applicable.
* @param size The maximum allowed number of byte for the message.
* @return A pointer to the new PDU object or @c NULL on error.
*/
coap_pdu_t *coap_pdu_init(coap_pdu_type_t type, coap_pdu_code_t code,
coap_mid_t mid, size_t size);
/**
* Creates a new CoAP PDU.
*
* @param type The type of the PDU (one of COAP_MESSAGE_CON, COAP_MESSAGE_NON,
* COAP_MESSAGE_ACK, COAP_MESSAGE_RST).
* @param code The message code of the PDU.
* @param session The session that will be using this PDU
*
* @return The skeletal PDU or @c NULL if failure.
*/
coap_pdu_t *coap_new_pdu(coap_pdu_type_t type, coap_pdu_code_t code,
coap_session_t *session);
/**
* Dispose of an CoAP PDU and frees associated storage.
* Not that in general you should not call this function directly.
* When a PDU is sent with coap_send(), coap_delete_pdu() will be called
* automatically for you.
*
* @param pdu The PDU for free off.
*/
void coap_delete_pdu(coap_pdu_t *pdu);
/**
* Duplicate an existing PDU. Specific options can be ignored and not copied
* across. The PDU data payload is not copied across.
*
* @param old_pdu The PDU to duplicate
* @param session The session that will be using this PDU.
* @param token_length The length of the token to use in this duplicated PDU.
* @param token The token to use in this duplicated PDU.
* @param drop_options A list of options not to copy into the duplicated PDU.
* If @c NULL, then all options are copied across.
*
* @return The duplicated PDU or @c NULL if failure.
*/
coap_pdu_t *
coap_pdu_duplicate(const coap_pdu_t *old_pdu,
coap_session_t *session,
size_t token_length,
const uint8_t *token,
coap_opt_filter_t *drop_options);
/**
* Adds token of length @p len to @p pdu.
* Adding the token destroys any following contents of the pdu. Hence options
* and data must be added after coap_add_token() has been called. In @p pdu,
* length is set to @p len + @c 4, and max_delta is set to @c 0. This function
* returns @c 0 on error or a value greater than zero on success.
*
* @param pdu The PDU where the token is to be added.
* @param len The length of the new token.
* @param data The token to add.
*
* @return A value greater than zero on success, or @c 0 on error.
*/
int coap_add_token(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
/**
* Adds option of given number to pdu that is passed as first
* parameter.
* coap_add_option() destroys the PDU's data, so coap_add_data() must be called
* after all options have been added. As coap_add_token() destroys the options
* following the token, the token must be added before coap_add_option() is
* called. This function returns the number of bytes written or @c 0 on error.
*
* Note: Where possible, the option data needs to be stripped of leading zeros
* (big endian) to reduce the amount of data needed in the PDU, as well as in
* some cases the maximum data size of an opton can be exceeded if not stripped
* and hence be illegal. This is done by using coap_encode_var_safe() or
* coap_encode_var_safe8().
*
* @param pdu The PDU where the option is to be added.
* @param number The number of the new option.
* @param len The length of the new option.
* @param data The data of the new option.
*
* @return The overall length of the option or @c 0 on failure.
*/
size_t coap_add_option(coap_pdu_t *pdu,
coap_option_num_t number,
size_t len,
const uint8_t *data);
/**
* Adds given data to the pdu that is passed as first parameter. Note that the
* PDU's data is destroyed by coap_add_option(). coap_add_data() must be called
* only once per PDU, otherwise the result is undefined.
*
* @param pdu The PDU where the data is to be added.
* @param len The length of the data.
* @param data The data to add.
*
* @return @c 1 if success, else @c 0 if failure.
*/
int coap_add_data(coap_pdu_t *pdu,
size_t len,
const uint8_t *data);
/**
* Adds given data to the pdu that is passed as first parameter but does not
* copy it. Note that the PDU's data is destroyed by coap_add_option().
* coap_add_data() must be have been called once for this PDU, otherwise the
* result is undefined.
* The actual data must be copied at the returned location.
*
* @param pdu The PDU where the data is to be added.
* @param len The length of the data.
*
* @return Where to copy the data of len to, or @c NULL is error.
*/
uint8_t *coap_add_data_after(coap_pdu_t *pdu, size_t len);
/**
* Retrieves the length and data pointer of specified PDU. Returns 0 on error or
* 1 if *len and *data have correct values. Note that these values are destroyed
* with the pdu.
*
* @param pdu The specified PDU.
* @param len Returns the length of the current data
* @param data Returns the ptr to the current data
*
* @return @c 1 if len and data are correctly filled in, else
* @c 0 if there is no data.
*/
int coap_get_data(const coap_pdu_t *pdu,
size_t *len,
const uint8_t **data);
/**
* Retrieves the data from a PDU, with support for large bodies of data that
* spans multiple PDUs.
*
* Note: The data pointed to on return is destroyed when the PDU is destroyed.
*
* @param pdu The specified PDU.
* @param len Returns the length of the current data
* @param data Returns the ptr to the current data
* @param offset Returns the offset of the current data from the start of the
* body comprising of many blocks (RFC7959)
* @param total Returns the total size of the body.
* If offset + length < total, then there is more data to follow.
*
* @return @c 1 if len, data, offset and total are correctly filled in, else
* @c 0 if there is no data.
*/
int coap_get_data_large(const coap_pdu_t *pdu,
size_t *len,
const uint8_t **data,
size_t *offset,
size_t *total);
/**
* Gets the PDU code associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The PDU code.
*/
coap_pdu_code_t coap_pdu_get_code(const coap_pdu_t *pdu);
/**
* Sets the PDU code in the @p pdu.
*
* @param pdu The PDU object.
* @param code The code to set in the PDU.
*/
void coap_pdu_set_code(coap_pdu_t *pdu, coap_pdu_code_t code);
/**
* Gets the PDU type associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The PDU type.
*/
coap_pdu_type_t coap_pdu_get_type(const coap_pdu_t *pdu);
/**
* Sets the PDU type in the @p pdu.
*
* @param pdu The PDU object.
* @param type The type to set for the PDU.
*/
void coap_pdu_set_type(coap_pdu_t *pdu, coap_pdu_type_t type);
/**
* Gets the token associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The token information.
*/
coap_bin_const_t coap_pdu_get_token(const coap_pdu_t *pdu);
/**
* Gets the message id associated with @p pdu.
*
* @param pdu The PDU object.
*
* @return The message id.
*/
coap_mid_t coap_pdu_get_mid(const coap_pdu_t *pdu);
/**
* Sets the message id in the @p pdu.
*
* @param pdu The PDU object.
* @param mid The message id value to set in the PDU.
*
*/
void coap_pdu_set_mid(coap_pdu_t *pdu, coap_mid_t mid);
/** @} */
#endif /* COAP_PDU_H_ */

View File

@ -1,7 +1,9 @@
/*
* resource.h -- generic resource handling
*
* Copyright (C) 2010,2011,2014,2015 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2010,2011,2014-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
@ -15,100 +17,62 @@
#ifndef COAP_RESOURCE_H_
#define COAP_RESOURCE_H_
# include <assert.h>
#ifndef COAP_RESOURCE_CHECK_TIME
/** The interval in seconds to check if resources have changed. */
#define COAP_RESOURCE_CHECK_TIME 2
#endif /* COAP_RESOURCE_CHECK_TIME */
#include "uthash.h"
#include "async.h"
#include "block.h"
#include "str.h"
#include "pdu.h"
#include "net.h"
#include "subscribe.h"
/**
* Definition of message handler function (@sa coap_resource_t).
* @defgroup coap_resource Resource Configuraton
* API functions for setting up resources
* @{
*/
/**
* Definition of message handler function
*/
typedef void (*coap_method_handler_t)
(coap_context_t *,
struct coap_resource_t *,
(coap_resource_t *,
coap_session_t *,
coap_pdu_t *,
coap_binary_t * /* token */,
coap_string_t * /* query string */,
const coap_pdu_t * /* request */,
const coap_string_t * /* query string */,
coap_pdu_t * /* response */);
#define COAP_ATTR_FLAGS_RELEASE_NAME 0x1
#define COAP_ATTR_FLAGS_RELEASE_VALUE 0x2
typedef struct coap_attr_t {
struct coap_attr_t *next;
coap_str_const_t *name;
coap_str_const_t *value;
int flags;
} coap_attr_t;
/** The URI passed to coap_resource_init() is free'd by coap_delete_resource(). */
#define COAP_RESOURCE_FLAGS_RELEASE_URI 0x1
/**
* Notifications will be sent non-confirmable by default. RFC 7641 Section 4.5
* https://tools.ietf.org/html/rfc7641#section-4.5
* Libcoap will always send every fifth packet as confirmable.
*/
#define COAP_RESOURCE_FLAGS_NOTIFY_NON 0x0
/**
* Notifications will be sent confirmable by default. RFC 7641 Section 4.5
* Notifications will be sent confirmable. RFC 7641 Section 4.5
* https://tools.ietf.org/html/rfc7641#section-4.5
*/
#define COAP_RESOURCE_FLAGS_NOTIFY_CON 0x2
typedef struct coap_resource_t {
unsigned int dirty:1; /**< set to 1 if resource has changed */
unsigned int partiallydirty:1; /**< set to 1 if some subscribers have not yet
* been notified of the last change */
unsigned int observable:1; /**< can be observed */
unsigned int cacheable:1; /**< can be cached */
unsigned int is_unknown:1; /**< resource created for unknown handler */
/**
* Used to store handlers for the seven coap methods @c GET, @c POST, @c PUT,
* @c DELETE, @c FETCH, @c PATCH and @c IPATCH.
* coap_dispatch() will pass incoming requests to the handler
* that corresponds to its request method or generate a 4.05 response if no
* handler is available.
*/
coap_method_handler_t handler[7];
UT_hash_handle hh;
coap_attr_t *link_attr; /**< attributes to be included with the link format */
coap_subscription_t *subscribers; /**< list of observers for this resource */
/**
* Request URI Path for this resource. This field will point into static
* or allocated memory which must remain there for the duration of the
* resource.
*/
coap_str_const_t *uri_path; /**< the key used for hash lookup for this resource */
int flags;
/**
* The next value for the Observe option. This field must be increased each
* time the resource changes. Only the lower 24 bits are sent.
*/
unsigned int observe;
/**
* This pointer is under user control. It can be used to store context for
* the coap handler.
*/
void *user_data;
} coap_resource_t;
/**
* Notifications will always be sent non-confirmable. This is in
* violation of RFC 7641 Section 4.5
* https://tools.ietf.org/html/rfc7641#section-4.5
* but required by the DOTS signal channel protocol which needs to operate in
* lossy DDoS attack environments.
* https://tools.ietf.org/html/rfc8782#section-4.4.2.1
*/
#define COAP_RESOURCE_FLAGS_NOTIFY_NON_ALWAYS 0x4
/**
* Creates a new resource object and initializes the link field to the string
@ -173,6 +137,45 @@ coap_resource_t *coap_resource_init(coap_str_const_t *uri_path,
*/
coap_resource_t *coap_resource_unknown_init(coap_method_handler_t put_handler);
/**
* Creates a new resource object for handling proxy URIs.
* This function returns the new coap_resource_t object.
*
* Note: There can only be one proxy resource handler per context - attaching
* a new one overrides the previous definition.
*
* @param handler The PUT/POST/GET etc. handler that handles all request types.
* @param host_name_count The number of provided host_name_list entries. A
* minimum of 1 must be provided.
* @param host_name_list Array of depth host_name_count names that this proxy
* is known by.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_resource_t *coap_resource_proxy_uri_init(coap_method_handler_t handler,
size_t host_name_count, const char *host_name_list[]);
/**
* Returns the resource identified by the unique string @p uri_path. If no
* resource was found, this function returns @c NULL.
*
* @param context The context to look for this resource.
* @param uri_path The unique string uri of the resource.
*
* @return A pointer to the resource or @c NULL if not found.
*/
coap_resource_t *coap_get_resource_from_uri_path(coap_context_t *context,
coap_str_const_t *uri_path);
/**
* Get the uri_path from a @p resource.
*
* @param resource The CoAP resource to check.
*
* @return The uri_path if it exists or @c NULL otherwise.
*/
coap_str_const_t* coap_resource_get_uri_path(coap_resource_t *resource);
/**
* Sets the notification message type of resource @p resource to given
* @p mode
@ -181,38 +184,43 @@ coap_resource_t *coap_resource_unknown_init(coap_method_handler_t put_handler);
* @param mode Must be one of @c COAP_RESOURCE_FLAGS_NOTIFY_NON
* or @c COAP_RESOURCE_FLAGS_NOTIFY_CON.
*/
COAP_STATIC_INLINE void
coap_resource_set_mode(coap_resource_t *resource, int mode) {
resource->flags = (resource->flags &
~(COAP_RESOURCE_FLAGS_NOTIFY_CON|COAP_RESOURCE_FLAGS_NOTIFY_NON)) |
(mode & (COAP_RESOURCE_FLAGS_NOTIFY_CON|COAP_RESOURCE_FLAGS_NOTIFY_NON));
}
void coap_resource_set_mode(coap_resource_t *resource, int mode);
/**
* Sets the user_data. The user_data is exclusively used by the library-user
* and can be used as context in the handler functions.
* and can be used as user defined context in the handler functions.
*
* @param r Resource to attach the data to
* @param data Data to attach to the user_data field. This pointer is only used for
* storage, the data remains under user control
* @param resource Resource to attach the data to
* @param data Data to attach to the user_data field. This pointer is
* only used for storage, the data remains under user control
*/
COAP_STATIC_INLINE void
coap_resource_set_userdata(coap_resource_t *r, void *data) {
r->user_data = data;
}
void coap_resource_set_userdata(coap_resource_t *resource, void *data);
/**
* Gets the user_data. The user_data is exclusively used by the library-user
* and can be used as context in the handler functions.
*
* @param r Resource to retrieve the user_darta from
* @param resource Resource to retrieve the user_data from
*
* @return The user_data pointer
*/
COAP_STATIC_INLINE void *
coap_resource_get_userdata(coap_resource_t *r) {
return r->user_data;
}
void *coap_resource_get_userdata(coap_resource_t *resource);
/**
* Definition of release resource user_data callback function
*/
typedef void (*coap_resource_release_userdata_handler_t)(void *user_data);
/**
* Defines the context wide callback to use to when the resource is deleted
* to release the data held in the resource's user_data.
*
* @param context The context to associate the release callback with
* @param callback The callback to invoke when the resource is deleted or NULL
*
*/
void coap_resource_release_userdata_handler(coap_context_t *context,
coap_resource_release_userdata_handler_t callback);
/**
* Registers the given @p resource for @p context. The resource must have been
@ -237,11 +245,16 @@ void coap_add_resource(coap_context_t *context, coap_resource_t *resource);
int coap_delete_resource(coap_context_t *context, coap_resource_t *resource);
/**
* Deletes all resources from given @p context and frees their storage.
* Registers the specified @p handler as message handler for the request type @p
* method
*
* @param context The CoAP context with the resources to be deleted.
* @param resource The resource for which the handler shall be registered.
* @param method The CoAP request method to handle.
* @param handler The handler to register with @p resource.
*/
void coap_delete_all_resources(coap_context_t *context);
void coap_register_handler(coap_resource_t *resource,
coap_request_t method,
coap_method_handler_t handler);
/**
* Registers a new attribute with the given @p resource. As the
@ -291,13 +304,13 @@ coap_attr_t *coap_find_attr(coap_resource_t *resource,
coap_str_const_t *name);
/**
* Deletes an attribute.
* Note: This is for internal use only, as it is not deleted from its chain.
* Returns @p attribute's value.
*
* @param attr Pointer to a previously created attribute.
* @param attribute Pointer to attribute.
*
* @return Attribute's value or @c NULL.
*/
void coap_delete_attr(coap_attr_t *attr);
coap_str_const_t *coap_attr_get_value(coap_attr_t *attribute);
/**
* Status word to encode the result of conditional print or copy operations such
@ -341,17 +354,7 @@ coap_print_status_t coap_print_link(const coap_resource_t *resource,
size_t *len,
size_t *offset);
/**
* Registers the specified @p handler as message handler for the request type @p
* method
*
* @param resource The resource for which the handler shall be registered.
* @param method The CoAP request method to handle.
* @param handler The handler to register with @p resource.
*/
void coap_register_handler(coap_resource_t *resource,
unsigned char method,
coap_method_handler_t handler);
/** @} */
/**
* Returns the resource identified by the unique string @p uri_path. If no
@ -365,155 +368,6 @@ void coap_register_handler(coap_resource_t *resource,
coap_resource_t *coap_get_resource_from_uri_path(coap_context_t *context,
coap_str_const_t *uri_path);
/**
* @addtogroup observe
*/
/**
* Adds the specified peer as observer for @p resource. The subscription is
* identified by the given @p token. This function returns the registered
* subscription information if the @p observer has been added, or @c NULL on
* error.
*
* @param resource The observed resource.
* @param session The observer's session
* @param token The token that identifies this subscription.
* @param query The query string, if any. subscription will
take ownership of the string.
* @param has_block2 If Option Block2 defined.
* @param block2 Contents of Block2 if Block 2 defined.
* @return A pointer to the added/updated subscription
* information or @c NULL on error.
*/
coap_subscription_t *coap_add_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token,
coap_string_t *query,
int has_block2,
coap_block_t block2);
/**
* Returns a subscription object for given @p peer.
*
* @param resource The observed resource.
* @param session The observer's session
* @param token The token that identifies this subscription or @c NULL for
* any token.
* @return A valid subscription if exists or @c NULL otherwise.
*/
coap_subscription_t *coap_find_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token);
/**
* Marks an observer as alive.
*
* @param context The CoAP context to use.
* @param session The observer's session
* @param token The corresponding token that has been used for the
* subscription.
*/
void coap_touch_observer(coap_context_t *context,
coap_session_t *session,
const coap_binary_t *token);
/**
* Removes any subscription for @p observer from @p resource and releases the
* allocated storage. The result is @c 1 if an observation relationship with @p
* observer and @p token existed, @c 0 otherwise.
*
* @param resource The observed resource.
* @param session The observer's session.
* @param token The token that identifies this subscription or @c NULL for
* any token.
* @return @c 1 if the observer has been deleted, @c 0 otherwise.
*/
int coap_delete_observer(coap_resource_t *resource,
coap_session_t *session,
const coap_binary_t *token);
/**
* Removes any subscription for @p session and releases the allocated storage.
*
* @param context The CoAP context to use.
* @param session The observer's session.
*/
void coap_delete_observers(coap_context_t *context, coap_session_t *session);
/**
* Checks for all known resources, if they are dirty and notifies subscribed
* observers.
*/
void coap_check_notify(coap_context_t *context);
#define RESOURCES_ADD(r, obj) \
HASH_ADD(hh, (r), uri_path->s[0], (obj)->uri_path->length, (obj))
#define RESOURCES_DELETE(r, obj) \
HASH_DELETE(hh, (r), (obj))
#define RESOURCES_ITER(r,tmp) \
coap_resource_t *tmp, *rtmp; \
HASH_ITER(hh, (r), tmp, rtmp)
#define RESOURCES_FIND(r, k, res) { \
HASH_FIND(hh, (r), (k)->s, (k)->length, (res)); \
}
/** @} */
coap_print_status_t coap_print_wellknown(coap_context_t *,
unsigned char *,
size_t *, size_t,
coap_opt_t *);
void
coap_handle_failed_notify(coap_context_t *,
coap_session_t *,
const coap_binary_t *);
/**
* Set whether a @p resource is observable. If the resource is observable
* and the client has set the COAP_OPTION_OBSERVE in a request packet, then
* whenever the state of the resource changes (a call to
* coap_resource_trigger_observe()), an Observer response will get sent.
*
* @param resource The CoAP resource to use.
* @param mode @c 1 if Observable is to be set, @c 0 otherwise.
*
*/
COAP_STATIC_INLINE void
coap_resource_set_get_observable(coap_resource_t *resource, int mode) {
resource->observable = mode ? 1 : 0;
}
/**
* Initiate the sending of an Observe packet for all observers of @p resource,
* optionally matching @p query if not NULL
*
* @param resource The CoAP resource to use.
* @param query The Query to match against or NULL
*
* @return @c 1 if the Observe has been triggered, @c 0 otherwise.
*/
int
coap_resource_notify_observers(coap_resource_t *resource,
const coap_string_t *query);
/**
* Get the UriPath from a @p resource.
*
* @param resource The CoAP resource to check.
*
* @return The UriPath if it exists or @c NULL otherwise.
*/
COAP_STATIC_INLINE coap_str_const_t*
coap_resource_get_uri_path(coap_resource_t *resource) {
if (resource)
return resource->uri_path;
return NULL;
}
/**
* @deprecated use coap_resource_notify_observers() instead.
*/

View File

@ -0,0 +1,204 @@
/*
* str.h -- strings to be used in the CoAP library
*
* Copyright (C) 2010-2011 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_STR_H_
#define COAP_STR_H_
#include <string.h>
/**
* @defgroup string String handling support
* API functions for handling strings and binary data
* @{
*/
/*
* Note: string and binary use equivalent objects.
* string is likely to contain readable textual information, binary will not.
*/
/**
* CoAP string data definition
*/
typedef struct coap_string_t {
size_t length; /**< length of string */
uint8_t *s; /**< string data */
} coap_string_t;
/**
* CoAP string data definition with const data
*/
typedef struct coap_str_const_t {
size_t length; /**< length of string */
const uint8_t *s; /**< read-only string data */
} coap_str_const_t;
#define COAP_SET_STR(st,l,v) { (st)->length = (l), (st)->s = (v); }
/**
* CoAP binary data definition
*/
typedef struct coap_binary_t {
size_t length; /**< length of binary data */
uint8_t *s; /**< binary data */
} coap_binary_t;
/**
* CoAP binary data definition with const data
*/
typedef struct coap_bin_const_t {
size_t length; /**< length of binary data */
const uint8_t *s; /**< read-only binary data */
} coap_bin_const_t;
/**
* Returns a new string object with at least size+1 bytes storage allocated.
* It is the responsibility of the caller to fill in all the appropriate
* information.
* The string must be released using coap_delete_string().
*
* @param size The size to allocate for the string data.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_string_t *coap_new_string(size_t size);
/**
* Deletes the given string and releases any memory allocated.
*
* @param string The string to free off.
*/
void coap_delete_string(coap_string_t *string);
/**
* Returns a new const string object with at least size+1 bytes storage
* allocated, and the provided data copied into the string object.
* The string must be released using coap_delete_str_const().
*
* @param data The data to put in the new string object.
* @param size The size to allocate for the binary string data.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_str_const_t *coap_new_str_const(const uint8_t *data, size_t size);
/**
* Deletes the given const string and releases any memory allocated.
*
* @param string The string to free off.
*/
void coap_delete_str_const(coap_str_const_t *string);
/**
* Returns a new binary object with at least size bytes storage allocated.
* It is the responsibility of the caller to fill in all the appropriate
* information.
* The coap_binary_t object must be released using coap_delete_binary().
*
* @param size The size to allocate for the binary data.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_binary_t *coap_new_binary(size_t size);
/**
* Deletes the given coap_binary_t object and releases any memory allocated.
*
* @param binary The coap_binary_t object to free off.
*/
void coap_delete_binary(coap_binary_t *binary);
/**
* Resizes the given coap_binary_t object.
* It is the responsibility of the caller to fill in all the appropriate
* additional information.
*
* Note: If there is an error, @p binary will separately need to be released by
* coap_delete_binary().
*
* @param binary The coap_binary_t object to resize.
* @param new_size The new size to allocate for the binary data.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_binary_t *coap_resize_binary(coap_binary_t *binary, size_t new_size);
/**
* Take the specified byte array (text) and create a coap_bin_const_t *
* Returns a new const binary object with at least size bytes storage
* allocated, and the provided data copied into the binary object.
* The binary data must be released using coap_delete_bin_const().
*
* @param data The data to put in the new string object.
* @param size The size to allocate for the binary data.
*
* @return A pointer to the new object or @c NULL on error.
*/
coap_bin_const_t *coap_new_bin_const(const uint8_t *data, size_t size);
/**
* Deletes the given const binary data and releases any memory allocated.
*
* @param binary The binary data to free off.
*/
void coap_delete_bin_const(coap_bin_const_t *binary);
#ifndef COAP_MAX_STR_CONST_FUNC
#define COAP_MAX_STR_CONST_FUNC 2
#endif /* COAP_MAX_STR_CONST_FUNC */
/**
* Take the specified byte array (text) and create a coap_str_const_t *
*
* Note: the array is 2 deep as there are up to two callings of
* coap_make_str_const in a function call. e.g. coap_add_attr().
* Caution: If there are local variable assignments, these will cycle around
* the var[COAP_MAX_STR_CONST_FUNC] set. No current examples do this.
*
* @param string The const string to convert to a coap_str_const_t *
*
* @return A pointer to one of two static variables containing the
* coap_str_const_t * result
*/
coap_str_const_t *coap_make_str_const(const char *string);
/**
* Compares the two strings for equality
*
* @param string1 The first string.
* @param string2 The second string.
*
* @return @c 1 if the strings are equal
* @c 0 otherwise.
*/
#define coap_string_equal(string1,string2) \
((string1)->length == (string2)->length && ((string1)->length == 0 || \
((string1)->s && (string2)->s && \
memcmp((string1)->s, (string2)->s, (string1)->length) == 0)))
/**
* Compares the two binary data for equality
*
* @param binary1 The first binary data.
* @param binary2 The second binary data.
*
* @return @c 1 if the binary data is equal
* @c 0 otherwise.
*/
#define coap_binary_equal(binary1,binary2) \
((binary1)->length == (binary2)->length && ((binary1)->length == 0 || \
((binary1)->s && (binary2)->s && \
memcmp((binary1)->s, (binary2)->s, (binary1)->length) == 0)))
/** @} */
#endif /* COAP_STR_H_ */

View File

@ -0,0 +1,68 @@
/*
* subscribe.h -- subscription handling for CoAP
* see RFC7641
*
* Copyright (C) 2010-2012,2014-2021 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
/**
* @file subscribe.h
* @brief Defines the application visible subscribe information
*/
#ifndef COAP_SUBSCRIBE_H_
#define COAP_SUBSCRIBE_H_
/**
* @defgroup observe Resource Observation
* API functions for interfacing with the observe handling (RFC7641)
* @{
*/
/**
* The value COAP_OBSERVE_ESTABLISH in a GET/FETCH request option
* COAP_OPTION_OBSERVE indicates a new observe relationship for (sender
* address, token) is requested.
*/
#define COAP_OBSERVE_ESTABLISH 0
/**
* The value COAP_OBSERVE_CANCEL in a GET/FETCH request option
* COAP_OPTION_OBSERVE indicates that the observe relationship for (sender
* address, token) must be cancelled.
*/
#define COAP_OBSERVE_CANCEL 1
/**
* Set whether a @p resource is observable. If the resource is observable
* and the client has set the COAP_OPTION_OBSERVE in a request packet, then
* whenever the state of the resource changes (a call to
* coap_resource_trigger_observe()), an Observer response will get sent.
*
* @param resource The CoAP resource to use.
* @param mode @c 1 if Observable is to be set, @c 0 otherwise.
*
*/
void coap_resource_set_get_observable(coap_resource_t *resource, int mode);
/**
* Initiate the sending of an Observe packet for all observers of @p resource,
* optionally matching @p query if not NULL
*
* @param resource The CoAP resource to use.
* @param query The Query to match against or NULL
*
* @return @c 1 if the Observe has been triggered, @c 0 otherwise.
*/
int
coap_resource_notify_observers(coap_resource_t *resource,
const coap_string_t *query);
/** @} */
#endif /* COAP_SUBSCRIBE_H_ */

View File

@ -1,7 +1,9 @@
/*
* uri.h -- helper functions for URI treatment
*
* Copyright (C) 2010-2011,2016 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2010-2020 Olaf Bergmann <bergmann@tzi.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
@ -13,25 +15,27 @@
#include <stdint.h>
#include "str.h"
struct coap_pdu_t;
/**
* The scheme specifiers. Secure schemes have an odd numeric value,
* others are even.
*/
enum coap_uri_scheme_t {
COAP_URI_SCHEME_COAP=0,
COAP_URI_SCHEME_COAPS=1,
COAP_URI_SCHEME_COAP_TCP=2,
COAP_URI_SCHEME_COAPS_TCP=3
};
typedef enum coap_uri_scheme_t {
COAP_URI_SCHEME_COAP = 0,
COAP_URI_SCHEME_COAPS, /* 1 */
COAP_URI_SCHEME_COAP_TCP, /* 2 */
COAP_URI_SCHEME_COAPS_TCP, /* 3 */
COAP_URI_SCHEME_HTTP, /* 4 Proxy-Uri only */
COAP_URI_SCHEME_HTTPS /* 5 Proxy-Uri only */
} coap_uri_scheme_t;
/** This mask can be used to check if a parsed URI scheme is secure. */
#define COAP_URI_SCHEME_SECURE_MASK 0x01
/**
* Representation of parsed URI. Components may be filled from a string with
* coap_split_uri() and can be used as input for option-creation functions.
* coap_split_uri() or coap_split_proxy_uri() and can be used as input for
* option-creation functions.
*/
typedef struct {
coap_str_const_t host; /**< host part of the URI */
@ -79,17 +83,36 @@ coap_uri_t *coap_clone_uri(const coap_uri_t *uri);
* Parses a given string into URI components. The identified syntactic
* components are stored in the result parameter @p uri. Optional URI
* components that are not specified will be set to { 0, 0 }, except for the
* port which is set to @c COAP_DEFAULT_PORT. This function returns @p 0 if
* parsing succeeded, a value less than zero otherwise.
* port which is set to the default port for the protocol. This function
* returns @p 0 if parsing succeeded, a value less than zero otherwise.
*
* @param str_var The string to split up.
* @param len The actual length of @p str_var
* @param uri The coap_uri_t object to store the result.
*
* @return @c 0 on success, or < 0 on error.
*
*/
int coap_split_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri);
/**
* Parses a given string into URI components. The identified syntactic
* components are stored in the result parameter @p uri. Optional URI
* components that are not specified will be set to { 0, 0 }, except for the
* port which is set to default port for the protocol. This function returns
* @p 0 if parsing succeeded, a value less than zero otherwise.
* Note: This function enforces that the given string is in Proxy-Uri format
* as well as supports different schema such as http.
*
* @param str_var The string to split up.
* @param len The actual length of @p str_var
* @param uri The coap_uri_t object to store the result.
*
* @return @c 0 on success, or < 0 on error.
*
*/
int coap_split_proxy_uri(const uint8_t *str_var, size_t len, coap_uri_t *uri);
/**
* Splits the given URI path into segments. Each segment is preceded
* by an option pseudo-header with delta-value 0 and the actual length
@ -131,16 +154,22 @@ int coap_split_query(const uint8_t *s,
/**
* Extract query string from request PDU according to escape rules in 6.5.8.
* @param request Request PDU.
* @return Reconstructed and escaped query string part.
* @return Reconstructed and escaped query string part or @c NULL if
* no query was contained in @p request. The coap_string_t
* object returned by this function must be released with
* coap_delete_string.
*/
coap_string_t *coap_get_query(const struct coap_pdu_t *request);
coap_string_t *coap_get_query(const coap_pdu_t *request);
/**
* Extract uri_path string from request PDU
* @param request Request PDU.
* @return Reconstructed and escaped uri path string part.
* @return Reconstructed and escaped uri path string part or @c NULL
* if no URI-Path was contained in @p request. The
* coap_string_t object returned by this function must be
* released with coap_delete_string.
*/
coap_string_t *coap_get_uri_path(const struct coap_pdu_t *request);
coap_string_t *coap_get_uri_path(const coap_pdu_t *request);
/** @} */

View File

@ -1,5 +1,5 @@
/*
Copyright (c) 2003-2017, Troy D. Hanson http://troydhanson.github.com/uthash/
Copyright (c) 2003-2021, Troy D. Hanson http://troydhanson.github.io/uthash/
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -24,12 +24,22 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef UTHASH_H
#define UTHASH_H
#define UTHASH_VERSION 2.0.2
#define UTHASH_VERSION 2.3.0
#include <string.h> /* memcmp, memset, strlen */
#include <stddef.h> /* ptrdiff_t */
#include <stdlib.h> /* exit */
#if defined(HASH_DEFINE_OWN_STDINT) && HASH_DEFINE_OWN_STDINT
/* This codepath is provided for backward compatibility, but I plan to remove it. */
#warning "HASH_DEFINE_OWN_STDINT is deprecated; please use HASH_NO_STDINT instead"
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
#elif defined(HASH_NO_STDINT) && HASH_NO_STDINT
#else
#include <stdint.h> /* uint8_t, uint32_t */
#endif
/* These macros use decltype or the earlier __typeof GNU extension.
As decltype is only available in newer compilers (VS2010 or gcc 4.3+
when compiling c++ source) this code uses whatever method is needed
@ -62,26 +72,6 @@ do {
} while (0)
#endif
/* a number of the hash function use uint32_t which isn't defined on Pre VS2010 */
#if defined(_WIN32)
#if defined(_MSC_VER) && _MSC_VER >= 1600
#include <stdint.h>
#elif defined(__WATCOMC__) || defined(__MINGW32__) || defined(__CYGWIN__)
#include <stdint.h>
#else
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
#endif
#elif defined(__GNUC__) && !defined(__VXWORKS__)
#include <stdint.h>
#else
typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
#endif
#ifndef uthash_fatal
#define uthash_fatal(msg) exit(-1) /* fatal error (out of memory,etc) */
#endif
#ifndef uthash_malloc
#define uthash_malloc(sz) malloc(sz) /* malloc fcn */
#endif
@ -91,13 +81,18 @@ typedef unsigned char uint8_t;
#ifndef uthash_bzero
#define uthash_bzero(a,n) memset(a,'\0',n)
#endif
#ifndef uthash_memcmp
#define uthash_memcmp(a,b,n) memcmp(a,b,n)
#endif
#ifndef uthash_strlen
#define uthash_strlen(s) strlen(s)
#endif
#ifndef HASH_FUNCTION
#define HASH_FUNCTION(keyptr,keylen,hashv) HASH_JEN(keyptr, keylen, hashv)
#endif
#ifndef HASH_KEYCMP
#define HASH_KEYCMP(a,b,n) memcmp(a,b,n)
#endif
#ifndef uthash_noexpand_fyi
#define uthash_noexpand_fyi(tbl) /* can be defined to log noexpand */
#endif
@ -105,6 +100,32 @@ typedef unsigned char uint8_t;
#define uthash_expand_fyi(tbl) /* can be defined to log expands */
#endif
#ifndef HASH_NONFATAL_OOM
#define HASH_NONFATAL_OOM 0
#endif
#if HASH_NONFATAL_OOM
/* malloc failures can be recovered from */
#ifndef uthash_nonfatal_oom
#define uthash_nonfatal_oom(obj) do {} while (0) /* non-fatal OOM error */
#endif
#define HASH_RECORD_OOM(oomed) do { (oomed) = 1; } while (0)
#define IF_HASH_NONFATAL_OOM(x) x
#else
/* malloc failures result in lost memory, hash tables are unusable */
#ifndef uthash_fatal
#define uthash_fatal(msg) exit(-1) /* fatal OOM error */
#endif
#define HASH_RECORD_OOM(oomed) uthash_fatal("out of memory")
#define IF_HASH_NONFATAL_OOM(x)
#endif
/* initial number of buckets */
#define HASH_INITIAL_NUM_BUCKETS 32U /* initial number of buckets */
#define HASH_INITIAL_NUM_BUCKETS_LOG2 5U /* lg2 of initial number of buckets */
@ -113,11 +134,21 @@ typedef unsigned char uint8_t;
/* calculate the element whose hash handle address is hhp */
#define ELMT_FROM_HH(tbl,hhp) ((void*)(((char*)(hhp)) - ((tbl)->hho)))
/* calculate the hash handle from element address elp */
#define HH_FROM_ELMT(tbl,elp) ((UT_hash_handle *)(((char*)(elp)) + ((tbl)->hho)))
#define HH_FROM_ELMT(tbl,elp) ((UT_hash_handle*)(void*)(((char*)(elp)) + ((tbl)->hho)))
#define HASH_ROLLBACK_BKT(hh, head, itemptrhh) \
do { \
struct UT_hash_handle *_hd_hh_item = (itemptrhh); \
unsigned _hd_bkt; \
HASH_TO_BKT(_hd_hh_item->hashv, (head)->hh.tbl->num_buckets, _hd_bkt); \
(head)->hh.tbl->buckets[_hd_bkt].count++; \
_hd_hh_item->hh_next = NULL; \
_hd_hh_item->hh_prev = NULL; \
} while (0)
#define HASH_VALUE(keyptr,keylen,hashv) \
do { \
HASH_FCN(keyptr, keylen, hashv); \
HASH_FUNCTION(keyptr, keylen, hashv); \
} while (0)
#define HASH_FIND_BYHASHVALUE(hh,head,keyptr,keylen,hashval,out) \
@ -134,23 +165,27 @@ do {
#define HASH_FIND(hh,head,keyptr,keylen,out) \
do { \
unsigned _hf_hashv; \
HASH_VALUE(keyptr, keylen, _hf_hashv); \
HASH_FIND_BYHASHVALUE(hh, head, keyptr, keylen, _hf_hashv, out); \
(out) = NULL; \
if (head) { \
unsigned _hf_hashv; \
HASH_VALUE(keyptr, keylen, _hf_hashv); \
HASH_FIND_BYHASHVALUE(hh, head, keyptr, keylen, _hf_hashv, out); \
} \
} while (0)
#ifdef HASH_BLOOM
#define HASH_BLOOM_BITLEN (1UL << HASH_BLOOM)
#define HASH_BLOOM_BYTELEN (HASH_BLOOM_BITLEN/8UL) + (((HASH_BLOOM_BITLEN%8UL)!=0UL) ? 1UL : 0UL)
#define HASH_BLOOM_MAKE(tbl) \
#define HASH_BLOOM_MAKE(tbl,oomed) \
do { \
(tbl)->bloom_nbits = HASH_BLOOM; \
(tbl)->bloom_bv = (uint8_t*)uthash_malloc(HASH_BLOOM_BYTELEN); \
if (!(tbl)->bloom_bv) { \
uthash_fatal("out of memory"); \
HASH_RECORD_OOM(oomed); \
} else { \
uthash_bzero((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \
(tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \
} \
uthash_bzero((tbl)->bloom_bv, HASH_BLOOM_BYTELEN); \
(tbl)->bloom_sig = HASH_BLOOM_SIGNATURE; \
} while (0)
#define HASH_BLOOM_FREE(tbl) \
@ -162,39 +197,49 @@ do {
#define HASH_BLOOM_BITTEST(bv,idx) (bv[(idx)/8U] & (1U << ((idx)%8U)))
#define HASH_BLOOM_ADD(tbl,hashv) \
HASH_BLOOM_BITSET((tbl)->bloom_bv, (hashv & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
HASH_BLOOM_BITSET((tbl)->bloom_bv, ((hashv) & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
#define HASH_BLOOM_TEST(tbl,hashv) \
HASH_BLOOM_BITTEST((tbl)->bloom_bv, (hashv & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
HASH_BLOOM_BITTEST((tbl)->bloom_bv, ((hashv) & (uint32_t)((1UL << (tbl)->bloom_nbits) - 1U)))
#else
#define HASH_BLOOM_MAKE(tbl)
#define HASH_BLOOM_MAKE(tbl,oomed)
#define HASH_BLOOM_FREE(tbl)
#define HASH_BLOOM_ADD(tbl,hashv)
#define HASH_BLOOM_TEST(tbl,hashv) (1)
#define HASH_BLOOM_BYTELEN 0U
#endif
#define HASH_MAKE_TABLE(hh,head) \
#define HASH_MAKE_TABLE(hh,head,oomed) \
do { \
(head)->hh.tbl = (UT_hash_table*)uthash_malloc(sizeof(UT_hash_table)); \
if (!(head)->hh.tbl) { \
uthash_fatal("out of memory"); \
HASH_RECORD_OOM(oomed); \
} else { \
uthash_bzero((head)->hh.tbl, sizeof(UT_hash_table)); \
(head)->hh.tbl->tail = &((head)->hh); \
(head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
(head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \
(head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \
(head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \
HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
(head)->hh.tbl->signature = HASH_SIGNATURE; \
if (!(head)->hh.tbl->buckets) { \
HASH_RECORD_OOM(oomed); \
uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
} else { \
uthash_bzero((head)->hh.tbl->buckets, \
HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
HASH_BLOOM_MAKE((head)->hh.tbl, oomed); \
IF_HASH_NONFATAL_OOM( \
if (oomed) { \
uthash_free((head)->hh.tbl->buckets, \
HASH_INITIAL_NUM_BUCKETS*sizeof(struct UT_hash_bucket)); \
uthash_free((head)->hh.tbl, sizeof(UT_hash_table)); \
} \
) \
} \
} \
uthash_bzero((head)->hh.tbl, sizeof(UT_hash_table)); \
(head)->hh.tbl->tail = &((head)->hh); \
(head)->hh.tbl->num_buckets = HASH_INITIAL_NUM_BUCKETS; \
(head)->hh.tbl->log2_num_buckets = HASH_INITIAL_NUM_BUCKETS_LOG2; \
(head)->hh.tbl->hho = (char*)(&(head)->hh) - (char*)(head); \
(head)->hh.tbl->buckets = (UT_hash_bucket*)uthash_malloc( \
HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
if (!(head)->hh.tbl->buckets) { \
uthash_fatal("out of memory"); \
} \
uthash_bzero((head)->hh.tbl->buckets, \
HASH_INITIAL_NUM_BUCKETS * sizeof(struct UT_hash_bucket)); \
HASH_BLOOM_MAKE((head)->hh.tbl); \
(head)->hh.tbl->signature = HASH_SIGNATURE; \
} while (0)
#define HASH_REPLACE_BYHASHVALUE_INORDER(hh,head,fieldname,keylen_in,hashval,add,replaced,cmpfcn) \
@ -264,17 +309,58 @@ do {
} while (0)
#endif
#define HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh,head,keyptr,keylen_in,hashval,add,cmpfcn) \
#if HASH_NONFATAL_OOM
#define HASH_ADD_TO_TABLE(hh,head,keyptr,keylen_in,hashval,add,oomed) \
do { \
if (!(oomed)) { \
unsigned _ha_bkt; \
(head)->hh.tbl->num_items++; \
HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], hh, &(add)->hh, oomed); \
if (oomed) { \
HASH_ROLLBACK_BKT(hh, head, &(add)->hh); \
HASH_DELETE_HH(hh, head, &(add)->hh); \
(add)->hh.tbl = NULL; \
uthash_nonfatal_oom(add); \
} else { \
HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
} \
} else { \
(add)->hh.tbl = NULL; \
uthash_nonfatal_oom(add); \
} \
} while (0)
#else
#define HASH_ADD_TO_TABLE(hh,head,keyptr,keylen_in,hashval,add,oomed) \
do { \
unsigned _ha_bkt; \
(head)->hh.tbl->num_items++; \
HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], hh, &(add)->hh, oomed); \
HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
} while (0)
#endif
#define HASH_ADD_KEYPTR_BYHASHVALUE_INORDER(hh,head,keyptr,keylen_in,hashval,add,cmpfcn) \
do { \
IF_HASH_NONFATAL_OOM( int _ha_oomed = 0; ) \
(add)->hh.hashv = (hashval); \
(add)->hh.key = (char*) (keyptr); \
(add)->hh.keylen = (unsigned) (keylen_in); \
if (!(head)) { \
(add)->hh.next = NULL; \
(add)->hh.prev = NULL; \
(head) = (add); \
HASH_MAKE_TABLE(hh, head); \
HASH_MAKE_TABLE(hh, add, _ha_oomed); \
IF_HASH_NONFATAL_OOM( if (!_ha_oomed) { ) \
(head) = (add); \
IF_HASH_NONFATAL_OOM( } ) \
} else { \
void *_hs_iter = (head); \
(add)->hh.tbl = (head)->hh.tbl; \
@ -291,11 +377,7 @@ do {
HASH_APPEND_LIST(hh, head, add); \
} \
} \
(head)->hh.tbl->num_items++; \
HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], &(add)->hh); \
HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
HASH_ADD_TO_TABLE(hh, head, keyptr, keylen_in, hashval, add, _ha_oomed); \
HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE_INORDER"); \
} while (0)
@ -314,24 +396,22 @@ do {
#define HASH_ADD_KEYPTR_BYHASHVALUE(hh,head,keyptr,keylen_in,hashval,add) \
do { \
unsigned _ha_bkt; \
IF_HASH_NONFATAL_OOM( int _ha_oomed = 0; ) \
(add)->hh.hashv = (hashval); \
(add)->hh.key = (const void *) (keyptr); \
(add)->hh.key = (const void*) (keyptr); \
(add)->hh.keylen = (unsigned) (keylen_in); \
if (!(head)) { \
(add)->hh.next = NULL; \
(add)->hh.prev = NULL; \
(head) = (add); \
HASH_MAKE_TABLE(hh, head); \
HASH_MAKE_TABLE(hh, add, _ha_oomed); \
IF_HASH_NONFATAL_OOM( if (!_ha_oomed) { ) \
(head) = (add); \
IF_HASH_NONFATAL_OOM( } ) \
} else { \
(add)->hh.tbl = (head)->hh.tbl; \
HASH_APPEND_LIST(hh, head, add); \
} \
(head)->hh.tbl->num_items++; \
HASH_TO_BKT(hashval, (head)->hh.tbl->num_buckets, _ha_bkt); \
HASH_ADD_TO_BKT((head)->hh.tbl->buckets[_ha_bkt], &(add)->hh); \
HASH_BLOOM_ADD((head)->hh.tbl, hashval); \
HASH_EMIT_KEY(hh, head, keyptr, keylen_in); \
HASH_ADD_TO_TABLE(hh, head, keyptr, keylen_in, hashval, add, _ha_oomed); \
HASH_FSCK(hh, head, "HASH_ADD_KEYPTR_BYHASHVALUE"); \
} while (0)
@ -394,17 +474,25 @@ do {
HASH_DEL_IN_BKT((head)->hh.tbl->buckets[_hd_bkt], _hd_hh_del); \
(head)->hh.tbl->num_items--; \
} \
HASH_FSCK(hh, head, "HASH_DELETE"); \
HASH_FSCK(hh, head, "HASH_DELETE_HH"); \
} while (0)
/* convenience forms of HASH_FIND/HASH_ADD/HASH_DEL */
#define HASH_FIND_STR(head,findstr,out) \
HASH_FIND(hh,head,findstr,(unsigned)uthash_strlen(findstr),out)
do { \
unsigned _uthash_hfstr_keylen = (unsigned)uthash_strlen(findstr); \
HASH_FIND(hh, head, findstr, _uthash_hfstr_keylen, out); \
} while (0)
#define HASH_ADD_STR(head,strfield,add) \
HASH_ADD(hh,head,strfield[0],(unsigned)uthash_strlen(add->strfield),add)
do { \
unsigned _uthash_hastr_keylen = (unsigned)uthash_strlen((add)->strfield); \
HASH_ADD(hh, head, strfield[0], _uthash_hastr_keylen, add); \
} while (0)
#define HASH_REPLACE_STR(head,strfield,add,replaced) \
HASH_REPLACE(hh,head,strfield[0],(unsigned)uthash_strlen(add->strfield),add,replaced)
do { \
unsigned _uthash_hrstr_keylen = (unsigned)uthash_strlen((add)->strfield); \
HASH_REPLACE(hh, head, strfield[0], _uthash_hrstr_keylen, add, replaced); \
} while (0)
#define HASH_FIND_INT(head,findint,out) \
HASH_FIND(hh,head,findint,sizeof(int),out)
#define HASH_ADD_INT(head,intfield,add) \
@ -424,7 +512,8 @@ do {
* This is for uthash developer only; it compiles away if HASH_DEBUG isn't defined.
*/
#ifdef HASH_DEBUG
#define HASH_OOPS(...) do { fprintf(stderr,__VA_ARGS__); exit(-1); } while (0)
#include <stdio.h> /* fprintf, stderr */
#define HASH_OOPS(...) do { fprintf(stderr, __VA_ARGS__); exit(-1); } while (0)
#define HASH_FSCK(hh,head,where) \
do { \
struct UT_hash_handle *_thh; \
@ -491,13 +580,6 @@ do {
#define HASH_EMIT_KEY(hh,head,keyptr,fieldlen)
#endif
/* default to Jenkin's hash unless overridden e.g. DHASH_FUNCTION=HASH_SAX */
#ifdef HASH_FUNCTION
#define HASH_FCN HASH_FUNCTION
#else
#define HASH_FCN HASH_JEN
#endif
/* The Bernstein hash function, used in Perl prior to v5.6. Note (x<<5+x)=x*33. */
#define HASH_BER(key,keylen,hashv) \
do { \
@ -596,8 +678,8 @@ do {
case 4: _hj_i += ( (unsigned)_hj_key[3] << 24 ); /* FALLTHROUGH */ \
case 3: _hj_i += ( (unsigned)_hj_key[2] << 16 ); /* FALLTHROUGH */ \
case 2: _hj_i += ( (unsigned)_hj_key[1] << 8 ); /* FALLTHROUGH */ \
case 1: _hj_i += _hj_key[0]; \
default: ; /* does not happen */ \
case 1: _hj_i += _hj_key[0]; /* FALLTHROUGH */ \
default: ; \
} \
HASH_JEN_MIX(_hj_i, _hj_j, hashv); \
} while (0)
@ -645,6 +727,8 @@ do {
case 1: hashv += *_sfh_key; \
hashv ^= hashv << 10; \
hashv += hashv >> 1; \
break; \
default: ; \
} \
\
/* Force "avalanching" of final 127 bits */ \
@ -656,87 +740,6 @@ do {
hashv += hashv >> 6; \
} while (0)
#ifdef HASH_USING_NO_STRICT_ALIASING
/* The MurmurHash exploits some CPU's (x86,x86_64) tolerance for unaligned reads.
* For other types of CPU's (e.g. Sparc) an unaligned read causes a bus error.
* MurmurHash uses the faster approach only on CPU's where we know it's safe.
*
* Note the preprocessor built-in defines can be emitted using:
*
* gcc -m64 -dM -E - < /dev/null (on gcc)
* cc -## a.c (where a.c is a simple test file) (Sun Studio)
*/
#if (defined(__i386__) || defined(__x86_64__) || defined(_M_IX86))
#define MUR_GETBLOCK(p,i) p[i]
#else /* non intel */
#define MUR_PLUS0_ALIGNED(p) (((unsigned long)p & 3UL) == 0UL)
#define MUR_PLUS1_ALIGNED(p) (((unsigned long)p & 3UL) == 1UL)
#define MUR_PLUS2_ALIGNED(p) (((unsigned long)p & 3UL) == 2UL)
#define MUR_PLUS3_ALIGNED(p) (((unsigned long)p & 3UL) == 3UL)
#define WP(p) ((uint32_t*)((unsigned long)(p) & ~3UL))
#if (defined(__BIG_ENDIAN__) || defined(SPARC) || defined(__ppc__) || defined(__ppc64__))
#define MUR_THREE_ONE(p) ((((*WP(p))&0x00ffffff) << 8) | (((*(WP(p)+1))&0xff000000) >> 24))
#define MUR_TWO_TWO(p) ((((*WP(p))&0x0000ffff) <<16) | (((*(WP(p)+1))&0xffff0000) >> 16))
#define MUR_ONE_THREE(p) ((((*WP(p))&0x000000ff) <<24) | (((*(WP(p)+1))&0xffffff00) >> 8))
#else /* assume little endian non-intel */
#define MUR_THREE_ONE(p) ((((*WP(p))&0xffffff00) >> 8) | (((*(WP(p)+1))&0x000000ff) << 24))
#define MUR_TWO_TWO(p) ((((*WP(p))&0xffff0000) >>16) | (((*(WP(p)+1))&0x0000ffff) << 16))
#define MUR_ONE_THREE(p) ((((*WP(p))&0xff000000) >>24) | (((*(WP(p)+1))&0x00ffffff) << 8))
#endif
#define MUR_GETBLOCK(p,i) (MUR_PLUS0_ALIGNED(p) ? ((p)[i]) : \
(MUR_PLUS1_ALIGNED(p) ? MUR_THREE_ONE(p) : \
(MUR_PLUS2_ALIGNED(p) ? MUR_TWO_TWO(p) : \
MUR_ONE_THREE(p))))
#endif
#define MUR_ROTL32(x,r) (((x) << (r)) | ((x) >> (32 - (r))))
#define MUR_FMIX(_h) \
do { \
_h ^= _h >> 16; \
_h *= 0x85ebca6bu; \
_h ^= _h >> 13; \
_h *= 0xc2b2ae35u; \
_h ^= _h >> 16; \
} while (0)
#define HASH_MUR(key,keylen,hashv) \
do { \
const uint8_t *_mur_data = (const uint8_t*)(key); \
const int _mur_nblocks = (int)(keylen) / 4; \
uint32_t _mur_h1 = 0xf88D5353u; \
uint32_t _mur_c1 = 0xcc9e2d51u; \
uint32_t _mur_c2 = 0x1b873593u; \
uint32_t _mur_k1 = 0; \
const uint8_t *_mur_tail; \
const uint32_t *_mur_blocks = (const uint32_t*)(_mur_data+(_mur_nblocks*4)); \
int _mur_i; \
for (_mur_i = -_mur_nblocks; _mur_i != 0; _mur_i++) { \
_mur_k1 = MUR_GETBLOCK(_mur_blocks,_mur_i); \
_mur_k1 *= _mur_c1; \
_mur_k1 = MUR_ROTL32(_mur_k1,15); \
_mur_k1 *= _mur_c2; \
\
_mur_h1 ^= _mur_k1; \
_mur_h1 = MUR_ROTL32(_mur_h1,13); \
_mur_h1 = (_mur_h1*5U) + 0xe6546b64u; \
} \
_mur_tail = (const uint8_t*)(_mur_data + (_mur_nblocks*4)); \
_mur_k1=0; \
switch ((keylen) & 3U) { \
case 0: break; \
case 3: _mur_k1 ^= (uint32_t)_mur_tail[2] << 16; /* FALLTHROUGH */ \
case 2: _mur_k1 ^= (uint32_t)_mur_tail[1] << 8; /* FALLTHROUGH */ \
case 1: _mur_k1 ^= (uint32_t)_mur_tail[0]; \
_mur_k1 *= _mur_c1; \
_mur_k1 = MUR_ROTL32(_mur_k1,15); \
_mur_k1 *= _mur_c2; \
_mur_h1 ^= _mur_k1; \
} \
_mur_h1 ^= (uint32_t)(keylen); \
MUR_FMIX(_mur_h1); \
hashv = _mur_h1; \
} while (0)
#endif /* HASH_USING_NO_STRICT_ALIASING */
/* iterate over items in a known bucket to find desired item */
#define HASH_FIND_IN_BKT(tbl,hh,head,keyptr,keylen_in,hashval,out) \
do { \
@ -747,7 +750,7 @@ do {
} \
while ((out) != NULL) { \
if ((out)->hh.hashv == (hashval) && (out)->hh.keylen == (keylen_in)) { \
if (uthash_memcmp((out)->hh.key, keyptr, keylen_in) == 0) { \
if (HASH_KEYCMP((out)->hh.key, keyptr, keylen_in) == 0) { \
break; \
} \
} \
@ -760,7 +763,7 @@ do {
} while (0)
/* add an item to a bucket */
#define HASH_ADD_TO_BKT(head,addhh) \
#define HASH_ADD_TO_BKT(head,hh,addhh,oomed) \
do { \
UT_hash_bucket *_ha_head = &(head); \
_ha_head->count++; \
@ -772,7 +775,12 @@ do {
_ha_head->hh_head = (addhh); \
if ((_ha_head->count >= ((_ha_head->expand_mult + 1U) * HASH_BKT_CAPACITY_THRESH)) \
&& !(addhh)->tbl->noexpand) { \
HASH_EXPAND_BUCKETS((addhh)->tbl); \
HASH_EXPAND_BUCKETS(addhh,(addhh)->tbl, oomed); \
IF_HASH_NONFATAL_OOM( \
if (oomed) { \
HASH_DEL_IN_BKT(head,addhh); \
} \
) \
} \
} while (0)
@ -821,53 +829,56 @@ do {
* ceil(n/b) = (n>>lb) + ( (n & (b-1)) ? 1:0)
*
*/
#define HASH_EXPAND_BUCKETS(tbl) \
#define HASH_EXPAND_BUCKETS(hh,tbl,oomed) \
do { \
unsigned _he_bkt; \
unsigned _he_bkt_i; \
struct UT_hash_handle *_he_thh, *_he_hh_nxt; \
UT_hash_bucket *_he_new_buckets, *_he_newbkt; \
_he_new_buckets = (UT_hash_bucket*)uthash_malloc( \
2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U); \
if (!_he_new_buckets) { \
uthash_fatal("out of memory"); \
} \
uthash_bzero(_he_new_buckets, \
2UL * (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
(tbl)->ideal_chain_maxlen = \
((tbl)->num_items >> ((tbl)->log2_num_buckets+1U)) + \
((((tbl)->num_items & (((tbl)->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \
(tbl)->nonideal_items = 0; \
for (_he_bkt_i = 0; _he_bkt_i < (tbl)->num_buckets; _he_bkt_i++) { \
_he_thh = (tbl)->buckets[ _he_bkt_i ].hh_head; \
while (_he_thh != NULL) { \
_he_hh_nxt = _he_thh->hh_next; \
HASH_TO_BKT(_he_thh->hashv, (tbl)->num_buckets * 2U, _he_bkt); \
_he_newbkt = &(_he_new_buckets[_he_bkt]); \
if (++(_he_newbkt->count) > (tbl)->ideal_chain_maxlen) { \
(tbl)->nonideal_items++; \
_he_newbkt->expand_mult = _he_newbkt->count / (tbl)->ideal_chain_maxlen; \
HASH_RECORD_OOM(oomed); \
} else { \
uthash_bzero(_he_new_buckets, \
sizeof(struct UT_hash_bucket) * (tbl)->num_buckets * 2U); \
(tbl)->ideal_chain_maxlen = \
((tbl)->num_items >> ((tbl)->log2_num_buckets+1U)) + \
((((tbl)->num_items & (((tbl)->num_buckets*2U)-1U)) != 0U) ? 1U : 0U); \
(tbl)->nonideal_items = 0; \
for (_he_bkt_i = 0; _he_bkt_i < (tbl)->num_buckets; _he_bkt_i++) { \
_he_thh = (tbl)->buckets[ _he_bkt_i ].hh_head; \
while (_he_thh != NULL) { \
_he_hh_nxt = _he_thh->hh_next; \
HASH_TO_BKT(_he_thh->hashv, (tbl)->num_buckets * 2U, _he_bkt); \
_he_newbkt = &(_he_new_buckets[_he_bkt]); \
if (++(_he_newbkt->count) > (tbl)->ideal_chain_maxlen) { \
(tbl)->nonideal_items++; \
if (_he_newbkt->count > _he_newbkt->expand_mult * (tbl)->ideal_chain_maxlen) { \
_he_newbkt->expand_mult++; \
} \
} \
_he_thh->hh_prev = NULL; \
_he_thh->hh_next = _he_newbkt->hh_head; \
if (_he_newbkt->hh_head != NULL) { \
_he_newbkt->hh_head->hh_prev = _he_thh; \
} \
_he_newbkt->hh_head = _he_thh; \
_he_thh = _he_hh_nxt; \
} \
_he_thh->hh_prev = NULL; \
_he_thh->hh_next = _he_newbkt->hh_head; \
if (_he_newbkt->hh_head != NULL) { \
_he_newbkt->hh_head->hh_prev = _he_thh; \
} \
_he_newbkt->hh_head = _he_thh; \
_he_thh = _he_hh_nxt; \
} \
uthash_free((tbl)->buckets, (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
(tbl)->num_buckets *= 2U; \
(tbl)->log2_num_buckets++; \
(tbl)->buckets = _he_new_buckets; \
(tbl)->ineff_expands = ((tbl)->nonideal_items > ((tbl)->num_items >> 1)) ? \
((tbl)->ineff_expands+1U) : 0U; \
if ((tbl)->ineff_expands > 1U) { \
(tbl)->noexpand = 1; \
uthash_noexpand_fyi(tbl); \
} \
uthash_expand_fyi(tbl); \
} \
uthash_free((tbl)->buckets, (tbl)->num_buckets * sizeof(struct UT_hash_bucket)); \
(tbl)->num_buckets *= 2U; \
(tbl)->log2_num_buckets++; \
(tbl)->buckets = _he_new_buckets; \
(tbl)->ineff_expands = ((tbl)->nonideal_items > ((tbl)->num_items >> 1)) ? \
((tbl)->ineff_expands+1U) : 0U; \
if ((tbl)->ineff_expands > 1U) { \
(tbl)->noexpand = 1; \
uthash_noexpand_fyi(tbl); \
} \
uthash_expand_fyi(tbl); \
} while (0)
@ -977,7 +988,8 @@ do {
_src_hh = _src_hh->hh_next) { \
_elt = ELMT_FROM_HH((src)->hh_src.tbl, _src_hh); \
if (cond(_elt)) { \
_dst_hh = (UT_hash_handle*)(((char*)_elt) + _dst_hho); \
IF_HASH_NONFATAL_OOM( int _hs_oomed = 0; ) \
_dst_hh = (UT_hash_handle*)(void*)(((char*)_elt) + _dst_hho); \
_dst_hh->key = _src_hh->key; \
_dst_hh->keylen = _src_hh->keylen; \
_dst_hh->hashv = _src_hh->hashv; \
@ -988,14 +1000,30 @@ do {
} \
if ((dst) == NULL) { \
DECLTYPE_ASSIGN(dst, _elt); \
HASH_MAKE_TABLE(hh_dst, dst); \
HASH_MAKE_TABLE(hh_dst, dst, _hs_oomed); \
IF_HASH_NONFATAL_OOM( \
if (_hs_oomed) { \
uthash_nonfatal_oom(_elt); \
(dst) = NULL; \
continue; \
} \
) \
} else { \
_dst_hh->tbl = (dst)->hh_dst.tbl; \
} \
HASH_TO_BKT(_dst_hh->hashv, _dst_hh->tbl->num_buckets, _dst_bkt); \
HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt], _dst_hh); \
HASH_BLOOM_ADD(_dst_hh->tbl, _dst_hh->hashv); \
HASH_ADD_TO_BKT(_dst_hh->tbl->buckets[_dst_bkt], hh_dst, _dst_hh, _hs_oomed); \
(dst)->hh_dst.tbl->num_items++; \
IF_HASH_NONFATAL_OOM( \
if (_hs_oomed) { \
HASH_ROLLBACK_BKT(hh_dst, dst, _dst_hh); \
HASH_DELETE_HH(hh_dst, dst, _dst_hh); \
_dst_hh->tbl = NULL; \
uthash_nonfatal_oom(_elt); \
continue; \
} \
) \
HASH_BLOOM_ADD(_dst_hh->tbl, _dst_hh->hashv); \
_last_elt = _elt; \
_last_elt_hh = _dst_hh; \
} \

View File

@ -1,5 +1,5 @@
/*
Copyright (c) 2007-2017, Troy D. Hanson http://troydhanson.github.com/uthash/
Copyright (c) 2007-2021, Troy D. Hanson http://troydhanson.github.io/uthash/
All rights reserved.
Redistribution and use in source and binary forms, with or without
@ -24,7 +24,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#ifndef UTLIST_H
#define UTLIST_H
#define UTLIST_VERSION 2.0.2
#define UTLIST_VERSION 2.3.0
#include <assert.h>
@ -358,8 +358,8 @@ do {
do { \
LDECLTYPE(head) _tmp; \
if (head) { \
LL_LOWER_BOUND(head, _tmp, add, cmp); \
LL_APPEND_ELEM(head, _tmp, add); \
LL_LOWER_BOUND2(head, _tmp, add, cmp, next); \
LL_APPEND_ELEM2(head, _tmp, add, next); \
} else { \
(head) = (add); \
(head)->next = NULL; \
@ -651,14 +651,14 @@ do {
} while (0)
#define DL_INSERT_INORDER(head,add,cmp) \
DL_INSERT_INORDER2(head,add,cmp,next)
DL_INSERT_INORDER2(head,add,cmp,prev,next)
#define DL_INSERT_INORDER2(head,add,cmp,next) \
#define DL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
LDECLTYPE(head) _tmp; \
if (head) { \
DL_LOWER_BOUND(head, _tmp, add, cmp); \
DL_APPEND_ELEM(head, _tmp, add); \
DL_LOWER_BOUND2(head, _tmp, add, cmp, next); \
DL_APPEND_ELEM2(head, _tmp, add, prev, next); \
} else { \
(head) = (add); \
(head)->prev = (head); \
@ -825,7 +825,7 @@ do {
/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */
#undef DL_INSERT_INORDER2
#define DL_INSERT_INORDER2(head,add,cmp,next) \
#define DL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
if ((head) == NULL) { \
(add)->prev = (add); \
@ -838,7 +838,7 @@ do {
(head) = (add); \
} else { \
char *_tmp = (char*)(head); \
while ((char*)(head)->next != _tmp && (cmp((head)->next, add)) < 0) { \
while ((head)->next && (cmp((head)->next, add)) < 0) { \
(head) = (head)->next; \
} \
(add)->prev = (head); \
@ -892,14 +892,14 @@ do {
} while (0)
#define CDL_INSERT_INORDER(head,add,cmp) \
CDL_INSERT_INORDER2(head,add,cmp,next)
CDL_INSERT_INORDER2(head,add,cmp,prev,next)
#define CDL_INSERT_INORDER2(head,add,cmp,next) \
#define CDL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
LDECLTYPE(head) _tmp; \
if (head) { \
CDL_LOWER_BOUND(head, _tmp, add, cmp); \
CDL_APPEND_ELEM(head, _tmp, add); \
CDL_LOWER_BOUND2(head, _tmp, add, cmp, next); \
CDL_APPEND_ELEM2(head, _tmp, add, prev, next); \
} else { \
(head) = (add); \
(head)->next = (head); \
@ -1044,7 +1044,7 @@ do {
/* Here are VS2008 / NO_DECLTYPE replacements for a few functions */
#undef CDL_INSERT_INORDER2
#define CDL_INSERT_INORDER2(head,add,cmp,next) \
#define CDL_INSERT_INORDER2(head,add,cmp,prev,next) \
do { \
if ((head) == NULL) { \
(add)->prev = (add); \

View File

@ -1,631 +0,0 @@
/*
* coap_dtls.h -- (Datagram) Transport Layer Support for libcoap
*
* Copyright (C) 2016 Olaf Bergmann <bergmann@tzi.org>
* Copyright (C) 2017 Jean-Claude Michelou <jcm@spinetix.com>
*
* This file is part of the CoAP library libcoap. Please see README for terms
* of use.
*/
#ifndef COAP_DTLS_H_
#define COAP_DTLS_H_
#include "coap_time.h"
#include "str.h"
struct coap_context_t;
struct coap_session_t;
struct coap_dtls_pki_t;
/**
* @defgroup dtls DTLS Support
* API functions for interfacing with DTLS libraries.
* @{
*/
/**
* Check whether DTLS is available.
*
* @return @c 1 if support for DTLS is enabled, or @c 0 otherwise.
*/
int coap_dtls_is_supported(void);
/**
* Check whether TLS is available.
*
* @return @c 1 if support for TLS is enabled, or @c 0 otherwise.
*/
int coap_tls_is_supported(void);
typedef enum coap_tls_library_t {
COAP_TLS_LIBRARY_NOTLS = 0, /**< No DTLS library */
COAP_TLS_LIBRARY_TINYDTLS, /**< Using TinyDTLS library */
COAP_TLS_LIBRARY_OPENSSL, /**< Using OpenSSL library */
COAP_TLS_LIBRARY_GNUTLS, /**< Using GnuTLS library */
COAP_TLS_LIBRARY_MBEDTLS, /**< Using MbedTLS library */
} coap_tls_library_t;
/**
* The structure used for returning the underlying (D)TLS library
* information.
*/
typedef struct coap_tls_version_t {
uint64_t version; /**< (D)TLS runtime Library Version */
coap_tls_library_t type; /**< Library type. One of COAP_TLS_LIBRARY_* */
uint64_t built_version; /**< (D)TLS Built against Library Version */
} coap_tls_version_t;
/**
* Determine the type and version of the underlying (D)TLS library.
*
* @return The version and type of library libcoap was compiled against.
*/
coap_tls_version_t *coap_get_tls_library_version(void);
/**
* Additional Security setup handler that can be set up by
* coap_context_set_pki().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to do some additional checks/changes/updates.
*
* @param tls_session The security session definition - e.g. SSL * for OpenSSL.
* NULL if server call-back.
* This will be dependent on the underlying TLS library -
* see coap_get_tls_library_version()
* @param setup_data A structure containing setup data originally passed into
* coap_context_set_pki() or coap_new_client_session_pki().
*
* @return @c 1 if successful, else @c 0.
*/
typedef int (*coap_dtls_security_setup_t)(void* tls_session,
struct coap_dtls_pki_t *setup_data);
/**
* CN Validation call-back that can be set up by coap_context_set_pki().
* Invoked when libcoap has done the validation checks at the TLS level,
* but the application needs to check that the CN is allowed.
* CN is the SubjectAltName in the cert, if not present, then the leftmost
* Common Name (CN) component of the subject name.
*
* @param cn The determined CN from the certificate
* @param asn1_public_cert The ASN.1 DER encoded X.509 certificate
* @param asn1_length The ASN.1 length
* @param coap_session The CoAP session associated with the certificate update
* @param depth Depth in cert chain. If 0, then client cert, else a CA
* @param validated TLS layer can find no issues if 1
* @param arg The same as was passed into coap_context_set_pki()
* in setup_data->cn_call_back_arg
*
* @return @c 1 if accepted, else @c 0 if to be rejected.
*/
typedef int (*coap_dtls_cn_callback_t)(const char *cn,
const uint8_t *asn1_public_cert,
size_t asn1_length,
struct coap_session_t *coap_session,
unsigned depth,
int validated,
void *arg);
/**
* The enum used for determining the provided PKI ASN.1 (DER) Private Key
* formats.
*/
typedef enum coap_asn1_privatekey_type_t {
COAP_ASN1_PKEY_NONE, /**< NONE */
COAP_ASN1_PKEY_RSA, /**< RSA type */
COAP_ASN1_PKEY_RSA2, /**< RSA2 type */
COAP_ASN1_PKEY_DSA, /**< DSA type */
COAP_ASN1_PKEY_DSA1, /**< DSA1 type */
COAP_ASN1_PKEY_DSA2, /**< DSA2 type */
COAP_ASN1_PKEY_DSA3, /**< DSA3 type */
COAP_ASN1_PKEY_DSA4, /**< DSA4 type */
COAP_ASN1_PKEY_DH, /**< DH type */
COAP_ASN1_PKEY_DHX, /**< DHX type */
COAP_ASN1_PKEY_EC, /**< EC type */
COAP_ASN1_PKEY_HMAC, /**< HMAC type */
COAP_ASN1_PKEY_CMAC, /**< CMAC type */
COAP_ASN1_PKEY_TLS1_PRF, /**< TLS1_PRF type */
COAP_ASN1_PKEY_HKDF /**< HKDF type */
} coap_asn1_privatekey_type_t;
/**
* The enum used for determining the PKI key formats.
*/
typedef enum coap_pki_key_t {
COAP_PKI_KEY_PEM = 0, /**< The PKI key type is PEM file */
COAP_PKI_KEY_ASN1, /**< The PKI key type is ASN.1 (DER) */
COAP_PKI_KEY_PEM_BUF, /**< The PKI key type is PEM buffer */
} coap_pki_key_t;
/**
* The structure that holds the PKI PEM definitions.
*/
typedef struct coap_pki_key_pem_t {
const char *ca_file; /**< File location of Common CA in PEM format */
const char *public_cert; /**< File location of Public Cert in PEM format */
const char *private_key; /**< File location of Private Key in PEM format */
} coap_pki_key_pem_t;
/**
* The structure that holds the PKI PEM buffer definitions.
*/
typedef struct coap_pki_key_pem_buf_t {
const uint8_t *ca_cert; /**< PEM buffer Common CA Cert */
const uint8_t *public_cert; /**< PEM buffer Public Cert */
const uint8_t *private_key; /**< PEM buffer Private Key */
size_t ca_cert_len; /**< PEM buffer CA Cert length */
size_t public_cert_len; /**< PEM buffer Public Cert length */
size_t private_key_len; /**< PEM buffer Private Key length */
} coap_pki_key_pem_buf_t;
/**
* The structure that holds the PKI ASN.1 (DER) definitions.
*/
typedef struct coap_pki_key_asn1_t {
const uint8_t *ca_cert; /**< ASN1 (DER) Common CA Cert */
const uint8_t *public_cert; /**< ASN1 (DER) Public Cert */
const uint8_t *private_key; /**< ASN1 (DER) Private Key */
size_t ca_cert_len; /**< ASN1 CA Cert length */
size_t public_cert_len; /**< ASN1 Public Cert length */
size_t private_key_len; /**< ASN1 Private Key length */
coap_asn1_privatekey_type_t private_key_type; /**< Private Key Type */
} coap_pki_key_asn1_t;
/**
* The structure that holds the PKI key information.
*/
typedef struct coap_dtls_key_t {
coap_pki_key_t key_type; /**< key format type */
union {
coap_pki_key_pem_t pem; /**< for PEM file keys */
coap_pki_key_pem_buf_t pem_buf; /**< for PEM memory keys */
coap_pki_key_asn1_t asn1; /**< for ASN.1 (DER) file keys */
} key;
} coap_dtls_key_t;
/**
* Server Name Indication (SNI) Validation call-back that can be set up by
* coap_context_set_pki().
* Invoked if the SNI is not previously seen and prior to sending a certificate
* set back to the client so that the appropriate certificate set can be used
* based on the requesting SNI.
*
* @param sni The requested SNI
* @param arg The same as was passed into coap_context_set_pki()
* in setup_data->sni_call_back_arg
*
* @return New set of certificates to use, or @c NULL if SNI is to be rejected.
*/
typedef coap_dtls_key_t *(*coap_dtls_sni_callback_t)(const char *sni,
void* arg);
#define COAP_DTLS_PKI_SETUP_VERSION 1 /**< Latest PKI setup version */
/**
* The structure used for defining the PKI setup data to be used.
*/
typedef struct coap_dtls_pki_t {
uint8_t version; /** Set to 1 to support this version of the struct */
/* Options to enable different TLS functionality in libcoap */
uint8_t verify_peer_cert; /**< 1 if peer cert is to be verified */
uint8_t require_peer_cert; /**< 1 if peer cert is required */
uint8_t allow_self_signed; /**< 1 if self signed certs are allowed */
uint8_t allow_expired_certs; /**< 1 if expired certs are allowed */
uint8_t cert_chain_validation; /**< 1 if to check cert_chain_verify_depth */
uint8_t cert_chain_verify_depth; /**< recommended depth is 3 */
uint8_t check_cert_revocation; /**< 1 if revocation checks wanted */
uint8_t allow_no_crl; /**< 1 ignore if CRL not there */
uint8_t allow_expired_crl; /**< 1 if expired crl is allowed */
uint8_t allow_bad_md_hash; /**< 1 if expired certs are allowed */
uint8_t allow_short_rsa_length; /**< 1 if expired certs are allowed */
uint8_t reserved[4]; /**< Reserved - must be set to 0 for
future compatibility */
/* Size of 4 chosen to align to next
* parameter, so if newly defined option
* it can use one of the reserverd slot so
* no need to change
* COAP_DTLS_PKI_SETUP_VERSION and just
* decrement the reserved[] count.
*/
/** CN check call-back function.
* If not NULL, is called when the TLS connection has passed the configured
* TLS options above for the application to verify if the CN is valid.
*/
coap_dtls_cn_callback_t validate_cn_call_back;
void *cn_call_back_arg; /**< Passed in to the CN call-back function */
/** SNI check call-back function.
* If not @p NULL, called if the SNI is not previously seen and prior to
* sending a certificate set back to the client so that the appropriate
* certificate set can be used based on the requesting SNI.
*/
coap_dtls_sni_callback_t validate_sni_call_back;
void *sni_call_back_arg; /**< Passed in to the sni call-back function */
/** Additional Security call-back handler that is invoked when libcoap has
* done the standerd, defined validation checks at the TLS level,
* If not @p NULL, called from within the TLS Client Hello connection
* setup.
*/
coap_dtls_security_setup_t additional_tls_setup_call_back;
char* client_sni; /**< If not NULL, SNI to use in client TLS setup.
Owned by the client app and must remain valid
during the call to coap_new_client_session_pki() */
coap_dtls_key_t pki_key; /**< PKI key definition */
} coap_dtls_pki_t;
/** @} */
/**
* @defgroup dtls_internal DTLS Support (Internal)
* Internal API functions for interfacing with DTLS libraries.
* @{
*/
/**
* Creates a new DTLS context for the given @p coap_context. This function
* returns a pointer to a new DTLS context object or @c NULL on error.
*
* Internal function.
*
* @param coap_context The CoAP context where the DTLS object shall be used.
*
* @return A DTLS context object or @c NULL on error.
*/
void *
coap_dtls_new_context(struct coap_context_t *coap_context);
typedef enum coap_dtls_role_t {
COAP_DTLS_ROLE_CLIENT, /**< Internal function invoked for client */
COAP_DTLS_ROLE_SERVER /**< Internal function invoked for server */
} coap_dtls_role_t;
/**
* Set the DTLS context's default PSK information.
* This does the PSK specifics following coap_dtls_new_context().
* If @p COAP_DTLS_ROLE_SERVER, then identity hint will also get set.
* If @p COAP_DTLS_ROLE_SERVER, then the information will get put into the
* TLS library's context (from which sessions are derived).
* If @p COAP_DTLS_ROLE_CLIENT, then the information will get put into the
* TLS library's session.
*
* Internal function.
*
* @param coap_context The CoAP context.
* @param identity_hint The default PSK server identity hint sent to a client.
* Required parameter. If @p NULL, will be set to "".
* Empty string is a valid hint.
* This parameter is ignored if COAP_DTLS_ROLE_CLIENT
* @param role One of @p COAP_DTLS_ROLE_CLIENT or @p COAP_DTLS_ROLE_SERVER
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_psk(struct coap_context_t *coap_context,
const char *identity_hint,
coap_dtls_role_t role);
/**
* Set the DTLS context's default server PKI information.
* This does the PKI specifics following coap_dtls_new_context().
* If @p COAP_DTLS_ROLE_SERVER, then the information will get put into the
* TLS library's context (from which sessions are derived).
* If @p COAP_DTLS_ROLE_CLIENT, then the information will get put into the
* TLS library's session.
*
* Internal function.
*
* @param coap_context The CoAP context.
* @param setup_data Setup information defining how PKI is to be setup.
* Required parameter. If @p NULL, PKI will not be
* set up.
* @param role One of @p COAP_DTLS_ROLE_CLIENT or @p COAP_DTLS_ROLE_SERVER
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_pki(struct coap_context_t *coap_context,
coap_dtls_pki_t *setup_data,
coap_dtls_role_t role);
/**
* Set the dtls context's default Root CA information for a client or server.
*
* Internal function.
*
* @param coap_context The current coap_context_t object.
* @param ca_file If not @p NULL, is the full path name of a PEM encoded
* file containing all the Root CAs to be used.
* @param ca_dir If not @p NULL, points to a directory containing PEM
* encoded files containing all the Root CAs to be used.
*
* @return @c 1 if successful, else @c 0.
*/
int
coap_dtls_context_set_pki_root_cas(struct coap_context_t *coap_context,
const char *ca_file,
const char *ca_dir);
/**
* Check whether one of the coap_dtls_context_set_{psk|pki}() functions have
* been called.
*
* Internal function.
*
* @param coap_context The current coap_context_t object.
*
* @return @c 1 if coap_dtls_context_set_{psk|pki}() called, else @c 0.
*/
int coap_dtls_context_check_keys_enabled(struct coap_context_t *coap_context);
/**
* Releases the storage allocated for @p dtls_context.
*
* Internal function.
*
* @param dtls_context The DTLS context as returned by coap_dtls_new_context().
*/
void coap_dtls_free_context(void *dtls_context);
/**
* Create a new client-side session. This should send a HELLO to the server.
*
* Internal function.
*
* @param coap_session The CoAP session.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_dtls_new_client_session(struct coap_session_t *coap_session);
/**
* Create a new DTLS server-side session.
* Called after coap_dtls_hello() has returned @c 1, signalling that a validated
* HELLO was received from a client.
* This should send a HELLO to the server.
*
* Internal function.
*
* @param coap_session The CoAP session.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the DTLS session.
*/
void *coap_dtls_new_server_session(struct coap_session_t *coap_session);
/**
* Terminates the DTLS session (may send an ALERT if necessary) then frees the
* underlying TLS library object containing security parameters for the session.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_free_session(struct coap_session_t *coap_session);
/**
* Notify of a change in the CoAP session's MTU, for example after
* a PMTU update.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_session_update_mtu(struct coap_session_t *coap_session);
/**
* Send data to a DTLS peer.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data pointer to data.
* @param data_len Number of bytes to send.
*
* @return @c 0 if this would be blocking, @c -1 if there is an error or the
* number of cleartext bytes sent.
*/
int coap_dtls_send(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Check if timeout is handled per CoAP session or per CoAP context.
*
* Internal function.
*
* @return @c 1 of timeout and retransmit is per context, @c 0 if it is
* per session.
*/
int coap_dtls_is_context_timeout(void);
/**
* Do all pending retransmits and get next timeout
*
* Internal function.
*
* @param dtls_context The DTLS context.
*
* @return @c 0 if no event is pending or date of the next retransmit.
*/
coap_tick_t coap_dtls_get_context_timeout(void *dtls_context);
/**
* Get next timeout for this session.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param now The current time in ticks.
*
* @return @c 0 If no event is pending or ticks time of the next retransmit.
*/
coap_tick_t coap_dtls_get_timeout(struct coap_session_t *coap_session,
coap_tick_t now);
/**
* Handle a DTLS timeout expiration.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_dtls_handle_timeout(struct coap_session_t *coap_session);
/**
* Handling incoming data from a DTLS peer.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Encrypted datagram.
* @param data_len Encrypted datagram size.
*
* @return Result of coap_handle_dgram on the decrypted CoAP PDU
* or @c -1 for error.
*/
int coap_dtls_receive(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Handling client HELLO messages from a new candiate peer.
* Note that session->tls is empty.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Encrypted datagram.
* @param data_len Encrypted datagram size.
*
* @return @c 0 if a cookie verification message has been sent, @c 1 if the
* HELLO contains a valid cookie and a server session should be created,
* @c -1 if the message is invalid.
*/
int coap_dtls_hello(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len);
/**
* Get DTLS overhead over cleartext PDUs.
*
* Internal function.
*
* @param coap_session The CoAP session.
*
* @return Maximum number of bytes added by DTLS layer.
*/
unsigned int coap_dtls_get_overhead(struct coap_session_t *coap_session);
/**
* Create a new TLS client-side session.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param connected Updated with whether the connection is connected yet or not.
* @c 0 is not connected, @c 1 is connected.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_tls_new_client_session(struct coap_session_t *coap_session, int *connected);
/**
* Create a TLS new server-side session.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param connected Updated with whether the connection is connected yet or not.
* @c 0 is not connected, @c 1 is connected.
*
* @return Opaque handle to underlying TLS library object containing security
* parameters for the session.
*/
void *coap_tls_new_server_session(struct coap_session_t *coap_session, int *connected);
/**
* Terminates the TLS session (may send an ALERT if necessary) then frees the
* underlying TLS library object containing security parameters for the session.
*
* Internal function.
*
* @param coap_session The CoAP session.
*/
void coap_tls_free_session( struct coap_session_t *coap_session );
/**
* Send data to a TLS peer, with implicit flush.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Pointer to data.
* @param data_len Number of bytes to send.
*
* @return @c 0 if this should be retried, @c -1 if there is an error
* or the number of cleartext bytes sent.
*/
ssize_t coap_tls_write(struct coap_session_t *coap_session,
const uint8_t *data,
size_t data_len
);
/**
* Read some data from a TLS peer.
*
* Internal function.
*
* @param coap_session The CoAP session.
* @param data Pointer to data.
* @param data_len Maximum number of bytes to read.
*
* @return @c 0 if this should be retried, @c -1 if there is an error
* or the number of cleartext bytes read.
*/
ssize_t coap_tls_read(struct coap_session_t *coap_session,
uint8_t *data,
size_t data_len
);
/**
* Initialize the underlying (D)TLS Library layer.
*
* Internal function.
*
*/
void coap_dtls_startup(void);
/** @} */
/**
* @ingroup logging
* Sets the (D)TLS logging level to the specified @p level.
* Note: coap_log_level() will influence output if at a specified level.
*
* @param level The logging level to use - LOG_*
*/
void coap_dtls_set_log_level(int level);
/**
* @ingroup logging
* Get the current (D)TLS logging.
*
* @return The current log level (one of LOG_*).
*/
int coap_dtls_get_log_level(void);
#endif /* COAP_DTLS_H */

View File

@ -21,27 +21,28 @@
extern "C" {
#endif
#include "libcoap.h"
#include "coap3/libcoap.h"
#include "address.h"
#include "async.h"
#include "bits.h"
#include "block.h"
#include "coap_dtls.h"
#include "coap_event.h"
#include "coap_io.h"
#include "coap_time.h"
#include "coap_debug.h"
#include "encode.h"
#include "mem.h"
#include "net.h"
#include "option.h"
#include "pdu.h"
#include "prng.h"
#include "resource.h"
#include "str.h"
#include "subscribe.h"
#include "uri.h"
#include "coap3/coap_forward_decls.h"
#include "coap3/address.h"
#include "coap3/async.h"
#include "coap3/block.h"
#include "coap3/coap_cache.h"
#include "coap3/coap_dtls.h"
#include "coap3/coap_event.h"
#include "coap3/coap_io.h"
#include "coap3/coap_time.h"
#include "coap3/coap_debug.h"
#include "coap3/encode.h"
#include "coap3/mem.h"
#include "coap3/net.h"
#include "coap3/option.h"
#include "coap3/pdu.h"
#include "coap3/coap_prng.h"
#include "coap3/resource.h"
#include "coap3/str.h"
#include "coap3/subscribe.h"
#include "coap3/uri.h"
#ifdef __cplusplus
}

View File

@ -30,6 +30,7 @@
#define HAVE_NETDB_H
#define HAVE_NETINET_IN_H
#define HAVE_STRUCT_CMSGHDR
#define COAP_DISABLE_TCP 0
#define ipi_spec_dst ipi_addr
struct in6_pktinfo {
@ -53,8 +54,6 @@ struct in6_pktinfo {
#define COAP_CONSTRAINED_STACK 1
#define ESPIDF_VERSION
#define _POSIX_TIMERS 1
#define gai_strerror(x) "gai_strerror() not supported"
#endif /* WITH_POSIX */

View File

@ -23,6 +23,7 @@
#define CONFIG_BOOTLOADER_WDT_ENABLE 1
#define CONFIG_BOOTLOADER_WDT_TIME_MS 9000
#define CONFIG_BOOTLOADER_RESERVE_RTC_SIZE 0x0
#define CONFIG_BOOTLOADER_FLASH_XMC_SUPPORT 1
#define CONFIG_SECURE_BOOT_SUPPORTS_RSA 1
#define CONFIG_SECURE_TARGET_HAS_SECURE_ROM_DL_MODE 1
#define CONFIG_BOOT_ROM_LOG_ALWAYS_ON 1
@ -68,16 +69,7 @@
#define CONFIG_APPTRACE_DEST_NONE 1
#define CONFIG_APPTRACE_LOCK_ENABLE 1
#define CONFIG_BT_ENABLED 1
#define CONFIG_BT_CTRL_ESP32C3 1
#define CONFIG_BT_SOC_SUPPORT_5_0 1
#define CONFIG_BTDM_CTRL_BR_EDR_SCO_DATA_PATH_EFF 0
#define CONFIG_BTDM_CTRL_PCM_ROLE_EFF 0
#define CONFIG_BTDM_CTRL_PCM_POLAR_EFF 0
#define CONFIG_BTDM_CTRL_BLE_MAX_CONN_EFF 0
#define CONFIG_BTDM_CTRL_BR_EDR_MAX_ACL_CONN_EFF 0
#define CONFIG_BTDM_CTRL_BR_EDR_MAX_SYNC_CONN_EFF 0
#define CONFIG_BTDM_CTRL_PINNED_TO_CORE 0
#define CONFIG_BTDM_BLE_SLEEP_CLOCK_ACCURACY_INDEX_EFF 1
#define CONFIG_BT_CTRL_MODE_EFF 1
#define CONFIG_BT_CTRL_BLE_MAX_ACT 10
#define CONFIG_BT_CTRL_BLE_MAX_ACT_EFF 10
@ -169,9 +161,6 @@
#define CONFIG_BT_BLE_RPA_SUPPORTED 1
#define CONFIG_BT_BLE_50_FEATURES_SUPPORTED 1
#define CONFIG_BT_BLE_42_FEATURES_SUPPORTED 1
#define CONFIG_BT_RESERVE_DRAM 0xdb5c
#define CONFIG_BT_NIMBLE_ENABLE_CONN_REATTEMPT 1
#define CONFIG_BT_NIMBLE_USE_ESP_TIMER 1
#define CONFIG_COAP_MBEDTLS_PSK 1
#define CONFIG_COAP_LOG_DEFAULT_LEVEL 0
#define CONFIG_ADC_DISABLE_DAC 1
@ -213,6 +202,8 @@
#define CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES_FOUR 1
#define CONFIG_ESP32C3_UNIVERSAL_MAC_ADDRESSES 4
#define CONFIG_ESP_SLEEP_POWER_DOWN_FLASH 1
#define CONFIG_ESP_IPC_TASK_STACK_SIZE 1024
#define CONFIG_LCD_PANEL_IO_FORMAT_BUF_SIZE 32
#define CONFIG_ESP_NETIF_IP_LOST_TIMER_INTERVAL 120
#define CONFIG_ESP_NETIF_TCPIP_LWIP 1
#define CONFIG_ESP_NETIF_TCPIP_ADAPTER_COMPATIBLE_LAYER 1
@ -224,8 +215,11 @@
#define CONFIG_ESP_SYSTEM_SINGLE_CORE_MODE 1
#define CONFIG_ESP_SYSTEM_RTC_FAST_MEM_AS_HEAP_DEPCHECK 1
#define CONFIG_ESP_SYSTEM_ALLOW_RTC_FAST_MEM_AS_HEAP 1
#define CONFIG_ESP_SYSTEM_MEMPROT_DEPCHECK 1
#define CONFIG_ESP_SYSTEM_MEMPROT_FEATURE 1
#define CONFIG_ESP_SYSTEM_MEMPROT_FEATURE_LOCK 1
#define CONFIG_ESP_SYSTEM_MEMPROT_CPU_PREFETCH_PAD_SIZE 16
#define CONFIG_ESP_SYSTEM_MEMPROT_MEM_ALIGN_SIZE 512
#define CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE 32
#define CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE 2304
#define CONFIG_ESP_MAIN_TASK_STACK_SIZE 3584
@ -241,7 +235,7 @@
#define CONFIG_ESP_TASK_WDT 1
#define CONFIG_ESP_TASK_WDT_TIMEOUT_S 5
#define CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0 1
#define CONFIG_ESP_IPC_TASK_STACK_SIZE 1024
#define CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_4 1
#define CONFIG_ESP_TIME_FUNCS_USE_RTC_TIMER 1
#define CONFIG_ESP_TIME_FUNCS_USE_ESP_TIMER 1
#define CONFIG_ESP_TIMER_TASK_STACK_SIZE 3584
@ -298,9 +292,13 @@
#define CONFIG_FMB_TIMER_PORT_ENABLED 1
#define CONFIG_FMB_TIMER_GROUP 0
#define CONFIG_FMB_TIMER_INDEX 0
#define CONFIG_FMB_MASTER_TIMER_GROUP 0
#define CONFIG_FMB_MASTER_TIMER_INDEX 0
#define CONFIG_FREERTOS_UNICORE 1
#define CONFIG_FREERTOS_NO_AFFINITY 0x7FFFFFFF
#define CONFIG_FREERTOS_CORETIMER_0 1
#define CONFIG_FREERTOS_TICK_SUPPORT_SYSTIMER 1
#define CONFIG_FREERTOS_CORETIMER_SYSTIMER_LVL1 1
#define CONFIG_FREERTOS_SYSTICK_USES_SYSTIMER 1
#define CONFIG_FREERTOS_OPTIMIZED_SCHEDULER 1
#define CONFIG_FREERTOS_HZ 1000
#define CONFIG_FREERTOS_ASSERT_ON_UNTESTED_FUNCTION 1
@ -319,6 +317,7 @@
#define CONFIG_FREERTOS_TASK_FUNCTION_WRAPPER 1
#define CONFIG_FREERTOS_CHECK_MUTEX_GIVEN_BY_OWNER 1
#define CONFIG_FREERTOS_DEBUG_OCDAWARE 1
#define CONFIG_FREERTOS_ENABLE_TASK_SNAPSHOT 1
#define CONFIG_HAL_ASSERTION_EQUALS_SYSTEM 1
#define CONFIG_HAL_DEFAULT_ASSERTION_LEVEL 2
#define CONFIG_HEAP_POISONING_DISABLED 1
@ -346,7 +345,6 @@
#define CONFIG_LWIP_DHCPS_MAX_STATION_NUM 8
#define CONFIG_LWIP_IPV6 1
#define CONFIG_LWIP_IPV6_NUM_ADDRESSES 3
#define CONFIG_LWIP_IPV6_RDNSS_MAX_DNS_SERVERS 0
#define CONFIG_LWIP_NETIF_LOOPBACK 1
#define CONFIG_LWIP_LOOPBACK_MAX_PBUFS 8
#define CONFIG_LWIP_MAX_ACTIVE_TCP 16
@ -373,7 +371,7 @@
#define CONFIG_LWIP_IPV6_ND6_NUM_NEIGHBORS 5
#define CONFIG_LWIP_ICMP 1
#define CONFIG_LWIP_MAX_RAW_PCBS 16
#define CONFIG_LWIP_DHCP_MAX_NTP_SERVERS 1
#define CONFIG_LWIP_SNTP_MAX_SERVERS 1
#define CONFIG_LWIP_SNTP_UPDATE_DELAY 3600000
#define CONFIG_LWIP_ESP_LWIP_ASSERT 1
#define CONFIG_LWIP_HOOK_TCP_ISN_DEFAULT 1
@ -624,5 +622,5 @@
#define CONFIG_TIMER_TASK_STACK_SIZE CONFIG_ESP_TIMER_TASK_STACK_SIZE
#define CONFIG_TOOLPREFIX CONFIG_SDK_TOOLPREFIX
#define CONFIG_UDP_RECVMBOX_SIZE CONFIG_LWIP_UDP_RECVMBOX_SIZE
#define CONFIG_ARDUINO_IDF_COMMIT "c69f0ec32"
#define CONFIG_ARDUINO_IDF_COMMIT "3e370c4296"
#define CONFIG_ARDUINO_IDF_BRANCH "master"

View File

@ -13,12 +13,11 @@
#include "soc/i2s_periph.h"
#include "soc/rtc_periph.h"
#include "soc/soc_caps.h"
#include "hal/i2s_hal.h"
#include "hal/i2s_types.h"
#include "driver/periph_ctrl.h"
#include "esp_intr_alloc.h"
#if SOC_I2S_SUPPORTS_ADC_DAC
#if SOC_I2S_SUPPORTS_ADC
#include "driver/adc.h"
#endif
@ -28,7 +27,119 @@ extern "C" {
#define I2S_PIN_NO_CHANGE (-1) /*!< Use in i2s_pin_config_t for pins which should not be changed */
typedef intr_handle_t i2s_isr_handle_t;
/**
* @brief I2S port number, the max port number is (I2S_NUM_MAX -1).
*/
typedef enum {
I2S_NUM_0 = 0, /*!< I2S port 0 */
#if SOC_I2S_NUM > 1
I2S_NUM_1 = 1, /*!< I2S port 1 */
#endif
I2S_NUM_MAX, /*!< I2S port max */
} i2s_port_t;
#if SOC_I2S_SUPPORTS_PCM
/**
* @brief I2S PCM configuration
*
*/
typedef struct {
i2s_pcm_compress_t pcm_type; /*!< I2S PCM a/u-law decompress or compress type */
} i2s_pcm_cfg_t;
#endif
#if SOC_I2S_SUPPORTS_PDM_TX
/**
* @brief Default I2S PDM Up-Sampling Rate configuration
*/
#define I2S_PDM_DEFAULT_UPSAMPLE_CONFIG(rate) { \
.sample_rate = rate, \
.fp = 960, \
.fs = (rate) / 100, \
}
/**
* @brief I2S PDM up-sample rate configuration
* @note TX PDM can only be set to the following two upsampling rate configurations:
* 1: fp = 960, fs = sample_rate / 100, in this case, Fpdm = 128*48000
* 2: fp = 960, fs = 480, in this case, Fpdm = 128*Fpcm = 128*sample_rate
* If the pdm receiver do not care the pdm serial clock, it's recommended set Fpdm = 128*48000.
* Otherwise, the second configuration should be applied.
*/
typedef struct {
int sample_rate; /*!< I2S PDM sample rate */
int fp; /*!< I2S PDM TX upsampling paramater. Normally it should be set to 960 */
int fs; /*!< I2S PDM TX upsampling paramater. When it is set to 480, the pdm clock frequency Fpdm = 128 * sample_rate, when it is set to sample_rate / 100 Fpdm will be fixed to 128*48000 */
} i2s_pdm_tx_upsample_cfg_t;
#endif
/**
* @brief I2S pin number for i2s_set_pin
*
*/
typedef struct {
int mck_io_num; /*!< MCK in out pin*/
int bck_io_num; /*!< BCK in out pin*/
int ws_io_num; /*!< WS in out pin*/
int data_out_num; /*!< DATA out pin*/
int data_in_num; /*!< DATA in pin*/
} i2s_pin_config_t;
/**
* @brief I2S driver configuration parameters
*
*/
typedef struct {
i2s_mode_t mode; /*!< I2S work mode */
uint32_t sample_rate; /*!< I2S sample rate */
i2s_bits_per_sample_t bits_per_sample; /*!< I2S sample bits in one channel */
i2s_channel_fmt_t channel_format; /*!< I2S channel format.*/
i2s_comm_format_t communication_format; /*!< I2S communication format */
int intr_alloc_flags; /*!< Flags used to allocate the interrupt. One or multiple (ORred) ESP_INTR_FLAG_* values. See esp_intr_alloc.h for more info */
int dma_buf_count; /*!< I2S DMA Buffer Count */
int dma_buf_len; /*!< I2S DMA Buffer Length */
bool use_apll; /*!< I2S using APLL as main I2S clock, enable it to get accurate clock */
bool tx_desc_auto_clear; /*!< I2S auto clear tx descriptor if there is underflow condition (helps in avoiding noise in case of data unavailability) */
int fixed_mclk; /*!< I2S using fixed MCLK output. If use_apll = true and fixed_mclk > 0, then the clock output for i2s is fixed and equal to the fixed_mclk value. If fixed_mclk set, mclk_multiple won't take effect */
i2s_mclk_multiple_t mclk_multiple; /*!< The multiple of I2S master clock(MCLK) to sample rate */
i2s_bits_per_chan_t bits_per_chan; /*!< I2S total bits in one channel only take effect when larger than 'bits_per_sample', default '0' means equal to 'bits_per_sample' */
#if SOC_I2S_SUPPORTS_TDM
i2s_channel_t chan_mask; /*!< I2S active channel bit mask, set value in `i2s_channel_t` to enable specific channel, the bit map of active channel can not exceed (0x1<<total_chan). */
uint32_t total_chan; /*!< I2S Total number of channels. If it is smaller than the biggest active channel number, it will be set to this number automatically. */
bool left_align; /*!< Set to enable left alignment */
bool big_edin; /*!< Set to enable big edin */
bool bit_order_msb; /*!< Set to enable msb order */
bool skip_msk; /*!< Set to enable skip mask. If it is enabled, only the data of the enabled channels will be sent, otherwise all data stored in DMA TX buffer will be sent */
#endif // SOC_I2S_SUPPORTS_TDM
} i2s_driver_config_t;
typedef i2s_driver_config_t i2s_config_t; // for backward compatible
typedef intr_handle_t i2s_isr_handle_t; // for backward compatible
/**
* @brief I2S event queue types
*
*/
typedef enum {
I2S_EVENT_DMA_ERROR,
I2S_EVENT_TX_DONE, /*!< I2S DMA finish sent 1 buffer*/
I2S_EVENT_RX_DONE, /*!< I2S DMA finish received 1 buffer*/
I2S_EVENT_TX_Q_OVF, /*!< I2S DMA sent queue overflow*/
I2S_EVENT_RX_Q_OVF, /*!< I2S DMA receive queue overflow*/
I2S_EVENT_MAX, /*!< I2S event max index*/
} i2s_event_type_t;
/**
* @brief Event structure used in I2S event queue
*
*/
typedef struct {
i2s_event_type_t type; /*!< I2S event type */
size_t size; /*!< I2S data size for I2S_DATA event*/
} i2s_event_t;
/**
* @brief Set I2S pin number
@ -37,7 +148,7 @@ typedef intr_handle_t i2s_isr_handle_t;
* The I2S peripheral output signals can be connected to multiple GPIO pads.
* However, the I2S peripheral input signal can only be connected to one GPIO pad.
*
* @param i2s_num I2S_NUM_0 or I2S_NUM_1
* @param i2s_num I2S port number
*
* @param pin I2S Pin structure, or NULL to set 2-channel 8-bit internal DAC pin configuration (GPIO25 & GPIO26)
*
@ -54,15 +165,15 @@ typedef intr_handle_t i2s_isr_handle_t;
*/
esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin);
#if SOC_I2S_SUPPORTS_PDM
#if SOC_I2S_SUPPORTS_PDM_RX
/**
* @brief Set PDM mode down-sample rate
* In PDM RX mode, there would be 2 rounds of downsample process in hardware.
* In the first downsample process, the sampling number can be 16 or 8.
* In the second downsample process, the sampling number is fixed as 8.
* So the clock frequency in PDM RX mode would be (fpcm * 64) or (fpcm * 128) accordingly.
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param dsr i2s RX down sample rate for PDM mode.
* @param i2s_num I2S port number
* @param downsample i2s RX down sample rate for PDM mode.
*
* @note After calling this function, it would call i2s_set_clk inside to update the clock frequency.
* Please call this function after I2S driver has been initialized.
@ -72,13 +183,31 @@ esp_err_t i2s_set_pin(i2s_port_t i2s_num, const i2s_pin_config_t *pin);
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_NO_MEM Out of memory
*/
esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr);
esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t downsample);
#endif
#if SOC_I2S_SUPPORTS_PDM_TX
/**
* @brief Set TX PDM mode up-sample rate
* @note If you have set PDM mode while calling 'i2s_driver_install',
* default PDM TX upsample parameters have already been set,
* no need to call this function again if you don't have to change the default configuration
*
* @param i2s_num I2S port number
* @param upsample_cfg Set I2S PDM up-sample rate configuration
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_NO_MEM Out of memory
*/
esp_err_t i2s_set_pdm_tx_up_sample(i2s_port_t i2s_num, const i2s_pdm_tx_upsample_cfg_t *upsample_cfg);
#endif
/**
* @brief Install and start I2S driver.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @param i2s_config I2S configurations - see i2s_config_t struct
*
@ -92,24 +221,26 @@ esp_err_t i2s_set_pdm_rx_down_sample(i2s_port_t i2s_num, i2s_pdm_dsr_t dsr);
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_NO_MEM Out of memory
* - ESP_ERR_INVALID_STATE Current I2S port is in use
*/
esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void* i2s_queue);
esp_err_t i2s_driver_install(i2s_port_t i2s_num, const i2s_config_t *i2s_config, int queue_size, void *i2s_queue);
/**
* @brief Uninstall I2S driver.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_INVALID_STATE I2S port has been uninstalled by others (e.g. LCD i80)
*/
esp_err_t i2s_driver_uninstall(i2s_port_t i2s_num);
/**
* @brief Write data to I2S DMA transmit buffer.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @param src Source address to write from
*
@ -133,7 +264,7 @@ esp_err_t i2s_write(i2s_port_t i2s_num, const void *src, size_t size, size_t *by
/**
* @brief Write data to I2S DMA transmit buffer while expanding the number of bits per sample. For example, expanding 16-bit PCM to 32-bit PCM.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @param src Source address to write from
*
@ -164,7 +295,7 @@ esp_err_t i2s_write_expand(i2s_port_t i2s_num, const void *src, size_t size, siz
/**
* @brief Read data from I2S DMA receive buffer
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @param dest Destination address to read into
*
@ -190,7 +321,7 @@ esp_err_t i2s_read(i2s_port_t i2s_num, void *dest, size_t size, size_t *bytes_re
*
* `bit_clock = rate * (number of channels) * bits_per_sample`
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @param rate I2S sample rate (ex: 8000, 44100...)
*
@ -208,7 +339,7 @@ esp_err_t i2s_set_sample_rates(i2s_port_t i2s_num, uint32_t rate);
*
* Disables I2S TX/RX, until i2s_start() is called.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @return
* - ESP_OK Success
@ -222,9 +353,9 @@ esp_err_t i2s_stop(i2s_port_t i2s_num);
* It is not necessary to call this function after i2s_driver_install() (it is started automatically), however it is necessary to call it after i2s_stop().
*
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @return
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
@ -235,7 +366,7 @@ esp_err_t i2s_start(i2s_port_t i2s_num);
*
* Pushes zero-byte samples into the TX DMA buffer, until it is full.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @return
* - ESP_OK Success
@ -243,37 +374,63 @@ esp_err_t i2s_start(i2s_port_t i2s_num);
*/
esp_err_t i2s_zero_dma_buffer(i2s_port_t i2s_num);
#if SOC_I2S_SUPPORTS_PCM
/**
* @brief Configure I2S a/u-law decompress or compress
*
* @note This function should be called after i2s driver installed
* Only take effecttive when the i2s 'communication_format' is set to 'I2S_COMM_FORMAT_STAND_PCM_SHORT' or 'I2S_COMM_FORMAT_STAND_PCM_LONG'
*
* @param i2s_num I2S port number
*
* @param pcm_cfg including mode selection and a/u-law decompress or compress configuration paramater
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_pcm_config(i2s_port_t i2s_num, const i2s_pcm_cfg_t *pcm_cfg);
#endif
/**
* @brief Set clock & bit width used for I2S RX and TX.
*
* Similar to i2s_set_sample_rates(), but also sets bit width.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* 1. stop i2s;
* 2. calculate mclk, bck, bck_factor
* 3. malloc dma buffer;
* 4. start i2s
*
* @param i2s_num I2S port number
*
* @param rate I2S sample rate (ex: 8000, 44100...)
*
* @param bits I2S bit width (I2S_BITS_PER_SAMPLE_16BIT, I2S_BITS_PER_SAMPLE_24BIT, I2S_BITS_PER_SAMPLE_32BIT)
* @param bits_cfg I2S bits configuration
* the low 16 bits is for data bits per sample in one channel (see 'i2s_bits_per_sample_t')
* the high 16 bits is for total bits in one channel (see 'i2s_bits_per_chan_t')
* high 16bits =0 means same as the bits per sample.
*
* @param ch I2S channel, (I2S_CHANNEL_MONO, I2S_CHANNEL_STEREO)
* @param ch I2S channel, (I2S_CHANNEL_MONO, I2S_CHANNEL_STEREO or specific channel in TDM mode)
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_ERR_NO_MEM Out of memory
*/
esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, i2s_bits_per_sample_t bits, i2s_channel_t ch);
esp_err_t i2s_set_clk(i2s_port_t i2s_num, uint32_t rate, uint32_t bits_cfg, i2s_channel_t ch);
/**
* @brief get clock set on particular port number.
*
* @param i2s_num I2S_NUM_0, I2S_NUM_1
* @param i2s_num I2S port number
*
* @return
* - actual clock set by i2s driver
*/
float i2s_get_clk(i2s_port_t i2s_num);
#if SOC_I2S_SUPPORTS_ADC_DAC
#if SOC_I2S_SUPPORTS_ADC
/**
* @brief Set built-in ADC mode for I2S DMA, this function will initialize ADC pad,
* and set ADC parameters.
@ -309,7 +466,9 @@ esp_err_t i2s_adc_enable(i2s_port_t i2s_num);
* - ESP_ERR_INVALID_STATE Driver state error
*/
esp_err_t i2s_adc_disable(i2s_port_t i2s_num);
#endif // SOC_I2S_SUPPORTS_ADC
#if SOC_I2S_SUPPORTS_DAC
/**
* @brief Set I2S dac mode, I2S built-in DAC is disabled by default
*
@ -324,7 +483,7 @@ esp_err_t i2s_adc_disable(i2s_port_t i2s_num);
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t i2s_set_dac_mode(i2s_dac_mode_t dac_mode);
#endif //SOC_I2S_SUPPORTS_ADC_DAC
#endif //SOC_I2S_SUPPORTS_DAC
#ifdef __cplusplus

View File

@ -24,6 +24,38 @@ extern "C" {
typedef intr_handle_t ledc_isr_handle_t;
/**
* @brief LEDC callback event type
*/
typedef enum {
LEDC_FADE_END_EVT /**< LEDC fade end event */
} ledc_cb_event_t;
/**
* @brief LEDC callback parameter
*/
typedef struct {
ledc_cb_event_t event; /**< Event name */
uint32_t speed_mode; /**< Speed mode of the LEDC channel group */
uint32_t channel; /**< LEDC channel (0 - LEDC_CHANNEL_MAX-1) */
uint32_t duty; /**< LEDC current duty of the channel, the range of duty is [0, (2**duty_resolution) - 1] */
} ledc_cb_param_t;
/**
* @brief Type of LEDC event callback
* @param param LEDC callback parameter
* @param user_arg User registered data
*/
typedef bool (* ledc_cb_t)(const ledc_cb_param_t *param, void *user_arg);
/**
* @brief Group of supported LEDC callbacks
* @note The callbacks are all running under ISR environment
*/
typedef struct {
ledc_cb_t fade_cb; /**< LEDC fade_end callback function */
} ledc_cbs_t;
/**
* @brief LEDC channel configuration
* Configure LEDC channel with the given channel/output gpio_num/interrupt/source timer/frequency(Hz)/LEDC duty resolution
@ -57,7 +89,7 @@ esp_err_t ledc_timer_config(const ledc_timer_config_t* timer_conf);
* control one LEDC channel in different tasks at the same time.
* A thread-safe version of API is ledc_set_duty_and_update
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
*
* @return
* - ESP_OK Success
@ -72,7 +104,7 @@ esp_err_t ledc_update_duty(ledc_mode_t speed_mode, ledc_channel_t channel);
*
* @param gpio_num The LEDC output gpio
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param ledc_channel LEDC channel (0-7), select from ledc_channel_t
* @param ledc_channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
*
* @return
* - ESP_OK Success
@ -85,7 +117,7 @@ esp_err_t ledc_set_pin(int gpio_num, ledc_mode_t speed_mode, ledc_channel_t ledc
* Disable LEDC output, and set idle level
*
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param idle_level Set output idle level after LEDC stops.
*
* @return
@ -129,8 +161,8 @@ uint32_t ledc_get_freq(ledc_mode_t speed_mode, ledc_timer_t timer_num);
* @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
* Other duty operations will have to wait until the fade operation has finished.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]
* @param hpoint Set the LEDC hpoint value(max: 0xfffff)
*
* @return
@ -143,7 +175,7 @@ esp_err_t ledc_set_duty_with_hpoint(ledc_mode_t speed_mode, ledc_channel_t chann
* @brief LEDC get hpoint value, the counter value when the output is set high level.
*
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @return
* - LEDC_ERR_VAL if parameter error
* - Others Current hpoint value of LEDC channel
@ -160,8 +192,8 @@ int ledc_get_hpoint(ledc_mode_t speed_mode, ledc_channel_t channel);
* @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
* Other duty operations will have to wait until the fade operation has finished.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]
*
* @return
* - ESP_OK Success
@ -173,7 +205,7 @@ esp_err_t ledc_set_duty(ledc_mode_t speed_mode, ledc_channel_t channel, uint32_t
* @brief LEDC get duty
*
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
*
* @return
* - LEDC_ERR_DUTY if parameter error
@ -187,8 +219,8 @@ uint32_t ledc_get_duty(ledc_mode_t speed_mode, ledc_channel_t channel);
* @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
* Other duty operations will have to wait until the fade operation has finished.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution)]
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param duty Set the start of the gradient duty, the range of duty setting is [0, (2**duty_resolution) - 1]
* @param fade_direction Set the direction of the gradient
* @param step_num Set the number of the gradient
* @param duty_cycle_num Set how many LEDC tick each time the gradient lasts
@ -274,7 +306,7 @@ esp_err_t ledc_timer_resume(ledc_mode_t speed_mode, ledc_timer_t timer_sel);
* @brief Bind LEDC channel with the selected timer
*
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel index (0-7), select from ledc_channel_t
* @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param timer_sel LEDC timer index (0-3), select from ledc_timer_t
*
* @return
@ -293,7 +325,7 @@ esp_err_t ledc_bind_channel_timer(ledc_mode_t speed_mode, ledc_channel_t channel
* @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
* Other duty operations will have to wait until the fade operation has finished.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. ,
* @param channel LEDC channel index (0-7), select from ledc_channel_t
* @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
* @param scale Controls the increase or decrease step scale.
* @param cycle_num increase or decrease the duty every cycle_num cycles
@ -316,8 +348,8 @@ esp_err_t ledc_set_fade_with_step(ledc_mode_t speed_mode, ledc_channel_t channel
* @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
* Other duty operations will have to wait until the fade operation has finished.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode. ,
* @param channel LEDC channel index (0-7), select from ledc_channel_t
* @param target_duty Target duty of fading.( 0 - (2 ** duty_resolution - 1)))
* @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
* @param max_fade_time_ms The maximum time of the fading ( ms ).
*
* @return
@ -368,8 +400,8 @@ esp_err_t ledc_fade_start(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_f
* Other duty operations will have to wait until the fade operation has finished.
*
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel (0-7), select from ledc_channel_t
* @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution)]
* @param channel LEDC channel (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param duty Set the LEDC duty, the range of duty setting is [0, (2**duty_resolution) - 1]
* @param hpoint Set the LEDC hpoint value(max: 0xfffff)
*
*/
@ -381,8 +413,8 @@ esp_err_t ledc_set_duty_and_update(ledc_mode_t speed_mode, ledc_channel_t channe
* @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
* Other duty operations will have to wait until the fade operation has finished.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel index (0-7), select from ledc_channel_t
* @param target_duty Target duty of fading.( 0 - (2 ** duty_resolution - 1)))
* @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
* @param max_fade_time_ms The maximum time of the fading ( ms ).
* @param fade_mode choose blocking or non-blocking mode
* @return
@ -399,7 +431,7 @@ esp_err_t ledc_set_fade_time_and_start(ledc_mode_t speed_mode, ledc_channel_t ch
* @note If a fade operation is running in progress on that channel, the driver would not allow it to be stopped.
* Other duty operations will have to wait until the fade operation has finished.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel index (0-7), select from ledc_channel_t
* @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param target_duty Target duty of fading [0, (2**duty_resolution) - 1]
* @param scale Controls the increase or decrease step scale.
* @param cycle_num increase or decrease the duty every cycle_num cycles
@ -414,3 +446,18 @@ esp_err_t ledc_set_fade_step_and_start(ledc_mode_t speed_mode, ledc_channel_t ch
#ifdef __cplusplus
}
#endif
/**
* @brief LEDC callback registration function
* @note The callback is called from an ISR, it must never attempt to block, and any FreeRTOS API called must be ISR capable.
* @param speed_mode Select the LEDC channel group with specified speed mode. Note that not all targets support high speed mode.
* @param channel LEDC channel index (0 - LEDC_CHANNEL_MAX-1), select from ledc_channel_t
* @param cbs Group of LEDC callback functions
* @param user_arg user registered data for the callback function
* @return
* - ESP_ERR_INVALID_ARG Parameter error
* - ESP_OK Success
* - ESP_ERR_INVALID_STATE Fade function not installed.
* - ESP_FAIL Fade function init error
*/
esp_err_t ledc_cb_register(ledc_mode_t speed_mode, ledc_channel_t channel, ledc_cbs_t *cbs, void *user_arg);

View File

@ -11,7 +11,6 @@
#include "esp_err.h"
#include "soc/soc.h"
#include "driver/gpio.h"
#include "driver/periph_ctrl.h"
#include "esp_intr_alloc.h"
#include "hal/mcpwm_types.h"
@ -46,8 +45,7 @@ typedef enum {
} mcpwm_io_signals_t;
/**
* @brief MCPWM pin number for
*
* @brief pin number for MCPWM
*/
typedef struct {
int mcpwm0a_out_num; /*!<MCPWM0A out pin*/
@ -71,9 +69,9 @@ typedef struct {
* @brief Select MCPWM unit
*/
typedef enum {
MCPWM_UNIT_0 = 0, /*!<MCPWM unit0 selected*/
MCPWM_UNIT_1, /*!<MCPWM unit1 selected*/
MCPWM_UNIT_MAX, /*!<Num of MCPWM units on ESP32*/
MCPWM_UNIT_0, /*!<MCPWM unit0 selected*/
MCPWM_UNIT_1, /*!<MCPWM unit1 selected*/
MCPWM_UNIT_MAX, /*!<Max number of MCPWM units*/
} mcpwm_unit_t;
_Static_assert(MCPWM_UNIT_MAX == SOC_MCPWM_GROUPS, "MCPWM unit number not equal to chip capabilities");
@ -82,19 +80,19 @@ _Static_assert(MCPWM_UNIT_MAX == SOC_MCPWM_GROUPS, "MCPWM unit number not equal
* @brief Select MCPWM timer
*/
typedef enum {
MCPWM_TIMER_0 = 0, /*!<Select MCPWM timer0*/
MCPWM_TIMER_1, /*!<Select MCPWM timer1*/
MCPWM_TIMER_2, /*!<Select MCPWM timer2*/
MCPWM_TIMER_MAX, /*!<Num of MCPWM timers on ESP32*/
MCPWM_TIMER_0, /*!<Select MCPWM timer0*/
MCPWM_TIMER_1, /*!<Select MCPWM timer1*/
MCPWM_TIMER_2, /*!<Select MCPWM timer2*/
MCPWM_TIMER_MAX, /*!<Max number of timers in a unit*/
} mcpwm_timer_t;
/**
* @brief Select MCPWM operator
*/
typedef enum {
MCPWM_GEN_A = 0, /*!<Select MCPWMXA, where 'X' is operator number*/
MCPWM_GEN_B, /*!<Select MCPWMXB, where 'X' is operator number*/
MCPWM_GEN_MAX, /*!<Num of generators to each operator of MCPWM*/
MCPWM_GEN_A, /*!<Select MCPWMXA, where 'X' is operator number*/
MCPWM_GEN_B, /*!<Select MCPWMXB, where 'X' is operator number*/
MCPWM_GEN_MAX, /*!<Num of generators to each operator of MCPWM*/
} mcpwm_generator_t;
//definitions and macros to be back-compatible before IDFv4.1
@ -107,42 +105,61 @@ typedef mcpwm_generator_t mcpwm_operator_t; ///< @deprecated
* @brief MCPWM carrier oneshot mode, in this mode the width of the first pulse of carrier can be programmed
*/
typedef enum {
MCPWM_ONESHOT_MODE_DIS = 0, /*!<Enable oneshot mode*/
MCPWM_ONESHOT_MODE_EN, /*!<Disable oneshot mode*/
MCPWM_ONESHOT_MODE_DIS, /*!<Enable oneshot mode*/
MCPWM_ONESHOT_MODE_EN, /*!<Disable oneshot mode*/
} mcpwm_carrier_os_t;
/**
* @brief MCPWM carrier output inversion, high frequency carrier signal active with MCPWM signal is high
*/
typedef enum {
MCPWM_CARRIER_OUT_IVT_DIS = 0, /*!<Enable carrier output inversion*/
MCPWM_CARRIER_OUT_IVT_EN, /*!<Disable carrier output inversion*/
MCPWM_CARRIER_OUT_IVT_DIS, /*!<Enable carrier output inversion*/
MCPWM_CARRIER_OUT_IVT_EN, /*!<Disable carrier output inversion*/
} mcpwm_carrier_out_ivt_t;
/**
* @brief MCPWM select fault signal input
*/
typedef enum {
MCPWM_SELECT_F0 = 0, /*!<Select F0 as input*/
MCPWM_SELECT_F1, /*!<Select F1 as input*/
MCPWM_SELECT_F2, /*!<Select F2 as input*/
MCPWM_SELECT_F0, /*!<Select F0 as input*/
MCPWM_SELECT_F1, /*!<Select F1 as input*/
MCPWM_SELECT_F2, /*!<Select F2 as input*/
} mcpwm_fault_signal_t;
/**
* @brief MCPWM select sync signal input
*/
typedef enum {
MCPWM_SELECT_SYNC0 = 4, /*!<Select SYNC0 as input*/
MCPWM_SELECT_SYNC1, /*!<Select SYNC1 as input*/
MCPWM_SELECT_SYNC2, /*!<Select SYNC2 as input*/
MCPWM_SELECT_NO_INPUT, /*!<No sync input selected*/
MCPWM_SELECT_TIMER0_SYNC, /*!<Select software sync signal from timer0 as input*/
MCPWM_SELECT_TIMER1_SYNC, /*!<Select software sync signal from timer1 as input*/
MCPWM_SELECT_TIMER2_SYNC, /*!<Select software sync signal from timer2 as input*/
MCPWM_SELECT_GPIO_SYNC0, /*!<Select GPIO SYNC0 as input*/
MCPWM_SELECT_GPIO_SYNC1, /*!<Select GPIO SYNC1 as input*/
MCPWM_SELECT_GPIO_SYNC2, /*!<Select GPIO SYNC2 as input*/
} mcpwm_sync_signal_t;
// backward compatibility
#define MCPWM_SELCT_SYNC0 MCPWM_SELCT_GPIO_SYNC0
#define MCPWM_SELCT_SYNC1 MCPWM_SELCT_GPIO_SYNC1
#define MCPWM_SELCT_SYNC2 MCPWM_SELCT_GPIO_SYNC2
/**
* @brief MCPWM timer sync event trigger
*/
typedef enum {
MCPWM_SWSYNC_SOURCE_SYNCIN, /*!<the input sync signal will be routed to its sync output path*/
MCPWM_SWSYNC_SOURCE_TEZ, /*!<sync signal generated when timer counts to zero*/
MCPWM_SWSYNC_SOURCE_TEP, /*!<sync signal generated when timer counts to peak*/
MCPWM_SWSYNC_SOURCE_DISABLED, /*!<timer does not generate sync signals*/
} mcpwm_timer_sync_trigger_t;
/**
* @brief MCPWM select triggering level of fault signal
*/
typedef enum {
MCPWM_LOW_LEVEL_TGR = 0, /*!<Fault condition occurs when fault input signal goes from high to low, currently not supported*/
MCPWM_HIGH_LEVEL_TGR, /*!<Fault condition occurs when fault input signal goes low to high*/
MCPWM_LOW_LEVEL_TGR, /*!<Fault condition occurs when fault input signal goes from high to low*/
MCPWM_HIGH_LEVEL_TGR, /*!<Fault condition occurs when fault input signal goes low to high*/
} mcpwm_fault_input_level_t;
/**
@ -154,6 +171,16 @@ typedef enum {
MCPWM_BOTH_EDGE = BIT(1) | BIT(0), /*!<Capture both edges*/
} mcpwm_capture_on_edge_t;
/**
* @brief Interrupt masks for MCPWM capture
*/
__attribute__ ((deprecated("please use callback function to avoid directly accessing registers")))
typedef enum {
MCPWM_LL_INTR_CAP0 = BIT(27), ///< Capture 0 happened
MCPWM_LL_INTR_CAP1 = BIT(28), ///< Capture 1 happened
MCPWM_LL_INTR_CAP2 = BIT(29), ///< Capture 2 happened
} mcpwm_intr_t;
/**
* @brief Select type of MCPWM counter
*/
@ -181,15 +208,15 @@ typedef enum {
*/
typedef enum {
MCPWM_DEADTIME_BYPASS = 0, /*!<Bypass the deadtime*/
MCPWM_BYPASS_RED, /*!<MCPWMXA = no change, MCPWMXB = falling edge delay*/
MCPWM_BYPASS_FED, /*!<MCPWMXA = rising edge delay, MCPWMXB = no change*/
MCPWM_ACTIVE_HIGH_MODE, /*!<MCPWMXA = rising edge delay, MCPWMXB = falling edge delay*/
MCPWM_ACTIVE_LOW_MODE, /*!<MCPWMXA = compliment of rising edge delay, MCPWMXB = compliment of falling edge delay*/
MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, /*!<MCPWMXA = rising edge delay, MCPWMXB = compliment of falling edge delay*/
MCPWM_ACTIVE_LOW_COMPLIMENT_MODE, /*!<MCPWMXA = compliment of rising edge delay, MCPWMXB = falling edge delay*/
MCPWM_ACTIVE_RED_FED_FROM_PWMXA, /*!<MCPWMXA = MCPWMXB = rising edge delay as well as falling edge delay, generated from MCPWMXA*/
MCPWM_ACTIVE_RED_FED_FROM_PWMXB, /*!<MCPWMXA = MCPWMXB = rising edge delay as well as falling edge delay, generated from MCPWMXB*/
MCPWM_DEADTIME_TYPE_MAX,
MCPWM_BYPASS_RED, /*!<MCPWMXA Out = MCPWMXA In with no delay, MCPWMXB Out = MCPWMXA In with falling edge delay*/
MCPWM_BYPASS_FED, /*!<MCPWMXA Out = MCPWMXA In with rising edge delay, MCPWMXB Out = MCPWMXB In with no delay*/
MCPWM_ACTIVE_HIGH_MODE, /*!<MCPWMXA Out = MCPWMXA In with rising edge delay, MCPWMXB Out = MCPWMXA In with falling edge delay*/
MCPWM_ACTIVE_LOW_MODE, /*!<MCPWMXA Out = MCPWMXA In with compliment of rising edge delay, MCPWMXB Out = MCPWMXA In with compliment of falling edge delay*/
MCPWM_ACTIVE_HIGH_COMPLIMENT_MODE, /*!<MCPWMXA Out = MCPWMXA In with rising edge delay, MCPWMXB = MCPWMXA In with compliment of falling edge delay*/
MCPWM_ACTIVE_LOW_COMPLIMENT_MODE, /*!<MCPWMXA Out = MCPWMXA In with compliment of rising edge delay, MCPWMXB Out = MCPWMXA In with falling edge delay*/
MCPWM_ACTIVE_RED_FED_FROM_PWMXA, /*!<MCPWMXA Out = MCPWMXB Out = MCPWMXA In with rising edge delay as well as falling edge delay*/
MCPWM_ACTIVE_RED_FED_FROM_PWMXB, /*!<MCPWMXA Out = MCPWMXB Out = MCPWMXB In with rising edge delay as well as falling edge delay*/
MCPWM_DEADTIME_TYPE_MAX, /*!<Maximum number of supported dead time modes*/
} mcpwm_deadtime_type_t;
/**
@ -220,11 +247,40 @@ typedef mcpwm_output_action_t mcpwm_action_on_pwmxb_t;
* @brief MCPWM select capture signal input
*/
typedef enum {
MCPWM_SELECT_CAP0 = 0, /*!<Select CAP0 as input*/
MCPWM_SELECT_CAP1, /*!<Select CAP1 as input*/
MCPWM_SELECT_CAP2, /*!<Select CAP2 as input*/
MCPWM_SELECT_CAP0, /*!<Select CAP0 as input*/
MCPWM_SELECT_CAP1, /*!<Select CAP1 as input*/
MCPWM_SELECT_CAP2, /*!<Select CAP2 as input*/
} mcpwm_capture_signal_t;
/**
* @brief MCPWM capture channel ID alias
*/
typedef mcpwm_capture_signal_t mcpwm_capture_channel_id_t;
/**
* @brief event data that will be passed into ISR callback
*/
typedef struct {
mcpwm_capture_on_edge_t cap_edge; /*!<Which signal edge is detected*/
uint32_t cap_value; /*!<Corresponding timestamp when event occurs. Clock rate = APB(usually 80M)*/
} cap_event_data_t;
/**
* @brief Type of capture event callback
* @param mcpwm MCPWM unit(0-1)
* @param cap_channel capture channel ID
* @param edata Capture event data, contains capture edge and capture value, fed by the driver
* @param user_data User registered data, passed from `mcpwm_capture_config_t`
*
* @note Since this an ISR callback so do not do anything that may block and call APIs that is designed to be used within ISR(usually has '_ISR' postfix)
*
* @return Whether a task switch is needed after the callback function returns,
* this is usually due to the callback wakes up some high priority task.
*
*/
typedef bool (*cap_isr_cb_t)(mcpwm_unit_t mcpwm, mcpwm_capture_channel_id_t cap_channel, const cap_event_data_t *edata,
void *user_data);
/**
* @brief MCPWM config structure
*/
@ -237,7 +293,7 @@ typedef struct {
} mcpwm_config_t;
/**
* @brief MCPWM config carrier structure
* @brief MCPWM carrier configuration structure
*/
typedef struct {
uint8_t carrier_period; /*!<Set carrier period = (carrier_period + 1)*800ns, carrier_period should be < 16*/
@ -247,10 +303,29 @@ typedef struct {
mcpwm_carrier_out_ivt_t carrier_ivt_mode; /*!<Invert output of carrier*/
} mcpwm_carrier_config_t;
/**
* @brief MCPWM config capture structure
*/
typedef struct {
mcpwm_capture_on_edge_t cap_edge; /*!<Set capture edge*/
uint32_t cap_prescale; /*!<Prescale of capture signal, ranging from 1 to 256*/
cap_isr_cb_t capture_cb; /*!<User defined capture event callback, running under interrupt context */
void *user_data; /*!<User defined ISR callback function args*/
} mcpwm_capture_config_t;
/**
* @brief MCPWM config sync structure
*/
typedef struct {
mcpwm_sync_signal_t sync_sig; /*!<Set sync input signal that will cause timer to sync*/
uint32_t timer_val; /*!<Counter value to be set after sync, in 0 ~ 999, unit: 1 / 1000 * peak*/
mcpwm_timer_direction_t count_direction; /*!<Counting direction to be set after sync */
} mcpwm_sync_config_t;
/**
* @brief This function initializes each gpio signal for MCPWM
* @note
* This function initializes one gpio at a time.
*
* @note This function initializes one gpio at a time.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param io_signal set MCPWM signals, each MCPWM unit has 6 output(MCPWMXA, MCPWMXB) and 9 input(SYNC_X, FAULT_X, CAP_X)
@ -265,8 +340,8 @@ esp_err_t mcpwm_gpio_init(mcpwm_unit_t mcpwm_num, mcpwm_io_signals_t io_signal,
/**
* @brief Initialize MCPWM gpio structure
* @note
* This function can be used to initialize more then one gpio at a time.
*
* @note This function initialize a group of MCPWM GPIOs at a time.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param mcpwm_pin MCPWM pin structure
@ -279,6 +354,10 @@ esp_err_t mcpwm_set_pin(mcpwm_unit_t mcpwm_num, const mcpwm_pin_config_t *mcpwm_
/**
* @brief Initialize MCPWM parameters
* @note
* The default resolution configured for MCPWM group and timer are 160M / 16 = 10M and 10M / 10 = 1M
* The default resolution can be changed by calling mcpwm_group_set_resolution() and mcpwm_timer_set_resolution(),
* before calling this function.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param timer_num set timer number(0-2) of MCPWM, each MCPWM unit has 3 timers.
@ -290,6 +369,39 @@ esp_err_t mcpwm_set_pin(mcpwm_unit_t mcpwm_num, const mcpwm_pin_config_t *mcpwm_
*/
esp_err_t mcpwm_init( mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, const mcpwm_config_t *mcpwm_conf);
/**
* @brief Set resolution of the MCPWM group
* @note
* This will override default resolution of group(=10,000,000).
* This WILL NOT automatically update frequency and duty. Call mcpwm_set_frequency() and mcpwm_set_duty() manually
* to set them back.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param resolution set expected frequency resolution
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t mcpwm_group_set_resolution(mcpwm_unit_t mcpwm_num, unsigned long int resolution);
/**
* @brief Set resolution of each timer
* @note
* This WILL override default resolution of timer(=1,000,000).
* This WILL NOT automatically update frequency and duty. Call mcpwm_set_frequency() and mcpwm_set_duty() manually
* to set them back.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param timer_num set timer number(0-2) of MCPWM, each MCPWM unit has 3 timers
* @param resolution set expected frequency resolution
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t mcpwm_timer_set_resolution(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, unsigned long int resolution);
/**
* @brief Set frequency(in Hz) of MCPWM timer
*
@ -333,7 +445,7 @@ esp_err_t mcpwm_set_duty_in_us(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num,
/**
* @brief Set duty either active high or active low(out of phase/inverted)
* @note
* @note
* Call this function every time after mcpwm_set_signal_high or mcpwm_set_signal_low to resume with previously set duty cycle
*
* @param mcpwm_num set MCPWM unit(0-1)
@ -370,6 +482,18 @@ uint32_t mcpwm_get_frequency(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num);
*/
float mcpwm_get_duty(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t gen);
/**
* @brief Get duty cycle of each operator in us
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param timer_num set timer number(0-2) of MCPWM, each MCPWM unit has 3 timers
* @param gen set the generator(MCPWMXA/MCPWMXB), 'x' is operator number selected
*
* @return
* - duty cycle in us of each operator
*/
uint32_t mcpwm_get_duty_in_us(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_operator_t gen);
/**
* @brief Use this function to set MCPWM signal high
*
@ -570,7 +694,7 @@ esp_err_t mcpwm_fault_init(mcpwm_unit_t mcpwm_num, mcpwm_fault_input_level_t int
/**
* @brief Set oneshot mode on fault detection, once fault occur in oneshot mode reset is required to resume MCPWM signals
* @note
* @note
* currently low level triggering is not supported
*
* @param mcpwm_num set MCPWM unit(0-1)
@ -588,7 +712,7 @@ esp_err_t mcpwm_fault_set_oneshot_mode(mcpwm_unit_t mcpwm_num, mcpwm_timer_t tim
/**
* @brief Set cycle-by-cycle mode on fault detection, once fault occur in cyc mode MCPWM signal resumes as soon as fault signal becomes inactive
* @note
* @note
* currently low level triggering is not supported
*
* @param mcpwm_num set MCPWM unit(0-1)
@ -619,20 +743,20 @@ esp_err_t mcpwm_fault_deinit(mcpwm_unit_t mcpwm_num, mcpwm_fault_signal_t fault_
/**
* @brief Initialize capture submodule
*
* @note Enabling capture feature could also enable the capture interrupt,
* @note Enabling capture feature would also enable the capture interrupt event,
* users have to register an interrupt handler by `mcpwm_isr_register`, and in there, query the capture data.
* @note The capture timer uses APB_CLK (typically 80MHz) as the count source.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param cap_edge set capture edge, BIT(0) - negative edge, BIT(1) - positive edge
* @param cap_sig capture pin, which needs to be enabled
* @param num_of_pulse count time between rising/falling edge between 2 *(pulses mentioned), counter uses APB_CLK
* [0~MCPWM_LL_MAX_PRESCALE] (MCPWM_LL_MAX_PRESCALE = 255 on ESP32);
* @param num_of_pulse Input capture signal prescaling, ranges from 0 to 255, representing prescaling from 1 to 256.
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
__attribute__((deprecated("please use mcpwm_capture_enable_channel instead")))
esp_err_t mcpwm_capture_enable(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t cap_sig, mcpwm_capture_on_edge_t cap_edge,
uint32_t num_of_pulse);
@ -646,13 +770,39 @@ esp_err_t mcpwm_capture_enable(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t ca
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
__attribute__((deprecated("please use mcpwm_capture_disable_channel instead")))
esp_err_t mcpwm_capture_disable(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t cap_sig);
/**
* @brief Enable capture channel
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param cap_channel capture channel, which needs to be enabled
* @param cap_conf capture channel configuration
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t mcpwm_capture_enable_channel(mcpwm_unit_t mcpwm_num, mcpwm_capture_channel_id_t cap_channel, const mcpwm_capture_config_t *cap_conf);
/**
* @brief Disable capture channel
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param cap_channel capture channel, which needs to be disabled
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t mcpwm_capture_disable_channel(mcpwm_unit_t mcpwm_num, mcpwm_capture_channel_id_t cap_channel);
/**
* @brief Get capture value
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param cap_sig capture pin on which value is to be measured
* @param cap_sig capture channel on which value is to be measured
*
* @return
* Captured value
@ -663,7 +813,7 @@ uint32_t mcpwm_capture_signal_get_value(mcpwm_unit_t mcpwm_num, mcpwm_capture_si
* @brief Get edge of capture signal
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param cap_sig capture pin of whose edge is to be determined
* @param cap_sig capture channel of whose edge is to be determined
*
* @return
* Capture signal edge: 1 - positive edge, 2 - negtive edge
@ -671,20 +821,36 @@ uint32_t mcpwm_capture_signal_get_value(mcpwm_unit_t mcpwm_num, mcpwm_capture_si
uint32_t mcpwm_capture_signal_get_edge(mcpwm_unit_t mcpwm_num, mcpwm_capture_signal_t cap_sig);
/**
* @brief Initialize sync submodule
* @brief Initialize sync submodule and sets the signal that will cause the timer be loaded with pre-defined value
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param timer_num set timer number(0-2) of MCPWM, each MCPWM unit has 3 timers
* @param sync_sig set the synchronization pin, which needs to be enabled
* @param sync_sig set the synchronization input signal
* @param phase_val phase value in 1/1000 (for 86.7%, phase_val = 867) which timer moves to on sync signal
*
* @note Count direction is undefined within this API
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
__attribute__((deprecated("please use mcpwm_sync_configure() instead")))
esp_err_t mcpwm_sync_enable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_sync_signal_t sync_sig,
uint32_t phase_val);
/**
* @brief Initialize sync submodule and sets the signal that will cause the timer be loaded with pre-defined value
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param timer_num set timer number(0-2) of MCPWM, each MCPWM unit has 3 timers
* @param sync_conf sync configuration on this timer
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t mcpwm_sync_configure(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, const mcpwm_sync_config_t *sync_conf);
/**
* @brief Disable sync submodule on given timer
*
@ -697,6 +863,49 @@ esp_err_t mcpwm_sync_enable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcp
*/
esp_err_t mcpwm_sync_disable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num);
/**
* @brief Set sync output on given timer
* Configures what event triggers MCPWM timer to output a sync signal.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param timer_num set timer number(0-2) of MCPWM, each MCPWM unit has 3 timers
* @param trigger set the trigger that will cause the timer to generate a software sync signal.
* Specifically, `MCPWM_SWSYNC_SOURCE_DISABLED` will disable the timer from generating sync signal.
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Parameter error
*/
esp_err_t mcpwm_set_timer_sync_output(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num, mcpwm_timer_sync_trigger_t trigger);
/**
* @brief Trigger a software sync event and sends it to a specific timer.
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param timer_num set timer number(0-2) of MCPWM, each MCPWM unit has 3 timers
*
* @note This software sync event will have the same effect as hw one, except that:
* - On esp32s3 the soft sync event can be routed to its output if `MCPWM_SWSYNC_SOURCE_SYNCIN` is selected via `mcpwm_set_timer_sync_output()`
* - On esp32 there is no such behavior and soft sync event will only take effect on this timer and can not be propagated to others.
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Function pointer error.
*/
esp_err_t mcpwm_timer_trigger_soft_sync(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num);
/**
* @brief Set external GPIO sync input inverter
*
* @param mcpwm_num set MCPWM unit(0-1)
* @param sync_sig set sync signal of MCPWM, only supports GPIO sync signal
* @param invert whether GPIO sync source input is inverted (to get negative edge trigger)
*
* @return
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Function pointer error.
*/
esp_err_t mcpwm_sync_invert_gpio_synchro(mcpwm_unit_t mcpwm_num, mcpwm_sync_signal_t sync_sig, bool invert);
/**
* @brief Register MCPWM interrupt handler, the handler is an ISR.
* the handler will be attached to the same CPU core that this function is running on.
@ -713,8 +922,9 @@ esp_err_t mcpwm_sync_disable(mcpwm_unit_t mcpwm_num, mcpwm_timer_t timer_num);
* - ESP_OK Success
* - ESP_ERR_INVALID_ARG Function pointer error.
*/
esp_err_t mcpwm_isr_register(mcpwm_unit_t mcpwm_num, void (*fn)(void *), void *arg, int intr_alloc_flags, intr_handle_t *handle);
__attribute__((deprecated("interrupt events are handled by driver, please use callback")))
esp_err_t mcpwm_isr_register(mcpwm_unit_t mcpwm_num, void (*fn)(void *), void *arg, int intr_alloc_flags,
intr_handle_t *handle);
#ifdef __cplusplus
}

View File

@ -6,20 +6,104 @@
#pragma once
#include "freertos/FreeRTOS.h"
#include "esp_types.h"
#include "esp_err.h"
#include "esp_intr_alloc.h"
#include "freertos/FreeRTOS.h"
#include "driver/gpio.h"
#include "soc/soc_caps.h"
#include "hal/pcnt_types.h"
#include "soc/pcnt_periph.h"
#ifdef __cplusplus
extern "C" {
#endif
#define PCNT_PIN_NOT_USED (-1) /*!< When selected for a pin, this pin will not be used */
typedef intr_handle_t pcnt_isr_handle_t;
/**
* @brief PCNT port number, the max port number is (PCNT_PORT_MAX - 1).
*/
typedef enum {
PCNT_PORT_0, /*!< PCNT port 0 */
PCNT_PORT_MAX, /*!< PCNT port max */
} pcnt_port_t;
/**
* @brief Selection of all available PCNT units
*/
typedef enum {
PCNT_UNIT_0, /*!< PCNT unit 0 */
PCNT_UNIT_1, /*!< PCNT unit 1 */
PCNT_UNIT_2, /*!< PCNT unit 2 */
PCNT_UNIT_3, /*!< PCNT unit 3 */
#if SOC_PCNT_UNITS_PER_GROUP > 4
PCNT_UNIT_4, /*!< PCNT unit 4 */
PCNT_UNIT_5, /*!< PCNT unit 5 */
PCNT_UNIT_6, /*!< PCNT unit 6 */
PCNT_UNIT_7, /*!< PCNT unit 7 */
#endif
PCNT_UNIT_MAX,
} pcnt_unit_t;
/**
* @brief Selection of channels available for a single PCNT unit
*/
typedef enum {
PCNT_CHANNEL_0, /*!< PCNT channel 0 */
PCNT_CHANNEL_1, /*!< PCNT channel 1 */
PCNT_CHANNEL_MAX,
} pcnt_channel_t;
/**
* @brief Selection of counter's events the may trigger an interrupt
*/
typedef enum {
PCNT_EVT_THRES_1 = 1 << 2, /*!< PCNT watch point event: threshold1 value event */
PCNT_EVT_THRES_0 = 1 << 3, /*!< PCNT watch point event: threshold0 value event */
PCNT_EVT_L_LIM = 1 << 4, /*!< PCNT watch point event: Minimum counter value */
PCNT_EVT_H_LIM = 1 << 5, /*!< PCNT watch point event: Maximum counter value */
PCNT_EVT_ZERO = 1 << 6, /*!< PCNT watch point event: counter value zero event */
PCNT_EVT_MAX
} pcnt_evt_type_t;
/**
* @brief Selection of available modes that determine the counter's action depending on the state of the control signal's input GPIO
* @note Configuration covers two actions, one for high, and one for low level on the control input
*/
typedef pcnt_channel_level_action_t pcnt_ctrl_mode_t;
#define PCNT_MODE_KEEP PCNT_CHANNEL_LEVEL_ACTION_KEEP /*!< Control mode: won't change counter mode*/
#define PCNT_MODE_REVERSE PCNT_CHANNEL_LEVEL_ACTION_INVERSE /*!< Control mode: invert counter mode(increase -> decrease, decrease -> increase) */
#define PCNT_MODE_DISABLE PCNT_CHANNEL_LEVEL_ACTION_HOLD /*!< Control mode: Inhibit counter(counter value will not change in this condition) */
#define PCNT_MODE_MAX 3
/**
* @brief Selection of available modes that determine the counter's action on the edge of the pulse signal's input GPIO
* @note Configuration covers two actions, one for positive, and one for negative edge on the pulse input
*/
typedef pcnt_channel_edge_action_t pcnt_count_mode_t;
#define PCNT_COUNT_DIS PCNT_CHANNEL_EDGE_ACTION_HOLD /*!< Counter mode: Inhibit counter(counter value will not change in this condition) */
#define PCNT_COUNT_INC PCNT_CHANNEL_EDGE_ACTION_INCREASE /*!< Counter mode: Increase counter value */
#define PCNT_COUNT_DEC PCNT_CHANNEL_EDGE_ACTION_DECREASE /*!< Counter mode: Decrease counter value */
#define PCNT_COUNT_MAX 3
/**
* @brief Pulse Counter configuration for a single channel
*/
typedef struct {
int pulse_gpio_num; /*!< Pulse input GPIO number, if you want to use GPIO16, enter pulse_gpio_num = 16, a negative value will be ignored */
int ctrl_gpio_num; /*!< Control signal input GPIO number, a negative value will be ignored */
pcnt_ctrl_mode_t lctrl_mode; /*!< PCNT low control mode */
pcnt_ctrl_mode_t hctrl_mode; /*!< PCNT high control mode */
pcnt_count_mode_t pos_mode; /*!< PCNT positive edge count mode */
pcnt_count_mode_t neg_mode; /*!< PCNT negative edge count mode */
int16_t counter_h_lim; /*!< Maximum counter value */
int16_t counter_l_lim; /*!< Minimum counter value */
pcnt_unit_t unit; /*!< PCNT unit number */
pcnt_channel_t channel; /*!< the PCNT channel */
} pcnt_config_t;
/**
* @brief Configure Pulse Counter unit
* @note
@ -173,7 +257,6 @@ esp_err_t pcnt_get_event_value(pcnt_unit_t unit, pcnt_evt_type_t evt_type, int16
* @param unit PCNT unit number
* @param status Pointer to accept event status word
* @return
*
* - ESP_OK Success
* - ESP_ERR_INVALID_STATE pcnt driver has not been initialized
* - ESP_ERR_INVALID_ARG Parameter error
@ -363,42 +446,6 @@ void pcnt_isr_service_uninstall(void);
*/
esp_err_t pcnt_isr_handler_remove(pcnt_unit_t unit);
/**
* @addtogroup pcnt-examples
*
* @{
*
* EXAMPLE OF PCNT CONFIGURATION
* ==============================
* @code{c}
* //1. Config PCNT unit
* pcnt_config_t pcnt_config = {
* .pulse_gpio_num = 4, //set gpio4 as pulse input gpio
* .ctrl_gpio_num = 5, //set gpio5 as control gpio
* .channel = PCNT_CHANNEL_0, //use unit 0 channel 0
* .lctrl_mode = PCNT_MODE_REVERSE, //when control signal is low, reverse the primary counter mode(inc->dec/dec->inc)
* .hctrl_mode = PCNT_MODE_KEEP, //when control signal is high, keep the primary counter mode
* .pos_mode = PCNT_COUNT_INC, //increment the counter
* .neg_mode = PCNT_COUNT_DIS, //keep the counter value
* .counter_h_lim = 10,
* .counter_l_lim = -10,
* };
* pcnt_unit_config(&pcnt_config); //init unit
* @endcode
*
* EXAMPLE OF PCNT EVENT SETTING
* ==============================
* @code{c}
* //2. Configure PCNT watchpoint event.
* pcnt_set_event_value(PCNT_UNIT_0, PCNT_EVT_THRES_1, 5); //set thres1 value
* pcnt_event_enable(PCNT_UNIT_0, PCNT_EVT_THRES_1); //enable thres1 event
* @endcode
*
* For more examples please refer to PCNT example code in IDF_PATH/examples
*
* @}
*/
#ifdef __cplusplus
}
#endif

View File

@ -63,7 +63,8 @@ extern "C"
#define SPICOMMON_BUSFLAG_DUAL (1<<6) ///< Check MOSI and MISO pins can output. Or indicates bus able to work under DIO mode.
#define SPICOMMON_BUSFLAG_WPHD (1<<7) ///< Check existing of WP and HD pins. Or indicates WP & HD pins initialized.
#define SPICOMMON_BUSFLAG_QUAD (SPICOMMON_BUSFLAG_DUAL|SPICOMMON_BUSFLAG_WPHD) ///< Check existing of MOSI/MISO/WP/HD pins as output. Or indicates bus able to work under QIO mode.
#define SPICOMMON_BUSFLAG_IO4_IO7 (1<<8) ///< Check existing of IO4~IO7 pins. Or indicates IO4~IO7 pins initialized.
#define SPICOMMON_BUSFLAG_OCTAL (SPICOMMON_BUSFLAG_QUAD|SPICOMMON_BUSFLAG_IO4_IO7) ///< Check existing of MOSI/MISO/WP/HD/SPIIO4/SPIIO5/SPIIO6/SPIIO7 pins as output. Or indicates bus able to work under octal mode.
#define SPICOMMON_BUSFLAG_NATIVE_PINS SPICOMMON_BUSFLAG_IOMUX_PINS
/**
@ -95,18 +96,34 @@ typedef spi_common_dma_t spi_dma_chan_t;
* @note Be advised that the slave driver does not use the quadwp/quadhd lines and fields in spi_bus_config_t refering to these lines will be ignored and can thus safely be left uninitialized.
*/
typedef struct {
int mosi_io_num; ///< GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.
int miso_io_num; ///< GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.
int sclk_io_num; ///< GPIO pin for Spi CLocK signal, or -1 if not used.
int quadwp_io_num; ///< GPIO pin for WP (Write Protect) signal which is used as D2 in 4-bit communication modes, or -1 if not used.
int quadhd_io_num; ///< GPIO pin for HD (HolD) signal which is used as D3 in 4-bit communication modes, or -1 if not used.
int max_transfer_sz; ///< Maximum transfer size, in bytes. Defaults to 4092 if 0 when DMA enabled, or to `SOC_SPI_MAXIMUM_BUFFER_SIZE` if DMA is disabled.
uint32_t flags; ///< Abilities of bus to be checked by the driver. Or-ed value of ``SPICOMMON_BUSFLAG_*`` flags.
int intr_flags; /**< Interrupt flag for the bus to set the priority, and IRAM attribute, see
* ``esp_intr_alloc.h``. Note that the EDGE, INTRDISABLED attribute are ignored
* by the driver. Note that if ESP_INTR_FLAG_IRAM is set, ALL the callbacks of
* the driver, and their callee functions, should be put in the IRAM.
*/
union {
int mosi_io_num; ///< GPIO pin for Master Out Slave In (=spi_d) signal, or -1 if not used.
int data0_io_num; ///< GPIO pin for spi data0 signal in quad/octal mode, or -1 if not used.
};
union {
int miso_io_num; ///< GPIO pin for Master In Slave Out (=spi_q) signal, or -1 if not used.
int data1_io_num; ///< GPIO pin for spi data1 signal in quad/octal mode, or -1 if not used.
};
int sclk_io_num; ///< GPIO pin for SPI Clock signal, or -1 if not used.
union {
int quadwp_io_num; ///< GPIO pin for WP (Write Protect) signal, or -1 if not used.
int data2_io_num; ///< GPIO pin for spi data2 signal in quad/octal mode, or -1 if not used.
};
union {
int quadhd_io_num; ///< GPIO pin for HD (Hold) signal, or -1 if not used.
int data3_io_num; ///< GPIO pin for spi data3 signal in quad/octal mode, or -1 if not used.
};
int data4_io_num; ///< GPIO pin for spi data4 signal in octal mode, or -1 if not used.
int data5_io_num; ///< GPIO pin for spi data5 signal in octal mode, or -1 if not used.
int data6_io_num; ///< GPIO pin for spi data6 signal in octal mode, or -1 if not used.
int data7_io_num; ///< GPIO pin for spi data7 signal in octal mode, or -1 if not used.
int max_transfer_sz; ///< Maximum transfer size, in bytes. Defaults to 4092 if 0 when DMA enabled, or to `SOC_SPI_MAXIMUM_BUFFER_SIZE` if DMA is disabled.
uint32_t flags; ///< Abilities of bus to be checked by the driver. Or-ed value of ``SPICOMMON_BUSFLAG_*`` flags.
int intr_flags; /**< Interrupt flag for the bus to set the priority, and IRAM attribute, see
* ``esp_intr_alloc.h``. Note that the EDGE, INTRDISABLED attribute are ignored
* by the driver. Note that if ESP_INTR_FLAG_IRAM is set, ALL the callbacks of
* the driver, and their callee functions, should be put in the IRAM.
*/
} spi_bus_config_t;

View File

@ -155,6 +155,8 @@ esp_err_t spicommon_slave_free_dma(spi_host_device_t host_id);
* - ``SPICOMMON_BUSFLAG_DUAL``: Make sure both MISO and MOSI are output capable so that DIO mode is capable.
* - ``SPICOMMON_BUSFLAG_WPHD`` Make sure WP and HD are set to valid output GPIOs.
* - ``SPICOMMON_BUSFLAG_QUAD``: Combination of ``SPICOMMON_BUSFLAG_DUAL`` and ``SPICOMMON_BUSFLAG_WPHD``.
* - ``SPICOMMON_BUSFLAG_IO4_IO7``: Make sure spi data4 ~ spi data7 are set to valid output GPIOs.
* - ``SPICOMMON_BUSFLAG_OCTAL``: Combination of ``SPICOMMON_BUSFLAG_QUAL`` and ``SPICOMMON_BUSFLAG_IO4_IO7``.
* @param[out] flags_o A SPICOMMON_BUSFLAG_* flag combination of bus abilities will be written to this address.
* Leave to NULL if not needed.
* - ``SPICOMMON_BUSFLAG_IOMUX_PINS``: The bus is connected to iomux pins.
@ -163,6 +165,8 @@ esp_err_t spicommon_slave_free_dma(spi_host_device_t host_id);
* - ``SPICOMMON_BUSFLAG_DUAL``: The bus is capable with DIO mode.
* - ``SPICOMMON_BUSFLAG_WPHD`` The bus has WP and HD connected.
* - ``SPICOMMON_BUSFLAG_QUAD``: Combination of ``SPICOMMON_BUSFLAG_DUAL`` and ``SPICOMMON_BUSFLAG_WPHD``.
* - ``SPICOMMON_BUSFLAG_IO4_IO7``: The bus has spi data4 ~ spi data7 connected.
* - ``SPICOMMON_BUSFLAG_OCTAL``: Combination of ``SPICOMMON_BUSFLAG_QUAL`` and ``SPICOMMON_BUSFLAG_IO4_IO7``.
* @return
* - ESP_ERR_INVALID_ARG if parameter is invalid
* - ESP_OK on success

View File

@ -104,8 +104,11 @@ typedef struct {
#define SPI_TRANS_VARIABLE_CMD (1<<5) ///< Use the ``command_bits`` in ``spi_transaction_ext_t`` rather than default value in ``spi_device_interface_config_t``.
#define SPI_TRANS_VARIABLE_ADDR (1<<6) ///< Use the ``address_bits`` in ``spi_transaction_ext_t`` rather than default value in ``spi_device_interface_config_t``.
#define SPI_TRANS_VARIABLE_DUMMY (1<<7) ///< Use the ``dummy_bits`` in ``spi_transaction_ext_t`` rather than default value in ``spi_device_interface_config_t``.
#define SPI_TRANS_SET_CD (1<<7) ///< Set the CD pin
#define SPI_TRANS_CS_KEEP_ACTIVE (1<<8) ///< Keep CS active after data transfer
#define SPI_TRANS_MULTILINE_CMD (1<<9) ///< The data lines used at command phase is the same as data phase (otherwise, only one data line is used at command phase)
#define SPI_TRANS_MODE_OCT (1<<10) ///< Transmit/receive data in 8-bit mode
#define SPI_TRANS_MULTILINE_ADDR SPI_TRANS_MODE_DIOQIO_ADDR ///< The data lines used at address phase is the same as data phase (otherwise, only one data line is used at address phase)
/**
* This structure describes one SPI transaction. The descriptor should not be modified until the transaction finishes.
*/
@ -148,7 +151,7 @@ typedef struct {
} spi_transaction_ext_t ;
typedef struct spi_device_t* spi_device_handle_t; ///< Handle for a device on a SPI bus
typedef struct spi_device_t *spi_device_handle_t; ///< Handle for a device on a SPI bus
/**
* @brief Allocate a device on a SPI bus
*
@ -341,7 +344,7 @@ void spi_device_release_bus(spi_device_handle_t dev);
*
* @return Actual working frequency that most fit.
*/
int spi_cal_clock(int fapb, int hz, int duty_cycle, uint32_t* reg_o) __attribute__((deprecated));
int spi_cal_clock(int fapb, int hz, int duty_cycle, uint32_t *reg_o) __attribute__((deprecated));
/**
* @brief Calculate the working frequency that is most close to desired frequency.
@ -368,7 +371,7 @@ int spi_get_actual_clock(int fapb, int hz, int duty_cycle);
*
* @note If **dummy_o* is not zero, it means dummy bits should be applied in half duplex mode, and full duplex mode may not work.
*/
void spi_get_timing(bool gpio_is_used, int input_delay_ns, int eff_clk, int* dummy_o, int* cycles_remain_o);
void spi_get_timing(bool gpio_is_used, int input_delay_ns, int eff_clk, int *dummy_o, int *cycles_remain_o);
/**
* @brief Get the frequency limit of current configurations.

View File

@ -130,8 +130,9 @@ esp_err_t timer_set_counter_mode(timer_group_t group_num, timer_idx_t timer_num,
esp_err_t timer_set_auto_reload(timer_group_t group_num, timer_idx_t timer_num, timer_autoreload_t reload);
/**
* @brief Set hardware timer source clock divider. Timer groups clock are divider from APB clock.
*
* @brief Set hardware divider of the source clock to the timer group.
* By default, the source clock is APB clock running at 80 MHz.
* For more information, please check Chapter Reset and Clock in Chip Technical Reference Manual.
* @param group_num Timer group number, 0 for TIMERG0 or 1 for TIMERG1
* @param timer_num Timer index, 0 for hw_timer[0] & 1 for hw_timer[1]
* @param divider Timer clock divider value. The divider's range is from from 2 to 65536.

View File

@ -47,23 +47,24 @@ extern "C" {
*/
#define TWAI_ALERT_TX_IDLE 0x00000001 /**< Alert(1): No more messages to transmit */
#define TWAI_ALERT_TX_SUCCESS 0x00000002 /**< Alert(2): The previous transmission was successful */
#define TWAI_ALERT_BELOW_ERR_WARN 0x00000004 /**< Alert(4): Both error counters have dropped below error warning limit */
#define TWAI_ALERT_ERR_ACTIVE 0x00000008 /**< Alert(8): TWAI controller has become error active */
#define TWAI_ALERT_RECOVERY_IN_PROGRESS 0x00000010 /**< Alert(16): TWAI controller is undergoing bus recovery */
#define TWAI_ALERT_BUS_RECOVERED 0x00000020 /**< Alert(32): TWAI controller has successfully completed bus recovery */
#define TWAI_ALERT_ARB_LOST 0x00000040 /**< Alert(64): The previous transmission lost arbitration */
#define TWAI_ALERT_ABOVE_ERR_WARN 0x00000080 /**< Alert(128): One of the error counters have exceeded the error warning limit */
#define TWAI_ALERT_BUS_ERROR 0x00000100 /**< Alert(256): A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus */
#define TWAI_ALERT_TX_FAILED 0x00000200 /**< Alert(512): The previous transmission has failed (for single shot transmission) */
#define TWAI_ALERT_RX_QUEUE_FULL 0x00000400 /**< Alert(1024): The RX queue is full causing a frame to be lost */
#define TWAI_ALERT_ERR_PASS 0x00000800 /**< Alert(2048): TWAI controller has become error passive */
#define TWAI_ALERT_BUS_OFF 0x00001000 /**< Alert(4096): Bus-off condition occurred. TWAI controller can no longer influence bus */
#define TWAI_ALERT_RX_FIFO_OVERRUN 0x00002000 /**< Alert(8192): An RX FIFO overrun has occurred */
#define TWAI_ALERT_TX_RETRIED 0x00004000 /**< Alert(16384): An message transmission was cancelled and retried due to an errata workaround */
#define TWAI_ALERT_PERIPH_RESET 0x00008000 /**< Alert(32768): The TWAI controller was reset */
#define TWAI_ALERT_ALL 0x0000FFFF /**< Bit mask to enable all alerts during configuration */
#define TWAI_ALERT_RX_DATA 0x00000004 /**< Alert(4): A frame has been received and added to the RX queue */
#define TWAI_ALERT_BELOW_ERR_WARN 0x00000008 /**< Alert(8): Both error counters have dropped below error warning limit */
#define TWAI_ALERT_ERR_ACTIVE 0x00000010 /**< Alert(16): TWAI controller has become error active */
#define TWAI_ALERT_RECOVERY_IN_PROGRESS 0x00000020 /**< Alert(32): TWAI controller is undergoing bus recovery */
#define TWAI_ALERT_BUS_RECOVERED 0x00000040 /**< Alert(64): TWAI controller has successfully completed bus recovery */
#define TWAI_ALERT_ARB_LOST 0x00000080 /**< Alert(128): The previous transmission lost arbitration */
#define TWAI_ALERT_ABOVE_ERR_WARN 0x00000100 /**< Alert(256): One of the error counters have exceeded the error warning limit */
#define TWAI_ALERT_BUS_ERROR 0x00000200 /**< Alert(512): A (Bit, Stuff, CRC, Form, ACK) error has occurred on the bus */
#define TWAI_ALERT_TX_FAILED 0x00000400 /**< Alert(1024): The previous transmission has failed (for single shot transmission) */
#define TWAI_ALERT_RX_QUEUE_FULL 0x00000800 /**< Alert(2048): The RX queue is full causing a frame to be lost */
#define TWAI_ALERT_ERR_PASS 0x00001000 /**< Alert(4096): TWAI controller has become error passive */
#define TWAI_ALERT_BUS_OFF 0x00002000 /**< Alert(8192): Bus-off condition occurred. TWAI controller can no longer influence bus */
#define TWAI_ALERT_RX_FIFO_OVERRUN 0x00004000 /**< Alert(16384): An RX FIFO overrun has occurred */
#define TWAI_ALERT_TX_RETRIED 0x00008000 /**< Alert(32768): An message transmission was cancelled and retried due to an errata workaround */
#define TWAI_ALERT_PERIPH_RESET 0x00010000 /**< Alert(65536): The TWAI controller was reset */
#define TWAI_ALERT_ALL 0x0001FFFF /**< Bit mask to enable all alerts during configuration */
#define TWAI_ALERT_NONE 0x00000000 /**< Bit mask to disable all alerts during configuration */
#define TWAI_ALERT_AND_LOG 0x00010000 /**< Bit mask to enable alerts to also be logged when they occur. Note that logging from the ISR is disabled if CONFIG_TWAI_ISR_IN_IRAM is enabled (see docs). */
#define TWAI_ALERT_AND_LOG 0x00020000 /**< Bit mask to enable alerts to also be logged when they occur. Note that logging from the ISR is disabled if CONFIG_TWAI_ISR_IN_IRAM is enabled (see docs). */
/** @endcond */

View File

@ -28,7 +28,10 @@ extern "C" {
#endif
#define UART_NUM_MAX (SOC_UART_NUM) /*!< UART port max */
#define UART_PIN_NO_CHANGE (-1) /*!< Constant for uart_set_pin function which indicates that UART pin should not be changed */
/* @brief When calling `uart_set_pin`, instead of GPIO number, `UART_PIN_NO_CHANGE`
* can be provided to keep the currently allocated pin.
*/
#define UART_PIN_NO_CHANGE (-1)
#define UART_FIFO_LEN SOC_UART_FIFO_LEN ///< Length of the UART HW FIFO
#define UART_BITRATE_MAX SOC_UART_BITRATE_MAX ///< Maximum configurable bitrate
@ -380,14 +383,23 @@ esp_err_t uart_isr_register(uart_port_t uart_num, void (*fn)(void*), void * arg,
esp_err_t uart_isr_free(uart_port_t uart_num);
/**
* @brief Set UART pin number
* @brief Assign signals of a UART peripheral to GPIO pins
*
* @note If the GPIO number configured for a UART signal matches one of the
* IOMUX signals for that GPIO, the signal will be connected directly
* via the IOMUX. Otherwise the GPIO and signal will be connected via
* the GPIO Matrix. For example, if on an ESP32 the call
* `uart_set_pin(0, 1, 3, -1, -1)` is performed, as GPIO1 is UART0's
* default TX pin and GPIO3 is UART0's default RX pin, both will be
* connected to respectively U0TXD and U0RXD through the IOMUX, totally
* bypassing the GPIO matrix.
* The check is performed on a per-pin basis. Thus, it is possible to have
* RX pin binded to a GPIO through the GPIO matrix, whereas TX is binded
* to its GPIO through the IOMUX.
*
* @note Internal signal can be output to multiple GPIO pads.
* Only one GPIO pad can connect with input signal.
*
* @note Instead of GPIO number a macro 'UART_PIN_NO_CHANGE' may be provided
to keep the currently allocated pin.
*
* @param uart_num UART port number, the max port number is (UART_NUM_MAX -1).
* @param tx_io_num UART TX pin GPIO number.
* @param rx_io_num UART RX pin GPIO number.

View File

@ -0,0 +1,48 @@
/*
* SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
// DO NOT USE THESE APIS IN YOUR APPLICATIONS
// The following APIs are for internal use, public to other IDF components, but not for users' applications.
#pragma once
#include "esp_err.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Register an I2S or I2S variant driver object to platform
*
* @note This private API is used to avoid applications from using the same I2S instance for different purpose.
* @note This function will help enable the peripheral APB clock as well.
*
* @param driver_obj Driver object
* @param port_id I2S port number
* @return
* - ESP_OK: The specific I2S port is free and register the new device object successfully
* - ESP_ERR_INVALID_ARG: Invalid argument, e.g. wrong port_id
* - ESP_ERR_NOT_FOUND: Specific I2S port is not available
*/
esp_err_t i2s_priv_register_object(void *driver_obj, int port_id);
/**
* @brief Deregister I2S or I2S variant driver object from platform
*
* @note This function will help disable the peripheral APB clock as well.
*
* @param port_id I2S port number
* @return
* - ESP_OK: Deregister I2S port successfully (i.e. that I2S port can used used by other users after this function returns)
* - ESP_ERR_INVALID_ARG: Invalid argument, e.g. wrong port_id
* - ESP_ERR_INVALID_STATE: Specific I2S port is free already
*/
esp_err_t i2s_priv_deregister_object(int port_id);
#ifdef __cplusplus
}
#endif

View File

@ -9,7 +9,7 @@ extern "C" {
#endif
// md5_digest_table 7a50fdd084e3b80b143c5bd2a36f9c26
// md5_digest_table ef33779021404fbaddc878eefebaddc1
// This file was generated from the file esp_efuse_table.csv. DO NOT CHANGE THIS FILE MANUALLY.
// If you want to change some fields, you need to change esp_efuse_table.csv file
// then run `efuse_common_table` or `efuse_custom_table` command it will generate this file.
@ -51,7 +51,6 @@ extern const esp_efuse_desc_t* ESP_EFUSE_RD_DIS_KEY3[];
extern const esp_efuse_desc_t* ESP_EFUSE_RD_DIS_KEY4[];
extern const esp_efuse_desc_t* ESP_EFUSE_RD_DIS_KEY5[];
extern const esp_efuse_desc_t* ESP_EFUSE_RD_DIS_SYS_DATA_PART2[];
extern const esp_efuse_desc_t* ESP_EFUSE_DIS_RTC_RAM_BOOT[];
extern const esp_efuse_desc_t* ESP_EFUSE_DIS_ICACHE[];
extern const esp_efuse_desc_t* ESP_EFUSE_DIS_USB_JTAG[];
extern const esp_efuse_desc_t* ESP_EFUSE_DIS_DOWNLOAD_ICACHE[];
@ -125,6 +124,7 @@ extern const esp_efuse_desc_t* ESP_EFUSE_ADC1_CAL_VOL_ATTEN1[];
extern const esp_efuse_desc_t* ESP_EFUSE_ADC1_CAL_VOL_ATTEN2[];
extern const esp_efuse_desc_t* ESP_EFUSE_ADC1_CAL_VOL_ATTEN3[];
extern const esp_efuse_desc_t* ESP_EFUSE_USER_DATA[];
extern const esp_efuse_desc_t* ESP_EFUSE_USER_DATA_MAC_CUSTOM[];
extern const esp_efuse_desc_t* ESP_EFUSE_KEY0[];
extern const esp_efuse_desc_t* ESP_EFUSE_KEY1[];
extern const esp_efuse_desc_t* ESP_EFUSE_KEY2[];

View File

@ -20,6 +20,9 @@
#include "mbedtls/ctr_drbg.h"
#include "mbedtls/error.h"
#include "mbedtls/certs.h"
#ifdef CONFIG_ESP_TLS_SERVER_SESSION_TICKETS
#include "mbedtls/ssl_ticket.h"
#endif
#elif CONFIG_ESP_TLS_USING_WOLFSSL
#include "wolfssl/wolfcrypt/settings.h"
#include "wolfssl/ssl.h"
@ -54,6 +57,15 @@ typedef struct psk_key_hint {
const char* hint; /*!< hint in PSK authentication mode in string format */
} psk_hint_key_t;
/**
* @brief esp-tls client session ticket ctx
*/
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
typedef struct esp_tls_client_session {
mbedtls_ssl_session saved_session;
} esp_tls_client_session_t;
#endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
/**
* @brief Keep alive parameters structure
*/
@ -168,9 +180,27 @@ typedef struct esp_tls_cfg {
directly with esp_tls_plain_tcp_connect() API */
struct ifreq *if_name; /*!< The name of interface for data to go through. Use the default interface without setting */
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
esp_tls_client_session_t *client_session; /*! Pointer for the client session ticket context. */
#endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
} esp_tls_cfg_t;
#ifdef CONFIG_ESP_TLS_SERVER
#if defined(CONFIG_ESP_TLS_SERVER_SESSION_TICKETS)
/**
* @brief Data structures necessary to support TLS session tickets according to RFC5077
*/
typedef struct esp_tls_server_session_ticket_ctx {
mbedtls_entropy_context entropy; /*!< mbedTLS entropy context structure */
mbedtls_ctr_drbg_context ctr_drbg; /*!< mbedTLS ctr drbg context structure.
CTR_DRBG is deterministic random
bit generation based on AES-256 */
mbedtls_ssl_ticket_context ticket_ctx; /*!< Session ticket generation context */
} esp_tls_server_session_ticket_ctx_t;
#endif
typedef struct esp_tls_cfg_server {
const char **alpn_protos; /*!< Application protocols required for HTTP2.
If HTTP2/ALPN support is required, a list
@ -222,7 +252,39 @@ typedef struct esp_tls_cfg_server {
unsigned int serverkey_password_len; /*!< String length of the password pointed to by
serverkey_password */
#if defined(CONFIG_ESP_TLS_SERVER_SESSION_TICKETS)
esp_tls_server_session_ticket_ctx_t * ticket_ctx; /*!< Session ticket generation context.
You have to call esp_tls_cfg_server_session_tickets_init
to use it.
Call esp_tls_cfg_server_session_tickets_free
to free the data associated with this context. */
#endif
} esp_tls_cfg_server_t;
/**
* @brief Initialize the server side TLS session ticket context
*
* This function initializes the server side tls session ticket context
* which holds all necessary data structures to enable tls session tickets
* according to RFC5077.
* Use esp_tls_cfg_server_session_tickets_free to free the data.
*
* @param[in] cfg server configuration as esp_tls_cfg_server_t
* @return
* ESP_OK if setup succeeded
* ESP_ERR_INVALID_ARG if context is already initialized
* ESP_ERR_NO_MEM if memory allocation failed
* ESP_ERR_NOT_SUPPORTED if session tickets are not available due to build configuration
* ESP_FAIL if setup failed
*/
esp_err_t esp_tls_cfg_server_session_tickets_init(esp_tls_cfg_server_t *cfg);
/**
* @brief Free the server side TLS session ticket context
*
* @param cfg server configuration as esp_tls_cfg_server_t
*/
void esp_tls_cfg_server_session_tickets_free(esp_tls_cfg_server_t *cfg);
#endif /* ! CONFIG_ESP_TLS_SERVER */
/**
@ -404,6 +466,10 @@ int esp_tls_conn_http_new_async(const char *url, const esp_tls_cfg_t *cfg, esp_t
* of bytes actually written to the TLS/SSL connection.
* - <0 if write operation was not successful, because either an
* error occured or an action must be taken by the calling process.
* - ESP_TLS_ERR_SSL_WANT_READ/
* ESP_TLS_ERR_SSL_WANT_WRITE.
* if the handshake is incomplete and waiting for data to be available for reading.
* In this case this functions needs to be called again when the underlying transport is ready for operation.
*/
static inline ssize_t esp_tls_conn_write(esp_tls_t *tls, const void *data, size_t datalen)
{
@ -609,6 +675,20 @@ void esp_tls_server_session_delete(esp_tls_t *tls);
*/
esp_err_t esp_tls_plain_tcp_connect(const char *host, int hostlen, int port, const esp_tls_cfg_t *cfg, esp_tls_error_handle_t error_handle, int *sockfd);
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
/**
* @brief Obtain the client session ticket
*
* This function should be called when the TLS connection is already established.
* This can be passed again in the esp_tls_cfg_t structure, to appropriate tls session create (e.g. esp_tls_conn_http_new) API for session resumption.
*
* @param[in] esp_tls context as esp_tls_t
* @return
* Pointer to the saved client session.
* NULL on Failure
*/
esp_tls_client_session_t *esp_tls_get_client_session(esp_tls_t *tls);
#endif /* CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS */
#ifdef __cplusplus
}
#endif

View File

@ -14,35 +14,42 @@ extern "C" {
#endif
#define ESP_ERR_ESP_TLS_BASE 0x8000 /*!< Starting number of ESP-TLS error codes */
/* generic esp-tls error codes */
#define ESP_ERR_ESP_TLS_CANNOT_RESOLVE_HOSTNAME (ESP_ERR_ESP_TLS_BASE + 0x01) /*!< Error if hostname couldn't be resolved upon tls connection */
#define ESP_ERR_ESP_TLS_CANNOT_CREATE_SOCKET (ESP_ERR_ESP_TLS_BASE + 0x02) /*!< Failed to create socket */
#define ESP_ERR_ESP_TLS_UNSUPPORTED_PROTOCOL_FAMILY (ESP_ERR_ESP_TLS_BASE + 0x03) /*!< Unsupported protocol family */
#define ESP_ERR_ESP_TLS_FAILED_CONNECT_TO_HOST (ESP_ERR_ESP_TLS_BASE + 0x04) /*!< Failed to connect to host */
#define ESP_ERR_ESP_TLS_SOCKET_SETOPT_FAILED (ESP_ERR_ESP_TLS_BASE + 0x05) /*!< failed to set/get socket option */
#define ESP_ERR_MBEDTLS_CERT_PARTLY_OK (ESP_ERR_ESP_TLS_BASE + 0x06) /*!< mbedtls parse certificates was partly successful */
#define ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED (ESP_ERR_ESP_TLS_BASE + 0x07) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED (ESP_ERR_ESP_TLS_BASE + 0x08) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x09) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0A) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0B) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0C) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0D) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0E) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED (ESP_ERR_ESP_TLS_BASE + 0x0F) /*!< mbedtls api returned failed */
#define ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x10) /*!< mbedtls api returned failed */
#define ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED (ESP_ERR_ESP_TLS_BASE + 0x11) /*!< mbedtls api returned failed */
#define ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT (ESP_ERR_ESP_TLS_BASE + 0x12) /*!< new connection in esp_tls_low_level_conn connection timeouted */
#define ESP_ERR_WOLFSSL_SSL_SET_HOSTNAME_FAILED (ESP_ERR_ESP_TLS_BASE + 0x13) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_SSL_CONF_ALPN_PROTOCOLS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x14) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_CERT_VERIFY_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x15) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_KEY_VERIFY_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x16) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x17) /*!< wolfSSL api returned failed */
#define ESP_ERR_WOLFSSL_CTX_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x18) /*!< wolfSSL api returned failed */
#define ESP_ERR_WOLFSSL_SSL_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x19) /*!< wolfSSL api returned failed */
#define ESP_ERR_WOLFSSL_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x1A) /*!< wolfSSL api returned failed */
#define ESP_ERR_ESP_TLS_CONNECTION_TIMEOUT (ESP_ERR_ESP_TLS_BASE + 0x06) /*!< new connection in esp_tls_low_level_conn connection timeouted */
#define ESP_ERR_ESP_TLS_SE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x07) /*< esp-tls use Secure Element returned failed */
#define ESP_ERR_ESP_TLS_TCP_CLOSED_FIN (ESP_ERR_ESP_TLS_BASE + 0x08) /*< esp-tls's TPC transport connection has benn closed (in a clean way) */
/* mbedtls specific error codes */
#define ESP_ERR_MBEDTLS_CERT_PARTLY_OK (ESP_ERR_ESP_TLS_BASE + 0x10) /*!< mbedtls parse certificates was partly successful */
#define ESP_ERR_MBEDTLS_CTR_DRBG_SEED_FAILED (ESP_ERR_ESP_TLS_BASE + 0x11) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_SET_HOSTNAME_FAILED (ESP_ERR_ESP_TLS_BASE + 0x12) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_CONFIG_DEFAULTS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x13) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_CONF_ALPN_PROTOCOLS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x14) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_X509_CRT_PARSE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x15) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_CONF_OWN_CERT_FAILED (ESP_ERR_ESP_TLS_BASE + 0x16) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x17) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x18) /*!< mbedtls api returned error */
#define ESP_ERR_MBEDTLS_PK_PARSE_KEY_FAILED (ESP_ERR_ESP_TLS_BASE + 0x19) /*!< mbedtls api returned failed */
#define ESP_ERR_MBEDTLS_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x1A) /*!< mbedtls api returned failed */
#define ESP_ERR_MBEDTLS_SSL_CONF_PSK_FAILED (ESP_ERR_ESP_TLS_BASE + 0x1B) /*!< mbedtls api returned failed */
#define ESP_ERR_MBEDTLS_SSL_TICKET_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x1C) /*!< mbedtls api returned failed */
/* wolfssl specific error codes */
#define ESP_ERR_WOLFSSL_SSL_SET_HOSTNAME_FAILED (ESP_ERR_ESP_TLS_BASE + 0x31) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_SSL_CONF_ALPN_PROTOCOLS_FAILED (ESP_ERR_ESP_TLS_BASE + 0x32) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_CERT_VERIFY_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x33) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_KEY_VERIFY_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x34) /*!< wolfSSL api returned error */
#define ESP_ERR_WOLFSSL_SSL_HANDSHAKE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x35) /*!< wolfSSL api returned failed */
#define ESP_ERR_WOLFSSL_CTX_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x36) /*!< wolfSSL api returned failed */
#define ESP_ERR_WOLFSSL_SSL_SETUP_FAILED (ESP_ERR_ESP_TLS_BASE + 0x37) /*!< wolfSSL api returned failed */
#define ESP_ERR_WOLFSSL_SSL_WRITE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x38) /*!< wolfSSL api returned failed */
#define ESP_ERR_ESP_TLS_SE_FAILED (ESP_ERR_ESP_TLS_BASE + 0x1B) /*< esp-tls use Secure Element returned failed */
#define ESP_ERR_ESP_TLS_TCP_CLOSED_FIN (ESP_ERR_ESP_TLS_BASE + 0x1C) /*< esp-tls's TPC transport connection has benn closed (in a clean way) */
/**
* Definition of errors reported from IO API (potentially non-blocking) in case of error:

View File

@ -76,6 +76,22 @@ int esp_mbedtls_server_session_create(esp_tls_cfg_server_t *cfg, int sockfd, esp
* /note :- The function can only be used with mbedtls ssl library
*/
void esp_mbedtls_server_session_delete(esp_tls_t *tls);
#ifdef CONFIG_ESP_TLS_SERVER_SESSION_TICKETS
/**
* Internal function to setup server side session ticket context
*
* /note :- The function can only be used with mbedtls ssl library
*/
esp_err_t esp_mbedtls_server_session_ticket_ctx_init(esp_tls_server_session_ticket_ctx_t *cfg);
/**
* Internal function to free server side session ticket context
*
* /note :- The function can only be used with mbedtls ssl library
*/
void esp_mbedtls_server_session_ticket_ctx_free(esp_tls_server_session_ticket_ctx_t *cfg);
#endif
#endif
/**
@ -83,6 +99,13 @@ void esp_mbedtls_server_session_delete(esp_tls_t *tls);
*/
esp_err_t set_client_config(const char *hostname, size_t hostlen, esp_tls_cfg_t *cfg, esp_tls_t *tls);
#ifdef CONFIG_ESP_TLS_CLIENT_SESSION_TICKETS
/**
* Internal Callback for mbedtls_get_client_session
*/
esp_tls_client_session_t *esp_mbedtls_get_client_session(esp_tls_t *tls);
#endif
/**
* Internal Callback for mbedtls_init_global_ca_store
*/

View File

@ -30,6 +30,7 @@ typedef enum {
ESP_ADC_CAL_VAL_EFUSE_VREF = 0, /**< Characterization based on reference voltage stored in eFuse*/
ESP_ADC_CAL_VAL_EFUSE_TP = 1, /**< Characterization based on Two Point values stored in eFuse*/
ESP_ADC_CAL_VAL_DEFAULT_VREF = 2, /**< Characterization based on default reference voltage*/
ESP_ADC_CAL_VAL_EFUSE_TP_FIT = 3, /**< Characterization based on Two Point values and fitting curve coefficients stored in eFuse */
ESP_ADC_CAL_VAL_MAX,
ESP_ADC_CAL_VAL_NOT_SUPPORTED = ESP_ADC_CAL_VAL_MAX,
} esp_adc_cal_value_t;
@ -48,6 +49,7 @@ typedef struct {
uint32_t vref; /**< Vref used by lookup table*/
const uint32_t *low_curve; /**< Pointer to low Vref curve of lookup table (NULL if unused)*/
const uint32_t *high_curve; /**< Pointer to high Vref curve of lookup table (NULL if unused)*/
uint8_t version; /**< ADC Calibration */
} esp_adc_cal_characteristics_t;
/**
@ -129,8 +131,8 @@ uint32_t esp_adc_cal_raw_to_voltage(uint32_t adc_reading, const esp_adc_cal_char
*
* @return
* - ESP_OK: ADC read and converted to mV
* - ESP_ERR_TIMEOUT: Error, timed out attempting to read ADC
* - ESP_ERR_INVALID_ARG: Error due to invalid arguments
* - ESP_ERR_INVALID_STATE: Reading result is invalid. Try to read again.
*/
esp_err_t esp_adc_cal_get_voltage(adc_channel_t channel, const esp_adc_cal_characteristics_t *chars, uint32_t *voltage);

View File

@ -89,6 +89,14 @@ extern "C" {
// Forces data into noinit section to avoid initialization after restart.
#define __NOINIT_ATTR _SECTION_ATTR_IMPL(".noinit", __COUNTER__)
#if CONFIG_SPIRAM_ALLOW_NOINIT_SEG_EXTERNAL_MEMORY
// Forces data into external memory noinit section to avoid initialization after restart.
#define EXT_RAM_NOINIT_ATTR _SECTION_ATTR_IMPL(".ext_ram_noinit", __COUNTER__)
#else
// Place in internal noinit section
#define EXT_RAM_NOINIT_ATTR __NOINIT_ATTR
#endif
// Forces data into RTC slow memory of .noinit section.
// Any variable marked with this attribute will keep its value
// after restart or during a deep sleep / wake cycle.
@ -155,4 +163,3 @@ FORCE_INLINE_ATTR TYPE& operator<<=(TYPE& a, int b) { a <<= b; return a; }
}
#endif
#endif /* __ESP_ATTR_H__ */

View File

@ -265,12 +265,24 @@ esp_err_t esp_eth_receive(esp_eth_handle_t hdl, uint8_t *buf, uint32_t *length)
*
* @param[in] hdl: handle of Ethernet driver
* @param[in] cmd: IO control command
* @param[in] data: specificed data for command
* @param[in, out] data: address of data for `set` command or address where to store the data when used with `get` command
*
* @return
* - ESP_OK: process io command successfully
* - ESP_ERR_INVALID_ARG: process io command failed because of some invalid argument
* - ESP_FAIL: process io command failed because some other error occurred
*
* The following IO control commands are supported:
* @li @c ETH_CMD_S_MAC_ADDR sets Ethernet interface MAC address. @c data argument is pointer to MAC address buffer with expected size of 6 bytes.
* @li @c ETH_CMD_G_MAC_ADDR gets Ethernet interface MAC address. @c data argument is pointer to a buffer to which MAC address is to be copied. The buffer size must be at least 6 bytes.
* @li @c ETH_CMD_S_PHY_ADDR sets PHY address in range of <0-31>. @c data argument is pointer to memory of uint32_t datatype from where the configuration option is read.
* @li @c ETH_CMD_G_PHY_ADDR gets PHY address. @c data argument is pointer to memory of uint32_t datatype to which the PHY address is to be stored.
* @li @c ETH_CMD_G_SPEED gets current Ethernet link speed. @c data argument is pointer to memory of eth_speed_t datatype to which the speed is to be stored.
* @li @c ETH_CMD_S_PROMISCUOUS sets/resets Ethernet interface promiscuous mode. @c data argument is pointer to memory of bool datatype from which the configuration option is read.
* @li @c ETH_CMD_S_FLOW_CTRL sets/resets Ethernet interface flow control. @c data argument is pointer to memory of bool datatype from which the configuration option is read.
* @li @c ETH_CMD_G_DUPLEX_MODE gets current Ethernet link duplex mode. @c data argument is pointer to memory of eth_duplex_t datatype to which the duplex mode is to be stored.
* @li @c ETH_CMD_S_PHY_LOOPBACK sets/resets PHY to/from loopback mode. @c data argument is pointer to memory of bool datatype from which the configuration option is read.
*
*/
esp_err_t esp_eth_ioctl(esp_eth_handle_t hdl, esp_eth_io_cmd_t cmd, void *data);

View File

@ -89,6 +89,7 @@ typedef enum {
ETH_CMD_S_PROMISCUOUS, /*!< Set promiscuous mode */
ETH_CMD_S_FLOW_CTRL, /*!< Set flow control */
ETH_CMD_G_DUPLEX_MODE, /*!< Get Duplex mode */
ETH_CMD_S_PHY_LOOPBACK,/*!< Set PHY loopback */
} esp_eth_io_cmd_t;
/**

View File

@ -391,7 +391,7 @@ typedef struct {
#define ETH_MAC_DEFAULT_CONFIG() \
{ \
.sw_reset_timeout_ms = 100, \
.rx_task_stack_size = 4096, \
.rx_task_stack_size = 2048, \
.rx_task_prio = 15, \
.smi_mdc_gpio_num = 23, \
.smi_mdio_gpio_num = 18, \

View File

@ -19,6 +19,12 @@
extern "C" {
#endif
/**
* @brief Handle of netif glue - an intermediate layer between netif and Ethernet driver
*
*/
typedef struct esp_eth_netif_glue_t* esp_eth_netif_glue_handle_t;
/**
* @brief Create a netif glue for Ethernet driver
* @note netif glue is used to attach io driver to TCP/IP netif
@ -26,20 +32,23 @@ extern "C" {
* @param eth_hdl Ethernet driver handle
* @return glue object, which inherits esp_netif_driver_base_t
*/
void *esp_eth_new_netif_glue(esp_eth_handle_t eth_hdl);
esp_eth_netif_glue_handle_t esp_eth_new_netif_glue(esp_eth_handle_t eth_hdl);
/**
* @brief Delete netif glue of Ethernet driver
*
* @param glue netif glue
* @param eth_netif_glue netif glue
* @return -ESP_OK: delete netif glue successfully
*/
esp_err_t esp_eth_del_netif_glue(void *glue);
esp_err_t esp_eth_del_netif_glue(esp_eth_netif_glue_handle_t eth_netif_glue);
/**
* @brief Register default IP layer handlers for Ethernet
*
* @note: Ethernet handle might not yet properly initialized when setting up these default handlers
* @warning: This function is deprecated and is kept here only for compatibility reasons. Registration
* of default IP layer handlers for Ethernet is now handled automatically. Do not call this
* function if you want to use multiple Ethernet instances at a time.
*
* @param[in] esp_netif esp network interface handle created for Ethernet driver
* @return
@ -47,12 +56,15 @@ esp_err_t esp_eth_del_netif_glue(void *glue);
* - ESP_OK: set default IP layer handlers successfully
* - others: other failure occurred during register esp_event handler
*/
esp_err_t esp_eth_set_default_handlers(void *esp_netif);
esp_err_t esp_eth_set_default_handlers(void *esp_netif) __attribute__ ((deprecated));
/**
* @brief Unregister default IP layer handlers for Ethernet
*
* @warning: This function is deprecated and is kept here only for compatibility reasons. Unregistration
* of default IP layer handlers for Ethernet is now handled automatically if not registered
* by calling esp_eth_set_default_handlers.
*
* @param[in] esp_netif esp network interface handle created for Ethernet driver
* @return
* - ESP_ERR_INVALID_ARG: invalid parameter (esp_netif is NULL)

View File

@ -1,4 +1,4 @@
// Copyright 2019 Espressif Systems (Shanghai) PTE LTD
// Copyright 2019-2021 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.
@ -173,6 +173,19 @@ struct esp_eth_phy_s {
*/
esp_err_t (*advertise_pause_ability)(esp_eth_phy_t *phy, uint32_t ability);
/**
* @brief
*
* @param[in] phy: Ethernet PHY instance
* @param[in] enable: enables or disables PHY loopback
*
* @return
* - ESP_OK: configures PHY instance loopback function successfully
* - ESP_FAIL: PHY instance loopback configuration failed because some error occurred
*
*/
esp_err_t (*loopback)(esp_eth_phy_t *phy, bool enable);
/**
* @brief Free memory of Ethernet PHY instance
*
@ -232,7 +245,7 @@ esp_eth_phy_t *esp_eth_phy_new_ip101(const eth_phy_config_t *config);
esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config);
/**
* @brief Create a PHY instance of LAN8720
* @brief Create a PHY instance of LAN87xx
*
* @param[in] config: configuration of PHY
*
@ -240,7 +253,23 @@ esp_eth_phy_t *esp_eth_phy_new_rtl8201(const eth_phy_config_t *config);
* - instance: create PHY instance successfully
* - NULL: create PHY instance failed because some error occurred
*/
esp_eth_phy_t *esp_eth_phy_new_lan8720(const eth_phy_config_t *config);
esp_eth_phy_t *esp_eth_phy_new_lan87xx(const eth_phy_config_t *config);
/**
* @brief Create a PHY instance of LAN8720
*
* @note For ESP-IDF backwards compatibility reasons. In all other cases, use esp_eth_phy_new_lan87xx instead.
*
* @param[in] config: configuration of PHY
*
* @return
* - instance: create PHY instance successfully
* - NULL: create PHY instance failed because some error occurred
*/
static inline esp_eth_phy_t *esp_eth_phy_new_lan8720(const eth_phy_config_t *config)
{
return esp_eth_phy_new_lan87xx(config);
}
/**
* @brief Create a PHY instance of DP83848

View File

@ -23,7 +23,10 @@
#include "freertos/semphr.h"
#include "esp_event_base.h"
// Legacy event loop not implemented on Linux target
#if !CONFIG_IDF_TARGET_LINUX
#include "esp_event_legacy.h"
#endif
#ifdef __cplusplus
extern "C" {

View File

@ -126,6 +126,21 @@ typedef enum {
ESP_HID_COD_MIN_MAX
} esp_hid_cod_min_t;
/* HID transaction Types */
typedef enum {
ESP_HID_TRANS_HANDSHAKE = 0,
ESP_HID_TRANS_CONTROL = 1,
ESP_HID_TRANS_GET_REPORT = 4,
ESP_HID_TRANS_SET_REPORT = 5,
ESP_HID_TRANS_GET_PROTOCOL = 6,
ESP_HID_TRANS_SET_PROTOCOL = 7,
ESP_HID_TRANS_GET_IDLE = 8,
ESP_HID_TRANS_SET_IDLE = 9,
ESP_HID_TRANS_DATA = 10,
ESP_HID_TRANS_DATAC = 11,
ESP_HID_TRANS_MAX
} esp_hid_trans_type_t;
/**
* @brief HID report item structure
*/

View File

@ -53,11 +53,28 @@ typedef struct esp_hidd_dev_s esp_hidd_dev_t;
* @brief HIDD callback parameters union
*/
typedef union {
/**
* @brief ESP_HIDD_START_EVENT
* @note Used only for Classic Bluetooth.
*/
struct {
esp_err_t status; /*!< HID device operation status */
} start; /*!< HID callback param of ESP_HIDD_START_EVENT */
/**
* @brief ESP_HIDD_STOP_EVENT
* @note Used only for Classic Bluetooth.
*/
struct {
esp_err_t status; /*!< HID device operation status */
} stop; /*!< HID callback param of ESP_HIDD_STOP_EVENT */
/**
* @brief ESP_HIDD_CONNECT_EVENT
*/
struct {
esp_hidd_dev_t *dev; /*!< HID device structure */
esp_err_t status; /*!< HID device operation status, used only for Classic Bluetooth */
} connect; /*!< HID callback param of ESP_HIDD_CONNECT_EVENT */
/**
@ -66,6 +83,7 @@ typedef union {
struct {
esp_hidd_dev_t *dev; /*!< HID device structure */
int reason; /*!< Indicate the reason of disconnection */
esp_err_t status; /*!< HID device operation status, used only for Classic Bluetooth */
} disconnect; /*!< HID callback param of ESP_HIDD_DISCONNECT_EVENT */
/**
@ -90,6 +108,8 @@ typedef union {
uint16_t length; /*!< data length */
uint8_t *data; /*!< The pointer to the data */
uint8_t map_index; /*!< HID config report map index */
uint8_t trans_type; /*!< HID device feature transaction type, used only for Classic Bluetooth */
uint8_t report_type; /*!< HID device feature report type, used only for Classic Bluetooth */
} feature; /*!< HID callback param of ESP_HIDD_FEATURE_EVENT */
/**

View File

@ -42,6 +42,8 @@ typedef enum {
ESP_HIDH_INPUT_EVENT, /*!< Received HID device INPUT report */
ESP_HIDH_FEATURE_EVENT, /*!< Received HID device FEATURE report */
ESP_HIDH_CLOSE_EVENT, /*!< HID device closed */
ESP_HIDH_START_EVENT, /*!< HID host stack started, used only for Classic Bluetooth */
ESP_HIDH_STOP_EVENT, /*!< HID host stack stopped, used only for Classic Bluetooth */
ESP_HIDH_MAX_EVENT, /*!< HID events end marker */
} esp_hidh_event_t;
@ -49,11 +51,28 @@ typedef enum {
* @brief HIDH callback parameters union
*/
typedef union {
/**
* @brief ESP_HIDH_START_EVENT
* @note Used only for Classic Bluetooth.
*/
struct {
esp_err_t status; /*!< HID host operation status */
} start; /*!< HID callback param of ESP_HIDH_START_EVENT */
/**
* @brief ESP_HIDH_STOP_EVENT
* @note Used only for Classic Bluetooth.
*/
struct {
esp_err_t status; /*!< HID host operation status */
} stop; /*!< HID callback param of ESP_HIDH_STOP_EVENT */
/**
* @brief ESP_HIDH_OPEN_EVENT
*/
struct {
esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
esp_err_t status; /*!< HID host operation status, used only for Classic Bluetooth */
} open; /*!< HID callback param of ESP_HIDH_OPEN_EVENT */
/**
@ -62,6 +81,7 @@ typedef union {
struct {
esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device. */
int reason; /*!< Reason why the connection was closed. BLE Only */
esp_err_t status; /*!< HID host operation status, used only for Classic Bluetooth */
} close; /*!< HID callback param of ESP_HIDH_CLOSE_EVENT */
/**
@ -70,6 +90,7 @@ typedef union {
struct {
esp_hidh_dev_t *dev; /*!< HID Remote bluetooth device */
uint8_t level; /*!< Battery Level (0-100%) */
esp_err_t status; /*!< HID host operation status */
} battery; /*!< HID callback param of ESP_HIDH_BATTERY_EVENT */
/**
@ -80,7 +101,7 @@ typedef union {
esp_hid_usage_t usage; /*!< HID report usage */
uint16_t report_id; /*!< HID report index */
uint16_t length; /*!< HID data length */
uint8_t *data; /*!< The pointer to the HID data */
uint8_t *data; /*!< The pointer to the HID data */
uint8_t map_index; /*!< HID report map index */
} input; /*!< HID callback param of ESP_HIDH_INPUT_EVENT */
@ -92,8 +113,10 @@ typedef union {
esp_hid_usage_t usage; /*!< HID report usage */
uint16_t report_id; /*!< HID report index */
uint16_t length; /*!< HID data length */
uint8_t *data; /*!< The pointer to the HID data */
uint8_t *data; /*!< The pointer to the HID data */
uint8_t map_index; /*!< HID report map index */
esp_err_t status; /*!< HID host operation status, used only for Classic Bluetooth */
esp_hid_trans_type_t trans_type; /*!< HID host feature transaction type, used only for Classic Bluetooth */
} feature; /*!< HID callback param of ESP_HIDH_FEATURE_EVENT */
} esp_hidh_event_data_t;
@ -101,6 +124,7 @@ typedef union {
typedef struct {
esp_event_handler_t callback;
uint16_t event_stack_size;
void *callback_arg;
} esp_hidh_config_t;
/**
@ -136,6 +160,14 @@ esp_err_t esp_hidh_dev_close(esp_hidh_dev_t *dev);
*/
esp_err_t esp_hidh_dev_free(esp_hidh_dev_t *dev);
/**
* @brief Check if the device still exists.
* @param dev : pointer to the device
*
* @return: true if exists
*/
bool esp_hidh_dev_exists(esp_hidh_dev_t *dev);
/**
* @brief Send an OUTPUT report to the device
* @param dev : pointer to the device
@ -173,6 +205,79 @@ esp_err_t esp_hidh_dev_feature_set(esp_hidh_dev_t *dev, size_t map_index, size_t
*/
esp_err_t esp_hidh_dev_feature_get(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, size_t max_len, uint8_t *data, size_t *length);
/**
* @brief Set_Report command.
* @note For now, this function used only for Classic Bluetooth.
*
* @param dev : pointer to the device
* @param map_index : index of the device report map
* @param report_id : id of the HID FEATURE report
* @param report_type : report type, defines in `esp_hid_common.h`
* @param data : pointer to the data to send
* @param length : length of the data to send
*
* @return: ESP_OK on success
*/
esp_err_t esp_hidh_dev_set_report(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type,
uint8_t *data, size_t length);
/**
* @brief Get_Report command.
* @note For now, this function used only for Classic Bluetooth.
*
* @param dev : pointer to the device
* @param map_index : index of the device report map
* @param report_id : id of the HID FEATURE report
* @param report_type : report type, defines in `esp_hid_common.h`
* @param max_len : size of the buffer that will hold the data
*
* @return: ESP_OK on success
*/
esp_err_t esp_hidh_dev_get_report(esp_hidh_dev_t *dev, size_t map_index, size_t report_id, int report_type,
size_t max_len);
/**
* @brief Get_Idle Command.
* @note For now, this function used only for Classic Bluetooth.
*
* @param dev : pointer to the device
*
* @return: ESP_OK on success
*/
esp_err_t esp_hidh_dev_get_idle(esp_hidh_dev_t *dev);
/**
* @brief Set_Idle Command.
* @note For now, this function used only for Classic Bluetooth.
*
* @param dev : pointer to the device
* @param idle_time : idle_time
*
* @return: ESP_OK on success
*/
esp_err_t esp_hidh_dev_set_idle(esp_hidh_dev_t *dev, uint8_t idle_time);
/**
* @brief Get_Protocol Command.
* @note For now, this function used only for Classic Bluetooth.
*
* @param dev : pointer to the device
*
* @return: ESP_OK on success
*/
esp_err_t esp_hidh_dev_get_protocol(esp_hidh_dev_t *dev);
/**
* @brief Set_Protocol Command.
* @note For now, this function used only for Classic Bluetooth.
*
* @param dev : pointer to the device
* @param protocol_mode : protocol_mode
*
* @return: ESP_OK on success
*/
esp_err_t esp_hidh_dev_set_protocol(esp_hidh_dev_t *dev, uint8_t protocol_mode);
/**
* @brief Dump the properties of HID Device to UART
* @param dev : pointer to the HID Device

View File

@ -113,6 +113,8 @@ typedef struct {
size_t client_cert_len; /*!< Length of the buffer pointed to by client_cert_pem. May be 0 for null-terminated pem */
const char *client_key_pem; /*!< SSL client key, PEM format as string, if the server requires to verify client */
size_t client_key_len; /*!< Length of the buffer pointed to by client_key_pem. May be 0 for null-terminated pem */
const char *client_key_password; /*!< Client key decryption password string */
size_t client_key_password_len; /*!< String length of the password pointed to by client_key_password */
const char *user_agent; /*!< The User Agent string to send with HTTP requests */
esp_http_client_method_t method; /*!< HTTP Method */
int timeout_ms; /*!< Network timeout in milliseconds */

View File

@ -941,6 +941,24 @@ esp_err_t httpd_req_get_url_query_str(httpd_req_t *r, char *buf, size_t buf_len)
*/
esp_err_t httpd_query_key_value(const char *qry, const char *key, char *val, size_t val_size);
/**
* @brief Get the value string of a cookie value from the "Cookie" request headers by cookie name.
*
* @param[in] req Pointer to the HTTP request
* @param[in] cookie_name The cookie name to be searched in the request
* @param[out] val Pointer to the buffer into which the value of cookie will be copied if the cookie is found
* @param[inout] val_size Pointer to size of the user buffer "val". This variable will contain cookie length if
* ESP_OK is returned and required buffer length incase ESP_ERR_HTTPD_RESULT_TRUNC is returned.
*
* @return
* - ESP_OK : Key is found in the cookie string and copied to buffer
* - ESP_ERR_NOT_FOUND : Key not found
* - ESP_ERR_INVALID_ARG : Null arguments
* - ESP_ERR_HTTPD_RESULT_TRUNC : Value string truncated
* - ESP_ERR_NO_MEM : Memory allocation failure
*/
esp_err_t httpd_req_get_cookie_val(httpd_req_t *req, const char *cookie_name, char *val, size_t *val_size);
/**
* @brief Test if a URI matches the given wildcard template.
*
@ -1585,6 +1603,11 @@ typedef struct httpd_ws_frame {
size_t len; /*!< Length of the WebSocket data */
} httpd_ws_frame_t;
/**
* @brief Transfer complete callback
*/
typedef void (*transfer_complete_cb)(esp_err_t err, int socket, void *arg);
/**
* @brief Receive and parse a WebSocket frame
*
@ -1645,6 +1668,35 @@ esp_err_t httpd_ws_send_frame_async(httpd_handle_t hd, int fd, httpd_ws_frame_t
*/
httpd_ws_client_info_t httpd_ws_get_fd_info(httpd_handle_t hd, int fd);
/**
* @brief Sends data to to specified websocket synchronously
*
* @param[in] handle Server instance data
* @param[in] socket Socket descriptor
* @param[in] frame Websocket frame
* @return
* - ESP_OK : On successful
* - ESP_FAIL : When socket errors occurs
* - ESP_ERR_NO_MEM : Unable to allocate memory
*/
esp_err_t httpd_ws_send_data(httpd_handle_t handle, int socket, httpd_ws_frame_t *frame);
/**
* @brief Sends data to to specified websocket asynchronously
*
* @param[in] handle Server instance data
* @param[in] socket Socket descriptor
* @param[in] frame Websocket frame
* @param[in] callback Callback invoked after sending data
* @param[in] arg User data passed to provided callback
* @return
* - ESP_OK : On successful
* - ESP_FAIL : When socket errors occurs
* - ESP_ERR_NO_MEM : Unable to allocate memory
*/
esp_err_t httpd_ws_send_data_async(httpd_handle_t handle, int socket, httpd_ws_frame_t *frame,
transfer_complete_cb callback, void *arg);
#endif /* CONFIG_HTTPD_WS_SUPPORT */
/** End of WebSocket related stuff
* @}

Some files were not shown because too many files have changed in this diff Show More