//打开设备
int ctUsb_open(int devId)
{
int handle = -1;
CTUsbMsg msg;
int msglen, incnt;
memset(&msg, 0, sizeof(msg));
snprintf(msg.msgData, sizeof(msg.msgData), "OPEN");
msg.msgLen = strlen(msg.msgData) + sizeof(devId);
memcpy(&msg.msgData[4], &devId, sizeof(devId));
int xreason;
handle = ctUsb_connect(USB_ADMIN_DATA_PORT);
if (handle > 0)
{
write(handle, &msg, msg.msgLen + sizeof(msg.msgLen));
incnt = ctUsb_msgRecive(handle, &msglen, sizeof(msglen));
if (incnt > 0)
{
ctUsb_msgRecive(handle, &xreason, msglen);
}
}
g_errno = xreason;
return handle;
}
//创建socket连接
static int ctUsb_connect(int portNum)
{
int sockfd, addrsize;
struct sockaddr_in seraddr;
memset(&seraddr, 0, sizeof(seraddr));
seraddr.sin_family = AF_INET;
seraddr.sin_port = htons(portNum);
seraddr.sin_addr.s_addr = inet_addr("127.0.0.1");
addrsize = sizeof(struct sockaddr_in);
sockfd = socket(AF_INET, SOCK_STREAM, 0);
if (sockfd < 0)
{
printf("could not create socket\n");
}
if (connect(sockfd, (struct sockaddr*)&seraddr, addrsize) == -1)
{
printf("could not connect usbAdmin\n");
close(sockfd);
sockfd = -1;
}
return sockfd;
}
/**
* 配置设备属性
* @param handle(IN)
* @param request(IN)
* @param arg(IN/OUT)
* @param reason(OUT)
* @return Result
*/
int ctUsb_ioctl(int handle, int request, void *arg, int arglen)
{
CTUsbMsg msg;
int len, xreason, n;
int requestx = request;
memset(&msg, 0, sizeof(msg));
snprintf(msg.msgData, sizeof(msg.msgData), "IOCTL");
len = strlen(msg.msgData);
memcpy(&msg.msgData[len], &requestx, sizeof(requestx));
memcpy(&msg.msgData[len + 4], &arglen, sizeof(arglen));
if (arg != NULL)
{
memcpy(&msg.msgData[len + 8], arg, arglen);
}
msg.msgLen = 5 + sizeof(requestx) + arglen;
write(handle, &msg, msg.msgLen + sizeof(msg.msgLen));
n = ctUsb_msgRecive(handle, &len, sizeof(len));
if (n > 0)
{
ctUsb_msgRecive(handle, (char*)arg, len);
}
n = ctUsb_msgRecive(handle, &len, sizeof(len));
if (n > 0)
{
ctUsb_msgRecive(handle, &xreason, len);
}
g_errno = xreason;
return 0;
}
为了方便理解,我用java写一个调用的方法:
public static void main(String[] args){
int handle=open(devcId);//创建socket连接 此处handle是什么意思,接着调用ctUsb_ioctl方法要把此handle 传给 ctUsb_ioctl方法,(我自己的理解表示socket对象,但在java中要 怎么用int 类型的变量代表一个socket对象呢)
ctUsb_ioctl(handle,......)//
}
请大家帮忙给分析一下 C的程序流程,考虑用java如何实现!!!先拜谢了!!