uint8_t WaspGPRS::sendDataFTP(char* file, char* path, uint8_t id) { char command[100]; char aux='"'; long previous=0; uint8_t answer=0; uint8_t end=0; uint32_t i,j=0; sprintf(command,"AT%s%c,,%c%c,%c%s%c,0%c%c", AT_FTP_SEND, id, aux, aux, aux, file, aux, '\r', '\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("CONNECT",20,0,0); if(answer!=1) return 0; serialFlush(PORT_USED); Utils.strExplode(path,'/'); while( path[i]!='\0' ) { if( path[i]== '/' ) j++; i++; } i=0; SD.ON(); while( j>0 ) { if(!SD.cd(Utils.arguments[i])){ SD.OFF(); return 0; } i++; j--; } i=0; j=0; while( !end ) { printString(SD.cat(file,250*i,250),PORT_USED); while( SD.buffer[j]!='\0' ) j++; if( j<249 ) end=1; i++; j=0; } printString(GPRS_PATTERN,PORT_USED); SD.OFF(); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("OK",20,0,0); if(answer!=1) return 0; return 1; }
byte WaspGPRS::sendCommand(char* theText, char* endOfCommand, char* expectedAnswer, int MAX_TIMEOUT, int sendOnce) { int timeout = 0; for (int i = 0; i < 100; i++) received[i] = ' '; int length=sprintf(theCommand, "%s%s", theText,endOfCommand); // try sending the command // wait for serial response timeout = 0; serialFlush(PORT_USED); while(!serialAvailable(PORT_USED) && timeout < MAX_TIMEOUT) { if (!sendOnce || !timeout) { printString(theCommand,PORT_USED); USB.print('T'); } delay(DELAY_ON_SEND); timeout++; }; int answer= waitForData( expectedAnswer, MAX_TIMEOUT, timeout, 0); return answer; }
/* uploadFile() - uploads a file to a FTP server * * This function uploads a file to a FTP server * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::downloadFile(char* file, char* path, char* user, char* passw, char* ftp_server, uint8_t ftp_port) { char command[70]; long previous=0; uint8_t answer=0; char aux='"'; uint16_t index=0; uint16_t i=0; uint8_t end=0; char* aux2; uint8_t j=0; uint8_t id=0; if(!setFlowControl()) return 0; if(!configureGPRS()) return 0; // Connect to FTP Server sprintf(command,"AT%s,%c%s%c,%c%s%c,%c%s%c,%u,1%c%c",AT_FTP_PARAM,aux,ftp_server,aux,aux,user,aux,aux,passw,aux,ftp_port,'\r','\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("+KFTPCFG: ",20,0,0); if(answer!=1) return 0; id=serialRead(PORT_USED); if( !readDataFTP(file, path, id) ) return 0; return 1; }
int receiveThing(T& data, vector<int> sockets) { int s; if (!waitForData(sockets, s)) return false; return receiveThing(data,s); }
/* readURL(url) - access to the specified URL and stores the info read in 'data_URL' variable * * This function access to the specified URL and stores the info read in 'data_URL' variable * * It modifies 'flag' if expected answer is not received after sending a command to GPRS module * * It stores in 'socket_ID' the TCP session ID assigned to the last call to create a socket * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::readURL(const char* url) { char command[30]; char* data=",20"; uint8_t answer=0; long previous=0; uint8_t byteIN=0; uint8_t a=0; if(!configureGPRS()) return 0; if(!createSocket(url,"80",GPRS_CLIENT)) return 0; serialFlush(PORT_USED); sprintf(command,"%s%c%c","GET / HTTP/1.0",'\r','\n'); if(!sendData(command,socket_ID)){ closeSocket(socket_ID); return 0; } waitForData("+KTCP_DATA:",20,0,0); if(readData(socket_ID,GPRS_DATA_LENGTH)<0){ closeSocket(socket_ID); return 0; } while(!closeSocket(socket_ID)) closeSocket(socket_ID); return 1; }
static jint receiveBytes(SharedMemoryConnection *connection, void *bytes, jint length) { Stream *stream = &connection->incoming; SharedStream *shared = stream->shared; jint fragmentStart; jint fragmentLength; jint index = 0; jint maxLength; CHECK_ERROR(enterMutex(stream, connection->shutdown)); while (index < length) { CHECK_ERROR(waitForData(connection, stream)); SHMEM_ASSERT(!EMPTY(stream)); fragmentStart = shared->readOffset; if (fragmentStart < shared->writeOffset) { maxLength = shared->writeOffset - fragmentStart; } else { maxLength = SHARED_BUFFER_SIZE - fragmentStart; } fragmentLength = MIN(maxLength, length - index); memcpy((jbyte *)bytes + index, shared->buffer + fragmentStart, fragmentLength); shared->readOffset = ADD_OFFSET(fragmentStart, fragmentLength); index += fragmentLength; shared->isFull = JNI_FALSE; STREAM_INVARIANT(stream); CHECK_ERROR(signalSpace(stream)); } CHECK_ERROR(leaveMutex(stream)); return SYS_OK; }
void GraphRetargetingTask::execute() { // 1. Clone the graph. RCP<AnimationGraph> graph = _graph->clone(); // 2. Retarget each animation in each node. // Fetch all animations contained in the graph. Vector< SkeletalAnimation* > anims; _graph->getAnimations( anims ); // Retarget all animation. Vector< RCP< Resource<SkeletalAnimation> > > animsRes (anims.size()); for( uint i = 0; i < anims.size(); ++i ) { animsRes[i] = ResManager::retarget( anims[i], _skel.ptr(), this ); } waitForAll(); // Replace all animations. Map< SkeletalAnimation*, RCP<SkeletalAnimation> > ranims; for( uint i = 0; i < anims.size(); ++i ) { ranims[anims[i]] = waitForData( animsRes[i].ptr() ); } graph->replaceAnimations( ranims ); _res->data( graph.ptr() ); }
/* sendData(data,socket) - sends 'data' to the specified 'socket' * * This function sends 'data' to the specified 'socket' * * It modifies 'flag' if expected answer is not received after sending a command to GPRS module * * It gets from 'socket_ID' the TCP session ID assigned to the last call of creating a socket * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::sendData(const char* data, uint8_t* socket) { char command[30]; uint8_t answer=0; long previous=0; uint8_t byteIN=0; uint8_t counter=0; uint8_t i=0; while(data[counter]!='\0') counter++; counter+=2; while(socket[i]!='\r') i++; counter+=i-1; serialFlush(PORT_USED); switch(i) { case 1: sprintf(command,"%s%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],counter,'\r','\n'); break; case 2: sprintf(command,"%s%c%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],socket[1],counter,'\r','\n'); break; case 3: sprintf(command,"%s%c%c%c,%u%c%c",AT_GPRS_TCP_SND,socket[0],socket[1],socket[2],counter,'\r','\n'); break; } printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); delay(10); answer=waitForData("CONNECT",20,0,0); if(answer!=1) return 0; answer=0; counter=0; while( (counter<3) && (answer!=1) ) { serialFlush(PORT_USED); delay(20); writeData(data); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); delay(10); answer=waitForData("OK",20,0,0); counter++; } if(answer!=1) return 0; return 1; }
/* sendMail() - sends an email * * This function sends an email * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::sendMail(char* from, char* to, char* subject, char* body, char* user, char* passw, char* smtp_server, uint16_t port) { uint8_t counter=0; char command[30]; long previous=0; uint8_t answer=0; if(!setFlowControl()) return 0; if(!configureGPRS()) return 0; if(!setEmailParams(smtp_server, port, from)) return 0; if(!setEmailPwd(user, passw)) return 0; if(!setEmailDestination(to)) return 0; if(!setEmailSubject(subject)) return 0; while( body[counter]!='\0' ) counter++; counter+=2; serialFlush(PORT_USED); sprintf(command,"AT%s1,%u%c%c",AT_SMTP_SEND,counter,'\r','\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("CONNECT",20,0,0); if(answer!=1) return 0; printString(body,PORT_USED); printByte('\r',PORT_USED); printByte('\n',PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("OK",20,0,0); if(answer!=1) return 0; return 1; }
size_t Serial::read(char* data, size_t length) { waitForData(); logger.debug(MODULE, "Reading data"); ssize_t br = ::read(fd, data, length); logger.debug(MODULE, "Read %u bytes", br); if (br == -1) { throw SerialException("Could not read data"); } return br; }
/* check(void) - Checks if GPRS is connected to the network * * This function checks if GPRS module is connected to the network. If not, it has no sense working with GPRS. * * It sends a command to GPRS module DEFAULT_TIMEOUT times. If GPRS module does not connect within these tries, function * exits. * * Returns '1' when connected and '0' if not */ uint8_t WaspGPRS::check() { char byte; uint8_t timeout=1;///DEFAULT_TIMEOUT; char command[11]; uint8_t answer=0; while(timeout) { sprintf(command,"%s%c%c","AT+CREG?",'\r','\n'); serialFlush(PORT_USED); printString(command,PORT_USED); answer=waitForData("+CREG: 0,1",1,0,0); switch(answer){ case 0: break; case 1: connected = 1; return 1; break; case 2: break; } // delay(1000); USB.print('x'); printString(command,PORT_USED); answer=waitForData("+CREG: 0,5",1,0,0); switch(answer){ case 0: break; case 1: connected = 1; return 1; break; case 2: break; } delay(100); timeout--; } connected = 0; return 0; }
/* getIfReady() - gets if GPRS module is ready or not * * This function gets if GPRS module is ready or not * * It modifies 'flag' if expected answer is not received after sending a command to GPRS module * * Returns nothing. It changes the value of 'not_ready' */ void WaspGPRS::getIfReady() { char command[20]; char aux='"'; uint8_t answer=0; long previous=0; printString(AT_COMMAND,PORT_USED); printByte('\r',PORT_USED); printByte('\n',PORT_USED); /* while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) );*/ delay(10); answer=waitForData("OK",2,0,0); if(answer==1) not_ready=0; else not_ready=1; }
int64 Socket::readImpl(char *buffer, int64 length) { ASSERT(m_fd); ASSERT(length > 0); IOStatusHelper io("socket::recv", m_address.c_str(), m_port); int recvFlags = 0; if (m_timeout > 0) { int flags = fcntl(m_fd, F_GETFL, 0); if ((flags & O_NONBLOCK) == 0) { if (!waitForData()) { return 0; } recvFlags = MSG_DONTWAIT; // polled, so no need to wait any more } } int64 ret = recv(m_fd, buffer, length, recvFlags); return (ret < 0) ? 0 : ret; }
//------------------------------------------------------------------------------ //! void ProceduralAnimation::execute() { _anim = new SkeletalAnimation(); // Prepare to build the animation. VMState* vm = VM::open( VM_CAT_ANIM | VM_CAT_MATH, true ); VM::userData( vm, this ); VM::doFile( vm, _animPath, 0 ); VM::close( vm ); // Load skeleton. RCP< Resource<Skeleton> > skelRes = ResManager::getSkeleton( _skelId, this ); _anim->skeleton( waitForData( skelRes.ptr() ) ); _res->data( _anim.ptr() ); //StdErr << "Animation: " << _animPath << " rate=" << _anim->rate() << " dur=" << _anim->duration() << nl; }
jint shmemBase_receiveByte(SharedMemoryConnection *connection, jbyte *data) { Stream *stream = &connection->incoming; SharedStream *shared = stream->shared; int offset; CHECK_ERROR(enterMutex(stream, connection->shutdown)); CHECK_ERROR(waitForData(connection, stream)); SHMEM_ASSERT(!EMPTY(stream)); offset = shared->readOffset; *data = shared->buffer[offset]; shared->readOffset = ADD_OFFSET(offset, 1); shared->isFull = JNI_FALSE; STREAM_INVARIANT(stream); CHECK_ERROR(leaveMutex(stream)); CHECK_ERROR(signalSpace(stream)); return SYS_OK; }
void ClientConnection::handleRead( const boost::system::error_code& error ) { if ( error == 0) { // invoke a thread to handle the message boost::thread worker( &ClientConnection::handleReadInThread, this, message ); // back to listen waitForData(); } else { // if an error occurs, close the connection std::stringstream stream; stream << "ClientConnection (" << technicalId << ") > handleRead call with error code: " << error.value() << " --> " << error.message(); AsyncLogger::getInstance()->log( stream.str() ); connectionManager->closeConnection( shared_from_this() ); } }
int64_t Socket::readImpl(char *buffer, int64_t length) { assert(m_fd); assert(length > 0); IOStatusHelper io("socket::recv", m_address.c_str(), m_port); int recvFlags = 0; if (m_timeout > 0) { int flags = fcntl(m_fd, F_GETFL, 0); if ((flags & O_NONBLOCK) == 0) { if (!waitForData()) { m_eof = true; return 0; } recvFlags = MSG_DONTWAIT; // polled, so no need to wait any more } } int64_t ret = recv(m_fd, buffer, length, recvFlags); if (ret == 0 || (ret == -1 && errno != EWOULDBLOCK)) { m_eof = true; } return (ret < 0) ? 0 : ret; }
int64_t SSLSocket::readImpl(char *buffer, int64_t length) { int64_t nr_bytes = 0; if (m_data->m_ssl_active) { IOStatusHelper io("sslsocket::recv", getAddress().c_str(), getPort()); bool retry = true; do { if (m_data->m_is_blocked) { waitForData(); if (timedOut()) { break; } // could get here and we only have parts of an SSL packet } nr_bytes = SSL_read(m_data->m_handle, buffer, length); if (nr_bytes > 0) break; /* we got the data */ retry = handleError(nr_bytes, false); setEof(!retry && errno != EAGAIN && !SSL_pending(m_data->m_handle)); } while (retry); } else { nr_bytes = Socket::readImpl(buffer, length); } return nr_bytes < 0 ? 0 : nr_bytes; }
QVariant RedisConnectionOverSsh::execute(QString command) { if (command.isEmpty()) { return QVariant(); } /* * Send command */ QByteArray byteArray = Command::getByteRepresentation(command); const char* cString = byteArray.constData(); socket->write(cString, byteArray.size()); //wait for ready read syncTimer->start(config.executeTimeout); syncLoop->exec(); if (!syncTimer->isActive()) { return QVariant(); } /* * Get response */ Response response; QByteArray availableData; int currExecutionTime = 0; bool dataReaded = false; while(!response.isValid()) { availableData = socket->read(MAX_BUFFER_SIZE); if (availableData.size() > 0) { response.appendToSource(availableData); } else { while (currExecutionTime <= config.executeTimeout) { waitForData(5); currExecutionTime +=5; availableData = socket->read(MAX_BUFFER_SIZE); if (availableData.size() > 0) { response.appendToSource(availableData); currExecutionTime = 0; dataReaded = true; break; } } if (dataReaded) { dataReaded = false; continue; } break; } } return response.getValue(); }
char * requestUrlfromPlugin(int to_plugin, uintptr_t plugin_instance, const char *url) { size_t len = 0, ulen = 0, bytes = 0; urlRequest request; FILE *infile; int linecount; int linelen; char buf[2004]; char encodedUrl[2000]; ppPluginSocket p = (ppPluginSocket)gglobal()->PluginSocket.prv; LOCK_PLUGIN_COMMUNICATION /* encode the url - if it has funny characters (eg, spaces) asciify them in accordance to some HTML web standard */ URLencod(encodedUrl,url,2000); #ifdef PLUGINSOCKETVERBOSE pluginprint ("NEW REQUEST\n",url); pluginprint ("requestURL fromPlugin, getting %s\n",url); pluginprint (" ... encoded is %s\n",encodedUrl); #endif request.instance = (void *) plugin_instance; request.notifyCode = 0; /* get a file */ len = FILENAME_MAX * sizeof(char); memset(request.url, 0, len); memset(p->return_url, 0, len); ulen = strlen(encodedUrl) + 1; memmove(request.url, encodedUrl, ulen); bytes = sizeof(urlRequest); #ifdef PLUGINSOCKETVERBOSE pluginprint ("requestURL fromPlugin, step 1\n",""); pluginprint ("sending url request to socket %d\n",to_plugin); #endif if (write(to_plugin, (urlRequest *) &request, bytes) < 0) { #ifdef PLUGINSOCKETVERBOSE pluginprint ("write failed in requestUrlfromPlugin",""); #endif return NULL; } #ifdef PLUGINSOCKETVERBOSE pluginprint ("requestURL fromPlugin, step 2\n",""); #endif /* wait around for a bit to see if this is going to pass or fail */ if (!waitForData(to_plugin)) { request.notifyCode = -99; /* destroy stream */ if (write(to_plugin, (urlRequest *) &request, bytes) < 0) { #ifdef PLUGINSOCKETVERBOSE pluginprint ("write failed in requestUrlfromPlugin",""); #endif UNLOCK_PLUGIN_COMMUNICATION return NULL; }
//------------------------------------------------------------------------------ //! void ProceduralWorld::execute() { RCP<World> world( new World() ); // Create working context for this vm. WorldContext context( this ); context._world = world.ptr(); context._ref = Reff::identity(); context.curDir( ResManager::dir(_id) ); // Open vm. VMState* vm = VM::open( VM_CAT_WORLD | VM_CAT_MATH, true ); // keep context pointer into vm. VM::userData( vm, &context ); // Push parameters. if( _params.isValid() ) { VM::push( vm, *_params ); // Execute script. VM::doFile( vm, _path, 1, 0 ); } else { // Execute script. VM::doFile( vm, _path, 0 ); } // Wait for all auxilliary task (loading resource) to finish. waitForAll(); // Assign all resources. // Geometry. for( size_t i = 0; i < context._geomRes.size(); ++i ) { WorldContext::GeomEntityPair& p = context._geomRes[i]; p.first->geometry( waitForData( p.second ) ); } // MaterialSet. for( size_t i = 0; i < context._matRes.size(); ++i ) { WorldContext::MatEntityPair& p = context._matRes[i]; p.first->materialSet( waitForData( p.second ) ); } // BrainProgram. for( size_t i = 0; i < context._brainRes.size(); ++i ) { WorldContext::BrainProgPair& p = context._brainRes[i]; p.first->program( waitForData( p.second ) ); } // Animation graph. for( size_t i = 0; i < context._graphRes.size(); ++i ) { WorldContext::SkelGraphPair& p = context._graphRes[i]; // Retarget animation graph to skeleton. if( p.first->skeleton() ) { RCP< Resource<AnimationGraph> > graph = ResManager::retarget( waitForData( p.second ), p.first->skeleton(), this ); context.keepResource( graph.ptr() ); p.second = graph.ptr(); } } // Wait for all auxilliary task (loading resource) to finish. waitForAll(); // At this point, the skeletal entity's animation graph has been retargetted, and the brains are set. for( size_t i = 0; i < context._graphRes.size(); ++i ) { WorldContext::SkelGraphPair& p = context._graphRes[i]; SkeletalEntity* skel = p.first; Brain* brain = skel->brain(); AnimationGraph* graph = waitForData( p.second ); if( brain == NULL ) { StdErr << "Attempted to set an animation graph (" << (void*)graph << ") on a brainless skeletal entity (" << (void*)skel << "); ignoring." << nl; continue; } // Connect the graph into the skeletal entity through a puppeteer action. PuppeteerAction* action = (PuppeteerAction*)brain->findAction( PuppeteerAction::actionType() ); if( action == NULL ) { action = new PuppeteerAction(); brain->actions().pushBack( action ); } action->entity( skel ); action->graph( graph ); } // Guarantee a material group in every entity. uint n = world->numEntities(); for( uint i = 0; i < n; ++i ) { Entity* e = world->entity( i ); if( e->materialSet() == nullptr ) { MaterialSet* ms = new MaterialSet(); ms->add( Material::white() ); e->materialSet( ms ); } } VM::close( vm ); _res->data( world.ptr() ); }
/* createSocket(ip, port) - creates a TCP/IP connection to the specified IP and PORT * * This function creates a TCP/IP connection to the specified IP and PORT * * It modifies 'flag' if expected answer is not received after sending a command to GPRS module * * It stores in 'socket_ID' the TCP session ID assigned to the last call to create a socket * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::createSocket(const char* ip,const char* port, uint8_t mode) { char command[40]; char aux='"'; uint8_t answer=0; uint8_t byteIN=0; long previous=0; uint8_t counter=0; uint8_t i=0; flag &= ~(GPRS_ERROR_SOCKET); switch(mode){ case GPRS_CLIENT: sprintf(command,"%s0,%u,%c%s%c,%s%c%c",AT_GPRS_TCP_CFG,mode,aux,ip,aux,port,'\r','\n'); break; case GPRS_SERVER: sprintf(command,"%s0,%u,%s%c%c",AT_GPRS_TCP_CFG,mode,port,'\r','\n'); break; } while( counter<3 ) { serialFlush(PORT_USED); printString(command,PORT_USED); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); delay(10); answer=waitForData("+KTCPCFG:",20,0,0); if(answer==1) break; counter++; } if(answer!=1) return 0; serialRead(PORT_USED); delay(30); i=0; while(i<4){ socket_ID[i]=0; i++; } i=0; while( socket_ID[i]!='\r' && i<4 && serialAvailable(PORT_USED) ){ socket_ID[i]=serialRead(PORT_USED); i++; } delay(50); answer=0; counter=0; while( counter<3 ) { serialFlush(PORT_USED); printString(AT_GPRS_TCP_CNX,PORT_USED); i=0; while( socket_ID[i]!='\r' ){ printByte(socket_ID[i],PORT_USED); i++; } printByte('\r',PORT_USED); printByte('\n',PORT_USED); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<3000) ); delay(10); answer=waitForData("OK",20,0,10); if(answer==1) break; counter++; } if(answer!=1) return 0; return 1; }
void wait_for_data(lirc_t timeout) { waitForData(timeout); }
/* readData(socket,data_length) - reads data of 'data_length' size from socket ID 'socket' * * This function reads data of 'data_length' size from socket ID 'socket' * * It modifies 'flag' if expected answer is not received after sending a command to GPRS module * * It stores in 'socket_ID' the TCP session ID assigned to the last call to create a socket * * Returns '1' on success, '-1' if error and '0' if more data is available */ int8_t WaspGPRS::readData(uint8_t* socket, const char* data_length) { char command[30]; uint8_t answer=0; long previous=0; uint8_t a=0; uint8_t byteIN=0; uint8_t aux[10]; uint8_t i=0; while(socket[i]!='\r') i++; switch(i) { case 1: sprintf(command,"%s%c,%s%c%c",AT_GPRS_TCP_RCV,socket[0],data_length,'\r','\n'); break; case 2: sprintf(command,"%s%c%c,%s%c%c",AT_GPRS_TCP_RCV,socket[0],socket[1],data_length,'\r','\n'); break; case 3: sprintf(command,"%s%c%c%c,%s%c%c",AT_GPRS_TCP_RCV,socket[0],socket[1],socket[2],data_length,'\r','\n'); break; } serialFlush(PORT_USED); delay(50); printString(command,PORT_USED); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("CONNECT",20,0,0); if(answer!=1) return -1; serialRead(PORT_USED); serialRead(PORT_USED); while( a<GPRS_DATA_LENGTH_U && serialAvailable(PORT_USED) ) { aux[0]=serialRead(PORT_USED); if(aux[0]=='E'){ aux[1]=serialRead(PORT_USED); if(aux[1]=='N'){ aux[2]=serialRead(PORT_USED); if(aux[2]=='D'){ aux[3]=serialRead(PORT_USED); if(aux[3]=='M'){ aux[4]=serialRead(PORT_USED); if(aux[4]=='E'){ aux[5]=serialRead(PORT_USED); if(aux[5]=='S'){ answer=1; break; } else{ data_URL[a]=aux[0]; data_URL[a+1]=aux[1]; data_URL[a+2]=aux[2]; data_URL[a+3]=aux[3]; data_URL[a+4]=aux[4]; data_URL[a+5]=aux[5]; a+=6; } } else{ data_URL[a]=aux[0]; data_URL[a+1]=aux[1]; data_URL[a+2]=aux[2]; data_URL[a+3]=aux[3]; data_URL[a+4]=aux[4]; a+=5; } } else{ data_URL[a]=aux[0]; data_URL[a+1]=aux[1]; data_URL[a+2]=aux[2]; data_URL[a+3]=aux[3]; a+=4; } } else{ data_URL[a]=aux[0]; data_URL[a+1]=aux[1]; data_URL[a+2]=aux[2]; a+=3; } } else{ data_URL[a]=aux[0]; data_URL[a+1]=aux[1]; a+=2; } } else{ data_URL[a]=aux[0]; a++; } } data_read+=a; if(answer && a<(GPRS_DATA_LENGTH_U-10)) return 1; if(answer && a>=(GPRS_DATA_LENGTH_U-10)) return 0; return -1; }
/* readMail() - reads an email * * This function reads an email * * Returns '1' on success and '0' if error */ uint8_t WaspGPRS::readMail(char* user, char* passw, char* pop3_server, uint16_t port) { char command[70]; long previous=0; uint8_t answer=0; char aux='"'; uint16_t index=0; uint16_t i=0; uint8_t end=0; char* aux2; uint8_t j=0; if(!setFlowControl()) return 0; if(!configureGPRS()) return 0; // Connect to POP3 Server sprintf(command,"AT%s%c%s%c,%u,%c%s%c,%c%s%c%c%c",AT_POP3_PARAM,aux,pop3_server,aux,port,aux,user,aux,aux,passw,aux,'\r','\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("OK",20,0,0); if(answer!=1) return 0; // Get the index printString(AT_POP3_LIST,PORT_USED); printByte('\r',PORT_USED); printByte('\n',PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("+KPOPLIST:",20,0,0); if(answer!=1) return 0; i=0; while( serialRead(PORT_USED)!=':' ); serialRead(PORT_USED); index=serialRead(PORT_USED); serialFlush(PORT_USED); // Read the email printString(AT_POP3_READ,PORT_USED); printByte(index,PORT_USED); printByte('\r',PORT_USED); printByte('\n',PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); aux2 = (char*) calloc(MAX_SIZE_POP3,sizeof(char)); if( aux2==NULL ) return 0; previous=millis(); i=0; while( millis()-previous<5000 ) { while( serialAvailable(PORT_USED) && i<MAX_SIZE_POP3 ) { aux2[i]=serialRead(PORT_USED); i++; previous=millis(); } } if( i>=MAX_SIZE_POP3 ) aux2[i-1]='\0'; else aux2[i]='\0'; i=0; j=0; i=waitForData(aux2,"Return-path: <"); emailAddress[j]=aux2[i]; while( emailAddress[j]!='>' && i!=0 ) { i++; j++; emailAddress[j]=aux2[i]; if(j>=30) break; } emailAddress[j]='\0'; i=0; j=0; i=waitForData(aux2,"ubject: "); subject[j]=aux2[i]; while( subject[j]!='\r' && i!=0 ) { i++; j++; subject[j]=aux2[i]; if(j>=30) break; } subject[j]='\0'; i=0; j=0; i=waitForData(aux2,"*/*"); while( j<=GPRS_MAX_DATA && !end && i!=0 ) { body[j]=aux2[i]; if( body[j]=='*' ) { j++; i++; body[j]=aux2[i]; if( body[j]=='/' ) { j++; i++; body[j]=aux2[i]; if( body[j]=='*' ) { j++; i++; body[j]='\0'; end=1; } } } j++; i++; } body[j-1]='\0'; i=0; free(aux2); aux2=NULL; return 1; }
uint8_t WaspGPRS::readDataFTP(char* file, char* path, uint8_t id) { char command[50]; char aux='"'; long previous=0; uint8_t answer=0; uint8_t end=0; int i,j=0; uint8_t timeout=0; uint8_t MAX_TIMEOUT=10; char* aux2; uint16_t length=0; sprintf(command,"AT%s%c,,%c%c,%c%s%c,0%c%c", AT_FTP_RECV, id, aux, aux, aux, file, aux, '\r', '\n'); printString(command,PORT_USED); previous=millis(); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); delay(10); answer=waitForData("CONNECT",30,0,0); if(answer!=1){ free(aux2); aux2=NULL; return 0; } delay(20); while( (!serialAvailable(PORT_USED)) && ((millis()-previous)<10000) ); aux2 = (char*) calloc(MAX_SIZE_FTP,sizeof(char)); if( aux2==NULL ) return 0; serialRead(PORT_USED); serialRead(PORT_USED); previous=millis(); i=0; while( millis()-previous<5000 ) { while( serialAvailable(PORT_USED) && i<MAX_SIZE_FTP ) { aux2[i]=serialRead(PORT_USED); i++; previous=millis(); } } if( i>=MAX_SIZE_FTP ) aux2[i-1]='\0'; else aux2[i]='\0'; length=waitForData(aux2,GPRS_PATTERN); if( !length ) length=i; else { j=Utils.sizeOf(GPRS_PATTERN); length-=j; } i=0; j=0; Utils.strExplode(path,'/'); while( path[i]!='\0' ) { if( path[i]== '/' ) j++; i++; } i=0; SD.ON(); while( j>0 ) { if(!SD.cd(Utils.arguments[i])){ SD.OFF(); free(aux2); aux2=NULL; return 0; } i++; j--; } SD.create(Utils.arguments[i]); if(!SD.append(Utils.arguments[i],aux2,length)){ SD.OFF(); free(aux2); aux2=NULL; return 0; } SD.ls(); SD.OFF(); free(aux2); aux2=NULL; return 1; }