Nicholas Herriot / VodafoneK3770Lib
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBDeviceConnected.h Source File

USBDeviceConnected.h

00001 /* Copyright (c) 2010-2011 mbed.org, MIT License
00002 *
00003 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00004 * and associated documentation files (the "Software"), to deal in the Software without
00005 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00006 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00007 * Software is furnished to do so, subject to the following conditions:
00008 *
00009 * The above copyright notice and this permission notice shall be included in all copies or
00010 * substantial portions of the Software.
00011 *
00012 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00013 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00014 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00015 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00016 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00017 */
00018 
00019 #ifndef USBDEVICECONNECTED_H
00020 #define USBDEVICECONNECTED_H
00021 
00022 #include "stdint.h"
00023 #include "Endpoint.h"
00024 
00025 #define MAX_ENDPOINT_PER_INTERFACE  4
00026 #define MAX_INTF                    4
00027 
00028 typedef struct {
00029     bool in_use;
00030     uint8_t nb_endpoint;
00031     uint8_t intf_class;
00032     uint8_t intf_subclass;
00033     uint8_t intf_protocol;
00034     Endpoint * ep[MAX_ENDPOINT_PER_INTERFACE];
00035     FunctionPointer detach;
00036 }INTERFACE;
00037 
00038 
00039 class USBDeviceConnected {
00040 public:
00041 
00042     /*
00043     * Constructor
00044     */
00045     USBDeviceConnected();
00046 
00047     /*
00048     * Attach an endpoint to this device
00049     *
00050     * @param ep pointeur on the endpoint which will be attached
00051     * @returns true if successful, false otherwise
00052     */
00053     bool addEndpoint(uint8_t intf_nb, Endpoint * ep);
00054 
00055     /*
00056     * Retrieve an endpoint by its TYPE and DIRECTION
00057     *
00058     * @param type type of the endpoint looked for
00059     * @param direction of the endpoint looked for
00060     * @returns pointer on the endpoint if found, NULL otherwise
00061     */
00062     Endpoint * getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir);
00063     
00064     /*
00065     * Retrieve an endpoint by its index
00066     *
00067     * @param index index of the endpoint
00068     * @returns pointer on the endpoint if found, NULL otherwise
00069     */
00070     Endpoint * getEndpoint(uint8_t intf_nb, uint8_t index);
00071     
00072     bool addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol);
00073     
00074     uint8_t getNbInterface() {return nb_interf;};
00075     
00076     INTERFACE * getInterface(uint8_t index);
00077 
00078     /**
00079      *  Attach a member function to call when a the device has been disconnected
00080      *
00081      *  @param tptr pointer to the object to call the member function on
00082      *  @param mptr pointer to the member function to be called
00083      */
00084     template<typename T>
00085     void onDisconnect(uint8_t intf_nb, T* tptr, void (T::*mptr)(void)) {
00086         if ((mptr != NULL) && (tptr != NULL)) {
00087             intf[intf_nb].detach.attach(tptr, mptr);
00088         }
00089     }
00090 
00091     /**
00092      * Attach a callback called when the device has been disconnected
00093      *
00094      * @param fptr function pointer
00095      */
00096     void onDisconnect(uint8_t intf_nb, void (*fn)(void)) {
00097         if (fn != NULL) {
00098             intf[intf_nb].detach.attach(fn);
00099         }
00100     }
00101 
00102     /*
00103     * Disconnect the device by calling a callback function registered by a driver
00104     */
00105     void disconnect();
00106 
00107     /*
00108     * Setters
00109     */
00110     void init(uint8_t hub, uint8_t port, bool lowSpeed);
00111     void setAddress(uint8_t addr) {
00112         this->addr = addr;
00113     };
00114     void setVid(uint16_t vid) {
00115         this->vid = vid;
00116     };
00117     void setPid(uint16_t pid) {
00118         this->pid = pid;
00119     };
00120     void setClass(uint8_t device_class) {
00121         this->device_class = device_class;
00122     };
00123     void setSubClass(uint8_t device_subclass) {
00124         this->device_subclass = device_subclass;
00125     };
00126     void setProtocol(uint8_t pr) {
00127         proto = pr;
00128     };
00129     void setSizeControlEndpoint(uint32_t size) {
00130         sizeControlEndpoint = size;
00131     };
00132     void activeAddress() {
00133         activeAddr = true;
00134     };
00135     void setEnumerated() {
00136         enumerated = true;
00137     };
00138 
00139     /*
00140     * Getters
00141     */
00142     uint8_t     getPort() {
00143         return port;
00144     };
00145     uint8_t     getHub() {
00146         return hub;
00147     };
00148     uint8_t     getAddress() {
00149         return addr;
00150     };
00151     uint16_t    getVid() {
00152         return vid;
00153     };
00154     uint16_t    getPid() {
00155         return pid;
00156     };
00157     uint8_t     getClass() {
00158         return device_class;
00159     };
00160     uint8_t     getSubClass() {
00161         return device_subclass;
00162     };
00163     uint8_t     getProtocol() {
00164         return proto;
00165     };
00166     bool        getSpeed() {
00167         return speed;
00168     };
00169     uint32_t    getSizeControlEndpoint() {
00170         return sizeControlEndpoint;
00171     };
00172     bool        isActiveAddress() {
00173         return activeAddr;
00174     };
00175     bool        isEnumerated() {
00176         return enumerated;
00177     };
00178 
00179 
00180 private:
00181     INTERFACE intf[MAX_INTF];
00182     //Endpoint * ep[MAX_ENDPOINT_PER_DEVICE];
00183     uint32_t sizeControlEndpoint;
00184     uint8_t hub;
00185     uint8_t port;
00186     uint16_t vid;
00187     uint16_t pid;
00188     uint8_t addr;
00189     uint8_t device_class;
00190     uint8_t device_subclass;
00191     uint8_t proto;
00192     bool speed;
00193     bool activeAddr;
00194     bool enumerated;
00195     
00196     uint8_t nb_interf;
00197 
00198     
00199     void init();
00200 
00201 };
00202 
00203 #endif