|
| 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 "USBHost/USBEndpoint.h" |
| 22 | +#include "USBHost/USBHostConf.h" |
| 23 | +#include "rtos.h" |
| 24 | +#include "Callback.h" |
| 25 | + |
| 26 | +class USBHostHub; |
| 27 | + |
| 28 | +typedef struct { |
| 29 | + bool in_use; |
| 30 | + uint8_t nb_endpoint; |
| 31 | + uint8_t intf_class; |
| 32 | + uint8_t intf_subclass; |
| 33 | + uint8_t intf_protocol; |
| 34 | + USBEndpoint * ep[MAX_ENDPOINT_PER_INTERFACE]; |
| 35 | + mbed::Callback<void()> detach; |
| 36 | + char name[10]; |
| 37 | +} INTERFACE; |
| 38 | + |
| 39 | +/** |
| 40 | +* USBDeviceConnected class |
| 41 | +*/ |
| 42 | +class USBDeviceConnected |
| 43 | +{ |
| 44 | +public: |
| 45 | + |
| 46 | + /** |
| 47 | + * Constructor |
| 48 | + */ |
| 49 | + USBDeviceConnected(); |
| 50 | + |
| 51 | + /** |
| 52 | + * Attach an USBEndpoint to this device |
| 53 | + * |
| 54 | + * @param intf_nb interface number |
| 55 | + * @param ep pointeur on the USBEndpoint which will be attached |
| 56 | + * @returns true if successful, false otherwise |
| 57 | + */ |
| 58 | + bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep); |
| 59 | + |
| 60 | + /** |
| 61 | + * Retrieve an USBEndpoint by its TYPE and DIRECTION |
| 62 | + * |
| 63 | + * @param intf_nb the interface on which to lookup the USBEndpoint |
| 64 | + * @param type type of the USBEndpoint looked for |
| 65 | + * @param dir direction of the USBEndpoint looked for |
| 66 | + * @param index the index of the USBEndpoint whitin the interface |
| 67 | + * @returns pointer on the USBEndpoint if found, NULL otherwise |
| 68 | + */ |
| 69 | + USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0); |
| 70 | + |
| 71 | + /** |
| 72 | + * Retrieve an USBEndpoint by its index |
| 73 | + * |
| 74 | + * @param intf_nb interface number |
| 75 | + * @param index index of the USBEndpoint |
| 76 | + * @returns pointer on the USBEndpoint if found, NULL otherwise |
| 77 | + */ |
| 78 | + USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index); |
| 79 | + |
| 80 | + /** |
| 81 | + * Add a new interface to this device |
| 82 | + * |
| 83 | + * @param intf_nb interface number |
| 84 | + * @param intf_class interface class |
| 85 | + * @param intf_subclass interface subclass |
| 86 | + * @param intf_protocol interface protocol |
| 87 | + * @returns true if successful, false otherwise |
| 88 | + */ |
| 89 | + bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); |
| 90 | + |
| 91 | + /** |
| 92 | + * Get a specific interface |
| 93 | + * |
| 94 | + * @param index index of the interface to be fetched |
| 95 | + * @returns interface |
| 96 | + */ |
| 97 | + INTERFACE * getInterface(uint8_t index); |
| 98 | + |
| 99 | + /** |
| 100 | + * Attach a member function to call when a the device has been disconnected |
| 101 | + * |
| 102 | + * @param intf_nb interface number |
| 103 | + * @param tptr pointer to the object to call the member function on |
| 104 | + * @param mptr pointer to the member function to be called |
| 105 | + */ |
| 106 | + template<typename T> |
| 107 | + inline void onDisconnect(uint8_t intf_nb, T* tptr, void (T::*mptr)(void)) { |
| 108 | + if ((mptr != NULL) && (tptr != NULL)) { |
| 109 | + intf[intf_nb].detach = mbed::callback(tptr, mptr); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + /** |
| 114 | + * Attach a callback called when the device has been disconnected |
| 115 | + * |
| 116 | + * @param intf_nb interface number |
| 117 | + * @param fn function pointer |
| 118 | + */ |
| 119 | + inline void onDisconnect(uint8_t intf_nb, void (*fn)(void)) { |
| 120 | + if (fn != NULL) { |
| 121 | + intf[intf_nb].detach = fn; |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + /** |
| 126 | + * Disconnect the device by calling a callback function registered by a driver |
| 127 | + */ |
| 128 | + void disconnect(); |
| 129 | + |
| 130 | + // setters |
| 131 | + void init(uint8_t hub, uint8_t port, bool lowSpeed); |
| 132 | + inline void setAddress(uint8_t addr_) { addr = addr_; }; |
| 133 | + inline void setVid(uint16_t vid_) { vid = vid_; }; |
| 134 | + inline void setPid(uint16_t pid_) { pid = pid_; }; |
| 135 | + inline void setClass(uint8_t device_class_) { device_class = device_class_; }; |
| 136 | + inline void setSubClass(uint8_t device_subclass_) { device_subclass = device_subclass_; }; |
| 137 | + inline void setProtocol(uint8_t pr) { proto = pr; }; |
| 138 | + inline void setSizeControlEndpoint(uint32_t size) { sizeControlEndpoint = size; }; |
| 139 | + inline void activeAddress(bool active) { activeAddr = active; }; |
| 140 | + inline void setEnumerated() { enumerated = true; }; |
| 141 | + inline void setNbIntf(uint8_t nb_intf) {nb_interf = nb_intf; }; |
| 142 | + inline void setHubParent(USBHostHub * hub) { hub_parent = hub; }; |
| 143 | + inline void setName(const char * name_, uint8_t intf_nb) { strcpy(intf[intf_nb].name, name_); }; |
| 144 | + |
| 145 | + //getters |
| 146 | + inline uint8_t getPort() { return port; }; |
| 147 | + inline uint8_t getHub() { return hub_nb; }; |
| 148 | + inline uint8_t getAddress() { return addr; }; |
| 149 | + inline uint16_t getVid() { return vid; }; |
| 150 | + inline uint16_t getPid() { return pid; }; |
| 151 | + inline uint8_t getClass() { return device_class; }; |
| 152 | + inline uint8_t getSubClass() { return device_subclass; }; |
| 153 | + inline uint8_t getProtocol() { return proto; }; |
| 154 | + inline bool getSpeed() { return speed; }; |
| 155 | + inline uint32_t getSizeControlEndpoint() { return sizeControlEndpoint; }; |
| 156 | + inline bool isActiveAddress() { return activeAddr; }; |
| 157 | + inline bool isEnumerated() { return enumerated; }; |
| 158 | + inline USBHostHub * getHubParent() { return hub_parent; }; |
| 159 | + inline uint8_t getNbIntf() { return nb_interf; }; |
| 160 | + inline const char * getName(uint8_t intf_nb) { return intf[intf_nb].name; }; |
| 161 | + |
| 162 | + // in case this device is a hub |
| 163 | + USBHostHub * hub; |
| 164 | + |
| 165 | +private: |
| 166 | + USBHostHub * hub_parent; |
| 167 | + |
| 168 | + INTERFACE intf[MAX_INTF]; |
| 169 | + uint32_t sizeControlEndpoint; |
| 170 | + uint8_t hub_nb; |
| 171 | + uint8_t port; |
| 172 | + uint16_t vid; |
| 173 | + uint16_t pid; |
| 174 | + uint8_t addr; |
| 175 | + uint8_t device_class; |
| 176 | + uint8_t device_subclass; |
| 177 | + uint8_t proto; |
| 178 | + bool speed; |
| 179 | + volatile bool activeAddr; |
| 180 | + volatile bool enumerated; |
| 181 | + uint8_t nb_interf; |
| 182 | + |
| 183 | + void init(); |
| 184 | +}; |
| 185 | + |
| 186 | +#endif |
0 commit comments