Corrections, improvements and additions to the incomplete RNDISHost demo.
[pub/lufa.git] / Demos / Host / Incomplete / RNDISEthernetHost / Lib / RNDISCommands.c
index e248b18..4802bed 100644 (file)
@@ -98,18 +98,20 @@ uint8_t RNDIS_KeepAlive(void)
        return HOST_SENDCONTROL_Successful;\r
 }\r
 \r
-uint8_t RNDIS_InitializeDevice(uint16_t MaxPacketSize, RNDIS_Initialize_Complete_t* InitMessageResponse)\r
+uint8_t RNDIS_InitializeDevice(uint16_t HostMaxPacketSize, uint16_t* DeviceMaxPacketSize)\r
 {\r
        uint8_t ErrorCode;\r
 \r
-       RNDIS_Initialize_Message_t InitMessage;\r
+       RNDIS_Initialize_Message_t  InitMessage;\r
+       RNDIS_Initialize_Complete_t InitMessageResponse;\r
 \r
        InitMessage.MessageType     = REMOTE_NDIS_INITIALIZE_MSG;\r
        InitMessage.MessageLength   = sizeof(RNDIS_Initialize_Message_t);\r
        InitMessage.RequestId       = RequestID++;\r
+\r
        InitMessage.MajorVersion    = REMOTE_NDIS_VERSION_MAJOR;\r
        InitMessage.MinorVersion    = REMOTE_NDIS_VERSION_MINOR;\r
-       InitMessage.MaxTransferSize = sizeof(RNDIS_Packet_Message_t) + MaxPacketSize;\r
+       InitMessage.MaxTransferSize = HostMaxPacketSize;\r
        \r
        if ((ErrorCode = RNDIS_SendEncapsulatedCommand(&InitMessage,\r
                                                       sizeof(RNDIS_Initialize_Message_t))) != HOST_SENDCONTROL_Successful)\r
@@ -117,11 +119,16 @@ uint8_t RNDIS_InitializeDevice(uint16_t MaxPacketSize, RNDIS_Initialize_Complete
                return ErrorCode;\r
        }\r
        \r
-       if ((ErrorCode = RNDIS_GetEncapsulatedResponse(InitMessageResponse,\r
+       if ((ErrorCode = RNDIS_GetEncapsulatedResponse(&InitMessageResponse,\r
                                                       sizeof(RNDIS_Initialize_Complete_t))) != HOST_SENDCONTROL_Successful)\r
        {\r
                return ErrorCode;\r
        }\r
+\r
+       if (InitMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)\r
+         return RNDIS_COMMAND_FAILED;\r
+         \r
+       *DeviceMaxPacketSize = InitMessageResponse.MaxTransferSize;\r
        \r
        return HOST_SENDCONTROL_Successful;\r
 }\r
@@ -138,13 +145,13 @@ uint8_t RNDIS_SetRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
        \r
        RNDIS_Set_Complete_t SetMessageResponse;\r
        \r
-       SetMessageData.SetMessage.MessageType   = REMOTE_NDIS_SET_MSG;\r
-       SetMessageData.SetMessage.MessageLength = sizeof(RNDIS_Set_Message_t) + Length;\r
-       SetMessageData.SetMessage.RequestId     = RequestID++;\r
+       SetMessageData.SetMessage.MessageType    = REMOTE_NDIS_SET_MSG;\r
+       SetMessageData.SetMessage.MessageLength  = sizeof(RNDIS_Set_Message_t) + Length;\r
+       SetMessageData.SetMessage.RequestId      = RequestID++;\r
                        \r
-       SetMessageData.SetMessage.Oid           = Oid;\r
+       SetMessageData.SetMessage.Oid            = Oid;\r
        SetMessageData.SetMessage.InformationBufferLength = Length;\r
-       SetMessageData.SetMessage.InformationBufferOffset = 0;\r
+       SetMessageData.SetMessage.InformationBufferOffset = (sizeof(RNDIS_Set_Message_t) - sizeof(RNDIS_Message_Header_t));\r
        SetMessageData.SetMessage.DeviceVcHandle = 0;\r
        \r
        memcpy(&SetMessageData.ContigiousBuffer, Buffer, Length);\r
@@ -161,19 +168,22 @@ uint8_t RNDIS_SetRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
                return ErrorCode;\r
        }\r
 \r
+       if (SetMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)\r
+         return RNDIS_COMMAND_FAILED;\r
+         \r
        return HOST_SENDCONTROL_Successful;\r
 }\r
 \r
-uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)\r
+uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid, void* Buffer, uint16_t MaxLength)\r
 {\r
        uint8_t ErrorCode;\r
 \r
-       RNDIS_Query_Message_t  QueryMessage;\r
+       RNDIS_Query_Message_t QueryMessage;\r
 \r
        struct\r
        {\r
                RNDIS_Query_Complete_t QueryMessageResponse;\r
-               uint8_t              ContigiousBuffer[Length];\r
+               uint8_t                ContigiousBuffer[MaxLength];\r
        } QueryMessageResponseData;\r
 \r
        QueryMessage.MessageType    = REMOTE_NDIS_QUERY_MSG;\r
@@ -181,7 +191,7 @@ uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
        QueryMessage.RequestId      = RequestID++;\r
                        \r
        QueryMessage.Oid            = Oid;\r
-       QueryMessage.InformationBufferLength = Length;\r
+       QueryMessage.InformationBufferLength = 0;\r
        QueryMessage.InformationBufferOffset = 0;\r
        QueryMessage.DeviceVcHandle = 0;\r
 \r
@@ -197,12 +207,15 @@ uint8_t RNDIS_QueryRNDISProperty(uint32_t Oid, void* Buffer, uint16_t Length)
                return ErrorCode;\r
        }\r
 \r
-       memcpy(Buffer, &QueryMessageResponseData.ContigiousBuffer, Length);\r
+       if (QueryMessageResponseData.QueryMessageResponse.Status != REMOTE_NDIS_STATUS_SUCCESS)\r
+         return RNDIS_COMMAND_FAILED;\r
+\r
+       memcpy(Buffer, &QueryMessageResponseData.ContigiousBuffer, MaxLength);\r
 \r
        return HOST_SENDCONTROL_Successful;\r
 }\r
 \r
-uint8_t RNDIS_GetPacketSize(uint16_t* PacketSize)\r
+uint8_t RNDIS_GetPacketLength(uint16_t* PacketLength)\r
 {\r
        uint8_t ErrorCode;\r
 \r
@@ -213,7 +226,9 @@ uint8_t RNDIS_GetPacketSize(uint16_t* PacketSize)
                return ErrorCode;\r
        }\r
 \r
-       *PacketSize = (uint16_t)DeviceMessage.DataLength;\r
+       *PacketLength = (uint16_t)DeviceMessage.DataLength;\r
+       \r
+       Pipe_Discard_Stream(DeviceMessage.DataOffset - (sizeof(RNDIS_Packet_Message_t) - sizeof(RNDIS_Message_Header_t)));\r
        \r
        return PIPE_RWSTREAM_NoError;\r
 }\r