Skip to content

Commit

Permalink
Add [undocumented] hostname command with esp ESP-AT firmware
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Sep 28, 2019
1 parent f8fc49f commit 4beb087
Show file tree
Hide file tree
Showing 8 changed files with 224 additions and 0 deletions.
88 changes: 88 additions & 0 deletions src/esp/esp_hostname.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/**
* \file esp_hostname.c
* \brief Hostname API
*/

/*
* Copyright (c) 2019 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of ESP-AT library.
*
* Author: Tilen MAJERLE <[email protected]>
*/
#include "esp/esp_private.h"
#include "esp/esp_hostname.h"
#include "esp/esp_mem.h"

#if ESP_CFG_HOSTNAME || __DOXYGEN__

/**
* \brief Set hostname of WiFi station
* \param[in] hostname: Name of ESP host
* \param[in] evt_fn: Callback function called when command has finished. Set to `NULL` when not used
* \param[in] evt_arg: Custom argument for event callback function
* \param[in] blocking: Status whether command should be blocking or not
* \return \ref espOK on success, member of \ref espr_t enumeration otherwise
*/
espr_t
esp_hostname_set(const char* hostname,
const esp_api_cmd_evt_fn evt_fn, void* const evt_arg, const uint32_t blocking) {
ESP_MSG_VAR_DEFINE(msg);

ESP_ASSERT("hostname != NULL", hostname != NULL);

ESP_MSG_VAR_ALLOC(msg, blocking);
ESP_MSG_VAR_SET_EVT(msg, evt_fn, evt_arg);
ESP_MSG_VAR_REF(msg).cmd_def = ESP_CMD_WIFI_CWHOSTNAME_SET;
ESP_MSG_VAR_REF(msg).msg.wifi_hostname.hostname_set = hostname;

return espi_send_msg_to_producer_mbox(&ESP_MSG_VAR_REF(msg), espi_initiate_cmd, 1000);
}

/**
* \brief Get hostname of WiFi station
* \param[in] hostname: Pointer to output variable holding memory to save hostname
* \param[in] size: Size of buffer for hostname. Size includes memory for `NULL` termination
* \param[in] evt_fn: Callback function called when command has finished. Set to `NULL` when not used
* \param[in] evt_arg: Custom argument for event callback function
* \param[in] blocking: Status whether command should be blocking or not
* \return \ref espOK on success, member of \ref espr_t enumeration otherwise
*/
espr_t
esp_hostname_get(char* hostname, size_t size,
const esp_api_cmd_evt_fn evt_fn, void* const evt_arg, const uint32_t blocking) {
ESP_MSG_VAR_DEFINE(msg);

ESP_ASSERT("hostname != NULL", hostname != NULL);
ESP_ASSERT("size > 0", size > 0);

ESP_MSG_VAR_ALLOC(msg, blocking);
ESP_MSG_VAR_SET_EVT(msg, evt_fn, evt_arg);
ESP_MSG_VAR_REF(msg).cmd_def = ESP_CMD_WIFI_CWHOSTNAME_GET;
ESP_MSG_VAR_REF(msg).msg.wifi_hostname.hostname_get = hostname;
ESP_MSG_VAR_REF(msg).msg.wifi_hostname.length = size;

return espi_send_msg_to_producer_mbox(&ESP_MSG_VAR_REF(msg), espi_initiate_cmd, 1000);
}

#endif /* ESP_CFG_HOSTNAME || __DOXYGEN__ */
19 changes: 19 additions & 0 deletions src/esp/esp_int.c
Original file line number Diff line number Diff line change
Expand Up @@ -664,6 +664,10 @@ espi_parse_received(esp_recv_t* rcv) {
} else if (CMD_IS_CUR(ESP_CMD_TCPIP_CIPSNTPTIME) && !strncmp(rcv->data, "+CIPSNTPTIME", 12)) {
espi_parse_cipsntptime(rcv->data, esp.msg); /* Parse CIPSNTPTIME entry */
#endif /* ESP_CFG_SNTP */
#if ESP_CFG_HOSTNAME
} else if (CMD_IS_CUR(ESP_CMD_WIFI_CWHOSTNAME_GET) && !strncmp(rcv->data, "+CWHOSTNAME", 11)) {
espi_parse_hostname(rcv->data, esp.msg);/* Parse HOSTNAME entry */
#endif /* ESP_CFG_HOSTNAME */
} else if (CMD_IS_CUR(ESP_CMD_WIFI_CWDHCP_GET) && !strncmp(rcv->data, "+CWDHCP", 7)) {
espi_parse_cwdhcp(rcv->data); /* Parse CWDHCP state */
}
Expand Down Expand Up @@ -1748,6 +1752,21 @@ espi_initiate_cmd(esp_msg_t* msg) {
break;
}
#endif /* ESP_CFG_WPS */
#if ESP_CFG_HOSTNAME
case ESP_CMD_WIFI_CWHOSTNAME_SET: { /* List stations connected on access point */
AT_PORT_SEND_BEGIN_AT();
AT_PORT_SEND_CONST_STR("+CWHOSTNAME=");
espi_send_string(msg->msg.wifi_hostname.hostname_set, 1, 1, 0);
AT_PORT_SEND_END_AT();
break;
}
case ESP_CMD_WIFI_CWHOSTNAME_GET: { /* List stations connected on access point */
AT_PORT_SEND_BEGIN_AT();
AT_PORT_SEND_CONST_STR("+CWHOSTNAME?");
AT_PORT_SEND_END_AT();
break;
}
#endif /* ESP_CFG_HOSTNAME */
#if ESP_CFG_MDNS
case ESP_CMD_WIFI_MDNS: { /* Set mDNS parameters */
AT_PORT_SEND_BEGIN_AT();
Expand Down
32 changes: 32 additions & 0 deletions src/esp/esp_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -622,6 +622,38 @@ espi_parse_cipsntptime(const char* str, esp_msg_t* msg) {

#endif /* ESP_CFG_SNTP || __DOXYGEN__ */

#if ESP_CFG_HOSTNAME || __DOXYGEN__

/**
* \brief Parse received message for HOSTNAME
* \param[in] str: Pointer to input string starting with +CWHOSTNAME
* \param[in] msg: Pointer to message
* \return `1` on success, `0` otherwise
*/
uint8_t
espi_parse_hostname(const char* str, esp_msg_t* msg) {
size_t i;
if (!CMD_IS_DEF(ESP_CMD_WIFI_CWHOSTNAME_GET)) {
return 0;
}
if (*str == '+') { /* Check input string */
str += 12;
}
msg->msg.wifi_hostname.hostname_get[0] = 0;
if (*str != '\r') {
i = 0;
while (i < (msg->msg.wifi_hostname.length - 1) && *str && *str != '\r') {
msg->msg.wifi_hostname.hostname_get[i] = *str;
i++;
str++;
}
msg->msg.wifi_hostname.hostname_get[i] = 0;
}
return 1;
}

#endif /* ESP_CFG_HOSTNAME || __DOXYGEN__ */

/**
* \brief Parse received message for DHCP
* \param[in] str: Pointer to input string starting with +CWDHCP
Expand Down
9 changes: 9 additions & 0 deletions src/include/esp/esp_config_default.h
Original file line number Diff line number Diff line change
Expand Up @@ -607,6 +607,15 @@
#define ESP_CFG_SNTP 0
#endif

/**
* \brief Enables `1` or disables `0` support for hostname with AT commands
*
*/
#ifndef ESP_CFG_HOSTNAME
#define ESP_CFG_HOSTNAME 0
#endif


/**
* \brief Enables `1` or disables `0` support for mDNS
*
Expand Down
60 changes: 60 additions & 0 deletions src/include/esp/esp_hostname.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* \file esp_hostname.h
* \brief Hostname API
*/

/*
* Copyright (c) 2019 Tilen MAJERLE
*
* Permission is hereby granted, free of charge, to any person
* obtaining a copy of this software and associated documentation
* files (the "Software"), to deal in the Software without restriction,
* including without limitation the rights to use, copy, modify, merge,
* publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
* OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* This file is part of ESP-AT library.
*
* Author: Tilen MAJERLE <[email protected]>
*/
#ifndef ESP_HDR_HOSTNAME_H
#define ESP_HDR_HOSTNAME_H

#ifdef __cplusplus
extern "C" {
#endif

#include "esp/esp.h"

/**
* \ingroup ESP
* \defgroup ESP_HOSTNAME Hostname API
* \brief Hostname API
* \{
*/

espr_t esp_hostname_set(const char* hostname, const esp_api_cmd_evt_fn evt_fn, void* const evt_arg, const uint32_t blocking);
espr_t esp_hostname_get(char* hostname, size_t size, const esp_api_cmd_evt_fn evt_fn, void* const evt_arg, const uint32_t blocking);

/**
* \}
*/

#ifdef __cplusplus
}
#endif

#endif /* ESP_HDR_HOSTNAME_H */
3 changes: 3 additions & 0 deletions src/include/esp/esp_includes.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ extern "C" {
#if ESP_CFG_SNTP || __DOXYGEN__
#include "esp/esp_sntp.h"
#endif /* ESP_CFG_SNTP || __DOXYGEN__ */
#if ESP_CFG_HOSTNAME || __DOXYGEN__
#include "esp/esp_hostname.h"
#endif /* ESP_CFG_HOSTNAME || __DOXYGEN__ */
#if ESP_CFG_DNS || __DOXYGEN__
#include "esp/esp_dns.h"
#endif /* ESP_CFG_DNS || __DOXYGEN__ */
Expand Down
1 change: 1 addition & 0 deletions src/include/esp/esp_parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ uint8_t espi_parse_cwjap(const char* str, esp_msg_t* msg);
uint8_t espi_parse_cwlif(const char* str, esp_msg_t* msg);
uint8_t espi_parse_cipdomain(const char* src, esp_msg_t* msg);
uint8_t espi_parse_cipsntptime(const char* str, esp_msg_t* msg);
uint8_t espi_parse_hostname(const char* str, esp_msg_t* msg);
uint8_t espi_parse_link_conn(const char* str);

uint8_t espi_parse_at_sdk_version(const char* str, esp_sw_version_t* version_out);
Expand Down
12 changes: 12 additions & 0 deletions src/include/esp/esp_private.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ typedef enum {
#if ESP_CFG_MDNS || __DOXYGEN__
ESP_CMD_WIFI_MDNS, /*!< Configure MDNS function */
#endif /* ESP_CFG_MDNS || __DOXYGEN__ */
#if ESP_CFG_HOSTNAME || __DOXYGEN__
ESP_CMD_WIFI_CWHOSTNAME_SET, /*!< Set device hostname */
ESP_CMD_WIFI_CWHOSTNAME_GET, /*!< Get device hostname */
#endif /* ESP_CFG_HOSTNAME || __DOXYGEN__ */

/* TCP/IP related commands */
#if ESP_CFG_DNS || __DOXYGEN__
Expand Down Expand Up @@ -295,6 +299,14 @@ typedef struct esp_msg {
uint8_t en; /*!< Enable/disable DHCP settings */
} wifi_cwdhcp; /*!< Set DHCP settings */

#if ESP_CFG_HOSTNAME || __DOXYGEN__
struct {
const char* hostname_set; /*!< Hostname set value */
char* hostname_get; /*!< Hostname get value */
size_t length; /*!< Length of buffer when reading hostname */
} wifi_hostname; /*!< Set or get hostname structure */
#endif /* ESP_CFG_HOSTNAME || __DOXYGEN__ */

/* Connection based commands */
struct {
esp_conn_t** conn; /*!< Pointer to pointer to save connection used */
Expand Down

0 comments on commit 4beb087

Please sign in to comment.