1 /* mbed USBHost Library
2 * Copyright (c) 2006-2013 ARM Limited
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #ifndef USBDEVICECONNECTED_H
18 #define USBDEVICECONNECTED_H
21 #include "USBEndpoint.h"
22 #include "USBHostConf.h"
31 uint8_t intf_subclass;
32 uint8_t intf_protocol;
33 USBEndpoint * ep[MAX_ENDPOINT_PER_INTERFACE];
34 FunctionPointer detach;
39 * USBDeviceConnected class
41 class USBDeviceConnected
51 * Attach an USBEndpoint to this device
53 * @param intf_nb interface number
54 * @param ep pointeur on the USBEndpoint which will be attached
55 * @returns true if successful, false otherwise
57 bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep);
60 * Retrieve an USBEndpoint by its TYPE and DIRECTION
62 * @param intf_nb the interface on which to lookup the USBEndpoint
63 * @param type type of the USBEndpoint looked for
64 * @param dir direction of the USBEndpoint looked for
65 * @param index the index of the USBEndpoint whitin the interface
66 * @returns pointer on the USBEndpoint if found, NULL otherwise
68 USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0);
71 * Retrieve an USBEndpoint by its index
73 * @param intf_nb interface number
74 * @param index index of the USBEndpoint
75 * @returns pointer on the USBEndpoint if found, NULL otherwise
77 USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index);
80 * Add a new interface to this device
82 * @param intf_nb interface number
83 * @param intf_class interface class
84 * @param intf_subclass interface subclass
85 * @param intf_protocol interface protocol
86 * @returns true if successful, false otherwise
88 bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol);
91 * Get a specific interface
93 * @param index index of the interface to be fetched
96 INTERFACE * getInterface(uint8_t index);
99 * Attach a member function to call when a the device has been disconnected
101 * @param intf_nb interface number
102 * @param tptr pointer to the object to call the member function on
103 * @param mptr pointer to the member function to be called
106 inline void onDisconnect(uint8_t intf_nb, T* tptr, void (T::*mptr)(void)) {
107 if ((mptr != NULL) && (tptr != NULL)) {
108 intf[intf_nb].detach.attach(tptr, mptr);
113 * Attach a callback called when the device has been disconnected
115 * @param intf_nb interface number
116 * @param fn function pointer
118 inline void onDisconnect(uint8_t intf_nb, void (*fn)(void)) {
120 intf[intf_nb].detach.attach(fn);
125 * Disconnect the device by calling a callback function registered by a driver
130 void init(uint8_t hub, uint8_t port, bool lowSpeed);
131 inline void setAddress(uint8_t addr_) { addr = addr_; };
132 inline void setVid(uint16_t vid_) { vid = vid_; };
133 inline void setPid(uint16_t pid_) { pid = pid_; };
134 inline void setClass(uint8_t device_class_) { device_class = device_class_; };
135 inline void setSubClass(uint8_t device_subclass_) { device_subclass = device_subclass_; };
136 inline void setProtocol(uint8_t pr) { proto = pr; };
137 inline void setSizeControlEndpoint(uint32_t size) { sizeControlEndpoint = size; };
138 inline void activeAddress(bool active) { activeAddr = active; };
139 inline void setEnumerated() { enumerated = true; };
140 inline void setNbIntf(uint8_t nb_intf) {nb_interf = nb_intf; };
141 inline void setHubParent(USBHostHub * hub) { hub_parent = hub; };
142 inline void setName(const char * name_, uint8_t intf_nb) { strcpy(intf[intf_nb].name, name_); };
145 inline uint8_t getPort() { return port; };
146 inline uint8_t getHub() { return hub_nb; };
147 inline uint8_t getAddress() { return addr; };
148 inline uint16_t getVid() { return vid; };
149 inline uint16_t getPid() { return pid; };
150 inline uint8_t getClass() { return device_class; };
151 inline uint8_t getSubClass() { return device_subclass; };
152 inline uint8_t getProtocol() { return proto; };
153 inline bool getSpeed() { return speed; };
154 inline uint32_t getSizeControlEndpoint() { return sizeControlEndpoint; };
155 inline bool isActiveAddress() { return activeAddr; };
156 inline bool isEnumerated() { return enumerated; };
157 inline USBHostHub * getHubParent() { return hub_parent; };
158 inline uint8_t getNbIntf() { return nb_interf; };
159 inline const char * getName(uint8_t intf_nb) { return intf[intf_nb].name; };
161 // in case this device is a hub
165 USBHostHub * hub_parent;
167 INTERFACE intf[MAX_INTF];
168 uint32_t sizeControlEndpoint;
174 uint8_t device_class;
175 uint8_t device_subclass;
178 volatile bool activeAddr;
179 volatile bool enumerated;