USB Host Shield 2.0
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
adk.h
Go to the documentation of this file.
1 /* Copyright (C) 2011 Circuits At Home, LTD. All rights reserved.
2 
3 This software may be distributed and modified under the terms of the GNU
4 General Public License version 2 (GPL2) as published by the Free Software
5 Foundation and appearing in the file GPL2.TXT included in the packaging of
6 this file. Please note that GPL2 Section 2[b] requires that all works based
7 on this software must also be made publicly available under the terms of
8 the GPL2 ("Copyleft").
9 
10 Contact information
11 -------------------
12 
13 Circuits At Home, LTD
14 Web : http://www.circuitsathome.com
15 e-mail : support@circuitsathome.com
16  */
17 
18 /* Google ADK interface support header */
19 
20 #if !defined(_ADK_H_)
21 #define _ADK_H_
22 
23 #include <inttypes.h>
24 #include <avr/pgmspace.h>
25 #include "avrpins.h"
26 #include "max3421e.h"
27 #include "usbhost.h"
28 #include "usb_ch9.h"
29 #include "Usb.h"
30 
31 #if defined(ARDUINO) && ARDUINO >=100
32 #include "Arduino.h"
33 #else
34 #include <WProgram.h>
35 #endif
36 
37 #include "printhex.h"
38 #include "hexdump.h"
39 #include "message.h"
40 
41 #include "confdescparser.h"
42 
43 #define ADK_VID 0x18D1
44 #define ADK_PID 0x2D00
45 #define ADB_PID 0x2D01
46 
47 /* requests */
48 
49 #define ADK_GETPROTO 51 //check USB accessory protocol version
50 #define ADK_SENDSTR 52 //send identifying string
51 #define ADK_ACCSTART 53 //start device in accessory mode
52 
53 #define bmREQ_ADK_GET USB_SETUP_DEVICE_TO_HOST|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE
54 #define bmREQ_ADK_SEND USB_SETUP_HOST_TO_DEVICE|USB_SETUP_TYPE_VENDOR|USB_SETUP_RECIPIENT_DEVICE
55 
56 #define ACCESSORY_STRING_MANUFACTURER 0
57 #define ACCESSORY_STRING_MODEL 1
58 #define ACCESSORY_STRING_DESCRIPTION 2
59 #define ACCESSORY_STRING_VERSION 3
60 #define ACCESSORY_STRING_URI 4
61 #define ACCESSORY_STRING_SERIAL 5
62 
63 #define ADK_MAX_ENDPOINTS 3 //endpoint 0, bulk_IN, bulk_OUT
64 
65 class ADK;
66 
67 class ADK : public USBDeviceConfig, public UsbConfigXtracter {
68 private:
69  /* ID strings */
70  const char* manufacturer;
71  const char* model;
72  const char* description;
73  const char* version;
74  const char* uri;
75  const char* serial;
76 
77  /* ADK proprietary requests */
78  uint8_t getProto(uint8_t* adkproto);
79  uint8_t sendStr(uint8_t index, const char* str);
80  uint8_t switchAcc(void);
81 
82 protected:
83  static const uint8_t epDataInIndex; // DataIn endpoint index
84  static const uint8_t epDataOutIndex; // DataOUT endpoint index
85 
86  /* mandatory members */
88  uint8_t bAddress;
89  uint8_t bConfNum; // configuration number
90 
91  uint8_t bNumEP; // total number of EP in the configuration
92  bool ready;
93 
94  /* Endpoint data structure */
96 
98 
99 public:
100  ADK(USB *pUsb, const char* manufacturer,
101  const char* model,
102  const char* description,
103  const char* version,
104  const char* uri,
105  const char* serial);
106 
107  // Methods for receiving and sending data
108  uint8_t RcvData(uint16_t *nbytesptr, uint8_t *dataptr);
109  uint8_t SndData(uint16_t nbytes, uint8_t *dataptr);
110 
111 
112  // USBDeviceConfig implementation
113  virtual uint8_t ConfigureDevice(uint8_t parent, uint8_t port, bool lowspeed);
114  virtual uint8_t Init(uint8_t parent, uint8_t port, bool lowspeed);
115  virtual uint8_t Release();
116 
117  virtual uint8_t Poll() {
118  return 0;
119  };
120 
121  virtual uint8_t GetAddress() {
122  return bAddress;
123  };
124 
125  virtual bool isReady() {
126  return ready;
127  };
128 
129  virtual boolean VIDPIDOK(uint16_t vid, uint16_t pid) {
130  return (vid == ADK_VID && (pid == ADK_PID || pid == ADB_PID));
131  };
132 
133  //UsbConfigXtracter implementation
134  virtual void EndpointXtract(uint8_t conf, uint8_t iface, uint8_t alt, uint8_t proto, const USB_ENDPOINT_DESCRIPTOR *ep);
135 }; //class ADK : public USBDeviceConfig ...
136 
137 /* get ADK protocol version */
138 
139 /* returns 2 bytes in *adkproto */
140 inline uint8_t ADK::getProto(uint8_t* adkproto) {
141  return( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_GET, ADK_GETPROTO, 0, 0, 0, 2, 2, adkproto, NULL));
142 }
143 
144 /* send ADK string */
145 inline uint8_t ADK::sendStr(uint8_t index, const char* str) {
146  return( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_SEND, ADK_SENDSTR, 0, 0, index, strlen(str) + 1, strlen(str) + 1, (uint8_t*) str, NULL));
147 }
148 
149 /* switch to accessory mode */
150 inline uint8_t ADK::switchAcc(void) {
151  return( pUsb->ctrlReq(bAddress, 0, bmREQ_ADK_SEND, ADK_ACCSTART, 0, 0, 0, 0, 0, NULL, NULL));
152 }
153 
154 #endif // _ADK_H_