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:
Thu Jan 23 22:18:11 2020 +0000
Revision:
0:768ae9838086
Child:
1:4f2c1fcc6fb6
Initial version.

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 "MIIFmzCCBIOgAwIBAgIRAJRuZHok8dQjAQAAAAApJGwwDQYJKoZIhvcNAQELBQAw\n"
star297 0:768ae9838086 16 "QjELMAkGA1UEBhMCVVMxHjAcBgNVBAoTFUdvb2dsZSBUcnVzdCBTZXJ2aWNlczET\n"
star297 0:768ae9838086 17 "MBEGA1UEAxMKR1RTIENBIDFPMTAeFw0xOTAzMTMyMTMwNTdaFw0yMDAzMTEyMTMw\n"
star297 0:768ae9838086 18 "NTdaMGgxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH\n"
star297 0:768ae9838086 19 "Ew1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgTExDMRcwFQYDVQQDEw5m\n"
star297 0:768ae9838086 20 "aXJlYmFzZWlvLmNvbTCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBALkd\n"
star297 0:768ae9838086 21 "/jeM+PrwIYmM6IE28iN7CjKnBWy+tqNrs5z2AJwM5iLgtgcbc+THuXGElTN7zAWB\n"
star297 0:768ae9838086 22 "SKWv+ezAGe5NiTEEUiWgX1z1VFDj85xpz+Cysn/Oi8MtoN6h8yCmYPtXv8YPInw0\n"
star297 0:768ae9838086 23 "JLkSM5e2uuWVwc/tR1Fh+awpiYQFqGm6DvMq6yAYB/nuL3Gyc4XFgRrxje+VQ+g3\n"
star297 0:768ae9838086 24 "oWE+9NVfqu4smFCutSIGlEdRhGdr/m2u+sc+dnBZNoVOqwSyyFyjpAZvMMvuE5Ip\n"
star297 0:768ae9838086 25 "3ovfA1tq57nl9eQw8fcKsdXOeeXYHc1KBUUC7Xb8F/1+upVML8a5GMULpYkClLXt\n"
star297 0:768ae9838086 26 "OKRRfCJa53YynILJdPkCAwEAAaOCAmQwggJgMA4GA1UdDwEB/wQEAwIFoDATBgNV\n"
star297 0:768ae9838086 27 "HSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMB0GA1UdDgQWBBS03YhV70g6\n"
star297 0:768ae9838086 28 "+7MUxtOiluRi1T2GAzAfBgNVHSMEGDAWgBSY0fhuEOvPm+xgnxiQG6DrfQn9KzBk\n"
star297 0:768ae9838086 29 "BggrBgEFBQcBAQRYMFYwJwYIKwYBBQUHMAGGG2h0dHA6Ly9vY3NwLnBraS5nb29n\n"
star297 0:768ae9838086 30 "L2d0czFvMTArBggrBgEFBQcwAoYfaHR0cDovL3BraS5nb29nL2dzcjIvR1RTMU8x\n"
star297 0:768ae9838086 31 "LmNydDArBgNVHREEJDAigg5maXJlYmFzZWlvLmNvbYIQKi5maXJlYmFzZWlvLmNv\n"
star297 0:768ae9838086 32 "bTAhBgNVHSAEGjAYMAgGBmeBDAECAjAMBgorBgEEAdZ5AgUDMC8GA1UdHwQoMCYw\n"
star297 0:768ae9838086 33 "JKAioCCGHmh0dHA6Ly9jcmwucGtpLmdvb2cvR1RTMU8xLmNybDCCAQIGCisGAQQB\n"
star297 0:768ae9838086 34 "1nkCBAIEgfMEgfAA7gB1ALIeBcyLos2KIE6HZvkruYolIGdr2vpw57JJUy3vi5Be\n"
star297 0:768ae9838086 35 "AAABaXkuFzgAAAQDAEYwRAIgVssWhLEYItcpljMfJfkBhnxUrw32ik3C7IyBhkS6\n"
star297 0:768ae9838086 36 "jQcCIAHzoq/0QsRXY8RITJ5z6pThPzJhiSeLhWb6v2MDs6+NAHUAXqdz+d9WwOe1\n"
star297 0:768ae9838086 37 "Nkh90EngMnqRmgyEoRIShBh1loFxRVgAAAFpeS4XEQAABAMARjBEAiBgwY3jVuyU\n"
star297 0:768ae9838086 38 "HTDrfXt7Hhq3AL9kD8hASq/qIXFvHAel1AIgV2DY0UI7PvVl2uHicvaL9BfUPQjP\n"
star297 0:768ae9838086 39 "8/7b5W5jd6HEKbowDQYJKoZIhvcNAQELBQADggEBAAFG/f9zTyPTiYjjbPKpIC3T\n"
star297 0:768ae9838086 40 "MMD8dWvny1DJpL2fQPMcpAkkduW7sFG8mLRh14mSBsWqAusj99nAKt6NJhxZewhp\n"
star297 0:768ae9838086 41 "JKwbcawanW+w2DUzFr0FauVxNIs4xPxcgkCNAGeUj8ZI+cDcSaMOY4WxNrjOgbbI\n"
star297 0:768ae9838086 42 "feOxHsCmIWxch6/vNVxh+54BZFCy723DL8Fo/tDRU73b+gxPJusJva4ABuXSqSFT\n"
star297 0:768ae9838086 43 "EDaRGzdj8AKkoDmRkWLcEg/6a/2dfw8VV7fIAlqVxNQwZWZLtIsBGazxZ5xflrVz\n"
star297 0:768ae9838086 44 "I5AHejNR7guDaWAimhkSOlG11W8BzPtdAo/xJ6JyI4K5kPGIXucniVRmQfmojxc=\n"
star297 0:768ae9838086 45 "-----END CERTIFICATE-----\n"
star297 0:768ae9838086 46 "-----BEGIN CERTIFICATE-----\n"
star297 0:768ae9838086 47 "MIIESjCCAzKgAwIBAgINAeO0mqGNiqmBJWlQuDANBgkqhkiG9w0BAQsFADBMMSAw\n"
star297 0:768ae9838086 48 "HgYDVQQLExdHbG9iYWxTaWduIFJvb3QgQ0EgLSBSMjETMBEGA1UEChMKR2xvYmFs\n"
star297 0:768ae9838086 49 "U2lnbjETMBEGA1UEAxMKR2xvYmFsU2lnbjAeFw0xNzA2MTUwMDAwNDJaFw0yMTEy\n"
star297 0:768ae9838086 50 "MTUwMDAwNDJaMEIxCzAJBgNVBAYTAlVTMR4wHAYDVQQKExVHb29nbGUgVHJ1c3Qg\n"
star297 0:768ae9838086 51 "U2VydmljZXMxEzARBgNVBAMTCkdUUyBDQSAxTzEwggEiMA0GCSqGSIb3DQEBAQUA\n"
star297 0:768ae9838086 52 "A4IBDwAwggEKAoIBAQDQGM9F1IvN05zkQO9+tN1pIRvJzzyOTHW5DzEZhD2ePCnv\n"
star297 0:768ae9838086 53 "UA0Qk28FgICfKqC9EksC4T2fWBYk/jCfC3R3VZMdS/dN4ZKCEPZRrAzDsiKUDzRr\n"
star297 0:768ae9838086 54 "mBBJ5wudgzndIMYcLe/RGGFl5yODIKgjEv/SJH/UL+dEaltN11BmsK+eQmMF++Ac\n"
star297 0:768ae9838086 55 "xGNhr59qM/9il71I2dN8FGfcddwuaej4bXhp0LcQBbjxMcI7JP0aM3T4I+DsaxmK\n"
star297 0:768ae9838086 56 "FsbjzaTNC9uzpFlgOIg7rR25xoynUxv8vNmkq7zdPGHXkxWY7oG9j+JkRyBABk7X\n"
star297 0:768ae9838086 57 "rJfoucBZEqFJJSPk7XA0LKW0Y3z5oz2D0c1tJKwHAgMBAAGjggEzMIIBLzAOBgNV\n"
star297 0:768ae9838086 58 "HQ8BAf8EBAMCAYYwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMCMBIGA1Ud\n"
star297 0:768ae9838086 59 "EwEB/wQIMAYBAf8CAQAwHQYDVR0OBBYEFJjR+G4Q68+b7GCfGJAboOt9Cf0rMB8G\n"
star297 0:768ae9838086 60 "A1UdIwQYMBaAFJviB1dnHB7AagbeWbSaLd/cGYYuMDUGCCsGAQUFBwEBBCkwJzAl\n"
star297 0:768ae9838086 61 "BggrBgEFBQcwAYYZaHR0cDovL29jc3AucGtpLmdvb2cvZ3NyMjAyBgNVHR8EKzAp\n"
star297 0:768ae9838086 62 "MCegJaAjhiFodHRwOi8vY3JsLnBraS5nb29nL2dzcjIvZ3NyMi5jcmwwPwYDVR0g\n"
star297 0:768ae9838086 63 "BDgwNjA0BgZngQwBAgIwKjAoBggrBgEFBQcCARYcaHR0cHM6Ly9wa2kuZ29vZy9y\n"
star297 0:768ae9838086 64 "ZXBvc2l0b3J5LzANBgkqhkiG9w0BAQsFAAOCAQEAGoA+Nnn78y6pRjd9XlQWNa7H\n"
star297 0:768ae9838086 65 "TgiZ/r3RNGkmUmYHPQq6Scti9PEajvwRT2iWTHQr02fesqOqBY2ETUwgZQ+lltoN\n"
star297 0:768ae9838086 66 "FvhsO9tvBCOIazpswWC9aJ9xju4tWDQH8NVU6YZZ/XteDSGU9YzJqPjY8q3MDxrz\n"
star297 0:768ae9838086 67 "mqepBCf5o8mw/wJ4a2G6xzUr6Fb6T8McDO22PLRL6u3M4Tzs3A2M1j6bykJYi8wW\n"
star297 0:768ae9838086 68 "IRdAvKLWZu/axBVbzYmqmwkm5zLSDW5nIAJbELCQCZwMH56t2Dvqofxs6BBcCFIZ\n"
star297 0:768ae9838086 69 "USpxu6x6td0V7SvJCCosirSmIatj/9dSSVDQibet8q/7UK4v4ZUN80atnZz1yg==\n"
star297 0:768ae9838086 70 "-----END CERTIFICATE-----\n";
star297 0:768ae9838086 71
star297 0:768ae9838086 72 /*
star297 0:768ae9838086 73 // how to build the Firebase connection FirebaseUrl string for 'method overide'
star297 0:768ae9838086 74
star297 0:768ae9838086 75 strcpy(FirebaseUrl, FirebaseID); // Firebase account ID
star297 0:768ae9838086 76
star297 0:768ae9838086 77 // this bit in the middle to send .json data structure and authority
star297 0:768ae9838086 78 // for any REST api functions e.g. GET, PUT, POST, PATCH, DELETE
star297 0:768ae9838086 79 // replace the PUT with required function
star297 0:768ae9838086 80
star297 0:768ae9838086 81 strcat(FirebaseUrl, "/Parent/Child/.json?x-http-method-override=PUT&auth=");
star297 0:768ae9838086 82 strcat(FirebaseUrl, FirebaseAuth); // Firebase account authorisation key
star297 0:768ae9838086 83 */
star297 0:768ae9838086 84
star297 0:768ae9838086 85 // set Firebase project information..
star297 0:768ae9838086 86 char FirebaseUrl[300]; // dimension to suit required character space
star297 0:768ae9838086 87 const char FirebaseID[100] = "https://projectID.firebaseio.com"; // project ID
star297 0:768ae9838086 88 const char FirebaseAuth[100]= "Web API key"; // web API key
star297 0:768ae9838086 89
star297 0:768ae9838086 90 char* getData;
star297 0:768ae9838086 91
star297 0:768ae9838086 92 /*
star297 0:768ae9838086 93 Connects to the network using the mbed_app.json ESP8266 WIFI networking interface,
star297 0:768ae9838086 94 you can also swap this out with a driver for a different networking interface or
star297 0:768ae9838086 95 if you use ETHERNET: change mbed_app.json "target.network-default-interface-type" : "ETHERNET",
star297 0:768ae9838086 96 */
star297 0:768ae9838086 97
star297 0:768ae9838086 98 NetworkInterface* network;
star297 0:768ae9838086 99 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 100 nsapi_error_t r;
star297 0:768ae9838086 101
star297 0:768ae9838086 102 NetworkInterface *connect_to_default_network_interface() {
star297 0:768ae9838086 103 printf("Connecting to network...\n\n");
star297 0:768ae9838086 104
star297 0:768ae9838086 105 NetworkInterface* network = NetworkInterface::get_default_instance();
star297 0:768ae9838086 106
star297 0:768ae9838086 107 if (!network) {
star297 0:768ae9838086 108 printf("No network interface found, select an interface in 'mbed_app.json'\n");
star297 0:768ae9838086 109 return NULL;
star297 0:768ae9838086 110 }
star297 0:768ae9838086 111 nsapi_error_t connect_status = network->connect();
star297 0:768ae9838086 112 if (connect_status != NSAPI_ERROR_OK) {
star297 0:768ae9838086 113 printf("Failed to connect to network (%d)\n", connect_status);
star297 0:768ae9838086 114 return NULL;
star297 0:768ae9838086 115 }
star297 0:768ae9838086 116 printf("Connected to the network!\n");
star297 0:768ae9838086 117 printf("IP address: %s\n", network->get_ip_address());
star297 0:768ae9838086 118 printf("MAC: %s\n", network->get_mac_address());
star297 0:768ae9838086 119 printf("Netmask: %s\n", network->get_netmask());
star297 0:768ae9838086 120 printf("Gateway: %s\n", network->get_gateway());
star297 0:768ae9838086 121 return network;
star297 0:768ae9838086 122 }
star297 0:768ae9838086 123
star297 0:768ae9838086 124 void startTLSreusesocket(char *FirebaseID) {
star297 0:768ae9838086 125
star297 0:768ae9838086 126 if ((r = socket->open(network)) != NSAPI_ERROR_OK) {
star297 0:768ae9838086 127 printf("TLS socket open failed (%d)\n", r);
star297 0:768ae9838086 128 }
star297 0:768ae9838086 129 if ((r = socket->set_root_ca_cert(SSL_CA_PEM)) != NSAPI_ERROR_OK) {
star297 0:768ae9838086 130 printf("TLS socket set_root_ca_cert failed (%d)\n", r);
star297 0:768ae9838086 131 }
star297 0:768ae9838086 132 if ((r = socket->connect(FirebaseID, 443)) != NSAPI_ERROR_OK) {
star297 0:768ae9838086 133 printf("TLS socket connect failed (%d)\n", r);
star297 0:768ae9838086 134 }
star297 0:768ae9838086 135 }
star297 0:768ae9838086 136
star297 0:768ae9838086 137 void dump_response(HttpResponse* res) {
star297 0:768ae9838086 138 mbedtls_printf("Status: %d - %s\n", res->get_status_code(), res->get_status_message().c_str());
star297 0:768ae9838086 139 mbedtls_printf("Headers:\n");
star297 0:768ae9838086 140 for (size_t ix = 0; ix < res->get_headers_length(); ix++) {
star297 0:768ae9838086 141 mbedtls_printf("\t%s: %s\n", res->get_headers_fields()[ix]->c_str(), res->get_headers_values()[ix]->c_str());
star297 0:768ae9838086 142 }
star297 0:768ae9838086 143 mbedtls_printf("\nBody (%d bytes):\n\n%s\n", res->get_body_length(), res->get_body_as_string().c_str());
star297 0:768ae9838086 144 }
star297 0:768ae9838086 145
star297 0:768ae9838086 146 char *getFirebase(char* FirebaseUrl)
star297 0:768ae9838086 147 {
star297 0:768ae9838086 148 //HttpsRequest* get_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_GET, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 149 HttpsRequest* get_req = new HttpsRequest(socket, HTTP_GET, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 150 HttpResponse* get_res = get_req->send();
star297 0:768ae9838086 151 if (!get_res) {
star297 0:768ae9838086 152 time_t seconds = time(NULL);
star297 0:768ae9838086 153 printf("Https GET failed (error code %d), %s", get_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 154 socket->close();
star297 0:768ae9838086 155 delete socket;
star297 0:768ae9838086 156 delete get_req;
star297 0:768ae9838086 157 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 158 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 159 return 0;}
star297 0:768ae9838086 160 else{
star297 0:768ae9838086 161 //dump_response(get_res);
star297 0:768ae9838086 162 delete get_req;
star297 0:768ae9838086 163 getData = (char*)get_res->get_body_as_string().c_str();
star297 0:768ae9838086 164 return getData;
star297 0:768ae9838086 165 }
star297 0:768ae9838086 166 }
star297 0:768ae9838086 167
star297 0:768ae9838086 168 bool putFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 169 {
star297 0:768ae9838086 170 //HttpsRequest* put_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_PUT, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 171 HttpsRequest* put_req = new HttpsRequest(socket, HTTP_PUT, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 172 put_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 173 HttpResponse* put_res = put_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 174 if (!put_res) {
star297 0:768ae9838086 175 time_t seconds = time(NULL);
star297 0:768ae9838086 176 printf("Https PUT failed (error code %d), %s", put_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 177 socket->close();
star297 0:768ae9838086 178 delete socket;
star297 0:768ae9838086 179 delete put_req;
star297 0:768ae9838086 180 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 181 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 182 return 0;
star297 0:768ae9838086 183 }
star297 0:768ae9838086 184 else{
star297 0:768ae9838086 185 // dump_response(put_res);
star297 0:768ae9838086 186 delete put_req;
star297 0:768ae9838086 187 return put_res;
star297 0:768ae9838086 188 }
star297 0:768ae9838086 189 }
star297 0:768ae9838086 190
star297 0:768ae9838086 191 bool postFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 192 {
star297 0:768ae9838086 193 // HttpsRequest* post_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_POST, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 194 HttpsRequest* post_req = new HttpsRequest(socket, HTTP_POST, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 195 post_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 196 HttpResponse* post_res = post_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 197 if (!post_res) {
star297 0:768ae9838086 198 time_t seconds = time(NULL);
star297 0:768ae9838086 199 printf("Https POST failed (error code %d), %s", post_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 200 socket->close();
star297 0:768ae9838086 201 delete socket;
star297 0:768ae9838086 202 delete post_req;
star297 0:768ae9838086 203 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 204 startTLSreusesocket((char*)FirebaseID); // restart TLS reuse socket if failure
star297 0:768ae9838086 205 return 0;
star297 0:768ae9838086 206 }
star297 0:768ae9838086 207 else{
star297 0:768ae9838086 208 //dump_response(post_res);
star297 0:768ae9838086 209 delete post_req;
star297 0:768ae9838086 210 return post_res;
star297 0:768ae9838086 211 }
star297 0:768ae9838086 212 }
star297 0:768ae9838086 213
star297 0:768ae9838086 214 bool deleteFirebase(char* FirebaseUrl, char *stringToProcess)
star297 0:768ae9838086 215 {
star297 0:768ae9838086 216 // HttpsRequest* delete_req = new HttpsRequest(network, SSL_CA_PEM, HTTP_DELETE, FirebaseUrl); // non socket reuse function
star297 0:768ae9838086 217 HttpsRequest* delete_req = new HttpsRequest(socket, HTTP_DELETE, FirebaseUrl); // socket reuse function
star297 0:768ae9838086 218 delete_req->set_header("Content-Type", "application/json");
star297 0:768ae9838086 219 HttpResponse* delete_res = delete_req->send(stringToProcess, strlen(stringToProcess));
star297 0:768ae9838086 220 if (!delete_res) {
star297 0:768ae9838086 221 time_t seconds = time(NULL);
star297 0:768ae9838086 222 printf("Https DELETE failed (error code %d), %s", delete_req->get_error(), ctime(&seconds));
star297 0:768ae9838086 223 socket->close();
star297 0:768ae9838086 224 delete socket;
star297 0:768ae9838086 225 delete delete_req;
star297 0:768ae9838086 226 TLSSocket* socket = new TLSSocket();
star297 0:768ae9838086 227 startTLSreusesocket((char*)FirebaseID); // restart TLS socket reuse if failure
star297 0:768ae9838086 228 return 0;
star297 0:768ae9838086 229 }
star297 0:768ae9838086 230 else{
star297 0:768ae9838086 231 //dump_response(delete_res);
star297 0:768ae9838086 232 delete delete_req;
star297 0:768ae9838086 233 return delete_res;
star297 0:768ae9838086 234 }
star297 0:768ae9838086 235 }
star297 0:768ae9838086 236
star297 0:768ae9838086 237 #endif // _FIREBASE_H_