SideshowCommands.c 15 KB
Newer Older
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
/*
             LUFA Library
     Copyright (C) Dean Camera, 2009.
              
  dean [at] fourwalledcubicle [dot] com
      www.fourwalledcubicle.com
*/

/*
  Copyright 2009  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, and distribute this software
  and its documentation for any purpose and without fee is hereby
  granted, provided that the above copyright notice appear in all
  copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaim all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

#define  INCLUDE_FROM_SIDESHOWCOMMANDS_H
#include "SideshowCommands.h"

UNICODE_STRING_t(80) UserSID       = {LengthInBytes: sizeof(SECURITY_INTERACTIVE_RID_SID),
                                      UnicodeString: SECURITY_INTERACTIVE_RID_SID};

Unicode_String_t DeviceName        = {LengthInBytes: sizeof(L"LUFA Sideshow Device"),
                                      UnicodeString: L"LUFA Sideshow Device"};

Unicode_String_t Manufacturer      = {LengthInBytes: sizeof(L"Dean Camera"),
                                      UnicodeString: L"Dean Camera"};

Unicode_String_t SupportedLanguage = {LengthInBytes: sizeof(L"en-US:1"),
                                      UnicodeString: L"en-US:1"};
									  
void Sideshow_ProcessCommandPacket(void)
{
	SideShow_PacketHeader_t PacketHeader;
	
	Endpoint_SelectEndpoint(SIDESHOW_OUT_EPNUM);	
	Endpoint_Read_Stream_LE(&PacketHeader, sizeof(SideShow_PacketHeader_t));
	
	PacketHeader.Type.Response = true;

	printf("\r\nCmd: %lX", (PacketHeader.Type.TypeLong & 0x00FFFFFF));

	switch (PacketHeader.Type.TypeLong & 0x00FFFFFF)
	{
		case SIDESHOW_CMD_PING:
			SideShow_Ping(&PacketHeader);
			break;
		case SIDESHOW_CMD_SYNC:
			SideShow_Sync(&PacketHeader);
			break;
		case SIDESHOW_CMD_GET_CURRENT_USER:
			SideShow_GetCurrentUser(&PacketHeader);
			break;
		case SIDESHOW_CMD_SET_CURRENT_USER:
			SideShow_SetCurrentUser(&PacketHeader);
			break;
		case SIDESHOW_CMD_GET_CAPABILITIES:
			SideShow_GetCapabilities(&PacketHeader);
			break;			
		case SIDESHOW_CMD_GET_DEVICE_NAME:
			SideShow_GetString(&PacketHeader, &DeviceName);
			break;
		case SIDESHOW_CMD_GET_MANUFACTURER:
			SideShow_GetString(&PacketHeader, &Manufacturer);
			break;
		case SIDESHOW_CMD_GET_APPLICATION_ORDER:
			SideShow_GetApplicationOrder(&PacketHeader);
			break;
		case SIDESHOW_CMD_GET_SUPPORTED_ENDPOINTS:
			SideShow_GetSupportedEndpoints(&PacketHeader);
			break;
		case SIDESHOW_CMD_ADD_APPLICATION:
			SideShow_AddApplication(&PacketHeader);
			break;
		case SIDESHOW_CMD_ADD_CONTENT:
			SideShow_AddContent(&PacketHeader);
			break;
		case SIDESHOW_CMD_DELETE_CONTENT:
			SideShow_DeleteContent(&PacketHeader);
			break;
		case SIDESHOW_CMD_DELETE_ALL_CONTENT:
			SideShow_DeleteAllContent(&PacketHeader);
			break;		
		case SIDESHOW_CMD_DELETE_APPLICATION:
			SideShow_DeleteApplication(&PacketHeader);
			break;
		case SIDESHOW_CMD_DELETE_ALL_APPLICATIONS:
			SideShow_DeleteAllApplications(&PacketHeader);
			break;
		default:
			PacketHeader.Length -= sizeof(SideShow_PacketHeader_t);

			Endpoint_Discard_Stream(PacketHeader.Length);
108
			Endpoint_ClearOUT();
109
110
111
112
113
114

			PacketHeader.Length   = sizeof(SideShow_PacketHeader_t);
			PacketHeader.Type.NAK = true;
			
			Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);	
			Endpoint_Write_Stream_LE(&PacketHeader, sizeof(SideShow_PacketHeader_t));		
115
			Endpoint_ClearIN();
116
117
118
119
120
121
122
			
			printf(" UNK");
	}
}

static void SideShow_Ping(SideShow_PacketHeader_t* PacketHeader)
{
123
	Endpoint_ClearOUT();
124
125
126

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);	
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));		
127
	Endpoint_ClearIN();
128
129
130
131
132
133
134
}

static void SideShow_Sync(SideShow_PacketHeader_t* PacketHeader)
{
	GUID_t ProtocolGUID;

	Endpoint_Read_Stream_LE(&ProtocolGUID, sizeof(GUID_t));
135
	Endpoint_ClearOUT();
136
	
137
	if (!(GUID_COMPARE(&ProtocolGUID, (uint32_t[])STANDARD_PROTOCOL_GUID)))
138
	  PacketHeader->Type.NAK = true;
139
	
140
141
142
	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));		
	Endpoint_Write_Stream_LE(&ProtocolGUID, sizeof(GUID_t));
143
	Endpoint_ClearIN();
144
145
146
147
}

static void SideShow_GetCurrentUser(SideShow_PacketHeader_t* PacketHeader)
{
148
	Endpoint_ClearOUT();
149
150
151
152
153
154

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t) + sizeof(uint32_t) + UserSID.LengthInBytes;

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
	SideShow_Write_Unicode_String(&UserSID);
155
	Endpoint_ClearIN();
156
157
158
159
160
}

static void SideShow_SetCurrentUser(SideShow_PacketHeader_t* PacketHeader)
{
	SideShow_Read_Unicode_String(&UserSID, sizeof(UserSID.UnicodeString));
161
	Endpoint_ClearOUT();
162
163
164
165
166
	
	PacketHeader->Length = sizeof(SideShow_PacketHeader_t);

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
167
	Endpoint_ClearIN();
168
169
170
171
172
173
174
175
}

static void SideShow_GetCapabilities(SideShow_PacketHeader_t* PacketHeader)
{
	SideShow_PropertyKey_t  Property;
	SideShow_PropertyData_t PropertyData;

	Endpoint_Read_Stream_LE(&Property, sizeof(SideShow_PropertyKey_t));
176
	Endpoint_ClearOUT();
177
178
179
180
181
	
	printf(" ID: %lu", Property.PropertyID);

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t);

182
	if (GUID_COMPARE(&Property.PropertyGUID, (uint32_t[])SIDESHOW_PROPERTY_GUID))
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
	{
		switch (Property.PropertyID)
		{
			case PROPERTY_SIDESHOW_SCREENTYPE:
				PropertyData.DataType    = VT_I4;
				PropertyData.Data.Data32 = ScreenText;
				PacketHeader->Length += sizeof(uint32_t);
				
				break;
			case PROPERTY_SIDESHOW_SCREENWIDTH:
			case PROPERTY_SIDESHOW_CLIENTWIDTH:
				PropertyData.DataType    = VT_UI2;
				PropertyData.Data.Data16 = 16;
				PacketHeader->Length += sizeof(uint16_t);
			
				break;
			case PROPERTY_SIDESHOW_SCREENHEIGHT:
			case PROPERTY_SIDESHOW_CLIENTHEIGHT:
				PropertyData.DataType    = VT_UI2;
				PropertyData.Data.Data16 = 2;
				PacketHeader->Length += sizeof(uint16_t);
			
				break;
			case PROPERTY_SIDESHOW_COLORDEPTH:
				PropertyData.DataType    = VT_UI2;
				PropertyData.Data.Data16 = 1;
				PacketHeader->Length += sizeof(uint16_t);
			
				break;
			case PROPERTY_SIDESHOW_COLORTYPE:
				PropertyData.DataType    = VT_UI2;
				PropertyData.Data.Data16 = BlackAndWhiteDisplay;
				PacketHeader->Length += sizeof(uint16_t);
			
				break;
			case PROPERTY_SIDESHOW_DATACACHE:
				PropertyData.DataType    = VT_BOOL;
				PropertyData.Data.Data16 = false;
				PacketHeader->Length += sizeof(uint16_t);
			
				break;
			case PROPERTY_SIDESHOW_SUPPORTEDLANGS:
			case PROPERTY_SIDESHOW_CURRENTLANG:
				PropertyData.DataType    = VT_LPWSTR;
				PropertyData.Data.DataPointer = &SupportedLanguage;
				PacketHeader->Length += SupportedLanguage.LengthInBytes;
			
				break;
			default:
				PropertyData.DataType    = VT_EMPTY;
				break;
		}
	}
236
	else if (GUID_COMPARE(&Property.PropertyGUID, (uint32_t[])DEVICE_PROPERTY_GUID))
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	{
		switch (Property.PropertyID)
		{
			case PROPERTY_DEVICE_DEVICETYPE:
				PropertyData.DataType    = VT_UI4;
				PropertyData.Data.Data32 = GenericDevice;
				PacketHeader->Length += sizeof(uint32_t);
				
				break;
		}
	}	
	else
	{
		PacketHeader->Type.NAK = true;		
		
		printf(" WRONG GUID");
		printf(" %lX %lX %lX %lX", Property.PropertyGUID.Chunks[0], Property.PropertyGUID.Chunks[1],
		                           Property.PropertyGUID.Chunks[2],  Property.PropertyGUID.Chunks[3]);		
	}

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
	
	if (!(PacketHeader->Type.NAK))
	{
		switch (PropertyData.DataType)
		{
			case VT_UI4:
			case VT_I4:
				Endpoint_Write_Stream_LE(&PropertyData.Data.Data32, sizeof(uint32_t));
				break;
			case VT_UI2:
			case VT_I2:
			case VT_BOOL:
				Endpoint_Write_Stream_LE(&PropertyData.Data.Data16, sizeof(uint16_t));
				break;
			case VT_LPWSTR:
				SideShow_Write_Unicode_String((Unicode_String_t*)PropertyData.Data.Data16);
				break;
		}
	}
	
279
	Endpoint_ClearIN();
280
281
282
283
284
	return;
}

static void SideShow_GetString(SideShow_PacketHeader_t* PacketHeader, void* UnicodeStruct)
{
285
	Endpoint_ClearOUT();
286
287
288
289
290
291
292

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t) +
	                       sizeof(uint32_t) + ((Unicode_String_t*)UnicodeStruct)->LengthInBytes;
	
	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
	SideShow_Write_Unicode_String(UnicodeStruct);
293
	Endpoint_ClearIN();
294
295
296
297
}

static void SideShow_GetApplicationOrder(SideShow_PacketHeader_t* PacketHeader)
{
298
299
	uint8_t  TotalApplications = 0;
		   
300
	Endpoint_ClearOUT();
301

302
303
304
305
306
307
	for (uint8_t App = 0; App < MAX_APPLICATIONS; App++)
	{
		if (InstalledApplications[App].InUse)
		  TotalApplications++;
	}

308
	PacketHeader->Length = sizeof(SideShow_PacketHeader_t) +
309
	                       sizeof(uint32_t) + (TotalApplications * sizeof(GUID_t));
310
311
312
	
	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
313
	Endpoint_Write_DWord_LE(TotalApplications);
314
315
316
	
	for (uint8_t App = 0; App < MAX_APPLICATIONS; App++)
	{
317
		if (InstalledApplications[App].InUse)
318
319
320
		  Endpoint_Write_Stream_LE(&InstalledApplications[App].ApplicationID, sizeof(GUID_t));
	}

321
	Endpoint_ClearIN();
322
323
324
325
326
327
}

static void SideShow_GetSupportedEndpoints(SideShow_PacketHeader_t* PacketHeader)
{
	GUID_t SupportedEndpointGUID = (GUID_t){Chunks: SIMPLE_CONTENT_FORMAT_GUID};

328
	Endpoint_ClearOUT();
329
330
331
332
333
334
335

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t) + sizeof(uint32_t) + sizeof(GUID_t);
	
	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
	Endpoint_Write_DWord_LE(1);
	Endpoint_Write_Stream_LE(&SupportedEndpointGUID, sizeof(GUID_t));
336
	Endpoint_ClearIN();
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
}

static void SideShow_AddApplication(SideShow_PacketHeader_t* PacketHeader)
{
	SideShow_Application_t* CurrApp;
	GUID_t                  ApplicationID;

	Endpoint_Read_Stream_LE(&ApplicationID, sizeof(GUID_t));

	CurrApp = SideShow_GetApplicationFromGUID(&ApplicationID);

	if (CurrApp == NULL)
	  CurrApp = SideShow_GetFreeApplication();

	if (CurrApp == NULL)
	{
		PacketHeader->Length -= sizeof(SideShow_PacketHeader_t) + sizeof(GUID_t);

		Endpoint_Discard_Stream(PacketHeader->Length);
356
		Endpoint_ClearOUT();
357
358
359
360
361
362
363
364
365
366
367
368
369

		PacketHeader->Type.NAK = true;
	}
	else
	{
		CurrApp->ApplicationID = ApplicationID;
		Endpoint_Read_Stream_LE(&CurrApp->EndpointID, sizeof(GUID_t));
		SideShow_Read_Unicode_String(&CurrApp->ApplicationName, sizeof(CurrApp->ApplicationName.UnicodeString));
		Endpoint_Read_Stream_LE(&CurrApp->CachePolicy, sizeof(uint32_t));
		Endpoint_Read_Stream_LE(&CurrApp->OnlineOnly, sizeof(uint32_t));
		SideShow_Discard_Byte_Stream();
		SideShow_Discard_Byte_Stream();
		SideShow_Discard_Byte_Stream();
370
		Endpoint_ClearOUT();
371
372
373
374
375
376
377
378
379
380
		
		CurrApp->InUse = true;
		CurrApp->HaveContent = false;
		CurrApp->CurrentContentID = 1;
	}

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t);

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
381
	Endpoint_ClearIN();
382
383
384
385
386
387
388
}

static void SideShow_DeleteApplication(SideShow_PacketHeader_t* PacketHeader)
{
	GUID_t ApplicationGUID;
	
	Endpoint_Read_Stream_LE(&ApplicationGUID, sizeof(GUID_t));	
389
	Endpoint_ClearOUT();
390
391
392
393

	SideShow_Application_t* AppToDelete = SideShow_GetApplicationFromGUID(&ApplicationGUID);

	if (AppToDelete != NULL)
394
	  AppToDelete->InUse = false;
395
396
397
398
399
400
401
	else
	  PacketHeader->Type.NAK = true;

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t);

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
402
	Endpoint_ClearIN();
403
404
405
406
}

static void SideShow_DeleteAllApplications(SideShow_PacketHeader_t* PacketHeader)
{
407
	Endpoint_ClearOUT();
408
409
410
411
412
413
	
	for (uint8_t App = 0; App < MAX_APPLICATIONS; App++)
	  InstalledApplications[App].InUse = false;

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
414
	Endpoint_ClearIN();
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
}

static void SideShow_AddContent(SideShow_PacketHeader_t* PacketHeader)
{
	GUID_t ApplicationID;
	GUID_t EndpointID;
	SideShow_Application_t* Application;
	
	Endpoint_Read_Stream_LE(&ApplicationID, sizeof(GUID_t));
	Endpoint_Read_Stream_LE(&EndpointID, sizeof(GUID_t));
	
	Application = SideShow_GetApplicationFromGUID(&ApplicationID);
	
	if (Application == NULL)
	{
		SideShow_Discard_Byte_Stream();
		PacketHeader->Type.NAK = true;
	}
	else if (!(SideShow_AddSimpleContent(PacketHeader, Application)))
	{
		PacketHeader->Type.NAK = true;
	}
	
438
	Endpoint_ClearOUT();
439
440
441
442
443

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t);

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
444
	Endpoint_ClearIN();
445
446
447
448
449
450
451
452
453
454
455
}

static void SideShow_DeleteContent(SideShow_PacketHeader_t* PacketHeader)
{
	GUID_t   ApplicationID;
	GUID_t   EndpointID;
	uint32_t ContentID;

	Endpoint_Read_Stream_LE(&ApplicationID, sizeof(GUID_t));
	Endpoint_Read_Stream_LE(&EndpointID, sizeof(GUID_t));
	Endpoint_Read_Stream_LE(&ContentID, sizeof(uint32_t));
456
	Endpoint_ClearOUT();
457
458
459
460
461
462
463
464
465
466
467
468
	
	SideShow_Application_t* Application = SideShow_GetApplicationFromGUID(&ApplicationID);
	
	if ((Application != NULL) && (Application->CurrentContentID == ContentID))
	  Application->HaveContent = false;
	else
	  PacketHeader->Type.NAK = true;
	  
	PacketHeader->Length = sizeof(SideShow_PacketHeader_t);

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
469
	Endpoint_ClearIN();
470
471
472
473
474
475
476
477
478
}

static void SideShow_DeleteAllContent(SideShow_PacketHeader_t* PacketHeader)
{
	GUID_t ApplicationID;
	GUID_t EndpointID;

	Endpoint_Read_Stream_LE(&ApplicationID, sizeof(GUID_t));
	Endpoint_Read_Stream_LE(&EndpointID, sizeof(GUID_t));
479
	Endpoint_ClearOUT();
480
481
482
483
484
485
486
487
488
489
490
491

	SideShow_Application_t* Application = SideShow_GetApplicationFromGUID(&ApplicationID);
	
	if (Application != NULL)
	  Application->HaveContent = false;
	else
	  PacketHeader->Type.NAK = true;	  

	PacketHeader->Length = sizeof(SideShow_PacketHeader_t);

	Endpoint_SelectEndpoint(SIDESHOW_IN_EPNUM);
	Endpoint_Write_Stream_LE(PacketHeader, sizeof(SideShow_PacketHeader_t));
492
	Endpoint_ClearIN();  
493
}