Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface NetworkAPI mbed-rtos mbed
Fork of NetRelais by
controller.hpp@10:22d49341340c, 2012-07-19 (annotated)
- Committer:
- NegativeBlack
- Date:
- Thu Jul 19 11:13:50 2012 +0000
- Revision:
- 10:22d49341340c
Implemented TCP/IP based digital io interface.
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| NegativeBlack | 10:22d49341340c | 1 | /** |
| NegativeBlack | 10:22d49341340c | 2 | * Copyright (c) 2012, Roy van Dam <roy@vandam-innovations.com> |
| NegativeBlack | 10:22d49341340c | 3 | * All rights reserved. |
| NegativeBlack | 10:22d49341340c | 4 | * |
| NegativeBlack | 10:22d49341340c | 5 | * Redistribution and use in source and binary forms, with or without |
| NegativeBlack | 10:22d49341340c | 6 | * modification, are permitted provided that the following conditions are met: |
| NegativeBlack | 10:22d49341340c | 7 | * |
| NegativeBlack | 10:22d49341340c | 8 | * 1. Redistributions of source code must retain the above copyright notice, this |
| NegativeBlack | 10:22d49341340c | 9 | * list of conditions and the following disclaimer. |
| NegativeBlack | 10:22d49341340c | 10 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
| NegativeBlack | 10:22d49341340c | 11 | * this list of conditions and the following disclaimer in the documentation |
| NegativeBlack | 10:22d49341340c | 12 | * and/or other materials provided with the distribution. |
| NegativeBlack | 10:22d49341340c | 13 | * |
| NegativeBlack | 10:22d49341340c | 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| NegativeBlack | 10:22d49341340c | 15 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| NegativeBlack | 10:22d49341340c | 16 | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| NegativeBlack | 10:22d49341340c | 17 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| NegativeBlack | 10:22d49341340c | 18 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| NegativeBlack | 10:22d49341340c | 19 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| NegativeBlack | 10:22d49341340c | 20 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| NegativeBlack | 10:22d49341340c | 21 | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| NegativeBlack | 10:22d49341340c | 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| NegativeBlack | 10:22d49341340c | 23 | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| NegativeBlack | 10:22d49341340c | 24 | */ |
| NegativeBlack | 10:22d49341340c | 25 | |
| NegativeBlack | 10:22d49341340c | 26 | #ifndef _CONTROLLER_HPP_ |
| NegativeBlack | 10:22d49341340c | 27 | #define _CONTROLLER_HPP_ |
| NegativeBlack | 10:22d49341340c | 28 | |
| NegativeBlack | 10:22d49341340c | 29 | #include <cstdio> |
| NegativeBlack | 10:22d49341340c | 30 | #include <cstring> |
| NegativeBlack | 10:22d49341340c | 31 | #include <vector> |
| NegativeBlack | 10:22d49341340c | 32 | |
| NegativeBlack | 10:22d49341340c | 33 | #include "mbed.h" |
| NegativeBlack | 10:22d49341340c | 34 | |
| NegativeBlack | 10:22d49341340c | 35 | #include "NetworkAPI/select.hpp" |
| NegativeBlack | 10:22d49341340c | 36 | #include "NetworkAPI/tcp/socket.hpp" |
| NegativeBlack | 10:22d49341340c | 37 | |
| NegativeBlack | 10:22d49341340c | 38 | class Controller |
| NegativeBlack | 10:22d49341340c | 39 | { |
| NegativeBlack | 10:22d49341340c | 40 | protected: |
| NegativeBlack | 10:22d49341340c | 41 | typedef std::vector<DigitalOut *> DigitalOutputList; |
| NegativeBlack | 10:22d49341340c | 42 | typedef std::vector<DigitalIn *> DigitalInputList; |
| NegativeBlack | 10:22d49341340c | 43 | |
| NegativeBlack | 10:22d49341340c | 44 | struct { |
| NegativeBlack | 10:22d49341340c | 45 | DigitalOutputList output; |
| NegativeBlack | 10:22d49341340c | 46 | DigitalInputList input; |
| NegativeBlack | 10:22d49341340c | 47 | } _io; |
| NegativeBlack | 10:22d49341340c | 48 | |
| NegativeBlack | 10:22d49341340c | 49 | struct { |
| NegativeBlack | 10:22d49341340c | 50 | network::tcp::Socket server; |
| NegativeBlack | 10:22d49341340c | 51 | network::tcp::Socket client; |
| NegativeBlack | 10:22d49341340c | 52 | } _network; |
| NegativeBlack | 10:22d49341340c | 53 | |
| NegativeBlack | 10:22d49341340c | 54 | enum Command { |
| NegativeBlack | 10:22d49341340c | 55 | C_None, |
| NegativeBlack | 10:22d49341340c | 56 | C_Read, |
| NegativeBlack | 10:22d49341340c | 57 | C_Write |
| NegativeBlack | 10:22d49341340c | 58 | }; |
| NegativeBlack | 10:22d49341340c | 59 | |
| NegativeBlack | 10:22d49341340c | 60 | enum ParseState { |
| NegativeBlack | 10:22d49341340c | 61 | S_Init, |
| NegativeBlack | 10:22d49341340c | 62 | S_Index, |
| NegativeBlack | 10:22d49341340c | 63 | S_Execute, |
| NegativeBlack | 10:22d49341340c | 64 | }; |
| NegativeBlack | 10:22d49341340c | 65 | |
| NegativeBlack | 10:22d49341340c | 66 | enum ParseError { |
| NegativeBlack | 10:22d49341340c | 67 | E_None = 0, |
| NegativeBlack | 10:22d49341340c | 68 | E_Internal = -1, |
| NegativeBlack | 10:22d49341340c | 69 | E_InvalidCommand = -2, |
| NegativeBlack | 10:22d49341340c | 70 | E_InvalidFormat = -3, |
| NegativeBlack | 10:22d49341340c | 71 | E_UnknownIndex = -4, |
| NegativeBlack | 10:22d49341340c | 72 | E_InvalidValue = -5 |
| NegativeBlack | 10:22d49341340c | 73 | }; |
| NegativeBlack | 10:22d49341340c | 74 | |
| NegativeBlack | 10:22d49341340c | 75 | public: |
| NegativeBlack | 10:22d49341340c | 76 | ~Controller(); |
| NegativeBlack | 10:22d49341340c | 77 | |
| NegativeBlack | 10:22d49341340c | 78 | int start(int port = 61850, int max_pending = 1); |
| NegativeBlack | 10:22d49341340c | 79 | int stop(); |
| NegativeBlack | 10:22d49341340c | 80 | |
| NegativeBlack | 10:22d49341340c | 81 | int dispatch(); |
| NegativeBlack | 10:22d49341340c | 82 | |
| NegativeBlack | 10:22d49341340c | 83 | int addOutput(DigitalOut *output); |
| NegativeBlack | 10:22d49341340c | 84 | DigitalOut *getOutput(size_t index); |
| NegativeBlack | 10:22d49341340c | 85 | |
| NegativeBlack | 10:22d49341340c | 86 | int addInput(DigitalIn *input); |
| NegativeBlack | 10:22d49341340c | 87 | DigitalIn *getInput(size_t index); |
| NegativeBlack | 10:22d49341340c | 88 | |
| NegativeBlack | 10:22d49341340c | 89 | protected: |
| NegativeBlack | 10:22d49341340c | 90 | bool _outputExists(DigitalOut *output); |
| NegativeBlack | 10:22d49341340c | 91 | bool _inputExists(DigitalIn *input); |
| NegativeBlack | 10:22d49341340c | 92 | |
| NegativeBlack | 10:22d49341340c | 93 | int _parseCommand(network::Buffer &buffer); |
| NegativeBlack | 10:22d49341340c | 94 | }; |
| NegativeBlack | 10:22d49341340c | 95 | |
| NegativeBlack | 10:22d49341340c | 96 | #endif // _CONTROLLER_HPP_ |
