Fix mismatched function call
[platform/core/connectivity/wifi-mesh-manager.git] / src / mesh-interface.c
index 61ba23d..189fff0 100644 (file)
 #define IPV4_MAX_LENGTH   16
 #define BUF_LENGTH        256
 
+#define MESH_DEFAULT_BASE_INTERFACE      "wlan0"
+#define MESH_DEFAULT_MESH_INTERFACE      "mesh0"
+#define MESH_DEFAULT_BRIDGE_INTERFACE    "br0"
+#define MESH_DEFAULT_EXTERNAL_INTERFACE  "eth0"
+#define MESH_DEFAULT_SOFTAP_INTERFACE    "wlan1"
+
+typedef enum {
+       ETHERNET_CABLE_DETACHED = 0,
+       ETHERNET_CABLE_ATTACHED
+} cable_state_e;
+
 int mesh_interface_set(const char *interface, const char* ip_addr,
                mesh_set_interface_type_e type)
 {
@@ -120,6 +131,7 @@ int mesh_interface_set(const char *interface, const char* ip_addr,
        return MESHD_ERROR_NONE;
 }
 
+#if 0
 /* Returns interface name in sequence order which is exists */
 static char* _get_interface_exists_in_seq(const char* prefix)
 {
@@ -169,35 +181,163 @@ static char* _get_interface_not_exists_in_seq(const char* prefix)
 
        return res;
 }
+#endif
+
+/* Returns interface name in sequence order which is exists */
+static bool _check_interface_exists(const char* if_name)
+{
+       int ret;
+       char buf[32];
+       int i = 0;
+       const int IF_INDEX_MAX = 9;
+
+       for (i = 0; i <= IF_INDEX_MAX; i++) {
+               snprintf(buf, sizeof(buf), "/sys/class/net/%s", if_name);
+
+               ret = access(buf, F_OK);
+               if (ret >= 0) {
+                       /* This name is exists. */
+                       return true;
+               }
+       }
+
+       return false;
+}
+
+char* mesh_interface_get_address(const char* if_name)
+{
+       FILE *pf;
+       char buf[32];
+       char *result = NULL;
+       int cnt = 0;
+
+       snprintf(buf, sizeof(buf), "/sys/class/net/%s/address", if_name);
+       pf = fopen(buf, "r");
+       if (NULL != pf) {
+               cnt = fscanf(pf, "%s", buf);
+               MESH_LOGD("Interface[%s] address[%s] %d", if_name, buf, cnt);
+               result = g_strdup(buf);
+
+               fclose(pf);
+       }
+
+       return result;
+}
 
 int mesh_interface_initialize(mesh_interface_s *info)
 {
-       info->bridge_interface = _get_interface_not_exists_in_seq("br");
+       if (NULL == info) {
+               MESH_LOGE("Invalid parameter");
+               return MESHD_ERROR_INVALID_PARAMETER;
+       }
+
+       info->bridge_interface = g_strdup(MESH_DEFAULT_BRIDGE_INTERFACE);
        if (NULL == info->bridge_interface) {
                MESH_LOGE("Failed to get bridge interface !");
                return MESHD_ERROR_OPERATION_FAILED;
        }
 
-       info->base_interface = _get_interface_exists_in_seq("wlan");
+       info->base_interface = g_strdup(MESH_DEFAULT_BASE_INTERFACE);
        if (NULL == info->bridge_interface) {
                MESH_LOGE("Failed to get base interface !");
                return MESHD_ERROR_OPERATION_FAILED;
        }
 
-       info->mesh_interface = _get_interface_not_exists_in_seq("mesh");
+       info->mesh_interface = g_strdup(MESH_DEFAULT_MESH_INTERFACE);
        if (NULL == info->bridge_interface) {
                MESH_LOGE("Failed to get mesh interface !");
                return MESHD_ERROR_OPERATION_FAILED;
        }
 
-       info->softap_interface = g_strdup("wlan1"); /* CHECK: interface changed */
-       info->external_interface = _get_interface_exists_in_seq("eth");
-       info->mesh_id = g_strdup("meshnet");
-       if (NULL == info->bridge_interface) {
-               MESH_LOGE("Failed to get mesh id !");
-               return MESHD_ERROR_OPERATION_FAILED;
+       info->softap_interface = g_strdup(MESH_DEFAULT_SOFTAP_INTERFACE);
+       info->external_interface = g_strdup(MESH_DEFAULT_EXTERNAL_INTERFACE);
+
+       MESH_LOGD("Interface configuration for mesh network :");
+       MESH_LOGD("  Base    : [%s]", info->base_interface);
+       MESH_LOGD("  Mesh    : [%s]", info->mesh_interface);
+       MESH_LOGD("  Bridge  : [%s]", info->bridge_interface);
+       MESH_LOGD("  SoftAP  : [%s]", info->softap_interface);
+       MESH_LOGD("  External: [%s]", info->external_interface);
+
+       return MESHD_ERROR_NONE;
+}
+
+static int _check_ethernet_cable_plugin_status(const char* interface,
+               cable_state_e *status)
+{
+       FILE *fd = NULL;
+       int ret = -1;
+       int rv = 0;
+       char error_buf[256] = {0, };
+       char file_path[256] = {0, };
+
+       snprintf(file_path, 256, "/sys/class/net/%s/carrier", interface);
+
+       if (0 == access(file_path, F_OK)) {
+               fd = fopen(file_path, "r");
+               if (fd == NULL) {
+                       MESH_LOGE("Error! Could not open /sys/class/net/%s/carrier file",
+                                       interface);
+                       return MESHD_ERROR_IO_ERROR;
+               }
+       } else {
+               MESH_LOGE("Error! Could not access /sys/class/net/%s/carrier file",
+                               interface);
+               return MESHD_ERROR_IO_ERROR;
+       }
+
+       errno = 0;
+       rv = fscanf(fd, "%d", &ret);
+       if (rv < 0) {
+               strerror_r(errno, error_buf, 256);
+               MESH_LOGE("Error! Failed to read from file, rv:[%d], error:[%s]",
+                               rv, error_buf);
+               fclose(fd);
+               return MESHD_ERROR_IO_ERROR;
+       }
+
+       if (ret == 1) {
+               MESH_LOGD("/sys/class/net/%s/carrier : [%d]", interface, ret);
+               *status = ETHERNET_CABLE_ATTACHED;
+       } else if (ret == 0) {
+               MESH_LOGD("/sys/class/net/%s/carrier : [%d]", interface, ret);
+               *status = ETHERNET_CABLE_DETACHED;
        }
-       info->mesh_channel = 7;
 
+       fclose(fd);
        return MESHD_ERROR_NONE;
 }
+
+int mesh_interface_check_external_exists(const char* external_interface, bool *state)
+{
+       /* TODO: Current logic checks only ethernet interface.
+                       This logic should consider wireless interface if can */
+       int ret = MESHD_ERROR_NONE;
+       cable_state_e cable_state = ETHERNET_CABLE_DETACHED;
+
+       if (NULL == external_interface || NULL == state) {
+               return MESHD_ERROR_INVALID_PARAMETER;
+       }
+
+       bool ex = _check_interface_exists(external_interface);
+       if (FALSE == ex) {
+               MESH_LOGE("External interface[%s] was not found.", external_interface);
+               return MESHD_ERROR_INVALID_PARAMETER;
+       }
+
+       /* If external interface seems Ethernet, check cable state */
+       if (g_str_has_prefix(external_interface, "eth")) {
+               ret = _check_ethernet_cable_plugin_status(external_interface, &cable_state);
+               if (MESHD_ERROR_NONE != ret) {
+                       MESH_LOGE("Failed to get Ethernet cable state");
+                       return MESHD_ERROR_OPERATION_FAILED;
+               }
+       }
+
+       *state = FALSE;
+       if (ETHERNET_CABLE_ATTACHED == cable_state) {
+               *state = TRUE;
+       }
+
+       return MESHD_ERROR_NONE;
+}
\ No newline at end of file