Skip to content

Commit

Permalink
std::string changes - lint
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartpittaway committed Aug 30, 2023
1 parent 7af23bb commit 555447c
Show file tree
Hide file tree
Showing 10 changed files with 92 additions and 80 deletions.
4 changes: 2 additions & 2 deletions ESPController/include/pylon_canbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ extern uint8_t TotalNumberOfCells();
extern Rules rules;
extern currentmonitoring_struct currentMonitor;
extern diybms_eeprom_settings mysettings;
extern char hostname[16];
extern std::string hostname;
extern ControllerState _controller_state;
extern uint32_t canbus_messages_failed_sent;
extern uint32_t canbus_messages_sent;
extern uint32_t canbus_messages_received;

extern void send_canbus_message(uint32_t identifier, uint8_t *buffer, uint8_t length);
extern void send_canbus_message(uint32_t identifier, const uint8_t *buffer,const uint8_t length);

#endif
4 changes: 2 additions & 2 deletions ESPController/include/tft.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,8 @@ extern TaskHandle_t updatetftdisplay_task_handle;
extern avrprogramsettings _avrsettings;
extern currentmonitoring_struct currentMonitor;
extern bool wifi_isconnected;
extern char hostname[16];
extern char ip_string[16];
extern std::string hostname;
extern std::string ip_string;
extern PacketRequestGenerator prg;
extern PacketReceiveProcessor receiveProc;
extern uint32_t canbus_messages_received;
Expand Down
4 changes: 2 additions & 2 deletions ESPController/include/victron_canbus.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ void victron_message_373();
void victron_message_35f();
void victron_message_374_375_376_377();

extern void send_canbus_message(uint32_t identifier, uint8_t *buffer, uint8_t length);
extern void send_canbus_message(uint32_t identifier, const uint8_t *buffer,const uint8_t length);

extern uint8_t TotalNumberOfCells();
extern Rules rules;
extern currentmonitoring_struct currentMonitor;
extern diybms_eeprom_settings mysettings;
extern char hostname[16];
extern std::string hostname;
extern ControllerState _controller_state;
extern uint32_t canbus_messages_failed_sent;
extern uint32_t canbus_messages_sent;
Expand Down
4 changes: 2 additions & 2 deletions ESPController/include/webserver_json_post.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ extern Rules rules;
extern CardAction card_action;

extern avrprogramsettings _avrsettings;
extern wifi_eeprom_settings _wificonfig;

extern void stopMqtt();
extern void ConfigureRS485();
Expand All @@ -34,9 +35,9 @@ extern void CurrentMonitorSetAdvancedSettings(currentmonitoring_struct newvalues
extern void CurrentMonitorSetRelaySettingsInternal(currentmonitoring_struct newvalues);
extern void CurrentMonitorSetRelaySettingsExternal(currentmonitoring_struct newvalues);
extern void setCacheControl(httpd_req_t *req);
extern wifi_eeprom_settings _wificonfig;
extern void configureSNTP(long gmtOffset_sec, int daylightOffset_sec, const char *server1);
extern void DefaultConfiguration(diybms_eeprom_settings *_myset);
extern bool SaveWIFIJson(const wifi_eeprom_settings* setting);

esp_err_t post_savebankconfig_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_saventp_json_handler(httpd_req_t *req, bool urlEncoded);
Expand Down Expand Up @@ -67,6 +68,5 @@ esp_err_t post_savecurrentmon_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_saverules_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t post_restoreconfig_json_handler(httpd_req_t *req, bool urlEncoded);
esp_err_t save_data_handler(httpd_req_t *req);
bool SaveWIFIJson();

#endif
2 changes: 1 addition & 1 deletion ESPController/include/webserver_json_requests.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extern ControllerState _controller_state;
extern void formatCurrentDateTime(char* buf, size_t buf_size);
extern void setNoStoreCacheControl(httpd_req_t *req);
extern char CookieValue[20 + 1];
extern char hostname[16];
extern std::string hostname;

extern uint32_t time100;
extern uint32_t time20;
Expand Down
84 changes: 72 additions & 12 deletions ESPController/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ volatile bool emergencyStop = false;
bool _sd_card_installed = false;

// Used for WIFI hostname and also sent to Victron over CANBUS
char hostname[16];
char ip_string[16]; // xxx.xxx.xxx.xxx
std::string hostname;
std::string ip_string; // xxx.xxx.xxx.xxx

bool wifi_isconnected = false;

Expand Down Expand Up @@ -1566,7 +1566,7 @@ static void startMDNS()
}
else
{
mdns_hostname_set(hostname);
mdns_hostname_set(hostname.c_str());
mdns_instance_name_set("diybms");
mdns_service_add(nullptr, "_http", "_tcp", 80, nullptr, 0);
}
Expand Down Expand Up @@ -1648,11 +1648,14 @@ static void event_handler(void *, esp_event_base_t event_base,

startMDNS();

snprintf(ip_string, sizeof(ip_string), IPSTR, IP2STR(&event->ip_info.ip));
char buffer[20];
snprintf(buffer, sizeof(buffer), IPSTR, IP2STR(&event->ip_info.ip));
ip_string.clear();
ip_string.append(buffer);

wake_up_tft(true);

ESP_LOGI(TAG, "You can access DIYBMS interface at http://%s.local or http://%s", hostname, ip_string);
ESP_LOGI(TAG, "You can access DIYBMS interface at http://%s.local or http://%s", hostname.c_str(), ip_string.c_str());
}
}

Expand All @@ -1666,6 +1669,57 @@ bool LoadWiFiConfig()
return reply;
}

bool SaveWIFIJson(const wifi_eeprom_settings* setting)
{
if (!_sd_card_installed)
{
return false;
}

if (_avrsettings.programmingModeEnabled)
{
return false;
}

StaticJsonDocument<512> doc;

JsonObject wifi = doc.createNestedObject("wifi");
wifi["ssid"] = setting->wifi_ssid;
wifi["password"] = setting->wifi_passphrase;

// Manual IP settings
wifi["ip"] = IPAddress(setting->wifi_ip).toString();
wifi["gateway"] = IPAddress(setting->wifi_gateway).toString();
wifi["netmask"] = IPAddress(setting->wifi_netmask).toString();
wifi["dns1"] = IPAddress(setting->wifi_dns1).toString();
wifi["dns2"] = IPAddress(setting->wifi_dns2).toString();
wifi["manualconfig"] = setting->manualConfig;

if (hal.GetVSPIMutex())
{
ESP_LOGI(TAG, "Creating folder");
SD.mkdir("/diybms");

// Get the file
ESP_LOGI(TAG, "Write SD file %s", wificonfigfilename);

if (SD.exists(wificonfigfilename))
{
ESP_LOGI(TAG, "Delete existing %s", wificonfigfilename);
SD.remove(wificonfigfilename);
}

File file = SD.open(wificonfigfilename, "w");
serializeJson(doc, file);
file.close();

hal.ReleaseVSPIMutex();
return true;
}

return false;
}

void BuildHostname()
{
uint32_t chipId = 0;
Expand All @@ -1674,8 +1728,11 @@ void BuildHostname()
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
}
// DIYBMS-00000000
memset(&hostname, 0, sizeof(hostname));
snprintf(hostname, sizeof(hostname), "DIYBMS-%08X", chipId);
char buffer[10];
snprintf(buffer, sizeof(buffer), "%08X", chipId);

hostname.clear();
hostname.append("DIYBMS-").append(buffer);
}

void wifi_init_sta(void)
Expand Down Expand Up @@ -1759,10 +1816,10 @@ void wifi_init_sta(void)
ESP_ERROR_CHECK(esp_wifi_set_rssi_threshold(-80));
ESP_ERROR_CHECK(esp_wifi_start());

ESP_ERROR_CHECK_WITHOUT_ABORT(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_set_hostname(netif, hostname));
ESP_ERROR_CHECK_WITHOUT_ABORT(tcpip_adapter_set_hostname(TCPIP_ADAPTER_IF_STA, hostname.c_str()));
ESP_ERROR_CHECK_WITHOUT_ABORT(esp_netif_set_hostname(netif, hostname.c_str()));

ESP_LOGI(TAG, "Hostname: %s", hostname);
ESP_LOGI(TAG, "Hostname: %s", hostname.c_str());

ESP_LOGD(TAG, "wifi_init_sta finished");
}
Expand Down Expand Up @@ -2570,7 +2627,7 @@ static const char *ESP32_TWAI_STATUS_STRINGS[] = {
"RECOVERY UNDERWAY" // CAN_STATE_RECOVERING
};

void send_canbus_message(uint32_t identifier, uint8_t *buffer, uint8_t length)
void send_canbus_message(uint32_t identifier, const uint8_t *buffer, const uint8_t length)
{
twai_message_t message;
message.identifier = identifier;
Expand Down Expand Up @@ -3444,7 +3501,10 @@ bool AreWifiConfigurationsTheSame(const wifi_eeprom_settings *a, const wifi_eepr

return true;
}

/// @brief CHECK FOR THE PRESENCE OF A wifi.json CONFIG FILE ON THE SD CARD AND AUTOMATICALLY CONFIGURE WIFI
/// @param existingConfigValid true if the wifi settings in FLASH are valid/correctly stored
/// @return TRUE if the WIFI config on the card is different, false if identical
bool LoadWiFiConfigFromSDCard(const bool existingConfigValid)
{
StaticJsonDocument<2048> json;
Expand Down Expand Up @@ -3559,7 +3619,7 @@ const std::array<log_level_t, 21> log_levels =
log_level_t{.tag = "*", .level = ESP_LOG_DEBUG},
{.tag = "wifi", .level = ESP_LOG_WARN},
{.tag = "dhcpc", .level = ESP_LOG_WARN},
{.tag = "diybms", .level = ESP_LOG_VERBOSE},
{.tag = "diybms", .level = ESP_LOG_DEBUG},
{.tag = "diybms-avrisp", .level = ESP_LOG_INFO},
{.tag = "diybms-hal", .level = ESP_LOG_INFO},
{.tag = "diybms-influxdb", .level = ESP_LOG_INFO},
Expand Down
4 changes: 2 additions & 2 deletions ESPController/src/tft.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@ void TFTDrawWifiDetails()

if (wifi_isconnected)
{
x += tft.drawString(hostname, x, y);
x += tft.drawString(hostname.c_str(), x, y);
x += 10;
x += tft.drawString(ip_string, x, y);
x += tft.drawString(ip_string.c_str(), x, y);

// Draw RSSI on bottom right corner
// Received Signal Strength in dBm
Expand Down
9 changes: 6 additions & 3 deletions ESPController/src/victron_canbus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ static constexpr const char *const TAG = "diybms-victron";
// Transmit the DIYBMS hostname via two CAN Messages
void victron_message_370_371()
{
send_canbus_message(0x370, (uint8_t *)&hostname, 8);
send_canbus_message(0x371, (uint8_t *)&hostname[8], 8);
char buffer[16+1];
strncpy(buffer,hostname.c_str(),sizeof(buffer));

send_canbus_message(0x370, (const uint8_t *)&buffer[0], 8);
send_canbus_message(0x371, (const uint8_t *)&buffer[8], 8);
}

void victron_message_35e()
{
send_canbus_message(0x35e, (uint8_t *)&hostname, 6);
send_canbus_message(0x35e, (const uint8_t*)hostname.c_str(), 6);
}

void victron_message_35f()
Expand Down
55 changes: 2 additions & 53 deletions ESPController/src/webserver_json_post.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -217,62 +217,11 @@ esp_err_t post_saveconfigurationtoflash_json_handler(httpd_req_t *req, bool urlE
return SendSuccess(req);
}

bool SaveWIFIJson()
{
const char *wificonfigfilename = "/diybms/wifi.json";

if (!_sd_card_installed)
{
return false;
}

if (_avrsettings.programmingModeEnabled)
{
return false;
}

StaticJsonDocument<512> doc;

JsonObject wifi = doc.createNestedObject("wifi");
wifi["ssid"] = _wificonfig.wifi_ssid;
wifi["password"] = _wificonfig.wifi_passphrase;

// Manual IP settings
wifi["ip"] = IPAddress(_wificonfig.wifi_ip).toString();
wifi["gateway"] = IPAddress(_wificonfig.wifi_gateway).toString();
wifi["netmask"] = IPAddress(_wificonfig.wifi_netmask).toString();
wifi["dns1"] = IPAddress(_wificonfig.wifi_dns1).toString();
wifi["dns2"] = IPAddress(_wificonfig.wifi_dns2).toString();
wifi["manualconfig"] = _wificonfig.manualConfig;

if (hal.GetVSPIMutex())
{
ESP_LOGI(TAG, "Creating folder");
SD.mkdir("/diybms");

// Get the file
ESP_LOGI(TAG, "Write SD file %s", wificonfigfilename);

if (SD.exists(wificonfigfilename))
{
ESP_LOGI(TAG, "Delete existing %s", wificonfigfilename);
SD.remove(wificonfigfilename);
}

File file = SD.open(wificonfigfilename, "w");
serializeJson(doc, file);
file.close();

hal.ReleaseVSPIMutex();
return true;
}

return false;
}

esp_err_t post_savewificonfigtosdcard_json_handler(httpd_req_t *req, bool)
{
if (SaveWIFIJson())
if (SaveWIFIJson(&_wificonfig))
{
return SendSuccess(req);
}
Expand Down Expand Up @@ -768,7 +717,7 @@ esp_err_t post_savenetconfig_json_handler(httpd_req_t *req, bool urlEncoded)
SaveWIFI(&_wificonfig);

// Attempt to save to SD card - but this may not be installed
SaveWIFIJson();
SaveWIFIJson(&_wificonfig);

return SendSuccess(req);
}
Expand Down
2 changes: 1 addition & 1 deletion ESPController/src/webserver_json_requests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -733,7 +733,7 @@ esp_err_t content_handler_settings(httpd_req_t *req)
settings["MinutesTimeZone"] = mysettings.minutesTimeZone;
settings["DST"] = mysettings.daylight;

settings["HostName"] = hostname;
settings["HostName"] = hostname.c_str();

time_t now;
if (time(&now))
Expand Down

0 comments on commit 555447c

Please sign in to comment.