ICMP & UDP implementation over PPP


#1

//
/
File : ping.h /
/
-------------------------------------------------------------------------
/
/
Object : Customer application header /
/
/
/
contents : ADL Ping GPRS Sample /
/
/
/

$
/
/
***/

#ifndef ADL_EXAMPLE_PING_GPRS
#define ADL_EXAMPLE_PING_GPRS

#define htons(A) ((((A) & 0xFF00) >> 8) | (((A) & 0x00FF) << 8))
#define htonl(A) ((((A) & 0xFF000000) >> 24) | (((A) & 0x00FF0000) >> 8) | (((A) & 0x0000FF00) << 8) | (((A) & 0x000000FF) << 24))

#define TCP 0x06
#define UDP 0x11
#define ICMP 0x01

#define ECHO 8
#define ECHO_REPLY 0
#define TRACEROUTE 30

// Flash objects ID
typedef enum
{
PING_FLHID_APN,
PING_FLHID_LOGIN,
PING_FLHID_PASSWORD,
PING_FLHID_IP,

	PING_FLHID_LAST

} ping_FlashIds_e;

typedef struct {
u8 Version_HLen;
u8 Service;
u16 TotalLength;
u16 ID;
u16 Frag;
u8 TTL;
u8 Protocol;
u16 Checksum;
u32 SourceAddress;
u32 DestAddress;
//u8 Payload [10];
} IPDatagram;

//u8 IPAddress [4];
typedef struct {
u8 Type;
u8 Code;
u16 Checksum;
u16 Identifier;
u16 SeqNumber;
u8 payload[2];
} ICMPDatagram;//8

typedef struct {
u16 src_portno; // Source port number
u16 dst_portno; // Destination port number
u16 udp_length; // UDP packet length
u16 udp_checksum; // UDP checksum (optional)
u8 payload[2];
} UDPDatagram;
/* prototypes */

// GPRS Command Handler
s8 GPRS_Handler(u16 Event,u8 Cid);

// FCM Comand Handler
/* FCM control events handler /
bool FCM_ControlHandler( u8 Event);
/
FCM data events handler */
bool FCM_DataHandler( u16 DataLen, u8 * Data);

// Ping Timer Handler
void Ping_TimerHandler ( u8 ID );

// Ping GPRS Start Up Command Handler
//void Cmd_PING_Handler(adl_atCmdPreParser_t *paras);
// Customer application initialisation
void adl_main ( adl_InitType_e InitType );

bool Wind4_Handler( adl_atUnsolicited_t * param );

void gprsTimer( u8 ID );

void Ping_UpdateGSETParameter ( u8 id, ascii ** ParamStr, bool bWrite );

u32 ConvertIP_atoi ( ascii * aIP );

ascii * ConvertIP_itoa ( u32 iIP, ascii * aIP );

u16 checksum(u16 *buffer, s32 size);

void Cmd_TCPIP_Handler(adl_atCmdPreParser_t *paras);

void invokeHandler(u8 ikey);
#endif

#include “adl_global.h”
#include “ping.h”

#define KEY_NONE 0xFF

/***********************************************************************/
/
Mandatory variables /
/
-------------------------------------------------------------------------
/
/
wm_apmCustomStack /
/
wm_apmCustomStackSize /
/
-------------------------------------------------------------------------
/
/***************************************************************************/
u32 wm_apmCustomStack [ 512 ];
const u16 wm_apmCustomStackSize = sizeof ( wm_apmCustomStack );

// Flash objects handle
static const ascii * PING_FLH_HANDLE = “WmPING”;
static const u8 PACKETSIZE = 30;
// Parameters of the GPRS account
adl_gprsSetupParams_t SetupParams;
adl_gprsInfosCid_t InfosCid;

s8 FCM_Handler = 0;
s8 fcmReady = 0;

static u32 HostAddr; //IP Host Addr from the PING response
static u8 Ping_Send, Ping_Rec, ping_Time = 10;
static u16 ping_PacketSize = 30, ping_LastPacketSize;
static adl_tmr_t* Ping_Timer;
static s32 ping_RequestMode;
static u32 ping_TotalSize = 400, ping_NbPackets;

u16 scasyip_send_udp(u8 *buff, u32 host, u16 hostport, u32 LocalAddr, u16 localport, u8 *msg){
u16 size, udpsize;
int iUdpChecksumSize;
IPDatagram ipPackage;
UDPDatagram udpPackage;
u8 * ptr;
//fill ip package
ipPackage.Version_HLen = 0x45;
ipPackage.Service = 0;
ipPackage.TotalLength = htons(PACKETSIZE);
ipPackage.ID = 0;
ipPackage.Frag = 0;
ipPackage.TTL = 64;
ipPackage.Protocol = UDP;
ipPackage.Checksum = 0;
ipPackage.SourceAddress = htonl(LocalAddr);
ipPackage.DestAddress = htonl(host);
ipPackage.Checksum = checksum ((u16 *)(&ipPackage), sizeof(ipPackage) );
//fill udp datagram
udpPackage.dst_portno = htons(hostport);
udpPackage.src_portno = htons(localport);
udpsize = sizeof(udpPackage);
udpPackage.udp_length = htons(udpsize);
udpPackage.udp_checksum = 0;
udpPackage.payload[0] = 1;
udpPackage.payload[1] = 2;
//Calculates the pseudo-header checksum of a UDP packet
ptr = buff;
wm_memset(buff, 0, PACKETSIZE);

wm_memcpy(ptr, &ipPackage.SourceAddress,  sizeof(ipPackage.SourceAddress));  
ptr += sizeof(ipPackage.SourceAddress);
iUdpChecksumSize += sizeof(ipPackage.SourceAddress);

wm_memcpy(ptr, &ipPackage.DestAddress, sizeof(ipPackage.DestAddress)); 
ptr += sizeof(ipPackage.DestAddress);
iUdpChecksumSize += sizeof(ipPackage.DestAddress);

ptr++;
iUdpChecksumSize += 1;

wm_memcpy(ptr, &ipPackage.Protocol, sizeof(ipPackage.Protocol)); 
ptr += sizeof(ipPackage.Protocol);
iUdpChecksumSize += sizeof(ipPackage.Protocol);

wm_memcpy(ptr, &udpPackage.udp_length, sizeof(udpPackage.udp_length)); 
ptr += sizeof(udpPackage.udp_length);
iUdpChecksumSize += sizeof(udpPackage.udp_length);

wm_memcpy(ptr, &udpPackage, sizeof(udpPackage)); 
ptr += sizeof(udpPackage);
iUdpChecksumSize += sizeof(udpPackage);

udpPackage.udp_checksum = checksum( (u16 *)buff, iUdpChecksumSize);

//udpPackage.udp_checksum = checksum( (u16 *)(&udpPackage), sizeof(udpPackage) );
wm_memset(buff, 0, PACKETSIZE);
wm_memcpy(buff, &ipPackage, sizeof(ipPackage) );
wm_memcpy(buff + sizeof(ipPackage), &udpPackage, sizeof(udpPackage) );

size = sizeof(ipPackage) + sizeof(udpPackage);
return size;

}
void scasyip_send_ping (u8 * buff, u32 host, u32 LocalAddr, u16 * size, u16 PayloadLength ){
IPDatagram ipPackage;
ICMPDatagram icmpPackage;
//fill ip package
ipPackage.Version_HLen = 0x45;
ipPackage.Service = 0;
ipPackage.TotalLength = htons(PACKETSIZE);
ipPackage.ID = 0;
ipPackage.Frag = 0;
ipPackage.TTL = 64;
ipPackage.Protocol = ICMP;
ipPackage.Checksum = 0;
ipPackage.SourceAddress = htonl(LocalAddr);
ipPackage.DestAddress = htonl(host);
ipPackage.Checksum = checksum ((u16 *)(&ipPackage), sizeof(ipPackage) );
//DUMP(1, &ipPackage, sizeof(ipPackage) );
//fill ICMP package
icmpPackage.Type = ECHO;
icmpPackage.Code = 0;
icmpPackage.Checksum = 0;
icmpPackage.Identifier = 0;
icmpPackage.SeqNumber = 0;
icmpPackage.payload[0] = 32;
icmpPackage.payload[1] = 33;
icmpPackage.Checksum = checksum ((u16 *)(&icmpPackage), sizeof(icmpPackage));

//DUMP(1, &icmpPackage, sizeof(icmpPackage) );
wm_memcpy(buff, &ipPackage, sizeof(ipPackage) );
wm_memcpy(buff + sizeof(ipPackage), &icmpPackage, sizeof(icmpPackage) );

//DUMP(1, buff, PACKETSIZE);
*size = sizeof(ipPackage)+sizeof(icmpPackage);
//return buff;

}
u8 scasyip_check_ping(u8 * buf, u16 DataLen){
IPDatagram *iphdr = NULL;
ICMPDatagram *icmphdr = NULL;
u16 iphdrlen;
u32 tick;
static u32 icmpcount = 0;
DUMP(1, buf, DataLen );
iphdr = (IPDatagram *)buf;
// Number of 32-bit words * 4 = bytes
iphdrlen = (iphdr->Version_HLen & 0x0F) * 4;

icmphdr = (ICMPDatagram *)(buf + iphdrlen);

if (icmphdr->Type != ECHO_REPLY) 
{
    TRACE((1, "nonecho type %d recvd\n", icmphdr->Type));
}

//printf("%d bytes from %s:", bytes, inet_ntoa(from->sin_addr));
TRACE((1, " icmp_seq = %d. ", icmphdr->Type));

//icmpcount++;
return icmphdr->Type;

}
/* Send a Ping packet on GPRS FCM flow */
void SendPingPacket ( void )
{
u16 size, PayloadSize;// = ping_PacketSize - ip_get_header_size();
u8 * PacketToSend;
s32 sFcmReturn;

PacketToSend = (u8 *)adl_memGet(PACKETSIZE);
wm_memset(PacketToSend, 0, PACKETSIZE);
scasyip_send_ping (PacketToSend, HostAddr, InfosCid.LocalIP, &size, PayloadSize ); 

TRACE((1, "package size:%d", size));
DUMP ( 1, PacketToSend, size );
/* Send packet */
sFcmReturn = adl_fcmSendData ( FCM_Handler, PacketToSend, size );
if ( sFcmReturn < OK )
{
    TRACE (( 1, "Problem to send the packet" ));    
    adl_atSendResponse ( ADL_AT_UNS, "\r\nCannot send the packet\r\n" );            
}
else
{
    
    if ( sFcmReturn == ADL_FCM_RET_OK_WAIT_RESUME )
    {
        // Wait for resume before going on...
		TRACE( (1, "Wait for resume before going on...") );
    }
    else
    {
        /* Start Timer */
        Ping_Timer = adl_tmrSubscribe ( FALSE, ping_Time, ADL_TMR_TYPE_100MS, Ping_TimerHandler );
    }
}

// Release buffer
adl_memRelease ( PacketToSend );

}

void sendUdpPackage( void ){
u16 size, PayloadSize;// = ping_PacketSize - ip_get_header_size();
u8 * PacketToSend;
s32 sFcmReturn;
ascii message[10];

PacketToSend = (u8 *)adl_memGet(PACKETSIZE);
wm_memset( PacketToSend, 0, PACKETSIZE );
size = scasyip_send_udp (PacketToSend, HostAddr, 2008, InfosCid.LocalIP, 57258, message ); 

TRACE((1, "package size:%d", size));
DUMP ( 1, PacketToSend, size );
/* Send packet */
sFcmReturn = adl_fcmSendData ( FCM_Handler, PacketToSend, size );
//Ping_Send++;
if ( sFcmReturn < OK )
{
    TRACE (( 1, "Problem to send udp packet" ));    
    adl_atSendResponse ( ADL_AT_UNS, "\r\nCannot send udp packet\r\n" );            
}
else
{
    /* OK */
    //ascii Rsp [ 40 ];
    TRACE (( 1, "udp Sent"));
    
    if ( sFcmReturn == ADL_FCM_RET_OK_WAIT_RESUME )
    {
        // Wait for resume before going on...
		TRACE( (1, "Wait for resume before going on...") );
    }
    
}

// Release buffer
adl_memRelease ( PacketToSend );

}

/*******************************************************************/
/
Function : Ping_TimerHandler /
/
-------------------------------------------------------------------------
/
/
Object : Timer handler /
/
/
/
-------------------------------------------------------------------------
/
/
Variable Name |IN |OUT|GLB| Utilisation /
/
--------------------±–±--±–±---------------------------------------
/
/
ID | | | | Timer ID /
/
--------------------±–±--±–±---------------------------------------
/
/**************************************************************************/
void Ping_TimerHandler ( u8 ID )
{
ascii NewText [ 50 ];
TRACE (( 1, “Embedded : Send next packet” ));
fcmReady = 1;
Ping_Send++;
/
The End */
wm_sprintf ( NewText, “\r\nResult : Ping Sent %d , Resp Received %d\r\n”, Ping_Send, Ping_Rec );
adl_atSendResponse ( ADL_AT_UNS, NewText );

}

bool Wind4_Handler( adl_atUnsolicited_t *param ){
adl_atUnSoUnSubscribe("+WIND: 4", Wind4_Handler);
adl_tmrSubscribe( FALSE, 1, ADL_TMR_TYPE_100MS, gprsTimer );
return TRUE;
}

bool keyHandler(adl_atUnsolicited_t *param){
u8 ikey = KEY_NONE, ipressed = 0;
ascii lBuffer[ 3 ];
s8 returnCode;
if(param){
ikey = wm_atoi( wm_strGetParameterString(lBuffer, param->StrData, 1) );
ipressed = wm_atoi( wm_strGetParameterString(lBuffer, param->StrData, 2) );
TRACE((1, “key %d is %d”, ikey, ipressed));
//20 -map 21 -up 22 -ok 23 -down
if(ikey==20 && ipressed==1){//map
wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
//wm_sndDtmfPlay(WM_SND_DEST_BUZZER, ‘A’, 0, 10);
//wm_sndMelodyPlay(WM_SND_DEST_BUZZER, myMelody, 6, 0, 9);

		wm_strcpy(SetupParams.APN, "CMNET");
		
		returnCode = adl_gprsSetup ( 1, SetupParams );
		
		TRACE (( 1, "GPRS Setup return : %d", returnCode ));
	}else if(ikey==21 && ipressed==1 && fcmReady == 1){//up
		//wm_sndToneStop(WM_SND_DEST_BUZZER);
		//wm_sndDtmfStop(WM_SND_DEST_BUZZER);
		wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
		fcmReady = 0;
		SendPingPacket();
		//stopGPS();
	}else if(ikey==22 && ipressed==1){//ok
	  
		wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
		// Stop the application
		Ping_Send = 0;
		Ping_Rec = 0;
		fcmReady = 0;
		adl_gprsDeact ( 1 );
		adl_fcmUnsubscribe(FCM_Handler);
	}else if(ikey==23 && ipressed==1 && fcmReady == 1){//down
		//udpTimer();
		//sendUdpPackage();
		wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
		sendUdpPackage();
	 
	}
}
return FALSE;

}
void invokeHandler(u8 ikey){
s8 returnCode;
if(ikey==0){//map
wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
//wm_sndDtmfPlay(WM_SND_DEST_BUZZER, ‘A’, 0, 10);
//wm_sndMelodyPlay(WM_SND_DEST_BUZZER, myMelody, 6, 0, 9);

	wm_strcpy(SetupParams.APN, "CMNET");
	
	returnCode = adl_gprsSetup ( 1, SetupParams );
	
	TRACE (( 1, "GPRS Setup return : %d", returnCode ));
}else if(ikey==1 && fcmReady == 1){//up
	//wm_sndToneStop(WM_SND_DEST_BUZZER);
	//wm_sndDtmfStop(WM_SND_DEST_BUZZER);
	wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
	fcmReady = 0;
	SendPingPacket();
	//stopGPS();
}else if(ikey==2){//ok
	
	wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
	// Stop the application
	Ping_Send = 0;
	Ping_Rec = 0;
	fcmReady = 0;
	adl_gprsDeact ( 1 );
	adl_fcmUnsubscribe(FCM_Handler);
}else if(ikey==3 && fcmReady == 1){//down
	//udpTimer();
	//sendUdpPackage();
	wm_sndTonePlay(WM_SND_DEST_BUZZER, 100, 10, 9);
	sendUdpPackage();
	
}

}
/*******************************************************************/
/
Function : adl_main /
/
-------------------------------------------------------------------------
/
/
Object : Customer application initialisation /
/
/
/
-------------------------------------------------------------------------
/
/
Variable Name |IN |OUT|GLB| Utilisation /
/
--------------------±–±--±–±---------------------------------------
/
/
InitType | | | | Application start mode reason /
/
--------------------±–±--±–±---------------------------------------
/
/***************************************************************************/
void adl_main ( adl_InitType_e InitType )
{
TRACE (( 1, “Embedded : Ping Appli Init” ));

adl_atCmdCreate("at+cmer=,1",FALSE, (adl_atRspHandler_t) NULL, NULL);
adl_atUnSoSubscribe("+CKEV: ", keyHandler);

adl_atUnSoSubscribe("+WIND: 4", (adl_atUnSoHandler_t)Wind4_Handler);
adl_atCmdSubscribe("at+cmd", Cmd_TCPIP_Handler, ADL_CMD_TYPE_TEST
												| ADL_CMD_TYPE_PARA
												| ADL_CMD_TYPE_READ
												| 0x0051 );

}

void gprsTimer(u8 ID){
if ( adl_gprsSubscribe ( GPRS_Handler ) == OK )
{
adl_atSendResponse ( ADL_AT_UNS, “\r\nPing Appli is started\r\n” );
TRACE (( 1, “Embedded : Ping Appli is started” ));

    HostAddr = ConvertIP_atoi("221.218.192.133");
    
    // Subscribe to the GPRS settings flash object
    adl_flhSubscribe ( PING_FLH_HANDLE, PING_FLHID_LAST );
    
    // Read settings from flash (if possible)
    Ping_UpdateGSETParameter ( PING_FLHID_APN, &SetupParams.APN, FALSE );
    Ping_UpdateGSETParameter ( PING_FLHID_LOGIN, &SetupParams.Login, FALSE );
    Ping_UpdateGSETParameter ( PING_FLHID_PASSWORD, &SetupParams.Password, FALSE );
    Ping_UpdateGSETParameter ( PING_FLHID_IP, &SetupParams.FixedIP, FALSE );

}
else
{
    adl_atSendResponse ( ADL_AT_UNS, "\r\nPing Appli init error\r\n" );
    TRACE (( 1, "Embedded : Ping Appli init error" ));
} 		

}

/*******************************************************************/
/
Function : PING_Handler /
/
-------------------------------------------------------------------------
/
/
Objet : GPRS Service Events Handler /
/
/
/
Return : sReturn to or not to forward the Event /
/
/
/
-------------------------------------------------------------------------
/
/
Variable Name |IN |OUT|GLB| Utilisation /
/
--------------------±–±--±–±---------------------------------------
/
/
Event | | | | Event from GPRS Service /
/
--------------------±–±--±–±---------------------------------------
/
/***************************************************************************/
s8 GPRS_Handler ( u16 Event,u8 Cid )
{
s8 sReturn;
ascii buf [ 200 ];
sReturn = ADL_GPRS_NO_FORWARD; // by default - no forward events to ext app

TRACE (( 1, "Embedded : Event receive %d", Event ));        
switch ( Event )
{
    case ADL_GPRS_EVENT_RING_GPRS :
        TRACE (( 1, "ADL_GPRS_EVENT_RING_GPRS" ));
    break;
    
    case ADL_GPRS_EVENT_NW_CONTEXT_DEACT :
        TRACE (( 1, "ADL_GPRS_EVENT_NW_CONTEXT_DEACT" ));
    break;
    
    case ADL_GPRS_EVENT_ME_CONTEXT_DEACT:
        TRACE (( 1, "ADL_GPRS_EVENT_ME_CONTEXT_DEACT" ));
    break;
    
    case ADL_GPRS_EVENT_NW_DETACH :
        TRACE (( 1, "ADL_GPRS_EVENT_NW_DETACH" ));
    break;
    
    case ADL_GPRS_EVENT_ME_DETACH :
        TRACE (( 1, "ADL_GPRS_EVENT_ME_DETACH" ));
    break;
    
    case ADL_GPRS_EVENT_NW_CLASS_B :
        TRACE (( 1, "ADL_GPRS_EVENT_NW_CLASS_B" ));
    break;
    
    case ADL_GPRS_EVENT_NW_CLASS_CG :
        TRACE (( 1, "ADL_GPRS_EVENT_NW_CLASS_CG" ));
    break;
    
    case ADL_GPRS_EVENT_NW_CLASS_CC :
        TRACE (( 1, "ADL_GPRS_EVENT_NW_CLASS_CC" ));
    break;
    
    case ADL_GPRS_EVENT_ME_CLASS_B :
        TRACE (( 1, "ADL_GPRS_EVENT_ME_CLASS_B" ));
    break;
    
    case ADL_GPRS_EVENT_ME_CLASS_CG :
        TRACE (( 1, "ADL_GPRS_EVENT_ME_CLASS_CG" ));
    break;
    
    case ADL_GPRS_EVENT_ME_CLASS_CC :
        TRACE (( 1, "ADL_GPRS_EVENT_ME_CLASS_CC" ));
    break;
    
    case ADL_GPRS_EVENT_NO_CARRIER :
        TRACE (( 1, "ADL_GPRS_EVENT_NO_CARRIER" ));
    break;
    
    case ADL_GPRS_EVENT_DEACTIVATE_OK :
        TRACE (( 1, "ADL_GPRS_EVENT_DEACTIVATE_OK" ));
        sReturn = ADL_GPRS_FORWARD;
    break;
    
    case ADL_GPRS_EVENT_DEACTIVATE_OK_FROM_EXT :
        TRACE (( 1, "ADL_GPRS_EVENT_DEACTIVATE_OK_FROM_EXT" ));
        sReturn = ADL_GPRS_FORWARD; 
    break;
    
    case ADL_GPRS_EVENT_ANSWER_OK :
        TRACE (( 1, "ADL_GPRS_EVENT_ANSWER_OK" ));
    break;
    
    case ADL_GPRS_EVENT_ANSWER_OK_FROM_EXT :
        TRACE (( 1, "ADL_GPRS_EVENT_ANSWER_OK_FROM_EXT" ));
        sReturn = ADL_GPRS_FORWARD; 
    break;
    
    case ADL_GPRS_EVENT_ACTIVATE_OK :
    {
        ascii localIP[ 20 ], DNS1 [ 20 ], DNS2 [ 20 ], Gateway [ 20 ];
        TRACE (( 1, "Embedded : PDP Ctxt %d Activation OK",Cid ));

        adl_gprsGetCidInformations ( 1, &InfosCid );

        TRACE (( 1, "Embedded  : IPCP Information DNS1 %u, DNS2 %u, Gateway %u", InfosCid.DNS1, InfosCid.DNS2, InfosCid.Gateway ));
        wm_sprintf ( buf, "\r\nIPCP Information :\r\nLocal IP %s\r\nDNS1 %s\r\nDNS2 %s\r\nGateway %s\r\n",
                          ConvertIP_itoa ( InfosCid.LocalIP, localIP ),
                          ConvertIP_itoa ( InfosCid.DNS1, DNS1 ),
                          ConvertIP_itoa ( InfosCid.DNS2, DNS2 ),
                          ConvertIP_itoa ( InfosCid.Gateway, Gateway ) );
        adl_atSendResponse ( ADL_AT_UNS, buf );

        /* Open FCM Flows */
        FCM_Handler = adl_fcmSubscribe ( ADL_FCM_FLOW_GPRS,
                                         FCM_ControlHandler,
                                         FCM_DataHandler );
		fcmReady = 1;
    }
    break;

    case ADL_GPRS_EVENT_GPRS_DIAL_OK_FROM_EXT :
        TRACE (( 1, "ADL_GPRS_EVENT_GPRS_DIAL_OK_FROM_EXT" ));
        sReturn = ADL_GPRS_FORWARD;
    break;
    
    case ADL_GPRS_EVENT_ACTIVATE_OK_FROM_EXT :
        TRACE (( 1, "ADL_GPRS_EVENT_ACTIVATE_OK_FROM_EXT" ));
        adl_atSendResponse ( ADL_AT_UNS, "\r\nADL_GPRS_EVENT_ACTIVATE_OK_FROM_EXT\r\n" );
        sReturn = ADL_GPRS_FORWARD;
    break;
    
    case ADL_GPRS_EVENT_HANGUP_OK_FROM_EXT :
        TRACE (( 1, "ADL_GPRS_EVENT_HANGUP_OK_FROM_EXT" ));
        sReturn = ADL_GPRS_FORWARD; 
    break;
    
    case ADL_GPRS_EVENT_DEACTIVATE_KO :
        TRACE (( 1, "ADL_GPRS_EVENT_DEACTIVATE_KO" ));
    break;
    
    case ADL_GPRS_EVENT_DEACTIVATE_KO_FROM_EXT :
        TRACE (( 1, "ADL_GPRS_EVENT_DEACTIVATE_KO_FROM_EXT" ));
        sReturn = ADL_GPRS_FORWARD;
    break;
    
    case ADL_GPRS_EVENT_ACTIVATE_KO_FROM_EXT :
        TRACE (( 1, "ADL_GPRS_EVENT_ACTIVATE_KO_FROM_EXT" ));
        sReturn = ADL_GPRS_FORWARD;
    break;
    
    case ADL_GPRS_EVENT_ACTIVATE_KO :
        TRACE (( 1, "ADL_GPRS_EVENT_ACTIVATE_KO" ));
    break;
    
    case ADL_GPRS_EVENT_ANSWER_OK_AUTO :
        TRACE (( 1, "ADL_GPRS_EVENT_ANSWER_OK_AUTO" ));
    break;

    case ADL_GPRS_EVENT_SETUP_OK :
        TRACE (( 1, "PDP Ctxt Cid %d Setup is OK",Cid));

        /* Activate context */
        adl_gprsAct ( 1 );
    break;
    
    case ADL_GPRS_EVENT_SETUP_KO :
        TRACE (( 1, "ADL_GPRS_EVENT_SETUP_KO" ));    
    break;
    
    case ADL_GPRS_EVENT_ME_ATTACH :
        TRACE (( 1, "ADL_GPRS_EVENT_ME_ATTACH" ));
    break;
    
    case ADL_GPRS_EVENT_ME_UNREG :
        TRACE (( 1, "ADL_GPRS_EVENT_ME_UNREG" ));
		adl_atCmdCreate("AT+CGATT=1", FALSE, (adl_atRspHandler_t) NULL, NULL);
    break;
    
    case ADL_GPRS_EVENT_ME_UNREG_SEARCHING :
        TRACE (( 1, "ADL_GPRS_EVENT_ME_UNREG_SEARCHING" ));    
    break;
    
    default :
        TRACE (( 1, "Embedded : GPRS_Handler Event not treated"));
    break;
}

return sReturn;

}

/*******************************************************************/
/
Function : FCM_ControlHandler /
/
-------------------------------------------------------------------------
/
/
Objet : FCM Control Events Handler /
/
/
/
Return : return value is not relevant /
/
/
/
-------------------------------------------------------------------------
/
/
Variable Name |IN |OUT|GLB| Utilisation /
/
--------------------±–±--±–±---------------------------------------
/
/
Event | | | | Event from GPRS Service /
/
--------------------±–±--±–±---------------------------------------
/
/***************************************************************************/
bool FCM_ControlHandler( u8 Event)
{
switch ( Event )
{
case ADL_FCM_RET_ERROR_GSM_GPRS_ALREADY_OPENNED :
TRACE (( 1, “Flow GSM already Opened”));
break;

    case ADL_RET_ERR_PARAM :
        TRACE (( 1, "Open Flow GPRS Parameters Error"));
    break;
    
    case ADL_RET_ERR_ALREADY_SUBSCRIBED :
        TRACE (( 1, "Flow GPRS already subscribed"));
    break;
    
    case ADL_FCM_EVENT_FLOW_OPENNED :
        TRACE (( 1, "ADL_FCM_EVENT_FLOW_OPENNED" ));
        
        // Check the request mode
        if ( ping_RequestMode == 1 )
        {
            /* Sending the First Ping */
            SendPingPacket();
        }
    break;
    
    case ADL_FCM_EVENT_FLOW_CLOSED :
        TRACE (( 1, "ADL_FCM_EVENT_FLOW_CLOSED" ));
    break;
    
    case ADL_FCM_EVENT_RESUME :
        TRACE (( 1, "ADL_FCM_EVENT_RESUME" ));
        
        /* Start Timer */
        Ping_Timer = adl_tmrSubscribe ( FALSE, ping_Time, ADL_TMR_TYPE_100MS, Ping_TimerHandler );
    break;
    
    case ADL_FCM_EVENT_MEM_RELEASE :
        TRACE (( 1, "ADL_FCM_EVENT_MEM_RELEASE" ));
    break;
    
    default :
        TRACE (( 1, "Embedded : FCM_ControlHandler Event not processed" ));
    break;
}

return TRUE;

}

/*******************************************************************/
/
Function : FCM_DataHandler /
/
-------------------------------------------------------------------------
/
/
Objet : FCM Data Events Handler /
/
/
/
Return : return TRUE to release the credit, or FALSE if the credit /
/
must not be released /
/
/
/
-------------------------------------------------------------------------
/
/
Variable Name |IN |OUT|GLB| Utilisation /
/
--------------------±–±--±–±---------------------------------------
/
/
Event | | | | Event from GPRS Service /
/
--------------------±–±--±–±---------------------------------------
/
/***************************************************************************/
bool FCM_DataHandler ( u16 DataLen, u8 * Data )
{
u8 result;

TRACE (( 1, "\r\n<- Receive FCM Data Blocks \r\n"));
//DUMP ( 1, Data, DataLen );

//Check the Ping response	
result = scasyip_check_ping ( Data, DataLen );

if ( result == 0 )
{
    adl_atSendResponse ( ADL_AT_UNS,"\r\nPing Rsp OK\r\n");
}
else if ( result == 8 )
{
    adl_atSendResponse ( ADL_AT_UNS,"\r\nPing Rsp KO : bad chksum\r\n");
}
else if ( result == 7 )
{
    adl_atSendResponse ( ADL_AT_UNS,"\r\nEcho Reply OK\r\n");
}

// If we have really received an IP Packet
if ( result == 0 || result == 8 || result == 7 )
{
    // Count one more received packet
    Ping_Rec++;
}

return TRUE;

}

/* Convert an IP address from u32 to ascii * */
ascii * ConvertIP_itoa ( u32 iIP, ascii * aIP )
{
wm_sprintf ( aIP, “%d.%d.%d.%d”,
( iIP >> 24 ) & 0xFF,
( iIP >> 16 ) & 0xFF,
( iIP >> 8 ) & 0xFF,
iIP & 0xFF );
return aIP;
}

/* Convert an IP address from ascii * to u32 */
u32 ConvertIP_atoi ( ascii * aIP )
{
ascii pTmp[4];
u8 i,j,k;
u8 pDest[4];
u32 native;

/* Convert Ascii PDP addr to Integer PDP addr */
i=0;
j=0;
k=0;

/* Octet 1 a N of the IP address */
while(aIP[i] != 0)
{
	while(aIP[i] != 0x2E && aIP[i] != 0)
	{     
		pTmp[j] = aIP[i];
		j++;
		i++;
	}
	pTmp[j] = 0;
	pDest[k]=(u8)wm_atoi((void*)pTmp);
	k++;
	if(aIP[i] == 0x2E)
	{
		i++;
	}
	j=0;
}

/* Convert into u32 */
native = 0;

native =  pDest[0] << 24;
native |= pDest[1] << 16;
native |= pDest[2] << 8;
native |= pDest[3];

return native;

}

u16 checksum(u16 *buffer, s32 size)
{
unsigned long cksum=0;

while (size > 1)
{
    cksum += *buffer++;
    size  -= sizeof(u16);   
}
if (size)
{
    cksum += *(u8*)buffer;   
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16); 

return (u16)(~cksum); 

}

// GPRS parameters flash update
void Ping_UpdateGSETParameter ( u8 id, ascii ** ParamStr, bool bWrite )
{
// Check for mode
if ( bWrite )
{
// Check for provided string
if ( *ParamStr != NULL )
{
// Directly Write parameter
adl_flhWrite ( PING_FLH_HANDLE, id, wm_strlen ( *ParamStr ) + 1, *ParamStr );
}
else
{
// Erase parameter (if any)
adl_flhErase ( PING_FLH_HANDLE, id );
}
}
else
{
// Read parameter
s32 Length = adl_flhExist ( PING_FLH_HANDLE, id );

    // Check if the object exists
    if ( Length >= 0 )
    {
        // Check if the provided string was allocated
        if ( *ParamStr != NULL )
        {
            // Release the previous string
            adl_memRelease ( *ParamStr );
        }
        
        // Allocate memory for the parameter
        *ParamStr = adl_memGet ( Length );
        
        // Read from flash
        adl_flhRead ( PING_FLH_HANDLE, id, Length, *ParamStr );
    }
}

}
void Cmd_TCPIP_Handler(adl_atCmdPreParser_t *paras)
{
switch(paras->Type)
{
case ADL_CMD_TYPE_TEST:
break;
case ADL_CMD_TYPE_PARA:
{ u8 operation = ( u8 ) wm_atoi ( ADL_GET_PARAM ( paras, 0 ) );
invokeHandler(operation);
break;
}
}
}


#2
  1. set gprs connection
    at+cmd=0
  2. send ping package
    at+cmd=1
  3. send udp package
    at+cmd=3
  4. deactive gprs connecton
    at+cmd=2

#3

append, you need set your own apn, username, password