Simplify using of UnbufferedSerial(Serial), USBCDC, TCP, SMTP, NTP Fork : https://github.com/YSI-LPS/lib_Transmission

Dependents:   lib_Transmission_Serial_example 2022_TICE_Electrolyse lib_Transmission_TCP_example

Committer:
YSI
Date:
Tue Mar 09 09:59:18 2021 +0000
Revision:
16:3ef69ffede76
Parent:
15:b2da6ab01a21
Child:
18:15778c8a97a1
remove delivery and add auto detect for HTTP request

Who changed what in which revision?

UserRevisionLine numberNew contents of line
YSI 0:2fc6fc3b5e15 1 /** Transmission class
YSI 0:2fc6fc3b5e15 2 *
YSI 0:2fc6fc3b5e15 3 * @purpose library for Transmission
YSI 0:2fc6fc3b5e15 4 *
YSI 0:2fc6fc3b5e15 5 * Use to Transmission
YSI 0:2fc6fc3b5e15 6 *
YSI 0:2fc6fc3b5e15 7 * Example:
YSI 0:2fc6fc3b5e15 8 * @code
YSI 0:2fc6fc3b5e15 9 * #include "mbed.h"
YSI 0:2fc6fc3b5e15 10 *
YSI 0:2fc6fc3b5e15 11 * int main()
YSI 0:2fc6fc3b5e15 12 * {
YSI 0:2fc6fc3b5e15 13 * while(1)
YSI 0:2fc6fc3b5e15 14 * {
YSI 0:2fc6fc3b5e15 15 * }
YSI 0:2fc6fc3b5e15 16 * }
YSI 0:2fc6fc3b5e15 17 * @endcode
YSI 0:2fc6fc3b5e15 18 * @file lib_Transmission.h
YSI 0:2fc6fc3b5e15 19 * @date Jun 2020
YSI 0:2fc6fc3b5e15 20 * @author Yannic Simon
YSI 0:2fc6fc3b5e15 21 */
YSI 4:9a4ab4f406ab 22 #ifndef TRANSMISSION_H
YSI 4:9a4ab4f406ab 23 #define TRANSMISSION_H
YSI 14:9e3accc681c4 24 #define NDEBUG
YSI 0:2fc6fc3b5e15 25
YSI 0:2fc6fc3b5e15 26 #include "mbed.h"
YSI 15:b2da6ab01a21 27 #include "USBCDC.h"
YSI 0:2fc6fc3b5e15 28 #include "EthernetInterface.h"
YSI 0:2fc6fc3b5e15 29 #include <sstream>
YSI 0:2fc6fc3b5e15 30
YSI 15:b2da6ab01a21 31 #define TRANSMISSION_DEFAULT_BUFFER_SIZE 1072 // taille des buffers de reception
YSI 14:9e3accc681c4 32 #define TRANSMISSION_DEFAULT_SMTP_SERVER "129.175.212.70" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("smtp.u-psud.fr")
YSI 14:9e3accc681c4 33 #define TRANSMISSION_DEFAULT_NTP_SERVER "129.175.34.43" // IP sinon obligation d'utilisation du DNS avec _eth.getHostByName("ntp.u-psud.fr")
YSI 0:2fc6fc3b5e15 34
YSI 0:2fc6fc3b5e15 35 /** Transmission class
YSI 0:2fc6fc3b5e15 36 */
YSI 0:2fc6fc3b5e15 37 class Transmission
YSI 0:2fc6fc3b5e15 38 {
YSI 0:2fc6fc3b5e15 39 public:
YSI 15:b2da6ab01a21 40 /**
YSI 15:b2da6ab01a21 41 *
YSI 15:b2da6ab01a21 42 * @param
YSI 15:b2da6ab01a21 43 * @param
YSI 15:b2da6ab01a21 44 * @returns
YSI 15:b2da6ab01a21 45 */
YSI 15:b2da6ab01a21 46 typedef enum { USB, SERIAL, TCP, HTTP, ANY }
YSI 15:b2da6ab01a21 47 enum_trans_delivery;
YSI 15:b2da6ab01a21 48 /**
YSI 15:b2da6ab01a21 49 *
YSI 15:b2da6ab01a21 50 * @param
YSI 15:b2da6ab01a21 51 * @param
YSI 15:b2da6ab01a21 52 * @returns
YSI 15:b2da6ab01a21 53 */
YSI 14:9e3accc681c4 54 typedef enum { WHITE, CYAN, MAGENTA_ACCEPT, BLUE_CLIENT, YELLOW_CONNECTING, GREEN_GLOBAL_UP, RED_DISCONNECTED, BLACK_INITIALIZE }
YSI 14:9e3accc681c4 55 enum_trans_status;
YSI 15:b2da6ab01a21 56 /**
YSI 15:b2da6ab01a21 57 *
YSI 15:b2da6ab01a21 58 * @param
YSI 15:b2da6ab01a21 59 * @param
YSI 15:b2da6ab01a21 60 * @returns
YSI 15:b2da6ab01a21 61 */
YSI 14:9e3accc681c4 62 struct { const char *RETURN_OK; const char *RETURN_NO_CONTENT; const char *RETURN_MOVED; const char *RETURN_FOUND; const char *RETURN_SEE_OTHER; const char *RETURN_REDIRECT; const char *RETURN_NOT_FOUND; }
YSI 14:9e3accc681c4 63 http = { "HTTP/1.1 200 OK\r\n", "HTTP/1.1 204 No Content\r\n", "HTTP/1.1 301 Moved Permanently\r\n", "HTTP/1.1 302 Found\r\n", "HTTP/1.1 303 See Other\r\n", "HTTP/1.1 307 Temporary Redirect\r\n", "HTTP/1.1 404 Not Found\r\n" };
YSI 0:2fc6fc3b5e15 64 /** make new Transmission instance
YSI 0:2fc6fc3b5e15 65 * connected to
YSI 0:2fc6fc3b5e15 66 *
YSI 0:2fc6fc3b5e15 67 * @param
YSI 0:2fc6fc3b5e15 68 * @param
YSI 0:2fc6fc3b5e15 69 */
YSI 14:9e3accc681c4 70 Transmission(
YSI 14:9e3accc681c4 71 #if MBED_MAJOR_VERSION > 5
YSI 14:9e3accc681c4 72 UnbufferedSerial *serial,
YSI 14:9e3accc681c4 73 #else
YSI 14:9e3accc681c4 74 Serial *serial,
YSI 14:9e3accc681c4 75 #endif
YSI 15:b2da6ab01a21 76 EthernetInterface *eth,
YSI 15:b2da6ab01a21 77 USBCDC *usb,
YSI 15:b2da6ab01a21 78 string (*processing)(string),
YSI 15:b2da6ab01a21 79 void (*startuped)(void) = NULL,
YSI 15:b2da6ab01a21 80 bool caseIgnore = true);
YSI 15:b2da6ab01a21 81 /** make new Transmission instance
YSI 15:b2da6ab01a21 82 * connected to
YSI 15:b2da6ab01a21 83 *
YSI 15:b2da6ab01a21 84 * @param
YSI 15:b2da6ab01a21 85 * @param
YSI 15:b2da6ab01a21 86 */
YSI 15:b2da6ab01a21 87 Transmission(
YSI 15:b2da6ab01a21 88 #if MBED_MAJOR_VERSION > 5
YSI 15:b2da6ab01a21 89 UnbufferedSerial *serial,
YSI 15:b2da6ab01a21 90 #else
YSI 15:b2da6ab01a21 91 Serial *serial,
YSI 15:b2da6ab01a21 92 #endif
YSI 15:b2da6ab01a21 93 string (*processing)(string),
YSI 15:b2da6ab01a21 94 bool caseIgnore = true);
YSI 15:b2da6ab01a21 95 /** make new Transmission instance
YSI 15:b2da6ab01a21 96 * connected to
YSI 15:b2da6ab01a21 97 *
YSI 15:b2da6ab01a21 98 * @param
YSI 15:b2da6ab01a21 99 * @param
YSI 15:b2da6ab01a21 100 */
YSI 15:b2da6ab01a21 101 Transmission(
YSI 15:b2da6ab01a21 102 EthernetInterface *eth,
YSI 15:b2da6ab01a21 103 string (*processing)(string),
YSI 15:b2da6ab01a21 104 void (*startuped)(void) = NULL,
YSI 15:b2da6ab01a21 105 bool caseIgnore = true);
YSI 15:b2da6ab01a21 106 /** make new Transmission instance
YSI 15:b2da6ab01a21 107 * connected to
YSI 15:b2da6ab01a21 108 *
YSI 15:b2da6ab01a21 109 * @param
YSI 15:b2da6ab01a21 110 * @param
YSI 15:b2da6ab01a21 111 */
YSI 15:b2da6ab01a21 112 Transmission(
YSI 15:b2da6ab01a21 113 USBCDC *usb,
YSI 15:b2da6ab01a21 114 string (*processing)(string),
YSI 15:b2da6ab01a21 115 bool caseIgnore = true);
YSI 0:2fc6fc3b5e15 116 /**
YSI 0:2fc6fc3b5e15 117 *
YSI 0:2fc6fc3b5e15 118 * @param
YSI 0:2fc6fc3b5e15 119 * @param
YSI 14:9e3accc681c4 120 * @returns
YSI 4:9a4ab4f406ab 121 */
YSI 14:9e3accc681c4 122 string ip(const bool& SET, const char* IP="", const uint16_t& PORT=80, const uint16_t& TIMEOUT=100);
YSI 4:9a4ab4f406ab 123 /**
YSI 4:9a4ab4f406ab 124 *
YSI 4:9a4ab4f406ab 125 * @param
YSI 4:9a4ab4f406ab 126 * @param
YSI 14:9e3accc681c4 127 * @returns
YSI 4:9a4ab4f406ab 128 */
YSI 14:9e3accc681c4 129 string ip(void);
YSI 4:9a4ab4f406ab 130 /**
YSI 4:9a4ab4f406ab 131 *
YSI 4:9a4ab4f406ab 132 * @param
YSI 4:9a4ab4f406ab 133 * @param
YSI 14:9e3accc681c4 134 * @returns
YSI 4:9a4ab4f406ab 135 */
YSI 11:de94dcd67561 136 enum_trans_status recv(void);
YSI 4:9a4ab4f406ab 137 /**
YSI 4:9a4ab4f406ab 138 *
YSI 4:9a4ab4f406ab 139 * @param
YSI 4:9a4ab4f406ab 140 * @param
YSI 14:9e3accc681c4 141 * @returns
YSI 4:9a4ab4f406ab 142 */
YSI 15:b2da6ab01a21 143 nsapi_error_t send(const string& BUFFER="", const enum_trans_delivery& DELIVERY=ANY);
YSI 15:b2da6ab01a21 144 /**
YSI 15:b2da6ab01a21 145 *
YSI 15:b2da6ab01a21 146 * @param
YSI 15:b2da6ab01a21 147 * @param
YSI 15:b2da6ab01a21 148 * @returns
YSI 15:b2da6ab01a21 149 */
YSI 14:9e3accc681c4 150 bool smtp(const char* MAIL, const char* FROM="", const char* SUBJECT="", const char* DATA="", const char* SERVER=TRANSMISSION_DEFAULT_SMTP_SERVER);
YSI 4:9a4ab4f406ab 151 /**
YSI 4:9a4ab4f406ab 152 *
YSI 4:9a4ab4f406ab 153 * @param
YSI 4:9a4ab4f406ab 154 * @param
YSI 14:9e3accc681c4 155 * @returns
YSI 4:9a4ab4f406ab 156 */
YSI 14:9e3accc681c4 157 time_t ntp(const char* SERVER=TRANSMISSION_DEFAULT_NTP_SERVER);
YSI 0:2fc6fc3b5e15 158
YSI 0:2fc6fc3b5e15 159 private:
YSI 12:e22ff63d237c 160 #if MBED_MAJOR_VERSION > 5
YSI 15:b2da6ab01a21 161 UnbufferedSerial *_serial = NULL;
YSI 12:e22ff63d237c 162 #else
YSI 15:b2da6ab01a21 163 Serial *_serial = NULL;
YSI 12:e22ff63d237c 164 #endif
YSI 14:9e3accc681c4 165 TCPSocket _serverTCP, *_clientTCP = NULL;
YSI 15:b2da6ab01a21 166 EventQueue *_queue = NULL;
YSI 15:b2da6ab01a21 167 EthernetInterface *_eth = NULL;
YSI 15:b2da6ab01a21 168 USBCDC *_usb = NULL;
YSI 15:b2da6ab01a21 169 bool _caseIgnore = false;
YSI 14:9e3accc681c4 170
YSI 14:9e3accc681c4 171 /* Serial */
YSI 14:9e3accc681c4 172 void serial_event(void);
YSI 0:2fc6fc3b5e15 173
YSI 8:7193327bed34 174 /* EthernetInterface */
YSI 8:7193327bed34 175 void eth_state(void);
YSI 8:7193327bed34 176 bool eth_connect(void);
YSI 8:7193327bed34 177 void eth_event(nsapi_event_t, intptr_t);
YSI 8:7193327bed34 178 intptr_t eth_status(const string&, const intptr_t&);
YSI 8:7193327bed34 179 nsapi_error_t eth_error(const string& SOURCE, const nsapi_error_t& CODE);
YSI 8:7193327bed34 180
YSI 8:7193327bed34 181 /* serverTCP */
YSI 8:7193327bed34 182 bool serverTCP_connect(void);
YSI 8:7193327bed34 183 void serverTCP_accept(void);
YSI 8:7193327bed34 184 void serverTCP_event(void);
YSI 8:7193327bed34 185
YSI 10:25e049353db5 186 /* Transmission */
YSI 15:b2da6ab01a21 187 void (*_startuped)(void);
YSI 15:b2da6ab01a21 188 string (*_processing)(string);
YSI 15:b2da6ab01a21 189 void preprocessing(char *buffer, const enum_trans_delivery);
YSI 14:9e3accc681c4 190 struct { enum_trans_status status; bool SET; bool BREAK; bool DHCP; bool CONNECT; string IP; uint16_t TIMEOUT; uint16_t PORT; }
YSI 14:9e3accc681c4 191 message = { RED_DISCONNECTED, false, false, false, false, "", 100, 80 };
YSI 8:7193327bed34 192 };
YSI 0:2fc6fc3b5e15 193 #endif