#include <errno.h>
-#include <net_connection.h>
-
#include "mesh.h"
#include "mesh-log.h"
#include "mesh-util.h"
#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)
{
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)
{
return res;
}
+#endif
/* Returns interface name in sequence order which is exists */
static bool _check_interface_exists(const char* if_name)
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;
}
/* TODO: Current logic checks only ethernet interface.
This logic should consider wireless interface if can */
int ret = MESHD_ERROR_NONE;
- connection_h handle;
- connection_ethernet_cable_state_e cable_state =
- CONNECTION_ETHERNET_CABLE_DETACHED;
+ cable_state_e cable_state = ETHERNET_CABLE_DETACHED;
if (NULL == external_interface || NULL == state) {
return MESHD_ERROR_INVALID_PARAMETER;
return MESHD_ERROR_INVALID_PARAMETER;
}
- ret = connection_create(&handle);
- if (CONNECTION_ERROR_NONE != ret) {
- MESH_LOGE("Failed to get connection handle");
- return MESHD_ERROR_OPERATION_FAILED;
- }
-
- ret = connection_get_ethernet_cable_state(handle, &cable_state);
- if (CONNECTION_ERROR_NONE != ret) {
- MESH_LOGE("Failed to get ethernet cable state");
- return MESHD_ERROR_OPERATION_FAILED;
+ /* 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 (CONNECTION_ETHERNET_CABLE_ATTACHED == cable_state) {
+ if (ETHERNET_CABLE_ATTACHED == cable_state) {
*state = TRUE;
}