Пример #1
0
int receive_data(int fd, int revents, void *cb_data)
{
	struct sr_dev_inst *sdi;
	struct dev_context *devc;
	struct dmm_info *dmm;
	void *info;

	(void)fd;

	if (!(sdi = cb_data))
		return TRUE;

	if (!(devc = sdi->priv))
		return TRUE;

	dmm = (struct dmm_info *)sdi->driver;

	if (revents == G_IO_IN) {
		/* Serial data arrived. */
		info = g_malloc(dmm->info_size);
		handle_new_data(sdi, info);
		g_free(info);
	} else {
		/* Timeout; send another packet request if DMM needs it. */
		if (dmm->packet_request && (req_packet(sdi) < 0))
			return FALSE;
	}

	if (sr_sw_limits_check(&devc->limits))
		sdi->driver->dev_acquisition_stop(sdi);

	return TRUE;
}
Пример #2
0
static void handle_new_data(struct sr_dev_inst *sdi, void *info)
{
	struct dmm_info *dmm;
	struct dev_context *devc;
	int len, i, offset = 0;
	struct sr_serial_dev_inst *serial;

	dmm = (struct dmm_info *)sdi->driver;

	devc = sdi->priv;
	serial = sdi->conn;

	/* Try to get as much data as the buffer can hold. */
	len = DMM_BUFSIZE - devc->buflen;
	len = serial_read_nonblocking(serial, devc->buf + devc->buflen, len);
	if (len == 0)
		return; /* No new bytes, nothing to do. */
	if (len < 0) {
		sr_err("Serial port read error: %d.", len);
		return;
	}
	devc->buflen += len;

	/* Now look for packets in that data. */
	while ((devc->buflen - offset) >= dmm->packet_size) {
		if (dmm->packet_valid(devc->buf + offset)) {
			handle_packet(devc->buf + offset, sdi, info);
			offset += dmm->packet_size;

			/* Request next packet, if required. */
			if (!dmm->packet_request)
				break;
			if (dmm->req_timeout_ms || dmm->req_delay_ms)
				devc->req_next_at = g_get_monotonic_time() +
					dmm->req_delay_ms * 1000;
			req_packet(sdi);
		} else {
			offset++;
		}
	}

	/* If we have any data left, move it to the beginning of our buffer. */
	for (i = 0; i < devc->buflen - offset; i++)
		devc->buf[i] = devc->buf[offset + i];
	devc->buflen -= offset;
}
Пример #3
0
int tftp_get_file(char *hostip,char *filename ,char *buf){

	char  mode[12] = "octet";
	int port = 69;
	char opcode =1;
	int sock, server_len, len, opt;	//,n;
	//struct hostent *host;		/*for host information */
	 struct sockaddr_in server;	//, client; /*the address structure for both the server and client */
	/*Create the socket, a -1 will show us an error */

	if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
	{
		printf ("Client: Socket could not be created");
		return 0;
	}
	 printf ("tftp_get_file\n");
	
	/*set the address values for the server */
	memset (&server, 0, sizeof (server));	/*Clear the structure */
	server.sin_family = AF_INET;	/*address family for TCP and UDP */
	
	server.sin_addr.s_addr=inet_addr(hostip);
	//server.sin_addr.s_addr = htonl (INADDR_ANY); /*use any address */
	server.sin_port = htons (port);	/*pick a free port */

	server_len = sizeof (server);	/*get the length of the server address */

	//memset (buf, 0, BUFSIZ);	/*clear the buffer */
	/* this is the first request message */
	len = req_packet (opcode, filename, mode, buf);
	printf ("the len is %d\n",len);
	//printf ("the server.sin_addr is %s\n",server.sin_addr.s_addr);
	if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, server_len) != len)
	{
		perror ("Client: sendto has returend an error");
		exit (ERROR);
	}
	strncpy (mode, "octet", sizeof (mode) - 1);
	tget (filename, server, mode, sock);
	 

}
Пример #4
0
int
main (int argc, char **argv)
{
  /*local variables */
  extern char *optarg;
  int sock, server_len, len, opt;	//,n;
  char opcode, filename[196], mode[12] = "octet";
  struct hostent *host;		/*for host information */
  struct sockaddr_in server;	//, client; /*the address structure for both the server and client */
  FILE *fp;			/*a pointer to a file that the client will send or get from the server */

  if (argc < 2)
    {
      help (argv[0]);
      return 0;
    }
  if (!(host = gethostbyname (argv[1])))
    {
      perror ("Client could not get host address information");
      exit (2);
    }

/* All of the following deals with command line switches */
  while ((opt = getopt (argc, argv, "dnoh:P:p:g:l:w:")) != -1)	/* this function is handy */
    {
      switch (opt)
	{
	case 'd':		/* debug mode (no opts) */
	  debug = 1;
	  break;
	case 'P':		/* Port (opt required) */
	  port = atoi (optarg);
	  if (debug)
	    {
	      printf ("Client: The port number is: %d\n", port);
	    }
	  break;
	case 'p':		/* put a file on the server */
	  strncpy (filename, optarg, sizeof (filename) - 1);
	  opcode = WRQ;
	  fp = fopen (filename, "r");	/*opened the file for reading */
	  if (fp == NULL)
	    {
	      printf ("Client: file could not be opened\n");
	      return 0;
	    }
	  if (debug)
	    {
	      printf ("Client: The file name is: %s and can be read",
		      filename);
	    }
	  fclose (fp);
	  break;
	case 'g':		/*get a file from the server */
	  strncpy (filename, optarg, sizeof (filename) - 1);
	  opcode = RRQ;
	  fp = fopen (filename, "w");	/*opened the file for writting */
	  if (fp == NULL)
	    {
	      printf ("Client: file could not be created\n");
	      return 0;
	    }
	  if (debug)
	    {
	      printf ("Client: The file name is: %s and it has been created",
		      filename);
	    }
	  fclose (fp);
	  break;
	case 'w':		/* Get the window size */
	  ackfreq = atoi (optarg);
	  if (debug)
	    {
	      printf ("Client: Window size is: %i\n", ackfreq);
	    }
	  //ackfreq = atoi (optarg);
	  if (ackfreq > MAXACKFREQ)
	    {
	      printf
		("Client: Sorry, you specified an ack frequency higher than the maximum allowed (Requested: %d Max: %d)\n",
		 ackfreq, MAXACKFREQ);
	      return 0;
	    }
	  else if (w_size == 0)
	    {
	      printf ("Client: Sorry, you have to ack sometime.\n");
	      return 0;
	    }
	  break;
	case 'l':		/* packet length */
	  datasize = atoi (optarg);
	  if (debug)
	    {
	      printf ("Client: Packet length is: %i bytes\n", datasize);
	    }
	  if (datasize > MAXDATASIZE)
	    {
	      printf
		("Client: Sorry, you specified a data size higher than the maximum allowed (Requested: %d Max: %d)\n",
		 datasize, MAXDATASIZE);
	      return 0;
	    }
	  break;
	case 'h':		/* Help (no opts) */
	  help (argv[0]);
	  return (0);
	  break;
	case 'o':
	  strncpy (mode, "octet", sizeof (mode) - 1);
	  if (debug)
	    {
	      printf ("Client: The mode is set to octet\n");
	    }
	  break;
	case 'n':
	  strncpy (mode, "netascii", sizeof (mode) - 1);
	  if (debug)
	    {
	      printf ("Client: The mode is set to netascii\n");
	    }
	  break;
	default:		/* everything else */
	  help (argv[0]);
	  return (0);
	  break;
	}			//end of switch
    }				//end of while loop

/*check for valid input*/
  if (argc < 5 || argc > 12)
    {
      printf ("Client: wrong number of arguments: %d\n", argc);
      help (argv[0]);
      exit (ERROR);
    }

/* Done dealing with switches and the command line*/


  /*Create the socket, a -1 will show us an error */
  if ((sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
    {
      printf ("Client: Socket could not be created");
      return 0;
    }


/*set the address values for the server */
  memset (&server, 0, sizeof (server));	/*Clear the structure */
  server.sin_family = AF_INET;	/*address family for TCP and UDP */
  memcpy (&server.sin_addr, host->h_addr, host->h_length);	/*set address of server taken from gethostbyname function */
  //server.sin_addr.s_addr = htonl (INADDR_ANY); /*use any address */
  server.sin_port = htons (port);	/*pick a free port */


  server_len = sizeof (server);	/*get the length of the server address */

  if (debug)
    printf ("Client: size of server_address is : %d bytes\n", server_len);

  memset (buf, 0, BUFSIZ);	/*clear the buffer */
  /* this is the first request message */
  len = req_packet (opcode, filename, mode, buf);

  if (debug)
    printf ("Client: The request packt's length is: %d bytes\n", len);

  if (sendto (sock, buf, len, 0, (struct sockaddr *) &server, server_len) !=
      len)
    {
      perror ("Client: sendto has returend an error");
      exit (ERROR);
    }
  if (debug)
    ip_port (server);
  switch (opcode)
    {
    case RRQ:			/*read from the server, download */
      tget (filename, server, mode, sock);
      break;
    case WRQ:			/*write to the server, upload */
      tsend (filename, server, mode, sock);
      break;
    default:
      printf ("Invalid opcode detected. Ignoring packet.");
    }
  close (sock);			/* close the socket */
  return 1;
}				//end of main