mbed Phone Platform
Dependencies: ulaw mbed ConfigFile
Diff: IpLine.cpp
- Revision:
- 5:30e2847d241b
- Parent:
- 4:41a3534d085f
- Child:
- 6:bd62b12de751
--- a/IpLine.cpp Fri Jan 07 18:20:41 2011 +0000 +++ b/IpLine.cpp Thu Jan 20 13:50:10 2011 +0000 @@ -9,7 +9,7 @@ #define PACKET_IDENT 0x6465626d // "mbed" IpLine::IpLine (AnalogOut p_dac, AnalogIn p_adc) : dac(p_dac), adc(p_adc), dial(DIAL_SIZE), dabuf(DATA_SIZE * 4), adbuf(DATA_SIZE * 2) -, led1(LED1), led2(LED2), led3(LED3), led4(LED4) { +, led_y(p25), led_g(p26), eth_link(P1_25), eth_speed(P1_26) { EthernetErr r; mode = ModeOff; @@ -21,6 +21,7 @@ // dhcp eth = new EthernetNetIf; // eth = new EthernetNetIf(IpAddr(192,168,10,200), IpAddr(255,255,255,0), IpAddr(0,0,0,0), IpAddr(0,0,0,0)); + led_g = eth_link ? 0 : 1; r = eth->setup(); if (r) { printf("Error %d in setup.\n", r); @@ -38,12 +39,12 @@ if (mode == ModeTalk && hook == HookOn) { // dac + if (dabuf.available() < DATA_SIZE) { // down sample dabuf.get(c); + dac.write_u16(ulaw2pcm(c)); dabuf.get(c); - dac.write_u16(ulaw2pcm(c)); -led1 = 0; led2 = 0; led3 = 1; } else if (dabuf.use() < DATA_SIZE) { // over sample @@ -57,34 +58,18 @@ } dataskip = 1; } -led1 = 1; led2 = 0; led3 = 0; } else { // normal dabuf.get(c); dac.write_u16(ulaw2pcm(c)); -led1 = 0; led2 = 1; led3 = 0; } // adc -// adbuf.put(pcm2ulaw(adc.read_u16())); - adbuf.put(adc.read_u16() >> 8); + adbuf.put(pcm2ulaw(adc.read_u16())); +// adbuf.put(adc.read_u16() >> 8); } - if (timeout) { - timeout --; - - if (timeout == 0) { - if (last.target) { - // re-try - send(&last); - timeout = NET_TIMEOUT; - last.target = PhoneNone; - } else { - // timeout - status = StatusNg; - } - } - } + if (timeout > 1) timeout --; } /// network @@ -92,6 +77,9 @@ Net::poll(); + led_g = eth_link ? 0 : 1; + led_y = 0; + if (mode == ModeTalk && adbuf.use() >= DATA_SIZE) { // send struct ipline_packet packet; @@ -105,6 +93,19 @@ send(&packet); } } + + if (timeout == 1) { + if (last.target != PhoneNone) { + // re-try + send(&last); + timeout = NET_TIMEOUT; + last.target = PhoneNone; + } else { + // timeout + status = StatusNg; + } + timeout = 0; + } } /// recv packet @@ -116,11 +117,11 @@ if (e != UDPSOCKET_READABLE) return; // get data + led_g = 0; len = udpsock->recvfrom((char *)&packet, sizeof(struct ipline_packet), &recv); if (packet.header.ident != PACKET_IDENT) return; -led4 = 1; if (packet.header.status != StatusNone) { // recv ack if (packet.header.num == last.num) { @@ -132,7 +133,9 @@ if (packet.header.mode == ModeData && len > sizeof(struct ipline_header)) { // data - dabuf.put(packet.data, packet.len); + if (packet.len <= len - sizeof(struct ipline_header) - 2) { + dabuf.put(packet.data, packet.len); + } } else if (len == sizeof(struct ipline_header)) { @@ -163,8 +166,6 @@ case ModeTalk: if (recv.getIp() == remote.getIp()) { - adbuf.clear(); - dabuf.clear(); hook = HookOn; packet.header.status = StatusOk; } @@ -173,20 +174,19 @@ // send ack send(&packet.header); - timeout = NET_RETRY; /* for (int i = 0; i < len; i ++) { printf(" %02x", ((char *)&packet)[i]); } */ } -led4 = 0; } /// send packet (header only) void IpLine::send (struct ipline_header *header) { + led_y = 1; header->ident = PACKET_IDENT; if (! header->num) { header->num = packet_num; @@ -195,16 +195,12 @@ } udpsock->sendto((char *)header, sizeof(struct ipline_header), &remote); memcpy(&last, header, sizeof(struct ipline_header)); - - for (int i = 0; i < sizeof(struct ipline_header); i ++) { - printf(" %02x", ((char *)header)[i]); - } - printf("(H)\r\n"); } /// send packet void IpLine::send (struct ipline_packet *packet) { + led_y = 1; packet->header.ident = PACKET_IDENT; if (! packet->header.num) { packet->header.num = packet_num; @@ -224,6 +220,8 @@ switch (mode) { case ModeReady: hook = HookOff; + adbuf.clear(); + dabuf.clear(); break; case ModeBT: @@ -231,8 +229,6 @@ hook = HookOff; case ModeRing: case ModeTalk: - adbuf.clear(); - dabuf.clear(); status = StatusNone; header.num = 0; header.target = remotetarget; @@ -303,5 +299,5 @@ j = j + (xor128() >> 16); } j = (j - 0x60000) & 0xffff; - return 0x3fff + (j >> 1); + return 0x3fff + (j >> 2); }