Calling all Wii programmers, new coder needs some help.

Discussion in 'Wii - Hacking' started by RiCK420, Oct 21, 2013.

  1. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    Hello.
    So I started looking into programming for the Wii.
    I installed DevKitPPC and successfully completed the HelloWorld tutorial.
    Compiled the dol and ran it. So far so good.

    Now I am trying to write my own first program... A very simple one that when ran will tell how much disk space is left on the USB hard drive.
    I have been looking over the usbstorage.h file, as this was the only one I could see that had anything to do with the USB hard drive.
    I believe the command I need to use is this one: s32 USBStorage_ReadCapacity(usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors);
    However I am unsure how to actually use it, I'm guessing I need to fill in some information for each variable inside the ()'s, but I have no clue what to put in.

    If you could shed some light for me, it would be much appreciated.

    Warning: Spoilers inside!
     
    spacepimp likes this.


  2. Arras

    Arras GBAtemp Guru

    Member
    5,858
    2,673
    Sep 14, 2010
    Netherlands
    I'm guessing *dev is a pointer that points to the USB device itself, *sector_size is the sector size of the USB device and *n_sectors is the amount of sectors it has. I'm not sure what lun is, but it's some sort of number. Maybe you need to use other functions to get these variables; I've never tried programming for the Wii (or even C).
     
    RiCK420 likes this.
  3. ilman

    ilman Gbatemp's Official Noise Eraser

    Member
    1,130
    243
    Jul 25, 2010
    Shibuya
    I have no idea how to program on the Wii, but USBStorage_ReadCapacity is a function. Functions are premade code that you can use by setting some values in the (). How you do this is you add a variable of the first type (in your example, a usbstorage_handle variable), add a coma, add a variable of the second type (in this case, a u8 variable) then the third type and so forth.
    It's good to learn how functions function (bad pun, sorry) if you want to get serious with programming.

    Hope I helped. :)
     
    RiCK420 likes this.
  4. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    Thanks for the reply.

    I do understand how a function works, I program in some other languages, AutoIt, PHP, etc.

    I am new to C++, and programming specifically for the Wii though.
     
  5. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    Not sure if having this visible helps anyone with helping me?

    usbstorage.h

    Code:
     
    #ifndef __USBSTORAGE_H__
    #define __USBSTORAGE_H__
    
    #if defined(HW_RVL)
    
    #include <gctypes.h>
    #include <ogc/mutex.h>
    #include <ogc/disc_io.h>
    #include <ogc/system.h>
    
    #ifdef __cplusplus
       extern "C" {
    #endif /* __cplusplus */
    
    #define    USBSTORAGE_OK            0
    #define    USBSTORAGE_ENOINTERFACE        -10000
    #define    USBSTORAGE_ESENSE        -10001
    #define    USBSTORAGE_ESHORTWRITE        -10002
    #define    USBSTORAGE_ESHORTREAD        -10003
    #define    USBSTORAGE_ESIGNATURE        -10004
    #define    USBSTORAGE_ETAG            -10005
    #define    USBSTORAGE_ESTATUS        -10006
    #define    USBSTORAGE_EDATARESIDUE        -10007
    #define    USBSTORAGE_ETIMEDOUT        -10008
    #define    USBSTORAGE_EINIT        -10009
    #define USBSTORAGE_PROCESSING    -10010
    
    typedef struct
    {
        u8 configuration;
        u32 interface;
        u32 altInterface;
        u8 bInterfaceSubClass;
    
        u8 ep_in;
        u8 ep_out;
    
        u8 max_lun;
        u32 *sector_size;
    
        s32 usb_fd;
    
        mutex_t lock;
        syswd_t alarm;
        s32 retval;
    
        u32 tag;
        u8 suspended;
    
        u8 *buffer;
    } usbstorage_handle;
    
    #define B_RAW_DEVICE_DATA_IN 0x01
    #define B_RAW_DEVICE_COMMAND 0
    
    typedef struct {
       uint8_t         command[16];
       uint8_t         command_length;
       uint8_t         flags;
       uint8_t         scsi_status;
       void*           data;
       size_t          data_length;
    } raw_device_command;
    
    s32 USBStorage_Initialize();
    
    s32 USBStorage_Open(usbstorage_handle *dev, s32 device_id, u16 vid, u16 pid);
    s32 USBStorage_Close(usbstorage_handle *dev);
    s32 USBStorage_Reset(usbstorage_handle *dev);
    
    s32 USBStorage_GetMaxLUN(usbstorage_handle *dev);
    s32 USBStorage_MountLUN(usbstorage_handle *dev, u8 lun);
    s32 USBStorage_Suspend(usbstorage_handle *dev);
    s32 USBStorage_IsDVD();
    s32 USBStorage_ioctl(int request, ...);
    
    s32 USBStorage_ReadCapacity(usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors);
    s32 USBStorage_Read(usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, u8 *buffer);
    s32 USBStorage_Write(usbstorage_handle *dev, u8 lun, u32 sector, u16 n_sectors, const u8 *buffer);
    s32 USBStorage_StartStop(usbstorage_handle *dev, u8 lun, u8 lo_ej, u8 start, u8 imm);
    
    #define DEVICE_TYPE_WII_USB (('W'<<24)|('U'<<16)|('S'<<8)|'B')
    
    extern DISC_INTERFACE __io_usbstorage;
    
    #ifdef __cplusplus
       }
    #endif /* __cplusplus */
    
    #endif /* HW_RVL */
    
    #endif /* __USBSTORAGE_H__ */
    
     
    
     
  6. JoostinOnline

    JoostinOnline Certified Crash Test Dummy

    Member
    10,955
    3,740
    Apr 2, 2011
    United States
    The Twilight Zone
    1) Don't add the data types to the functions when you are running them. That's only for declaring/defining a function.
    2) You want to do something like this:
    Code:
    usbstorage_handle *usb_device;
    u32 *sector_size = 0;
    u32 *num_of_sectors = 0;
    u8 lun = 0; // Logical Unit Number (partition)
     
    USBStorage_ReadCapacity(&usb_device, lun, &sector_size, &num_of_sectors)
     
    RiCK420 likes this.
  7. Oxybelis

    Oxybelis GBAtemp Fan

    Member
    348
    74
    Jan 10, 2010
    Looks like this is a code for USD Storage device.
    You should find code for reading file system unless you want to parse partitions and file systems on device himself.
     
  8. JoostinOnline

    JoostinOnline Certified Crash Test Dummy

    Member
    10,955
    3,740
    Apr 2, 2011
    United States
    The Twilight Zone
    jonthedit, loco365, Foxi4 and 2 others like this.
  9. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    I want to get disk space usage stats like how much space is left on each partition of the USB connected hard drive.
     
  10. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    When I try this:

    Code:
        USBStorage_Initialize();
       
        usbstorage_handle *usb_device;
        u32 *sector_size = 0;
        u32 *num_of_sectors = 0;
        u8 lun = 0; // Logical Unit Number (partition)
       
        USBStorage_ReadCapacity(&usb_device, lun, &sector_size, &num_of_sectors);
    
    Make outputs these errors:

    Code:
    > "make"
    template.c
    c:/projects/wii/FreeSpace/source/template.c: In function 'main':
    c:/projects/wii/FreeSpace/source/template.c:111:2: warning: passing argument 1 of 'USBStorage_ReadCapacity' from incompatible pointer type [enabled by default]
    c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'struct usbstorage_handle *' but argument is of type 'struct usbstorage_handle **'
    c:/projects/wii/FreeSpace/source/template.c:111:2: warning: passing argument 3 of 'USBStorage_ReadCapacity' from incompatible pointer type [enabled by default]
    c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32 **'
    c:/projects/wii/FreeSpace/source/template.c:111:2: warning: passing argument 4 of 'USBStorage_ReadCapacity' from incompatible pointer type [enabled by default]
    c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32 **'
    linking ... FreeSpace.elf
    output ... FreeSpace.dol
     
    > Process Exit Code: 0
    > Time Taken: 00:02
    

    PS: If I removed the *'s, it doesn't give any errors, was I supposed to remove them?
     
  11. snikerz

    snikerz GBAtemp Advanced Fan

    Member
    502
    59
    Nov 30, 2008
    Lesotho
    remove teh &s
     
  12. JoostinOnline

    JoostinOnline Certified Crash Test Dummy

    Member
    10,955
    3,740
    Apr 2, 2011
    United States
    The Twilight Zone
    Oh duh. Sorry about that.
     
  13. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    OK, i put the *'s back and removed the &'s instead.. but now it outputs:

    Code:
    > "make"
    template.c
    c:/projects/wii/FreeSpace/source/template.c: In function 'main':
    c:/projects/wii/FreeSpace/source/template.c:82:23: warning: 'usb_device' is used uninitialized in this function [-Wuninitialized]
    linking ... FreeSpace.elf
    output ... FreeSpace.dol
     
    > Process Exit Code: 0
    > Time Taken: 00:01
    
    Entire code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <gccore.h>
    #include <wiiuse/wpad.h>
    #include <fat.h>
    #include <ogc/usbstorage.h>
    #include <string.h>
    #include <dirent.h>
    
    
    
    static void *xfb = NULL;
    static GXRModeObj *rmode = NULL;
    
    //---------------------------------------------------------------------------------
    int main(int argc, char **argv) {
    //---------------------------------------------------------------------------------
    
        // Initialise the video system
        VIDEO_Init();
        
        // This function initialises the attached controllers
        WPAD_Init();
        
        // Obtain the preferred video mode from the system
        // This will correspond to the settings in the Wii menu
        rmode = VIDEO_GetPreferredMode(NULL);
    
        // Allocate memory for the display in the uncached region
        xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
        
        // Initialise the console, required for printf
        console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
        
        // Set up the video registers with the chosen mode
        VIDEO_Configure(rmode);
        
        // Tell the video hardware where our display memory is
        VIDEO_SetNextFramebuffer(xfb);
        
        // Make the display visible
        VIDEO_SetBlack(FALSE);
    
        // Flush the video register changes to the hardware
        VIDEO_Flush();
    
        // Wait for Video setup to complete
        VIDEO_WaitVSync();
        if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
    
    
        // The console understands VT terminal escape codes
        // This positions the cursor on row 2, column 0
        // we can use variables for this with format codes too
        // e.g. printf ("\x1b[%d;%dH", row, column );
        printf("\x1b[2;0H");
        printf("RiCK420 has taken over your Wii! \n");
        printf("\n");
        printf("...Just Kidding! Please wait patiently! \n");
        printf("\n");
        
        // RiCK's code BEGIN // 
        
        //s32 USBStorage_ReadCapacity(usbstorage_handle *dev, u8 lun, u32 *sector_size, u32 *n_sectors);
        
        //USBStorage_Initialize();
        
        usbstorage_handle *usb_device;
        u32 *sector_size = 0;
        u32 *num_of_sectors = 0;
        u8 lun = 0; // Logical Unit Number (partition)
        
    
        //USBStorage_Initialize();
        
        if (!USBStorage_Initialize()) {
            printf("Unable to initialize subsystem. \n");
            printf("\n");
        }
        else {
            printf("Initialized subsystem. \n");
            printf("\n");
        }
         
        USBStorage_ReadCapacity(usb_device, lun, sector_size, num_of_sectors);
        
        
        // Exiting Routine
        
        printf("Process complete, press [HOME] to EXIT! \n");
        printf("\n");
        
        printf("...EXITING! \n");
        exit(0);
        
        // RiCK's code END //
    
        while(1) {
    
            // Call WPAD_ScanPads each loop, this reads the latest controller states
            WPAD_ScanPads();
    
            // WPAD_ButtonsDown tells us which buttons were pressed in this loop
            // this is a "one shot" state which will not fire again until the button has been released
            u32 pressed = WPAD_ButtonsDown(0);
    
            // We return to the launcher application via exit
            if ( pressed & WPAD_BUTTON_HOME ) exit(0);
    
            // Wait for the next frame
            VIDEO_WaitVSync();
        }
    
        return 0;
    }
    
    
     
  14. JoostinOnline

    JoostinOnline Certified Crash Test Dummy

    Member
    10,955
    3,740
    Apr 2, 2011
    United States
    The Twilight Zone
    No, don't use an * either. It's a pointer. You'd be pointing to a pointer.
     
  15. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    Removing the *'s make it cause way more errors:

    Code:
    > "make" 
    template.c
    c:/projects/wii/FreeSpace/source/template.c: In function 'main':
    c:/projects/wii/FreeSpace/source/template.c:85:2: error: incompatible type for argument 1 of 'USBStorage_ReadCapacity'
    c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'struct usbstorage_handle *' but argument is of type 'usbstorage_handle'
    c:/projects/wii/FreeSpace/source/template.c:85:2: warning: passing argument 3 of 'USBStorage_ReadCapacity' makes pointer from integer without a cast [enabled by default]
    c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32'
    c:/projects/wii/FreeSpace/source/template.c:85:2: warning: passing argument 4 of 'USBStorage_ReadCapacity' makes pointer from integer without a cast [enabled by default]
    c:/devkitPro/libogc/include/ogc/usbstorage.h:77:5: note: expected 'u32 *' but argument is of type 'u32'
    make[1]: *** [template.o] Error 1
    "make": *** [build] Error 2
    
    > Process Exit Code: 2
    > Time Taken: 00:01
    
     
  16. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
    BTW JoostinOnline, I have been studying that Tutorial you linked me too, but it doesn't seem to offer any advanced stuff, like what I am trying to do. I already understand most of the elements it teaches from programming in other languages.
     
  17. JoostinOnline

    JoostinOnline Certified Crash Test Dummy

    Member
    10,955
    3,740
    Apr 2, 2011
    United States
    The Twilight Zone
  18. RiCK420
    OP

    RiCK420 Contributing Member

    Member
    172
    62
    Oct 2, 2013
    United States
  19. JoostinOnline

    JoostinOnline Certified Crash Test Dummy

    Member
    10,955
    3,740
    Apr 2, 2011
    United States
    The Twilight Zone
    I don't know why it's not working. The warnings don't make sense. Anyway, I don't understand why you are jumping into something like this when you don't even know C or C++. You have to start out small. It doesn't matter that you know other languages.

    All my projects are open source if you want to take a look. You can find the source at HacksDen.
     
  20. JoostinOnline

    JoostinOnline Certified Crash Test Dummy

    Member
    10,955
    3,740
    Apr 2, 2011
    United States
    The Twilight Zone
    Okay, took a better look at it, and this is what you need to do:
    Code:
    usbstorage_handle usb_device;
    u32 sector_size, num_of_sectors;
    u8 lun = 0; // Logical Unit Number (partition)
     
    if (!USBStorage_Initialize()) {
        printf("Unable to initialize subsystem. \n");
        printf("\n");
    }
    else {
        printf("Initialized subsystem. \n");
        printf("\n");
    }
     
    USBStorage_ReadCapacity(&usb_device, lun, &sector_size, &num_of_sectors);