source: mainline/uspace/app/usb/example.c@ ca07cd3

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since ca07cd3 was b8a3cda, checked in by Vojtech Horky <vojtechhorky@…>, 15 years ago

VHCD handles new methods

Not completely, though. Will finish later.

  • Property mode set to 100644
File size: 4.7 KB
Line 
1/*
2 * Copyright (c) 2010 Vojtech Horky
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/** @addtogroup usb
30 * @{
31 */
32/**
33 * @file
34 * @brief Simple application that connects to USB/HCD.
35 */
36
37#include <stdio.h>
38#include <stdlib.h>
39#include <unistd.h>
40#include <vfs/vfs.h>
41#include <fcntl.h>
42#include <errno.h>
43#include <str_error.h>
44#include <bool.h>
45#include <async.h>
46
47#include <usb/hcd.h>
48#include <usb/devreq.h>
49
50#define LOOPS 5
51#define MAX_SIZE_RECEIVE 64
52#define NAME "hcd-example"
53
54#define DEV_HCD_NAME "hcd-virt"
55
56#define __QUOTEME(x) #x
57#define _QUOTEME(x) __QUOTEME(x)
58
59#define VERBOSE_EXEC(cmd, fmt, ...) \
60 (printf("%s: %s" fmt "\n", NAME, _QUOTEME(cmd), __VA_ARGS__), cmd(__VA_ARGS__))
61
62static void fibril_sleep(size_t sec)
63{
64 while (sec-- > 0) {
65 async_usleep(1000*1000);
66 }
67}
68
69static void client_connection(ipc_callid_t iid, ipc_call_t *icall)
70{
71 ipc_answer_0(iid, EOK);
72 printf("%s: client connection()\n", NAME);
73
74 while (true) {
75 ipc_callid_t callid;
76 ipc_call_t call;
77 int rc;
78 void * buffer;
79 size_t len;
80
81 callid = async_get_call(&call);
82
83 switch (IPC_GET_METHOD(call)) {
84 case IPC_M_USB_HCD_DATA_SENT:
85 printf("%s: >> Data sent over USB (handle %d, outcome %s).\n",
86 NAME, IPC_GET_ARG1(call),
87 usb_str_transaction_outcome(IPC_GET_ARG2(call)));
88 ipc_answer_0(callid, EOK);
89 break;
90
91 case IPC_M_USB_HCD_DATA_RECEIVED:
92 printf("%s: << Data received over USB (handle %d, outcome %s).\n",
93 NAME, IPC_GET_ARG1(call),
94 usb_str_transaction_outcome(IPC_GET_ARG2(call)));
95 if (IPC_GET_ARG2(call) != USB_OUTCOME_OK) {
96 ipc_answer_0(callid, EOK);
97 break;
98 }
99 len = IPC_GET_ARG3(call);
100 if (len > 0) {
101 rc = async_data_write_accept(&buffer, false,
102 1, MAX_SIZE_RECEIVE,
103 0, &len);
104 if (rc != EOK) {
105 ipc_answer_0(callid, rc);
106 break;
107 }
108 free(buffer);
109 }
110 printf("%s: << Received %uB long buffer (handle %d).\n",
111 NAME, len, IPC_GET_ARG1(call));
112 ipc_answer_0(callid, EOK);
113 break;
114
115 case IPC_M_PHONE_HUNGUP:
116 printf("%s: hang-up.\n", NAME);
117 return;
118
119 default:
120 printf("%s: method %d called.\n", NAME, IPC_GET_METHOD(call));
121 ipc_answer_0(callid, EOK);
122 break;
123 }
124 }
125}
126
127int main(int argc, char * argv[])
128{
129 int hcd_phone = usb_hcd_create_phones(DEV_HCD_NAME, client_connection);
130 if (hcd_phone < 0) {
131 printf("%s: Unable to start comunication with HCD at usb://%s (%d: %s).\n",
132 NAME, DEV_HCD_NAME, hcd_phone, str_error(hcd_phone));
133 return 1;
134 }
135
136 usb_target_t target = {0, 0};
137 usb_device_request_setup_packet_t setup_packet = {
138 .request_type = 0,
139 .request = USB_DEVREQ_SET_ADDRESS,
140 .index = 0,
141 .length = 0,
142 };
143 setup_packet.value = 5;
144 int rc;
145
146 printf("%s: usb_hcd_transfer_control_write_setup(...)\n", NAME);
147 rc = usb_hcd_transfer_control_write_setup(hcd_phone, target,
148 &setup_packet, sizeof(setup_packet), NULL);
149 if (rc != EOK) {
150 printf("%s: failed setting address (%d).\n", NAME, rc);
151 return rc;
152 }
153
154 printf("%s: usb_hcd_transfer_control_write_status(...)\n", NAME);
155 rc = usb_hcd_transfer_control_write_status(hcd_phone, target, NULL);
156 if (rc != EOK) {
157 printf("%s: failed completing control transfer (%d).\n", NAME, rc);
158 return rc;
159 }
160
161 printf("%s: sleeping for a while...\n", NAME);
162 fibril_sleep(5);
163
164 printf("%s: exiting.\n", NAME);
165
166 ipc_hangup(hcd_phone);
167
168 return 0;
169}
170
171
172/** @}
173 */
Note: See TracBrowser for help on using the repository browser.