Skip to content

Commit d5ad836

Browse files
committedNov 16, 2017
Move PluggableUSB out of core
The core need to define EP_BUFFER_TYPE (eg: uint8_t uint32_t EP_BUFFER_NAME (eg: _initEndpoints)
1 parent 48eb5d1 commit d5ad836

File tree

2 files changed

+193
-0
lines changed

2 files changed

+193
-0
lines changed
 

‎api/PluggableUSB.cpp

+115
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
PluggableUSB.cpp
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+
#include "USBAPI.h"
21+
#include "PluggableUSB.h"
22+
23+
#if defined(USBCON)
24+
#ifdef PLUGGABLE_USB_ENABLED
25+
26+
extern _EP_BUFFER_TYPE _EP_BUFFER_NAME[];
27+
28+
int PluggableUSB_::getInterface(uint8_t* interfaceCount)
29+
{
30+
int sent = 0;
31+
PluggableUSBModule* node;
32+
for (node = rootNode; node; node = node->next) {
33+
int res = node->getInterface(interfaceCount);
34+
if (res < 0)
35+
return -1;
36+
sent += res;
37+
}
38+
return sent;
39+
}
40+
41+
int PluggableUSB_::getDescriptor(USBSetup& setup)
42+
{
43+
PluggableUSBModule* node;
44+
for (node = rootNode; node; node = node->next) {
45+
int ret = node->getDescriptor(setup);
46+
// ret!=0 -> request has been processed
47+
if (ret)
48+
return ret;
49+
}
50+
return 0;
51+
}
52+
53+
void PluggableUSB_::getShortName(char *iSerialNum)
54+
{
55+
PluggableUSBModule* node;
56+
for (node = rootNode; node; node = node->next) {
57+
iSerialNum += node->getShortName(iSerialNum);
58+
}
59+
*iSerialNum = 0;
60+
}
61+
62+
bool PluggableUSB_::setup(USBSetup& setup)
63+
{
64+
PluggableUSBModule* node;
65+
for (node = rootNode; node; node = node->next) {
66+
if (node->setup(setup)) {
67+
return true;
68+
}
69+
}
70+
return false;
71+
}
72+
73+
bool PluggableUSB_::plug(PluggableUSBModule *node)
74+
{
75+
if ((lastEp + node->numEndpoints) > USB_ENDPOINTS) {
76+
return false;
77+
}
78+
79+
if (!rootNode) {
80+
rootNode = node;
81+
} else {
82+
PluggableUSBModule *current = rootNode;
83+
while (current->next) {
84+
current = current->next;
85+
}
86+
current->next = node;
87+
}
88+
89+
node->pluggedInterface = lastIf;
90+
node->pluggedEndpoint = lastEp;
91+
lastIf += node->numInterfaces;
92+
for (uint8_t i = 0; i < node->numEndpoints; i++) {
93+
_EP_BUFFER_NAME[lastEp] = node->endpointType[i];
94+
lastEp++;
95+
}
96+
return true;
97+
// restart USB layer???
98+
}
99+
100+
PluggableUSB_& PluggableUSB()
101+
{
102+
static PluggableUSB_ obj;
103+
return obj;
104+
}
105+
106+
PluggableUSB_::PluggableUSB_() : lastIf(CDC_ACM_INTERFACE + CDC_INTERFACE_COUNT),
107+
lastEp(CDC_FIRST_ENDPOINT + CDC_ENPOINT_COUNT),
108+
rootNode(NULL)
109+
{
110+
// Empty
111+
}
112+
113+
#endif
114+
115+
#endif /* if defined(USBCON) */

‎api/PluggableUSB.h

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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+
// core need to define
27+
// EP_BUFFER_TYPE
28+
// EP_BUFFER_NAME
29+
30+
#if defined(USBCON)
31+
32+
class PluggableUSBModule {
33+
public:
34+
PluggableUSBModule(uint8_t numEps, uint8_t numIfs, EP_BUFFER_TYPE *epType) :
35+
numEndpoints(numEps), numInterfaces(numIfs), endpointType(epType)
36+
{ }
37+
38+
protected:
39+
virtual bool setup(USBSetup& setup) = 0;
40+
virtual int getInterface(uint8_t* interfaceCount) = 0;
41+
virtual int getDescriptor(USBSetup& setup) = 0;
42+
virtual uint8_t getShortName(char *name) { name[0] = 'A'+pluggedInterface; return 1; }
43+
44+
uint8_t pluggedInterface;
45+
uint8_t pluggedEndpoint;
46+
47+
const uint8_t numEndpoints;
48+
const uint8_t numInterfaces;
49+
const EP_BUFFER_TYPE *endpointType;
50+
51+
PluggableUSBModule *next = NULL;
52+
53+
friend class PluggableUSB_;
54+
};
55+
56+
class PluggableUSB_ {
57+
public:
58+
PluggableUSB_();
59+
bool plug(PluggableUSBModule *node);
60+
int getInterface(uint8_t* interfaceCount);
61+
int getDescriptor(USBSetup& setup);
62+
bool setup(USBSetup& setup);
63+
void getShortName(char *iSerialNum);
64+
65+
private:
66+
uint8_t lastIf;
67+
uint8_t lastEp;
68+
PluggableUSBModule* rootNode;
69+
};
70+
71+
// Replacement for global singleton.
72+
// This function prevents static-initialization-order-fiasco
73+
// https://isocpp.org/wiki/faq/ctors#static-init-order-on-first-use
74+
PluggableUSB_& PluggableUSB();
75+
76+
#endif
77+
78+
#endif

0 commit comments

Comments
 (0)