📄 chat.h
字号:
/*****************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC1010 EXAMPLE PROGRAM *
* *** + + *** CC1010 CHAT *
* *** +++ *** *
* *** *** *
* *********** *
* ********* *
* *
*****************************************************************************
* Author: JOL *
*****************************************************************************
* Revision history: *
* *
* $Log: chat.h,v $
* Revision 1.2 2002/11/19 13:48:44 kht
* Added startup macros
*
* Revision 1.1 2002/10/14 10:02:25 tos
* Initial version in CVS.
*
* *
****************************************************************************/
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <chipcon/cc1010eb.h>
#include <chipcon/vt100.h>
#include <chipcon/cul.h>
#include <chipcon/hal.h>
#define CLK_FREQ 14746
#define BUFFER_LENGTH 255
/********************************** MESSAGES **************************************
Message Code Source Destination Message Data
-----------------------------------------------------------------------------------
MSG_JOIN_REQUEST New client Server [Name]
MSG_JOIN_ACCEPTED Server New client [Assigned SPP address][Name]
MSG_JOIN_DENIED Server New client -
MSG_DISCONNECTED Server Client -
MSG_NEW_CLIENT_LIST Server All clients [(CI.addr|CI.name) list]
MSG_CHAT_BROADCAST_UP Client Server [The message]
MSG_CHAT_BROADCAST_DOWN Server All clients [The message][C's address]
MSG_DIFFIE_HELLMAN_UP Client 1 Client 2 [Key generation material from C1]
MSG_DIFFIE_HELLMAN_DOWN Client 2 Client 1 [Key generation material from C2]
MSG_PING Server All clients [Valid address bitmask, 2 bytes]
**********************************************************************************/
// SPP flag mask
#define MSG_MASK 0xF0
// SPP flags
#define MSG_JOIN_REQUEST 0x00
#define MSG_JOIN_ACCEPTED 0x10
#define MSG_JOIN_DENIED 0x20
#define MSG_NEW_CLIENT_LIST 0x30
#define MSG_CHAT_UP 0x40
#define MSG_CHAT_DOWN 0x50
#define MSG_DIFFIE_HELLMAN_UP 0x60
#define MSG_DIFFIE_HELLMAN_DOWN 0x70
#define MSG_PING 0x80
//----------------------------------------------------------------------------
// SPECIAL ADDRESSES (SPP)
#define NEW_CLIENT_ADDRESS 254
#define SERVER_ADDRESS 255
#define INVALID_ADDRESS 0
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// UART0 FLOW CONTROL
#define UART0_FLOW_CONTROL (P1_5)
#define STOP 1
#define READY 0
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// STATUS BAR (AT THE TOP OF THE CHAT WINDOW)
void printStatus(byte statusCode, byte address);
#define KEY_EXCHANGE_FAILED 0
#define KEY_EXCHANGE_OK 1
#define DECRYPTION_MODE 2
#define DISCONNECTED 3
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// PING RECEPTION
// The server transmits a "ping" (SPP_BROADCAST) at regular intervals
// If a client doesn't receive any pings for NO_PING_TIMEOUT/100 seconds,
// it will assume that the radio link is down, and disconnect.
#define NO_PING_TIMEOUT 2000
extern word xdata noPingTicks;
//----------------------------------------------------------------------------
//----------------------------------------------------------------------------
// CLIENT INFORMATION
#define MAX_CLIENTS 16 // Max 16 clients (including the server)
#define NAME_LENGTH 11
typedef struct {
char name[NAME_LENGTH]; // My name
byte address; // SPP address
bool isDESKeyValid; // Is the key *pDESKey valid?
byte *pDESKey; // A pointer to some modulo-8 address in pKeyBuffer
byte rxFlags; // The last RXI.flags received from this client
byte txFlags; // The last TXI.flags transmitted to this client
} CLIENT_INFO;
#define CI_KEY_LENGTH 8 // DES key length (when stored in the key buffer)
extern byte xdata pKeyBuffer[CI_KEY_LENGTH * MAX_CLIENTS];
//----------------------------------------------------------------------------
void disconnected (void);
#define MAX_INPUT_LENGTH 68
#define TERMINAL_KEY_ENTER 0x0D
#define TERMINAL_KEY_BACKSPACE 0x08
#define VT100_CHAT_SECTION() \
do { \
printf("\0"); \
VT100_GO_TO_POS(19, 80); \
VT100_SET_BGCOLOR(BLACK, DIM); \
VT100_SET_FGCOLOR(WHITE, DIM); \
} while (0)
#define VT100_INPUT_SECTION() \
do { \
printf("\0"); \
VT100_GO_TO_POS(20, 1); \
VT100_SET_BGCOLOR(CYAN, DIM); \
VT100_SET_FGCOLOR(WHITE, BRIGHT); \
} while (0)
#define VT100_CLIENT_LIST_SECTION() \
do { \
printf("\0"); \
VT100_GO_TO_POS(21, 1); \
VT100_SET_BGCOLOR(BLACK, DIM); \
VT100_SET_FGCOLOR(WHITE, DIM); \
} while (0)
#define VT100_INIT_LOGIN_SCREEN() \
do { \
printf("\0"); \
VT100_SET_BGCOLOR(BLACK, DIM); \
VT100_SET_FGCOLOR(CYAN, BRIGHT); \
VT100_CLEAR_SCREEN(); \
} while (0)
#define VT100_STATUS_SECTION() \
do { \
printf("\0"); \
VT100_GO_TO_POS(1, 1); \
} while (0)
#define VT100_INIT_CHAT_SCREEN() \
do { \
printf("\0"); \
VT100_SET_SCROLL_REGION(2, 19); \
VT100_SET_BGCOLOR(BLACK, DIM); \
VT100_CLEAR_SCREEN(); \
VT100_INPUT_SECTION(); \
VT100_CLEAR_LINE(); \
} while (0)
// Reception timeouts
#define SHORT_RX_TIMEOUT 10
#define LONG_RX_TIMEOUT (pMyCI->address == SERVER_ADDRESS ? 75 : 150)
#define RETRY_TX_TIMEOUT (30 + (rand() & 0x001F))
// Function prototypes
void printMessage(char xdata *pName, bool isPrivate, char xdata *pMessage, byte messageLen);
void printClientList(void);
void clearInputSection(void);
void initClientInfo(void);
void replaceClientInfo(byte *pCIBuffer, byte ciCount);
CLIENT_INFO xdata * getClientInfo(byte address);
void addClientInfo(byte address, char xdata *pName);
void delClientInfo(byte address);
bool setClientKey(byte address, byte xdata *pKeys);
bool regClient(byte xdata *pAddress, char xdata *pName);
void processMessage(void);
void broadcastClientList();
void broadcastMessage(byte sourceAddress, char xdata *pMessage, byte messageLen);
bool diffieHellmanUp(byte destAddress);
void diffieHellmanDown(CLIENT_INFO xdata *pSourceCI, byte xdata *pKeyMaterial);
// Request to join the chat room
byte sendJoinRequest(void);
#define JOIN_ACCEPTED 0 // OK!
#define JOIN_DENIED 1 // The server is full!
#define JOIN_ERROR 2 // Some RF error (retry)
bool sendJoinAccepted(byte address, char xdata *pName); // SERVER: Positive answer
void sendJoinDenied(); // SERVER: Negative answer
extern byte xdata pMessageTXBuffer[BUFFER_LENGTH];
extern byte xdata pAdminTXBuffer[BUFFER_LENGTH];
extern byte xdata pRXBuffer[BUFFER_LENGTH];
extern byte xdata *pInputBuffer;
extern SPP_RX_INFO xdata RXI;
extern SPP_TX_INFO xdata TXI;
extern CLIENT_INFO xdata CI[MAX_CLIENTS];
extern CLIENT_INFO xdata *pMyCI;
extern byte xdata clientCount;
extern byte xdata noDecryption;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -