_mu ki / mbedServer_v3

Description: 【背景】 「ネットワークが物理的に接続されていることを確かめたい.」というニーズで製作した.. 【動作】 mbedのローカルに保存されたIPアドレスに,他のPC等からpingを打つと,mbedはpong返す. ファイルに書かれた各アドレスは,外部のボタン(設定,左,下,上,右)で編集可能である. ただし,編集後のIPアドレスでpingを返すためには,mbedを再起動させる必要がある.(なぜか) 【ハードウェア】 ☆board Orange の他に,タクトスイッチをプルアップで5つ使用している.

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 //#include "HTTPServer.h"
00004 #include "TextLCD.h"
00005 #include "mbedServer.h"
00006 
00007 EthernetNetIf *eth;
00008 IpAddr ipaddr, netmask, gateway, nameserver;
00009 
00010 DigitalOut led1(LED1);
00011 DigitalOut led2(LED2);
00012 DigitalOut power(p10);
00013 
00014 
00015 TextLCD lcd(p24, p26, p27, p28, p29, p30); // rs, e, d0-d3 
00016 LocalFileSystem local("local");
00017 
00018 char word[4][16] = {"IPAddress","NetworkMask", "Gateway", "DomainName"}; 
00019 int ipe[4][12]={{0},{0},{0},{0}};
00020 
00021 
00022 void ipe_print(int mode){
00023     lcd.locate(0,1);
00024     lcd.printf("%1d%1d%1d.%1d%1d%1d.%1d%1d%1d.%1d%1d%1d",
00025     ipe[mode][0],ipe[mode][1],ipe[mode][2],
00026     ipe[mode][3],ipe[mode][4],ipe[mode][5],
00027     ipe[mode][6],ipe[mode][7],ipe[mode][8],
00028     ipe[mode][9],ipe[mode][10],ipe[mode][11]);
00029 };
00030 
00031 
00032 void ipe_input(int mode, int ip0, int ip1, int ip2, int ip3){
00033     int tmp[4]={ip0, ip1, ip2, ip3};
00034     
00035     ipe[mode][2] = tmp[0]%10;   tmp[0]=tmp[0]/10; // 1keta
00036     ipe[mode][1] = tmp[0]%10;   tmp[0]=tmp[0]/10; // 2keta
00037     ipe[mode][0] = tmp[0]%10;   tmp[0]=tmp[0]/10; // 3keta
00038 
00039     ipe[mode][5] = tmp[1]%10;   tmp[1]=tmp[1]/10; // 1keta
00040     ipe[mode][4] = tmp[1]%10;   tmp[1]=tmp[1]/10; // 2keta
00041     ipe[mode][3] = tmp[1]%10;   tmp[1]=tmp[1]/10; // 3keta
00042 
00043     ipe[mode][8] = tmp[2]%10;   tmp[2]=tmp[2]/10; // 1keta
00044     ipe[mode][7] = tmp[2]%10;   tmp[2]=tmp[2]/10; // 2keta
00045     ipe[mode][6] = tmp[2]%10;   tmp[2]=tmp[2]/10; // 3keta
00046 
00047     ipe[mode][11] = tmp[3]%10;   tmp[3]=tmp[3]/10; // 1keta
00048     ipe[mode][10] = tmp[3]%10;   tmp[3]=tmp[3]/10; // 2keta
00049     ipe[mode][9] = tmp[3]%10;   tmp[3]=tmp[3]/10; // 3keta
00050     
00051 }
00052 
00053 
00054 int config_write(char filename[32]){
00055     FILE *conf_fp;
00056     int i, j;
00057     int ip;
00058     char str[32];
00059 
00060     sprintf(str,"/local/%s",filename);
00061     conf_fp = fopen(str,"w");    
00062     
00063     if(NULL == conf_fp){
00064         lcd.printf("File access error");
00065         return -1;
00066     }
00067 
00068     for(i=0; i<4; i++){
00069         fprintf(conf_fp,"%s ",word[i]);
00070 
00071         for(j=0; j<12; j= j+3){
00072             ip = 100*ipe[i][j] + 10*ipe[i][j+1] + ipe[i][j+2];
00073             fprintf(conf_fp,"%3d,",ip);
00074         }
00075         fprintf(conf_fp,"\n");
00076     }
00077   
00078     fclose(conf_fp);
00079     return 0;    
00080 }
00081 
00082 
00083 
00084 int config_read(char filename[32]){
00085     FILE *conf_fp;
00086     char buf[80], name[16];
00087     char str[32];
00088     int ip0, ip1, ip2, ip3;
00089 
00090     sprintf(str,"/local/%s",filename);
00091     conf_fp = fopen(str,"r");    
00092 
00093     if(NULL == conf_fp){
00094         lcd.printf("File access error");
00095         wait(2);
00096         return -1;
00097     }else{;}
00098 
00099     while(fgets(buf,sizeof(buf),conf_fp) != NULL){
00100          lcd.cls(); //debug
00101         if(sscanf(buf, "%s %d,%d,%d,%d", name, &ip0, &ip1, &ip2, &ip3) == 0){
00102             lcd.printf("error...");
00103             return -1;
00104         }else{;}
00105         
00106         if(strcmp(name,word[0])==0){
00107             /* debug */
00108             lcd.printf("IP Address\n");
00109             lcd.printf("%d.%d.%d.%d.",ip0, ip1, ip2, ip3);
00110             ipaddr = IpAddr(ip0, ip1, ip2, ip3);
00111             ipe_input( 0, ip0, ip1, ip2, ip3);
00112         }else{;}
00113 
00114         if(strcmp(name,word[1])==0){
00115             /* debug */
00116             lcd.printf("NetworkMask\n");
00117             lcd.printf("%d.%d.%d.%d.",ip0, ip1, ip2, ip3);
00118             netmask = IpAddr(ip0, ip1, ip2, ip3);
00119             ipe_input( 1, ip0, ip1, ip2, ip3);
00120         }else{;}
00121 
00122         if(strcmp(name,word[2])==0){
00123             /* debug */
00124             lcd.printf("Gateway\n");
00125             lcd.printf("%d.%d.%d.%d.",ip0, ip1, ip2, ip3);
00126             gateway = IpAddr(ip0, ip1, ip2, ip3);
00127             ipe_input( 2, ip0, ip1, ip2, ip3);
00128 
00129         }else{;}
00130 
00131         if(strcmp(name,word[3])==0){
00132             /* debug */
00133             lcd.printf("Domain Name\n");
00134             lcd.printf("%d.%d.%d.%d.",ip0, ip1, ip2, ip3);
00135             nameserver = IpAddr(ip0, ip1, ip2, ip3);
00136             ipe_input( 3, ip0, ip1, ip2, ip3);
00137         }else{;}
00138     };
00139 
00140     fclose(conf_fp);   
00141     return 0;    
00142 }
00143 
00144 
00145 int ip_setup(){            
00146     lcd.cls();
00147     lcd.printf("Setup...\n");
00148     /* debug */
00149     lcd.cls();    lcd.locate(0,0);
00150     lcd.printf("ipaddr\n");
00151     lcd.printf("%3d,%3d,%3d,%3d", (int)ipaddr[0], (int)ipaddr[1], (int)ipaddr[2], (int)ipaddr[3]);
00152     wait(2);
00153     lcd.cls();    lcd.locate(0,0);
00154     lcd.printf("netmask\n");
00155     lcd.printf("%3d,%3d,%3d,%3d", (int)netmask[0], (int)netmask[1], (int)netmask[2], (int)netmask[3]);
00156     wait(2);
00157     lcd.cls();    lcd.locate(0,0);
00158     lcd.printf("gateway\n");
00159     lcd.printf("%3d,%3d,%3d,%3d", (int)gateway[0], (int)gateway[1], (int)gateway[2], (int)gateway[3]);
00160     wait(2);
00161     lcd.cls();    lcd.locate(0,0);
00162     lcd.printf("nameserver\n");
00163     lcd.printf("%3d,%3d,%3d,%3d", (int)nameserver[0], (int)nameserver[1], (int)nameserver[2], (int)nameserver[3]);
00164     wait(2);
00165 
00166 
00167     if (ipaddr[0] == 255) {
00168         // dhcp ip address
00169         eth = new EthernetNetIf;
00170     }else{
00171         // static ip address
00172         eth = new EthernetNetIf(ipaddr, netmask, gateway, nameserver);      
00173     }
00174     EthernetErr ethErr = eth->setup();
00175 
00176     if(ethErr){
00177         lcd.cls();
00178         lcd.printf("Error %d in setup.", ethErr);
00179         return -1;
00180     }else{;}
00181     return 0;
00182 
00183 }
00184 
00185 
00186 int edit(){
00187     int mode = -1;
00188     int i = 0, j= 0;
00189     int mode_buf,i_buf;
00190 
00191     Leftkey.rise(&LeftClick);
00192     Downkey.rise(&DownClick);
00193     Upkey.rise(&UpClick);
00194     Rightkey.rise(&RightClick);
00195 
00196 
00197     while(Set.num > 0 ){
00198         mode_buf = mode;
00199         i_buf = i;
00200 
00201         lcd.cls();
00202         lcd.locate(15,0); lcd.printf("%d",Set.num);
00203         mode = Set.num - 1;
00204         if(mode != mode_buf){
00205             Right.num = 0;
00206             Left.num = 0;       
00207         }else{;}
00208         
00209         i = (Right.num - Left.num)%12;
00210         if(i < 0){
00211             i = 12 +i;
00212         }else{;}
00213         if(mode != mode_buf || i != i_buf){
00214             Down.num =0;
00215             Up.num = ipe[mode][i]; 
00216         }else{;}
00217 
00218         j = (Up.num - Down.num)%10;
00219         if(j < 0){
00220             j = 10 +j;
00221         }else{;}
00222  
00223         if(mode == 0){
00224                     lcd.locate(0,0);  lcd.printf("IP Address");
00225                     ipe[mode][i] = j;
00226         }else if(mode == 1){
00227                     lcd.locate(0,0);  lcd.printf("Network Mask");
00228                     ipe[mode][i] = j;
00229         }else if(mode == 2){
00230                     lcd.locate(0,0);  lcd.printf("Gateway");
00231                     ipe[mode][i] = j;
00232         }else if(mode == 3){
00233                     lcd.locate(0,0);  lcd.printf("Domain Name");
00234                     ipe[mode][i] = j;
00235         }else{
00236             Set.num = 0;
00237             break;
00238         }
00239         
00240         ipe_print(mode);   
00241         wait(0.1);   
00242    }
00243     /* debug */
00244     lcd.locate(0,0);  lcd.printf("Edit done");
00245     wait(1);
00246     return 0;
00247 }
00248 
00249 
00250 int main() {
00251     int i=0;
00252     char filename[32];
00253 
00254 
00255     lcd.printf("Setting up...");
00256     Set.num = 0;
00257     Left.num = 0;
00258     Right.num = 0;
00259     Down.num = 0;
00260     Up.num = 0;
00261     power = 1;
00262 
00263     sprintf(filename,"IPCONFIG.TXT");
00264     if(config_read(filename) != 0){
00265         return -1;
00266     };
00267 
00268     if(ip_setup() != 0){
00269         return -1;
00270     }else{;}
00271 
00272     lcd.cls();
00273     lcd.printf("Setup OK");
00274  
00275     IpAddr ip = eth->getIp() ;
00276  
00277     lcd.cls();
00278     lcd.printf("IP Address\n");
00279     lcd.printf("%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);
00280 
00281     Setkey.rise(&SetClick); 
00282 
00283   //Listen indefinitely
00284     do{
00285         Net::poll();
00286         if(i>5){
00287             led1=!led1; //Show that we are alive
00288             i = 0;
00289         }else{
00290             i++;
00291         }
00292 
00293         /* Settei mode*/
00294         if(Set.num >= 1){
00295             led2 = 1;
00296             if(edit()!= 0){
00297                 return -1;
00298             }else{;}
00299             
00300             if(config_write(filename) != 0){
00301                 return -1;
00302             }else{;}
00303             
00304             break;
00305         }
00306         wait(0.1);
00307     }while(1);
00308 
00309     led2 = 0;
00310     lcd.cls(); lcd.locate(0,0);
00311     lcd.printf("Please restart.\n");
00312 
00313     return 0;
00314 }