Skip to content

Commit

Permalink
cfg: move DEVICE and MANUFACTURER to functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr committed Apr 8, 2020
1 parent 492a359 commit 2dcf1b2
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 29 deletions.
3 changes: 2 additions & 1 deletion code/espurna/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ BOARD MODULE

#pragma once

String getIdentifier();
const String& getChipId();
const String& getIdentifier();

String getEspurnaModules();
String getEspurnaOTAModules();
Expand Down
23 changes: 19 additions & 4 deletions code/espurna/board.ino
Original file line number Diff line number Diff line change
Expand Up @@ -303,10 +303,25 @@ PROGMEM const char espurna_sensors[] =

//--------------------------------------------------------------------------------

String getIdentifier() {
char buffer[20];
snprintf_P(buffer, sizeof(buffer), PSTR("%s-%06X"), APP_NAME, ESP.getChipId());
return String(buffer);
const String& getChipId() {
static String value;
if (!value.length()) {
char buffer[7];
value.reserve(sizeof(buffer));
snprintf_P(buffer, sizeof(buffer), PSTR("%06X"), ESP.getChipId());
value = buffer;
}
return value;
}

const String& getIdentifier() {
static String value;
if (!value.length()) {
value += APP_NAME;
value += '-';
value += getChipId();
}
return value;
}

String getEspurnaModules() {
Expand Down
9 changes: 5 additions & 4 deletions code/espurna/homeassistant.ino
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Copyright (C) 2017-2019 by Xose Pérez <xose dot perez at gmail dot com>
#include "mqtt.h"
#include "relay.h"
#include "rpc.h"
#include "utils.h"
#include "ws.h"

bool _ha_enabled = false;
Expand Down Expand Up @@ -78,8 +79,8 @@ struct ha_config_t {
deviceConfig.createNestedArray("identifiers").add(identifier.c_str());
deviceConfig["name"] = name.c_str();
deviceConfig["sw_version"] = version.c_str();
deviceConfig["manufacturer"] = MANUFACTURER;
deviceConfig["model"] = DEVICE;
deviceConfig["manufacturer"] = getDevice().c_str();
deviceConfig["model"] = getManufacturer().c_str();
}

ha_config_t() : ha_config_t(DEFAULT_BUFFER_SIZE) {}
Expand Down Expand Up @@ -394,8 +395,8 @@ void _haSensorYaml(unsigned char index, JsonObject& root) {
void _haGetDeviceConfig(JsonObject& config) {
config.createNestedArray("identifiers").add(getIdentifier());
config["name"] = getSetting("desc", getSetting("hostname"));
config["manufacturer"] = MANUFACTURER;
config["model"] = DEVICE;
config["manufacturer"] = getManufacturer().c_str();
config["model"] = getDevice().c_str();
config["sw_version"] = String(APP_NAME) + " " + APP_VERSION + " (" + getCoreVersion() + ")";
}

Expand Down
6 changes: 4 additions & 2 deletions code/espurna/ssdp.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ https://github.com/esp8266/Arduino/issues/2283#issuecomment-299635604

#include <ESP8266SSDP.h>

#include "utils.h"

const char _ssdp_template[] PROGMEM =
"<?xml version=\"1.0\"?>"
"<root xmlns=\"urn:schemas-upnp-org:device-1-0\">"
Expand Down Expand Up @@ -54,7 +56,7 @@ void ssdpSetup() {
APP_NAME, // modelName
APP_VERSION, // modelNumber
APP_WEBSITE, // modelURL
DEVICE_NAME, // manufacturer
getBoardName().c_str(), // manufacturer
"", // manufacturerURL
chipId // UUID
);
Expand All @@ -71,7 +73,7 @@ void ssdpSetup() {
SSDP.setModelName(APP_NAME);
SSDP.setModelNumber(APP_VERSION);
SSDP.setModelURL(APP_WEBSITE);
SSDP.setManufacturer(DEVICE_NAME);
SSDP.setManufacturer(getBoardName());
SSDP.setManufacturerURL("");
SSDP.setURL("/");
SSDP.begin();
Expand Down
4 changes: 2 additions & 2 deletions code/espurna/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ extern "C" uint32_t _SPIFFS_end;
void setDefaultHostname();

void setBoardName();
String getBoardName();
String getAdminPass();

const String& getDevice();
const String& getManufacturer();
const String& getCoreVersion();
const String& getCoreRevision();

Expand Down
24 changes: 17 additions & 7 deletions code/espurna/utils.ino
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,35 @@ PROGMEM const char* const custom_reset_string[] = {

void setDefaultHostname() {
if (strlen(HOSTNAME) > 0) {
setSetting("hostname", HOSTNAME);
setSetting("hostname", F(HOSTNAME));
} else {
setSetting("hostname", getIdentifier());
}
}

void setBoardName() {
if (!isEspurnaCore()) {
setSetting("boardName", DEVICE_NAME);
}
const String& getDevice() {
static const String value(F(DEVICE));
return value;
}

const String& getManufacturer() {
static const String value(F(MANUFACTURER));
return value;
}

String getBoardName() {
static const String defaultValue(DEVICE_NAME);
static const String defaultValue(F(DEVICE_NAME));
return getSetting("boardName", defaultValue);
}

void setBoardName() {
if (!isEspurnaCore()) {
setSetting("boardName", F(DEVICE_NAME));
}
}

String getAdminPass() {
static const String defaultValue(ADMIN_PASS);
static const String defaultValue(F(ADMIN_PASS));
return getSetting("adminPass", defaultValue);
}

Expand Down
10 changes: 4 additions & 6 deletions code/espurna/ws.ino
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Copyright (C) 2016-2019 by Xose Pérez <xose dot perez at gmail dot com>

#include "system.h"
#include "web.h"
#include "utils.h"
#include "ws.h"
#include "ws_internal.h"

Expand Down Expand Up @@ -387,9 +388,6 @@ bool _wsOnKeyCheck(const char * key, JsonVariant& value) {
}

void _wsOnConnected(JsonObject& root) {
char chipid[7];
snprintf_P(chipid, sizeof(chipid), PSTR("%06X"), ESP.getChipId());

root["webMode"] = WEB_MODE_NORMAL;

root["app_name"] = APP_NAME;
Expand All @@ -398,12 +396,12 @@ void _wsOnConnected(JsonObject& root) {
#if defined(APP_REVISION)
root["app_revision"] = APP_REVISION;
#endif
root["manufacturer"] = MANUFACTURER;
root["chipid"] = String(chipid);
root["device"] = getDevice().c_str();
root["manufacturer"] = getManufacturer().c_str();
root["chipid"] = getChipId().c_str();
root["mac"] = WiFi.macAddress();
root["bssid"] = WiFi.BSSIDstr();
root["channel"] = WiFi.channel();
root["device"] = DEVICE;
root["hostname"] = getSetting("hostname");
root["desc"] = getSetting("desc");
root["network"] = getNetwork();
Expand Down
6 changes: 3 additions & 3 deletions code/platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -280,15 +280,15 @@ build_flags = ${common.build_flags} -DJUSTWIFI_ENABLE_WPS=1

[env:espurna-core-webui-1MB]
extends = env:esp8266-1m-base
src_build_flags = -DESPURNA_CORE_WEB
src_build_flags = -DESPURNA_CORE_WEBUI

[env:espurna-core-webui-2MB]
extends = env:esp8266-2m-base
src_build_flags = -DESPURNA_CORE_WEB
src_build_flags = -DESPURNA_CORE_WEBUI

[env:espurna-core-webui-4MB]
extends = env:esp8266-4m-base
src_build_flags = -DESPURNA_CORE_WEB
src_build_flags = -DESPURNA_CORE_WEBUI

# ------------------------------------------------------------------------------
# DEVELOPMENT BOARDS
Expand Down

0 comments on commit 2dcf1b2

Please sign in to comment.