📄 gprs.c
字号:
if (!BLUE_READLINE(com, 3, 1000L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (strEqualsIgnoreCase(resp, RESULT_ERROR_VERBOSE)) { // An error here likely means that the TCP/IP stack has not been previously // started with the command "AT+WOPEN=1". Normally, this should not occur // since "AT+WOPEN=1" should be automatically called each time the GPRS // comport is openned. Possibly, a new modem was installed since the comport // was openned (a highly unlikely occurance, but possible), so the best we // can hope for at this time is to re-attempt to initialize the startup // commands. blueStartupReset = utTrue; return utFalse; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } //logDEBUG(LOGSRC,"[GPRS2] %s", resp); } comPortFlush(com, 100L); /* APN server [ AT#APNSERV="" ] */ // Expect "OK" | "ERROR" comPortWriteATFmt(com, "#APNSERV=\"%s\"", apnServ); while (utTrue) { if (!BLUE_READLINE(com, 1, 1500L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* APN [ AT#APNUN="" ] */ // Expect "OK" | "ERROR" comPortWriteATFmt(com, "#APNUN=\"%s\"", apnUser); while (utTrue) { if (!BLUE_READLINE(com, 1, 1500L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* APN password [ AT#APNPW="" ] */ // Expect "OK" | "ERROR" comPortWriteATFmt(com, "#APNPW=\"%s\"", apnPass); while (utTrue) { if (!BLUE_READLINE(com, 1, 1500L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* clear DNS? */ const char *dns_1 = blueGetDNS(1); const char *dns_2 = blueGetDNS(2); if ((!dns_1 || !*dns_1) && (!dns_2 || !*dns_2)) { logDEBUG(LOGSRC,"Clearing DNS: %s, %s", DNS_NULL, DNS_NULL); if (!_blueSetDNS(DNS_NULL, DNS_NULL)) { return utFalse; } } return utTrue;}// ----------------------------------------------------------------------------/* return true if modem is open */utBool blueIsOpen(){ return comPortIsOpen(&blueComPort);}/* open modem serial port */utBool blueOpenCom(){ ComPort_t *com = &blueComPort; comPortInitStruct(com); /* port config */ const char *portName = propGetString(PROP_CFG_XPORT_PORT, "0"); UInt32 portBPS = propGetUInt32(PROP_CFG_XPORT_BPS, 0L); if (portBPS == 0L) { portBPS = MODEM_SPEED_BPS; } /* open port */ if (comPortOpen(com,portName,portBPS,"8N1",utFalse) == (ComPort_t*)0) { if (utcIsTimerExpired(blueLastComErrorTimer,900L)) { blueLastComErrorTimer = utcGetTimer(); logCRITICAL(LOGSRC,"Unable to open GPRS port: %s", portName); //, strerror(errno)); } threadSleepMS(1000L); return utFalse; } /* comport logging? */ if (propGetBoolean(PROP_CFG_XPORT_DEBUG,utFalse)) { comPortSetDebugLogger(com, 0); } /* reset */ blueConnectionReset = utTrue; /* return success */ threadSleepMS(500L); // wait return utTrue; }void blueCloseCom(){ ComPort_t *com = &blueComPort; if (comPortIsOpen(com)) { _blueStopConnection(); comPortClose(com); }}// ----------------------------------------------------------------------------int blueConnect(){ /* open port? */ if (!blueIsOpen() && !blueOpenCom()) { // unable to open port (error already printed) return -1; } /* set DTR/RTS true */ ComPort_t *com = &blueComPort; comPortSetDTR(com, utTrue); comPortSetRTS(com, utTrue); /* flush anything currently in the buffers */ comPortFlush(com, 0L); /* init & precheck connectability */ if (!_blueInitModem()) { return 0; } /* get connected ip address */ char *ip = _blueStartConnection(); if (!ip) { // no ip address, still unable to connect return 0; } logINFO(LOGSRC,"IP Address: %s\n", ip); return 1;}// ----------------------------------------------------------------------------/* write string (open socket assumed) */int blueWrite(const char *buf, int bufLen){ ComPort_t *com = &blueComPort; if (bufLen > 0) { return comPortWrite(com, (UInt8*)buf, bufLen); } return bufLen;}/* write string (open socket assumed) */int blueWriteString(const char *msg){ ComPort_t *com = &blueComPort; int msgLen = strlen(msg); if (msgLen > 0) { logDEBUG(LOGSRC,"Sending: %s", msg); comPortWriteString(com, msg); if (msg[msgLen - 1] != '\r') { comPortWrite(com, (UInt8*)"\r", 1); msgLen++; } } return msgLen;}/* write formatted string (open socket assumed) */int blueWriteStringFmt(const char *fmt, ...){ char out[512]; va_list ap; va_start(ap, fmt); vsprintf(out, fmt, ap); va_end(ap); return blueWriteString(out);}// ----------------------------------------------------------------------------static char readBuffer[PACKET_MAX_ENCODED_LENGTH] = { 0 };static int readLen = 0;static int readPtr = 0;/* read bytes (open socket assumed) */int blueRead(char *resp, int respLen, long timeoutMS){ /* refill buffer */ if ((readLen <= 0) || (readPtr >= readLen)) { //logINFO(LOGSRC,"Filling String buffer ..."); readLen = blueReadString(readBuffer, sizeof(readBuffer) - 1, timeoutMS); if (readLen < 0) { logINFO(LOGSRC,"Read error ..."); return -1; } else if (strEqualsIgnoreCase(readBuffer, RESULT_SOCKET_CLOSED)) { blueTCPSocketOpen = utFalse; readLen = 0; return -1; } readBuffer[readLen++] = '\r'; // re-add line terminator readBuffer[readLen] = 0; // terminate readPtr = 0; //logINFO(LOGSRC,"Read: %s", readBuffer); } /* return requested data */ int maxLen = respLen; if (maxLen > (readLen - readPtr)) { maxLen = readLen - readPtr; } memcpy(resp, &readBuffer[readPtr], maxLen); readPtr += maxLen; return maxLen;}/* read string (open socket assumed) */int blueReadString(char *resp, int respLen, long timeoutMS){ //ComPort_t *com = &blueComPort; char buff[PACKET_MAX_ENCODED_LENGTH]; /* no space to hold data */ if (respLen <= 0) { return 0; } /* already have data in the read buffer? */ if (readPtr < readLen) { int maxLen = respLen; if (maxLen > (readLen - readPtr)) { maxLen = readLen - readPtr; } memcpy(resp, &readBuffer[readPtr], maxLen); readPtr += maxLen; if (resp[maxLen - 1] == '\r') { maxLen--; } resp[maxLen] = 0; return maxLen; } /* read response from server */ int R = (timeoutMS < 0L)? 2 : 1; long T = labs(timeoutMS) / (long)R; if (!BLUE_READLINE(com, R, T, buff, sizeof(buff))) { // socket may still be open return -1; } if (strEqualsIgnoreCase(buff, RESULT_SOCKET_CLOSED)) { blueTCPSocketOpen = utFalse; return -1; } //logDEBUG(LOGSRC,"Response: %s", buff); /* return response */ int buffLen = strlen(buff); if (buffLen < respLen) { strcpy(resp, buff); } else { buffLen = respLen - 1; // truncate strncpy(resp, buff, buffLen); resp[buffLen] = 0; } return buffLen;}// ----------------------------------------------------------------------------/* write datagram (open connection assumed) */utBool blueSendDatagram(char *buf, int bufLen){ ComPort_t *com = &blueComPort; char resp[600]; const char *server = blueGetDataHost(); int port = blueGetDataPort(); logDEBUG(LOGSRC,"Openning UDP socket [%s:%d]", server, port); /* set DNS */ const char *dns_1 = blueGetDNS(1); const char *dns_2 = blueGetDNS(2); if ((dns_1 && *dns_1) || (dns_2 && *dns_2)) { logDEBUG(LOGSRC,"Setting DNS: %s, %s", dns_1, dns_2); if (!_blueSetDNS(dns_1, dns_2)) { return utFalse; } } /* UDP server [ AT#UDPSERV="<host>" ] */ // Expect "OK" comPortWriteATFmt(com, "#UDPSERV=\"%s\"", server); while (utTrue) { if (!BLUE_READLINE(com, 1, 1000L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* UDP port [ AT#UDPPORT=<port> ] */ // Expect "OK" comPortWriteATFmt(com, "#UDPPORT=%d", port); while (utTrue) { if (!BLUE_READLINE(com, 1, 1000L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* open [ AT#OUDP ] */ // Expect "Ok_Info_WaitingForData" comPortWriteAT(com, "#OUDP"); while (utTrue) { // long timeout (since this is a UDP conection, this should occur quickly) if (!BLUE_READLINE(com, 6, 20000L, resp, sizeof(resp))) { return utFalse; } //if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { return utFalse; } // <-- should not get this if (strEqualsIgnoreCase(resp, RESULT_UDP_ACTIVATION)) { break; } // success if (strEqualsIgnoreCase(resp, RESULT_SOCKET_CLOSED)) { return utFalse; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { if (strEqualsIgnoreCase(resp, RESULT_INACTV_PHYS_LAYER)) { // TCP stack not initialized? // if we get here we need to close the current connection and start over. blueConnectionReset = utTrue; } else if (strEqualsIgnoreCase(resp, RESULT_DNS_LOOKUP_FAILED)) { // DNS failed to locate our server // if we get here we need to close the current connection and start over. blueConnectionReset = utTrue; } return utFalse; } } comPortFlush(com, 100L); /* write data */ int writeLen = blueWrite(buf, bufLen); logDEBUG(LOGSRC,"Datagram written [%d]", writeLen); /* close socket */ blueCloseSocket(); /* datagram written */ return utTrue;}// ----------------------------------------------------------------------------utBool bluePing(const char *server){ ComPort_t *com = &blueComPort; char resp[128]; int pingDelay = 1; int pingNum = 5; /* Ping delay [ AT#PINGDELAY=1 ] */ // Expect "OK comPortWriteATFmt(com, "#PINGDELAY=%d", pingDelay); while (utTrue) { if (!BLUE_READLINE(com, 1, 1000L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* Ping delay [ AT#PINGNUM=5 ] */ // Expect "OK comPortWriteATFmt(com, "#PINGNUM=%d", pingNum); while (utTrue) { if (!BLUE_READLINE(com, 1, 1000L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* Ping delay [ AT#PINGREMOTE=server ] */ // Expect "OK comPortWriteATFmt(com, "#PINGREMOTE=\"%s\"", server); while (utTrue) { if (!BLUE_READLINE(com, 1, 1000L, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } } comPortFlush(com, 100L); /* Ping delay [ AT#PING ] */ // Expect "OK comPortWriteAT(com, "#PING"); while (utTrue) { Int32 tmout = pingDelay * 1000L; if (!BLUE_READLINE(com, 3, tmout, resp, sizeof(resp))) { return utFalse; } if (strEqualsIgnoreCase(resp, RESULT_OK_VERBOSE)) { break; } if (ERROR_CHECK(resp) || CME_ERROR_CHECK(resp)) { return utFalse; } logINFO(LOGSRC,"Ping: %s\n", resp); } comPortFlush(com, 100L); return utTrue;}// ----------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -