OSPort.h 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. /*****************************************************************
  2. *****************************************************************
  3. *** **
  4. *** (C)Copyright 2005-2006, American Megatrends Inc. **
  5. *** **
  6. *** All Rights Reserved. **
  7. *** **
  8. *** 6145-F, Northbelt Parkway, Norcross, **
  9. *** **
  10. *** Georgia - 30071, USA. Phone-(770)-246-8600. **
  11. *** **
  12. *****************************************************************
  13. *****************************************************************
  14. ******************************************************************
  15. *
  16. * OSPort.h
  17. * OS Specific porting functions.
  18. *
  19. * Author: Govind Kothandapani <govindk@ami.com>
  20. ******************************************************************/
  21. #ifndef OSPORT_H
  22. #define OSPORT_H
  23. #include <Types.h>
  24. #include <stdio.h>
  25. #include <unistd.h>
  26. #include <stdlib.h>
  27. #include <string.h>
  28. #include <fcntl.h>
  29. #include <time.h>
  30. #include <sys/types.h>
  31. #include <sys/ipc.h>
  32. #include <sys/shm.h>
  33. #include <sys/sem.h>
  34. #include <sys/msg.h>
  35. #include <sys/stat.h>
  36. #include <sys/errno.h>
  37. #include <sys/socket.h>
  38. #include <sys/wait.h>
  39. #include <netinet/in.h>
  40. #include <pthread.h>
  41. #include <termio.h>
  42. //#include "unix.h"
  43. #include "bmc_type.h"
  44. #include <unistd.h>
  45. #include <sys/syscall.h>
  46. #define gettid() syscall(SYS_gettid)
  47. /*------------------------------------------------------------------
  48. * Timeout Macros
  49. *------------------------------------------------------------------*/
  50. #define WAIT_NONE 0
  51. #define WAIT_INFINITE (-1)
  52. /*---------------------------------------------------------------------
  53. * Error Macros
  54. *--------------------------------------------------------------------*/
  55. #define OS_NO_ERR 0
  56. /*---------------------------------------------------------------------
  57. * Memory related macros
  58. *--------------------------------------------------------------------*/
  59. #define _FAR_
  60. /*---------------------------------------------------------------------*
  61. * Task related macros.
  62. *---------------------------------------------------------------------*/
  63. #define OS_TASK_MAIN(TaskMain) int main (int argc, char *argv [])
  64. #define OS_TASK_RETURN return 0
  65. /*---- Handle to a Task ID -----*/
  66. typedef pid_t HTaskID_T;
  67. /*---- Get the currentTask ID -----*/
  68. #define GET_TASK_ID() getpid()
  69. #define GET_THREAD_ID() gettid()
  70. #if 0
  71. /*---- Create a new task ---------------------------------------------*/
  72. #define OS_CREATE_TASK(task, pArg, err ,pid) \
  73. do \
  74. { \
  75. if(((pid) = fork()) < 0) { \
  76. (err) = errno; \
  77. } else if((pid) == 0) { \
  78. if(execve((task), (pArg), NULL) < 0) { \
  79. char buf[256]; \
  80. char *msg; \
  81. msg = (char *)strerror_r(errno, buf, 256); \
  82. IPMI_ERROR("execv %s failed: %d, %s", (task), errno, msg); \
  83. exit(errno); \
  84. } \
  85. } \
  86. } while (0)
  87. /*---- Stop a task ---------------------------------------------*/
  88. #define OS_STOP_TASK(pid) \
  89. do \
  90. { \
  91. kill(pid,15); \
  92. } while (0)
  93. /*---- Suspend a Task ---------------------------------------------*/
  94. #define OS_SUSPEND_TASK(Prio, pErr) \
  95. do \
  96. { \
  97. } while (0)
  98. #endif
  99. /*---- Create a Thread --------------------------------------------*/
  100. #define OS_CREATE_THREAD(Thread, Arg, pErr) \
  101. do \
  102. { \
  103. pthread_t thread_id; \
  104. pthread_attr_t threadAttr; \
  105. pthread_attr_init( &threadAttr ); \
  106. pthread_attr_setdetachstate( &threadAttr, PTHREAD_CREATE_DETACHED );\
  107. if (0 != pthread_create (&thread_id, &threadAttr, Thread, Arg)) \
  108. { \
  109. IPMI_ERROR ("Error creating thread\n"); \
  110. } \
  111. } while (0)
  112. /*---- Delete a Thread --------------------------------------------*/
  113. #define OS_DELETE_THREAD() \
  114. do \
  115. { \
  116. pthread_t thread_id; \
  117. thread_id = pthread_self(); \
  118. pthread_detach(thread_id); \
  119. pthread_exit(NULL); \
  120. } while (0)
  121. /*------------ OS Stack Type -------------------------------------*/
  122. #define OS_STACK OS_STK
  123. /*----------------- Shared Memory Macros -------------------------*/
  124. typedef _FAR_ void* HSharedMem_T;
  125. #define OS_CREATE_SHARED_MEM(Key, hShmMem, Size) \
  126. do \
  127. { \
  128. int shmid; \
  129. shmid = shmget (Key, Size, 0777 | IPC_CREAT); \
  130. if(shmid == -1) \
  131. { \
  132. TCRIT("error in shmget in CREATE macro with key %d\n",Key); \
  133. } \
  134. hShmMem = shmat (shmid, 0, 0); \
  135. if (hShmMem == (void *)-1) \
  136. { \
  137. TCRIT("error in shmat in CREATE macro with key %d\n",Key); \
  138. } \
  139. } while (0)
  140. #define OS_GET_SHARED_MEM(Key, hShmMem, Size) \
  141. do \
  142. { \
  143. int shmid; \
  144. shmid = shmget (Key, Size, 0777); \
  145. if(shmid == -1) \
  146. { \
  147. TCRIT("Error getting shared memory in GET macro with key %d\n",Key); \
  148. } \
  149. hShmMem = shmat (shmid, 0, 0); \
  150. if (hShmMem == (void *)-1) \
  151. { \
  152. TCRIT("error in shmat in CREATE macro with key %d\n",Key); \
  153. } \
  154. } while (0)
  155. #define OS_RELEASE_SHARED_MEM(hShmMem) shmdt (hShmMem)
  156. /*-----------------------------------------------------------------
  157. * Queue specific macros, functions and typedefs
  158. *-----------------------------------------------------------------*/
  159. /*---- Handle to a Queue -----*/
  160. //typedef long HQueue_T;
  161. /*------ Allocate memory to hold the queue -------------------------*/
  162. #define OS_ALLOC_Q(QueueName, Size)
  163. #define OS_CREATE_Q(Key, Size, Instance) \
  164. if(Instance != COMMON_QUEUE) \
  165. { \
  166. sprintf(keyInstance, "%s%d", Key, Instance); \
  167. } \
  168. else \
  169. { \
  170. sprintf(keyInstance,"%s",Key); \
  171. } \
  172. AddQueue(keyInstance); \
  173. if (-1 == mkfifo (keyInstance, 0777) && (errno != EEXIST)) \
  174. { \
  175. IPMI_ERROR ("Error creating named pipe %s\n", keyInstance); \
  176. }
  177. #define OS_GET_Q(Key, RDWR,Instance) \
  178. if(Instance != COMMON_QUEUE) \
  179. { \
  180. sprintf(keyInstance, "%s%d", Key, Instance); \
  181. } \
  182. else \
  183. { \
  184. sprintf(keyInstance,"%s",Key); \
  185. } \
  186. if (GetQueue(keyInstance,RDWR) == -1) \
  187. { \
  188. IPMI_ERROR ("Attempt to open before creating pipe %s\n", keyInstance); \
  189. } \
  190. #define OS_CLOSE_Q(hQueue) \
  191. close (hQueue);
  192. #define POST_TO_Q(pBuf,Size,key,pErr,BMCInst) \
  193. { \
  194. AddToQueue(pBuf,key,Size,BMCInst); \
  195. }
  196. /*------- Add this message to the queue ----------------------------*/
  197. #define OS_ADD_TO_Q(pBuf, Size, hQueue, pErr) \
  198. *pErr = write (hQueue, pBuf, Size) \
  199. /*------ Get the message from the queue -----------------------------*/
  200. #define OS_GET_FROM_Q(pBuf, Size, hQueue, timeout, pErr) \
  201. { \
  202. int ReadLen = 0, Left, Len; \
  203. Left = Size; \
  204. while( Left > 0 ) \
  205. { \
  206. Len = read (hQueue, (INT8U*)pBuf + ReadLen, Left ); \
  207. if( Len < 0 ) \
  208. { \
  209. if( errno == EINTR || errno == EAGAIN ) \
  210. { \
  211. continue; \
  212. } \
  213. else \
  214. { \
  215. *pErr = -1; \
  216. break; \
  217. } \
  218. } \
  219. ReadLen += Len; \
  220. Left -= Len; \
  221. } \
  222. *pErr = ReadLen; \
  223. }
  224. /*-------- Get the number of messages in the Queue --------------------*/
  225. #define OS_GET_Q_DEPTH(hQueue, Depth, Size) \
  226. do \
  227. { \
  228. struct stat Stat; \
  229. if(0 == fstat (hQueue, &Stat)) \
  230. { \
  231. Depth = Stat.st_size/Size; \
  232. } \
  233. else { Depth = 0; } \
  234. } while (0)
  235. /*----------------Locking mechanism for Queue-----------------------*/
  236. #define LOCK_QUEUE(hQueue) \
  237. if (-1 == file_lock_write (hQueue)) \
  238. { \
  239. IPMI_WARNING ("Error locking Queue\n"); \
  240. }
  241. #define UNLOCK_QUEUE(hQueue) \
  242. if (-1 == file_unlock (hQueue)) \
  243. { \
  244. IPMI_WARNING ("Error unlocking Queue\n"); \
  245. } \
  246. /*---------------- Semaphores -------------------------------------*/
  247. typedef int Semaphore_T;
  248. #define CREATE_SEMAPHORE(Key) semget (Key, 1, 0777 | IPC_CREAT)
  249. #define GET_SEMAPHORE(Key) semget (Key, 1, 0777)
  250. #define RELEASE_SEMAPHORE(hSem) \
  251. do { \
  252. struct sembuf Sembuf; \
  253. Sembuf.sem_num = 0; \
  254. Sembuf.sem_op = -1; \
  255. Sembuf.sem_flg = 0; \
  256. semop (hSem, &Sembuf, 1); \
  257. }while (0)
  258. #define LOCK_SEMAPHORE(hSem, Timeout, pErr) \
  259. do { \
  260. struct sembuf Sembuf [2]; \
  261. Sembuf [0].sem_num = 0; \
  262. Sembuf [0].sem_op = 0; \
  263. Sembuf [0].sem_flg = 0; \
  264. Sembuf [1].sem_num = 0; \
  265. Sembuf [1].sem_op = 1; \
  266. Sembuf [1].sem_flg = 0; \
  267. if (Timeout == WAIT_NONE) \
  268. { \
  269. Sembuf [0].sem_flg |= IPC_NOWAIT; \
  270. semop (hSem, Sembuf, 1); \
  271. } \
  272. else if (Timeout == WAIT_INFINITE) \
  273. { \
  274. semop (hSem, Sembuf, 2); \
  275. } \
  276. else \
  277. { \
  278. IPMI_ERROR ("OSPort.h : ERROR\n"); \
  279. /*OSSemPend (EVENT_ID, TIMEOUT, ERR); */\
  280. } \
  281. } while (0)
  282. /*------------- Mutexes -------------------------------------------*/
  283. #define OS_CREATE_MUTEX(hMutex,MutexName) \
  284. if ((hMutex = mutex_create_recursive (MutexName)) == NULL) \
  285. { \
  286. IPMI_ERROR ("Error creating Mutex %s\n",MutexName); \
  287. } \
  288. #define OS_ACQUIRE_MUTEX(hMutex, Timeout) \
  289. if (Timeout == WAIT_INFINITE) \
  290. { \
  291. if (-1 == mutex_lock_recursive (hMutex, 15 )) \
  292. { \
  293. TCRIT ("Error acquiring %s mutex in Task ID %d \n", hMutex->MutexName, getpid()); \
  294. } \
  295. } \
  296. else \
  297. { \
  298. if (-1 == mutex_lock_recursive (hMutex, Timeout )) \
  299. { \
  300. TCRIT ("Error acquiring %s mutex in Task ID %d \n", hMutex->MutexName, getpid()); \
  301. } \
  302. }
  303. #define OS_RELEASE_MUTEX(hMutex) \
  304. if (-1 == mutex_unlock_recursive (hMutex)) \
  305. { \
  306. IPMI_ERROR ("Error releasing mutex\n"); \
  307. }
  308. #define OS_DESTROY_MUTEX(hMutex) \
  309. if (-1 == mutex_destroy_recursive (hMutex)) \
  310. { \
  311. IPMI_ERROR ("Error releasing mutex\n"); \
  312. }
  313. #define OS_CREATE_TASK_THREAD(Thread,Arg,pErr,ThreadID) \
  314. do \
  315. { \
  316. if(0 != pthread_create(&ThreadID,NULL,Thread,Arg)) \
  317. { \
  318. IPMI_ERROR("Error creating thread \n"); \
  319. } \
  320. } while (0)
  321. /*------------- ThreadMutexes -------------------------------------------*/
  322. #define OS_THREAD_MUTEX_DECLARE(MutexName) pthread_mutex_t MutexName
  323. #define OS_THREAD_MUTEX_INIT(MutexName, Type) \
  324. do { \
  325. pthread_mutexattr_t attr; \
  326. pthread_mutexattr_init(&attr); \
  327. pthread_mutexattr_settype(&attr, Type); \
  328. pthread_mutex_init(&MutexName, &attr); \
  329. pthread_mutexattr_destroy(&attr); \
  330. } while (0);
  331. #define OS_THREAD_MUTEX_DEFINE(MutexName) pthread_mutex_t MutexName = PTHREAD_MUTEX_INITIALIZER;
  332. #define OS_THREAD_MUTEX_ACQUIRE(MutexName, Timeout) \
  333. do{ \
  334. if (Timeout == WAIT_INFINITE) \
  335. { \
  336. if (0 != pthread_mutex_lock(MutexName)) \
  337. { \
  338. IPMI_ERROR ("Error acquiring mutex in Thread ID %d\n", getpid()); \
  339. } \
  340. } \
  341. else \
  342. { \
  343. struct timespec ts; \
  344. ts.tv_sec = time(NULL) + Timeout; \
  345. ts.tv_nsec = 0L; \
  346. if (ETIMEDOUT == pthread_mutex_timedlock (MutexName, &ts)) \
  347. { \
  348. IPMI_ERROR ("Error acquiring in Thread ID %d \n", getpid()); \
  349. } \
  350. } \
  351. }while(0);
  352. #define OS_THREAD_MUTEX_RELEASE(MutexName) \
  353. do { \
  354. if( 0 != pthread_mutex_unlock(MutexName)) \
  355. { \
  356. IPMI_ERROR("Error in releasing mutex in Thread ID %d\n",getpid()); \
  357. } \
  358. } while (0);
  359. #define OS_THREAD_MUTEX_DESTROY(MutexName) \
  360. do{ \
  361. if( 0 != pthread_mutex_destroy(MutexName)) \
  362. { \
  363. IPMI_ERROR("Error in destroying mutex of Thread ID %d\n",getpid()); \
  364. } \
  365. }while(0);
  366. /*---------------Thread local storage (TLS)--------------------------*/
  367. #define OS_THREAD_TLS_CREATE(key) \
  368. do{ \
  369. pthread_key_create(key,NULL); \
  370. }while(0);
  371. #define OS_THREAD_TLS_SET(key,val) \
  372. do{ \
  373. if( 0 != pthread_setspecific(key,val)) \
  374. { \
  375. IPMI_ERROR("Error in setting TLS data %d \n",getpid()); \
  376. } \
  377. }while(0);
  378. #define OS_THREAD_TLS_GET(key,val) \
  379. do{ \
  380. val = pthread_getspecific(key); \
  381. }while(0);
  382. /*-------------------------Thread Priority--------------------------------*/
  383. /*-------------------------SCHED_FIFO/RR------------------------------*/
  384. #define HIGH 75
  385. #define LOW 1
  386. #define OS_THREAD_SET_PRIORITY(thrd_id,policy,priority) \
  387. do{ \
  388. struct sched_param param; \
  389. param.sched_priority = priority; \
  390. if(0 != pthread_setschedparam(thrd_id,policy,&param)) \
  391. { \
  392. IPMI_ERROR("Error while setting the thread priority\n"); \
  393. } \
  394. }while(0);
  395. #define OS_THREAD_GET_PRIORITY(thrd_id,policy,priority) \
  396. do{ \
  397. struct sched_param param; \
  398. if(0 != pthread_getschedparam(thrd_id,&policy,&param)) \
  399. { \
  400. IPMI_ERROR("Error while setting the thread priority\n"); \
  401. } \
  402. *priority = param.sched_priority; \
  403. }while(0);
  404. /*------------- Events -------------------------------------------*/
  405. typedef int Event_T;
  406. #define CREATE_EVENT(KEY) semget (KEY, 1, 0777 | IPC_CREAT)
  407. #define GET_EVENT(KEY) semget (KEY, 1, 0777)
  408. #define DELETE_EVENT(EVENT, ERR)
  409. #define SIGNAL_EVENT(EVENT_ID) \
  410. do { \
  411. struct sembuf Sembuf; \
  412. \
  413. Sembuf.sem_num = 0; \
  414. Sembuf.sem_op = 1; \
  415. Sembuf.sem_flg = 0; \
  416. \
  417. semop (EVENT_ID, &Sembuf, 1); \
  418. }while (0)
  419. #define WAIT_FOR_EVENT(EVENT_ID, TIMEOUT, ERR)\
  420. do { \
  421. struct sembuf Sembuf; \
  422. \
  423. Sembuf.sem_num = 0; \
  424. Sembuf.sem_op = -1; \
  425. Sembuf.sem_flg = 0; \
  426. \
  427. if (TIMEOUT == WAIT_NONE) \
  428. { \
  429. Sembuf.sem_flg |= IPC_NOWAIT; \
  430. semop (EVENT_ID, &Sembuf, 1); \
  431. } \
  432. else if (TIMEOUT == WAIT_INFINITE) \
  433. { \
  434. semop (EVENT_ID, &Sembuf, 1); \
  435. } \
  436. else \
  437. { \
  438. /*OSSemPend (EVENT_ID, TIMEOUT, ERR); */ \
  439. } \
  440. } while (0)
  441. /*-------------- Time related macros ------------------------------*/
  442. #define OS_TIME_DELAY_HMSM(Hrs, Mins, Secs, Millisecs) \
  443. do { \
  444. usleep ( (Hrs * 3600 * 1000 * 1000) + (Mins * 60 * 1000 * 1000) + (Secs * 1000 * 1000) + (Millisecs * 1000)); \
  445. } while (0)
  446. /**
  447. * @def GET_SYSTEM_TIME_STAMP
  448. * @brief Gets the system time since the Epoch (00:00:00 UTC, January 1, 1970),
  449. * measured in seconds.
  450. **/
  451. #define GET_SYSTEM_TIME_STAMP() time(NULL)
  452. /**
  453. * @def SET_SYSTEM_TIME_STAMP
  454. * @brief Sets the system time since the Epoch (00:00:00 UTC, January 1, 1970),
  455. * measured in seconds.
  456. * @param TIME 32 bit value in seconds from January 1, 1970 GMT.
  457. **/
  458. #define SET_SYSTEM_TIME_STAMP(TIME) stime((time_t *)TIME)
  459. typedef int SOCKET;
  460. #define _fmemcpy memcpy
  461. #define _fmemcmp memcmp
  462. #define _fmemset memset
  463. #define _fstrcpy strcpy
  464. #define _fstrlen strlen
  465. #define _fstrcat strcat
  466. #define _fstrcmp strcmp
  467. #endif /* OSPORT_H */