1 | /* |
2 | PluggableUSB.h |
3 | Copyright (c) 2015 Arduino LLC |
4 | |
5 | This library is free software; you can redistribute it and/or |
6 | modify it under the terms of the GNU Lesser General Public |
7 | License as published by the Free Software Foundation; either |
8 | version 2.1 of the License, or (at your option) any later version. |
9 | |
10 | This library is distributed in the hope that it will be useful, |
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
13 | Lesser General Public License for more details. |
14 | |
15 | You should have received a copy of the GNU Lesser General Public |
16 | License along with this library; if not, write to the Free Software |
17 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
18 | */ |
19 |
|
20 | #ifndef PUSB_h |
21 | #define PUSB_h |
22 |
|
23 | #include "USBAPI.h" |
24 | #include <stdint.h> |
25 |
|
26 | #if defined(USBCON) |
27 |
|
28 | class PluggableUSBModule { |
29 | public: |
30 | PluggableUSBModule(uint8_t numEps, uint8_t numIfs, uint8_t *epType) : |
31 | numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType) |
32 | { } |
33 |
|
34 | protected: |
35 | virtual bool setup(USBSetup& setup) = 0; |
36 | virtual int getInterface(uint8_t* interfaceCount) = 0; |
37 | virtual int getDescriptor(USBSetup& setup) = 0; |
38 | virtual uint8_t getShortName(char *name) { name[0] = 'A'+pluggedInterface; return 1; } |
39 |
|
40 | uint8_t pluggedInterface; |
41 | uint8_t pluggedEndpoint; |
42 |
|
43 | const uint8_t numEndpoints; |
44 | const uint8_t numInterfaces; |
45 | const uint8_t *endpointType; |
46 |
|
47 | PluggableUSBModule *next = NULL; |
48 |
|
49 | friend class PluggableUSB_; |
50 | }; |
51 |
|
52 | class PluggableUSB_ { |
53 | public: |
54 | PluggableUSB_(); |
55 | bool plug(PluggableUSBModule *node); |
56 | int getInterface(uint8_t* interfaceCount); |
57 | int getDescriptor(USBSetup& setup); |
58 | bool setup(USBSetup& setup); |
59 | void getShortName(char *iSerialNum); |
60 |
|
61 | private: |
62 | uint8_t lastIf; |
63 | uint8_t lastEp; |
64 | PluggableUSBModule* rootNode; |
65 | }; |
66 |
|
67 | // Replacement for global singleton. |
68 | // This function prevents static-initialization-order-fiasco |
69 | // https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use |
70 | PluggableUSB_& PluggableUSB(); |
71 |
|
72 | #endif |
73 |
|
74 | #endif |
75 |
|