Home | History | Annotate | Download | only in nexus
      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 <stdlib.h>
     18 #include <sys/socket.h>
     19 #include <sys/select.h>
     20 #include <sys/time.h>
     21 #include <sys/types.h>
     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;
     37 }
     38 
     39 int WifiScanner::start(bool active) {
     40     mActive = active;
     41 
     42     if(pipe(mCtrlPipe))
     43         return -1;
     44 
     45     if (pthread_create(&mThread, NULL, WifiScanner::threadStart, this))
     46         return -1;
     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 
     57 int WifiScanner::stop() {
     58     char c = 0;
     59 
     60     if (write(mCtrlPipe[1], &c, 1) != 1) {
     61         LOGE("Error writing to control pipe (%s)", strerror(errno));
     62         return -1;
     63     }
     64 
     65     void *ret;
     66     if (pthread_join(mThread, &ret)) {
     67         LOGE("Error joining to scanner thread (%s)", strerror(errno));
     68         return -1;
     69     }
     70 
     71     close(mCtrlPipe[0]);
     72     close(mCtrlPipe[1]);
     73     return 0;
     74 }
     75 
     76 void WifiScanner::run() {
     77     LOGD("Starting wifi scanner (active = %d)", mActive);
     78 
     79     while(1) {
     80         fd_set read_fds;
     81         struct timeval to;
     82         int rc = 0;
     83 
     84         to.tv_usec = 0;
     85         to.tv_sec = mPeriod;
     86 
     87         FD_ZERO(&read_fds);
     88         FD_SET(mCtrlPipe[0], &read_fds);
     89 
     90         if (mSuppl->triggerScan(mActive)) {
     91             LOGW("Error triggering scan (%s)", strerror(errno));
     92         }
     93 
     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
    102     LOGD("Stopping wifi scanner");
    103 }
    104