Changeset 56cb9bd in mainline for uspace/app/usb/example.c


Ignore:
Timestamp:
2010-10-26T13:49:50Z (13 years ago)
Author:
Vojtech Horky <vojtechhorky@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
fa2a361
Parents:
2185776
Message:

Example of async HC communication

The example `usb' app first sets address of the device listening
on the default address and then (using the new address) asks for
its standard device descriptor which then dumps.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/app/usb/example.c

    r2185776 r56cb9bd  
    6060        (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
    6161
     62#define EXEC2(cmd, fmt, ...) \
     63        do { \
     64                printf("%s: " fmt " = ", NAME, __VA_ARGS__); \
     65                fflush(stdout); \
     66                int _rc = cmd; \
     67                if (_rc != EOK) { \
     68                        printf("E%d\n", _rc); \
     69                        printf("%s: ... aborting.\n", NAME); \
     70                        exit(_rc); \
     71                } \
     72                printf("EOK\n"); \
     73        } while (false)
     74#define EXEC(cmd, fmt, ...) \
     75        EXEC2(cmd(__VA_ARGS__), _QUOTEME(cmd) fmt, __VA_ARGS__)
     76
    6277static void fibril_sleep(size_t sec)
    6378{
     
    7085{
    7186        ipc_answer_0(iid, EOK);
    72         printf("%s: client connection()\n", NAME);
     87        //printf("%s: client connection()\n", NAME);
    7388       
    7489        while (true) {
     
    114129                               
    115130                        case IPC_M_PHONE_HUNGUP:
    116                                 printf("%s: hang-up.\n", NAME);
     131                                //printf("%s: hang-up.\n", NAME);
    117132                                return;
    118133                               
     
    125140}
    126141
     142static void data_dump(uint8_t *data, size_t len)
     143{
     144        size_t i;
     145        for (i = 0; i < len; i++) {
     146                printf("  0x%02X", data[i]);
     147                if (((i > 0) && (((i+1) % 10) == 0))
     148                    || (i + 1 == len)) {
     149                        printf("\n");
     150                }
     151        }
     152}
     153
    127154int main(int argc, char * argv[])
    128155{
     
    134161        }
    135162       
     163        printf("%s: example communication with HCD\n", NAME);
     164       
    136165        usb_target_t target = {0, 0};
     166        usb_handle_t handle;
     167       
     168       
     169       
    137170        usb_device_request_setup_packet_t setup_packet = {
    138171                .request_type = 0,
     
    142175        };
    143176        setup_packet.value = 5;
     177       
     178        printf("\n%s: === setting device address to %d ===\n", NAME,
     179            (int)setup_packet.value);
     180        EXEC2(usb_hcd_async_transfer_control_write_setup(hcd_phone, target,
     181            &setup_packet, sizeof(setup_packet), &handle),
     182            "usb_hcd_async_transfer_control_write_setup(%d, {%d:%d}, &data, %u, &h)",
     183            hcd_phone, target.address, target.endpoint, sizeof(setup_packet));
     184           
     185        EXEC(usb_hcd_async_wait_for, "(h=%x)", handle);
     186       
     187        EXEC2(usb_hcd_async_transfer_control_write_status(hcd_phone, target,
     188            &handle),
     189            "usb_hcd_async_transfer_control_write_status(%d, {%d:%d}, &h)",
     190            hcd_phone, target.address, target.endpoint);
     191       
     192        EXEC(usb_hcd_async_wait_for, "(h=%x)", handle);
     193       
     194        target.address = setup_packet.value;
     195       
     196       
     197        printf("\n%s: === getting standard device descriptor ===\n", NAME);
     198        usb_device_request_setup_packet_t get_descriptor = {
     199                .request_type = 128,
     200                .request = USB_DEVREQ_GET_DESCRIPTOR,
     201                .index = 0,
     202                .length = MAX_SIZE_RECEIVE,
     203        };
     204        get_descriptor.value_low = 0;
     205        get_descriptor.value_high = 1;
     206       
     207        uint8_t descriptor[MAX_SIZE_RECEIVE];
     208        size_t descriptor_length;
     209       
     210        EXEC2(usb_hcd_async_transfer_control_read_setup(hcd_phone, target,
     211            &get_descriptor, sizeof(get_descriptor), &handle),
     212            "usb_hcd_async_transfer_control_read_setup(%d, {%d:%d}, &data, %u, &h)",
     213            hcd_phone, target.address, target.endpoint, sizeof(get_descriptor));
     214       
     215        EXEC(usb_hcd_async_wait_for, "(h=%x)", handle);
     216       
     217        usb_handle_t data_handle;
     218        EXEC2(usb_hcd_async_transfer_control_read_data(hcd_phone, target,
     219            descriptor, MAX_SIZE_RECEIVE, &descriptor_length, &data_handle),
     220            "usb_hcd_async_transfer_control_read_data(%d, {%d:%d}, &data, %u, &len, &h2)",
     221            hcd_phone, target.address, target.endpoint, MAX_SIZE_RECEIVE);
     222       
     223        EXEC2(usb_hcd_async_transfer_control_read_status(hcd_phone, target,
     224            &handle),
     225            "usb_hcd_async_transfer_control_read_status(%d, {%d:%d}, &h)",
     226            hcd_phone, target.address, target.endpoint);
     227       
     228        EXEC(usb_hcd_async_wait_for, "(h=%x)", handle);
     229        EXEC(usb_hcd_async_wait_for, "(h2=%x)", data_handle);
     230       
     231        printf("%s: standard device descriptor dump (%dB):\n", NAME, descriptor_length);
     232        data_dump(descriptor, descriptor_length);
     233       
     234        fibril_sleep(1);
     235       
     236#if 0
    144237        int rc;
    145238       
     
    161254        printf("%s: sleeping for a while...\n", NAME);
    162255        fibril_sleep(5);
    163        
     256#endif
     257
    164258        printf("%s: exiting.\n", NAME);
    165259       
Note: See TracChangeset for help on using the changeset viewer.