ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Ok. I need help bad. I am trying to make a program that will receive data through a USB port, and then retransmit it accross TCP/IP, so a client can talk with the device over TCP/IP.
I don't know anything about USB. So how do I talk to a USB device (say I want to get raw input from a keyboard... I know, I can do this simpler ways, but this is just an example, and the only USB device I have at the moment in which I can talk with) in an OS independant way. If there aren't any good libraries for USB that are OSI, I'll just write my code so it is OSI by using native Windows and Linux and Mac drivers.
I must do this. I must do it relitivly soon. I don't know even where to start. Please help me!!!
Sorry, when I re-read your post, I realize I misunderstood what you wanted. If you are talking about a device such as the keyboard or the mouse, then you can do that using x-windows. For example, using the secure shell, you can forward x-windows events.
Also, remember the Unix design philosophy that everything is a file. So you can redirect the output of an audio device for example to a socket. However whatever you do should be going through the secure shell, or one or the other computer will be wide open.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "usb.h"
int main(int argc, char **argv)
{
struct usb_bus *bus;
int c, i, a, c1, i1;
struct usb_bus *busses;
char Bytes[256];
usb_init();
usb_find_busses();
usb_find_devices();
busses = usb_get_busses();
for (bus = busses; bus; bus = bus->next)
{
struct usb_device *dev;
for (dev = bus->devices; dev; dev = dev->next)
{
/* Loop through all of the configurations */
for (c = 0; c < dev->descriptor.bNumConfigurations; c++)
{
/* Loop through all of the interfaces */
for (i = 0; i < dev->config[c].bNumInterfaces; i++)
{
/* Loop through all of the alternate settings */
for (a = 0; a < dev->config[c].interface[i].num_altsetting; a++)
{
if (dev->config[c].interface[i].altsetting[a].bInterfaceClass >= 0)
{
printf("Device class = %d\n", dev->config[c].interface[i].altsetting[a].bInterfaceClass);
if (dev->descriptor.bDeviceClass >= 0)
{
usb_dev_handle *Handle = usb_open(dev);
if (Handle)
{
printf("Handle opened for device!\n");
i1=usb_claim_interface(Handle, dev->config[c].interface[i].altsetting[a].bInterfaceNumber);
if (i1>=0)
{
printf("Interface claimed!\n");
while(1)
{
c1 = usb_bulk_read(Handle, USB_ENDPOINT_TYPE_BULK, Bytes, 256, 32000);
if (c1<0)
{
printf("Failed device read\n");
break;
}
else
printf("%c", Bytes[0]);
}
usb_release_interface(Handle, dev->config[c].interface[i].altsetting[a].bInterfaceNumber);
}
else printf("Claiming interface failed!\n");
usb_close(Handle);
}
else
{
printf("Unable to open handle to device!\n");
}
}
}
}
}
}
}
}
}
When run, it goes through all the devices connected to the USB ports, and tries to open and read data from them. I know the code is messy, and rather, bad... but I am just trying to do some tests so I can better understand how to do USB programming. It fails at printf("Claiming interface failed!\n"); and quits. I tried skipping this step and just reading from the device. But this fails as well. What must I do to read, say, input from a keyboard?
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.