mbed Phone Platform

Dependencies:   ulaw mbed ConfigFile

Files at this revision

API Documentation at this revision

Comitter:
okini3939
Date:
Fri Jan 21 16:06:15 2011 +0000
Parent:
5:30e2847d241b
Commit message:

Changed in this revision

ConfigFile.lib Show annotated file Show diff for this revision Revisions of this file
IpLine.cpp Show annotated file Show diff for this revision Revisions of this file
IpLine.h Show annotated file Show diff for this revision Revisions of this file
Line.cpp Show annotated file Show diff for this revision Revisions of this file
Line.h Show annotated file Show diff for this revision Revisions of this file
config.cpp 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
phone.h Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ConfigFile.lib	Fri Jan 21 16:06:15 2011 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/shintamainjp/code/ConfigFile/#f6ceafabe9f8
--- a/IpLine.cpp	Thu Jan 20 13:50:10 2011 +0000
+++ b/IpLine.cpp	Fri Jan 21 16:06:15 2011 +0000
@@ -8,25 +8,18 @@
 #define NET_TIMEOUT (FREQ * 3)
 #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)
+IpLine::IpLine (EthernetNetIf *p_eth,  AnalogOut p_dac, AnalogIn p_adc) : dac(p_dac), adc(p_adc), dial(DIAL_SIZE), dabuf(DATA_SIZE * 4), adbuf(DATA_SIZE * 2)
 , led_y(p25), led_g(p26), eth_link(P1_25), eth_speed(P1_26) {
-    EthernetErr r;
 
     mode = ModeOff;
     status = StatusNone;
     timeout = 0;
     packet_num = 1;
     dataskip = 0;
+    timerled = 0;
 
-    // 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));
+    eth = p_eth;
     led_g = eth_link ? 0 : 1;
-    r = eth->setup();
-    if (r) {
-        printf("Error %d in setup.\n", r);
-        return;
-    }
 
     udpsock = new UDPSocket;
     udpsock->setOnEvent(this, &IpLine::onLisnerEvent);
@@ -54,7 +47,7 @@
                 if (! dabuf.get(c)) {
                     dac.write_u16(ulaw2pcm(c));
                 } else {
-                    dac.write_u16(gaussian());
+                    //dac.write_u16(gaussian());
                 }
                 dataskip = 1;
             }
@@ -65,11 +58,16 @@
         }
 
         // adc
-        adbuf.put(pcm2ulaw(adc.read_u16()));
-//        adbuf.put(adc.read_u16() >> 8);
+        if (wait) {
+            wait --;
+        } else {
+            adbuf.put(pcm2ulaw(adc.read_u16()));
+//            adbuf.put(adc.read_u16() >> 8);
+        }
     }
 
     if (timeout > 1) timeout --;
+    if (timerled > 0) timerled --;
 }
 
 /// network
@@ -78,7 +76,7 @@
     Net::poll();
 
     led_g = eth_link ? 0 : 1;
-    led_y = 0;
+    if (! timerled) led_y = 0;
 
     if (mode == ModeTalk && adbuf.use() >= DATA_SIZE) {
         // send
@@ -117,8 +115,9 @@
     if (e != UDPSOCKET_READABLE) return;
 
     // get data
-    led_g = 0;
     len = udpsock->recvfrom((char *)&packet, sizeof(struct ipline_packet), &recv);
+    led_y = 1;
+    timerled = FREQ / 10;
 
     if (packet.header.ident != PACKET_IDENT) return;
 
@@ -146,8 +145,7 @@
         case ModeRing:
             // ring --> onhook, dial
             if (mode == ModeReady) {
-                dial.put(1);
-                dial.put(10);
+                dial.put(11);
                 dial.put(packet.header.target);
                 dial.put(12);
                 hook = HookOn;
@@ -174,11 +172,12 @@
 
         // send ack
         send(&packet.header);
-/*
+
       for (int i = 0; i < len; i ++) {
         printf(" %02x", ((char *)&packet)[i]);
       }
-*/
+      printf("\r\n");
+
     }
 }
 
@@ -187,6 +186,7 @@
 void IpLine::send (struct ipline_header *header) {
 
     led_y = 1;
+    timerled = FREQ / 10;
     header->ident = PACKET_IDENT;
     if (! header->num) {
         header->num = packet_num;
@@ -201,6 +201,7 @@
 void IpLine::send (struct ipline_packet *packet) {
 
     led_y = 1;
+    timerled = FREQ / 10;
     packet->header.ident = PACKET_IDENT;
     if (! packet->header.num) {
         packet->header.num = packet_num;
@@ -214,6 +215,8 @@
 int IpLine::enter (enum Mode newmode) {
     struct ipline_header header;
 
+    if (eth_link) return -1; // link down
+
     mode = newmode;
     status = StatusOk;
 
@@ -222,6 +225,7 @@
         hook = HookOff;
         adbuf.clear();
         dabuf.clear();
+        wait = FREQ / 2;
         break;
 
     case ModeBT:
--- a/IpLine.h	Thu Jan 20 13:50:10 2011 +0000
+++ b/IpLine.h	Fri Jan 21 16:06:15 2011 +0000
@@ -25,7 +25,7 @@
  */
 class IpLine {
 public:
-    IpLine (AnalogOut, AnalogIn);
+    IpLine (EthernetNetIf *, AnalogOut, AnalogIn);
 
     void intr ();
     void poll ();
@@ -34,10 +34,10 @@
     void settarget (enum PhoneType, char *);
 
 private:
-    enum PhoneType remotetarget;
-    enum Mode mode;
-    enum Status status;
-    int hook, packet_num, dataskip, timeout, dialconut;
+    volatile enum PhoneType remotetarget;
+    volatile enum Mode mode;
+    volatile enum Status status;
+    volatile int hook, packet_num, dataskip, timeout, dialconut, wait, timerled;
     EthernetNetIf *eth;
     UDPSocket *udpsock;
     Host remote;
--- a/Line.cpp	Thu Jan 20 13:50:10 2011 +0000
+++ b/Line.cpp	Fri Jan 21 16:06:15 2011 +0000
@@ -28,6 +28,7 @@
     dialcount = 0;
     hooktimer = 0;
     tonecount = 0;
+    hooktimer2 = 0;
     hook_last = hook;
 }
 
@@ -59,14 +60,16 @@
         // off hook
         if (hooktimer) hooktimer --;
 
-        if (! hook_last) {
+        if (! hook_last && (dialcount > 0 || hooktimer2 >= DIAL_TIME)) {
             // dial trigger
             dialtimer = DIAL_TIME;
             dialcount ++;
+            hooktimer2 = 0;
         }
     } else {
         // on hook
         hooktimer = HOOK_TIME;
+        hooktimer2 ++;
     }
     hook_last = hook;
 
--- a/Line.h	Thu Jan 20 13:50:10 2011 +0000
+++ b/Line.h	Fri Jan 21 16:06:15 2011 +0000
@@ -15,10 +15,10 @@
     int scan (enum Scan);
 
 private:
-    enum Mode mode;
-    enum Status status;
-    int dialtimer, dialcount, hooktimer, tonecount;
-    int hook_last;
+    volatile enum Mode mode;
+    volatile enum Status status;
+    volatile int dialtimer, dialcount, hooktimer, tonecount, hooktimer2;
+    volatile int hook_last;
     DigitalOut line, xline;
     DigitalIn hook;
     AnalogOut dac;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/config.cpp	Fri Jan 21 16:06:15 2011 +0000
@@ -0,0 +1,88 @@
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "ConfigFile.h"
+#include "phone.h"
+
+LocalFileSystem local("local");
+
+extern EthernetNetIf *eth;
+extern int useipline;
+extern struct PhoneBook phonebook[];
+
+char* chop (char *s) {
+    int i;
+
+    for (i = strlen(s) - 1; i >= 0; i --) {
+        if (s[i] == ' ' || s[i] == '\n' || s[i] == '\r') {
+            s[i] = 0;
+        } else {
+            break;
+        }
+    }
+    return s;
+}
+
+int config () {
+    int i, j;
+    ConfigFile cfg;
+    char buf[80], key[20];
+    int ip0, ip1, ip2, ip3;
+    EthernetErr r;
+    IpAddr ipaddr, netmask, gateway, nameserver; 
+
+    if (! cfg.read("/local/phone.cfg")) {
+printf("config err\r\n");
+        return -1;
+    }
+
+    if (cfg.getValue("IPADDRESS", buf, sizeof(buf))) {
+        chop(buf);
+        if (strcmp(buf, "DHCP") == 0) {
+            eth = new EthernetNetIf;
+        } else {
+            sscanf(buf, "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
+            ipaddr = IpAddr(ip0, ip1, ip2, ip3);
+            if (cfg.getValue("NETMASK", buf, sizeof(buf))) {
+                sscanf(chop(buf), "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
+                netmask = IpAddr(ip0, ip1, ip2, ip3);
+            }
+            if (cfg.getValue("GATEWAY", buf, sizeof(buf))) {
+                sscanf(chop(buf), "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
+                gateway = IpAddr(ip0, ip1, ip2, ip3);
+            }
+            if (cfg.getValue("NAMESERVER", buf, sizeof(buf))) {
+                sscanf(chop(buf), "%d.%d.%d.%d", &ip0, &ip1, &ip2, &ip3);
+                nameserver = IpAddr(ip0, ip1, ip2, ip3);
+            }
+            eth = new EthernetNetIf(ipaddr, netmask, gateway, nameserver);
+        }
+        r = eth->setup();
+        if (! r) {
+            useipline = 1;
+        }
+    }
+
+    for (i = 0; i < PB_SIZE; i ++) {
+        sprintf(key, "DIAL[%d]", i);
+        if (cfg.getValue(key, buf, sizeof(buf))) {
+            for (j = 0; j < strlen(buf) && j < DIAL_SIZE; j ++) {
+                if (buf[j] == '0') {
+                    phonebook[i].dial[j] = 10;
+                } else
+                if (buf[j] >= '1' && buf[j] <= '9') {
+                    phonebook[i].dial[j] = buf[j] - '0';
+                }
+            }
+            phonebook[i].dial[j] = 0;
+        }
+        sprintf(key, "TYPE[%d]", i);
+        if (cfg.getValue(key, buf, sizeof(buf))) {
+            phonebook[i].target = (enum PhoneType)atoi(buf);
+        }
+        sprintf(key, "ADDR[%d]", i);
+        cfg.getValue(key, phonebook[i].hostname, sizeof(phonebook[i].hostname));
+    }
+    
+    return 0;
+}
+    
\ No newline at end of file
--- a/main.cpp	Thu Jan 20 13:50:10 2011 +0000
+++ b/main.cpp	Fri Jan 21 16:06:15 2011 +0000
@@ -9,10 +9,13 @@
  */
 
 #include "mbed.h"
+#include "EthernetNetIf.h"
 #include "phone.h"
 #include "Line.h"
 #include "IpLine.h"
 
+EthernetNetIf *eth;
+
 Serial pc(USBTX, USBRX);
 Ticker ticker;
 
@@ -23,32 +26,19 @@
 DigitalOut mixlocal(p21), mixline(p22), micsp(p23);
 Line line1(p12, p13, p11, dac);
 Line line2(p14, p15, p16, dac);
-IpLine ipline(dac, adc);
-
-volatile int timeout;
-int dialcount;
-char dial[DIAL_SIZE];
-enum PhoneType activesrc, activedest;
+IpLine *ipline;
 
-struct PhoneBook phonebook[PB_SIZE] = {
-    {{1, 10, 1}, PhoneLine1, ""},
-    {{1, 10, 2}, PhoneLine2, ""},
-    {{1, 10, 3}, PhoneMicSp, ""},
-    {{2, 10, 1}, PhoneLine1, "192.168.1.2"},
-    {{2, 10, 2}, PhoneLine2, "192.168.1.2"},
-    {{2, 10, 3}, PhoneMicSp, "192.168.1.2"}
-/*
-    {{2, 10, 1}, PhoneLine1, "192.168.10.100"},
-    {{2, 10, 2}, PhoneLine2, "192.168.10.100"},
-    {{2, 10, 3}, PhoneMicSp, "192.168.10.100"}
-*/
-};
-
+volatile int timeout, dialcount, useipline;
+volatile enum PhoneType activesrc, activedest;
+char dial[DIAL_SIZE];
+struct PhoneBook phonebook[PB_SIZE];
 
 void int_sample () {
     line1.intr();
     line2.intr();
-    ipline.intr();
+    if (useipline) {
+        ipline->intr();
+    }
 
     if (timeout) timeout --;
 }
@@ -56,15 +46,21 @@
 int getpb (enum PhoneType *target, char *hostname) {
     int i, j;
 
+    if (dial[0] == 11 && dial[1] >= 1 && dial[1] <= 3) {
+        *target = (enum PhoneType)dial[1];
+        hostname[0] = 0;
+        return 1;
+    }
+
     for (i = 0; i < PB_SIZE; i ++) {
         for (j = 0; j < DIAL_SIZE; j ++) {
-            if (phonebook[i].dial[j] == 0 || dial[j] == 0 ||
-              j >= dialcount || phonebook[i].dial[j] != dial[j]) break;
-            if (j == dialcount - 1) {
+            if (phonebook[i].dial[j] == 0 && dial[j] == 0) {
                 *target = phonebook[i].target;
                 strncpy(hostname, phonebook[i].hostname, HOSTNAME_SIZE);
                 return 1;
             }
+            if (phonebook[i].dial[j] == 0 || dial[j] == 0 ||
+              j >= dialcount || phonebook[i].dial[j] != dial[j]) break;
         }
     }
 
@@ -105,7 +101,7 @@
         return micsp.enter(mode);
 */
     case PhoneIpLine:
-        return ipline.enter(mode);
+        return ipline->enter(mode);
     }
     return 0;
 }
@@ -122,7 +118,7 @@
         return micsp.scan(type);
 */
     case PhoneIpLine:
-        return ipline.scan(type);
+        return ipline->scan(type);
     }
     return 0;
 }
@@ -162,12 +158,15 @@
             char buf[HOSTNAME_SIZE];
             enum PhoneType p;
             // call
+            dial[dialcount] = 0;
             if (getpb(&p, buf)) {
+printf("-> %d %s\r\n", p, buf);
                 if (buf[0] == 0) {
                     activedest = p;
-                } else {
+                } else
+                if (useipline) {
                     activedest = PhoneIpLine;
-                    ipline.settarget(p, buf);
+                    ipline->settarget(p, buf);
                 }
                 enterline(num, ModeCall);
                 enterline(activedest, ModeRing);
@@ -257,20 +256,29 @@
     int i;
     
     timeout = 0;
+    useipline = 0;
     dialcount = 0;
     activesrc = PhoneNone;
     activedest = PhoneNone;
+
+    config();
+
     line1.enter(ModeReady);
     line2.enter(ModeReady);
-    ipline.enter(ModeReady);
+    if (useipline) {
+        ipline = new IpLine(eth, dac, adc);
+        ipline->enter(ModeReady);
+    }
 
 //    NVIC_SetPriority(TIMER3_IRQn, 0); // preemption=1, sub-priority=1
     ticker.attach_us(&int_sample, 1000000 / FREQ);
 
     for (;;) {
-        led1 = 1;
-        ipline.poll();
-        led1 = 0;
+        if (useipline) {
+            led1 = 1;
+            ipline->poll();
+            led1 = 0;
+        }
         led2 = 1;
         line1.poll();
         led2 = 0;
@@ -288,10 +296,12 @@
         if (i != scanline(PhoneLine2, ScanMode))
             pc.printf("(2) %d -> %d\r\n", i, scanline(PhoneLine2, ScanMode));
         
-        i = scanline(PhoneIpLine, ScanMode);
-        checkline(PhoneIpLine);
-        if (i != scanline(PhoneIpLine, ScanMode))
-            pc.printf("(3) %d -> %d\r\n", i, scanline(PhoneIpLine, ScanMode));
+        if (useipline) {
+            i = scanline(PhoneIpLine, ScanMode);
+            checkline(PhoneIpLine);
+            if (i != scanline(PhoneIpLine, ScanMode))
+                pc.printf("(3) %d -> %d\r\n", i, scanline(PhoneIpLine, ScanMode));
+        }
         
     }
 }
--- a/phone.h	Thu Jan 20 13:50:10 2011 +0000
+++ b/phone.h	Fri Jan 21 16:06:15 2011 +0000
@@ -86,4 +86,6 @@
 #define HookOff 0
 #define HookOn 1
 
+int config ();
+
 #endif
\ No newline at end of file