2021-04-02 02:49:49 -04:00
|
|
|
// Copyright 2021 Espressif Systems (Shanghai) CO LTD
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License
|
|
|
|
|
|
|
|
#include "esp_check.h"
|
2021-06-08 03:49:05 -04:00
|
|
|
#include "esp_openthread.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "esp_openthread_common_macro.h"
|
|
|
|
#include "esp_openthread_lock.h"
|
2021-06-08 03:49:05 -04:00
|
|
|
#include "esp_openthread_netif_glue_priv.h"
|
2021-06-08 06:12:53 -04:00
|
|
|
#include "esp_openthread_platform.h"
|
2021-04-02 02:49:49 -04:00
|
|
|
#include "esp_openthread_types.h"
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "openthread/instance.h"
|
|
|
|
#include "openthread/tasklet.h"
|
|
|
|
|
2021-06-08 03:49:05 -04:00
|
|
|
static void esp_openthread_state_callback(otChangedFlags changed_flags, void *ctx)
|
2021-04-02 02:49:49 -04:00
|
|
|
{
|
2021-06-08 03:49:05 -04:00
|
|
|
esp_openthread_netif_glue_state_callback(changed_flags);
|
2021-04-02 02:49:49 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 03:49:05 -04:00
|
|
|
static esp_err_t register_esp_openthread_state_callbacks(void)
|
2021-04-02 02:49:49 -04:00
|
|
|
{
|
2021-06-08 03:49:05 -04:00
|
|
|
otInstance *instance = esp_openthread_get_instance();
|
|
|
|
ESP_RETURN_ON_FALSE(otSetStateChangedCallback(instance, esp_openthread_state_callback, NULL) == OT_ERROR_NONE,
|
|
|
|
ESP_FAIL, OT_PLAT_LOG_TAG, "Failed to install OpenThread state callback");
|
|
|
|
return ESP_OK;
|
2021-04-02 02:49:49 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 03:49:05 -04:00
|
|
|
esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *config)
|
2021-04-02 02:49:49 -04:00
|
|
|
{
|
2021-06-08 03:49:05 -04:00
|
|
|
ESP_RETURN_ON_ERROR(esp_openthread_platform_init(config), OT_PLAT_LOG_TAG,
|
|
|
|
"Failed to initialize OpenThread platform driver");
|
|
|
|
ESP_RETURN_ON_FALSE(otInstanceInitSingle() != NULL, ESP_FAIL, OT_PLAT_LOG_TAG,
|
|
|
|
"Failed to initialize OpenThread instance");
|
|
|
|
|
|
|
|
return register_esp_openthread_state_callbacks();
|
2021-04-02 02:49:49 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 03:49:05 -04:00
|
|
|
esp_err_t esp_openthread_launch_mainloop(void)
|
2021-04-02 02:49:49 -04:00
|
|
|
{
|
2021-06-08 03:49:05 -04:00
|
|
|
esp_openthread_mainloop_context_t mainloop;
|
|
|
|
otInstance *instance = esp_openthread_get_instance();
|
|
|
|
esp_err_t error = ESP_OK;
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
FD_ZERO(&mainloop.read_fds);
|
|
|
|
FD_ZERO(&mainloop.write_fds);
|
|
|
|
FD_ZERO(&mainloop.error_fds);
|
|
|
|
|
|
|
|
mainloop.max_fd = -1;
|
|
|
|
mainloop.timeout.tv_sec = 10;
|
|
|
|
mainloop.timeout.tv_usec = 0;
|
|
|
|
|
|
|
|
esp_openthread_lock_acquire(portMAX_DELAY);
|
|
|
|
esp_openthread_platform_update(&mainloop);
|
|
|
|
if (otTaskletsArePending(instance)) {
|
|
|
|
mainloop.timeout.tv_sec = 0;
|
|
|
|
mainloop.timeout.tv_usec = 0;
|
|
|
|
}
|
|
|
|
esp_openthread_lock_release();
|
|
|
|
|
|
|
|
if (select(mainloop.max_fd + 1, &mainloop.read_fds, &mainloop.write_fds, &mainloop.error_fds,
|
|
|
|
&mainloop.timeout) >= 0) {
|
|
|
|
esp_openthread_lock_acquire(portMAX_DELAY);
|
|
|
|
otTaskletsProcess(instance);
|
|
|
|
error = esp_openthread_platform_process(instance, &mainloop);
|
|
|
|
esp_openthread_lock_release();
|
|
|
|
if (error != ESP_OK) {
|
|
|
|
ESP_LOGE(OT_PLAT_LOG_TAG, "esp_openthread_platform_process failed");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
error = ESP_FAIL;
|
|
|
|
ESP_LOGE(OT_PLAT_LOG_TAG, "OpenThread system polling failed");
|
|
|
|
break;
|
|
|
|
}
|
2021-04-02 02:49:49 -04:00
|
|
|
}
|
2021-06-08 03:49:05 -04:00
|
|
|
return error;
|
2021-04-02 02:49:49 -04:00
|
|
|
}
|
|
|
|
|
2021-06-08 03:49:05 -04:00
|
|
|
esp_err_t esp_openthread_deinit(void)
|
2021-04-02 02:49:49 -04:00
|
|
|
{
|
2021-06-08 03:49:05 -04:00
|
|
|
otInstanceFinalize(esp_openthread_get_instance());
|
|
|
|
return esp_openthread_platform_deinit();
|
2021-04-02 02:49:49 -04:00
|
|
|
}
|