Delta / Mbed 2 deprecated NNN40_APmodeToSTAmodeByHTTPServer

Dependencies:   WIFI_API_32kRAM mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetInterface.h"
00003 #include "mbed_rpc.h"
00004 #include "RPCCommand.h"
00005 #include "HTTPServer.h"
00006 #include "Formatter.h"
00007 #include "RequestHandler.h"
00008 #include "RPCType.h"
00009 
00010 #include "WIFIDevice.h"
00011 
00012 #define SERVER_PORT 80
00013 
00014 #define AP_MODE_SSID "AP_MODE_SSID"
00015 #define AP_MODE_PASSWORD "0123456789"
00016 
00017 WIFIDevice wifi;
00018 EthernetInterface eth;
00019 
00020 void DeltaRequestHandler::handle(const RPCCommand& cmd, char *reply)
00021 {
00022     char *pch;
00023 
00024     pch=strchr(cmd.get_func_name(),'/');
00025     cmd.get_func_name()[pch-cmd.get_func_name()] = '\0';
00026     
00027     //must call disconnect before switch between AP and STA mode
00028     eth.disconnect();
00029     
00030     wifi.setNetwork(cmd.get_func_name(),&(cmd.get_func_name()[pch-cmd.get_func_name()+1]),0);
00031     eth.init();
00032     printf("\n\rStart STA mode...\n");
00033     wait(1);
00034 
00035     if(eth.connect(40000) == 0)
00036         printf("AP connect OK...\n\r");
00037     else
00038         printf("AP connect FAIL...\n\r");
00039 
00040     printf("STA mode IP Address: %s\n\r",eth.getIPAddress());
00041     printf("STA mode MAC Address: %s\n\r",eth.getMACAddress());
00042     printf("STA mode Gateway Address: %s\n\r",eth.getGateway());
00043     printf("STA mode Network Mask: %s\n\r",eth.getNetworkMask());
00044 
00045     eth.disconnect();//must call disconnect before switch between AP and STA mod
00046 
00047     switch(cmd.get_type()) {
00048         case CREATE :
00049             printf("CREATE\n");
00050             putHandler.handle(cmd, reply);
00051             break;
00052         case DELETE :
00053             printf("CREATE\n");
00054             deleteHandler.handle(cmd, reply);
00055             break;
00056         case FUNCTION_CALL :
00057             printf("FUNCTION_CALL\n");
00058             getHandler.handle(cmd, reply);
00059             break;
00060         default :
00061            // printf("default\n");
00062             break;
00063     }
00064 
00065 }
00066 
00067 HTTPServer create_simple_server()
00068 {
00069     HTTPServer srv;
00070     srv.add_request_handler("DELETE", new DeleteRequestHandler());
00071     srv.add_request_handler("GET", new GetRequestHandler());
00072     srv.add_request_handler("PUT", new PutRequestHandler());
00073     return srv;
00074 }
00075 
00076 HTTPServer create_interactive_server()
00077 {
00078     HTTPServer srv(new InteractiveHTMLFormatter());
00079     srv.add_request_handler("GET", new ComplexRequestHandler());
00080     return srv;
00081 }
00082 
00083 HTTPServer create_delta_server()
00084 {
00085     HTTPServer srv(new DeltaWifiSettingHTMLFormatter());
00086     srv.add_request_handler("GET", new DeltaRequestHandler());
00087     return srv;
00088 }
00089 
00090 int main(void)
00091 {
00092     RPCType::instance().register_types();
00093     printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
00094     printf("\r*************** Program Start ***************\n\r");
00095 
00096     wifi.setAccessPoint(AP_MODE_SSID, AP_MODE_PASSWORD, SECURITY_WPA2_TKIP_PSK, 2);
00097     if(eth.init()) {
00098         printf("Error while initializing the ethernet interface.\n\r");
00099         return -1;
00100     }
00101     if(eth.connect(60000)) {
00102         printf("Error while starting the ethernet interface.\n\r");
00103         return -1;
00104     }
00105 
00106     printf("AP Mode IP Address is %s\n\r", eth.getIPAddress());
00107 
00108     HTTPServer srv = create_delta_server();
00109 
00110     if(!srv.init(SERVER_PORT)) {
00111         eth.disconnect();
00112         return -1;
00113     }
00114 
00115     srv.run();
00116     return 0;
00117 }