2018.07.26
Dependencies: FATFileSystem2 mbed-rtos
Fork of USBHost by
USBHost/USBDeviceConnected.h@43:78f328f311dc, 2018-07-26 (annotated)
- Committer:
- sayzyas
- Date:
- Thu Jul 26 00:19:16 2018 +0000
- Revision:
- 43:78f328f311dc
2018.07.26
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sayzyas | 43:78f328f311dc | 1 | /* mbed USBHost Library |
sayzyas | 43:78f328f311dc | 2 | * Copyright (c) 2006-2013 ARM Limited |
sayzyas | 43:78f328f311dc | 3 | * |
sayzyas | 43:78f328f311dc | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sayzyas | 43:78f328f311dc | 5 | * you may not use this file except in compliance with the License. |
sayzyas | 43:78f328f311dc | 6 | * You may obtain a copy of the License at |
sayzyas | 43:78f328f311dc | 7 | * |
sayzyas | 43:78f328f311dc | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
sayzyas | 43:78f328f311dc | 9 | * |
sayzyas | 43:78f328f311dc | 10 | * Unless required by applicable law or agreed to in writing, software |
sayzyas | 43:78f328f311dc | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
sayzyas | 43:78f328f311dc | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sayzyas | 43:78f328f311dc | 13 | * See the License for the specific language governing permissions and |
sayzyas | 43:78f328f311dc | 14 | * limitations under the License. |
sayzyas | 43:78f328f311dc | 15 | */ |
sayzyas | 43:78f328f311dc | 16 | |
sayzyas | 43:78f328f311dc | 17 | #ifndef USBDEVICECONNECTED_H |
sayzyas | 43:78f328f311dc | 18 | #define USBDEVICECONNECTED_H |
sayzyas | 43:78f328f311dc | 19 | |
sayzyas | 43:78f328f311dc | 20 | #include "stdint.h" |
sayzyas | 43:78f328f311dc | 21 | #include "USBEndpoint.h" |
sayzyas | 43:78f328f311dc | 22 | #include "USBHostConf.h" |
sayzyas | 43:78f328f311dc | 23 | #include "rtos.h" |
sayzyas | 43:78f328f311dc | 24 | |
sayzyas | 43:78f328f311dc | 25 | class USBHostHub; |
sayzyas | 43:78f328f311dc | 26 | |
sayzyas | 43:78f328f311dc | 27 | typedef struct { |
sayzyas | 43:78f328f311dc | 28 | bool in_use; |
sayzyas | 43:78f328f311dc | 29 | uint8_t nb_endpoint; |
sayzyas | 43:78f328f311dc | 30 | uint8_t intf_class; |
sayzyas | 43:78f328f311dc | 31 | uint8_t intf_subclass; |
sayzyas | 43:78f328f311dc | 32 | uint8_t intf_protocol; |
sayzyas | 43:78f328f311dc | 33 | USBEndpoint * ep[MAX_ENDPOINT_PER_INTERFACE]; |
sayzyas | 43:78f328f311dc | 34 | FunctionPointer detach; |
sayzyas | 43:78f328f311dc | 35 | char name[10]; |
sayzyas | 43:78f328f311dc | 36 | } INTERFACE; |
sayzyas | 43:78f328f311dc | 37 | |
sayzyas | 43:78f328f311dc | 38 | /** |
sayzyas | 43:78f328f311dc | 39 | * USBDeviceConnected class |
sayzyas | 43:78f328f311dc | 40 | */ |
sayzyas | 43:78f328f311dc | 41 | class USBDeviceConnected |
sayzyas | 43:78f328f311dc | 42 | { |
sayzyas | 43:78f328f311dc | 43 | public: |
sayzyas | 43:78f328f311dc | 44 | |
sayzyas | 43:78f328f311dc | 45 | /** |
sayzyas | 43:78f328f311dc | 46 | * Constructor |
sayzyas | 43:78f328f311dc | 47 | */ |
sayzyas | 43:78f328f311dc | 48 | USBDeviceConnected(); |
sayzyas | 43:78f328f311dc | 49 | |
sayzyas | 43:78f328f311dc | 50 | /** |
sayzyas | 43:78f328f311dc | 51 | * Attach an USBEndpoint to this device |
sayzyas | 43:78f328f311dc | 52 | * |
sayzyas | 43:78f328f311dc | 53 | * @param intf_nb interface number |
sayzyas | 43:78f328f311dc | 54 | * @param ep pointeur on the USBEndpoint which will be attached |
sayzyas | 43:78f328f311dc | 55 | * @returns true if successful, false otherwise |
sayzyas | 43:78f328f311dc | 56 | */ |
sayzyas | 43:78f328f311dc | 57 | bool addEndpoint(uint8_t intf_nb, USBEndpoint * ep); |
sayzyas | 43:78f328f311dc | 58 | |
sayzyas | 43:78f328f311dc | 59 | /** |
sayzyas | 43:78f328f311dc | 60 | * Retrieve an USBEndpoint by its TYPE and DIRECTION |
sayzyas | 43:78f328f311dc | 61 | * |
sayzyas | 43:78f328f311dc | 62 | * @param intf_nb the interface on which to lookup the USBEndpoint |
sayzyas | 43:78f328f311dc | 63 | * @param type type of the USBEndpoint looked for |
sayzyas | 43:78f328f311dc | 64 | * @param dir direction of the USBEndpoint looked for |
sayzyas | 43:78f328f311dc | 65 | * @param index the index of the USBEndpoint whitin the interface |
sayzyas | 43:78f328f311dc | 66 | * @returns pointer on the USBEndpoint if found, NULL otherwise |
sayzyas | 43:78f328f311dc | 67 | */ |
sayzyas | 43:78f328f311dc | 68 | USBEndpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index = 0); |
sayzyas | 43:78f328f311dc | 69 | |
sayzyas | 43:78f328f311dc | 70 | /** |
sayzyas | 43:78f328f311dc | 71 | * Retrieve an USBEndpoint by its index |
sayzyas | 43:78f328f311dc | 72 | * |
sayzyas | 43:78f328f311dc | 73 | * @param intf_nb interface number |
sayzyas | 43:78f328f311dc | 74 | * @param index index of the USBEndpoint |
sayzyas | 43:78f328f311dc | 75 | * @returns pointer on the USBEndpoint if found, NULL otherwise |
sayzyas | 43:78f328f311dc | 76 | */ |
sayzyas | 43:78f328f311dc | 77 | USBEndpoint * getEndpoint(uint8_t intf_nb, uint8_t index); |
sayzyas | 43:78f328f311dc | 78 | |
sayzyas | 43:78f328f311dc | 79 | /** |
sayzyas | 43:78f328f311dc | 80 | * Add a new interface to this device |
sayzyas | 43:78f328f311dc | 81 | * |
sayzyas | 43:78f328f311dc | 82 | * @param intf_nb interface number |
sayzyas | 43:78f328f311dc | 83 | * @param intf_class interface class |
sayzyas | 43:78f328f311dc | 84 | * @param intf_subclass interface subclass |
sayzyas | 43:78f328f311dc | 85 | * @param intf_protocol interface protocol |
sayzyas | 43:78f328f311dc | 86 | * @returns true if successful, false otherwise |
sayzyas | 43:78f328f311dc | 87 | */ |
sayzyas | 43:78f328f311dc | 88 | bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol); |
sayzyas | 43:78f328f311dc | 89 | |
sayzyas | 43:78f328f311dc | 90 | /** |
sayzyas | 43:78f328f311dc | 91 | * Get a specific interface |
sayzyas | 43:78f328f311dc | 92 | * |
sayzyas | 43:78f328f311dc | 93 | * @param index index of the interface to be fetched |
sayzyas | 43:78f328f311dc | 94 | * @returns interface |
sayzyas | 43:78f328f311dc | 95 | */ |
sayzyas | 43:78f328f311dc | 96 | INTERFACE * getInterface(uint8_t index); |
sayzyas | 43:78f328f311dc | 97 | |
sayzyas | 43:78f328f311dc | 98 | /** |
sayzyas | 43:78f328f311dc | 99 | * Attach a member function to call when a the device has been disconnected |
sayzyas | 43:78f328f311dc | 100 | * |
sayzyas | 43:78f328f311dc | 101 | * @param intf_nb interface number |
sayzyas | 43:78f328f311dc | 102 | * @param tptr pointer to the object to call the member function on |
sayzyas | 43:78f328f311dc | 103 | * @param mptr pointer to the member function to be called |
sayzyas | 43:78f328f311dc | 104 | */ |
sayzyas | 43:78f328f311dc | 105 | template<typename T> |
sayzyas | 43:78f328f311dc | 106 | inline void onDisconnect(uint8_t intf_nb, T* tptr, void (T::*mptr)(void)) { |
sayzyas | 43:78f328f311dc | 107 | if ((mptr != NULL) && (tptr != NULL)) { |
sayzyas | 43:78f328f311dc | 108 | intf[intf_nb].detach.attach(tptr, mptr); |
sayzyas | 43:78f328f311dc | 109 | } |
sayzyas | 43:78f328f311dc | 110 | } |
sayzyas | 43:78f328f311dc | 111 | |
sayzyas | 43:78f328f311dc | 112 | /** |
sayzyas | 43:78f328f311dc | 113 | * Attach a callback called when the device has been disconnected |
sayzyas | 43:78f328f311dc | 114 | * |
sayzyas | 43:78f328f311dc | 115 | * @param intf_nb interface number |
sayzyas | 43:78f328f311dc | 116 | * @param fn function pointer |
sayzyas | 43:78f328f311dc | 117 | */ |
sayzyas | 43:78f328f311dc | 118 | inline void onDisconnect(uint8_t intf_nb, void (*fn)(void)) { |
sayzyas | 43:78f328f311dc | 119 | if (fn != NULL) { |
sayzyas | 43:78f328f311dc | 120 | intf[intf_nb].detach.attach(fn); |
sayzyas | 43:78f328f311dc | 121 | } |
sayzyas | 43:78f328f311dc | 122 | } |
sayzyas | 43:78f328f311dc | 123 | |
sayzyas | 43:78f328f311dc | 124 | /** |
sayzyas | 43:78f328f311dc | 125 | * Disconnect the device by calling a callback function registered by a driver |
sayzyas | 43:78f328f311dc | 126 | */ |
sayzyas | 43:78f328f311dc | 127 | void disconnect(); |
sayzyas | 43:78f328f311dc | 128 | |
sayzyas | 43:78f328f311dc | 129 | // setters |
sayzyas | 43:78f328f311dc | 130 | void init(uint8_t hub, uint8_t port, bool lowSpeed); |
sayzyas | 43:78f328f311dc | 131 | inline void setAddress(uint8_t addr_) { addr = addr_; }; |
sayzyas | 43:78f328f311dc | 132 | inline void setVid(uint16_t vid_) { vid = vid_; }; |
sayzyas | 43:78f328f311dc | 133 | inline void setPid(uint16_t pid_) { pid = pid_; }; |
sayzyas | 43:78f328f311dc | 134 | inline void setClass(uint8_t device_class_) { device_class = device_class_; }; |
sayzyas | 43:78f328f311dc | 135 | inline void setSubClass(uint8_t device_subclass_) { device_subclass = device_subclass_; }; |
sayzyas | 43:78f328f311dc | 136 | inline void setProtocol(uint8_t pr) { proto = pr; }; |
sayzyas | 43:78f328f311dc | 137 | inline void setSizeControlEndpoint(uint32_t size) { sizeControlEndpoint = size; }; |
sayzyas | 43:78f328f311dc | 138 | inline void activeAddress(bool active) { activeAddr = active; }; |
sayzyas | 43:78f328f311dc | 139 | inline void setEnumerated() { enumerated = true; }; |
sayzyas | 43:78f328f311dc | 140 | inline void setNbIntf(uint8_t nb_intf) {nb_interf = nb_intf; }; |
sayzyas | 43:78f328f311dc | 141 | inline void setHubParent(USBHostHub * hub) { hub_parent = hub; }; |
sayzyas | 43:78f328f311dc | 142 | inline void setName(const char * name_, uint8_t intf_nb) { strcpy(intf[intf_nb].name, name_); }; |
sayzyas | 43:78f328f311dc | 143 | |
sayzyas | 43:78f328f311dc | 144 | //getters |
sayzyas | 43:78f328f311dc | 145 | inline uint8_t getPort() { return port; }; |
sayzyas | 43:78f328f311dc | 146 | inline uint8_t getHub() { return hub_nb; }; |
sayzyas | 43:78f328f311dc | 147 | inline uint8_t getAddress() { return addr; }; |
sayzyas | 43:78f328f311dc | 148 | inline uint16_t getVid() { return vid; }; |
sayzyas | 43:78f328f311dc | 149 | inline uint16_t getPid() { return pid; }; |
sayzyas | 43:78f328f311dc | 150 | inline uint8_t getClass() { return device_class; }; |
sayzyas | 43:78f328f311dc | 151 | inline uint8_t getSubClass() { return device_subclass; }; |
sayzyas | 43:78f328f311dc | 152 | inline uint8_t getProtocol() { return proto; }; |
sayzyas | 43:78f328f311dc | 153 | inline bool getSpeed() { return speed; }; |
sayzyas | 43:78f328f311dc | 154 | inline uint32_t getSizeControlEndpoint() { return sizeControlEndpoint; }; |
sayzyas | 43:78f328f311dc | 155 | inline bool isActiveAddress() { return activeAddr; }; |
sayzyas | 43:78f328f311dc | 156 | inline bool isEnumerated() { return enumerated; }; |
sayzyas | 43:78f328f311dc | 157 | inline USBHostHub * getHubParent() { return hub_parent; }; |
sayzyas | 43:78f328f311dc | 158 | inline uint8_t getNbIntf() { return nb_interf; }; |
sayzyas | 43:78f328f311dc | 159 | inline const char * getName(uint8_t intf_nb) { return intf[intf_nb].name; }; |
sayzyas | 43:78f328f311dc | 160 | |
sayzyas | 43:78f328f311dc | 161 | // in case this device is a hub |
sayzyas | 43:78f328f311dc | 162 | USBHostHub * hub; |
sayzyas | 43:78f328f311dc | 163 | |
sayzyas | 43:78f328f311dc | 164 | private: |
sayzyas | 43:78f328f311dc | 165 | USBHostHub * hub_parent; |
sayzyas | 43:78f328f311dc | 166 | |
sayzyas | 43:78f328f311dc | 167 | INTERFACE intf[MAX_INTF]; |
sayzyas | 43:78f328f311dc | 168 | uint32_t sizeControlEndpoint; |
sayzyas | 43:78f328f311dc | 169 | uint8_t hub_nb; |
sayzyas | 43:78f328f311dc | 170 | uint8_t port; |
sayzyas | 43:78f328f311dc | 171 | uint16_t vid; |
sayzyas | 43:78f328f311dc | 172 | uint16_t pid; |
sayzyas | 43:78f328f311dc | 173 | uint8_t addr; |
sayzyas | 43:78f328f311dc | 174 | uint8_t device_class; |
sayzyas | 43:78f328f311dc | 175 | uint8_t device_subclass; |
sayzyas | 43:78f328f311dc | 176 | uint8_t proto; |
sayzyas | 43:78f328f311dc | 177 | bool speed; |
sayzyas | 43:78f328f311dc | 178 | volatile bool activeAddr; |
sayzyas | 43:78f328f311dc | 179 | volatile bool enumerated; |
sayzyas | 43:78f328f311dc | 180 | uint8_t nb_interf; |
sayzyas | 43:78f328f311dc | 181 | |
sayzyas | 43:78f328f311dc | 182 | void init(); |
sayzyas | 43:78f328f311dc | 183 | }; |
sayzyas | 43:78f328f311dc | 184 | |
sayzyas | 43:78f328f311dc | 185 | #endif |