rel_1.6.0 init

This commit is contained in:
guocheng.kgc 2020-06-18 20:06:52 +08:00 committed by shengdong.dsd
commit 27b3e2883d
19359 changed files with 8093121 additions and 0 deletions

View file

@ -0,0 +1,479 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <string.h>
#include <stdlib.h>
#include <sys/time.h>
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/semphr.h>
#include <aos/aos.h>
#define ms2tick(ms) (((ms)+portTICK_PERIOD_MS-1)/portTICK_PERIOD_MS)
void aos_reboot(void)
{
}
int aos_get_hz(void)
{
return 100;
}
const char *aos_version_get(void)
{
return "aos-linux-xxx";
}
#define AOS_MAGIC 0x20171020
typedef struct {
StaticTask_t fTask;
uint32_t key_bitmap;
void *keys[4];
void *stack;
char name[32];
uint32_t magic;
} AosStaticTask_t;
struct targ {
AosStaticTask_t *task;
void (*fn)(void *);
void *arg;
};
static void dfl_entry(void *arg)
{
struct targ *targ = arg;
void (*fn)(void *) = targ->fn;
void *farg = targ->arg;
free(targ);
fn(farg);
vTaskDelete(NULL);
}
void vPortCleanUpTCB(void *pxTCB)
{
AosStaticTask_t *task = (AosStaticTask_t *)pxTCB;
if (task->magic != AOS_MAGIC)
return;
free(task->stack);
free(task);
}
int aos_task_new(const char *name, void (*fn)(void *), void *arg,
int stack_size)
{
xTaskHandle xHandle;
AosStaticTask_t *task = malloc(sizeof(AosStaticTask_t));
struct targ *targ = malloc(sizeof(*targ));
void *stack = malloc(stack_size);
bzero(stack, stack_size);
bzero(task, sizeof(*task));
task->key_bitmap = 0xfffffff0;
task->stack = stack;
strncpy(task->name, name, sizeof(task->name)-1);
task->magic = AOS_MAGIC;
targ->task = task;
targ->fn = fn;
targ->arg = arg;
stack_size /= sizeof(StackType_t);
xHandle = xTaskCreateStatic(dfl_entry, name, stack_size, targ,
10, stack, &task->fTask);
if (xHandle == NULL) {
free(task);
free(stack);
free(targ);
}
return xHandle ? 0 : -1;
}
int aos_task_new_ext(aos_task_t *task, const char *name, void (*fn)(void *), void *arg,
int stack_size, int prio)
{
return aos_task_new(name, fn, arg, stack_size);
}
void aos_task_exit(int code)
{
vTaskDelete(NULL);
}
const char *aos_task_name(void)
{
AosStaticTask_t *task = (AosStaticTask_t *)xTaskGetCurrentTaskHandle();
if (task->magic != AOS_MAGIC)
return "unknown";
return task->name;
}
int aos_task_key_create(aos_task_key_t *key)
{
AosStaticTask_t *task = (AosStaticTask_t *)xTaskGetCurrentTaskHandle();
int i;
if (task->magic != AOS_MAGIC)
return -1;
for (i=0;i<4;i++) {
if (task->key_bitmap & (1 << i))
continue;
task->key_bitmap |= 1 << i;
*key = i;
return 0;
}
return -1;
}
void aos_task_key_delete(aos_task_key_t key)
{
AosStaticTask_t *task = (AosStaticTask_t *)xTaskGetCurrentTaskHandle();
if (task->magic != AOS_MAGIC)
return;
task->key_bitmap &= ~(1 << key);
}
int aos_task_setspecific(aos_task_key_t key, void *vp)
{
AosStaticTask_t *task = (AosStaticTask_t *)xTaskGetCurrentTaskHandle();
if (key >= 4)
return -1;
if (task->magic != AOS_MAGIC)
return -1;
task->keys[key] = vp;
return 0;
}
void *aos_task_getspecific(aos_task_key_t key)
{
AosStaticTask_t *task = (AosStaticTask_t *)xTaskGetCurrentTaskHandle();
if (key >= 4)
return NULL;
if (task->magic != AOS_MAGIC)
return NULL;
return task->keys[key];
}
int aos_mutex_new(aos_mutex_t *mutex)
{
SemaphoreHandle_t mux = xSemaphoreCreateMutex();
mutex->hdl = mux;
return mux != NULL ? 0 : -1;
}
void aos_mutex_free(aos_mutex_t *mutex)
{
vSemaphoreDelete(mutex->hdl);
}
int aos_mutex_lock(aos_mutex_t *mutex, unsigned int ms)
{
if (mutex) {
xSemaphoreTake(mutex->hdl, ms == AOS_WAIT_FOREVER ? portMAX_DELAY : ms2tick(ms));
}
return 0;
}
int aos_mutex_unlock(aos_mutex_t *mutex)
{
if (mutex) {
xSemaphoreGive(mutex->hdl);
}
return 0;
}
int aos_mutex_is_valid(aos_mutex_t *mutex)
{
return mutex->hdl != NULL;
}
int aos_sem_new(aos_sem_t *sem, int count)
{
SemaphoreHandle_t s = xSemaphoreCreateCounting(128, count);
sem->hdl = s;
return 0;
}
void aos_sem_free(aos_sem_t *sem)
{
if (sem == NULL) {
return;
}
vSemaphoreDelete(sem->hdl);
}
int aos_sem_wait(aos_sem_t *sem, unsigned int ms)
{
if (sem == NULL) {
return -1;
}
int ret = xSemaphoreTake(sem->hdl, ms == AOS_WAIT_FOREVER ? portMAX_DELAY : ms2tick(ms));
return ret == pdPASS ? 0 : -1;
}
void aos_sem_signal(aos_sem_t *sem)
{
if (sem == NULL) {
return;
}
xSemaphoreGive(sem->hdl);
}
int aos_sem_is_valid(aos_sem_t *sem)
{
return sem && sem->hdl != NULL;
}
void aos_sem_signal_all(aos_sem_t *sem)
{
aos_sem_signal(sem);
}
int aos_queue_new(aos_queue_t *queue, void *buf, unsigned int size, int max_msg)
{
xQueueHandle q;
q = xQueueCreate(size / max_msg, max_msg);
queue->hdl = q;
return 0;
}
void aos_queue_free(aos_queue_t *queue)
{
vQueueDelete(queue->hdl);
}
int aos_queue_send(aos_queue_t *queue, void *msg, unsigned int size)
{
return xQueueSend(queue->hdl, msg, portMAX_DELAY) == pdPASS ? 0 : -1;
}
int aos_queue_recv(aos_queue_t *queue, unsigned int ms, void *msg,
unsigned int *size)
{
return xQueueReceive(queue->hdl, msg, ms == AOS_WAIT_FOREVER ? portMAX_DELAY : ms2tick(ms))
== pdPASS ? 0 : -1;
}
int aos_queue_is_valid(aos_queue_t *queue)
{
return queue && queue->hdl != NULL;
}
void *aos_queue_buf_ptr(aos_queue_t *queue)
{
return NULL;
}
int aos_timer_new(aos_timer_t *timer, void (*fn)(void *, void *),
void *arg, int ms, int repeat)
{
return -1;
}
void aos_timer_free(aos_timer_t *timer)
{
}
int aos_timer_start(aos_timer_t *timer)
{
return -1;
}
int aos_timer_stop(aos_timer_t *timer)
{
return -1;
}
int aos_timer_change(aos_timer_t *timer, int ms)
{
return -1;
}
int aos_workqueue_create(aos_workqueue_t *workqueue, int pri, int stack_size)
{
return -1;
}
struct work {
void (*fn)(void *);
void *arg;
int dly;
};
int aos_work_init(aos_work_t *work, void (*fn)(void *), void *arg, int dly)
{
struct work *w = malloc(sizeof(*w));
w->fn = fn;
w->arg = arg;
w->dly = dly;
work->hdl = w;
return 0;
}
void aos_work_destroy(aos_work_t *work)
{
free(work->hdl);
}
int aos_work_run(aos_workqueue_t *workqueue, aos_work_t *work)
{
return aos_work_sched(work);
}
static void worker_entry(void *arg)
{
struct work *w = arg;
if (w->dly) {
usleep(w->dly * 1000);
}
w->fn(w->arg);
}
int aos_work_sched(aos_work_t *work)
{
struct work *w = work->hdl;
return aos_task_new("worker", worker_entry, w, 8192);
}
int aos_work_cancel(aos_work_t *work)
{
return -1;
}
void *aos_zalloc(unsigned int size)
{
return calloc(size, 1);
}
void *aos_malloc(unsigned int size)
{
return malloc(size);
}
void *aos_realloc(void *mem, unsigned int size)
{
return realloc(mem, size);
}
void aos_alloc_trace(void *addr, size_t allocator)
{
}
void aos_free(void *mem)
{
free(mem);
}
#ifndef timercmp
#define timercmp(a, b, op) ({ \
unsigned long long _v1 = (a)->tv_sec * 1000000ULL + (a)->tv_usec; \
unsigned long long _v2 = (b)->tv_sec * 1000000ULL + (b)->tv_usec; \
_v1 op _v2; \
})
void timersub(struct timeval *a, struct timeval *b, struct timeval *res)
{
res->tv_usec = a->tv_usec - b->tv_usec;
res->tv_sec = a->tv_sec - b->tv_sec;
if (res->tv_usec < 0) {
res->tv_usec += 1000000;
res->tv_sec -= 1;
}
}
#endif
static struct timeval sys_start_time;
long long aos_now(void)
{
struct timeval tv;
long long ns;
gettimeofday(&tv, NULL);
timersub(&tv, &sys_start_time, &tv);
ns = tv.tv_sec * 1000000LL + tv.tv_usec;
return ns * 1000LL;
}
long long aos_now_ms(void)
{
struct timeval tv;
long long ms;
gettimeofday(&tv, NULL);
timersub(&tv, &sys_start_time, &tv);
ms = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
return ms;
}
void aos_msleep(int ms)
{
usleep(ms * 1000);
}
void aos_init(void)
{
gettimeofday(&sys_start_time, NULL);
}
void aos_start(void)
{
}
void dumpsys_task_func(void)
{
#if configUSE_TRACE_FACILITY
TaskStatus_t *pxTaskStatusArray;
volatile UBaseType_t uxArraySize, x;
uint32_t ulTotalRunTime, ulStatsAsPercentage;
uxArraySize = uxTaskGetNumberOfTasks();
pxTaskStatusArray = malloc( uxArraySize * sizeof( TaskStatus_t ) );
if( pxTaskStatusArray == NULL )
return;
uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalRunTime );
// For percentage calculations.
ulTotalRunTime /= 100UL;
// Avoid divide by zero errors.
if( ulTotalRunTime > 0 )
{
// For each populated position in the pxTaskStatusArray array,
// format the raw data as human readable ASCII data
for( x = 0; x < uxArraySize; x++ )
{
// What percentage of the total run time has the task used?
// This will always be rounded down to the nearest integer.
// ulTotalRunTimeDiv100 has already been divided by 100.
ulStatsAsPercentage = pxTaskStatusArray[ x ].ulRunTimeCounter / ulTotalRunTime;
if( ulStatsAsPercentage > 0UL )
{
printf("%s\t\t%u\t\t%u%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter, ulStatsAsPercentage );
}
else
{
printf("%s\t\t%u\t\t<1%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter );
}
}
}
free( pxTaskStatusArray );
#endif
}

View file

@ -0,0 +1,436 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include <sys/prctl.h>
#include <pthread.h>
#undef WITH_LWIP
#undef WITH_SAL
#include <aos/aos.h>
#include <poll.h>
void aos_reboot(void)
{
exit(0);
}
int aos_get_hz(void)
{
return 100;
}
const char *aos_version_get(void)
{
return "aos-linux-xxx";
}
struct targ {
const char *name;
void (*fn)(void *);
void *arg;
};
static void *dfl_entry(void *arg)
{
struct targ *targ = arg;
void (*fn)(void *) = targ->fn;
void *farg = targ->arg;
prctl(PR_SET_NAME, (unsigned long)targ->name, 0, 0, 0);
free(targ);
fn(farg);
return 0;
}
int aos_task_new(const char *name, void (*fn)(void *), void *arg,
int stack_size)
{
int ret;
pthread_t th;
struct targ *targ = malloc(sizeof(*targ));
targ->name = strdup(name);
targ->fn = fn;
targ->arg = arg;
ret = pthread_create(&th, NULL, dfl_entry, targ);
if (ret == 0) ret = pthread_detach(th);
return ret;
}
int aos_task_new_ext(aos_task_t *task, const char *name, void (*fn)(void *), void *arg,
int stack_size, int prio)
{
return aos_task_new(name, fn, arg, stack_size);
}
void aos_task_exit(int code)
{
pthread_exit((void *)code);
}
const char *aos_task_name(void)
{
static char name[16];
prctl(PR_GET_NAME, (unsigned long)name, 0, 0, 0);
return name;
}
int aos_task_key_create(aos_task_key_t *key)
{
return pthread_key_create(key, NULL);
}
void aos_task_key_delete(aos_task_key_t key)
{
pthread_key_delete(key);
}
int aos_task_setspecific(aos_task_key_t key, void *vp)
{
return pthread_setspecific(key, vp);
}
void *aos_task_getspecific(aos_task_key_t key)
{
return pthread_getspecific(key);
}
int aos_mutex_new(aos_mutex_t *mutex)
{
pthread_mutex_t *mtx = malloc(sizeof(*mtx));
pthread_mutex_init(mtx, NULL);
mutex->hdl = mtx;
return 0;
}
void aos_mutex_free(aos_mutex_t *mutex)
{
pthread_mutex_destroy(mutex->hdl);
free(mutex->hdl);
}
int aos_mutex_lock(aos_mutex_t *mutex, unsigned int timeout)
{
if (mutex) {
pthread_mutex_lock(mutex->hdl);
}
return 0;
}
int aos_mutex_unlock(aos_mutex_t *mutex)
{
if (mutex) {
pthread_mutex_unlock(mutex->hdl);
}
return 0;
}
int aos_mutex_is_valid(aos_mutex_t *mutex)
{
return mutex->hdl != NULL;
}
#include <semaphore.h>
int aos_sem_new(aos_sem_t *sem, int count)
{
sem_t *s = malloc(sizeof(*s));
sem_init(s, 0, count);
sem->hdl = s;
return 0;
}
void aos_sem_free(aos_sem_t *sem)
{
if (sem == NULL) {
return;
}
sem_destroy(sem->hdl);
free(sem->hdl);
}
int aos_sem_wait(aos_sem_t *sem, unsigned int timeout)
{
int sec;
int nsec;
if (sem == NULL) {
return -EINVAL;
}
if (timeout == AOS_WAIT_FOREVER) {
return sem_wait(sem->hdl);
} else if (timeout == 0) {
return sem_trywait(sem->hdl);
}
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
sec = timeout / 1000;
nsec = (timeout % 1000) * 1000;
ts.tv_nsec += nsec;
sec += (ts.tv_nsec / 1000000000);
ts.tv_nsec %= 1000000000;
ts.tv_sec += sec;
return sem_timedwait(sem->hdl, &ts);
}
void aos_sem_signal(aos_sem_t *sem)
{
if (sem == NULL) {
return;
}
sem_post(sem->hdl);
}
int aos_sem_is_valid(aos_sem_t *sem)
{
return sem && sem->hdl != NULL;
}
void aos_sem_signal_all(aos_sem_t *sem)
{
sem_post(sem->hdl);
}
struct queue {
int fds[2];
void *buf;
int size;
int msg_size;
};
int aos_queue_new(aos_queue_t *queue, void *buf, unsigned int size, int max_msg)
{
struct queue *q = malloc(sizeof(*q));
pipe(q->fds);
q->buf = buf;
q->size = size;
q->msg_size = max_msg;
queue->hdl = q;
return 0;
}
void aos_queue_free(aos_queue_t *queue)
{
struct queue *q = queue->hdl;
close(q->fds[0]);
close(q->fds[1]);
free(q);
}
int aos_queue_send(aos_queue_t *queue, void *msg, unsigned int size)
{
struct queue *q = queue->hdl;
write(q->fds[1], msg, size);
return 0;
}
int aos_queue_recv(aos_queue_t *queue, unsigned int ms, void *msg,
unsigned int *size)
{
struct queue *q = queue->hdl;
struct pollfd rfd = {
.fd = q->fds[0],
.events = POLLIN,
};
poll(&rfd, 1, ms);
if (rfd.revents & POLLIN) {
int len = read(q->fds[0], msg, q->msg_size);
*size = len;
return len < 0 ? -1 : 0;
}
return -1;
}
int aos_queue_is_valid(aos_queue_t *queue)
{
return queue->hdl != NULL;
}
void *aos_queue_buf_ptr(aos_queue_t *queue)
{
struct queue *q = queue->hdl;
return q->buf;
}
int aos_timer_new(aos_timer_t *timer, void (*fn)(void *, void *),
void *arg, int ms, int repeat)
{
return -1;
}
void aos_timer_free(aos_timer_t *timer)
{
}
int aos_timer_start(aos_timer_t *timer)
{
return -1;
}
int aos_timer_stop(aos_timer_t *timer)
{
return -1;
}
int aos_timer_change(aos_timer_t *timer, int ms)
{
return -1;
}
int aos_workqueue_create(aos_workqueue_t *workqueue, int pri, int stack_size)
{
return -1;
}
struct work {
void (*fn)(void *);
void *arg;
int dly;
};
int aos_work_init(aos_work_t *work, void (*fn)(void *), void *arg, int dly)
{
struct work *w = malloc(sizeof(*w));
w->fn = fn;
w->arg = arg;
w->dly = dly;
work->hdl = w;
return 0;
}
void aos_work_destroy(aos_work_t *work)
{
free(work->hdl);
}
int aos_work_run(aos_workqueue_t *workqueue, aos_work_t *work)
{
return aos_work_sched(work);
}
static void worker_entry(void *arg)
{
struct work *w = arg;
if (w->dly) {
usleep(w->dly * 1000);
}
w->fn(w->arg);
}
int aos_work_sched(aos_work_t *work)
{
struct work *w = work->hdl;
return aos_task_new("worker", worker_entry, w, 8192);
}
int aos_work_cancel(aos_work_t *work)
{
return -1;
}
void *aos_zalloc(unsigned int size)
{
return calloc(size, 1);
}
void *aos_malloc(unsigned int size)
{
return malloc(size);
}
void *aos_realloc(void *mem, unsigned int size)
{
return realloc(mem, size);
}
void aos_alloc_trace(void *addr, size_t allocator)
{
}
void aos_free(void *mem)
{
free(mem);
}
static struct timeval sys_start_time;
long long aos_now(void)
{
struct timeval tv;
long long ns;
gettimeofday(&tv, NULL);
timersub(&tv, &sys_start_time, &tv);
ns = tv.tv_sec * 1000000LL + tv.tv_usec;
return ns * 1000LL;
}
long long aos_now_ms(void)
{
struct timeval tv;
long long ms;
gettimeofday(&tv, NULL);
timersub(&tv, &sys_start_time, &tv);
ms = tv.tv_sec * 1000LL + tv.tv_usec / 1000;
return ms;
}
void aos_msleep(int ms)
{
usleep(ms * 1000);
}
void aos_init(void)
{
gettimeofday(&sys_start_time, NULL);
}
void aos_start(void)
{
while (1) {
usleep(1000 * 1000 * 100);
}
}
#include <stdio.h>
#include <sys/types.h>
#include <dirent.h>
void dumpsys_task_func(void)
{
DIR *proc = opendir("/proc/self/task");
while (1) {
struct dirent *ent = readdir(proc);
if (!ent) {
break;
}
if (ent->d_name[0] == '.') {
continue;
}
char fn[128];
snprintf(fn, sizeof fn, "/proc/self/task/%s/comm", ent->d_name);
FILE *fp = fopen(fn, "r");
if (!fp) {
continue;
}
bzero(fn, sizeof fn);
fread(fn, sizeof(fn) - 1, 1, fp);
fclose(fp);
printf("%8s - %s", ent->d_name, fn);
}
closedir(proc);
}

View file

@ -0,0 +1,926 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#include <k_api.h>
#include <errno.h>
#include <aos/aos.h>
#include "errno_mapping.h"
#if (RHINO_CONFIG_KOBJ_DYN_ALLOC == 0)
#warning "RHINO_CONFIG_KOBJ_DYN_ALLOC is disabled!"
#endif
#define MS2TICK(ms) krhino_ms_to_ticks(ms)
static unsigned int used_bitmap;
static int error_mapping(int ret)
{
switch(ret) {
case RHINO_SYS_SP_ERR:
case RHINO_NULL_PTR:
case RHINO_MM_FREE_ADDR_ERR:
return -EFAULT;
case RHINO_INV_PARAM:
case RHINO_INV_ALIGN:
case RHINO_KOBJ_TYPE_ERR:
case RHINO_MM_POOL_SIZE_ERR:
case RHINO_MM_ALLOC_SIZE_ERR:
case RHINO_INV_SCHED_WAY:
case RHINO_TASK_INV_STACK_SIZE:
case RHINO_BEYOND_MAX_PRI:
case RHINO_BUF_QUEUE_INV_SIZE:
case RHINO_BUF_QUEUE_SIZE_ZERO:
case RHINO_BUF_QUEUE_MSG_SIZE_OVERFLOW:
case RHINO_QUEUE_FULL:
case RHINO_QUEUE_NOT_FULL:
case RHINO_SEM_OVF:
case RHINO_WORKQUEUE_EXIST:
case RHINO_WORKQUEUE_NOT_EXIST:
case RHINO_WORKQUEUE_WORK_EXIST:
return -EINVAL;
case RHINO_KOBJ_BLK:
return -EAGAIN;
case RHINO_NO_MEM:
return -ENOMEM;
case RHINO_KOBJ_DEL_ERR:
case RHINO_SCHED_DISABLE:
case RHINO_SCHED_ALREADY_ENABLED:
case RHINO_SCHED_LOCK_COUNT_OVF:
case RHINO_TASK_NOT_SUSPENDED:
case RHINO_TASK_DEL_NOT_ALLOWED:
case RHINO_TASK_SUSPEND_NOT_ALLOWED:
case RHINO_SUSPENDED_COUNT_OVF:
case RHINO_PRI_CHG_NOT_ALLOWED:
case RHINO_NOT_CALLED_BY_INTRPT:
case RHINO_NO_THIS_EVENT_OPT:
case RHINO_TIMER_STATE_INV:
case RHINO_BUF_QUEUE_FULL:
case RHINO_SEM_TASK_WAITING:
case RHINO_MUTEX_NOT_RELEASED_BY_OWNER:
case RHINO_WORKQUEUE_WORK_RUNNING:
return -EPERM;
case RHINO_TRY_AGAIN:
case RHINO_WORKQUEUE_BUSY:
return -EAGAIN;
default:
return -1;
}
}
extern void hal_reboot(void);
void aos_reboot(void)
{
hal_reboot();
}
int aos_get_hz(void)
{
return RHINO_CONFIG_TICKS_PER_SECOND;
}
const char *aos_version_get(void)
{
return SYSINFO_KERNEL_VERSION;
}
#if (RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)
int aos_task_new(const char *name, void (*fn)(void *), void *arg,
int stack_size)
{
int ret;
ktask_t *task_handle = NULL;
ret = (int)krhino_task_dyn_create(&task_handle, name, arg, AOS_DEFAULT_APP_PRI, 0,
stack_size / sizeof(cpu_stack_t), fn, 1u);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_task_new_ext(aos_task_t *task, const char *name, void (*fn)(void *), void *arg,
int stack_size, int prio)
{
int ret;
ret = (int)krhino_task_dyn_create((ktask_t **)(&(task->hdl)), name, arg, prio, 0,
stack_size / sizeof(cpu_stack_t), fn, 1u);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
void aos_task_exit(int code)
{
(void)code;
krhino_task_dyn_del(NULL);
}
#endif
const char *aos_task_name(void)
{
return krhino_cur_task_get()->task_name;
}
int aos_task_key_create(aos_task_key_t *key)
{
int i;
for (i = RHINO_CONFIG_TASK_INFO_NUM - 1; i >= 0; i--) {
if (!((1 << i) & used_bitmap)) {
used_bitmap |= 1 << i;
*key = i;
return 0;
}
}
return -EINVAL;
}
void aos_task_key_delete(aos_task_key_t key)
{
if (key >= RHINO_CONFIG_TASK_INFO_NUM) {
return;
}
used_bitmap &= ~(1 << key);
}
#if (RHINO_CONFIG_TASK_INFO > 0)
int aos_task_setspecific(aos_task_key_t key, void *vp)
{
int ret;
ret = krhino_task_info_set(krhino_cur_task_get(), key, vp);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
void *aos_task_getspecific(aos_task_key_t key)
{
void *vp = NULL;
krhino_task_info_get(krhino_cur_task_get(), key, &vp);
return vp;
}
#endif
#if (RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)
int aos_mutex_new(aos_mutex_t *mutex)
{
kstat_t ret;
kmutex_t *m;
if (mutex == NULL) {
return -EINVAL;
}
m = aos_malloc(sizeof(kmutex_t));
if (m == NULL) {
return -ENOMEM;
}
ret = krhino_mutex_create(m, "AOS");
if (ret != RHINO_SUCCESS) {
aos_free(m);
return ERRNO_MAPPING(ret);
}
mutex->hdl = m;
return 0;
}
void aos_mutex_free(aos_mutex_t *mutex)
{
if (mutex == NULL) {
return;
}
krhino_mutex_del(mutex->hdl);
aos_free(mutex->hdl);
mutex->hdl = NULL;
}
int aos_mutex_lock(aos_mutex_t *mutex, unsigned int timeout)
{
kstat_t ret;
if (mutex == NULL) {
return -EINVAL;
}
if (timeout == AOS_WAIT_FOREVER) {
ret = krhino_mutex_lock(mutex->hdl, RHINO_WAIT_FOREVER);
} else {
ret = krhino_mutex_lock(mutex->hdl, MS2TICK(timeout));
}
/* rhino allow nested */
if (ret == RHINO_MUTEX_OWNER_NESTED) {
ret = RHINO_SUCCESS;
}
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_mutex_unlock(aos_mutex_t *mutex)
{
kstat_t ret;
if (mutex == NULL) {
return -EINVAL;
}
ret = krhino_mutex_unlock(mutex->hdl);
/* rhino allow nested */
if (ret == RHINO_MUTEX_OWNER_NESTED) {
ret = RHINO_SUCCESS;
}
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_mutex_is_valid(aos_mutex_t *mutex)
{
kmutex_t *k_mutex;
if (mutex == NULL) {
return 0;
}
k_mutex = mutex->hdl;
if (k_mutex == NULL) {
return 0;
}
return 1;
}
#endif
#if ((RHINO_CONFIG_KOBJ_DYN_ALLOC > 0)&&(RHINO_CONFIG_SEM > 0))
int aos_sem_new(aos_sem_t *sem, int count)
{
kstat_t ret;
ksem_t *s;
if (sem == NULL) {
return -EINVAL;
}
s = aos_malloc(sizeof(ksem_t));
if (s == NULL) {
return -ENOMEM;
}
ret = krhino_sem_create(s, "AOS", count);
if (ret != RHINO_SUCCESS) {
aos_free(s);
return ERRNO_MAPPING(ret);
}
sem->hdl = s;
return 0;
}
void aos_sem_free(aos_sem_t *sem)
{
if (sem == NULL) {
return;
}
krhino_sem_del(sem->hdl);
aos_free(sem->hdl);
sem->hdl = NULL;
}
int aos_sem_wait(aos_sem_t *sem, unsigned int timeout)
{
kstat_t ret;
if (sem == NULL) {
return -EINVAL;
}
if (timeout == AOS_WAIT_FOREVER) {
ret = krhino_sem_take(sem->hdl, RHINO_WAIT_FOREVER);
} else {
ret = krhino_sem_take(sem->hdl, MS2TICK(timeout));
}
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
void aos_sem_signal(aos_sem_t *sem)
{
if (sem == NULL) {
return;
}
krhino_sem_give(sem->hdl);
}
int aos_sem_is_valid(aos_sem_t *sem)
{
ksem_t *k_sem;
if (sem == NULL) {
return 0;
}
k_sem = sem->hdl;
if (k_sem == NULL) {
return 0;
}
return 1;
}
void aos_sem_signal_all(aos_sem_t *sem)
{
if (sem == NULL) {
return;
}
krhino_sem_give_all(sem->hdl);
}
#endif
#if (RHINO_CONFIG_EVENT_FLAG > 0)
int aos_event_new(aos_event_t *event, unsigned int flags)
{
int ret;
ret = (int)krhino_event_dyn_create((kevent_t **)(&(event->hdl)), "AOS", flags);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
void aos_event_free(aos_event_t *event)
{
if (event == NULL) {
return;
}
krhino_event_dyn_del(event->hdl);
event->hdl = NULL;
}
int aos_event_get
(
aos_event_t *event,
unsigned int flags,
unsigned char opt,
unsigned int *actl_flags,
unsigned int timeout
)
{
kstat_t ret;
if (event == NULL) {
return -EINVAL;
}
if (timeout == AOS_WAIT_FOREVER) {
ret = krhino_event_get(event->hdl, flags, opt, actl_flags, RHINO_WAIT_FOREVER);
} else {
ret = krhino_event_get(event->hdl, flags, opt, actl_flags, MS2TICK(timeout));
}
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_event_set(aos_event_t *event, unsigned int flags, unsigned char opt)
{
kstat_t ret;
if (event == NULL) {
return -EINVAL;
}
ret = krhino_event_set(event->hdl, flags, opt);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
#endif
#if (RHINO_CONFIG_BUF_QUEUE > 0)
int aos_queue_new(aos_queue_t *queue, void *buf, unsigned int size, int max_msg)
{
kstat_t ret;
kbuf_queue_t *q;
if ((queue == NULL) || (buf == NULL)) {
return -EINVAL;
}
q = aos_malloc(sizeof(kbuf_queue_t));
if (q == NULL) {
return -ENOMEM;
}
ret = krhino_buf_queue_create(q, "AOS", buf, size, max_msg);
if (ret != RHINO_SUCCESS) {
aos_free(q);
return ERRNO_MAPPING(ret);
}
queue->hdl = q;
return 0;
}
void aos_queue_free(aos_queue_t *queue)
{
if (queue == NULL) {
return;
}
krhino_buf_queue_del(queue->hdl);
aos_free(queue->hdl);
queue->hdl = NULL;
}
int aos_queue_send(aos_queue_t *queue, void *msg, unsigned int size)
{
int ret;
if ((queue == NULL) || (msg == NULL)) {
return -EINVAL;
}
ret = krhino_buf_queue_send(queue->hdl, msg, size);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_queue_recv(aos_queue_t *queue, unsigned int ms, void *msg,
unsigned int *size)
{
int ret;
if (queue == NULL) {
return -EINVAL;
}
if (ms == AOS_WAIT_FOREVER) {
ret = krhino_buf_queue_recv(queue->hdl, RHINO_WAIT_FOREVER, msg, size);
} else {
ret = krhino_buf_queue_recv(queue->hdl, MS2TICK(ms), msg, size);
}
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_queue_is_valid(aos_queue_t *queue)
{
kbuf_queue_t *k_queue;
if (queue == NULL) {
return 0;
}
k_queue = queue->hdl;
if (k_queue == NULL) {
return 0;
}
return 1;
}
void *aos_queue_buf_ptr(aos_queue_t *queue)
{
if (!aos_queue_is_valid(queue)) {
return NULL;
}
return ((kbuf_queue_t *)queue->hdl)->buf;
}
#endif
#if (RHINO_CONFIG_TIMER > 0)
int aos_timer_new(aos_timer_t *timer, void (*fn)(void *, void *),
void *arg, int ms, int repeat)
{
kstat_t ret;
if (timer == NULL) {
return -EINVAL;
}
if (repeat == 0) {
ret = krhino_timer_dyn_create(((ktimer_t **)(&timer->hdl)), "AOS", (timer_cb_t)fn, MS2TICK(ms), 0, arg, 1);
} else {
ret = krhino_timer_dyn_create(((ktimer_t **)(&timer->hdl)), "AOS", (timer_cb_t)fn, MS2TICK(ms), MS2TICK(ms),
arg, 1);
}
if (ret != RHINO_SUCCESS) {
return ERRNO_MAPPING(ret);
}
return 0;
}
int aos_timer_new_ext(aos_timer_t *timer, void (*fn)(void *, void *),
void *arg, int ms, int repeat, unsigned char auto_run)
{
kstat_t ret;
if (timer == NULL) {
return -EINVAL;
}
if (repeat == 0) {
ret = krhino_timer_dyn_create(((ktimer_t **)(&timer->hdl)), "AOS", (timer_cb_t)fn, MS2TICK(ms), 0, arg, auto_run);
} else {
ret = krhino_timer_dyn_create(((ktimer_t **)(&timer->hdl)), "AOS", (timer_cb_t)fn, MS2TICK(ms), MS2TICK(ms),
arg, auto_run);
}
if (ret != RHINO_SUCCESS) {
return ERRNO_MAPPING(ret);
}
return 0;
}
void aos_timer_free(aos_timer_t *timer)
{
if (timer == NULL) {
return;
}
krhino_timer_dyn_del(timer->hdl);
timer->hdl = NULL;
}
int aos_timer_start(aos_timer_t *timer)
{
int ret;
if (timer == NULL) {
return -EINVAL;
}
ret = krhino_timer_start(timer->hdl);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_timer_stop(aos_timer_t *timer)
{
int ret;
if (timer == NULL) {
return -EINVAL;
}
ret = krhino_timer_stop(timer->hdl);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_timer_change(aos_timer_t *timer, int ms)
{
int ret;
if (timer == NULL) {
return -EINVAL;
}
ret = krhino_timer_change(timer->hdl, MS2TICK(ms), MS2TICK(ms));
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
#endif
#if (RHINO_CONFIG_WORKQUEUE > 0)
int aos_workqueue_create(aos_workqueue_t *workqueue, int pri, int stack_size)
{
kstat_t ret;
cpu_stack_t *stk;
kworkqueue_t *wq;
if (workqueue == NULL) {
return -EINVAL;
}
if (stack_size < sizeof(cpu_stack_t)) {
return -EINVAL;
}
stk = aos_malloc(stack_size);
if (stk == NULL) {
return -ENOMEM;
}
wq = aos_malloc(sizeof(kworkqueue_t));
if (wq == NULL) {
aos_free(stk);
return -ENOMEM;
}
ret = krhino_workqueue_create(wq, "AOS", pri, stk,
stack_size / sizeof(cpu_stack_t));
if (ret != RHINO_SUCCESS) {
aos_free(wq);
aos_free(stk);
return ERRNO_MAPPING(ret);
}
workqueue->hdl = wq;
workqueue->stk = stk;
return 0;
}
int aos_work_init(aos_work_t *work, void (*fn)(void *), void *arg, int dly)
{
kstat_t ret;
kwork_t *w;
if (work == NULL) {
return -EINVAL;
}
w = aos_malloc(sizeof(kwork_t));
if (w == NULL) {
return -ENOMEM;
}
ret = krhino_work_init(w, fn, arg, MS2TICK(dly));
if (ret != RHINO_SUCCESS) {
aos_free(w);
return ERRNO_MAPPING(ret);
}
work->hdl = w;
return 0;
}
void aos_work_destroy(aos_work_t *work)
{
kwork_t *w;
if (work == NULL) {
return;
}
w = work->hdl;
if (w->timer != NULL) {
krhino_timer_stop(w->timer);
krhino_timer_dyn_del(w->timer);
}
aos_free(work->hdl);
work->hdl = NULL;
}
int aos_work_run(aos_workqueue_t *workqueue, aos_work_t *work)
{
int ret;
if ((workqueue == NULL) || (work == NULL)) {
return -EINVAL;
}
ret = krhino_work_run(workqueue->hdl, work->hdl);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_work_sched(aos_work_t *work)
{
int ret;
if (work == NULL) {
return -EINVAL;
}
ret = krhino_work_sched(work->hdl);
if (ret == RHINO_SUCCESS) {
return 0;
}
return ERRNO_MAPPING(ret);
}
int aos_work_cancel(aos_work_t *work)
{
int ret;
if (work == NULL) {
return -EINVAL;
}
ret = krhino_work_cancel(work->hdl);
if (ret != RHINO_SUCCESS) {
return -EBUSY;
}
return 0;
}
#endif
#if (RHINO_CONFIG_MM_TLF > 0)
void *aos_zalloc(unsigned int size)
{
void *tmp = NULL;
if (size == 0) {
return NULL;
}
#if (RHINO_CONFIG_MM_DEBUG > 0u && RHINO_CONFIG_GCC_RETADDR > 0u)
if ((size & AOS_UNSIGNED_INT_MSB) == 0) {
tmp = krhino_mm_alloc(size | AOS_UNSIGNED_INT_MSB);
#ifndef AOS_BINS
#if defined (__CC_ARM)
krhino_owner_attach(g_kmm_head, tmp, __return_address());
#elif defined (__GNUC__)
krhino_owner_attach(g_kmm_head, tmp, (size_t)__builtin_return_address(0));
#endif /* __CC_ARM */
#endif
} else {
tmp = krhino_mm_alloc(size);
}
#else
tmp = krhino_mm_alloc(size);
#endif
if (tmp) {
memset(tmp, 0, size);
}
return tmp;
}
void *aos_malloc(unsigned int size)
{
void *tmp = NULL;
if (size == 0) {
return NULL;
}
#if (RHINO_CONFIG_MM_DEBUG > 0u && RHINO_CONFIG_GCC_RETADDR > 0u)
if ((size & AOS_UNSIGNED_INT_MSB) == 0) {
tmp = krhino_mm_alloc(size | AOS_UNSIGNED_INT_MSB);
#ifndef AOS_BINS
#if defined (__CC_ARM)
krhino_owner_attach(g_kmm_head, tmp, __return_address());
#elif defined (__GNUC__)
krhino_owner_attach(g_kmm_head, tmp, (size_t)__builtin_return_address(0));
#endif /* __CC_ARM */
#endif
} else {
tmp = krhino_mm_alloc(size);
}
#else
tmp = krhino_mm_alloc(size);
#endif
return tmp;
}
void *aos_realloc(void *mem, unsigned int size)
{
void *tmp = NULL;
#if (RHINO_CONFIG_MM_DEBUG > 0u && RHINO_CONFIG_GCC_RETADDR > 0u)
if ((size & AOS_UNSIGNED_INT_MSB) == 0) {
tmp = krhino_mm_realloc(mem, size | AOS_UNSIGNED_INT_MSB);
#ifndef AOS_BINS
#if defined (__CC_ARM)
krhino_owner_attach(g_kmm_head, tmp, __return_address());
#elif defined (__GNUC__)
krhino_owner_attach(g_kmm_head, tmp, (size_t)__builtin_return_address(0));
#endif /* __CC_ARM */
#endif
} else {
tmp = krhino_mm_realloc(mem, size);
}
#else
tmp = krhino_mm_realloc(mem, size);
#endif
return tmp;
}
void aos_alloc_trace(void *addr, size_t allocator)
{
#if (RHINO_CONFIG_MM_DEBUG > 0u && RHINO_CONFIG_GCC_RETADDR > 0u)
krhino_owner_attach(g_kmm_head, addr, allocator);
#endif
}
void aos_free(void *mem)
{
if (mem == NULL) {
return;
}
krhino_mm_free(mem);
}
#endif
long long aos_now(void)
{
return krhino_sys_time_get() * 1000 * 1000;
}
long long aos_now_ms(void)
{
return krhino_sys_time_get();
}
void aos_msleep(int ms)
{
krhino_task_sleep(MS2TICK(ms));
}
void aos_init(void)
{
krhino_init();
}
void aos_start(void)
{
krhino_start();
}

View file

@ -0,0 +1,13 @@
/*
* Copyright (C) 2015-2017 Alibaba Group Holding Limited
*/
#ifndef ERRNO_MAPPING_H
#define ERRNO_MAPPING_H
#include <errno.h>
#include <aos/aos.h>
#define ERRNO_MAPPING(ret) error_mapping(ret)
#endif /* ERRNO_MAPPING_H */