|
| 1 | +From 4e325607f78eed6aeb0d7618325b6fe9fdce028e Mon Sep 17 00:00:00 2001 |
| 2 | +From: Tanguy Pruvot < [email protected]> |
| 3 | +Date: Mon, 4 Jun 2012 23:09:53 +0200 |
| 4 | +Subject: [PATCH 1/2] SoftAP: allow a hostapd service and handle the wifi channel |
| 5 | + |
| 6 | +using command parameters, or the new property "wifi.ap.channel" |
| 7 | + |
| 8 | +Change-Id: I91414562aee5c9f70389935f4c699b34da79f6a6 |
| 9 | +--- |
| 10 | + Android.mk | 4 +++ |
| 11 | + SoftapController.cpp | 92 +++++++++++++++++++++++++++++++++++++++++++++----- |
| 12 | + 2 files changed, 87 insertions(+), 9 deletions(-) |
| 13 | + |
| 14 | +diff --git a/Android.mk b/Android.mk |
| 15 | +index c09b634..ab894cd 100644 |
| 16 | +--- a/Android.mk |
| 17 | ++++ b/Android.mk |
| 18 | +@@ -52,6 +52,10 @@ ifneq ($(BOARD_HOSTAPD_DRIVER),) |
| 19 | + endif |
| 20 | + endif |
| 21 | + |
| 22 | ++ifneq ($(BOARD_HOSTAPD_SERVICE_NAME),) |
| 23 | ++ LOCAL_CFLAGS += -DHOSTAPD_SERVICE_NAME=\"$(BOARD_HOSTAPD_SERVICE_NAME)\" |
| 24 | ++endif |
| 25 | ++ |
| 26 | + ifneq ($(BOARD_HOSTAPD_NO_ENTROPY),) |
| 27 | + LOCAL_CFLAGS += -DHOSTAPD_NO_ENTROPY |
| 28 | + endif |
| 29 | +diff --git a/SoftapController.cpp b/SoftapController.cpp |
| 30 | +index 820811f..b9d745d 100644 |
| 31 | +--- a/SoftapController.cpp |
| 32 | ++++ b/SoftapController.cpp |
| 33 | +@@ -35,6 +35,7 @@ |
| 34 | + |
| 35 | + #define LOG_TAG "SoftapController" |
| 36 | + #include <cutils/log.h> |
| 37 | ++#include <cutils/properties.h> |
| 38 | + #include <netutils/ifc.h> |
| 39 | + #include <private/android_filesystem_config.h> |
| 40 | + #include "wifi.h" |
| 41 | +@@ -45,8 +46,13 @@ |
| 42 | + #define HOSTAPD_DRIVER_NAME "nl80211" |
| 43 | + #endif |
| 44 | + |
| 45 | ++#define AP_DEFAULT_CHANNEL 5 |
| 46 | ++#define AP_SOCKET_PATH "/data/misc/wifi/hostapd" |
| 47 | ++ |
| 48 | + static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf"; |
| 49 | + |
| 50 | ++extern "C" int system_nosh(const char *command); |
| 51 | ++ |
| 52 | + SoftapController::SoftapController() { |
| 53 | + mPid = 0; |
| 54 | + mSock = socket(AF_INET, SOCK_DGRAM, 0); |
| 55 | +@@ -183,6 +189,7 @@ int SoftapController::stopDriver(char *iface) { |
| 56 | + int SoftapController::startSoftap() { |
| 57 | + pid_t pid = 1; |
| 58 | + int ret = 0; |
| 59 | ++ char value[PROPERTY_VALUE_MAX]; |
| 60 | + |
| 61 | + if (mPid) { |
| 62 | + LOGE("Softap already started"); |
| 63 | +@@ -192,6 +199,36 @@ int SoftapController::startSoftap() { |
| 64 | + LOGE("Softap startap - failed to open socket"); |
| 65 | + return -1; |
| 66 | + } |
| 67 | ++#ifdef HOSTAPD_SERVICE_NAME |
| 68 | ++ |
| 69 | ++#ifndef HOSTAPD_NO_ENTROPY |
| 70 | ++ ensure_entropy_file_exists(); |
| 71 | ++#endif |
| 72 | ++ ret = system_nosh("/system/bin/start " HOSTAPD_SERVICE_NAME); |
| 73 | ++ pid = (ret == 0); |
| 74 | ++ |
| 75 | ++ usleep(AP_BSS_START_DELAY); |
| 76 | ++ property_get("init.svc." HOSTAPD_SERVICE_NAME, value, "stopped"); |
| 77 | ++ if (strcmp(value, "running") == 0) { |
| 78 | ++ LOGD("hostapd service started"); |
| 79 | ++ } else { |
| 80 | ++ ret = -1; |
| 81 | ++ } |
| 82 | ++ |
| 83 | ++ *mBuf = 0; |
| 84 | ++ ret = setCommand(mIface, "AP_BSS_START"); |
| 85 | ++ if (ret) { |
| 86 | ++ LOGE("Softap startap - failed: %d", ret); |
| 87 | ++ } |
| 88 | ++ else { |
| 89 | ++ mPid = pid; |
| 90 | ++ LOGD("Softap started"); |
| 91 | ++ usleep(AP_BSS_START_DELAY); |
| 92 | ++ } |
| 93 | ++ |
| 94 | ++ return ret; |
| 95 | ++#else |
| 96 | ++ |
| 97 | + #ifdef HAVE_HOSTAPD |
| 98 | + if ((pid = fork()) < 0) { |
| 99 | + LOGE("fork failed (%s)", strerror(errno)); |
| 100 | +@@ -210,7 +247,7 @@ int SoftapController::startSoftap() { |
| 101 | + HOSTAPD_CONF_FILE, (char *) NULL)) { |
| 102 | + LOGE("execl failed (%s)", strerror(errno)); |
| 103 | + } |
| 104 | +-#endif |
| 105 | ++#endif /* HAVE_HOSTAPD */ |
| 106 | + LOGE("Should never get here!"); |
| 107 | + return -1; |
| 108 | + } else { |
| 109 | +@@ -226,7 +263,7 @@ int SoftapController::startSoftap() { |
| 110 | + } |
| 111 | + } |
| 112 | + return ret; |
| 113 | +- |
| 114 | ++#endif /* HOSTAPD_SERVICE_NAME */ |
| 115 | + } |
| 116 | + |
| 117 | + int SoftapController::stopSoftap() { |
| 118 | +@@ -237,6 +274,25 @@ int SoftapController::stopSoftap() { |
| 119 | + return 0; |
| 120 | + } |
| 121 | + |
| 122 | ++#ifdef HOSTAPD_SERVICE_NAME |
| 123 | ++ LOGD("Stopping hostapd service"); |
| 124 | ++ // use the hostapd service defined in init.rc |
| 125 | ++ if (system_nosh("/system/bin/stop " HOSTAPD_SERVICE_NAME)) |
| 126 | ++ { |
| 127 | ++ LOGE("stop failed (%s)", strerror(errno)); |
| 128 | ++ } |
| 129 | ++ if (mSock < 0) { |
| 130 | ++ LOGE("Softap stopap - failed to open socket"); |
| 131 | ++ return -1; |
| 132 | ++ } |
| 133 | ++ *mBuf = 0; |
| 134 | ++ ret = setCommand(mIface, "AP_BSS_STOP"); |
| 135 | ++ mPid = 0; |
| 136 | ++ LOGD("Softap service stopped: %d", ret); |
| 137 | ++ usleep(AP_BSS_STOP_DELAY); |
| 138 | ++ return ret; |
| 139 | ++#else |
| 140 | ++ |
| 141 | + #ifdef HAVE_HOSTAPD |
| 142 | + LOGD("Stopping Softap service"); |
| 143 | + kill(mPid, SIGTERM); |
| 144 | +@@ -252,6 +308,7 @@ int SoftapController::stopSoftap() { |
| 145 | + LOGD("Softap service stopped: %d", ret); |
| 146 | + usleep(AP_BSS_STOP_DELAY); |
| 147 | + return ret; |
| 148 | ++#endif /* HOSTAPD_SERVICE_NAME */ |
| 149 | + } |
| 150 | + |
| 151 | + bool SoftapController::isSoftapStarted() { |
| 152 | +@@ -284,7 +341,7 @@ int SoftapController::addParam(int pos, const char *cmd, const char *arg) |
| 153 | + int SoftapController::setSoftap(int argc, char *argv[]) { |
| 154 | + char psk_str[2*SHA256_DIGEST_LENGTH+1]; |
| 155 | + int ret = 0, i = 0, fd; |
| 156 | +- char *ssid, *iface; |
| 157 | ++ char *ssid; |
| 158 | + |
| 159 | + if (mSock < 0) { |
| 160 | + LOGE("Softap set - failed to open socket"); |
| 161 | +@@ -296,11 +353,11 @@ int SoftapController::setSoftap(int argc, char *argv[]) { |
| 162 | + } |
| 163 | + |
| 164 | + strncpy(mIface, argv[3], sizeof(mIface)); |
| 165 | +- iface = argv[2]; |
| 166 | + |
| 167 | + #ifdef HAVE_HOSTAPD |
| 168 | + char *wbuf = NULL; |
| 169 | + char *fbuf = NULL; |
| 170 | ++ int channel = 0; |
| 171 | + |
| 172 | + if (argc > 4) { |
| 173 | + ssid = argv[4]; |
| 174 | +@@ -308,18 +365,35 @@ int SoftapController::setSoftap(int argc, char *argv[]) { |
| 175 | + ssid = (char *)"AndroidAP"; |
| 176 | + } |
| 177 | + |
| 178 | +- asprintf(&wbuf, "interface=%s\ndriver=" HOSTAPD_DRIVER_NAME "\nctrl_interface=" |
| 179 | +- "/data/misc/wifi/hostapd\nssid=%s\nchannel=6\n", iface, ssid); |
| 180 | ++ if (argc > 7) { |
| 181 | ++ channel = atoi(argv[7]); |
| 182 | ++ } else { |
| 183 | ++ char value[PROPERTY_VALUE_MAX]; |
| 184 | ++ property_get("wifi.ap.channel", value, "0"); |
| 185 | ++ channel = atoi(value); |
| 186 | ++ } |
| 187 | ++ if (channel == 0) { |
| 188 | ++ channel = AP_DEFAULT_CHANNEL; |
| 189 | ++ LOGV("No valid wifi channel specified, using default"); |
| 190 | ++ } |
| 191 | ++ |
| 192 | ++ asprintf(&wbuf, "interface=%s\ndriver=" HOSTAPD_DRIVER_NAME "\n" |
| 193 | ++ "ctrl_interface=" AP_SOCKET_PATH "\n" |
| 194 | ++ "ssid=%s\nchannel=%d\n", mIface, ssid, channel); |
| 195 | ++ |
| 196 | ++ LOGV("%s", wbuf); |
| 197 | + |
| 198 | + if (argc > 5) { |
| 199 | + if (!strcmp(argv[5], "wpa-psk")) { |
| 200 | + generatePsk(ssid, argv[6], psk_str); |
| 201 | + asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str); |
| 202 | +- } else if (!strcmp(argv[5], "wpa2-psk")) { |
| 203 | ++ } else if (!strncmp(argv[5], "wpa2", 4)) { |
| 204 | + generatePsk(ssid, argv[6], psk_str); |
| 205 | + asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str); |
| 206 | + } else if (!strcmp(argv[5], "open")) { |
| 207 | + asprintf(&fbuf, "%s", wbuf); |
| 208 | ++ } else { |
| 209 | ++ LOGE("Invalid softap security type '%s'!\n", argv[5]); |
| 210 | + } |
| 211 | + } else { |
| 212 | + asprintf(&fbuf, "%s", wbuf); |
| 213 | +@@ -397,7 +471,7 @@ int SoftapController::setSoftap(int argc, char *argv[]) { |
| 214 | + sprintf(&mBuf[i], "END"); |
| 215 | + |
| 216 | + /* system("iwpriv eth0 WL_AP_CFG ASCII_CMD=AP_CFG,SSID=\"AndroidAP\",SEC=\"open\",KEY=12345,CHANNEL=1,PREAMBLE=0,MAX_SCB=8,END"); */ |
| 217 | +- ret = setCommand(iface, "AP_SET_CFG"); |
| 218 | ++ ret = setCommand(mIface, "AP_SET_CFG"); |
| 219 | + if (ret) { |
| 220 | + LOGE("Softap set - failed: %d", ret); |
| 221 | + } |
| 222 | +@@ -435,7 +509,7 @@ int SoftapController::fwReloadSoftap(int argc, char *argv[]) |
| 223 | + char *fwpath; |
| 224 | + |
| 225 | + if (mSock < 0) { |
| 226 | +- LOGE("Softap fwrealod - failed to open socket"); |
| 227 | ++ LOGE("Softap fwreload - failed to open socket"); |
| 228 | + return -1; |
| 229 | + } |
| 230 | + if (argc < 4) { |
| 231 | +-- |
| 232 | +1.7.10.4 |
| 233 | + |
0 commit comments