![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
server/client
Dependencies: mbed NetServicesMin
Revision 1:6e61ee662fd3, committed 2012-02-23
- Comitter:
- recotana
- Date:
- Thu Feb 23 02:38:05 2012 +0000
- Parent:
- 0:432b571e11a3
- Commit message:
- added standalone test mode
Changed in this revision
diff -r 432b571e11a3 -r 6e61ee662fd3 XBee.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/XBee.lib Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/okini3939/code/XBee/#6efb3541af61
diff -r 432b571e11a3 -r 6e61ee662fd3 config.h --- a/config.h Sun Feb 19 06:21:50 2012 +0000 +++ b/config.h Thu Feb 23 02:38:05 2012 +0000 @@ -1,14 +1,41 @@ #ifndef _CONFIG_ #define _CONFIG_ +#include "mbed.h" -#define kSserverIp 192,168,0,111 +//comentout -> nomal(do not debug printf) +//#define DEBUG + +//standalone -> mbed ether echo back mode +//#define _SERVER_TEST_MODE_ + +#ifdef DEBUG +#define DBGV(fmt, ...) printf(fmt, __VA_ARGS__) +#define DBGF(X) 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 kRouterIp 192,168,0,1 #define kServerPort 12345 -#define kClientIp 192,168,0,2 +//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 kLedSender kXBeeNo1DL +#define kLedReciever kXBeeNo2DL + + + + typedef enum{ close, connecting,
diff -r 432b571e11a3 -r 6e61ee662fd3 data_models.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_models.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,64 @@ +#include "types.h" +#include "data_models.h" +#include "EthernetNetIf.h" + + +void initPayload(Payload *_payload) +{ + _payload->size=sizeof(Payload); + for(int i=0 ; i < sizeof(_payload->id.personalId) ; i++) _payload->id.personalId[i]=0; + + _payload->message.frameNumber=0; + _payload->message.device=SERVER; + _payload->message.command=SEND; + + for(int i=0 ; i < sizeof(_payload->message.data) ; i++) _payload->message.data[i]=0; +} + +void payloadToNetwork(Payload *_payload) +{ + + _payload->size = htons(_payload->size); + _payload->id.head = htons(_payload->id.head); + _payload->message.totalSize = htons(_payload->message.totalSize); + _payload->message.frameNumber = htons(_payload->message.frameNumber); + +} +void networkToPayload(Payload *_payload) +{ + _payload->size = ntohs(_payload->size); + _payload->id.head = ntohs(_payload->id.head); + _payload->message.totalSize = ntohs(_payload->message.totalSize); + _payload->message.frameNumber = ntohs(_payload->message.frameNumber); +} + +void dumpPayload(Payload *_payload) +{ + printf("\r\n======== dump!!\r\n"); + printf("payload\nsize:%04X\r\n ",_payload->size); + + printf("Payload id_head:%04X\r\n personal id:",_payload->id.head); + for(int i=0 ; i < sizeof(_payload->id.personalId) ; i++){ + printf("%d:%02X ",i,_payload->id.personalId[i]); + } + + printf("\r\n"); + + printf("total size:%d , frame number:%d , divece:%0X , command:%02X\r\n" + ,_payload->message.totalSize + ,_payload->message.frameNumber + ,_payload->message.device + ,_payload->message.command); + + printf("message data:\r\n"); + for(int i=0 ; i < sizeof(_payload->message.data) ; i++){ + printf("%d:%02X ",i,_payload->message.data[i]); + } + + printf("\r\n--- RAW DATA -------\r\n"); + for(int i=0 ; i < sizeof(Payload) ; i++){ + printf("%d:%02X ",i,((uint8_t*)_payload)[i]); + } + printf("\r\n"); + +} \ No newline at end of file
diff -r 432b571e11a3 -r 6e61ee662fd3 data_models.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/data_models.h Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,53 @@ +#ifndef _DATA_MODELS_ +#define _DATA_MODELS_ + +#include "types.h" + +typedef enum{ + SERVER, + LED_SENDER, + LED_RECIEVER +} Device; + +typedef enum{ + SEND, + RECIEVE, + PING, + ACK +} Command; + +// +//for TCP/IP data structure +// + +//48byte message data +typedef struct{ + uint16_t totalSize; //uint16_t + uint16_t + uint8_t + uint8_t + 42byte + uint16_t frameNumber; + Device device; + Command command; + uint8_t data[42]; +} Message; + +//16byte payload id +typedef struct{ + uint16_t head; //reserve 0x0000 + uint8_t personalId[14]; +} PayloadID; + +//66byte payload +typedef struct{ + uint16_t size; // 2(size) + 64(payload id + message) = 66 byte + PayloadID id; // 16 byte + Message message; // 48 byte +} Payload; + + +void initPayload(Payload *_payload); + +void payloadToNetwork(Payload *_payload); +void networkToPayload(Payload *_payload); + +void dumpPayload(Payload *_payload); + +#endif \ No newline at end of file
diff -r 432b571e11a3 -r 6e61ee662fd3 leds.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/leds.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,43 @@ +#include "mbed.h" +#include "leds.h" + +Timeout led_ether; +Timeout led_xbee; +Timeout led_device; + +DigitalOut ledXb(LED1); +DigitalOut ledDev(LED2); +DigitalOut ledTcpCon(LED3); +DigitalOut ledEt(LED4); + +void offEther(void){ + ledEt=0; +} +void offXBee(void){ + ledXb=0; +} +void offDevice(void){ + ledDev=0; +} + +void ledEther(void) +{ + ledEt=1; + led_ether.attach(&offEther,kLedOnTime); +} +void ledXBee(void) +{ + ledXb=1; + led_xbee.attach(&offXBee,kLedOnTime); +} +void ledDevice(void) +{ + ledDev=1; + led_device.attach(&offDevice,kLedOnTime); +} + +void ledTcpConnect(bool isConnect) +{ + if(isConnect) ledTcpCon=1; + else ledTcpCon=0; +} \ No newline at end of file
diff -r 432b571e11a3 -r 6e61ee662fd3 leds.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/leds.h Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,11 @@ +#ifndef _LEDS_ +#define _LEDS_ + +#define kLedOnTime 0.2 + +void ledEther(void); +void ledXBee(void); +void ledDevice(void); +void ledTcpConnect(bool isConnect); + +#endif \ No newline at end of file
diff -r 432b571e11a3 -r 6e61ee662fd3 main.cpp --- a/main.cpp Sun Feb 19 06:21:50 2012 +0000 +++ b/main.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -1,17 +1,78 @@ #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" EthernetNetIf eth ; Ticker connectCheck; - + +#ifdef _SERVER_TEST_MODE_ + +#include "data_models.h" + +DigitalIn recSw(p20); +Timeout chatter; +bool isPressed; + +void testModeInit(void) +{ + recSw.mode(PullUp); +} + +void testSendRecieveData() +{ + Payload payload; + initPayload(&payload); + payload.size=sizeof(Payload); + + payload.id.head=0x0000; + uint8_t tmpid[14]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38,0x39,0x40,0x41,0x42,0x43,0x44}; + for(int i=0;i<sizeof(tmpid);i++) payload.id.personalId[i]=tmpid[i]; + + + payload.message.frameNumber=1; + payload.message.device=LED_RECIEVER; + payload.message.command=RECIEVE; + + uint8_t tmpdata[42]={ + 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, + 41,42}; + for(int i=0;i<sizeof(tmpdata);i++) payload.message.data[i]=tmpdata[i]; + + payload.message.totalSize= + sizeof(payload.message.totalSize) + +sizeof(payload.message.frameNumber) + +sizeof(payload.message.device) + +sizeof(payload.message.command) + +sizeof(payload.message.data); + + tcpSendPayload(&payload); + +} + + +#endif + + + int main() { + #ifdef _SERVER_TEST_MODE_ + testModeInit(); + #endif + + xbeeInit(); - EthernetNetIf eth( //) -- static IP address + EthernetNetIf eth( // -- static IP address IpAddr(kSserverIp), // IP Address IpAddr(kSubnetMask), // Subnet Mask IpAddr(kRouterIp), // Default Gateway @@ -20,20 +81,34 @@ if( eth.setup() != ETH_OK ) { - printf("Ether Setup Error\r\n"); + DBGF("Ether Setup Error\r\n"); return -1; } - printf("Setup OK\r\n"); + DBGF("Setup OK\r\n"); IpAddr ip = eth.getIp() ; - printf("mbed IP Address is [%d.%d.%d.%d]\r\n", ip[0], ip[1], ip[2], ip[3]) ; + DBGV("mbed IP Address is [%d.%d.%d.%d]\r\n", ip[0], ip[1], ip[2], ip[3]) ; serverInit(); + clientInit(); - clientInit(); connectCheckStart( IpAddr(kClientIp),kClientPort ); + wait(2); + ledEther(); + ledXBee(); + ledDevice(); while(1) { Net::poll(); + + #ifdef _SERVER_TEST_MODE_ + if(!recSw){ + testSendRecieveData(); + wait(2); + } + #else + xbeeRecieveRouting(); + #endif + } }
diff -r 432b571e11a3 -r 6e61ee662fd3 net_routing.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/net_routing.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,38 @@ +#include "net_routing.h" +#include "types.h" +#include "config.h" +#include "xbee_lib.h" +#include "tcp_client.h" + +void netRouting(Payload *_payload) +{ + + + #ifdef _SERVER_TEST_MODE_ + tcpSendPayload(_payload); + + #else + + uint32_t xbeeAdr=0; + + switch(_payload->message.device) + { + case LED_SENDER: + { + xbeeAdr = kLedSender; + break; + } + + case LED_RECIEVER: + { + xbeeAdr = kLedReciever; + break; + } + + } + + + xbeeSend(xbeeAdr,_payload); + #endif + +} \ No newline at end of file
diff -r 432b571e11a3 -r 6e61ee662fd3 net_routing.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/net_routing.h Thu Feb 23 02:38:05 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
diff -r 432b571e11a3 -r 6e61ee662fd3 tcp_client.cpp --- a/tcp_client.cpp Sun Feb 19 06:21:50 2012 +0000 +++ b/tcp_client.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -1,10 +1,14 @@ +#include "types.h" #include "mbed.h" #include "config.h" #include "tcp_error.h" #include "tcp_client.h" +#include "leds.h" + extern IpAddr inCommingIp; + Host server; TCPSocket sendSock; ConnectStatus clientStatus; @@ -15,62 +19,73 @@ int clientPort; void onSendEvent (TCPSocketEvent e) { - printf("... client event arrival!\n"); + DBGF("... client event arrival!\r\n"); switch(e) { - case TCPSOCKET_CONNECTED: // Ú±®¹ + case TCPSOCKET_CONNECTED: { - char str[]="connect!"; - sendSock.send(str,strlen(str) ); // M + ledTcpConnect(true); + + char str[]="wellcome! server connected!"; + + sendSock.send(str,strlen(str) ); + clientStatus=connected; - printf("TCPSOCKET_CONNECTED!!\n"); + DBGF("TCPSOCKET_CONNECTED!!\r\n"); break; } - case TCPSOCKET_WRITEABLE: // MÂ\iM®¹j + case TCPSOCKET_WRITEABLE: { - printf("TCPSOCKET_WRITEABLE!!\n"); + DBGF("TCPSOCKET_WRITEABLE!!\r\n"); clientStatus=connected; + ledTcpConnect(true); break; } - case TCPSOCKET_READABLE: // óMf[^ è - { printf("TCPSOCKET_READABLE!!\n"); + case TCPSOCKET_READABLE: + { DBGF("TCPSOCKET_READABLE!!\r\n"); clientStatus=connected; + ledTcpConnect(true); break; } case TCPSOCKET_CONTIMEOUT: { - printf("TCPSOCKET_CONTIMEOUT!!\n"); + DBGF("TCPSOCKET_CONTIMEOUT!!\r\n"); socketClose(); + ledTcpConnect(false); break; } case TCPSOCKET_CONRST: { - printf("TCPSOCKET_CONRST!!\n"); + DBGF("TCPSOCKET_CONRST!!\r\n"); socketClose(); + ledTcpConnect(false); break; } case TCPSOCKET_CONABRT: { - printf("TCPSOCKET_CONABRT!!\n"); + DBGF("TCPSOCKET_CONABRT!!\r\n"); socketClose(); + ledTcpConnect(false); break; } case TCPSOCKET_ERROR: - { printf("TCPSOCKET_ERROR\n"); + { DBGF("TCPSOCKET_ERROR\r\n"); socketClose(); + ledTcpConnect(false); break; } case TCPSOCKET_DISCONNECTED: { - printf("TCPSOCKET_DISCONNECTED...\n"); + DBGF("TCPSOCKET_DISCONNECTED...\r\n"); socketClose(); + ledTcpConnect(false); break; } } - printf("...client event out\n"); + DBGF("...client event out\r\n"); } void clientInit(void){ @@ -81,11 +96,11 @@ int clientConnect(IpAddr ip , int port) { if(clientStatus == connecting){ - printf("socket already connecting..\n"); + DBGF("socket already connecting..\r\n"); return 0; } else if(clientStatus == connected){ - printf("socket already connected..\n"); + DBGF("socket already connected..\r\n"); return 0; } @@ -93,7 +108,7 @@ TCPSocketErr err= sendSock.connect(server); if (err != TCPSOCKET_OK) { - printf("connect Error. "); + DBGF("connect Error. "); dispTcpError(err); clientStatus=close; return -1; @@ -103,8 +118,11 @@ } int tcpSend(char *data,int length){ - - return sendSock.send(data, length); + + ledEther(); + int ret = sendSock.send(data, length); + + return ret; } @@ -127,7 +145,17 @@ { clientIp=ip; clientPort=port; - clientCheck.attach(&checkConnect,3); - + clientCheck.attach(&checkConnect,3); } +int tcpSendPayload(Payload *_payload) +{ + uint8_t tmp[_payload->size]; + + memcpy(tmp,(uint8_t*)_payload,_payload->size); + + payloadToNetwork((Payload *)tmp); + + return tcpSend((char*)tmp,_payload->size); +} +
diff -r 432b571e11a3 -r 6e61ee662fd3 tcp_client.h --- a/tcp_client.h Sun Feb 19 06:21:50 2012 +0000 +++ b/tcp_client.h Thu Feb 23 02:38:05 2012 +0000 @@ -3,7 +3,7 @@ #include "EthernetNetIf.h" #include "TCPSocket.h" - +#include "data_models.h" void clientInit(void); @@ -13,6 +13,7 @@ void connectCheckStart(IpAddr ip , int port); int tcpSend(char *data,int length); +int tcpSendPayload(Payload *_payload); void checkConnect(void); void socketClose(void);
diff -r 432b571e11a3 -r 6e61ee662fd3 tcp_error.cpp --- a/tcp_error.cpp Sun Feb 19 06:21:50 2012 +0000 +++ b/tcp_error.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -3,16 +3,16 @@ #include "TCPSocket.h" char tcpErrMes[8][100]={ - "TCPSOCKET_SETUP TCPSocket not properly configured.\n", - "TCPSOCKET_TIMEOUT Connection timed out.\n", - "TCPSOCKET_IF Interface has problems, does not exist or is not initialized.\n", - "TCPSOCKET_MEM Not enough mem.\n", - "TCPSOCKET_INUSE Interface / Port is in use.\n", - "TCPSOCKET_EMPTY Connections queue is empty.\n", - "TCPSOCKET_RST Connection was reset by remote host.\n", - "TCPSOCKET_OK Success.\n" + "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){ - printf("%s\n",tcpErrMes[err]) ; + printf("%s\r\n",tcpErrMes[err]) ; } \ No newline at end of file
diff -r 432b571e11a3 -r 6e61ee662fd3 tcp_server.cpp --- a/tcp_server.cpp Sun Feb 19 06:21:50 2012 +0000 +++ b/tcp_server.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -1,29 +1,38 @@ +#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" + TCPSocket listenSock; TCPSocket* recieveSock; Host client; IpAddr inCommingIp; + + extern bool isHostConnected; int serverInit(void) { listenSock.setOnEvent(&onServerListnerEvent) ; - printf("Bindding...\r\n") ; + DBGF("Bindding...\r\n") ; if ( listenSock.bind(Host(IpAddr(), kServerPort)) != ETH_OK ){ - printf("Bindding Error.\r\n") ; + DBGF("Bindding Error.\r\n") ; return -1 ; } printf("Listen...\r\n"); if ( listenSock.listen() != ETH_OK ){ - printf("Listen Error.\r\n") ; + DBGF("Listen Error.\r\n") ; return -1 ; } return 0; @@ -32,13 +41,13 @@ //Listner Event void onServerListnerEvent(TCPSocketEvent e) { - printf("server ---IN TCPSocketEvent ---\r\n"); + DBGF("server ---IN TCPSocketEvent ---\r\n"); if ( e == TCPSOCKET_ACCEPT ){ - printf("Listening: TCP Socket Accepted\r\n"); + DBGF("Listening: TCP Socket Accepted\r\n"); if ( listenSock.accept(&client, &recieveSock) != TCPSOCKET_OK ) { - printf("onTcpSocketEvent Error \r\n"); + DBGF("onTcpSocketEvent Error \r\n"); return; } @@ -46,75 +55,85 @@ //Get Client Side IP Address inCommingIp = client.getIp(); - printf("Controler IP Address is [%d.%d.%d.%d].\r\n", inCommingIp[0], inCommingIp[1], inCommingIp[2], inCommingIp[3]); + DBGV("Controler IP Address is [%d.%d.%d.%d].\r\n", inCommingIp[0], inCommingIp[1], inCommingIp[2], inCommingIp[3]); } - printf("server --- OUT TCPSocketEvent ---\r\n\r\n") ; + DBGF("server --- OUT TCPSocketEvent ---\r\n\r\n") ; } void onServerRecievetEvent(TCPSocketEvent e) { - printf("server --- IN ConnectEvent ---\r\n"); + DBGF("server --- IN ConnectEvent ---\r\n"); switch(e) { case TCPSOCKET_CONNECTED: - printf("Connected to host.\r\n") ; + DBGF("Connected to host.\r\n") ; break; case TCPSOCKET_WRITEABLE: - printf("Can write data to buf.\r\n"); + DBGF("Can write data to buf.\r\n"); break; case TCPSOCKET_READABLE: - printf("Data in buf.\r\n"); - char buf[128] ; - while ( int len = recieveSock->recv(buf,sizeof(buf)) ) { + DBGF("Data in buf.\r\n"); + + ledEther(); + + Payload pl; + initPayload(&pl); + + while ( int len = recieveSock->recv((char*)&pl,256) ) { - int err =tcpSend(buf, len); - - recieveDump(buf,len); + //raw data dump + // dumpPayload(&payload); + + networkToPayload(&pl); + + netRouting(&pl); + } + ; break; case TCPSOCKET_CONTIMEOUT: - printf("Connection timed out.\r\n"); + DBGF("Connection timed out.\r\n"); break ; case TCPSOCKET_CONRST: - printf("Connection was reset by remote host.\r\n"); + DBGF("Connection was reset by remote host.\r\n"); break ; case TCPSOCKET_CONABRT: - printf("Connection was aborted.\r\n") ; + DBGF("Connection was aborted.\r\n") ; break ; case TCPSOCKET_ERROR: - printf("Unknown error.\r\n") ; + DBGF("Unknown error.\r\n") ; break ; case TCPSOCKET_DISCONNECTED: - printf("Tcp Socket Disconnected\r\n") ; + DBGF("Tcp Socket Disconnected\r\n") ; recieveSock->close() ; break; } - printf("--- OUT ConnectEvent ---\r\n\r\n"); + DBGF("--- OUT ConnectEvent ---\r\n\r\n"); } void recieveDump(char *data , int length){ - printf("size = %d\n -- -id\n",length); + DBGV("size = %d\n -- -id\n",length); for(int i=0;i<16;i++) printf("%d:%0x ",i,data[i]); - printf("\n--- data\n"); + DBGF("\n--- data\n"); int count=0; for(int i=0;i<(length-16);i++){ - printf("%02d:%02x ",i,data[16+i]); + DBGV("%02d:%02x ",i,data[16+i]); if( count==15 ) {count=0; printf("\n");} else count++; } - printf("\n---frame end\n"); + DBGF("\n---frame end\n"); }
diff -r 432b571e11a3 -r 6e61ee662fd3 types.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/types.h Thu Feb 23 02:38:05 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
diff -r 432b571e11a3 -r 6e61ee662fd3 util.cpp
diff -r 432b571e11a3 -r 6e61ee662fd3 xbee_lib.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xbee_lib.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,45 @@ +#include "mbed.h" +#include "types.h" +#include "config.h" +#include "XBee.h" +#include "xbee_lib.h" + +#include "leds.h" + +XBee xbee(p13, p14); +ZBRxResponse zbRx = ZBRxResponse(); + +void xbeeInit(void) +{ + xbee.begin(9800); +} + +void xbeeSend(uint32_t addr , Payload *_payload) +{ + ledXBee(); + + 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
diff -r 432b571e11a3 -r 6e61ee662fd3 xbee_lib.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xbee_lib.h Thu Feb 23 02:38:05 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
diff -r 432b571e11a3 -r 6e61ee662fd3 xbee_rec_route.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xbee_rec_route.cpp Thu Feb 23 02:38:05 2012 +0000 @@ -0,0 +1,96 @@ +#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) { + + ledXBee(); + + 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); + /* + printf("xbee recieve!! size:%d\r\n",length); + + printf("---------dump end\r\n"); + */ + + + + } + // printf("\r\n"); + } + + +} + +void routing(Payload *_payload) +{ +// dumpPayload(_payload); + switch(_payload->message.command) + { + case SEND: + { + _payload->message.command=ACK; + // xbeeSend(0x4086B0D5,_payload); + DBGF("******* send! *******\r\n"); + } + break; + + case RECIEVE: + { + DBGF("****** recieve! *******\r\n"); + } + break; + + case PING: + { + _payload->message.command=ACK; + // xbeeSend(0x4086B0D5,_payload); + tcpSendPayload(_payload); + DBGF("******* ping! **************\r\n"); + } + break; + + case ACK: + { + tcpSendPayload(_payload); + DBGF("******* ACK! **************\r\n"); + } + break; + } + +}
diff -r 432b571e11a3 -r 6e61ee662fd3 xbee_rec_route.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/xbee_rec_route.h Thu Feb 23 02:38:05 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