iec10x.c 4.9 KB

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