ticket.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. #include "ticket.h"
  2. static T_UINT32 ticket;
  3. //清空投票
  4. void ticketReset(void){
  5. int i;
  6. ticket=0;
  7. //for(i=0;i<TICKET_PT_MAX;i++) ticketDeVote(i);
  8. ticketDeVote(TICKET_PT_DOMAIN);
  9. ticketDeVote(TICKET_PT_NETWORK);
  10. ticketDeVote(TICKET_PT_QUECOM);
  11. ticketDeVote(TICKET_PT_HOOK);
  12. ticketDeVote(TICKET_PT_TUP);
  13. }
  14. //投票
  15. void ticketVote(TICKET_ENUM vote){
  16. #if defined ENABLE_TICKET
  17. ticket &= ~(1<<vote);
  18. #endif
  19. }
  20. //撤票
  21. void ticketDeVote(TICKET_ENUM vote){
  22. #if defined ENABLE_TICKET
  23. ticket |= 1<<vote;
  24. #endif
  25. }
  26. //投票结果
  27. static T_BOOL isTicketReady(void){
  28. if(0==ticket) return TRUE;
  29. else return FALSE;
  30. }
  31. LSAPI_OSI_Thread_t *timeThread=NULL;
  32. LSAPI_OSI_Timer_t *ptimer_t = NULL;
  33. static T_BOOL timerSleepStatus=FALSE;
  34. void Timeroutcallback(void *param){
  35. wlog_info("Timeroutcallback enter");
  36. LSAPI_OSI_TimerStop(ptimer_t);
  37. threadPostEvent(timeThread,USER_EVENT_TIMEOUT);
  38. }
  39. /*
  40. 串口、网络等唤醒深度睡眠接口
  41. */
  42. void WakeupNow(char *name){
  43. #if defined ENABLE_TICKET
  44. if(ptimer_t==NULL || timeThread==NULL || timerSleepStatus==FALSE) return;
  45. wlog_info("WakeupNow by %s",name);
  46. LSAPI_OSI_TimerStop(ptimer_t);
  47. threadPostEvent(timeThread,USER_EVENT_TIMEOUT);
  48. #endif
  49. }
  50. static T_INT32 getMintWaitCnt(void);
  51. static void addWaitCnt(T_INT32 value);
  52. PT_THREAD (ptTicketTask(pt_timer_t *ptPool, struct pt *pt)){
  53. static pt_timer_t ptTimer;
  54. static T_UINT8 sleepDlyTime;
  55. T_INT32 value;
  56. T_UINT32 sleeptime;
  57. static int64_t lastSleepTime;
  58. char info[30];
  59. PT_BEGIN(pt);
  60. while(1){
  61. if(FALSE==isTicketReady()) sleepDlyTime=0;
  62. else{
  63. if(++sleepDlyTime>=3){//延时3秒
  64. sleepDlyTime=0;
  65. //开启下次唤醒的定时器,也就是发心跳的时刻即可
  66. value=getMintWaitCnt();
  67. if(value>0){
  68. sleeptime=value*1000;
  69. snprintf(info, sizeof(info),"Waitevent:%d\r\n",value);
  70. outterInfo(info,strlen(info));
  71. timeThread=LSAPI_OSI_ThreadCurrent();
  72. if(NULL == ptimer_t) ptimer_t = LSAPI_OSI_TimerCreate(timeThread, Timeroutcallback, NULL);
  73. if(NULL != ptimer_t){
  74. if(FALSE==LSAPI_OSI_TimerStart(ptimer_t, sleeptime)){
  75. wlog_warn("sleep timer start failed");
  76. goto TICKET_END;
  77. }
  78. }else{
  79. wlog_warn("sleep timer create failed");
  80. goto TICKET_END;
  81. }
  82. lastSleepTime=LSAPI_OSI_UpTime();
  83. LSAPI_OSI_Event_t event = {};
  84. timerSleepStatus=TRUE;
  85. while(1){
  86. LSAPI_OSI_EventWait(timeThread, &event);
  87. if(event.id==USER_EVENT_TIMEOUT) break;
  88. }
  89. value=(LSAPI_OSI_UpTime()-lastSleepTime)/1000;
  90. ticketReset();
  91. timerSleepStatus=FALSE;
  92. addWaitCnt(value);
  93. }else wlog_warn("value %d, no sleep",value);
  94. }
  95. }
  96. TICKET_END:
  97. PTTimerStart(ptPool, &ptTimer,100);//need set to 1 seconds
  98. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  99. }
  100. PT_END(pt);
  101. }
  102. static T_INT32 getMintWaitCnt(void){
  103. //获取所有计数器,并得出最小值
  104. #define VALUE_SIZE 3
  105. T_INT32 value[VALUE_SIZE],temp,i;
  106. temp=0xfffffff;
  107. for(i=0;i<VALUE_SIZE;i++) value[i]=0xfffffff;
  108. i=0;
  109. value[i++]=getPocMinCnt();
  110. #if defined ENABLE_BUBIAO_GPS
  111. value[i++]=getBubiaoMinCnt();
  112. value[i++]=getGpsMinCnt();
  113. #endif
  114. for(i=0;i<VALUE_SIZE;i++){
  115. temp=(value[i]>temp)?temp:value[i];
  116. }
  117. return temp;
  118. }
  119. static void addWaitCnt(T_INT32 value){
  120. //更新所有计数器值
  121. pocCntUpdate(value);
  122. #if defined ENABLE_BUBIAO_GPS
  123. bubiaoCntUpdate(value);
  124. gpsCntUpdate(value);
  125. #endif
  126. }
  127. /*定时显示一下票箱状态*/
  128. void ticketStatusShow(void){
  129. static unsigned char cnt=0;
  130. char info[100]="TICKET:";
  131. char tbuf[10];
  132. unsigned char i;
  133. if(cnt==0){
  134. if(ticket==0) wlog_info("TICKET:Idle");
  135. else{
  136. for(i=TICKET_PT_DOMAIN;i<TICKET_PT_MAX;i++){
  137. if(ticket & (1<<i)){
  138. snprintf(tbuf, sizeof(tbuf), "%d ",i);
  139. strcat(info, tbuf);
  140. }
  141. }
  142. wlog_info(info);
  143. }
  144. }
  145. if(++cnt>=10) cnt=0;//10秒打印一次
  146. }