int main(int argc, char **argv) { char filename[256]; char *newline; int abort; FILE *file_to_send; int fd_serial; uint8_t *packet_buffer; long int size; long int i; struct termios options; if (argc != 2) { printf("Hardware BitCoin wallet tester\n"); printf("Usage: %s <serial device>\n", argv[0]); printf("\n"); printf("Example: %s /dev/ttyUSB0\n", argv[0]); exit(1); } // Attempt to open serial link. fd_serial = open(argv[1], O_RDWR | O_NOCTTY); if (fd_serial == -1) { printf("Could not open device \"%s\"\n", argv[1]); printf("Make sure you have permission to open it. In many systems, only\n"); printf("root can access devices by default.\n"); exit(1); } fcntl(fd_serial, F_SETFL, 0); // block on reads tcgetattr(fd_serial, &options); cfsetispeed(&options, B57600); // baud rate 57600 cfsetospeed(&options, B57600); options.c_cflag |= (CLOCAL | CREAD); // enable receiver and set local mode on options.c_cflag &= ~PARENB; // no parity options.c_cflag &= ~CSTOPB; // 1 stop bit options.c_cflag &= ~CSIZE; // character size mask options.c_cflag |= CS8; // 8 data bits options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG); // raw input options.c_iflag &= ~(IXON | IXOFF | IXANY); // no software flow control options.c_oflag &= ~OPOST; // raw output tcsetattr(fd_serial, TCSANOW, &options); tx_bytes_to_ack = DEFAULT_ACKNOWLEDGE_INTERVAL; rx_bytes_to_ack = DEFAULT_ACKNOWLEDGE_INTERVAL; abort = 0; do { // Get filename from user. printf("Enter file to send (blank to quit): "); fgets(filename, sizeof(filename), stdin); newline = strrchr(filename, '\r'); if (newline != NULL) { *newline = '\0'; } newline = strrchr(filename, '\n'); if (newline != NULL) { *newline = '\0'; } if (strcmp(filename, "")) { file_to_send = fopen(filename, "rb"); if (file_to_send == NULL) { printf("Couldn't open file \"%s\"\n", filename); } else { // Get file length then read entire contents of file. fseek(file_to_send, 0, SEEK_END); size = ftell(file_to_send); fseek(file_to_send, 0, SEEK_SET); packet_buffer = malloc(size); fread(packet_buffer, size, 1, file_to_send); fclose(file_to_send); printf("Sending packet: "); displayPacket(packet_buffer, size); // Send the packet. for (i = 0; i < size; i++) { sendByte(packet_buffer[i], fd_serial); } free(packet_buffer); // Get and display response packet. packet_buffer = receivePacket(fd_serial); size = 5 + readU32LittleEndian(&(packet_buffer[1])); printf("Received packet: "); displayPacket(packet_buffer, size); free(packet_buffer); } } else { abort = 1; } } while (!abort); close(fd_serial); exit(0); }
// -------------------------------------------------------------------- // processPacket // -------------------------------------------------------------------- bool processPacket(int sock, File* outFile, bool display, bool &close) { LogPacketHeader packetHeader((LogType)0); struct tm *timeInfo=NULL; bool magicOK = false; int magic=0; static Byte data[LOG_DATA_MAX_LENGTH]; close=false; // look for magic while (!magicOK) { Byte byte=0; while(1) { if (peekFromSocket(sock, &byte, 1)==0) { if (byte==0xa0) break; else readFromSocket(sock, &byte, 1); } } if (peekFromSocket(sock, &magic, sizeof(int))==0) { if (magic != LOG_MAGIC_NBR) { printf ("*** Packet corrupted try to resynchronize (magic " ": 0x%x != 0x%x) !!!\n", magic, LOG_MAGIC_NBR); readFromSocket(sock, &magic, sizeof(int)); } else { break; } } else { return false; } } if (readFromSocket(sock, &packetHeader, sizeof(LogPacketHeader))==0) { if (display) { printf(" timeStamp = "); timeInfo = localtime(&packetHeader.timeStamp.tv_sec); printf("%02d/%02d/%02d %02d:%02d:%02d,%06d\n", timeInfo->tm_mday, timeInfo->tm_mon+1, timeInfo->tm_year+1900, timeInfo->tm_hour, timeInfo->tm_min, timeInfo->tm_sec, (int)packetHeader.timeStamp.tv_usec); } if (packetHeader.type == LOG_TYPE_CLOSE) { printf("Closing log\n"); close=true; return false; } //printf("read length=%d\n", packetHeader.length); if (readFromSocket(sock, data, packetHeader.length)==0) { if (display) { displayPacket(packetHeader, data); } } storePacket(outFile, packetHeader, data); return true; } else { printf("read error\n"); return false; } }