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