Network Services

Dependents:   PwrCond_mbed5

Fork of W5500Interface_K22F by Andrew McCartney

Committer:
dgriffin65
Date:
Thu Jun 15 20:29:03 2017 +0000
Revision:
15:14382459c8b7
Converted to a single library

Who changed what in which revision?

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