int diff_waveforms(char *file1, char *file2, char *conv_str1, char *conv_str2, int verbose, FILE *fp) { SP_FILE *sp1, *sp2; char *buff1=CNULL, *buff2=CNULL; int n1, n2, i, fail=0, blocksize=512; int return_value = 0; SP_INTEGER samples_processed=0, file1_snb, file2_snb, file1_chcnt; SP_INTEGER file2_chcnt; if ((sp1 = sp_open(file1, "r")) == SPNULL){ fail=1; if (verbose) fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",file1,file2); if (verbose) fprintf(fp,"sp_open failed on file %s\n",file1); sp_print_return_status(fp); } if ((sp2 = sp_open(file2, "r")) == SPNULL){ if (fail == 0) if (verbose) fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n", file1,file2); fail=1; if (verbose) fprintf(fp,"sp_open failed on file %s\n",file2); sp_print_return_status(fp); goto FATAL_QUIT; } if (fail) goto FATAL_QUIT; if (conv_str1 != CNULL) if (sp_set_data_mode(sp1,conv_str1) != 0){ fail=1; if (verbose) fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n", file1,file2); if (verbose) fprintf(fp,"sp_set_data_mode failed on file %s\n",file1); sp_print_return_status(fp); } if (conv_str2 != CNULL) if (sp_set_data_mode(sp2,conv_str2) != 0){ if (fail == 0) if (verbose) fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n", file1,file2); fail=1; if (verbose) fprintf(fp,"sp_set_data_mode failed on file %s\n",file2); sp_print_return_status(fp); } if (fail) goto FATAL_QUIT; if (sp_h_get_field(sp1,SAMPLE_N_BYTES_FIELD, T_INTEGER,(void *)&file1_snb) != 0){ fprintf(spfp,"Unable to retieve %s field from file '%s'\n", SAMPLE_N_BYTES_FIELD,file1); goto FATAL_QUIT; } if (sp_h_get_field(sp2,SAMPLE_N_BYTES_FIELD, T_INTEGER,(void *)&file2_snb) != 0){ fprintf(spfp,"Unable to retieve %s field from file '%s'\n", SAMPLE_N_BYTES_FIELD,file2); goto FATAL_QUIT; } if (sp_h_get_field(sp1,CHANNEL_COUNT_FIELD, T_INTEGER,(void *)&file1_chcnt) != 0){ fprintf(spfp,"Unable to retieve %s field from file '%s'\n", CHANNEL_COUNT_FIELD,file1); goto FATAL_QUIT; } if (sp_h_get_field(sp2,CHANNEL_COUNT_FIELD, T_INTEGER,(void *)&file2_chcnt) != 0){ fprintf(spfp,"Unable to retieve %s field from file '%s'\n", CHANNEL_COUNT_FIELD,file2); goto FATAL_QUIT; } if ((buff1 = (char *)sp_data_alloc(sp1,blocksize)) == CNULL){ if (verbose) fprintf(fp,"Unable to malloc memory for buffer 1\n"); goto FATAL_QUIT; } if ((buff2 = (char *)sp_data_alloc(sp2,blocksize)) == CNULL){ if (verbose) fprintf(fp,"Unable to malloc memory for buffer 2\n"); goto FATAL_QUIT; } if (file1_snb != file2_snb){ if (verbose){ fprintf(fp,"Files '%s' and '%s' do not have the same", file1,file2); fprintf(fp," sample_n_bytes value, %d and %d respectively\n", file1_snb,file2_snb); } goto FATAL_QUIT; } if (file1_chcnt != file2_chcnt){ if (verbose){ fprintf(fp,"Files '%s' and '%s' do not have the same", file1,file2); fprintf(fp," channel_count value, %d and %d respectively\n", file1_chcnt,file2_chcnt); } goto FATAL_QUIT; } do { int record_size = file1_snb * file1_chcnt; n1 = sp_read_data((char *)buff1,blocksize,sp1); if ((n1 == 0) && (sp1->read_spifr->waveform->samples_read != sp1->read_spifr->status->user_sample_count)){ sp_print_return_status(fp); goto FATAL_QUIT; } n2 = sp_read_data((char *)buff2,blocksize,sp2); if ((n2 == 0) && (sp2->read_spifr->waveform->samples_read != sp2->read_spifr->status->user_sample_count)){ sp_print_return_status(fp); goto FATAL_QUIT; } if (n1 != n2){ if (verbose) fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n", file1,file2); if (verbose) fprintf(fp," %d samples read from '%s'\n",n1,file1); if (verbose) fprintf(fp," %d samples read from '%s'\n",n2,file2); goto FATAL_QUIT; } for (i=0; i<n1; i++) { if (memcmp(buff1 + i*record_size, buff2 + i*record_size,record_size)){ int bn; if (verbose){ fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s sample %d\n", file1,file2,samples_processed+i); fprintf(fp," File 1: "); for (bn=0; bn < record_size; bn++) { fprintf(fp,"%2x ", *((unsigned char *)(buff1 + i*record_size + bn))); if ((bn+1) % file1_snb == 0) fprintf(fp,"| "); } fprintf(fp,"\n File 2: "); for (bn=0; bn < record_size; bn++) { fprintf(fp,"%2x ", *((unsigned char *)(buff2 + i*record_size + bn))); if ((bn+1) % file1_snb == 0) fprintf(fp,"| "); } fprintf(fp,"\n"); } goto FATAL_QUIT; } } samples_processed += n1; } while (!sp_eof(sp1)); if (!sp_eof(sp2)){ if (verbose) fprintf(fp,"DIFFERENT WAVEFORM: files %s and %s\n",file1,file2); if (verbose) fprintf(fp," file %s not at eof\n",file2); goto FATAL_QUIT; } return_value = 0; goto CLEAN_UP; FATAL_QUIT: /* Failed routine */ return_value = 100; CLEAN_UP: if (sp1 != SPNULL) { if (buff1 != (char *)NULL) sp_data_free(sp1,buff1); sp_close(sp1); } if (sp2 != SPNULL) { if (buff2 != (char *)NULL) sp_data_free(sp2,buff2); sp_close(sp2); } return(return_value); }
static VALUE rb_sp_close (VALUE self, VALUE eid) { int _eid = FIX2INT (eid); return INT2FIX (sp_close (_eid)); }
int main(int argc, char *argv[]) { /* select部份 */ int opt = TRUE; int master_socket, addrlen, new_socket, client_socket[MAXCLIENTS], max_clients = MAXCLIENTS, activity, i, valread, sd; user_content_t *my_contents[MAXCLIENTS + 1]; int max_sd; struct timeval tv; /* select超时 */ //set of socket descriptors fd_set readfds; /* IP部份 */ struct addrinfo hints, *res; /* 连接到target的用到的 */ struct sockaddr_in address; /* 每个客户端的缓冲区和索引 */ char buffer[MAXCLIENTS][MAXLEN]; char *buffer_p[MAXCLIENTS]; //data buffer of 1K int buffer_data_size[MAXCLIENTS]; char itoa_buffer[8]; /* ip地址从网络顺序转成char数组 */ char *header = NULL; /* 将转换好的ip地址:封包成header */ /* 串口部份 固定变量 */ static char com_devicename[] = "/dev/ttyUSB0"; /* 固定的linux串口设备文件 */ user_content_t *my_com_conf = my_malloc(sizeof(user_content_t));/* 串口配置 */ /* 串口部份 动态变量 */ #ifdef MODULE_SERIAL char buffer_com[MAXLEN]; /* 串口缓冲区 */ char *buffer_com_p=buffer_com; int buffer_com_data_size=0; #endif /* 蓝牙部份 */ #ifdef MODULE_BLUETOOTH struct sockaddr_rc blue_rem_addr = {0}; char blue_buffer[MAXLEN],blue_sender_MAC[18]; int blue_fd,blue_fd_client,blue_bytes_read; socklen_t blue_opt=sizeof(blue_rem_addr); user_content_t *blue_user_content; #endif /* args参数 */ char *PORT; if (argc != 2) { fprintf(stderr, "usage: %s listen-port\n", argv[0]); return 1; } PORT = argv[1]; #ifdef MODULE_SERIAL /* 打开串口,须root权限 */ if(NULL==(my_com_conf=open_com(com_devicename))) { printf("error open com!\n"); return 1; } #endif #ifdef MODULE_BLUETOOTH /* 打开蓝牙 */ blue_fd=create_bluetooth_socket(); if(blue_fd<0) { printf("error bluetooth fd is -1\n"); return -1; } listen(blue_fd, 1); #endif //initialise all client_socket[] to 0 so not checked for (i = 0; i < max_clients; i++) { client_socket[i] = 0; } if ((master_socket = create_server_socket("0.0.0.0", PORT)) < 0) { printf("error create socket fd\n"); return 1; } //set master socket to allow multiple connections , this is just a good habit, it will work without this if (setsockopt(master_socket, SOL_SOCKET, SO_REUSEADDR, (char *) &opt, sizeof(opt)) < 0) { perror("setsockopt"); exit(EXIT_FAILURE); } //try to specify maximum of 3 pending connections for the master socket if (listen(master_socket, 3) < 0) { perror("listen"); exit(EXIT_FAILURE); } printf("Listening on port %s \n", PORT); //accept the incoming connection addrlen = sizeof(address); puts("Waiting for connections ..."); while (TRUE) { //clear the socket set FD_ZERO(&readfds); //add master socket to set FD_SET(master_socket, &readfds); max_sd = master_socket; #ifdef MODULE_SERIAL FD_SET(my_com_conf->fd,&readfds); max_sd = max_sd>my_com_conf->fd?max_sd:my_com_conf->fd; #endif #ifdef MODULE_BLUETOOTH FD_SET(blue_fd,&readfds); max_sd = max_sd>blue_fd?max_sd:blue_fd; #endif //add child sockets to set for (i = 0; i < max_clients; i++) { //socket descriptor sd = client_socket[i]; //if valid socket descriptor then add to read list if (sd > 0) FD_SET(sd, &readfds); //highest file descriptor number, need it for the select function if (sd > max_sd) max_sd = sd; } //wait for an activity on one of the sockets , timeout is NULL , so wait indefinitely activity = select(max_sd + 1, &readfds, NULL, NULL, NULL); if ((activity < 0) && (errno != EINTR)) { printf("select error"); } //If something happened on the master socket , then its an incoming connection if (FD_ISSET(master_socket, &readfds)) { if ((new_socket = accept(master_socket, (struct sockaddr *) &address, (socklen_t*) &addrlen)) < 0) { perror("accept"); exit(EXIT_FAILURE); } //add new socket to array of sockets for (i = 0; i < max_clients; i++) { //if position is empty, create new one if (client_socket[i] == 0) { client_socket[i] = new_socket; // 初始化buffer buffer_p[i] = buffer[i]; memset(buffer_p[i], 0, MAXLEN); buffer_data_size[i] = 0; printf("accepted #%d client\n", i); break; } } } #ifdef MODULE_SERIAL // 串口读 if (FD_ISSET(my_com_conf->fd, &readfds)) { /* 非阻塞读取 */ valread=sp_nonblocking_read(my_com_conf->com_port,buffer_com_p+buffer_com_data_size,MAXLEN); if(valread<0) { printf("read data from com error: %d\n",valread); return 1; buffer_com_data_size=0; buffer_com_p=buffer_com; } else { buffer_com_data_size+=valread; /* 读完所有数据,串口数据包必须以\r\n结尾 */ if(buffer_com[buffer_com_data_size-2]==13 && buffer_com[buffer_com_data_size-1]==10) { printf("- - - - - - - - - -\nread from COM ok\n"); buffer_com_p[buffer_com_data_size]=0; my_contents[MAXCLIENTS]=new_user_content_from_str(buffer_com,com_devicename,get_direction(buffer_com)); if(!my_contents[MAXCLIENTS]) { printf("invalid packet!\n"); } else { printf(" %s",com_devicename); redirect_from_user_content(my_contents[MAXCLIENTS]); } my_free(my_contents[MAXCLIENTS]); /* reset buffer offset */ buffer_com_data_size=0; buffer_com_p=buffer_com; } } } #endif #ifdef MODULE_BLUETOOTH // 蓝牙读 if(FD_ISSET(blue_fd,&readfds)) { // accept one connection blue_fd_client = accept(blue_fd, (struct sockaddr *)&blue_rem_addr, &blue_opt); ba2str( &blue_rem_addr.rc_bdaddr, blue_sender_MAC ); // read data from the client blue_bytes_read = read(blue_fd_client, blue_buffer, sizeof(blue_buffer)); if( blue_bytes_read > 0 ) { printf("- - - - - - - - - -\nread from bluetooth ok\n"); blue_buffer[blue_bytes_read]=0; blue_user_content=new_user_content_from_str(blue_buffer,blue_sender_MAC,get_direction(blue_buffer)); if(!blue_user_content) { printf("invalid packet!\n"); } else { printf(" %s",blue_sender_MAC); redirect_from_user_content(blue_user_content); } my_free(blue_user_content); } else { printf("bluetooth recv data error!\n"); } // close connection close(blue_fd_client); } #endif // 局域网ip读 for (i = 0; i < max_clients; i++) { sd = client_socket[i]; if (FD_ISSET(sd, &readfds)) { //Check if it was for closing , and also read the incoming message if ((valread = read(sd, buffer_p[i] + buffer_data_size[i], MAXLEN)) == 0) { //Somebody disconnected , get his details and print buffer[i][buffer_data_size[i]] = 0; getpeername(sd, (struct sockaddr*) &address, (socklen_t*) &addrlen); printf( "- - - - - - - - - -\nread %d bytes from LAN client\n", buffer_data_size[i]); //Close the socket and mark as 0 in list for reuse close(sd); client_socket[i] = 0; /* convert port(interger) to char* */ sprintf(itoa_buffer, "%d", ntohs(address.sin_port)); header = get_header_ipv4(inet_ntoa(address.sin_addr), itoa_buffer); /* create relay struct: from LAN ip, to serial */ my_contents[i] = new_user_content_from_str(buffer[i], header, get_direction(buffer[i])); if (!my_contents[i]) { printf("invalid packet!\n"); } else { printf(" %s", header); redirect_from_user_content(my_contents[i]); } my_free(header); my_free(my_contents[i]); } else { /* 累加数据 */ buffer_data_size[i] += valread; } } } } #ifdef MODULE_SERIAL sp_close(my_com_conf->com_port); sp_free_port(my_com_conf->com_port); sp_free_config(my_com_conf->com_conf); my_free(my_com_conf); #endif #ifdef MODULE_BLUETOOTH close(blue_fd); #endif close(master_socket); printf("exit..\n"); return 0; }