Commits

Jérôme Vuarand committed 0ff19f0

Added support for the creation of a libusb_transfer object.

Comments (0)

Files changed (4)

 	gc = true,
 })
 
+table.insert(structs, {
+	cname = 'transfer',
+	gc = true,
+})
+
 ------------------------------------------------------------------------------
 
 local enums_h = assert(io.open('enums.h', 'wb'))
 };
 extern struct luaL_Reg libusb_device_handle__methods[];
 
+struct libusb_transfer* luausb_to_transfer(lua_State* L, int index)
+{
+	struct libusb_transfer* udata;
+	udata = *(struct libusb_transfer**)lua_touserdata(L, index);
+	if (!udata)
+	{
+		luaL_argerror(L, index, "context has already been deinitialized");
+		return NULL;
+	}
+	return udata;
+}
+
+struct libusb_transfer* luausb_check_transfer(lua_State* L, int index)
+{
+	struct libusb_transfer* udata;
+	udata = *(struct libusb_transfer**)luaL_checkudata(L, index, "struct libusb_transfer");
+	if (!udata)
+	{
+		luaL_argerror(L, index, "context has already been deinitialized");
+		return NULL;
+	}
+	return udata;
+}
+
+void luausb_push_transfer(lua_State* L, const struct libusb_transfer* value, int owner)
+{
+	const struct libusb_transfer** udata;
+	if (owner < 0) owner = lua_gettop(L) + 1 + owner;
+	udata = (const struct libusb_transfer**)lua_newuserdata(L, sizeof(struct libusb_transfer*));
+	*udata = value;
+	luaL_getmetatable(L, "struct libusb_transfer");
+	lua_setmetatable(L, -2);
+	if (owner != 0)
+	{
+		lua_createtable(L, 1, 0);
+		lua_pushvalue(L, owner);
+		lua_rawseti(L, -2, 1);
+		setuservalue(L, -2);
+	}
+}
+
+static struct luaL_Reg libusb_transfer__getters[] = {
+	{0, 0},
+};
+int luausb_transfer_gc(lua_State* L);
+static struct luaL_Reg libusb_transfer__metamethods[] = {
+	{"__index", luausb_generic_index},
+	{"__tostring", luausb_generic_tostring},
+	{"__gc", luausb_transfer_gc},
+	{0, 0},
+};
+extern struct luaL_Reg libusb_transfer__methods[];
+
 void luausb_init_structs(lua_State* L)
 {
 	/* device_descriptor */
 	lua_pushliteral(L, "device_handle");
 	lua_setfield(L, -2, "typename");
 	lua_pop(L, 1);
+	/* transfer */
+	luaL_newmetatable(L, "struct libusb_transfer");
+	lua_pushvalue(L, 1);
+	setfuncs(L, libusb_transfer__metamethods, 1);
+	lua_newtable(L);
+	lua_pushvalue(L, 1);
+	setfuncs(L, libusb_transfer__methods, 1);
+	lua_setfield(L, -2, "methods");
+	lua_newtable(L);
+	lua_pushvalue(L, 1);
+	setfuncs(L, libusb_transfer__getters, 1);
+	lua_setfield(L, -2, "getters");
+	lua_pushliteral(L, "transfer");
+	lua_setfield(L, -2, "typename");
+	lua_pop(L, 1);
 }
 struct libusb_device_handle* luausb_to_device_handle(lua_State* L, int index);
 struct libusb_device_handle* luausb_check_device_handle(lua_State* L, int index);
 void luausb_push_device_handle(lua_State* L, const struct libusb_device_handle* value, int owner);
+struct libusb_transfer* luausb_to_transfer(lua_State* L, int index);
+struct libusb_transfer* luausb_check_transfer(lua_State* L, int index);
+void luausb_push_transfer(lua_State* L, const struct libusb_transfer* value, int owner);
 void luausb_init_structs(lua_State* L);
 	return 1;
 }
 
+BINDING(alloc_transfer)
+{
+	int iso_packets;
+	struct libusb_transfer* result;
+	
+	iso_packets = (int)luaL_optnumber(L, 1, 0); /* :FIXME: handle overflow */
+	
+	result = libusb_alloc_transfer(iso_packets);
+	if (!result)
+	{
+		lua_pushnil(L);
+		lua_pushstring(L, "allocation error");
+		return 2;
+	}
+	
+	luausb_push_transfer(L, result, 0);
+	return 1;
+}
+
+int luausb_transfer_gc(lua_State* L)
+{
+	struct libusb_transfer** dev;
+	
+	dev = (struct libusb_transfer**)lua_touserdata(L, 1);
+	if (*dev)
+	{
+		libusb_free_transfer(*dev);
+		*dev = 0;
+	}
+	return 0;
+}
+
+BINDING(free_transfer)
+{
+	luausb_check_transfer(L, 1);
+	return luausb_transfer_gc(L);
+}
+
 /****************************************************************************/
 
 int luausb_get_interface_descriptor_endpoint(lua_State* L)
 
 static luaL_Reg functions[] = {
 	BIND(init)
+	BIND(alloc_transfer)
 	BIND(cpu_to_le16)
 	BIND(le16_to_cpu)
 	{0, 0},
 	{0, 0},
 };
 
+struct luaL_Reg libusb_transfer__methods[] = {
+	{"free", lua__libusb_free_transfer},
+	{0, 0},
+};
+
 /****************************************************************************/
 
 LUAMOD_API int luaopen_module(lua_State* L)