message.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  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. //printf("---> PostMsg ok, fd = %d\n", fd);
  35. return 0;
  36. }
  37. /*------ Get the message from the queue -----------------------------*/
  38. int OS_GET_FROM_Q( int fd, uint8_t *pBuf, int Size, int16_t timeout)
  39. {
  40. int ReadLen = 0, Left, Len;
  41. int Err = 0;
  42. Left = Size;
  43. while( Left > 0 )
  44. {
  45. Len = read (fd, (uint8_t*)pBuf + ReadLen, Left );
  46. if( Len < 0 )
  47. {
  48. if( errno == EINTR || errno == EAGAIN )
  49. {
  50. continue;
  51. }
  52. else
  53. {
  54. Err = -1;
  55. break;
  56. }
  57. }
  58. ReadLen += Len;
  59. Left -= Len;
  60. }
  61. Err = ReadLen;
  62. return Err;
  63. }
  64. /**
  65. * @fn GetMsg
  66. * @brief Gets the message posted to this task.
  67. * @param MsgPkt - Pointer to the buffer to hold the message packet.
  68. * @param Queue - Queue to fetch the message from.
  69. * @param Timeout - Number of seconds to wait.
  70. *
  71. * WAIT_NONE - If the function has to return immediately.
  72. * WAIT_INFINITE - If the function has to wait infinitely.
  73. * NOTE :
  74. * @return 0 if success, -1 if failed.
  75. **/
  76. int
  77. GetMsg (int fd, MsgPkt_T* pMsgPkt, int16_t Timeout)
  78. {
  79. int Err;
  80. int Size;
  81. /* check for limited wait time */
  82. if (Timeout >= 0)
  83. {
  84. struct timeval Timeval;
  85. fd_set fdRead;
  86. int n, RetVal;
  87. FD_ZERO (&fdRead);
  88. FD_SET (fd, &fdRead);
  89. n = fd + 1;
  90. Timeval.tv_sec = Timeout;
  91. Timeval.tv_usec = 0;
  92. RetVal = select (n, &fdRead, NULL, NULL, &Timeval);
  93. if (-1 == RetVal)
  94. {
  95. printf ("Message.c : Error waiting on Queue, log1\n");
  96. return -1;
  97. }
  98. else if (0 == RetVal)
  99. {
  100. //printf ("Message.c : Error waiting on Queue, log2\n");
  101. return -2;
  102. }
  103. }
  104. /* Get the header first*/
  105. Size = sizeof (MsgPkt_T) - MSG_PAYLOAD_SIZE;
  106. if(-1 == OS_GET_FROM_Q (fd, (uint8_t*)pMsgPkt, Size, WAIT_INFINITE))
  107. {
  108. printf ("Message.c : GetMsg %s\n",strerror(errno));
  109. return -1;
  110. }
  111. /* Get the payload data */
  112. Size = pMsgPkt->Size;
  113. if(-1 == OS_GET_FROM_Q (fd, pMsgPkt->Data, Size, WAIT_INFINITE))
  114. {
  115. printf ("Message.c : GetMsg %s\n",strerror(errno));
  116. return -1;
  117. }
  118. return 0;
  119. }