VALUE interfacekit_change_trigger_set(VALUE self, VALUE index, VALUE rate_thresh) {
  PhidgetInfo *info = device_info(self);
  InterfaceKitInfo *ifkit_info = info->type_info;

  // If ratiometric state is not uniform, raise an exception
  if (!interfacekit_ratiometric_state_is_uniform(ifkit_info))
    interfacekit_raise_ratiometric_not_uniform();

  if (TYPE(index) != T_FIXNUM)
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_MUST_BE_FIXNUM);

  if (TYPE(rate_thresh) != T_FIXNUM)
    rb_raise(rb_eTypeError, MSG_CHANGE_TRIG_VALUE_MUST_BE_FIXNUM);

  int index_int = FIX2INT(index);
  if ((ifkit_info->analog_input_count == 0) || (index_int > (ifkit_info->analog_input_count-1)))
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_TOO_HIGH);

  int rate_thresh_int = FIX2INT(rate_thresh);
  // These limits are specified in the phidget documentation (and, just kind of make sense)
  if ((rate_thresh_int < 1) || (rate_thresh_int > 1000))
    rb_raise(rb_eTypeError, MSG_CHANGE_TRIG_EXCEEDS_LIMIT);
  
  ensure(CPhidgetInterfaceKit_setSensorChangeTrigger(
    (CPhidgetInterfaceKitHandle)info->handle, index_int, rate_thresh_int));

  ifkit_info->data_rates[index_int] = 0;
  ifkit_info->sensor_change_triggers[index_int] = rate_thresh_int;

  return rate_thresh;
}
bool Sim_ImgEngine::init(const std::map<std::string, double> &config)
{
#if 0
for (auto item : config)
    std::cout << "key: " << item.first << ", value=" << item.second << std::endl;
#else
   try {
      m_device_num = (int)config.at("device_num");
      m_exposure_us = (int)config.at("exposure_us");
      m_delay_us = (int)config.at("delay_us");
      m_conversion_factor = config.at("conversion_factor");
      m_height_px = (int)config.at("height_px");
      m_width_px = (int)config.at("width_px");
      m_pelSize = (int)config.at("pel_size_um");
      m_binh = (int)config.at("binh");
      m_binv = (int)config.at("binv");
      m_trigger_mode = (int)config.at("trigger_mode");
      m_doubleImage_mode = (int)config.at("doubleImage_mode");
      m_timestamp_mode = (int)config.at("timestamp_mode");
   }
   catch (const std::out_of_range& oor) {
       std::cerr << "Out of Range error: " << oor.what() << '\n';
     }
#endif
   m_Log->LogInfo("Sim driver loaded. ");
   std::cerr << device_info();

   return true;
}
Esempio n. 3
0
static int
devfs_lookup(struct vnode *dvp, char *name, struct vnode *vp)
{
	struct devinfo info;
	int error, i;

	DPRINTF(("devfs_lookup:%s\n", name));

	if (*name == '\0')
		return ENOENT;

	i = 0;
	error = 0;
	info.cookie = 0;
	for (;;) {
		error = device_info(&info);
		if (error) {
			printf("device %s not found\n", name);
			return ENOENT;
		}
		if (!strncmp(info.name, name, MAXDEVNAME))
			break;
		i++;
	}
	vp->v_type = (info.flags & D_CHR) ? VCHR : VBLK;
	if (info.flags & D_TTY)
		vp->v_flags |= VISTTY;

	vp->v_mode = (mode_t)(S_IRUSR | S_IWUSR);
	return 0;
}
VALUE interfacekit_data_rate_set(VALUE self, VALUE index, VALUE rate) {
  PhidgetInfo *info = device_info(self);
  InterfaceKitInfo *ifkit_info = info->type_info;

  // If ratiometric state is not uniform, raise an exception
  if (!interfacekit_ratiometric_state_is_uniform(ifkit_info))
    interfacekit_raise_ratiometric_not_uniform();

  if (TYPE(index) != T_FIXNUM)
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_MUST_BE_FIXNUM);

  if (TYPE(rate) != T_FIXNUM)
    rb_raise(rb_eTypeError, MSG_DATA_RATE_VALUE_MUST_BE_FIXNUM);

  int index_int = FIX2INT(index);
  if ((ifkit_info->analog_input_count == 0) || (index_int > (ifkit_info->analog_input_count-1)))
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_TOO_HIGH);

  int rate_int = FIX2INT(rate);
  if ((rate_int > ifkit_info->data_rates_min[index_int]) || (rate_int < ifkit_info->data_rates_max[index_int]))
    rb_raise(rb_eTypeError, MSG_DATA_RATE_EXCEEDS_LIMIT);
  
  ensure(CPhidgetInterfaceKit_setDataRate(
    (CPhidgetInterfaceKitHandle)info->handle, index_int, rate_int));

  ifkit_info->data_rates[index_int] = rate_int;
  ifkit_info->sensor_change_triggers[index_int] = 0;

  return rate;
}
Esempio n. 5
0
VALUE stepper_initialize(VALUE self, VALUE serial) {
  PhidgetInfo *info = device_info(self);
  CPhidgetStepperHandle stepper = 0;
  ensure(CPhidgetStepper_create(&stepper));
  info->handle = (CPhidgetHandle)stepper;
  return rb_call_super(1, &serial);
}
Esempio n. 6
0
/*
 * @vp: vnode of the directory.
 */
static int
devfs_readdir(struct vnode *vp, struct file *fp, struct dirent *dir)
{
	struct devinfo info;
	int error, i;

	DPRINTF(("devfs_readdir offset=%d\n", fp->f_offset));

	i = 0;
	error = 0;
	info.cookie = 0;
	do {
		error = device_info(&info);
		if (error)
			return ENOENT;
	} while (i++ != fp->f_offset);

	dir->d_type = 0;
	if (info.flags & D_CHR)
		dir->d_type = DT_CHR;
	else if (info.flags & D_BLK)
		dir->d_type = DT_BLK;
	strlcpy((char *)&dir->d_name, info.name, sizeof(dir->d_name));
	dir->d_fileno = fp->f_offset;
//	dir->d_namlen = strlen(dir->d_name);

	DPRINTF(("devfs_readdir: %s\n", dir->d_name));
	fp->f_offset++;
	return 0;
}
VALUE interfacekit_output_set(VALUE self, VALUE index, VALUE is_on) {
  PhidgetInfo *info = device_info(self);
  InterfaceKitInfo *ifkit_info = info->type_info;

  if (TYPE(index) != T_FIXNUM)
    rb_raise(rb_eTypeError, MSG_OUTPUT_INDEX_MUST_BE_FIXNUM);

  int index_int = FIX2INT(index);
  if ((ifkit_info->digital_output_count == 0) || (index_int > (ifkit_info->digital_output_count-1)))
    rb_raise(rb_eTypeError, MSG_OUTPUT_INDEX_TOO_HIGH);

  int phidget_bool;
  
  if (TYPE(is_on) == T_TRUE) 
    phidget_bool = PTRUE;
  else if (TYPE(is_on) == T_FALSE)
    phidget_bool = PFALSE;
  else
    rb_raise(rb_eTypeError, MSG_OUTPUT_VALUE_MUST_BE_BOOL);

  ensure(CPhidgetInterfaceKit_setOutputState(
    (CPhidgetInterfaceKitHandle)info->handle, index_int, phidget_bool));

  ifkit_info->digital_output_states[index_int] = phidget_bool;

  return is_on;
}
Esempio n. 8
0
static int show_sysfs_attribs(struct rc_device *rc_dev)
{
	static struct sysfs_names *names, *cur;
	int fd;

	names = find_device(NULL);
	if (!names)
		return -1;
	for (cur = names; cur->next; cur = cur->next) {
		if (cur->name) {
			if (get_attribs(rc_dev, cur->name))
				return -1;
			fprintf(stderr, "Found %s (%s) with:\n",
				rc_dev->sysfs_name,
				rc_dev->input_name);
			fprintf(stderr, "\tDriver %s, table %s\n",
				rc_dev->drv_name,
				rc_dev->keytable_name);
			fprintf(stderr, "\tSupported protocols: ");
			show_proto(rc_dev->supported);
			fprintf(stderr, "\n\t");
			display_proto(rc_dev);
			fd = open(rc_dev->input_name, O_RDONLY);
			if (fd > 0) {
				device_info(fd, "\t");
				show_evdev_attribs(fd);
				close(fd);
			} else {
				printf("\tExtra capabilities: <access denied>\n");
			}
		}
	}
	return 0;
}
Esempio n. 9
0
void CddaLister::Init() {
  cdio_init();
#ifdef Q_OS_DARWIN
  if (!cdio_have_driver(DRIVER_OSX)) {
    qLog(Error) << "libcdio was compiled without support for OS X!";
  }
#endif
  char **devices = cdio_get_devices(DRIVER_DEVICE);
  if (!devices) {
    qLog(Debug) << "No CD devices found";
    return;
  }
  for (; *devices != NULL; ++devices) {
    QString device(*devices);
    QFileInfo device_info(device);
    if (device_info.isSymLink()) {
      device = device_info.symLinkTarget();
    }
#ifdef Q_OS_DARWIN
    // Every track is detected as a separate device on Darwin. The raw disk looks
    // like /dev/rdisk1
    if (!device.contains(QRegExp("^/dev/rdisk[0-9]$"))) {
      continue;
    }
#endif
    if (!devices_list_.contains(device)) {
      devices_list_ << device;
      emit DeviceAdded(device);
    }
  }
}
Esempio n. 10
0
VALUE textled_initialize(VALUE self, VALUE serial) {
  PhidgetInfo *info = device_info(self);
  CPhidgetTextLEDHandle textled = 0;
  ensure(CPhidgetTextLED_create(&textled));
  info->handle = (CPhidgetHandle)textled;
  return rb_call_super(1, &serial);
}
Esempio n. 11
0
VALUE analog_initialize(VALUE self, VALUE serial) {
  PhidgetInfo *info = device_info(self);
  CPhidgetAnalogHandle analog = 0;
  ensure(CPhidgetAnalog_create(&analog));
  info->handle = (CPhidgetHandle)analog;
  return rb_call_super(1, &serial);
}
VALUE frequencycounter_initialize(VALUE self, VALUE serial) {
  PhidgetInfo *info = device_info(self);
  CPhidgetFrequencyCounterHandle frequencycounter = 0;
  ensure(CPhidgetFrequencyCounter_create(&frequencycounter));
  info->handle = (CPhidgetHandle)frequencycounter;
  return rb_call_super(1, &serial);
}
Esempio n. 13
0
/*
 * Get system information
 */
int
sys_info(int type, void *buf)
{
    struct info_memory infomem;
    struct info_timer infotmr;
    struct info_thread infothr;
    struct info_device infodev;
    int err = 0;

    if (buf == NULL || !user_area(buf))
        return EFAULT;

    sched_lock();

    switch (type) {
    case INFO_KERNEL:
        err = umem_copyout(&infokern, buf, sizeof(infokern));
        break;

    case INFO_MEMORY:
        page_info(&infomem);
        kpage_info(&infomem);
        err = umem_copyout(&infomem, buf, sizeof(infomem));
        break;

    case INFO_THREAD:
        if (umem_copyin(buf, &infothr, sizeof(infothr))) {
            err = EFAULT;
            break;
        }
        if ((err = thread_info(&infothr)))
            break;
        infothr.cookie++;
        err = umem_copyout(&infothr, buf, sizeof(infothr));
        break;

    case INFO_DEVICE:
        if (umem_copyin(buf, &infodev, sizeof(infodev))) {
            err = EFAULT;
            break;
        }
        if ((err = device_info(&infodev)))
            break;
        infodev.cookie++;
        err = umem_copyout(&infodev, buf, sizeof(infodev));
        break;

    case INFO_TIMER:
        timer_info(&infotmr);
        err = umem_copyout(&infotmr, buf, sizeof(infotmr));
        break;

    default:
        err = EINVAL;
        break;
    }
    sched_unlock();
    return err;
}
VALUE interfacekit_close(VALUE self) {
  PhidgetInfo *info = device_info(self);

  ensure(CPhidgetInterfaceKit_set_OnInputChange_Handler((CPhidgetInterfaceKitHandle) info->handle, NULL, NULL));
  ensure(CPhidgetInterfaceKit_set_OnSensorChange_Handler((CPhidgetInterfaceKitHandle) info->handle, NULL, NULL));

  return rb_call_super(0,NULL);
}
Esempio n. 15
0
VALUE gps_close(VALUE self) {
  PhidgetInfo *info = device_info(self);

  ensure(CPhidgetGPS_set_OnPositionChange_Handler((CPhidgetGPSHandle)info->handle, NULL, NULL));
 	ensure(CPhidgetGPS_set_OnPositionFixStatusChange_Handler((CPhidgetGPSHandle)info->handle, NULL, NULL));

  return rb_call_super(0,NULL);
}
VALUE interfacekit_sensor_raw(VALUE self, VALUE index) {
  PhidgetInfo *info = device_info(self);
  InterfaceKitInfo *ifkit_info = info->type_info;

  int ret;

  if ((TYPE(index) != T_FIXNUM))
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_MUST_BE_FIXNUM);

  int index_int = FIX2INT(index);
  if ((ifkit_info->analog_input_count == 0) || (index_int > (ifkit_info->analog_input_count-1)))
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_TOO_HIGH);

  ensure(CPhidgetInterfaceKit_getSensorRawValue(
    (CPhidgetInterfaceKitHandle)info->handle,index_int,&ret));

  return INT2FIX(ret);
}
Esempio n. 17
0
static void device_list(int fd, int max_dev)
{
	struct hci_dev_list_req *dl;
	struct hci_dev_req *dr;
	int i;

	dl = malloc(max_dev * sizeof(*dr) + sizeof(*dl));
	if (!dl) {
		perror("Failed to allocate device list memory");
		return;
	}

	memset(dl, 0, max_dev * sizeof(*dr) + sizeof(*dl));
	dl->dev_num = max_dev;

	dr = dl->dev_req;

	if (ioctl(fd, HCIGETDEVLIST, (void *) dl) < 0) {
		perror("Failed to get device list");
		goto done;
	}

	for (i = 0; i < dl->dev_num; i++, dr++) {
		struct timeval tmp_tv, *tv = NULL;
		uint8_t type = 0xff, bus = 0xff;
		char str[18], name[8] = "";
		bdaddr_t bdaddr;

		bacpy(&bdaddr, BDADDR_ANY);

		if (!gettimeofday(&tmp_tv, NULL))
			tv = &tmp_tv;

		device_info(fd, dr->dev_id, &type, &bus, &bdaddr, name);
		ba2str(&bdaddr, str);
		packet_new_index(tv, dr->dev_id, str, type, bus, name);
		open_device(dr->dev_id);
	}

done:
	free(dl);
}
Esempio n. 18
0
VALUE gps_initialize(VALUE self, VALUE serial) {
  PhidgetInfo *info = device_info(self);
  
  GpsInfo *gps_info = ALLOC(GpsInfo); 
  memset(gps_info, 0, sizeof(GpsInfo));

  gps_info->sample_rate = sample_create();

  CPhidgetGPSHandle gps = 0;

  ensure(CPhidgetGPS_create(&gps));

  ensure(CPhidgetGPS_set_OnPositionChange_Handler( gps, gps_on_position_change, info));
 	ensure(CPhidgetGPS_set_OnPositionFixStatusChange_Handler(gps, gps_on_fix_change, info));

  info->handle = (CPhidgetHandle)gps;
  info->on_type_detach = gps_on_detach;
  info->on_type_free = gps_on_free;
  info->type_info = gps_info;

  return rb_call_super(1, &serial);
}
VALUE interfacekit_initialize(VALUE self, VALUE serial) {
  PhidgetInfo *info = device_info(self);
  
  InterfaceKitInfo *ifkit_info = ALLOC(InterfaceKitInfo); 
  memset(ifkit_info, 0, sizeof(InterfaceKitInfo));
  ifkit_info->is_data_rates_known = false;
  ifkit_info->is_dual_ratiometric_mode = false;

  CPhidgetInterfaceKitHandle interfacekit = 0;
  ensure(CPhidgetInterfaceKit_create(&interfacekit));

  info->handle = (CPhidgetHandle)interfacekit;
  info->on_type_attach = interfacekit_on_attach;
  info->on_type_detach = interfacekit_on_detach;
  info->on_type_free = interfacekit_on_free;
  info->type_info = ifkit_info;

  ensure(CPhidgetInterfaceKit_set_OnInputChange_Handler(interfacekit, interfacekit_on_digital_change, info));
  ensure(CPhidgetInterfaceKit_set_OnSensorChange_Handler(interfacekit, interfacekit_on_analog_change, info));

  return rb_call_super(1, &serial);
}
VALUE interfacekit_ratiometric_set(VALUE self, VALUE is_ratiometric) {
  PhidgetInfo *info = device_info(self);
  InterfaceKitInfo *ifkit_info = device_type_info(self);

  if (!info->is_attached) return Qnil;

  bool is_ratiometric_bool;

  if (TYPE(is_ratiometric) == T_TRUE) 
    is_ratiometric_bool = true;
  else if (TYPE(is_ratiometric) == T_FALSE)
    is_ratiometric_bool = false;
  else
    rb_raise(rb_eTypeError, MSG_RATIOMETRIC_MUST_BE_BOOL);

  for(int i=0; i<ifkit_info->analog_input_count;i++)
    ifkit_info->is_ratiometric[i] = is_ratiometric_bool;

  ensure(interfacekit_assert_ratiometric_state( info ));

  return is_ratiometric;
}
VALUE interfacekit_ratiometric(int argc, VALUE* argv, VALUE self) {
  VALUE index;
  VALUE is_ratiometric;

  rb_scan_args(argc, argv, "11", &index, &is_ratiometric);

  if (TYPE(index) != T_FIXNUM)
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_MUST_BE_FIXNUM);

  PhidgetInfo *info = device_info(self);
  InterfaceKitInfo *ifkit_info = info->type_info;

  int index_int = FIX2INT(index);
  if ((ifkit_info->analog_input_count == 0) || (index_int > (ifkit_info->analog_input_count-1)))
    rb_raise(rb_eTypeError, MSG_SENSOR_INDEX_TOO_HIGH);

  VALUE ret;  
  if(NIL_P(is_ratiometric)) {
    // It's a get request:
    ret = (ifkit_info->is_ratiometric[index_int]) ? Qtrue : Qfalse;
  } else {
    if (TYPE(is_ratiometric) == T_TRUE) 
      ifkit_info->is_ratiometric[index_int] = true;
    else if (TYPE(is_ratiometric) == T_FALSE)
      ifkit_info->is_ratiometric[index_int] = false;
    else
      rb_raise(rb_eTypeError, MSG_RATIOMETRIC_MUST_BE_BOOL);

    ret = is_ratiometric;

    if (interfacekit_ratiometric_state_is_uniform(ifkit_info))
      ensure(interfacekit_assert_ratiometric_state(info));
    else
      ensure(interfacekit_assert_dual_ratiometric_mode(info));
  }

  return ret;
}
int main(int argc, char* argv[])
{
    CFMutableDictionaryRef out = device_info(-1, NULL);
    
    if (out == NULL)
    {
        fprintf(stderr, "device_info(-1, NULL) failed\n");
        return -1;
    }
    
    if (argc > 1 )
    {
        CFStringRef key = CFStringCreateWithCString(kCFAllocatorDefault, argv[1], kCFStringEncodingASCII);
        CFTypeRef value = CFDictionaryGetValue(out, key);
        if (value != NULL)
        {
            *stderr = *stdout;//HAX
            CFShow(value);
        }
        else
            fprintf(stderr, "key %s not found\n", argv[1]);
        CFRelease(key);
        CFRelease(out);
        return 0;
    }
   
    writePlistToStdout(out);
    /*CFStringRef plistFileName = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("%@.plist"), CFDictionaryGetValue(out, CFSTR("dataVolumeUUID")));
    
    CFStringRef printString = CFStringCreateWithFormat(kCFAllocatorDefault, NULL, CFSTR("Writing results to %@\n"), plistFileName);
    CFShow(printString);
    CFRelease(printString);
    
    saveResults(plistFileName, out);
    CFRelease(plistFileName);*/
    CFRelease(out);
    return 0;
}
Esempio n. 23
0
int main(int argc, char *argv[])
{
	int dev_from_class = 0, write_cnt;
	int fd;
	static struct sysfs_names *names;
	struct rc_device	  rc_dev;

	argp_parse(&argp, argc, argv, 0, 0, 0);

	/* Just list all devices */
	if (!clear && !readtable && !keys.next && !ch_proto && !cfg.next && !test && !delay && !period) {
		if (devicename) {
			fd = open(devicename, O_RDONLY);
			if (fd < 0) {
				perror("Can't open device");
				return -1;
			}
			device_info(fd, "");
			close(fd);
			return 0;
		}
		if (show_sysfs_attribs(&rc_dev))
			return -1;

		return 0;
	}

	if (cfg.next && (clear || keys.next || ch_proto || devicename)) {
		fprintf (stderr, "Auto-mode can be used only with --read, --debug and --sysdev options\n");
		return -1;
	}
	if (!devicename) {
		names = find_device(devclass);
		if (!names)
			return -1;
		rc_dev.sysfs_name = names->name;
		if (get_attribs(&rc_dev, names->name)) {
			free_names(names);
			return -1;
		}
		names->name = NULL;
		free_names(names);

		devicename = rc_dev.input_name;
		dev_from_class++;
	}

	if (cfg.next) {
		struct cfgfile *cur;
		char *fname, *name;
		int rc;

		for (cur = &cfg; cur->next; cur = cur->next) {
			if ((!rc_dev.drv_name || strcasecmp(cur->driver, rc_dev.drv_name)) && strcasecmp(cur->driver, "*"))
				continue;
			if ((!rc_dev.keytable_name || strcasecmp(cur->table, rc_dev.keytable_name)) && strcasecmp(cur->table, "*"))
				continue;
			break;
		}

		if (!cur->next) {
			if (debug)
				fprintf(stderr, "Table for %s, %s not found. Keep as-is\n",
				       rc_dev.drv_name, rc_dev.keytable_name);
			return 0;
		}
		if (debug)
			fprintf(stderr, "Table for %s, %s is on %s file.\n",
				rc_dev.drv_name, rc_dev.keytable_name,
				cur->fname);
		if (cur->fname[0] == '/' || ((cur->fname[0] == '.') && strchr(cur->fname, '/'))) {
			fname = cur->fname;
			rc = parse_keyfile(fname, &name);
			if (rc < 0) {
				fprintf(stderr, "Can't load %s table\n", fname);
				return -1;
			}
		} else {
			fname = malloc(strlen(cur->fname) + strlen(IR_KEYTABLE_USER_DIR) + 2);
			strcpy(fname, IR_KEYTABLE_USER_DIR);
			strcat(fname, "/");
			strcat(fname, cur->fname);
			rc = parse_keyfile(fname, &name);
			if (rc != 0) {
				fname = malloc(strlen(cur->fname) + strlen(IR_KEYTABLE_SYSTEM_DIR) + 2);
				strcpy(fname, IR_KEYTABLE_SYSTEM_DIR);
				strcat(fname, "/");
				strcat(fname, cur->fname);
				rc = parse_keyfile(fname, &name);
			}
			if (rc != 0) {
				fprintf(stderr, "Can't load %s table from %s or %s\n", cur->fname, IR_KEYTABLE_USER_DIR, IR_KEYTABLE_SYSTEM_DIR);
				return -1;
			}
		}
		if (!keys.next) {
			fprintf(stderr, "Empty table %s\n", fname);
			return -1;
		}
		clear = 1;
	}

	if (debug)
		fprintf(stderr, "Opening %s\n", devicename);
	fd = open(devicename, O_RDONLY);
	if (fd < 0) {
		perror(devicename);
		return -1;
	}
	if (dev_from_class)
		free(devicename);
	if (get_input_protocol_version(fd))
		return -1;

	/*
	 * First step: clear, if --clear is specified
	 */
	if (clear) {
		clear_table(fd);
		fprintf(stderr, "Old keytable cleared\n");
	}

	/*
	 * Second step: stores key tables from file or from commandline
	 */
	write_cnt = add_keys(fd);
	if (write_cnt)
		fprintf(stderr, "Wrote %d keycode(s) to driver\n", write_cnt);

	/*
	 * Third step: change protocol
	 */
	if (ch_proto) {
		rc_dev.current = ch_proto;
		if (set_proto(&rc_dev))
			fprintf(stderr, "Couldn't change the IR protocols\n");
		else {
			fprintf(stderr, "Protocols changed to ");
			show_proto(rc_dev.current);
			fprintf(stderr, "\n");
		}
	}

	/*
	 * Fourth step: display current keytable
	 */
	if (readtable)
		display_table(&rc_dev, fd);

	/*
	 * Fiveth step: change repeat rate/delay
	 */
	if (delay || period) {
		unsigned int new_delay, new_period;
		get_rate(fd, &new_delay, &new_period);
		if (delay)
			new_delay = delay;
		if (period)
			new_period = period;
		set_rate(fd, new_delay, new_period);
	}

	if (test)
		test_event(fd);

	return 0;
}
Esempio n. 24
0
static void stack_internal_callback(int fd, uint32_t events, void *user_data)
{
	unsigned char buf[HCI_MAX_FRAME_SIZE];
	unsigned char control[32];
	struct msghdr msg;
	struct iovec iov;
	struct cmsghdr *cmsg;
	ssize_t len;
	hci_event_hdr *eh;
	evt_stack_internal *si;
	evt_si_device *sd;
	struct timeval *tv = NULL;
	struct timeval ctv;
	uint8_t type = 0xff, bus = 0xff;
	char str[18], name[8] = "";
	bdaddr_t bdaddr;

	bacpy(&bdaddr, BDADDR_ANY);

	if (events & (EPOLLERR | EPOLLHUP)) {
		mainloop_remove_fd(fd);
		return;
	}

	iov.iov_base = buf;
	iov.iov_len = sizeof(buf);

	memset(&msg, 0, sizeof(msg));
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = control;
	msg.msg_controllen = sizeof(control);

	len = recvmsg(fd, &msg, MSG_DONTWAIT);
	if (len < 0)
		return;

	for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL;
					cmsg = CMSG_NXTHDR(&msg, cmsg)) {
		if (cmsg->cmsg_level != SOL_HCI)
			continue;

		switch (cmsg->cmsg_type) {
		case HCI_CMSG_TSTAMP:
			memcpy(&ctv, CMSG_DATA(cmsg), sizeof(ctv));
			tv = &ctv;
			break;
		}
	}

	if (len < 1 + HCI_EVENT_HDR_SIZE + EVT_STACK_INTERNAL_SIZE +
							EVT_SI_DEVICE_SIZE)
		return;

	if (buf[0] != HCI_EVENT_PKT)
		return;

	eh = (hci_event_hdr *) (buf + 1);
	if (eh->evt != EVT_STACK_INTERNAL)
		return;

	si = (evt_stack_internal *) (buf + 1 + HCI_EVENT_HDR_SIZE);
	if (si->type != EVT_SI_DEVICE)
		return;

	sd = (evt_si_device *) &si->data;

	switch (sd->event) {
	case HCI_DEV_REG:
		device_info(fd, sd->dev_id, &type, &bus, &bdaddr, name);
		ba2str(&bdaddr, str);
		packet_new_index(tv, sd->dev_id, str, type, bus, name);
		open_device(sd->dev_id);
		break;
	case HCI_DEV_UNREG:
		ba2str(&bdaddr, str);
		packet_del_index(tv, sd->dev_id, str);
		break;
	}
}
Esempio n. 25
0
int main(int argc, char *argv[])
{
    int ret;
    int num_pkts; 
    char *dev = NULL;  
    char errbuf[PCAP_ERRBUF_SIZE]; 

    pcap_t *dev_handle;
    const u_char *packet;
    struct pcap_pkthdr hdr;
    struct ether_header *eptr;

#ifdef WITH_HISTORY
    conn = connect_to_database(LOCALHOST, DATABASE, USER, PW);
#endif

    char *ival = NULL;
    int iflag = 0;
    int c;
    opterr = 0;

    while ((c = getopt(argc, argv, "i:csh")) != -1)
    {
        switch (c)
        {

            /* select interface */
            case 'i':
                ival = optarg;
                iflag = 1;
                break;

            /* clear history table */
            case 'c':
#ifdef WITH_HISTORY
                clear_history(conn);
                exit_nicely("History cleared");
#else
                exit_nicely("History database not in use.");
#endif

            /* show history */
            case 's':
#ifdef WITH_HISTORY
                show_history(conn);
                exit_nicely(NULL);
#else
                exit_nicely("History database not in use.");
#endif
  
            /* print help */
            case 'h':
                fprintf(stdout, "netSnarf Help:\n\tsnarf [OPTION]"
                                "\nOPTIONS\n"
                                "\t-c    "
                                "clear history records (requires database module)\n"
                                "\t-h    "
                                "help, prints cli options\n"
                                "\t-i <interface>    "
                                "selects network interface to use for sniffing\n"
                                "\t-s    "
                                "show history records (requires database module)\n");
                exit_nicely("");

            case '?':
                if (optopt == 'i')
                {
                    fprintf(stderr,
                            "Option -%c requires an argurment.\n", optopt);
                }
                else if (isprint(optopt))
                {
                    fprintf(stderr, "Unknown option '-%c'.\n", optopt);
                }
                else
                {
                    fprintf(stderr, 
                            "Unknown option character '\\x%x'.\n", optopt);
                }
                return 1;

            default:
                fprintf(stdout, "Starting packet sniffer\n");

        }
    }

    /* Select device: wlan if developing, else lookup or supply via cli */
    if (iflag)
        dev = ival;
    else
    {
#ifdef WLAN
        dev = "wlan0";
#else
        dev = pcap_lookupdev(errbuf);
#endif
    }

    if (dev == NULL) error_nicely(errbuf, __LINE__);

    /* Get basic information on capture interface/device */
    device_info(dev, errbuf);

    dev_handle = pcap_open_live(dev, BUFSIZ, 0, -1, errbuf);
    if (!dev_handle) error_nicely(errbuf, __LINE__);

    /* primary packet handling engine */
    pcap_loop(dev_handle, num_pkts, handle_packet, NULL);

    if (conn)
        mysql_close(conn);

    return 0;
}