1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #ifndef __UVOICE_AMP_H__
6 #define __UVOICE_AMP_H__
7
8
9 #include "amp_platform.h"
10 #include "amp_kv.h"
11 #include "amp_fs.h"
12 #include "amp_defines.h"
13 #include "amp_system.h"
14 #include "wrappers_defs.h"
15
16 typedef void *os_dir_t;
17 typedef void *os_dirent_t;
18 typedef void *os_file_t;
19
20 typedef void *os_message_queue_t;
21 typedef void *os_mutex_t;
22 typedef void *os_sem_t;
23 typedef void *os_task_t;
24 typedef void *os_timer_t;
25
26
27 #define OS_SEEK_SET HAL_SEEK_SET
28 #define OS_SEEK_CUR HAL_SEEK_CUR
29 #define OS_SEEK_END HAL_SEEK_END
30
31 #define OS_F_OK 0
32 #define OS_X_OK 1
33 #define OS_W_OK 2
34 #define OS_R_OK 3
35
36 #define OS_FILE_OPEN_FAIL(stream) (stream == NULL)
37 #define OS_FILE_OPENING(stream) (stream != NULL)
38 #define OS_FILE_CLOSED (NULL)
39
40 #define OS_WAIT_FOREVER PLATFORM_WAIT_INFINITE
41
42 #define os_container_of(ptr, type, member) \
43 ((type *)((char *)(ptr) - offsetof(type, member)))
44
45 #define UVOICE_TASK_PRI_DEFAULT (amp_get_default_task_priority() - 1)
46 #define UVOICE_TASK_PRI_IDLE (amp_get_default_task_priority() - 1)
47 #define UVOICE_TASK_PRI_LOWEST (amp_get_default_task_priority() - 1)
48 #define UVOICE_TASK_PRI_LOWER (amp_get_default_task_priority() - 1)
49 #define UVOICE_TASK_PRI_NORMAL (amp_get_default_task_priority() - 1)
50 #define UVOICE_TASK_PRI_HIGHER (amp_get_default_task_priority() - 1)
51 #define UVOICE_TASK_PRI_HIGHEST (amp_get_default_task_priority() - 1)
52 #define UVOICE_TASK_PRI_REALTIME (amp_get_default_task_priority() - 1)
53
54
55 #ifdef UVOICE_BUILD_RELEASE
56 #define M_LOGD(fmt, ...)
57 #else
58 #define M_LOGD(fmt, ...) amp_printf("%s: "fmt, __func__, ##__VA_ARGS__)
59 #endif
60 #define M_LOGI(fmt, ...) amp_printf("%s: "fmt, __func__, ##__VA_ARGS__)
61 #define M_LOGW(fmt, ...) amp_printf("%s: "fmt, __func__, ##__VA_ARGS__)
62 #define M_LOGE(fmt, ...) amp_printf("%s: "fmt, __func__, ##__VA_ARGS__)
63 #define M_LOGR(fmt, ...) amp_printf(fmt, ##__VA_ARGS__)
64
65 #define AFM_MAIN 0x1
66 #define AFM_EXTN 0x2
67
snd_zalloc(size_t size,int flags)68 static inline void *snd_zalloc(size_t size, int flags)
69 {
70 void *mem = amp_malloc(size);
71 if (mem)
72 memset(mem, 0, size);
73 return mem;
74 }
75
snd_free(void * mem)76 static inline void snd_free(void *mem)
77 {
78 amp_free(mem);
79 }
80
snd_realloc(void * old,size_t newsize,int flags)81 static inline void *snd_realloc(void *old, size_t newsize, int flags)
82 {
83 void *mem = amp_realloc(old, newsize);
84 if (mem)
85 memset(mem, 0, newsize);
86 return mem;
87 }
88
os_msleep(int msec)89 static inline void os_msleep(int msec)
90 {
91 amp_msleep(msec);
92 }
93
os_usleep(int usec)94 static inline void os_usleep(int usec)
95 {
96 amp_msleep(usec / 1000);
97 }
98
os_current_time(void)99 static inline long long os_current_time(void)
100 {
101 return amp_uptime();
102 }
103
os_get_mac_address(char * mac)104 static inline int os_get_mac_address(char *mac)
105 {
106 return 0;
107 }
108
109
os_kv_get(const char * key,void * buffer,int * len)110 static inline int os_kv_get(const char *key, void *buffer, int *len)
111 {
112 return amp_kv_get(key, buffer, len);
113 }
114
os_kv_set(const char * key,const void * buffer,int len,int sync)115 static inline int os_kv_set(const char *key, const void *buffer, int len, int sync)
116 {
117 return amp_kv_set(key, buffer, len, sync);
118 }
119
os_mkdir(const char * path)120 static inline int os_mkdir(const char *path)
121 {
122 return amp_mkdir(path);
123 }
124
os_opendir(const char * path)125 static inline os_dir_t *os_opendir(const char *path)
126 {
127 return -1;
128 }
129
os_readdir(os_dir_t * dir)130 static inline os_dirent_t *os_readdir(os_dir_t *dir)
131 {
132 return -1;
133 }
134
os_closedir(os_dir_t * dir)135 static inline int os_closedir(os_dir_t *dir)
136 {
137 return -1;
138 }
139
os_fopen(const char * filename,const char * mode)140 static inline os_file_t os_fopen(const char *filename, const char *mode)
141 {
142 return amp_fopen(filename, mode);
143 }
144
os_fread(void * buffer,size_t size,size_t count,os_file_t fp)145 static inline size_t os_fread(void *buffer, size_t size, size_t count, os_file_t fp)
146 {
147 return amp_fread(buffer, size, count, fp);
148 }
149
os_fwrite(const void * buffer,size_t size,size_t count,os_file_t fp)150 static inline size_t os_fwrite(const void *buffer, size_t size, size_t count, os_file_t fp)
151 {
152 return amp_fwrite(buffer, size, count, fp);
153 }
154
os_ftell(os_file_t fp)155 static inline long os_ftell(os_file_t fp)
156 {
157 return amp_ftell(fp);
158 }
159
os_fseek(os_file_t fp,long offset,int whence)160 static inline long os_fseek(os_file_t fp, long offset, int whence)
161 {
162 int current_pos = -1;
163 return amp_fseek(fp, offset, whence, ¤t_pos);
164 }
165
os_fgets(char * buffer,int size,os_file_t fp)166 static inline char *os_fgets(char *buffer, int size, os_file_t fp)
167 {
168 return 0;
169 }
170
os_fprintf(os_file_t fp,const char * format,...)171 static inline int os_fprintf(os_file_t fp, const char *format, ...)
172 {
173 int ret;
174 va_list args;
175 va_start(args, format);
176 ret = vfprintf(fp, format, args);
177 va_end(args);
178 return ret;
179 }
180
os_feof(os_file_t fp)181 static inline int os_feof(os_file_t fp)
182 {
183 return 0;
184 }
185
os_ferror(os_file_t fp)186 static inline int os_ferror(os_file_t fp)
187 {
188 return 0;
189 }
190
os_fclose(os_file_t fp)191 static inline int os_fclose(os_file_t fp)
192 {
193 return amp_fclose(fp);
194 }
195
os_access(const char * filename,int mode)196 static inline int os_access(const char *filename, int mode)
197 {
198 os_file_t fp = os_fopen(filename, "r");
199 if (!fp)
200 return -1;
201 os_fclose(fp);
202 return 0;
203 }
204
os_remove(const char * filename)205 static inline int os_remove(const char *filename)
206 {
207 return amp_remove(filename);
208 }
209
os_message_queue_send(os_message_queue_t mq,void * msg,unsigned int size,unsigned int timeout)210 static inline int os_message_queue_send(os_message_queue_t mq, void *msg,
211 unsigned int size, unsigned int timeout)
212 {
213 return amp_queue_send(mq, msg, size, timeout);
214 }
215
os_message_queue_recv(os_message_queue_t mq,void * msg,unsigned int size,unsigned int timeout)216 static inline int os_message_queue_recv(os_message_queue_t mq, void *msg,
217 unsigned int size, unsigned int timeout)
218 {
219 return amp_queue_recv(mq, msg, size, timeout);
220 }
221
os_message_queue_create(int count,int size)222 static inline os_message_queue_t os_message_queue_create(int count, int size)
223 {
224 return amp_queue_create(count, size);
225 }
226
os_message_queue_free(os_message_queue_t mq)227 static inline int os_message_queue_free(os_message_queue_t mq)
228 {
229 return amp_queue_delete(mq);
230 }
231
os_event_post(uint16_t type,uint16_t code,int value)232 static inline int os_event_post(uint16_t type, uint16_t code, int value)
233 {
234 return -1;
235 }
236
os_event_register(uint16_t type,void * cb,void * data)237 static inline int os_event_register(uint16_t type, void *cb, void *data)
238 {
239 return -1;
240 }
241
os_event_unregister(uint16_t type,void * cb,void * data)242 static inline int os_event_unregister(uint16_t type, void *cb, void *data)
243 {
244 return -1;
245 }
246
os_mutex_lock(os_mutex_t mutex,unsigned int timeout)247 static inline int os_mutex_lock(os_mutex_t mutex, unsigned int timeout)
248 {
249 amp_mutex_lock(mutex);
250 return 0;
251 }
252
os_mutex_unlock(os_mutex_t mutex)253 static inline int os_mutex_unlock(os_mutex_t mutex)
254 {
255 amp_mutex_unlock(mutex);
256 return 0;
257 }
258
os_mutex_new(void)259 static inline os_mutex_t os_mutex_new(void)
260 {
261 return amp_mutex_create();
262 }
263
os_mutex_free(os_mutex_t mutex)264 static inline void os_mutex_free(os_mutex_t mutex)
265 {
266 amp_mutex_destroy(mutex);
267 }
268
os_sem_is_valid(os_sem_t sem)269 static inline int os_sem_is_valid(os_sem_t sem)
270 {
271 return 1;
272 }
273
os_sem_wait(os_sem_t sem,unsigned int timeout)274 static inline int os_sem_wait(os_sem_t sem, unsigned int timeout)
275 {
276 return amp_semaphore_wait(sem, timeout);
277 }
278
os_sem_signal(os_sem_t sem)279 static inline void os_sem_signal(os_sem_t sem)
280 {
281 amp_semaphore_post(sem);
282 }
283
os_sem_signal_all(os_sem_t sem)284 static inline void os_sem_signal_all(os_sem_t sem)
285 {
286 amp_semaphore_post(sem);
287 }
288
os_sem_new(int count)289 static inline os_sem_t os_sem_new(int count)
290 {
291 return amp_semaphore_create();
292 }
293
os_sem_free(os_sem_t sem)294 static inline void os_sem_free(os_sem_t sem)
295 {
296 amp_semaphore_destroy(sem);
297 }
298
os_timer_change(os_timer_t timer,int internal_ms)299 static inline int os_timer_change(os_timer_t timer, int internal_ms)
300 {
301 return -1;
302 }
303
os_timer_start(os_timer_t timer)304 static inline int os_timer_start(os_timer_t timer)
305 {
306 return -1;
307 }
308
os_timer_stop(os_timer_t timer)309 static inline int os_timer_stop(os_timer_t timer)
310 {
311 return -1;
312 }
313
os_timer_new(void (* func)(void *,void *),void * arg,int internal_ms,int repeat,unsigned char auto_run)314 static inline os_timer_t os_timer_new(void (*func)(void *, void *), void *arg,
315 int internal_ms, int repeat, unsigned char auto_run)
316 {
317 return NULL;
318 }
319
os_timer_free(os_timer_t timer)320 static inline void os_timer_free(os_timer_t timer)
321 {
322 }
323
os_task_create(os_task_t * task,const char * name,void * (* fn)(void *),void * arg,int stack_size,int pri)324 static inline int os_task_create(os_task_t *task, const char *name,
325 void *(*fn)(void *), void *arg, int stack_size, int pri)
326 {
327 amp_os_thread_param_t task_params = {0};
328 int stack_used = stack_size;
329 task_params.name = name;
330 task_params.priority = pri;
331 task_params.stack_size = stack_size;
332 return amp_thread_create(task, fn, arg, &task_params, &stack_used);
333 }
334
os_task_exit(os_task_t task)335 static inline int os_task_exit(os_task_t task)
336 {
337 return amp_thread_delete(task);
338 }
339
os_partition_name(int pt)340 static inline const char *os_partition_name(int pt)
341 {
342 return NULL;
343 }
344
os_partition_size(int pt)345 static inline int os_partition_size(int pt)
346 {
347 return -1;
348 }
349
os_partition_read(int pt,uint32_t * offset,uint8_t * buffer,uint32_t len)350 static inline int os_partition_read(int pt, uint32_t *offset, uint8_t *buffer, uint32_t len)
351 {
352 return -1;
353 }
354
os_partition_write(int pt,uint32_t * offset,const uint8_t * buffer,uint32_t len)355 static inline int os_partition_write(int pt, uint32_t *offset, const uint8_t *buffer , uint32_t len)
356 {
357 return -1;
358 }
359
os_partition_erase(int pt,uint32_t offset,uint32_t len)360 static inline int os_partition_erase(int pt, uint32_t offset, uint32_t len)
361 {
362 return -1;
363 }
364
365
366 #endif /* __UVOICE_AMP_H__ */
367