vdi_osal.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535
  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/EncoderErrorLog.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. #if 1
  86. va_list ptr;
  87. char logBuf[MAX_PRINT_LENGTH] = {0};
  88. char* prefix = "";
  89. char* postfix= "";
  90. if (level > max_log_level)
  91. return;
  92. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  93. pthread_mutex_lock(&s_log_mutex);
  94. #endif
  95. if ((log_decor & LOG_HAS_COLOR)) {
  96. postfix = ANSI_COLOR_RESET;
  97. switch (level) {
  98. case INFO: prefix = ANSI_COLOR_INFO; break;
  99. case ERR: prefix = ANSI_COLOR_ERR "[ERROR]"; break;
  100. case TRACE: prefix = ANSI_COLOR_TRACE; break;
  101. case WARN: prefix = ANSI_COLOR_WARN"[WARN ]"; break;
  102. default: prefix = ""; break;
  103. }
  104. }
  105. va_start( ptr, format );
  106. vsnprintf( logBuf, MAX_PRINT_LENGTH, format, ptr );
  107. va_end(ptr);
  108. #ifdef ANDROID
  109. if (level == ERR) ALOGE("%s", logBuf);
  110. else ALOGI("%s", logBuf);
  111. fputs(logBuf, stderr);
  112. #else
  113. fputs(prefix, stderr);
  114. fputs(logBuf, stderr);
  115. fputs(postfix, stderr);
  116. #endif
  117. if ((log_decor & LOG_HAS_FILE) && fpLog)
  118. {
  119. osal_fwrite(logBuf, strlen(logBuf), 1,fpLog);
  120. osal_fflush(fpLog);
  121. }
  122. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  123. pthread_mutex_unlock(&s_log_mutex);
  124. #endif
  125. #endif
  126. }
  127. void LogMsg1(int level, const char *format, ...)
  128. {
  129. va_list ptr;
  130. char logBuf[MAX_PRINT_LENGTH] = {0};
  131. char* prefix = "";
  132. char* postfix= "";
  133. if (level > max_log_level)
  134. return;
  135. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  136. pthread_mutex_lock(&s_log_mutex);
  137. #endif
  138. if ((log_decor & LOG_HAS_COLOR)) {
  139. postfix = ANSI_COLOR_RESET;
  140. switch (level) {
  141. case INFO: prefix = ANSI_COLOR_INFO; break;
  142. case ERR: prefix = ANSI_COLOR_ERR "[ERROR]"; break;
  143. case TRACE: prefix = ANSI_COLOR_TRACE; break;
  144. case WARN: prefix = ANSI_COLOR_WARN"[WARN ]"; break;
  145. default: prefix = ""; break;
  146. }
  147. }
  148. va_start( ptr, format );
  149. vsnprintf( logBuf, MAX_PRINT_LENGTH, format, ptr );
  150. va_end(ptr);
  151. #ifdef ANDROID
  152. if (level == ERR) ALOGE("%s", logBuf);
  153. else ALOGI("%s", logBuf);
  154. fputs(logBuf, stderr);
  155. #else
  156. fputs(prefix, stderr);
  157. fputs(logBuf, stderr);
  158. fputs(postfix, stderr);
  159. #endif
  160. if ((log_decor & LOG_HAS_FILE) && fpLog)
  161. {
  162. osal_fwrite(logBuf, strlen(logBuf), 1,fpLog);
  163. osal_fflush(fpLog);
  164. }
  165. #if defined(SUPPORT_SW_UART) || defined(SUPPORT_SW_UART_V2)
  166. pthread_mutex_unlock(&s_log_mutex);
  167. #endif
  168. }
  169. void osal_init_keyboard()
  170. {
  171. tcgetattr(0,&initial_settings);
  172. new_settings = initial_settings;
  173. new_settings.c_lflag &= ~ICANON;
  174. new_settings.c_lflag &= ~ECHO;
  175. //new_settings.c_lflag &= ~ISIG;
  176. new_settings.c_cc[VMIN] = 1;
  177. new_settings.c_cc[VTIME] = 0;
  178. tcsetattr(0, TCSANOW, &new_settings);
  179. peek_character = -1;
  180. }
  181. void osal_close_keyboard()
  182. {
  183. tcsetattr(0, TCSANOW, &initial_settings);
  184. }
  185. int osal_kbhit()
  186. {
  187. unsigned char ch;
  188. int nread;
  189. if (peek_character != -1) return 1;
  190. new_settings.c_cc[VMIN]=0;
  191. tcsetattr(0, TCSANOW, &new_settings);
  192. nread = read(0,&ch,1);
  193. new_settings.c_cc[VMIN]=1;
  194. tcsetattr(0, TCSANOW, &new_settings);
  195. if(nread == 1)
  196. {
  197. peek_character = (int)ch;
  198. return 1;
  199. }
  200. return 0;
  201. }
  202. int osal_getch()
  203. {
  204. int val;
  205. char ch;
  206. if(peek_character != -1)
  207. {
  208. val = peek_character;
  209. peek_character = -1;
  210. return val;
  211. }
  212. read(0,&ch,1);
  213. return ch;
  214. }
  215. int osal_flush_ch(void)
  216. {
  217. fflush(stdout);
  218. return 1;
  219. }
  220. void * osal_memcpy(void * dst, const void * src, int count)
  221. {
  222. return memcpy(dst, src, count);//lint !e670
  223. }
  224. int osal_memcmp(const void* src, const void* dst, int size)
  225. {
  226. return memcmp(src, dst, size);
  227. }
  228. void * osal_memset(void *dst, int val, int count)
  229. {
  230. return memset(dst, val, count);
  231. }
  232. void * osal_malloc(int size)
  233. {
  234. return malloc(size);
  235. }
  236. void * osal_realloc(void* ptr, int size)
  237. {
  238. return realloc(ptr, size);
  239. }
  240. void osal_free(void *p)
  241. {
  242. free(p);//lint -e{424}
  243. }
  244. int osal_fflush(osal_file_t fp)
  245. {
  246. return fflush(fp);
  247. }
  248. int osal_feof(osal_file_t fp)
  249. {
  250. return feof((FILE *)fp);
  251. }
  252. osal_file_t osal_fopen(const char * osal_file_tname, const char * mode)
  253. {
  254. return fopen(osal_file_tname, mode);
  255. }
  256. size_t osal_fwrite(const void * p, int size, int count, osal_file_t fp)
  257. {
  258. return fwrite(p, size, count, fp);
  259. }
  260. size_t osal_fread(void *p, int size, int count, osal_file_t fp)
  261. {
  262. return fread(p, size, count, fp);
  263. }
  264. long osal_ftell(osal_file_t fp)
  265. {
  266. return ftell(fp);
  267. }
  268. int osal_fseek(osal_file_t fp, long offset, int origin)
  269. {
  270. return fseek(fp, offset, origin);
  271. }
  272. int osal_fclose(osal_file_t fp)
  273. {
  274. return fclose(fp); //lint !e482
  275. }
  276. int osal_fscanf(osal_file_t fp, const char * _Format, ...)
  277. {
  278. int ret;
  279. va_list arglist;
  280. va_start(arglist, _Format);
  281. ret = vfscanf(fp, _Format, arglist);
  282. va_end(arglist);
  283. return ret;
  284. }
  285. int osal_fprintf(osal_file_t fp, const char * _Format, ...)
  286. {
  287. int ret;
  288. va_list arglist;
  289. va_start(arglist, _Format);
  290. ret = vfprintf(fp, _Format, arglist);
  291. va_end(arglist);
  292. return ret;
  293. }
  294. void osal_msleep(Uint32 millisecond)
  295. {
  296. usleep(millisecond*1000);
  297. }
  298. osal_thread_t osal_thread_create(void(*start_routine)(void*), void*arg)
  299. {
  300. Int32 ret;
  301. pthread_t* thread = (pthread_t*)osal_malloc(sizeof(pthread_t));
  302. osal_thread_t handle = NULL;
  303. if ((ret=pthread_create(thread, NULL, (void*(*)(void*))start_routine, arg)) != 0) {
  304. osal_free(thread);
  305. VLOG(ERR, "<%s:%d> Failed to pthread_create ret(%d)\n", __FUNCTION__, __LINE__, ret);
  306. }
  307. else {
  308. handle = (osal_thread_t)thread;
  309. }
  310. return handle; //lint !e593
  311. }
  312. Int32 osal_thread_join(osal_thread_t thread, void** retval)
  313. {
  314. Int32 ret;
  315. pthread_t pthreadHandle;
  316. if (thread == NULL) {
  317. VLOG(ERR, "%s:%d invalid thread handle\n", __FUNCTION__, __LINE__);
  318. return 2;
  319. }
  320. pthreadHandle = *(pthread_t*)thread;
  321. if ((ret=pthread_join(pthreadHandle, retval)) != 0) {
  322. osal_free(thread);
  323. VLOG(ERR, "%s:%d Failed to pthread_join ret(%d)\n", __FUNCTION__, __LINE__, ret);
  324. return 2;
  325. }
  326. osal_free(thread);
  327. return 0;
  328. }
  329. Int32 osal_thread_timedjoin(osal_thread_t thread, void** retval, Uint32 millisecond)
  330. {
  331. Int32 ret;
  332. pthread_t pthreadHandle;
  333. struct timespec absTime;
  334. if (thread == NULL) {
  335. VLOG(ERR, "%s:%d invalid thread handle\n", __FUNCTION__, __LINE__);
  336. return 2;
  337. }
  338. pthreadHandle = *(pthread_t*)thread;
  339. clock_gettime(CLOCK_REALTIME, &absTime);
  340. absTime.tv_nsec += millisecond*1000000;
  341. if (1000000000 < absTime.tv_nsec) {
  342. absTime.tv_sec++;
  343. absTime.tv_nsec -= 1000000000;
  344. }
  345. if ((ret=pthread_timedjoin_np(pthreadHandle, retval, &absTime)) == 0) {
  346. osal_free(thread);
  347. return 0;
  348. }
  349. else if (ret == ETIMEDOUT) {
  350. return 1;
  351. }
  352. else {
  353. return 2;
  354. }
  355. }
  356. osal_mutex_t osal_mutex_create(void)
  357. {
  358. pthread_mutex_t* mutex = (pthread_mutex_t*)osal_malloc(sizeof(pthread_mutex_t));
  359. if (mutex == NULL) {
  360. VLOG(ERR, "<%s:%d> failed to allocate memory\n", __FUNCTION__, __LINE__);
  361. return NULL;
  362. }
  363. if (pthread_mutex_init(mutex, NULL) < 0) {
  364. osal_free(mutex);
  365. VLOG(ERR, "<%s:%d> failed to pthread_mutex_init() errno(%d)\n", __FUNCTION__, __LINE__, errno);
  366. return NULL; //lint !e429
  367. }
  368. return (osal_mutex_t)mutex;
  369. }
  370. void osal_mutex_destroy(osal_mutex_t mutex)
  371. {
  372. Int32 ret;
  373. if (mutex == NULL) {
  374. VLOG(ERR, "<%s:%d> Invalid mutex handle\n", __FUNCTION__, __LINE__);
  375. return;
  376. }
  377. if ((ret=pthread_mutex_destroy(mutex)) != 0) {
  378. VLOG(ERR, "<%s:%d> Failed to pthread_mutex_destroy(). ret(%d)\n", __FUNCTION__, __LINE__, ret);
  379. }
  380. osal_free(mutex);
  381. }
  382. BOOL osal_mutex_lock(osal_mutex_t mutex)
  383. {
  384. Int32 ret;
  385. if (mutex == NULL) {
  386. VLOG(ERR, "<%s:%d> Invalid mutex handle\n", __FUNCTION__, __LINE__);
  387. return FALSE;
  388. }
  389. if ((ret=pthread_mutex_lock((pthread_mutex_t*)mutex)) != 0) {
  390. VLOG(ERR, "<%s:%d> Failed to pthread_mutex_lock() ret(%d)\n", __FUNCTION__, __LINE__, ret);
  391. return FALSE; //lint !e454
  392. }
  393. return TRUE; //lint !e454
  394. }
  395. BOOL osal_mutex_unlock(osal_mutex_t mutex)
  396. {
  397. Int32 ret;
  398. if (mutex == NULL) {
  399. VLOG(ERR, "<%s:%d> Invalid mutex handle\n", __FUNCTION__, __LINE__);
  400. return FALSE;
  401. }
  402. if ((ret=pthread_mutex_unlock((pthread_mutex_t*)mutex)) != 0) { //lint !e455
  403. VLOG(ERR, "<%s:%d> Failed to pthread_mutex_unlock(). ret(%d)\n", __FUNCTION__, __LINE__, ret);
  404. return FALSE;
  405. }
  406. return TRUE;
  407. }
  408. osal_sem_t osal_sem_init(Uint32 count)
  409. {
  410. sem_t* semaphore;
  411. semaphore = (sem_t*)malloc(sizeof(sem_t));
  412. if (sem_init(semaphore, 0, count) < 0) {
  413. VLOG(INFO, "<%s:%d> Failed to sem_init\n", __FUNCTION__, __LINE__);
  414. free(semaphore);
  415. semaphore = NULL;
  416. }
  417. return (osal_sem_t)semaphore;
  418. }
  419. void osal_sem_destroy(osal_sem_t sem)
  420. {
  421. sem_t* semaphore = (sem_t*)sem;
  422. sem_destroy(semaphore);
  423. free(semaphore);
  424. }
  425. void osal_sem_wait(osal_sem_t sem)
  426. {
  427. sem_t* semaphore = (sem_t*)sem;
  428. sem_wait(semaphore);
  429. }
  430. void osal_sem_post(osal_sem_t sem)
  431. {
  432. sem_t* semaphore = (sem_t*)sem;
  433. sem_post(semaphore);
  434. }
  435. Uint64 osal_gettime(void)
  436. {
  437. struct timespec tp;
  438. clock_gettime(CLOCK_MONOTONIC, &tp);
  439. return (tp.tv_sec*1000 + tp.tv_nsec/1000000);
  440. }