message_hook.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #include "hook.h"
  2. #include "bmc_main.h"
  3. #include <semaphore.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6. #include <sys/stat.h>
  7. extern sem_t sem_QUEUE;
  8. #define WAIT_QUEUE do{ sem_wait(&sem_QUEUE); }while(0);
  9. #define POST_QUEUE do{ sem_post(&sem_QUEUE); }while(0);
  10. //#define WAIT_QUEUE
  11. //#define POST_QUEUE
  12. /**
  13. ** @fn AddToQueue
  14. ** @brief Post a buffer to the destination task.
  15. ** @param pBuf - Buffer to be posted.
  16. ** @param Queuepath - Queuepath to post this buffer to.
  17. ** @return 0 if success, -1 if failed.
  18. ***/
  19. int
  20. AddToQueue (void* pBuf, INT8S *Queuepath,INT32U Size)
  21. {
  22. int Err,i=0;
  23. for(i=0;i< MAX_IPMI_IFCQ;i++)
  24. {
  25. if(strcmp(g_IPMIIfcQueue[i].IfcName,Queuepath) == 0)
  26. {
  27. // LOCK_QUEUE (g_IPMIIfcQueue[i].IfcQ);
  28. OS_ADD_TO_Q (pBuf, Size, g_IPMIIfcQueue[i].IfcQ,&Err);
  29. // UNLOCK_QUEUE (g_IPMIIfcQueue[i].IfcQ);
  30. if ((Err == -1) || (Err != Size))
  31. {
  32. printf ("Message.c : Post To Queue %x %s\n",errno, strerror(errno));
  33. return -1;
  34. }
  35. break;
  36. }
  37. }
  38. if(i == MAX_IPMI_IFCQ)
  39. {
  40. return -1;
  41. }
  42. return 0;
  43. }
  44. /**
  45. ** @fn AddQueue
  46. ** @brief Adds the Queue to the common Queue Handle structure
  47. ** @param Queuepath - Queue Path
  48. ** @return Return 0
  49. ***/
  50. int AddQueue(INT8S *Queuepath)
  51. {
  52. int i=0;
  53. struct stat buf;
  54. WAIT_QUEUE
  55. if(stat(Queuepath,&buf) == 0)
  56. {
  57. if(unlink(Queuepath) == -1)
  58. {
  59. printf("Failed to unlink %s \n",Queuepath);
  60. }
  61. }
  62. for(i=0; i< MAX_IPMI_IFCQ;i++)
  63. {
  64. if(strncmp(g_IPMIIfcQueue[i].IfcName,"",1) == 0)
  65. {
  66. strcpy(g_IPMIIfcQueue[i].IfcName,Queuepath);
  67. break;
  68. }
  69. }
  70. POST_QUEUE
  71. return 0;
  72. }
  73. /**
  74. ** @fn GetQueueHandle
  75. ** @brief Gets the Queue Handle from the Common Queue Handle Structure
  76. ** @param Queuepath - Queue Path
  77. ** @param IfcQ Handle for the needed Queue
  78. ** @return Return 0
  79. ***/
  80. int GetQueueHandle(INT8S *Queuepath,HQueue_T *IfcQ)
  81. {
  82. int i=0;
  83. WAIT_QUEUE
  84. for(i=0;i<MAX_IPMI_IFCQ;i++)
  85. {
  86. if(strcmp(g_IPMIIfcQueue[i].IfcName,Queuepath) == 0)
  87. {
  88. *IfcQ = g_IPMIIfcQueue[i].IfcQ;
  89. break;
  90. }
  91. }
  92. POST_QUEUE
  93. if(i == MAX_IPMI_IFCQ)
  94. {
  95. return -1;
  96. }
  97. return 0;
  98. }
  99. /**
  100. * @fn GetQueue
  101. * @brief Gets the Queue Handle from the Common
  102. * Queue Handle Structure
  103. * @param Queuepath - Queue Path
  104. * @return Return 0
  105. **/
  106. int GetQueue(INT8S *Queuepath,int flags)
  107. {
  108. int i=0;
  109. WAIT_QUEUE
  110. for(i=0;i<MAX_IPMI_IFCQ;i++)
  111. {
  112. if(strcmp(g_IPMIIfcQueue[i].IfcName,Queuepath) == 0)
  113. {
  114. g_IPMIIfcQueue[i].IfcQ = open (Queuepath, flags);
  115. if(g_IPMIIfcQueue[i].IfcQ == -1)
  116. {
  117. printf ("Error opening named pipe %s\n",Queuepath);
  118. perror("");
  119. }
  120. break;
  121. }
  122. }
  123. POST_QUEUE
  124. if(i == MAX_IPMI_IFCQ)
  125. {
  126. return -1;
  127. }
  128. return 0;
  129. }
  130. /**
  131. ** @fn PostMsg
  132. ** @brief Post a message to the destination task.
  133. ** @param MsgPkt - Message to be posted.
  134. ** @param Queue - Queue to post this message to.
  135. ** @return 0 if success, -1 if failed.
  136. ***/
  137. int
  138. PostMsg (MsgPkt_T* pMsgPkt, INT8S *Queuepath)
  139. {
  140. int Err,i=0;
  141. INT16U Size;
  142. Size = sizeof (MsgPkt_T) - MSG_PAYLOAD_SIZE + pMsgPkt->Size;
  143. // printf("Queuepath is %s\n",Queuepath);
  144. for(i=0;i<MAX_IPMI_IFCQ;i++)
  145. {
  146. if(strstr(g_IPMIIfcQueue[i].IfcName,Queuepath) != NULL)
  147. {
  148. //printf("_IPMIIfcQueue[i].IfcName is %s\n",g_IPMIIfcQueue[i].IfcName);
  149. //printf("g_IPMIIfcQueue[i].IfcQ is ");
  150. //printf("%d\n",g_IPMIIfcQueue[i].IfcQ);
  151. OS_ADD_TO_Q (pMsgPkt, Size, g_IPMIIfcQueue[i].IfcQ, &Err);
  152. if ((Err == -1) || (Err != Size))
  153. {
  154. printf ("Message.c : PostMsg %x %s\n",errno, strerror(errno));
  155. return -1;
  156. }
  157. break;
  158. }
  159. }
  160. if(i == MAX_IPMI_IFCQ)
  161. {
  162. return -1;
  163. }
  164. return 0;
  165. }
  166. /**
  167. * @fn GetMsg
  168. * @brief Gets the message posted to this task.
  169. * @param MsgPkt - Pointer to the buffer to hold the message packet.
  170. * @param Queue - Queue to fetch the message from.
  171. * @param Timeout - Number of seconds to wait.
  172. *
  173. * WAIT_NONE - If the function has to return immediately.
  174. * WAIT_INFINITE - If the function has to wait infinitely.
  175. * NOTE :
  176. * @return 0 if success, -1 if failed.
  177. **/
  178. int
  179. GetMsg (_FAR_ MsgPkt_T* pMsgPkt, INT8S *Queuepath, INT16S Timeout)
  180. {
  181. int Err,i=0;
  182. int Size;
  183. WAIT_QUEUE
  184. for(i=0;i<MAX_IPMI_IFCQ;i++)
  185. {
  186. if(strcmp(g_IPMIIfcQueue[i].IfcName,Queuepath) == 0)
  187. {
  188. break;
  189. }
  190. }
  191. POST_QUEUE
  192. if(i == MAX_IPMI_IFCQ)
  193. {
  194. printf("Message.c : Unable to Get Queue : %s",Queuepath);
  195. return -1;
  196. }
  197. /* check for limited wait time */
  198. if (Timeout >= 0)
  199. {
  200. struct timeval Timeval;
  201. fd_set fdRead;
  202. int n, RetVal;
  203. FD_ZERO (&fdRead);
  204. FD_SET (g_IPMIIfcQueue[i].IfcQ, &fdRead);
  205. n = g_IPMIIfcQueue[i].IfcQ + 1;
  206. Timeval.tv_sec = Timeout;
  207. Timeval.tv_usec = 0;
  208. RetVal = select (n, &fdRead, NULL, NULL, &Timeval);
  209. if (-1 == RetVal)
  210. {
  211. printf ("Message.c : Error waiting on Queue [Error : %s, Queue : %s, Timeout : %d]\n",
  212. strerror (errno), g_IPMIIfcQueue[i].IfcName, Timeout);
  213. return -1;
  214. }
  215. else if (0 == RetVal)
  216. {
  217. return -2;
  218. }
  219. }
  220. /* Get the header first*/
  221. Size = sizeof (MsgPkt_T) - MSG_PAYLOAD_SIZE;
  222. OS_GET_FROM_Q (pMsgPkt, Size, g_IPMIIfcQueue[i].IfcQ, WAIT_INFINITE, &Err);
  223. if (Err == -1)
  224. {
  225. IPMI_WARNING ("Message.c : GetMsg %s\n",strerror(errno));
  226. return -1;
  227. }
  228. /* Get the payload data */
  229. Size = pMsgPkt->Size;
  230. OS_GET_FROM_Q (pMsgPkt->Data, Size, g_IPMIIfcQueue[i].IfcQ, WAIT_INFINITE, &Err);
  231. if (Err == -1)
  232. {
  233. IPMI_WARNING ("Message.c : GetMsg %s\n",strerror(errno));
  234. return -1;
  235. }
  236. return 0;
  237. }