• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <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 
36 #ifdef HAVE_BLUETOOTH
37 extern "C" int bt_is_enabled();
38 #endif
39 
PanController()40 PanController::PanController() {
41     mPid = 0;
42 }
43 
~PanController()44 PanController::~PanController() {
45 }
46 
startPan()47 int PanController::startPan() {
48     pid_t pid;
49 
50 #ifdef HAVE_BLUETOOTH
51     if (!bt_is_enabled()) {
52         LOGE("Cannot start PAN services - Bluetooth not running");
53         errno = ENODEV;
54         return -1;
55     }
56 #else
57     LOGE("Cannot start PAN services - No Bluetooth support");
58     errno = ENODEV;
59     return -1;
60 #endif
61 
62     if (mPid) {
63         LOGE("PAN already started");
64         errno = EBUSY;
65         return -1;
66     }
67 
68    if ((pid = fork()) < 0) {
69         LOGE("fork failed (%s)", strerror(errno));
70         return -1;
71     }
72 
73     if (!pid) {
74         if (execl("/system/bin/pand", "/system/bin/pand", "--nodetach", "--listen",
75                   "--role", "NAP", (char *) NULL)) {
76             LOGE("execl failed (%s)", strerror(errno));
77         }
78         LOGE("Should never get here!");
79         return 0;
80     } else {
81         mPid = pid;
82     }
83     return 0;
84 
85 }
86 
stopPan()87 int PanController::stopPan() {
88     if (mPid == 0) {
89         LOGE("PAN already stopped");
90         return 0;
91     }
92 
93     LOGD("Stopping PAN services");
94     kill(mPid, SIGTERM);
95     waitpid(mPid, NULL, 0);
96     mPid = 0;
97     LOGD("PAN services stopped");
98     return 0;
99 }
100 
isPanStarted()101 bool PanController::isPanStarted() {
102     return (mPid != 0 ? true : false);
103 }
104