261 lines
8.1 KiB
C++
261 lines
8.1 KiB
C++
/*
|
|
* ebusd - daemon for communication with eBUS heating systems.
|
|
* Copyright (C) 2014-2017 John Baier <ebusd@ebusd.eu>
|
|
*
|
|
* This program is free software: you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef EBUSD_MAINLOOP_H_
|
|
#define EBUSD_MAINLOOP_H_
|
|
|
|
#include <string>
|
|
#include <list>
|
|
#include <vector>
|
|
#include "ebusd/bushandler.h"
|
|
#include "ebusd/datahandler.h"
|
|
#include "ebusd/network.h"
|
|
#include "lib/ebus/message.h"
|
|
#include "lib/utils/rotatefile.h"
|
|
|
|
/** \file mainloop.h
|
|
* The main loop for the TCP client interface and regular tasks such as resolving scanned data.
|
|
*/
|
|
|
|
namespace ebusd {
|
|
|
|
/**
|
|
* The main loop handling requests from connected clients.
|
|
*/
|
|
class MainLoop : public Thread, DeviceListener {
|
|
public:
|
|
/**
|
|
* Construct the main loop and create network and bus handling components.
|
|
* @param opt the program options.
|
|
* @param device the @a Device instance.
|
|
* @param messages the @a MessageMap instance.
|
|
*/
|
|
MainLoop(const struct options opt, Device *device, MessageMap* messages);
|
|
|
|
/**
|
|
* Destructor.
|
|
*/
|
|
~MainLoop();
|
|
|
|
/**
|
|
* Get the @a BusHandler instance.
|
|
* @return the created @a BusHandler instance.
|
|
*/
|
|
BusHandler* getBusHandler() { return m_busHandler; }
|
|
|
|
/**
|
|
* Add a client @a NetMessage to the queue.
|
|
* @param message the client @a NetMessage to handle.
|
|
*/
|
|
void addMessage(NetMessage* message) { m_netQueue.push(message); }
|
|
|
|
// @copydoc
|
|
virtual void notifyDeviceData(const unsigned char byte, bool received);
|
|
|
|
|
|
protected:
|
|
// @copydoc
|
|
virtual void run();
|
|
|
|
|
|
private:
|
|
/**
|
|
* Decode and execute client message.
|
|
* @param data the data string to decode (may be empty).
|
|
* @param connected set to false when the client connection shall be closed.
|
|
* @param isHttp true for HTTP message.
|
|
* @param listening set to true when the client is in listening mode.
|
|
* @param reload set to true when the configuration files were reloaded.
|
|
* @return result string to send back to the client.
|
|
*/
|
|
string decodeMessage(const string& data, const bool isHttp, bool& connected, bool& listening, bool& reload);
|
|
|
|
/**
|
|
* Parse the hex master message from the remaining arguments.
|
|
* @param args the arguments passed to the command.
|
|
* @param argPos the index of the first argument to parse.
|
|
* @param master the master @a SymbolString to write the data to.
|
|
* @return the result from parsing the arguments.
|
|
*/
|
|
result_t parseHexMaster(vector<string> &args, size_t argPos, SymbolString& master);
|
|
|
|
/**
|
|
* Execute the read command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeRead(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the write command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeWrite(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the hex command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeHex(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the find command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeFind(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the listen command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @param listening set to true when the client is in listening mode.
|
|
* @return the result string.
|
|
*/
|
|
string executeListen(vector<string> &args, bool& listening);
|
|
|
|
/**
|
|
* Execute the state command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeState(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the grab command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeGrab(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the scan command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeScan(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the log command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeLog(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the raw command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeRaw(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the dump command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeDump(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the reload command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeReload(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the info command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @return the result string.
|
|
*/
|
|
string executeInfo(vector<string> &args);
|
|
|
|
/**
|
|
* Execute the quit command.
|
|
* @param args the arguments passed to the command (starting with the command itself), or empty for help.
|
|
* @param connected set to false when the client connection shall be closed.
|
|
* @return the result string.
|
|
*/
|
|
string executeQuit(vector<string> &args, bool& connected);
|
|
|
|
/**
|
|
* Execute the help command.
|
|
* @return the result string.
|
|
*/
|
|
string executeHelp();
|
|
|
|
/**
|
|
* Execute the HTTP GET command.
|
|
* @param args the arguments passed to the command (starting with the command itself).
|
|
* @param connected set to false when the client connection shall be closed.
|
|
* @return the result string.
|
|
*/
|
|
string executeGet(vector<string> &args, bool& connected);
|
|
|
|
/** the @a Device instance. */
|
|
Device* m_device;
|
|
|
|
/** the number of reconnects requested from the @a Device. */
|
|
unsigned int m_reconnectCount;
|
|
|
|
/** the @a RotateFile for writing sent/received bytes in log format, or NULL. */
|
|
RotateFile* m_logRawFile;
|
|
|
|
/** whether raw logging to @p logNotice is enabled (only relevant if m_logRawFile is NULL). */
|
|
bool m_logRawEnabled;
|
|
|
|
/** the @a RotateFile for dumping received data, or NULL. */
|
|
RotateFile* m_dumpFile;
|
|
|
|
/** the @a MessageMap instance. */
|
|
MessageMap* m_messages;
|
|
|
|
/** the own master address for sending on the bus. */
|
|
const unsigned char m_address;
|
|
|
|
/** whether to pick configuration files matching initial scan. */
|
|
const bool m_scanConfig;
|
|
|
|
/** the initial address to scan for @a m_scanConfig
|
|
* (@a ESC=none, 0xfe=broadcast ident, @a SYN=full scan, else: single slave address). */
|
|
const unsigned char m_initialScan;
|
|
|
|
/** whether to enable the hex command. */
|
|
const bool m_enableHex;
|
|
|
|
/** the created @a BusHandler instance. */
|
|
BusHandler* m_busHandler;
|
|
|
|
/** the created @a Network instance. */
|
|
Network* m_network;
|
|
|
|
/** the @a NetMessage @a Queue. */
|
|
Queue<NetMessage*> m_netQueue;
|
|
|
|
/** the path for HTML files served by the HTTP port. */
|
|
string m_htmlPath;
|
|
|
|
/** the registered @a DataHandler instances. */
|
|
list<DataHandler*> m_dataHandlers;
|
|
};
|
|
|
|
} // namespace ebusd
|
|
|
|
#endif // EBUSD_MAINLOOP_H_
|