Skip to content

Commit

Permalink
Ota state lifetime handling fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
abobija committed Apr 1, 2021
1 parent 2135e53 commit 8b059d1
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 32 deletions.
4 changes: 2 additions & 2 deletions include/discord_ota.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ typedef struct discord_ota* discord_ota_handle_t;

typedef struct {
char* prefix; /*<! Prefix with which message needs to start in order to perform OTA update */
bool multiple_ota; /*<! Set this to true if there is need to perform OTA update on multiple devices at once */
bool success_feedback_disabled; /*<! Disable sending feedback on failure */
bool error_feedback_disabled; /*<! Disable sending feedback on success */
bool administrator_only_disabled; /*<! Disable option that only Administrators can perform OTA update */
discord_channel_t* channel; /*<! Channel in which OTA update can be performed. Id or Name can be provided. Id has higher priority over the channel Name (if both are provided). Set to NULL to allow all channels. Note: Maybe you will need to increase the size of Api buffer for using this option with providing channel Name instead of Id */
bool multiple_ota; /*<! Set this to true if there is need to perform OTA update on multiple devices at once */
} discord_ota_config_t;

/**
* @brief Initialize discord OTA ability
* @param client Discord bot handle
* @param config OTA config
* @param config OTA config. Provide NULL for default configuration
* @return ESP_OK on success
*/
esp_err_t discord_ota_init(discord_handle_t client, discord_ota_config_t* config);
Expand Down
81 changes: 51 additions & 30 deletions src/discord_ota.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,22 @@ typedef struct {
static discord_ota_token_t discord_ota_get_token();
static esp_err_t discord_ota_connected_handler(discord_handle_t client);
static esp_err_t discord_ota_disconnected_handler(discord_handle_t client);
static esp_err_t discord_ota(discord_handle_t client, discord_message_t* firmware_message);
static esp_err_t discord_ota_perform(discord_handle_t client, discord_message_t* firmware_message);

static void ota_state_reset(discord_handle_t client) {
discord_ota_handle_t ota = client->ota;

// reset everything except config

if(ota->buffer) { free(ota->buffer); }
ota->buffer_offset = 0;
ota->update_handle = 0;
ota->update_partition = NULL;
ota->image_was_checked = false;
ota->error = DISCORD_OTA_OK;
ota->download_percentage = 0;
ota->download_percentage_checkpoint = 0;
}

static void ota_on_connected(void* handler_arg, esp_event_base_t base, int32_t event_id, void* event_data) {
discord_ota_connected_handler((discord_handle_t) handler_arg);
Expand All @@ -86,7 +101,7 @@ static void ota_on_connected(void* handler_arg, esp_event_base_t base, int32_t e
static void ota_on_message(void* handler_arg, esp_event_base_t base, int32_t event_id, void* event_data) {
discord_event_data_t* data = (discord_event_data_t*) event_data;
discord_message_t* msg = (discord_message_t*) data->ptr;
discord_ota((discord_handle_t) handler_arg, msg);
discord_ota_perform((discord_handle_t) handler_arg, msg);
}

static void ota_on_disconnected(void* handler_arg, esp_event_base_t base, int32_t event_id, void* event_data) {
Expand All @@ -99,7 +114,7 @@ esp_err_t discord_ota_init(discord_handle_t client, discord_ota_config_t* config
}

if(client->running || client->state >= DISCORD_STATE_OPEN) {
DISCORD_LOGE("Fail to init. Initialization of OTA should be done before login");
DISCORD_LOGE("Fail to init. Initialization of OTA should be done before Discord login");
return ESP_ERR_INVALID_STATE;
}

Expand All @@ -112,11 +127,16 @@ esp_err_t discord_ota_init(discord_handle_t client, discord_ota_config_t* config

if(config) {
ota->config->prefix = STRDUP(config->prefix);
ota->config->multiple_ota = config->multiple_ota;
ota->config->success_feedback_disabled = config->success_feedback_disabled;
ota->config->error_feedback_disabled = config->error_feedback_disabled;
ota->config->administrator_only_disabled = config->administrator_only_disabled;
ota->config->channel = config->channel;
ota->config->multiple_ota = config->multiple_ota;
if(config->channel) {
ota->config->channel = cu_ctor(discord_channel_t,
.id = STRDUP(config->channel->id),
.name = STRDUP(config->channel->name)
);
}
}

if((err = discord_register_events(client, DISCORD_EVENT_MESSAGE_RECEIVED, ota_on_message, client)) != ESP_OK) {
Expand Down Expand Up @@ -323,7 +343,7 @@ static esp_err_t ota_status_message_content(discord_ota_handle_t ota_hndl, char*
* @param config Configuration for OTA update. Provide NULL for default configurations
* @return ESP_OK on success
*/
static esp_err_t discord_ota(discord_handle_t client, discord_message_t* firmware_message) {
static esp_err_t discord_ota_perform(discord_handle_t client, discord_message_t* firmware_message) {
if(!client || !client->ota || !firmware_message) {
return ESP_ERR_INVALID_ARG;
}
Expand All @@ -335,8 +355,6 @@ static esp_err_t discord_ota(discord_handle_t client, discord_message_t* firmwar
size_t cmd_pieces_len = 0;
char* subcmd = NULL;

ota->error = DISCORD_OTA_OK; // reset error from the last time

if(firmware_message->author->bot) { // ignore messages from other bots
goto _return;
}
Expand All @@ -356,6 +374,8 @@ static esp_err_t discord_ota(discord_handle_t client, discord_message_t* firmwar
goto _return; // ignore message
}

DISCORD_LOGI("Triggered");

cmd_pieces = estr_split(firmware_message->content, ' ', &cmd_pieces_len);

if(cmd_pieces_len != 3) {
Expand Down Expand Up @@ -505,20 +525,11 @@ static esp_err_t discord_ota(discord_handle_t client, discord_message_t* firmwar
goto _error;
}

// release buffer if it stays somehow allocated from the last time
// this should not happen but for any case...
if(ota->buffer) {
free(ota->buffer);
ota->buffer = NULL;
}

// allocate new buffer
if(!(ota->buffer = malloc(DISCORD_OTA_BUFFER_SIZE))) {
err = ESP_ERR_NO_MEM;
goto _error;
}

ota->buffer_offset = 0;

DISCORD_LOGI("Gathering new firmware informations...");

Expand Down Expand Up @@ -594,6 +605,8 @@ static esp_err_t discord_ota(discord_handle_t client, discord_message_t* firmwar
_return:
cu_list_free(cmd_pieces, cmd_pieces_len); // no nullcheck needed
free(subcmd);
ota_state_reset(client);
DISCORD_LOGI("Finished");
return err;
}

Expand Down Expand Up @@ -623,6 +636,17 @@ esp_err_t discord_ota_keep(bool keep_or_rollback) {
return err;
}

void discord_ota_config_free(discord_ota_handle_t ota) {
if(!ota || !ota->config) {
return;
}

free(ota->config->prefix);
discord_channel_free(ota->config->channel);
free(ota->config);
ota->config = NULL;
}

void discord_ota_destroy(discord_handle_t client) {
if(!client || !client->ota) {
return;
Expand All @@ -632,8 +656,7 @@ void discord_ota_destroy(discord_handle_t client) {
if(ota->update_handle) {
esp_ota_abort(ota->update_handle);
}
free(ota->config->prefix);
free(ota->config);
discord_ota_config_free(ota);
free(ota->buffer);
discord_unregister_events(client, DISCORD_EVENT_CONNECTED, ota_on_connected);
discord_unregister_events(client, DISCORD_EVENT_MESSAGE_RECEIVED, ota_on_message);
Expand All @@ -659,23 +682,21 @@ static discord_ota_token_t discord_ota_get_token() {
char* nvs_token = NULL;
size_t nvs_token_len = 0;

if((token.err = nvs_open(DISCORD_NVS_NAMESPACE, NVS_READONLY, &nvs)) != ESP_OK) {
if(token.err == ESP_ERR_NVS_NOT_FOUND) {
token.err = ESP_OK;
if((token.err = nvs_open(DISCORD_NVS_NAMESPACE, NVS_READONLY, &nvs)) == ESP_OK) {
nvs_get_str(nvs, DISCORD_NVS_KEY_TOKEN, NULL, &nvs_token_len);

if(nvs_token_len > 0) {
nvs_token = malloc(nvs_token_len);
nvs_get_str(nvs, DISCORD_NVS_KEY_TOKEN, nvs_token, &nvs_token_len);
}

return token;
nvs_close(nvs);
}

nvs_get_str(nvs, DISCORD_NVS_KEY_TOKEN, NULL, &nvs_token_len);

if(nvs_token_len > 0) {
nvs_token = malloc(nvs_token_len);
nvs_get_str(nvs, DISCORD_NVS_KEY_TOKEN, nvs_token, &nvs_token_len);
if(token.err == ESP_ERR_NVS_NOT_FOUND) {
token.err = ESP_OK;
}

nvs_close(nvs);

if(nvs_token) {
token.type = DISCORD_OTA_TOKEN_FROM_NVS;
token.val = nvs_token;
Expand Down

0 comments on commit 8b059d1

Please sign in to comment.