/* system-dependent communications i/o routines for embedded Kermit. */ /* Adapted to VxWorks from Unix version, by Airvana, Inc., Chelmsford MA (USA) http://www.airvananet.com, included with EK by permission, Oct 2002. */ /* The sample i/o routines for VxWorks that provide packet i/o functions on the console (login) device. Copy this file, rename it appropriately, and replace the contents of each routine appropriately for your platform. Device i/o: int devopen() Communications device - open int pktmode() Communications device - enter/exit packet mode int readpkt() Communications device - read a packet int tx_data() Communications device - send data int devclose() Communications device - close int inchk() Communications device - check if bytes are ready to read File i/o: int openfile() File - open for input or output ULONG fileinfo() Get input file modtime and size int readfile() Input file - read data int writefile() Output file - write data int closefile() Input or output file - close Full definitions below, prototypes in kermit.h. These routines must handle speed setting, parity, flow control, and similar items without the kermit() routines knowing anything about it. If parity is in effect, these routines must add it to outbound characters and strip it from inbound characters. */ #include "vxWorks.h" #include "iolib.h" #include "stdio.h" #include "stat.h" #include "time.h" #include "errno.h" #include "kermit.h" extern int consoleFd; extern char consoleName[20]; #ifdef DEBUG extern int debug; extern FILE * dp; #endif /* DEBUG */ UCHAR o_buf[OBUFLEN]; /* File output buffer */ static int ttyfd = -1, ifile = -1, ofile = -1; /* File descriptors */ static int din = -1, dout = -1, derr = -1; /* global io */ static int devoptions = 0; /* D E V O P E N -- Open communications device */ /* Call with: string pointer to device name. This routine should get the current device settings and save them so devclose() can restore them. It should open the device. If the device is a serial port, devopen() set the speed, stop bits, flow control, etc. Returns: 0 on failure, 1 on success. */ int devopen(char *device) { ttyfd = consoleFd; #if 0 /* shut down console as global io */ din = ioGlobalStdGet(STD_IN); dout = ioGlobalStdGet(STD_OUT); derr = ioGlobalStdGet(STD_ERR); if (din == consoleFd) ioGlobalStdSet(STD_IN, -1); if (dout == consoleFd) ioGlobalStdSet(STD_OUT, -1); if (derr == consoleFd) ioGlobalStdSet(STD_ERR, -1); consoleFd = -1; #endif /* save options */ devoptions = ioctl(ttyfd, FIOGETOPTIONS, 0); /* flush */ ioctl(ttyfd, FIOFLUSH, 0); /* set to raw mode */ (void)ioctl(ttyfd, FIOSETOPTIONS, OPT_RAW); return(1); } /* D E V C L O S E -- Closes the current open communications device */ /* Call with: nothing Closes the device and puts it back the way it was found by devopen(). Returns: 0 on failure, 1 on success. */ int devclose(void) { #if 0 /* flush */ ioctl(ttyfd, FIOFLUSH, 0); #endif /* restore options */ (void)ioctl(ttyfd, FIOSETOPTIONS, devoptions); #if 0 /* restore console as global io */ consoleFd = ttyfd; if (din != -1) { ioGlobalStdSet(STD_IN, din); din = -1; } if (dout != -1) { ioGlobalStdSet(STD_OUT, dout); dout = -1; } if (derr != -1) { ioGlobalStdSet(STD_ERR, derr); derr = -1; } #endif ttyfd = -1; return(1); } /* P K T M O D E -- Put communications device into or out of packet mode */ /* Call with: 0 to put in normal (cooked) mode, 1 to put in packet (raw) mode. For a "dumb i/o device" like an i/o port that does not have a login attached to it, this routine can usually be a no-op. Returns: 0 on failure, 1 on success. */ int pktmode(short on) { if (ttyfd < 0) /* Device must be open */ return(0); return(1); } /* I N C H K -- Check if input waiting */ /* Check if input is waiting to be read, needed for sliding windows. This sample version simply looks in the stdin buffer (which is not portable even among different Unixes). If your platform does not provide a way to look at the device input buffer without blocking and without actually reading from it, make this routine return -1. On success, returns the numbers of characters waiting to be read, i.e. that can be safely read without blocking. */ int inchk() { int bytesRead = 0; if (ttyfd < 0) return(0); ioctl(ttyfd, FIONREAD, (int)&bytesRead); return bytesRead; } /* R E A D P K T -- Read a packet from the communications device */ /* Looks for start of Kermit packet (soh), then reads everything between it and the end of the packet (eom) into the indicated buffer. The size of the data that was read; 0 on timeout or other possibly correctable error; -1 on fatal error, such as loss of connection, or no buffer to read into. */ int readpkt(UCHAR *p, struct k_data * k) { char x; int n, max; short flag; UCHAR c; /* Timeout not implemented in this sample. It should not be needed. All non-embedded Kermits that are capable of making connections are also capable of timing out, and only one Kermit needs to time out. */ #ifdef F_CTRLC short ccn; ccn = 0; #endif /* F_CTRLC */ #ifdef DEBUG if (debug) { fprintf(dp, "readpkt p=%ld, k->r_maxlen=%d, k->r_soh=%d, k->r_eom=%d\n", (ULONG) p,k->r_maxlen,k->r_soh,k->r_eom ); } #endif /* DEBUG */ if (ttyfd < 0 || !p) { /* Device not open or no buffer */ #ifdef DEBUG if (debug) fprintf(dp,"readpkt FAIL ttyfd=%d p=%ul\n",ttyfd,(ULONG)p); #endif /* DEBUG */ return(-1); } flag = n = 0; /* Init local variables */ while (1) { read(ttyfd, &x, 1); c = (k->parity) ? x & 0x7f : x & 0xff; /* Strip parity */ #ifdef F_CTRLC /* In remote mode only: three consecutive ^C's to quit */ if (k->remote && c == (UCHAR) 3) { if (++ccn > 2) { #ifdef DEBUG if (debug) fprintf(dp,"READPKT ^C^C^C\n"); #endif /* DEBUG */ return(-1); } } else { ccn = 0; } #endif /* F_CTRLC */ #ifdef DEBUG #if 0 if (debug) fprintf(dp, "READPKT %d(%c)\n", c, c); #endif #endif if (!flag && c != k->r_soh) /* No start of packet yet */ continue; /* so discard these bytes. */ if (c == k->r_soh) { /* Start of packet */ flag = 1; /* Remember */ continue; /* But discard */ } else if (c == k->r_eom) { /* End of packet */ #ifdef DEBUG *p = NUL; /* Terminate for printing */ #endif /* DEBUG */ return(n); } else { /* Contents of packet */ if (n++ > k->r_maxlen) /* Check length */ return(0); else *p++ = x & 0xff; } } #ifdef DEBUG if (debug) fprintf(dp,"READPKT FAIL (end)\n"); #endif /* DEBUG */ return(-1); } /* T X _ D A T A -- Writes n bytes of data to communication device. */ /* Returns: X_OK on success. X_ERROR on failure to write - i/o error. */ int tx_data(UCHAR *p, int n, short parity) { int x; int max; max = 10; /* Loop breaker */ #ifdef COMMENT #ifdef DEBUG if (debug) fprintf(dp,"tx_data[%s], n=%d\n",p,n); #endif /* DEBUG */ #endif /* COMMENT */ while (n > 0) { /* Keep trying till done */ x = write(ttyfd,(char *)p,n); #ifdef DEBUG if (debug) fprintf(dp,"tx_data write=%d, max=%d\n",x, max); if (x < 0) perror("TX_DATA WRITE ERROR"); #endif /* DEBUG */ if (x < 0 || --max < 1) /* Errors are fatal */ return(X_ERROR); n -= x; p += x; } return(X_OK); /* Success */ } /* O P E N F I L E -- Open output file */ /* Call with: Pointer to filename. Size in bytes. Creation date in format yyyymmdd hh:mm:ss, e.g. 19950208 14:00:00 Mode: 1 = read, 2 = create, 3 = append. Returns: X_OK on success. X_ERROR on failure, including rejection based on name, size, or date. */ int openfile(UCHAR * s, int mode, struct k_data * k) { switch (mode) { case 1: /* Read */ if (!(ifile = open((char *)s, O_RDONLY, 0))) { #ifdef DEBUG if (debug) perror("s"); #endif /* DEBUG */ return(X_ERROR); } k->s_first = 1; /* Set up for getkpt */ k->zinbuf[0] = '\0'; /* Initialize buffer */ k->zinptr = k->zinbuf; /* Set up buffer pointer */ k->zincnt = 0; /* and count */ #ifdef DEBUG if (debug) fprintf(dp,"openfile %s read ok\n",s); #endif /* DEBUG */ return(X_OK); case 2: /* Write (create) */ ofile = open((char *)s, (O_WRONLY | O_CREAT | O_TRUNC), 0); if (ofile < 0) { #ifdef DEBUG if (debug) perror("s"); #endif /* DEBUG */ return(X_ERROR); } #ifdef DEBUG if (debug) fprintf(dp,"openfile %s create ok\n",s); #endif /* DEBUG */ return(X_OK); #ifdef COMMENT case 3: /* Append (not used) */ ofile = open((char *)s,(O_WRONLY|O_APPEND), 0); if (ofile < 0) { #ifdef DEBUG if (debug) perror("s"); #endif /* DEBUG */ return(X_ERROR); } #ifdef DEBUG if (debug) fprintf(dp,"openfile %s append ok\n",s); #endif /* DEBUG */ return(X_OK); #endif /* COMMENT */ default: return(X_ERROR); } } /* F I L E I N F O -- Get info about existing file */ /* Call with: Pointer to filename Pointer to buffer for date-time string Length of date-time string buffer (must be at least 18 bytes) Pointer to int file type: <0: This return replace the int with 0 (text) or 1 (binary) >=0: File type is already determined or forced by caller. Returns: X_ERROR on failure. 0L or greater on success == file length. Date-time string set to yyyymmdd hh:mm:ss modtime of file. If date can't be determined, first byte of buffer is set to NUL. Type set to 0 (text) or 1 (binary) if it was < 0 at entry. */ ULONG fileinfo(UCHAR * filename, UCHAR * buf, int buflen, short * type, short mode) { struct stat statbuf; struct tm * timestamp, * localtime(); #ifdef F_SCAN #define SCANBUF 1024 #define SCANSIZ 49152 FILE * fp; /* File scan pointer */ char inbuf[SCANBUF]; /* and buffer */ #endif /* F_SCAN */ if (!buf) return(X_ERROR); buf[0] = '\0'; if (buflen < 18) return(X_ERROR); if (stat((char *)filename,&statbuf) < 0) return(X_ERROR); timestamp = localtime(&(statbuf.st_mtime)); sprintf((char *)buf,"%04d%02d%02d %02d:%02d:%02d", timestamp->tm_year + 1900, timestamp->tm_mon + 1, timestamp->tm_mday, timestamp->tm_hour, timestamp->tm_min, timestamp->tm_sec ); #ifdef F_SCAN /* Here we determine if the file is text or binary if the transfer mode is not forced. This is an extremely crude sample, which diagnoses any file that contains a control character other than HT, LF, FF, or CR as binary. A more thorough content analysis can be done that accounts for various character sets as well as various forms of Unicode (UTF-8, UTF-16, etc). Or the diagnosis could be based wholly or in part on the filename. etc etc. Or the implementation could skip this entirely by not defining F_SCAN and/or by always calling this routine with type set to -1. */ if (!mode) { /* File type determination requested */ int isbinary = 1; fp = fopen((char *)filename,"r"); /* Open the file for scanning */ if (fp) { int n = 0, count = 0; char c, * p; #ifdef DEBUG if (debug) fprintf(dp,"fileinfo scanning %s\n",filename); #endif /* DEBUG */ isbinary = 0; while (count < SCANSIZ && !isbinary) { /* Scan this much */ n = fread(inbuf,1,SCANBUF,fp); if (n == EOF || n == 0) break; count += n; p = inbuf; while (n--) { c = *p++; if (c < 32 || c == 127) { if (c != 9 && /* Tab */ c != 10 && /* LF */ c != 12 && /* FF */ c != 13) { /* CR */ isbinary = 1; #ifdef DEBUG if (debug) fprintf(dp,"fileinfo BINARY\n"); #endif /* DEBUG */ break; } } } } fclose(fp); *type = isbinary; } } #endif /* F_SCAN */ return((ULONG)(statbuf.st_size)); } /* R E A D F I L E -- Read data from a file */ int readfile(struct k_data * k) { #ifdef DEBUG if (debug) { fprintf(dp,"readfile zincnt=%d binary=%d\n",k->zincnt,k->binary); if (!k->zinptr) { fprintf(dp,"readfile ZINPTR NOT SET\n"); return(X_ERROR); } } #endif /* DEBUG */ if (k->zincnt < 1) { /* Nothing in buffer - must refill */ if (k->binary) { /* Binary - just read raw buffers */ k->zincnt = read(ifile, (char *)k->zinbuf, IBUFLEN); #ifdef DEBUG if (debug) fprintf(dp,"readfile binary ok zincnt=%d\n",k->zincnt); #endif /* DEBUG */ } else { /* Text mode needs LF/CRLF handling */ char c; /* Current character */ for (k->zincnt = 0; (k->zincnt < (IBUFLEN - 2)); (k->zincnt)++) { if (read(ifile, &c, 1) < 0) break; if (c == '\n') /* Have newline? */ k->zinbuf[(k->zincnt)++] = '\r'; /* Insert CR */ k->zinbuf[k->zincnt] = c; } #ifdef DEBUG k->zinbuf[k->zincnt] = '\0'; if (debug) { fprintf(dp,"readfile text ok zincnt=%d\n",k->zincnt); /* fprintf(dp,"readfile zinbuf=[%s]\n",k->zinbuf); */ } #endif /* DEBUG */ } k->zinbuf[k->zincnt] = '\0'; /* Terminate. */ if (k->zincnt == 0) /* Check for EOF */ return(-1); k->zinptr = k->zinbuf; /* Not EOF - reset pointer */ } (k->zincnt)--; /* Return first byte. */ return(*(k->zinptr)++ & 0xff); } /* W R I T E F I L E -- Write data to file */ /* Call with: String pointer Length Returns: X_OK on success X_ERROR on failure, such as i/o error, space used up, etc */ int writefile(struct k_data * k, UCHAR * s, int n) { int rc = X_OK; if (k->binary) { /* Binary mode, just write it */ if (write(ofile,(char *)s,n) != n) rc = X_ERROR; } else { /* Text mode, skip CRs */ UCHAR * p, * q; int i; q = s; while (1) { for (p = q, i = 0; ((*p) && (*p != (UCHAR)13)); p++, i++) ; if (i > 0) { if (write(ofile,(char *)q,i) != i) { rc = X_ERROR; break; } } if (!*p) break; q = p+1; } } #ifdef DEBUG if (rc == X_ERROR) perror("WRITE"); #endif /* DEBUG */ return(rc); } /* C L O S E F I L E -- Close output file */ /* Mode = 1 for input file, mode = 2 or 3 for output file. For output files, the character c is the character (if any) from the Z packet data field. If it is Z or X, it means the file transfer was canceled in midstream by the sender, and the file is therefore incomplete. This routine should check for that and decide what to do. */ int closefile(UCHAR c, int mode) { int rc; rc = X_OK; if (mode == 1) { if (close(ifile) < 0) rc = X_ERROR; } else if (mode == 2 || mode == 3) { if (close(ofile) < 0) rc = X_ERROR; } else if ((k->ikeep == 0) && /* Don't keep incomplete files */ (c == 'D')) { /* This file was incomplete */ unlink(k->filename); /* Delete it. */ } return(rc); }