Numworks Epsilon  1.4.1
Graphing Calculator Operating System
calculator.h
Go to the documentation of this file.
1 #ifndef ION_DEVICE_USB_CALCULATOR_H
2 #define ION_DEVICE_USB_CALCULATOR_H
3 
4 #include "device.h"
5 #include "dfu_interface.h"
6 #include "stack/bos_descriptor.h"
8 #include "stack/descriptor.h"
16 #include "stack/url_descriptor.h"
18 #include <stddef.h>
19 #include <assert.h>
20 
21 namespace Ion {
22 namespace USB {
23 namespace Device {
24 
25 class Calculator : public Device {
26 public:
27  static void PollAndReset(bool exitWithKeyboard); // Return true if reset is needed
28  Calculator(const char * serialNumber) :
29  Device(&m_dfuInterface),
30  m_deviceDescriptor(
31  0x0210, /* bcdUSB: USB Specification Number which the device complies
32  * to. Must be greater than 0x0200 to use the BOS. */
33  0, // bDeviceClass: The class is defined by the interface.
34  0, // bDeviceSUBClass: The subclass is defined by the interface.
35  0, // bDeviceProtocol: The protocol is defined by the interface.
36  64, // bMaxPacketSize0: Maximum packet size for endpoint 0
37  0x0483, // idVendor
38  0xA291, // idProduct
39  0x0100, // bcdDevice: Device Release Number
40  1, // iManufacturer: Index of the manufacturer name string, see m_descriptor
41  2, // iProduct: Index of the product name string, see m_descriptor
42  3, // iSerialNumber: Index of the SerialNumber string, see m_descriptor
43  1), // bNumConfigurations
44  m_dfuFunctionalDescriptor(
45  0b0011, /* bmAttributes:
46  * - bitWillDetach: If true, the device will perform a bus
47  * detach-attach sequence when it receives a DFU_DETACH
48  * request. The host must not issue a USB Reset.
49  * - bitManifestationTolerant: if true, the device is able to
50  * communicate via USB after Manifestation phase. The
51  * manifestation phase implies a reset in the calculator, so,
52  * even if the device is still plugged, it needs to be
53  * re-enumerated to communicate.
54  * - bitCanUpload
55  * - bitCanDnload */
56  0, /* wDetachTimeOut: Time, in milliseconds, that the device in APP
57  * mode will wait after receipt of the DFU_DETACH request before
58  * switching to DFU mode. It does not apply to the calculator.*/
59  2048, // wTransferSize: Maximum number of bytes that the device can accept per control-write transaction
60  0x0100),// bcdDFUVersion
61  m_interfaceDescriptor(
62  0, // bInterfaceNumber
63  k_dfuInterfaceAlternateSetting, // bAlternateSetting
64  0, // bNumEndpoints: Other than endpoint 0
65  0xFE, // bInterfaceClass: DFU (http://www.usb.org/developers/defined_class)
66  1, // bInterfaceSubClass: DFU
67  2, // bInterfaceProtocol: DFU Mode (not DFU Runtime, which would be 1)
68  4, // iInterface: Index of the Interface string, see m_descriptor
69  &m_dfuFunctionalDescriptor),
70  m_configurationDescriptor(
71  9 + 9 + 9, // wTotalLength: configuration descriptor + interface descriptor + dfu functional descriptor lengths
72  1, // bNumInterfaces
73  k_bConfigurationValue, // bConfigurationValue
74  0, // iConfiguration: No string descriptor for the configuration
75  0x80, /* bmAttributes:
76  * Bit 7: Reserved, set to 1
77  * Bit 6: Self Powered
78  * Bit 5: Remote Wakeup (allows the device to wake up the host when the host is in suspend)
79  * Bit 4..0: Reserved, set to 0 */
80  0x32, // bMaxPower: half of the Maximum Power Consumption
81  &m_interfaceDescriptor),
82  m_webUSBPlatformDescriptor(
83  k_webUSBVendorCode,
84  k_webUSBLandingPageIndex),
85  m_bosDescriptor(
86  5 + 24, // wTotalLength: BOS descriptor + webusb platform descriptor lengths
87  1, // bNumDeviceCapabilities
88  &m_webUSBPlatformDescriptor),
89  m_languageStringDescriptor(),
90  m_manufacturerStringDescriptor("NumWorks"),
91  m_productStringDescriptor("NumWorks Calculator"),
92  m_serialNumberStringDescriptor(serialNumber),
93  m_interfaceStringDescriptor("@Flash/0x08000000/04*016Kg,01*064Kg,07*128Kg"),
94  //m_interfaceStringDescriptor("@SRAM/0x20000000/01*256Ke"),
95  /* Switch to this descriptor to use dfu-util to write in the SRAM.
96  * FIXME Should be an alternate Interface. */
97  m_microsoftOSStringDescriptor(k_microsoftOSVendorCode),
98  m_workshopURLDescriptor(URLDescriptor::Scheme::HTTPS, "workshop.numworks.com"),
99  m_extendedCompatIdDescriptor("WINUSB"),
100  m_descriptors{
101  &m_deviceDescriptor, // Type = Device, Index = 0
102  &m_configurationDescriptor, // Type = Configuration, Index = 0
103  &m_languageStringDescriptor, // Type = String, Index = 0
104  &m_manufacturerStringDescriptor, // Type = String, Index = 1
105  &m_productStringDescriptor, // Type = String, Index = 2
106  &m_serialNumberStringDescriptor, // Type = String, Index = 3
107  &m_interfaceStringDescriptor, // Type = String, Index = 4
108  &m_bosDescriptor // Type = BOS, Index = 0
109  },
110  m_dfuInterface(this, &m_ep0, k_dfuInterfaceAlternateSetting)
111  {
112  }
113 protected:
114  virtual Descriptor * descriptor(uint8_t type, uint8_t index) override;
115  virtual void setActiveConfiguration(uint8_t configurationIndex) override {
116  assert(configurationIndex == k_bConfigurationValue);
117  }
118  virtual uint8_t getActiveConfiguration() override {
119  return k_bConfigurationValue;
120  }
121  bool processSetupInRequest(SetupPacket * request, uint8_t * transferBuffer, uint16_t * transferBufferLength, uint16_t transferBufferMaxLength) override;
122 
123 private:
124  static constexpr uint8_t k_bConfigurationValue = 1;
125  static constexpr uint8_t k_dfuInterfaceAlternateSetting = 0;
126  static constexpr uint8_t k_webUSBVendorCode = 1;
127  static constexpr uint8_t k_webUSBLandingPageIndex = 1;
128  static constexpr uint8_t k_microsoftOSVendorCode = 2;
129 
130  // WebUSB and MicrosoftOSDescriptor commands
131  bool getURLCommand(uint8_t * transferBuffer, uint16_t * transferBufferLength, uint16_t transferBufferMaxLength);
132  bool getExtendedCompatIDCommand(uint8_t * transferBuffer, uint16_t * transferBufferLength, uint16_t transferBufferMaxLength);
133 
134  // Descriptors
135  DeviceDescriptor m_deviceDescriptor;
136  DFUFunctionalDescriptor m_dfuFunctionalDescriptor;
137  InterfaceDescriptor m_interfaceDescriptor;
138  ConfigurationDescriptor m_configurationDescriptor;
139  WebUSBPlatformDescriptor m_webUSBPlatformDescriptor;
140  BOSDescriptor m_bosDescriptor;
141  LanguageIDStringDescriptor m_languageStringDescriptor;
142  StringDescriptor m_manufacturerStringDescriptor;
143  StringDescriptor m_productStringDescriptor;
144  StringDescriptor m_serialNumberStringDescriptor;
145  StringDescriptor m_interfaceStringDescriptor;
146  MicrosoftOSStringDescriptor m_microsoftOSStringDescriptor;
147  URLDescriptor m_workshopURLDescriptor;
148  ExtendedCompatIDDescriptor m_extendedCompatIdDescriptor;
149 
150  Descriptor * m_descriptors[8];
151  /* m_descriptors contains only descriptors that sould be returned via the
152  * method descriptor(uint8_t type, uint8_t index), so do not count descriptors
153  * included in other descriptors or returned by other functions. */
154 
155  // Interface
156  DFUInterface m_dfuInterface;
157 };
158 
159 }
160 }
161 }
162 
163 #endif
Calculator(const char *serialNumber)
Definition: calculator.h:28
#define assert(e)
Definition: assert.h:9
virtual uint8_t getActiveConfiguration() override
Definition: calculator.h:118
unsigned short uint16_t
Definition: stdint.h:5
unsigned char uint8_t
Definition: stdint.h:4
virtual Descriptor * descriptor(uint8_t type, uint8_t index) override
Definition: calculator.cpp:42
Definition: app.cpp:5
bool processSetupInRequest(SetupPacket *request, uint8_t *transferBuffer, uint16_t *transferBufferLength, uint16_t transferBufferMaxLength) override
Definition: calculator.cpp:63
virtual void setActiveConfiguration(uint8_t configurationIndex) override
Definition: calculator.h:115
static void PollAndReset(bool exitWithKeyboard)
Definition: calculator.cpp:11
Definition: backlight.h:6