📄 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, 2004 by Texas Instruments Inc.
//--------------------------------------------------------------------------
#include <netmain.h>
#include <_stack.h>
#include "client.h"
#include "../../../tools/common/console/console.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;
static char OutBuf2[OUTMAX];
static int OutCnt2 = 0;
static char ConBufferIn[OUTMAX];
static char ConBufferOut[OUTMAX*2];
//
// 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 );
}
}
static void sioInputChar2(char c)
{
// If we're running, add character to buffer and wake task
if( hOurSem )
{
if( OutCnt2 < OUTMAX )
OutBuf2[OutCnt2++] = c;
// Post to our task semaphore
SemPost( hOurSem );
}
}
//
// sctrlAbort()
//
// Abort Serial Port Control Function
//
void sctrlAbort()
{
fRunning = 0;
SemPost( hOurSem );
}
//
// sctrl()
//
// Serial Port Control Function
//
void sctrl()
{
uint ports = 1;
uint status = 0;
uint fConnectMsg = 0;
HANDLE hHDLC = 0;
SOCKET sio = INVALID_SOCKET; // For use with console
struct sockaddr_in sin1; // For use with console
int cnt,i,j; // For use with console
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 );
// Setup the 2nd serial port driver (if any) for console
llSerialConfig( 2, 19200,
HAL_SERIAL_MODE_8N1, HAL_SERIAL_FLOWCTRL_NONE );
// Open the 2nd serial port driver (if any) for basic IO
if( llSerialOpen( 2, &sioInputChar2 ) )
ports = 2;
llExit();
fdOpenSession( TaskSelf() );
printf("SCtrl: Ready, Ports=%d\n",ports);
while(1)
{
SemPend( hOurSem, 200 );
SemReset( hOurSem, 0 );
// See if we're still running
if( !fRunning )
{
llEnter();
llSerialClose(1);
if( ports == 2 )
llSerialClose(2);
llExit();
goto Exit;
}
//------------------------------
// Read character from buffer
// OutBuf = Start of characters to read
// OutCnt = Number of characters to read
//------------------------------
// This is where a modem state machine would normally
// be placed. In this demo, there is no modem, thus
// we'll zap the input by setting OutCnt back to zero
OutCnt = 0;
//
// At this point in the example, we assume we have just got a
// "CONNECT" message from the modem. Thus, if there is no active
// HDLC session, we create one.
//
if( !hHDLC )
{
// Send a string to the server to wake it up
_llSerialSend( 1, (UINT8 *)"CLIENT", 6 );
// Send a string to the server to wake it up
_llSerialSend( 1, (UINT8 *)"CLIENT", 6 );
TaskSleep(500);
// Create the PPP instance
hHDLC = hdlcNew( 1, PPPFLG_CLIENT | PPPFLG_OPT_USE_MSE |
PPPFLG_SIOPT_SENDCMAP | PPPFLG_SIOPT_RECVCMAP,
0, "username", "password");
}
// If we do have an HDLC connection, get the status
if( hHDLC )
{
status = hdlcGetStatus( hHDLC );
// If disconnected, print message and close
if( status >= SI_CSTATUS_DISCONNECT )
{
printf("SCtrl: Disconnected (%d)\n",status);
hdlcFree( hHDLC );
hHDLC = 0;
fConnectMsg = 0;
}
// Else if connected, print message and break
else if( status == SI_CSTATUS_CONNECTED && !fConnectMsg )
{
printf("SCtrl: Connected\n");
fConnectMsg = 1;
}
}
//
// The second part of this loop is to allow the 2nd serial
// port (if any) to access the console program that is norally
// supplied via Telnet.
//
// If idle, see if any data came in on serial port 2
if( sio==INVALID_SOCKET && OutCnt2 )
{
// There is data in the keyboard buffer. Try and spawn
// the console program
// We toss the key(s) that invoked the connection
OutCnt2 = 0;
// This is a local connection, so clear out SA
bzero( &sin1, sizeof(struct sockaddr_in) );
sin1.sin_family = AF_INET;
sin1.sin_len = sizeof( sin1 );
// Launch the console
sio = ConsoleOpen( &sin1 );
}
// Stuff to do when a console is running
if( sio != INVALID_SOCKET )
{
// We have characters to output...
do
{
cnt = recv(sio, ConBufferIn, sizeof(ConBufferIn), MSG_DONTWAIT);
if( cnt > 0 )
{
for( i=0,j=0; i<cnt; i++ )
{
if( ConBufferIn[i] == '\n' )
ConBufferOut[j++] = '\r';
ConBufferOut[j++] = ConBufferIn[i];
}
_llSerialSend( 2, (UINT8 *)ConBufferOut, j );
}
} while( cnt > 0 );
// On close socket or error, break out of loop
if( !cnt || (cnt<0 && fdError() != EWOULDBLOCK) )
goto consoleError;
// Check for STDIN data
if( OutCnt2 )
{
cnt = send( sio, OutBuf2, OutCnt2, 0 );
OutCnt2 = 0;
}
// On error, break out of loop
if( cnt < 0 && fdError() != EWOULDBLOCK )
{
consoleError:
fdClose(sio);
sio = INVALID_SOCKET;
}
}
}
Exit:
//
// General Cleanup
//
if( hHDLC )
hdlcFree( hHDLC );
if( hOurSem )
{
SemDelete( hOurSem );
hOurSem = 0;
}
if( sio != INVALID_SOCKET )
{
fdClose( sio );
ConsoleClose();
}
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 + -