示例#1
0
文件: flock_hl.c 项目: scrime/FoB
flock_t
flock_hl_open (char * device,
               int number_of_birds,
               flock_bird_record_mode_t mode,
               int group_mode,
               int stream_mode)
{
  flock_t flock;
  int success;

  flock_init ();

  flock = flock_open (device, O_NONBLOCK, number_of_birds);
  if (flock == NULL)
    {
      REPORT (perror ("flock_hl_open: open failed"));
      return NULL;
    }

  success = flock_check_status (flock) &&
    flock_auto_configure (flock) &&
    flock_set_record_mode_all_birds (flock, mode) &&
    flock_set_group_mode (flock, group_mode);

  if (!success)
    {
      REPORT (fprintf (stderr, "Cannot initialize flock.\n"));
      return NULL;
    }

  flock_set_stream_mode (flock, stream_mode);

  return flock;
}
示例#2
0
int
flock_range_in_steps (int fd, int is_set, short l_type,
                      int start, int end, int step)
{
        int         ret = 0;
        int         i = 0;
        struct      flock f = {0,};

        for (i = start; i+step < end; i += step) {
                flock_init (&f, l_type, i, step);
                ret = fcntl (fd, (is_set) ? F_SETLKW : F_GETLK, &f);
                if (ret) {
                        perror ("fcntl");
                        goto out;
                }
        }
out:
        return ret;
}
示例#3
0
int main (int argc, char **argv)
{
    int fd = -1;
    int ret = 1;
    char *fname = NULL;
    struct flock f = {0,};
    struct flock cp = {0,};

    if (argc < 2)
        goto out;

    fname = argv[1];
    fd = open (fname, O_RDWR);
    if (fd == -1) {
        perror ("open");
        goto out;
    }

    flock_init (&f, F_WRLCK, 0, 3);
    flock_cp (&cp, &f);
    ret = fcntl (fd, F_SETLK, &f);
    if (ret) {
        perror ("fcntl");
        goto out;
    }
    if (!are_flocks_sane (&f, &cp)) {
        ret = 1;
        goto out;
    }

    flock_init (&f, F_WRLCK, 3, 3);
    flock_cp (&cp, &f);
    ret = fcntl (fd, F_SETLK, &f);
    if (ret) {
        perror ("fcntl");
        goto out;
    }
    if (!are_flocks_sane (&f, &cp)) {
        ret = 1;
        goto out;
    }

    flock_init (&f, F_WRLCK, 3, 3);
    flock_cp (&cp, &f);
    ret = fcntl (fd, F_GETLK, &f);
    if (ret) {
        perror ("fcntl");
        return 1;
    }
    GETLK_OWNER_CHECK (f, cp, out);

    flock_init (&f, F_RDLCK, 3, 3);
    flock_cp (&cp, &f);
    ret = fcntl (fd, F_GETLK, &f);
    if (ret) {
        perror ("fcntl");
        return 1;
    }
    GETLK_OWNER_CHECK (f, cp, out);

out:
    if (fd != -1)
        close (fd);
    return ret;
}
示例#4
0
文件: raw.c 项目: scrime/FoB
int
main (int argc, char ** argv)
{
  unsigned char command;
  flock_response_t response;
  flock_t flock;
  int i;

  flock_init ();

  /* Opening flock with non blocking behavior. */
  fprintf (stderr, "Opening flock.\n");
  flock = flock_open ("/dev/ttyS0", O_NDELAY, NUMBER_OF_BIRDS);

  if (flock == NULL)
    {
      fprintf (stderr, "main: flock_make failed\n");
      exit (EXIT_FAILURE);
    }

  /* Examining whole flock status. */
  fprintf (stderr, "Getting flock status.\n");
  flock_command_display (stderr, flock_status);
  flock_write (flock, flock_status, 2);

  sleep (1);

  /* Trying super-expanded mode response.  (O_NDELAY (non blocking
     mode) required.) */
  fprintf (stderr, "  Trying super-expanded mode.\n");
  response = flock_read (flock, 126);

  if (response->size == -1)
    {
      /* Trying expanded mode response-> */
      fprintf (stderr, "  Trying expanded mode.\n");
      response = flock_read (flock, 30);
    }

  if (response->size == -1)
    {
      /* Trying normal mode response-> */
      fprintf (stderr, "  Trying normal mode.\n");
      response = flock_read (flock, 14);
    }

  if (response->size <= 0)
    {
      fprintf (stderr,
	       "main: getting flock status failed (are birds flying?)\n");
      return EXIT_FAILURE;
    }

  display_response (response);

  /* Getting status of master bird. */

#define MASTER_ADDRESS 1

  fprintf (stderr, "Getting status of bird #%d.\n", MASTER_ADDRESS);

  command = FLOCK_COMMAND_RS232_TO_FBB | MASTER_ADDRESS;

  flock_command_display (stderr, &command);
  flock_write (flock, &command, 1);

  flock_command_display (stderr, bird_status);
  flock_write (flock, bird_status, 2);

  sleep (1);
  response = flock_read (flock, 2);

  if (response->size <= 0)
    {
      fprintf (stderr,
	       "main: getting status of bird #%d failed (is bird flying?)\n",
	       MASTER_ADDRESS);
      return EXIT_FAILURE;
    }

  display_response (response);

  /* Getting status of slave bird. */

#define SLAVE_ADDRESS 2

  fprintf (stderr, "Getting status of bird #%d.\n", SLAVE_ADDRESS);

  command = FLOCK_COMMAND_RS232_TO_FBB | SLAVE_ADDRESS;

  flock_command_display (stderr, &command);
  flock_write (flock, &command, 1);

  flock_command_display (stderr, bird_status);
  flock_write (flock, bird_status, 2);

  sleep (1);
  response = flock_read (flock, 2);

  if (response->size <= 0)
    {
      fprintf (stderr,
	       "main: getting status of bird #%d failed (is bird flying?)\n",
	       SLAVE_ADDRESS);
      return EXIT_FAILURE;
    }

  display_response (response);

  /* Auto configuration. */
  fprintf (stderr, "Auto configuring.\n");
  flock_command_display (stderr, auto_config);
  flock_write (flock, auto_config, 3);
  sleep (1);

  /* Examining group mode. */
  fprintf (stderr, "Getting current group mode.\n");
  flock_command_display (stderr, examine_group_mode);
  flock_write (flock, examine_group_mode, 2);

  sleep (1);
  response = flock_read (flock, 1);
  display_response (response);

  /* Setting group mode. */
  fprintf (stderr, "Setting group mode.\n");
  flock_command_display (stderr, set_group_mode);
  flock_write (flock, set_group_mode, 3);

  /* Getting records. */

#define NUMBER_OF_RECORDS 10
#define SIZEOF_RECORD 26

  fprintf (stderr, "Getting %d records.\n", NUMBER_OF_RECORDS);

  for (i = 0; i < NUMBER_OF_RECORDS; i++)
    {
      struct flock_bird_record_s rec;
      double t1, t2;

      flock_command_display (stderr, point);
      flock_write (flock, point, 1);

      t1 = now ();

      /* Active loop. (Generally bad, especially when device was
         opened in non-blocking mode, but this is only a test.) */
      while (1)
	{
	  response = flock_read (flock, SIZEOF_RECORD);

	  if (response == NULL)
	    {
	      perror ("flock_read");
	      exit (EXIT_FAILURE);
	    }

	  if (response->size != -1)
	    break;
	}

      t2 = now ();

      fprintf (stderr, "waited %.0f ms\n", t2 - t1);

      display_response (response);

      /* Position of master. */
      flock_bird_record_fill (&rec,
			      response->data,
			      flock_bird_record_mode_position_angles);
      display_record (&rec);

      /* Position of slave. */
      flock_bird_record_fill (&rec,
			      response->data + (SIZEOF_RECORD / 2),
			      flock_bird_record_mode_position_angles);
      display_record (&rec);
    }

  /* Closing flock. */
  fprintf (stderr, "Closing flock.\n");
  flock_close (flock);

  return EXIT_SUCCESS;
}
示例#5
0
void startup(int initial_flock_size){
    flock_init();
    r   = apop_rng_alloc(972);
    for(int i=0; i< initial_flock_size; i++)
        add_to_flock(new_chick(NULL));
}