This commit is contained in:
刘可亮
2025-07-22 11:15:46 +08:00
parent d164b333ed
commit 11c97ef399
2870 changed files with 951307 additions and 26675 deletions

View File

@@ -0,0 +1,39 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <nuttx/config.h>
#include <stdint.h>
#include <pthread.h>
#include "nimble/nimble_npl.h"
static pthread_mutex_t s_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
uint32_t
ble_npl_hw_enter_critical(void)
{
pthread_mutex_lock(&s_mutex);
return 0;
}
void
ble_npl_hw_exit_critical(uint32_t ctx)
{
pthread_mutex_unlock(&s_mutex);
}

View File

@@ -0,0 +1,212 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <nuttx/config.h>
#include <assert.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <signal.h>
#include "nimble/nimble_npl.h"
#ifndef CONFIG_NIMBLE_CALLOUT_THREAD_STACKSIZE
#define CONFIG_NIMBLE_CALLOUT_THREAD_STACKSIZE 1024
#endif
struct ble_npl_callout *pending_callout = NULL;
bool thread_started = false;
pthread_t callout_thread;
pthread_mutex_t callout_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t callout_cond = PTHREAD_COND_INITIALIZER;
static pthread_addr_t
callout_handler(pthread_addr_t arg)
{
struct ble_npl_callout *c;
while (true) {
pthread_mutex_lock(&callout_mutex);
while (!pending_callout) {
pthread_cond_wait(&callout_cond, &callout_mutex);
}
c = pending_callout;
pending_callout = NULL;
pthread_mutex_unlock(&callout_mutex);
/* Invoke callback */
if (c->c_evq) {
ble_npl_eventq_put(c->c_evq, &c->c_ev);
} else {
c->c_ev.ev_cb(&c->c_ev);
}
}
return NULL;
}
static void
ble_npl_callout_timer_cb(union sigval sv)
{
struct ble_npl_callout *c = (struct ble_npl_callout *)sv.sival_ptr;
assert(c);
pthread_mutex_lock(&callout_mutex);
pending_callout = c;
pthread_cond_signal(&callout_cond);
pthread_mutex_unlock(&callout_mutex);
}
void
ble_npl_callout_init(struct ble_npl_callout *c,
struct ble_npl_eventq *evq,
ble_npl_event_fn *ev_cb,
void *ev_arg)
{
struct sigevent event;
if (!thread_started) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, CONFIG_NIMBLE_CALLOUT_THREAD_STACKSIZE);
pthread_create(&callout_thread, &attr, callout_handler, NULL);
pthread_setname_np(callout_thread, "ble_npl_callout");
thread_started = true;
}
/* Initialize the callout. */
memset(c, 0, sizeof(*c));
c->c_ev.ev_cb = ev_cb;
c->c_ev.ev_arg = ev_arg;
c->c_evq = evq;
c->c_active = false;
event.sigev_notify = SIGEV_THREAD;
event.sigev_value.sival_ptr = c; // put callout obj in signal args
event.sigev_notify_function = ble_npl_callout_timer_cb;
event.sigev_notify_attributes = NULL;
timer_create(CLOCK_REALTIME, &event, &c->c_timer);
}
bool
ble_npl_callout_is_active(struct ble_npl_callout *c)
{
/* TODO: seek native posix method to determine whether timer_t is active.
TODO: fix bug where one-shot timer is still active after fired. */
return c->c_active;
}
int
ble_npl_callout_inited(struct ble_npl_callout *c)
{
return (c->c_timer != NULL);
}
ble_npl_error_t
ble_npl_callout_reset(struct ble_npl_callout *c,
ble_npl_time_t ticks)
{
struct itimerspec its;
if (ticks < 0) {
return BLE_NPL_EINVAL;
}
if (ticks == 0) {
ticks = 1;
}
c->c_ticks = ble_npl_time_get() + ticks;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0; /* one shot */
its.it_value.tv_sec = (ticks / 1000);
its.it_value.tv_nsec = (ticks % 1000) * 1000000; /* expiration */
its.it_value.tv_nsec %= 1000000000;
c->c_active = true;
timer_settime(c->c_timer, 0, &its, NULL);
return BLE_NPL_OK;
}
int
ble_npl_callout_queued(struct ble_npl_callout *c)
{
struct itimerspec its;
timer_gettime(c->c_timer, &its);
return ((its.it_value.tv_sec > 0) ||
(its.it_value.tv_nsec > 0));
}
void
ble_npl_callout_stop(struct ble_npl_callout *c)
{
if (!ble_npl_callout_inited(c)) {
return;
}
struct itimerspec its;
its.it_interval.tv_sec = 0;
its.it_interval.tv_nsec = 0;
its.it_value.tv_sec = 0;
its.it_value.tv_nsec = 0;
timer_settime(c->c_timer, 0, &its, NULL);
c->c_active = false;
}
ble_npl_time_t
ble_npl_callout_get_ticks(struct ble_npl_callout *co)
{
return co->c_ticks;
}
void
ble_npl_callout_set_arg(struct ble_npl_callout *co, void *arg)
{
co->c_ev.ev_arg = arg;
}
uint32_t
ble_npl_callout_remaining_ticks(struct ble_npl_callout *co,
ble_npl_time_t now)
{
ble_npl_time_t rt;
uint32_t exp;
struct itimerspec its;
timer_gettime(co->c_timer, &its);
exp = its.it_value.tv_sec * 1000;
if (exp > now) {
rt = exp - now;
} else {
rt = 0;
}
return rt;
}

View File

@@ -0,0 +1,182 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <nuttx/config.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>
#include "nimble/nimble_npl.h"
static struct ble_npl_eventq dflt_evq =
{
.mq = (mqd_t)-1
};
int qnum = 0;
struct ble_npl_eventq *
ble_npl_eventq_dflt_get(void)
{
return &dflt_evq;
}
void
ble_npl_eventq_init(struct ble_npl_eventq *evq)
{
char path[PATH_MAX];
struct mq_attr attr;
attr.mq_maxmsg = 16;
attr.mq_msgsize = sizeof(struct ble_npl_event*);
attr.mq_flags = 0;
qnum++;
sprintf(path, "/nimbleq%i", qnum);
evq->mq = mq_open(path, O_CREAT | O_RDWR, 0, &attr);
DEBUGASSERT((intptr_t)evq->mq >= 0);
}
bool
ble_npl_eventq_is_empty(struct ble_npl_eventq *evq)
{
struct mq_attr attr;
mq_getattr(evq->mq, &attr);
return (attr.mq_curmsgs == 0);
}
int
ble_npl_eventq_inited(const struct ble_npl_eventq *evq)
{
return (evq->mq != ((mqd_t)-1));
}
void
ble_npl_eventq_put(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
{
int ret;
if (ev->ev_queued)
{
return;
}
ev->ev_queued = 1;
ret = mq_send(evq->mq, (const char*)&ev, sizeof(ev), 0);
DEBUGASSERT(ret == 0);
UNUSED(ret);
}
struct ble_npl_event *
ble_npl_eventq_get(struct ble_npl_eventq *evq,
ble_npl_time_t tmo)
{
struct ble_npl_event *ev = NULL;
if (tmo == BLE_NPL_TIME_FOREVER)
{
do
{
mq_receive(evq->mq, (char*)&ev, sizeof(ev), NULL);
} while (ev && ev->ev_queued == 0);
}
else
{
struct timespec now, t;
clock_gettime(CLOCK_REALTIME, &now);
t.tv_sec += tmo / USEC_PER_SEC;
t.tv_nsec = (tmo - t.tv_sec) * NSEC_PER_MSEC;
clock_timespec_add(&now, &t, &t);
do
{
mq_timedreceive(evq->mq, (char*)&ev, sizeof(ev), NULL, &t);
} while (ev && ev->ev_queued == 0);
}
if (ev) {
ev->ev_queued = 0;
}
return ev;
}
void
ble_npl_eventq_run(struct ble_npl_eventq *evq)
{
struct ble_npl_event *ev;
ev = ble_npl_eventq_get(evq, BLE_NPL_TIME_FOREVER);
ble_npl_event_run(ev);
}
// ========================================================================
// Event Implementation
// ========================================================================
void
ble_npl_event_init(struct ble_npl_event *ev, ble_npl_event_fn *fn,
void *arg)
{
memset(ev, 0, sizeof(*ev));
ev->ev_cb = fn;
ev->ev_arg = arg;
}
bool
ble_npl_event_is_queued(struct ble_npl_event *ev)
{
return ev->ev_queued;
}
void *
ble_npl_event_get_arg(struct ble_npl_event *ev)
{
return ev->ev_arg;
}
void
ble_npl_event_set_arg(struct ble_npl_event *ev, void *arg)
{
ev->ev_arg = arg;
}
void
ble_npl_event_run(struct ble_npl_event *ev)
{
assert(ev->ev_cb != NULL);
ev->ev_cb(ev);
}
void
ble_npl_eventq_remove(struct ble_npl_eventq *evq, struct ble_npl_event *ev)
{
/* we simply mark the event as unqueued. we will ignore these elements
* when receiving from the queue */
ev->ev_queued = 0;
}

View File

@@ -0,0 +1,82 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <nuttx/config.h>
#include <errno.h>
#include <pthread.h>
#include "os/os.h"
#include "nimble/nimble_npl.h"
ble_npl_error_t
ble_npl_mutex_init(struct ble_npl_mutex *mu)
{
if (!mu) {
return BLE_NPL_INVALID_PARAM;
}
pthread_mutexattr_init(&mu->attr);
pthread_mutexattr_settype(&mu->attr, PTHREAD_MUTEX_RECURSIVE);
pthread_mutex_init(&mu->lock, &mu->attr);
return BLE_NPL_OK;
}
ble_npl_error_t
ble_npl_mutex_release(struct ble_npl_mutex *mu)
{
if (!mu) {
return BLE_NPL_INVALID_PARAM;
}
if (pthread_mutex_unlock(&mu->lock)) {
return BLE_NPL_BAD_MUTEX;
}
return BLE_NPL_OK;
}
ble_npl_error_t
ble_npl_mutex_pend(struct ble_npl_mutex *mu, uint32_t timeout)
{
int err;
if (!mu) {
return BLE_NPL_INVALID_PARAM;
}
if (timeout == BLE_NPL_TIME_FOREVER) {
err = pthread_mutex_lock(&mu->lock);
} else {
err = clock_gettime(CLOCK_REALTIME, &mu->wait);
if (err) {
return BLE_NPL_ERROR;
}
mu->wait.tv_sec += timeout / 1000;
mu->wait.tv_nsec += (timeout % 1000) * 1000000;
err = pthread_mutex_timedlock(&mu->lock, &mu->wait);
if (err == ETIMEDOUT) {
return BLE_NPL_TIMEOUT;
}
}
return (err) ? BLE_NPL_ERROR : BLE_NPL_OK;
}

View File

@@ -0,0 +1,100 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <nuttx/config.h>
#include <assert.h>
#include <errno.h>
#include <semaphore.h>
#include "os/os.h"
#include "nimble/nimble_npl.h"
ble_npl_error_t
ble_npl_sem_init(struct ble_npl_sem *sem, uint16_t tokens)
{
if (!sem) {
return BLE_NPL_INVALID_PARAM;
}
sem_init(&sem->lock, 0, tokens);
return BLE_NPL_OK;
}
ble_npl_error_t
ble_npl_sem_release(struct ble_npl_sem *sem)
{
int err;
if (!sem) {
return BLE_NPL_INVALID_PARAM;
}
err = sem_post(&sem->lock);
return (err) ? BLE_NPL_ERROR : BLE_NPL_OK;
}
ble_npl_error_t
ble_npl_sem_pend(struct ble_npl_sem *sem, uint32_t timeout)
{
int err = 0;
struct timespec wait;
if (!sem) {
return BLE_NPL_INVALID_PARAM;
}
if (timeout == BLE_NPL_TIME_FOREVER) {
err = sem_wait(&sem->lock);
} else {
err = clock_gettime(CLOCK_REALTIME, &wait);
if (err) {
return BLE_NPL_ERROR;
}
wait.tv_sec += timeout / 1000;
wait.tv_nsec += (timeout % 1000) * 1000000;
err = sem_timedwait(&sem->lock, &wait);
if (err && errno == ETIMEDOUT) {
return BLE_NPL_TIMEOUT;
}
}
return (err) ? BLE_NPL_ERROR : BLE_NPL_OK;
}
uint16_t
ble_npl_sem_get_count(struct ble_npl_sem *sem)
{
int count;
assert(sem);
assert(&sem->lock);
sem_getvalue(&sem->lock, &count);
/* NuttX can return negative value, return 0 in that case */
if (count < 0) {
count = 0;
}
return count;
}

View File

@@ -0,0 +1,130 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <nuttx/config.h>
#include <errno.h>
#include "os/os.h"
#include "nimble/nimble_npl.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Initialize a task.
*
* This function initializes the task structure pointed to by t,
* clearing and setting it's stack pointer, provides sane defaults
* and sets the task as ready to run, and inserts it into the operating
* system scheduler.
*
* @param t The task to initialize
* @param name The name of the task to initialize
* @param func The task function to call
* @param arg The argument to pass to this task function
* @param prio The priority at which to run this task
* @param sanity_itvl The time at which this task should check in with the
* sanity task. OS_WAIT_FOREVER means never check in
* here.
* @param stack_bottom A pointer to the bottom of a task's stack
* @param stack_size The overall size of the task's stack.
*
* @return 0 on success, non-zero on failure.
*/
int
ble_npl_task_init(struct ble_npl_task *t, const char *name, ble_npl_task_func_t func,
void *arg, uint8_t prio, ble_npl_time_t sanity_itvl,
ble_npl_stack_t *stack_bottom, uint16_t stack_size)
{
int err;
if ((t == NULL) || (func == NULL)) {
return OS_INVALID_PARM;
}
err = pthread_attr_init(&t->attr);
if (err) return err;
err = pthread_attr_getschedparam (&t->attr, &t->param);
if (err) return err;
#if CONFIG_RR_INTERVAL > 0
err = pthread_attr_setschedpolicy(&t->attr, SCHED_RR);
if (err) return err;
#endif
t->param.sched_priority = prio;
err = pthread_attr_setschedparam (&t->attr, &t->param);
if (err) return err;
t->name = name;
err = pthread_create(&t->handle, &t->attr, func, arg);
if (err == ENOMEM)
{
err = OS_ENOMEM;
}
else
{
pthread_setname_np(t->handle, t->name);
}
return err;
}
/*
* Removes specified task
* XXX
* NOTE: This interface is currently experimental and not ready for common use
*/
int
ble_npl_task_remove(struct ble_npl_task *t)
{
return pthread_cancel(t->handle);
}
/**
* Return the number of tasks initialized.
*
* @return number of tasks initialized
*/
uint8_t
ble_npl_task_count(void)
{
return 0;
}
void *
ble_npl_get_current_task_id(void)
{
return (void *)(uintptr_t)pthread_self();
}
bool
ble_npl_os_started(void)
{
return true;
}
void
ble_npl_task_yield(void)
{
pthread_yield();
}
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,85 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 <nuttx/config.h>
#include <assert.h>
#include <stdint.h>
#include <string.h>
#include "os/os.h"
#include "nimble/nimble_npl.h"
#include <unistd.h>
#include <time.h>
/**
* Return ticks [ms] since system start as uint32_t.
*/
ble_npl_time_t
ble_npl_time_get(void)
{
struct timespec now;
if (clock_gettime(CLOCK_MONOTONIC, &now)) {
return 0;
}
return now.tv_sec * 1000.0 + now.tv_nsec / 1000000.0;
}
ble_npl_error_t
ble_npl_time_ms_to_ticks(uint32_t ms, ble_npl_time_t *out_ticks)
{
*out_ticks = ms;
return BLE_NPL_OK;
}
ble_npl_error_t
ble_npl_time_ticks_to_ms(ble_npl_time_t ticks, uint32_t *out_ms)
{
*out_ms = ticks;
return BLE_NPL_OK;
}
ble_npl_time_t
ble_npl_time_ms_to_ticks32(uint32_t ms)
{
return ms;
}
uint32_t
ble_npl_time_ticks_to_ms32(ble_npl_time_t ticks)
{
return ticks;
}
void
ble_npl_time_delay(ble_npl_time_t ticks)
{
struct timespec sleep_time;
long ms = ble_npl_time_ticks_to_ms32(ticks);
uint32_t s = ms / 1000;
ms -= s * 1000;
sleep_time.tv_sec = s;
sleep_time.tv_nsec = ms * 1000000;
nanosleep(&sleep_time, NULL);
}