123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433 |
- /**********************************************************************************
- * File Name: MW3650Mode.c
- * Function Describe: Header file for MW3650Mode.c
- * Function Describe:device for modem
- * Relate Module:
- * Explain: the modem must be using ZTE MW3650
- * Date: 2016-3-11
- ***********************************************************************************/
- #include "includes.h"
- /****************************************************************
- 获得WCDMA的版本号
- 与CDMA的主要不同之处在于pMsg->MsgData的位置不同
- *****************************************************************/
- int ModemGetMW3650Ver(void)
- {
- int i,ver;
- SUT_MODEM_AT_MSG *pMsg;
- for(i=0;i<20;i++){
- //When send "AT+ZINVER\r" to modem,it will return "+ZINVER: MW3650_SMZA100 [Nov 17 2015 11:27:28]"
- if(ModemSendAT2WaitAckMsg("AT+ZINVER\r\n","+ZINVER:",&pMsg,100)){
- ver=(pMsg->MsgData[19]-0x30)*100+(pMsg->MsgData[20]-0x30)*10+(pMsg->MsgData[21]-0x30);
- _free_box(AtMpool,pMsg);
- return ver;
- }
- }
- return 0;
- }
- /********************************************************************
- ModemCheckSIM
- 检查SIM卡是否准备好
- Ready: return 1
- Error: return 0
- *********************************************************************/
- int ModemCheckSIM(void)
- {
- int i;
- SUT_MODEM_AT_MSG *pMsg;
- for(i=0;i<10;i++){
- if(ModemSendAT2WaitAckMsg("AT+CPIN?\r\n","+CPIN: READY",&pMsg,100)){
- _free_box(AtMpool,pMsg);
- return 1;
- }
- }
- return 0;
- }
- /********************************************************************
- *设置网络MW3650的APN---Define PDP Context
- char buf[55];
- char buf1[110];
- SUT_MODEM_AT_MSG *pMsg;
- RunMake(THIS_FILE_ID);
- for(i=0;i<5;i++){
- ModemSendAT("AT+POC=020000\r\n");
- t=50;
- while(t--){
- if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
- if(0==ModemStrCmp((char *)pMsg->MsgData,"+POC:02")){
- sprintf(buf,"ip=%s;id=%lu",sutProductPara.PIP,sutProductPara.PSN);//ip=183.131.13.10;id=159900101;
- AscStrToHexStr(buf,buf1);
- if(0!=ModemStrCmp((char *)&pMsg->MsgData[13],buf1)){
- //重新设置IP和POC
- sprintf(buf,"ip=%s;id=%lu;pwd=%s;",sutProductPara.PIP,sutProductPara.PSN,sutProductPara.MEID);
- AscStrToHexStr(buf,buf1);
- ModemSendAT("AT+POC=010000");
- ModemSendAT(buf1);
- ModemSendAT("\r\n");
- _free_box(AtMpool,pMsg);
- return 1;
- }else{
- _free_box(AtMpool,pMsg);
- return 0;
- }
- }
- _free_box(AtMpool,pMsg);
- }
- }
- }
- *********************************************************************/
- int ModemCheckPDP(void)
- {
- int i,t;
- SUT_MODEM_AT_MSG *pMsg;
- // return 0;
- for(i=0;i<3;i++){
- //if(ModemSendAT2WaitAckMsg("AT+ZINVER\r\n","+ZINVER:",&pMsg,100)){
- if(ModemSendAT2WaitAckMsg("AT+CGDCONT?\r\n","+CGDCONT: 1,\"IP\",\"UNIM2M.NJM2MAPN\"",&pMsg,100)){
- _free_box(AtMpool,pMsg);
- return 1;
- }else{
- ModemSendAT("AT+CGDCONT=1,\"IP\",\"UNIM2M.NJM2MAPN\"\r\n");
- }
- }
- ModemSendAT("AT+CGDCONT?\r\n");
- return 0;
- }
- /*****************************************************
- if(0==strcmp("NULL",sutProductPara.APN)){
- SlwTrace(INF,"param APN='NULL',do nothing!\r\n");
- ModemSendAT("AT+CGDCONT?\r\n");
- return 0;
- }
- for(i=0;i<3;i++){
- sprintf(buf,"AT+CGDCONT=1,\"IP\",\"%s\"\r\n",sutProductPara.APN);
- ModemSendAT(buf);
- sprintf(ckbuf,"+CGDCONT: 1,\"IP\",\"%s\"",sutProductPara.APN);
- if(ModemSendAT2WaitAckMsg("AT+CGDCONT?\r\n",ckbuf,&pMsg,100)){
- _free_box(AtMpool,pMsg);
- return 1;
- }
- }
- ***********************************************************/
- //要求设置APN为UNIM2M.NJM2MAPN
- /********************************************************************
- *return 1 重写
- *return 0 不用重写
- #if 1
- //先读APN
- //再判断APN和预期值是否一致
- //如果不一致,最后重新设置
- //预期的APN是"NULL"则什么也不做
- if(0==strcmp("NULL",pApnName)){
- SlwTrace(INF,"param APN='NULL',do nothing!\r\n");
- ModemSendAT("AT+CGDCONT?\r\n");
- return 0;
- }
- //查询模块内已有的值
- for(k=0;k<3;k++){
- ModemSendAT("AT+CGDCONT?\r\n");
- t=50;
- while(t--){
- if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
- if(0==ModemStrCmp((char *)pMsg->MsgData,"+CGDCONT: 1")){
- pTemp=(char *)&(pMsg->MsgData[18]);
- for(i=0;i<15;i++){
- if(*pTemp=='"'){
- pTemp++;
- break;
- }
- name[i]=*pTemp++;
- }
- name[i]=0;
- for(i=0;i<15;i++){
- if(*pTemp=='"')break;
- pass[i]=*pTemp++;
- }
- pass[i]=0;
- _free_box(AtMpool,pMsg);
- break;
- }else{
- _free_box(AtMpool,pMsg);
- }
- }
- }
- }
- if(i==3){
- SlwTrace(INF,"Read APN Timeout!\r\n");
- return 0;
- }
- //打印查到的模块内的值
- SlwTrace(INF,"Read APN: ");
- SlwTrace(INF,name);
- SlwTrace(INF,",");
- SlwTrace(INF,pass);
- SlwTrace(INF,"\r\n");
- //比较模块内的值和MCU内的值,不同则重写
- if(0!=strcmp(pApnName,name) || 0!=strcmp(pApnPass,pass)){
- sprintf(buf,"AT+CGDCONT=1,\"%s\",\"%s\"\r\n",pApnName,pApnPass);
- ModemSendAT(buf);
- SlwTrace(INF,buf);
- return 1;
- }
- return 0;
- #else
- return ModemCheckPDP();
- #endif
- *********************************************************************/
- int ModemSetMW3650APN(char *pApnName,char *pApnPass)
- {
- //先读APN
- //AT+ZPIDPWD?
- //+ZPIDPWD: card,card
- //AT+CGDCONT?先查询读取
- //
- int t;
- char name[16]={0};
- char pass[16]={0};
- char buf[30];
- char *pTemp;
- int k,i,len;
- SUT_MODEM_AT_MSG *pMsg;
- // RunMake(THIS_FILE_ID);
- for(k=0;k<3;k++){
- // ModemSendAT("AT+ZPIDPWD?\r\n");//+ZPIDPWD: card,card
- ModemSendAT("AT+CGDCONT?\r\n");
- t=50;
- while(t--){
- if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
- //if(0==ModemStrCmp((char *)pMsg->MsgData,"+ZPIDPWD:")){
- if(0==ModemStrCmp((char *)pMsg->MsgData,"+CGDCONT: 1")){
- //get name +CGDCONT: 1,"IP","UNIM2M.NJM2MAPN","0.0.0.0",0,0
- //APN UNIM2M.NJM2MAPN
- pTemp=(char *)&(pMsg->MsgData[18]);
- for(i=0;i<15;i++){
- if(*pTemp=='"'){
- pTemp++;
- break;
- }
- name[i]=*pTemp++;
- }
- name[i]=0;
- //get password PDP_addr
- for(i=0;i<15;i++){
- //if(*pTemp<0x20)break;
- if(*pTemp=='"')break;
- pass[i]=*pTemp++;
- }
- pass[i]=0;
- _free_box(AtMpool,pMsg);
- break;
- }else{
- _free_box(AtMpool,pMsg);
- }
- }
- }
- }
- if(i==3){
- SlwTrace(INF,"Read APN Timeout!\r\n");
- return 0;
- }
- SlwTrace(INF,"APN Read:");
- SlwTrace(INF,name);
- SlwTrace(INF,",");
- SlwTrace(INF,pass);
- SlwTrace(INF,"\r\n");
- //--比较,不同则重写
- if(0!=strcmp(pApnName,name) || 0!=strcmp(pApnPass,pass)){
- sprintf(buf,"AT+CGDCONT=1,\"%s\",\"%s\"\r\n",pApnName,pApnPass);
- ModemSendAT(buf);
- return 1;
- }
- return 0;
- }
- int MW3650ModemWaitRegistered(void)
- {
- int i,j,k;
- char buffer[2];
- SUT_MODEM_AT_MSG *pMsg;
- //wait CREG OK
- for(i=0;i<10;i++){
- if(ModemSendAT2WaitAckMsg("AT+CREG?\r\n","+CREG:",&pMsg,100)){
- if(pMsg->MsgData[9]=='1' || pMsg->MsgData[9]=='5'){
- _free_box(AtMpool,pMsg);
- break;
- }else{
- _free_box(AtMpool,pMsg);
- }
- }
- os_dly_wait(200);
- }
- if(i==10){
- SlwTrace(INF,"Network not registered!!\r\n");
- return 0;
- }
- SlwTrace(INF,"Network Registered!!\r\n");
- //AT+COPS?
- //设置COPS AT+COPS=1,0,"CHN-UNICOM",2
-
- //ModemSendAT("AT+COPS=1,0,\"CHN-UNICOM\",2");//
- //os_dly_wait(200);//
-
- for(i=0;i<10;i++){
- // ModemSendAT("AT+COPS?");//
- // if(ModemSendAT2WaitAckMsg("+\r\n","+COPS:",&pMsg,100)){
- if(ModemSendAT2WaitAckMsg("AT+COPS?\r\n","+COPS:",&pMsg,100)){
- //找到第二个,
- k=0;
- for(j=0;j<pMsg->MsgLen;j++){
- if(pMsg->MsgData[j]==',')k++;
- if(k==3)break;
- }
- if(j<pMsg->MsgLen && pMsg->MsgData[j+1]=='2'){
- _free_box(AtMpool,pMsg);
- break;
- }else{
- _free_box(AtMpool,pMsg);
- os_dly_wait(200);
- }
- }
- }
- if(i==10){
- SlwTrace(INF,"COPS FAIL!\r\n");
- return 0;
- }
-
- SlwTrace(INF,"COPS OK!\r\n");
- }
- void CheckCardStatMW3650(void)
- {
- unsigned short susCt=0;
- SUT_MODEM_AT_MSG *pMsg;
- if(!ModemCheckSIM())
- //卡不在位,循环播报请插卡
- {
- susCt=900;
- SetPowerEn(0);
- SlwTrace(DEBUG,"Check SIM Card Fail!\r\n");
- #if(USING_LANGUAGE==USING_CHINESE)
- ModemSendAT("AT+ZTTS=1,\"F78BD2636153\"\r");//请插卡
- #else
- ModemSendAT("AT+ZTTS=1,\"69006E0073006500720074006300610072006400\"\r");//insert cart
- #endif
- SpeakerEnable();
- os_dly_wait(150);
- SpeakerDisable();
- while(1){
- if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
- //释放消息缓冲区
- _free_box(AtMpool,pMsg);
- continue;
- }
- sutPocStatus.ModemInactiveCt=0;
- if(0==g_ucModemTaskEn)continue;
- //GPS Uart消息处理
- if(g_usUart2RecvLen){
- process_nema((char *)RxBuffer2);
- g_usUart2RecvLen=0;
- }
- if(++susCt>=3500)susCt=0;
- else continue;
- #if(USING_LANGUAGE==USING_CHINESE)
- ModemSendAT("AT+ZTTS=1,\"F78BD2636153\"\r");//请插卡
- #else
- ModemSendAT("AT+ZTTS=1,\"69006E0073006500720074006300610072006400\"\r");//insert cart
- #endif
- SpeakerEnable();
- os_dly_wait(150);
- SpeakerDisable();
- SlwTrace(INF,"No SIM Card!\r\n");
- }
- }
- }
- int MW3650ModemSendTcpData(unsigned char socket,unsigned char *pData,unsigned short len)
- {
- int i;
- char buf[142];
- char tmp[5];
- unsigned char temp=0;
- unsigned short debuglen;
- SUT_MODEM_AT_MSG *pMsg;
- /*此处要修改成为MW3650的AT指令*/
- //AT^IPSEND=1,"2929B100075986B38400070D"
- //sprintf(buf,"AT+ZIPSEND=%d,%d\r",socket,len);
- if(len>100){
- SlwTrace(INF,"ModemSendTcpData Over!");
- return 0;
- }
- sprintf(buf,"AT^IPSEND=%d,\"\r\n",socket);
- //ModemSendAT("AT^IPSEND=1,\"29298000285986B383150725154118022375491140132200070128F801E24000007F0000050000000100004F0D\");
- HexToStr(pData,&buf[13],len);
- i=13+len*2;
- buf[i++]='\"';buf[i++]='\r';buf[i++]='\n';buf[i++]='\0';
- ModemSendAT(buf);
- SlwTrace(DEBUG,buf);
- //^IPRECV:1,183.131.13.6,1391,0,292921000559B100CC0D
- ModemSendData(pData,len);
- if(g_ucModemSentTcpCt<255)g_ucModemSentTcpCt++;
- sprintf(buf,"TcpSend[%d]\r\n",len);
- SlwTrace(INF,buf);
- #if 0
- if(len>45)debuglen=45;
- else debuglen=len;
-
- buf[0]='>';buf[1]='>';buf[2]=0;
- for(i=0;i<debuglen;i++){
- sprintf(tmp,"%02x,",pData[i]);
- strcat(buf,tmp);
- }
- strcat(buf,"\r\n");
- SlwTrace(DEBUG,buf);
- #endif
- sutGpsInfo.isServerLogin = OPEN;
- return 0;
- }
- /*******************************************************************
- *ModemGetIMEI
- 返回:0--未获取 1--获取成功
- ********************************************************************/
- int MW3650ModemGetIMEI(void)
- {
- int t;
- int i,len;
- SUT_MODEM_AT_MSG *pMsg;
- // RunMake(THIS_FILE_ID);
- for(i=0;i<60;i++){
- ModemSendAT("AT+CGSN\r\n");//
- t=100;
- while(t--){
- if(OS_R_TMO!=os_mbx_wait(AtMsgBox,(void **)&pMsg,1)){
- SlwTrace(INF,(char *)pMsg->MsgData);
- if(0==ModemStrCmp((char *)pMsg->MsgData,"+CGSN")){
- len=strlen((char *)&(pMsg->MsgData[6]));
- if(16==len){
- for(i=0;i<14;i++)sutProductPara.MEID[i]=pMsg->MsgData[6+i];
- sutProductPara.MEID[14]=0;
- // SaveProductParaToFlash();
- _free_box(AtMpool,pMsg);
- return 1;
- }else if(17==len){
- for(i=0;i<15;i++)sutProductPara.MEID[i]=pMsg->MsgData[6+i];
- sutProductPara.MEID[15]=0;
- // SaveProductParaToFlash();
- _free_box(AtMpool,pMsg);
- return 1;
- }
- }
- _free_box(AtMpool,pMsg);
- }
- }
- }
- // strcpy(sutSMSpara.Mesage,name);
- // SaveProductParaToFlash();
- return 0;
- }
|