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

📄 lap.c

📁 MTK平台绝密核心代码之 外设管理(红外线)
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************
*  Copyright Statement:
*  --------------------
*  This software is protected by Copyright and the information contained
*  herein is confidential. The software may not be copied and the information
*  contained herein may not be used or disclosed except with the written
*  permission of MediaTek Inc. (C) 2005
*
*  BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
*  THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
*  RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
*  AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
*  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
*  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
*  NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
*  SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
*  SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
*  THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
*  NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
*  SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
*  BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
*  LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
*  AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
*  OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
*  MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE. 
*
*  THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
*  WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
*  LAWS PRINCIPLES.  ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
*  RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
*  THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/

/*****************************************************************************
 *           
 * Filename:
 * ---------    
 *    LAP.c
 *
 * Project:
 * --------
 *   Maui_Software
 *
 * Description:
 * ------------
 *   This file contains the comman part of LAP
 *
 * Author:
 * -------
 * -------
 *
 *****************************************************************************/
#include "kal_release.h"      /* Basic data type */
//#include "kal_nucleus_common_defs.h"/*tick transition*/
#include "stack_common.h"
#include "stack_msgs.h"
#include "app_ltlcom.h"       /* Task message communiction */
#include "stack_timer.h"      /* Stack timer */
#include "intrCtrl.h"
#include "irda_enums.h"       /* irda enum */  
#include "gprs_flc_kal.h"  
#include "gprs_flc_common_data_types.h"
#include "flc_tunable_para.h"
#include "irconfig.h"	/* IrPro system configuration */
#include "lap.h"	      /* LAP source header file */
#include "lmp.h"	      /* LMP source header file */
#include "iap_p.h"	   /* IAP primary stack header file */
#include "iap_s.h"	   /* IAP primary stack header file */
#include "support.h"	   /* system support functions */
#include "externs.h"	   /* system external definitions */
#include "irda.h"
#include "irframer_sw.h"
#include "stack_config.h"
#include "kal_trace.h"                     
#include "irda_trace.h"



/* connection parameters */
kal_uint32  new_baud;
kal_int16   new_ttt;
kal_int16   new_size;
kal_uint8   new_xbofs;
kal_uint8   new_minttt;
kal_uint8   new_ld;

kal_uint8   stack;		/* stack type 0=secondary, 1=primary */
kal_uint8   tgbuf;		/* free buffer count */
struct  LAP_REQUEST_Q lap_request_q; 	/* lap request queue */	
struct  DECODE_Q decode_q;		/* lap decode queue */
struct  CTRL_BUFF_Q ctrl_buff_q;

kal_int16   max_frame_size; /* max frame size set at negotiation */
FBUF    * CurBuf; 	/* ptr to next available buffer */
kal_uint8	remote_busy; 	/* true: RNR command received, false: RR command received*/
kal_uint8	local_busy;  	/* set by application to signal remote for busy */
FBUF    * unacked_frame;/* pointer to unacked I frame */
kal_uint8	Ns;  		/* sequence number of transmitted frame */
kal_uint8	Nr;  		/* sequence number of the next expected sequence frame */
kal_uint8	Vr;  		/* next sequenced frame expected to be received */
kal_uint8	Vs;  		/* next sequenced frame to be transmitted */
kal_uint8	As;  		/* the I-response sequence number already sent */
kal_uint8	connection_address; /* used in local address field of all frames */
kal_uint32	destination_address; /* the remote connection address */
kal_uint8    *  tx_lap_frame; /* temporary pointer */
kal_uint32  dev_address;    /* the local device address */
kal_uint8   irlap_state;	/* irlap state machine state */
kal_uint8	  bofs;		/* beginning of frame byte pad */
kal_uint8	  window_size;	/* current window size */
kal_uint8   irp_connect;	/* LAP connect flag */
kal_uint16  media_busy_mark;/* time mark use to calculate media busy time delta */
kal_uint8   media_busy;     /* low level media busy flag */
kal_uint8   LAP_discon_code;/* LAP disconnect reason code */
kal_uint8   lap_disconnect_flag;/*if set 1, LAP will send RD/DISC to remote */
                            /*station when he has right*/
kal_uint8   max_buf_count;
extern kal_bool irda_tun_on;
/*************************************************************************
* FUNCTION
*	IRLAP_Return_Maxframe_Size
*
* DESCRIPTION
*	This function is used to decode a LAP frame 
*
* PARAMETERS
*  FBUF * dframe
* RETURNS
*  0 - known frame   
*  1 - unknown command 
*  2 - frame address not for us
*  3 - unknown format type
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint16 IRLAP_Return_Maxframe_Size ( void )
{
    return (max_frame_size);      
}   
/*************************************************************************
* FUNCTION
*	IRLAP_frame_decode
*
* DESCRIPTION
*	This function is used to decode a LAP frame 
*
* PARAMETERS
*  FBUF * dframe
* RETURNS
*  0 - known frame   
*  1 - unknown command 
*  2 - frame address not for us
*  3 - unknown format type
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint8 IRLAP_frame_decode ( FBUF  * dframe )
{
	kal_uint8  *  frame;
	

#ifdef IRDA_KAL_TRACE  
     kal_trace(TRACE_FUNC, IRDA_MSG1); 
#endif
   if(irda_tun_on==KAL_FALSE)
   {
      Freebuf((FBUF*)dframe);/* free all control frame buffers */
      return 0;
   }   
   /*restart open timer if receiving a new frame*/
   if(irda_open_time!=0xffff)
      stack_start_timer(&irda_context.irda_open_timer, IRDA_OPEN_TIMER_INDEX,       
      irda_open_time*KAL_TICKS_1_MIN);
   
	frame=(kal_uint8*)dframe+FBUF_HEAD;
	
	/* check for test frame */
	if (frame[1]==TEST_COMMAND)
	{
		START_WD_TIMER(); 
		if (irlap_state == IRLAP_NDM )
		{
			dframe->type=TEST_FRAME_TYPE;
			Com_write(Format_T_packet(1,0,dframe,0));
			return(0);
		}
		/* is this valid to have the primary respond to a test frame in peer mode? */
		//else if(irlap_state == IRLAP_NRMS )
		else
		{
		/* just respond with the link "connect address" test response 
			*/
			dframe->type=TEST_FRAME_TYPE;
			Com_write(Format_T_packet(1,0,dframe,0));
			return(0);
		}
	}
	#if 0/*remove this part to driver to speed up*/	
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
/* under construction !*/
	#endif
	
	/* used for monitoring traffic across links */
	
	/* this frame is not for us */
	if (((frame[1]&0x01)==I_FRAME_HEAD)&&(dframe->size>2))/*I Frame, 2 LAP, 2/3 LMP+TTP*/
	{
		/* I frame */
		if (stack)
		{
			Do_i_frame_p(dframe);
		}
		else
		{
			Do_i_frame_s(dframe);
		}
	}
	else 
	{
		switch (frame[1]&0x03)
		{
		case S_FRAME_HEAD:/*S Frame*/
			if (stack)
			{
				Do_s_frame_p(frame);
			}
			else
			{
				Do_s_frame_s(frame);
			}
			break;
		case U_FRAME_HEAD:/*U Frame*/
			if (stack)
			{
				Do_u_frame_p(frame);
			}
			else{
				Do_u_frame_s(frame);
			}

			
			break;
		default:
			if ( irlap_state==IRLAP_NRMP)
			{
				/* is c/r bit = res and, p/f bit set */
				if (((frame[0]&0x01)==0)&&(frame[1]&0x10))
				{	
					SEND_S_COMMAND();
					if (stack) 
						START_F_TIMER();
				}
			}
			if(dframe->stat==B_ALLOC)
		   		Freebuf((FBUF*)dframe);/* free all control frame buffers */
			return(3);
			/* unknown frame type */
		}
		if(dframe->stat==B_ALLOC)
		   Freebuf((FBUF*)dframe);/* free all control frame buffers */
	}
	return(0);
}

/*************************************************************************
* FUNCTION
*	flush_ctrl_buff_q
*
* DESCRIPTION
*	This function is used to free all control buffer in control_buff_q 
*
* PARAMETERS
*  None

* RETURNS
*  None
*************************************************************************/
void flush_ctrl_buff_q(void)
{
  peer_buff_struct* tmp_peer_buf_ptr=0;
  kal_uint8  i; 
  for(i=0;i<MAX_TTP_FRAMES;i++)
  {
     if(ctrl_buff_q.frame[i]!=0)
     { 
        gprs_flc_free_peer_buff ( 
                           IRDA_DATA,
                           0,
                           GPRS_FLC_UL,
                           0,
                           //tmp_peer_buf_ptr);	
                           ctrl_buff_q.frame[i]);
     }                      
     /*reset all control buffer pointer as 0*/                  
     ctrl_buff_q.frame[i]=0;   
   }                        
     ctrl_buff_cnt=0;                                
   /*initialize Q*/
   INIT_Q(ctrl_buff_q,MAX_TTP_FRAMES);                       
}  
/*************************************************************************
* FUNCTION
*	IrLAP_LINK_init
*
* DESCRIPTION
*	This function is used to initialize the LAP link
*
* PARAMETERS
*
* RETURNS
*	0 if success
*  1 if no buffers
*
* GLOBALS AFFECTED
* initializes:
. timer vars
. our device address
. connection params
. allocates the receive frame buffer
. This should only have to be called once at powerup.
*************************************************************************/
kal_uint8 IrLAP_LINK_init( void )
{
	kal_uint8 result=0;


	LAP_discon_code=0;	
	/*lpa disconnect flag*/
	lap_disconnect_flag =0;  	
	/* init default link disconnect timeout */
	new_ld = DEFAULT_LINK_TIMEOUT;	
	
	/*free unacked frame*/
	if(unacked_frame!=0) 
	   Freebuf(unacked_frame);
	   unacked_frame=0;	
	
	/*flush decode q and lap_request q*/   
	IRQMask(IRQ_IrDA_CODE);
	QUEUE_FLUSH(decode_q,MAX_DECODE_FRAMES);		
	QUEUE_FLUSH(lap_request_q,MAX_REQUEST);	
	InitBuf();
	IRQUnmask(IRQ_IrDA_CODE);		
	INIT_Q(lap_request_q,MAX_REQUEST);
	INIT_Q(decode_q,MAX_DECODE_FRAMES);
	
   flush_ctrl_buff_q();	   	
	//INIT_Q(ctrl_buff_q,MAX_TTP_FRAMES);
	
	/* generate random 32bit device address */
	/* dev address can't be 0 or -1 (all ff's )*/
	dev_address= random32();
	while((dev_address==0x00000000)||(dev_address==0xffffffff))
		dev_address= random32();
		
	/* init state vars */	
	rx_lap_frame=0;	
		
	ENABLE_PRIMARY_STACK();/*@@@ stack =1*/	
	/* init connection params */
	
	Apply_default_connection_params();	
	return(result);
}


/*************************************************************************
* FUNCTION
*	Apply_default_connection_params
*
* DESCRIPTION
*	This function is used to apply default LAP link connection parameters
*
* PARAMETERS
*  None
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void Apply_default_connection_params( void )
{
	
	max_frame_size		   =IRLAP_FRAME_SIZE;
	irp_connect 		   =DISCONNECT_INDICATION; 
	irlap_state		      =IRLAP_NDM;
	window_size		      =DEFAULT_WINDOW_SIZE;
	bofs			         =CONTENTION_BOFS;
	
	/* set media busy true */
	media_busy		      =1; 
	/* default,p timer must never exceed 500 MS */
	p_timeout		=P_MSTIMEOUT;	
	/* set ftimer timeout to an arbitrary large number */
	N1			=20;		
	old_slot=127;	
	f_mstimeout = F_MSTIMEOUT_DEFAULT; 	
	IRDA_SetBof(bofs);		
}



/*************************************************************************
* FUNCTION
*	Init_connection_state
*
* DESCRIPTION
*	This function is used to initialize the connection state
*
* PARAMETERS
*  kal_uint8 state
* RETURNS
*	None
*
* GLOBALS AFFECTED
*
*************************************************************************/
void Init_connection_state( kal_uint8 state )
{
	irlap_state = state;
	remote_busy = FALSE;
	local_busy  = FALSE;
	Vr = 0;
	Vs = 0;
	As = 127;  /* ??? */
}


/*************************************************************************
* FUNCTION
*	Format_I_Packet
*
* DESCRIPTION
*	This function implements xyz entity's create handler.
*
* PARAMETERS
*  kal_uint8 cr
   kal_uint8 pf
   FBUF * dframe 
   kal_uint16 size
* RETURNS
*	Pointer to formated frame packet
*
* GLOBALS AFFECTED
*
*************************************************************************/
FBUF  * Format_I_Packet ( kal_uint8 cr,
						 kal_uint8 pf,
						 FBUF  * dframe, 
						 kal_uint16 size )
{
      kal_uint8  *  frame;	

#ifdef IRDA_KAL_TRACE  	  
    kal_trace(TRACE_FUNC, IRDA_MSG3);  
#endif
	
	frame=(kal_uint8  *)dframe+FBUF_HEAD;
	
	frame[0]=(connection_address&0xfe)|cr;/*A field */
	frame[1]=(Vr<<5)|(Vs<<1)|pf; /*C field*/
   dframe->size=2+size;/*+2 for A and C field*/					   
   return(dframe);
}
/*************************************************************************
* FUNCTION
*	Format_packet
*
* DESCRIPTION
*	This function is used to get a frame and Format the packet
*
* PARAMETERS
*  kal_uint8 cr : bit0=command/response bit, 
   bit1=address conflict
   bit2=final XID command frame 
   kal_uint8 type : type of packet to format 
   valid types are:
   SNRM_COMMAND
   UA_RESPONSE
   XID_COMMAND
   DISC_COMMAND
   RR_COMMAND
   RD_RESPONSE
* RETURNS
*	tx_lap_frame is used as a temporary global 
   pointer to misc buffe
*
* GLOBALS AFFECTED
*
*************************************************************************/
kal_uint32 testptr;
FBUF  * Format_packet ( kal_uint8 cr,kal_uint8 type)
{
	FBUF   *  tmpptr1;
	kal_uint8  *  frame;
	kal_int16     fsize=0;
	kal_uint32    tmplong;
	
#ifdef IRDA_KAL_TRACE  
     kal_trace(TRACE_FUNC, IRDA_MSG4);  	  	  
#endif		
	if ((tmpptr1=Getbuf())==0)
	{
	   ASSERT(0);
	}

	if (testptr == 0)
	   testptr = (kal_uint32)tmpptr1;

	frame=(kal_uint8  *)tmpptr1+FBUF_HEAD;
	
	if ( type == SNRM_COMMAND ) 
		frame[0]= 0xff; /*Broadcast Address*/
	else if ( type == UA_RESPONSE  ) 
		frame[0]= connection_address; 
	else
		frame[0]=(connection_address&0xfe)|(cr&1);
	
	frame[1]=type; 
	

	if ( type == XID_COMMAND )
	{ 
		frame[0]|=0xfe; /* or in broadcast address 0xfe */
		frame[2]=0x01;  /* format id */
		
		/* src 32bit address */
		tmplong= Swap_long_order(dev_address);
		xmemcpy((kal_uint8  *)&frame[3],(kal_uint8  *)&tmplong,4);
		
		/* dest 32bit address */
		tmplong=0xffffffff;/*broadcast address*/
		xmemcpy((kal_uint8  *)&frame[7],(kal_uint8  *)&tmplong,4);
		

		switch(MAX_SLOTS)/*6*/
		{
			case 1:
				frame[11]=0x00;       /* 1 slots */
				break;
			case 6:
				frame[11]=0x01;       /* 6 slots */
				break;
			case 8:
				frame[11]=0x02;       /* 8 slots */
				break;
			case 16:
				frame[11]=0x03;       /* 16 slots */
				break;
			default:

⌨️ 快捷键说明

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