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

📄 chatclientinfo.c

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

/*****************************************************************************
 *****************************************************************************
 *************     FUNCTIONS USED TO MAINTAIN A CLIENT TABLE     *************
 *****************************************************************************
 ****************************************************************************/
#include "chat.h"


//----------------------------------------------------------------------------
//  void initClientInfo(void)
//
//  Description:
//      Initialize the client info table (CI) by setting all SPP 
//		addresses to invalid values, and make sure that pDESKeys points to a
//		modulo-8 address
//----------------------------------------------------------------------------
void initClientInfo(void) {
	byte n;
	clientCount = 0;

	// Reset all
	for (n = 0; n < MAX_CLIENTS; n++) {
		CI[n].address = INVALID_ADDRESS;
		CI[n].pDESKey = pKeyBuffer + (CI_KEY_LENGTH * n);
	}

	// We'll take the first one
	pMyCI = &CI[0];

} // initClientInfo




//----------------------------------------------------------------------------
//  bool getClientInfo(byte address, CLIENT_INFO xdata *pCI)
//
//  Description:
//      Get a pointer to a client info entry
//
//  Arguments:
//      byte address
//			The SPP address of the client in *pCI
//
//	Returned value:
//		CLIENT_INFO xdata *
//			The returned pointer
//----------------------------------------------------------------------------
CLIENT_INFO xdata * getClientInfo(byte address) {
	byte n;

	for (n = 0; n < MAX_CLIENTS; n++) {
		if (CI[n].address == address) {
			return &CI[n];
		}
	}
	return NULL;
} // getClientInfo




//----------------------------------------------------------------------------
//  bool regClient(byte xdata *pAddress, char xdata *pName)
//
//  Description:
//      SERVER: Add a new client to the client info table (CI)
//
//  Arguments:
//      byte xdata *pAddress
//			The assigned SPP address
//
//      char xdata *pName
//			The new client's name
//
//	Returned value:
//		TRUE if successful, FALSE otherwise (too many users)
//----------------------------------------------------------------------------
bool regClient(byte xdata *pAddress, char xdata *pName) {
	byte n, m;
	bool addressUsed;
	byte address;
	byte addIndex;
	
	if (clientCount < MAX_CLIENTS) {

		// Find an empty slot
		for (n = 1; n < MAX_CLIENTS; n++) {
			if (CI[n].address == INVALID_ADDRESS) {
				addIndex = n;
				break;
			}
		}

		// Avoid duplicate names!
		for (n = 0; n < MAX_CLIENTS; n++) {
			if (CI[n].address != INVALID_ADDRESS) {
				if (memcmp(CI[n].name, pName, NAME_LENGTH) == 0) {
					return FALSE;
				}
			}
		}
		memcpy(CI[addIndex].name, pName, NAME_LENGTH);

		// Assign an address between 1 and MAX_CLIENTS
		for (address = 1; address < (MAX_CLIENTS + 1); address++) {
			addressUsed = FALSE;
			for (m = 1; m < MAX_CLIENTS; m++) {
				if (address == CI[m].address) {
					addressUsed = TRUE;
					break;
				}
			}
			if (!addressUsed) {
				*pAddress = CI[addIndex].address = address;
				break;
			}
		}

		// Other inits
		CI[addIndex].rxFlags = SPP_SEQUENCE_BIT;
		CI[addIndex].txFlags = 0x00;
		CI[addIndex].isDESKeyValid = FALSE;
		clientCount++;
		return TRUE;
	} else {
		return FALSE;
	}
} // regClient




//----------------------------------------------------------------------------
//  bool addClientInfo(char *pName, byte address)
//
//  Description:
//      CLIENT: Add a new client to the client info table (CI)
//
//  Arguments:
//      byte address
//			The SPP address of the new client
//
//      char xdata *pName
//			The new client's name
//
//	Returned value:
//		TRUE if successful, FALSE otherwise (too many users)
//----------------------------------------------------------------------------
void addClientInfo(byte address, char xdata *pName) {
	byte n;
	byte addIndex;
	
	// Make sure that the client isn't already registered
	for (n = 0; n < MAX_CLIENTS; n++) {
		if (CI[n].address == address) {
			// Update the name
			return;
		}
	}

	// Find an empty slot
	for (n = 0; n < MAX_CLIENTS; n++) {
		if (CI[n].address == INVALID_ADDRESS) {
			addIndex = n;
			break;
		}
	}

	// Set name, address, key-valid-flag and spp flags
	memcpy(CI[addIndex].name, pName, NAME_LENGTH);
	CI[addIndex].address = address;
	CI[addIndex].isDESKeyValid = FALSE;
	CI[addIndex].rxFlags = SPP_SEQUENCE_BIT;
	CI[addIndex].txFlags = 0x00;
	clientCount++;

} // addClientInfo




//----------------------------------------------------------------------------
//  void delClientInfo(byte xdata address)
//
//  Description:
//      Remove a client from the client info table (CI)
//
//  Arguments:
//      byte address
//			The SPP address of the removed client
//----------------------------------------------------------------------------
void delClientInfo(byte address) {
	byte n;
	for (n = 0; n < MAX_CLIENTS; n++) {
		if (CI[n].address == address) {
			CI[n].address = INVALID_ADDRESS;
			clientCount--;
			break;
		}
	}
} // delClientInfo




//----------------------------------------------------------------------------
//  void replaceClientInfo(byte *pCIBuffer, byte ciCount)
//
//  Description:
//      Compare the received client list with our own table and make changes 
//		as necessary.
//
//	Arguments:
//		byte *pCIBuffer
//			The new client list (address0 | name0 | address1 | name1 | ...)
//		byte ciCount
//			The new total number of clients (including the server)
//----------------------------------------------------------------------------
void replaceClientInfo(byte *pCIBuffer, byte ciCount) {
	byte n,m;
	bool clientStillActive;

	// Delete the recently removed client(s)
	for (n = 1; n < MAX_CLIENTS; n++) {
		if (CI[n].address != INVALID_ADDRESS) {
			clientStillActive = FALSE;
			for (m = 0; m < ciCount; m++) {
				if (pCIBuffer[m * (1 + NAME_LENGTH)] == CI[n].address) {
					clientStillActive = TRUE;
				}
			}				
	
			if (!clientStillActive) {
				delClientInfo(CI[n].address);
			}
		}
	}

	// Just try to re-add all the clients (duplicates are handled by the add function)
	n = 0;
	while (n < (ciCount * (1 + NAME_LENGTH))) {
		addClientInfo(pCIBuffer[n], &pCIBuffer[n + 1]);
		n += 1 + NAME_LENGTH;
	}

} // replaceClientInfo

⌨️ 快捷键说明

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