blob: 08e3024a76fa42987aac9ae8ddc06bf50e4afa6e [file] [log] [blame]
San Mehat1bdac9e2010-01-21 14:03:06 -08001/*
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 <errno.h>
19#include <fcntl.h>
20
21#include <sys/socket.h>
22#include <sys/stat.h>
23#include <sys/types.h>
24#include <sys/wait.h>
25
26#include <netinet/in.h>
27#include <arpa/inet.h>
28
Rene Bolldorf7826bd02010-08-17 19:07:01 +020029#ifdef HAVE_BLUETOOTH
San Mehat1bdac9e2010-01-21 14:03:06 -080030#include <bluedroid/bluetooth.h>
Rene Bolldorf7826bd02010-08-17 19:07:01 +020031#endif
San Mehat1bdac9e2010-01-21 14:03:06 -080032
33#define LOG_TAG "PanController"
34#include <cutils/log.h>
35
36#include "PanController.h"
37
San Mehat03997102010-01-21 15:53:32 -080038#ifdef HAVE_BLUETOOTH
San Mehat1bdac9e2010-01-21 14:03:06 -080039extern "C" int bt_is_enabled();
San Mehat03997102010-01-21 15:53:32 -080040#endif
San Mehat1bdac9e2010-01-21 14:03:06 -080041
42PanController::PanController() {
43 mPid = 0;
44}
45
46PanController::~PanController() {
47}
48
49int PanController::startPan() {
50 pid_t pid;
51
San Mehat03997102010-01-21 15:53:32 -080052#ifdef HAVE_BLUETOOTH
San Mehat1bdac9e2010-01-21 14:03:06 -080053 if (!bt_is_enabled()) {
54 LOGE("Cannot start PAN services - Bluetooth not running");
55 errno = ENODEV;
56 return -1;
57 }
San Mehat03997102010-01-21 15:53:32 -080058#else
59 LOGE("Cannot start PAN services - No Bluetooth support");
60 errno = ENODEV;
61 return -1;
62#endif
San Mehat1bdac9e2010-01-21 14:03:06 -080063
64 if (mPid) {
65 LOGE("PAN already started");
66 errno = EBUSY;
67 return -1;
68 }
69
70 if ((pid = fork()) < 0) {
71 LOGE("fork failed (%s)", strerror(errno));
72 return -1;
73 }
74
75 if (!pid) {
76 if (execl("/system/bin/pand", "/system/bin/pand", "--nodetach", "--listen",
77 "--role", "NAP", (char *) NULL)) {
78 LOGE("execl failed (%s)", strerror(errno));
79 }
80 LOGE("Should never get here!");
81 return 0;
82 } else {
83 mPid = pid;
84 }
85 return 0;
86
87}
88
89int PanController::stopPan() {
90 if (mPid == 0) {
91 LOGE("PAN already stopped");
92 return 0;
93 }
94
95 LOGD("Stopping PAN services");
96 kill(mPid, SIGTERM);
97 waitpid(mPid, NULL, 0);
98 mPid = 0;
99 LOGD("PAN services stopped");
100 return 0;
101}
102
103bool PanController::isPanStarted() {
104 return (mPid != 0 ? true : false);
105}