Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.07 KB | None | 0 0
  1. #include <stdint.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <fcntl.h>
  7. #include <termios.h>
  8. #include <algorithm>
  9.  
  10. #define SERIAL "/dev/ttyS0"
  11. #define BAUDRATE B9600
  12.  
  13. #define NUM_ENDBUF 8
  14.  
  15. int tty_fd, flags;
  16.  
  17. void send(unsigned char what) {
  18.  
  19.     write(tty_fd, &what ,1);
  20. }
  21.  
  22. void send(unsigned char* buffer, uint32_t length) {
  23.  
  24. //  sleep(1);
  25.     write(tty_fd, buffer, length);
  26. }
  27.  
  28. void recieve(unsigned char* buffer, unsigned int length) {
  29.  
  30.     read(tty_fd,buffer,length);
  31. }
  32.  
  33. int main(int argc,char** argv)
  34. {  
  35.     if (argc<2) {
  36.         printf("Usage: com <filename>\n");
  37.         return 1;
  38.     }
  39.  
  40.     // Load file
  41.     FILE* file = fopen(argv[1], "rb");
  42.     if (file==NULL) {
  43.         printf("File %s not found\n", argv[1]);
  44.         return 1;
  45.     }
  46.  
  47.     // Get file length 
  48.     fseek(file,0,SEEK_END);
  49.     uint32_t length = ftell(file);
  50.     fseek(file,0,SEEK_SET);
  51.  
  52.     // Load data
  53.     unsigned char* buffer = new unsigned char[length];
  54.     fread(buffer, sizeof(char),length,file);
  55.  
  56.     printf("Sending file %s (%d bytes, %d chunks)\n", argv[1], length, length>>8);
  57.  
  58.     struct termios tio;
  59.     memset(&tio,0,sizeof(tio));
  60.  
  61.     cfsetospeed(&tio,BAUDRATE);    
  62.     cfsetispeed(&tio,BAUDRATE);
  63.  
  64.     tio.c_iflag=0;
  65.     tio.c_oflag=0;
  66.     tio.c_cflag=CS8|CREAD|CLOCAL|CRTSCTS; // Obs, CRTSCTS รคr ny
  67.     tio.c_lflag=0;
  68.     tio.c_cc[VMIN]=1;
  69.     tio.c_cc[VTIME]=5;
  70.     if((tty_fd = open(SERIAL , O_RDWR)) == -1){ //  | O_DSYNC
  71.             printf("Error while opening %s\n\r", SERIAL);
  72.             return -1;
  73.     }
  74.     tcsetattr(tty_fd,TCSANOW,&tio);
  75.  
  76.     tcflush(tty_fd, TCIOFLUSH);
  77.  
  78.     // Send size as four bytes, MSB first, to initialize transfer
  79.     send((length>>24)&0xff);
  80.     send((length>>16)&0xff);
  81.     send((length>>8)&0xff);
  82.     send((length)&0xff);
  83.  
  84.     printf("Sent length\n");
  85.  
  86.     bool done=false;
  87.     uint16_t lastchunknr=-1;
  88.     while (!done) {
  89.  
  90.         // Get chunk number
  91.         uint16_t chunknr=0;
  92.    
  93.         uint8_t input[2];
  94.         recieve( &input[0], 2);
  95.  
  96.         chunknr=(input[0]<<8) | input[1];
  97.    
  98.         //printf("\nchunknr:%x\n", chunknr);
  99.  
  100.         // If chunk $ffff is asked for, kindly exit
  101.         if (chunknr==0xffff) {
  102.  
  103.             printf("\nQuitting...\n");
  104.             exit(0);
  105.         }
  106.  
  107.         // Just debug
  108.         if (chunknr==lastchunknr) printf("\n\n\rResending chunk %d\n\n",chunknr);
  109.         lastchunknr=chunknr;
  110.        
  111.         uint32_t chunklength = std::min(uint32_t(256),uint32_t(length-chunknr*256));
  112.  
  113.         // Send checksum
  114.         uint8_t checksum=0;
  115.         for (uint32_t i=0; i<chunklength; i++) {
  116.             checksum+=buffer[chunknr*256+i];  // (will %256 automatic)
  117.         }
  118.         printf("\nSending chunk [%3d of %3d]   (checksum:%3d, length:%3d) ", chunknr,length>>8, checksum, chunklength);
  119.         fflush(stdout);    
  120.  
  121.         send(checksum);
  122.  
  123.         send(uint8_t(chunklength-1));
  124.  
  125.         tcdrain(tty_fd);
  126.  
  127.         // Send part
  128.         send(buffer+(chunknr<<8), chunklength);
  129.  
  130.         tcdrain(tty_fd);
  131.  
  132.         // Send some crap at the end just to make sure, code
  133.         // timeout on the amiga to cope with this instead
  134.         uint8_t endbuf[NUM_ENDBUF];
  135.         for (int i=0; i<NUM_ENDBUF; i++) endbuf[i]=65;
  136.         write(tty_fd,endbuf, NUM_ENDBUF);
  137.  
  138.         tcdrain(tty_fd);
  139.     }
  140.    
  141.     printf("File sent, exiting...\n\n");
  142.  
  143.     close(tty_fd);
  144.     return EXIT_SUCCESS;
  145. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement