ModBusTCP with some fixes

Committer:
gulchi
Date:
Thu Jun 29 10:17:29 2017 +0000
Revision:
2:fcd20e2cd110
Parent:
HTTPD.h@1:b724fdb741e7
Child:
3:ebea8e061ae6
init v1

Who changed what in which revision?

UserRevisionLine numberNew contents of line
okini3939 0:d18dff347122 1 /* Copyright (C) 2013 Hiroshi Suga, MIT License
okini3939 0:d18dff347122 2 *
okini3939 0:d18dff347122 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
okini3939 0:d18dff347122 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
okini3939 0:d18dff347122 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
okini3939 0:d18dff347122 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
okini3939 0:d18dff347122 7 * furnished to do so, subject to the following conditions:
okini3939 0:d18dff347122 8 *
okini3939 0:d18dff347122 9 * The above copyright notice and this permission notice shall be included in all copies or
okini3939 0:d18dff347122 10 * substantial portions of the Software.
okini3939 0:d18dff347122 11 *
okini3939 0:d18dff347122 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
okini3939 0:d18dff347122 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
okini3939 0:d18dff347122 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
okini3939 0:d18dff347122 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
okini3939 0:d18dff347122 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
okini3939 0:d18dff347122 17 */
okini3939 0:d18dff347122 18
gulchi 2:fcd20e2cd110 19 #ifndef MODBUS_TCP_H
gulchi 2:fcd20e2cd110 20 #define MODBUS_TCP
okini3939 0:d18dff347122 21
okini3939 0:d18dff347122 22 #include "mbed.h"
okini3939 0:d18dff347122 23 #include "rtos.h"
dgriffin65 1:b724fdb741e7 24 #include "NetworkStack.h"
dgriffin65 1:b724fdb741e7 25 #include "EthInterface.h"
okini3939 0:d18dff347122 26 #include "CBuffer.h"
okini3939 0:d18dff347122 27
dgriffin65 1:b724fdb741e7 28 #define DEBUG
okini3939 0:d18dff347122 29
gulchi 2:fcd20e2cd110 30 #define MODBUS_TCP_PORT 502
gulchi 2:fcd20e2cd110 31 #define MODBUS_MAX_CLIENTS 5
gulchi 2:fcd20e2cd110 32 #define MODBUS_KEEPALIVE 10
gulchi 2:fcd20e2cd110 33 #define MODBUS_TIMEOUT 15000
gulchi 2:fcd20e2cd110 34 #define MODBUS_MAX_HANDLES 10
okini3939 0:d18dff347122 35
gulchi 2:fcd20e2cd110 36 #define MODBUS_CMD_SIZE 100
gulchi 2:fcd20e2cd110 37 #define MODBUS_BUF_SIZE 256
gulchi 2:fcd20e2cd110 38 #define MODBUS_STACK_SIZE (1024 * 6)
okini3939 0:d18dff347122 39 //#define HTTPD_ENABLE_CLOSER
okini3939 0:d18dff347122 40
okini3939 0:d18dff347122 41 //Debug is disabled by default
okini3939 0:d18dff347122 42 #if defined(DEBUG) and (!defined(TARGET_LPC11U24))
okini3939 0:d18dff347122 43 #define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__);
okini3939 0:d18dff347122 44 #define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__);
okini3939 0:d18dff347122 45 #define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__);
okini3939 0:d18dff347122 46 #define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__);
okini3939 0:d18dff347122 47 #else
okini3939 0:d18dff347122 48 #define DBG(x, ...)
okini3939 0:d18dff347122 49 #define WARN(x, ...)
okini3939 0:d18dff347122 50 #define ERR(x, ...)
okini3939 0:d18dff347122 51 #define INFO(x, ...)
okini3939 0:d18dff347122 52 #endif
okini3939 0:d18dff347122 53
okini3939 0:d18dff347122 54
gulchi 2:fcd20e2cd110 55 class Modbus {
okini3939 0:d18dff347122 56 public:
okini3939 0:d18dff347122 57
gulchi 2:fcd20e2cd110 58 enum Mode {
okini3939 0:d18dff347122 59 MODE_REQUEST,
okini3939 0:d18dff347122 60 MODE_REQSTR,
okini3939 0:d18dff347122 61 MODE_HEADER,
okini3939 0:d18dff347122 62 MODE_BODY,
okini3939 0:d18dff347122 63 MODE_ENTER,
okini3939 0:d18dff347122 64 MODE_ERROR,
okini3939 0:d18dff347122 65 MODE_WEBSOCKET,
okini3939 0:d18dff347122 66 MODE_WEBSOCKET_MASK,
okini3939 0:d18dff347122 67 MODE_WEBSOCKET_BODY,
okini3939 0:d18dff347122 68 MODE_WEBSOCKET_ENTER,
okini3939 0:d18dff347122 69 };
okini3939 0:d18dff347122 70
okini3939 0:d18dff347122 71 struct STATE {
okini3939 0:d18dff347122 72 Thread *thread;
dgriffin65 1:b724fdb741e7 73 TCPSocket *client;
okini3939 0:d18dff347122 74 volatile Request req;
okini3939 0:d18dff347122 75 volatile Mode mode;
okini3939 0:d18dff347122 76 CircBuffer <char>*buf;
gulchi 2:fcd20e2cd110 77
gulchi 2:fcd20e2cd110 78
okini3939 0:d18dff347122 79 char *querystring;
okini3939 0:d18dff347122 80 int enter;
okini3939 0:d18dff347122 81 int length;
okini3939 0:d18dff347122 82 int n;
okini3939 0:d18dff347122 83 int keepalive;
gulchi 2:fcd20e2cd110 84
okini3939 0:d18dff347122 85 };
okini3939 0:d18dff347122 86
gulchi 2:fcd20e2cd110 87 Modbus ();
okini3939 0:d18dff347122 88
gulchi 2:fcd20e2cd110 89 int start (NetworkStack *ns, int port = MODBUS_TCP_PORT);
okini3939 0:d18dff347122 90
okini3939 0:d18dff347122 91 // --- HTTPD_req.cpp ---
okini3939 0:d18dff347122 92 void httpdError (int id, int err);
okini3939 0:d18dff347122 93
gulchi 2:fcd20e2cd110 94
okini3939 0:d18dff347122 95 // --- HTTPD_util.cpp ---
gulchi 2:fcd20e2cd110 96
gulchi 2:fcd20e2cd110 97
gulchi 2:fcd20e2cd110 98
dgriffin65 1:b724fdb741e7 99 TCPSocket *getClientSocket(int id) {
dgriffin65 1:b724fdb741e7 100 if (id >= HTTPD_MAX_CLIENTS) return NULL;
dgriffin65 1:b724fdb741e7 101 return _state[id].client;
dgriffin65 1:b724fdb741e7 102 }
okini3939 0:d18dff347122 103 int send (int id, const char *body, int len, const char *header = NULL);
dgriffin65 1:b724fdb741e7 104 int sendstr (int id, const char *buf);
dgriffin65 1:b724fdb741e7 105 int hprintf(int id, const char* format, ...);
okini3939 0:d18dff347122 106 int receive (int id, char *buf, int len);
okini3939 0:d18dff347122 107 int attach (const char *uri, const char *dir);
okini3939 0:d18dff347122 108 int attach (const char *uri, void (*funcCgi)(int id));
gulchi 2:fcd20e2cd110 109
okini3939 0:d18dff347122 110
gulchi 2:fcd20e2cd110 111 static Modbus * getInstance() {
okini3939 0:d18dff347122 112 return _inst;
okini3939 0:d18dff347122 113 };
okini3939 0:d18dff347122 114
okini3939 0:d18dff347122 115 private :
gulchi 2:fcd20e2cd110 116 static Modbus *_inst;
okini3939 0:d18dff347122 117 Thread *_daemon;
dgriffin65 1:b724fdb741e7 118 TCPServer _server;
dgriffin65 1:b724fdb741e7 119
dgriffin65 1:b724fdb741e7 120 NetworkStack *m_ns;
okini3939 0:d18dff347122 121
gulchi 2:fcd20e2cd110 122 #ifdef MODBUS_ENABLE_CLOSER
gulchi 2:fcd20e2cd110 123 struct STATE _state[MODBUS_MAX_CLIENTS + 1];
okini3939 0:d18dff347122 124 #else
gulchi 2:fcd20e2cd110 125 struct STATE _state[MODBUS_MAX_CLIENTS];
okini3939 0:d18dff347122 126 #endif
okini3939 0:d18dff347122 127
okini3939 0:d18dff347122 128 struct HANDLER {
okini3939 0:d18dff347122 129 char *uri;
okini3939 0:d18dff347122 130 char *dir;
okini3939 0:d18dff347122 131 void (*funcCgi)(int id);
gulchi 2:fcd20e2cd110 132 } _handler[MODBUS_MAX_HANDLES];
okini3939 0:d18dff347122 133
okini3939 0:d18dff347122 134 int _handler_count;
okini3939 0:d18dff347122 135
dgriffin65 1:b724fdb741e7 136 static void daemon ();
okini3939 0:d18dff347122 137 static void child (void const *arg);
okini3939 0:d18dff347122 138 static void closer (void const *arg);
okini3939 0:d18dff347122 139
okini3939 0:d18dff347122 140 // --- HTTPD_req.cpp ---
okini3939 0:d18dff347122 141 int httpdFile (int id, char *dir);
okini3939 0:d18dff347122 142 void recvData (int id, char c);
okini3939 0:d18dff347122 143 int parseRequest (int id);
okini3939 0:d18dff347122 144 int parseHeader (int id);
okini3939 0:d18dff347122 145 void reqContentLength (int id, const char *buf);
okini3939 0:d18dff347122 146 void reqConnection (int id, const char *buf);
okini3939 0:d18dff347122 147 void reqUpgrade (int id, const char *buf);
okini3939 0:d18dff347122 148 void reqWebSocketVersion (int id, const char *buf);
okini3939 0:d18dff347122 149 void reqWebSocketKey (int id, const char *buf);
okini3939 0:d18dff347122 150
gulchi 2:fcd20e2cd110 151
okini3939 0:d18dff347122 152
okini3939 0:d18dff347122 153 // --- HTTPD_util.cpp ---
okini3939 0:d18dff347122 154 int getHandler (const char *uri);
okini3939 0:d18dff347122 155 char *mimetype (char *file);
okini3939 0:d18dff347122 156 int strnicmp (const char *p1, const char *p2, int n);
okini3939 0:d18dff347122 157 int from_hex (int ch);
okini3939 0:d18dff347122 158 int to_hex (int code);
okini3939 0:d18dff347122 159 };
okini3939 0:d18dff347122 160
okini3939 0:d18dff347122 161 #endif