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:43:48 2020 +0000
Revision:
1:4f2c1fcc6fb6
Parent:
0:768ae9838086
Child:
2:cde6181a8320
'Depreciated' updates

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 // set Firebase project information..
star297 1:4f2c1fcc6fb6 61 char FirebaseUrl[300]; // dimension to suit required character space
star297 1:4f2c1fcc6fb6 62 const char FirebaseID[100] = "https://mbed-a5059.firebaseio.com"; // project ID
star297 1:4f2c1fcc6fb6 63 const char FirebaseAuth[100]= "YPXm66X4m82sdy3srbPwcPYvpdNwrNUCAJ9xsZsL"; // web API key
star297 1:4f2c1fcc6fb6 64
star297 1:4f2c1fcc6fb6 65 char* getData;
star297 1:4f2c1fcc6fb6 66 int TLSfailText,httpfail;
star297 1:4f2c1fcc6fb6 67 /* Port number used to connect to the server */
star297 1:4f2c1fcc6fb6 68 const int server_port = 443;
star297 1:4f2c1fcc6fb6 69
star297 1:4f2c1fcc6fb6 70 #define IP "192.168.1.180"
star297 1:4f2c1fcc6fb6 71 #define GATEWAY "192.168.1.1"
star297 1:4f2c1fcc6fb6 72 #define NETMASK "255.255.255.0"
star297 0:768ae9838086 73
star297 0:768ae9838086 74 /*
star297 0:768ae9838086 75 Connects to the network using the mbed_app.json ESP8266 WIFI networking interface,
star297 0:768ae9838086 76 you can also swap this out with a driver for a different networking interface or
star297 0:768ae9838086 77 if you use ETHERNET: change mbed_app.json "target.network-default-interface-type" : "ETHERNET",
star297 0:768ae9838086 78 */
star297 0:768ae9838086 79
star297 1:4f2c1fcc6fb6 80 NetworkInterface* net;
star297 0:768ae9838086 81 TLSSocket* socket = new TLSSocket();
star297 1:4f2c1fcc6fb6 82 SocketAddress Fbase;
star297 1:4f2c1fcc6fb6 83 nsapi_error_t result;
star297 0:768ae9838086 84
star297 0:768ae9838086 85 NetworkInterface *connect_to_default_network_interface() {
star297 0:768ae9838086 86 printf("Connecting to network...\n\n");
star297 0:768ae9838086 87
star297 1:4f2c1fcc6fb6 88 NetworkInterface* net = NetworkInterface::get_default_instance();
star297 1:4f2c1fcc6fb6 89
star297 1:4f2c1fcc6fb6 90 // set static IP, not working on WIFI at the moment
star297 1:4f2c1fcc6fb6 91 //net->set_network((SocketAddress)IP,(SocketAddress)NETMASK,(SocketAddress)GATEWAY);
star297 1:4f2c1fcc6fb6 92
star297 1:4f2c1fcc6fb6 93 if (!net) {
star297 0:768ae9838086 94 printf("No network interface found, select an interface in 'mbed_app.json'\n");
star297 0:768ae9838086 95 return NULL;
star297 0:768ae9838086 96 }
star297 1:4f2c1fcc6fb6 97 nsapi_error_t connect_status = net->connect();
star297 0:768ae9838086 98 if (connect_status != NSAPI_ERROR_OK) {
star297 0:768ae9838086 99 printf("Failed to connect to network (%d)\n", connect_status);
star297 0:768ae9838086 100 return NULL;
star297 0:768ae9838086 101 }
star297 1:4f2c1fcc6fb6 102 SocketAddress net_addr;
star297 1:4f2c1fcc6fb6 103 net->get_ip_address(&net_addr);
star297 1:4f2c1fcc6fb6 104 printf("IP address: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 1:4f2c1fcc6fb6 105 net->get_netmask(&net_addr);
star297 1:4f2c1fcc6fb6 106 printf("Netmask: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 1:4f2c1fcc6fb6 107 net->get_gateway(&net_addr);
star297 1:4f2c1fcc6fb6 108 printf("Gateway: %s\n", net_addr.get_ip_address() ? net_addr.get_ip_address() : "None");
star297 1:4f2c1fcc6fb6 109 printf("MAC: %s\n", net->get_mac_address());
star297 1:4f2c1fcc6fb6 110 return net;
star297 0:768ae9838086 111 }
star297 0:768ae9838086 112
star297 0:768ae9838086 113 void startTLSreusesocket(char *FirebaseID) {
star297 0:768ae9838086 114
star297 1:4f2c1fcc6fb6 115 if ((result = socket->open(net)) != NSAPI_ERROR_OK) {
star297 1:4f2c1fcc6fb6 116 printf("TLS socket open failed (%d)\n", result);
star297 0:768ae9838086 117 }
star297 1:4f2c1fcc6fb6 118 if ((result = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
star297 1:4f2c1fcc6fb6 119 printf("TLS socket set_root_ca_cert failed (%d)\n", result);
star297 1:4f2c1fcc6fb6 120 }
star297 1:4f2c1fcc6fb6 121 net->gethostbyname(FirebaseID, &Fbase);
star297 1:4f2c1fcc6fb6 122 Fbase.set_port(server_port);
star297 1:4f2c1fcc6fb6 123 if ((result = socket->connect(Fbase)) != NSAPI_ERROR_OK) {
star297 1:4f2c1fcc6fb6 124 printf("Connect failure:\n%d\n", result);
star297 1:4f2c1fcc6fb6 125 return;
star297 0:768ae9838086 126 }
star297 1:4f2c1fcc6fb6 127 printf("Successfully connected to:\n%s \nat port: %u\n",
star297 1:4f2c1fcc6fb6 128 FirebaseID, server_port);
star297 0:768ae9838086 129 }
star297 0:768ae9838086 130
star297 0:768ae9838086 131 void dump_response(HttpResponse* res) {
star297 0:768ae9838086 132 mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
star297 0:768ae9838086 133 mbedtls_printf("Headers:\n");
star297 0:768ae9838086 134 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
star297 0:768ae9838086 135 mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
star297 0:768ae9838086 136 }
star297 0:768ae9838086 137 mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
star297 0:768ae9838086 138 }
star297 0:768ae9838086 139
star297 0:768ae9838086 140 char *getFirebase(char* FirebaseUrl)
star297 0:768ae9838086 141 {
star297 1:4f2c1fcc6fb6 142 //HttpsRequest* get_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_GET, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 143 HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 144 HttpResponse* get_res = get_req->send();
star297 0:768ae9838086 145 if (!get_res) {
star297 0:768ae9838086 146 time_t seconds = time(NULL);
star297 0:768ae9838086 147 printf("Https GET failed (error code %d), %s", get_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 148 socket->close();
star297 0:768ae9838086 149 delete socket;
star297 0:768ae9838086 150 delete get_req;
star297 0:768ae9838086 151 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 152 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 153 return 0;}
star297 0:768ae9838086 154 else{
star297 0:768ae9838086 155 //dump_response(get_res);
star297 0:768ae9838086 156 delete get_req;
star297 0:768ae9838086 157 getData = (char*)get_res->get_body_as_string().c_str();
star297 0:768ae9838086 158 return getData;
star297 0:768ae9838086 159 }
star297 0:768ae9838086 160 }
star297 0:768ae9838086 161
star297 0:768ae9838086 162 bool putFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 163 {
star297 1:4f2c1fcc6fb6 164 //HttpsRequest* put_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_PUT, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 165 HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 166 put_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 167 HttpResponse* put_res = put_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 168 if (!put_res) {
star297 0:768ae9838086 169 time_t seconds = time(NULL);
star297 0:768ae9838086 170 printf("Https PUT failed (error code %d), %s", put_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 171 socket->close();
star297 0:768ae9838086 172 delete socket;
star297 0:768ae9838086 173 delete put_req;
star297 0:768ae9838086 174 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 175 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 176 return 0;
star297 0:768ae9838086 177 }
star297 0:768ae9838086 178 else{
star297 1:4f2c1fcc6fb6 179 //dump_response(put_res);
star297 0:768ae9838086 180 delete put_req;
star297 0:768ae9838086 181 return put_res;
star297 0:768ae9838086 182 }
star297 0:768ae9838086 183 }
star297 0:768ae9838086 184
star297 0:768ae9838086 185 bool postFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 186 {
star297 1:4f2c1fcc6fb6 187 // HttpsRequest* post_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_POST, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 188 HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 189 post_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 190 HttpResponse* post_res = post_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 191 if (!post_res) {
star297 0:768ae9838086 192 time_t seconds = time(NULL);
star297 0:768ae9838086 193 printf("Https POST failed (error code %d), %s", post_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 194 socket->close();
star297 0:768ae9838086 195 delete socket;
star297 0:768ae9838086 196 delete post_req;
star297 0:768ae9838086 197 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 198 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 199 return 0;
star297 0:768ae9838086 200 }
star297 0:768ae9838086 201 else{
star297 0:768ae9838086 202 //dump_response(post_res);
star297 0:768ae9838086 203 delete post_req;
star297 0:768ae9838086 204 return post_res;
star297 0:768ae9838086 205 }
star297 0:768ae9838086 206 }
star297 0:768ae9838086 207
star297 0:768ae9838086 208 bool deleteFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 209 {
star297 1:4f2c1fcc6fb6 210 // HttpsRequest* delete_req = new HttpsRequest(net, SSL_CA_PEM, HTTP_DELETE, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 211 HttpsRequest* delete_req = new HttpsRequest(socket, HTTP_DELETE, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 212 delete_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 213 HttpResponse* delete_res = delete_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 214 if (!delete_res) {
star297 0:768ae9838086 215 time_t seconds = time(NULL);
star297 0:768ae9838086 216 printf("Https DELETE failed (error code %d), %s", delete_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 217 socket->close();
star297 0:768ae9838086 218 delete socket;
star297 0:768ae9838086 219 delete delete_req;
star297 0:768ae9838086 220 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 221 startTLSreusesocket((char*)FirebaseID); // restart TLS socket reuse if failure
star297 0:768ae9838086 222 return 0;
star297 0:768ae9838086 223 }
star297 0:768ae9838086 224 else{
star297 0:768ae9838086 225 //dump_response(delete_res);
star297 0:768ae9838086 226 delete delete_req;
star297 0:768ae9838086 227 return delete_res;
star297 0:768ae9838086 228 }
star297 0:768ae9838086 229 }
star297 0:768ae9838086 230
star297 0:768ae9838086 231 #endif // _FIREBASE_H_