int FTPRxFlush(TCP_SOCKET FTPtoflush, int timeout) { int counter = 0, counter2 = 0; char res[50]; int len; // Waiting for the first characters while (FTPRxLen(FTPtoflush) == 0) { vTaskDelay(1); counter++; if (counter == timeout) return 1; // No chars to flush } counter = 0; while (FTPRxLen(FTPtoflush)) { // The size of dummy array res is 50 char, so len is resized, in case there are more chars in buffer len = FTPRxLen(FTPtoflush); if (len > 49) len = 49; FTPRead( FTPtoflush, res, len); res[len] = 0; debug(res); // Waiting for new characters on FTP buffer, the timeout is inserted as parameter by the user while (FTPRxLen(FTPtoflush) == 0 ) { counter++; vTaskDelay(1); if (counter == timeout) { // Timeout is reached, but a check on the last char is // performed, since it must be '\r' or '\n' if ( (res[len-1]=='\r') || (res[len-1]=='\n') ) break; else { counter2++; if (counter2 == 3) break; counter = 0; } } } } return 0; // FTP buffer flushed }
int FTPRxRead(TCP_SOCKET FTPtoflush, int timeout, char *buffer, int totLen) { int counter = 0, counter2 = 0; int len, count = 0; // Waiting for the first characters while (FTPRxLen(FTPtoflush) == 0) { vTaskDelay(1); counter++; if (counter == timeout) return 1; // No chars to read } counter = 0; while (FTPRxLen(FTPtoflush)) { // The size of dummy array res is 50 char, so len is resized, in case there are more chars in buffer len = FTPRxLen(FTPtoflush); if (len > totLen) len = totLen; FTPRead( FTPtoflush, &buffer[count], len); count += len; // Waiting for new characters on FTP buffer, the timeout is inserted as parameter by the user while (FTPRxLen(FTPtoflush) == 0 ) { counter++; vTaskDelay(1); if (counter == timeout) { // Timeout is reached, but a check on the last char is // performed, since it must be '\r' or '\n' if ( (buffer[count-1]=='\r') || (buffer[count-1]=='\n') ) break; else { counter2++; if (counter2 == 3) break; counter = 0; } } } } return 0; // FTP buffer read }
int FTPAnswer(TCP_SOCKET FTPtoread, char* res_code) { int ind = 0; int counter = 0; // Waiting fore at least three chars answer... while (FTPRxLen(FTPtoread) < 3) { vTaskDelay(10); counter++; if (counter == 50) return 1; // Timeout } while ( (FTPRxLen(FTPtoread)) && (ind < 3) ) { FTPRead( FTPtoread, (res_code + ind), 1); ind++; } return 0; }
long FTPStreamRead(char dest[], int len, BYTE timeout) { long count = 0; int toRead; DWORD tick1, tick2; if ( (FTPisConn(dataSocket)) && (streamStat == FTP_STREAM_READING) ) { tick1 = TickGetDiv64K(); while (count < len) { toRead = FTPRxLen(dataSocket); // Resizing bytes to read according to buffer size if (toRead > (len - count)) toRead = len - count; FTPRead(dataSocket, &dest[count], toRead); count += toRead; streamRBytes += toRead; // No data to read, checking timeout and chars read if (toRead == 0) { // check on file ending, if file is not finished, checking timeout if (streamRBytes == streamLen) { debug("FTPStreamRead: EOF reached\n"); streamStat = FTP_STREAM_EOF; return count; } tick2 = TickGetDiv64K(); IOPut(o4, toggle); if ( (tick2 -tick1) > timeout) { // Timeout occured during reading, a check on data connection is required if (!FTPisConn(dataSocket)) { debug("FTPStreamRead: timeout by server disconnection\n"); errHandling(&dataSocket); streamStat = FTP_STREAM_NOT_CONN; return count; } else { debug("FTPStreamRead: timeout\n"); return count; } } } /* else { sprintf(dbg, "count:%d\n", count); UARTWrite(1, dbg); sprintf(dbg, "toRead:%d\n", toRead); UARTWrite(1, dbg); } */ } return count; } else if (!FTPisConn(dataSocket)) return FTP_DATA_NO_CONNECTED; else return FTP_STREAM_INVALID_OP; }
/** * Open a PassiveMode data exchange between Flyport (Client) and the ServerName * \param sockpasv - The handle of the socket for commands exchange. * \param ServerName - The server data IP returned by the FTP Server * \return The TCP_SOCKET to use for data transfer in Passive Mode. */ TCP_SOCKET FTPClientPasv(TCP_SOCKET sockpasv, char ServerName[]) { unsigned long int dataport=0; char *word; char word1[10]; char buffertot[100]; char servReply[30]; int counter = 0; FTPRxFlush(sockpasv, 100); BYTE strpasv[]={"pasv\r\n"}; FTPMultiWrite(sockpasv,strpasv,6); while (FTPRxLen(sockpasv) == 0) { vTaskDelay(10); counter++; if (counter == 50) break; } counter = 0; int indsum=0; while (FTPRxLen(sockpasv)>0) { int toread = FTPRxLen(sockpasv); FTPRead(sockpasv , (char *) (buffertot+indsum) , toread); indsum += toread; while (FTPRxLen(sockpasv) == 0) { vTaskDelay(10); counter++; if (counter == 20) break; } } word = strtok(buffertot,",()"); int indsub=1; while (word != NULL) { word = strtok(NULL, ",()"); if (indsub == 1) { sprintf(servReply, "%s", word); strcat(servReply, "."); } if (indsub == 2) { strcat(servReply, word); strcat(servReply, "."); } else if (indsub == 3) { strcat(servReply, word); strcat(servReply, "."); } else if (indsub == 4) { strcat(servReply, word); } else if (indsub == 5) { dataport=atoi(word); dataport=dataport*256; int xy; xy=sprintf(word1,"%li",dataport); } else if (indsub == 6) { dataport += atoi(word); sprintf(word1,"%li",dataport); } indsub++; } xFrontEndStatRet = 2; ToSend=20; sprintf(ServerName, "%s", servReply); return TCPGenericOpen(ServerName, TCP_OPEN_RAM_HOST , word1 , 4); }
long FTPStreamRead(char dest[], int len, BYTE timeout) { long count = 0, count2 = 0; int toRead; DWORD tick1, tick2; if (streamStat == FTP_STREAM_READING) { tick1 = TickGetDiv64K(); while (count < len) { count2 = 0; toRead = FTPRxLen(dataSocket); while ((toRead < len) && (toRead + streamRBytes < streamLen)) { toRead = FTPRxLen(dataSocket); count2++; if (count2 == 3) break; } // Resizing bytes to read according to buffer size if (toRead > (len - count)) toRead = len - count; FTPRead(dataSocket, &dest[count], toRead); count += toRead; streamRBytes += toRead; // No data to read, checking timeout and chars read if (toRead == 0) { // check on file ending, if file is not finished, checking timeout if (streamRBytes == streamLen) { _dbgwrite("FTPStreamRead: EOF reached\r\n"); streamStat = FTP_STREAM_EOF; return count; } tick2 = TickGetDiv64K(); if ( (tick2 -tick1) > timeout) { // Timeout occured during reading, a check on data connection is required if (!FTPisConn(dataSocket)) { _dbgwrite("FTPStreamRead: timeout by server disconnection\r\n"); errHandling(&dataSocket); streamStat = FTP_STREAM_NOT_CONN; return count; } else { _dbgwrite("FTPStreamRead: timeout\r\n"); return count; } } } } return count; } else if (!FTPisConn(dataSocket)) { errHandling(&dataSocket); return FTP_DATA_NO_CONNECTED; } else return FTP_STREAM_INVALID_OP; }