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