San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 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 | */ |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 16 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 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 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 26 | #include "SupplicantListener.h" |
San Mehat | 78828ff | 2009-06-01 08:38:45 -0700 | [diff] [blame] | 27 | #include "ISupplicantEventHandler.h" |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame^] | 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" |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 35 | |
San Mehat | 78828ff | 2009-06-01 08:38:45 -0700 | [diff] [blame] | 36 | SupplicantListener::SupplicantListener(ISupplicantEventHandler *handlers, |
| 37 | struct wpa_ctrl *monitor) : |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 38 | SocketListener(wpa_ctrl_get_fd(monitor), false) { |
San Mehat | 78828ff | 2009-06-01 08:38:45 -0700 | [diff] [blame] | 39 | mHandlers = handlers; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 40 | mMonitor = monitor; |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame^] | 41 | mFactory = new SupplicantEventFactory(); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 42 | } |
| 43 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 44 | bool SupplicantListener::onDataAvailable(SocketClient *cli) { |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 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)); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 52 | return false; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 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 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame^] | 63 | SupplicantEvent *evt = mFactory->createEvent(buf, nread); |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 64 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame^] | 65 | if (!evt) { |
| 66 | LOGW("Dropping unknown supplicant event '%s'", buf); |
| 67 | return true; |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 68 | } |
| 69 | |
San Mehat | 3aff2d1 | 2009-06-15 14:10:44 -0700 | [diff] [blame^] | 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 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 106 | delete evt; |
San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -0700 | [diff] [blame] | 107 | |
San Mehat | dc26607 | 2009-05-06 11:16:52 -0700 | [diff] [blame] | 108 | return true; |
| 109 | } |