Skip to content

Commit 14ed4f0

Browse files
committed
client: add network connect/disconnect callbacks
1 parent a6e6d45 commit 14ed4f0

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

main/main.c

Lines changed: 49 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,49 @@ static const char *TAG = "main";
5454
static EventGroupHandle_t mender_client_events;
5555
#define MENDER_CLIENT_EVENT_RESTART (1 << 0)
5656

57+
/**
58+
* @brief Network connnect callback
59+
* @return MENDER_OK if network is connected following the request, error code otherwise
60+
*/
61+
static mender_err_t
62+
network_connect_cb(void) {
63+
64+
ESP_LOGI(TAG, "Mender client connect network");
65+
66+
/* This callback can be used to configure network connection */
67+
/* Note that the application can connect the network before if required */
68+
/* This callback only indicates the mender-client requests network access now */
69+
/* In this example this helper function configures Wi-Fi or Ethernet, as selected in menuconfig */
70+
/* Read "Establishing Wi-Fi or Ethernet Connection" section in examples/protocols/README.md for more information */
71+
if (ESP_OK != example_connect()) {
72+
ESP_LOGE(TAG, "Unable to connect network");
73+
return MENDER_FAIL;
74+
}
75+
76+
return MENDER_OK;
77+
}
78+
79+
/**
80+
* @brief Network release callback
81+
* @return MENDER_OK if network is released following the request, error code otherwise
82+
*/
83+
static mender_err_t
84+
network_release_cb(void) {
85+
86+
ESP_LOGI(TAG, "Mender client released network");
87+
88+
/* This callback can be used to release network connection */
89+
/* Note that the application can keep network activated if required */
90+
/* This callback only indicates the mender-client doesn't request network access now */
91+
/* in this example this helper function disconnects the network */
92+
if (ESP_OK != example_disconnect()) {
93+
ESP_LOGE(TAG, "Unable to disconnect network");
94+
return MENDER_FAIL;
95+
}
96+
97+
return MENDER_OK;
98+
}
99+
57100
/**
58101
* @brief Authentication success callback
59102
* @return MENDER_OK if application is marked valid and success deployment status should be reported to the server, error code otherwise
@@ -420,18 +463,11 @@ app_main(void) {
420463
ESP_ERROR_CHECK(esp_netif_init());
421464
ESP_ERROR_CHECK(esp_event_loop_create_default());
422465

423-
/*
424-
* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
425-
* Read "Establishing Wi-Fi or Ethernet Connection" section in
426-
* examples/protocols/README.md for more information about this function.
427-
*/
428-
ESP_ERROR_CHECK(example_connect());
429-
430466
/* Read base MAC address of the device */
431467
uint8_t mac[6];
432468
char mac_address[18];
433-
ESP_ERROR_CHECK(esp_base_mac_addr_get(mac));
434-
snprintf(mac_address, sizeof(mac_address), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
469+
ESP_ERROR_CHECK(esp_read_mac(mac, ESP_MAC_WIFI_STA));
470+
sprintf(mac_address, "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
435471
ESP_LOGI(TAG, "MAC address of the device '%s'", mac_address);
436472

437473
/* Create mender-client event group */
@@ -446,7 +482,7 @@ app_main(void) {
446482

447483
/* Compute artifact name */
448484
char artifact_name[128];
449-
snprintf(artifact_name, sizeof(artifact_name), "%s-v%s", running_app_info.project_name, running_app_info.version);
485+
sprintf(artifact_name, "%s-v%s", running_app_info.project_name, running_app_info.version);
450486

451487
/* Retrieve device type */
452488
char *device_type = running_app_info.project_name;
@@ -461,7 +497,9 @@ app_main(void) {
461497
.authentication_poll_interval = 0,
462498
.update_poll_interval = 0,
463499
.recommissioning = false };
464-
mender_client_callbacks_t mender_client_callbacks = { .authentication_success = authentication_success_cb,
500+
mender_client_callbacks_t mender_client_callbacks = { .network_connect = network_connect_cb,
501+
.network_release = network_release_cb,
502+
.authentication_success = authentication_success_cb,
465503
.authentication_failure = authentication_failure_cb,
466504
.deployment_status = deployment_status_cb,
467505
.restart = restart_cb };

0 commit comments

Comments
 (0)