blob: cf717337e81de49760ded58b41f666f231388cb9 [file] [log] [blame]
San Mehatc4a895b2009-06-23 21:10:57 -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#include <stdio.h>
17#include <errno.h>
18#include <stdlib.h>
19#include <sys/socket.h>
20#include <sys/select.h>
21#include <sys/time.h>
22#include <sys/types.h>
23#include <sys/un.h>
24
25#define LOG_TAG "WifiStatusPoller"
26#include <cutils/log.h>
27
28#include "WifiStatusPoller.h"
29#include "IWifiStatusPollerHandler.h"
30
31
32WifiStatusPoller::WifiStatusPoller(IWifiStatusPollerHandler *handler) :
33 mHandlers(handler) {
34 mPollingInterval = 5;
35 mStarted = false;
36}
37
38int WifiStatusPoller::start() {
39
40 if (pipe(mCtrlPipe))
41 return -1;
42
43 if (pthread_create(&mThread, NULL, WifiStatusPoller::threadStart, this))
44 return -1;
45
46 return 0;
47}
48
49int WifiStatusPoller::stop() {
50 char c = 0;
51
52 if (write(mCtrlPipe[1], &c, 1) != 1) {
53 LOGE("Error writing to control pipe (%s)", strerror(errno));
54 return -1;
55 }
56
57 void *ret;
58 if (pthread_join(mThread, &ret)) {
59 LOGE("Error joining to listener thread (%s)", strerror(errno));
60 return -1;
61 }
62 close(mCtrlPipe[0]);
63 close(mCtrlPipe[1]);
64 return 0;
65}
66
67void *WifiStatusPoller::threadStart(void *obj) {
68 WifiStatusPoller *me = reinterpret_cast<WifiStatusPoller *>(obj);
69
70 me->mStarted = true;
71 LOGD("Starting");
72 me->run();
73 me->mStarted = false;
74 LOGD("Stopping");
75 pthread_exit(NULL);
76 return NULL;
77}
78
79void WifiStatusPoller::run() {
80
81 while(1) {
82 struct timeval to;
83 fd_set read_fds;
84 int rc = 0;
85 int max = 0;
86
87 FD_ZERO(&read_fds);
88 to.tv_usec = 0;
89 to.tv_sec = mPollingInterval;
90
91 FD_SET(mCtrlPipe[0], &read_fds);
92 max = mCtrlPipe[0];
93
94 if ((rc = select(max + 1, &read_fds, NULL, NULL, &to)) < 0) {
95 LOGE("select failed (%s)", strerror(errno));
96 sleep(1);
97 continue;
98 } else if (!rc) {
99 mHandlers->onStatusPollInterval();
100 }
101 if (FD_ISSET(mCtrlPipe[0], &read_fds))
102 break;
103 }
104}