Arianna autonomous DAQ firmware
Dependencies: mbed SDFileSystemFilinfo AriSnProtocol NetServicesMin AriSnComm MODSERIAL PowerControlClkPatch DS1820OW
Websocket.h@110:d1da040a0cf2, 2015-11-24 (annotated)
- Committer:
- uci1
- Date:
- Tue Nov 24 21:52:27 2015 +0000
- Revision:
- 110:d1da040a0cf2
- Parent:
- 6:6f002d202f59
Stn32 (slow) with conf name. SD stall fix. No interface chip. Safety nets.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
uci1 | 0:664899e0b988 | 1 | /** |
uci1 | 0:664899e0b988 | 2 | * @author Samuel Mokrani |
uci1 | 0:664899e0b988 | 3 | * |
uci1 | 0:664899e0b988 | 4 | * @section LICENSE |
uci1 | 0:664899e0b988 | 5 | * |
uci1 | 0:664899e0b988 | 6 | * Copyright (c) 2011 mbed |
uci1 | 0:664899e0b988 | 7 | * |
uci1 | 0:664899e0b988 | 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
uci1 | 0:664899e0b988 | 9 | * of this software and associated documentation files (the "Software"), to deal |
uci1 | 0:664899e0b988 | 10 | * in the Software without restriction, including without limitation the rights |
uci1 | 0:664899e0b988 | 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
uci1 | 0:664899e0b988 | 12 | * copies of the Software, and to permit persons to whom the Software is |
uci1 | 0:664899e0b988 | 13 | * furnished to do so, subject to the following conditions: |
uci1 | 0:664899e0b988 | 14 | * |
uci1 | 0:664899e0b988 | 15 | * The above copyright notice and this permission notice shall be included in |
uci1 | 0:664899e0b988 | 16 | * all copies or substantial portions of the Software. |
uci1 | 0:664899e0b988 | 17 | * |
uci1 | 0:664899e0b988 | 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
uci1 | 0:664899e0b988 | 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
uci1 | 0:664899e0b988 | 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
uci1 | 0:664899e0b988 | 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
uci1 | 0:664899e0b988 | 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
uci1 | 0:664899e0b988 | 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
uci1 | 0:664899e0b988 | 24 | * THE SOFTWARE. |
uci1 | 0:664899e0b988 | 25 | * |
uci1 | 0:664899e0b988 | 26 | * @section DESCRIPTION |
uci1 | 0:664899e0b988 | 27 | * Simple websocket client |
uci1 | 0:664899e0b988 | 28 | * |
uci1 | 0:664899e0b988 | 29 | */ |
uci1 | 0:664899e0b988 | 30 | |
uci1 | 0:664899e0b988 | 31 | #ifndef WEBSOCKET_H |
uci1 | 0:664899e0b988 | 32 | #define WEBSOCKET_H |
uci1 | 0:664899e0b988 | 33 | |
uci1 | 6:6f002d202f59 | 34 | #ifdef IGNORE_THIS_FILE |
uci1 | 6:6f002d202f59 | 35 | |
uci1 | 0:664899e0b988 | 36 | #include "mbed.h" |
uci1 | 0:664899e0b988 | 37 | //#include "Wifly.h" |
uci1 | 0:664899e0b988 | 38 | #include <string> |
uci1 | 0:664899e0b988 | 39 | |
uci1 | 0:664899e0b988 | 40 | |
uci1 | 0:664899e0b988 | 41 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 42 | #include "EthernetNetIf.h" |
uci1 | 0:664899e0b988 | 43 | #include "TCPSocket.h" |
uci1 | 0:664899e0b988 | 44 | #include "dnsresolve.h" |
uci1 | 0:664899e0b988 | 45 | #endif //target |
uci1 | 0:664899e0b988 | 46 | |
uci1 | 3:24c5f0f50bf1 | 47 | #define RX_BUF_SIZE 1024 |
uci1 | 3:24c5f0f50bf1 | 48 | #define RX_RECV_BYTES 512 |
uci1 | 0:664899e0b988 | 49 | |
uci1 | 0:664899e0b988 | 50 | /** Websocket client Class. |
uci1 | 0:664899e0b988 | 51 | * |
uci1 | 0:664899e0b988 | 52 | * Warning: you must use a wifi module (Wifly RN131-C) or an ethernet network to use this class |
uci1 | 0:664899e0b988 | 53 | * |
uci1 | 0:664899e0b988 | 54 | * Example (wifi network): |
uci1 | 0:664899e0b988 | 55 | * @code |
uci1 | 0:664899e0b988 | 56 | * #include "mbed.h" |
uci1 | 0:664899e0b988 | 57 | * #include "Wifly.h" |
uci1 | 0:664899e0b988 | 58 | * #include "Websocket.h" |
uci1 | 0:664899e0b988 | 59 | * |
uci1 | 0:664899e0b988 | 60 | * DigitalOut l1(LED1); |
uci1 | 0:664899e0b988 | 61 | * |
uci1 | 0:664899e0b988 | 62 | * //Here, we create an instance, with pins 9 and 10 connecting to the |
uci1 | 0:664899e0b988 | 63 | * //WiFly's TX and RX pins, and pin 21 to RESET. We are connecting to the |
uci1 | 0:664899e0b988 | 64 | * //"mbed" network, password "password", and we are using WPA. |
uci1 | 0:664899e0b988 | 65 | * Wifly wifly(p9, p10, p21, "mbed", "password", true); |
uci1 | 0:664899e0b988 | 66 | * |
uci1 | 0:664899e0b988 | 67 | * //Here, we create a Websocket instance in 'wo' (write-only) mode |
uci1 | 0:664899e0b988 | 68 | * //on the 'samux' channel |
uci1 | 0:664899e0b988 | 69 | * Websocket ws("ws://sockets.mbed.org/ws/samux/wo", &wifly); |
uci1 | 0:664899e0b988 | 70 | * |
uci1 | 0:664899e0b988 | 71 | * |
uci1 | 0:664899e0b988 | 72 | * int main() { |
uci1 | 0:664899e0b988 | 73 | * while (1) { |
uci1 | 0:664899e0b988 | 74 | * |
uci1 | 0:664899e0b988 | 75 | * //we connect the network |
uci1 | 0:664899e0b988 | 76 | * while (!wifly.join()) { |
uci1 | 0:664899e0b988 | 77 | * wifly.reset(); |
uci1 | 0:664899e0b988 | 78 | * } |
uci1 | 0:664899e0b988 | 79 | * |
uci1 | 0:664899e0b988 | 80 | * //we connect to the websocket server |
uci1 | 0:664899e0b988 | 81 | * while (!ws.connect()); |
uci1 | 0:664899e0b988 | 82 | * |
uci1 | 0:664899e0b988 | 83 | * while (1) { |
uci1 | 0:664899e0b988 | 84 | * wait(0.5); |
uci1 | 0:664899e0b988 | 85 | * |
uci1 | 0:664899e0b988 | 86 | * //Send Hello world |
uci1 | 0:664899e0b988 | 87 | * ws.send("Hello World! over Wifi"); |
uci1 | 0:664899e0b988 | 88 | * |
uci1 | 0:664899e0b988 | 89 | * // show that we are alive |
uci1 | 0:664899e0b988 | 90 | * l1 = !l1; |
uci1 | 0:664899e0b988 | 91 | * } |
uci1 | 0:664899e0b988 | 92 | * } |
uci1 | 0:664899e0b988 | 93 | * } |
uci1 | 0:664899e0b988 | 94 | * @endcode |
uci1 | 0:664899e0b988 | 95 | * |
uci1 | 0:664899e0b988 | 96 | * |
uci1 | 0:664899e0b988 | 97 | * |
uci1 | 0:664899e0b988 | 98 | * Example (ethernet network): |
uci1 | 0:664899e0b988 | 99 | * @code |
uci1 | 0:664899e0b988 | 100 | * #include "mbed.h" |
uci1 | 0:664899e0b988 | 101 | * #include "Websocket.h" |
uci1 | 0:664899e0b988 | 102 | * |
uci1 | 0:664899e0b988 | 103 | * Timer tmr; |
uci1 | 0:664899e0b988 | 104 | * |
uci1 | 0:664899e0b988 | 105 | * //Here, we create a Websocket instance in 'wo' (write-only) mode |
uci1 | 0:664899e0b988 | 106 | * //on the 'samux' channel |
uci1 | 0:664899e0b988 | 107 | * Websocket ws("ws://sockets.mbed.org/ws/samux/wo"); |
uci1 | 0:664899e0b988 | 108 | * |
uci1 | 0:664899e0b988 | 109 | * int main() { |
uci1 | 0:664899e0b988 | 110 | * while (1) { |
uci1 | 0:664899e0b988 | 111 | * |
uci1 | 0:664899e0b988 | 112 | * while (!ws.connect()); |
uci1 | 0:664899e0b988 | 113 | * |
uci1 | 0:664899e0b988 | 114 | * tmr.start(); |
uci1 | 0:664899e0b988 | 115 | * while (1) { |
uci1 | 0:664899e0b988 | 116 | * if (tmr.read() > 0.5) { |
uci1 | 0:664899e0b988 | 117 | * ws.send("Hello World! over Ethernet"); |
uci1 | 0:664899e0b988 | 118 | * tmr.start(); |
uci1 | 0:664899e0b988 | 119 | * } |
uci1 | 0:664899e0b988 | 120 | * Net::poll(); |
uci1 | 0:664899e0b988 | 121 | * } |
uci1 | 0:664899e0b988 | 122 | * } |
uci1 | 0:664899e0b988 | 123 | * } |
uci1 | 0:664899e0b988 | 124 | * @endcode |
uci1 | 0:664899e0b988 | 125 | */ |
uci1 | 0:664899e0b988 | 126 | class Websocket |
uci1 | 0:664899e0b988 | 127 | { |
uci1 | 0:664899e0b988 | 128 | public: |
uci1 | 0:664899e0b988 | 129 | /** |
uci1 | 0:664899e0b988 | 130 | * Constructor |
uci1 | 0:664899e0b988 | 131 | * |
uci1 | 0:664899e0b988 | 132 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
uci1 | 0:664899e0b988 | 133 | * @param wifi pointer to a wifi module (the communication will be establish by this module) |
uci1 | 0:664899e0b988 | 134 | */ |
uci1 | 0:664899e0b988 | 135 | // Websocket(char * url, Wifly * wifi); |
uci1 | 0:664899e0b988 | 136 | |
uci1 | 0:664899e0b988 | 137 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 138 | /** |
uci1 | 0:664899e0b988 | 139 | * Constructor for an ethernet communication |
uci1 | 0:664899e0b988 | 140 | * |
uci1 | 0:664899e0b988 | 141 | * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80) |
uci1 | 0:664899e0b988 | 142 | */ |
uci1 | 0:664899e0b988 | 143 | Websocket(char * url); |
uci1 | 0:664899e0b988 | 144 | #endif //target |
uci1 | 0:664899e0b988 | 145 | |
uci1 | 0:664899e0b988 | 146 | /** |
uci1 | 0:664899e0b988 | 147 | * Connect to the websocket url |
uci1 | 0:664899e0b988 | 148 | * |
uci1 | 0:664899e0b988 | 149 | *@return true if the connection is established, false otherwise |
uci1 | 0:664899e0b988 | 150 | */ |
uci1 | 3:24c5f0f50bf1 | 151 | bool connect(const uint32_t timeout); |
uci1 | 0:664899e0b988 | 152 | |
uci1 | 0:664899e0b988 | 153 | /** |
uci1 | 0:664899e0b988 | 154 | * Send a string according to the websocket format |
uci1 | 0:664899e0b988 | 155 | * |
uci1 | 0:664899e0b988 | 156 | * @param str string to be sent |
uci1 | 0:664899e0b988 | 157 | */ |
uci1 | 3:24c5f0f50bf1 | 158 | void send(const char * str); |
uci1 | 0:664899e0b988 | 159 | |
uci1 | 0:664899e0b988 | 160 | // CJR: add ones for binary data |
uci1 | 3:24c5f0f50bf1 | 161 | bool sendBinary(const char* str, const uint32_t len, char* const bbuf); |
uci1 | 3:24c5f0f50bf1 | 162 | bool sendBinary(const char* hbuf, const uint32_t hlen, FILE* f, const uint32_t nbytes, |
uci1 | 3:24c5f0f50bf1 | 163 | char* const bbuf); |
uci1 | 0:664899e0b988 | 164 | |
uci1 | 0:664899e0b988 | 165 | /** |
uci1 | 0:664899e0b988 | 166 | * Read a websocket message |
uci1 | 0:664899e0b988 | 167 | * |
uci1 | 0:664899e0b988 | 168 | * @param message pointer to the string to be read (null if drop frame) |
uci1 | 0:664899e0b988 | 169 | * |
uci1 | 0:664899e0b988 | 170 | * @return true if a string has been read, false otherwise |
uci1 | 0:664899e0b988 | 171 | */ |
uci1 | 3:24c5f0f50bf1 | 172 | bool read(char * message, uint32_t& len_msg, const uint32_t maxlen, |
uci1 | 3:24c5f0f50bf1 | 173 | const uint32_t timeout, |
uci1 | 3:24c5f0f50bf1 | 174 | char* const bbuf, const uint32_t bbsize); |
uci1 | 0:664899e0b988 | 175 | |
uci1 | 0:664899e0b988 | 176 | /** |
uci1 | 0:664899e0b988 | 177 | * To see if there is a websocket connection active |
uci1 | 0:664899e0b988 | 178 | * |
uci1 | 0:664899e0b988 | 179 | * @return true if there is a connection active |
uci1 | 0:664899e0b988 | 180 | */ |
uci1 | 0:664899e0b988 | 181 | bool connected(); |
uci1 | 0:664899e0b988 | 182 | |
uci1 | 0:664899e0b988 | 183 | /** |
uci1 | 0:664899e0b988 | 184 | * Close the websocket connection |
uci1 | 0:664899e0b988 | 185 | * |
uci1 | 0:664899e0b988 | 186 | * @return true if the connection has been closed, false otherwise |
uci1 | 0:664899e0b988 | 187 | */ |
uci1 | 0:664899e0b988 | 188 | bool close(); |
uci1 | 0:664899e0b988 | 189 | |
uci1 | 0:664899e0b988 | 190 | /** |
uci1 | 0:664899e0b988 | 191 | * Accessor: get path from the websocket url |
uci1 | 0:664899e0b988 | 192 | * |
uci1 | 0:664899e0b988 | 193 | * @return path |
uci1 | 0:664899e0b988 | 194 | */ |
uci1 | 0:664899e0b988 | 195 | std::string getPath(); |
uci1 | 0:664899e0b988 | 196 | |
uci1 | 0:664899e0b988 | 197 | enum Type { |
uci1 | 0:664899e0b988 | 198 | WIF, |
uci1 | 0:664899e0b988 | 199 | ETH |
uci1 | 0:664899e0b988 | 200 | }; |
uci1 | 0:664899e0b988 | 201 | |
uci1 | 0:664899e0b988 | 202 | |
uci1 | 0:664899e0b988 | 203 | private: |
uci1 | 0:664899e0b988 | 204 | |
uci1 | 3:24c5f0f50bf1 | 205 | bool sendBinaryDirect(const char* str, const uint32_t len); |
uci1 | 3:24c5f0f50bf1 | 206 | bool sendBinaryDirect(const char* hbuf, const uint32_t hlen, FILE* f, const uint32_t nbytes); |
uci1 | 3:24c5f0f50bf1 | 207 | bool sendBinaryB64txt(const char* str, const uint32_t len, char* const bbuf); |
uci1 | 3:24c5f0f50bf1 | 208 | bool sendBinaryB64txt(const char* hbuf, const uint32_t hlen, FILE* f, const uint32_t nbytes, |
uci1 | 3:24c5f0f50bf1 | 209 | char* const bbuf); |
uci1 | 3:24c5f0f50bf1 | 210 | |
uci1 | 0:664899e0b988 | 211 | void fillFields(char * url); |
uci1 | 0:664899e0b988 | 212 | void sendOpcode(uint8_t opcode); |
uci1 | 0:664899e0b988 | 213 | void sendLength(uint32_t len); |
uci1 | 0:664899e0b988 | 214 | void sendMask(); |
uci1 | 0:664899e0b988 | 215 | int sendChar(uint8_t c); |
uci1 | 0:664899e0b988 | 216 | |
uci1 | 0:664899e0b988 | 217 | std::string ip_domain; |
uci1 | 0:664899e0b988 | 218 | std::string path; |
uci1 | 0:664899e0b988 | 219 | std::string port; |
uci1 | 0:664899e0b988 | 220 | |
uci1 | 0:664899e0b988 | 221 | // Wifly * wifi; |
uci1 | 0:664899e0b988 | 222 | |
uci1 | 0:664899e0b988 | 223 | #ifdef TARGET_LPC1768 |
uci1 | 0:664899e0b988 | 224 | void onTCPSocketEvent(TCPSocketEvent e); |
uci1 | 0:664899e0b988 | 225 | bool eth_connected; |
uci1 | 0:664899e0b988 | 226 | bool eth_readable; |
uci1 | 0:664899e0b988 | 227 | bool eth_writeable; |
uci1 | 3:24c5f0f50bf1 | 228 | char eth_rx[RX_BUF_SIZE]; |
uci1 | 0:664899e0b988 | 229 | bool response_server_eth; |
uci1 | 0:664899e0b988 | 230 | bool new_msg; |
uci1 | 0:664899e0b988 | 231 | |
uci1 | 0:664899e0b988 | 232 | EthernetNetIf * eth; |
uci1 | 0:664899e0b988 | 233 | TCPSocket * sock; |
uci1 | 0:664899e0b988 | 234 | IpAddr * server_ip; |
uci1 | 0:664899e0b988 | 235 | #endif //target |
uci1 | 0:664899e0b988 | 236 | |
uci1 | 0:664899e0b988 | 237 | Type netif; |
uci1 | 3:24c5f0f50bf1 | 238 | |
uci1 | 3:24c5f0f50bf1 | 239 | static const bool kUseB64; |
uci1 | 0:664899e0b988 | 240 | }; |
uci1 | 0:664899e0b988 | 241 | |
uci1 | 6:6f002d202f59 | 242 | #endif |
uci1 | 6:6f002d202f59 | 243 | |
uci1 | 0:664899e0b988 | 244 | #endif |