blob: f4ae8ee9747bba5a29ee428110b2f3626c696658 [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
29#include <bluedroid/bluetooth.h>
30
31#define LOG_TAG "PanController"
32#include <cutils/log.h>
33
34#include "PanController.h"
35
36extern "C" int bt_is_enabled();
37
38PanController::PanController() {
39 mPid = 0;
40}
41
42PanController::~PanController() {
43}
44
45int PanController::startPan() {
46 pid_t pid;
47
48 if (!bt_is_enabled()) {
49 LOGE("Cannot start PAN services - Bluetooth not running");
50 errno = ENODEV;
51 return -1;
52 }
53
54 if (mPid) {
55 LOGE("PAN already started");
56 errno = EBUSY;
57 return -1;
58 }
59
60 if ((pid = fork()) < 0) {
61 LOGE("fork failed (%s)", strerror(errno));
62 return -1;
63 }
64
65 if (!pid) {
66 if (execl("/system/bin/pand", "/system/bin/pand", "--nodetach", "--listen",
67 "--role", "NAP", (char *) NULL)) {
68 LOGE("execl failed (%s)", strerror(errno));
69 }
70 LOGE("Should never get here!");
71 return 0;
72 } else {
73 mPid = pid;
74 }
75 return 0;
76
77}
78
79int PanController::stopPan() {
80 if (mPid == 0) {
81 LOGE("PAN already stopped");
82 return 0;
83 }
84
85 LOGD("Stopping PAN services");
86 kill(mPid, SIGTERM);
87 waitpid(mPid, NULL, 0);
88 mPid = 0;
89 LOGD("PAN services stopped");
90 return 0;
91}
92
93bool PanController::isPanStarted() {
94 return (mPid != 0 ? true : false);
95}