Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 26 additions & 3 deletions firmware_v5/telelogger/teleclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -549,6 +549,7 @@ bool TeleClientHTTP::transmit(const char* packetBuffer, unsigned int packetSize)
}

char path[256];
char url[256];
bool success = false;
int len;
#if SERVER_PROTOCOL == PROTOCOL_HTTPS_GET
Expand All @@ -559,7 +560,20 @@ bool TeleClientHTTP::transmit(const char* packetBuffer, unsigned int packetSize)
} else {
len = snprintf(url, sizeof(url), "%s/push?id=%s", SERVER_PATH, devid);
}

#if ENABLE_WIFI
if (wifi.connected()) {
Serial.print("[WIFI] ");
Serial.println(url);
success = wifi.send(METHOD_GET, url);
}
else
#else
Serial.print("[CELL] ");
Serial.println(url);
success = cell.send(METHOD_GET, SERVER_HOST, SERVER_PORT, url);
#endif

#else
len = snprintf(path, sizeof(path), "%s/post/%s", SERVER_PATH, devid);
#if ENABLE_WIFI
Expand Down Expand Up @@ -654,17 +668,26 @@ bool TeleClientHTTP::connect(bool quick)
return false;
}
if (quick) return true;

if (SERVER_HOST != "hub.freematics.com") {
Serial.println("[NET] Skipping notify call - assuming OsmAnd protocol");
return true;
}

if (!login) {
Serial.print("LOGIN(");
Serial.print(SERVER_HOST);
Serial.print(':');
Serial.print(SERVER_PORT);
Serial.println(")...");
// log in or reconnect to Freematics Hub
if (notify(EVENT_LOGIN)) {
lastSyncTime = millis();
login = true;
if (!notify(EVENT_LOGIN)) {
Serial.println("[NET] Login failed");
return false;
}
Serial.println("[NET] Login successful");
lastSyncTime = millis();
login = true;
}
return true;
}
Expand Down
42 changes: 31 additions & 11 deletions libraries/FreematicsPlus/FreematicsNetwork.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,32 +129,51 @@ void WifiUDP::close()
bool WifiHTTP::open(const char* host, uint16_t port)
{
if (!host) return true;
if (client.connect(host, port)) {
m_state = HTTP_CONNECTED;
m_host = host;
return true;

m_useSSL = (port == 443);
if (m_useSSL) {
// HTTPS
secureClient.setInsecure();
if (secureClient.connect(host, port)) {
m_state = HTTP_CONNECTED;
m_host = host;
return true;
}
} else {
m_state = HTTP_ERROR;
return false;
// HTTP
if (client.connect(host, port)) {
m_state = HTTP_CONNECTED;
m_host = host;
return true;
}
}

m_state = HTTP_ERROR;
return false;
}

void WifiHTTP::close()
{
client.stop();
if (m_useSSL) {
secureClient.stop();
} else {
client.stop();
}
m_state = HTTP_DISCONNECTED;
}

bool WifiHTTP::send(HTTP_METHOD method, const char* path, const char* payload, int payloadSize)
{
String header = genHeader(method, path, payload, payloadSize);
int len = header.length();
if (client.write(header.c_str(), len) != len) {
WiFiClient* activeClient = m_useSSL ? (WiFiClient*)&secureClient : &client;

if (activeClient->write(header.c_str(), len) != len) {
m_state = HTTP_DISCONNECTED;
return false;
}
if (payloadSize) {
if (client.write(payload, payloadSize) != payloadSize) {
if (activeClient->write(payload, payloadSize) != payloadSize) {
m_state = HTTP_ERROR;
return false;
}
Expand All @@ -170,13 +189,14 @@ char* WifiHTTP::receive(char* buffer, int bufsize, int* pbytes, unsigned int tim
int contentLen = 0;
char* content = 0;
bool keepAlive = true;
WiFiClient* activeClient = m_useSSL ? (WiFiClient*)&secureClient : &client;

for (uint32_t t = millis(); millis() - t < timeout && bytes < bufsize; ) {
if (!client.available()) {
if (!activeClient->available()) {
delay(1);
continue;
}
buffer[bytes++] = client.read();
buffer[bytes++] = activeClient->read();
buffer[bytes] = 0;
if (content) {
if (++contentBytes == contentLen) break;
Expand Down
3 changes: 3 additions & 0 deletions libraries/FreematicsPlus/FreematicsNetwork.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <Arduino.h>
#include <WiFi.h>
#include <WiFiUdp.h>
#include <WiFiClientSecure.h>

#include "esp_system.h"
#include "esp_log.h"
Expand Down Expand Up @@ -96,6 +97,8 @@ class WifiHTTP : public HTTPClient, public ClientWIFI
char* receive(char* buffer, int bufsize, int* pbytes = 0, unsigned int timeout = HTTP_CONN_TIMEOUT);
private:
WiFiClient client;
WiFiClientSecure secureClient;
bool m_useSSL = false;
};

typedef enum {
Expand Down