libhomegear-base  0.7
Base library for Homegear and Homegear family modules.
Ssdp.h
Go to the documentation of this file.
1 /* Copyright 2013-2019 Homegear GmbH
2  *
3  * libhomegear-base is free software: you can redistribute it and/or
4  * modify it under the terms of the GNU Lesser General Public License as
5  * published by the Free Software Foundation, either version 3 of the
6  * License, or (at your option) any later version.
7  *
8  * libhomegear-base is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU Lesser General Public License for more details.
12  *
13  * You should have received a copy of the GNU Lesser General Public
14  * License along with libhomegear-base. If not, see
15  * <http://www.gnu.org/licenses/>.
16  *
17  * In addition, as a special exception, the copyright holders give
18  * permission to link the code of portions of this program with the
19  * OpenSSL library under certain conditions as described in each
20  * individual source file, and distribute linked combinations
21  * including the two.
22  * You must obey the GNU Lesser General Public License in all respects
23  * for all of the code used other than OpenSSL. If you modify
24  * file(s) with this exception, you may extend this exception to your
25  * version of the file(s), but you are not obligated to do so. If you
26  * do not wish to do so, delete this exception statement from your
27  * version. If you delete this exception statement from all source
28  * files in the program, then also delete it here.
29 */
30 
31 #ifndef SSDP_H_
32 #define SSDP_H_
33 
34 #include "../Encoding/Http.h"
35 #include "../Variable.h"
36 
37 #include <string>
38 #include <vector>
39 #include <memory>
40 #include <set>
41 #include <unordered_map>
42 #include <atomic>
43 
44 namespace BaseLib {
45 
46 class SharedObjects;
47 class FileDescriptor;
48 
49 class SsdpInfo {
50  public:
51  SsdpInfo();
52  SsdpInfo(std::string ip, int32_t port, std::string path, PVariable info);
53  virtual ~SsdpInfo();
54  std::string ip() { return _ip; }
55  void setIp(std::string value) { _ip = value; }
56  int32_t port() { return _port; }
57  void setPort(int32_t value) { _port = value; }
58  void setPath(std::string value) { _path = value; }
59  std::string location() { return _location; }
60  void setLocation(std::string value) { _location = value; }
61  const PVariable info() { return _info; }
62  void setInfo(PVariable value) { _info = value; }
63  void addField(std::string key, std::string value) { _fields.emplace(key, value); }
64  std::string getField(std::string key) {
65  auto fieldsIterator = _fields.find(key);
66  if (fieldsIterator != _fields.end()) return fieldsIterator->second; else return "";
67  }
68  std::unordered_map<std::string, std::string> &getFields() { return _fields; };
69  private:
70  std::string _ip;
71  int32_t _port;
72  std::string _path;
73  std::string _location;
74  PVariable _info;
75  std::unordered_map<std::string, std::string> _fields;
76 };
77 
78 class Ssdp {
79  public:
80  Ssdp(BaseLib::SharedObjects *baseLib);
81  virtual ~Ssdp();
82 
90  void searchDevices(const std::string &stHeader, uint32_t timeout, std::vector<SsdpInfo> &devices);
91 
100  void searchDevicesPassive(const std::string &stHeader, uint32_t timeout, std::vector<SsdpInfo> &devices, std::atomic_bool &abort);
101  private:
102  BaseLib::SharedObjects *_bl = nullptr;
103  std::string _address;
104 
105  void getAddress();
106  void sendSearchBroadcast(std::shared_ptr<FileDescriptor> &serverSocketDescriptor, const std::string &stHeader, uint32_t timeout);
107  void processPacket(Http &http, const std::string &stHeader, std::map<std::string, SsdpInfo> &info);
108  void processPacketPassive(Http &http, const std::string &stHeader, std::map<std::string, SsdpInfo> &info);
109  void getDeviceInfo(std::map<std::string, SsdpInfo> &info, std::vector<SsdpInfo> &devices);
110  std::shared_ptr<FileDescriptor> getSocketDescriptor(int32_t port, bool bindToMulticast);
111 };
112 
113 }
114 #endif
Definition: Ssdp.h:49
int32_t port()
Definition: Ssdp.h:56
This is the base library main class.
Definition: BaseLib.h:95
Definition: BaseLib.cpp:34
void addField(std::string key, std::string value)
Definition: Ssdp.h:63
const PVariable info()
Definition: Ssdp.h:61
SsdpInfo()
Definition: Ssdp.cpp:38
std::string location()
Definition: Ssdp.h:59
PVariable value
Definition: UiElements.h:217
Definition: Ssdp.h:78
virtual ~SsdpInfo()
Definition: Ssdp.cpp:48
std::shared_ptr< Variable > PVariable
Definition: PhysicalInterfaceSettings.h:41
std::string getField(std::string key)
Definition: Ssdp.h:64
std::unordered_map< std::string, std::string > & getFields()
Definition: Ssdp.h:68
void setIp(std::string value)
Definition: Ssdp.h:55
void setPort(int32_t value)
Definition: Ssdp.h:57
Definition: Http.h:57
void setLocation(std::string value)
Definition: Ssdp.h:60
std::string ip()
Definition: Ssdp.h:54
void setInfo(PVariable value)
Definition: Ssdp.h:62
void setPath(std::string value)
Definition: Ssdp.h:58