Library for Firebase, PUT, PATCH, POST, GET, DELETE operations supported, (others are available, todo). Based on Mbed's https-example. Tested on STM32F767 using ETHERNET and ESP8266 WIFI interfaces and STM32F446 using ESP8266 WIFI interface.

Dependents:   Firebase-Example

Committer:
star297
Date:
Sun Mar 08 14:49:02 2020 +0000
Revision:
2:cde6181a8320
Parent:
1:4f2c1fcc6fb6
Child:
3:4d7c08734a91
code tidy

Who changed what in which revision?

UserRevisionLine numberNew contents of line
star297 0:768ae9838086 1 #ifndef _FIREBASE_H_
star297 0:768ae9838086 2 #define _FIREBASE_H_
star297 0:768ae9838086 3
star297 0:768ae9838086 4 #include "mbed.h"
star297 0:768ae9838086 5 #include "NetworkInterface.h"
star297 0:768ae9838086 6 #include "https_request.h"
star297 0:768ae9838086 7
star297 0:768ae9838086 8 /* List of trusted root CA certificates
star297 0:768ae9838086 9 * currently two: Amazon, the CA for os.mbed.com and Let's Encrypt,
star297 0:768ae9838086 10 * the CA for httpbin.org
star297 0:768ae9838086 11 * To add more root certificates, just concatenate them.
star297 0:768ae9838086 12 */
star297 0:768ae9838086 13 const char SSL_CA_PEM[] =
star297 0:768ae9838086 14 "-----BEGIN CERTIFICATE-----\n"
star297 0:768ae9838086 15 "MIIESjCCAzKgAwIBAgINAeO0mqGNiqmBJWlQuDANBgkqhkiG9w0BAQsFADBMMSAw\n"
star297 0:768ae9838086 16 "HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEGA1UEChMKR2xvYmFs\n"
star297 0:768ae9838086 17 "U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xNzA2MTUwMDAwNDJaFw0yMTEy\n"
star297 0:768ae9838086 18 "MTUwMDAwNDJaMEIxCzAJBgNVBAYTAlVTMR4wHAYDVQQKExVHb29nbGUgVHJ1c3Qg\n"
star297 0:768ae9838086 19 "U2VydmljZXMxEzARBgNVBAMTCkdUUyBDQSAxTzEwggEiMA0GCSqGSIb3DQEBAQUA\n"
star297 0:768ae9838086 20 "A4IBDwAwggEKAoIBAQDQGM9F1IvN05zkQO9+tN1pIRvJzzyOTHW5DzEZhD2ePCnv\n"
star297 0:768ae9838086 21 "UA0Qk28FgICfKqC9EksC4T2fWBYk/jCfC3R3VZMdS/dN4ZKCEPZRrAzDsiKUDzRr\n"
star297 0:768ae9838086 22 "mBBJ5wudgzndIMYcLe/RGGFl5yODIKgjEv/SJH/UL+dEaltN11BmsK+eQmMF++Ac\n"
star297 0:768ae9838086 23 "xGNhr59qM/9il71I2dN8FGfcddwuaej4bXhp0LcQBbjxMcI7JP0aM3T4I+DsaxmK\n"
star297 0:768ae9838086 24 "FsbjzaTNC9uzpFlgOIg7rR25xoynUxv8vNmkq7zdPGHXkxWY7oG9j+JkRyBABk7X\n"
star297 0:768ae9838086 25 "rJfoucBZEqFJJSPk7XA0LKW0Y3z5oz2D0c1tJKwHAgMBAAGjggEzMIIBLzAOBgNV\n"
star297 0:768ae9838086 26 "HQ8BAf8EBAMCAYYwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1Ud\n"
star297 0:768ae9838086 27 "EwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFJjR+G4Q68+b7GCfGJAboOt9Cf0rMB8G\n"
star297 0:768ae9838086 28 "A1UdIwQYMBaAFJviB1dnHB7AagbeWbSaLd/cGYYuMDUGCCsGAQUFBwEBBCkwJzAl\n"
star297 0:768ae9838086 29 "BggrBgEFBQcwAYYZaHR0cDovL29jc3AucGtpLmdvb2cvZ3NyMjAyBgNVHR8EKzAp\n"
star297 0:768ae9838086 30 "MCegJaAjhiFodHRwOi8vY3JsLnBraS5nb29nL2dzcjIvZ3NyMi5jcmwwPwYDVR0g\n"
star297 0:768ae9838086 31 "BDgwNjA0BgZngQwBAgIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly9wa2kuZ29vZy9y\n"
star297 0:768ae9838086 32 "ZXBvc2l0b3J5LzANBgkqhkiG9w0BAQsFAAOCAQEAGoA+Nnn78y6pRjd9XlQWNa7H\n"
star297 0:768ae9838086 33 "TgiZ/r3RNGkmUmYHPQq6Scti9PEajvwRT2iWTHQr02fesqOqBY2ETUwgZQ+lltoN\n"
star297 0:768ae9838086 34 "FvhsO9tvBCOIazpswWC9aJ9xju4tWDQH8NVU6YZZ/XteDSGU9YzJqPjY8q3MDxrz\n"
star297 0:768ae9838086 35 "mqepBCf5o8mw/wJ4a2G6xzUr6Fb6T8McDO22PLRL6u3M4Tzs3A2M1j6bykJYi8wW\n"
star297 0:768ae9838086 36 "IRdAvKLWZu/axBVbzYmqmwkm5zLSDW5nIAJbELCQCZwMH56t2Dvqofxs6BBcCFIZ\n"
star297 0:768ae9838086 37 "USpxu6x6td0V7SvJCCosirSmIatj/9dSSVDQibet8q/7UK4v4ZUN80atnZz1yg==\n"
star297 0:768ae9838086 38 "-----END CERTIFICATE-----\n";
star297 0:768ae9838086 39
star297 0:768ae9838086 40 /*
star297 0:768ae9838086 41 // how to build the Firebase connection FirebaseUrl string for 'method overide'
star297 0:768ae9838086 42
star297 0:768ae9838086 43 strcpy(FirebaseUrl, FirebaseID); // Firebase account ID
star297 0:768ae9838086 44
star297 0:768ae9838086 45 // this bit in the middle to send .json data structure and authority
star297 0:768ae9838086 46 // for any REST api functions e.g. GET, PUT, POST, PATCH, DELETE
star297 0:768ae9838086 47 // replace the PUT with required function
star297 0:768ae9838086 48
star297 0:768ae9838086 49 strcat(FirebaseUrl, "/Parent/Child/.json?x-http-method-override=PUT&auth=");
star297 0:768ae9838086 50 strcat(FirebaseUrl, FirebaseAuth); // Firebase account authorisation key
star297 0:768ae9838086 51 */
star297 0:768ae9838086 52
star297 1:4f2c1fcc6fb6 53 /*
star297 0:768ae9838086 54 // set Firebase project information..
star297 0:768ae9838086 55 char FirebaseUrl[300]; // dimension to suit required character space
star297 0:768ae9838086 56 const char FirebaseID[100] = "https://projectID.firebaseio.com"; // project ID
star297 0:768ae9838086 57 const char FirebaseAuth[100]= "Web API key"; // web API key
star297 1:4f2c1fcc6fb6 58 */
star297 0:768ae9838086 59
star297 1:4f2c1fcc6fb6 60 char* getData;
star297 1:4f2c1fcc6fb6 61 int TLSfailText,httpfail;
star297 1:4f2c1fcc6fb6 62 /* Port number used to connect to the server */
star297 1:4f2c1fcc6fb6 63 const int server_port = 443;
star297 1:4f2c1fcc6fb6 64
star297 1:4f2c1fcc6fb6 65 #define IP "192.168.1.180"
star297 1:4f2c1fcc6fb6 66 #define GATEWAY "192.168.1.1"
star297 1:4f2c1fcc6fb6 67 #define NETMASK "255.255.255.0"
star297 0:768ae9838086 68
star297 0:768ae9838086 69 /*
star297 0:768ae9838086 70 Connects to the network using the mbed_app.json ESP8266 WIFI networking interface,
star297 0:768ae9838086 71 you can also swap this out with a driver for a different networking interface or
star297 0:768ae9838086 72 if you use ETHERNET: change mbed_app.json "target.network-default-interface-type" : "ETHERNET",
star297 0:768ae9838086 73 */
star297 0:768ae9838086 74
star297 1:4f2c1fcc6fb6 75 NetworkInterface* net;
star297 0:768ae9838086 76 TLSSocket* socket = new TLSSocket();
star297 1:4f2c1fcc6fb6 77 SocketAddress Fbase;
star297 1:4f2c1fcc6fb6 78 nsapi_error_t result;
star297 0:768ae9838086 79
star297 0:768ae9838086 80 NetworkInterface *connect_to_default_network_interface() {
star297 0:768ae9838086 81 printf("Connecting to network...\n\n");
star297 0:768ae9838086 82
star297 1:4f2c1fcc6fb6 83 NetworkInterface* net = NetworkInterface::get_default_instance();
star297 1:4f2c1fcc6fb6 84
star297 1:4f2c1fcc6fb6 85 // set static IP, not working on WIFI at the moment
star297 1:4f2c1fcc6fb6 86 //net->set_network((SocketAddress)IP,(SocketAddress)NETMASK,(SocketAddress)GATEWAY);
star297 1:4f2c1fcc6fb6 87
star297 1:4f2c1fcc6fb6 88 if (!net) {
star297 0:768ae9838086 89 printf("No network interface found, select an interface in 'mbed_app.json'\n");
star297 0:768ae9838086 90 return NULL;
star297 0:768ae9838086 91 }
star297 1:4f2c1fcc6fb6 92 nsapi_error_t connect_status = net->connect();
star297 0:768ae9838086 93 if (connect_status != NSAPI_ERROR_OK) {
star297 0:768ae9838086 94 printf("Failed to connect to network (%d)\n", connect_status);
star297 0:768ae9838086 95 return NULL;
star297 0:768ae9838086 96 }
star297 1:4f2c1fcc6fb6 97 SocketAddress net_addr;
star297 1:4f2c1fcc6fb6 98 net->get_ip_address(&net_addr);
star297 1:4f2c1fcc6fb6 99 printf("IP address: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 1:4f2c1fcc6fb6 100 net->get_netmask(&net_addr);
star297 1:4f2c1fcc6fb6 101 printf("Netmask: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 1:4f2c1fcc6fb6 102 net->get_gateway(&net_addr);
star297 1:4f2c1fcc6fb6 103 printf("Gateway: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 1:4f2c1fcc6fb6 104 printf("MAC: %s\n", net->get_mac_address());
star297 1:4f2c1fcc6fb6 105 return net;
star297 0:768ae9838086 106 }
star297 0:768ae9838086 107
star297 0:768ae9838086 108 void startTLSreusesocket(char *FirebaseID) {
star297 0:768ae9838086 109
star297 1:4f2c1fcc6fb6 110 if ((result = socket->open(net)) != NSAPI_ERROR_OK) {
star297 1:4f2c1fcc6fb6 111 printf("TLS socket open failed (%d)\n", result);
star297 0:768ae9838086 112 }
star297 1:4f2c1fcc6fb6 113 if ((result = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
star297 1:4f2c1fcc6fb6 114 printf("TLS socket set_root_ca_cert failed (%d)\n", result);
star297 1:4f2c1fcc6fb6 115 }
star297 1:4f2c1fcc6fb6 116 net->gethostbyname(FirebaseID, &Fbase);
star297 1:4f2c1fcc6fb6 117 Fbase.set_port(server_port);
star297 1:4f2c1fcc6fb6 118 if ((result = socket->connect(Fbase)) != NSAPI_ERROR_OK) {
star297 1:4f2c1fcc6fb6 119 printf("Connect failure:\n%d\n", result);
star297 1:4f2c1fcc6fb6 120 return;
star297 0:768ae9838086 121 }
star297 1:4f2c1fcc6fb6 122 printf("Successfully connected to:\n%s \nat port: %u\n",
star297 1:4f2c1fcc6fb6 123 FirebaseID, server_port);
star297 0:768ae9838086 124 }
star297 0:768ae9838086 125
star297 0:768ae9838086 126 void dump_response(HttpResponse* res) {
star297 0:768ae9838086 127 mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
star297 0:768ae9838086 128 mbedtls_printf("Headers:\n");
star297 0:768ae9838086 129 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
star297 0:768ae9838086 130 mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
star297 0:768ae9838086 131 }
star297 0:768ae9838086 132 mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
star297 0:768ae9838086 133 }
star297 0:768ae9838086 134
star297 0:768ae9838086 135 char *getFirebase(char* FirebaseUrl)
star297 0:768ae9838086 136 {
star297 1:4f2c1fcc6fb6 137 //HttpsRequest* get_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_GET, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 138 HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 139 HttpResponse* get_res = get_req->send();
star297 0:768ae9838086 140 if (!get_res) {
star297 0:768ae9838086 141 time_t seconds = time(NULL);
star297 0:768ae9838086 142 printf("Https GET failed (error code %d), %s", get_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 143 socket->close();
star297 0:768ae9838086 144 delete socket;
star297 0:768ae9838086 145 delete get_req;
star297 0:768ae9838086 146 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 147 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 148 return 0;}
star297 0:768ae9838086 149 else{
star297 0:768ae9838086 150 //dump_response(get_res);
star297 0:768ae9838086 151 delete get_req;
star297 0:768ae9838086 152 getData = (char*)get_res->get_body_as_string().c_str();
star297 0:768ae9838086 153 return getData;
star297 0:768ae9838086 154 }
star297 0:768ae9838086 155 }
star297 0:768ae9838086 156
star297 0:768ae9838086 157 bool putFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 158 {
star297 1:4f2c1fcc6fb6 159 //HttpsRequest* put_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_PUT, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 160 HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 161 put_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 162 HttpResponse* put_res = put_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 163 if (!put_res) {
star297 0:768ae9838086 164 time_t seconds = time(NULL);
star297 0:768ae9838086 165 printf("Https PUT failed (error code %d), %s", put_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 166 socket->close();
star297 0:768ae9838086 167 delete socket;
star297 0:768ae9838086 168 delete put_req;
star297 0:768ae9838086 169 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 170 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 171 return 0;
star297 0:768ae9838086 172 }
star297 0:768ae9838086 173 else{
star297 1:4f2c1fcc6fb6 174 //dump_response(put_res);
star297 0:768ae9838086 175 delete put_req;
star297 0:768ae9838086 176 return put_res;
star297 0:768ae9838086 177 }
star297 0:768ae9838086 178 }
star297 0:768ae9838086 179
star297 0:768ae9838086 180 bool postFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 181 {
star297 1:4f2c1fcc6fb6 182 // HttpsRequest* post_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_POST, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 183 HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 184 post_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 185 HttpResponse* post_res = post_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 186 if (!post_res) {
star297 0:768ae9838086 187 time_t seconds = time(NULL);
star297 0:768ae9838086 188 printf("Https POST failed (error code %d), %s", post_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 189 socket->close();
star297 0:768ae9838086 190 delete socket;
star297 0:768ae9838086 191 delete post_req;
star297 0:768ae9838086 192 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 193 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 194 return 0;
star297 0:768ae9838086 195 }
star297 0:768ae9838086 196 else{
star297 0:768ae9838086 197 //dump_response(post_res);
star297 0:768ae9838086 198 delete post_req;
star297 0:768ae9838086 199 return post_res;
star297 0:768ae9838086 200 }
star297 0:768ae9838086 201 }
star297 0:768ae9838086 202
star297 0:768ae9838086 203 bool deleteFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 204 {
star297 1:4f2c1fcc6fb6 205 // HttpsRequest* delete_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_DELETE, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 206 HttpsRequest* delete_req = new HttpsRequest(socket, HTTP_DELETE, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 207 delete_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 208 HttpResponse* delete_res = delete_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 209 if (!delete_res) {
star297 0:768ae9838086 210 time_t seconds = time(NULL);
star297 0:768ae9838086 211 printf("Https DELETE failed (error code %d), %s", delete_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 212 socket->close();
star297 0:768ae9838086 213 delete socket;
star297 0:768ae9838086 214 delete delete_req;
star297 0:768ae9838086 215 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 216 startTLSreusesocket((char*)FirebaseID); // restart TLS socket reuse if failure
star297 0:768ae9838086 217 return 0;
star297 0:768ae9838086 218 }
star297 0:768ae9838086 219 else{
star297 0:768ae9838086 220 //dump_response(delete_res);
star297 0:768ae9838086 221 delete delete_req;
star297 0:768ae9838086 222 return delete_res;
star297 0:768ae9838086 223 }
star297 0:768ae9838086 224 }
star297 0:768ae9838086 225
star297 0:768ae9838086 226 #endif // _FIREBASE_H_