コード例 #1
0
ファイル: usage.c プロジェクト: tjohann/mydriver
int
main(int argc, char *argv[])
{
	struct client_config client;
	memset(&client, 0, sizeof(client));

	int c;
	while ((c = getopt(argc, argv, "rwp:b:a:h")) != -1) {
		switch (c) {
		case 'r':
			client.mode |= MODE_READ;
			break;
		case 'w':
			client.mode |= MODE_WRITE;
			break;
		case 'p':
			client.pin_irq = atoi(optarg);
			break;
		case 'b':
			client.adapter_nr = atoi(optarg);
			break;
		case 'a':
			client.addr = strtoul(optarg, NULL, 16);
			break;
		case 'h':
		usage();
			break;
		default:
			fprintf(stderr, "ERROR: no valid argument\n");
			usage();
		}
	}

	print_client_config(&client);

	if (!is_client_config_valid(&client)) {
		fprintf(stderr,"client config is not valid\n");
		usage();
	}

	int fd = open_device(client.mode);
	if (fd == -1)
		usage();

	int ret = ioctl(fd, IOCTL_SET_I2C_CLIENT , &client);
	if (ret == -1) {
		fprintf(stderr, "could not set client values\n");
		return EXIT_FAILURE;
	}

	if (work_mode(fd, client.mode) == -1)
		usage();

	close(fd);
	return EXIT_SUCCESS;
}
コード例 #2
0
ファイル: dgclient.c プロジェクト: raaghu91/NP
int main(int argc, char **argv)
{

  int n,sockfd, on=1,i,j,maxfdp,optval=1;
  char recvline[LINE_MAX], in_packet[PACKET_LEN];	
  client_config_t config;
  int num_ifi = 0;
  pthread_t tid;
  thread_arg* arg;
  bool done = FALSE, is_probe = FALSE, is_error = FALSE;
  unsigned int ack_seq, seq, timestamp;
  unsigned short curr_win;

  circ_buffer_t rcv_buffer;

  ifi_t *ifi_array[IFI_MAX];

  read_client_config("client.in", &config);
  print_client_config(&config);

  num_ifi = get_ifi(ifi_array);
  print_ifi(ifi_array, num_ifi);

  srand(config.seed);
  init_circular_buffer(&rcv_buffer, config.window_size);
  
  rtt_init(&rttinfo);

  if (connection_setup(&sockfd, ifi_array, num_ifi, &rcv_buffer, &config) < 0)
    err_sys("[Error] Connection Setup Error, Terminating..\n");

  /* TODO: Recv ACK and connect to the new port */

  arg = (thread_arg*)calloc(1, sizeof(thread_arg));
  arg->rcv_buf = &rcv_buffer;
  arg->config = &config;
  arg->sockfd = sockfd;
  Pthread_create(&tid, NULL, &consumer_thread, arg);

  /* Below is the Producer Logic which reads from the socket and fills 
   * up the receive Buffer.
   */
  while (!done)
  {
    if ((n = read(sockfd, in_packet, PACKET_LEN)) < 0)
    {
      if (errno == EINTR) 
        continue;
      else
        err_sys("[Error] Unknown Read Error");
    }

    packet_info_t *pkt_info = get_packet_info(in_packet, n);

    if (!IS_DATA(pkt_info) && !(is_probe = IS_PROBE(pkt_info))
          && !(is_error = IS_ERR(pkt_info)))
    {
      free_pkt_info(pkt_info);
      continue;
    }

    if (consume_random_packet(pkt_info, config.prob_loss, TRUE))
    {
      free_pkt_info(pkt_info);
      continue;
    }

    if(IS_EOF(pkt_info) || is_error)
      done = TRUE;

    Pthread_mutex_lock(&buf_mutex);
    /* Special Handling for Probes & Errors, send an ACK, don't store in buffer */
    if(!is_probe && !is_error)
    {
      write_to_buffer(&rcv_buffer , pkt_info);
    }
    /* Save off these values as we are releasing the lock below */
    curr_win = window_size(&rcv_buffer); 
    ack_seq = NEXT_ACK(&rcv_buffer);
    seq = pkt_info->seq;
    timestamp = pkt_info->timestamp;

    Pthread_mutex_unlock(&buf_mutex);

    if(is_probe)
      printf("[Info] Persist Timer Response [Ack:%u] [Window Size:%hu]\n", ack_seq, curr_win);
    else
      printf("[Info] Received [Seq: %u] Responding with [Ack:%u] [Window Size:%hu]\n", 
          seq, ack_seq, curr_win);

    send_ack(sockfd, curr_win, seq, ack_seq, timestamp, config.prob_loss);

  }
  pthread_exit(NULL);
}