Counter

Dependencies:   EthernetInterface NTPClient SDFileSystem TextLCD WebSocketClient mbed-rtos mbed Socket lwip-eth lwip-sys lwip FATFileSystem

Committer:
Tuxitheone
Date:
Mon Feb 29 18:59:15 2016 +0000
Revision:
0:ecaf3e593122
TankCounter

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Tuxitheone 0:ecaf3e593122 1 /**
Tuxitheone 0:ecaf3e593122 2 * @author Samuel Mokrani
Tuxitheone 0:ecaf3e593122 3 *
Tuxitheone 0:ecaf3e593122 4 * @section LICENSE
Tuxitheone 0:ecaf3e593122 5 *
Tuxitheone 0:ecaf3e593122 6 * Copyright (c) 2011 mbed
Tuxitheone 0:ecaf3e593122 7 *
Tuxitheone 0:ecaf3e593122 8 * Permission is hereby granted, free of charge, to any person obtaining a copy
Tuxitheone 0:ecaf3e593122 9 * of this software and associated documentation files (the "Software"), to deal
Tuxitheone 0:ecaf3e593122 10 * in the Software without restriction, including without limitation the rights
Tuxitheone 0:ecaf3e593122 11 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
Tuxitheone 0:ecaf3e593122 12 * copies of the Software, and to permit persons to whom the Software is
Tuxitheone 0:ecaf3e593122 13 * furnished to do so, subject to the following conditions:
Tuxitheone 0:ecaf3e593122 14 *
Tuxitheone 0:ecaf3e593122 15 * The above copyright notice and this permission notice shall be included in
Tuxitheone 0:ecaf3e593122 16 * all copies or substantial portions of the Software.
Tuxitheone 0:ecaf3e593122 17 *
Tuxitheone 0:ecaf3e593122 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
Tuxitheone 0:ecaf3e593122 19 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
Tuxitheone 0:ecaf3e593122 20 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
Tuxitheone 0:ecaf3e593122 21 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
Tuxitheone 0:ecaf3e593122 22 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Tuxitheone 0:ecaf3e593122 23 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
Tuxitheone 0:ecaf3e593122 24 * THE SOFTWARE.
Tuxitheone 0:ecaf3e593122 25 *
Tuxitheone 0:ecaf3e593122 26 * @section DESCRIPTION
Tuxitheone 0:ecaf3e593122 27 * Simple websocket client
Tuxitheone 0:ecaf3e593122 28 *
Tuxitheone 0:ecaf3e593122 29 */
Tuxitheone 0:ecaf3e593122 30
Tuxitheone 0:ecaf3e593122 31 #ifndef WEBSOCKET_H
Tuxitheone 0:ecaf3e593122 32 #define WEBSOCKET_H
Tuxitheone 0:ecaf3e593122 33
Tuxitheone 0:ecaf3e593122 34 #include "mbed.h"
Tuxitheone 0:ecaf3e593122 35
Tuxitheone 0:ecaf3e593122 36 #include "TCPSocketConnection.h"
Tuxitheone 0:ecaf3e593122 37
Tuxitheone 0:ecaf3e593122 38 /** Websocket client Class.
Tuxitheone 0:ecaf3e593122 39 *
Tuxitheone 0:ecaf3e593122 40 * Example (ethernet network):
Tuxitheone 0:ecaf3e593122 41 * @code
Tuxitheone 0:ecaf3e593122 42 * #include "mbed.h"
Tuxitheone 0:ecaf3e593122 43 * #include "EthernetInterface.h"
Tuxitheone 0:ecaf3e593122 44 * #include "Websocket.h"
Tuxitheone 0:ecaf3e593122 45 *
Tuxitheone 0:ecaf3e593122 46 * int main() {
Tuxitheone 0:ecaf3e593122 47 * EthernetInterface eth;
Tuxitheone 0:ecaf3e593122 48 * eth.init(); //Use DHCP
Tuxitheone 0:ecaf3e593122 49 * eth.connect();
Tuxitheone 0:ecaf3e593122 50 * printf("IP Address is %s\n\r", eth.getIPAddress());
Tuxitheone 0:ecaf3e593122 51 *
Tuxitheone 0:ecaf3e593122 52 * Websocket ws("ws://sockets.mbed.org:443/ws/demo/rw");
Tuxitheone 0:ecaf3e593122 53 * ws.connect();
Tuxitheone 0:ecaf3e593122 54 *
Tuxitheone 0:ecaf3e593122 55 * while (1) {
Tuxitheone 0:ecaf3e593122 56 * int res = ws.send("WebSocket Hello World!");
Tuxitheone 0:ecaf3e593122 57 *
Tuxitheone 0:ecaf3e593122 58 * if (ws.read(recv)) {
Tuxitheone 0:ecaf3e593122 59 * printf("rcv: %s\r\n", recv);
Tuxitheone 0:ecaf3e593122 60 * }
Tuxitheone 0:ecaf3e593122 61 *
Tuxitheone 0:ecaf3e593122 62 * wait(0.1);
Tuxitheone 0:ecaf3e593122 63 * }
Tuxitheone 0:ecaf3e593122 64 * }
Tuxitheone 0:ecaf3e593122 65 * @endcode
Tuxitheone 0:ecaf3e593122 66 */
Tuxitheone 0:ecaf3e593122 67
Tuxitheone 0:ecaf3e593122 68 class Websocket
Tuxitheone 0:ecaf3e593122 69 {
Tuxitheone 0:ecaf3e593122 70 public:
Tuxitheone 0:ecaf3e593122 71 /**
Tuxitheone 0:ecaf3e593122 72 * Constructor
Tuxitheone 0:ecaf3e593122 73 *
Tuxitheone 0:ecaf3e593122 74 * @param url The Websocket url in the form "ws://ip_domain[:port]/path" (by default: port = 80)
Tuxitheone 0:ecaf3e593122 75 */
Tuxitheone 0:ecaf3e593122 76 Websocket(char * url);
Tuxitheone 0:ecaf3e593122 77
Tuxitheone 0:ecaf3e593122 78 /**
Tuxitheone 0:ecaf3e593122 79 * Connect to the websocket url
Tuxitheone 0:ecaf3e593122 80 *
Tuxitheone 0:ecaf3e593122 81 *@return true if the connection is established, false otherwise
Tuxitheone 0:ecaf3e593122 82 */
Tuxitheone 0:ecaf3e593122 83 bool connect();
Tuxitheone 0:ecaf3e593122 84
Tuxitheone 0:ecaf3e593122 85 /**
Tuxitheone 0:ecaf3e593122 86 * Send a string according to the websocket format (see rfc 6455)
Tuxitheone 0:ecaf3e593122 87 *
Tuxitheone 0:ecaf3e593122 88 * @param str string to be sent
Tuxitheone 0:ecaf3e593122 89 *
Tuxitheone 0:ecaf3e593122 90 * @returns the number of bytes sent
Tuxitheone 0:ecaf3e593122 91 */
Tuxitheone 0:ecaf3e593122 92 int send(char * str);
Tuxitheone 0:ecaf3e593122 93
Tuxitheone 0:ecaf3e593122 94 /**
Tuxitheone 0:ecaf3e593122 95 * Read a websocket message
Tuxitheone 0:ecaf3e593122 96 *
Tuxitheone 0:ecaf3e593122 97 * @param message pointer to the string to be read (null if drop frame)
Tuxitheone 0:ecaf3e593122 98 *
Tuxitheone 0:ecaf3e593122 99 * @return true if a websocket frame has been read
Tuxitheone 0:ecaf3e593122 100 */
Tuxitheone 0:ecaf3e593122 101 bool read(char * message);
Tuxitheone 0:ecaf3e593122 102
Tuxitheone 0:ecaf3e593122 103 /**
Tuxitheone 0:ecaf3e593122 104 * To see if there is a websocket connection active
Tuxitheone 0:ecaf3e593122 105 *
Tuxitheone 0:ecaf3e593122 106 * @return true if there is a connection active
Tuxitheone 0:ecaf3e593122 107 */
Tuxitheone 0:ecaf3e593122 108 bool is_connected();
Tuxitheone 0:ecaf3e593122 109
Tuxitheone 0:ecaf3e593122 110 /**
Tuxitheone 0:ecaf3e593122 111 * Close the websocket connection
Tuxitheone 0:ecaf3e593122 112 *
Tuxitheone 0:ecaf3e593122 113 * @return true if the connection has been closed, false otherwise
Tuxitheone 0:ecaf3e593122 114 */
Tuxitheone 0:ecaf3e593122 115 bool close();
Tuxitheone 0:ecaf3e593122 116
Tuxitheone 0:ecaf3e593122 117 /*
Tuxitheone 0:ecaf3e593122 118 * Accessor: get path from the websocket url
Tuxitheone 0:ecaf3e593122 119 *
Tuxitheone 0:ecaf3e593122 120 * @return path
Tuxitheone 0:ecaf3e593122 121 */
Tuxitheone 0:ecaf3e593122 122 char* getPath();
Tuxitheone 0:ecaf3e593122 123
Tuxitheone 0:ecaf3e593122 124 private:
Tuxitheone 0:ecaf3e593122 125
Tuxitheone 0:ecaf3e593122 126 void fillFields(char * url);
Tuxitheone 0:ecaf3e593122 127 int parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
Tuxitheone 0:ecaf3e593122 128 int sendOpcode(uint8_t opcode, char * msg);
Tuxitheone 0:ecaf3e593122 129 int sendLength(uint32_t len, char * msg);
Tuxitheone 0:ecaf3e593122 130 int sendMask(char * msg);
Tuxitheone 0:ecaf3e593122 131 int readChar(char * pC, bool block = true);
Tuxitheone 0:ecaf3e593122 132
Tuxitheone 0:ecaf3e593122 133 char scheme[8];
Tuxitheone 0:ecaf3e593122 134 uint16_t port;
Tuxitheone 0:ecaf3e593122 135 char host[32];
Tuxitheone 0:ecaf3e593122 136 char path[64];
Tuxitheone 0:ecaf3e593122 137
Tuxitheone 0:ecaf3e593122 138 TCPSocketConnection socket;
Tuxitheone 0:ecaf3e593122 139
Tuxitheone 0:ecaf3e593122 140 int read(char * buf, int len, int min_len = -1);
Tuxitheone 0:ecaf3e593122 141 int write(char * buf, int len);
Tuxitheone 0:ecaf3e593122 142 };
Tuxitheone 0:ecaf3e593122 143
Tuxitheone 0:ecaf3e593122 144 #endif