123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- /*
- main.c
- authod: wjl
- 应用程序主要使用pt来完成
- */
- #include "includes.h"
- //pt任务部分声明
- pt_timer_t pt_timerPool;
- struct pt pt_NetWorkTask;
- struct pt pt_QueueComReadTask;
- struct pt pt_QueueRecordReadTask;
- struct pt pt_DomainTask;
- struct pt pt_TTSTask;
- struct pt pt_PlayerTask;
- struct pt pt_RecorderTask;
- struct pt pt_AuthLoginTask;
- struct pt pt_LocationTask;
- struct pt pt_McuIapTask;
- struct pt pt_LbsMotionTask;
- struct pt pt_HookTask;
- struct pt pt_NearTask;
- struct pt pt_VoiceUploadTask;
- struct pt pt_HearTickTask;
- #ifdef ENABLE_ANT_MODE
- struct pt pt_McuAckTask;
- #endif
- #if defined ENABLE_TICKET
- struct pt pt_TicketTask;
- #endif
- /*读取模块相关信息*/
- void modemInfoRead(void){
- snprintf(app.opVersion,sizeof(app.opVersion),"%s",LSAPI_SYS_getOPVERSION());
- wlog_info("OpVersion:%s",app.opVersion);
- snprintf(app.fmVersion,sizeof(app.fmVersion),"%s",LSAPI_SYS_getFMVERSION());
- wlog_info("FmVersion:%s",app.fmVersion);
- }
- /*
- ptTickRun
- pt 需要的时间池用于pt任务调试
- */
- void ptTickRun(void){
- PTTimerTick(&pt_timerPool); //pt需要的系统时钟,最好是准确的
- }
- //初始化数据及外设接口
- static void InitStart(void){
- if(0!=msgQueueSet()){//初始化消息队列
- wlog_error("msgQueueSet err");
- }
- app_init();//应用数据测试,需要比para_inits先调用
- para_inits();//系统参数测试,需要在app_init后调用
- recordsInit();//初始化记录文件区
- authLoginInit();
- onWorkChecking(TRUE);
- onWorkTimerInit();
- userTimerInit();
- bspInit();//外设部分初始化
- #ifdef ENABLE_ANT_MODE
- wlog_info("%s VER:%s MODULE:%s TEST:%d CUST:%d ant mode start...", APP_NAME, APP_VERSION, APP_MODULE_TYPE, APP_TEST, APP_CUSTOMER);
- #else
- wlog_info("%s VER:%s MODULE:%s TEST:%d CUST:%d start...", APP_NAME, APP_VERSION, APP_MODULE_TYPE, APP_TEST, APP_CUSTOMER);
- #endif
- //先发消息,MCU先把电源锁住
- setInitMcu();
- //配置收到手机短信主动上报内容
- innerInfo("AT+CNMI=0,2,0,0,0\r\n",19);
- //再振动
- vibraNow();
- //检测是否有未播放文件
- if(TRUE==vFileGetUnplaySet()) vibratWorkNow(TRUE);
- }
- #ifdef ENABLE_ANT_MODE
- PT_THREAD (ptMcuAckTask(pt_timer_t *ptPool, struct pt *pt)){
- static pt_timer_t ptTimer;
- //定时发设置MCU指令给MCU
- PT_BEGIN(pt);
- while(1){
- setInitMcu();
- PTTimerStart(ptPool, &ptTimer,500);//need set to 1 seconds
- PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
- }
- PT_END(pt);
- }
- #endif
- /*
- taskRun
- 应用程序主要任务入口
- */
- static void taskRun(void){
- //深度休眠投票:每个PT如果空闲了,则向属于自己的票据投上
- //当票箱满员后,当前任务开启一个下次唤醒的定时器,并交出系统资源调度
- #ifndef ENABLE_ANT_MODE //天线调试模式
- if(app.tgMode==FALSE){
- //ptDomainTask-->domain.c 负责域名解析任务,应用将需要解析的要求推入域名列队,域名任务会自动消耗
- ptDomainTask(&pt_timerPool, &pt_DomainTask);//has better run first
- //ptNetWorkTask-->netWork.c 负责网络任务,包括注册,拨号,设备硬件参数等
- ptNetWorkTask(&pt_timerPool, &pt_NetWorkTask);
- //ptQueueRecordReadTask-->plyRec.c 语音消息队列读取任务
- ptQueueRecordReadTask(&pt_timerPool, &pt_QueueRecordReadTask);
- //ptTTSTask-->tts.c TTS播报任务
- ptTTSTask(&pt_timerPool, &pt_TTSTask);
- //ptPlayerTask-->plyRec.c 对讲语音播放逻辑控制任务
- ptPlayerTask(&pt_timerPool, &pt_PlayerTask);
- //ptRecorderTask-->plyRec.c 对讲语音录制逻辑控制任务
- ptRecorderTask(&pt_timerPool, &pt_RecorderTask);
- //ptAuthLoginTask-->authlogin.c 系统开机后登陆IOT服务器
- ptAuthLoginTask(&pt_timerPool, &pt_AuthLoginTask);
- //ptLocationTask-->location.c 负责处理GPS跟LSB业务
- ptLocationTask(&pt_timerPool, &pt_LocationTask);
- //ptMcuIapTask-->mcuIap.c 负责处理更新IAP的业务
- ptMcuIapTask(&pt_timerPool, &pt_McuIapTask);
- //ptLbsMotionTask-->location.c 负责LSB处理业务
- ptLbsMotionTask(&pt_timerPool, &pt_LbsMotionTask);
- //ptHookTask-->hook.c 钩子任务
- ptHookTask(&pt_timerPool, &pt_HookTask);
- //ptNearTask-->location.c 负责附近信息业务的收集
- ptNearTask(&pt_timerPool, &pt_NearTask);
- //ptVoiceUploadTask-->plyRec.c 录制语音上传服务器任务控制
- ptVoiceUploadTask(&pt_timerPool, &pt_VoiceUploadTask);
- //ptHearTickTask-->authlogin.c
- // ptHearTickTask(&pt_timerPool, &pt_HearTickTask);//请放在ptAuthLoginTask后面调用
- #if defined ENABLE_TICKET
- //ptTickTask-->ticket.c 深度睡眠任务
- ptTicketTask(&pt_timerPool, &pt_TicketTask);
- #endif
- }
- #else
- ptMcuAckTask(&pt_timerPool, &pt_McuAckTask);
- #endif
- if(FALSE==app.tmMode)
- //ptQueueComReadTask-->app.c 通用消息队列读取任务
- ptQueueComReadTask(&pt_timerPool, &pt_QueueComReadTask);
- }
- T_BOOL appQuit=FALSE;
- #ifdef USE_DETECTED_THREAD
- static unsigned int ptTickCnt=0;
- #endif
- /*定义一个定时器用于其它作用,比如按键SOS超时,定时器的话,在休眠时也能用*/
- LSAPI_OSI_Timer_t *ptimer_user = NULL;
- unsigned char userTimerFun=0;
- void userTimerCb(void *param){
- switch(userTimerFun){
- case 1:
- app.ksos=1;
- wlog_info("keyPress Timeout, vibra");
- vibraNow();
- if(app.onWorkZone==0 && sut_para.sosUpoadEnable_N==0) wlog_warn("un_onworkzone,sos disable, quit upload");
- else warnSendNow(WARN_SOS);
- break;
- }
- userTimerFun=0;
- if(NULL != ptimer_user) LSAPI_OSI_TimerStop(ptimer_user);
- }
- static void userTimerEntry(void *param){
- ptimer_user = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), userTimerCb, NULL);
- if(NULL==ptimer_user){
- wlog_error("ptimer_user timer error !!!!!");
- return;
- }
- wlog_info("ptimer_user entry start ok");
- LSAPI_OSI_Event_t event = {};
- for(;;){LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);}
- LSAPI_OSI_ThreadExit();
- }
- void userTimerInit(void){
- if(NULL==LSAPI_OSI_ThreadCreate("userTimerEntry", userTimerEntry, NULL, LSAPI_OSI_PRIORITY_NORMAL, USER_TIMER_THREAD_STACK, 4)){
- wlog_error("userTimer Thread create failed!");
- }
- }
- void userTimerStart(T_UINT32 timeMs,unsigned char fun){
- if(NULL==ptimer_user){
- wlog_error("ptimer_user null, start failed");
- return;
- }
- wlog_info("userTimerStart:%d ms",timeMs);
- userTimerFun=fun;
- LSAPI_OSI_TimerStart(ptimer_user, timeMs);
- }
- void userTimerSTOP(void){
- wlog_info("userTimer stop");
- if(NULL != ptimer_user) LSAPI_OSI_TimerStop(ptimer_user);
- }
- //主线程根据系统时间产生pt心跳,系统可以休眠
- static int64_t lasttimems;
- /*pt心跳*/
- void ptTimePro(void){
- int i;
- int64_t timems,j;
- timems=LSAPI_OSI_UpTime();
- j=timems-lasttimems;
- if(j>=10){
- for(i=0;i<j/10;i++) ptTickRun();
- lasttimems=timems-j%10;
- }else if(j<0) lasttimems=timems;//刷固件或其它情况,有可能LSAPI_OSI_UpTime返回的值有较大变化,会导致pt任务无法执行
- }
- void hpocRun(void){
- InitStart();
- wlog_info("start loop");
- modemInfoRead();
- lasttimems=LSAPI_OSI_UpTime();
- while(appQuit==FALSE){
- #ifdef USE_DETECTED_THREAD
- ptTickCnt++;
- #endif
- taskRun();
- LSAPI_OSI_ThreadSleep(5);
- ptTimePro();
- }
- }
- ///////////////////////////////////////////////
- #define TIME_OFFSET_SECOND 32*CFW_TIME_ZONE_SECOND
- int64_t getBaseSeconds(void){
- return (LSAPI_OSI_EpochSecond() + TIME_OFFSET_SECOND);
- }
- void getMyClock(MY_CLOCK *myclock){
- time_t lt = getBaseSeconds();
- struct tm ltm;
- gmtime_r(<, <m);
- myclock->year=ltm.tm_year + 1900;
- myclock->month=ltm.tm_mon+1;
- myclock->day=ltm.tm_mday;
- myclock->hour=ltm.tm_hour;//+USER_TIME_ZONE;
- myclock->min=ltm.tm_min;
- myclock->sec=ltm.tm_sec;
- }
- void setMyClock(MY_CLOCK myclock){
- struct tm ltm;
- memset(<m,0,sizeof(ltm));
- ltm.tm_year=myclock.year-1900;
- ltm.tm_mon=myclock.month-1;
- ltm.tm_mday=myclock.day;
- ltm.tm_hour=myclock.hour-USER_TIME_ZONE;
- ltm.tm_min=myclock.min;
- ltm.tm_sec=myclock.sec;
- LSAPI_OSI_SetEpochTime(1000*mktime(<m));
- }
- unsigned char *getBCDDateTime(void){
- static unsigned char bcdDate[6];
- MY_CLOCK myclock;
- getMyClock(&myclock);
- bcdDate[0]=decToBCD(myclock.year-2000);
- bcdDate[1]=decToBCD(myclock.month);
- bcdDate[2]=decToBCD(myclock.day);
- bcdDate[3]=decToBCD(myclock.hour);
- bcdDate[4]=decToBCD(myclock.min);
- bcdDate[5]=decToBCD(myclock.sec);
- return bcdDate;
- }
- #ifdef USE_DETECTED_THREAD
- //用于检测pt滴答是否正常
- void detector(void *param){
- char info[80];
- wlog_info("detector start");
- for(;;){
- LSAPI_OSI_ThreadSleep(500);
- snprintf(info, sizeof(info),"<tick>ptTickCnt:%d\r\n",ptTickCnt);
- outterInfo(info, strlen(info));
- }
- }
- #endif
|