Add beginnings of a RNDIS Ethernet Host demo.
[pub/USBasp.git] / Demos / Host / Incomplete / RNDISEthernetHost / Lib / RNDISCommands.c
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2009.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.fourwalledcubicle.com
7 */
8
9 /*
10 Copyright 2009 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12 Permission to use, copy, modify, and distribute this software
13 and its documentation for any purpose and without fee is hereby
14 granted, provided that the above copyright notice appear in all
15 copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
20
21 The author disclaim all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
28 this software.
29 */
30
31 /** \file
32 *
33 * RNDOS Device commands, to issue RNDIS commands to the device for
34 * the control and data transfer between the host and RNDIS device.
35 */
36
37 #include "RNDISCommands.h"
38
39 uint32_t RequestID = 0;
40
41 uint8_t RNDIS_SendEncapsulatedCommand(void* Buffer, uint16_t Length)
42 {
43 USB_ControlRequest = (USB_Request_Header_t)
44 {
45 .bmRequestType = (REQDIR_HOSTTODEVICE | REQTYPE_CLASS | REQREC_INTERFACE),
46 .bRequest = REQ_SendEncapsulatedCommand,
47 .wValue = 0,
48 .wIndex = 0,
49 .wLength = Length,
50 };
51
52 printf("==== RNDIS CONTROL REQUEST ====\r\n");
53 uint8_t* Data = Buffer;
54 for (uint16_t i = 0; i < Length / 8; i++)
55 {
56 for (uint16_t j = 0; (j < 8) && i*8+j < Length; j++)
57 printf("%02X ", *(Data++));
58
59 printf("\r\n");
60 }
61 printf("==== ********************* ====\r\n");
62
63 /* Select the control pipe for the request transfer */
64 Pipe_SelectPipe(PIPE_CONTROLPIPE);
65
66 return USB_Host_SendControlRequest(Buffer);
67 }
68
69 uint8_t RNDIS_GetEncapsulatedResponse(void* Buffer, uint16_t Length)
70 {
71 USB_ControlRequest = (USB_Request_Header_t)
72 {
73 .bmRequestType = (REQDIR_DEVICETOHOST | REQTYPE_CLASS | REQREC_INTERFACE),
74 .bRequest = REQ_GetEncapsulatedResponse,
75 .wValue = 0,
76 .wIndex = 0,
77 .wLength = Length,
78 };
79
80 /* Select the control pipe for the request transfer */
81 Pipe_SelectPipe(PIPE_CONTROLPIPE);
82
83 return USB_Host_SendControlRequest(Buffer);
84 }
85
86 uint8_t RNDIS_InitializeDevice(uint16_t MaxPacketSize, RNDIS_Initialize_Complete_t* InitMessageResponse)
87 {
88 uint8_t ErrorCode;
89
90 RNDIS_Initialize_Message_t InitMessage;
91
92 InitMessage.MessageType = REMOTE_NDIS_INITIALIZE_MSG;
93 InitMessage.MessageLength = sizeof(RNDIS_Initialize_Message_t);
94 InitMessage.RequestId = RequestID++;
95 InitMessage.MajorVersion = REMOTE_NDIS_VERSION_MAJOR;
96 InitMessage.MinorVersion = REMOTE_NDIS_VERSION_MINOR;
97 InitMessage.MaxTransferSize = sizeof(RNDIS_Packet_Message_t) + MaxPacketSize;
98
99 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&InitMessage,
100 sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)
101 {
102 return ErrorCode;
103 }
104
105 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(InitMessageResponse,
106 sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)
107 {
108 return ErrorCode;
109 }
110
111 return HOST_SENDCONTROL_Successful;
112 }
113
114 uint8_t RNDIS_SetRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
115 {
116 uint8_t ErrorCode;
117
118 struct
119 {
120 RNDIS_Set_Message_t SetMessage;
121 uint8_t ContigiousBuffer[Length];
122 } SetMessageData;
123
124 RNDIS_Set_Complete_t SetMessageResponse;
125
126 SetMessageData.SetMessage.MessageType = REMOTE_NDIS_SET_MSG;
127 SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;
128 SetMessageData.SetMessage.RequestId = RequestID++;
129
130 SetMessageData.SetMessage.Oid = Oid;
131 SetMessageData.SetMessage.InformationBufferLength = Length;
132 SetMessageData.SetMessage.InformationBufferOffset = 0;
133 SetMessageData.SetMessage.DeviceVcHandle = 0;
134
135
136 if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&SetMessageData,
137 SetMessageData.SetMessage.MessageLength)) != HOST_SENDCONTROL_Successful)
138 {
139 return ErrorCode;
140 }
141
142 if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&SetMessageResponse,
143 sizeof(RNDIS_Set_Complete_t))) != HOST_SENDCONTROL_Successful)
144 {
145 return ErrorCode;
146 }
147
148 return HOST_SENDCONTROL_Successful;
149 }