Exemplo n.º 1
0
static int usb_open(struct aura_node *node, const char *opts)
{
	int ret; 
	struct usb_dev_info *inf = calloc(1, sizeof(*inf));

	if (!inf)
		return -ENOMEM;

	ret = libusb_init(&inf->ctx);
	if (ret != 0) 
		return -EIO;

	inf->io_buf_size = 256;
	inf->optbuf = strdup(opts);
	inf->dev_descr.device_found_func = usb_start_ops;
	inf->dev_descr.arg = inf;
	inf->node = node;
	parse_params(inf);
	
	ncusb_start_descriptor_watching(node, inf->ctx);
	aura_set_transportdata(node, inf);	

	slog(4, SLOG_INFO, "usb: vid 0x%x pid 0x%x vendor %s product %s serial %s", 
	     inf->dev_descr.vid, inf->dev_descr.pid, inf->dev_descr.vendor, 
	     inf->dev_descr.product, inf->dev_descr.serial);

	inf->ctrlbuf   = malloc(inf->io_buf_size);
	if (!inf->ctrlbuf)
		goto err_free_inf;
	inf->itransfer = libusb_alloc_transfer(0);
	if (!inf->itransfer)
		goto err_free_cbuf;

 	inf->ctransfer = libusb_alloc_transfer(0);
	if (!inf->ctransfer)
		goto err_free_int;
	
	slog(1, SLOG_INFO, "usb: Now looking for a matching device");

	ncusb_watch_for_device(inf->ctx, &inf->dev_descr);

	return 0;
	
err_free_int:
	libusb_free_transfer(inf->itransfer);
err_free_cbuf:
	free(inf->ctrlbuf);
err_free_inf:
	libusb_exit(inf->ctx);
	free(inf);
	return -ENOMEM;
}
Exemplo n.º 2
0
static int susb_open(struct aura_node *node, const char *conf)
{
	struct libusb_context *ctx;
	struct usb_dev_info *inf = calloc(1, sizeof(*inf));
	int ret;
	lua_State *L;

	if (!inf)
		return -ENOMEM;

	/* Assume 3 retries by default */
	inf->control_retry_max = 3;
	ret = libusb_init(&ctx);
	if (ret != 0)
		goto err_free_inf;
	inf->ctx = ctx;

	L = luaL_newstate();
	if (!L)
		goto err_free_ctx;

	inf->ctransfer = libusb_alloc_transfer(0);
	if (!inf->ctransfer)
		goto err_free_lua;

	luaL_openlibs(L);
	luaopen_auracore(L);
	lua_setglobal(L, "aura");

	slog(2, SLOG_INFO, "usbsimple: config file %s", conf);

	const char loader_scr[] =
		"return require(\"aura/conf-loader\")\n";
	char *scr;
	const char *libpath = getenv("AURA_LUA_SCRIPT_PATH");
	if (libpath) {
		int ret = asprintf(&scr, "package.path=\"%s/?.lua;\"..package.path\n%s\n",
		libpath, loader_scr);
		if (ret == -1)
			goto err_free_ct;
	} else {
		scr = (char *) loader_scr;
	}
	ret = luaL_loadbuffer(L, scr, strlen(scr), "ldr");
	if (libpath)
		free(scr);

	if (ret) {
		slog(0, SLOG_ERROR, lua_tostring(L, -1));
		slog(0, SLOG_ERROR, "usbsimple: config file load error");
		goto err_free_ct;
	}

	lua_call(L, 0, 1);

	lua_pushstring(L, conf);
	lua_setglobal(L, "simpleconf");

	lua_pushlightuserdata(L, node);
	lua_setglobal(L, "node");

	/*  We need to push all format tokens to lua.
	 *  We do it here to be always in sync with format.h
	 */

	lua_settoken(L, "UINT8", URPC_U8);
	lua_settoken(L, "UINT16", URPC_U16);
	lua_settoken(L, "UINT32", URPC_U32);
	lua_settoken(L, "UINT64", URPC_U64);

	lua_settoken(L, "SINT8", URPC_S8);
	lua_settoken(L, "SINT16", URPC_S16);
	lua_settoken(L, "SINT32", URPC_S32);
	lua_settoken(L, "SINT64", URPC_S64);

	lua_settoken(L, "FMT_BIN", URPC_BIN);

	ret = lua_pcall(L, 0, 7, 0);
	if (ret) {
		const char *err = lua_tostring(L, -1);
		slog(0, SLOG_FATAL, "usbsimple: %s", err);
		goto err_free_ct;
	}

	inf->dev_descr.vid = lua_tonumber(L, -7);
	inf->dev_descr.pid = lua_tonumber(L, -6);
	inf->dev_descr.vendor = lua_strfromstack(L, -5);
	inf->dev_descr.product = lua_strfromstack(L, -4);
	inf->dev_descr.serial = lua_strfromstack(L, -3);
	inf->etbl = lua_touserdata(L, -2);
	if (lua_isnumber(L, -1)) {
		inf->control_retry_max = lua_tonumber(L, -1);
		slog(4, SLOG_DEBUG, "Adjusting control transfer retry count to %d",
		     inf->control_retry_max);
	}

	inf->dev_descr.device_found_func = usb_start_ops;
	inf->dev_descr.device_left_func = usb_stop_ops;
	inf->dev_descr.arg = inf;
	inf->node = node;

	inf->timer = ncusb_timer_create(node, inf->ctx);
	if (!inf->timer)
		goto err_free_ct;

	/* We no not need this state anymore */
	lua_close(L);
	aura_set_transportdata(node, inf);

	return 0;
err_free_ct:
	libusb_free_transfer(inf->ctransfer);
err_free_lua:
	lua_close(L);
err_free_ctx:
	libusb_exit(inf->ctx);
err_free_inf:
	free(inf);
	return -ENOMEM;
}