WiFi-Hal: Clean up the return codes throughout wifi-hal

Use error codes from "enum wifi_error" throughout the wifi-hal.
Convert the error codes returned by kernel to "enum wifi_error"
codes appropriately before passing them to upper layers.

Change-Id: I84bbaaf6dacde78508deed2db8dbffd9e8b3f974
CRs-Fixed: 2081875
diff --git a/qcwcn/wifi_hal/wificonfig.cpp b/qcwcn/wifi_hal/wificonfig.cpp
index 8a09488..c244c58 100644
--- a/qcwcn/wifi_hal/wificonfig.cpp
+++ b/qcwcn/wifi_hal/wificonfig.cpp
@@ -39,7 +39,7 @@
                                          wifi_interface_handle iface,
                                          int extended_dtim)
 {
-    int ret = 0;
+    wifi_error ret;
     WiFiConfigCommand *wifiConfigCommand;
     struct nlattr *nlData;
     interface_info *ifaceInfo = getIfaceInfo(iface);
@@ -60,7 +60,7 @@
 
     /* Create the NL message. */
     ret = wifiConfigCommand->create();
-    if (ret < 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_extended_dtim_config_set: failed to create NL msg. "
             "Error:%d", ret);
         goto cleanup;
@@ -68,7 +68,7 @@
 
     /* Set the interface Id of the message. */
     ret = wifiConfigCommand->set_iface_id(ifaceInfo->name);
-    if (ret < 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_extended_dtim_config_set: failed to set iface id. "
             "Error:%d", ret);
         goto cleanup;
@@ -82,8 +82,9 @@
         goto cleanup;
     }
 
-    if (wifiConfigCommand->put_u32(
-        QCA_WLAN_VENDOR_ATTR_WIFI_CONFIG_DYNAMIC_DTIM, extended_dtim)) {
+    ret = wifiConfigCommand->put_u32(
+                  QCA_WLAN_VENDOR_ATTR_WIFI_CONFIG_DYNAMIC_DTIM, extended_dtim);
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_extended_dtim_config_set(): failed to put vendor data. "
             "Error:%d", ret);
         goto cleanup;
@@ -93,21 +94,22 @@
     /* Send the NL msg. */
     wifiConfigCommand->waitForRsp(false);
     ret = wifiConfigCommand->requestEvent();
-    if (ret != 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_extended_dtim_config_set(): requestEvent Error:%d", ret);
         goto cleanup;
     }
 
 cleanup:
     delete wifiConfigCommand;
-    return (wifi_error)ret;
+    return ret;
 }
 
 /* Set the country code to driver. */
 wifi_error wifi_set_country_code(wifi_interface_handle iface,
                                  const char* country_code)
 {
-    int requestId, ret = 0;
+    int requestId;
+    wifi_error ret;
     WiFiConfigCommand *wifiConfigCommand;
     wifi_handle wifiHandle = getWifiHandle(iface);
 
@@ -130,12 +132,13 @@
 
     /* Create the NL message with NL80211_CMD_REQ_SET_REG NL cmd. */
     ret = wifiConfigCommand->create_generic(NL80211_CMD_REQ_SET_REG);
-    if (ret < 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_country_code: failed to create NL msg. Error:%d", ret);
         goto cleanup;
     }
 
-    if (wifiConfigCommand->put_string(NL80211_ATTR_REG_ALPHA2, country_code)) {
+    ret = wifiConfigCommand->put_string(NL80211_ATTR_REG_ALPHA2, country_code);
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_country_code: put country code failed. Error:%d", ret);
         goto cleanup;
     }
@@ -143,7 +146,7 @@
     /* Send the NL msg. */
     wifiConfigCommand->waitForRsp(false);
     ret = wifiConfigCommand->requestEvent();
-    if (ret != 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_country_code(): requestEvent Error:%d", ret);
         goto cleanup;
     }
@@ -151,7 +154,7 @@
 
 cleanup:
     delete wifiConfigCommand;
-    return (wifi_error)ret;
+    return ret;
 }
 
 wifi_error wifi_set_beacon_wifi_iface_stats_averaging_factor(
@@ -159,7 +162,7 @@
                                                 wifi_interface_handle iface,
                                                 u16 factor)
 {
-    int ret = 0;
+    wifi_error ret;
     WiFiConfigCommand *wifiConfigCommand;
     struct nlattr *nlData;
     interface_info *ifaceInfo = getIfaceInfo(iface);
@@ -178,7 +181,7 @@
 
     /* Create the NL message. */
     ret = wifiConfigCommand->create();
-    if (ret < 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_beacon_wifi_iface_stats_averaging_factor: failed to "
             "create NL msg. Error:%d", ret);
         goto cleanup;
@@ -186,7 +189,7 @@
 
     /* Set the interface Id of the message. */
     ret = wifiConfigCommand->set_iface_id(ifaceInfo->name);
-    if (ret < 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_beacon_wifi_iface_stats_averaging_factor: failed to "
             "set iface id. Error:%d", ret);
         goto cleanup;
@@ -211,7 +214,7 @@
     /* Send the NL msg. */
     wifiConfigCommand->waitForRsp(false);
     ret = wifiConfigCommand->requestEvent();
-    if (ret != 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_beacon_wifi_iface_stats_averaging_factor(): "
             "requestEvent Error:%d", ret);
         goto cleanup;
@@ -219,14 +222,14 @@
 
 cleanup:
     delete wifiConfigCommand;
-    return (wifi_error)ret;
+    return ret;
 }
 
 wifi_error wifi_set_guard_time(wifi_request_id id,
                                wifi_interface_handle iface,
                                u32 guard_time)
 {
-    int ret = 0;
+    wifi_error ret;
     WiFiConfigCommand *wifiConfigCommand;
     struct nlattr *nlData;
     interface_info *ifaceInfo = getIfaceInfo(iface);
@@ -246,14 +249,14 @@
 
     /* Create the NL message. */
     ret = wifiConfigCommand->create();
-    if (ret < 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_guard_time: failed to create NL msg. Error:%d", ret);
         goto cleanup;
     }
 
     /* Set the interface Id of the message. */
     ret = wifiConfigCommand->set_iface_id(ifaceInfo->name);
-    if (ret < 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_guard_time: failed to set iface id. Error:%d", ret);
         goto cleanup;
     }
@@ -276,14 +279,14 @@
     /* Send the NL msg. */
     wifiConfigCommand->waitForRsp(false);
     ret = wifiConfigCommand->requestEvent();
-    if (ret != 0) {
+    if (ret != WIFI_SUCCESS) {
         ALOGE("wifi_set_guard_time(): requestEvent Error:%d", ret);
         goto cleanup;
     }
 
 cleanup:
     delete wifiConfigCommand;
-    return (wifi_error)ret;
+    return ret;
 }
 
 WiFiConfigCommand::WiFiConfigCommand(wifi_handle handle,
@@ -302,27 +305,26 @@
 }
 
 /* This function implements creation of Vendor command */
-int WiFiConfigCommand::create() {
-    int ret = mMsg.create(NL80211_CMD_VENDOR, 0, 0);
-    if (ret < 0) {
+wifi_error WiFiConfigCommand::create()
+{
+    wifi_error ret = mMsg.create(NL80211_CMD_VENDOR, 0, 0);
+    if (ret != WIFI_SUCCESS)
         return ret;
-    }
 
     /* Insert the oui in the msg */
     ret = mMsg.put_u32(NL80211_ATTR_VENDOR_ID, mVendor_id);
-    if (ret < 0)
-        goto out;
+    if (ret != WIFI_SUCCESS)
+        return ret;
     /* Insert the subcmd in the msg */
     ret = mMsg.put_u32(NL80211_ATTR_VENDOR_SUBCMD, mSubcmd);
-    if (ret < 0)
-        goto out;
-out:
+
     return ret;
 }
 
 /* This function implements creation of generic NL command */
-int WiFiConfigCommand::create_generic(u8 cmdId) {
-    int ret = mMsg.create(cmdId, 0, 0);
+wifi_error WiFiConfigCommand::create_generic(u8 cmdId)
+{
+    wifi_error ret = mMsg.create(cmdId, 0, 0);
     return ret;
 }
 
@@ -372,43 +374,49 @@
  * We don't wait for any response back in case of wificonfig,
  * thus no wait for condition.
  */
-int WiFiConfigCommand::requestEvent()
+wifi_error WiFiConfigCommand::requestEvent()
 {
-    int res = -1;
+    int status;
+    wifi_error res = WIFI_SUCCESS;
     struct nl_cb *cb;
 
     cb = nl_cb_alloc(NL_CB_DEFAULT);
     if (!cb) {
         ALOGE("%s: Callback allocation failed",__FUNCTION__);
-        res = -1;
+        res = WIFI_ERROR_OUT_OF_MEMORY;
         goto out;
     }
 
-    res = nl_send_auto_complete(mInfo->cmd_sock, mMsg.getMessage());
-    if (res < 0)
+    status = nl_send_auto_complete(mInfo->cmd_sock, mMsg.getMessage());
+    if (status < 0) {
+        res = mapKernelErrortoWifiHalError(status);
         goto out;
-    res = 1;
+    }
+    status = 1;
 
-    nl_cb_err(cb, NL_CB_CUSTOM, error_handler_wifi_config, &res);
+    nl_cb_err(cb, NL_CB_CUSTOM, error_handler_wifi_config, &status);
     nl_cb_set(cb, NL_CB_FINISH, NL_CB_CUSTOM, finish_handler_wifi_config,
-        &res);
-    nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler_wifi_config, &res);
+        &status);
+    nl_cb_set(cb, NL_CB_ACK, NL_CB_CUSTOM, ack_handler_wifi_config, &status);
 
     /* Err is populated as part of finish_handler. */
-    while (res > 0){
+    while (status > 0) {
          nl_recvmsgs(mInfo->cmd_sock, cb);
     }
 
-    /* Only wait for the asynchronous event if HDD returns success, res=0 */
-    if (!res && (mWaitforRsp == true)) {
+    if (status < 0) {
+        res = mapKernelErrortoWifiHalError(status);
+        goto out;
+    }
+
+    if (mWaitforRsp == true) {
         struct timespec abstime;
         abstime.tv_sec = 4;
         abstime.tv_nsec = 0;
         res = mCondition.wait(abstime);
-        if (res == ETIMEDOUT)
-        {
+        if (res == WIFI_ERROR_TIMED_OUT)
             ALOGE("%s: Time out happened.", __FUNCTION__);
-        }
+
         ALOGV("%s: Command invoked return value:%d, mWaitForRsp=%d",
             __FUNCTION__, res, mWaitforRsp);
     }