vdi_osal.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  1. /*
  2. * Copyright (c) 2019, Chips&Media
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. *
  14. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  15. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  16. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  17. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  18. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  19. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  20. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  21. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  22. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  23. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  24. */
  25. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <stdarg.h>
  29. #include <errno.h>
  30. #define __USE_GNU
  31. #include <pthread.h>
  32. #include <semaphore.h>
  33. #include "vpuconfig.h"
  34. #include "../vdi_osal.h"
  35. #include <time.h>
  36. #include <termios.h>
  37. static struct termios initial_settings, new_settings;
  38. static int peek_character = -1;
  39. #define ANSI_COLOR_ERR "\x1b[31m" // RED
  40. #define ANSI_COLOR_TRACE "\x1b[32m" // GREEN
  41. #define ANSI_COLOR_WARN "\x1b[33m" // YELLOW
  42. #define ANSI_COLOR_BLUE "\x1b[34m" // BLUE
  43. #define ANSI_COLOR_INFO ""
  44. // For future
  45. #define ANSI_COLOR_MAGENTA "\x1b[35m" // MAGENTA
  46. #define ANSI_COLOR_CYAN "\x1b[36m" // CYAN
  47. #define ANSI_COLOR_RESET "\x1b[0m" // RESET
  48. static unsigned log_decor = LOG_HAS_TIME | LOG_HAS_FILE | LOG_HAS_MICRO_SEC |
  49. LOG_HAS_NEWLINE |
  50. LOG_HAS_SPACE | LOG_HAS_COLOR;
  51. static int max_log_level = ERR;
  52. static FILE *fpLog = NULL;
  53. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  54. static pthread_mutex_t s_log_mutex;
  55. #endif
  56. int InitLog()
  57. {
  58. fpLog = osal_fopen("/tmp/DecoderErrorLog.txt", "w");
  59. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  60. pthread_mutex_init(&s_log_mutex, NULL);
  61. #endif
  62. return 1;
  63. }
  64. void DeInitLog()
  65. {
  66. if (fpLog)
  67. {
  68. osal_fclose(fpLog);
  69. fpLog = NULL;
  70. }
  71. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  72. pthread_mutex_destroy(&s_log_mutex);
  73. #endif
  74. }
  75. void SetMaxLogLevel(int level)
  76. {
  77. max_log_level = level;
  78. }
  79. int GetMaxLogLevel()
  80. {
  81. return max_log_level;
  82. }
  83. void LogMsg(int level, const char *format, ...)
  84. {
  85. va_list ptr;
  86. char logBuf[MAX_PRINT_LENGTH] = {0};
  87. char* prefix = "";
  88. char* postfix= "";
  89. if (level > max_log_level)
  90. return;
  91. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  92. pthread_mutex_lock(&s_log_mutex);
  93. #endif
  94. if ((log_decor & LOG_HAS_COLOR)) {
  95. postfix = ANSI_COLOR_RESET;
  96. switch (level) {
  97. case INFO: prefix = ANSI_COLOR_INFO; break;
  98. case ERR: prefix = ANSI_COLOR_ERR "[ERROR]"; break;
  99. case TRACE: prefix = ANSI_COLOR_TRACE; break;
  100. case WARN: prefix = ANSI_COLOR_WARN"[WARN ]"; break;
  101. default: prefix = ""; break;
  102. }
  103. }
  104. va_start( ptr, format );
  105. vsnprintf( logBuf, MAX_PRINT_LENGTH, format, ptr );
  106. va_end(ptr);
  107. #ifdef ANDROID
  108. if (level == ERR) ALOGE("%s", logBuf);
  109. else ALOGI("%s", logBuf);
  110. fputs(logBuf, stderr);
  111. #else
  112. fputs(prefix, stderr);
  113. fputs(logBuf, stderr);
  114. fputs(postfix, stderr);
  115. #endif
  116. if ((log_decor & LOG_HAS_FILE) && fpLog)
  117. {
  118. osal_fwrite(logBuf, strlen(logBuf), 1,fpLog);
  119. osal_fflush(fpLog);
  120. }
  121. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  122. pthread_mutex_unlock(&s_log_mutex);
  123. #endif
  124. }
  125. void osal_init_keyboard()
  126. {
  127. tcgetattr(0,&initial_settings);
  128. new_settings = initial_settings;
  129. new_settings.c_lflag &= ~ICANON;
  130. new_settings.c_lflag &= ~ECHO;
  131. //new_settings.c_lflag &= ~ISIG;
  132. new_settings.c_cc[VMIN] = 1;
  133. new_settings.c_cc[VTIME] = 0;
  134. tcsetattr(0, TCSANOW, &new_settings);
  135. peek_character = -1;
  136. }
  137. void osal_close_keyboard()
  138. {
  139. tcsetattr(0, TCSANOW, &initial_settings);
  140. }
  141. int osal_kbhit()
  142. {
  143. unsigned char ch;
  144. int nread;
  145. if (peek_character != -1) return 1;
  146. new_settings.c_cc[VMIN]=0;
  147. tcsetattr(0, TCSANOW, &new_settings);
  148. nread = read(0,&ch,1);
  149. new_settings.c_cc[VMIN]=1;
  150. tcsetattr(0, TCSANOW, &new_settings);
  151. if(nread == 1)
  152. {
  153. peek_character = (int)ch;
  154. return 1;
  155. }
  156. return 0;
  157. }
  158. int osal_getch()
  159. {
  160. int val;
  161. char ch;
  162. if(peek_character != -1)
  163. {
  164. val = peek_character;
  165. peek_character = -1;
  166. return val;
  167. }
  168. read(0,&ch,1);
  169. return ch;
  170. }
  171. int osal_flush_ch(void)
  172. {
  173. fflush(stdout);
  174. return 1;
  175. }
  176. void * osal_memcpy(void * dst, const void * src, int count)
  177. {
  178. return memcpy(dst, src, count);//lint !e670
  179. }
  180. int osal_memcmp(const void* src, const void* dst, int size)
  181. {
  182. return memcmp(src, dst, size);
  183. }
  184. void * osal_memset(void *dst, int val, int count)
  185. {
  186. return memset(dst, val, count);
  187. }
  188. void * osal_malloc(int size)
  189. {
  190. return malloc(size);
  191. }
  192. void * osal_realloc(void* ptr, int size)
  193. {
  194. return realloc(ptr, size);
  195. }
  196. void osal_free(void *p)
  197. {
  198. free(p);//lint -e{424}
  199. }
  200. int osal_fflush(osal_file_t fp)
  201. {
  202. return fflush(fp);
  203. }
  204. int osal_feof(osal_file_t fp)
  205. {
  206. return feof((FILE *)fp);
  207. }
  208. osal_file_t osal_fopen(const char * osal_file_tname, const char * mode)
  209. {
  210. return fopen(osal_file_tname, mode);
  211. }
  212. size_t osal_fwrite(const void * p, int size, int count, osal_file_t fp)
  213. {
  214. return fwrite(p, size, count, fp);
  215. }
  216. size_t osal_fread(void *p, int size, int count, osal_file_t fp)
  217. {
  218. return fread(p, size, count, fp);
  219. }
  220. long osal_ftell(osal_file_t fp)
  221. {
  222. return ftell(fp);
  223. }
  224. int osal_fseek(osal_file_t fp, long offset, int origin)
  225. {
  226. return fseek(fp, offset, origin);
  227. }
  228. int osal_fclose(osal_file_t fp)
  229. {
  230. return fclose(fp); //lint !e482
  231. }
  232. int osal_fscanf(osal_file_t fp, const char * _Format, ...)
  233. {
  234. int ret;
  235. va_list arglist;
  236. va_start(arglist, _Format);
  237. ret = vfscanf(fp, _Format, arglist);
  238. va_end(arglist);
  239. return ret;
  240. }
  241. int osal_fprintf(osal_file_t fp, const char * _Format, ...)
  242. {
  243. int ret;
  244. va_list arglist;
  245. va_start(arglist, _Format);
  246. ret = vfprintf(fp, _Format, arglist);
  247. va_end(arglist);
  248. return ret;
  249. }
  250. void osal_msleep(Uint32 millisecond)
  251. {
  252. usleep(millisecond*1000);
  253. }
  254. osal_thread_t osal_thread_create(void(*start_routine)(void*), void*arg)
  255. {
  256. Int32 ret;
  257. pthread_t* thread = (pthread_t*)osal_malloc(sizeof(pthread_t));
  258. osal_thread_t handle = NULL;
  259. if ((ret=pthread_create(thread, NULL, (void*(*)(void*))start_routine, arg)) != 0) {
  260. osal_free(thread);
  261. VLOG(ERR, "<%s:%d> Failed to pthread_create ret(%d)\n", __FUNCTION__, __LINE__, ret);
  262. }
  263. else {
  264. handle = (osal_thread_t)thread;
  265. }
  266. return handle; //lint !e593
  267. }
  268. Int32 osal_thread_join(osal_thread_t thread, void** retval)
  269. {
  270. Int32 ret;
  271. pthread_t pthreadHandle;
  272. if (thread == NULL) {
  273. VLOG(ERR, "%s:%d invalid thread handle\n", __FUNCTION__, __LINE__);
  274. return 2;
  275. }
  276. pthreadHandle = *(pthread_t*)thread;
  277. if ((ret=pthread_join(pthreadHandle, retval)) != 0) {
  278. osal_free(thread);
  279. VLOG(ERR, "%s:%d Failed to pthread_join ret(%d)\n", __FUNCTION__, __LINE__, ret);
  280. return 2;
  281. }
  282. osal_free(thread);
  283. return 0;
  284. }
  285. Int32 osal_thread_timedjoin(osal_thread_t thread, void** retval, Uint32 millisecond)
  286. {
  287. Int32 ret;
  288. pthread_t pthreadHandle;
  289. struct timespec absTime;
  290. if (thread == NULL) {
  291. VLOG(ERR, "%s:%d invalid thread handle\n", __FUNCTION__, __LINE__);
  292. return 2;
  293. }
  294. pthreadHandle = *(pthread_t*)thread;
  295. clock_gettime(CLOCK_REALTIME, &absTime);
  296. absTime.tv_nsec += millisecond*1000000;
  297. if (1000000000 < absTime.tv_nsec) {
  298. absTime.tv_sec++;
  299. absTime.tv_nsec -= 1000000000;
  300. }
  301. if ((ret=pthread_timedjoin_np(pthreadHandle, retval, &absTime)) == 0) {
  302. osal_free(thread);
  303. return 0;
  304. }
  305. else if (ret == ETIMEDOUT) {
  306. return 1;
  307. }
  308. else {
  309. return 2;
  310. }
  311. }
  312. osal_mutex_t osal_mutex_create(void)
  313. {
  314. pthread_mutex_t* mutex = (pthread_mutex_t*)osal_malloc(sizeof(pthread_mutex_t));
  315. if (mutex == NULL) {
  316. VLOG(ERR, "<%s:%d> failed to allocate memory\n", __FUNCTION__, __LINE__);
  317. return NULL;
  318. }
  319. if (pthread_mutex_init(mutex, NULL) < 0) {
  320. osal_free(mutex);
  321. VLOG(ERR, "<%s:%d> failed to pthread_mutex_init() errno(%d)\n", __FUNCTION__, __LINE__, errno);
  322. return NULL; //lint !e429
  323. }
  324. return (osal_mutex_t)mutex;
  325. }
  326. void osal_mutex_destroy(osal_mutex_t mutex)
  327. {
  328. Int32 ret;
  329. if (mutex == NULL) {
  330. VLOG(ERR, "<%s:%d> Invalid mutex handle\n", __FUNCTION__, __LINE__);
  331. return;
  332. }
  333. if ((ret=pthread_mutex_destroy(mutex)) != 0) {
  334. VLOG(ERR, "<%s:%d> Failed to pthread_mutex_destroy(). ret(%d)\n", __FUNCTION__, __LINE__, ret);
  335. }
  336. osal_free(mutex);
  337. }
  338. BOOL osal_mutex_lock(osal_mutex_t mutex)
  339. {
  340. Int32 ret;
  341. if (mutex == NULL) {
  342. VLOG(ERR, "<%s:%d> Invalid mutex handle\n", __FUNCTION__, __LINE__);
  343. return FALSE;
  344. }
  345. if ((ret=pthread_mutex_lock((pthread_mutex_t*)mutex)) != 0) {
  346. VLOG(ERR, "<%s:%d> Failed to pthread_mutex_lock() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  347. return FALSE; //lint !e454
  348. }
  349. return TRUE; //lint !e454
  350. }
  351. BOOL osal_mutex_unlock(osal_mutex_t mutex)
  352. {
  353. Int32 ret;
  354. if (mutex == NULL) {
  355. VLOG(ERR, "<%s:%d> Invalid mutex handle\n", __FUNCTION__, __LINE__);
  356. return FALSE;
  357. }
  358. if ((ret=pthread_mutex_unlock((pthread_mutex_t*)mutex)) != 0) { //lint !e455
  359. VLOG(ERR, "<%s:%d> Failed to pthread_mutex_unlock(). ret(%d)\n", __FUNCTION__, __LINE__, ret);
  360. return FALSE;
  361. }
  362. return TRUE;
  363. }
  364. osal_sem_t osal_sem_init(Uint32 count)
  365. {
  366. sem_t* semaphore;
  367. semaphore = (sem_t*)malloc(sizeof(sem_t));
  368. if (sem_init(semaphore, 0, count) < 0) {
  369. VLOG(INFO, "<%s:%d> Failed to sem_init\n", __FUNCTION__, __LINE__);
  370. free(semaphore);
  371. semaphore = NULL;
  372. }
  373. return (osal_sem_t)semaphore;
  374. }
  375. void osal_sem_destroy(osal_sem_t sem)
  376. {
  377. sem_t* semaphore = (sem_t*)sem;
  378. sem_destroy(semaphore);
  379. free(semaphore);
  380. }
  381. void osal_sem_wait(osal_sem_t sem)
  382. {
  383. sem_t* semaphore = (sem_t*)sem;
  384. sem_wait(semaphore);
  385. }
  386. void osal_sem_post(osal_sem_t sem)
  387. {
  388. sem_t* semaphore = (sem_t*)sem;
  389. sem_post(semaphore);
  390. }
  391. Uint64 osal_gettime(void)
  392. {
  393. struct timespec tp;
  394. clock_gettime(CLOCK_MONOTONIC, &tp);
  395. return (tp.tv_sec*1000 + tp.tv_nsec/1000000);
  396. }