Fork of https://os.mbed.com/teams/mbed_example/code/WebSocketClient/ Update to MbedOS6

Committer:
JohnnyK
Date:
Mon May 24 19:40:27 2021 +0000
Revision:
12:495c97f41a64
Parent:
11:9ee267dafdd7
repair - write return

Who changed what in which revision?

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