]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/USBHost/USBHost/USBDeviceConnected.h
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / USBHost / USBHost / USBDeviceConnected.h
1 /* mbed USBHost Library
2  * Copyright (c) 2006-2013 ARM Limited
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 #ifndef USBDEVICECONNECTED_H
18 #define USBDEVICECONNECTED_H
19
20 #include "stdint.h"
21 #include "USBEndpoint.h"
22 #include "USBHostConf.h"
23 #include "rtos.h"
24
25 class USBHostHub;
26
27 typedef struct {
28     bool in_use;
29     uint8_t nb_endpoint;
30     uint8_t intf_class;
31     uint8_t intf_subclass;
32     uint8_t intf_protocol;
33     USBEndpoint * ep[MAX_ENDPOINT_PER_INTERFACE];
34     FunctionPointer detach;
35     char name[10];
36 } INTERFACE;
37
38 /**
39 * USBDeviceConnected class
40 */
41 class USBDeviceConnected
42 {
43 public:
44
45     /**
46     * Constructor
47     */
48     USBDeviceConnected();
49
50     /**
51     * Attach an USBEndpoint to this device
52     *
53     * @param intf_nb interface number
54     * @param ep pointeur on the USBEndpoint which will be attached
55     * @returns true if successful, false otherwise
56     */
57     bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep);
58
59     /**
60     * Retrieve an USBEndpoint by its TYPE and DIRECTION
61     *
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
67     */
68     USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0);
69
70     /**
71     * Retrieve an USBEndpoint by its index
72     *
73     * @param intf_nb interface number
74     * @param index index of the USBEndpoint
75     * @returns pointer on the USBEndpoint if found, NULL otherwise
76     */
77     USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index);
78
79     /**
80     * Add a new interface to this device
81     *
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
87     */
88     bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol);
89
90     /**
91     * Get a specific interface
92     *
93     * @param index index of the interface to be fetched
94     * @returns interface
95     */
96     INTERFACE * getInterface(uint8_t index);
97
98     /**
99      *  Attach a member function to call when a the device has been disconnected
100      *
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
104      */
105     template<typename T>
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);
109         }
110     }
111
112     /**
113      * Attach a callback called when the device has been disconnected
114      *
115      *  @param intf_nb interface number
116      *  @param fn function pointer
117      */
118     inline void onDisconnect(uint8_t intf_nb, void (*fn)(void)) {
119         if (fn != NULL) {
120             intf[intf_nb].detach.attach(fn);
121         }
122     }
123
124     /**
125     * Disconnect the device by calling a callback function registered by a driver
126     */
127     void disconnect();
128
129     // setters
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_); };
143
144     //getters
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; };
160
161     // in case this device is a hub
162     USBHostHub * hub;
163
164 private:
165     USBHostHub * hub_parent;
166
167     INTERFACE intf[MAX_INTF];
168     uint32_t sizeControlEndpoint;
169     uint8_t hub_nb;
170     uint8_t port;
171     uint16_t vid;
172     uint16_t pid;
173     uint8_t addr;
174     uint8_t device_class;
175     uint8_t device_subclass;
176     uint8_t proto;
177     bool speed;
178     volatile bool activeAddr;
179     volatile bool enumerated;
180     uint8_t nb_interf;
181
182     void init();
183 };
184
185 #endif