Skip to content

Commit

Permalink
feat: update for new mdns cmds and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxwelltoo committed Dec 13, 2023
1 parent 31651d2 commit 79b0c0f
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 38 deletions.
11 changes: 4 additions & 7 deletions porting/el_network.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ typedef enum {

typedef struct mdns_record {
char host_name[SSCMA_MDNS_HOST_NAME_LEN];
char serv_name[SSCMA_MDNS_SERV_NAME_LEN];
// properties: server, port, protocol, destination
char server[SSCMA_MDNS_SERV_NAME_LEN];
char destination[SSCMA_MDNS_DEST_NAME_LEN];
char protocol[8];
uint16_t port;
bool is_enabled;
} mdns_record_t;

typedef void (*status_cb_t)(el_net_sta_t sta);
Expand All @@ -69,10 +71,6 @@ class Network {
void init() {
this->init(nullptr);
};
void init(status_cb_t cb, mdns_record_t record) {
this->init(cb);
this->mdns = record;
}
virtual void deinit() = 0;
el_net_sta_t status() {
return this->network_status;
Expand Down Expand Up @@ -119,7 +117,6 @@ class Network {
protected:
bool _is_present;
el_net_sta_t network_status;
mdns_record_t mdns;
};

} // namespace edgelab
Expand Down
8 changes: 8 additions & 0 deletions porting/himax/we2/el_network_at.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@
#define AT_TX_MAX_LEN 16384 // 默认固件的AT指令长度阈值为256
#define AT_RX_MAX_LEN 4096 // 可能连续收到多条消息

#define MDNS_ITEM_SERVER "server"
#define MDNS_ITEM_PORT "port"
#define MDNS_ITEM_PROT "proto"
#define MDNS_ITEM_DEST "dest"

#define SNTP_SERVER_CN "cn.ntp.org.cn"
#define SNTP_SERVER_US "us.pool.ntp.org"
#define SNTP_SERVER_CN2 "ntp.sjtu.edu.cn"
Expand All @@ -31,6 +36,9 @@
#define AT_STR_CWQAP "CWQAP"
#define AT_STR_CIPSTA "CIPSTA"

#define AT_STR_MDNSSTART "MDNS_START"
#define AT_STR_MDNSADD "MDNS_ADD"

#define AT_STR_MQTTUSERCFG "MQTTUSERCFG"
#define AT_STR_MQTTCONN "MQTTCONN"
#define AT_STR_MQTTPUB "MQTTPUB"
Expand Down
47 changes: 33 additions & 14 deletions porting/himax/we2/el_network_we2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -224,17 +224,6 @@ el_err_code_t NetworkWE2::join(const char* ssid, const char* pwd) {
EL_LOGD("AT CWJAP ERROR : %d\n", err);
return err;
}

if (this->mdns.is_enabled) {
// TODO: check parameter, and support multiple service
sprintf(at.tbuf, AT_STR_HEADER "MDNS=1,\"%s\",\"%s\",%d" AT_STR_CRLF,
this->mdns.host_name, this->mdns.serv_name, this->mdns.port);
err = at_send(&at, AT_SHORT_TIME_MS);
if (err != EL_OK) {
EL_LOGD("AT MDNS ERROR : %d\n", err);
return err;
}
}
sprintf(at.tbuf, AT_STR_HEADER AT_STR_CIPSTA "?" AT_STR_CRLF);
at_send(&at, AT_SHORT_TIME_MS);

Expand All @@ -256,15 +245,45 @@ el_err_code_t NetworkWE2::quit() {
}

el_err_code_t NetworkWE2::set_mdns(mdns_record_t record) {
this->mdns = record;
el_err_code_t err = EL_OK;
sprintf(at.tbuf, AT_STR_HEADER "MDNS=1,\"%s\",\"%s\",%d" AT_STR_CRLF,
this->mdns.host_name, this->mdns.serv_name, this->mdns.port);
sprintf(at.tbuf, AT_STR_HEADER AT_STR_MDNSSTART "=\"%s\"" AT_STR_CRLF,
record.host_name);
err = at_send(&at, AT_SHORT_TIME_MS);
if (err != EL_OK) {
EL_LOGD("AT MDNS ERROR : %d\n", err);
return err;
}

sprintf(at.tbuf, AT_STR_HEADER AT_STR_MDNSADD "=\"%s\",\"%s\"" AT_STR_CRLF,
MDNS_ITEM_SERVER, record.server);
err = at_send(&at, AT_SHORT_TIME_MS);
if (err != EL_OK) {
EL_LOGD("AT MDNS ADD %s ERROR : %d\n", MDNS_ITEM_SERVER, err);
return err;
}
sprintf(at.tbuf, AT_STR_HEADER AT_STR_MDNSADD "=\"%s\",\"%d\"" AT_STR_CRLF,
MDNS_ITEM_PORT, record.port);
err = at_send(&at, AT_SHORT_TIME_MS);
if (err != EL_OK) {
EL_LOGD("AT MDNS ADD %s ERROR : %d\n", MDNS_ITEM_PORT, err);
return err;
}
sprintf(at.tbuf, AT_STR_HEADER AT_STR_MDNSADD "=\"%s\",\"%s\"" AT_STR_CRLF,
MDNS_ITEM_PROT, record.protocol);
err = at_send(&at, AT_SHORT_TIME_MS);
if (err != EL_OK) {
EL_LOGD("AT MDNS ADD %s ERROR : %d\n", MDNS_ITEM_PROT, err);
return err;
}
sprintf(at.tbuf, AT_STR_HEADER AT_STR_MDNSADD "=\"%s\",\"%s\"" AT_STR_CRLF,
MDNS_ITEM_DEST, record.destination);
err = at_send(&at, AT_SHORT_TIME_MS);
if (err != EL_OK) {
EL_LOGD("AT MDNS ADD %s ERROR : %d\n", MDNS_ITEM_DEST, err);
return err;
}

return EL_OK;
}

el_err_code_t NetworkWE2::connect(const mqtt_server_config_t mqtt_cfg, topic_cb_t cb) {
Expand Down
11 changes: 4 additions & 7 deletions sscma/definations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,13 +56,11 @@
#define SSCMA_MQTT_DISCOVER_TOPIC "sscma/%s/discover"

#define SSCMA_MQTT_CLIENT_ID_FMT "%s_%s_%s"
#define SSCMA_MQTT_PUB_FMT "sscma/%s/%s/tx"
#define SSCMA_MQTT_SUB_FMT "sscma/%s/%s/rx"
#define SSCMA_MQTT_DESTINATION_FMT "sscma/%s/%s"

#define SSCMA_MDNS_SERV_FMT "_%s_sscma"
#define SSCMA_MDNS_HOST_FMT "%s.%s.%s"
#define SSCMA_MDNS_SERV_NAME_LEN 96
#define SSCMA_MDNS_HOST_NAME_LEN 160
#define SSCMA_MDNS_HOST_NAME_LEN 64
#define SSCMA_MDNS_DEST_NAME_LEN 128
#define SSCMA_MDNS_SERV_NAME_LEN 128
#define SSCMA_MDNS_PORT 3141

static_assert(SSCMA_MQTT_DEFAULT_PORT > 0);
Expand All @@ -71,4 +69,3 @@ static_assert(SSCMA_MQTT_DEFAULT_SSL_PORT > 0);
static_assert(SSCMA_MQTT_DEFAULT_SSL_PORT < 65536);
static_assert(SSCMA_MQTT_TOPIC_LEN > SSCMA_MQTT_CLIENT_ID_LEN);
static_assert(SSCMA_MDNS_SERV_NAME_LEN > SSCMA_MQTT_CLIENT_ID_LEN);
static_assert(SSCMA_MDNS_HOST_NAME_LEN > SSCMA_MQTT_ADDRESS_LEN);
24 changes: 14 additions & 10 deletions sscma/interface/transport/mqtt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,15 +198,19 @@ class MQTT final : public Supervisable, public Transport {
auto server_config = _mqtt_server_config.load().second.second;
mdns_record_t record{};

std::snprintf(record.serv_name, (sizeof record.serv_name) - 1, SSCMA_MDNS_SERV_FMT, server_config.client_id);
std::snprintf(record.host_name,
(sizeof record.host_name) - 1,
SSCMA_MDNS_HOST_FMT,
std::snprintf(record.host_name, sizeof(record.host_name) - 1,
"%s", server_config.client_id);

// properties: server, port, protocol, destination
std::snprintf(record.server, sizeof(record.server) - 1,
"%s", server_config.address);
std::snprintf(record.protocol, sizeof(record.protocol) - 1,
"%s", server_config.use_ssl ? "mqtts" : "mqtt");
std::snprintf(record.destination, sizeof(record.destination) - 1,
SSCMA_MQTT_DESTINATION_FMT,
SSCMA_AT_API_MAJOR_VERSION,
server_config.use_ssl ? "mqtts" : "mqtt",
server_config.address);
record.port = server_config.port;
record.is_enabled = true;
server_config.client_id);
record.port = server_config.port;

_network->set_mdns(record);
}
Expand All @@ -228,14 +232,14 @@ class MQTT final : public Supervisable, public Transport {

std::snprintf(pubsub_config.pub_topic,
sizeof(pubsub_config.pub_topic) - 1,
SSCMA_MQTT_PUB_FMT,
SSCMA_MQTT_DESTINATION_FMT "/tx",
SSCMA_AT_API_MAJOR_VERSION,
server_config.client_id);
pubsub_config.pub_qos = 0;

std::snprintf(pubsub_config.sub_topic,
sizeof(pubsub_config.sub_topic) - 1,
SSCMA_MQTT_SUB_FMT,
SSCMA_MQTT_DESTINATION_FMT "/rx",
SSCMA_AT_API_MAJOR_VERSION,
server_config.client_id);
pubsub_config.sub_qos = 0;
Expand Down

0 comments on commit 79b0c0f

Please sign in to comment.