📄 sctrl.c
字号:
/*
* Copyright 2006 by Texas Instruments Incorporated.
* All rights reserved. Property of Texas Instruments Incorporated.
* Restricted rights to use, duplicate or disclose this code are
* granted through contract.
*
* @(#) TCP/IP_Network_Developers_Kit 1.91.00.08 08-22-2006 (ndk-a08)
*/
//--------------------------------------------------------------------------
// IP Stack Server Demonstration Program
//--------------------------------------------------------------------------
// SCTRL.C
//
// This module is where serial port characters go before a connection
// has been established. It is in charge of launching the HDLC client
// or server.
//
// Author: Michael A. Denio
// Copyright 1999, 2001 by Texas Instruments Inc.
//--------------------------------------------------------------------------
#include <netmain.h>
#include <_stack.h>
#include "../../../tools/common/hdlc/hdlcif.h"
#define OUTMAX 128
static char OutBuf[OUTMAX];
static int OutCnt = 0;
static HANDLE hOurSem = 0;
static uint fRunning = 0;
//
// This function is called from the serial port drvier to service
// a character
//
static void sioInputChar(char c)
{
// If we're running, add character to buffer and wake task
if( hOurSem )
{
if( OutCnt < OUTMAX )
OutBuf[OutCnt++] = c;
// Post to our task semaphore
SemPost( hOurSem );
}
}
//
// sctrlAbort()
//
// Abort Serial Port Control Function
//
sctrlAbort()
{
fRunning = 0;
SemPost( hOurSem );
}
//
// sctrl()
//
// Serial Port Control Function
//
void sctrl( uint DevSerial )
{
uint status = 0;
uint fConnectMsg = 0;
uint fInput = 0;
HANDLE hHDLC = 0;
CI_ACCT CA;
hOurSem = SemCreate( 0 );
if( !hOurSem )
goto Exit;
fRunning = 1;
// HAL functions without leading underscore '_' must be
// called from within a llEnter()/llExit() pairing.
llEnter();
// Setup the serial port driver
llSerialConfig( 1, 115200,
HAL_SERIAL_MODE_8N1, HAL_SERIAL_FLOWCTRL_NONE );
// Open the serial port driver for basic IO
llSerialOpen( 1, &sioInputChar );
llExit();
//
// Add a test account to the configuration. For the test, use
// Username = "username" and Password = "password". These are
// the values the CLIENT example will use.
//
strcpy( CA.Username, "username" );
strcpy( CA.Password, "password" );
CA.Flags = CFG_ACCTFLG_CH1;
CfgAddEntry( 0, CFGTAG_ACCT, CFGITEM_ACCT_PPP,
CFG_ADDMODE_NOSAVE, sizeof(CI_ACCT),
(UINT8 *)&CA, 0 );
printf("SCtrl: Ready\n");
while(1)
{
SemPend( hOurSem, 1000 );
// See if we're still running
if( !fRunning )
{
llEnter();
llSerialClose(1);
llExit();
goto Exit;
}
//------------------------------
// Read character from buffer
// OutBuf = Start of characters to read
// OutCnt = Number of characters to read
//------------------------------
// In this example program, we don't examine input
// for the serial port. This would be where a modem
// state machine would be placed. Here, we simply
// flag that we got some serial data input, then
// immediately launch the HDLC server session.
fInput = OutCnt;
OutCnt = 0;
//
// If we don't have an active HDLC session, and we have got
// serial port activity, we launch a new HDLC session.
//
if( !hHDLC && fInput )
{
printf("SCtrl: Launching Server\n");
// Send a string to the client to wake it up
_llSerialSend( 1, (UINT8 *)"CLIENTSERVER", 12 );
//
// NOTE: In this demo we specify a server IP address of
// 1.2.1.1, a netmask of 255.255.255.0, and a
// client IP address of 1.2.1.2 . Normally, these
// would be application configurable values.
//
hHDLC = hdlcsNew( 1, PPPFLG_SERVER | PPPFLG_OPT_USE_MSE |
PPPFLG_OPT_AUTH_PAP | PPPFLG_CH1 |
PPPFLG_SIOPT_SENDCMAP | PPPFLG_SIOPT_RECVCMAP,
0, htonl(0x01020101), htonl(0xffffff00),
htonl(0x01020102) );
}
// If we have an active HDLC session, get the status
if( hHDLC )
{
status = hdlcsGetStatus( hHDLC );
// If disconnected, print message and close
if( status >= SI_CSTATUS_DISCONNECT )
{
printf("SCtrl: Disconnected\n");
hdlcsFree( hHDLC );
hHDLC = 0;
fConnectMsg = 0;
}
// Else if connected, print message
else if( status == SI_CSTATUS_CONNECTED && !fConnectMsg )
{
printf("SCtrl: Connected\n");
fConnectMsg = 1;
}
}
}
Exit:
//
// General Cleanup
//
if( hHDLC )
hdlcsFree( hHDLC );
if( hOurSem )
SemDelete( hOurSem );
printf("SCtrl: Exit\n");
// This task is killed by the system - here, we block
TaskBlock( TaskSelf() );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -