123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488 |
- #include "includes.h"
- COLONY_TYPEDEF sutColony;
- unsigned char CCID[21];
- void ConolyInit(void)
- {
- memset((unsigned char *)&sutColony, 0, sizeof(COLONY_TYPEDEF));
- ColonyFeature(1);//默认开机处理一次业务
- }
- /**********************************************************************************************************************
- 格式:AT+ICCID
- +GETICCID:0x89860315402521836592
- +SCID:98680036904030021872
- ***********************************************************************************************************************/
- short ModemGetCCID(void)
- {
- short t;
- short i,len,ver;
- char buf[80],bufTemp[3];
-
- SUT_MODEM_AT_MSG *pMsg=&sutAtmPro;
- ver=0;
- for(i=0;i<5;i++){
- t=100;
- ModemSendAT("AT+ICCID\r\n");
- while(t--)
- {//^SCID: 89860316452001903429
- if(g_ucQuitModemInitNow==1) return -1;
-
- if((pMsg->MsgLen = MsgQueueAccept(pMsg->MsgData, sizeof(pMsg->MsgData))))
- {
- if(0==ModemStrCmp((char *)pMsg->MsgData,"^SCID:"))
- {
- memcpy(CCID, &pMsg->MsgData[7], sizeof(CCID));
- CCID[20]=0;
- snprintf(buf, sizeof(buf), "[Minit]ICCID:%s[", CCID);
- bufTemp[0] = CCID[4];
- bufTemp[1] = CCID[5];
- bufTemp[2] = 0;
- switch(atoi(bufTemp))
- {
- case 1:
- case 9:thisYunYingShang = CHINA_UNICOM;
- strcat(buf, "CHINA_UNICOM]");
- break;
- case 0:
- case 2:
- case 4:
- case 7:thisYunYingShang = CHINA_MOBILE;
- strcat(buf, "CHINA_MOBILE]");
- break;
- case 3:
- case 6:
- case 8:
- case 11:thisYunYingShang = CHINA_TELECOM;
- strcat(buf, "CHINA_TELECOM]");
- break;
- default:strcat(buf, "UNKNOW-ICCID]");
- break;
- }
- SlwTrace(INF, buf,1);
- return 1;
- }
- }else os_dly_wait(1);
- }
- }
- SlwTrace(INF, "[Minit]Get CCID failed",1);
- return 0;
- }
- void ColonyTcpStatus(STATUE ipStatus)
- {
- char ipFlag;
- if(ipStatus == OPEN) ipFlag=1;
- else ipFlag=0;
-
- if(ipFlag != sutColony.targetConnectOk) //1
- {
- if(ipFlag==1)//连接上了
- sutColony.targetDataSendTime=0;
- else//突然断开了
- ColonyParaReset();
- sutColony.targetConnectOk=ipFlag;
- }
- }
- static void SendHgsData(void);
- void ColonyTick(void)
- {
- char buf[50];
- static char step=0;
-
- if(sutColony.colonyCtl ==0) return;
- if(sutGpsStatus.PPPStatus != OPEN) return;
- switch(step)
- {
- case 0://获取域名
- if(sutColony.domainGetOk==1) break; //获取成功返回
- SlwTrace(INF, "Colony step1",1);
- if(sutColony.domainSendTick==0){
- memset(buf,0,sizeof(buf));
- snprintf(buf,sizeof(buf),"AT+DOMAIN=%s\r\n",sutProductPara.domain);
- SlwTrace(INF,buf,1);
- ModemSendAT(buf);
- }
-
- if(++sutColony.domainSendTick>=3) sutColony.domainSendTick=0;
- break;
-
- case 1://发起连接
- if(sutColony.domainGetOk==1)
- {
- if(sutColony.targetConnectOk==0){ //连接不成功
- if(++sutColony.targetTryTime >= 3){
- ColonyParaReset();
- sutColony.colonyCtl=0;
- SlwTrace(INF, "Timeout,Quit Colony",1);
- break;
- }
- SlwTrace(INF, "Colony step2",1);
- snprintf(buf, sizeof(buf),"AT+TCPOPEN=%d,%s:%d\r\n",COLONY_SER_SOCKET,sutColony.targetIp,COLONY_SER_PORT);
- ModemSendAT(buf);
- }
- }
- break;
- case 2://检测目标IP是否连接成功
- if(sutColony.domainGetOk==0) break;
- SlwTrace(INF, "Colony step3",1);
- snprintf(buf, sizeof(buf), "AT+TCPSTATUS=%d\r\n", COLONY_SER_SOCKET);
- ModemSendAT(buf);
- break;
- case 3://发数据
- SlwTrace(INF, "Colony step4",1);
- if(sutColony.targetConnectOk==1){
- SendHgsData();
- SlwTrace(INF,"Colony step5",1);
- }
- break;
- }
- if(++step>3) step=0;
- }
- void Colony_RecvHandle(char *data, unsigned short len)
- {
- unsigned short i;
- unsigned char sum,type,zerocnt;
- unsigned short thisLen;
- unsigned char newIP[4];
- unsigned char newGPSIP[4];
- char buf[60];
- char buf1[120];
- char buf2[40];
- char *p=data;
- unsigned char status;
- unsigned char Right,AckStatus;
- unsigned char newCip[4],newPip[4],newGip[4];
- unsigned short newCPort,newPPort,newGPort;
- SUTDS ds;
- unsigned char zeroNum;
- char needSave=0;
- status=0;
-
- if(sutColony.colonyCtl==0) return;
- //check load data
-
- SlwTrace(INF,"show data...........",1);
- PrintBufferData((unsigned char*)data,len);
-
- type = *p++;
- thisLen = *p++;
- thisLen <<= 8;
- thisLen &= 0xff00;
- thisLen |= *p++;
- if((1+2+thisLen) != len)
- {
- snprintf(buf1, sizeof(buf1), "Len err");
- status=1;
- goto EXIT;
- }
-
-
- sum = 0;
- for(i=0;i<thisLen+2;i++) sum += data[i];
- if(sum != data[len -1])
- {
- snprintf(buf1, sizeof(buf1), "Sum err");
- status=2;
- goto EXIT;
- }
-
- if(type != 0x12 && type != 0x19)
- {
- snprintf(buf1, sizeof(buf1), "Type unSupport");
- status=3;
- goto EXIT;
- }
-
- //收到正常的应答包
- if(type == 0x12)
- {
- Right = *p++;
- if(0xEA != Right)
- {
- snprintf(buf1, sizeof(buf1), "Right err");
- status=4;
- goto EXIT;
- }
- memcpy(newIP, data+23, 4);//
- snprintf(buf, sizeof(buf), "NewIP:%d.%d.%d.%d",newIP[0],newIP[1],newIP[2],newIP[3]);
- SlwTrace(INF, buf,1);
- memcpy(newGPSIP,data+27,4);
- snprintf(buf2, sizeof(buf2), "NewGPSIP:%d.%d.%d.%d",newGPSIP[0],newGPSIP[1],newGPSIP[2],newGPSIP[3]);
- SlwTrace(INF, buf2,1);
-
-
- //?????0,???????IP,???????IP??
- zerocnt=0;
- for(i=0;i<4;i++)
- {
- if(newIP[i] == 0) zerocnt++;
- }
- if(zerocnt >= 2)
- {
- SlwTrace(INF, "err_IP",1);
- status=5;
- goto EXIT;
- }
- zerocnt=0;
- for(i=0;i<4;i++)
- {
- if(newGPSIP[i] == 0) zerocnt++;
- }
- if(zerocnt >= 2)
- {
- SlwTrace(INF, "err_GPSIP",1);
- status=6;
- goto EXIT;
- }
-
-
- snprintf(buf2, sizeof(buf2), "%d.%d.%d.%d", newGPSIP[0],newGPSIP[1],newGPSIP[2],newGPSIP[3]);
- if(strcmp(sutProductPara.PIP,buf) != 0){
- strcpy(sutProductPara.GIP,buf2);
- memset(buf2,0,sizeof(buf2));
- snprintf(buf, sizeof(buf), "AT+TCPCLOSE=%d\r\n", GPS_DATA_SOCKET);
- ModemSendAT(buf);
- }
-
- snprintf(buf, sizeof(buf), "%d.%d.%d.%d", newIP[0],newIP[1],newIP[2],newIP[3]);
- if(strcmp(sutProductPara.PIP,buf) != 0)
- {//????IP
- IWDG_ReloadCounter();
- //1?????
- snprintf(sutProductPara.PIP,sizeof(sutProductPara.PIP), "%d.%d.%d.%d", newIP[0],newIP[1],newIP[2],newIP[3]);
- SaveProductParaToFlash("HGS_IP");
- //2??????
- snprintf(buf, sizeof(buf),"id=%lu;ip=%s;pwd=%s;",sutProductPara.PSN,sutProductPara.PIP,sutProductPara.MEID);
- AscStrToHexStr(buf,buf1);
- ModemSendAT("AT+POC=010000");
- ModemSendAT(buf1);
- ModemSendAT("\r\n");
- //reset whole system
- SlwTrace(INF, "Reboot...",1);
- os_dly_wait(50);
- ModemSendAT("AT+POC=040000\r\n");
- // os_dly_wait(100);
- // while(1);
-
- }else
- {
- snprintf(buf1, sizeof(buf1),"Same_IP");
- status=0;
- goto EXIT;
- }
-
- EXIT:
- SlwTrace(INF, buf1,1);
- SlwTrace(INF,"conlony over.........",1);
- //成功拿到一包数据后,认为完成处理了
- ColonyFeature(0);
- }
- }
- void ColonyDomainGet(char *pMsg)
- {//128.4.2.1 后面不能有多的数据了
- unsigned short i,j;
- unsigned char dotNum;
- unsigned char findFlag;
- //1、找到第3个点
- i=0;dotNum=0;findFlag=0;j=0;
- do{
- if(findFlag==1){
- if(pMsg[i] < '0' || pMsg[i] > '9'){
- //有多的数据
- if(strlen(pMsg+i) > 2) return;//不正常的数据包
- if(pMsg[i] =='\r') pMsg[i]=0;
- }
- if(pMsg[i] == '\r')pMsg[i]=0;
- }
- if(pMsg[i] == '.'){
- if(++dotNum == 3) findFlag=1;
- }
- i++;
- }while(pMsg[i] !='\0');
- j=strlen(pMsg);
- if(j<=15){
- memset(sutColony.targetIp, 0, sizeof(sutColony.targetIp));
- strncpy(sutColony.targetIp, pMsg, j);
- sutColony.domainGetOk=1;
- sutColony.targetTryTime=0;
- printf("get targetip:%s\r\n",sutColony.targetIp);
- }
- }
- void ColonyParaReset(void)
- {
- sutColony.domainGetOk=0;
- sutColony.domainSendTick=0;
- sutColony.targetConnectOk=0;;
- sutColony.targetTryTime=0;
- sutColony.targetDataSendTime=0;
- }
- void ColonyFeature(char status)
- {
- if(status >1) return;
- sutColony.colonyCtl=status;
- ColonyParaReset();
- }
- /////////proto parts
- static unsigned short SendTcpDataFill(unsigned char *buffer, unsigned char cmd,unsigned char status,unsigned char value)
- {
- unsigned char *p = buffer;
- unsigned short i,j;
- unsigned char nowCIP[4],nowPIP[4],nowGIP[4];
- unsigned short nowCPort,nowPPort,nowGPort;
- SUTDL dl;
- SUTDS ds;
- i=0;
-
- //PSN
- dl.Data.ulData = sutProductPara.PSN; //4字节
- p[i++] = dl.Data.ucData.b4;
- p[i++] = dl.Data.ucData.b3;
- p[i++] = dl.Data.ucData.b2;
- p[i++] = dl.Data.ucData.b1;
-
- //PASS
- if(cmd == 0x12)
- {
- dl.Data.ulData = 0;
- p[i++] = dl.Data.ucData.b4;
- p[i++] = dl.Data.ucData.b3;
- p[i++] = dl.Data.ucData.b2;
- p[i++] = dl.Data.ucData.b1;
- }
-
- //PName
- for(j=0;j<8;j++) p[i++]= sutProductPara.ProductName[j];
-
- //CurVer
- ds.Data.usData = sutProductPara.ProductVersion;
- p[i++] = ds.Data.ucData.b2;
- p[i++] = ds.Data.ucData.b1;
-
- //CCID
- for(j=0;j<20;j++) p[i++]= CCID[j];
-
- return i;
- }
- static unsigned short SendTcpSeverPack(unsigned char *Buffer,unsigned char CMD,unsigned short Datalen)
- {
- unsigned short j,i=0;
- unsigned char *pBuf=Buffer,sum;
- SUTDS ds;
- pBuf[0]=CMD;
- ds.Data.usData = Datalen+1;
- pBuf[1]=ds.Data.ucData.b2;
- pBuf[2]=ds.Data.ucData.b1;
-
- sum=0;
- for(j=0;j<1+2+Datalen;j++) sum += pBuf[j];
- pBuf[3+Datalen]=sum;
- return (3+Datalen+1);
- }
- static void SendHgsData(void)
- {
- unsigned short len;
- unsigned char HgsBuf[60];
-
- len=SendTcpDataFill(&HgsBuf[3],0x12,0,0);
- len=SendTcpSeverPack(HgsBuf,0x12,len);
-
- M9507CSendTcpData(COLONY_SER_SOCKET, HgsBuf, len);
-
- PrintBufferData(HgsBuf,len);
- }
- //end of proto parts
- /*获取集群GPSIP与PIP*/
- void GetDomainIp(char *pMsg,char type)
- {//128.4.2.1 后面不能有多的数据了
- unsigned short i,j;
- unsigned char dotNum;
- unsigned char findFlag;
- //1、找到第3个点
- unsigned char buf[40];
- i=0;dotNum=0;findFlag=0;j=0;
- do{
- if(findFlag==1){
- if(pMsg[i] < '0' || pMsg[i] > '9'){
- //有多的数据
- if(strlen(pMsg+i) > 2) return;//不正常的数据包 // 2222
-
- if(pMsg[i] =='\r') pMsg[i]=0;
- }
- if(pMsg[i] == '\r')pMsg[i]=0;
- }
- if(pMsg[i] == '.'){
- if(++dotNum == 3) findFlag=1;
- }
- i++;
- }while(pMsg[i] !='\0');
- j=strlen(pMsg);
- if(j<=15)
- {
-
- if(type=='p'){
- if(strcmp(sutProductPara.PIP,pMsg) != 0){
- memset(sutProductPara.PIP, 0, sizeof(sutProductPara.PIP));
- strncpy(sutProductPara.PIP, pMsg, j);
- //strcat(buf,sutProductPara.PIP);
- snprintf(buf,sizeof(buf),"SET newPIP:%s",sutProductPara.PIP);
- SlwTrace(INF,buf,1);
- SaveProductParaToFlash("save new IP");
- //printf("SET newPIP:%s\r\n",sutProductPara.PIP);
- }
- }else if(type=='g'){
- if(strcmp(sutProductPara.GIP,pMsg) != 0)
- {
- memset(sutProductPara.GIP, 0, sizeof(sutProductPara.GIP));
- strncpy(sutProductPara.GIP, pMsg, j);
- snprintf(buf,sizeof(buf),"set new GIP:%s\r\n",sutProductPara.GIP);
- SlwTrace(INF,buf,1);
- snprintf(buf, sizeof(buf), "AT+TCPCLOSE=%d\r\n", GPS_DATA_SOCKET);
- ModemSendAT(buf);
- SaveProductParaToFlash("save new GIP");
- }
- }
- }
- }
- /* IP 查询,网络模式查询,及处于2-4G查询*/
- void AtQuery()
- {
- static unsigned char send_sucCt=0;
-
- switch(send_sucCt){
-
- case 0:
- ModemSendAT("AT+SYSCMD=cat /tmp/group-config\r\n");
- break;
- case 1:
- ModemSendAT("AT+MODODREX?\r\n");
- break;
- case 2:
- ModemSendAT("AT^SYSINFO\r\n");
- break;
- }
- if(++send_sucCt>2) send_sucCt=0;
- }
|