Skip to content

Commit 174a38b

Browse files
committed
Merge branch 'task/ota_improvements' into 'master'
ota: Minor improvements See merge request app-frameworks/esp-rainmaker!295
2 parents 4f1ce2d + 1cd07a0 commit 174a38b

File tree

3 files changed

+51
-17
lines changed

3 files changed

+51
-17
lines changed

components/esp_rainmaker/include/esp_rmaker_ota.h

+25
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ typedef enum {
3434
OTA_STATUS_FAILED,
3535
/** OTA was delayed by the application */
3636
OTA_STATUS_DELAYED,
37+
/** OTA rejected due to some reason (wrong project, version, etc.) */
38+
OTA_STATUS_REJECTED,
3739
} ota_status_t;
3840

3941
/** OTA Workflow type */
@@ -145,6 +147,29 @@ esp_err_t esp_rmaker_ota_enable(esp_rmaker_ota_config_t *ota_config, esp_rmaker_
145147
*/
146148
esp_err_t esp_rmaker_ota_report_status(esp_rmaker_ota_handle_t ota_handle, ota_status_t status, char *additional_info);
147149

150+
/** Default OTA callback
151+
*
152+
* This is the default OTA callback which will get used if you do not pass your own callback. You can call this
153+
* even from your callback, in case you want better control on when the OTA can proceed and yet let the actual
154+
* OTA process be managed by the RainMaker Core.
155+
*
156+
* @param[in] handle An OTA handle assigned by the ESP RainMaker Core
157+
* @param[in] ota_data The data to be used for the OTA
158+
*
159+
* @return ESP_OK if the OTA was successful
160+
* @return ESP_FAIL if the OTA failed.
161+
* */
162+
esp_err_t esp_rmaker_ota_default_cb(esp_rmaker_ota_handle_t handle, esp_rmaker_ota_data_t *ota_data);
163+
164+
/** Fetch OTA Info
165+
*
166+
* For OTA using Topics, this API can be used to explicitly ask the backend if an OTA is available.
167+
* If it is, then the OTA callback would get invoked.
168+
*
169+
* @return ESP_OK if the OTA fetch publish message was successful.
170+
* @return error on failure
171+
*/
172+
esp_err_t esp_rmaker_ota_fetch(void);
148173
#ifdef __cplusplus
149174
}
150175
#endif

components/esp_rainmaker/src/ota/esp_rmaker_ota.c

+16-11
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ char *esp_rmaker_ota_status_to_string(ota_status_t status)
4747
return "failed";
4848
case OTA_STATUS_DELAYED:
4949
return "delayed";
50+
case OTA_STATUS_REJECTED:
51+
return "rejected";
5052
default:
5153
return "invalid";
5254
}
@@ -108,27 +110,28 @@ static esp_err_t validate_image_header(esp_rmaker_ota_handle_t ota_handle,
108110
ESP_LOGD(TAG, "Running firmware version: %s", running_app_info.version);
109111
}
110112

111-
#ifndef CONFIG_ESP_RMAKER_SKIP_VERSION_CHECK
112-
if (memcmp(new_app_info->version, running_app_info.version, sizeof(new_app_info->version)) == 0) {
113-
ESP_LOGW(TAG, "Current running version is same as the new. We will not continue the update.");
114-
esp_rmaker_ota_report_status(ota_handle, OTA_STATUS_FAILED, "Same version received");
115-
return ESP_FAIL;
116-
}
117-
#endif
118-
119113
#ifndef CONFIG_ESP_RMAKER_SKIP_PROJECT_NAME_CHECK
120114
if (memcmp(new_app_info->project_name, running_app_info.project_name, sizeof(new_app_info->project_name)) != 0) {
121115
ESP_LOGW(TAG, "OTA Image built for Project: %s. Expected: %s",
122116
new_app_info->project_name, running_app_info.project_name);
123-
esp_rmaker_ota_report_status(ota_handle, OTA_STATUS_FAILED, "Project Name mismatch");
117+
esp_rmaker_ota_report_status(ota_handle, OTA_STATUS_REJECTED, "Project Name mismatch");
124118
return ESP_FAIL;
125119
}
126120
#endif
127121

122+
#ifndef CONFIG_ESP_RMAKER_SKIP_VERSION_CHECK
123+
if (memcmp(new_app_info->version, running_app_info.version, sizeof(new_app_info->version)) == 0) {
124+
ESP_LOGW(TAG, "Current running version is same as the new. We will not continue the update.");
125+
esp_rmaker_ota_report_status(ota_handle, OTA_STATUS_REJECTED, "Same version received");
126+
return ESP_FAIL;
127+
}
128+
#endif
129+
130+
128131
return ESP_OK;
129132
}
130133

131-
static esp_err_t esp_rmaker_ota_default_cb(esp_rmaker_ota_handle_t ota_handle, esp_rmaker_ota_data_t *ota_data)
134+
esp_err_t esp_rmaker_ota_default_cb(esp_rmaker_ota_handle_t ota_handle, esp_rmaker_ota_data_t *ota_data)
132135
{
133136
if (!ota_data->url) {
134137
return ESP_FAIL;
@@ -146,7 +149,8 @@ static esp_err_t esp_rmaker_ota_default_cb(esp_rmaker_ota_handle_t ota_handle, e
146149
.cert_pem = ota_data->server_cert,
147150
.timeout_ms = 5000,
148151
.buffer_size = DEF_HTTP_RX_BUFFER_SIZE,
149-
.buffer_size_tx = buffer_size_tx
152+
.buffer_size_tx = buffer_size_tx,
153+
.keep_alive_enable = true
150154
};
151155
#ifdef CONFIG_ESP_RMAKER_SKIP_COMMON_NAME_CHECK
152156
config.skip_cert_common_name_check = true;
@@ -220,6 +224,7 @@ static esp_err_t esp_rmaker_ota_default_cb(esp_rmaker_ota_handle_t ota_handle, e
220224
}
221225
}
222226
if (err != ESP_OK) {
227+
ESP_LOGE(TAG, "ESP_HTTPS_OTA upgrade failed %s", esp_err_to_name(err));
223228
char description[40];
224229
snprintf(description, sizeof(description), "OTA failed: Error %s", esp_err_to_name(err));
225230
esp_rmaker_ota_report_status(ota_handle, OTA_STATUS_FAILED, description);

components/esp_rainmaker/src/ota/esp_rmaker_ota_using_topics.c

+10-6
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,12 @@ static void ota_url_handler(const char *topic, void *payload, size_t payload_len
169169
return;
170170
}
171171

172-
#ifdef CONFIG_ESP_RMAKER_OTA_AUTOFETCH
173-
static void esp_rmaker_ota_fetch(void *priv)
172+
esp_err_t esp_rmaker_ota_fetch(void)
174173
{
175174
esp_rmaker_node_info_t *info = esp_rmaker_node_get_info(esp_rmaker_get_node());
176175
if (!info) {
177176
ESP_LOGE(TAG, "Node info not found. Cant send otafetch request");
178-
return;
177+
return ESP_FAIL;
179178
}
180179
char publish_payload[150];
181180
json_gen_str_t jstr;
@@ -192,8 +191,13 @@ static void esp_rmaker_ota_fetch(void *priv)
192191
if (err != ESP_OK) {
193192
ESP_LOGE(TAG, "OTA Fetch Publish Error %d", err);
194193
}
194+
return err;
195+
}
196+
197+
void esp_rmaker_ota_timer_cb_fetch(void *priv)
198+
{
199+
esp_rmaker_ota_fetch();
195200
}
196-
#endif /* CONFIG_ESP_RMAKER_OTA_AUTOFETCH */
197201

198202
static esp_err_t esp_rmaker_ota_subscribe(void *priv_data)
199203
{
@@ -215,10 +219,10 @@ static void esp_rmaker_ota_work_fn(void *priv_data)
215219
{
216220
esp_rmaker_ota_subscribe(priv_data);
217221
#ifdef CONFIG_ESP_RMAKER_OTA_AUTOFETCH
218-
esp_rmaker_ota_fetch(priv_data);
222+
esp_rmaker_ota_fetch();
219223
if (ota_autofetch_period > 0) {
220224
esp_timer_create_args_t autofetch_timer_conf = {
221-
.callback = esp_rmaker_ota_fetch,
225+
.callback = esp_rmaker_ota_timer_cb_fetch,
222226
.arg = priv_data,
223227
.dispatch_method = ESP_TIMER_TASK,
224228
.name = "ota_autofetch_tm"

0 commit comments

Comments
 (0)