USB Host Library for Sprint Dongles
Fork of USBHostWANDongleSprint by
USBHost/USBDeviceConnected.cpp@9:2a7b7333245f, 2012-12-10 (annotated)
- Committer:
- donatien
- Date:
- Mon Dec 10 18:18:35 2012 +0000
- Revision:
- 9:2a7b7333245f
- Parent:
- 0:bfed5767d0a5
Merge
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:bfed5767d0a5 | 1 | /* Copyright (c) 2010-2012 mbed.org, MIT License |
donatien | 0:bfed5767d0a5 | 2 | * |
donatien | 0:bfed5767d0a5 | 3 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 0:bfed5767d0a5 | 4 | * and associated documentation files (the "Software"), to deal in the Software without |
donatien | 0:bfed5767d0a5 | 5 | * restriction, including without limitation the rights to use, copy, modify, merge, publish, |
donatien | 0:bfed5767d0a5 | 6 | * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the |
donatien | 0:bfed5767d0a5 | 7 | * Software is furnished to do so, subject to the following conditions: |
donatien | 0:bfed5767d0a5 | 8 | * |
donatien | 0:bfed5767d0a5 | 9 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 0:bfed5767d0a5 | 10 | * substantial portions of the Software. |
donatien | 0:bfed5767d0a5 | 11 | * |
donatien | 0:bfed5767d0a5 | 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 0:bfed5767d0a5 | 13 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 0:bfed5767d0a5 | 14 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 0:bfed5767d0a5 | 15 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 0:bfed5767d0a5 | 16 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 0:bfed5767d0a5 | 17 | */ |
donatien | 0:bfed5767d0a5 | 18 | |
donatien | 0:bfed5767d0a5 | 19 | #include "USBDeviceConnected.h" |
donatien | 0:bfed5767d0a5 | 20 | |
donatien | 0:bfed5767d0a5 | 21 | USBDeviceConnected::USBDeviceConnected() { |
donatien | 0:bfed5767d0a5 | 22 | init(); |
donatien | 0:bfed5767d0a5 | 23 | } |
donatien | 0:bfed5767d0a5 | 24 | |
donatien | 0:bfed5767d0a5 | 25 | void USBDeviceConnected::init() { |
donatien | 0:bfed5767d0a5 | 26 | hub = 0; |
donatien | 0:bfed5767d0a5 | 27 | port = 0; |
donatien | 0:bfed5767d0a5 | 28 | vid = 0; |
donatien | 0:bfed5767d0a5 | 29 | pid = 0; |
donatien | 0:bfed5767d0a5 | 30 | nb_interf = 0; |
donatien | 0:bfed5767d0a5 | 31 | enumerated = false; |
donatien | 0:bfed5767d0a5 | 32 | activeAddr = false; |
donatien | 0:bfed5767d0a5 | 33 | sizeControlEndpoint = 8; |
donatien | 0:bfed5767d0a5 | 34 | device_class = 0; |
donatien | 0:bfed5767d0a5 | 35 | device_subclass = 0; |
donatien | 0:bfed5767d0a5 | 36 | proto = 0; |
donatien | 0:bfed5767d0a5 | 37 | speed = false; |
donatien | 0:bfed5767d0a5 | 38 | for (int i = 0; i < MAX_INTF; i++) { |
donatien | 0:bfed5767d0a5 | 39 | memset((void *)&intf[i], 0, sizeof(INTERFACE)); |
donatien | 0:bfed5767d0a5 | 40 | intf[i].in_use = false; |
donatien | 0:bfed5767d0a5 | 41 | for (int j = 0; j < MAX_ENDPOINT_PER_INTERFACE; j++) { |
donatien | 0:bfed5767d0a5 | 42 | intf[i].ep[j] = NULL; |
donatien | 0:bfed5767d0a5 | 43 | } |
donatien | 0:bfed5767d0a5 | 44 | } |
donatien | 0:bfed5767d0a5 | 45 | } |
donatien | 0:bfed5767d0a5 | 46 | |
donatien | 0:bfed5767d0a5 | 47 | INTERFACE * USBDeviceConnected::getInterface(uint8_t index) { |
donatien | 0:bfed5767d0a5 | 48 | if (index >= MAX_INTF) { |
donatien | 0:bfed5767d0a5 | 49 | return NULL; |
donatien | 0:bfed5767d0a5 | 50 | } |
donatien | 0:bfed5767d0a5 | 51 | return &intf[index]; |
donatien | 0:bfed5767d0a5 | 52 | } |
donatien | 0:bfed5767d0a5 | 53 | |
donatien | 0:bfed5767d0a5 | 54 | bool USBDeviceConnected::addInterface(uint8_t intf_nb, uint8_t intf_class, uint8_t intf_subclass, uint8_t intf_protocol) { |
donatien | 0:bfed5767d0a5 | 55 | if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use)) { |
donatien | 0:bfed5767d0a5 | 56 | return false; |
donatien | 0:bfed5767d0a5 | 57 | } |
donatien | 0:bfed5767d0a5 | 58 | intf[intf_nb].in_use = true; |
donatien | 0:bfed5767d0a5 | 59 | intf[intf_nb].intf_class = intf_class; |
donatien | 0:bfed5767d0a5 | 60 | intf[intf_nb].intf_subclass = intf_subclass; |
donatien | 0:bfed5767d0a5 | 61 | intf[intf_nb].intf_protocol = intf_protocol; |
donatien | 0:bfed5767d0a5 | 62 | intf[intf_nb].nb_endpoint = 0; |
donatien | 0:bfed5767d0a5 | 63 | nb_interf++; |
donatien | 0:bfed5767d0a5 | 64 | return true; |
donatien | 0:bfed5767d0a5 | 65 | } |
donatien | 0:bfed5767d0a5 | 66 | |
donatien | 0:bfed5767d0a5 | 67 | bool USBDeviceConnected::addEndpoint(uint8_t intf_nb, USBEndpoint * ept) { |
donatien | 0:bfed5767d0a5 | 68 | if ((intf_nb >= MAX_INTF) || (intf[intf_nb].in_use == false) || (intf[intf_nb].nb_endpoint >= MAX_ENDPOINT_PER_INTERFACE)) { |
donatien | 0:bfed5767d0a5 | 69 | return false; |
donatien | 0:bfed5767d0a5 | 70 | } |
donatien | 0:bfed5767d0a5 | 71 | intf[intf_nb].nb_endpoint++; |
donatien | 0:bfed5767d0a5 | 72 | |
donatien | 0:bfed5767d0a5 | 73 | for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) { |
donatien | 0:bfed5767d0a5 | 74 | if (intf[intf_nb].ep[i] == NULL) { |
donatien | 0:bfed5767d0a5 | 75 | intf[intf_nb].ep[i] = ept; |
donatien | 0:bfed5767d0a5 | 76 | return true; |
donatien | 0:bfed5767d0a5 | 77 | } |
donatien | 0:bfed5767d0a5 | 78 | } |
donatien | 0:bfed5767d0a5 | 79 | return false; |
donatien | 0:bfed5767d0a5 | 80 | } |
donatien | 0:bfed5767d0a5 | 81 | |
donatien | 0:bfed5767d0a5 | 82 | void USBDeviceConnected::init(uint8_t hub, uint8_t port, bool lowSpeed) { |
donatien | 0:bfed5767d0a5 | 83 | init(); |
donatien | 0:bfed5767d0a5 | 84 | this->hub = hub; |
donatien | 0:bfed5767d0a5 | 85 | this->port = port; |
donatien | 0:bfed5767d0a5 | 86 | this->speed = lowSpeed; |
donatien | 0:bfed5767d0a5 | 87 | } |
donatien | 0:bfed5767d0a5 | 88 | |
donatien | 0:bfed5767d0a5 | 89 | void USBDeviceConnected::disconnect() { |
donatien | 0:bfed5767d0a5 | 90 | for(int i = 0; i < nb_interf; i++) { |
donatien | 0:bfed5767d0a5 | 91 | intf[i].detach.call(); |
donatien | 0:bfed5767d0a5 | 92 | } |
donatien | 0:bfed5767d0a5 | 93 | init(); |
donatien | 0:bfed5767d0a5 | 94 | } |
donatien | 0:bfed5767d0a5 | 95 | |
donatien | 0:bfed5767d0a5 | 96 | |
donatien | 0:bfed5767d0a5 | 97 | |
donatien | 0:bfed5767d0a5 | 98 | USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, ENDPOINT_TYPE type, ENDPOINT_DIRECTION dir, uint8_t index) { |
donatien | 0:bfed5767d0a5 | 99 | if (intf_nb >= MAX_INTF) { |
donatien | 0:bfed5767d0a5 | 100 | return NULL; |
donatien | 0:bfed5767d0a5 | 101 | } |
donatien | 0:bfed5767d0a5 | 102 | for (int i = 0; i < MAX_ENDPOINT_PER_INTERFACE; i++) { |
donatien | 0:bfed5767d0a5 | 103 | if ((intf[intf_nb].ep[i]->getType() == type) && (intf[intf_nb].ep[i]->getDir() == dir)) { |
donatien | 0:bfed5767d0a5 | 104 | if(index) |
donatien | 0:bfed5767d0a5 | 105 | { |
donatien | 0:bfed5767d0a5 | 106 | index--; |
donatien | 0:bfed5767d0a5 | 107 | } |
donatien | 0:bfed5767d0a5 | 108 | else |
donatien | 0:bfed5767d0a5 | 109 | { |
donatien | 0:bfed5767d0a5 | 110 | return intf[intf_nb].ep[i]; |
donatien | 0:bfed5767d0a5 | 111 | } |
donatien | 0:bfed5767d0a5 | 112 | } |
donatien | 0:bfed5767d0a5 | 113 | } |
donatien | 0:bfed5767d0a5 | 114 | return NULL; |
donatien | 0:bfed5767d0a5 | 115 | } |
donatien | 0:bfed5767d0a5 | 116 | |
donatien | 0:bfed5767d0a5 | 117 | USBEndpoint * USBDeviceConnected::getEndpoint(uint8_t intf_nb, uint8_t index) { |
donatien | 0:bfed5767d0a5 | 118 | if ((intf_nb >= MAX_INTF) || (index >= MAX_ENDPOINT_PER_INTERFACE)) { |
donatien | 0:bfed5767d0a5 | 119 | return NULL; |
donatien | 0:bfed5767d0a5 | 120 | } |
donatien | 0:bfed5767d0a5 | 121 | return intf[intf_nb].ep[index]; |
donatien | 0:bfed5767d0a5 | 122 | } |