int main(int argc, char **argv) { int serfd; if (argc != 4) { fprintf(stderr, "Usage: %s <port> <device> <rate> - act as a serial forwarder on <port>\n" "(listens to serial port <device> at baud rate <rate>)\n" , argv[0]); exit(2); } if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) fprintf(stderr, "Warning: failed to ignore SIGPIPE.\n"); open_serial(argv[2], platform_baud_rate(argv[3])); serfd = serial_source_fd(src); open_server_socket(atoi(argv[1])); for (;;) { fd_set rfds; int maxfd = -1; struct timeval zero; int serial_empty; int ret; zero.tv_sec = zero.tv_usec = 0; FD_ZERO(&rfds); fd_wait(&rfds, &maxfd, serfd); fd_wait(&rfds, &maxfd, server_socket); wait_clients(&rfds, &maxfd); serial_empty = serial_source_empty(src); if (serial_empty) ret = select(maxfd + 1, &rfds, NULL, NULL, NULL); else { ret = select(maxfd + 1, &rfds, NULL, NULL, &zero); check_serial(); } if (ret >= 0) { if (FD_ISSET(serfd, &rfds)) check_serial(); if (FD_ISSET(server_socket, &rfds)) check_new_client(); check_clients(&rfds); } } }
int dummy_check(char *s) { printf("Checking serial...\n"); sleep(2); int r = check_serial(s); return r; }
int main(int argc, char **argv) { int serfd; if (argc != 4) { fprintf(stderr, "Usage: %s <device> <rate> <file> - act as a serial forwarder on <port>\n" "(listens to serial port <device> at baud rate <rate>)\n", argv[0]); exit(2); } if (signal(SIGPIPE, SIG_IGN) == SIG_ERR) fprintf(stderr, "Warning: failed to ignore SIGPIPE.\n"); if (signal(SIGINT, sigproc) == SIG_ERR) fprintf(stderr, "Warning: failed to set the SIGINT interruption.\n"); srcId = -1; open_serial(argv[1], platform_baud_rate(argv[2])); serfd = serial_source_fd(src); open_file_fd(argv[3]); dup2(filefd, 1); for (;;) { fd_set rfds; int maxfd = -1; struct timeval zero; int serial_empty; int ret; zero.tv_sec = zero.tv_usec = 0; FD_ZERO(&rfds); fd_wait(&rfds, &maxfd, serfd); serial_empty = serial_source_empty(src); check_serial(); if (ret >= 0) { if (FD_ISSET(serfd, &rfds)) check_serial(); } } }
int main(int argc, char *argv[]){ if (argc < 2){ printf("usage: %s <serial number>\n", argv[0]); return 0; } char *serial = argv[1]; if (check_serial(serial) == 1){ printf("\n############## Good Job! ##############\n############## The serial is valid! ##############\n\n"); }else{ printf("\n############## Try again! ##############\n############## Invalid serial! ##############\n\n"); } }
/* static */ vrpn_Atmel * vrpn_Atmel::Create(char* name, vrpn_Connection *c, char *port, long baud, int channel_count , int * channel_mode) { int fd; #ifdef VRPN_ATMEL_SERIAL_VRPN if ( (fd=vrpn_open_commport(port, baud)) == -1) { fprintf(stderr,"vrpn_Atmel: can't Open serial port\n"); return NULL; } #else // Open the serial port if ( (fd=openPort(port, baud, &init_params)) == -1) { fprintf(stderr,"vrpn_Atmel: can't Open serial port\n"); return NULL; } // look if the atmel is connected if (! check_serial(fd) ) { return NULL; } #endif vrpn_Atmel * self = new vrpn_Atmel(name, c, fd); if ( (self->vrpn_Analog_Server::setNumChannels(channel_count) != channel_count) || (self->vrpn_Analog_Output_Server::setNumChannels(channel_count) != channel_count) ) { fprintf(stderr,"vrpn_Atmel: the requested number of channels is not available\n"); delete self; return NULL; } // init the channels based on the infos given from the config file self->init_channel_mode(channel_mode); self->_status = VRPN_ATMEL_STATUS_WAITING_FOR_CONNECTION; return self; }
/*----------------------------------------------------------------------------*/ void print_portscan(ASerialPort_t* aPort) { int test, cCount = 0; printf("--------------------\n"); printf("COM Port Scan Result\n"); printf("--------------------\n"); for(test=1;test<=MAX_COM_PORT;test++) { if(check_serial(aPort,test)) { printf("%s%d: ",aPort->mPortName,COM_PORT(test)); cCount++; printf("Ready.\n"); } } printf("\nDetected Port(s): %d\n\n",cCount); }
/*!***************************************************************************** ******************************************************************************* \note clear_serial \date Oct 2000 \remarks empties the serial buffer ******************************************************************************* Function Parameters: [in]=input,[out]=output \param[in] fd : file descriptor ******************************************************************************/ int clear_serial(int fd) { int n_bytes; int n_bytes_read; char buf[10000]; while(TRUE) { n_bytes = check_serial(fd); if (n_bytes > 10000) { n_bytes = 10000; n_bytes_read = read_serial(fd,n_bytes,buf); } else { n_bytes_read = read_serial(fd,n_bytes,buf); break; } } return TRUE; }
/*!***************************************************************************** ******************************************************************************* \note read_frame \date Oct 2000 \remarks Reads a frame from the serial port. Note: all date comes as ASCII characters!!! Weird ..... The following characters demark the frame: start of frame : ^ end of frame : \n start of camera 2: / end of channel : ; end of number : space For the following readings, the NewtonLabs hardware needs to be set to: - report frame counter (otherwise this function reports errors) - only report x and y of the blobs, no other data (this could be changed if needed) - only allow one object per channel (can be improved in the future, too, but requires to use temporal reasoning to avoid confusing two objects with the same color, and also occlusions) Currently, blob numbering coincides with the the color channel number. Thus, an empty frame would look like: 1213;;;;/;;;;/ A valid frame with one blob would looke like: 4475460 356 269;;;;/238 174;;;;/ ******************************************************************************* Function Parameters: [in]=input,[out]=output none returns the number of bytes read ******************************************************************************/ static int read_frame(void) { int i,j; char *start_camera[N_CAMERAS+1]; char *startbuf; int rc; char buffer[1000]; char store_char; char *cptr; while (TRUE) { if ((rc=check_serial(serial_fd)) > 0) { rc=read_serial(serial_fd,rc,buffer); if (buffer[rc-1]=='\n') break; else printf("Last character was not CR in LINE mode serial connection\n"); } nanosleep(&nsleep,NULL); } /* where is the start of camera 2 data: at the first "/" */ start_camera[CAMERA_2] = strchr(buffer,'/'); if (start_camera[CAMERA_2]==NULL) { printf("Invalid Frame: / character not found\n"); printf("Frame = >%s<\n",buffer); return FALSE; } *(start_camera[CAMERA_2]) = '\0'; ++start_camera[CAMERA_2]; /* where is the start of camera 1 data: more tricky: it is before the first ";", and after the first space, if blob1 exists */ cptr = strchr(buffer,';'); start_camera[CAMERA_1] = strchr(buffer,' '); if (start_camera[CAMERA_1]==NULL) start_camera[CAMERA_1]=cptr; else if (cptr<start_camera[CAMERA_1]) start_camera[CAMERA_1]=cptr; store_char = *(start_camera[CAMERA_1]); *(start_camera[CAMERA_1])='\0'; /* read the frame counter and the first blob of camera 1 */ if (sscanf(buffer,"%d",&the_frame.counter) == 0) { printf("Invalid Frame: frame counter not found\n"); return FALSE; } *(start_camera[CAMERA_1])=store_char; /* loop through both cameras and parse the data */ for (i=1; i<=N_CAMERAS; ++i) { startbuf=start_camera[i]; for (j=1; j<=MAX_BLOBS; ++j) { /* find the next semicolon */ cptr = strchr(startbuf,';'); if (cptr==NULL) break; *cptr = '\0'; /* read the data */ if (sscanf(startbuf,"%d %d",&the_frame.blobinfo[j][i].x, &the_frame.blobinfo[j][i].y) != 2) the_frame.blobinfo[j][i].status = FALSE; else the_frame.blobinfo[j][i].status = TRUE; startbuf = cptr+1; } } return TRUE; }
/*!***************************************************************************** ******************************************************************************* \note acquire_blobs \date Oct 2000 \remarks read the current information about the blobs ******************************************************************************* Function Parameters: [in]=input,[out]=output \param[in] blobs : array of blob structures ******************************************************************************/ int acquire_blobs(Blob2D blobs[][2+1]) { int i,j; int rc; char buffer[2]; int start_time = 0; /* reset all the blobs to be non existent */ for (i = 1; i<=MAX_BLOBS; ++i) { the_frame.blobinfo[i][CAMERA_1].status = FALSE; the_frame.blobinfo[i][CAMERA_2].status = FALSE; } start_time = (int) time(NULL); while( TRUE ) { /* Wait for data in the serial buffer */ if (check_serial(serial_fd) > 0) { if (read_serial(serial_fd,1,buffer)==1) if (buffer[0] == '^') /* this is the start of frame character */ break; } nanosleep(&nsleep,NULL); if (abs(start_time - (int) time(NULL)) > 10) { printf("Error: Timeout when reading from vision hardware\n"); printf("Switch to no-hardware mode\n"); no_hardware_flag = TRUE; return TRUE; } } /* now we are at the beginning of a frame */ last_counter = the_frame.counter; /* read the entire frame */ rc = read_frame(); ++frame_counter; if (vision_servo_calls == 0) { /* synchronize the v->frame_counter and the_frame.counter */ frame_counter = the_frame.counter; last_counter = frame_counter - 1; } ++count_all_frames; /* frame_counter and the_frame.counter should always coincide, anything else counts as a lost frame */ count_lost_frames += abs(frame_counter - the_frame.counter); frame_counter = the_frame.counter; /* was the frame counter advanced and was read_frame successful ?*/ if ( (the_frame.counter == last_counter + 1) && rc ) { ; } else { /* reset all the blobs to be non existent */ for (i = 1; i<=MAX_BLOBS; ++i) { the_frame.blobinfo[i][CAMERA_1].status = FALSE; the_frame.blobinfo[i][CAMERA_2].status = FALSE; } ++count_lost_frames; } /* copy the data into the global structures */ for (i=1; i<=MAX_BLOBS; ++i) { /* if (the_frame.blobinfo[i][CAMERA_1].status && the_frame.blobinfo[i][CAMERA_2].status) { blobs[i][CAMERA_1].status = blobs[i][CAMERA_2].status = TRUE; blobs[i][CAMERA_1].x[_X_] = the_frame.blobinfo[i][CAMERA_1].x; blobs[i][CAMERA_1].x[_Y_] = the_frame.blobinfo[i][CAMERA_1].y; blobs[i][CAMERA_2].x[_X_] = the_frame.blobinfo[i][CAMERA_2].x; blobs[i][CAMERA_2].x[_Y_] = the_frame.blobinfo[i][CAMERA_2].y; } else { blobs[i][CAMERA_1].status = blobs[i][CAMERA_2].status = FALSE; } */ if (the_frame.blobinfo[i][CAMERA_1].status) { blobs[i][CAMERA_1].status = TRUE; blobs[i][CAMERA_1].x[_X_] = the_frame.blobinfo[i][CAMERA_1].x; blobs[i][CAMERA_1].x[_Y_] = the_frame.blobinfo[i][CAMERA_1].y; } else { blobs[i][CAMERA_1].status = FALSE; } if (the_frame.blobinfo[i][CAMERA_2].status) { blobs[i][CAMERA_2].status = TRUE; blobs[i][CAMERA_2].x[_X_] = the_frame.blobinfo[i][CAMERA_2].x; blobs[i][CAMERA_2].x[_Y_] = the_frame.blobinfo[i][CAMERA_2].y; } else { blobs[i][CAMERA_2].status = FALSE; } } return TRUE; }
int acquire_blobs(Blob2D blobs[][2+1]) { int i,j,r,m; int rc; static char buffer[MAX_CHARS]; static int n_buffer = 0; char buffer2[MAX_CHARS]; int n_buffer2 = 0; int count = 0; int run = TRUE; // reset all the blobs to be non existent for (i = 1; i<=max_blobs; ++i) { the_frame.blobinfo[i][CAMERA_1].status = FALSE; the_frame.blobinfo[i][CAMERA_2].status = FALSE; } while( run ) { // check for data in the serial port buffer if ( (rc=check_serial(serial_fd)) > 0) { if (rc > MAX_CHARS-n_buffer) { rc = MAX_CHARS-n_buffer; printf("buffer overflow\n"); } rc = read_serial(serial_fd,rc,&(buffer[n_buffer])); n_buffer += rc; } // look for a complete frame in the data for (j=n_buffer-1; j>=0; --j) { if (buffer[j] == '\n') // found the end of a frame at j break; } for (i=j; i>=0; --i) { if (buffer[i] == '^') { // found the beginning of a frame at i run = FALSE; break; } } if (i<0 && j>=0) { // no beginning of frame found despite end of frame for (r = j+1; r<n_buffer; ++r) buffer[r-(j+1)] = buffer[r]; n_buffer -= j+1; printf("discard early buffer\n"); } if (!run) break; taskDelay(ns2ticks(WAIT_IN_NS)); if (++count > 10000) { printf("Error: Timeout when reading from vision hardware\n"); printf("Switch to no-hardware mode\n"); no_hardware_flag = TRUE; return TRUE; } } // re-arrange the buffer for (r = i+1; r<j; ++r) buffer2[r-(i+1)] = buffer[r]; buffer2[j-(i+1)] = '\0'; n_buffer2 = j-(i+1); for (r = j+1; r<n_buffer; ++r) buffer[r-(j+1)] = buffer[r]; n_buffer -= j+1; /* now we are at the beginning of a frame */ last_counter = the_frame.counter; /* read the entire frame */ rc = read_frame(buffer2,n_buffer2); ++frame_counter; if (vision_servo_calls < 10) { /* synchronize the v->frame_counter and the_frame.counter */ frame_counter = the_frame.counter; last_counter = frame_counter - 1; } ++count_all_frames; /* frame_counter and the_frame.counter should always coincide, anything else counts as a lost frame */ count_lost_frames += abs(frame_counter - the_frame.counter); frame_counter = the_frame.counter; /* was the frame counter advanced and was read_frame successful ?*/ if ( (the_frame.counter == last_counter + 1) && rc ) { ; } else { /* reset all the blobs to be non existent */ for (i = 1; i<=max_blobs; ++i) { the_frame.blobinfo[i][CAMERA_1].status = FALSE; the_frame.blobinfo[i][CAMERA_2].status = FALSE; } ++count_lost_frames; } /* copy the data into the global structures */ for (i=1; i<=max_blobs; ++i) { /* if (the_frame.blobinfo[i][CAMERA_1].status && the_frame.blobinfo[i][CAMERA_2].status) { blobs[i][CAMERA_1].status = blobs[i][CAMERA_2].status = TRUE; blobs[i][CAMERA_1].x[_X_] = the_frame.blobinfo[i][CAMERA_1].x; blobs[i][CAMERA_1].x[_Y_] = the_frame.blobinfo[i][CAMERA_1].y; blobs[i][CAMERA_2].x[_X_] = the_frame.blobinfo[i][CAMERA_2].x; blobs[i][CAMERA_2].x[_Y_] = the_frame.blobinfo[i][CAMERA_2].y; } else { blobs[i][CAMERA_1].status = blobs[i][CAMERA_2].status = FALSE; } */ if (the_frame.blobinfo[i][CAMERA_1].status) { blobs[i][CAMERA_1].status = TRUE; blobs[i][CAMERA_1].x[_X_] = the_frame.blobinfo[i][CAMERA_1].x; blobs[i][CAMERA_1].x[_Y_] = the_frame.blobinfo[i][CAMERA_1].y; } else { blobs[i][CAMERA_1].status = FALSE; } if (the_frame.blobinfo[i][CAMERA_2].status) { blobs[i][CAMERA_2].status = TRUE; blobs[i][CAMERA_2].x[_X_] = the_frame.blobinfo[i][CAMERA_2].x; blobs[i][CAMERA_2].x[_Y_] = the_frame.blobinfo[i][CAMERA_2].y; } else { blobs[i][CAMERA_2].status = FALSE; } } return TRUE; }
static int32_t bulcrypt_get_emm_type(EMM_PACKET *ep, struct s_reader *reader) { char dump_emm_sn[64], dump_card_sn[64]; unsigned int emm_len = check_sct_len(ep->emm, 3); int32_t ret = FALSE; memset(ep->hexserial, 0, 8); if (emm_len < 176) { rdr_debug_mask(reader, D_EMM, "emm_len < 176 (%u): %s", emm_len, cs_hexdump(1, ep->emm, 12, dump_emm_sn, sizeof(dump_emm_sn))); ep->type = UNKNOWN; return FALSE; } cs_hexdump(1, reader->hexserial, 3, dump_card_sn, sizeof(dump_card_sn)); #define compare_hex_serial(serial_len) \ do { \ cs_hexdump(1, ep->hexserial, serial_len, dump_emm_sn, sizeof(dump_emm_sn)); \ ret = memcmp(ep->hexserial, reader->hexserial, serial_len) == 0; \ } while(0) #define check_serial(serial_len) \ do { \ memcpy(ep->hexserial, ep->emm + 3, serial_len); \ compare_hex_serial(serial_len); \ } while(0) #define check_serial_skip_first(serial_len) \ do { \ memcpy(ep->hexserial, ep->emm + 4, serial_len); \ compare_hex_serial(serial_len); \ } while(0) switch (ep->emm[0]) { case BULCRYPT_EMM_UNIQUE_82: case BULCRYPT_EMM_UNIQUE_85: ep->type = UNIQUE; check_serial(3); if (ret) rdr_log_sensitive(reader, "EMM_UNIQUE-%02x-{%02x}, emm_sn = {%s}, card_sn = {%s}", ep->emm[0], ep->emm[6], dump_emm_sn, dump_card_sn); break; case BULCRYPT_EMM_SHARED_84: ep->type = SHARED; check_serial(2); if (ret) rdr_log_sensitive(reader, "EMM_SHARED-%02x-{%02x-%02x}, emm_sn = {%s}, card_sn = {%s}", ep->emm[0], ep->emm[5], ep->emm[6], dump_emm_sn, dump_card_sn); break; case BULCRYPT_EMM_8a: ep->type = UNKNOWN; check_serial_skip_first(2); if (ret) rdr_log_sensitive(reader, "EMM_UNKNOWN-%02x-{%02x-%02x}, emm_sn = {%s}, card_sn = {%s}", ep->emm[0], ep->emm[5], ep->emm[6], dump_emm_sn, dump_card_sn); break; case BULCRYPT_EMM_8b: ep->type = GLOBAL; check_serial_skip_first(1); if (ret) rdr_log_sensitive(reader, "EMM_GLOBAL-%02x-{%02x-%02x}, emm_sn = {%s}, card_sn = {%s}", ep->emm[0], ep->emm[5], ep->emm[6], dump_emm_sn, dump_card_sn); break; case BULCRYPT_EMM_FILLER: ep->type = UNKNOWN; break; default: ep->type = UNKNOWN; rdr_log(reader, "UNKNOWN_EMM len: %u, %s..", emm_len, cs_hexdump(1, ep->emm, 12, dump_emm_sn, sizeof(dump_emm_sn))); break; } return ret; }