GDP group 24 node core

Dependencies:   EthernetInterface SDFileSystem mbed-rtos mbed snail MbedJSONValue

Committer:
Trumple
Date:
Sun Dec 14 22:28:24 2014 +0000
Revision:
2:1cbb20dd1733
Parent:
1:27b35752c5d0
Child:
12:daddfc44a0f5
Implement getLocalAddress, fix getNetworkParameters, implement timestamp acquisition, implement HTTP response parsing

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Trumple 1:27b35752c5d0 1 #include "mbed.h"
Trumple 1:27b35752c5d0 2 #include "http.h"
Trumple 1:27b35752c5d0 3 #include <string>
Trumple 1:27b35752c5d0 4 #include <vector>
Trumple 1:27b35752c5d0 5
Trumple 2:1cbb20dd1733 6 void http::connect()
Trumple 1:27b35752c5d0 7 {
Trumple 1:27b35752c5d0 8 #ifdef DEBUG
Trumple 1:27b35752c5d0 9 printf("[HTTP] Ethernet connecting...\r\n");
Trumple 1:27b35752c5d0 10 #endif
Trumple 1:27b35752c5d0 11
Trumple 1:27b35752c5d0 12 eth.init();
Trumple 1:27b35752c5d0 13 eth.connect();
Trumple 1:27b35752c5d0 14
Trumple 1:27b35752c5d0 15 #ifdef DEBUG
Trumple 1:27b35752c5d0 16 printf("[HTTP] Ethernet connected, IP: %s\r\n", eth.getIPAddress());
Trumple 1:27b35752c5d0 17 #endif
Trumple 1:27b35752c5d0 18 }
Trumple 1:27b35752c5d0 19
Trumple 1:27b35752c5d0 20 string http::get(string address, int port, string url, int replyTimeout)
Trumple 1:27b35752c5d0 21 {
Trumple 1:27b35752c5d0 22 #ifdef DEBUG
Trumple 2:1cbb20dd1733 23 printf("[HTTP] Sending GET request to %s:%i%s\r\n", address.c_str(), port, url.c_str());
Trumple 1:27b35752c5d0 24 #endif
Trumple 1:27b35752c5d0 25
Trumple 1:27b35752c5d0 26 TCPSocketConnection sock;
Trumple 1:27b35752c5d0 27 sock.connect(address.c_str(), port);
Trumple 1:27b35752c5d0 28
Trumple 1:27b35752c5d0 29 #ifdef DEBUG
Trumple 1:27b35752c5d0 30 printf("[HTTP] Connected to endpoint...\r\n");
Trumple 1:27b35752c5d0 31 #endif
Trumple 1:27b35752c5d0 32
Trumple 1:27b35752c5d0 33 string httpget = "GET " + url + " HTTP/1.1";
Trumple 1:27b35752c5d0 34 httpget += "\nHost: " + address + "\n\n";
Trumple 1:27b35752c5d0 35
Trumple 1:27b35752c5d0 36 //get a writable char* (I.E. not const char* returned by c_str), put it into a vector
Trumple 1:27b35752c5d0 37 vector<char> writable(httpget.begin(), httpget.end());
Trumple 1:27b35752c5d0 38 writable.push_back('\0');
Trumple 1:27b35752c5d0 39
Trumple 1:27b35752c5d0 40 sock.send_all(&writable[0], writable.size()-1);
Trumple 1:27b35752c5d0 41
Trumple 1:27b35752c5d0 42 string message = this->receiveFromSock(sock, replyTimeout);
Trumple 1:27b35752c5d0 43
Trumple 1:27b35752c5d0 44 sock.close();
Trumple 1:27b35752c5d0 45
Trumple 1:27b35752c5d0 46 return this->parse(message);
Trumple 1:27b35752c5d0 47 }
Trumple 1:27b35752c5d0 48
Trumple 1:27b35752c5d0 49 string http::post(string address, int port, string url, string jsonPayload, int replyTimeout)
Trumple 1:27b35752c5d0 50 {
Trumple 1:27b35752c5d0 51 #ifdef DEBUG
Trumple 2:1cbb20dd1733 52 printf("[HTTP] Sending POST request to %s:%i%s\r\n", address.c_str(), port, url.c_str());
Trumple 1:27b35752c5d0 53 #endif
Trumple 1:27b35752c5d0 54
Trumple 1:27b35752c5d0 55 TCPSocketConnection sock;
Trumple 1:27b35752c5d0 56 sock.connect(address.c_str(), port);
Trumple 1:27b35752c5d0 57
Trumple 1:27b35752c5d0 58 #ifdef DEBUG
Trumple 1:27b35752c5d0 59 printf("[HTTP] Connected to endpoint...\r\n");
Trumple 1:27b35752c5d0 60 #endif
Trumple 1:27b35752c5d0 61
Trumple 2:1cbb20dd1733 62 char buffer[20];
Trumple 2:1cbb20dd1733 63 sprintf(buffer, "%i", jsonPayload.size());
Trumple 2:1cbb20dd1733 64 string contentLengthStr = string(buffer);
Trumple 1:27b35752c5d0 65
Trumple 1:27b35752c5d0 66 string httppost = "POST " + url + " HTTP/1.1";
Trumple 1:27b35752c5d0 67 httppost += "\nHost: " + address;
Trumple 1:27b35752c5d0 68 httppost += "\nContent-Type: application/json";
Trumple 1:27b35752c5d0 69 httppost += "\nContent-Length: " + contentLengthStr;
Trumple 2:1cbb20dd1733 70 httppost += "\nAuthorization: Key 1";
Trumple 1:27b35752c5d0 71 httppost += "\n\n" + jsonPayload;
Trumple 1:27b35752c5d0 72
Trumple 1:27b35752c5d0 73 //to get a writable char* (I.E. not const char* returned by string.c_str), put it into a vector
Trumple 1:27b35752c5d0 74 vector<char> writable(httppost.begin(), httppost.end());
Trumple 1:27b35752c5d0 75 writable.push_back('\0');
Trumple 1:27b35752c5d0 76
Trumple 1:27b35752c5d0 77 sock.send_all(&writable[0], writable.size()-1);
Trumple 1:27b35752c5d0 78
Trumple 1:27b35752c5d0 79 string message = this->receiveFromSock(sock, replyTimeout);
Trumple 1:27b35752c5d0 80
Trumple 1:27b35752c5d0 81 sock.close();
Trumple 1:27b35752c5d0 82
Trumple 1:27b35752c5d0 83 return this->parse(message);
Trumple 1:27b35752c5d0 84 }
Trumple 1:27b35752c5d0 85
Trumple 1:27b35752c5d0 86 string http::receiveFromSock(TCPSocketConnection sock, int replyTimeout)
Trumple 1:27b35752c5d0 87 {
Trumple 1:27b35752c5d0 88 char buffer[1024];
Trumple 1:27b35752c5d0 89 int receiveByteCount;
Trumple 1:27b35752c5d0 90 string message;
Trumple 1:27b35752c5d0 91
Trumple 1:27b35752c5d0 92 while (true)
Trumple 1:27b35752c5d0 93 {
Trumple 1:27b35752c5d0 94 receiveByteCount = sock.receive(buffer, sizeof(buffer)-1);//spare a byte for null termination byte
Trumple 1:27b35752c5d0 95 if (receiveByteCount <= 0)
Trumple 1:27b35752c5d0 96 break;
Trumple 1:27b35752c5d0 97 buffer[receiveByteCount] = '\0';
Trumple 1:27b35752c5d0 98 message += buffer;
Trumple 1:27b35752c5d0 99 }
Trumple 1:27b35752c5d0 100
Trumple 1:27b35752c5d0 101 return message;
Trumple 1:27b35752c5d0 102 }
Trumple 1:27b35752c5d0 103
Trumple 1:27b35752c5d0 104 string http::parse(string httpReply)
Trumple 1:27b35752c5d0 105 {
Trumple 2:1cbb20dd1733 106 int payloadBegin = httpReply.find("\r\n\r\n");
Trumple 2:1cbb20dd1733 107
Trumple 2:1cbb20dd1733 108 if (payloadBegin > -1)
Trumple 2:1cbb20dd1733 109 {
Trumple 2:1cbb20dd1733 110 string payload(httpReply.begin() + payloadBegin + 4, httpReply.end());
Trumple 2:1cbb20dd1733 111
Trumple 2:1cbb20dd1733 112 return payload;
Trumple 2:1cbb20dd1733 113 }
Trumple 2:1cbb20dd1733 114 else
Trumple 2:1cbb20dd1733 115 {
Trumple 2:1cbb20dd1733 116 return "";
Trumple 2:1cbb20dd1733 117 }
Trumple 1:27b35752c5d0 118 }