void net_hpib(int sfd, char *cmd, bool reply) { int n; sprintf(buf, "%s\r\n", cmd); write(sfd, buf, strlen(buf)); if (reply) { n = net_client_read(sfd, buf, 64, TRUE); buf[n] = 0; printf("%s", buf); } }
int net_poll(net_server *s) { int num_events = epoll_wait(s->epfd, s->events, s->max_events, -1); if(num_events == -1 && errno != EINTR) { log_fatal("An error occured while waiting for fd events: %s.", strerror(errno)); } int i, num_read; for(i = 0; i < num_events; i++) { net_file_event *fe = (net_file_event *) s->events[i].data.ptr; if(fe->type == net_file_event_type_client) { if(s->events[i].events & EPOLLIN) { if((num_read = net_client_read((net_client *)fe->ptr)) <= 0) { if(num_read == -1) log_error("Failed to read data from client. Closing client.\n"); net_client_close(s, (net_client *)fe->ptr); net_file_event_destroy(fe); continue; } } if (s->events[i].events & EPOLLOUT) { if(net_client_write((net_client *)fe->ptr) == -1) { log_error("Failed to write data from client. Closing client.\n"); net_client_close(s, (net_client *)fe->ptr); net_file_event_destroy(fe); continue; } } if (s->events[i].events & EPOLLHUP || s->events[i].events & EPOLLERR) { net_client_close(s, (net_client *)fe->ptr); net_file_event_destroy(fe); } } else if(fe->type == net_file_event_type_server && s->events[i].events & EPOLLIN) { net_client *c = net_server_accept((net_server *)fe->ptr); if(!c) { log_info("Failed to accept client connection: %s\n", strerror(errno)); } else { log_info("Client %s:%d connected\n", c->ip, c->port); } } } return num_events; }
int main(int argc, char *argv[]) { int i, j, sfd, e; char *bp; u1_t *s, *s2; bool rng, fast_mode = FALSE; i=1; if ((i < argc) && (strcmp(argv[i], "-f") == 0)) { fast_mode = TRUE; i++; } sfd = net_connect(NET_HPIB, CLIENT, (i < argc)? argv[i]:HOST, HPIB_TCP_PORT); if (fast_mode) { #ifndef DO_CALC printf("WARNING: remember, calculation currently compiled out\n"); #endif net_hpib(sfd, "tb2", FALSE); // tb2 is a virtual cmd we define for fast mode for (i=0; i<NLOOP_FAST; i++) { s4_t n1n2; double ti; bp = buf; s = (u1_t *) buf; net_client_read(sfd, bp, HPIB_BYTES_PER_PKT, FALSE); #ifdef DO_CALC for (j=0; j<HPIB_MEAS_PER_FAST_PKT; j++) { // ARM is little-endian n1n2 = (s4_t) ((s[1] << 8) | (u1_t) s[0]); s += 2; ti = ((double) n1n2 / 256.0) * 5.0e-9; rng = ((ti < 98.0e-9) || (ti > 99.9e+9))? TRUE:FALSE; if (rng) printf("%d: out of range:\n", j); if (rng) { if (ti < 1.0e-8) { printf("%1.2f ns\n", ti * 1.0e8); } else { printf("%2.2f ns\n", ti * 1.0e9); } } } #endif } } else { // take a couple of measurements the regular way net_hpib(sfd, "md2", TRUE); net_hpib(sfd, "mr", TRUE); // switch to binary mode net_hpib(sfd, "tb1", FALSE); for (i=0; i<NLOOP; i++) { u4_t n; u1_t n0st, n1n2h, n1n2l, n0h, n0l; s4_t n1n2, n0; double ti; bp = buf; s = (u1_t *) buf; net_client_read(sfd, bp, HPIB_BYTES_PER_PKT, FALSE); for (j=0; j<HPIB_MEAS_PER_PKT; j++) { e=0; n0st = s[0]; n1n2h = s[1]; n1n2l = s[2]; n0h = s[3]; n0l = s[4]; s += 5; if (isActive(N0ST_PLL_OOL, n0st)) { printf("PLL UNLOCKED\n"); e=1; } if (isActive(N0ST_N0_OVFL, n0st)) { printf("N0 OVFL\n"); e=1; } n1n2 = ((n0st & N0ST_N1N2) << 16) | (n1n2h << 8) | n1n2l; // convert from 18-bit 2s complement if (n1n2 >= 0x20000) { n1n2 = n1n2 - 0x40000; } n0 = (n0h << 8) | n0l; if ((n0st & N0ST_N0_POS) == 0) { n0 = -n0; } ti = (((double) n1n2 / 256.0) + (double) n0) * 5.0e-9; rng = ((ti < 99.0e-9) || (ti > 99.9e+9))? TRUE:FALSE; if (rng) { printf("out of range: 0x%02x\n", n0st & N0ST_STATUS); e=1; } if (rng) { if (ti < 1.0e-8) { printf("%1.2f ns\n", ti * 1.0e8); } else { printf("%2.2f ns\n", ti * 1.0e9); } } if (e) { s2 = (u1_t *) &buf[5]; printf("%d %d/%d: 0x%x %d %d %d %d 0x%x %d %d %d %d\n", i, j, HPIB_MEAS_PER_PKT, s[-5], s[-4], s[-3], s[-2], s[-1], s2[-5], s2[-4], s2[-3], s2[-2], s2[-1]); } } } } // switch out of binary mode net_hpib(sfd, "tb0md1", FALSE); }