Repository navigation
Expand file tree
/
Copy pathPropertiesPusherADK.cpp
More file actions
164 lines (137 loc) · 4.98 KB
/
Copy pathPropertiesPusherADK.cpp
File metadata and controls
164 lines (137 loc) · 4.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "PropertiesPusherADK.h"
ANDROID_ACCESSORY_INFORMATION myDeviceInfo = {
manufacturer, sizeof(manufacturer),
model, sizeof(model),
description, sizeof(description),
version, sizeof(version),
uri, sizeof(uri),
serial, sizeof(serial)
};
StringStream ADKStream;
TokenParser ADKToken(&ADKStream);
// Replyes for Android device
#define REPLY_OK "ok"
#define REPLY_GETOUT "getout"
#define REPLY_UNKNOWN_CMD "dontunderstand"
// Local variables
BOOL deviceAttached = FALSE;
static void* deviceHandle = NULL;
static char read_buffer[128];
static char write_buffer[128];
int write_size;
BOOL writeInProgress = FALSE;
BOOL USBEventHandlerApplication( uint8_t address, USB_EVENT event, void *data, DWORD size ) {
BOOL fRet = FALSE;
// Call event handler from base host controller
// (this is important to be done, because it also turns on and off power on microcontroller
// pins on events EVENT_VBUS_REQUEST_POWER и EVENT_VBUS_RELEASE_POWER)
fRet = USBHost.DefaultEventHandler(address, event, data, size);
switch( event ) {
case EVENT_ANDROID_DETACH:
deviceAttached = FALSE;
return TRUE;
break;
case EVENT_ANDROID_ATTACH:
deviceAttached = TRUE;
deviceHandle = data;
return TRUE;
default :
break;
}
return fRet;
}
/**
* Process input - parse string, execute command.
* @return size of reply in bytes (0 for no reply).
*/
int processInput(Quick *q, Properties *properties, char* buffer, int size, char* reply_buffer) {
int replySize = 0;
reply_buffer[0] = 0;
buffer[size++] = '\r'; //make string end with a carriage return for token parser
buffer[size] = 0;
strcpy(ADKStream.inbuffer,buffer);
ADKStream.inbufferlength = size;
ADKStream.inbufferposition = 0;
ADKStream.outbufferposition = 0;
while(ADKStream.inbufferlength>ADKStream.inbufferposition) {
if(ADKToken.scan()) {
q->command(ADKToken);
properties->evaluate(ADKToken);
//additional commands from android
//if(ADKToken.compare("somecommand")) {
//}
}
}
strcpy(reply_buffer, ADKStream.outbuffer);// Prepare reply
replySize = strlen(reply_buffer) + 1;
return replySize;
}
void ProcessHost(Quick *q, Properties *properties) {
DWORD readSize;
BOOL readyToRead = TRUE;
DWORD writeSize;
uint8_t errorCode;
// Run periodic tasks to keep USB stack alive and healthy.
// Run at least once per cycle or when you need to update inner state of USB host controller.
USBTasks();
if(deviceAttached) {
// Read data from Android device - wait for a command
if(readyToRead) {
// Call is not blocked - will check if read is complete with AndroidAppIsReadComplete
errorCode = USBAndroidHost.AppRead(deviceHandle, (uint8_t*)&read_buffer, (DWORD)sizeof(read_buffer));
if(errorCode == USB_SUCCESS) {
// Received command - will not read the next one until all data is received,
// will check for that in next loop iterations.
readyToRead = FALSE;
}
}
// Let's check if read is complete
if(USBAndroidHost.AppIsReadComplete(deviceHandle, &errorCode, &readSize)) {
if(errorCode == USB_SUCCESS) {
// Data portion is read, let's add finishing zero to make it zero-terminated string.
read_buffer[readSize] = 0;
// and we can execute the command now, reply will go to write_buffer
writeSize = processInput(q, properties, read_buffer, readSize, write_buffer);
// Allow to read next command
readyToRead = TRUE;
readSize = 0;
// If writeSize is not 0, send back reply - init write procedure
// for the next loop iteration (data is already inside write_buffer)
write_size = writeSize;
}
else {
// Serial1.print("Error trying to complete read: errorCode=");
// Serial1.println(errorCode, HEX);
}
}
// Send data to Android device
if(write_size > 0 && !writeInProgress) {
// Serial1.print("Write: ");
// Serial1.print(write_buffer);
// Serial1.println();
writeSize = write_size;
// Require command is already in the buffer to be sent.
// Call is not blocked - will check if write is complete with AndroidAppIsWriteComplete
errorCode = USBAndroidHost.AppWrite(deviceHandle, (uint8_t*)&write_buffer, (DWORD) writeSize);
if(errorCode == USB_SUCCESS) {
writeInProgress = TRUE;
}
else {
Serial1.print("Error trying to complete read: errorCode=");
Serial1.println(errorCode, HEX);
write_size = 0;
}
}
if(writeInProgress) {
// Let's check if write is complete
if(USBAndroidHost.AppIsWriteComplete(deviceHandle, &errorCode, &writeSize)) {
writeInProgress = FALSE;
write_size = 0;
if(errorCode != USB_SUCCESS) {
// Serial1.print("Error trying to complete read: errorCode=");
// Serial1.println(errorCode, HEX);
}
}
}
}
}