local fork (temporary)

Dependents:   VodafoneUSBModem_bleedingedge2

Fork of USBHostWANDongle_bleedingedge by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers USBDeviceConnected.cpp Source File

USBDeviceConnected.cpp

00001 /* Copyright (c) 2010-2012 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 #include "USBDeviceConnected.h"
00020 
00021 USBDeviceConnected::USBDeviceConnected() {
00022     init();
00023 }
00024 
00025 void USBDeviceConnected::init() {
00026     hub = 0;
00027     port = 0;
00028     vid = 0;
00029     pid = 0;
00030     nb_interf = 0;
00031     enumerated = false;
00032     activeAddr = false;
00033     sizeControlEndpoint = 8;
00034     device_class = 0;
00035     device_subclass = 0;
00036     proto = 0;
00037     speed = false;
00038     for (int i = 0; i < MAX_INTF; i++) {
00039         memset((void *)&intf[i], 0, sizeof(INTERFACE));
00040         intf[i].in_use = false;
00041         for (int j = 0; j < MAX_ENDPOINT_PER_INTERFACE; j++) {
00042             intf[i].ep[j] = NULL;
00043         }
00044     }
00045 }
00046 
00047 INTERFACE * USBDeviceConnected::getInterface(uint8_t index) {
00048     if (index >= MAX_INTF) {
00049         return NULL;
00050     }
00051     return &intf[index];
00052 }
00053 
00054 bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) {
00055     if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use)) {
00056         return false;
00057     }
00058     intf[intf_nb].in_use = true;
00059     intf[intf_nb].intf_class = intf_class;
00060     intf[intf_nb].intf_subclass = intf_subclass;
00061     intf[intf_nb].intf_protocol = intf_protocol;
00062     intf[intf_nb].nb_endpoint = 0;
00063     nb_interf++;
00064     return true;
00065 }
00066 
00067 bool USBDeviceConnected::addEndpoint(uint8_t intf_nb, USBEndpoint * ept) {
00068     if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use == false) || (intf[intf_nb].nb_endpoint >= MAX_ENDPOINT_PER_INTERFACE)) {
00069         return false;
00070     }
00071     intf[intf_nb].nb_endpoint++;
00072 
00073     for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
00074         if (intf[intf_nb].ep[i] == NULL) {
00075             intf[intf_nb].ep[i] = ept;
00076             return true;
00077         }
00078     }
00079     return false;
00080 }
00081 
00082 void USBDeviceConnected::init(uint8_t hub, uint8_t port, bool lowSpeed) {
00083     init();
00084     this->hub = hub;
00085     this->port = port;
00086     this->speed = lowSpeed;
00087 }
00088 
00089 void USBDeviceConnected::disconnect() {
00090     for(int i = 0; i < nb_interf; i++) {
00091         intf[i].detach.call();
00092     }
00093     init();
00094 }
00095 
00096 
00097 
00098 USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index) {
00099     if (intf_nb >= MAX_INTF) {
00100         return NULL;
00101     }
00102     for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) {
00103         if ((intf[intf_nb].ep[i]->getType() == type) && (intf[intf_nb].ep[i]->getDir() == dir)) {
00104             if(index)
00105             {
00106               index--;
00107             }
00108             else
00109             {
00110               return intf[intf_nb].ep[i];
00111             }
00112         }
00113     }
00114     return NULL;
00115 }
00116 
00117 USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, uint8_t index) {
00118     if ((intf_nb >= MAX_INTF) || (index >= MAX_ENDPOINT_PER_INTERFACE)) {
00119         return NULL;
00120     }
00121     return intf[intf_nb].ep[index];
00122 }