esp8266 websocket and socket

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers modem.cpp Source File

modem.cpp

00001 /*
00002   modem.cpp
00003   2014 Copyright (c) Seeed Technology Inc.  All right reserved.
00004 
00005   Author:lawliet zou(lawliet.zou@gmail.com)
00006   2014-2-24
00007 
00008   This library is free software; you can redistribute it and/or
00009   modify it under the terms of the GNU Lesser General Public
00010   License as published by the Free Software Foundation; either
00011   version 2.1 of the License, or (at your option) any later version.
00012 
00013   This library is distributed in the hope that it will be useful,
00014   but WITHOUT ANY WARRANTY; without even the implied warranty of
00015   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016   Lesser General Public License for more details.
00017 
00018   You should have received a copy of the GNU Lesser General Public
00019   License along with this library; if not, write to the Free Software
00020   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00021 */
00022 
00023 #include "modem.h"
00024 
00025 char Modem::readByte(void)
00026 {
00027     return serialModem.getc();
00028 }
00029 
00030 bool Modem::readable()
00031 {
00032     return serialModem.readable();
00033 }
00034 
00035 int Modem::readBuffer(char *buffer,int count, unsigned int timeOut)
00036 {
00037     int i = 0;
00038     timeCnt.start();
00039     while(1) {
00040         while (serialModem.readable()) {
00041             char c = serialModem.getc();
00042             buffer[i++] = c;
00043             if(i >= count)break;
00044         }
00045         if(i >= count)break;
00046         if(timeCnt.read() > timeOut) {
00047             timeCnt.stop();
00048             timeCnt.reset();
00049             break;
00050         }
00051     }
00052     return 0;
00053 }
00054 
00055 void Modem::cleanBuffer(char *buffer, int count)
00056 {
00057     for(int i=0; i < count; i++) {
00058         buffer[i] = '\0';
00059     }
00060 }
00061 
00062 void Modem::sendCmd(const char* cmd)
00063 {
00064     serialModem.puts(cmd);
00065 }
00066 
00067 void Modem::sendATTest(void)
00068 {
00069     sendCmdAndWaitForResp("AT\r\n","OK",DEFAULT_TIMEOUT,CMD);
00070 }
00071 
00072 bool Modem::respCmp(const char *resp, unsigned int len, unsigned int timeout)
00073 {
00074     int sum=0;
00075     timeCnt.start();
00076 
00077     while(1) {
00078         if(serialModem.readable()) {
00079             char c = serialModem.getc();
00080             sum = (c==resp[sum]) ? sum+1 : 0;
00081             if(sum == len)break;
00082         }
00083         if(timeCnt.read() > timeout) {
00084             timeCnt.stop();
00085             timeCnt.reset();
00086             return false;
00087         }
00088     }
00089     timeCnt.stop();
00090     timeCnt.reset();
00091 
00092     return true;
00093 }
00094 
00095 int Modem::waitForResp(const char *resp, unsigned int timeout,DataType type)
00096 {
00097     int len = strlen(resp);
00098     int sum=0;
00099     timeCnt.start();
00100 
00101     while(1) {
00102         if(serialModem.readable()) {
00103             char c = serialModem.getc();
00104             sum = (c==resp[sum]) ? sum+1 : 0;
00105             if(sum == len)break;
00106         }
00107         if(timeCnt.read() > timeout) {
00108             timeCnt.stop();
00109             timeCnt.reset();
00110             return -1;
00111         }
00112     }
00113     timeCnt.stop();
00114     timeCnt.reset();
00115 
00116     if(type == CMD) {
00117         while(serialModem.readable()) {
00118             char c = serialModem.getc();
00119         }
00120     }
00121 
00122     return 0;
00123 }
00124 
00125 int Modem::sendCmdAndWaitForResp(const char* data, const char *resp, unsigned timeout,DataType type)
00126 {
00127     sendCmd(data);
00128     return waitForResp(resp,timeout,type);
00129 }