⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 rndis.c

📁 Contiki是一个开源
💻 C
📖 第 1 页 / 共 2 页
字号:
/** * \file rndis.c *         RNDIS Functions for mounting USB device as network interface * * \author *         Colin O'Flynn <coflynn@newae.com> * * \addtogroup usbstick *//* Copyright (c) 2008  Colin O'Flynn   The CDC code which this is based on is Copyright (c) Atmel Corporation 2008   All rights reserved.   Redistribution and use in source and binary forms, with or without   modification, are permitted provided that the following conditions are met:   * Redistributions of source code must retain the above copyright     notice, this list of conditions and the following disclaimer.   * Redistributions in binary form must reproduce the above copyright     notice, this list of conditions and the following disclaimer in     the documentation and/or other materials provided with the     distribution.   * Neither the name of the copyright holders nor the names of     contributors may be used to endorse or promote products derived     from this software without specific prior written permission.  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE  POSSIBILITY OF SUCH DAMAGE.*//*RNDIS Status Information:802.3 Support:    More or less working	 802.11 Support:	Incomplete, would just error out if you tried probably*//**  \addtogroup RNDIS  @{  */ //_____ I N C L U D E S ____________________________________________________#include "radio.h"#include "contiki.h"#include "config.h"#include "usb_drv.h"#include "usb_descriptors.h"#include "usb_specific_request.h"#include "rndis/rndis_protocol.h"#include "uip.h"#include "serial/uart_usb_lib.h"#include "sicslow_ethernet.h"#include <avr/pgmspace.h>#include <string.h>//_____ M A C R O S ________________________________________________________//_____ D E F I N I T I O N ________________________________________________//_____ P R I V A T E   D E C L A R A T I O N ______________________________extern PGM_VOID_P pbuffer;extern U8   data_to_transfer;//NB: If you change the OID list be sure to update this!!!//#define OID_LIST_LENGTH 50#define OID_LIST_LENGTH 35/** * \brief List of supported RNDIS OID's */prog_uint32_t OIDSupportedList[OID_LIST_LENGTH] = {		/* Required General */        OID_GEN_SUPPORTED_LIST            ,        OID_GEN_HARDWARE_STATUS           ,        OID_GEN_MEDIA_SUPPORTED           ,        OID_GEN_MEDIA_IN_USE              ,        OID_GEN_MAXIMUM_FRAME_SIZE        ,        OID_GEN_LINK_SPEED                ,        OID_GEN_TRANSMIT_BLOCK_SIZE       ,        OID_GEN_RECEIVE_BLOCK_SIZE        ,        OID_GEN_VENDOR_ID                 ,        OID_GEN_VENDOR_DESCRIPTION        ,        OID_GEN_CURRENT_PACKET_FILTER     ,        OID_GEN_MAXIMUM_TOTAL_SIZE        ,        OID_GEN_MEDIA_CONNECT_STATUS      ,        OID_GEN_VENDOR_DRIVER_VERSION     ,        OID_GEN_PHYSICAL_MEDIUM			  ,		/* Required Statistical */        OID_GEN_XMIT_OK                   ,        OID_GEN_RCV_OK                    ,        OID_GEN_XMIT_ERROR                ,        OID_GEN_RCV_ERROR                 ,        OID_GEN_RCV_NO_BUFFER             ,		/* Please configure us        */		OID_GEN_RNDIS_CONFIG_PARAMETER    ,		/* IEEE 802.3 (Ethernet) OIDs */        OID_802_3_PERMANENT_ADDRESS       ,        OID_802_3_CURRENT_ADDRESS         ,        OID_802_3_MULTICAST_LIST          ,        OID_802_3_MAXIMUM_LIST_SIZE       ,        OID_802_3_MAC_OPTIONS             ,        OID_802_3_RCV_ERROR_ALIGNMENT     ,        OID_802_3_XMIT_ONE_COLLISION      ,        OID_802_3_XMIT_MORE_COLLISIONS    ,/*802.11 OID's not fully implemented yet. Hence do not say we  support them */#ifdef DONOTEVERDEFINETHISORBADSTUFFHAPPENS		/* 802.11 OIDs */        OID_802_11_BSSID                  ,        OID_802_11_SSID                   ,        OID_802_11_NETWORK_TYPE_IN_USE    ,        OID_802_11_RSSI                   ,        OID_802_11_BSSID_LIST             ,        OID_802_11_BSSID_LIST_SCAN        ,        OID_802_11_INFRASTRUCTURE_MODE    ,        OID_802_11_SUPPORTED_RATES        ,        OID_802_11_CONFIGURATION          ,        OID_802_11_ADD_WEP                ,        OID_802_11_WEP_STATUS             ,        OID_802_11_REMOVE_WEP             ,        OID_802_11_DISASSOCIATE           ,        OID_802_11_AUTHENTICATION_MODE    ,        OID_802_11_RELOAD_DEFAULTS        ,#endif		/* Minimum power managment needed for USB */		OID_PNP_CAPABILITIES              ,		OID_PNP_QUERY_POWER               ,		OID_PNP_SET_POWER                 ,		OID_PNP_ENABLE_WAKE_UP            ,		OID_PNP_ADD_WAKE_UP_PATTERN       ,		OID_PNP_REMOVE_WAKE_UP_PATTERN	};rndis_state_t rndis_state;rndis_stat_t rndis_stat;uint8_t schedule_interrupt = 0;uint64_t rndis_ethernet_addr = 0x203478928323UL;//_____ D E C L A R A T I O N ______________________________________________void rndis_packetFilter(uint32_t newfilter);/******** RNDIS ********/#define ENC_BUF_SIZE    (4*OID_LIST_LENGTH + 32)// Command bufferU8 encapsulated_buffer[ENC_BUF_SIZE];//Do we have data to send back?U8 data_to_send = 0x00;/** * \brief Handles a "SEND ENCAPSULATED COMMAND" message. * * \return True on success, false on failure. */uint8_t send_encapsulated_command(uint16_t wLength){	U8 i = 0;	//Received setup message OK	Usb_ack_receive_setup();	//Crude bounds check	if (wLength > ENC_BUF_SIZE)		wLength = ENC_BUF_SIZE;	//For debugging: this shouldn't happen, just checked it didn't	//if (data_to_send) {	//	while(1);	//}	//Read in all the bytes...	uint8_t nb_counter;	while (wLength) {		nb_counter = EP_CONTROL_LENGTH;		//Wait for data to come in		while (!(Is_usb_receive_out()));					while(nb_counter && wLength) {						encapsulated_buffer[i] = Usb_read_byte();			i++;			wLength--;			nb_counter--;		}				Usb_ack_receive_out();	}	Usb_send_control_in();	switch(((rndis_generic_msg_t *)encapsulated_buffer)->MessageType)		{		/* Requests remote intilization. Respond with complete,		   eventually should probably do something */		case REMOTE_NDIS_INITIALIZE_MSG:				{								rndis_initialize_cmplt_t * m;				m = ((rndis_initialize_cmplt_t *)encapsulated_buffer);					//m->MessageID is same as before				m->MessageType = REMOTE_NDIS_INITIALIZE_CMPLT;				m->MessageLength = sizeof(rndis_initialize_cmplt_t);				m->MajorVersion = RNDIS_MAJOR_VERSION;				m->MinorVersion = RNDIS_MAJOR_VERSION;				m->Status = RNDIS_STATUS_SUCCESS;				m->DeviceFlags = RNDIS_DF_CONNECTIONLESS;				m->Medium = RNDIS_MEDIUM_802_3;				m->MaxPacketsPerTransfer = 1;				m->MaxTransferSize = 1338; /* Space for 1280 IP buffer, Ethernet Header, 				                              RNDIS messages */				m->PacketAlignmentFactor = 3;				m->AfListOffset = 0;				m->AfListSize = 0;				rndis_state = rndis_initialized;				data_to_send = m->MessageLength;				}			break;		case REMOTE_NDIS_HALT_MSG:			Led0_on();			Led1_on();			Led2_on();			Led3_on();			Usb_send_control_in();			while(1);						break;		case REMOTE_NDIS_QUERY_MSG:				rndis_query_process();			break;		case REMOTE_NDIS_SET_MSG:				{				rndis_set_process();				}			break;			case REMOTE_NDIS_RESET_MSG:				{				rndis_reset_cmplt_t * m;				m = ((rndis_reset_cmplt_t *)encapsulated_buffer);					rndis_state = rndis_uninitialized;				m->MessageType = REMOTE_NDIS_RESET_CMPLT;				m->MessageLength = sizeof(rndis_reset_cmplt_t);				m->Status = RNDIS_STATUS_SUCCESS;				m->AddressingReset = 1; /* Make it look like we did something */			//	m->AddressingReset = 0; //Windows halts if set to 1 for some reason				data_to_send = m->MessageLength;								}			break;		case REMOTE_NDIS_KEEPALIVE_MSG:				{				rndis_keepalive_cmplt_t * m;				m = (rndis_keepalive_cmplt_t *)encapsulated_buffer;				m->MessageType = REMOTE_NDIS_KEEPALIVE_CMPLT;				m->MessageLength = sizeof(rndis_keepalive_cmplt_t);				m->Status = RNDIS_STATUS_SUCCESS;				//We have data to send back				data_to_send = m->MessageLength;			break;			}		default:			Led2_on();			return FALSE;			break;		}		while(!(Is_usb_read_control_enabled()));		if (Is_usb_receive_out()) Usb_ack_receive_out();		rndis_send_interrupt();		return TRUE;}/** * \brief Send an interrupt over the interrupt endpoint to the host. */void rndis_send_interrupt(void)	{		//Schedule the interrupt to take place next	//time USB task is run	schedule_interrupt = 1;		}#define INFBUF ((uint32_t *)(encapsulated_buffer + sizeof(rndis_query_cmplt_t)))uint32_t oid_packet_filter = 0x0000000;uint16_t panid = 0xbaad;/** * \brief Function to handle a RNDIS "QUERY" command in the encapsulated_buffer */void rndis_query_process(void)	{	rndis_query_msg_t * m;	rndis_query_cmplt_t * c;	rndis_Status_t status = RNDIS_STATUS_SUCCESS;	m = (rndis_query_msg_t *)encapsulated_buffer;	c = (rndis_query_cmplt_t *)encapsulated_buffer;	/* We set up packet for sending one 4-byte response, which a lot of	   these will do. If you need more or less just change the defaults in	   the specific case */	c->MessageType = REMOTE_NDIS_QUERY_CMPLT;	//c->Status DO NOT SET YET - as m->Oid resides in this space. We still need it...	c->InformationBufferLength = 4;	c->InformationBufferOffset = 16;		switch (m->Oid) {		/**** GENERAL ****/        case OID_GEN_SUPPORTED_LIST:			c->InformationBufferLength = 4 * OID_LIST_LENGTH;			//Copy data to SRAM			memcpy_P(INFBUF, OIDSupportedList, 4*OID_LIST_LENGTH);			break;        case OID_GEN_HARDWARE_STATUS:       			*INFBUF = 0x00000000; /* Ready and Willing */  			break;        case OID_GEN_MEDIA_SUPPORTED:                   case OID_GEN_MEDIA_IN_USE: 			*INFBUF = NDIS_MEDIUM_802_3; /* NDIS_MEDIUM_WIRELESS_LAN instead? */  			break;			        case OID_GEN_MAXIMUM_FRAME_SIZE:			*INFBUF = (uint32_t) 1280; //1280 //102; /* Assume 25 octet header on 15.4 */			break;        case OID_GEN_LINK_SPEED:            			*INFBUF = (uint32_t) 100; /* in 100 bytes/sec units.. this is kinda a lie */			break;         case OID_GEN_TRANSMIT_BLOCK_SIZE:               case OID_GEN_RECEIVE_BLOCK_SIZE:   			*INFBUF = (uint32_t) 102; 			break;			             case OID_GEN_VENDOR_ID:			*INFBUF = 0xFFFFFF; /* No vendor ID ! */			break;			                         case OID_GEN_VENDOR_DESCRIPTION:        			c->InformationBufferLength = 8;			memcpy_P(INFBUF, PSTR("Atmel\0\0\0\0"), 8);			break;        case OID_GEN_CURRENT_PACKET_FILTER:     			*INFBUF =  oid_packet_filter;			break;        case OID_GEN_MAXIMUM_TOTAL_SIZE:        			*INFBUF = (uint32_t) 1300; //127; 			break;        case OID_GEN_MEDIA_CONNECT_STATUS: 			*INFBUF = NDIS_MEDIA_STATE_CONNECTED;			break;			     

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -