vdi_osal.c 11 KB

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