blob: 4c956ac723276406f70ebcec439e904cc13ab2b1 [file] [log] [blame]
San Mehat3c5a6f02009-05-22 15:36:13 -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 */
16
San Mehate67651c2009-05-12 15:50:49 -070017#include <stdlib.h>
18#include <sys/socket.h>
19#include <sys/select.h>
20#include <sys/time.h>
21#include <sys/types.h>
San Mehat1441e762009-05-07 11:37:10 -070022#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
31extern "C" int pthread_cancel(pthread_t thread);
32
33WifiScanner::WifiScanner(Supplicant *suppl, int period) {
34 mSuppl = suppl;
35 mPeriod = period;
36 mActive = false;
San Mehat1441e762009-05-07 11:37:10 -070037}
38
San Mehate67651c2009-05-12 15:50:49 -070039int WifiScanner::start(bool active) {
San Mehat1441e762009-05-07 11:37:10 -070040 mActive = active;
41
San Mehate67651c2009-05-12 15:50:49 -070042 if(pipe(mCtrlPipe))
San Mehat1441e762009-05-07 11:37:10 -070043 return -1;
44
San Mehate67651c2009-05-12 15:50:49 -070045 if (pthread_create(&mThread, NULL, WifiScanner::threadStart, this))
46 return -1;
San Mehat1441e762009-05-07 11:37:10 -070047 return 0;
48}
49
50void *WifiScanner::threadStart(void *obj) {
51 WifiScanner *me = reinterpret_cast<WifiScanner *>(obj);
52 me->run();
53 pthread_exit(NULL);
54 return NULL;
55}
56
San Mehate67651c2009-05-12 15:50:49 -070057int WifiScanner::stop() {
58 char c = 0;
San Mehat1441e762009-05-07 11:37:10 -070059
San Mehate67651c2009-05-12 15:50:49 -070060 if (write(mCtrlPipe[1], &c, 1) != 1) {
Steve Block01dda202012-01-06 14:13:42 +000061 ALOGE("Error writing to control pipe (%s)", strerror(errno));
San Mehate67651c2009-05-12 15:50:49 -070062 return -1;
San Mehat1441e762009-05-07 11:37:10 -070063 }
San Mehat1441e762009-05-07 11:37:10 -070064
San Mehate67651c2009-05-12 15:50:49 -070065 void *ret;
66 if (pthread_join(mThread, &ret)) {
Steve Block01dda202012-01-06 14:13:42 +000067 ALOGE("Error joining to scanner thread (%s)", strerror(errno));
San Mehate67651c2009-05-12 15:50:49 -070068 return -1;
69 }
San Mehat1441e762009-05-07 11:37:10 -070070
San Mehate67651c2009-05-12 15:50:49 -070071 close(mCtrlPipe[0]);
72 close(mCtrlPipe[1]);
San Mehat1441e762009-05-07 11:37:10 -070073 return 0;
74}
75
76void WifiScanner::run() {
Steve Block8d66c492011-12-20 16:07:45 +000077 ALOGD("Starting wifi scanner (active = %d)", mActive);
San Mehat1441e762009-05-07 11:37:10 -070078
79 while(1) {
San Mehate67651c2009-05-12 15:50:49 -070080 fd_set read_fds;
81 struct timeval to;
82 int rc = 0;
83
San Mehat48765672009-05-20 15:28:43 -070084 to.tv_usec = 0;
San Mehate67651c2009-05-12 15:50:49 -070085 to.tv_sec = mPeriod;
86
87 FD_ZERO(&read_fds);
88 FD_SET(mCtrlPipe[0], &read_fds);
89
San Mehat1441e762009-05-07 11:37:10 -070090 if (mSuppl->triggerScan(mActive)) {
Steve Blockae8b56c2012-01-05 22:25:38 +000091 ALOGW("Error triggering scan (%s)", strerror(errno));
San Mehat1441e762009-05-07 11:37:10 -070092 }
93
San Mehate67651c2009-05-12 15:50:49 -070094 if ((rc = select(mCtrlPipe[0] + 1, &read_fds, NULL, NULL, &to)) < 0) {
Steve Block01dda202012-01-06 14:13:42 +000095 ALOGE("select failed (%s) - sleeping for one scanner period", strerror(errno));
San Mehate67651c2009-05-12 15:50:49 -070096 sleep(mPeriod);
97 continue;
98 } else if (!rc) {
99 } else if (FD_ISSET(mCtrlPipe[0], &read_fds))
100 break;
101 } // while
Steve Block8d66c492011-12-20 16:07:45 +0000102 ALOGD("Stopping wifi scanner");
San Mehat1441e762009-05-07 11:37:10 -0700103}