/*--------------------------------------------------------------------------------------------------------------------- Tests the network connection to the drone by fetching the drone version number through the FTP server embedded on the drone. This is how FreeFlight checks if a drone sofware update is required. The FTP connection process is a quick and (very)dirty one. It uses FTP passive mode. ---------------------------------------------------------------------------------------------------------------------*/ int test_drone_connection() { vp_com_socket_t ftp_client; static Write ftp_write = NULL; static Read ftp_read = NULL; int timeout_windows = 1000; /*milliseconds*/ /* Connects to the FTP server */ wifi_config_socket(&ftp_client,VP_COM_CLIENT,FTP_PORT,WIFI_ARDRONE_IP); ftp_client.protocol = VP_COM_TCP; if(VP_FAILED(vp_com_init(wifi_com()))) return -1; if(VP_FAILED(vp_com_open(wifi_com(), &ftp_client, &ftp_read, &ftp_write))) return -2; setsockopt((int32_t)ftp_client.priv, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout_windows, sizeof(timeout_windows) ); /* Clean up */ vp_com_close(wifi_com(), &ftp_client); return 0; }
int test_drone_connection() { const char * passivdeModeHeader = "\r\n227 PASV ok ("; vp_com_socket_t ftp_client,ftp_client2; char buffer[1024]; static Write ftp_write = NULL; static Read ftp_read = NULL; int bytes_to_send,received_bytes; int i,L,x[6],port; int timeout_windows = 1000; /*milliseconds*/ vp_os_memset(buffer,0,sizeof(buffer)); wifi_config_socket(&ftp_client,VP_COM_CLIENT,FTP_PORT,WIFI_ARDRONE_IP); ftp_client.protocol = VP_COM_TCP; if(VP_FAILED(vp_com_init(wifi_com()))) return -1; if(VP_FAILED(vp_com_open(wifi_com(), &ftp_client, &ftp_read, &ftp_write))) return -2; setsockopt((int32_t)ftp_client.priv, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout_windows, sizeof(timeout_windows) ); bytes_to_send = _snprintf(buffer,sizeof(buffer),"%s", "USER anonymous\r\nCWD /\r\nPWD\r\nTYPE A\r\nPASV\r\nRETR version.txt\r\n"); ftp_write(&ftp_client,buffer,&bytes_to_send); Sleep(1000); received_bytes = sizeof(buffer); ftp_read(&ftp_client,buffer,&received_bytes); if (received_bytes<1) { vp_com_close(wifi_com(), &ftp_client); return -3; } L=received_bytes-strlen(passivdeModeHeader); for (i=0; i<L; i++) { if (strncmp((buffer+i),passivdeModeHeader,strlen(passivdeModeHeader))==0) break; } if (i==L) { vp_com_close(wifi_com(), &ftp_client); return -4; } i+=strlen(passivdeModeHeader); if (sscanf(buffer+i,"%i,%i,%i,%i,%i,%i)",&x[0],&x[1],&x[2],&x[3],&x[4],&x[5])!=6) { vp_com_close(wifi_com(), &ftp_client); return -5; } port=(x[4]<<8)+x[5]; wifi_config_socket(&ftp_client2,VP_COM_CLIENT,port,"192.168.1.1"); ftp_client2.protocol = VP_COM_TCP; if(VP_FAILED(vp_com_init(wifi_com()))) { vp_com_close(wifi_com(), &ftp_client2); return -6; } if(VP_FAILED(vp_com_open(wifi_com(), &ftp_client2, &ftp_read, &ftp_write))) { vp_com_close(wifi_com(), &ftp_client2); return -7; } received_bytes = sizeof(buffer); ftp_read(&ftp_client2,buffer,&received_bytes); if (received_bytes>0) { buffer[min(received_bytes,sizeof(buffer)-1)]=0; printf("无人机版本 %s 被检测到 ... 按下 <Enter> 开始应用.\n",buffer); getchar(); } vp_com_close(wifi_com(), &ftp_client); vp_com_close(wifi_com(), &ftp_client2); return 0; }
/*--------------------------------------------------------------------------------------------------------------------- Tests the network connection to the drone by fetching the drone version number through the FTP server embedded on the drone. This is how FreeFlight checks if a drone sofware update is required. The FTP connection process is a quick and (very)dirty one. It uses FTP passive mode. ---------------------------------------------------------------------------------------------------------------------*/ int test_drone_connection() { const char * passivdeModeHeader = "\r\n227 PASV ok ("; vp_com_socket_t ftp_client,ftp_client2; char buffer[1024]; static Write ftp_write = NULL; static Read ftp_read = NULL; int bytes_to_send,received_bytes; int i,L,x[6],port; int timeout_windows = 1000; /*milliseconds*/ vp_os_memset(buffer,0,sizeof(buffer)); /* Connects to the FTP server */ wifi_config_socket(&ftp_client,VP_COM_CLIENT,FTP_PORT,WIFI_ARDRONE_IP); ftp_client.protocol = VP_COM_TCP; if(VP_FAILED(vp_com_init(wifi_com()))) return -1; if(VP_FAILED(vp_com_open(wifi_com(), &ftp_client, &ftp_read, &ftp_write))) return -2; setsockopt((int32_t)ftp_client.priv, SOL_SOCKET, SO_RCVTIMEO, (const char*)&timeout_windows, sizeof(timeout_windows) ); /* Request version file */ bytes_to_send = _snprintf(buffer,sizeof(buffer),"%s", "USER anonymous\r\nCWD /\r\nPWD\r\nTYPE A\r\nPASV\r\nRETR version.txt\r\n"); ftp_write(&ftp_client,buffer,&bytes_to_send); /* Dirty. We should wait for data to arrive with some kind of synchronization or make the socket blocking.*/ Sleep(1000); /* Gets the data port */ received_bytes = sizeof(buffer); ftp_read(&ftp_client,buffer,&received_bytes); if (received_bytes<1) { vp_com_close(wifi_com(), &ftp_client); return -3; } L=received_bytes-strlen(passivdeModeHeader); /* Searches for the passive mode acknowlegment from the FTP server */ for (i=0;i<L;i++) { if (strncmp((buffer+i),passivdeModeHeader,strlen(passivdeModeHeader))==0) break; } if (i==L) { vp_com_close(wifi_com(), &ftp_client); return -4; } i+=strlen(passivdeModeHeader); if (sscanf(buffer+i,"%i,%i,%i,%i,%i,%i)",&x[0],&x[1],&x[2],&x[3],&x[4],&x[5])!=6) { vp_com_close(wifi_com(), &ftp_client); return -5; } port=(x[4]<<8)+x[5]; /* Connects to the FTP server data port */ wifi_config_socket(&ftp_client2,VP_COM_CLIENT,port,"192.168.1.1"); ftp_client2.protocol = VP_COM_TCP; if(VP_FAILED(vp_com_init(wifi_com()))) { vp_com_close(wifi_com(), &ftp_client2); return -6; } if(VP_FAILED(vp_com_open(wifi_com(), &ftp_client2, &ftp_read, &ftp_write))) { vp_com_close(wifi_com(), &ftp_client2); return -7; } /* Gets the data */ received_bytes = sizeof(buffer); ftp_read(&ftp_client2,buffer,&received_bytes); if (received_bytes>0) { buffer[min(received_bytes,sizeof(buffer)-1)]=0; printf("Drone version %s detected ... press <Enter> to start the application.\n",buffer); getchar(); } /* Clean up */ vp_com_close(wifi_com(), &ftp_client); vp_com_close(wifi_com(), &ftp_client2); return 0; }