• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include <errno.h>
18 #include <sys/types.h>
19 #include <pthread.h>
20 
21 #define LOG_TAG "SupplicantListener"
22 #include <cutils/log.h>
23 
24 #include "libwpa_client/wpa_ctrl.h"
25 
26 #include "SupplicantListener.h"
27 #include "ISupplicantEventHandler.h"
28 #include "SupplicantEventFactory.h"
29 #include "SupplicantEvent.h"
30 #include "SupplicantAssociatingEvent.h"
31 #include "SupplicantAssociatedEvent.h"
32 #include "SupplicantConnectedEvent.h"
33 #include "SupplicantScanResultsEvent.h"
34 #include "SupplicantStateChangeEvent.h"
35 
SupplicantListener(ISupplicantEventHandler * handlers,struct wpa_ctrl * monitor)36 SupplicantListener::SupplicantListener(ISupplicantEventHandler *handlers,
37                                        struct wpa_ctrl *monitor) :
38                     SocketListener(wpa_ctrl_get_fd(monitor), false) {
39     mHandlers = handlers;
40     mMonitor = monitor;
41     mFactory = new SupplicantEventFactory();
42 }
43 
onDataAvailable(SocketClient * cli)44 bool SupplicantListener::onDataAvailable(SocketClient *cli) {
45     char buf[255];
46     size_t buflen = sizeof(buf);
47     int rc;
48     size_t nread = buflen - 1;
49 
50     if ((rc = wpa_ctrl_recv(mMonitor, buf, &nread))) {
51         LOGE("wpa_ctrl_recv failed (%s)", strerror(errno));
52         return false;
53     }
54 
55     buf[nread] = '\0';
56     if (!rc && !nread) {
57         LOGD("Received EOF on supplicant socket\n");
58         strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
59         buf[buflen-1] = '\0';
60         return false;
61     }
62 
63     SupplicantEvent *evt = mFactory->createEvent(buf, nread);
64 
65     if (!evt) {
66         LOGW("Dropping unknown supplicant event '%s'", buf);
67         return true;
68     }
69 
70     // Call the appropriate handler
71     if (evt->getType() == SupplicantEvent::EVENT_ASSOCIATING)
72         mHandlers->onAssociatingEvent((SupplicantAssociatingEvent *) evt);
73     else if (evt->getType() == SupplicantEvent::EVENT_ASSOCIATED)
74         mHandlers->onAssociatedEvent((SupplicantAssociatedEvent *) evt);
75     else if (evt->getType() == SupplicantEvent::EVENT_CONNECTED)
76         mHandlers->onConnectedEvent((SupplicantConnectedEvent *) evt);
77     else if (evt->getType() == SupplicantEvent::EVENT_SCAN_RESULTS)
78         mHandlers->onScanResultsEvent((SupplicantScanResultsEvent *) evt);
79     else if (evt->getType() == SupplicantEvent::EVENT_STATE_CHANGE)
80         mHandlers->onStateChangeEvent((SupplicantStateChangeEvent *) evt);
81     else if (evt->getType() == SupplicantEvent::EVENT_CONNECTIONTIMEOUT)
82         mHandlers->onConnectionTimeoutEvent((SupplicantConnectionTimeoutEvent *) evt);
83     else if (evt->getType() == SupplicantEvent::EVENT_DISCONNECTED)
84         mHandlers->onDisconnectedEvent((SupplicantDisconnectedEvent *) evt);
85     else
86         LOGW("Whoops - no handler available for event '%s'\n", buf);
87 #if 0
88     else if (evt->getType() == SupplicantEvent::EVENT_TERMINATING)
89         mHandlers->onTerminatingEvent(evt);
90     else if (evt->getType() == SupplicantEvent::EVENT_PASSWORD_CHANGED)
91         mHandlers->onPasswordChangedEvent(evt);
92     else if (evt->getType() == SupplicantEvent::EVENT_EAP_NOTIFICATION)
93         mHandlers->onEapNotificationEvent(evt);
94     else if (evt->getType() == SupplicantEvent::EVENT_EAP_STARTED)
95         mHandlers->onEapStartedEvent(evt);
96     else if (evt->getType() == SupplicantEvent::EVENT_EAP_SUCCESS)
97         mHandlers->onEapSuccessEvent(evt);
98     else if (evt->getType() == SupplicantEvent::EVENT_EAP_FAILURE)
99         mHandlers->onEapFailureEvent(evt);
100     else if (evt->getType() == SupplicantEvent::EVENT_LINK_SPEED)
101         mHandlers->onLinkSpeedEvent(evt);
102     else if (evt->getType() == SupplicantEvent::EVENT_DRIVER_STATE)
103         mHandlers->onDriverStateEvent(evt);
104 #endif
105 
106     delete evt;
107 
108     return true;
109 }
110