iec10x.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #include "iec10x.h"
  2. #include "project_var.h"
  3. PIEC10X_T IEC10X = NULL;
  4. uint16_t Iec10x_Sta_Addr = 0;
  5. Iec10x_PrioQueue_T Iec10x_PrioQueueArray[IEC10X_PRIO_MAX];
  6. uint16_t IEC10X_Cp16time2a = 0;
  7. uint16_t IEC10X_Cp16time2a_V = 0;
  8. CP56Time2a_T IEC10X_Cp56time2a;
  9. /*******************************************************************************
  10. * Function Name : IEC10X_InitQ
  11. * Description : Init queue.
  12. * Input : None
  13. * Output : None
  14. * Return : 0 failure
  15. len sucess
  16. *******************************************************************************/
  17. void Iec10x_Lock(void)
  18. {
  19. #ifdef IEC10XLOCK
  20. if (IEC10X->LOCK != NULL)
  21. IEC10X->LOCK();
  22. #endif
  23. }
  24. void Iec10x_UnLock(void)
  25. {
  26. #ifdef IEC10XLOCK
  27. if (IEC10X->UNLOCK != NULL)
  28. IEC10X->UNLOCK();
  29. #endif
  30. }
  31. void IEC10X_InitQ(void)
  32. {
  33. uint8_t i;
  34. for (i = 0; i < IEC10X_PRIO_MAX; i++)
  35. {
  36. IEC10X->InitQueue(&Iec10x_PrioQueueArray[i]);
  37. }
  38. }
  39. void IEC10X_ClearQ(void)
  40. {
  41. uint8_t i;
  42. for (i = 0; i < IEC10X_PRIO_MAX; i++)
  43. {
  44. IEC10X->ClearQueue(&Iec10x_PrioQueueArray[i]);
  45. }
  46. }
  47. uint8_t IEC10X_GetPrio(uint8_t State)
  48. {
  49. /*Prio from 0~7*/
  50. uint8_t Prio = 0;
  51. return Prio;
  52. }
  53. void IEC10X_Enqueue(uint8_t *EnQBuf, uint16_t Length, uint8_t Prio,
  54. void (*IEC10XCallBack)(Iec10x_CallbackArg_T *Arg), Iec10x_CallbackArg_T *CallbackArg)
  55. {
  56. Iec10x_PrioNode_T *new_p;
  57. Iec10x_Lock();
  58. if (Length < IEC10X_HEADER_LENGTH)
  59. {
  60. LOG("IEC10X_Enqueue,buffer too short \r\n");
  61. goto END;
  62. }
  63. new_p = (Iec10x_PrioNode_T *)IEC10X->Malloc(sizeof(Iec10x_PrioNode_T) + Length - 1);
  64. if (new_p == NULL)
  65. {
  66. LOG("IEC10X_Enqueue,malloc error \r\n");
  67. goto END;
  68. }
  69. memcpy(new_p->value, EnQBuf, Length);
  70. new_p->Length = Length;
  71. /* Prio from 1~8, Array from 0~7*/
  72. if (Prio >= IEC10X_PRIO_MAX)
  73. {
  74. LOG("IEC10X_Enqueue, error Prio(%d) \r\n", Prio);
  75. goto END;
  76. }
  77. /* Set callback Argument */
  78. new_p->CallBack = IEC10XCallBack;
  79. if (CallbackArg != NULL)
  80. new_p->CallBackArg = *CallbackArg;
  81. new_p->CallBackArg.value = new_p->value;
  82. LOG("IEC10X_Enqueue,Prio(%d) elementNum(%d)len(%d)(%d) \r\n", Prio, Iec10x_PrioQueueArray[Prio].ElementNum, Length, new_p->Length);
  83. // DumpHEX(new_p->value, new_p->Length);
  84. IEC10X->enqueue(&Iec10x_PrioQueueArray[Prio], new_p);
  85. END:
  86. Iec10x_UnLock();
  87. return;
  88. }
  89. Iec10x_PrioNode_T *IEC10X_Dequeue(void)
  90. {
  91. uint8_t Prio;
  92. Iec10x_PrioNode_T *ret;
  93. Prio = IEC10X->GetPrio();
  94. if (Prio >= 8)
  95. {
  96. LOG("IEC10X_Dequeue, Error Prio(%d) \r\n", Prio);
  97. return 0;
  98. }
  99. ret = IEC10X->dequeue(&Iec10x_PrioQueueArray[Prio]);
  100. if (ret)
  101. {
  102. Iec10x_PrioQueueArray[Prio].ElementNum--;
  103. }
  104. LOG("IEC10X_Dequeue(%d) \r\n", ret->Length);
  105. return ret;
  106. }
  107. Iec10x_PrioNode_T *IEC10X_FindQHead(void)
  108. {
  109. uint8_t Prio;
  110. Iec10x_PrioNode_T *ret;
  111. Prio = IEC10X->GetPrio();
  112. if (Prio >= 8)
  113. {
  114. return 0;
  115. }
  116. ret = IEC10X->FindQHead(&Iec10x_PrioQueueArray[Prio]);
  117. return ret;
  118. }
  119. void Iec10x_Scheduled(int socketfd)
  120. {
  121. Iec10x_PrioNode_T *DeQNode;
  122. DeQNode = IEC10X_Dequeue();
  123. if (DeQNode)
  124. {
  125. LOG("<<-------------------IEC10X (%d)-------------------->> \r\n", DeQNode->Length);
  126. /* call back funtion */
  127. if (DeQNode->CallBack)
  128. DeQNode->CallBack(&(DeQNode->CallBackArg));
  129. DumpHEX(DeQNode->value, DeQNode->Length);
  130. IEC10X->Send(socketfd, (char *)(DeQNode->value), DeQNode->Length);
  131. Iec10x_Lock();
  132. IEC10X->Free(DeQNode);
  133. Iec10x_UnLock();
  134. }
  135. }
  136. /*******************************************************************************
  137. * Function Name : RegisterSim900aMoudle
  138. * Description : RegisterSim900aMoudle program
  139. * Input : None
  140. * Output : None
  141. * Return : None
  142. *******************************************************************************/
  143. int32_t RegisterIEC10XMoudle(void *_IEC10X)
  144. {
  145. int ret;
  146. if (NULL == _IEC10X)
  147. {
  148. return -1;
  149. }
  150. else
  151. {
  152. IEC10X = (PIEC10X_T)_IEC10X;
  153. if (NULL == IEC10X->Init)
  154. {
  155. return -1;
  156. }
  157. else
  158. {
  159. IEC10X_InitQ();
  160. // IEC10X_InitInfo();
  161. ret = IEC10X->Init();
  162. if (0 == ret)
  163. {
  164. #ifdef IEC101_STM32
  165. LOG("\r\nRegister \"%s\" IEC101 Success, < HuiXing 2014-2015 > ...\r\n", IEC10X->name);
  166. #elif defined(IEC104_STM32)
  167. LOG("\r\nRegister \"%s\" IEC104 Success, < HuiXing 2014-2015 > ...\r\n", IEC10X->name);
  168. #endif
  169. }
  170. return ret;
  171. }
  172. }
  173. }