1 /*
2 * Copyright (C) 2015-2020 Alibaba Group Holding Limited
3 */
4
5 #ifndef __UVOICE_LINUX_H__
6 #define __UVOICE_LINUX_H__
7
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stddef.h>
11 #include <stdarg.h>
12 #include <errno.h>
13 #include <unistd.h>
14 #include <sys/stat.h>
15 #include <sys/types.h>
16 #include <sys/time.h>
17 #include <sys/ipc.h>
18 #include <sys/msg.h>
19 #include <dirent.h>
20 #include <semaphore.h>
21 #include <pthread.h>
22
23 typedef DIR os_dir_t;
24 typedef struct dirent os_dirent_t;
25 typedef FILE * os_file_t;
26
27 typedef int os_queue_t;
28 typedef pthread_mutex_t * os_mutex_t;
29 typedef sem_t * os_sem_t;
30 typedef pthread_t os_task_t;
31 typedef struct timer_list * os_timer_t;
32
33
34 #define OS_SEEK_SET SEEK_SET
35 #define OS_SEEK_CUR SEEK_CUR
36 #define OS_SEEK_END SEEK_END
37
38 #define OS_F_OK F_OK
39 #define OS_X_OK X_OK
40 #define OS_W_OK W_OK
41 #define OS_R_OK R_OK
42
43 #define OS_FILE_OPEN_FAIL(stream) (stream == NULL)
44 #define OS_FILE_OPENING(stream) (stream != NULL)
45 #define OS_FILE_CLOSED (NULL)
46
47 #define OS_WAIT_FOREVER 0xffffffffu
48
49 #define os_container_of(ptr, type, member) \
50 ((type *)((char *)(ptr) - offsetof(type, member)))
51
52 enum {
53 UVOICE_TASK_PRI_DEFAULT = 0,
54 UVOICE_TASK_PRI_IDLE,
55 UVOICE_TASK_PRI_LOWEST,
56 UVOICE_TASK_PRI_LOWER,
57 UVOICE_TASK_PRI_NORMAL,
58 UVOICE_TASK_PRI_HIGHER,
59 UVOICE_TASK_PRI_HIGHEST,
60 UVOICE_TASK_PRI_REALTIME,
61 };
62
63
64 #ifdef UVOICE_BUILD_RELEASE
65 #define M_LOGD(fmt, ...)
66 #else
67 #define M_LOGD(fmt, ...) printf("%s: "fmt, __func__, ##__VA_ARGS__)
68 #endif
69 #define M_LOGI(fmt, ...) printf("%s: "fmt, __func__, ##__VA_ARGS__)
70 #define M_LOGW(fmt, ...) printf("%s: "fmt, __func__, ##__VA_ARGS__)
71 #define M_LOGE(fmt, ...) printf("%s: "fmt, __func__, ##__VA_ARGS__)
72 #define M_LOGR(fmt, ...) printf(fmt, ##__VA_ARGS__)
73
74 #define AFM_MAIN 0x1
75 #define AFM_EXTN 0x2
76
snd_zalloc(size_t size,int flags)77 static inline void *snd_zalloc(size_t size, int flags)
78 {
79 void *mem = malloc(size);
80 if (mem)
81 memset(mem, 0, size);
82 return mem;
83 }
84
snd_free(void * mem)85 static inline void snd_free(void *mem)
86 {
87 free(mem);
88 }
89
snd_realloc(void * old,size_t newsize,int flags)90 static inline void *snd_realloc(void *old, size_t newsize, int flags)
91 {
92 void *mem = realloc(old, newsize);
93 if (mem)
94 memset(mem, 0, newsize);
95 return mem;
96 }
97
os_msleep(int msec)98 static inline void os_msleep(int msec)
99 {
100 usleep(msec * 1000);
101 }
102
os_usleep(int usec)103 static inline void os_usleep(int usec)
104 {
105 usleep(usec);
106 }
107
os_current_time(void)108 static inline long long os_current_time(void)
109 {
110 struct timeval tt;
111 gettimeofday(&tt, NULL);
112 return tt.tv_sec * 1000 + tt.tv_usec / 1000;
113 }
114
os_get_mac_address(char * mac)115 static inline int os_get_mac_address(char *mac)
116 {
117 return 0;
118 }
119
120
os_kv_get(const char * key,void * buffer,int * len)121 static inline int os_kv_get(const char *key, void *buffer, int *len)
122 {
123 FILE *fp = fopen(key, "rb");
124 if (!fp)
125 return -ENOENT;
126 fread(buffer, *len, 1, fp);
127 if (ferror(fp))
128 return -EIO;
129 fclose(fp);
130 return 0;
131 }
132
os_kv_set(const char * key,const void * buffer,int len,int sync)133 static inline int os_kv_set(const char *key, const void *buffer, int len, int sync)
134 {
135 FILE *fp = fopen(key, "wb+");
136 if (!fp)
137 return -ENOENT;
138 fwrite(buffer, len, 1, fp);
139 if (ferror(fp))
140 return -EIO;
141 fclose(fp);
142 return 0;
143 }
144
os_mkdir(const char * path)145 static inline int os_mkdir(const char *path)
146 {
147 return mkdir(path, S_IRWXU); /* posix api, <dirent.h> required */
148 }
149
os_opendir(const char * path)150 static inline os_dir_t *os_opendir(const char *path)
151 {
152 return opendir(path);
153 }
154
os_readdir(os_dir_t * dir)155 static inline os_dirent_t *os_readdir(os_dir_t *dir)
156 {
157 return readdir(dir);
158 }
159
os_closedir(os_dir_t * dir)160 static inline int os_closedir(os_dir_t *dir)
161 {
162 return closedir(dir);
163 }
164
os_access(const char * filename,int mode)165 static inline int os_access(const char *filename, int mode)
166 {
167 return access(filename, mode);
168 }
169
os_fopen(const char * filename,const char * mode)170 static inline os_file_t os_fopen(const char *filename, const char *mode)
171 {
172 FILE *fp = fopen(filename, mode);
173 return (os_file_t)fp;
174 }
175
os_fread(void * buffer,size_t size,size_t count,os_file_t fp)176 static inline size_t os_fread(void *buffer, size_t size, size_t count, os_file_t fp)
177 {
178 return fread(buffer, size, count, fp);
179 }
180
os_fwrite(const void * buffer,size_t size,size_t count,os_file_t fp)181 static inline size_t os_fwrite(const void *buffer, size_t size, size_t count, os_file_t fp)
182 {
183 return fwrite(buffer, size, count, fp);
184 }
185
os_ftell(os_file_t fp)186 static inline long os_ftell(os_file_t fp)
187 {
188 return ftell(fp);
189 }
190
os_fseek(os_file_t fp,long offset,int whence)191 static inline long os_fseek(os_file_t fp, long offset, int whence)
192 {
193 return fseek(fp, offset, whence);
194 }
195
os_fgets(char * buffer,int size,os_file_t fp)196 static inline char *os_fgets(char *buffer, int size, os_file_t fp)
197 {
198 return fgets(buffer, size, fp);
199 }
200
os_fprintf(os_file_t fp,const char * format,...)201 static inline int os_fprintf(os_file_t fp, const char *format, ...)
202 {
203 int ret;
204 va_list args;
205 va_start(args, format);
206 ret = vfprintf(fp, format, args);
207 va_end(args);
208 return ret;
209 }
210
os_feof(os_file_t fp)211 static inline int os_feof(os_file_t fp)
212 {
213 return feof(fp);
214 }
215
os_ferror(os_file_t fp)216 static inline int os_ferror(os_file_t fp)
217 {
218 return ferror(fp);
219 }
220
os_fclose(os_file_t fp)221 static inline int os_fclose(os_file_t fp)
222 {
223 return fclose(fp);
224 }
225
os_remove(const char * filename)226 static inline int os_remove(const char *filename)
227 {
228 return remove(filename);
229 }
230
os_queue_send(os_queue_t * queue,void * msg,unsigned int size)231 static inline int os_queue_send(os_queue_t *queue, void *msg, unsigned int size)
232 {
233 return -1;
234 }
235
os_queue_recv(os_queue_t * queue,unsigned int ms,void * msg,unsigned int * size)236 static inline int os_queue_recv(os_queue_t *queue, unsigned int ms, void *msg,
237 unsigned int *size)
238 {
239 return -1;
240 }
241
os_queue_new(os_queue_t * queue,void * buffer,unsigned int size,int msg_size)242 static inline int os_queue_new(os_queue_t *queue, void *buffer, unsigned int size, int msg_size)
243 {
244 return -1;
245 }
246
os_queue_free(os_queue_t * queue)247 static inline void os_queue_free(os_queue_t *queue)
248 {
249 }
250
os_event_post(uint16_t type,uint16_t code,int value)251 static inline int os_event_post(uint16_t type, uint16_t code, int value)
252 {
253 return -1;
254 }
255
os_event_register(uint16_t type,void * cb,void * data)256 static inline int os_event_register(uint16_t type, void *cb, void *data)
257 {
258 return -1;
259 }
260
os_event_unregister(uint16_t type,void * cb,void * data)261 static inline int os_event_unregister(uint16_t type, void *cb, void *data)
262 {
263 return -1;
264 }
265
os_mutex_lock(os_mutex_t mutex,unsigned int timeout)266 static inline int os_mutex_lock(os_mutex_t mutex, unsigned int timeout)
267 {
268 return pthread_mutex_lock(mutex);
269 }
270
os_mutex_unlock(os_mutex_t mutex)271 static inline int os_mutex_unlock(os_mutex_t mutex)
272 {
273 return pthread_mutex_unlock(mutex);
274 }
275
os_mutex_new(void)276 static inline os_mutex_t os_mutex_new(void)
277 {
278 pthread_mutex_t *p_mutex = snd_zalloc(sizeof(pthread_mutex_t), AFM_EXTN);
279 if (!p_mutex)
280 return NULL;
281 if (pthread_mutex_init(p_mutex, NULL)) {
282 snd_free(p_mutex);
283 return NULL;
284 }
285 return p_mutex;
286 }
287
os_mutex_free(os_mutex_t mutex)288 static inline void os_mutex_free(os_mutex_t mutex)
289 {
290 pthread_mutex_destroy(mutex);
291 snd_free(mutex);
292 }
293
os_sem_is_valid(os_sem_t sem)294 static inline int os_sem_is_valid(os_sem_t sem)
295 {
296 return 1;
297 }
298
os_sem_wait(os_sem_t sem,unsigned int timeout)299 static inline int os_sem_wait(os_sem_t sem, unsigned int timeout)
300 {
301 struct timespec ts;
302 struct timeval tt;
303 gettimeofday(&tt, NULL);
304 ts.tv_sec = tt.tv_sec + timeout / 1000;
305 ts.tv_nsec = tt.tv_usec * 1000 + (timeout % 1000) * 1000 * 1000;
306 ts.tv_sec += ts.tv_nsec / (1000 * 1000 * 1000);
307 ts.tv_nsec %= (1000 * 1000 * 1000);
308 if (timeout == OS_WAIT_FOREVER)
309 return sem_wait(sem);
310 return sem_timedwait(sem, &ts);
311 }
312
os_sem_signal(os_sem_t sem)313 static inline void os_sem_signal(os_sem_t sem)
314 {
315 sem_post(sem);
316 }
317
os_sem_signal_all(os_sem_t sem)318 static inline void os_sem_signal_all(os_sem_t sem)
319 {
320 //TODO: Fix
321 sem_post(sem);
322 }
323
os_sem_new(int count)324 static inline os_sem_t os_sem_new(int count)
325 {
326 sem_t *sem = snd_zalloc(sizeof(sem_t), AFM_MAIN);
327 if (!sem)
328 return NULL;
329 if (sem_init(sem, 0, count)) {
330 snd_free(sem);
331 return NULL;
332 }
333 return sem;
334 }
335
os_sem_free(os_sem_t sem)336 static inline void os_sem_free(os_sem_t sem)
337 {
338 sem_destroy(sem);
339 snd_free(sem);
340 }
341
os_timer_change(os_timer_t timer,int internal_ms)342 static inline int os_timer_change(os_timer_t timer, int internal_ms)
343 {
344 return -1;
345 }
346
os_timer_start(os_timer_t timer)347 static inline int os_timer_start(os_timer_t timer)
348 {
349 return -1;
350 }
351
os_timer_stop(os_timer_t timer)352 static inline int os_timer_stop(os_timer_t timer)
353 {
354 return -1;
355 }
356
os_timer_new(void (* func)(void *,void *),void * arg,int internal_ms,int repeat,unsigned char auto_run)357 static inline os_timer_t os_timer_new(void (*func)(void *, void *), void *arg,
358 int internal_ms, int repeat, unsigned char auto_run)
359 {
360 return NULL;
361 }
362
os_timer_free(os_timer_t timer)363 static inline void os_timer_free(os_timer_t timer)
364 {
365 }
366
os_task_create(os_task_t * task,const char * name,void * (* fn)(void *),void * arg,int stack_size,int pri)367 static inline int os_task_create(os_task_t *task, const char *name,
368 void *(*fn)(void *), void *arg, int stack_size, int pri)
369 {
370 pthread_attr_t attr;
371 size_t def_stack_size = 0;
372 int ret;
373
374 pthread_attr_init(&attr);
375 if (!strcmp(name, "uvoice_event_task"))
376 pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
377 pthread_attr_getstacksize(&attr, &def_stack_size);
378 if (def_stack_size < stack_size)
379 pthread_attr_setstacksize(&attr, stack_size);
380
381 ret = pthread_create(task, &attr, fn, arg);
382 pthread_attr_destroy(&attr);
383 return ret;
384 }
385
os_task_exit(os_task_t task)386 static inline int os_task_exit(os_task_t task)
387 {
388 pthread_exit(NULL);
389 }
390
os_partition_name(int pt)391 static inline const char *os_partition_name(int pt)
392 {
393 return NULL;
394 }
395
os_partition_size(int pt)396 static inline int os_partition_size(int pt)
397 {
398 return -1;
399 }
400
os_partition_read(int pt,uint32_t * offset,uint8_t * buffer,uint32_t len)401 static inline int os_partition_read(int pt, uint32_t *offset, uint8_t *buffer, uint32_t len)
402 {
403 return -1;
404 }
405
os_partition_write(int pt,uint32_t * offset,const uint8_t * buffer,uint32_t len)406 static inline int os_partition_write(int pt, uint32_t *offset, const uint8_t *buffer , uint32_t len)
407 {
408 return -1;
409 }
410
os_partition_erase(int pt,uint32_t offset,uint32_t len)411 static inline int os_partition_erase(int pt, uint32_t offset, uint32_t len)
412 {
413 return -1;
414 }
415
416
417 #endif /* __UVOICE_LINUX_H__ */
418