Skip to content
This repository has been archived by the owner on Oct 13, 2019. It is now read-only.

Commit

Permalink
Update USB example
Browse files Browse the repository at this point in the history
  • Loading branch information
xerpi committed Aug 25, 2015
1 parent 3fe2a02 commit 14cd3e1
Showing 1 changed file with 72 additions and 50 deletions.
122 changes: 72 additions & 50 deletions examples/usb/source/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,75 +11,97 @@ Transfer 0x802400ff, length 0

#include "ps4.h"

#define debug(sock, format, ...)\
do {\
char buffer[512];\
int size = sprintf(buffer, format, ##__VA_ARGS__);\
sceNetSend(sock, buffer, size, 0);\
#define debug(...) \
do { \
char debug_buffer[512]; \
int size = sprintf(debug_buffer, ##__VA_ARGS__); \
sceNetSend(netdbg_sock, debug_buffer, size, 0); \
} while(0)

int _main(void) {
// Load modules
initLibc();
initNetwork();
initUsb();


// Connect to server
char socketName[] = "debug";


// Init netdebug
struct sockaddr_in server;

server.sin_family = htons(AF_INET);
server.sin_family = sceNetHtons(AF_INET);
server.sin_addr.s_addr = IP(192, 168, 0, 4);
server.sin_port = htons(9023);
server.sin_port = sceNetHtons(9023);
memset(server.sin_zero, 0, sizeof(server.sin_zero));
int sock = sceNetSocket(socketName, AF_INET, SOCK_STREAM, 0);
sceNetConnect(sock, (struct sockaddr *)&server, sizeof(server));

int netdbg_sock = sceNetSocket("netdebug", AF_INET, SOCK_STREAM, 0);
sceNetConnect(netdbg_sock, (struct sockaddr *)&server, sizeof(server));


// Read all USBs
libusb_device **list;
libusb_device_descriptor ddesc;

libusb_device_descriptor desc;
struct libusb_config_descriptor *config;
const struct libusb_interface *inter;
const struct libusb_interface_descriptor *interdesc;
const struct libusb_endpoint_descriptor *epdesc;

int ret = sceUsbdInit();
debug(sock, "Init %d\n", ret);
debug("sceUsbdInit %d\n", ret);

int count = sceUsbdGetDeviceList(&list);
debug(sock, "Get device list %d\n", count);
debug("Device list count %d\n", count);

int i;
for(i = 0; i < count; i++) {
ret = sceUsbdGetDeviceDescriptor(list[i], &ddesc);
debug(sock, "Get descriptor %d\n", ret);

unsigned short vendor = ddesc.idVendor;
unsigned short product = ddesc.idProduct;

debug(sock, "Vendor: %d, Product: %d\n", vendor, product);

libusb_device_handle *handle;
//ret = sceUsbdOpen(list[i], &handle);
handle = sceUsbdOpenDeviceWithVidPid(vendor, product);

debug(sock, "Open 0x%p\n", handle);

unsigned char data[512];
int length;
ret = sceUsbdBulkTransfer(handle, LIBUSB_ENDPOINT_IN, data, sizeof(data), &length, 0);

debug(sock, "Transfer 0x%08x, length %d\n", ret, length);

sceNetSend(sock, data, length, 0);

debug("Device %i\n", i);

ret = sceUsbdGetDeviceDescriptor(list[i], &desc);
debug(" Get device descriptor: %d\n", ret);

debug(" Device Class: 0x%02X\n", desc.bDeviceClass);
debug(" Vendor ID: 0x%04X\n", desc.idVendor);
debug(" Product ID: 0x%04X\n", desc.idProduct);
debug(" Number of possible configurations: %i\n", desc.bNumConfigurations);

ret = sceUsbdGetConfigDescriptor(list[i], 0, &config);
debug("\n Get config descriptor: %d\n", ret);

debug(" Number of interfaces: %i\n\n", config->bNumInterfaces);

int j;
for (j = 0; j < config->bNumInterfaces; j++) {
debug(" Interface %i\n", j);
inter = &config->interface[j];

debug(" Number of alternate settings: %i\n", inter->num_altsetting);

int k;
for (k = 0; k < inter->num_altsetting; k++) {
debug(" Interface %i\n", j);
interdesc = &inter->altsetting[j];

debug(" Interface Number: %i\n", interdesc->bInterfaceNumber);
debug(" Number of endpoints: %i\n", interdesc->bNumEndpoints);

int l;
for(l = 0; l < interdesc->bNumEndpoints; l++) {
debug(" Endpoint %i\n", l);
epdesc = &interdesc->endpoint[l];

debug(" Descriptor Type: 0x%02X\n", epdesc->bDescriptorType);
debug(" EP Address: 0x%02X\n", epdesc->bEndpointAddress);
debug(" EP Attributes: 0x%02X\n", epdesc->bmAttributes);
}
}
}

sceUsbdFreeConfigDescriptor(config);
}

sceUsbdFreeDeviceList(list, 1);



// Return to browser
sceUsbdExit();
sceNetSocketClose(sock);
sceNetSocketClose(netdbg_sock);

return 0;
}

0 comments on commit 14cd3e1

Please sign in to comment.