Add a copyright header
[dhcpcd-ui] / src / dhcpcd-qt / dhcpcd-wi.cpp
1 /*
2  * dhcpcd-qt
3  * Copyright 2014 Roy Marples <roy@marples.name>
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26
27 #include <QObject>
28 #include <QSocketNotifier>
29 #include <QTimer>
30
31 #include <cerrno>
32
33 #include "config.h"
34 #include "dhcpcd-wi.h"
35
36 DhcpcdWi::DhcpcdWi(DhcpcdQt *parent, DHCPCD_WPA *wpa)
37 {
38
39         this->dhcpcdQt = parent;
40         this->wpa = wpa;
41         scans = NULL;
42
43         int fd = dhcpcd_wpa_get_fd(wpa);
44         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
45         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
46         retryOpenTimer = NULL;
47 }
48
49 DhcpcdWi::~DhcpcdWi()
50 {
51
52         dhcpcd_wi_scans_free(scans);
53         if (notifier != NULL)
54                 delete notifier;
55 }
56
57 DHCPCD_WPA * DhcpcdWi::getWpa()
58 {
59
60         return wpa;
61 }
62
63 DHCPCD_WI_SCAN *DhcpcdWi::getScans()
64 {
65
66         return scans;
67 }
68 void DhcpcdWi::setScans(DHCPCD_WI_SCAN *scans)
69 {
70
71         dhcpcd_wi_scans_free(this->scans);
72         this->scans = scans;
73 }
74
75 void DhcpcdWi::wpaOpen()
76 {
77         int fd = dhcpcd_wpa_open(wpa);
78         static int last_error;
79
80         if (fd == -1) {
81                 if (errno != last_error) {
82                         last_error = errno;
83                         qCritical("%s: dhcpcd_wpa_open: %s",
84                             dhcpcd_wpa_if(wpa)->ifname,
85                             strerror(last_error));
86                 }
87                 return;
88         }
89
90         notifier = new QSocketNotifier(fd, QSocketNotifier::Read);
91         connect(notifier, SIGNAL(activated(int)), this, SLOT(dispatch()));
92         if (retryOpenTimer) {
93                 delete retryOpenTimer;
94                 retryOpenTimer = NULL;
95         }
96 }
97
98 void DhcpcdWi::dispatch()
99 {
100
101         if (dhcpcd_wpa_get_fd(wpa) == -1) {
102                 delete notifier;
103                 notifier = NULL;
104                 DHCPCD_IF *i = dhcpcd_wpa_if(wpa);
105                 if (i == NULL ||
106                     strcmp(i->reason, "DEPARTED") == 0 ||
107                     strcmp(i->reason, "STOPPED") == 0)
108                         return;
109                 qWarning("%s: %s",
110                     i->ifname,
111                     qPrintable(tr("dhcpcd WPA connection lost")));
112                 if (retryOpenTimer == NULL) {
113                         retryOpenTimer = new QTimer(this);
114                         connect(retryOpenTimer, SIGNAL(timeout()),
115                             this, SLOT(wpaOpen()));
116                         retryOpenTimer->start(DHCPCD_RETRYOPEN);
117                 }
118                 return;
119         }
120
121         dhcpcd_wpa_dispatch(wpa);
122 }