1 // Copyright 2021 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include "host_manager.hpp"
18 #include "socket_manager.hpp"
19 
20 #include <cstdint>
21 #include <mutex>
22 #include <string>
23 #include <thread>
24 
25 class Nemora
26 {
27   public:
28     /**
29      * Constructs a Nemora object.
30      * - iface_name: The networking interface to use (eg. eth0)
31      * - ipv4: Target IPv4 address for UDP communication, i.e., POST streaming.
32      * - ipv6: Target IPv6 address for UDP communication, i.e., POST streaming.
33      */
34     Nemora(const std::string& iface_name, const in_addr ipv4,
35            const in6_addr ipv6);
36 
37     /**
38      * Construct uninitialized Nemora object
39      */
40     Nemora();
41 
42     /**
43      * Cancels polling threads and destructs Nemora object.
44      */
45     ~Nemora() = default;
46 
47     /**
48      * Loops collecting the current state of event_data_ and sending via UDP.
49      */
50     void UdpPoll();
51 
52   private:
53     /**
54      * Initialize event_data_ with default values.
55      * This is used by constructors.
56      */
57     void InitEventData();
58 
59     /**
60      * Fetches MAC from host
61      * - mac: out-parameter for host mac address
62      * - iface_path: DBus path to network interface, typically
63      *   IFACE_ROOT + iface_path_.
64      *
65      * - returns: true if address was populated correctly, false if error
66      */
67     bool GetMacAddr(MacAddr* mac, const std::string& iface_path);
68 
69     /**
70      * Converts from string to struct
71      * - mac_addr: string of format MAC_FORMAT
72      * - mac: out-parameter with MAC from mac_addr populated. must be allocated
73      * by caller
74      *
75      * - returns: true if mac_addr was correct format, false otherwise
76      */
77     bool ParseMac(const std::string& mac_addr, MacAddr* mac);
78 
79     /**
80      * Update event_data_ from host.
81      * - postcodes: list of postcodes polled.
82      *   Forced to bind to temporary to avoid copying.
83      */
84     void UpdateEventData(std::vector<uint64_t>&& postcodes);
85 
86     NemoraEvent event_data_ = {};
87     std::mutex event_data_mutex_;
88 
89     SocketManager socketManager_;
90     HostManager hostManager_;
91     const std::string iface_path_;
92 };
93