MainTask.c 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048
  1. /********************************************************************************
  2. * File Name: MainTask.c
  3. * Function Describe:The main task for the system
  4. * Relate Module:
  5. * Explain: Hardware version is HS121
  6. * Writer: ShiLiangWen
  7. * Date: 2015.7.8
  8. *******************************************************************************/
  9. #define THIS_FILE_ID 8
  10. //---------------------------------------------------------------------------------
  11. #include "includes.h"
  12. #include "stm32f10x_it.h"
  13. //OS ID
  14. OS_TID idMainTask=0;
  15. U64 stkMainTask[MAIN_TASK_STK_SIZE];
  16. unsigned char modemInitReady=0;//0模块没开始初始化 1模块初始化成功 2模块初始化失败
  17. unsigned char modemPwrOffNow=0;
  18. unsigned char g_ucBackLightCnt;
  19. int g_iVbat=0;
  20. unsigned int g_LCDTemp = 0;
  21. unsigned char g_ucGtMode=0;
  22. unsigned char g_usOptionCnt;
  23. char sysStartTTS[START_TTS_LENGTH];
  24. //SUT_MSG_QUEUE ModemMsgQueue;
  25. //char ModemMsgBuffer[512];
  26. void FingerDaemonfinger(void);
  27. unsigned char g_ucUpdataVer=0;
  28. unsigned char g_ucKeyDownPressFlag=0;
  29. unsigned char g_ucGroupSelect=0;
  30. unsigned char g_ucUserSelect=0;
  31. unsigned char g_ucGroup_User_Select=0;
  32. unsigned char g_ucFun1KeyPressCt=0;//Fun1Key持续按下计数器,松开后清零
  33. unsigned char g_ucFun2KeyPressCt=0;//Fun2Key持续按下计数器,松开后清零
  34. int g_iFun1KeyEncodeCt=0; //Fun1Key按下时旋转Encode次数计数器,正数为顺时针,负数为逆时针,松开后清零
  35. int g_iFun2KeyEncodeCt=0; //Fun2Key按下时旋转Encode次数计数器,正数为顺时针,负数为逆时针,松开后清零
  36. unsigned int g_uikey_up_stat =0; //快捷键,用于进入GT模式 按下KEY_UP再按下OK
  37. unsigned char g_ucEnterGroupFlag=0;
  38. unsigned char pocTimeOutTime=30;//默认30分钟
  39. //======================================
  40. void EncodeKeyProcess(void);
  41. void iap_jump_app_s(void);
  42. int GetCheckCodeByPsnMEID(char *pCheckCode,unsigned long PSN,char *pMEID);
  43. void ReportCheckCode(void);
  44. //-----
  45. #ifdef USE_RUNNING_MAKE
  46. void RunningMake(unsigned short FileID,unsigned long line)
  47. {
  48. g_usFileID=FileID;
  49. g_ulFileLine=line;
  50. }
  51. #endif
  52. unsigned short g_usFileID=0;
  53. unsigned long g_ulFileLine=0;
  54. // unsigned char m_GroupSelect=0;//用于使用左边上下键去选择群组时使用 wjl
  55. unsigned char m_GroupSwitch=0;//表示是否曾经按过左边上下群组切换按键
  56. unsigned char m_GroupSwitchCnt=0;//按过的话,5秒内没按OK键则退出切换
  57. /***********************************************
  58. 强制复位
  59. ************************************************/
  60. void SystemReset(void)
  61. {
  62. //IWDG_Configuration();
  63. while(1);
  64. }
  65. void PowerCtrlInit(void)
  66. {
  67. GPIO_InitTypeDef GPIO_InitStructure;
  68. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
  69. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  70. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  71. GPIO_InitStructure.GPIO_Pin = PWR_EN_PIN;
  72. GPIO_Init(PWR_EN_PORT, &GPIO_InitStructure);
  73. PWR_EN_LOW;
  74. }
  75. /*************************************************************************
  76. 拷贝数据,并判断与原数据是否相同
  77. 相同返回0
  78. 不同返回1
  79. **************************************************************************/
  80. unsigned char CopyData(unsigned char *pDst,unsigned char *pSrc,unsigned char len)
  81. {
  82. unsigned char i;
  83. unsigned char flag=0;
  84. for(i=0;i<len;i++){
  85. if(*pDst!=*pSrc){
  86. flag=1;
  87. *pDst=*pSrc;
  88. }
  89. pDst++;
  90. pSrc++;
  91. }
  92. return flag;
  93. }
  94. /********************************************************************
  95. *开关机键检测
  96. *********************************************************************/
  97. int OnOffCheck(void)
  98. {
  99. static unsigned char sucCt=0;
  100. unsigned long k;
  101. k=ONOFF_CK_PORT->IDR;
  102. if((k&ONOFF_CK_PIN)==ONOFF_CK_PIN){
  103. if(sucCt<5)sucCt++;
  104. else return 1;
  105. }else{
  106. sucCt=0;
  107. }
  108. return 0;
  109. }
  110. void GPIO_Config_ALL_AIN(void);
  111. /**********************************************************************
  112. *系统关机
  113. ***********************************************************************/
  114. void SysShutDown(void)
  115. {
  116. GPIO_InitTypeDef GPIO_InitStructure;
  117. unsigned long k;
  118. int len;
  119. GPS_PWREN_LOW;
  120. MODEM_LED1_HIGH;
  121. MODEM_LED2_HIGH;
  122. ModemSendAT("AT+ZPWROFF\r\n");
  123. SlwTrace(INF,"Set modem OnOff down!",1);
  124. ModemSendAT("AT+ZPWROFF\r\n");
  125. //os_dly_wait(50);
  126. SlwTrace(INF,"Set system power off!",1);
  127. //SlwTraceWaitCompleted();
  128. MODEM_LED1_LOW;
  129. MODEM_LED2_LOW;
  130. //-------
  131. k=ONOFF_CK_PORT->IDR;
  132. if((k&ONOFF_CK_PIN)==0){
  133. SlwTrace(INF,"Reset system!",1);
  134. //SlwTraceWaitCompleted();
  135. SystemReset();
  136. }else{
  137. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  138. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  139. GPIO_InitStructure.GPIO_Pin = PWR_EN_PIN;
  140. GPIO_Init(PWR_EN_PORT, &GPIO_InitStructure);
  141. while(1);
  142. }
  143. }
  144. //从source查找长度为targetLen目标的target,找到后返回目标段后一字节的索引,没找到返回-1,source结束为sourceEndIndicator
  145. //witchOne: 找第几个目标
  146. short FindTargetIndex(char *source, char sourceEndIndicator, char *target, unsigned char targetLen,unsigned char witchOne)
  147. {
  148. unsigned short targetIndex;
  149. unsigned char targetNum;
  150. targetIndex=0;
  151. targetNum=0;
  152. while(*source != sourceEndIndicator)
  153. {
  154. if(0==memcmp(source, target, targetLen))
  155. {//找到了
  156. targetNum++;
  157. if(targetNum == witchOne)
  158. return (targetIndex+targetLen);
  159. }
  160. source++;
  161. targetIndex++;
  162. }
  163. return -1;
  164. }
  165. unsigned char AscToHex(unsigned char aHex)
  166. {
  167. if((aHex>=0)&&(aHex<=9))
  168. aHex += 0x30;
  169. else if((aHex>=10)&&(aHex<=15))//A-F
  170. //aHex += 0x37;
  171. aHex += 0x57;
  172. else aHex = 0xff;
  173. return aHex;
  174. }
  175. void AscStrToHexStr(char *AscStr, char *HexStr)
  176. {
  177. char *pAscStr=AscStr;
  178. unsigned char d,h,l;
  179. while(0!=(d=(unsigned char)*pAscStr++)){
  180. l=d&0x0f;
  181. h=d>>4;
  182. *HexStr++=AscToHex(h);
  183. *HexStr++=AscToHex(l);
  184. }
  185. *HexStr=0;
  186. }
  187. unsigned char apnUpdateFlag=0;
  188. void PcMsgHandle(char *msg)
  189. {
  190. const char *CmdList[]={
  191. "TM", //Trace Modem
  192. "TG", //Trace GPS
  193. "DM", //Download Modem
  194. "FOTA", //Fota Modem
  195. "STP", //Set Test Parameter
  196. "SMP", //Set Modem Parameter
  197. "SGP", //Set GPS Parameter
  198. "RMP", //Read Modem Parameter
  199. "PST", //PST Mode for modem
  200. "VER", //Read Version
  201. "BAT", //Battery voltage
  202. "APN", //SET APN
  203. "RAPN", //Read APN
  204. "BEEP", //Beep test
  205. "RGP",
  206. "RMIP", //Read Manager/Load IP
  207. "SMIP", //Set Manager/Load IP
  208. "RCIP", //Read ConfigIP
  209. "SCIP", //Set ConfigIP
  210. "SACC", //Set access point
  211. "RACC", //Read access point
  212. "RREC", //Read record info
  213. "RFIP", //read fota IP
  214. "SFIP", //set fota ip
  215. "RDOMAIN", //read main ip
  216. "SDOMAIN",//set main ip
  217. "RPOCDOMAIN", //read POCdomain
  218. "SPOCDOMAIN",//set POCdomain
  219. "" //必须以空字串为结束
  220. };
  221. int h,m,s;
  222. unsigned short Port;
  223. unsigned short Select;
  224. unsigned long PSN;
  225. char buf[150];
  226. char buf1[300];
  227. char strIP[40];
  228. char strAccount[20+20];
  229. char strPassword[20+20];
  230. unsigned short PID;//省域ID 16byte
  231. unsigned short CID; //市域ID16byte
  232. char Mfactur[10]; //制造商
  233. char TMod[42]; //终端型号 不足补0x00
  234. char TID[28]; //终端ID 不足补0x00 本来7个字节 为了跟星安一致 取4个字节8
  235. unsigned char PColor; //车牌颜色 未上牌取值为0
  236. char Plate[16]; // 车牌8
  237. char SIM[12];
  238. int i,j,len;
  239. char flag;
  240. const char *pCmd;
  241. char *pMsg;
  242. int CmdListNum=0;
  243. RunMake(THIS_FILE_ID);
  244. while(*CmdList[CmdListNum++]);//计算命令列表个数
  245. CmdListNum--;
  246. RunMake(THIS_FILE_ID);
  247. //查找与命令列表相同的命令位置
  248. for(i=0;i<CmdListNum;i++){
  249. pCmd=CmdList[i];
  250. pMsg=&msg[3];
  251. flag=0;
  252. j=0;
  253. while(*pCmd){
  254. if(*pCmd!=*pMsg){
  255. flag=1;
  256. break;
  257. }
  258. pCmd++;
  259. pMsg++;
  260. j++;
  261. }
  262. if(!flag){
  263. break;
  264. }
  265. }
  266. RunMake(THIS_FILE_ID);
  267. if(i==CmdListNum)return;
  268. //处理命令
  269. switch(i)
  270. {
  271. case 0://"TM"
  272. //idNetTask
  273. SetLedStatus(RedBlueSlowFlash);
  274. // g_ucNetTaskDisable=0xff;
  275. SlwTrace(INF,"GT+TM:OK",1);
  276. //SlwTraceWaitCompleted();
  277. ComSelect(COM_SEL_MODEM);
  278. g_ucGtMode=1;
  279. // MODEM_LED1_HIGH;
  280. // MODEM_LED2_LOW;
  281. // while(1){
  282. // IWDG_ReloadCounter();//喂狗
  283. // if(OnOffCheck()){
  284. // PWR_EN_LOW;
  285. // }
  286. // //OSTimeDlyHMSM(0, 0, 0, 1);
  287. // }
  288. break;
  289. case 1://"TG"
  290. SetLedStatus(RedBlueSlowFlash);
  291. SlwTrace(INF,"GT+TG:OK",1);
  292. //SlwTraceWaitCompleted();
  293. GPSInit();
  294. ComSelect(COM_SEL_GPS);
  295. break;
  296. case 2://"DM"
  297. SetLedStatus(RedBlueSlowFlash);
  298. //g_ucNetTaskDisable=0xff;
  299. SlwTrace(INF,"GT+DM:OK",1);
  300. //SlwTraceWaitCompleted();
  301. //OSTimeDlyHMSM(0, 0, 1, 0);
  302. ComSelect(COM_SEL_MODEM);
  303. MODEM_LED1_LOW;
  304. MODEM_LED2_HIGH;
  305. GPS_PWREN_LOW;
  306. ModemSetDtrLow();
  307. ModemSetRingLow();
  308. MODEM_PWRKEY_HIGH;
  309. MODEM_RESET_HIGH;
  310. //OSTimeDlyHMSM(0, 0, 1, 0);
  311. MODEM_RESET_LOW;
  312. while(1){
  313. IWDG_ReloadCounter();//喂狗
  314. if(OnOffCheck()){
  315. PWR_EN_LOW;
  316. }
  317. }
  318. break;
  319. case 3://"FOTA"
  320. SetLedStatus(RedBlueSlowFlash);
  321. if(msg[8]=='M' && msg[9]=='C' && msg[10]=='U' ){//GT+FOTA=MCU
  322. //g_ucNetTaskDisable=0xff;
  323. SlwTrace(INF,"GT+FOTA:MCU",1);
  324. //SlwTraceWaitCompleted();
  325. sutProductPara.IapFlag=0x55;
  326. SaveProductParaToFlash("COM-FOTA");
  327. while(1);
  328. }else if(msg[8]=='M' && msg[9]=='O' && msg[10]=='D' && msg[11]=='E' && msg[12]=='M' ){//GT+FOTA=MODEM 对讲版本
  329. //g_ucNetTaskDisable=0xff;
  330. SlwTrace(INF,"GT+FOTA:MODEM",1);
  331. //SlwTraceWaitCompleted();
  332. //OSTimeDlyHMSM(0, 0, 0, 100);
  333. ModemSendAT("at+poc=10000018\r\n");
  334. while(1){
  335. IWDG_ReloadCounter();//喂狗
  336. if(OnOffCheck()){
  337. PWR_EN_LOW;
  338. }
  339. }
  340. }
  341. break;
  342. case 4://"STP" GT+STP=IP=地址或域名;Port=端口号; Account=账号;Password=密码;
  343. //g_ucNetTaskDisable=0xff;
  344. //OSTimeDlyHMSM(0, 0, 0, 100);
  345. len=GetParaFromStr(msg,"IP",buf);
  346. if(len<1 || len>=40){
  347. SlwTrace(INF,"GT+STP:FAIL[IP Invalid]",1);
  348. break;
  349. }else strcpy(strIP,buf);
  350. len=GetParaFromStr(msg,"Port",buf);
  351. if(len==0)Port=0;
  352. else if(len>5){
  353. SlwTrace(INF,"GT+STP:FAIL[Port Invalid]",1);
  354. break;
  355. }else{
  356. Port=atoi(buf);
  357. }
  358. len=GetParaFromStr(msg,"Account",buf);
  359. if(len<1 || len>=20){
  360. SlwTrace(INF,"GT+STP:FAIL[Account Invalid]",1);
  361. break;
  362. }else strcpy(strAccount,buf);
  363. len=GetParaFromStr(msg,"Password",buf);
  364. if(len<1 || len>=20){
  365. SlwTrace(INF,"GT+STP:FAIL[Password Invalid]",1);
  366. break;
  367. }else strcpy(strPassword,buf);
  368. //send to modem
  369. snprintf(buf, sizeof(buf),"id=%s;ip=%s;pwd=%s;",strAccount,strIP,strPassword);
  370. AscStrToHexStr(buf,buf1);
  371. ModemSendAT("AT+POC=120000");
  372. ModemSendAT(buf1);
  373. ModemSendAT("\r\n");
  374. //send to pc
  375. snprintf(buf, sizeof(buf),"GT+STP:IP=%s;Port=%u;Account=%s;Password=%s",strIP,Port,strAccount,strPassword);
  376. SlwTrace(INF,buf,1);
  377. break;
  378. case 5://"SMP" GT+SMP=IP=地址或域名;Port=端口号;PSN=终端序列号
  379. len=GetParaFromStr(msg,"IP",buf);
  380. if(len<1 || len>=40){
  381. SlwTrace(INF,"GT+SMP:FAIL[IP Invalid]",1);
  382. break;
  383. }
  384. strcpy(strIP,buf);
  385. len=GetParaFromStr(msg,"Port",buf);
  386. if(len==0)Port=0;
  387. else if(len>5){
  388. SlwTrace(INF,"GT+SMP:FAIL[Port Invalid]",1);
  389. break;
  390. }else{
  391. Port=atoi(buf);
  392. }
  393. len=GetParaFromStr(msg,"PSN",buf);
  394. if(len<1 || len>10){
  395. SlwTrace(INF,"GT+SMP:FAIL[PSN Invalid]",1);
  396. break;
  397. }else{
  398. PSN=atol(buf);
  399. }
  400. //PSN
  401. sutProductPara.PSN=PSN;
  402. strcpy(sutProductPara.PIP,strIP);
  403. sutProductPara.PocPort=Port;
  404. //send to modem
  405. if(strlen(sutProductPara.HARDID)<14){
  406. SlwTrace(INF,"GT+SMP:FAIL[MEID Invalid]",1);
  407. break;
  408. }
  409. snprintf(buf, sizeof(buf),"id=%lu;ip=%s;pwd=%s;",sutProductPara.PSN,strIP,sutProductPara.HARDID);
  410. AscStrToHexStr(buf,buf1);
  411. ModemSendAT("AT+POC=010000");
  412. ModemSendAT(buf1);
  413. ModemSendAT("\r\n");
  414. //save para
  415. SaveProductParaToFlash("COM-SMP");
  416. snprintf(buf, sizeof(buf),"GT+SMP:IP=%s;Port=%u;PSN=%lu",sutProductPara.PIP,sutProductPara.PocPort,sutProductPara.PSN);
  417. SlwTrace(INF,buf,1);
  418. break;
  419. case 6://"SGP" GT+SGP=IP=地址或域名;Port=端口号;
  420. len=GetParaFromStr(msg,"IP",buf);
  421. if(len<1 || len>=40){
  422. SlwTrace(INF,"GT+SGP:FAIL[IP Invalid]",1);
  423. break;
  424. }else strcpy(strIP,buf);
  425. len=GetParaFromStr(msg,"Port",buf);
  426. if(len==0)Port=0;
  427. else if(len>5){
  428. SlwTrace(INF,"GT+SGP:FAIL[Port Invalid]",1);
  429. break;
  430. }else{
  431. Port=atoi(buf);
  432. }
  433. strcpy(sutProductPara.GIP,strIP);
  434. sutProductPara.GpsPort=Port;
  435. //Save para to flash
  436. SaveProductParaToFlash("COM-SGP");
  437. //echo
  438. snprintf(buf, sizeof(buf),"GT+SGP:IP=%s;Port=%u;",sutProductPara.GIP,sutProductPara.GpsPort);
  439. SlwTrace(INF,buf,1);
  440. break;
  441. case 7://"RMP" GT+RMP<CR><LF> PSN=终端序列号;MEID=模块MEID号;CNUM=电信号;UIMID=电信UIMID;
  442. snprintf(buf, sizeof(buf),"GT+RMP:PSN=%lu;MEID=%s;CNUM=0000000000;UIMID=0000000000;IP=%s;",sutProductPara.PSN,sutProductPara.HARDID,sutProductPara.PIP);
  443. SlwTrace(INF,buf,1);
  444. break;
  445. case 8://"PST"
  446. SetLedStatus(RedBlueSlowFlash);
  447. //g_ucNetTaskDisable=0xff;
  448. SlwTrace(INF,"GT+PST:OK",1);
  449. //SlwTraceWaitCompleted();
  450. os_dly_wait(1);
  451. ComSelect(COM_SEL_MODEM);
  452. ModemSetDtrLow();
  453. MODEM_PWRKEY_LOW;
  454. MODEM_RESET_HIGH;
  455. os_dly_wait(5);
  456. MODEM_RESET_LOW;
  457. while(1){
  458. IWDG_ReloadCounter();//喂狗
  459. if(OnOffCheck()){
  460. PWR_EN_LOW;
  461. }
  462. }
  463. break;
  464. case 9://"VER"
  465. snprintf(buf, sizeof(buf),"GT+VER:%s_V%d",sutProductPara.ProductName,sutProductPara.ProductVersion);
  466. SlwTrace(INF,buf,1);
  467. break;
  468. case 10://BAT
  469. snprintf(buf, sizeof(buf),"GT+BAT: %d V",g_iVbat);
  470. SlwTrace(INF,buf,1);
  471. // SlwTrace(INF, msg,1);
  472. // cntMeiTiao=atoi(&msg[7]);
  473. // SpeakerEnable();
  474. // //SetBeepByNoOS(878, 100);
  475. // SetBeep(878, 3000);
  476. break;
  477. case 11://APN
  478. /*
  479. 设置格式:GT+APN=Account=用户名;Password=密码;<CR><LF>
  480. 成功返回:GT+APN:Account=用户名;Password=密码;<CR><LF>
  481. 失败返回:GT+APN:FAIL[失败原因]<CR><LF>
  482. */
  483. len=GetParaFromStr(msg,"Account",buf);
  484. if(len<1 || len > 30){
  485. SlwTrace(INF,"GT+APN:FAIL[Account Invalid]",1);
  486. return;
  487. }
  488. strcpy(strAccount,buf);
  489. len=GetParaFromStr(msg,"Password",buf);
  490. if(len<1 || len > 30){
  491. SlwTrace(INF,"GT+APN:FAIL[Password Invalid]",1);
  492. return;
  493. }
  494. if(sutProductPara.APNIndex==3 && validApnNum != 1)
  495. {
  496. strcpy(sutProductPara.APNName,strAccount);
  497. strcpy(sutProductPara.APNPass,buf);
  498. SaveProductParaToFlash("COM-APN");
  499. }
  500. snprintf(buf1, sizeof(buf1),"GT+APN:Account=%s;Password=%s;",strAccount,buf);
  501. SlwTrace(INF,buf1,1);
  502. if(ApnFileIndex==-1 || HgsApnLen==0) break;
  503. if(0==strcmp(strAccount, showApnTable[6]) &&
  504. 0==strcmp(buf, showApnTable[7]))
  505. break;
  506. strcpy(showApnTable[6],strAccount);
  507. strcpy(showApnTable[7],buf);
  508. apnUpdateFlag=1;
  509. break;
  510. case 12://RAPN
  511. if(validApnNum==1){
  512. i=0;j=1;
  513. }
  514. else{//有多个时,只能读取可配置的那个
  515. i=6;j=7;
  516. }
  517. sprintf(buf1,"GT+RAPN:Account=%s;Password=%s;",showApnTable[i],showApnTable[j]);
  518. SlwTrace(INF,buf1,1);
  519. break;
  520. case 13://BEEP
  521. len=GetParaFromStr(msg,"F",buf);
  522. if(len)i=atoi(buf);
  523. else i=878;
  524. len=GetParaFromStr(msg,"T",buf);
  525. if(len)Port=atoi(buf);
  526. else Port=8;//80
  527. SpeakerEnable();
  528. SetBeep(i,Port);
  529. snprintf(buf, sizeof(buf),"BEEP=%dHz,%dms",i,Port*10);
  530. SlwTrace(INF,buf,1);
  531. break;
  532. case 14://RGP
  533. snprintf(buf, sizeof(buf),"GT+RGP:IP=%s;PORT=%d;",sutProductPara.GIP,sutProductPara.GpsPort);
  534. SlwTrace(INF,buf,1);
  535. break;
  536. case 15://RMIP
  537. snprintf(buf, sizeof(buf),"GT+RMIP:IP=%d.%d.%d.%d;PORT=%d;",sutProductPara.ManagerIP[0],sutProductPara.ManagerIP[1],sutProductPara.ManagerIP[2],sutProductPara.ManagerIP[3],sutProductPara.ManagerPort);
  538. SlwTrace(INF,buf,1);
  539. break;
  540. case 16://SMIP
  541. len=GetParaFromStr(msg,"IP",buf);
  542. if(len<1 || len>=40){
  543. SlwTrace(INF,"GT+SMIP:FAIL[IP Invalid]",1);
  544. break;
  545. }else strcpy(strIP,buf);
  546. len=GetParaFromStr(msg,"Port",buf);
  547. if(len==0)Port=0;
  548. else if(len>5){
  549. SlwTrace(INF,"GT+SMIP:FAIL[Port Invalid]",1);
  550. break;
  551. }else Port=atoi(buf);
  552. GetStringIP2Bytes(sutProductPara.ManagerIP, strIP);
  553. sutProductPara.ManagerPort=Port;
  554. //Save para to flash
  555. SaveProductParaToFlash("COM-SMIP");
  556. //echo
  557. snprintf(buf, sizeof(buf),"GT+SMIP:IP=%d.%d.%d.%d;Port=%u;",sutProductPara.ManagerIP[0],
  558. sutProductPara.ManagerIP[1],
  559. sutProductPara.ManagerIP[2],
  560. sutProductPara.ManagerIP[3],
  561. sutProductPara.ManagerPort);
  562. SlwTrace(INF,buf,1);
  563. break;
  564. case 17://RCIP
  565. snprintf(buf, sizeof(buf),"GT+RCIP:IP=%d.%d.%d.%d;PORT=%d;",sutProductPara.ConfigSerIP[0],
  566. sutProductPara.ConfigSerIP[1],
  567. sutProductPara.ConfigSerIP[2],
  568. sutProductPara.ConfigSerIP[3],
  569. sutProductPara.ConfigSerPort);
  570. SlwTrace(INF,buf,1);
  571. break;
  572. case 18://SCIP
  573. len=GetParaFromStr(msg,"IP",buf);
  574. if(len<1 || len>=40){
  575. SlwTrace(INF,"GT+SCIP:FAIL[IP Invalid]",1);
  576. break;
  577. }else strcpy(strIP,buf);
  578. len=GetParaFromStr(msg,"Port",buf);
  579. if(len==0)Port=0;
  580. else if(len>5){
  581. SlwTrace(INF,"GT+SCIP:FAIL[Port Invalid]",1);
  582. break;
  583. }else Port=atoi(buf);
  584. GetStringIP2Bytes(sutProductPara.ConfigSerIP, strIP);
  585. sutProductPara.ConfigSerPort=Port;
  586. //Save para to flash
  587. SaveProductParaToFlash("COM-SCIP");
  588. //echo
  589. snprintf(buf, sizeof(buf),"GT+SCIP:IP=%d.%d.%d.%d;Port=%u;",sutProductPara.ConfigSerIP[0],
  590. sutProductPara.ConfigSerIP[1],
  591. sutProductPara.ConfigSerIP[2],
  592. sutProductPara.ConfigSerIP[3],
  593. sutProductPara.ConfigSerPort);
  594. SlwTrace(INF,buf,1);
  595. break;
  596. case 19://SACC
  597. len=GetParaFromStr(msg,"Accessp",buf);
  598. if(len<0 || len > APN_LEN)
  599. {
  600. SlwTrace(INF,"GT+SACC:FAIL[Accessp Invalid]",1);
  601. return;
  602. }
  603. if(len ==0) sutProductPara.APNApn[0]=0;
  604. else strcpy(sutProductPara.APNApn,buf);
  605. SaveProductParaToFlash("COM-SACC");
  606. snprintf(buf1, sizeof(buf1),"GT+SACC:Accessp=%s;",sutProductPara.APNApn);
  607. SlwTrace(INF,buf1,1);
  608. break;
  609. case 20://RACC
  610. snprintf(buf, sizeof(buf),"GT+RACC:Accessp=%s;",sutProductPara.APNApn);
  611. SlwTrace(INF,buf,1);
  612. break;
  613. case 21://RREC
  614. sFlash_Read(buf1, RECORD_INFO_ADDRESS, 100);
  615. SlwTrace(INF, buf1, 1);
  616. break;
  617. case 22://RFIP
  618. snprintf(buf, sizeof(buf),"GT+RFIP:IP=%d.%d.%d.%d;PORT=%d;",sutProductPara.MFotaIP[0],
  619. sutProductPara.MFotaIP[1],
  620. sutProductPara.MFotaIP[2],
  621. sutProductPara.MFotaIP[3],
  622. sutProductPara.MFotaPort);
  623. SlwTrace(INF,buf,1);
  624. break;
  625. case 23://SCIP
  626. len=GetParaFromStr(msg,"IP",buf);
  627. if(len<1 || len>=40){
  628. SlwTrace(INF,"GT+SFIP:FAIL[IP Invalid]",1);
  629. break;
  630. }else strcpy(strIP,buf);
  631. len=GetParaFromStr(msg,"Port",buf);
  632. if(len==0)Port=0;
  633. else if(len>5){
  634. SlwTrace(INF,"GT+SFIP:FAIL[Port Invalid]",1);
  635. break;
  636. }else Port=atoi(buf);
  637. GetStringIP2Bytes(sutProductPara.MFotaIP, strIP);
  638. sutProductPara.MFotaPort=Port;
  639. //Save para to flash
  640. SaveProductParaToFlash("COM-SFIP");
  641. //echo
  642. snprintf(buf, sizeof(buf),"GT+SFIP:IP=%d.%d.%d.%d;Port=%u;",sutProductPara.MFotaIP[0],
  643. sutProductPara.MFotaIP[1],
  644. sutProductPara.MFotaIP[2],
  645. sutProductPara.MFotaIP[3],
  646. sutProductPara.MFotaPort);
  647. SlwTrace(INF,buf,1);
  648. break;
  649. case 24://RDOMAIN
  650. snprintf(buf,sizeof(buf),"GT+RDOMAIN:%s;",sutProductPara.domain);
  651. SlwTrace(INF,buf,1);
  652. break;
  653. case 25://SDOMAIN
  654. len=GetParaFromStr(msg,"SDOMAIN",buf);//buf=www.baidu.com ,return len
  655. if(len>40){
  656. SlwTrace(INF,"MAINIP LEN ERR",1);
  657. return;
  658. }
  659. memset(sutProductPara.domain,0,sizeof(sutProductPara.domain));
  660. strcpy(sutProductPara.domain,buf);
  661. snprintf(buf,sizeof(buf),"GT+SDOMAIN=%s",sutProductPara.domain);
  662. //Save para to flash
  663. SaveProductParaToFlash("COM-SDOMINIP");
  664. SlwTrace(INF,buf,1);
  665. break;
  666. case 26://RPOCDOMAIN
  667. snprintf(buf,sizeof(buf),"GT+RPOCDOMAIN:%s;",sutProductPara.fota_pocdomain);
  668. SlwTrace(INF,buf,1);
  669. break;
  670. case 27://SPOCDOMAIN
  671. len=GetParaFromStr(msg,"SPOCDOMAIN",buf);//buf=www.baidu.com ,return len
  672. if(len>40){
  673. SlwTrace(INF,"FOTAPOCMAIN LEN ERR",1);
  674. return;
  675. }
  676. memset(sutProductPara.domain,0,sizeof(sutProductPara.fota_pocdomain));
  677. strcpy(sutProductPara.fota_pocdomain,buf);
  678. snprintf(buf,sizeof(buf),"GT+SPOCDOMAIN=%s",sutProductPara.fota_pocdomain);
  679. SlwTrace(INF,buf,1);
  680. break;
  681. }
  682. RunMake(THIS_FILE_ID);
  683. }
  684. /*********************************************
  685. ************************************************/
  686. void TSMakeSendStrData(void){
  687. int i;
  688. static int Temp1[7];
  689. static int Table1[20]={0};
  690. //static int Table[20]={0};
  691. static int Temp[8];
  692. unsigned char SendBuf[100];
  693. unsigned short PacketLen,data1,data2,data3,data4;
  694. char buf1[300];
  695. // char str1[4];
  696. // int strData[4];
  697. // sprintf(str1,"%x",sutProductPara.proID);
  698. //发现Regist.proID 和egist.CityID输出的是他的BCD码
  699. //for (i=0;i<6;i+=2) {
  700. // Hexstr[i]='0'|(Bcdstr[i/2]>>4);
  701. // Hexstr[i+1]='0'|(Bcdstr[i/2]&0x0F);
  702. // }
  703. // BcdToHex(xx,xx);
  704. // Regist.proID =((sutProductPara.proID>>8)&0xff);
  705. // Regist.proID =(sutProductPara.proID&0xff);
  706. // Regist.proID=(sutProductPara.proID/16*10)+(sutProductPara.proID%16);//BCD转16进制
  707. Regist.proID=(sutProductPara.proID/10*16)+(sutProductPara.proID%10);//16进制转BCD
  708. // Regist.proID='0'|(sutProductPara.proID>>4);//
  709. // Regist.proID='0'|(sutProductPara.proID&0x0F);
  710. // sprintf(buf1,"proID=%d;\r\n",Regist.proID);
  711. // SlwTrace(INF,buf1);
  712. data1=sutProductPara.CityID/100;
  713. data3=(data1/10*16)+(data1%10);
  714. data2=sutProductPara.CityID%100;
  715. data4=(data2/10*16)+(data2%10);
  716. Regist.CityID = ((data4>>24)&0xffff);
  717. Regist.CityID = ((data4>>16)&0xffff);
  718. Regist.CityID = ((data3>>8)&0xffff);
  719. Regist.CityID = (data3&0xffff);
  720. for(i = 0; i < 5; i++){
  721. Regist.Manufacturers[i] = sutProductPara.Manufacturers[i];
  722. }
  723. for(i = 0; i < 20; i++){
  724. sscanf(sutProductPara.TerMod+2*i,"%2x",&Table1[i]);
  725. Regist.TerMod[i]= Table1[i];
  726. }
  727. // for(i=0;i<14;i+=2)
  728. // {
  729. // Regist.TerID[i/2]=(sutProductPara.TerID[i]<<4)|(sutProductPara.TerID[i+1]&0x0F);
  730. // }
  731. for(i = 0; i < 7; i++){
  732. sscanf(sutProductPara.TerID+2*i,"%2x",&Temp1[i]);
  733. Regist.TerID[i]= Temp1[i];
  734. }
  735. // sprintf(buf1,"TerID=%s;\r\n",sutProductPara.TerID);
  736. // SlwTrace(INF,buf1);
  737. Regist.PlateColor=sutProductPara.PlateColor;//
  738. for(i = 0; i < 8; i++){//16
  739. sscanf(sutProductPara.Plate+2*i,"%2x",&Temp[i]);
  740. Regist.Plate[i] = Temp[i];
  741. }
  742. PacketLen=TSGpsPacket(SendBuf,sutProductPara.PSN,TS_TERMINAL_REGISTRATION,(unsigned char *)&Regist,sizeof(REGISTRATION));
  743. MC8332ModemSendTcpData(0,SendBuf,PacketLen);
  744. }
  745. void SwitchGroupCtrl(void)
  746. {
  747. if(m_GroupSwitch)
  748. {
  749. if(++m_GroupSwitchCnt >=5)
  750. {
  751. //MeSpeak(ENCODE_GBK, "CDCBB3F6", 0);
  752. if(sutUIstatus.Status == UIS_STANDBY)
  753. ShowButton(1,107,"菜单");
  754. m_GroupSwitch=0;
  755. SlwTrace(INF, "QuitGroupSel",1);
  756. }
  757. }
  758. }
  759. void SwitchGroupPre(void)
  760. {
  761. //让菜单变成:确定
  762. ShowButton(1,107,"确定");
  763. m_GroupSwitch=1;
  764. m_GroupSwitchCnt=0;
  765. }
  766. //volatile unsigned int pttPressFreeCt;
  767. /***********************************************************************
  768. *按键按下事件
  769. ************************************************************************/
  770. void KeyPress(void)
  771. {
  772. char buf[30];
  773. static unsigned char sucReportFlag=0;
  774. if(g_ucGtMode==0){//在GT模式下不能与串口同时发送AT指令会冲突,导致重启
  775. UI_Key();
  776. }
  777. switch(g_ulKeyValue){
  778. //-------------------------------------------------------------------
  779. case KEY_PPT_MIC:
  780. case KEY_PPT://=====================KEY_PPT Press====================
  781. g_ucKeyMode=1;//单次模式
  782. SlwTrace(INF,"[KEY_PPT/KEY_PPT_MIC Press]",1);
  783. sutPocStatus.PPTpress=1;
  784. if((g_ucGtMode==0)&&(g_ucCallPhonePro==0))
  785. {
  786. //pttPressFreeCt=os_time_get()+40;
  787. ModemSendAT(POC_CMD_PPT_START);//
  788. //SlwTrace(INF, "Speak",1);
  789. }
  790. break;
  791. //-------------------------------------------------------------------
  792. case KEY_INFO://=====================KEY_UP Press==================
  793. g_ucKeyMode=1;//单次模式
  794. SlwTrace(INF,"[KEY_UP Press]",1);
  795. if(sutProductPara.QuickGroup==1&&(g_ucGtMode==0)&&(g_ucCallPhonePro==0)&&(sutUIstatus.Status == UIS_STANDBY)){
  796. g_ucEnterGroupFlag=1;
  797. snprintf(buf, sizeof(buf),"AT+POC=090000%08x\r\n",sutProductPara.QuickGroupId[1]);
  798. ModemSendAT(buf);
  799. }else if((g_ucGtMode==0)&&(g_ucCallPhonePro==0)&&(sutUIstatus.Status == UIS_STANDBY)&&(sutPocStatus.Logined))
  800. {
  801. ModemSendAT(POC_CMD_UP);
  802. SwitchGroupPre();
  803. }
  804. break;
  805. case KEY_FUNC://=====================KEY_DOWN Press=================
  806. g_ucKeyMode=1;//单次模式
  807. g_ucEnterGroupFlag=1;
  808. SlwTrace(INF,"[KEY_DOWN Press]",1);
  809. if(sutProductPara.QuickGroup==1&&(g_ucGtMode==0)&&(g_ucCallPhonePro==0)&&(sutUIstatus.Status == UIS_STANDBY)){
  810. snprintf(buf, sizeof(buf),"AT+POC=090000%08x\r\n",sutProductPara.QuickGroupId[2]);
  811. ModemSendAT(buf);
  812. }else if((g_ucGtMode==0)&&(g_ucCallPhonePro==0)&&(sutUIstatus.Status == UIS_STANDBY)&&(sutPocStatus.Logined))
  813. {
  814. ModemSendAT(POC_CMD_DOWN);
  815. SwitchGroupPre();
  816. }
  817. break;
  818. case KEY_PANEL_OK://=====================KEY_PANEL_OK Press=================
  819. if(sutProductPara.QuickGroup==1&&(g_ucGtMode==0)&&(g_ucCallPhonePro==0)&&(sutUIstatus.Status == UIS_STANDBY)){
  820. g_ucEnterGroupFlag=1;
  821. snprintf(buf, sizeof(buf),"AT+POC=090000%08x\r\n",sutProductPara.QuickGroupId[0]);
  822. ModemSendAT(buf);
  823. }else
  824. if(sutUIstatus.Status == UIS_STANDBY && m_GroupSwitch==0)
  825. UISetNextStatus(UIS_MENU_GROUP_SEL);
  826. SlwTrace(INF,"[KEY_PANEL_OK Press]",1);
  827. break;
  828. case KEY_PANEL_MENU://=====================KEY_PANEL_MENU Press=================
  829. if(m_GroupSwitch)
  830. {//
  831. m_GroupSwitch=0;
  832. snprintf(buf, sizeof(buf),"AT+POC=090000%s\r\n",sutPocStatus.switchGroupID);
  833. ModemSendAT(buf);
  834. ShowButton(1,107,"菜单");
  835. SlwTrace(INF, buf,0);
  836. }
  837. SlwTrace(INF,"[KEY_PANEL_MENU Press]",1);
  838. break;
  839. case KEY_PANEL_EXIT://=====================KEY_PANEL_EXIT Press=================
  840. SlwTrace(INF,"[KEY_PANEL_EXIT Press]",1);
  841. break;
  842. case KEY_PANEL_UP://=====================KEY_PANEL_UP Press=================
  843. SlwTrace(INF,"[KEY_PANEL_UP Press]",1);
  844. break;
  845. case KEY_PANEL_DOWN://=====================KEY_PANEL_DOWN Press=================
  846. SlwTrace(INF,"[KEY_PANEL_DOWN Press]",1);
  847. break;
  848. case KEY_EXIT_DOWN_GT://=====================KEY_UP_OK_GT Press=================
  849. // EnterGTMode();
  850. SlwTrace(INF,"[KEY_EXIT_DOWN_GT Press]",1);
  851. break;
  852. case KEY_P1_P3_UPDATE:
  853. SlwTrace(INF, "[KEY_P1_P3_UPDATE Press]",1);
  854. break;
  855. case KEY_OK_DOWN_GPS:
  856. SlwTrace(INF, "[KEY_OK_DOWN_GPS Press]",1);
  857. break;
  858. case KEY_OPTION:
  859. g_usOptionCnt++;
  860. //在待机界面按一下'S'键,网络允许情况下发SOS消息
  861. if(sutGpsInfo.isGpsAuthOk==1 && sutUIstatus.Status==UIS_STANDBY && g_ucModemTaskEn==1)
  862. {
  863. if(g_usOptionCnt ==3)
  864. TS_SendSOSMessage();
  865. }
  866. SlwTrace(INF, "[KEY_OPTION Press]",1);
  867. break;
  868. }
  869. }
  870. //长按 EXIT和下箭头3秒,只进入一次
  871. void GTMode_Detected(void)
  872. {
  873. static char GT_Flag=0;
  874. if(GT_Flag == 2) return;
  875. if(g_ulKeyValue == KEY_EXIT_DOWN_GT)
  876. {
  877. if(GT_Flag == 0)
  878. {
  879. g_ucUKC=0;
  880. GT_Flag ++;
  881. }else
  882. {
  883. if(g_ucUKC>3){
  884. EnterGTMode();
  885. GT_Flag++;
  886. }
  887. }
  888. }else
  889. {
  890. if(GT_Flag == 1)
  891. GT_Flag = 0;
  892. }
  893. }
  894. void UpdataAppVersion(void)
  895. {
  896. //正旋1格后松开,升级应用版本
  897. SpeakerEnable();
  898. ModemSendAT("AT+ZTTS=1,\"C6510759F466B065945E287548722C67F78B73513A678D51005F3A67\"\r"); //准备更新应用版本,请关机再开机
  899. SlwTrace(INF,"Update APP Version!",1);
  900. SetLedStatus(RedFastFlash);
  901. sutProductPara.IapFlag=0x55;
  902. SaveProductParaToFlash("MAIN-UP");
  903. while(1){
  904. IWDG_ReloadCounter();//喂狗
  905. os_dly_wait(1);
  906. if(OnOffCheck()){
  907. PWR_EN_LOW;
  908. }
  909. }
  910. }
  911. void UpdataPocVersion(void)
  912. {
  913. SpeakerEnable();
  914. ModemSendAT("AT+POC=10000018\r\n");
  915. SlwTrace(INF,"Update POC Version!",1);
  916. }
  917. void EnterGTMode(void)
  918. {
  919. ColonyFeature(0);
  920. g_ucModemTaskEn=0;
  921. SpeakerEnable();
  922. SetBeep(1176,10);
  923. os_dly_wait(20);
  924. SpeakerEnable();
  925. SetBeep(878,10);
  926. os_dly_wait(20);
  927. SpeakerEnable();
  928. SetBeep(1176,10);
  929. g_ucUART1RxMode=1;
  930. Uart1RxEnable(1);
  931. LedProcess(1);
  932. SetLedStatus(RedBlueFastFlash);
  933. SlwTrace(INF,"[EnterTxMode]",1);
  934. }
  935. void openModem()
  936. {
  937. MODEM_RESET_HIGH;
  938. os_dly_wait(100);
  939. MODEM_RESET_LOW;
  940. MODEM_PWRKEY_HIGH;
  941. os_dly_wait(100);
  942. MODEM_PWRKEY_LOW;
  943. }
  944. void closeModem()
  945. {
  946. MODEM_RESET_HIGH;
  947. MODEM_PWRKEY_HIGH;
  948. }
  949. /***********************************************************************
  950. *按键释放事件
  951. ************************************************************************/
  952. void KeyFree(void)
  953. {
  954. int i;
  955. char CheckCode[10];
  956. char buf[30];
  957. int flag;
  958. SUT_MODEM_AT_MSG *pMsg;
  959. //unsigned int temp;
  960. char pressFlag;
  961. //-------------------------------------------------------------------
  962. if(KEY_PPT==g_ulKeyValue || KEY_PPT_MIC == g_ulKeyValue)
  963. {//===============KEY_PPT Free=============
  964. SlwTrace(INF,"[KEY_PPT/KEY_PPT_MIC Free]",1);
  965. g_ucKeyPPTPress=0;
  966. sutPocStatus.PPTpress=0;
  967. if((g_ucGtMode==0)&&((g_ucCallPhonePro==0)))
  968. {
  969. //#error "这里好了,发杨测试看看,等VPDN测试完一次发"
  970. //os_dly_wait(25);
  971. // if(os_time_get() > pttPressFreeCt)
  972. // {//按放间隔足够长了,只延时20即可
  973. // temp=20;//防止释放太快,导致对方有可能听不到最后一个字
  974. // }else{
  975. // //按放间隔太短,补足30
  976. // temp=pttPressFreeCt-os_time_get();
  977. // }
  978. // os_dly_wait(temp);
  979. ModemSendAT(POC_CMD_PPT_STOP);//结束讲话 放开PPT键
  980. g_ucKeyMode=0;
  981. }
  982. }else if(KEY_INFO==g_ulKeyValue)
  983. {//=========KEY_UP Free==============
  984. SlwTrace(INF,"[KEY_UP Free]",1);
  985. g_ucKeyMode=0;
  986. g_uikey_up_stat=1;
  987. }else if(KEY_FUNC==g_ulKeyValue)
  988. {//=========KEY_DOWN Free============
  989. g_ucKeyMode=0;
  990. SlwTrace(INF,"[KEY_DOWN Free]",1);
  991. }
  992. else if(KEY_PANEL_OK==g_ulKeyValue)
  993. {//=========KEY_PANEL_OK Free==============
  994. SlwTrace(INF,"[KEY_PANEL_OK Free]",1);
  995. return;
  996. }
  997. else if(KEY_PANEL_MENU==g_ulKeyValue)
  998. {//=========KEY_PANEL_MENU Free==============
  999. SlwTrace(INF,"[KEY_PANEL_MENU Free]",1);
  1000. return;
  1001. }
  1002. else if(KEY_PANEL_EXIT==g_ulKeyValue)
  1003. {//=========KEY_PANEL_EXIT Free==============
  1004. g_uikey_up_stat=0;
  1005. SlwTrace(INF,"[KEY_PANEL_EXIT Free]",1);
  1006. return;
  1007. }
  1008. else if(KEY_PANEL_UP==g_ulKeyValue)
  1009. {//=========KEY_PANEL_UP Free============
  1010. //closeModem();
  1011. }
  1012. else if(KEY_PANEL_DOWN==g_ulKeyValue)
  1013. {//=========KEY_PANEL_DOWN Free============
  1014. //openModem();
  1015. }
  1016. else if(KEY_EXIT_DOWN_GT==g_ulKeyValue)
  1017. {//=====================KEY_EXIT_DOWN_GT Press=================
  1018. g_ulKeyValue=0;
  1019. SlwTrace(INF,"[KEY_EXIT_DOWN_GT Free]",1);
  1020. }else if(KEY_OPTION == g_ulKeyValue)
  1021. {
  1022. g_usOptionCnt=0;
  1023. SlwTrace(INF,"[KEY_OPTION Free]",1);
  1024. }
  1025. }
  1026. #if(USE_CONFIG_FUN==1)
  1027. void ConfigModeDetect(void)
  1028. {//检测进入配置服务器功能
  1029. //GT模式下
  1030. //长按OPTION键
  1031. char buf[20];
  1032. unsigned char timeout=150;
  1033. if(0 != g_ucModemTaskEn) return;
  1034. if(sutConfig.g_ubConfigFlag==0)
  1035. {
  1036. if(sutConfig.g_usOptionCnt ==5)
  1037. {
  1038. sutConfig.g_ubConfigFlag=1;
  1039. sutConfig.g_usOptionCnt=0;
  1040. snprintf(buf, sizeof(buf), "AT+TCPCLOSE=%d\r\n", GPS_DATA_SOCKET);
  1041. ModemSendAT(buf);
  1042. SlwTrace(INF, "ConfigModeActive",1);
  1043. GuiShowBmp(30,34,"logo.bmp");
  1044. GuiClearRect(0,55, LCD_WIDTH-1, 100);
  1045. GuiShowStr(8, 65, "正在访问配置服务器",1);
  1046. sutConfig.g_ucTcpRetryNum=CONFIG_TCP_RETRY_CNT;
  1047. sutConfig.g_ucConfigStatus=0x01;//上报列表
  1048. sutConfig.g_ubConfigFinished=0;//
  1049. tcpControl=2;
  1050. }
  1051. }
  1052. }
  1053. #endif
  1054. void apnUpdateFun(void)
  1055. {
  1056. int len;
  1057. char buf1[400];
  1058. char *pMsg;
  1059. if(apnUpdateFlag!=1) return;
  1060. apnUpdateFlag=0;
  1061. len=HgsApnLen;
  1062. if(HgsApnLen > sizeof(buf1)) len=sizeof(buf1);
  1063. len=ReadFileData(ApnFileIndex,0,len,(u8*)buf1);
  1064. if(len==0) return;
  1065. pMsg=strstr(buf1,"APN4=");
  1066. if(0==pMsg) return;
  1067. sprintf(pMsg+5, "%s,%s;\r\n",showApnTable[6],showApnTable[7]);
  1068. ModifyFileData(ApnFileIndex, 0, strlen(buf1), (unsigned char *)buf1);
  1069. }
  1070. void cmdShutDown(char type)
  1071. {
  1072. static short cntDown;
  1073. static char ttype;
  1074. char buf[30];
  1075. ttype=type;
  1076. SlwTrace(INF, "Try to PwrOff",1);
  1077. ModemSendAT("AT$QCPWRDN\r\n");
  1078. for(cntDown=6;cntDown>=0;cntDown--)
  1079. {
  1080. switch(ttype)
  1081. {
  1082. case 0:snprintf(buf, sizeof(buf), "正在关机[%d]", cntDown);break;
  1083. case 1:snprintf(buf, sizeof(buf), "低压关机[%d]", cntDown);break;
  1084. default:return;
  1085. }
  1086. ShowMessageBox(buf);
  1087. SlwTrace(INF, buf, 1);
  1088. IWDG_ReloadCounter();
  1089. os_dly_wait(100);
  1090. }
  1091. }
  1092. void PowerOffProtetion(void)
  1093. {
  1094. if(modemPwrOffNow==0) return;
  1095. LCDTimeOut(0);
  1096. GPS_PWREN_LOW;
  1097. cmdShutDown(0);
  1098. PWR_EN_LOW;
  1099. while(1);
  1100. }
  1101. unsigned int bootMapCnt=0;
  1102. unsigned char bootFlag=0;
  1103. /*******************************************************************
  1104. *MainTask
  1105. *主任务
  1106. ********************************************************************/
  1107. __task void MainTask(void)
  1108. {
  1109. int BeepFlag=0;
  1110. int KeyFlag,GTKeystat=0;
  1111. int RealTime;
  1112. int FileIndex,SMSIndex;
  1113. char *msg;
  1114. INT8U err;
  1115. unsigned char ucCheckVbatCt=0;
  1116. static unsigned int sucCt=0;
  1117. static unsigned char sucFunReportFlag=0;
  1118. unsigned short g_usModemProtectEn;
  1119. int Vbat;
  1120. RunMake(THIS_FILE_ID);
  1121. //Obtain own system task identification number. */
  1122. idMainTask = os_tsk_self();
  1123. memset((unsigned char *)&sutPocStatus,0,sizeof(SUT_POC_STATUS));
  1124. //开机提示音
  1125. SpeakerEnable();
  1126. os_dly_wait(10);
  1127. SetBeepByNoOS(888,100);
  1128. SpeakerDisable();
  1129. //Read Product param from flash
  1130. ReadProductParaFromFlash();
  1131. APNFileInit();
  1132. printf("\r\n%s_V%d.%d Start...\r\n",sutProductPara.ProductName,sutProductPara.ProductVersion,INTER_VER);
  1133. printf("PSN:%lu\r\n",sutProductPara.PSN);
  1134. g_ucUpdataVer=0;
  1135. SlwTrace(INF,"Main Task Start!",1);
  1136. //LCD设置参数初始化
  1137. FileIndex=GetFileIndex("LcdParam.ini");
  1138. ReadFileData(FileIndex,0,sizeof(SUT_LCD_PARAM),(unsigned char *)&sutLcdParam);
  1139. if(sutLcdParam.OnTimeIndex > 4){
  1140. sutLcdParam.OnTimeIndex = 0;
  1141. sutLcdParam.OnTime=10;
  1142. ModifyFileData(FileIndex,0,sizeof(SUT_LCD_PARAM),(unsigned char *)&sutLcdParam);
  1143. //到此,说明是重新烧代码了,则初始化GPS为默认值
  1144. sutProductPara.m_GPS_TimeIndex = DEFAULT_GPS_SEND_TIME_INDEX;
  1145. GpsTimeUpdate();
  1146. SaveProductParaToFlash("MAIN-PARA");
  1147. }
  1148. sutLcdParam.OnTimeCt = 0;
  1149. //电话本
  1150. SetPhoneNumFile("HGS.Tell");
  1151. //短信
  1152. SetMessageConfi();
  1153. //SetCodePara();
  1154. LedInit();
  1155. SlwTrace(INF,"Led Init...",1);
  1156. g_iFun2KeyEncodeCt=0;
  1157. bootMapCnt=os_time_get()+2000;//开机界面持续20秒
  1158. //create PocTask
  1159. idModemUartTask=os_tsk_create_user(ModemUartTask,3,&stkModemUartTask,sizeof(stkModemUartTask));
  1160. idModemTask=os_tsk_create_user(ModemTask,3,&stkModemTask,sizeof(stkModemTask));
  1161. //idTTSTask=os_tsk_create_user(TTSTask, 3, &stkTTSTask,sizeof(stkTTSTask));
  1162. RunMake(THIS_FILE_ID);
  1163. UI_Init();
  1164. while(1)
  1165. {
  1166. if(bootFlag==0)
  1167. {
  1168. if((os_time_get() > bootMapCnt) || sutPocStatus.FirstLogined)
  1169. bootFlag=1;
  1170. }
  1171. PowerOffProtetion();
  1172. if(bootFlag==1)
  1173. UI_Loop();
  1174. os_dly_wait(1);
  1175. IncomingSMS();
  1176. LocalTTSHandle();
  1177. //处理PC发送过来的消息
  1178. RunMake(THIS_FILE_ID);
  1179. if(g_usUart1RecvLen){
  1180. PcMsgHandle((char *)RxBuffer1);
  1181. g_usUart1RecvLen=0;
  1182. DMA1_Channel5->CNDTR = UART1_RX_BUFFER_SIZE;//重设DMA传输数据长度
  1183. DMA_Cmd(DMA1_Channel5,ENABLE);//打开DMA
  1184. }
  1185. RunMake(THIS_FILE_ID);
  1186. apnUpdateFun();
  1187. //键盘处理
  1188. if(0!=(KeyFlag=GetKey())){
  1189. //按键处理
  1190. if(1==KeyFlag){ //按键按下
  1191. KeyPress();
  1192. //if(g_ulKeyValue != KEY_PPT &&
  1193. // g_ulKeyValue != KEY_PPT_MIC)
  1194. //{
  1195. LCDTimeOut(0);
  1196. sutLcdParam.OnTimeCt=0;
  1197. //}
  1198. }else{ //按键释放
  1199. KeyFree();
  1200. }
  1201. }
  1202. RunMake(THIS_FILE_ID);
  1203. //LED处理
  1204. LedProcess(0);
  1205. #if(USE_CONFIG_FUN==1)
  1206. ConfigModeDetect();
  1207. #endif
  1208. GTMode_Detected();
  1209. //---以下控制每秒执行一次---
  1210. if(os_time_get() < sucCt) continue;
  1211. sucCt = os_time_get()+100;
  1212. RunMake(THIS_FILE_ID);
  1213. KeyCount();//键盘按下、释放计数控制
  1214. RunMake(THIS_FILE_ID);
  1215. //信号
  1216. //测量电池电压
  1217. Vbat=GetVbat();
  1218. if(Vbat>0)g_iVbat=Vbat;
  1219. //判断电池电压是否过低关机
  1220. if(ucCheckVbatCt<3)ucCheckVbatCt++;
  1221. else CheckVbat(g_iVbat);
  1222. //MODEM防护
  1223. FingerDaemonfinger(); //3.21关闭
  1224. SwitchGroupCtrl();
  1225. if(sutLcdParam.OnTime>0){
  1226. if(++sutLcdParam.OnTimeCt>=sutLcdParam.OnTime){
  1227. sutLcdParam.OnTimeCt=0;
  1228. if(sutPocStatus.FirstLogined==1)//没登陆过(刚开机),LCD一直亮着
  1229. LCDTimeOut(1);
  1230. }
  1231. }
  1232. }
  1233. }
  1234. void ADCInit(void)
  1235. {
  1236. ADC_InitTypeDef ADC_InitStructure;
  1237. GPIO_InitTypeDef GPIO_InitStructure;
  1238. RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
  1239. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
  1240. GPIO_InitStructure.GPIO_Pin =GPIO_Pin_0;
  1241. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IPU;//先内部上拉模式给IO充能
  1242. GPIO_Init(GPIOA,&GPIO_InitStructure);
  1243. DelayMs(100);
  1244. GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AIN;
  1245. GPIO_Init(GPIOA,&GPIO_InitStructure); //默认速度为两兆
  1246. DelayMs(200);//让电平恢复正常
  1247. //配置ADC的运行:
  1248. ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
  1249. ADC_InitStructure.ADC_ScanConvMode =DISABLE; //连续多通道模式
  1250. ADC_InitStructure.ADC_ContinuousConvMode =DISABLE;//ENABLE; //连续转换
  1251. ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; //转换不受外界决定
  1252. ADC_InitStructure.ADC_DataAlign =ADC_DataAlign_Right; //右对齐
  1253. ADC_InitStructure.ADC_NbrOfChannel =1; //扫描通道数
  1254. ADC_Init(ADC1,&ADC_InitStructure);
  1255. ADC_RegularChannelConfig(ADC1,ADC_Channel_0, 1,ADC_SampleTime_1Cycles5); //通道X,采样时间为1.5周期,1代表规则通道第1个这个1是啥意思我不太清楚只有是1的时候我的ADC才正常。
  1256. ADC_Cmd (ADC1,ENABLE); //使能或者失能指定的ADC
  1257. ADC_SoftwareStartConvCmd(ADC1,ENABLE);//使能或者失能指定的ADC的软件转换启动功能
  1258. }
  1259. void GPIO_Config_ALL_AIN(void)
  1260. {
  1261. GPIO_InitTypeDef GPIO_InitStructure;
  1262. /* Enable GPIOD and GPIOE clock */
  1263. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB
  1264. | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
  1265. | RCC_APB2Periph_AFIO, ENABLE);
  1266. /* Disable the Serial Wire Jtag Debug Port SWJ-DP */
  1267. GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
  1268. /* PA */
  1269. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  1270. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  1271. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  1272. GPIO_Init(GPIOA, &GPIO_InitStructure);
  1273. /* PB */
  1274. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  1275. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  1276. GPIO_Init(GPIOB, &GPIO_InitStructure);
  1277. /* PC */
  1278. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  1279. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  1280. GPIO_Init(GPIOC, &GPIO_InitStructure);
  1281. /* PD */
  1282. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  1283. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
  1284. GPIO_Init(GPIOD, &GPIO_InitStructure);
  1285. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA
  1286. | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD
  1287. , DISABLE);
  1288. }
  1289. /***********************************************************************
  1290. *获取电池电压
  1291. 返回值: -1为无效 ; 正数表示电池电压放大100倍,如 382表示3.82V
  1292. ***********************************************************************/
  1293. int GetVbat(void)
  1294. {
  1295. char buf[30];
  1296. static int Ct=0;
  1297. static int siVbat[10]={0,0,0,0,0,0,0,0,0,0};
  1298. int iVbat[10];
  1299. int max[5];
  1300. int sum;
  1301. int i,j,k;
  1302. int adc;
  1303. int Vbat;
  1304. #if 1
  1305. ADC_SoftwareStartConvCmd(ADC1,ENABLE);
  1306. while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
  1307. adc=ADC_GetConversionValue(ADC1);
  1308. if(adc<100 && adc>4095)
  1309. {
  1310. printf("VDAC OVER");
  1311. return -1;
  1312. }
  1313. #else
  1314. //采集ADC
  1315. if(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)!=RESET){//检查制定ADC标志位置1与否 ADC_FLAG_EOC 转换结束标志位 if(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)!=RESET){
  1316. ADC_SoftwareStartConvCmd(ADC1,ENABLE);
  1317. adc=ADC_GetConversionValue(ADC1);
  1318. if(adc<100 && adc>4095)
  1319. {
  1320. //printf("ERR1");
  1321. return -1;
  1322. }
  1323. }else
  1324. {
  1325. printf("ERR2");
  1326. return -1;
  1327. }
  1328. #endif
  1329. //循环存储
  1330. siVbat[Ct]=adc;
  1331. if(++Ct>9)Ct=0;
  1332. //printf("%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\r\n",siVbat[0],siVbat[1],siVbat[2],siVbat[3],siVbat[4],siVbat[5],siVbat[6],siVbat[7],siVbat[8],siVbat[9]);
  1333. //滤波处理,排列取3个最大值,3个最大中去掉第一大,其余求平均
  1334. //拷贝
  1335. for(i=0;i<10;i++){
  1336. iVbat[i]=siVbat[i];
  1337. }
  1338. for(i=0;i<4;i++){
  1339. max[i]=0;
  1340. for(j=0;j<10;j++){
  1341. if(iVbat[j]>max[i]){
  1342. max[i]=iVbat[j];
  1343. k=j;
  1344. }
  1345. }
  1346. iVbat[k]=0;
  1347. }
  1348. // printf("%d,%d,%d\r\n",max[0],max[1],max[2]);
  1349. sum=max[1]+max[2];
  1350. if(max[2]<200)
  1351. {
  1352. //printf("ERR3");
  1353. return -1;
  1354. }
  1355. Vbat=sum*308/4096;
  1356. return Vbat;
  1357. }
  1358. void Sys_Standby(void)
  1359. {
  1360. RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE); //使能PWR外设时钟
  1361. PWR_WakeUpPinCmd(ENABLE); //使能唤醒管脚功能,PA0
  1362. PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI);
  1363. }
  1364. /*******************************************************
  1365. *CheckVbat
  1366. *检查电池电压,控制低压报警,
  1367. *低压关机已由硬件监测
  1368. ******************************************************/
  1369. void CheckVbat(int Vbat)
  1370. {
  1371. static unsigned char sucCt=0;//提示“请充电”计数器
  1372. if(Vbat<MIN_PWR_LEVEL){//300
  1373. SlwTrace(INF,"Vbat low!shutdown now!",1);
  1374. os_dly_wait(1);
  1375. //关闭模块
  1376. if(sutPocStatus.ModemPowerOn==1){
  1377. #if 1
  1378. //发送机指令并等待7秒
  1379. cmdShutDown(1);
  1380. #else
  1381. SpeakerEnable();
  1382. ModemSendAT("AT+LSHTTSSTP\r\n");
  1383. os_dly_wait(30);
  1384. ModemSendAT("AT+LSHTTS=1,\"B5CDD1B9B4FDBBFA\"\r\n");//低压待机
  1385. IWDG_ReloadCounter();
  1386. os_dly_wait(100);
  1387. IWDG_ReloadCounter();
  1388. os_dly_wait(100);
  1389. SlwTrace(INF,"ModemPoweroff!",1);
  1390. ModemPoweroff();
  1391. #endif
  1392. }
  1393. sutPocStatus.ModemPowerOn=0;
  1394. PWR_EN_LOW;
  1395. Sleeping();
  1396. }else if(Vbat<WARN_PWR_LEVEL){//提示请充电 315
  1397. if(++sucCt>30){
  1398. sucCt=0;
  1399. //MeSpeak(ENCODE_GBK, "C7EBB3E4B5E7",0);//请充电
  1400. }
  1401. }else sucCt=0;
  1402. }
  1403. void GetXinHao(int CSQ,char *XinHao)
  1404. {
  1405. if(CSQ>31 || CSQ<1){
  1406. strcpy(XinHao,"2C00E065517FDC7EE14FF753");//无网络信号
  1407. }else if(CSQ>25){
  1408. //信号较强 "E14FF753838F3A5F"
  1409. strcpy(XinHao,"E14FF753838F3A5F");
  1410. }else if(CSQ>17){
  1411. //信号适中 "E14FF75302902D4E"
  1412. strcpy(XinHao,"E14FF75302902D4E");
  1413. }else if(CSQ>10){
  1414. //信号偏低 "E14FF7534F504E4F"
  1415. strcpy(XinHao,"E14FF7534F504E4F");
  1416. }else{
  1417. //信号很弱 "E14FF753885F315F"
  1418. strcpy(XinHao,"E14FF753885F315F");
  1419. }
  1420. }
  1421. void GetVbatPercent(int Vbat,char *Percent)
  1422. {
  1423. //10 20 30 40 50 60 70 80 90 100
  1424. const char *ccPercent[]={
  1425. "31003000",
  1426. "32003000",
  1427. "33003000",
  1428. "34003000",
  1429. "35003000",
  1430. "36003000",
  1431. "37003000",
  1432. "38003000",
  1433. "39003000",
  1434. "310030003000"};
  1435. //播报电量百分比 10,20,30, 40, 50, 60, 70, 80, 90,100
  1436. const int siVbat[] ={335,343,350,360,370,377,384,392,400,500};
  1437. int i;
  1438. for(i=0;i<10;i++){
  1439. if(Vbat<siVbat[i])break;
  1440. }
  1441. if(i>9)i=9;
  1442. strcpy(Percent,ccPercent[i]);
  1443. }
  1444. /***************************************************************************
  1445. 将数字转Unicode编码
  1446. pUnicode空间最长 5*4+1=21字节
  1447. ****************************************************************************/
  1448. void NumToUnicode(unsigned short Num,char *pUnicode)
  1449. {
  1450. char temp[6];
  1451. int i,j,len;
  1452. snprintf(temp, sizeof(temp),"%u",Num);//0~65535
  1453. len=strlen(temp);
  1454. j=0;
  1455. for(i=0;i<len;i++){
  1456. pUnicode[j++]=0x30+temp[i]/16;
  1457. pUnicode[j++]=0x30+temp[i]%16;
  1458. pUnicode[j++]=0x30;
  1459. pUnicode[j++]=0x30;
  1460. }
  1461. pUnicode[j]=0;
  1462. }
  1463. void ReportVersion(void)
  1464. {
  1465. char buf[150];
  1466. char temp[20];
  1467. snprintf(buf, sizeof(buf),"AT+ZTTS=1,\"945E287548722C67");
  1468. NumToUnicode(sutProductPara.ProductVersion,temp);
  1469. strcat(buf,temp);
  1470. strcat(buf,"2C00F95BB28B48722C67");
  1471. NumToUnicode((unsigned short)g_iModemVer,temp);
  1472. strcat(buf,temp);
  1473. strcat(buf,"\"\r\n");
  1474. SpeakerEnable();
  1475. ModemSendAT(buf);//应用版本
  1476. SlwTrace(INF,buf,0);
  1477. /*
  1478. SlwTrace(INF,"[ReportVersion]");
  1479. sprintf(buf,"Mcu=%u,Modem=%u\r\n",sutProductPara.ProductVersion,(unsigned short)g_iModemVer);
  1480. SlwTrace(INF,buf);
  1481. */
  1482. }
  1483. /*******************************************************************
  1484. *GetParaFromStr
  1485. *从Str中找到Para=后面至';'或非字符的字串并放入Value
  1486. 返回Value的长度
  1487. 举例:Str="GT+SMP=IP=192.168.1.1;Port=12345"
  1488. 如果Para="Port" 则Value将被赋值为"12345",并返回5
  1489. 如果Para="IP" 则Value将被赋值为"192.168.1.1",并返回11
  1490. 要求Para长度不大于20字节 ,Value长度不大于40字节
  1491. ********************************************************************/
  1492. int GetParaFromStr(char *Str,char *Para,char *Value)
  1493. {
  1494. int ValueLen=0,ParaLen=0;
  1495. char ParaTemp[22];
  1496. char *p;
  1497. char d;
  1498. int i=0;
  1499. if(0==*Str || 0==*Para)return 0;
  1500. while(0!=(d=*Para) && ParaLen<20){
  1501. ParaTemp[i++]=d;
  1502. if(d=='=' || d==';')return 0;
  1503. Para++;
  1504. ParaLen++;
  1505. }
  1506. ParaTemp[ParaLen++]='=';
  1507. ParaTemp[ParaLen]=0;
  1508. p=strstr(Str,ParaTemp);
  1509. p+=ParaLen;
  1510. //--
  1511. while(*p>0x20 && ';'!=*p && ValueLen<=80){
  1512. *Value=*p;
  1513. p++;
  1514. Value++;
  1515. ValueLen++;
  1516. }
  1517. *Value=0;
  1518. return ValueLen;
  1519. }
  1520. void IWDG_Configuration(unsigned int value)
  1521. {
  1522. // WWDG 时钟使能
  1523. RCC_APB1PeriphClockCmd(RCC_APB1Periph_WWDG, ENABLE);
  1524. if(RCC_GetFlagStatus(RCC_FLAG_IWDGRST) != RESET)RCC_ClearFlag();
  1525. //写入0x5555,用于允许狗狗寄存器写入功能
  1526. IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
  1527. //狗狗时钟分频,40K/256=156HZ(6.4ms)
  1528. IWDG_SetPrescaler(IWDG_Prescaler_256);
  1529. //喂狗时间
  1530. //IWDG_SetReload(200);1.28s/6.4MS=200 .注意不能大于0xfff
  1531. IWDG_SetReload(value);//3.2s/6.4MS=500 .注意不能大于0xfff
  1532. //喂狗
  1533. IWDG_ReloadCounter();
  1534. //使能狗狗
  1535. IWDG_Enable();
  1536. }
  1537. /*
  1538. 此处修改:删去ModemSendAT("2C00");另外pocTask.cReplaceStrWithN(&msg[9]、msg[17]);强制将字符串后面的\n\r去掉
  1539. */
  1540. void ReportUserGroupName(void)
  1541. {
  1542. #if POC_USER_NAME_LEN>POC_GROUP_NAME_LEN
  1543. char buf[POC_USER_NAME_LEN*4];
  1544. #else
  1545. char buf[POC_GROUP_NAME_LEN*4];
  1546. #endif
  1547. SpeakerEnable();
  1548. ModemSendAT("AT+ZTTS=1,\"");
  1549. AnsiToStrUnicode((uint8_t *)buf,sizeof(buf),sutPocStatus.LocalUser.name); //函数功能未实现,需要修改
  1550. ModemSendAT(buf);
  1551. AnsiToStrUnicode((uint8_t *)buf,sizeof(buf),sutPocStatus.LocalUser.name);
  1552. ModemSendAT(buf);
  1553. ModemSendAT("\"\r");
  1554. }
  1555. void SetPowerEn(unsigned char en)
  1556. {
  1557. GPIO_InitTypeDef GPIO_InitStructure;
  1558. if(en!=0){
  1559. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
  1560. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  1561. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1562. GPIO_InitStructure.GPIO_Pin = PWR_EN_PIN;
  1563. GPIO_Init(PWR_EN_PORT, &GPIO_InitStructure);
  1564. PWR_EN_HIGH;
  1565. }else{
  1566. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE);
  1567. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
  1568. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  1569. GPIO_InitStructure.GPIO_Pin = PWR_EN_PIN;
  1570. GPIO_Init(PWR_EN_PORT, &GPIO_InitStructure);
  1571. PWR_EN_LOW;
  1572. }
  1573. }
  1574. /*****************************************************************************************
  1575. *函数名:GetCheckCodeByPsnMEID
  1576. *功能:根据PSN号和MEID码,生成伪随机码
  1577. *入口:PSN--PSN码,无符号长整形,必须是大于150000000的数值,有可能是9位数也可能是10位数
  1578. pMEID--MEID字串,长度14位或15位,第16字节必须为0。
  1579. *出口:pCheckCode:--长度为6的字符串,第7字节为结束符0x00,因此必须留够7字节的空间!
  1580. 返回:1--成功 0--因参数非法而失败
  1581. ******************************************************************************************/
  1582. const unsigned char sucRandTable[256]={
  1583. 0x68,0x59,0x63,0x64,0xc8,0xcc,0xaa,0xa7,0xa2,0xab,0xa5,0x6e,0x6a,0x56,0x8a,0x57,
  1584. 0xf4,0xbb,0xfe,0xfa,0x26,0x21,0x2d,0x29,0x0d,0x09,0x3e,0x3a,0x37,0x32,0x0a,0x07,
  1585. 0xf7,0xf2,0xfb,0xf5,0xff,0xf6,0x1c,0x18,0x13,0x14,0xf1,0xfd,0xf9,0x0c,0x9f,0x08,
  1586. 0x9a,0x97,0x92,0x9b,0x95,0x9f,0x12,0x1b,0x15,0x2c,0x28,0x23,0xf9,0x2a,0x27,0x22,
  1587. 0x24,0x2e,0x67,0x25,0x2f,0x98,0xfc,0x75,0xf8,0xf3,0x3c,0x38,0x33,0x34,0x03,0x04,
  1588. 0x05,0x0f,0x06,0x01,0x7a,0x77,0x72,0x62,0x6b,0x83,0x84,0x8e,0xc4,0xcb,0x87,0x82,
  1589. 0x35,0x3f,0x36,0x4f,0x3d,0x39,0x9c,0x98,0x93,0x94,0x42,0x4f,0x46,0x41,0x81,0x8d,
  1590. 0x4d,0x49,0x71,0x7d,0x79,0x96,0x91,0x9d,0x99,0xc3,0xc4,0x1d,0xce,0xca,0x7f,0x76,
  1591. 0x7c,0x78,0x73,0x74,0x7e,0x7b,0x75,0x1e,0x1a,0x17,0x1f,0x16,0x11,0x1d,0x47,0x19,
  1592. 0x5d,0xc7,0xc2,0xcb,0xc5,0xcf,0xd1,0xdd,0xd9,0x4c,0x48,0xc6,0xc1,0xcd,0x61,0xc9,
  1593. 0xd7,0x8c,0xfd,0x88,0xa1,0xad,0xa9,0xb1,0xbd,0xb9,0x8b,0x85,0x8f,0x4b,0x45,0x86,
  1594. 0xdc,0xd8,0xd3,0xd4,0xde,0xd2,0xdb,0xd5,0xdf,0xd6,0x43,0x44,0x4e,0x4a,0xe7,0x47,
  1595. 0xac,0xa8,0xa3,0xa4,0xae,0xba,0x0d,0xb7,0xb2,0xaf,0xa6,0xe4,0xee,0xea,0xe7,0xe2,
  1596. 0xe1,0xed,0x0f,0xe9,0x66,0x61,0x6d,0x69,0xbb,0xb5,0xbf,0xb6,0xec,0xe8,0x0b,0xe3,
  1597. 0x52,0x5c,0x65,0x6f,0x58,0x53,0x54,0x0c,0x5e,0x5b,0x55,0x5f,0x31,0x2f,0x9a,0x0b,
  1598. 0x0b,0x5a,0x2b,0x9e,0x02,0x89,0x59,0x3b,0x0e,0x51,0x6c,0xeb,0xda,0xe6,0x31,0xf6
  1599. };
  1600. int GetCheckCodeByPsnMEID(char *pCheckCode,unsigned long PSN,char *pMEID)
  1601. {
  1602. char aucPSN[12];
  1603. char aucMEID[16];
  1604. unsigned char key[10];
  1605. unsigned char temp1,temp2;
  1606. int MeidLen,PsnLen;
  1607. int i,j;
  1608. //PSN合法性检测
  1609. if(PSN<150100000)return 0;
  1610. //MEID合法性检测
  1611. MeidLen=strlen(pMEID);
  1612. //if(MeidLen!=14 && MeidLen!=15)return 0; //MEID号长度必须为14或15
  1613. if(MeidLen!=14 && MeidLen!=15){
  1614. snprintf(aucPSN, sizeof(aucMEID),"%015lu",pMEID); //不够15位前面补零
  1615. }
  1616. //PSN转字符串,不够10位的前面补'0'
  1617. snprintf(aucPSN, sizeof(aucPSN),"%010lu",PSN);
  1618. PsnLen=strlen(aucPSN);
  1619. //取MEID的最后10个字符
  1620. j=0;
  1621. for(i=(MeidLen-10);i<MeidLen;i++){
  1622. aucMEID[j]=pMEID[i];
  1623. j++;
  1624. }
  1625. //所有字节进行相互异或运算,使每个字节的变化对整体结果都敏感
  1626. temp1=0;
  1627. temp2=0;
  1628. for(i=0;i<10;i++){
  1629. temp1^=(unsigned char)aucPSN[i];
  1630. temp2^=(unsigned char)aucMEID[i];
  1631. }
  1632. temp1^=temp2;
  1633. temp2=sucRandTable[temp1];
  1634. //循环查表异或运算,生成key
  1635. for(i=0;i<10;i++){
  1636. temp1=aucPSN[i]^temp2;
  1637. key[i]=aucMEID[i]^temp1;
  1638. temp2=sucRandTable[temp1];
  1639. }
  1640. //取最后6个key,分别除10取余后再转Ascii,作为结果,结果为6个数字串,如"123456"
  1641. j=0;
  1642. for(i=4;i<10;i++){
  1643. pCheckCode[j]=key[i]%10;
  1644. pCheckCode[j]+=0x30;
  1645. j++;
  1646. }
  1647. pCheckCode[6]=0; //结束符,方便调试打印和语音播报。
  1648. return 1;
  1649. }
  1650. //使用12位唯一ID生成唯一7位(14位)鉴权码
  1651. void makeAuthCode(unsigned char *auth)
  1652. {
  1653. unsigned char i,temp1,temp2;
  1654. unsigned char tempAuth[14]="hS";//前面设置成唯一的
  1655. unsigned char srcId[12];
  1656. GetSTM32ID(srcId);
  1657. temp1=0;
  1658. for(i=0;i<12;i++)
  1659. {
  1660. temp1 ^= srcId[i];
  1661. temp1 ^= i;
  1662. }
  1663. memcpy(&tempAuth[2], srcId, 12);
  1664. for(i=0;i<14;i++)
  1665. {
  1666. temp2 = tempAuth[i] ^ sucRandTable[temp1];
  1667. tempAuth[i] = temp1 ^ temp2;
  1668. }
  1669. //组合成7个字节内容
  1670. AscStrToHexStr(tempAuth, auth);
  1671. }
  1672. /***************************************************************************
  1673. 将数字串转Unicode编码,方便播报
  1674. pUnicode长度必须为pNum长度的8倍,否则内存溢出!
  1675. ****************************************************************************/
  1676. void NumStrToUnicode(char *pNum,char *pUnicode)
  1677. {
  1678. char temp[10];
  1679. int i,j,len;
  1680. len=strlen(pNum);
  1681. snprintf(temp, sizeof(temp),"len=%d",len);
  1682. SlwTrace(INF,temp,1);
  1683. j=0;
  1684. for(i=0;i<len;i++){
  1685. pUnicode[j++]='3';
  1686. pUnicode[j++]=pNum[i];
  1687. pUnicode[j++]='0';
  1688. pUnicode[j++]='0';
  1689. if(i==(len-1))break;//最后一个不用加逗号‘,’
  1690. //","
  1691. pUnicode[j++]='2';
  1692. pUnicode[j++]='0';
  1693. pUnicode[j++]='0';
  1694. pUnicode[j++]='0';
  1695. }
  1696. pUnicode[j]=0;
  1697. }
  1698. /**************************************************************
  1699. *ReportCheckCode
  1700. ***************************************************************/
  1701. void ReportCheckCode(void)
  1702. {
  1703. char temp[12];
  1704. char buf[80];
  1705. SpeakerEnable();
  1706. //终端编号xxxx
  1707. if(sutProductPara.PSN<150100000 || sutProductPara.PSN==0xffffffff){
  1708. SlwTrace(INF,"PSN Error!",1);
  1709. ModemSendAT("AT+ZTTS=1,\"C87EEF7A167FF7531995EF8B\"\r");//终端编号错误
  1710. return;
  1711. }
  1712. //校验码xxxx
  1713. if(GetCheckCodeByPsnMEID(temp,sutProductPara.PSN,sutProductPara.HARDID)){
  1714. snprintf(buf, sizeof(buf),"CheckCode=%s",temp);
  1715. SlwTrace(INF,buf,1);
  1716. NumStrToUnicode(temp,buf);
  1717. ModemSendAT("AT+ZTTS=1,\"21688C9A01782C00");//校验码,
  1718. ModemSendAT(buf);
  1719. ModemSendAT("\"\r");
  1720. }else{
  1721. SlwTrace(INF,"CheckCode=NULL",1);
  1722. ModemSendAT("AT+ZTTS=1,\"21688C9A0178B783D6533159258D\"\r");//校验码获取失败
  1723. }
  1724. IWDG_ReloadCounter();//喂狗
  1725. os_dly_wait(100);
  1726. IWDG_ReloadCounter();//喂狗
  1727. os_dly_wait(100);
  1728. IWDG_ReloadCounter();//喂狗
  1729. os_dly_wait(100);
  1730. IWDG_ReloadCounter();//喂狗
  1731. //终端编码xxx
  1732. snprintf(temp, sizeof(temp),"%lu",sutProductPara.PSN);
  1733. snprintf(buf, sizeof(buf),"CheckCode=%s",temp);
  1734. SlwTrace(INF,buf,1);
  1735. NumStrToUnicode(temp,buf);
  1736. ModemSendAT("AT+ZTTS=1,\"C87EEF7A167FF7532C00");//终端编码,
  1737. ModemSendAT(buf);
  1738. ModemSendAT("\"\r");
  1739. }
  1740. /************************************************************************************
  1741. ResetModem
  1742. *************************************************************************************/
  1743. void ResetModem(void)
  1744. {
  1745. //set modem reset
  1746. MODEM_RESET_HIGH;
  1747. os_dly_wait(100);
  1748. MODEM_RESET_LOW;
  1749. MODEM_PWRKEY_HIGH;
  1750. os_dly_wait(100);
  1751. MODEM_PWRKEY_LOW;
  1752. //set uart3 reset
  1753. Uart3Init();
  1754. }
  1755. /*************************************************************************************
  1756. *FingerDaemonfinger
  1757. *守护进程
  1758. *用于防止通讯模块死机,或者与通讯模块通讯的串口异常而重新启动模块或通讯驱动。
  1759. *用于防止GPS模块异常而重启GPS
  1760. *此函数需要确保每秒调用一次
  1761. 入口参数
  1762. sutPocStatus.FirstLogined
  1763. sutPocStatus.PocLostCt
  1764. sutPocStatus.ModemPowerOn
  1765. sutPocStatus.ModemInactiveCt
  1766. g_iCSQ
  1767. **************************************************************************************/
  1768. void FingerDaemonfinger(void)
  1769. {
  1770. static unsigned char sucPocLostTimeoutCount=0;//
  1771. static unsigned char sucModemInactiveCount=0;
  1772. unsigned short PocLostCtMax;
  1773. IWDG_ReloadCounter();//喂狗
  1774. //return;
  1775. if(g_ucModemTaskEn==0)return;
  1776. //如果曾经登陆过POC系统,然后离线,开始计数,直到再次登陆,如果一直不登陆超过一定时间,则重启模块和串口
  1777. if(sutPocStatus.FirstLogined){
  1778. if(sutPocStatus.Logined==0){
  1779. PocLostCtMax=pocTimeOutTime*60;
  1780. if(++sutPocStatus.PocLostCt>PocLostCtMax){
  1781. sutPocStatus.PocLostCt=0;
  1782. //长时间不登陆,重启模块及串口驱动部分
  1783. SlwTrace(INF,"PocLostTimeout!Reset Modem!",1);
  1784. ResetModem();
  1785. }
  1786. }else{
  1787. sutPocStatus.PocLostCt=0;
  1788. sucPocLostTimeoutCount=0;
  1789. }
  1790. }else{
  1791. //一个小时未登录复位模块
  1792. if(++sutPocStatus.PocLostCt>3600){
  1793. sutPocStatus.PocLostCt=0;
  1794. ResetModem();
  1795. }
  1796. }
  1797. //如果模块已开机,长时间收不到串口消息,则重启模块及串口驱动部分
  1798. if(sutPocStatus.ModemPowerOn==1){
  1799. if(sutPocStatus.ModemInactiveCt==0){//ModemInactiveCt必须在接收到Modem串口消息后才清零
  1800. sucModemInactiveCount=0;
  1801. }
  1802. if(++sutPocStatus.ModemInactiveCt>40){//等待时间15秒 4G模块重启后至少要15秒才能启动
  1803. sutPocStatus.ModemInactiveCt=1;
  1804. //长时间收不到串口消息,重启模块及串口驱动
  1805. SlwTrace(INF,"ModemRecvTimeout!Reset Modem!",1);
  1806. ResetModem();
  1807. }
  1808. }
  1809. //GPS守护
  1810. if(sutGpsInfo.isGpsWork>0){
  1811. if(++sutGpsInfo.GpsInactiveCt>10){
  1812. sutGpsInfo.GpsInactiveCt=0;
  1813. //复位GPS
  1814. GPSInit();
  1815. Uart3Init();
  1816. TSGpsDataInit();
  1817. }
  1818. }
  1819. }
  1820. /**************************************
  1821. *延时(带喂狗)
  1822. t 系统滴答为单位
  1823. ***************************************/
  1824. void OsDelayTime(unsigned short t)
  1825. {
  1826. int i;
  1827. for(i=0;i<t;i++){
  1828. os_dly_wait(1);
  1829. IWDG_ReloadCounter();//喂狗
  1830. }
  1831. }
  1832. //255.255.255.255
  1833. //-->0xff 0xff 0xff 0xff
  1834. unsigned char GetStringIP2Bytes(unsigned char *IPBytes,char *IPString)
  1835. {
  1836. unsigned char len,i,segment,j;
  1837. char *p;
  1838. char temp[3];
  1839. len = strlen(IPString);
  1840. if(len<7 || len >15) return 1;
  1841. segment=0;
  1842. j=0;
  1843. p=IPString;
  1844. for(i=0;i<len;i++)
  1845. {
  1846. if(IPString[i] == '.')
  1847. {
  1848. if(segment < 4) IPBytes[segment++] = atoi(p);
  1849. else return 2;
  1850. p=&IPString[i+1];
  1851. }else if(IPString[i] < '0' || IPString[i] > '9') return 3;
  1852. }
  1853. if(segment < 5) IPBytes[segment++] = atoi(p);
  1854. return 0;
  1855. }
  1856. void MakeStrEndByNewLine(char *str)
  1857. {//一定要以'\r'结尾
  1858. unsigned short i;
  1859. for(i=0;i<strlen(str);i++)
  1860. {
  1861. if(str[i] == '\r')
  1862. {
  1863. str[i]=0;
  1864. return;
  1865. }
  1866. }
  1867. }
  1868. uint16_t StrUnicodeToAnsi(uint8_t *dest,uint16_t size,const char *src)
  1869. {
  1870. //不需要转
  1871. }
  1872. uint16_t AnsiToStrUnicode(uint8_t *dest,uint16_t size,const char *src)
  1873. {
  1874. //不需要转
  1875. }
  1876. //"3132" --> 0x31,0x32
  1877. void StrAsciiToHex(char *src, unsigned char *des)
  1878. {
  1879. unsigned char temp[2],i;
  1880. if(strlen(src)%2) return;
  1881. while(0!=*src)
  1882. {
  1883. for(i=0;i<2;i++)
  1884. {
  1885. temp[i] = *src++;
  1886. if(temp[i] >= '0' && temp[i] <= '9') temp[i] -= 0x30;
  1887. else if(temp[i] >= 'A' && temp[i] <= 'F') temp[i] -= 0x37;
  1888. else temp[i] -= 0x57;
  1889. }
  1890. temp[0] <<= 4;
  1891. temp[0] &= 0xf0;
  1892. *des++=temp[0]|temp[1];
  1893. }
  1894. *des=0;
  1895. }
  1896. void PrintBufferData(unsigned char *data, unsigned short length)
  1897. {
  1898. unsigned short i;
  1899. printf("Data[%d]:\r\n",length);
  1900. for(i=0;i<length;i++) printf("%02x",data[i]);
  1901. printf("\r\n");
  1902. }