SupplicantListener.cpp revision 1441e769b2767e212a3d905bee2fd3535b484ff2
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#include <errno.h>
17#include <sys/types.h>
18#include <pthread.h>
19
20#define LOG_TAG "SupplicantListener"
21#include <cutils/log.h>
22
23#include "libwpa_client/wpa_ctrl.h"
24
25#include "Supplicant.h"
26#include "SupplicantListener.h"
27#include "SupplicantEvent.h"
28
29SupplicantListener::SupplicantListener(Supplicant *supplicant, struct wpa_ctrl *monitor) :
30                    SocketListener(wpa_ctrl_get_fd(monitor), false) {
31    mSupplicant = supplicant;
32    mMonitor = monitor;
33}
34
35bool SupplicantListener::onDataAvailable(SocketClient *cli) {
36    char buf[255];
37    size_t buflen = sizeof(buf);
38    int rc;
39    size_t nread = buflen - 1;
40
41    if ((rc = wpa_ctrl_recv(mMonitor, buf, &nread))) {
42        LOGE("wpa_ctrl_recv failed (%s)", strerror(errno));
43        return false;
44    }
45
46    buf[nread] = '\0';
47    if (!rc && !nread) {
48        LOGD("Received EOF on supplicant socket\n");
49        strncpy(buf, WPA_EVENT_TERMINATING " - signal 0 received", buflen-1);
50        buf[buflen-1] = '\0';
51        return false;
52    }
53
54    SupplicantEvent *evt = new SupplicantEvent(buf, nread);
55
56    // XXX: Make this a factory
57    // XXX: Instead of calling Supplicant directly
58    // extract an Interface and use that instead
59    if (evt->getType() == SupplicantEvent::EVENT_CONNECTED)
60        rc = mSupplicant->onConnectedEvent(evt);
61    else if (evt->getType() == SupplicantEvent::EVENT_DISCONNECTED)
62        rc = mSupplicant->onDisconnectedEvent(evt);
63    else if (evt->getType() == SupplicantEvent::EVENT_TERMINATING)
64        rc = mSupplicant->onTerminatingEvent(evt);
65    else if (evt->getType() == SupplicantEvent::EVENT_PASSWORD_CHANGED)
66        rc = mSupplicant->onPasswordChangedEvent(evt);
67    else if (evt->getType() == SupplicantEvent::EVENT_EAP_NOTIFICATION)
68        rc = mSupplicant->onEapNotificationEvent(evt);
69    else if (evt->getType() == SupplicantEvent::EVENT_EAP_STARTED)
70        rc = mSupplicant->onEapStartedEvent(evt);
71    else if (evt->getType() == SupplicantEvent::EVENT_EAP_SUCCESS)
72        rc = mSupplicant->onEapSuccessEvent(evt);
73    else if (evt->getType() == SupplicantEvent::EVENT_EAP_FAILURE)
74        rc = mSupplicant->onEapFailureEvent(evt);
75    else if (evt->getType() == SupplicantEvent::EVENT_SCAN_RESULTS)
76        rc = mSupplicant->onScanResultsEvent(evt);
77    else if (evt->getType() == SupplicantEvent::EVENT_STATE_CHANGE)
78        rc = mSupplicant->onStateChangeEvent(evt);
79    else if (evt->getType() == SupplicantEvent::EVENT_LINK_SPEED)
80        rc = mSupplicant->onLinkSpeedEvent(evt);
81    else if (evt->getType() == SupplicantEvent::EVENT_DRIVER_STATE)
82        rc = mSupplicant->onDriverStateEvent(evt);
83    else {
84        LOGW("Ignoring unknown event");
85    }
86
87    delete evt;
88
89    if (rc) {
90        LOGW("Handler %d (%s) error: %s", evt->getType(), evt->getEvent(), strerror(errno));
91        return false;
92    }
93    return true;
94}
95