Brandon Fictorie / Mbed 2 deprecated BF_Websocket

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Websocket.h Source File

Websocket.h

00001 /**
00002  * @author Samuel Mokrani
00003  *
00004  * @section LICENSE
00005  *
00006  * Copyright (c) 2011 mbed
00007  *
00008  * Permission is hereby granted, free of charge, to any person obtaining a copy
00009  * of this software and associated documentation files (the "Software"), to deal
00010  * in the Software without restriction, including without limitation the rights
00011  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00012  * copies of the Software, and to permit persons to whom the Software is
00013  * furnished to do so, subject to the following conditions:
00014  *
00015  * The above copyright notice and this permission notice shall be included in
00016  * all copies or substantial portions of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00019  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00020  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00021  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00022  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00023  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00024  * THE SOFTWARE.
00025  *
00026  * @section DESCRIPTION
00027  *    Simple websocket client
00028  *
00029  */
00030 
00031 #ifndef WEBSOCKET_H
00032 #define WEBSOCKET_H
00033 
00034 #include "mbed.h"
00035 #include <string>
00036 
00037 #ifdef TARGET_LPC1768
00038 #include "EthernetNetIf.h"
00039 #include "TCPSocket.h"
00040 #include "dnsresolve.h"
00041 #endif //target
00042 /** Websocket client Class.
00043  *
00044  * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class
00045  *
00046  * Example (wifi network):
00047  * @code
00048  * #include "mbed.h"
00049  * #include "Wifly.h"
00050  * #include "Websocket.h"
00051  *
00052  * DigitalOut l1(LED1);
00053  *
00054  * //Here, we create an instance, with pins 9 and 10 connecting to the
00055  * //WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the
00056  * //"mbed" network, password "password", and we are using WPA.
00057  * Wifly wifly(p9, p10, p21, "mbed", "password", true);
00058  *
00059  * //Here, we create a Websocket instance in 'wo' (write-only) mode
00060  * //on the 'samux' channel
00061  * Websocket ws("ws://sockets.mbed.org/ws/samux/wo", &wifly);
00062  *
00063  *
00064  * int main() {
00065  *     while (1) {
00066  *
00067  *         //we connect the network
00068  *         while (!wifly.join()) {
00069  *             wifly.reset();
00070  *         }
00071  *
00072  *         //we connect to the websocket server
00073  *         while (!ws.connect());
00074  *
00075  *         while (1) {
00076  *             wait(0.5);
00077  *
00078  *             //Send Hello world
00079  *             ws.send("Hello World! over Wifi");
00080  *
00081  *             // show that we are alive
00082  *             l1 = !l1;
00083  *         }
00084  *     }
00085  * }
00086  * @endcode
00087  *
00088  *
00089  *
00090  * Example (ethernet network):
00091  * @code
00092  * #include "mbed.h"
00093  * #include "Websocket.h"
00094  *
00095  * Timer tmr;
00096  *
00097  * //Here, we create a Websocket instance in 'wo' (write-only) mode
00098  * //on the 'samux' channel
00099  * Websocket ws("ws://sockets.mbed.org/ws/samux/wo");
00100  *
00101  * int main() {
00102  *     while (1) {
00103  *
00104  *         while (!ws.connect());
00105  *
00106  *         tmr.start();
00107  *         while (1) {
00108  *             if (tmr.read() > 0.5) {
00109  *                 ws.send("Hello World! over Ethernet");
00110  *                 tmr.start();
00111  *             }
00112  *             Net::poll();
00113  *         }
00114  *     }
00115  * }
00116  * @endcode
00117  */
00118 class Websocket {
00119 public:
00120     /**
00121      * Constructor
00122      *
00123      * @param url The Websocket url in the form "ws://ip_domain[:port]/path"  (by default: port = 80)
00124      * @param wifi pointer to a wifi module (the communication will be establish by this module)
00125      */
00126 
00127 #ifdef TARGET_LPC1768
00128     /**
00129      * Constructor for an ethernet communication
00130      *
00131      * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
00132      */
00133     Websocket(char * url);
00134 #endif //target
00135     /**
00136      * Connect to the websocket url
00137      *
00138      *@return true if the connection is established, false otherwise
00139      */
00140     bool connect();
00141 
00142     /**
00143      * Send a string according to the websocket format
00144      *
00145      * @param str string to be sent
00146      */
00147     void send(char * str);
00148 
00149     /**
00150      * Read a websocket message
00151      *
00152      * @param message pointer to the string to be read (null if drop frame)
00153      *
00154      * @return true if a string has been read, false otherwise
00155      */
00156     bool read(char * message);
00157 
00158     /**
00159      * To see if there is a websocket connection active
00160      *
00161      * @return true if there is a connection active
00162      */
00163     bool connected();
00164 
00165     /**
00166      * Close the websocket connection
00167      *
00168      * @return true if the connection has been closed, false otherwise
00169      */
00170     bool close();
00171 
00172     /**
00173      * Accessor: get path from the websocket url
00174      *
00175      * @return path
00176      */
00177     std::string getPath();
00178 
00179     enum Type {
00180         ETH
00181     };
00182 
00183 private:
00184 
00185     void fillFields(char * url);
00186     void sendOpcode(uint8_t opcode);
00187     void sendLength(uint32_t len);
00188     void sendMask();
00189     void sendChar(uint8_t c);
00190 
00191     std::string ip_domain;
00192     std::string path;
00193     std::string port;
00194 
00195 #ifdef TARGET_LPC1768
00196     void onTCPSocketEvent(TCPSocketEvent e);
00197     bool eth_connected;
00198     bool eth_readable;
00199     bool eth_writeable;
00200     char eth_rx[512];
00201     bool response_server_eth;
00202     bool new_msg;
00203 
00204     EthernetNetIf * eth;
00205     TCPSocket * sock;
00206     IpAddr * server_ip;
00207 #endif //target
00208     Type netif;
00209 
00210 };
00211 
00212 #endif