1 /**
2  * Copyright (c) 2015, Realsil Semiconductor Corporation. All rights reserved.
3  */
4 
5 #include <stdint.h>
6 #include <string.h>
7 #include <stdbool.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 
11 #include <mem_types.h>
12 #include "aos/kernel.h"
13 #include <aos/errno.h>
14 #include "k_api.h"
15 
16 
17 
18 #define HCI_OS_DBG
19 
os_mem_alloc_intern(RAM_TYPE ram_type,size_t size,const char * p_func,uint32_t file_line)20 void *os_mem_alloc_intern(RAM_TYPE ram_type, size_t size,
21                         const char *p_func, uint32_t file_line)
22 {
23 	HCI_OS_DBG("%s %d: %s\r\n",p_func, file_line, __func__);
24 
25 	return aos_malloc(size);
26 }
27 
os_mem_zalloc_intern(RAM_TYPE ram_type,size_t size,const char * p_func,uint32_t file_line)28 void *os_mem_zalloc_intern(RAM_TYPE ram_type, size_t size,
29 						const char *p_func, uint32_t file_line)
30 {
31 	HCI_OS_DBG("%s %d: %s\r\n",p_func, file_line, __func__);
32 	void *ptr = aos_zalloc(size);
33 
34     if (ptr == NULL) {
35         printf("os_mem_alloc_intern failed\n");
36     }
37 
38     return ptr;
39 }
40 
os_mem_free(void * p_block)41 void os_mem_free(void *p_block)
42 {
43 	aos_free(p_block);
44 }
45 
os_mem_peek(RAM_TYPE ram_type)46 size_t os_mem_peek(RAM_TYPE ram_type)
47 {
48 	extern k_mm_head *g_kmm_head;
49 	return g_kmm_head->free_size;
50 }
51 
52 typedef struct xqueue_desc_t {
53     aos_queue_t queue_hdl;
54     int message_size;
55     char *buf;
56 } xqueue_desc;
57 
os_msg_queue_create_intern(void ** pp_handle,uint32_t msg_num,uint32_t msg_size,const char * p_func,uint32_t file_line)58 bool os_msg_queue_create_intern(void **pp_handle, uint32_t msg_num, uint32_t msg_size,
59                                 const char *p_func, uint32_t file_line)
60 {
61 	HCI_OS_DBG("%s %d: %s\r\n",p_func, file_line, __func__);
62 	char *buf = aos_malloc(msg_size * msg_num);
63 
64     if (buf == NULL) {
65         return false;
66     }
67 
68     xqueue_desc *desc = aos_malloc(sizeof(xqueue_desc));
69     desc->message_size = msg_size;
70     desc->buf = buf;
71 
72     if (aos_queue_new(&desc->queue_hdl, buf, msg_size * msg_num, msg_size) != 0) {
73         aos_free(desc);
74         aos_free(buf);
75         return false;
76     }
77 
78     *pp_handle = desc;
79 	return true;
80 }
81 
os_msg_queue_delete_intern(void * p_handle,const char * p_func,uint32_t file_line)82 bool os_msg_queue_delete_intern(void *p_handle, const char *p_func, uint32_t file_line)
83 {
84 	HCI_OS_DBG("%s %d: %s\r\n",p_func, file_line, __func__);
85 	xqueue_desc *desc = (xqueue_desc *)p_handle;
86 
87     aos_queue_free(&desc->queue_hdl);
88     aos_free(desc->buf);
89     aos_free(desc);
90 
91 	return true;
92 }
93 
os_msg_send_intern(void * p_handle,void * p_msg,uint32_t wait_ms,const char * p_func,uint32_t file_line)94 bool os_msg_send_intern(void *p_handle, void *p_msg, uint32_t wait_ms,
95                         const char *p_func, uint32_t file_line)
96 {
97 	HCI_OS_DBG("%s %d: %s\r\n",p_func, file_line, __func__);
98 	 xqueue_desc *desc = (xqueue_desc *)p_handle;
99 
100     if (aos_queue_send(&desc->queue_hdl, p_msg, desc->message_size) == 0)
101         return true;
102 	else
103 		return false;
104 }
105 
os_msg_recv_intern(void * p_handle,void * p_msg,uint32_t wait_ms,const char * p_func,uint32_t file_line)106 bool os_msg_recv_intern(void *p_handle, void *p_msg, uint32_t wait_ms,
107                         const char *p_func, uint32_t file_line)
108 {
109 	HCI_OS_DBG("%s %d: %s\r\n",p_func, file_line, __func__);
110 	xqueue_desc *desc = (xqueue_desc *)p_handle;
111 
112     if (wait_ms == 0xFFFFFFFF)
113         wait_ms = 1000 * 1000 * 1000;
114 
115     if (aos_queue_recv(&desc->queue_hdl, wait_ms, p_msg, (size_t*)&desc->message_size) == 0)
116         return true;
117 	else
118 		return false;
119 }
120 
os_task_create(void ** pp_handle,const char * p_name,void (* p_routine)(void *),void * p_param,uint16_t stack_size,uint16_t priority)121 bool os_task_create(void **pp_handle, const char *p_name, void (*p_routine)(void *),
122 					void *p_param, uint16_t stack_size, uint16_t priority)
123 {
124 	int ret = 0;
125     ret = aos_task_new_ext((aos_task_t*)pp_handle, p_name, p_routine, p_param, stack_size * 4, AOS_DEFAULT_APP_PRI - priority + 8);
126 
127     if (ret != 0) {
128         printf("Create Task \"%s\" Failed! ret=%d\n", p_name, ret);
129     }
130 
131     HCI_OS_DBG("Create Task \"%s\"\n", p_name);
132 
133     if (ret == 0) {
134         return true;
135     } else {
136         return false;
137     }
138 }
139 
os_task_delete(void * p_handle)140 bool os_task_delete(void *p_handle)
141 {
142 	int ret = 0;
143 	ret = aos_task_delete(&p_handle);
144 	if (ret == 0) {
145         return true;
146     } else {
147         return false;
148     }
149 }
150 
os_delay(uint32_t ms)151 void os_delay(uint32_t ms)
152 {
153 	aos_msleep(ms);
154 }
155 
os_sem_create(void ** pp_handle,uint32_t init_count,uint32_t max_count)156 bool os_sem_create(void **pp_handle, uint32_t init_count, uint32_t max_count)
157 {
158 	aos_sem_t *aos_sema = aos_malloc(sizeof(aos_sem_t));
159     if (aos_sem_new(aos_sema, init_count) == 0) {
160 	    *pp_handle = aos_sema;
161 		return true;
162     } else
163 		return false;
164 }
165 
os_sem_delete(void * p_handle)166 bool os_sem_delete(void *p_handle)
167 {
168 	if (p_handle != NULL) {
169         aos_sem_free(p_handle);
170         aos_free(p_handle);
171     }
172 
173     p_handle = NULL;
174 	return true;
175 }
176 
os_sem_take(void * p_handle,uint32_t wait_ms)177 bool os_sem_take(void *p_handle, uint32_t wait_ms)
178 {
179 	if(wait_ms == 0xFFFFFFFF) {
180 		wait_ms = AOS_WAIT_FOREVER;
181 	}
182 
183 	if (aos_sem_wait(p_handle, wait_ms) == 0)
184     	return true;
185 	else
186 		return false;
187 }
188 
os_sem_give(void * p_handle)189 bool os_sem_give(void *p_handle)
190 {
191 	aos_sem_signal(p_handle);
192 	return true;
193 }
194