First
Dependencies: CPU_Usage NetworkManager RestAPI_Manager
Revision 0:8eda451f71fa, committed 2018-10-08
- Comitter:
- asaewing
- Date:
- Mon Oct 08 00:50:33 2018 +0000
- Commit message:
- First
Changed in this revision
diff -r 000000000000 -r 8eda451f71fa .gitignore --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/.gitignore Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,4 @@ +.build +.mbed +projectfiles +*.py*
diff -r 000000000000 -r 8eda451f71fa CPU_Usage.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/CPU_Usage.lib Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,1 @@ +http://os.mbed.com/users/dextorslabs/code/CPU_Usage/#807947fd7dde
diff -r 000000000000 -r 8eda451f71fa NetworkManager.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/NetworkManager.lib Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/asaewing/code/NetworkManager/#b12dd3a065c0
diff -r 000000000000 -r 8eda451f71fa README.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,57 @@ +# Getting started with Blinky on mbed OS + +This guide reviews the steps required to get Blinky working on an mbed OS platform. + +Please install [mbed CLI](https://github.com/ARMmbed/mbed-cli#installing-mbed-cli). + +## Import the example application + +From the command-line, import the example: + +``` +mbed import mbed-os-example-blinky +cd mbed-os-example-blinky +``` + +### Now compile + +Invoke `mbed compile`, and specify the name of your platform and your favorite toolchain (`GCC_ARM`, `ARM`, `IAR`). For example, for the ARM Compiler 5: + +``` +mbed compile -m K64F -t ARM +``` + +Your PC may take a few minutes to compile your code. At the end, you see the following result: + +``` +[snip] ++----------------------------+-------+-------+------+ +| Module | .text | .data | .bss | ++----------------------------+-------+-------+------+ +| Misc | 13939 | 24 | 1372 | +| core/hal | 16993 | 96 | 296 | +| core/rtos | 7384 | 92 | 4204 | +| features/FEATURE_IPV4 | 80 | 0 | 176 | +| frameworks/greentea-client | 1830 | 60 | 44 | +| frameworks/utest | 2392 | 512 | 292 | +| Subtotals | 42618 | 784 | 6384 | ++----------------------------+-------+-------+------+ +Allocated Heap: unknown +Allocated Stack: unknown +Total Static RAM memory (data + bss): 7168 bytes +Total RAM memory (data + bss + heap + stack): 7168 bytes +Total Flash memory (text + data + misc): 43402 bytes +Image: .\.build\K64F\ARM\mbed-os-example-blinky.bin +``` + +### Program your board + +1. Connect your mbed device to the computer over USB. +1. Copy the binary file to the mbed device. +1. Press the reset button to start the program. + +The LED on your platform turns on and off. + +## Troubleshooting + +If you have problems, you can review the [documentation](https://os.mbed.com/docs/latest/tutorials/debugging.html) for suggestions on what could be wrong and how to fix it.
diff -r 000000000000 -r 8eda451f71fa RestAPI_Manager.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RestAPI_Manager.lib Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/asaewing/code/RestAPI_Manager/#b3b078af7b16
diff -r 000000000000 -r 8eda451f71fa main.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,88 @@ +#if !FEATURE_LWIP + #error [NOT_SUPPORTED] LWIP not supported for this target +#endif + +#include "module_for_all.h" + +#define PORT 8080 + +#define IP1 "192.168.20.101" +#define IP2 "192.168.20.102" +#define GATEWAY "192.168.20.1" +#define MASK "255.255.255.0" + +const char* ip = 0; +const char* mac = 0; + +DigitalIn IP_Select2(PG_2); + +NetworkManager net_manager(IP1, GATEWAY, MASK, PORT); +RestAPI_Manager restAPI_manager(IP1, "1"); + +//NetworkManager net_manager(); +//RestAPI_Manager restAPI_manager(); + +Timer t; +CPU_Usage cpu(t, 1); + +bool EventLoop() { + uint8_t value = 0; + while (true) { + printf("$$ EventLoop New\r\n"); + LED_NetWait(); + + net_manager.AcceptEvent(); + //printf("$$ EventLoop run Response\r\n"); + LED_NetProcess(); + + std::string http_method = net_manager.getHttpMethod(); + std::string api_command = net_manager.getApiCommand(); + + std::string response = restAPI_manager.runCommand(http_method, api_command); + + value = cpu.update(); + //printf("CPU %i", value); + + net_manager.SendResponseEvent(response); + printf("$$ EventLoop Finish\r\n"); + } + + printf("$$ EventLoop ALL Finish\r\n"); + return false; +} + +/* Main function */ +int main(){ + + printf("mbed Power Start\r\n"); + LED_PowerStart(); + cpu.working(); + + if (IP_Select2.read() == 1){ + net_manager.SetConfig(IP2, GATEWAY, MASK, PORT); + restAPI_manager.SetConfig(IP2, "2"); + printf("mbed 02 Start\r\n"); + + } else { + net_manager.SetConfig(IP1, GATEWAY, MASK, PORT); + restAPI_manager.SetConfig(IP1, "1"); + printf("mbed 01 Start\r\n"); + } + + net_manager.ServerInit(); + printf("ServerInit\r\n"); + + if (net_manager.OpenServer()) return 0; + printf("OpenServer\r\n"); + + if (net_manager.BindPort()) return 0; + printf("BindPort\r\n"); + + if (net_manager.ListenConnections()) return 0; + printf("ListenConnections\r\n"); + + if (EventLoop()) { + cpu.stopped(); + return 0; + } +} \ No newline at end of file
diff -r 000000000000 -r 8eda451f71fa main.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.md Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,190 @@ +#if !FEATURE_LWIP + #error [NOT_SUPPORTED] LWIP not supported for this target +#endif + +#include "mbed.h" +#include "EthernetInterface.h" +#include "TCPServer.h" +#include "TCPSocket.h" + +#include <stdio.h> +#include <iostream> +#include <string.h> + +#define HTTP_STATUS_LINE "HTTP/1.0 200 OK" +#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8" +#define HTTP_MESSAGE_BODY "" \ +"<html>" "\r\n" \ +" <body style=\"display:flex;text-align:center\">" "\r\n" \ +" <div style=\"margin:auto\">" "\r\n" \ +" <h1>Hello ! mbed</h1>" "\r\n" \ +" <p>It works ! from mbed 01</p>" "\r\n" \ +" </div>" "\r\n" \ +" </body>" "\r\n" \ +"</html>" + +#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n" \ + HTTP_HEADER_FIELDS "\r\n" \ + "\r\n" \ + HTTP_MESSAGE_BODY "\r\n" + +#define PORT 8080 + +#define IP "192.168.0.101" +#define GATEWAY "192.168.0.1" +#define MASK "255.255.255.0" + +EthernetInterface eth; +TCPServer srv; +TCPSocket clt_sock; +SocketAddress clt_addr; + +const char *ip = 0; +const char *mac = 0; + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); + +void split(char **arr, char *str, const char *del) { + char *s = strtok(str, del); + while(s != NULL) { + *arr++ = s; + s = strtok(NULL, del); + } +} + +/* Define function */ +void ServerInit() { + eth.disconnect(); + eth.set_network(IP,MASK,GATEWAY); + eth.connect(); + + ip = eth.get_ip_address(); + mac = eth.get_mac_address(); + printf("mbed's IP address is: %s\n\r", ip ? ip : "No IP"); + printf("mbed's MAC address is: %s\n\r", mac ? mac : "No MAC"); +} + +bool OpenServer() { + /* Open the server on ethernet stack */ + int ii=srv.open(ð); + + if (ii<0) { + printf("open error: %i \r\n",ii); + return true; + } + printf("server open\r\n"); + return false; +} + +bool BindPort() { + /* Bind the HTTP port (TCP 80) to the server */ + int ii=srv.bind(ip, 80); + if (ii<0) { + printf("bind error: %i \r\n",ii); + return true; + } + printf("Bound\r\n"); + return false; +} + +bool ListenConnections() { + /* Can handle 5 simultaneous connections */ + srv.listen(5); + if (srv.listen()<0) { + printf("listen error\r\n"); + return true; + } + printf("Listening\r\n"); + return false; +} + +bool CheckAccept() { + int ii = srv.accept(&clt_sock); + if (ii<0) { + printf("accept error: %i \r\n",ii); + return true; + } + printf("Socket Accepted\r\n"); + return false; +} + +bool EventLoop() { + while (true) { + srv.accept(&clt_sock, &clt_addr); + + //printf("The target IP address is '%s'\n", eth.get_ip_address()); + //printf("The target MAC address is '%s'\n", eth.get_mac_address()); + printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port()); + + // Receive data + char buf[100]; + clt_sock.recv(buf, 100); + printf("recv: %s\n", buf); + + char *arr[10]; + split(arr, buf, " "); + + /*for(int jj=0;jj<10;jj++) { + printf("## %03d: %s\n", jj, arr[jj]); + }*/ + + if (strcmp(arr[0], "GET")==0) { + printf("## ##: GET\n"); + char response[] = HTTP_STATUS_LINE; + strcat(response, "\r\n"); + strcat(response, HTTP_HEADER_FIELDS); + strcat(response, "\r\n"); + strcat(response, "\r\n"); + strcat(response, "001 GET\r\n"); + clt_sock.send(response, strlen(response)); + } + + if (strcmp(arr[0], "POST")==0) { + printf("## ##: POST\n"); + char response[] = HTTP_STATUS_LINE; + strcat(response, "\r\n"); + strcat(response, HTTP_HEADER_FIELDS); + strcat(response, "\r\n"); + strcat(response, "\r\n"); + strcat(response, "002 POST\r\n"); + clt_sock.send(response, strlen(response)); + } + + // Send data + //clt_sock.send(HTTP_RESPONSE, strlen(HTTP_RESPONSE)); + + // Close the socket + clt_sock.close(); + + } + return false; +} + +/* Main function */ +int main(){ + printf("mbed HTTP server for REST API\n"); + + ServerInit(); + led1 = 1; + led2 = 0; + led3 = 0; + wait(0.1); + + if (OpenServer()) return 0; + + if (BindPort()) return 0; + + if (ListenConnections()) return 0; + + //if (CheckAccept()) return 0; + + led1 = 0; + led2 = 1; + led3 = 0; + wait(0.1); + + if (EventLoop()) return 0; + +} \ No newline at end of file
diff -r 000000000000 -r 8eda451f71fa main2.md --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main2.md Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,180 @@ +#ifndef _HTTP_REQUEST_ +#define _HTTP_REQUEST_ + +#include <string> +#include <vector> +#include <map> +#include "http_parser.h" +#include "http_response.h" +#include "http_request_builder.h" +#include "http_response_parser.h" +#include "http_parsed_url.h" + +/** + * @todo: + * - Userinfo parameter is not handled + * - Allow socket re-use + */ + + +/** + * \brief HttpRequest implements the logic for interacting with HTTPS servers. + */ +class HttpRequest { +public: + /** + * HttpRequest Constructor + * + * @param[in] aNetwork The network interface + * @param[in] aMethod HTTP method to use + * @param[in] url URL to the resource + * @param[in] aBodyCallback Callback on which to retrieve chunks of the response body. + If not set, the complete body will be allocated on the HttpResponse object, + which might use lots of memory. + */ + HttpRequest(NetworkInterface* aNetwork, http_method aMethod, const char* url, Callback<void(const char *at, size_t length)> aBodyCallback = 0) + : network(aNetwork), method(aMethod), body_callback(aBodyCallback) + { + error = 0; + response = NULL; + + parsed_url = new ParsedUrl(url); + request_builder = new HttpRequestBuilder(method, parsed_url); + } + + /** + * HttpRequest Constructor + */ + ~HttpRequest() { + // should response be owned by us? Or should user free it? + // maybe implement copy constructor on response... + if (response) { + delete response; + } + + if (parsed_url) { + delete parsed_url; + } + + if (request_builder) { + delete request_builder; + } + } + + /** + * Execute the request and receive the response. + */ + HttpResponse* send(const void* body = NULL, nsapi_size_t body_size = 0) { + if (response != NULL) { + // already executed this response + error = -2100; // @todo, make a lookup table with errors + return NULL; + } + + error = 0; + + TCPSocket socket; + + nsapi_error_t open_result = socket.open(network); + if (open_result != 0) { + error = open_result; + return NULL; + } + + nsapi_error_t connection_result = socket.connect(parsed_url->host(), parsed_url->port()); + if (connection_result != 0) { + error = connection_result; + return NULL; + } + + char* request = request_builder->build(body, body_size); + size_t request_size = strlen(request); + + nsapi_size_or_error_t send_result = socket.send(request, request_size); + + free(request); + + if (send_result != request_size) { + error = send_result; + return NULL; + } + + // Create a response object + response = new HttpResponse(); + // And a response parser + HttpResponseParser parser(response, body_callback); + + // Set up a receive buffer (on the heap) + uint8_t* recv_buffer = (uint8_t*)malloc(HTTP_RECEIVE_BUFFER_SIZE); + + // TCPSocket::recv is called until we don't have any data anymore + nsapi_size_or_error_t recv_ret; + while ((recv_ret = socket.recv(recv_buffer, HTTP_RECEIVE_BUFFER_SIZE)) > 0) { + // Pass the chunk into the http_parser + size_t nparsed = parser.execute((const char*)recv_buffer, recv_ret); + if (nparsed != recv_ret) { + // printf("Parsing failed... parsed %d bytes, received %d bytes\n", nparsed, recv_ret); + error = -2101; + free(recv_buffer); + return NULL; + } + // No more chunks? break out of this loop + if (recv_ret < HTTP_RECEIVE_BUFFER_SIZE) { + break; + } + } + // error? + if (recv_ret < 0) { + error = recv_ret; + free(recv_buffer); + return NULL; + } + + // When done, call parser.finish() + parser.finish(); + + // Free the receive buffer + free(recv_buffer); + + // Close the socket + socket.close(); + + return response; + } + + /** + * Set a header for the request. + * + * The 'Host' and 'Content-Length' headers are set automatically. + * Setting the same header twice will overwrite the previous entry. + * + * @param[in] key Header key + * @param[in] value Header value + */ + void set_header(string key, string value) { + request_builder->set_header(key, value); + } + + /** + * Get the error code. + * + * When send() fails, this error is set. + */ + nsapi_error_t get_error() { + return error; + } + +private: + NetworkInterface* network; + http_method method; + Callback<void(const char *at, size_t length)> body_callback; + + ParsedUrl* parsed_url; + + HttpRequestBuilder* request_builder; + HttpResponse* response; + + nsapi_error_t error; +}; + +#endif // _HTTP_REQUEST_ \ No newline at end of file
diff -r 000000000000 -r 8eda451f71fa mbed-os.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed-os.lib Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/mbed-os/#c9e63f14085f5751ff5ead79a7c0382d50a813a2
diff -r 000000000000 -r 8eda451f71fa module_for_all.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/module_for_all.cpp Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,55 @@ +#include "module_for_all.h" + +DigitalOut led1(LED1); +DigitalOut led2(LED2); +DigitalOut led3(LED3); + +void split(const std::string& tmpString, + std::vector<std::string>& stringVector, + const char* delim = " ") { + stringVector.clear(); + char* buffer = new char[tmpString.size() + 1]; + std::copy(tmpString.begin(), tmpString.end(), buffer); + char* p = std::strtok(buffer, delim); + do { + stringVector.push_back(p); + } while ((p = std::strtok(NULL, delim))); +} + +char* stringToChar(std::string tmpString) { + char* tmpChar = new char [tmpString.length()+1]; + std::strcpy(tmpChar, tmpString.c_str()); + return tmpChar; +} + +void LED_PowerStart() { + led1 = 1; + led2 = 0; + led3 = 0; + wait(0.1); +} + +void LED_NetWait() { + led1 = 0; + led2 = 1; + led3 = 0; + wait(0.1); +} + +void LED_NetProcess() { + led1 = 0; + led2 = 0; + led3 = 1; + wait(0.1); +} + +int countRoot(std::string& tmpString) { + int count = 0; + char* tmpAPI = stringToChar(tmpString); + for (int ii=0;ii<strlen(tmpAPI);ii++) { + if(tmpAPI[ii]=='/') { + count++; + } + } + return count; +} \ No newline at end of file
diff -r 000000000000 -r 8eda451f71fa module_for_all.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/module_for_all.h Mon Oct 08 00:50:33 2018 +0000 @@ -0,0 +1,32 @@ +#ifndef MODULE_FOR_ALL_H +#define MODULE_FOR_ALL_H + +#include <stdio.h> +#include <iostream> +#include <sstream> +#include <string> +#include <vector> +#include <iterator> + +#include "EthernetInterface.h" +#include "TCPServer.h" +#include "TCPSocket.h" + +#include "mbed.h" + +#include "NetworkManager.h" +#include "RestAPI_Manager.h" +#include "CPU_Usage.h" + +void split(const std::string&, + std::vector<std::string>&, + const char*); + +char* stringToChar(std::string); + +void LED_PowerStart(); +void LED_NetWait(); +void LED_NetProcess(); +int countRoot(std::string&); + +#endif \ No newline at end of file