123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- #include "ticket.h"
- static T_UINT32 ticket;
- //清空投票
- void ticketReset(void){
- int i;
- ticket=0;
- for(i=0;i<TICKET_PT_MAX;i++) ticketDeVote(i);
- }
- //投票
- void ticketVote(TICKET_ENUM vote){
- #if defined ENABLE_TICKET
- ticket &= ~(1<<vote);
- #endif
- }
- //撤票
- void ticketDeVote(TICKET_ENUM vote){
- #if defined ENABLE_TICKET
- ticket |= 1<<vote;
- #endif
- }
- //投票结果
- static T_BOOL isTicketReady(void){
- if(0==ticket) return TRUE;
- else return FALSE;
- }
- LSAPI_OSI_Thread_t *timeThread=NULL;
- LSAPI_OSI_Timer_t *ptimer_t = NULL;
- static T_BOOL timerSleepStatus=FALSE;
- void Timeroutcallback(void *param){
- wlog_info("Timeroutcallback enter");
- LSAPI_OSI_TimerStop(ptimer_t);
- threadPostEvent(timeThread,USER_EVENT_TIMEOUT,NULL);
- }
- /*
- 串口、网络等唤醒深度睡眠接口
- */
- void WakeupNow(char *name){
- #if defined ENABLE_TICKET
- if(ptimer_t==NULL || timeThread==NULL || timerSleepStatus==FALSE) return;
- wlog_info("WakeupNow by %s",name);
- LSAPI_OSI_TimerStop(ptimer_t);
- threadPostEvent(timeThread,USER_EVENT_TIMEOUT,NULL);
- #endif
- }
- static T_INT32 geMintWaitCnt(void);
- static void addWaitCnt(T_INT32 value);
- PT_THREAD (ptTicketTask(pt_timer_t *ptPool, struct pt *pt)){
- static pt_timer_t ptTimer;
- static T_UINT8 sleepDlyTime;
- T_INT32 value;
- static T_UINT32 sleeptime;
- static int64_t lastSleepTime;
- char info[30];
- PT_BEGIN(pt);
- while(1){
- if(FALSE==isTicketReady()) sleepDlyTime=0;
- else{
- if(++sleepDlyTime>=2){//延时2秒睡
- sleepDlyTime=0;
- //开启下次唤醒的定时器,也就是发心跳的时刻即可
- value=geMintWaitCnt();
- if(value>0){
- sleeptime=value*1000;
- snprintf(info, sizeof(info),"WaitEventTime:%d\r\n",value);
- outterInfo(info, strlen(info));
- //wlog_info("lastSleepTime:%d",value);
- timeThread=LSAPI_OSI_ThreadCurrent();
- if(NULL == ptimer_t) ptimer_t = LSAPI_OSI_TimerCreate(timeThread, Timeroutcallback, NULL);
- if(NULL != ptimer_t){
- if(FALSE==LSAPI_OSI_TimerStart(ptimer_t, sleeptime)){
- wlog_warn("sleep timer start failed");
- goto TICKET_END;
- }
- }else{
- wlog_warn("sleep timer create failed");
- goto TICKET_END;
- }
- lastSleepTime=LSAPI_OSI_UpTime();
- LSAPI_OSI_Event_t event = {};
- timerSleepStatus=TRUE;
- while(1){
- LSAPI_OSI_EventWait(timeThread, &event);
- if(event.id==USER_EVENT_TIMEOUT) break;
- }
- value=(LSAPI_OSI_UpTime()-lastSleepTime)/1000;
- ticketReset();
- timerSleepStatus=FALSE;
- //检测是否为负值,是否有可能获取时间的API会出现异常,导致出现负值,如果是负值,则使用sleeptime,可能有一秒误差,但没关系
- if(value<0) value=sleeptime/1000;//fanghu
- addWaitCnt(value);//更新相关计数器
- }
- }
- }
- TICKET_END:
- PTTimerStart(ptPool, &ptTimer,100);//need set to 1 seconds
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- PT_END(pt);
- }
- static T_INT32 geMintWaitCnt(void){
- //获取所有计数器,并得出最小值
- #define VALUE_SIZE 6
- T_INT32 value[VALUE_SIZE],temp,i;
- temp=NO_SLEEP_TIME;
- for(i=0;i<VALUE_SIZE;i++) value[i]=NO_SLEEP_TIME;
- i=0;
- value[i++]=getAuthLoginCnt();
- value[i++]=getMaipCnt();
- value[i++]=getLocationCnt();
- value[i++]=getGnssCnt();
- value[i++]=getNearCnt();
- value[i++]=getHeartCnt();
- for(i=0;i<VALUE_SIZE;i++){
- temp=(value[i]>temp)?temp:value[i];
- }
- return temp;
- }
- static void addWaitCnt(T_INT32 value){
- //更新所有计数器值
- authLoginCntUpdate(value);
- miapCntUpdate(value);
- locationCntUpdate(value);
- //gnss,near使用的是本地时间,不需要更新
- heartCntUpdate(value);
- }
- /*定时显示一下票箱状态*/
- void ticketStatusShow(void){
- static unsigned char cnt=0;
- char info[100]="TICKET:";
- char tbuf[10];
- unsigned char i;
- if(cnt==0){
- if(ticket==0) wlog_info("TICKET:Idle");
- else{
- for(i=TICKET_PT_DOMAIN;i<TICKET_PT_MAX;i++){
- if(ticket & (1<<i)){
- snprintf(tbuf, sizeof(tbuf), "%d ",i);
- strcat(info, tbuf);
- }
- }
- wlog_info(info);
- }
- }
- if(++cnt>=10) cnt=0;//10秒打印一次
- }
|