UDPversion
Dependencies: XBee mbed NetServicesMin
tcpip/tcp_client.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_error.h" |
recotana | 0:84a3b029656e | 5 | #include "tcp_client.h" |
recotana | 0:84a3b029656e | 6 | #include "tcp_server.h" |
recotana | 0:84a3b029656e | 7 | |
recotana | 0:84a3b029656e | 8 | #include "leds.h" |
recotana | 0:84a3b029656e | 9 | extern Serial pc; |
recotana | 0:84a3b029656e | 10 | |
recotana | 0:84a3b029656e | 11 | Host iPadserver; |
recotana | 0:84a3b029656e | 12 | |
recotana | 0:84a3b029656e | 13 | extern UDPSocket sock; |
recotana | 0:84a3b029656e | 14 | |
recotana | 0:84a3b029656e | 15 | extern IpAddr inCommingIp; |
recotana | 0:84a3b029656e | 16 | extern PrefServer pref; |
recotana | 0:84a3b029656e | 17 | |
recotana | 0:84a3b029656e | 18 | extern bool healthiPad; |
recotana | 0:84a3b029656e | 19 | bool isSend; |
recotana | 0:84a3b029656e | 20 | |
recotana | 0:84a3b029656e | 21 | |
recotana | 0:84a3b029656e | 22 | ConnectStatus iPadServerStatus; |
recotana | 0:84a3b029656e | 23 | |
recotana | 0:84a3b029656e | 24 | Timer checkTimer; |
recotana | 0:84a3b029656e | 25 | Ticker clientCheck; |
recotana | 0:84a3b029656e | 26 | |
recotana | 0:84a3b029656e | 27 | void clientInit(void){ |
recotana | 0:84a3b029656e | 28 | |
recotana | 0:84a3b029656e | 29 | iPadServerStatus=close; |
recotana | 0:84a3b029656e | 30 | |
recotana | 0:84a3b029656e | 31 | pref.destinationPort = kClientPort; |
recotana | 0:84a3b029656e | 32 | |
recotana | 0:84a3b029656e | 33 | uint8_t tmpip[4]={kClientIp}; |
recotana | 0:84a3b029656e | 34 | memcpy((void*)pref.destinationAddress,tmpip,4); |
recotana | 0:84a3b029656e | 35 | |
recotana | 0:84a3b029656e | 36 | 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) ; |
recotana | 0:84a3b029656e | 37 | healthiPad=true; |
recotana | 0:84a3b029656e | 38 | isSend=false; |
recotana | 0:84a3b029656e | 39 | |
recotana | 0:84a3b029656e | 40 | iPadserver = Host(IpAddr(kClientIp), kClientPort); |
recotana | 0:84a3b029656e | 41 | } |
recotana | 0:84a3b029656e | 42 | |
recotana | 0:84a3b029656e | 43 | |
recotana | 0:84a3b029656e | 44 | int udpSend(char *data,int length){ |
recotana | 0:84a3b029656e | 45 | |
recotana | 0:84a3b029656e | 46 | if(isSend) return -1; |
recotana | 0:84a3b029656e | 47 | |
recotana | 0:84a3b029656e | 48 | isSend = true; |
recotana | 0:84a3b029656e | 49 | |
recotana | 0:84a3b029656e | 50 | ledTcpTx(); |
recotana | 0:84a3b029656e | 51 | |
recotana | 0:84a3b029656e | 52 | int ret=sock.sendto(data, length, &iPadserver); |
recotana | 0:84a3b029656e | 53 | |
recotana | 0:84a3b029656e | 54 | isSend=false; |
recotana | 0:84a3b029656e | 55 | |
recotana | 0:84a3b029656e | 56 | return ret; |
recotana | 0:84a3b029656e | 57 | |
recotana | 0:84a3b029656e | 58 | } |
recotana | 0:84a3b029656e | 59 | |
recotana | 0:84a3b029656e | 60 | void checkConnect(void){ |
recotana | 1:3a46d2725374 | 61 | #ifdef DEBUG |
recotana | 0:84a3b029656e | 62 | static uint16_t checkSendCount=0; |
recotana | 1:3a46d2725374 | 63 | #endif |
recotana | 0:84a3b029656e | 64 | char d = kHealthCheckmbed; |
recotana | 0:84a3b029656e | 65 | |
recotana | 0:84a3b029656e | 66 | udpSend( &d , 1 ); |
recotana | 0:84a3b029656e | 67 | healthiPad=true; |
recotana | 0:84a3b029656e | 68 | DBGV("send mbed checker to iPad health check %d\n",checkSendCount++); |
recotana | 0:84a3b029656e | 69 | |
recotana | 0:84a3b029656e | 70 | } |
recotana | 0:84a3b029656e | 71 | |
recotana | 0:84a3b029656e | 72 | |
recotana | 0:84a3b029656e | 73 | |
recotana | 0:84a3b029656e | 74 | void connectCheckStart(void) |
recotana | 0:84a3b029656e | 75 | { |
recotana | 0:84a3b029656e | 76 | |
recotana | 0:84a3b029656e | 77 | clientCheck.attach(&checkConnect,kiPadServerCheckInterval); |
recotana | 0:84a3b029656e | 78 | } |
recotana | 0:84a3b029656e | 79 | |
recotana | 0:84a3b029656e | 80 | |
recotana | 0:84a3b029656e | 81 | |
recotana | 0:84a3b029656e | 82 | int udpSendPayload(Payload *_payload) |
recotana | 0:84a3b029656e | 83 | { |
recotana | 0:84a3b029656e | 84 | uint8_t tmp[_payload->size]; |
recotana | 0:84a3b029656e | 85 | |
recotana | 0:84a3b029656e | 86 | memcpy(tmp,(uint8_t*)_payload,_payload->size); |
recotana | 0:84a3b029656e | 87 | |
recotana | 0:84a3b029656e | 88 | payloadToNetwork((Payload *)tmp); |
recotana | 0:84a3b029656e | 89 | |
recotana | 0:84a3b029656e | 90 | return udpSend((char*)tmp,_payload->size); |
recotana | 0:84a3b029656e | 91 | } |
recotana | 0:84a3b029656e | 92 | |
recotana | 0:84a3b029656e | 93 |