Skip to content

Commit

Permalink
Compiles but might not run.
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul D.Smith committed Dec 27, 2023
1 parent d2215fc commit b7301db
Show file tree
Hide file tree
Showing 9 changed files with 388 additions and 81 deletions.
10 changes: 2 additions & 8 deletions esp32/cloudsmets/main/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
# As well as the source code, register the HTML and CSS files that we use to
# provide the web server.
idf_component_register(
SRCS "cs_app.c"
SRCS "cs_wifi.c"
SRCS "cs_web.c"
SRCS "cs_cfg.c"
SRCS "cs_ota.c"
SRCS "cs_mqtt.c"
SRCS "cs_time.c"
INCLUDE_DIRS ""
SRCS "cs_app.c" "cs_cfg.c" "cs_mqtt.c" "cs_ota.c" "cs_time.c" "cs_web.c" "cs_wifi.c" "cs_zigbee.c"
INCLUDE_DIRS "."
EMBED_TXTFILES index.html
EMBED_TXTFILES menu.html
EMBED_TXTFILES wifi.html
Expand Down
48 changes: 14 additions & 34 deletions esp32/cloudsmets/main/cs_mqtt.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,23 +23,13 @@

#include "cs_cfg.h"
#include "cs_time.h"
#include "cs_string.h"
#include "cs_mqtt.h"

/* Extension definitions to make coding simpler. */
#include "mbedtls_err.h"
#include "esp_mqtt_err.h"

/* Some helpful macros. */
#define MAYBE_FREE_STRING(X) if ((X.value) != NULL) { free((X).value); (X).value = NULL; (X).length = 0; }

#define MALLOC_ERROR_CHECK(DST, LEN) \
(DST) = malloc(LEN); \
if ((DST) == NULL) \
{ \
ESP_LOGE(TAG, "malloc failed for length: %d", (LEN)); \
ESP_ERROR_CHECK(ESP_FAIL); \
}

/**
* If we think we can reconnect immediately using a different access key,
* we try. Otherwise we wait for a minute.
Expand All @@ -57,16 +47,6 @@
#define SAS_TOKEN_AND_SE "&se="
#define SAS_TOKEN_AND_SE_LEN (sizeof(SAS_TOKEN_AND_SE) - 1)

/**
* A lot of the SAS token operations require a string-with-length so this simple
* structure tracks this. The length is the length WITHOUT NULL but we always
* NULL terminate so we can trace values.
*/
typedef struct
{
unsigned char *value;
size_t length;
} cs_string;

#define TAG cs_mqtt_task_name

Expand Down Expand Up @@ -138,7 +118,7 @@ static void quote_plus(cs_string *in_str, cs_string *out_str)
out_str->length = out_len;
if (out_str->length > 0)
{
MALLOC_ERROR_CHECK(out_str->value, (out_len + 1));
CS_STRING_MALLOC(out_str->value, (out_len + 1));
}
ESP_LOGV(TAG, "out_str len: %u, %p", out_str->length, out_str->value);

Expand Down Expand Up @@ -250,14 +230,14 @@ static void read_config()
unsigned char *sptr;

/* Free existing values and read new ones. */
MAYBE_FREE_STRING(iothub_url);
MAYBE_FREE_STRING(quoted_device_url);
MAYBE_FREE_STRING(sign_key);
MAYBE_FREE_STRING(device);
MAYBE_FREE_STRING(username);
MAYBE_FREE_STRING(access_keys[0]);
MAYBE_FREE_STRING(access_keys[1]);
MAYBE_FREE_STRING(sas_token);
CS_STRING_FREE(iothub_url);
CS_STRING_FREE(quoted_device_url);
CS_STRING_FREE(sign_key);
CS_STRING_FREE(device);
CS_STRING_FREE(username);
CS_STRING_FREE(access_keys[0]);
CS_STRING_FREE(access_keys[1]);
CS_STRING_FREE(sas_token);

/**
* Note that the length of read strings includes the NULL terminator so
Expand Down Expand Up @@ -304,7 +284,7 @@ static void read_config()
* Username is "<iothub-url-without-protocol>/devicename"
*/
username.length = iothub_url.length - PROTOCOL_MQTTS_LENGTH + 1+ device.length + 1;
MALLOC_ERROR_CHECK(username.value, username.length);
CS_STRING_MALLOC(username.value, username.length);
sptr = username.value;
memcpy(sptr, &iothub_url.value[PROTOCOL_MQTTS_LENGTH], (iothub_url.length - PROTOCOL_MQTTS_LENGTH));
sptr += iothub_url.length - PROTOCOL_MQTTS_LENGTH;
Expand All @@ -320,7 +300,7 @@ static void read_config()
#define DEVICES "/Devices/"
#define DEVICES_LEN (sizeof(DEVICES) - 1)
temp_url.length = iothub_url.length - PROTOCOL_MQTTS_LENGTH + DEVICES_LEN + device.length + 1;
MALLOC_ERROR_CHECK(temp_url.value, temp_url.length);
CS_STRING_MALLOC(temp_url.value, temp_url.length);
sptr = temp_url.value;
memcpy(sptr, &iothub_url.value[PROTOCOL_MQTTS_LENGTH], (iothub_url.length - PROTOCOL_MQTTS_LENGTH));
sptr += iothub_url.length - PROTOCOL_MQTTS_LENGTH;
Expand Down Expand Up @@ -351,7 +331,7 @@ static void read_config()
*/
sas_token.length = SAS_TOKEN_START_LEN + quoted_device_url.length + SAS_TOKEN_AND_SIG_LEN + URLENCODED_BASE64_SHA256_LEN + 10 + 1;
ESP_LOGV(TAG, "ST: %u", sas_token.length);
MALLOC_ERROR_CHECK(sas_token.value, sas_token.length);
CS_STRING_MALLOC(sas_token.value, sas_token.length);

/* Now we preload as much as we can into the token. */
sptr = sas_token.value;
Expand All @@ -372,7 +352,7 @@ static void read_config()
* plus a stringified time.
*/
sign_key.length = quoted_device_url.length + 1 + 11;
MALLOC_ERROR_CHECK(sign_key.value, sign_key.length);
CS_STRING_MALLOC(sign_key.value, sign_key.length);
sptr = sign_key.value;
memcpy(sptr, quoted_device_url.value, quoted_device_url.length);
sptr += quoted_device_url.length;
Expand Down
26 changes: 26 additions & 0 deletions esp32/cloudsmets/main/cs_string.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* It is helpful to have a string-with-length structure. Note that the length
* is the length WITHOUT NULL but we always NULL terminate so we can trace
* values.
*/
typedef struct
{
uint8_t *value;
size_t length;
} cs_string;

#define CS_STRING_FREE(X) \
if ((X.value) != NULL) \
{ \
free((X).value); \
(X).value = NULL; \
(X).length = 0; \
}

#define CS_STRING_MALLOC(DST, LEN) \
(DST) = malloc(LEN); \
if ((DST) == NULL) \
{ \
ESP_LOGE(TAG, "malloc failed for length: %d", (LEN)); \
ESP_ERROR_CHECK(ESP_FAIL); \
}
Loading

0 comments on commit b7301db

Please sign in to comment.