main.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /*
  2. main.c
  3. authod: wjl
  4. 应用程序主要使用pt来完成
  5. */
  6. #include "includes.h"
  7. //pt任务部分声明
  8. pt_timer_t pt_timerPool;
  9. struct pt pt_NetWorkTask;
  10. struct pt pt_QueueComReadTask;
  11. struct pt pt_QueueRecordReadTask;
  12. struct pt pt_DomainTask;
  13. struct pt pt_TTSTask;
  14. struct pt pt_PlayerTask;
  15. struct pt pt_RecorderTask;
  16. struct pt pt_AuthLoginTask;
  17. struct pt pt_LocationTask;
  18. struct pt pt_McuIapTask;
  19. struct pt pt_LbsMotionTask;
  20. struct pt pt_HookTask;
  21. struct pt pt_NearTask;
  22. struct pt pt_VoiceUploadTask;
  23. struct pt pt_HearTickTask;
  24. #ifdef ENABLE_ANT_MODE
  25. struct pt pt_McuAckTask;
  26. #endif
  27. #if defined ENABLE_TICKET
  28. struct pt pt_TicketTask;
  29. #endif
  30. /*读取模块相关信息*/
  31. void modemInfoRead(void){
  32. snprintf(app.opVersion,sizeof(app.opVersion),"%s",LSAPI_SYS_getOPVERSION());
  33. wlog_info("OpVersion:%s",app.opVersion);
  34. snprintf(app.fmVersion,sizeof(app.fmVersion),"%s",LSAPI_SYS_getFMVERSION());
  35. wlog_info("FmVersion:%s",app.fmVersion);
  36. }
  37. /*
  38. ptTickRun
  39. pt 需要的时间池用于pt任务调试
  40. */
  41. void ptTickRun(void){
  42. PTTimerTick(&pt_timerPool); //pt需要的系统时钟,最好是准确的
  43. }
  44. //初始化数据及外设接口
  45. static void InitStart(void){
  46. if(0!=msgQueueSet()){//初始化消息队列
  47. wlog_error("msgQueueSet err");
  48. }
  49. app_init();//应用数据测试,需要比para_inits先调用
  50. para_inits();//系统参数测试,需要在app_init后调用
  51. recordsInit();//初始化记录文件区
  52. authLoginInit();
  53. onWorkChecking(TRUE);
  54. onWorkTimerInit();
  55. userTimerInit();
  56. bspInit();//外设部分初始化
  57. #ifdef ENABLE_ANT_MODE
  58. 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);
  59. #else
  60. wlog_info("%s VER:%s MODULE:%s TEST:%d CUST:%d start...", APP_NAME, APP_VERSION, APP_MODULE_TYPE, APP_TEST, APP_CUSTOMER);
  61. #endif
  62. //先发消息,MCU先把电源锁住
  63. setInitMcu();
  64. //配置收到手机短信主动上报内容
  65. innerInfo("AT+CNMI=0,2,0,0,0\r\n",19);
  66. //再振动
  67. vibraNow();
  68. //检测是否有未播放文件
  69. if(TRUE==vFileGetUnplaySet()) vibratWorkNow(TRUE);
  70. }
  71. #ifdef ENABLE_ANT_MODE
  72. PT_THREAD (ptMcuAckTask(pt_timer_t *ptPool, struct pt *pt)){
  73. static pt_timer_t ptTimer;
  74. //定时发设置MCU指令给MCU
  75. PT_BEGIN(pt);
  76. while(1){
  77. setInitMcu();
  78. PTTimerStart(ptPool, &ptTimer,500);//need set to 1 seconds
  79. PT_WAIT_UNTIL(pt, PTTimerIsExpired(&ptTimer));
  80. }
  81. PT_END(pt);
  82. }
  83. #endif
  84. /*
  85. taskRun
  86. 应用程序主要任务入口
  87. */
  88. static void taskRun(void){
  89. //深度休眠投票:每个PT如果空闲了,则向属于自己的票据投上
  90. //当票箱满员后,当前任务开启一个下次唤醒的定时器,并交出系统资源调度
  91. #ifndef ENABLE_ANT_MODE //天线调试模式
  92. if(app.tgMode==FALSE){
  93. //ptDomainTask-->domain.c 负责域名解析任务,应用将需要解析的要求推入域名列队,域名任务会自动消耗
  94. ptDomainTask(&pt_timerPool, &pt_DomainTask);//has better run first
  95. //ptNetWorkTask-->netWork.c 负责网络任务,包括注册,拨号,设备硬件参数等
  96. ptNetWorkTask(&pt_timerPool, &pt_NetWorkTask);
  97. //ptQueueRecordReadTask-->plyRec.c 语音消息队列读取任务
  98. ptQueueRecordReadTask(&pt_timerPool, &pt_QueueRecordReadTask);
  99. //ptTTSTask-->tts.c TTS播报任务
  100. ptTTSTask(&pt_timerPool, &pt_TTSTask);
  101. //ptPlayerTask-->plyRec.c 对讲语音播放逻辑控制任务
  102. ptPlayerTask(&pt_timerPool, &pt_PlayerTask);
  103. //ptRecorderTask-->plyRec.c 对讲语音录制逻辑控制任务
  104. ptRecorderTask(&pt_timerPool, &pt_RecorderTask);
  105. //ptAuthLoginTask-->authlogin.c 系统开机后登陆IOT服务器
  106. ptAuthLoginTask(&pt_timerPool, &pt_AuthLoginTask);
  107. //ptLocationTask-->location.c 负责处理GPS跟LSB业务
  108. ptLocationTask(&pt_timerPool, &pt_LocationTask);
  109. //ptMcuIapTask-->mcuIap.c 负责处理更新IAP的业务
  110. ptMcuIapTask(&pt_timerPool, &pt_McuIapTask);
  111. //ptLbsMotionTask-->location.c 负责LSB处理业务
  112. ptLbsMotionTask(&pt_timerPool, &pt_LbsMotionTask);
  113. //ptHookTask-->hook.c 钩子任务
  114. ptHookTask(&pt_timerPool, &pt_HookTask);
  115. //ptNearTask-->location.c 负责附近信息业务的收集
  116. ptNearTask(&pt_timerPool, &pt_NearTask);
  117. //ptVoiceUploadTask-->plyRec.c 录制语音上传服务器任务控制
  118. ptVoiceUploadTask(&pt_timerPool, &pt_VoiceUploadTask);
  119. //ptHearTickTask-->authlogin.c
  120. // ptHearTickTask(&pt_timerPool, &pt_HearTickTask);//请放在ptAuthLoginTask后面调用
  121. #if defined ENABLE_TICKET
  122. //ptTickTask-->ticket.c 深度睡眠任务
  123. ptTicketTask(&pt_timerPool, &pt_TicketTask);
  124. #endif
  125. }
  126. #else
  127. ptMcuAckTask(&pt_timerPool, &pt_McuAckTask);
  128. #endif
  129. if(FALSE==app.tmMode)
  130. //ptQueueComReadTask-->app.c 通用消息队列读取任务
  131. ptQueueComReadTask(&pt_timerPool, &pt_QueueComReadTask);
  132. }
  133. T_BOOL appQuit=FALSE;
  134. #ifdef USE_DETECTED_THREAD
  135. static unsigned int ptTickCnt=0;
  136. #endif
  137. /*定义一个定时器用于其它作用,比如按键SOS超时,定时器的话,在休眠时也能用*/
  138. LSAPI_OSI_Timer_t *ptimer_user = NULL;
  139. unsigned char userTimerFun=0;
  140. void userTimerCb(void *param){
  141. switch(userTimerFun){
  142. case 1:
  143. app.ksos=1;
  144. wlog_info("keyPress Timeout, vibra");
  145. vibraNow();
  146. if(app.onWorkZone==0 && sut_para.sosUpoadEnable_N==0) wlog_warn("un_onworkzone,sos disable, quit upload");
  147. else warnSendNow(WARN_SOS);
  148. break;
  149. }
  150. userTimerFun=0;
  151. if(NULL != ptimer_user) LSAPI_OSI_TimerStop(ptimer_user);
  152. }
  153. static void userTimerEntry(void *param){
  154. ptimer_user = LSAPI_OSI_TimerCreate(LSAPI_OSI_ThreadCurrent(), userTimerCb, NULL);
  155. if(NULL==ptimer_user){
  156. wlog_error("ptimer_user timer error !!!!!");
  157. return;
  158. }
  159. wlog_info("ptimer_user entry start ok");
  160. LSAPI_OSI_Event_t event = {};
  161. for(;;){LSAPI_OSI_EventWait(LSAPI_OSI_ThreadCurrent(), &event);}
  162. LSAPI_OSI_ThreadExit();
  163. }
  164. void userTimerInit(void){
  165. if(NULL==LSAPI_OSI_ThreadCreate("userTimerEntry", userTimerEntry, NULL, LSAPI_OSI_PRIORITY_NORMAL, USER_TIMER_THREAD_STACK, 4)){
  166. wlog_error("userTimer Thread create failed!");
  167. }
  168. }
  169. void userTimerStart(T_UINT32 timeMs,unsigned char fun){
  170. if(NULL==ptimer_user){
  171. wlog_error("ptimer_user null, start failed");
  172. return;
  173. }
  174. wlog_info("userTimerStart:%d ms",timeMs);
  175. userTimerFun=fun;
  176. LSAPI_OSI_TimerStart(ptimer_user, timeMs);
  177. }
  178. void userTimerSTOP(void){
  179. wlog_info("userTimer stop");
  180. if(NULL != ptimer_user) LSAPI_OSI_TimerStop(ptimer_user);
  181. }
  182. //主线程根据系统时间产生pt心跳,系统可以休眠
  183. static int64_t lasttimems;
  184. /*pt心跳*/
  185. void ptTimePro(void){
  186. int i;
  187. int64_t timems,j;
  188. timems=LSAPI_OSI_UpTime();
  189. j=timems-lasttimems;
  190. if(j>=10){
  191. for(i=0;i<j/10;i++) ptTickRun();
  192. lasttimems=timems-j%10;
  193. }else if(j<0) lasttimems=timems;//刷固件或其它情况,有可能LSAPI_OSI_UpTime返回的值有较大变化,会导致pt任务无法执行
  194. }
  195. void hpocRun(void){
  196. InitStart();
  197. wlog_info("start loop");
  198. modemInfoRead();
  199. lasttimems=LSAPI_OSI_UpTime();
  200. while(appQuit==FALSE){
  201. #ifdef USE_DETECTED_THREAD
  202. ptTickCnt++;
  203. #endif
  204. taskRun();
  205. LSAPI_OSI_ThreadSleep(5);
  206. ptTimePro();
  207. }
  208. }
  209. ///////////////////////////////////////////////
  210. #define TIME_OFFSET_SECOND 32*CFW_TIME_ZONE_SECOND
  211. int64_t getBaseSeconds(void){
  212. return (LSAPI_OSI_EpochSecond() + TIME_OFFSET_SECOND);
  213. }
  214. void getMyClock(MY_CLOCK *myclock){
  215. time_t lt = getBaseSeconds();
  216. struct tm ltm;
  217. gmtime_r(&lt, &ltm);
  218. myclock->year=ltm.tm_year + 1900;
  219. myclock->month=ltm.tm_mon+1;
  220. myclock->day=ltm.tm_mday;
  221. myclock->hour=ltm.tm_hour;//+USER_TIME_ZONE;
  222. myclock->min=ltm.tm_min;
  223. myclock->sec=ltm.tm_sec;
  224. }
  225. void setMyClock(MY_CLOCK myclock){
  226. struct tm ltm;
  227. memset(&ltm,0,sizeof(ltm));
  228. ltm.tm_year=myclock.year-1900;
  229. ltm.tm_mon=myclock.month-1;
  230. ltm.tm_mday=myclock.day;
  231. ltm.tm_hour=myclock.hour-USER_TIME_ZONE;
  232. ltm.tm_min=myclock.min;
  233. ltm.tm_sec=myclock.sec;
  234. LSAPI_OSI_SetEpochTime(1000*mktime(&ltm));
  235. }
  236. unsigned char *getBCDDateTime(void){
  237. static unsigned char bcdDate[6];
  238. MY_CLOCK myclock;
  239. getMyClock(&myclock);
  240. bcdDate[0]=decToBCD(myclock.year-2000);
  241. bcdDate[1]=decToBCD(myclock.month);
  242. bcdDate[2]=decToBCD(myclock.day);
  243. bcdDate[3]=decToBCD(myclock.hour);
  244. bcdDate[4]=decToBCD(myclock.min);
  245. bcdDate[5]=decToBCD(myclock.sec);
  246. return bcdDate;
  247. }
  248. #ifdef USE_DETECTED_THREAD
  249. //用于检测pt滴答是否正常
  250. void detector(void *param){
  251. char info[80];
  252. wlog_info("detector start");
  253. for(;;){
  254. LSAPI_OSI_ThreadSleep(500);
  255. snprintf(info, sizeof(info),"<tick>ptTickCnt:%d\r\n",ptTickCnt);
  256. outterInfo(info, strlen(info));
  257. }
  258. }
  259. #endif