1. 源码参考这里
2. 修改的Makefile
-
HAVEUSB = 1
-
OPENWRT = 1
-
-
ifeq ($(OPENWRT), 1)
-
#CC = ~/OpenWrt-SDK-ar71xx-for-linux-i486-gcc-4.6-linaro_uClibc-0.9.33.2/staging_dir/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
-
CC = ~/OpenWrt-Toolchain-ar71xx-for-mips_r2-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mips_r2_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mips-openwrt-linux-gcc
-
#CC = ~/OpenWrt-Toolchain-ramips-for-mipsel_24kec+dsp-gcc-4.6-linaro_uClibc-0.9.33.2/toolchain-mipsel_24kec+dsp_gcc-4.6-linaro_uClibc-0.9.33.2/bin/mipsel-openwrt-linux-gcc
-
CFLAGS += -I ~/openwrt-lib/include -L ~/openwrt-lib/lib
-
-
else
-
CC = gcc
-
endif
-
-
CFLAGS += -Wall -O2
-
CFLAGS1 += -fPIC -shared #仅so 文件需要
-
-
ifeq ($(HAVEUSB), 1)
-
LFLAGS += -lusb
-
endif
-
-
#可执行文件名和相关的obj文件
-
#so的名称必须和c中代码对应
-
SHAREOBJ = lualibusb.so
-
SRCS += lualibusb.c
-
-
#以下无需修改
-
OBJS = $(SRCS:.c=.o)
-
-
all: SO_FILE
-
-
SO_FILE: $(OBJS)
-
$(CC) $(CFLAGS) $(CFLAGS1) $(SRCS) -o $(SHAREOBJ) $(LFLAGS)
-
-
.PHONY: clean
-
clean:
-
@echo "cleanning project"
-
$(RM) *.a $(OBJS) *~ *.so *.lo $(SHAREOBJ)
-
@echo "clean completed"
3. 修改后的lua的测试代码
-
--[[
-
libusb-0.1.12的lua调用例程
-
Authors: Jorge Visca, Andres Aguirre
-
MINA - INCO - Facultad de Ingeniería - UDELAR
-
http://luaforge.net/projects/lualibusb
-
]]--
-
-
print("--> Hello LUA, qiushui_007 test!")
-
-
--assert(package.loadlib("Debug/libluausb.so","luaopen_libusb"))()
-
local libusb = require "lualibusb"
-
--libusb.find_busses()
-
--libusb.find_devices()
-
-
print ("\n------------------------------------")
-
print ("-- A few exported constants:")
-
print ("USB_CLASS_HUB", USB_CLASS_HUB)
-
print ("USB_DT_STRING", USB_DT_STRING)
-
-
--[[
-
print ("\n------------------------------------")
-
print ("-- Dumping libusb:", libusb)
-
for k, _ in pairs(libusb) do
-
print (k)
-
end
-
]]--
-
-
local buses = libusb.get_busses()
-
local last_device
-
-
print ("\n------------------------------------")
-
print ("-- Dumping buses&devices:", buses)
-
for dirname, bus in pairs(buses) do
-
--print (k)
-
local devices = libusb.get_devices(bus)
-
for filename, device in pairs(devices) do
-
-
local descriptor = libusb.device_descriptor(device)
-
print ("Bus: " .. dirname, "Device: " .. filename,
-
"VID:PID : " .. string.format("%04x:%04x", descriptor.idVendor, descriptor.idProduct)) --, "desc:", descriptor.iProduct, s or err)
-
last_device = device
-
-
end
-
-
end
-
-
print ("\n------------------------------------")
-
print ("-- Device descriptor:", last_device)
-
local descriptor = libusb.device_descriptor(last_device)
-
for k, v in pairs(descriptor) do
-
print (k, v)
-
end
-
-
print ("\n------------------------------------")
-
print ("-- Device configuration:", last_device)
-
local config = libusb.device_config(last_device)
-
for k, v in pairs(config) do
-
print (k, v)
-
end
-
-
print ("\n------------------------------------")
-
print ("-- Interface settings:", last_device)
-
local altsettings = libusb.interface_alt_settings(config.interface)
-
for k, v in pairs(altsettings) do
-
print ("+ Alternative", k, v)
-
for ka, va in pairs(v) do
-
print (ka, va)
-
end
-
end
4. 将so和lua文件拷贝到WR703N上, 运行结果
4.1 U口不接任何设备
-
root@OpenWrt:/xutest# lua libusb.lua
-
--> Hello LUA, qiushui_007
-
-
------------------------------------
-
-- A few exported constants:
-
USB_CLASS_HUB 9
-
USB_DT_STRING 3
-
-
------------------------------------
-
-- Dumping buses&devices: table: 0x45e260
-
Bus: 001 Device: 001 VID:PID : 1d6b:0002
-
-
------------------------------------
-
-- Device descriptor: userdata: 0x466dd8
-
bDeviceProtocol 1
-
bMaxPacketSize0 64
-
bDeviceSubClass 0
-
bLength 18
-
bNumConfigurations 1
-
bcdUSB 512
-
iSerialNumber 1
-
iProduct 2
-
bcdDevice 771
-
iManufacturer 3
-
bDescriptorType 1
-
idProduct 2
-
bDeviceClass 9
-
idVendor 7531
-
-
------------------------------------
-
-- Device configuration: userdata: 0x466dd8
-
bmAttributes 224
-
iConfiguration 0
-
MaxPower 0
-
extra
-
bConfigurationValue 1
-
wTotalLength 25
-
bDescriptorType 2
-
bLength 9
-
bNumInterfaces 1
-
interface userdata: 0x45ef00
-
-
------------------------------------
-
-- Interface settings: userdata: 0x466dd8
-
+ Alternative 0 table: 0x465518
-
bInterfaceSubClass 0
-
bInterfaceClass 9
-
bLength 9
-
bNumEndpoints 1
-
bInterfaceNumber 0
-
extra
-
iInterface 0
-
bDescriptorType 4
-
bInterfaceProtocol 0
-
endpoint userdata: 0x464460
-
bAlternateSetting 0
-
root@OpenWrt:/xutest
4.2 接上CR95HF, VID = 0x0483, PID = 0xd0d0
-
root@OpenWrt:/xutest# lua libusb.lua
-
--> Hello LUA, qiushui_007
-
-
------------------------------------
-
-- A few exported constants:
-
USB_CLASS_HUB 9
-
USB_DT_STRING 3
-
-
------------------------------------
-
-- Dumping buses&devices: table: 0x539260
-
Bus: 001 Device: 001 VID:PID : 1d6b:0002
-
Bus: 001 Device: 009 VID:PID : 0483:d0d0
-
-
------------------------------------
-
-- Device descriptor: userdata: 0x541dd8
-
bDeviceProtocol 0
-
bMaxPacketSize0 64
-
bDeviceSubClass 0
-
bLength 18
-
bNumConfigurations 1
-
bcdUSB 512
-
iSerialNumber 3
-
iProduct 2
-
bcdDevice 512
-
iManufacturer 1
-
bDescriptorType 1
-
idProduct 53456
-
bDeviceClass 0
-
idVendor 1155
-
-
------------------------------------
-
-- Device configuration: userdata: 0x541dd8
-
bmAttributes 128
-
iConfiguration 0
-
MaxPower 100
-
extra
-
bConfigurationValue 1
-
wTotalLength 41
-
bDescriptorType 2
-
bLength 9
-
bNumInterfaces 1
-
interface userdata: 0x539f00
-
-
------------------------------------
-
-- Interface settings: userdata: 0x541dd8
-
+ Alternative 0 table: 0x540880
-
bInterfaceSubClass 0
-
bInterfaceClass 3
-
bLength 9
-
bNumEndpoints 2
-
bInterfaceNumber 1
-
extra !
-
iInterface 14
-
bDescriptorType 4
-
bInterfaceProtocol 0
-
endpoint userdata: 0x53ad28
-
bAlternateSetting 0
-
root@OpenWrt:/xutest
4.3 接上DS9490R, VID = 0x04fa, PID = 0x2490
-
root@OpenWrt:/xutest# lua libusb.lua
-
--> Hello LUA, qiushui_007
-
-
------------------------------------
-
-- A few exported constants:
-
USB_CLASS_HUB 9
-
USB_DT_STRING 3
-
-
------------------------------------
-
-- Dumping buses&devices: table: 0x619260
-
Bus: 001 Device: 001 VID:PID : 1d6b:0002
-
Bus: 001 Device: 010 VID:PID : 04fa:2490
-
-
------------------------------------
-
-- Device descriptor: userdata: 0x621dd8
-
bDeviceProtocol 255
-
bMaxPacketSize0 8
-
bDeviceSubClass 255
-
bLength 18
-
bNumConfigurations 1
-
bcdUSB 256
-
iSerialNumber 0
-
iProduct 0
-
bcdDevice 2
-
iManufacturer 0
-
bDescriptorType 1
-
idProduct 9360
-
bDeviceClass 255
-
idVendor 1274
-
-
------------------------------------
-
-- Device configuration: userdata: 0x621dd8
-
bmAttributes 224
-
iConfiguration 0
-
MaxPower 240
-
extra
-
bConfigurationValue 1
-
wTotalLength 129
-
bDescriptorType 2
-
bLength 9
-
bNumInterfaces 1
-
interface userdata: 0x619f00
-
-
------------------------------------
-
-- Interface settings: userdata: 0x621dd8
-
+ Alternative bInterfaceSubClass 255
-
lua: libusb.lua:66: bad argument #1 to 'pairs' (table expected, got number)
-
stack traceback:
-
[C]: in function 'pairs'
-
libusb.lua:66: in main chunk
-
[C]: ?
-
root@OpenWrt:/xutest
阅读(4097) | 评论(0) | 转发(0) |