Advertisement
xerpi

sixpair Wii

Dec 30th, 2013
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.72 KB | None | 0 0
  1. /*
  2.     trying to read data from a PS3 controller... (by xerpi)
  3. */
  4.  
  5. #include <gccore.h>
  6. #include <gcutil.h>
  7. #include <ogcsys.h>
  8. #include <ogc/ipc.h>
  9. #include <ogc/usb.h>
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <unistd.h>
  14. #include <wiiuse/wpad.h>
  15.  
  16. #define MAX_DEV 7
  17. #define VID       0x054C //Sony Corp.
  18. #define PID       0x0268 //Sixaxis and DS3
  19.  
  20. static void *xfb = NULL;
  21. static GXRModeObj *rmode = NULL;
  22. void init_video();
  23.  
  24. int find_and_set_mac();
  25. void set_controller_mac(int32_t fd, uint8_t *mac);
  26. void get_controller_mac(int32_t fd, uint8_t *mac);
  27.  
  28.  
  29. int main(int argc, char **argv) {
  30.     IOS_ReloadIOS(58);
  31.     usleep(500 * 1000);
  32.     init_video();
  33.     WPAD_Init();
  34.    
  35.     iosCreateHeap(1024 * 16);
  36.    
  37.     USB_Initialize();
  38.     usleep(10000);
  39.     while (find_and_set_mac() <= 0) {
  40.         printf("connect a ps3 controller\n");
  41.         usleep(1500 * 1000);
  42.     }
  43.     USB_Deinitialize();
  44.     printf("ps3 mac set, now press the PS button\n");
  45.    
  46.    
  47.     //Bluetooth connection...
  48.  
  49.     while(1) {
  50.         WPAD_ScanPads();
  51.         u32 pressed = WPAD_ButtonsDown(0);
  52.        
  53.        
  54.  
  55.         if ( pressed & WPAD_BUTTON_HOME ) exit(0);
  56.         VIDEO_WaitVSync();
  57.     }
  58.    
  59.    
  60.     return 0;
  61. }
  62.  
  63.  
  64. int find_and_set_mac()
  65. {
  66.     uint8_t dev_count;
  67.     usb_device_entry usb_dev_entry_list[MAX_DEV];
  68.    
  69.     if (USB_GetDeviceList(usb_dev_entry_list, MAX_DEV, USB_CLASS_HID, &dev_count) < 0) {
  70.         printf("Error getting device list.\n");
  71.         return -1;
  72.     }
  73.    
  74.     printf("found %i USB devices, looking for the PS3 controller...", dev_count);
  75.     if (dev_count == 0) return -2;
  76.    
  77.     int  i;
  78.     usb_device_entry *de;
  79.     for(i = 0; i < dev_count; i++) {
  80.         de = &usb_dev_entry_list[i];
  81.        
  82.         //Is it a PS3 controller?
  83.         if (de->vid == VID && de->pid == PID) {
  84.             printf("found!\n");
  85.             //Yeah it is, let's try to open it!
  86.             int32_t fd;
  87.             if (USB_OpenDevice(de->device_id, VID, PID, &fd) < 0) {
  88.                 printf ("error opening the controller\n");
  89.                 return -3;
  90.             }
  91.  
  92.             printf("now let's set local BD MAC!...");
  93.             uint8_t local_mac[6] = {0,0,0,0xff,0xff,0xff};
  94.             set_controller_mac(fd, local_mac);
  95.            
  96.             printf("checking if it was set correctly:\n");
  97.            
  98.             uint8_t mac[6];
  99.             get_controller_mac(fd, mac);
  100.            
  101.             printf("we got:  %02X:%02X:%02X:%02X:%02X:%02X from the controller\n", mac[0], mac[1], mac[2],
  102.                                                                                    mac[3], mac[4], mac[5]);
  103.                                                                        
  104.             int j, equals = 1;
  105.             for (j = 0;  j < 6 && equals; ++j) {
  106.                 if (local_mac[j] != mac[j]) equals = 0;
  107.             }
  108.            
  109.             if (equals) {
  110.                 printf ("mac set correctly!!\n");
  111.                 USB_CloseDevice(&fd);
  112.                 return 1;
  113.             } else {
  114.                 printf ("mac was not set correctly\n");
  115.                 USB_CloseDevice(&fd);
  116.                 return -4;
  117.             }
  118.         }
  119.     }    
  120.     printf ("not found\n");
  121.     return 0;
  122. }
  123.  
  124.  
  125. void set_controller_mac(int32_t fd, uint8_t *mac)
  126. {
  127.         uint8_t ATTRIBUTE_ALIGN(32) msg[8] = {0x01, 0x00, mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]};
  128.         USB_WriteCtrlMsg(fd,
  129.                         USB_REQTYPE_INTERFACE_SET,
  130.                         USB_REQ_SETREPORT,
  131.                         (USB_REPTYPE_FEATURE<<8) | 0xf5,
  132.                         0,
  133.                         8,
  134.                         msg);
  135. }
  136.  
  137. void get_controller_mac(int32_t fd, uint8_t *mac)
  138. {
  139.     uint8_t ATTRIBUTE_ALIGN(32) msg[8];
  140.     USB_WriteCtrlMsg(fd,
  141.                        USB_REQTYPE_INTERFACE_GET,
  142.                        USB_REQ_GETREPORT,
  143.                        (USB_REPTYPE_FEATURE<<8) | 0xf5,
  144.                        0,
  145.                        8,
  146.                        msg);
  147.  
  148.     mac[0] = msg[2];
  149.     mac[1] = msg[3];
  150.     mac[2] = msg[4];
  151.     mac[3] = msg[5];
  152.     mac[4] = msg[6];
  153.     mac[5] = msg[7];
  154. }
  155.  
  156.  
  157. void init_video()
  158. {
  159.     VIDEO_Init();
  160.    
  161.     rmode = VIDEO_GetPreferredMode(NULL);
  162.     xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
  163.     console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
  164.     VIDEO_Configure(rmode);
  165.     VIDEO_SetNextFramebuffer(xfb);
  166.    
  167.     // Make the display visible
  168.     VIDEO_SetBlack(FALSE);
  169.  
  170.     // Flush the video register changes to the hardware
  171.     VIDEO_Flush();
  172.  
  173.     // Wait for Video setup to complete
  174.     VIDEO_WaitVSync();
  175.     if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();  
  176.    
  177.     printf("\x1b[2;0H");  
  178. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement