UDPversion

Dependencies:   XBee mbed NetServicesMin

Files at this revision

API Documentation at this revision

Comitter:
recotana
Date:
Sat Apr 14 03:53:45 2012 +0000
Child:
1:3a46d2725374
Commit message:
UDPversion

Changed in this revision

NetServicesMin.lib Show annotated file Show diff for this revision Revisions of this file
XBee.lib Show annotated file Show diff for this revision Revisions of this file
common/data_models.cpp Show annotated file Show diff for this revision Revisions of this file
common/data_models.h Show annotated file Show diff for this revision Revisions of this file
common/disp_led/leds.cpp Show annotated file Show diff for this revision Revisions of this file
common/disp_led/leds.h Show annotated file Show diff for this revision Revisions of this file
common/types.h Show annotated file Show diff for this revision Revisions of this file
config.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
tcpip/net_routing.cpp Show annotated file Show diff for this revision Revisions of this file
tcpip/net_routing.h Show annotated file Show diff for this revision Revisions of this file
tcpip/tcp_client.cpp Show annotated file Show diff for this revision Revisions of this file
tcpip/tcp_client.h Show annotated file Show diff for this revision Revisions of this file
tcpip/tcp_error.cpp Show annotated file Show diff for this revision Revisions of this file
tcpip/tcp_error.h Show annotated file Show diff for this revision Revisions of this file
tcpip/tcp_server.cpp Show annotated file Show diff for this revision Revisions of this file
tcpip/tcp_server.h Show annotated file Show diff for this revision Revisions of this file
xbee/xbee_lib.cpp Show annotated file Show diff for this revision Revisions of this file
xbee/xbee_lib.h Show annotated file Show diff for this revision Revisions of this file
xbee/xbee_rec_route.cpp Show annotated file Show diff for this revision Revisions of this file
xbee/xbee_rec_route.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/NetServicesMin.lib	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/hlipka/code/NetServicesMin/#8b387bed54c2
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/XBee.lib	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/okini3939/code/XBee/#6efb3541af61
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/data_models.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,178 @@
+#include "types.h"
+#include "data_models.h"
+#include "EthernetNetIf.h"
+
+extern Serial pc;
+void initPayload(Payload *_payload)
+{
+    _payload->size=sizeof(Payload);
+    _payload->dummy = 0;
+    _payload->ftype = Ftype_Type_B;
+    
+    Frame *frame=&_payload->frame;
+    
+    for(int i=0 ; i < sizeof(frame->id.personalId) ; i++) frame->id.personalId[i]=0;
+    
+    frame->message.frameNumber=0xFFFF;
+    frame->message.device=SERVER;
+    frame->message.command=PING;
+    
+    for(int i=0 ; i < sizeof(_payload->frame.message.data) ; i++) _payload->frame.message.data[i]=0;
+}
+
+void payloadToNetwork(Payload *_payload)
+{
+    Frame *frame=&_payload->frame;
+    _payload->size               = htons(_payload->size);
+//    frame->id.head               = htons(frame->id.head);
+    frame->message.totalSize     = htons(frame->message.totalSize);
+    frame->message.frameNumber   = htons(frame->message.frameNumber);
+    frame->message.sum           = htons(frame->message.sum);
+    
+    if(frame->message.command == PING) {
+    
+    if(frame->message.device == SERVER)
+    {
+    
+        PrefServer *data;
+        data =  (PrefServer*)frame->message.data;
+        data->serverPort = htons(data->serverPort);
+        data->destinationPort = htons(data->destinationPort);
+        data->xbeeAdress = htonl(data->xbeeAdress);
+
+    }
+    else if(frame->message.device == LED_SENDER)
+    {
+        PrefSender *data;
+        data =  (PrefSender*)frame->message.data;
+        data->xbeeAdress = htonl(data->xbeeAdress);
+        data->currentFrameNumber = htons(data->currentFrameNumber);
+    }
+    else if(frame->message.device == LED_RECIEVER)
+    {
+        PrefReciever *data;
+        data =  (PrefReciever*)frame->message.data;
+        data->xbeeAdress = htonl(data->xbeeAdress);
+    }
+    }
+      
+}
+void networkToPayload(Payload *_payload)
+{
+    Frame *frame=&_payload->frame;
+    _payload->size               = ntohs(_payload->size);
+//    frame->id.head               = ntohs(frame->id.head);
+    frame->message.totalSize     = ntohs(frame->message.totalSize);
+    frame->message.frameNumber   = ntohs(frame->message.frameNumber);
+    frame->message.sum           = ntohs(frame->message.sum); 
+    
+    if(frame->message.command == PING){
+    
+    if(frame->message.device == SERVER)
+    {      
+        PrefServer *data;
+        data =  (PrefServer*)frame->message.data;
+        data->serverPort = ntohs(data->serverPort);
+        data->destinationPort = ntohs(data->destinationPort);
+        data->xbeeAdress = ntohl(data->xbeeAdress);
+    }
+    else if(frame->message.device == LED_SENDER)
+    {
+        PrefSender *data;
+        data =  (PrefSender*)frame->message.data;
+        data->xbeeAdress = ntohl(data->xbeeAdress);
+        data->currentFrameNumber = ntohs(data->currentFrameNumber);
+    }
+    else if(frame->message.device == LED_RECIEVER)
+    {
+        PrefReciever *data;
+        data =  (PrefReciever*)frame->message.data;
+        data->xbeeAdress = ntohl(data->xbeeAdress);
+    }
+    }
+}
+
+
+
+void initTestPayloadData(Payload *_payload)
+{
+   Frame *frame=&_payload->frame;
+   
+   initPayload(_payload);
+   _payload->size=sizeof(Payload);
+   
+//   frame->id.head=0x0000;
+//   uint8_t tmpid[14]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44};
+   uint8_t tmpid[16]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44,0x45,0x46};
+   for(int i=0;i<sizeof(tmpid);i++) frame->id.personalId[i]=tmpid[i];
+   
+   
+   frame->message.frameNumber=1;
+   frame->message.device=LED_RECIEVER;
+   frame->message.command=RECIEVE;
+   
+   uint8_t tmpdata[40]={
+    1,2,3,4,5,6,7,8,9,10,
+    11,12,13,14,15,16,17,18,19,20,
+    21,22,23,24,25,26,27,28,29,30,
+    31,32,33,34,35,36,37,38,39,40};
+   for(int i=0;i<sizeof(tmpdata);i++) frame->message.data[i]=tmpdata[i];
+   
+   frame->message.totalSize=
+     sizeof(frame->message.totalSize)
+    +sizeof(frame->message.frameNumber)
+    +sizeof(frame->message.device)
+    +sizeof(frame->message.command)
+    +sizeof(frame->message.data);
+    
+    uint16_t sum = culcSum(_payload);
+    
+    frame->message.sum = sum;
+
+}
+
+uint16_t culcSum(Payload *_payload){
+
+    uint16_t tmpSum = _payload->ftype;
+    uint8_t *data = (uint8_t *)&_payload->frame;
+    
+    for(uint8_t i=0 ; i<sizeof(Frame)-2 ; i++){
+        tmpSum += data[i];
+    }
+    return tmpSum;
+}
+
+void dumpPayload(Payload *_payload)
+{
+    Frame *frame=&_payload->frame;
+    pc.printf("\r\n======== dump!!\r\n");
+    pc.printf("payload\nsize:%04X\r\n ",_payload->size);
+    pc.printf("Type:%0X\r\n ",_payload->ftype);
+    
+//    pc.printf("Payload id_head:%04X\r\n personal id:",_payload->frame.id.head);
+    pc.printf("personal id:");  
+    for(int i=0 ; i < sizeof(frame->id.personalId) ; i++){
+        pc.printf("%02X:%c ",frame->id.personalId[i],frame->id.personalId[i]);
+    }
+    
+    pc.printf("\r\n");
+    
+    pc.printf("total size:%d , frame number:%d , divece:%0X , command:%02X , sum:%04X\r\n"
+        ,frame->message.totalSize
+        ,frame->message.frameNumber
+        ,frame->message.device
+        ,frame->message.command
+        ,frame->message.sum);
+    
+    pc.printf("message data:\r\n");
+    for(int i=0 ; i < sizeof(frame->message.data) ; i++){
+         pc.printf("%d:%02X ",i,_payload->frame.message.data[i]);
+    }
+    
+    pc.printf("\r\n--- RAW DATA -------\r\n");
+    for(int i=0 ; i < sizeof(Payload) ; i++){
+         pc.printf("%d:%02X ",i,((uint8_t*)_payload)[i]);
+    }
+    pc.printf("\r\n");
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/data_models.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,113 @@
+#ifndef _DATA_MODELS_
+#define _DATA_MODELS_
+
+#include "types.h"
+
+//frame type 
+typedef enum{
+    Ftype_NoID  =0,
+    Ftype_Type_A=1,
+    Ftype_Type_B=4
+} Ftype;
+
+typedef enum{
+    SERVER,
+    LED_SENDER,
+    LED_RECIEVER
+} Device;
+
+typedef enum{
+    PING,
+    ACK,
+    RECIEVE,
+    SEND_SINGLE,
+    SEND_CONTINUEOUS,
+    SEND_CONT_STOP,
+    SET_TX_GAIN,
+    SET_LIGHT_CONTROL
+} Command;
+//
+//for TCP/IP data structure
+//
+
+//message data  48byte(size fix) 
+typedef struct{
+    uint16_t totalSize;   //2(total size) + 2(frame number) + 1(device) + 1(command) + 40(data:max 40) <--- max 46
+    uint16_t frameNumber;
+    Device   device;
+    Command  command;
+    uint8_t  data[40]; //0 fill,size fix
+    uint16_t sum;
+} Message;
+
+//payload id  16byte 
+typedef struct{
+//    uint16_t head;              //reserve 0x0000
+    uint8_t  personalId[16]; 
+} ID;
+
+typedef struct{
+    ID      id;         // 16 byte
+    Message message;    // 48 byte
+} Frame;
+
+//66byte Payload
+typedef struct{
+    uint16_t    size;     // 2(size) + 1(dummy) + 1(Ftype) + 64(frame) = 68 byte
+    uint8_t     dummy;    //adjust Alignment
+    Ftype       ftype;  
+    Frame       frame;    //64byte
+} Payload;
+
+
+
+
+/* ---------------------------------------------------
+    message data structure
+---------------------------------------------------*/
+
+/*  light&#65533;@illuminate level  */
+typedef struct{
+    uint8_t gain;           //1~10
+    uint8_t middleLevel;    //1~10
+} TxGainData;
+
+typedef struct{
+    uint16_t currentFrameNumber;
+    TxGainData gainData;
+    uint8_t lightsw;        //0:light off 1:light on
+    uint8_t continueusMode; //0:single 1:continueus
+    uint8_t isSend;
+    uint8_t dummy;
+    uint32_t xbeeAdress;   
+} PrefSender;
+
+typedef struct{
+    uint32_t xbeeAdress;
+} PrefReciever;
+
+typedef struct{
+    uint32_t xbeeAdress;
+    
+    uint8_t serverAddress[4];
+    uint8_t subnetMask[4];
+    uint8_t routerAddress[4];
+    uint16_t serverPort;
+    
+    uint8_t destinationAddress[4];
+    uint16_t destinationPort;   
+} PrefServer;
+
+
+
+void initPayload(Payload *_payload);
+
+void payloadToNetwork(Payload *_payload);
+void networkToPayload(Payload *_payload);
+
+void initTestPayloadData(Payload *_payload);
+uint16_t culcSum(Payload *_payload);
+
+void dumpPayload(Payload *_payload);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/disp_led/leds.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,114 @@
+#include "mbed.h"
+#include "leds.h"
+
+#define kLedOnTime 0.05
+
+#define kTcpConnect p30
+#define kTcpRx p29
+#define kTcpTx p28
+
+#define kXBeeRx  p27
+#define kXBeeTx  p26
+
+Timeout led_tcp_rx;
+Timeout led_tcp_tx;
+Timeout led_xbee_snd;
+Timeout led_xbee_rec;
+
+DigitalOut ledTcpCon(kTcpConnect);
+DigitalOut ledTcpRec(kTcpRx);
+DigitalOut ledTcpSend(kTcpTx);
+
+DigitalOut ledXBeeRec(kXBeeRx);
+DigitalOut ledXBeeSend(kXBeeTx);
+
+
+void offTcpRx(void){
+    ledTcpRec=0;
+}
+void offTcpTx(void){
+    ledTcpSend=0;
+}
+void offXBeeRx(void){
+    ledXBeeRec=0;
+}
+void offXBeeTx(void){
+    ledXBeeSend=0;
+}
+
+void ledTcpRx(void)
+{
+    if(ledTcpRec == 1) return;
+    ledTcpRec=1;
+    led_tcp_rx.attach(&offTcpRx,kLedOnTime);
+}
+void ledTcpTx(void)
+{
+    if(ledTcpSend == 1) return;
+    ledTcpSend=1;
+    led_tcp_tx.attach(&offTcpTx,kLedOnTime);
+}
+void ledXBeeRx(void)
+{
+    if(ledXBeeRec == 1) return;
+    ledXBeeRec=1;
+    led_xbee_rec.attach(&offXBeeRx,kLedOnTime);
+}
+void ledXBeeTx(void)
+{
+    if(ledXBeeSend == 1) return;
+    ledXBeeSend=1;
+    led_xbee_snd.attach(&offXBeeTx,kLedOnTime);
+}
+
+
+void ledTcpConnect(bool isConnect)
+{
+    if(isConnect)   ledTcpCon=1;
+    else            ledTcpCon=0;
+}
+
+
+void startLed(void){
+    ledTcpCon=1;
+    wait(0.2);
+    ledTcpCon=0;
+    
+    ledTcpRec=1;
+    wait(0.2);
+    ledTcpRec=0;
+        
+    ledTcpSend=1;
+    wait(0.2);
+    ledTcpSend=0;
+    
+    ledXBeeRec=1;
+    wait(0.2);
+    ledXBeeRec=0;
+    
+    ledXBeeSend=1;
+    wait(0.2);
+    ledXBeeSend=0;
+
+    
+    wait(0.4);
+    
+    for(uint8_t i=0; i<2 ;i++){
+    
+        ledTcpCon=1;
+        ledTcpRec=1;
+        ledTcpSend=1;
+        ledXBeeRec=1;
+        ledXBeeSend=1;
+        wait(0.05);
+        
+        ledTcpCon=0;
+        ledTcpRec=0;
+        ledTcpSend=0;
+        ledXBeeRec=0;
+        ledXBeeSend=0;
+        wait(0.1);
+        
+    }
+
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/disp_led/leds.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,15 @@
+#ifndef _LEDS_
+#define _LEDS_
+
+
+
+void ledTcpRx(void);
+void ledTcpTx(void);
+void ledXBeeRx(void);
+void ledXBeeTx(void);
+
+void ledTcpConnect(bool isConnect);
+
+void startLed(void);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/common/types.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,114 @@
+/*----------------------------------------------------------------------------*/
+/* File Information                                                           */
+/*----------------------------------------------------------------------------*/
+/* Name       : types.h                                                       */
+/* Type       : C Programming Language Header                                 */
+/*----------------------------------------------------------------------------*/
+/*----------------------------------------------------------------------------*/
+
+#ifndef __TYPES_H__
+#define __TYPES_H__
+
+#include "stdint.h"
+/*
+typedef char                    int8_t;
+typedef unsigned    char        uint8_t;
+typedef signed      short       int16_t;
+typedef unsigned    short       uint16_t;
+typedef signed      int         int32_t;
+typedef unsigned    int         uint32_t;
+typedef signed      long long   int64_t;
+typedef unsigned    long long   uint64_t;
+*/
+//typedef bool                bool_t;
+typedef enum{TRUE, FALSE} bool_t;
+
+//=========================================================================
+//    byte bit access
+//=========================================================================
+typedef union{                    //    BYTE/NIBBLE/BIT access
+    uint8_t byte;                //    Byte access
+    struct{                        //    Nibble access
+        uint8_t lo : 4;        //        lower(Bit0 - 3)
+        uint8_t hi : 4;        //        upper(Bit4 - 7)
+    }nibble;
+    struct{                        //    Bit access
+        uint8_t b0 : 1;        //        Bit0
+        uint8_t b1 : 1;        //        Bit1
+        uint8_t b2 : 1;        //        Bit2
+        uint8_t b3 : 1;        //        Bit3
+        uint8_t b4 : 1;        //        Bit4
+        uint8_t b5 : 1;        //        Bit5
+        uint8_t b6 : 1;        //        Bit6
+        uint8_t b7 : 1;        //        Bit7
+    }bits;
+}byte_t;
+
+//=========================================================================
+//    word bit access
+//=========================================================================
+typedef union{                    //    WORD/BYTE/NIBBLE/BIT access
+    uint16_t word;                //    Word access
+    struct{                        //    Byte access
+        uint8_t b0;            //        upper byte
+        uint8_t b1;            //        lower byte
+    }byte;
+    struct    {                    //    Nibble access
+        uint8_t n0 : 4;        //        lower byte low(Bit 0 -  3)
+        uint8_t n1 : 4;        //        lower byte up (Bit 4 -  7)
+        uint8_t n2 : 4;        //        upper byte low(Bit 8 - 11)
+        uint8_t n3 : 4;        //        upper byte up (Bit12 - 15)
+    }nibble;
+    struct{                        //    Bit acces
+        uint8_t b0 : 1;        //        Bit0
+        uint8_t b1 : 1;        //        Bit1
+        uint8_t b2 : 1;        //        Bit2
+        uint8_t b3 : 1;        //        Bit3
+        uint8_t b4 : 1;        //        Bit4
+        uint8_t b5 : 1;        //        Bit5
+        uint8_t b6 : 1;        //        Bit6
+        uint8_t b7 : 1;        //        Bit7
+        uint8_t b8 : 1;        //        Bit8
+        uint8_t b9 : 1;        //        Bit9
+        uint8_t b10: 1;        //        Bit10
+        uint8_t b11: 1;        //        Bit11
+        uint8_t b12: 1;        //        Bit12
+        uint8_t b13: 1;        //        Bit13
+        uint8_t b14: 1;        //        Bit14
+        uint8_t b15: 1;        //        Bit15
+    }bits;
+}word_t;
+
+
+//=========================================================================
+//    ascii code
+//=========================================================================
+#define Z_NUL (0x00)
+#define Z_SOH (0x01)
+#define Z_STX (0x02)
+#define Z_ETX (0x03)
+#define Z_EOT (0x04)
+#define Z_ENQ (0x05)
+#define Z_ACK (0x06)
+#define Z_BEL (0x07)
+
+#define Z_BS  (0x08)
+#define Z_HT  (0x09)
+#define Z_LF  (0x0A)
+#define Z_HM  (0x0B)
+#define Z_FF  (0x0C)
+#define Z_CR  (0x0D)
+#define Z_SO  (0x0E)
+#define Z_SI  (0x0F)
+
+#define Z_DLE (0x10)
+#define Z_DC1 (0x11)
+#define Z_DC2 (0x12)
+#define Z_DC3 (0x13)
+#define Z_DC4 (0x14)
+#define Z_NAK (0x15)
+#define Z_SYN (0x16)
+#define Z_ETB (0x17)
+
+
+#endif    /* __TYPES_H__*/
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,50 @@
+#ifndef _CONFIG_
+#define _CONFIG_
+#include "mbed.h"
+
+//comentout -> nomal(do not debug printf)
+//#define DEBUG
+
+
+#ifdef DEBUG
+#define DBGV(fmt, ...) pc.printf(fmt, __VA_ARGS__)
+#define DBGF(X) pc.printf(X);
+#else
+#define DBGV(...)
+#define DBGF(...)
+#endif
+
+//mbed server.client
+#define kSserverIp  192,168,0,111
+#define kSubnetMask 255,255,255,0
+#define kRouterIp   192,168,0,1
+#define kServerPort 12345
+ 
+//iPad
+#define kClientIp   192,168,0,2
+#define kClientPort 12346
+
+#define kXBeeNo0DL 0x4086B0D5    //mbed server.client
+#define kXBeeNo1DL 0x4086AFDE   //sender
+#define kXBeeNo2DL 0x4086AFEA   //reciever
+
+#define kDeviceServer kXBeeNo0DL
+#define kLedSender    kXBeeNo1DL
+#define kLedReciever  kXBeeNo2DL
+
+#define kMyXBeeAddress kDeviceServer
+
+#define kXBeeBoudRate 115200
+
+#define kiPadServerCheckInterval    10
+
+#define kHealthCheckiPad 0xF0 //health check command
+#define kHealthCheckmbed 0xF1 //health check command
+
+typedef enum{
+    close,
+    connecting,
+    connected
+} ConnectStatus;
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,142 @@
+#include "mbed.h"
+#include "xbee_lib.h"
+#include "EthernetNetIf.h"
+#include "config.h"
+#include "tcp_server.h"
+#include "tcp_client.h"
+#include "xbee_rec_route.h"
+
+#include "leds.h"
+
+Serial pc(USBTX, USBRX);
+
+volatile PrefServer pref;
+
+EthernetNetIf eth ;   
+
+Ticker connectCheck;
+
+//#ifdef _SERVER_TEST_MODE_
+
+#include "data_models.h"
+
+DigitalIn  recSw(p20);
+Timeout chatter;
+bool isPressed;
+
+extern bool healthiPad;
+
+
+class Watchdog {  
+public:
+    void kick(float s) {
+        LPC_WDT->WDCLKSEL = 0x1;                // Set CLK src to PCLK
+        uint32_t clk = SystemCoreClock / 16;    // WD has a fixed /4 prescaler, PCLK default is /4 
+        LPC_WDT->WDTC = s * (float)clk;         
+        LPC_WDT->WDMOD = 0x3;                   // Enabled and Reset        
+        kick();
+    }
+    
+    void kick() {
+        LPC_WDT->WDFEED = 0xAA;
+        LPC_WDT->WDFEED = 0x55;
+    }
+};
+
+Watchdog w;
+
+
+
+
+
+
+void testModeInit(void)
+{
+    recSw.mode(PullUp);
+}
+
+void testSendRecieveData()
+{
+
+   Payload payload;
+   initTestPayloadData(&payload);
+
+   udpSendPayload(&payload); 
+   
+}
+
+
+//#endif
+
+
+
+int main() {
+
+    
+    
+    pc.baud(115200);
+
+//    #ifdef _SERVER_TEST_MODE_
+    testModeInit();
+//    #endif
+    
+    uint8_t tmpip[4]={kSserverIp};
+    uint8_t tmpsubnet[4]={kSubnetMask};
+    uint8_t tmprt[4]={kRouterIp};
+        
+    EthernetNetIf eth( //  -- static IP address
+        IpAddr(kSserverIp),     // IP Address
+        IpAddr(kSubnetMask),   // Subnet Mask
+        IpAddr(kRouterIp),     // Default Gateway
+        IpAddr(kRouterIp)      // DNS Server
+    ) ;
+
+    if( eth.setup() != ETH_OK )
+    {
+        pc.printf("Ether Setup Error\r\n");
+        return -1;
+    }
+    pc.printf("Ether Setup OK\r\n");
+
+    memcpy((void*)pref.serverAddress,tmpip,4);
+    pc.printf("mbed IP Address is [%d.%d.%d.%d]\r\n", pref.serverAddress[0], pref.serverAddress[1], pref.serverAddress[2], pref.serverAddress[3]) ;
+    
+    memcpy((void*)pref.subnetMask,tmpsubnet,4);
+    pc.printf("mbed subnet Address is [%d.%d.%d.%d]\r\n", pref.subnetMask[0], pref.subnetMask[1], pref.subnetMask[2], pref.subnetMask[3]) ;
+    
+    memcpy((void*)pref.routerAddress,tmprt,4);
+    pc.printf("mbed router Address is [%d.%d.%d.%d]\r\n", pref.routerAddress[0], pref.routerAddress[1], pref.routerAddress[2], pref.routerAddress[3]) ;
+
+    ;
+    
+    xbeeInit();
+
+    serverInit();
+    clientInit();
+    
+    startLed();
+    
+    connectCheckStart();
+ 
+//    w.kick(2.5); //watch dog timer set
+
+    while(1) {
+    
+        w.kick();
+        
+        Net::poll();
+       
+        
+  //      #ifdef _SERVER_TEST_MODE_
+        if(!recSw){
+            testSendRecieveData();
+            wait(0.5);
+        }
+  //     #else
+        xbeeRecieveRouting();
+  //       #endif
+        if(healthiPad)  ledTcpConnect(false);
+        else            ledTcpConnect(true);
+
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/14f4805c468c
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/net_routing.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,56 @@
+#include "net_routing.h"
+#include "types.h"
+#include "config.h"
+#include "xbee_lib.h"
+#include "tcp_client.h"
+
+
+extern PrefServer pref;
+extern Serial pc;
+
+void netRouting(Payload *_payload)
+{
+    
+
+    #ifdef _SERVER_TEST_MODE_
+        tcpSendPayload(_payload);
+
+    #else
+    
+    dumpPayload(_payload);
+    
+    uint32_t xbeeAdr=0;
+    
+    switch(_payload->frame.message.device)
+    {
+        case LED_SENDER:
+        {
+            xbeeAdr = kLedSender;
+            xbeeSend(xbeeAdr,_payload);
+            break;
+        }
+        
+        case LED_RECIEVER:
+        {
+            xbeeAdr = kLedReciever;
+            xbeeSend(xbeeAdr,_payload);
+            break;
+        }
+        case SERVER:
+        {
+            if(_payload->frame.message.command==PING){
+                memcpy(_payload->frame.message.data,&pref,sizeof(pref));
+                _payload->frame.message.device=SERVER;
+                udpSendPayload(_payload);
+                pc.printf("*******  ping!  **************\r\n");
+            }
+            break;
+        }
+    
+    }
+    
+
+    
+    #endif
+    
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/net_routing.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,8 @@
+#ifndef _RECIEVE_ROUTING_
+#define _RECIEVE_ROUTING_
+
+#include "data_models.h"
+
+void netRouting(Payload *payload);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/tcp_client.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,93 @@
+#include "types.h"
+#include "mbed.h"
+#include "config.h"
+#include "tcp_error.h"
+#include "tcp_client.h"
+#include "tcp_server.h"
+
+#include "leds.h"
+extern Serial pc;
+
+ Host iPadserver;
+
+extern UDPSocket sock;
+
+extern IpAddr inCommingIp;
+extern PrefServer pref;
+
+extern bool healthiPad;
+bool isSend;
+
+
+ConnectStatus iPadServerStatus;
+
+Timer checkTimer;
+Ticker clientCheck;
+
+void clientInit(void){
+ 
+    iPadServerStatus=close;
+    
+    pref.destinationPort = kClientPort;
+    
+    uint8_t tmpip[4]={kClientIp};   
+    memcpy((void*)pref.destinationAddress,tmpip,4);
+      
+    pc.printf("client IP Address is [%d.%d.%d.%d] Port:%d\r\n", pref.destinationAddress[0], pref.destinationAddress[1], pref.destinationAddress[2], pref.destinationAddress[3],pref.destinationPort) ;
+    healthiPad=true;
+    isSend=false;
+    
+    iPadserver = Host(IpAddr(kClientIp), kClientPort);
+}
+
+
+int udpSend(char *data,int length){
+
+    if(isSend) return -1;
+    
+    isSend = true;
+    
+    ledTcpTx();
+     
+    int ret=sock.sendto(data, length, &iPadserver);
+    
+    isSend=false;
+    
+    return ret;
+    
+}
+
+void checkConnect(void){
+static uint16_t checkSendCount=0;
+//    if(iPadServerStatus != close) return;
+    
+    char d = kHealthCheckmbed;
+    
+    udpSend( &d , 1 );
+    healthiPad=true;
+    DBGV("send mbed checker to iPad health check %d\n",checkSendCount++);
+
+}
+
+
+
+void connectCheckStart(void)
+{
+
+    clientCheck.attach(&checkConnect,kiPadServerCheckInterval);   
+}
+
+
+
+int udpSendPayload(Payload *_payload)
+{
+    uint8_t tmp[_payload->size];
+    
+    memcpy(tmp,(uint8_t*)_payload,_payload->size);
+    
+    payloadToNetwork((Payload *)tmp);
+    
+    return udpSend((char*)tmp,_payload->size);
+}
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/tcp_client.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,18 @@
+#ifndef _TCP_CLIENT_
+#define _TCP_CLIENT_
+
+#include "EthernetNetIf.h"
+#include "TCPSocket.h"
+#include "data_models.h"
+
+
+void clientInit(void);
+int udpSend(char *data,int length);
+int udpSendPayload(Payload *_payload);
+
+
+void connectCheckStart();
+void checkConnect(void);
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/tcp_error.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,18 @@
+#include "mbed.h"
+#include "tcp_error.h"
+#include "TCPSocket.h"
+extern Serial pc;
+char tcpErrMes[8][100]={
+    "TCPSOCKET_SETUP TCPSocket not properly configured.\r\n",
+    "TCPSOCKET_TIMEOUT Connection timed out.\r\n",
+    "TCPSOCKET_IF Interface has problems, does not exist or is not initialized.\r\n",
+    "TCPSOCKET_MEM Not enough mem.\r\n",
+    "TCPSOCKET_INUSE Interface / Port is in use.\r\n",
+    "TCPSOCKET_EMPTY Connections queue is empty.\r\n",
+    "TCPSOCKET_RST Connection was reset by remote host.\r\n",
+    "TCPSOCKET_OK Success.\r\n"
+};
+
+void dispTcpError(TCPSocketErr err){
+     pc.printf("%s\r\n",tcpErrMes[err]) ;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/tcp_error.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,8 @@
+#ifndef _TCP_ERROR_
+#define _TCP_ERROR_
+
+#include "TCPSocket.h"
+
+void dispTcpError(TCPSocketErr err);
+
+#endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/tcp_server.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,120 @@
+#include "types.h"
+#include "mbed.h"
+#include "config.h"
+#include "tcp_server.h"
+#include "tcp_client.h"
+
+#include "data_models.h"
+#include "xbee_lib.h"
+#include "net_routing.h"
+
+#include "leds.h"
+
+extern Serial pc;
+
+Host client;
+IpAddr inCommingIp;
+
+
+extern PrefServer pref;
+
+extern bool isHostConnected;
+
+
+UDPSocket sock;
+
+bool healthiPad;
+
+int serverInit(void)
+{
+ 
+    sock.setOnEvent(&onUDPSocketEvent);
+    
+    Host x(IpAddr(kSserverIp),kServerPort);
+    
+    pc.printf("seting bind\n");
+    UDPSocketErr udpErr = sock.bind( x );
+    
+    if ( udpErr != UDPSOCKET_OK ){
+     pc.printf("error %d\n", udpErr);
+     return -1;
+     }
+    pc.printf("server setting OK\n");
+   
+
+    return 0;
+}
+
+
+void onUDPSocketEvent(UDPSocketEvent e) {
+  static  uint16_t checkMbedSendCount=0;
+  static  uint16_t checkiPadRecCount=0;
+    char buff[256];
+   
+
+    if (e == UDPSOCKET_READABLE) 
+    {
+        ledTcpRx();
+        
+        Host client;
+    
+        int size = sock.recvfrom(buff, sizeof(buff), &client);
+
+        DBGV("recieve from %d.%d.%d.%d\r\n", (unsigned char)client.getIp()[0], (unsigned char)client.getIp()[1], (unsigned char)client.getIp()[2], (unsigned char)client.getIp()[3]);
+      
+        
+        char d;
+        if( size == 1)
+        {
+            switch(buff[0])
+            {
+                case kHealthCheckiPad:
+                {
+                     d=kHealthCheckiPad;
+                     udpSend(&d, 1);
+                     DBGV("return send mbed checker to iPad %d\n",checkiPadRecCount++);
+                }
+                break;
+                
+                case kHealthCheckmbed:
+                {
+                     healthiPad=false;
+                      DBGV("recieve mbed checker %d\n",checkMbedSendCount++);
+                }
+                break;
+            }
+            
+            return;
+        }
+        
+   
+        Payload pl;
+        memcpy( &pl , buff , sizeof(Payload) );
+        
+//        initPayload(&pl);
+
+//         dumpPayload(&pl); 
+                
+        networkToPayload(&pl);
+                 
+        netRouting(&pl);
+     }
+}
+
+
+void recieveDump(char *data , int length){
+     pc.printf("size = %d\n -- -id\n",length);
+     
+     for(int i=0;i<16;i++) pc.printf("%d:%0x ",i,data[i]);
+     pc.printf("\n--- data\n");
+     
+     int count=0;
+     for(int i=0;i<(length-16);i++){
+      pc.printf("%02d:%02x ",i,data[16+i]);
+      if( count==15 ) {count=0; pc.printf("\n");}
+      else count++;
+     }
+     
+     pc.printf("\n---frame end\n");
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/tcpip/tcp_server.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,20 @@
+#ifndef _TCP_SERVER_
+#define _TCP_SERVER_
+
+#include "UDPSocket.h"
+
+
+
+int serverInit(void);
+
+void onUDPSocketEvent(UDPSocketEvent e);
+/*
+void onServerListnerEvent(TCPSocketEvent e);
+void onServerRecievetEvent(TCPSocketEvent e);
+*/
+void recieveDump(char *data , int length);
+
+
+
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee/xbee_lib.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,53 @@
+#include "mbed.h"
+#include "types.h"
+#include "config.h"
+#include "XBee.h"
+#include "xbee_lib.h"
+
+#include "leds.h"
+extern Serial pc;
+
+XBee xbee(p13, p14);
+ZBRxResponse zbRx = ZBRxResponse();
+
+extern PrefServer pref;
+
+void xbeeInit(void)
+{
+    LPC_UART1->MCR |= (1<<7); // CTSEN
+    LPC_PINCON->PINSEL1 &= ~(3 << 2);
+    LPC_PINCON->PINSEL1 |= (1 << 2); // UART CTS
+    xbee.begin(kXBeeBoudRate);
+    pref.xbeeAdress = kMyXBeeAddress;
+    pc.printf("XBee setup OK!  XBee serial boudrate:%d address:%04X\r\n",kXBeeBoudRate,pref.xbeeAdress);
+}
+
+void xbeeSend(uint32_t addr , Payload *_payload)
+{
+    ledXBeeTx();
+    
+    XBeeAddress64 deviceAdr = XBeeAddress64(0x0013a200, addr);
+    
+    uint16_t length = sizeof(Payload);
+    _payload->size = length;
+    uint8_t tmp[length];
+    
+    memcpy(tmp , (uint8_t*)_payload , length);
+    
+    payloadToNetwork((Payload*)tmp);
+    ZBTxRequest zbTx = ZBTxRequest(deviceAdr , tmp , length );
+    
+    xbee.send(zbTx);
+    DBGF("+++++ xbee send!!\r\n");
+    
+ 
+}
+
+void xbeeReadPacket(void)
+{
+    xbee.readPacket();
+}
+bool isXbeeAvaiable(void)
+{
+   return xbee.getResponse().isAvailable();
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee/xbee_lib.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,13 @@
+#ifndef _XBEE_LIB_
+#define _XBEE_LIB_
+
+#include "types.h"
+#include "data_models.h"
+
+void xbeeInit(void);
+void xbeeSend( uint32_t addr , Payload *_payload );
+
+void xbeeReadPacket(void);
+bool isXbeeAvaiable(void);
+
+#endif
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee/xbee_rec_route.cpp	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,104 @@
+#include "xbee_rec_route.h"
+#include "config.h"
+#include "XBee.h"
+
+#include "types.h"
+
+#include "xbee_lib.h"
+#include "tcp_client.h"
+#include "leds.h"
+
+extern XBee xbee;
+extern ZBRxResponse zbRx;
+
+
+void xbeeRecieveRouting(void)
+{
+
+     xbeeReadPacket();
+    
+   
+        if (isXbeeAvaiable()) {
+            
+            
+    
+            DBGF("xbee aviable!\r\n");
+        
+            if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
+                
+                ledXBeeRx();
+                
+                xbee.getResponse().getZBRxResponse(zbRx);
+                
+                Payload payload;
+
+                uint16_t length = (unsigned char)zbRx.getData()[0];
+                length =(length<<8) + (unsigned char)zbRx.getData()[1];
+                
+                DBGV("length=%04X\r\n",length);
+                
+                memcpy(&payload, (unsigned char *)zbRx.getData(), length);
+
+                
+                
+                networkToPayload(&payload);
+               
+                routing(&payload);
+                /*
+                pc.printf("xbee recieve!! size:%d\r\n",length);
+                
+                pc.printf("---------dump end\r\n");
+                */
+                
+                
+           
+            }
+        //    pc.printf("\r\n");
+        }
+    
+    
+}
+
+void routing(Payload *_payload)
+{
+//     dumpPayload(_payload);
+    switch(_payload->frame.message.command)
+    {
+        case SEND_SINGLE:
+        {
+        //    _payload->frame.message.command=ACK;
+        //    xbeeSend(0x4086B0D5,_payload);
+            DBGF("*******  send!  *******\r\n");
+        }
+        break;
+        
+        case RECIEVE:
+        {
+            udpSendPayload(_payload);
+            DBGF("******  recieve!  *******\r\n");
+        }
+        break;
+        
+        case PING:
+        {   
+            
+            udpSendPayload(_payload);
+            DBGF("*******  ping!  **************\r\n");
+        }
+        break;
+        
+        case ACK:
+        {   
+            udpSendPayload(_payload);
+            DBGF("*******  ACK!  **************\r\n");
+        }
+        
+        case SET_TX_GAIN:
+        case SET_LIGHT_CONTROL:
+        {
+            udpSendPayload(_payload);
+        }
+        break;
+    }
+
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/xbee/xbee_rec_route.h	Sat Apr 14 03:53:45 2012 +0000
@@ -0,0 +1,8 @@
+#ifndef _XBEE_ROUTING_
+#define _XBEE_ROUTING_
+#include "data_models.h"
+
+void xbeeRecieveRouting(void); 
+void routing(Payload *_payload);
+
+#endif
\ No newline at end of file