/* * Copyright (c) 2009 by Michal Hucik * * Thies program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * MZ-800 Unicard is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with MZ-800 Unicard; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include #include #include #include #include "../crctab.c" #include #include #include #include #define SOCKET_ERROR -1 /* File attribute bits for directory entry */ #define AM_RDO 0x01 /* Read only */ #define AM_HID 0x02 /* Hidden */ #define AM_SYS 0x04 /* System */ #define AM_VOL 0x08 /* Volume label */ #define AM_LFN 0x0F /* LFN entry */ #define AM_DIR 0x10 /* Directory */ #define AM_ARC 0x20 /* Archive */ #define AM_MASK 0x3F /* Mask of defined bits */ static unsigned char g_tcp_mode = 0; void ReadResult ( int pd, unsigned char *result, unsigned char result_len ) { unsigned char count = 0; unsigned char end_line = 0x00; result_len--; // jeden bajt pro 0x00 na konci while ( 1 ) { read ( pd, result, 1 ); result [1] = '\x00'; if ( g_tcp_mode && count && ( 0x0a == result [0] ) ) { result [0] = '\x00'; return; } else if ( ( ! end_line ) && ( 0x0d == result [0] ) ) { end_line = 0x0d; if ( g_tcp_mode ) { return; }; } else if ( ( 0x0d == end_line ) && ( 0x0a == result [0] ) ) { result--; result [0] = '\x00'; //odstranime smeti z konce radku return; } else { end_line = 0x00; }; if ( ! ( g_tcp_mode && ! count && ( 0x0a == result [0] ) ) ) { result++; result_len--; count++; }; if ( ! result_len ) return; // vic mista uz v bufferu neni }; } void ClosePort ( int pd, struct termios *oldtio ) { if ( ! g_tcp_mode ) { if ( tcsetattr ( pd, TCSANOW, oldtio ) < 0 ) { perror ( "Could not set previous term attributes." ); }; } close( pd ); } void PrintBps ( struct timeb s_time, struct timeb e_time, long datasize ) { printf ( "\n*************************\n" ); printf ( "* Size: %12d B *\n", datasize ); if ( s_time.time != e_time.time ) { double Bps = datasize / ( ( e_time.time + ( e_time.millitm * 0.001 ) ) - ( s_time.time + ( s_time.millitm * 0.001 ) ) ); printf ( "* Speed: %10.2f Bps *\n", Bps ); } else { printf ( "* Speed: HIGH :-) *\n" ); }; printf ( "*************************\n\n" ); } int main ( int argc, char *argv[] ) { struct termios oldtio, newtio; int pd; long baudrate; char r_ip[15], *r_port; struct sockaddr_in saMy, saHis; unsigned char byte; unsigned char result[255]; unsigned char file_cmd[5]; FILE *fd; unsigned char i; unsigned char buff_count; char cmd[20]; char *c; long fsize, datasize; struct timeb s_time, e_time; if ( argc >= 3 ) { // CMD udelame non-cs i = 0; c = argv[2]; while ( c [ i ] != '\x00' ) { cmd [ i ] = tolower ( c [ i ] ); i++; if ( i == sizeof ( cmd ) ) break; }; cmd [ i ] = '\x00'; }; unsigned char arg_err = 0; if ( argc < 4 ) { arg_err = 1; if ( argc == 3 ) { if ( 0 == strcmp ( argv[2], "date" ) ) arg_err = 0; if ( 0 == strcmp ( argv[2], "laninfo" ) ) arg_err = 0; if ( 0 == strcmp ( argv[2], "netstat" ) ) arg_err = 0; if ( 0 == strcmp ( argv[2], "arp" ) ) arg_err = 0; if ( 0 == strcmp ( argv[2], "listen" ) ) arg_err = 0; }; }; if ( ! arg_err ) { if ( 0 == strncmp ( argv[1], "tcp:", 4 ) ) { arg_err = 1; char *c = strchr ( argv[1], ':' ); r_port = strrchr ( argv[1], ':' ); if ( c != r_port ) { c++; if ( ( r_port - c ) <= 16 ) { strncpy ( r_ip, c, ( r_port - c ) ); r_ip [ ( r_port - c ) ] = '\x00'; r_port++; g_tcp_mode = 1; arg_err = 0; }; }; }; }; if ( arg_err ) { printf ( "\n\nUsage:\n\tUshell [] []\n" ); printf ( "\nAvailable commands are: date, fddremount, filelist, readdir, mkdir, unlink, open, new, read, laninfo, netstat, arp, listen.\n\n" ); return ( -1 ); }; if ( g_tcp_mode ) { // inicializace tcp socketu //printf ( "TCP mode - r_ip=%s, r_port=%s\n", r_ip, r_port ); // vytvorime socket typu stream pd = socket ( PF_INET, SOCK_STREAM, IPPROTO_TCP ); if ( pd == SOCKET_ERROR ) { char errmsg[100]; sprintf ( errmsg, "Can't create a socket, err: (%3d)", errno ); perror ( errmsg ); return -1; }; // lokalni adresa saMy.sin_family = AF_INET; saMy.sin_port = htons ( 0 ); // cislo lokalniho portu si nechame pridelit saMy.sin_addr.s_addr = INADDR_ANY; // povesime se na socket if ( bind ( pd , (struct sockaddr*) &saMy, sizeof ( saMy ) ) == SOCKET_ERROR ) { char errmsg[100]; sprintf ( errmsg, "Bind failed (%3d)", errno ); perror ( errmsg ); close ( pd ); return -1; }; // nachystame si IP adresu saHis.sin_family = AF_INET; saHis.sin_port = htons ( atoi ( r_port ) ); saHis.sin_addr.s_addr = inet_addr ( r_ip ); if ( -1 == saHis.sin_addr.s_addr ) { printf ( "Can't obtain remote host IP address!\n" ); close ( pd ); return -1; }; // navazeme spojeni se serverem if ( connect ( pd, (struct sockaddr*) &saHis, sizeof ( saHis ) ) == SOCKET_ERROR ) { perror ( "Cannot connect to the remote host" ); close ( pd ); return -1; }; //ReadResult ( pd, result, sizeof ( result ) ); // \n //printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); // Unicard - TCP Shell printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strcmp ( result, "OK:RESET") != 0 ) { printf ( "Error Ushell->TCP_CONNECT: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; } else { // inicializace RS232 //printf ( "USART mode\n" ); baudrate = B115200; if ( ( pd = open ( argv[1], O_RDWR| O_NOCTTY ) ) < 0 ) { perror ( argv[1] ); return ( -1 ); }; if ( tcgetattr ( pd, &oldtio ) < 0 ) { perror ( "Could not get term attributes." ); return ( -1 ); }; /* 8 bits, baud rate and local control */ bzero ( &newtio, sizeof ( newtio ) ); newtio.c_cflag = CS8 | CLOCAL | baudrate | CREAD | CRTSCTS; newtio.c_lflag = 0; newtio.c_oflag = 0; newtio.c_iflag = IGNPAR; newtio.c_cc [ VMIN ] = 2; newtio.c_cc [ VTIME ] = 10; cfsetospeed ( &newtio, baudrate ); cfsetispeed ( &newtio, baudrate ); tcflush ( pd, TCIFLUSH ); if ( tcsetattr ( pd, TCSANOW, &newtio ) < 0 ) { perror ( "Could not set term attributes." ); return ( -1 ); }; setbuf ( stdout, NULL ); }; // RESET printf ("Sending Reset...\n"); byte = 0x00; write ( pd, &byte, 1 ); ReadResult ( pd, result, sizeof ( result ) ); if ( strcmp ( result, "OK:RESET") != 0 ) { printf ( "Error Ushell->RESET: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n", result ); // dekodovani prikazu if ( strcmp ( cmd, "mkdir" ) == 0 ) { printf ( "Sending MKDIR...\n" ); write ( pd, "MKDIR ", 6 ); write ( pd, argv[3], strlen ( argv[3] ) ); write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); } else if ( strcmp ( cmd, "unlink" ) == 0 ) { printf ( "Sending UNLINK...\n" ); write ( pd, "UNLINK ", 7 ); write ( pd, argv[3], strlen ( argv[3] ) ); write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); } else if ( strcmp ( cmd, "fddremount" ) == 0 ) { printf ( "Sending FDDREMOUNT...\n" ); write ( pd, "FDDREMOUNT ", 11 ); write ( pd, argv[3], 1 ); write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); } else if ( strcmp ( cmd, "filelist" ) == 0 ) { printf ( "Sending FILELIST...\n" ); write ( pd, "FILELIST ", 9 ); write ( pd, argv[3], strlen ( argv[3] ) ); write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->FILELIST: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n\n", result ); while ( 1 ) { ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "ERR:READDIR", 11 ) == 0 ) { printf ( "Error Ushell->FILELIST1: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; // Konec ? if ( strncmp ( result, "OK:FILELIST_DONE", 16 ) == 0 ) break; printf ( "\t%s", result ); int fname_len = strlen ( result ); char directory = result [ fname_len - 1 ]; ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "ERR:READDIR", 11 ) == 0 ) { printf ( "Error Ushell->FILELIST2: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; if ( '/' != directory ) { fname_len = ( 33 - fname_len ) + ( 10 - strlen ( result ) ); while ( fname_len-- ) { printf ( " " ); }; printf ( "%s", result ); }; printf ( "\n" ); }; printf ( "\n\t%s\n", result ); } else if ( strcmp ( cmd, "readdir" ) == 0 ) { printf ( "Sending READDIR...\n" ); write ( pd, "READDIR ", 8 ); write ( pd, argv[3], strlen ( argv[3] ) ); write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->READDIR: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n\n", result ); while ( 1 ) { // FILINFO ma 55 bajtu // my to vsak musime cist po castech //read ( pd, &result, 13 ); // 13 bajtu je nejkratsi odpoved // jakou muzeme dostat "ERR:READDIR\x0d\x0a" for ( i = 0; i < 12; i++ ) read ( pd, &result[i], 1 ); if ( strncmp ( result, "ERR:READDIR", 11 ) == 0 ) { result[i] = '\x00'; printf ( "Error Ushell->FILELIST1: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; // Konec ? if ( strncmp ( result, "OK:READDIR_DONE", 11 ) == 0 ) { result[i] = '\x00'; printf ("\n\t%s", result); break; }; // vyzvedneme zbytek z FILINFO //read ( pd, &result[12], 42 ); for ( i = i; i < 55; i++ ) read ( pd, &result[i], 1 ); // DWORD - 4B, File size // WORD - 2B, Last modified date // WORD - 2B, Last modified time // BYTE - 1B, Attribute // TCHAR - 13B, Short file name (8.3 format), zakonceno 0x00 // BYTE - 1B, LFN strlen // TCHAR - 32B, LFN, zakonceno 0x00 unsigned int fsize = result[0] + ( result[1]<<8 ) + ( result[2]<<16 ) + ( result[3]<<24 ); unsigned short mdate = result[4] + ( result[5]<<8 ); unsigned short mtime = result[6] + ( result[7]<<8 ); unsigned char fattr = result[8]; char *fname = &result[9]; char *lfname = &result[23]; char txtATTR[32]; txtATTR[0] = '\x00'; if ( fattr & AM_VOL ) { strcat ( txtATTR, "VLBL" ); }; if ( fattr & AM_DIR ) { strcat ( txtATTR, "DIR" ); }; if ( fattr & AM_RDO ) { if ( strlen ( txtATTR ) ) strcat ( txtATTR, "," ); strcat (txtATTR, "RO"); }; if ( fattr & AM_HID ) { if ( strlen ( txtATTR ) ) strcat ( txtATTR, "," ); strcat (txtATTR, "HID"); }; if ( fattr & AM_SYS ) { if ( strlen ( txtATTR ) ) strcat ( txtATTR, "," ); strcat (txtATTR, "SYS"); }; if ( fattr & AM_ARC ) { if ( strlen ( txtATTR ) ) strcat ( txtATTR, "," ); strcat (txtATTR, "ARC"); }; if ( strlen ( txtATTR ) ) { printf ( "[%s]", txtATTR ); } else { printf ( " " ); }; if ( 15 > strlen ( txtATTR ) ) { for ( i = 0; i < ( 15 - strlen ( txtATTR ) ); i++ ) { printf ( " " ); }; } else { printf ( " " ); }; char *filename; if ( '\x00' != lfname[0] ) { filename = lfname; } else { filename = fname; }; printf ( "%s", filename ); if ( 32 > strlen ( filename ) ) { for ( i = 0; i < ( 32 - strlen ( filename ) ); i++ ) { printf ( " " ); }; } else { printf ( " " ); }; char fsizeTXT[32]; if ( ! ( fattr & AM_DIR ) ) { if ( 1024 > fsize ) { sprintf ( fsizeTXT, "%12d B ", fsize ); } else if ( ( 1024 * 1024 ) > fsize ) { sprintf ( fsizeTXT, "%7.2f kB ", ( (float) fsize / 1024 ) ); } else { sprintf ( fsizeTXT, "%7.2f MB ", ( (float) fsize / ( 1024 * 1024 ) ) ); }; if ( 18 > strlen ( fsizeTXT ) ) { for ( i = 0; i < ( 18 - strlen ( fsizeTXT ) ); i++ ) { printf ( " " ); }; } else { printf ( " "); }; printf ( "%s", fsizeTXT ); } else { for ( i = 0; i < 18; i++ ) printf ( " " ); }; printf (" %02d-%02d-%d ", ( mdate & 31 ) , ( mdate >> 5 ) & 15 , ( ( mdate >> 9 ) + 1980 ) ); printf ("%02d:%02d:%02d\n", ( mtime >> 11 ), ( mtime >> 5 ) & 63, ( ( mtime & 31 ) << 1 ) ); }; ReadResult ( pd, result, sizeof ( result ) ); printf ( "%s\n", result ); } else if ( ( strcmp ( cmd, "open" ) == 0 ) || ( strcmp ( cmd, "new" ) == 0 ) ) { if ( argc != 5 ) { printf ( "\nError, usage: Ushell \n" ); ClosePort ( pd, &oldtio ); return ( -1 ); }; if( NULL == ( fd = fopen ( argv [3], "rb" ) ) ) { perror ( "Could not open src file" ); ClosePort ( pd, &oldtio ); return ( -1 ); }; if ( strcmp ( cmd, "open" ) == 0 ) { printf ( "Sending Open...\n" ); write ( pd, "OPEN ", 5 ); } else { printf ( "Sending New...\n" ); write ( pd, "NEW ", 4 ); }; write ( pd, argv[4], strlen ( argv[4] ) ); write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->OPEN/NEW: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n", result ); fseek ( fd, 0, SEEK_END ); fsize = ftell ( fd ); datasize = fsize; fseek ( fd, 0, SEEK_SET ); printf ( "Sending FSIZE: 0x%08lx\n", fsize ); for ( i = 0; i < 4; i++ ) { byte = (unsigned char) ( fsize >> ( i * 8 ) ) & 0xff; write ( pd, &byte, 1 ); }; ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); printf("Sending Data"); buff_count = 0x80; // velikost bufferu v unikarte unsigned long filecrc = 0; ftime ( &s_time ); while ( 1 ) { byte = fgetc ( fd ); write ( pd, &byte, 1); filecrc = UPDC32 ( byte, filecrc ); buff_count--; fsize--; if ( ! buff_count ) { buff_count = 0x80; printf ( "." ); }; if ( ! fsize ) break; }; ftime ( &e_time ); fclose ( fd ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:CLOSE", 8 ) != 0 ) { printf ( "\nError Ushell->CLOSE: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; char filecrcTXT[20]; sprintf ( filecrcTXT, "0x%08x", filecrc ); if ( 0 != strncmp ( filecrcTXT, &result[9], 10 ) ) { printf ( "\nError CRC32->FAILED: pc=%s, unicard=%s\n", filecrcTXT, &result[9] ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\n\t%s\n", result ); PrintBps ( s_time, e_time, datasize ); } else if ( strcmp ( cmd, "read" ) == 0 ) { if ( argc != 5 ) { printf ( "\nError, usage: Ushell read \n" ); ClosePort ( pd, &oldtio ); return ( -1 ); }; if( NULL == ( fd = fopen ( argv [4], "wb" ) ) ) { perror ( "Could not open dst file" ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "Sending Read...\n" ); write ( pd, "READ ", 5 ); write ( pd, argv[3], strlen ( argv[3] ) ); write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->READ: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n", result ); fsize = 0; for ( i = 0; i < 4; i++ ) { read ( pd, &byte, 1 ); fsize += (long) ( byte << ( i * 8) ); }; datasize = fsize; printf ( "Receiving FSIZE: 0x%08lx\n", fsize ); printf( "Receiving Data" ); unsigned long filecrc = 0; ftime ( &s_time ); void *rdata = malloc ( fsize ); char *c = rdata; buff_count = 0x80; // velikost bufferu v unikarte unsigned int recbuf = 0; while ( fsize-- ) { read ( pd, c, 1 ); filecrc = UPDC32 ( ( (int) *c) & 0xff , filecrc ); c++; buff_count--; if ( ! buff_count ) { buff_count = 0x80; printf ( "." ); }; }; ftime ( &e_time ); while ( fsize++ < ( datasize - 1 ) ) { fputc ( * (unsigned char*) rdata++, fd ); }; fclose ( fd ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:CLOSE", 8 ) != 0 ) { printf ( "\nError Ushell->CLOSE: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; char filecrcTXT[20]; sprintf ( filecrcTXT, "0x%08x", filecrc ); if ( 0 != strncmp ( filecrcTXT, &result[9], 10 ) ) { printf ( "\nError CRC32->FAILED: pc=%s, unicard=%s\n", filecrcTXT, &result[9] ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\n\t%s\n", result ); PrintBps ( s_time, e_time, datasize ); } else if ( strcmp ( cmd, "date" ) == 0 ) { if ( ! ( ( argc == 3 ) || ( argc == 5 ) ) ) { printf ( "\nError, usage: Ushell date\n" ); printf ( " Ushell date YYYY-MM-DD HH:MM:SS\n" ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "Sending DATE...\n" ); write ( pd, "DATE", 4 ); if ( argc == 5 ) { write ( pd, " ", 1 ); write ( pd, argv[3], strlen ( argv[3] ) ); write ( pd, " ", 1 ); write ( pd, argv[4], strlen ( argv[4] ) ); }; write ( pd, "\x0d", 1 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); } else if ( strcmp ( cmd, "laninfo" ) == 0 ) { printf ( "Sending LANINFO...\n" ); write ( pd, "LANINFO\x0d", 8 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->LANINFO: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n\n", result ); while ( 1 ) { ReadResult ( pd, result, sizeof ( result ) ); // Konec ? if ( strncmp ( result, "OK:LANINFO_DONE", 15 ) == 0 ) break; char *val = strchr ( result, ' '); val [ 0 ] = '\x00'; val++; printf ( "\t%s", result ); int i = strlen ( result ); for ( i; i < 20; i++ ) printf ( " " ); printf ( "%s", val ); printf ( "\n" ); }; printf ( "\n\t%s\n", result ); } else if ( strcmp ( cmd, "netstat" ) == 0 ) { printf ( "Sending NETSTAT...\n" ); write ( pd, "NETSTAT\x0d", 8 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->NETSTAT: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n\n", result ); while ( 1 ) { ReadResult ( pd, result, sizeof ( result ) ); // Konec ? if ( strncmp ( result, "OK:NETSTAT_DONE", 15 ) == 0 ) break; printf ( "\t%s\n", result ); }; printf ( "\n\t%s\n", result ); } else if ( strcmp ( cmd, "arp" ) == 0 ) { printf ( "Sending ARP...\n" ); write ( pd, "ARP\x0d", 8 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->ARP: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n\n", result ); while ( 1 ) { ReadResult ( pd, result, sizeof ( result ) ); // Konec ? if ( strncmp ( result, "OK:ARP_DONE", 11 ) == 0 ) break; printf ( "\t%s\n", result ); }; printf ( "\n\t%s\n", result ); } else if ( strcmp ( cmd, "listen" ) == 0 ) { printf ( "Sending LISTEN...\n" ); write ( pd, "LISTEN\x0d", 8 ); ReadResult ( pd, result, sizeof ( result ) ); printf ( "\t%s\n", result ); ReadResult ( pd, result, sizeof ( result ) ); if ( strncmp ( result, "OK:", 3 ) != 0 ) { printf ( "Error Ushell->LISTEN: %s\n", result ); ClosePort ( pd, &oldtio ); return ( -1 ); }; printf ( "\t%s\n\n", result ); while ( 1 ) { ReadResult ( pd, result, sizeof ( result ) ); // Konec ? if ( strncmp ( result, "OK:LISTEN_DONE", 14 ) == 0 ) break; printf ( "\t%s\n", result ); }; printf ( "\n\t%s\n", result ); } else { printf ( "\nError UNKNOWN COMMAND: %s\n", cmd ); ClosePort ( pd, &oldtio ); return ( -1 ); }; ClosePort ( pd, &oldtio ); return ( 0 ); };