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

📄 ubcsp.cpp

📁 关于S3C2442平台蓝牙模块驱动
💻 CPP
📖 第 1 页 / 共 3 页
字号:
#if SHOW_PACKET_ERRORS
	if (ubcsp_config.receive_packet->reliable)
	{
		printf (" : %10d         Recv SEQ: %d ACK %d\n",
			GetTickCount () % 100000,
			receive_seq,
			receive_ack);
			
			
	}
	else if (ubcsp_config.receive_packet->channel != 1)
	{
		printf (" : %10d          Recv        ACK %d\n",
			GetTickCount () % 100000,
			receive_ack);
			
	
	}
#endif

	/* Check for length errors */

#if UBCSP_CRC
	if (receive_crc)
	{
		/* If this packet had a CRC, then the length of the payload 
		   should be 2 less than the received size of the payload */

		if (length + 2 != ubcsp_config.receive_index)
		{
			/* Slip Length Error */

#if SHOW_PACKET_ERRORS
			printf ("\n######################## Slip Length Error (With CRC) %d,%d\n", length, ubcsp_config.receive_index - 2);
#endif

			/* If we have a payload length error, send an ack in return
			   this gets a packet to be resent as quickly as possible */

			ubcsp_config.send_ack = 1;
			return activity;
		}

		/* We have a CRC at the end of this packet */

		ubcsp_config.receive_index -= 2;

		/* Calculate the packet CRC */

		crc = 0xffff;

		/* CRC the packet header */

		for (loop = 0; loop < 4; loop ++)
		{
			crc = ubcsp_calc_crc (ubcsp_receive_header[loop], crc);
		}

		/* CRC the packet payload - without the CRC bytes */

		for (loop = 0; loop < ubcsp_config.receive_index; loop ++)
		{
			crc = ubcsp_calc_crc (ubcsp_config.receive_packet->payload[loop], crc);
		}

		/* Reverse the CRC */

		crc = ubcsp_crc_reverse (crc);

		/* Check the CRC is correct */

		if
		(
			(((crc & 0xff00) >> 8) != ubcsp_config.receive_packet->payload[ubcsp_config.receive_index]) ||
			((crc & 0xff) != ubcsp_config.receive_packet->payload[ubcsp_config.receive_index + 1])
		)
		{
#if SHOW_PACKET_ERRORS
			printf ("\n######################## CRC Error\n");
#endif

			/* If we have a packet crc error, send an ack in return
			   this gets a packet to be resent as quickly as possible */

			ubcsp_config.send_ack = 1;
			return activity;
		}
	}
	else
	{
#endif
		/* No CRC present, so just check the length of payload with that received */

		if (length != ubcsp_config.receive_index)
		{
			/* Slip Length Error */

#if SHOW_PACKET_ERRORS
			printf ("\n######################## Slip Length Error (No CRC) %d,%d\n", length, ubcsp_config.receive_index);
#endif

			/* If we have a payload length error, send an ack in return
			   this gets a packet to be resent as quickly as possible */

			ubcsp_config.send_ack = 1;
			return activity;
		}
#if UBCSP_CRC
	}
#endif

	/*** We have a fully formed packet having passed all data integrity checks ***/

	/* Check if we have an ACK for the last packet we sent */

	if (receive_ack != ubcsp_config.sequence_number)
	{
		/* Since we only have a window size of 1, if the ACK is not equal to SEQ
		   then the packet was sent */
	       //RETAILMSG(1,(TEXT("ubcsp_recevied_packet1 \n")));

		if((ubcsp_config.send_packet) && (ubcsp_config.send_packet->reliable))
		{
			/* We had sent a reliable packet, so clear this packet
			   Then increament the sequence number for the next packet */
                        //RETAILMSG(1,(TEXT("ubcsp_recevied_packet2 \n")));
			ubcsp_config.send_packet = 0;
			ubcsp_config.sequence_number ++;
			ubcsp_config.delay = 0;
			
		        //RETAILMSG(1,(TEXT("ubcsp_recevied_packet 4\n")));
		        /* Notify the caller that we have SENT a packet */
		 	activity |= UBCSP_PACKET_SENT;	
		}
		
		
		
	}

	/*** Now we can concentrate of the packet we have received ***/

	/* Check for Link Establishment packets */

	if (ubcsp_config.receive_packet->channel == 1)
	{
		/* Link Establishment */
		//RETAILMSG(1,(TEXT("ubcsp_recevied_packet5 \n")));

		ubcsp_config.delay = 0;

		/* Find which link establishment packet this payload means
		   This could return 5, meaning none */

		switch (ubcsp_which_le_payload (ubcsp_config.receive_packet->payload))
		{
			case 0:
			{
				/* SYNC Recv'd */
				//BT_DBGMSG(TEXT("$sync 0\r\n"));	
				//RETAILMSG(1,(TEXT("Recv SYNC \n")));

#if SHOW_LE_STATES
				printf ("Recv SYNC\n");
#endif

				/* If we receive a SYNC, then we respond to it with a SYNC RESP
				   but only if we are not active.
				   If we are active, then we have a PEER RESET */

				if (ubcsp_config.link_establishment_state < ubcsp_le_active)
				{
					ubcsp_config.link_establishment_resp = 1;
					//BT_DBGMSG(TEXT("$sync 01\r\n"));	
				        //RETAILMSG(1,(TEXT("Recv SYNC1 \n")));
				}
				else
				{
					/* Peer reset !!!! */

#if SHOW_LE_STATES
					printf ("\n\n\n\n\nPEER RESET\n\n");
					
#endif
                                        /* Reinitialize the link */
                                        //RETAILMSG(1,(TEXT("Recv SYNC2 \n")));
					ubcsp_initialize ();
					//BT_DBGMSG(TEXT("$sync 02\r\n"));		
					/* Tell the host what has happened */

					return UBCSP_PEER_RESET;
				}
				break;
			}

			case 1:
			{
				/* SYNC RESP Recv'd */
				//BT_DBGMSG(TEXT("$sync 1\r\n"));	
				//RETAILMSG(1,(TEXT("Recv SYNC RESP\n")));

#if SHOW_LE_STATES
				printf ("Recv SYNC RESP\n");
				////RETAILMSG(1,(TEXT("ubcsp_recevied_packet 4\n")));
#endif

				/* If we receive a SYNC RESP, push us into the initialized state */

				if (ubcsp_config.link_establishment_state < ubcsp_le_initialized)
				{
#if SHOW_LE_STATES
					printf ("Link Initialized\n");
#endif
					ubcsp_config.link_establishment_state = ubcsp_le_initialized;
					//BT_DBGMSG(TEXT("$sync 11\r\n"));	
				}

				break;
			}

			case 2:
			{
				/* CONF Recv'd */
				//RETAILMSG(1,(TEXT("Recv CONF\n")));
				//BT_DBGMSG(TEXT("$sync 2\r\n"));	
#if SHOW_LE_STATES
				printf ("Recv CONF\n");
				//RETAILMSG(1,(TEXT("ubcsp_recevied_packet 5\n")));
#endif

				/* If we receive a CONF, and we are initialized or active
				   then respond with a CONF RESP */

				if (ubcsp_config.link_establishment_state >= ubcsp_le_initialized)
				{
					//RETAILMSG(1,(TEXT("Recv CONF2\n")));
					//BT_DBGMSG(TEXT("$sync 21\r\n"));	
					ubcsp_config.link_establishment_resp = 2;
				}

				break;
			}

			case 3:
			{
				/* CONF RESP Recv'd */
				//RETAILMSG(1,(TEXT("Recv CONF RESP \n")));
				//BT_DBGMSG(TEXT("$sync 3\r\n"));	
#if SHOW_LE_STATES
				printf ("Recv CONF RESP\n");
				////RETAILMSG(1,(TEXT("ubcsp_recevied_packet 6\n")));
#endif    

				/* If we received a CONF RESP, then push us into the active state */

				if (ubcsp_config.link_establishment_state < ubcsp_le_active)
				{
#if SHOW_LE_STATES
					printf ("Link Active\n");
#endif
                                        //RETAILMSG(1,(TEXT("Link Active \n")));
					ubcsp_config.link_establishment_state = ubcsp_le_active;
					ubcsp_config.send_size = 0;
					//BT_DBGMSG(TEXT("$sync 31\r\n"));	
					return activity | UBCSP_PACKET_SENT;
				}

				break;
			}
		}

		/* We have finished processing Link Establishment packets */
	}
	else if (ubcsp_config.receive_index)
	{
		/* We have some payload data we need to process
		   but only if we are active - otherwise, we just ignore it */
		   
		//RETAILMSG(1,(TEXT("ubcsp_recevied_packet5 \n")));

		if (ubcsp_config.link_establishment_state == ubcsp_le_active)
		{
			//RETAILMSG(1,(TEXT("ubcsp_recevied_packet 6\n")));
			if (ubcsp_config.receive_packet->reliable)
			{
				/* If the packet we've just received was reliable
				   then send an ACK */

				ubcsp_config.send_ack = 1;
				//RETAILMSG(1,(TEXT("ubcsp_recevied_packet 7\n")));

				/* We the sequence number we received is the same as 
				   the last ACK we sent, then we have received a packet in sequence */

				if (receive_seq == ubcsp_config.ack_number)
				{
					/* Increase the ACK number - which will be sent in the next ACK 
					   or normal packet we send */
					   ////RETAILMSG(1,(TEXT("ubcsp_recevied_packet 8\n")));

					ubcsp_config.ack_number ++;

					/* Set the values in the receive_packet structure, so the caller
					   knows how much data we have */

					ubcsp_config.receive_packet->length = length;
					ubcsp_config.receive_packet = 0;

					/* Tell the caller that we have received a packet, and that it
					   will be ACK'ed */

					activity |= UBCSP_PACKET_RECEIVED | UBCSP_PACKET_ACK;
				}
			}
			else 
			{
				/* Set the values in the receive_packet structure, so the caller
				   knows how much data we have */
				//RETAILMSG(1,(TEXT("ubcsp_recevied_packet 9\n")));

				ubcsp_config.receive_packet->length = length;
				ubcsp_config.receive_packet = 0;

				/* Tell the caller that we have received a packet */

				activity |= UBCSP_PACKET_RECEIVED;
			}
		}
		
	}

	/* Just return any activity that occured */

	return activity;
}

/*****************************************************************************/
/**                                                                         **/
/** ubcsp_setup_packet                                                      **/
/**                                                                         **/
/** This function is called to setup a packet to be sent                    **/
/** This allows just a header, or a header and payload to be sent           **/
/** It also allows the header checksum to be precalcuated                   **/
/** or calculated here                                                      **/
/** part1 is always 4 bytes                                                 **/
/**                                                                         **/
/*****************************************************************************/

static void ubcsp_setup_packet (uint8 *part1, uint8 calc, uint8 *part2, uint16 len2)
{
	/* If we need to calculate the checksum, do that now */
       //RETAILMSG(1,(TEXT("ubcsp_setup_packet+++\n")));

	if (calc)
	{
		part1[3] =
			~(part1[0] + part1[1] + part1[2]);
	}

	/* Setup the header send pointer and size so we can clock this out */

	ubcsp_config.send_ptr = part1;
	ubcsp_config.send_size = 4;

	/* Setup the payload send pointer and size */

	ubcsp_config.next_send_ptr = part2;
	ubcsp_config.next_send_size = len2;

#if UBCSP_CRC
	/* Initialize the crc as required */

	ubcsp_config.send_crc = -1;

	ubcsp_config.need_send_crc = 1;
#endif
}

/*****************************************************************************/
/**                                                                         **/
/** ubcsp_sent_packet                                                       **/
/**                                                                         **/
/** Called when we have finished sending a packet                           **/
/** If this packet was unreliable, then notify caller, and clear the data   **/
/**                                                                         **/
/*****************************************************************************/

static uint8 ubcsp_sent_packet (void)
{
	//RETAILMSG(1,(TEXT("ubcsp_sent_packet+++\n")));
	if (ubcsp_config.send_packet)
	{
		//RETAILMSG(1,(TEXT("ubcsp_sent_packetA\n")));
		if (!ubcsp_config.send_packet->reliable)
		{
			/* We had a packet sent that was unreliable */

			/* Forget about this packet */
			//RETAILMSG(1,(TEXT("ubcsp_sent_packetB\n")));

			ubcsp_config.send_packet = 0;

			/* Notify caller that they can send another one */

			return UBCSP_PACKET_SENT;
		}
	}

	/* We didn't have a packet, or it was reliable
	   Must wait for ACK before allowing another packet to be sent */

	return 0;
}

/*****************************************************************************/

⌨️ 快捷键说明

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