A rough cleanup of netd/softap
- Added error code propagation
- Added more elaborate error messages
- Added stricter parameter checking in softap fwreload
- Cleaned up class signatures
- Removed a non-working default AP in setSoftap
- Removed obsolete functions:
- int addParam(int pos, const char *cmd, const char *arg);
- int setCommand(char *iface, const char *fname, unsigned buflen=0);
- Removed unused "softap clients" functionality
- Removed unused hostapd communication socket
Change-Id: Iad05069f6547f4b17481991018707d5f9db2500a
Signed-off-by: Alexander Levitskiy <sanek@google.com>
diff --git a/SoftapController.cpp b/SoftapController.cpp
index 5db8742..83a2b60 100644
--- a/SoftapController.cpp
+++ b/SoftapController.cpp
@@ -38,129 +38,101 @@
#include <netutils/ifc.h>
#include <private/android_filesystem_config.h>
#include "wifi.h"
+#include "ResponseCode.h"
#include "SoftapController.h"
static const char HOSTAPD_CONF_FILE[] = "/data/misc/wifi/hostapd.conf";
+static const char HOSTAPD_BIN_FILE[] = "/system/bin/hostapd";
-SoftapController::SoftapController() {
- mPid = 0;
- mSock = socket(AF_INET, SOCK_DGRAM, 0);
- if (mSock < 0)
- ALOGE("Failed to open socket");
-}
+SoftapController::SoftapController()
+ : mPid(0) {}
SoftapController::~SoftapController() {
- if (mSock >= 0)
- close(mSock);
}
int SoftapController::startSoftap() {
pid_t pid = 1;
- int ret = 0;
if (mPid) {
- ALOGE("Softap already started");
- return 0;
- }
- if (mSock < 0) {
- ALOGE("Softap startap - failed to open socket");
- return -1;
+ ALOGE("SoftAP is already running");
+ return ResponseCode::SoftapStatusResult;
}
if ((pid = fork()) < 0) {
ALOGE("fork failed (%s)", strerror(errno));
- return -1;
+ return ResponseCode::ServiceStartFailed;
}
if (!pid) {
ensure_entropy_file_exists();
- if (execl("/system/bin/hostapd", "/system/bin/hostapd",
+ if (execl(HOSTAPD_BIN_FILE, HOSTAPD_BIN_FILE,
"-e", WIFI_ENTROPY_FILE,
HOSTAPD_CONF_FILE, (char *) NULL)) {
ALOGE("execl failed (%s)", strerror(errno));
}
- ALOGE("Should never get here!");
- return -1;
+ ALOGE("SoftAP failed to start");
+ return ResponseCode::ServiceStartFailed;
} else {
mPid = pid;
- ALOGD("Softap startap - Ok");
+ ALOGD("SoftAP started successfully");
usleep(AP_BSS_START_DELAY);
}
- return ret;
-
+ return ResponseCode::SoftapStatusResult;
}
int SoftapController::stopSoftap() {
if (mPid == 0) {
- ALOGE("Softap already stopped");
- return 0;
+ ALOGE("SoftAP is not running");
+ return ResponseCode::SoftapStatusResult;
}
- ALOGD("Stopping Softap service");
+ ALOGD("Stopping the SoftAP service...");
kill(mPid, SIGTERM);
waitpid(mPid, NULL, 0);
- if (mSock < 0) {
- ALOGE("Softap stopap - failed to open socket");
- return -1;
- }
mPid = 0;
- ALOGD("Softap service stopped");
+ ALOGD("SoftAP stopped successfully");
usleep(AP_BSS_STOP_DELAY);
- return 0;
+ return ResponseCode::SoftapStatusResult;
}
bool SoftapController::isSoftapStarted() {
- return (mPid != 0 ? true : false);
+ return (mPid != 0);
}
/*
* Arguments:
- * argv[2] - wlan interface
- * argv[3] - SSID
- * argv[4] - Security
- * argv[5] - Key
- * argv[6] - Channel
- * argv[7] - Preamble
- * argv[8] - Max SCB
+ * argv[2] - wlan interface
+ * argv[3] - SSID
+ * argv[4] - Security
+ * argv[5] - Key
*/
int SoftapController::setSoftap(int argc, char *argv[]) {
char psk_str[2*SHA256_DIGEST_LENGTH+1];
- int ret = 0, i = 0, fd;
- char *ssid, *iface;
+ int ret = ResponseCode::SoftapStatusResult;
+ int i = 0;
+ int fd;
- if (mSock < 0) {
- ALOGE("Softap set - failed to open socket");
- return -1;
- }
if (argc < 4) {
- ALOGE("Softap set - missing arguments");
- return -1;
+ ALOGE("Softap set is missing arguments. Please use: softap <wlan iface> <SSID> <wpa2?-psk|open> <passphrase>");
+ return ResponseCode::CommandSyntaxError;
}
- iface = argv[2];
-
char *wbuf = NULL;
char *fbuf = NULL;
- if (argc > 3) {
- ssid = argv[3];
- } else {
- ssid = (char *)"AndroidAP";
- }
-
asprintf(&wbuf, "interface=%s\ndriver=nl80211\nctrl_interface="
"/data/misc/wifi/hostapd\nssid=%s\nchannel=6\nieee80211n=1\n",
- iface, ssid);
+ argv[2], argv[3]);
if (argc > 4) {
if (!strcmp(argv[4], "wpa-psk")) {
- generatePsk(ssid, argv[5], psk_str);
+ generatePsk(argv[3], argv[5], psk_str);
asprintf(&fbuf, "%swpa=1\nwpa_pairwise=TKIP CCMP\nwpa_psk=%s\n", wbuf, psk_str);
} else if (!strcmp(argv[4], "wpa2-psk")) {
- generatePsk(ssid, argv[5], psk_str);
+ generatePsk(argv[3], argv[5], psk_str);
asprintf(&fbuf, "%swpa=2\nrsn_pairwise=CCMP\nwpa_psk=%s\n", wbuf, psk_str);
} else if (!strcmp(argv[4], "open")) {
asprintf(&fbuf, "%s", wbuf);
@@ -174,11 +146,11 @@
ALOGE("Cannot update \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
free(wbuf);
free(fbuf);
- return -1;
+ return ResponseCode::OperationFailed;
}
if (write(fd, fbuf, strlen(fbuf)) < 0) {
ALOGE("Cannot write to \"%s\": %s", HOSTAPD_CONF_FILE, strerror(errno));
- ret = -1;
+ ret = ResponseCode::OperationFailed;
}
free(wbuf);
free(fbuf);
@@ -189,7 +161,7 @@
HOSTAPD_CONF_FILE, strerror(errno));
close(fd);
unlink(HOSTAPD_CONF_FILE);
- return -1;
+ return ResponseCode::OperationFailed;
}
if (fchown(fd, AID_SYSTEM, AID_WIFI) < 0) {
@@ -197,13 +169,47 @@
HOSTAPD_CONF_FILE, AID_WIFI, strerror(errno));
close(fd);
unlink(HOSTAPD_CONF_FILE);
- return -1;
+ return ResponseCode::OperationFailed;
}
close(fd);
return ret;
}
+/*
+ * Arguments:
+ * argv[2] - interface name
+ * argv[3] - AP or P2P or STA
+ */
+int SoftapController::fwReloadSoftap(int argc, char *argv[])
+{
+ int i = 0;
+ char *fwpath = NULL;
+
+ if (argc < 4) {
+ ALOGE("SoftAP fwreload is missing arguments. Please use: softap <wlan iface> <AP|P2P|STA>");
+ return ResponseCode::CommandSyntaxError;
+ }
+
+ if (strcmp(argv[3], "AP") == 0) {
+ fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
+ } else if (strcmp(argv[3], "P2P") == 0) {
+ fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
+ } else if (strcmp(argv[3], "STA") == 0) {
+ fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
+ }
+ if (!fwpath)
+ return ResponseCode::CommandParameterError;
+ if (wifi_change_fw_path((const char *)fwpath)) {
+ ALOGE("Softap fwReload failed");
+ return ResponseCode::OperationFailed;
+ }
+ else {
+ ALOGD("Softap fwReload - Ok");
+ }
+ return ResponseCode::SoftapStatusResult;
+}
+
void SoftapController::generatePsk(char *ssid, char *passphrase, char *psk_str) {
unsigned char psk[SHA256_DIGEST_LENGTH];
int j;
@@ -212,55 +218,6 @@
reinterpret_cast<const unsigned char *>(ssid), strlen(ssid),
4096, SHA256_DIGEST_LENGTH, psk);
for (j=0; j < SHA256_DIGEST_LENGTH; j++) {
- sprintf(&psk_str[j<<1], "%02x", psk[j]);
+ sprintf(&psk_str[j*2], "%02x", psk[j]);
}
- psk_str[j<<1] = '\0';
-}
-
-
-/*
- * Arguments:
- * argv[2] - interface name
- * argv[3] - AP or STA
- */
-int SoftapController::fwReloadSoftap(int argc, char *argv[])
-{
- int ret, i = 0;
- char *iface;
- char *fwpath;
-
- if (mSock < 0) {
- ALOGE("Softap fwrealod - failed to open socket");
- return -1;
- }
- if (argc < 4) {
- ALOGE("Softap fwreload - missing arguments");
- return -1;
- }
-
- iface = argv[2];
-
- if (strcmp(argv[3], "AP") == 0) {
- fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_AP);
- } else if (strcmp(argv[3], "P2P") == 0) {
- fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_P2P);
- } else {
- fwpath = (char *)wifi_get_fw_path(WIFI_GET_FW_PATH_STA);
- }
- if (!fwpath)
- return -1;
- ret = wifi_change_fw_path((const char *)fwpath);
- if (ret) {
- ALOGE("Softap fwReload - failed: %d", ret);
- }
- else {
- ALOGD("Softap fwReload - Ok");
- }
- return ret;
-}
-
-int SoftapController::clientsSoftap(char **retbuf)
-{
- /* TODO: implement over hostapd */
- return 0;
}