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

📄 chatjoin.c

📁 基于cc1010的设计实例
💻 C
字号:
/*****************************************************************************
 *                                                                           *
 *        **********                                                         *
 *       ************                                                        *
 *      ***        ***                                                       *
 *      ***   +++   ***                                                      *
 *      ***   + +   ***                                                      *
 *      ***   +                    CHIPCON CC1010 EXAMPLE PROGRAM            *
 *      ***   + +   ***                      CC1010 CHAT                     *
 *      ***   +++   ***                                                      *
 *      ***       ***                                                        *
 *       ***********                                                         *
 *        *********                                                          *
 *                                                                           *
 *****************************************************************************
 * Author:              JOL                                                  *
 *****************************************************************************
 * Revision history:                                                         *
 *                                                                           *
 * $Log: chatJoin.c,v $
 * Revision 1.1  2002/10/14 10:02:27  tos
 * Initial version in CVS.
 *
 *                                                                           *
 ****************************************************************************/

/*****************************************************************************
 *****************************************************************************
 *************        FUNCTIONS FOR JOINING THE CHAT ROOM        *************
 *****************************************************************************
 ****************************************************************************/
#include "chat.h"


//----------------------------------------------------------------------------
//  byte sendJoinAccepted()
//
//  Description:
//		SERVER:
//		Send a "Join Accepted" message to the new client
//
//	Parameters:
//		byte address
//			The assigned address
//		char xdata *pName	
//			The new client's name
//
//	Returned value:
//		byte
//			TRUE if successful (the message got through)
//			FALSE if not
//----------------------------------------------------------------------------
bool sendJoinAccepted(byte address, char xdata *pName) {
	
	// Prepare the transmission settings
	TXI.destination = NEW_CLIENT_ADDRESS;
	TXI.flags = MSG_JOIN_ACCEPTED | SPP_ACK_REQ;
	TXI.pDataBuffer = pAdminTXBuffer;
	TXI.dataLen = 1 + NAME_LENGTH;

	// Assigned SPP address and name
	TXI.pDataBuffer[0] = address;
	memcpy(&TXI.pDataBuffer[1], pName, NAME_LENGTH);

	// Send it
	sppSend(&TXI);
	while(SPP_STATUS() != SPP_IDLE_MODE);
	if (TXI.status == SPP_TX_FINISHED) {
		return TRUE;
	} else {
		return FALSE;
	}
} // sendJoinAccepted



	
//----------------------------------------------------------------------------
//  byte sendJoinDenied()
//
//  Description:
//		SERVER:
//		Send a "Join Denied" message to the new client (the server is full, or 
//		the name is already being used).
//
//	Returned value:
//		byte
//			JOIN_ACCEPTED, JOIN_DENIED or JOIN_ERROR
//----------------------------------------------------------------------------
void sendJoinDenied() {

	// Prepare the transmission settings
	TXI.destination = NEW_CLIENT_ADDRESS;
	TXI.flags = MSG_JOIN_DENIED | SPP_ACK_REQ;
	TXI.dataLen = 0;

	// Send it
	sppSend(&TXI);
	while (SPP_STATUS() != SPP_IDLE_MODE);

} // sendJoinDenied




//----------------------------------------------------------------------------
//  byte sendJoinRequest()
//
//  Description:
//      Request to join the chat room. This function will transmit a join
//		request, and wait for the answer to it. If successful (JOIN_ACCEPTED), 
//		it will update the client info table with the assigned SPP address.
//
//	Returned value:
//		byte
//			JOIN_ACCEPTED, JOIN_DENIED or JOIN_ERROR
//
//	Note: Our client name must be located in pMyCI->name
//----------------------------------------------------------------------------
byte sendJoinRequest() {

	// Prepare the RX and TX info structs
	RXI.maxDataLen = BUFFER_LENGTH;
	RXI.pDataBuffer = pRXBuffer;
	TXI.destination = SERVER_ADDRESS;
	TXI.pDataBuffer = pMyCI->name;
	TXI.dataLen = NAME_LENGTH;
	TXI.flags = MSG_JOIN_REQUEST | SPP_ACK_REQ;

	// Send the join request (return with an error code if it fails)
	sppSend(&TXI);
	while (SPP_STATUS() != SPP_IDLE_MODE);
	if (TXI.status != SPP_TX_FINISHED) {
		// RF error
		return JOIN_ERROR;
	}

	// Receive the answer to the join request (return with an error code if it fails)
	sppReceive(&RXI);
	while(SPP_STATUS() != SPP_IDLE_MODE);
	if (RXI.status != SPP_RX_FINISHED) {
		// RF error
		return JOIN_ERROR;
	} else {
		if ((RXI.flags & MSG_MASK) == MSG_JOIN_ACCEPTED) {
			if (memcmp(pMyCI->name, &(RXI.pDataBuffer[1]), NAME_LENGTH) == 0) {
				// Set my new address
				pMyCI->address = RXI.pDataBuffer[0];
				return JOIN_ACCEPTED;
			} else {
				// Not me
				return JOIN_ERROR;
			}
		} else if ((RXI.flags & MSG_MASK) == MSG_JOIN_DENIED) {
			return JOIN_DENIED;
		} else {
			return JOIN_ERROR;
		}
	}
} // sendJoinRequest

⌨️ 快捷键说明

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