message.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <sys/socket.h>
  5. #include <sys/un.h>
  6. #include <sys/prctl.h>
  7. #include "com_IPMI_App.h"
  8. #include "com_IPMIDefs.h"
  9. #include "com_Message.h"
  10. #include <errno.h>
  11. #include <fcntl.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15. /**
  16. ** @fn PostMsg
  17. ** @brief Post a message to the destination task.
  18. ** @param MsgPkt - Message to be posted.
  19. ** @param Queue - Queue to post this message to.
  20. ** @return 0 if success, -1 if failed.
  21. ***/
  22. int
  23. PostMsg (int fd, MsgPkt_T* pMsgPkt)
  24. {
  25. int Err,i=0;
  26. uint16_t Size;
  27. Size = sizeof (MsgPkt_T) - MSG_PAYLOAD_SIZE + pMsgPkt->Size;
  28. Err = write (fd, pMsgPkt, Size) ;
  29. if ((Err == -1) || (Err != Size))
  30. {
  31. printf ("Message.c : PostMsg %x %s\n",errno, strerror(errno));
  32. return -1;
  33. }
  34. return 0;
  35. }
  36. /*------ Get the message from the queue -----------------------------*/
  37. int OS_GET_FROM_Q( int fd, uint8_t *pBuf, int Size, int16_t timeout)
  38. {
  39. int ReadLen = 0, Left, Len;
  40. int Err = 0;
  41. Left = Size;
  42. while( Left > 0 )
  43. {
  44. Len = read (fd, (uint8_t*)pBuf + ReadLen, Left );
  45. if( Len < 0 )
  46. {
  47. if( errno == EINTR || errno == EAGAIN )
  48. {
  49. continue;
  50. }
  51. else
  52. {
  53. Err = -1;
  54. break;
  55. }
  56. }
  57. ReadLen += Len;
  58. Left -= Len;
  59. }
  60. Err = ReadLen;
  61. return Err;
  62. }
  63. /**
  64. * @fn GetMsg
  65. * @brief Gets the message posted to this task.
  66. * @param MsgPkt - Pointer to the buffer to hold the message packet.
  67. * @param Queue - Queue to fetch the message from.
  68. * @param Timeout - Number of seconds to wait.
  69. *
  70. * WAIT_NONE - If the function has to return immediately.
  71. * WAIT_INFINITE - If the function has to wait infinitely.
  72. * NOTE :
  73. * @return 0 if success, -1 if failed.
  74. **/
  75. int
  76. GetMsg (int fd, MsgPkt_T* pMsgPkt, int16_t Timeout)
  77. {
  78. int Err;
  79. int Size;
  80. /* check for limited wait time */
  81. if (Timeout >= 0)
  82. {
  83. struct timeval Timeval;
  84. fd_set fdRead;
  85. int n, RetVal;
  86. FD_ZERO (&fdRead);
  87. FD_SET (fd, &fdRead);
  88. n = fd + 1;
  89. Timeval.tv_sec = Timeout;
  90. Timeval.tv_usec = 0;
  91. RetVal = select (n, &fdRead, NULL, NULL, &Timeval);
  92. if (-1 == RetVal)
  93. {
  94. printf ("Message.c : Error waiting on Queue, log1\n");
  95. return -1;
  96. }
  97. else if (0 == RetVal)
  98. {
  99. //printf ("Message.c : Error waiting on Queue, log2\n");
  100. return -2;
  101. }
  102. }
  103. /* Get the header first*/
  104. Size = sizeof (MsgPkt_T) - MSG_PAYLOAD_SIZE;
  105. if(-1 == OS_GET_FROM_Q (fd, (uint8_t*)pMsgPkt, Size, WAIT_INFINITE))
  106. {
  107. printf ("Message.c : GetMsg %s\n",strerror(errno));
  108. return -1;
  109. }
  110. /* Get the payload data */
  111. Size = pMsgPkt->Size;
  112. if(-1 == OS_GET_FROM_Q (fd, pMsgPkt->Data, Size, WAIT_INFINITE))
  113. {
  114. printf ("Message.c : GetMsg %s\n",strerror(errno));
  115. return -1;
  116. }
  117. return 0;
  118. }