Renesas / SecureDweet
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SecureDweet.h Source File

SecureDweet.h

00001 /*
00002  * SecureDweet.h
00003  *
00004  *  Created on: Aug 15, 2016
00005  *      Author: Faheem Inayat
00006  * Created for: Renesas Electronics America HQ, Santa Clara, CA, USA
00007  *
00008  * Copyright (c) 2016 Renesas Electronics America (REA) and Faheem Inayat
00009  */
00010 /*
00011  * MIT License
00012  *
00013  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00014  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00015  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00016  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00017  * furnished to do so, subject to the following conditions:
00018  *
00019  * The above copyright notice and this permission notice shall be included in all copies or
00020  * substantial portions of the Software.
00021  *
00022  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00023  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00024  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00025  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00026  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00027  */
00028  
00029  /**
00030     Example
00031     
00032     @code
00033 
00034     #include "mbed.h"
00035     #include "rtos.h"
00036     #include "EthernetInterface.h"
00037     #include "string.h"
00038     #include "GRPeachBoard.h"
00039     #include "SecureDweet.h"
00040 
00041     #define THING_NAME  "_YOUR_THING_NAME_HERE_"
00042     #define WRITE_KEY   "_OPTIONAL_WRITE_KEY_IF_THING_IS_LOCKED_"
00043     #define READ_KEY    "_OPTIONAL_READ_KEY_IF_THING_IS_LOCKED_"
00044 
00045     void printAndFlush(void const * msg)
00046     {
00047         fprintf ( stdout, "%s", (char *) msg);
00048         fflush ( stdout);
00049     }
00050     
00051     bool setupEthernet(EthernetInterface * & ethernet)
00052     {
00053         printAndFlush ("\n\rConnecting Ethernet ...");
00054     
00055         RtosTimer * connectProgressTimer = new RtosTimer (printAndFlush, osTimerPeriodic, (void *) ".");
00056         connectProgressTimer->start (500);
00057     
00058         int err = ethernet->init ();
00059         char errMsg[256];
00060         if (err == 0)
00061         {
00062             err = ethernet->connect ();
00063             if (err == 0)
00064             {
00065                 sprintf (
00066                         errMsg,
00067                         "\n\rEthernet connected with specs:\n\r\tIP Address:  %s\n\r\tSubnet Mask: %s\n\r\tGateway:     %s\n\r\tMAC Address: %s\n\r",
00068                         ethernet->getIPAddress (), ethernet->getNetworkMask (), ethernet->getGateway (),
00069                         ethernet->getMACAddress ());
00070             }
00071             else
00072             {
00073                 sprintf (errMsg, " Ethernet connect failed with error: %d\n\r", err);
00074             }
00075         }
00076         else
00077         {
00078             sprintf (errMsg, " Ethernet init failed with error: %d\n\r", err);
00079         }
00080     
00081         printAndFlush (errMsg);
00082         connectProgressTimer->stop ();
00083         delete connectProgressTimer;
00084     
00085         return (err == 0);
00086     }
00087     
00088     int main(void)
00089     {
00090         EthernetInterface * ethernet = new EthernetInterface ();
00091         IDweetParser * device = new GRPeachBoard (); // Implementation of IDweetParser
00092     
00093         SecureDweet * dweetListener = new SecureDweet (device, THING_NAME, READ_KEY);
00094     
00095         while (true)
00096         {
00097             setupEthernet (ethernet);
00098     
00099             dweetListener -> fetchLatestDweet ();
00100             dweetListener -> listenToDweet(); // this is an infinite blocking function call
00101     
00102             // In another thread, use the following function call to stop listening
00103             // to dweets (exit from the above function call)
00104             // dweetListener -> stopListentingToDweet();
00105         }
00106     }
00107 
00108     @endcode
00109  */
00110 
00111 #ifndef __SECUREDWEET_H_
00112     #define __SECUREDWEET_H_
00113 
00114 #include "mbed.h"
00115 #include "stdio.h"
00116 #include "HttpText.h"
00117 #include "IDweetParser.h"
00118 #include "SecureHttpClient.h"
00119 
00120 class SecureDweet : public ISecureHttpClientChunkDataListener
00121 {
00122     private:
00123         IDweetParser * device;
00124 
00125         const char * thingName;
00126         const char * readKey;
00127         const char * writeKey;
00128 
00129         char * latestUrl;
00130         char * listenUrl;
00131 
00132         SecureHttpClient * httpClient;
00133         char * httpRxBuffer;
00134         HttpText * httpText;
00135 
00136         bool listeningFlag;
00137 
00138         SecureDweet(const SecureDweet & other);
00139 
00140     public:
00141         SecureDweet ( IDweetParser * _device, const char * _thingName, const char * _readKey = NULL, const char * _writeKey = NULL );
00142         virtual ~SecureDweet ();
00143 
00144         virtual void chunkRead ( IHttpDataIn * pDataIn );
00145 
00146         void fetchLatestDweet();
00147         void listenToDweet();
00148         void stopListentingToDweet ();
00149 };
00150 
00151 #endif /* __SECUREDWEET_H_ */
00152