1 /*
2  * Copyright (C) 2015-2017 Alibaba Group Holding Limited
3  */
4 
5 #include <ble_os.h>
6 #include <misc/util.h>
7 #include <misc/dlist.h>
8 
9 //#include <aos/aos.h> //yulong removed
10 // #include "aos/debug.h"
11 
12 #define BT_DBG_ENABLED IS_ENABLED(CONFIG_BLUETOOTH_DEBUG_CORE)
13 
14 #include <common/log.h>
15 #include <stddef.h>
16 #include <stdint.h>
17 #include <string.h>
18 #include "atomic.h"
19 #define OSAL_RHINO
20 
21 #ifdef OSAL_RHINO
22 #include <k_default_config.h>
23 #include <k_types.h>
24 #include <k_err.h>
25 #include <k_sys.h>
26 #include <k_list.h>
27 #include <k_ringbuf.h>
28 #include <k_obj.h>
29 #include <k_sem.h>
30 #include <k_queue.h>
31 #include <k_buf_queue.h>
32 #include <k_stats.h>
33 #include <k_time.h>
34 #include <k_task.h>
35 #include <port.h>
36 #include "bt_errno.h"
37 #endif
38 
k_queue_init(struct k_queue * queue)39 void k_queue_init(struct k_queue *queue)
40 {
41     int     stat;
42 
43     stat = krhino_sem_create(&queue->sem, "ble", 0);
44 
45     if (stat) {
46         //SYS_LOG_ERR("buf queue exhausted\n");
47     }
48 
49     sys_slist_init(&queue->queue_list);
50     sys_dlist_init(&queue->poll_events);
51 }
52 
handle_poll_events(struct k_queue * queue,bt_u32_t state)53 static inline void handle_poll_events(struct k_queue *queue, bt_u32_t state)
54 {
55     _handle_obj_poll_events(&queue->poll_events, state);
56 }
57 
k_queue_cancel_wait(struct k_queue * queue)58 void k_queue_cancel_wait(struct k_queue *queue)
59 {
60     krhino_sem_give(&queue->sem);
61     handle_poll_events(queue, K_POLL_STATE_NOT_READY);
62 }
63 
k_queue_insert(struct k_queue * queue,void * prev,void * node)64 void k_queue_insert(struct k_queue *queue, void *prev, void *node)
65 {
66     unsigned int key;
67     key = irq_lock();
68 
69     sys_slist_append(&queue->queue_list, node);
70     irq_unlock(key);
71     krhino_sem_give(&queue->sem);
72     handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
73 }
74 
k_now_ms()75 long long k_now_ms()
76 {
77     return aos_now_ms();
78 }
79 
k_queue_append(struct k_queue * queue,void * data)80 void k_queue_append(struct k_queue *queue, void *data)
81 {
82     k_queue_insert(queue, NULL, data);
83 }
84 
k_queue_prepend(struct k_queue * queue,void * data)85 void k_queue_prepend(struct k_queue *queue, void *data)
86 {
87     k_queue_insert(queue, NULL, data);
88 }
89 
k_queue_append_list(struct k_queue * queue,void * head,void * tail)90 void k_queue_append_list(struct k_queue *queue, void *head, void *tail)
91 {
92     struct net_buf *buf_tail = (struct net_buf *)head;
93 
94     for (buf_tail = (struct net_buf *)head; buf_tail;
95          buf_tail = buf_tail->frags) {
96         k_queue_append(queue, buf_tail);
97     }
98 
99     handle_poll_events(queue, K_POLL_STATE_DATA_AVAILABLE);
100 }
101 
k_queue_get(struct k_queue * queue,int32_t timeout)102 void *k_queue_get(struct k_queue *queue, int32_t timeout)
103 {
104     int ret;
105     void        *msg = NULL;
106     tick_t       ticks;
107 
108     if (timeout == K_FOREVER) {
109         ticks = RHINO_WAIT_FOREVER;
110     } else {
111         ticks = krhino_ms_to_ticks(timeout);
112     }
113 
114     ret = krhino_sem_take(&queue->sem, ticks);
115 
116     if (ret) {
117         return NULL;
118     } else {
119         unsigned int key;
120         key = irq_lock();
121         msg =  sys_slist_get(&queue->queue_list);
122         irq_unlock(key);
123         return msg;
124     }
125 }
126 
k_queue_is_empty(struct k_queue * queue)127 int k_queue_is_empty(struct k_queue *queue)
128 {
129     return (int)sys_slist_is_empty(&queue->queue_list);
130 }
131 
k_queue_count(struct k_queue * queue)132 int k_queue_count(struct k_queue *queue)
133 {
134     return queue->sem.count;
135 }
136 
k_sem_init(struct k_sem * sem,unsigned int initial_count,unsigned int limit)137 int k_sem_init(struct k_sem *sem, unsigned int initial_count,
138                unsigned int limit)
139 {
140     int ret;
141 
142     if (NULL == sem) {
143         BT_ERR("sem is NULL\n");
144         return -EINVAL;
145     }
146 
147     ret = krhino_sem_create(&sem->sem, "ble", initial_count);
148 	if (ret != 0)
149 		BT_ERR("%s failed: %d", __func__, ret);
150     sys_dlist_init(&sem->poll_events);
151     return ret;
152 }
153 
k_sem_take(struct k_sem * sem,uint32_t timeout)154 int k_sem_take(struct k_sem *sem, uint32_t timeout)
155 {
156     tick_t ticks;
157 	int ret;
158 
159     if (timeout == K_FOREVER) {
160         ticks = RHINO_WAIT_FOREVER;
161     } else {
162         ticks = krhino_ms_to_ticks(timeout);
163     }
164 
165     ret = krhino_sem_take(&sem->sem, ticks);
166     if (ret != 0) {
167         // aos_debug_backtrace_now(NULL);
168 		BT_ERR("%s failed: %d", __func__, ret);
169     }
170 	return ret;
171 }
172 
k_sem_give(struct k_sem * sem)173 int k_sem_give(struct k_sem *sem)
174 {
175 	int ret;
176     if (NULL == sem) {
177         BT_ERR("sem is NULL\n");
178         return -EINVAL;
179     }
180 
181     ret = krhino_sem_give(&sem->sem);
182     if (ret != 0) {
183         BT_ERR("%s failed: %d", __func__, ret);
184     }
185 
186     return 0;
187 }
188 
k_sem_delete(struct k_sem * sem)189 int k_sem_delete(struct k_sem *sem)
190 {
191     if (NULL == sem) {
192         BT_ERR("sem is NULL\n");
193         return -EINVAL;
194     }
195 
196     krhino_sem_del(&sem->sem);
197     return 0;
198 }
199 
k_sem_count_get(struct k_sem * sem)200 unsigned int k_sem_count_get(struct k_sem *sem)
201 {
202 #ifdef OSAL_RHINO
203     sem_count_t count;
204 
205     krhino_sem_count_get(&sem->sem, &count);
206     return (int)count;
207 #endif
208     return 0;
209 }
210 
211 
k_mutex_init(struct k_mutex * mutex)212 void k_mutex_init(struct k_mutex *mutex)
213 {
214     int stat;
215 
216     if (NULL == mutex) {
217         BT_ERR("mutex is NULL\n");
218         return;
219     }
220 
221     stat = krhino_mutex_create(&mutex->mutex, "ble");
222 
223     if (stat) {
224         BT_ERR("mutex buffer over\n");
225     }
226 
227     sys_dlist_init(&mutex->poll_events);
228     return;
229 }
230 
k_mutex_lock(struct k_mutex * mutex,bt_s32_t timeout)231 int k_mutex_lock(struct k_mutex *mutex, bt_s32_t timeout)
232 {
233     tick_t ticks;
234 	int ret;
235 
236     if (timeout == K_FOREVER) {
237         ticks = RHINO_WAIT_FOREVER;
238     } else {
239         ticks = krhino_ms_to_ticks(timeout);
240     }
241 
242     ret =  krhino_mutex_lock(&mutex->mutex, ticks);
243     if (ret != 0) {
244 		BT_ERR("%s failed: %d", __func__, ret);
245     }
246 	return ret;
247 }
248 
k_mutex_unlock(struct k_mutex * mutex)249 void k_mutex_unlock(struct k_mutex *mutex)
250 {
251 	int ret;
252     if (NULL == mutex) {
253         BT_ERR("mutex is NULL\n");
254         return;
255     }
256 
257     ret = krhino_mutex_unlock(&mutex->mutex);
258     if (ret != 0) {
259 		BT_ERR("%s failed: %d", __func__, ret);
260     }
261 }
262 
k_uptime_get()263 int64_t k_uptime_get()
264 {
265     return krhino_sys_time_get();
266 }
267 
k_uptime_get_32()268 uint32_t k_uptime_get_32()
269 {
270     return (uint32_t)krhino_sys_time_get();
271 }
272 
k_thread_spawn(struct k_thread * thread,const char * name,uint32_t * stack,uint32_t stack_size,k_thread_entry_t fn,void * arg,int prio)273 int k_thread_spawn(struct k_thread *thread, const char *name, uint32_t *stack, uint32_t stack_size, \
274                    k_thread_entry_t fn, void *arg, int prio)
275 {
276     int ret;
277 
278     if (stack == NULL || thread == NULL) {
279         BT_ERR("thread || stack is NULL");
280         return -1;
281     }
282 
283     ret = krhino_task_create(&thread->task, name, arg, prio, 0, stack, stack_size, fn, 1);
284 
285     if (ret) {
286         BT_ERR("creat task %s fail %d\n", name, ret);
287         return ret;
288     }
289 
290     return ret;
291 }
292 
293 
k_yield(void)294 int k_yield(void)
295 {
296     return 0;
297 }
298 
irq_lock(void)299 unsigned int irq_lock(void)
300 {
301     CPSR_ALLOC();
302     RHINO_CPU_INTRPT_DISABLE();
303     return cpsr;
304 }
305 
irq_unlock(unsigned int key)306 void irq_unlock(unsigned int key)
307 {
308     CPSR_ALLOC();
309     cpsr = key;
310     RHINO_CPU_INTRPT_ENABLE();
311 }
312 
_SysFatalErrorHandler(unsigned int reason,const void * pEsf)313 void _SysFatalErrorHandler(unsigned int reason, const void *pEsf) {};
314 
k_timer_init(k_timer_t * timer,k_timer_handler_t handle,void * args)315 void k_timer_init(k_timer_t *timer, k_timer_handler_t handle, void *args)
316 {
317     int ret;
318     ASSERT(timer, "timer is NULL");
319     BT_DBG("timer %p,handle %p,args %p", timer, handle, args);
320     timer->handler = handle;
321     timer->args    = args;
322     timer->timeout  = 0;
323     ret =
324         krhino_timer_create(&timer->timer, "AOS", (timer_cb_t)(timer->handler),
325                             krhino_ms_to_ticks(1000), 0, args, 0);
326 
327     if (ret) {
328         BT_DBG("fail to create a timer");
329     }
330 }
331 
k_timer_start(k_timer_t * timer,uint32_t timeout)332 void k_timer_start(k_timer_t *timer, uint32_t timeout)
333 {
334     int ret;
335     ASSERT(timer, "timer is NULL");
336     BT_DBG("timer %p,timeout %u", timer, timeout);
337     timer->timeout  = timeout;
338     timer->start_ms = aos_now_ms();
339 
340     ret = krhino_timer_stop(&timer->timer);
341 
342     if (ret) {
343         BT_ERR("fail to stop timer");
344     }
345 
346     ret = krhino_timer_change(&timer->timer, krhino_ms_to_ticks(timeout), 0);
347 
348     if (ret) {
349         BT_ERR("fail to change timeout");
350     }
351 
352     ret = krhino_timer_start(&timer->timer);
353 
354     if (ret) {
355         BT_ERR("fail to start timer");
356     }
357 }
358 
k_timer_stop(k_timer_t * timer)359 void k_timer_stop(k_timer_t *timer)
360 {
361     int ret;
362     ASSERT(timer, "timer is NULL");
363 
364     /**
365      * Timer may be reused, so its timeout value
366      * should be cleared when stopped.
367     */
368     if (!timer->timeout) {
369         return;
370     }
371 
372     BT_DBG("timer %p", timer);
373     ret = krhino_timer_stop(&timer->timer);
374 
375     if (ret) {
376         BT_ERR("fail to stop timer");
377     }
378 
379     timer->timeout = 0;
380 }
381 
k_sleep(int32_t duration)382 void k_sleep(int32_t duration)
383 {
384     aos_msleep(duration);
385 }
386 
k_current_get(void)387 void *k_current_get(void)
388 {
389     void *task;
390     task = krhino_cur_task_get();
391     return task;
392 }
393 
k_timer_is_started(k_timer_t * timer)394 bool k_timer_is_started(k_timer_t *timer)
395 {
396     ASSERT(timer, "timer is NULL");
397 
398     return timer->timeout ? true : false;
399 }
400 
log_strdup(const char * str)401 const char *log_strdup(const char *str)
402 {
403     return str;
404 }
405 
k_malloc(bt_u32_t size)406 void *k_malloc(bt_u32_t size)
407 {
408     return aos_malloc(size);
409 }
410 
411 
412 
413