San Mehat | 3c5a6f0 | 2009-05-22 15:36:13 -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 | */ |
| 16 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
| 18 | #include <sys/socket.h> |
| 19 | #include <sys/select.h> |
| 20 | #include <sys/time.h> |
| 21 | #include <sys/types.h> |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 22 | #include <errno.h> |
| 23 | #include <pthread.h> |
| 24 | |
| 25 | #define LOG_TAG "WifiScanner" |
| 26 | #include <cutils/log.h> |
| 27 | |
| 28 | #include "WifiScanner.h" |
| 29 | #include "Supplicant.h" |
| 30 | |
| 31 | extern "C" int pthread_cancel(pthread_t thread); |
| 32 | |
| 33 | WifiScanner::WifiScanner(Supplicant *suppl, int period) { |
| 34 | mSuppl = suppl; |
| 35 | mPeriod = period; |
| 36 | mActive = false; |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 37 | } |
| 38 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 39 | int WifiScanner::start(bool active) { |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 40 | mActive = active; |
| 41 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 42 | if(pipe(mCtrlPipe)) |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 43 | return -1; |
| 44 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 45 | if (pthread_create(&mThread, NULL, WifiScanner::threadStart, this)) |
| 46 | return -1; |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 47 | return 0; |
| 48 | } |
| 49 | |
| 50 | void *WifiScanner::threadStart(void *obj) { |
| 51 | WifiScanner *me = reinterpret_cast<WifiScanner *>(obj); |
| 52 | me->run(); |
| 53 | pthread_exit(NULL); |
| 54 | return NULL; |
| 55 | } |
| 56 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 57 | int WifiScanner::stop() { |
| 58 | char c = 0; |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 59 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 60 | if (write(mCtrlPipe[1], &c, 1) != 1) { |
| 61 | LOGE("Error writing to control pipe (%s)", strerror(errno)); |
| 62 | return -1; |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 63 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 64 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 65 | void *ret; |
| 66 | if (pthread_join(mThread, &ret)) { |
| 67 | LOGE("Error joining to scanner thread (%s)", strerror(errno)); |
| 68 | return -1; |
| 69 | } |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 70 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 71 | close(mCtrlPipe[0]); |
| 72 | close(mCtrlPipe[1]); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 73 | return 0; |
| 74 | } |
| 75 | |
| 76 | void WifiScanner::run() { |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 77 | LOGD("Starting wifi scanner (active = %d)", mActive); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 78 | |
| 79 | while(1) { |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 80 | fd_set read_fds; |
| 81 | struct timeval to; |
| 82 | int rc = 0; |
| 83 | |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 84 | to.tv_usec = 0; |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 85 | to.tv_sec = mPeriod; |
| 86 | |
| 87 | FD_ZERO(&read_fds); |
| 88 | FD_SET(mCtrlPipe[0], &read_fds); |
| 89 | |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 90 | if (mSuppl->triggerScan(mActive)) { |
| 91 | LOGW("Error triggering scan (%s)", strerror(errno)); |
| 92 | } |
| 93 | |
San Mehat | e67651c | 2009-05-12 15:50:49 -0700 | [diff] [blame] | 94 | if ((rc = select(mCtrlPipe[0] + 1, &read_fds, NULL, NULL, &to)) < 0) { |
| 95 | LOGE("select failed (%s) - sleeping for one scanner period", strerror(errno)); |
| 96 | sleep(mPeriod); |
| 97 | continue; |
| 98 | } else if (!rc) { |
| 99 | } else if (FD_ISSET(mCtrlPipe[0], &read_fds)) |
| 100 | break; |
| 101 | } // while |
San Mehat | 4876567 | 2009-05-20 15:28:43 -0700 | [diff] [blame] | 102 | LOGD("Stopping wifi scanner"); |
San Mehat | 1441e76 | 2009-05-07 11:37:10 -0700 | [diff] [blame] | 103 | } |