blob: 421d84d0f8bbd23f8a26b99eb006cecc78277596 [file] [log] [blame]
San Mehatdc266072009-05-06 11:16:52 -07001/*
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 */
San Mehat3c5a6f02009-05-22 15:36:13 -070016
San Mehatdc266072009-05-06 11:16:52 -070017#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
San Mehatdc266072009-05-06 11:16:52 -070026#include "SupplicantListener.h"
San Mehat78828ff2009-06-01 08:38:45 -070027#include "ISupplicantEventHandler.h"
San Mehat3aff2d12009-06-15 14:10:44 -070028#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"
San Mehatdc266072009-05-06 11:16:52 -070035
San Mehat78828ff2009-06-01 08:38:45 -070036SupplicantListener::SupplicantListener(ISupplicantEventHandler *handlers,
37 struct wpa_ctrl *monitor) :
San Mehatdc266072009-05-06 11:16:52 -070038 SocketListener(wpa_ctrl_get_fd(monitor), false) {
San Mehat78828ff2009-06-01 08:38:45 -070039 mHandlers = handlers;
San Mehatdc266072009-05-06 11:16:52 -070040 mMonitor = monitor;
San Mehat3aff2d12009-06-15 14:10:44 -070041 mFactory = new SupplicantEventFactory();
San Mehatdc266072009-05-06 11:16:52 -070042}
43
San Mehat1441e762009-05-07 11:37:10 -070044bool SupplicantListener::onDataAvailable(SocketClient *cli) {
San Mehatdc266072009-05-06 11:16:52 -070045 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))) {
Steve Block01dda202012-01-06 14:13:42 +000051 ALOGE("wpa_ctrl_recv failed (%s)", strerror(errno));
San Mehat1441e762009-05-07 11:37:10 -070052 return false;
San Mehatdc266072009-05-06 11:16:52 -070053 }
54
55 buf[nread] = '\0';
56 if (!rc && !nread) {
Steve Block8d66c492011-12-20 16:07:45 +000057 ALOGD("Received EOF on supplicant socket\n");
San Mehatdc266072009-05-06 11:16:52 -070058 strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
59 buf[buflen-1] = '\0';
60 return false;
61 }
62
San Mehat3aff2d12009-06-15 14:10:44 -070063 SupplicantEvent *evt = mFactory->createEvent(buf, nread);
San Mehatdc266072009-05-06 11:16:52 -070064
San Mehat3aff2d12009-06-15 14:10:44 -070065 if (!evt) {
Steve Blockae8b56c2012-01-05 22:25:38 +000066 ALOGW("Dropping unknown supplicant event '%s'", buf);
San Mehat3aff2d12009-06-15 14:10:44 -070067 return true;
San Mehatdc266072009-05-06 11:16:52 -070068 }
69
San Mehat3aff2d12009-06-15 14:10:44 -070070 // 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
Steve Blockae8b56c2012-01-05 22:25:38 +000086 ALOGW("Whoops - no handler available for event '%s'\n", buf);
San Mehat3aff2d12009-06-15 14:10:44 -070087#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
San Mehatdc266072009-05-06 11:16:52 -0700106 delete evt;
San Mehat3c5a6f02009-05-22 15:36:13 -0700107
San Mehatdc266072009-05-06 11:16:52 -0700108 return true;
109}