![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
UDPversion
Dependencies: XBee mbed NetServicesMin
tcpip/tcp_server.cpp@1:3a46d2725374, 2012-04-15 (annotated)
- Committer:
- recotana
- Date:
- Sun Apr 15 10:49:08 2012 +0000
- Revision:
- 1:3a46d2725374
- Parent:
- 0:84a3b029656e
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
recotana | 0:84a3b029656e | 1 | #include "types.h" |
recotana | 0:84a3b029656e | 2 | #include "mbed.h" |
recotana | 0:84a3b029656e | 3 | #include "config.h" |
recotana | 0:84a3b029656e | 4 | #include "tcp_server.h" |
recotana | 0:84a3b029656e | 5 | #include "tcp_client.h" |
recotana | 0:84a3b029656e | 6 | |
recotana | 0:84a3b029656e | 7 | #include "data_models.h" |
recotana | 0:84a3b029656e | 8 | #include "xbee_lib.h" |
recotana | 0:84a3b029656e | 9 | #include "net_routing.h" |
recotana | 0:84a3b029656e | 10 | |
recotana | 0:84a3b029656e | 11 | #include "leds.h" |
recotana | 0:84a3b029656e | 12 | |
recotana | 0:84a3b029656e | 13 | extern Serial pc; |
recotana | 0:84a3b029656e | 14 | |
recotana | 0:84a3b029656e | 15 | Host client; |
recotana | 0:84a3b029656e | 16 | IpAddr inCommingIp; |
recotana | 0:84a3b029656e | 17 | |
recotana | 0:84a3b029656e | 18 | |
recotana | 0:84a3b029656e | 19 | extern PrefServer pref; |
recotana | 1:3a46d2725374 | 20 | extern PrefSender spref; |
recotana | 1:3a46d2725374 | 21 | extern PrefReciever rpref; |
recotana | 0:84a3b029656e | 22 | |
recotana | 0:84a3b029656e | 23 | extern bool isHostConnected; |
recotana | 0:84a3b029656e | 24 | |
recotana | 0:84a3b029656e | 25 | |
recotana | 0:84a3b029656e | 26 | UDPSocket sock; |
recotana | 0:84a3b029656e | 27 | |
recotana | 0:84a3b029656e | 28 | bool healthiPad; |
recotana | 0:84a3b029656e | 29 | |
recotana | 0:84a3b029656e | 30 | int serverInit(void) |
recotana | 0:84a3b029656e | 31 | { |
recotana | 0:84a3b029656e | 32 | |
recotana | 0:84a3b029656e | 33 | sock.setOnEvent(&onUDPSocketEvent); |
recotana | 0:84a3b029656e | 34 | |
recotana | 0:84a3b029656e | 35 | Host x(IpAddr(kSserverIp),kServerPort); |
recotana | 0:84a3b029656e | 36 | |
recotana | 0:84a3b029656e | 37 | pc.printf("seting bind\n"); |
recotana | 0:84a3b029656e | 38 | UDPSocketErr udpErr = sock.bind( x ); |
recotana | 0:84a3b029656e | 39 | |
recotana | 0:84a3b029656e | 40 | if ( udpErr != UDPSOCKET_OK ){ |
recotana | 0:84a3b029656e | 41 | pc.printf("error %d\n", udpErr); |
recotana | 0:84a3b029656e | 42 | return -1; |
recotana | 0:84a3b029656e | 43 | } |
recotana | 0:84a3b029656e | 44 | pc.printf("server setting OK\n"); |
recotana | 1:3a46d2725374 | 45 | |
recotana | 1:3a46d2725374 | 46 | spref.currentFrameNumber = 0; |
recotana | 1:3a46d2725374 | 47 | spref.gainData.gain=10; |
recotana | 1:3a46d2725374 | 48 | spref.gainData.middleLevel=5; |
recotana | 1:3a46d2725374 | 49 | spref.lightsw=1; |
recotana | 1:3a46d2725374 | 50 | spref.continueusMode=0; |
recotana | 1:3a46d2725374 | 51 | spref.isSend=0; |
recotana | 0:84a3b029656e | 52 | |
recotana | 0:84a3b029656e | 53 | return 0; |
recotana | 0:84a3b029656e | 54 | } |
recotana | 0:84a3b029656e | 55 | |
recotana | 0:84a3b029656e | 56 | |
recotana | 0:84a3b029656e | 57 | void onUDPSocketEvent(UDPSocketEvent e) { |
recotana | 1:3a46d2725374 | 58 | #ifdef DEBUG |
recotana | 0:84a3b029656e | 59 | static uint16_t checkMbedSendCount=0; |
recotana | 0:84a3b029656e | 60 | static uint16_t checkiPadRecCount=0; |
recotana | 1:3a46d2725374 | 61 | #endif |
recotana | 0:84a3b029656e | 62 | char buff[256]; |
recotana | 0:84a3b029656e | 63 | |
recotana | 0:84a3b029656e | 64 | |
recotana | 0:84a3b029656e | 65 | if (e == UDPSOCKET_READABLE) |
recotana | 0:84a3b029656e | 66 | { |
recotana | 0:84a3b029656e | 67 | ledTcpRx(); |
recotana | 0:84a3b029656e | 68 | |
recotana | 0:84a3b029656e | 69 | Host client; |
recotana | 0:84a3b029656e | 70 | |
recotana | 0:84a3b029656e | 71 | int size = sock.recvfrom(buff, sizeof(buff), &client); |
recotana | 0:84a3b029656e | 72 | |
recotana | 0:84a3b029656e | 73 | 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]); |
recotana | 0:84a3b029656e | 74 | |
recotana | 0:84a3b029656e | 75 | |
recotana | 0:84a3b029656e | 76 | char d; |
recotana | 0:84a3b029656e | 77 | if( size == 1) |
recotana | 0:84a3b029656e | 78 | { |
recotana | 0:84a3b029656e | 79 | switch(buff[0]) |
recotana | 0:84a3b029656e | 80 | { |
recotana | 0:84a3b029656e | 81 | case kHealthCheckiPad: |
recotana | 0:84a3b029656e | 82 | { |
recotana | 0:84a3b029656e | 83 | d=kHealthCheckiPad; |
recotana | 0:84a3b029656e | 84 | udpSend(&d, 1); |
recotana | 0:84a3b029656e | 85 | DBGV("return send mbed checker to iPad %d\n",checkiPadRecCount++); |
recotana | 0:84a3b029656e | 86 | } |
recotana | 0:84a3b029656e | 87 | break; |
recotana | 0:84a3b029656e | 88 | |
recotana | 0:84a3b029656e | 89 | case kHealthCheckmbed: |
recotana | 0:84a3b029656e | 90 | { |
recotana | 0:84a3b029656e | 91 | healthiPad=false; |
recotana | 0:84a3b029656e | 92 | DBGV("recieve mbed checker %d\n",checkMbedSendCount++); |
recotana | 0:84a3b029656e | 93 | } |
recotana | 0:84a3b029656e | 94 | break; |
recotana | 0:84a3b029656e | 95 | } |
recotana | 0:84a3b029656e | 96 | |
recotana | 0:84a3b029656e | 97 | return; |
recotana | 0:84a3b029656e | 98 | } |
recotana | 0:84a3b029656e | 99 | |
recotana | 0:84a3b029656e | 100 | |
recotana | 0:84a3b029656e | 101 | Payload pl; |
recotana | 0:84a3b029656e | 102 | memcpy( &pl , buff , sizeof(Payload) ); |
recotana | 0:84a3b029656e | 103 | |
recotana | 1:3a46d2725374 | 104 | // dumpPayload(&pl); |
recotana | 0:84a3b029656e | 105 | |
recotana | 0:84a3b029656e | 106 | networkToPayload(&pl); |
recotana | 0:84a3b029656e | 107 | |
recotana | 0:84a3b029656e | 108 | netRouting(&pl); |
recotana | 0:84a3b029656e | 109 | } |
recotana | 0:84a3b029656e | 110 | } |
recotana | 0:84a3b029656e | 111 | |
recotana | 0:84a3b029656e | 112 | |
recotana | 0:84a3b029656e | 113 | void recieveDump(char *data , int length){ |
recotana | 0:84a3b029656e | 114 | pc.printf("size = %d\n -- -id\n",length); |
recotana | 0:84a3b029656e | 115 | |
recotana | 0:84a3b029656e | 116 | for(int i=0;i<16;i++) pc.printf("%d:%0x ",i,data[i]); |
recotana | 0:84a3b029656e | 117 | pc.printf("\n--- data\n"); |
recotana | 0:84a3b029656e | 118 | |
recotana | 0:84a3b029656e | 119 | int count=0; |
recotana | 0:84a3b029656e | 120 | for(int i=0;i<(length-16);i++){ |
recotana | 0:84a3b029656e | 121 | pc.printf("%02d:%02x ",i,data[16+i]); |
recotana | 0:84a3b029656e | 122 | if( count==15 ) {count=0; pc.printf("\n");} |
recotana | 0:84a3b029656e | 123 | else count++; |
recotana | 0:84a3b029656e | 124 | } |
recotana | 0:84a3b029656e | 125 | |
recotana | 0:84a3b029656e | 126 | pc.printf("\n---frame end\n"); |
recotana | 0:84a3b029656e | 127 | } |
recotana | 0:84a3b029656e | 128 |