-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'feature/public_ot_port' into 'master'
openthread: open source openthread port layer See merge request espressif/esp-idf!21803
- Loading branch information
Showing
37 changed files
with
3,733 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule lib
updated
9 files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "esp_openthread.h" | ||
#include "esp_check.h" | ||
#include "esp_openthread_border_router.h" | ||
#include "esp_openthread_common_macro.h" | ||
#include "esp_openthread_lock.h" | ||
#include "esp_openthread_platform.h" | ||
#include "esp_openthread_task_queue.h" | ||
#include "esp_openthread_types.h" | ||
#include "freertos/FreeRTOS.h" | ||
#include "lwip/dns.h" | ||
#include "openthread/instance.h" | ||
#include "openthread/netdata.h" | ||
#include "openthread/tasklet.h" | ||
|
||
esp_err_t esp_openthread_init(const esp_openthread_platform_config_t *config) | ||
{ | ||
ESP_RETURN_ON_ERROR(esp_openthread_platform_init(config), OT_PLAT_LOG_TAG, | ||
"Failed to initialize OpenThread platform driver"); | ||
esp_openthread_lock_acquire(portMAX_DELAY); | ||
ESP_RETURN_ON_FALSE(otInstanceInitSingle() != NULL, ESP_FAIL, OT_PLAT_LOG_TAG, | ||
"Failed to initialize OpenThread instance"); | ||
esp_openthread_lock_release(); | ||
|
||
return ESP_OK; | ||
} | ||
|
||
esp_err_t esp_openthread_launch_mainloop(void) | ||
{ | ||
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); | ||
error = esp_openthread_platform_process(instance, &mainloop); | ||
while (otTaskletsArePending(instance)) { | ||
otTaskletsProcess(instance); | ||
} | ||
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; | ||
} | ||
} | ||
return error; | ||
} | ||
|
||
esp_err_t esp_openthread_deinit(void) | ||
{ | ||
otInstanceFinalize(esp_openthread_get_instance()); | ||
return esp_openthread_platform_deinit(); | ||
} | ||
|
||
static void stub_task(void *context) | ||
{ | ||
// this is a empty function used for ot-task signal pending | ||
} | ||
|
||
void otTaskletsSignalPending(otInstance *aInstance) | ||
{ | ||
esp_openthread_task_queue_post(stub_task, NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021-2022 Espressif Systems (Shanghai) CO LTD | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
#include "esp_openthread_alarm.h" | ||
|
||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <sys/time.h> | ||
|
||
#include "esp_log.h" | ||
#include "esp_openthread_common_macro.h" | ||
#include "esp_openthread_platform.h" | ||
#include "esp_timer.h" | ||
#include "openthread-core-config.h" | ||
#include "freertos/FreeRTOS.h" | ||
#include "freertos/task.h" | ||
#include "openthread/platform/alarm-micro.h" | ||
#include "openthread/platform/alarm-milli.h" | ||
#include "openthread/platform/diag.h" | ||
#include "openthread/platform/radio.h" | ||
#include "openthread/platform/time.h" | ||
|
||
static uint64_t s_alarm_ms_t0 = 0; | ||
static uint64_t s_alarm_ms_dt = 0; | ||
static bool s_is_ms_running = false; | ||
static uint64_t s_alarm_us_t0 = 0; | ||
static uint64_t s_alarm_us_dt = 0; | ||
static bool s_is_us_running = false; | ||
static const char *alarm_workflow = "alarm"; | ||
|
||
uint64_t otPlatTimeGet(void) | ||
{ | ||
struct timeval tv_now; | ||
|
||
int err = gettimeofday(&tv_now, NULL); | ||
assert(err == 0); | ||
|
||
return (uint64_t)tv_now.tv_sec * US_PER_S + tv_now.tv_usec; | ||
} | ||
|
||
void otPlatAlarmMilliStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) | ||
{ | ||
OT_UNUSED_VARIABLE(aInstance); | ||
|
||
s_alarm_ms_t0 = aT0; | ||
s_alarm_ms_dt = aDt; | ||
s_is_ms_running = true; | ||
|
||
ESP_LOGD(OT_PLAT_LOG_TAG, "Millisecond timer alarm start running, t0=%llu, dt=%llu", s_alarm_ms_t0, s_alarm_ms_dt); | ||
} | ||
|
||
void otPlatAlarmMilliStop(otInstance *aInstance) | ||
{ | ||
OT_UNUSED_VARIABLE(aInstance); | ||
|
||
s_is_ms_running = false; | ||
} | ||
|
||
uint32_t otPlatAlarmMilliGetNow(void) | ||
{ | ||
return esp_timer_get_time() / US_PER_MS; | ||
} | ||
|
||
void otPlatAlarmMicroStartAt(otInstance *aInstance, uint32_t aT0, uint32_t aDt) | ||
{ | ||
OT_UNUSED_VARIABLE(aInstance); | ||
|
||
s_alarm_us_t0 = aT0; | ||
s_alarm_us_dt = aDt; | ||
s_is_us_running = true; | ||
|
||
ESP_LOGD(OT_PLAT_LOG_TAG, "Microsecond timer alarm start running, t0=%llu, dt=%llu", s_alarm_us_t0, s_alarm_us_dt); | ||
} | ||
|
||
void otPlatAlarmMicroStop(otInstance *aInstance) | ||
{ | ||
OT_UNUSED_VARIABLE(aInstance); | ||
s_is_us_running = false; | ||
} | ||
|
||
uint32_t otPlatAlarmMicroGetNow(void) | ||
{ | ||
return esp_timer_get_time(); | ||
} | ||
|
||
esp_err_t esp_openthread_alarm_init(void) | ||
{ | ||
return esp_openthread_platform_workflow_register(&esp_openthread_alarm_update, &esp_openthread_alarm_process, | ||
alarm_workflow); | ||
} | ||
|
||
void esp_openthread_alarm_deinit(void) | ||
{ | ||
esp_openthread_platform_workflow_unregister(alarm_workflow); | ||
} | ||
|
||
void esp_openthread_alarm_update(esp_openthread_mainloop_context_t *mainloop) | ||
{ | ||
struct timeval *timeout = &mainloop->timeout; | ||
uint64_t now = esp_timer_get_time(); | ||
int64_t remain_min_time_us = INT64_MAX; | ||
int64_t remaining_us = 0; | ||
if (s_is_ms_running) { | ||
remaining_us = (s_alarm_ms_dt + s_alarm_ms_t0) * US_PER_MS - now; | ||
if (remain_min_time_us > remaining_us) { | ||
remain_min_time_us = remaining_us; | ||
} | ||
} | ||
if (s_is_us_running) { | ||
remaining_us = s_alarm_us_dt + s_alarm_us_t0 - now; | ||
if (remain_min_time_us > remaining_us) { | ||
remain_min_time_us = remaining_us; | ||
} | ||
} | ||
if (remain_min_time_us > 0) { | ||
timeout->tv_sec = remain_min_time_us / US_PER_S; | ||
timeout->tv_usec = remain_min_time_us % US_PER_S; | ||
} else { | ||
timeout->tv_sec = 0; | ||
timeout->tv_usec = 0; | ||
} | ||
} | ||
|
||
esp_err_t esp_openthread_alarm_process(otInstance *aInstance, const esp_openthread_mainloop_context_t *mainloop) | ||
{ | ||
if (s_is_ms_running && s_alarm_ms_t0 + s_alarm_ms_dt <= otPlatAlarmMilliGetNow()) { | ||
s_is_ms_running = false; | ||
|
||
#if OPENTHREAD_CONFIG_DIAG_ENABLE | ||
if (otPlatDiagModeGet()) { | ||
otPlatDiagAlarmFired(aInstance); | ||
} else | ||
#endif | ||
{ | ||
otPlatAlarmMilliFired(aInstance); | ||
} | ||
|
||
ESP_LOGD(OT_PLAT_LOG_TAG, "Millisecond timer alarm fired"); | ||
} | ||
if (s_is_us_running && s_alarm_us_t0 + s_alarm_us_dt <= esp_timer_get_time()) { | ||
s_is_us_running = false; | ||
otPlatAlarmMicroFired(aInstance); | ||
ESP_LOGD(OT_PLAT_LOG_TAG, "Microsecond timer alarm fired"); | ||
} | ||
return ESP_OK; | ||
} |
Oops, something went wrong.