Asa Ewing / Mbed OS mbed-os-rest-api-V1-1

Dependencies:   CPU_Usage NetworkManager RestAPI_Manager

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #if !FEATURE_LWIP
00002     #error [NOT_SUPPORTED] LWIP not supported for this target
00003 #endif
00004 
00005 #include "module_for_all.h"
00006 
00007 #define PORT 8080
00008 
00009 #define IP1        "192.168.20.101"
00010 #define IP2        "192.168.20.102"
00011 #define GATEWAY    "192.168.20.1"
00012 #define MASK       "255.255.255.0"
00013 
00014 const char* ip = 0;
00015 const char* mac = 0;
00016 
00017 DigitalIn  IP_Select2(PG_2);
00018 
00019 NetworkManager  net_manager(IP1, GATEWAY, MASK, PORT);
00020 RestAPI_Manager restAPI_manager(IP1, "1");
00021 
00022 //NetworkManager  net_manager();
00023 //RestAPI_Manager restAPI_manager();
00024 
00025 Timer t;
00026 CPU_Usage cpu(t, 1);
00027 
00028 bool EventLoop() {
00029     uint8_t value = 0;
00030     while (true) {
00031         printf("$$ EventLoop New\r\n");
00032         LED_NetWait();
00033         
00034         net_manager.AcceptEvent();
00035         //printf("$$ EventLoop run Response\r\n");
00036         LED_NetProcess();
00037         
00038         std::string http_method = net_manager.getHttpMethod();
00039         std::string api_command = net_manager.getApiCommand();
00040         
00041         std::string response = restAPI_manager.runCommand(http_method, api_command);
00042         
00043         value = cpu.update();
00044         //printf("CPU %i", value);
00045         
00046         net_manager.SendResponseEvent(response);
00047         printf("$$ EventLoop Finish\r\n");
00048     }
00049     
00050     printf("$$ EventLoop ALL Finish\r\n");
00051     return false;
00052 }
00053 
00054 /* Main function */
00055 int main(){
00056     
00057     printf("mbed Power Start\r\n");
00058     LED_PowerStart();
00059     cpu.working();
00060     
00061     if (IP_Select2.read() == 1){
00062         net_manager.SetConfig(IP2, GATEWAY, MASK, PORT);
00063         restAPI_manager.SetConfig(IP2, "2");
00064         printf("mbed 02 Start\r\n");
00065 
00066     } else {
00067         net_manager.SetConfig(IP1, GATEWAY, MASK, PORT);
00068         restAPI_manager.SetConfig(IP1, "1");
00069         printf("mbed 01 Start\r\n");
00070     }
00071     
00072     net_manager.ServerInit();
00073     printf("ServerInit\r\n");
00074     
00075     if (net_manager.OpenServer()) return 0;
00076     printf("OpenServer\r\n");
00077     
00078     if (net_manager.BindPort()) return 0;
00079     printf("BindPort\r\n");
00080     
00081     if (net_manager.ListenConnections()) return 0;
00082     printf("ListenConnections\r\n");
00083     
00084     if (EventLoop()) {
00085         cpu.stopped();
00086         return 0;
00087     }
00088 }