ModBusTCP with some fixes
Diff: ModBus-TCP.h
- Revision:
- 2:fcd20e2cd110
- Parent:
- 1:b724fdb741e7
- Child:
- 3:ebea8e061ae6
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/ModBus-TCP.h Thu Jun 29 10:17:29 2017 +0000 @@ -0,0 +1,161 @@ +/* Copyright (C) 2013 Hiroshi Suga, MIT License + * + * Permission is hereby granted, free of charge, to any person obtaining a copy of this software + * and associated documentation files (the "Software"), to deal in the Software without restriction, + * including without limitation the rights to use, copy, modify, merge, publish, distribute, + * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all copies or + * substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING + * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND + * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, + * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + */ + +#ifndef MODBUS_TCP_H +#define MODBUS_TCP + +#include "mbed.h" +#include "rtos.h" +#include "NetworkStack.h" +#include "EthInterface.h" +#include "CBuffer.h" + +#define DEBUG + +#define MODBUS_TCP_PORT 502 +#define MODBUS_MAX_CLIENTS 5 +#define MODBUS_KEEPALIVE 10 +#define MODBUS_TIMEOUT 15000 +#define MODBUS_MAX_HANDLES 10 + +#define MODBUS_CMD_SIZE 100 +#define MODBUS_BUF_SIZE 256 +#define MODBUS_STACK_SIZE (1024 * 6) +//#define HTTPD_ENABLE_CLOSER + +//Debug is disabled by default +#if defined(DEBUG) and (!defined(TARGET_LPC11U24)) +#define DBG(x, ...) std::printf("[DBG]" x "\r\n", ##__VA_ARGS__); +#define WARN(x, ...) std::printf("[WARN]" x "\r\n", ##__VA_ARGS__); +#define ERR(x, ...) std::printf("[ERR]" x "\r\n", ##__VA_ARGS__); +#define INFO(x, ...) std::printf("[INFO]" x "\r\n", ##__VA_ARGS__); +#else +#define DBG(x, ...) +#define WARN(x, ...) +#define ERR(x, ...) +#define INFO(x, ...) +#endif + + +class Modbus { +public: + + enum Mode { + MODE_REQUEST, + MODE_REQSTR, + MODE_HEADER, + MODE_BODY, + MODE_ENTER, + MODE_ERROR, + MODE_WEBSOCKET, + MODE_WEBSOCKET_MASK, + MODE_WEBSOCKET_BODY, + MODE_WEBSOCKET_ENTER, + }; + + struct STATE { + Thread *thread; + TCPSocket *client; + volatile Request req; + volatile Mode mode; + CircBuffer <char>*buf; + + + char *querystring; + int enter; + int length; + int n; + int keepalive; + + }; + + Modbus (); + + int start (NetworkStack *ns, int port = MODBUS_TCP_PORT); + + // --- HTTPD_req.cpp --- + void httpdError (int id, int err); + + + // --- HTTPD_util.cpp --- + + + + TCPSocket *getClientSocket(int id) { + if (id >= HTTPD_MAX_CLIENTS) return NULL; + return _state[id].client; + } + int send (int id, const char *body, int len, const char *header = NULL); + int sendstr (int id, const char *buf); + int hprintf(int id, const char* format, ...); + int receive (int id, char *buf, int len); + int attach (const char *uri, const char *dir); + int attach (const char *uri, void (*funcCgi)(int id)); + + + static Modbus * getInstance() { + return _inst; + }; + +private : + static Modbus *_inst; + Thread *_daemon; + TCPServer _server; + + NetworkStack *m_ns; + +#ifdef MODBUS_ENABLE_CLOSER + struct STATE _state[MODBUS_MAX_CLIENTS + 1]; +#else + struct STATE _state[MODBUS_MAX_CLIENTS]; +#endif + + struct HANDLER { + char *uri; + char *dir; + void (*funcCgi)(int id); + } _handler[MODBUS_MAX_HANDLES]; + + int _handler_count; + + static void daemon (); + static void child (void const *arg); + static void closer (void const *arg); + + // --- HTTPD_req.cpp --- + int httpdFile (int id, char *dir); + void recvData (int id, char c); + int parseRequest (int id); + int parseHeader (int id); + void reqContentLength (int id, const char *buf); + void reqConnection (int id, const char *buf); + void reqUpgrade (int id, const char *buf); + void reqWebSocketVersion (int id, const char *buf); + void reqWebSocketKey (int id, const char *buf); + + + + // --- HTTPD_util.cpp --- + int getHandler (const char *uri); + char *mimetype (char *file); + int strnicmp (const char *p1, const char *p2, int n); + int from_hex (int ch); + int to_hex (int code); +}; + +#endif