MW3650Mode.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. /**********************************************************************************
  2. * File Name: MW3650Mode.c
  3. * Function Describe: Header file for MW3650Mode.c
  4. * Function Describe:device for modem
  5. * Relate Module:
  6. * Explain: the modem must be using ZTE MW3650
  7. * Date: 2016-3-11
  8. ***********************************************************************************/
  9. #include "includes.h"
  10. /****************************************************************
  11. 获得WCDMA的版本号
  12. 与CDMA的主要不同之处在于pMsg->MsgData的位置不同
  13. *****************************************************************/
  14. int ModemGetMW3650Ver(void)
  15. {
  16. int i,ver;
  17. SUT_MODEM_AT_MSG *pMsg;
  18. for(i=0;i<20;i++){
  19. //When send "AT+ZINVER\r" to modem,it will return "+ZINVER: MW3650_SMZA100 [Nov 17 2015 11:27:28]"
  20. if(ModemSendAT2WaitAckMsg("AT+ZINVER\r\n","+ZINVER:",&pMsg,100)){
  21. ver=(pMsg->MsgData[19]-0x30)*100+(pMsg->MsgData[20]-0x30)*10+(pMsg->MsgData[21]-0x30);
  22. _free_box(AtMpool,pMsg);
  23. return ver;
  24. }
  25. }
  26. return 0;
  27. }
  28. /********************************************************************
  29. ModemCheckSIM
  30. 检查SIM卡是否准备好
  31. Ready: return 1
  32. Error: return 0
  33. *********************************************************************/
  34. int ModemCheckSIM(void)
  35. {
  36. int i;
  37. SUT_MODEM_AT_MSG *pMsg;
  38. for(i=0;i<10;i++){
  39. if(ModemSendAT2WaitAckMsg("AT+CPIN?\r\n","+CPIN: READY",&pMsg,100)){
  40. _free_box(AtMpool,pMsg);
  41. return 1;
  42. }
  43. }
  44. return 0;
  45. }
  46. /********************************************************************
  47. *设置网络MW3650的APN---Define PDP Context
  48. char buf[55];
  49. char buf1[110];
  50. SUT_MODEM_AT_MSG *pMsg;
  51. RunMake(THIS_FILE_ID);
  52. for(i=0;i<5;i++){
  53. ModemSendAT("AT+POC=020000\r\n");
  54. t=50;
  55. while(t--){
  56. if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
  57. if(0==ModemStrCmp((char *)pMsg->MsgData,"+POC:02")){
  58. sprintf(buf,"ip=%s;id=%lu",sutProductPara.PIP,sutProductPara.PSN);//ip=183.131.13.10;id=159900101;
  59. AscStrToHexStr(buf,buf1);
  60. if(0!=ModemStrCmp((char *)&pMsg->MsgData[13],buf1)){
  61. //重新设置IP和POC
  62. sprintf(buf,"ip=%s;id=%lu;pwd=%s;",sutProductPara.PIP,sutProductPara.PSN,sutProductPara.MEID);
  63. AscStrToHexStr(buf,buf1);
  64. ModemSendAT("AT+POC=010000");
  65. ModemSendAT(buf1);
  66. ModemSendAT("\r\n");
  67. _free_box(AtMpool,pMsg);
  68. return 1;
  69. }else{
  70. _free_box(AtMpool,pMsg);
  71. return 0;
  72. }
  73. }
  74. _free_box(AtMpool,pMsg);
  75. }
  76. }
  77. }
  78. *********************************************************************/
  79. int ModemCheckPDP(void)
  80. {
  81. int i,t;
  82. SUT_MODEM_AT_MSG *pMsg;
  83. // return 0;
  84. for(i=0;i<3;i++){
  85. //if(ModemSendAT2WaitAckMsg("AT+ZINVER\r\n","+ZINVER:",&pMsg,100)){
  86. if(ModemSendAT2WaitAckMsg("AT+CGDCONT?\r\n","+CGDCONT: 1,\"IP\",\"UNIM2M.NJM2MAPN\"",&pMsg,100)){
  87. _free_box(AtMpool,pMsg);
  88. return 1;
  89. }else{
  90. ModemSendAT("AT+CGDCONT=1,\"IP\",\"UNIM2M.NJM2MAPN\"\r\n");
  91. }
  92. }
  93. ModemSendAT("AT+CGDCONT?\r\n");
  94. return 0;
  95. }
  96. /*****************************************************
  97. if(0==strcmp("NULL",sutProductPara.APN)){
  98. SlwTrace(INF,"param APN='NULL',do nothing!\r\n");
  99. ModemSendAT("AT+CGDCONT?\r\n");
  100. return 0;
  101. }
  102. for(i=0;i<3;i++){
  103. sprintf(buf,"AT+CGDCONT=1,\"IP\",\"%s\"\r\n",sutProductPara.APN);
  104. ModemSendAT(buf);
  105. sprintf(ckbuf,"+CGDCONT: 1,\"IP\",\"%s\"",sutProductPara.APN);
  106. if(ModemSendAT2WaitAckMsg("AT+CGDCONT?\r\n",ckbuf,&pMsg,100)){
  107. _free_box(AtMpool,pMsg);
  108. return 1;
  109. }
  110. }
  111. ***********************************************************/
  112. //要求设置APN为UNIM2M.NJM2MAPN
  113. /********************************************************************
  114. *return 1 重写
  115. *return 0 不用重写
  116. #if 1
  117. //先读APN
  118. //再判断APN和预期值是否一致
  119. //如果不一致,最后重新设置
  120. //预期的APN是"NULL"则什么也不做
  121. if(0==strcmp("NULL",pApnName)){
  122. SlwTrace(INF,"param APN='NULL',do nothing!\r\n");
  123. ModemSendAT("AT+CGDCONT?\r\n");
  124. return 0;
  125. }
  126. //查询模块内已有的值
  127. for(k=0;k<3;k++){
  128. ModemSendAT("AT+CGDCONT?\r\n");
  129. t=50;
  130. while(t--){
  131. if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
  132. if(0==ModemStrCmp((char *)pMsg->MsgData,"+CGDCONT: 1")){
  133. pTemp=(char *)&(pMsg->MsgData[18]);
  134. for(i=0;i<15;i++){
  135. if(*pTemp=='"'){
  136. pTemp++;
  137. break;
  138. }
  139. name[i]=*pTemp++;
  140. }
  141. name[i]=0;
  142. for(i=0;i<15;i++){
  143. if(*pTemp=='"')break;
  144. pass[i]=*pTemp++;
  145. }
  146. pass[i]=0;
  147. _free_box(AtMpool,pMsg);
  148. break;
  149. }else{
  150. _free_box(AtMpool,pMsg);
  151. }
  152. }
  153. }
  154. }
  155. if(i==3){
  156. SlwTrace(INF,"Read APN Timeout!\r\n");
  157. return 0;
  158. }
  159. //打印查到的模块内的值
  160. SlwTrace(INF,"Read APN: ");
  161. SlwTrace(INF,name);
  162. SlwTrace(INF,",");
  163. SlwTrace(INF,pass);
  164. SlwTrace(INF,"\r\n");
  165. //比较模块内的值和MCU内的值,不同则重写
  166. if(0!=strcmp(pApnName,name) || 0!=strcmp(pApnPass,pass)){
  167. sprintf(buf,"AT+CGDCONT=1,\"%s\",\"%s\"\r\n",pApnName,pApnPass);
  168. ModemSendAT(buf);
  169. SlwTrace(INF,buf);
  170. return 1;
  171. }
  172. return 0;
  173. #else
  174. return ModemCheckPDP();
  175. #endif
  176. *********************************************************************/
  177. int ModemSetMW3650APN(char *pApnName,char *pApnPass)
  178. {
  179. //先读APN
  180. //AT+ZPIDPWD?
  181. //+ZPIDPWD: card,card
  182. //AT+CGDCONT?先查询读取
  183. //
  184. int t;
  185. char name[16]={0};
  186. char pass[16]={0};
  187. char buf[30];
  188. char *pTemp;
  189. int k,i,len;
  190. SUT_MODEM_AT_MSG *pMsg;
  191. // RunMake(THIS_FILE_ID);
  192. for(k=0;k<3;k++){
  193. // ModemSendAT("AT+ZPIDPWD?\r\n");//+ZPIDPWD: card,card
  194. ModemSendAT("AT+CGDCONT?\r\n");
  195. t=50;
  196. while(t--){
  197. if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
  198. //if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZPIDPWD:")){
  199. if(0==ModemStrCmp((char *)pMsg->MsgData,"+CGDCONT: 1")){
  200. //get name +CGDCONT: 1,"IP","UNIM2M.NJM2MAPN","0.0.0.0",0,0
  201. //APN UNIM2M.NJM2MAPN
  202. pTemp=(char *)&(pMsg->MsgData[18]);
  203. for(i=0;i<15;i++){
  204. if(*pTemp=='"'){
  205. pTemp++;
  206. break;
  207. }
  208. name[i]=*pTemp++;
  209. }
  210. name[i]=0;
  211. //get password PDP_addr
  212. for(i=0;i<15;i++){
  213. //if(*pTemp<0x20)break;
  214. if(*pTemp=='"')break;
  215. pass[i]=*pTemp++;
  216. }
  217. pass[i]=0;
  218. _free_box(AtMpool,pMsg);
  219. break;
  220. }else{
  221. _free_box(AtMpool,pMsg);
  222. }
  223. }
  224. }
  225. }
  226. if(i==3){
  227. SlwTrace(INF,"Read APN Timeout!\r\n");
  228. return 0;
  229. }
  230. SlwTrace(INF,"APN Read:");
  231. SlwTrace(INF,name);
  232. SlwTrace(INF,",");
  233. SlwTrace(INF,pass);
  234. SlwTrace(INF,"\r\n");
  235. //--比较,不同则重写
  236. if(0!=strcmp(pApnName,name) || 0!=strcmp(pApnPass,pass)){
  237. sprintf(buf,"AT+CGDCONT=1,\"%s\",\"%s\"\r\n",pApnName,pApnPass);
  238. ModemSendAT(buf);
  239. return 1;
  240. }
  241. return 0;
  242. }
  243. int MW3650ModemWaitRegistered(void)
  244. {
  245. int i,j,k;
  246. char buffer[2];
  247. SUT_MODEM_AT_MSG *pMsg;
  248. //wait CREG OK
  249. for(i=0;i<10;i++){
  250. if(ModemSendAT2WaitAckMsg("AT+CREG?\r\n","+CREG:",&pMsg,100)){
  251. if(pMsg->MsgData[9]=='1' || pMsg->MsgData[9]=='5'){
  252. _free_box(AtMpool,pMsg);
  253. break;
  254. }else{
  255. _free_box(AtMpool,pMsg);
  256. }
  257. }
  258. os_dly_wait(200);
  259. }
  260. if(i==10){
  261. SlwTrace(INF,"Network not registered!!\r\n");
  262. return 0;
  263. }
  264. SlwTrace(INF,"Network Registered!!\r\n");
  265. //AT+COPS?
  266. //设置COPS AT+COPS=1,0,"CHN-UNICOM",2
  267. //ModemSendAT("AT+COPS=1,0,\"CHN-UNICOM\",2");//
  268. //os_dly_wait(200);//
  269. for(i=0;i<10;i++){
  270. // ModemSendAT("AT+COPS?");//
  271. // if(ModemSendAT2WaitAckMsg("+\r\n","+COPS:",&pMsg,100)){
  272. if(ModemSendAT2WaitAckMsg("AT+COPS?\r\n","+COPS:",&pMsg,100)){
  273. //找到第二个,
  274. k=0;
  275. for(j=0;j<pMsg->MsgLen;j++){
  276. if(pMsg->MsgData[j]==',')k++;
  277. if(k==3)break;
  278. }
  279. if(j<pMsg->MsgLen && pMsg->MsgData[j+1]=='2'){
  280. _free_box(AtMpool,pMsg);
  281. break;
  282. }else{
  283. _free_box(AtMpool,pMsg);
  284. os_dly_wait(200);
  285. }
  286. }
  287. }
  288. if(i==10){
  289. SlwTrace(INF,"COPS FAIL!\r\n");
  290. return 0;
  291. }
  292. SlwTrace(INF,"COPS OK!\r\n");
  293. }
  294. void CheckCardStatMW3650(void)
  295. {
  296. unsigned short susCt=0;
  297. SUT_MODEM_AT_MSG *pMsg;
  298. if(!ModemCheckSIM())
  299. //卡不在位,循环播报请插卡
  300. {
  301. susCt=900;
  302. SetPowerEn(0);
  303. SlwTrace(DEBUG,"Check SIM Card Fail!\r\n");
  304. #if(USING_LANGUAGE==USING_CHINESE)
  305. ModemSendAT("AT+ZTTS=1,\"F78BD2636153\"\r");//请插卡
  306. #else
  307. ModemSendAT("AT+ZTTS=1,\"69006E0073006500720074006300610072006400\"\r");//insert cart
  308. #endif
  309. SpeakerEnable();
  310. os_dly_wait(150);
  311. SpeakerDisable();
  312. while(1){
  313. if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
  314. //释放消息缓冲区
  315. _free_box(AtMpool,pMsg);
  316. continue;
  317. }
  318. sutPocStatus.ModemInactiveCt=0;
  319. if(0==g_ucModemTaskEn)continue;
  320. //GPS Uart消息处理
  321. if(g_usUart2RecvLen){
  322. process_nema((char *)RxBuffer2);
  323. g_usUart2RecvLen=0;
  324. }
  325. if(++susCt>=3500)susCt=0;
  326. else continue;
  327. #if(USING_LANGUAGE==USING_CHINESE)
  328. ModemSendAT("AT+ZTTS=1,\"F78BD2636153\"\r");//请插卡
  329. #else
  330. ModemSendAT("AT+ZTTS=1,\"69006E0073006500720074006300610072006400\"\r");//insert cart
  331. #endif
  332. SpeakerEnable();
  333. os_dly_wait(150);
  334. SpeakerDisable();
  335. SlwTrace(INF,"No SIM Card!\r\n");
  336. }
  337. }
  338. }
  339. int MW3650ModemSendTcpData(unsigned char socket,unsigned char *pData,unsigned short len)
  340. {
  341. int i;
  342. char buf[142];
  343. char tmp[5];
  344. unsigned char temp=0;
  345. unsigned short debuglen;
  346. SUT_MODEM_AT_MSG *pMsg;
  347. /*此处要修改成为MW3650的AT指令*/
  348. //AT^IPSEND=1,"2929B100075986B38400070D"
  349. //sprintf(buf,"AT+ZIPSEND=%d,%d\r",socket,len);
  350. if(len>100){
  351. SlwTrace(INF,"ModemSendTcpData Over!");
  352. return 0;
  353. }
  354. sprintf(buf,"AT^IPSEND=%d,\"\r\n",socket);
  355. //ModemSendAT("AT^IPSEND=1,\"29298000285986B383150725154118022375491140132200070128F801E24000007F0000050000000100004F0D\");
  356. HexToStr(pData,&buf[13],len);
  357. i=13+len*2;
  358. buf[i++]='\"';buf[i++]='\r';buf[i++]='\n';buf[i++]='\0';
  359. ModemSendAT(buf);
  360. SlwTrace(DEBUG,buf);
  361. //^IPRECV:1,183.131.13.6,1391,0,292921000559B100CC0D
  362. ModemSendData(pData,len);
  363. if(g_ucModemSentTcpCt<255)g_ucModemSentTcpCt++;
  364. sprintf(buf,"TcpSend[%d]\r\n",len);
  365. SlwTrace(INF,buf);
  366. #if 0
  367. if(len>45)debuglen=45;
  368. else debuglen=len;
  369. buf[0]='>';buf[1]='>';buf[2]=0;
  370. for(i=0;i<debuglen;i++){
  371. sprintf(tmp,"%02x,",pData[i]);
  372. strcat(buf,tmp);
  373. }
  374. strcat(buf,"\r\n");
  375. SlwTrace(DEBUG,buf);
  376. #endif
  377. sutGpsInfo.isServerLogin = OPEN;
  378. return 0;
  379. }
  380. /*******************************************************************
  381. *ModemGetIMEI
  382. 返回:0--未获取 1--获取成功
  383. ********************************************************************/
  384. int MW3650ModemGetIMEI(void)
  385. {
  386. int t;
  387. int i,len;
  388. SUT_MODEM_AT_MSG *pMsg;
  389. // RunMake(THIS_FILE_ID);
  390. for(i=0;i<60;i++){
  391. ModemSendAT("AT+CGSN\r\n");//
  392. t=100;
  393. while(t--){
  394. if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
  395. SlwTrace(INF,(char *)pMsg->MsgData);
  396. if(0==ModemStrCmp((char *)pMsg->MsgData,"+CGSN")){
  397. len=strlen((char *)&(pMsg->MsgData[6]));
  398. if(16==len){
  399. for(i=0;i<14;i++)sutProductPara.MEID[i]=pMsg->MsgData[6+i];
  400. sutProductPara.MEID[14]=0;
  401. // SaveProductParaToFlash();
  402. _free_box(AtMpool,pMsg);
  403. return 1;
  404. }else if(17==len){
  405. for(i=0;i<15;i++)sutProductPara.MEID[i]=pMsg->MsgData[6+i];
  406. sutProductPara.MEID[15]=0;
  407. // SaveProductParaToFlash();
  408. _free_box(AtMpool,pMsg);
  409. return 1;
  410. }
  411. }
  412. _free_box(AtMpool,pMsg);
  413. }
  414. }
  415. }
  416. // strcpy(sutSMSpara.Mesage,name);
  417. // SaveProductParaToFlash();
  418. return 0;
  419. }