Change for connection state support
authorJiwan Kim <ji-wan.kim@samsung.com>
Thu, 8 Jun 2017 07:13:08 +0000 (16:13 +0900)
committersaerome.kim <saerome.kim@samsung.com>
Mon, 17 Jul 2017 02:09:10 +0000 (11:09 +0900)
- Remove MESH_JOIN(LEFT)_NETWORK_EVENT
- Add MESH_CONNECTION_STATE_EVENT
- Add mesh_network_get_connection_state()
- Add connection state into network information

include/mesh.h
include/mesh_private.h
src/mesh.c
src/mesh_dbus.c
test/common.h
test/main.c
test/mesh_network.c

index 0d6d251..72e11f5 100644 (file)
@@ -88,13 +88,15 @@ typedef enum {
 #define MAX_BSSID_LEN 18
 
 /**
- * @brief Enumeration for the state of the Mesh network.
+ * @brief Enumeration for the connection state of the Mesh network.
  * @since_tizen 4.0
  */
 typedef enum {
-       MESH_STATE_DEACTIVATED = 0, /**< Mesh network is Deactivated */
-       MESH_STATE_ACTIVATED = 1, /**< Mesh network is activated */
-} mesh_state_e;
+       MESH_CONNECTION_STATE_DISCONNECTED = 0, /**< Mesh network is disconnected */
+       MESH_CONNECTION_STATE_ASSOCIATION, /**< Service is trying to establish low-level network connection to the mesh network. */
+       MESH_CONNECTION_STATE_CONFIGURATION, /**< Service is trying to retrieve/configure IP settings. */
+       MESH_CONNECTION_STATE_CONNECTED /**< Mesh network is connected */
+} mesh_connection_state_e;
 
 /**
  * @brief The events for mesh_event_cb
@@ -104,11 +106,9 @@ typedef enum {
 typedef enum {
        MESH_MESH_ENABLED_EVENT = 0x00, /**< This event is received after enabling mesh network */
        MESH_SCAN_DONE_EVENT = 0x01, /**< This event comes after scanning operation completed */
-       MESH_JOIN_NETWORK_EVENT = 0x02, /**< This event comes after joining a mesh network */
-       MESH_LEFT_NETWORK_EVENT = 0x03, /**< This event comes after left the current mesh network */
-       MESH_STATION_JOIN_EVENT = 0x04, /**< This event takes place when new station joined */
-       MESH_STATION_LEFT_EVENT = 0x05, /**< This event takes place when existing station left */
-       MESH_CONNECTED_EVENT = 0x06, /**< This event comes after all network connection sequence finished */
+       MESH_CONNECTION_STATE_EVENT = 0x02, /**< This event takes place when mesh network connection state is changed */
+       MESH_STATION_JOIN_EVENT = 0x03, /**< This event takes place when new station joined */
+       MESH_STATION_LEFT_EVENT = 0x04, /**< This event takes place when existing station left */
 } mesh_event_e;
 
 /**
@@ -132,14 +132,17 @@ typedef struct {
 } mesh_other_station_event_s;
 
 /**
- * @brief The structure type for the MESH_CONNECTED_EVENT callback data.
- * @details The result of connecting mesh network.
+ * @brief The structure type for the MESH_CONNECTION_STATE_EVENT callback data.
+ * @details The state of mesh network connection.
  *
  * @since_tizen 4.0
  */
 typedef struct {
-       int result; /**< The result of connecting mesh network. */
-} mesh_connected_event_s;
+       char mesh_id[MAX_MESHID_LEN]; /**< The Mesh ID of the network that generated the event */
+       char bssid[MAX_BSSID_LEN]; /**< The BSSID of the network that generated the event */
+       int channel; /**< The channel of the network that generated the event */
+       mesh_connection_state_e state; /**< The state of mesh network connection. */
+} mesh_connection_state_event_s;
 
 /**
  * @brief The structure type for response data of mesh_event_cb.
@@ -152,7 +155,7 @@ typedef struct {
        union {
                mesh_mesh_enabled_event_s *mesh_enable; /**< The result of enabling mesh system service. */
                mesh_other_station_event_s *sta_info; /**< This event comes from other stations. */
-               mesh_connected_event_s *connected_result; /**< The result of mesh network connection. */
+               mesh_connection_state_event_s *connection_state; /**< The state of mesh network connection. */
        } data;
 } mesh_event_data_s;
 
@@ -465,6 +468,22 @@ int mesh_network_get_data_rate(mesh_network_h network, int *data_rate);
 int mesh_network_set_data_rate(mesh_network_h network, int data_rate);
 
 /**
+ * @brief Gets the connection state.
+ * @details This function is to get the connection state
+ *
+ * @since_tizen 4.0
+ *
+ * @param[in] network The mesh network information handle.
+ * @param[out] state The maximum data rate.
+ *
+ * @return 0 on success, otherwise a negative error value.
+ * @retval #MESH_ERROR_NONE Successful
+ * @retval #MESH_ERROR_INVALID_PARAMETER Invalid parameter
+ *
+ */
+int mesh_network_get_connection_state(mesh_network_h network, mesh_connection_state_e *state);
+
+/**
  * @brief Gets address from mesh peer.
  * @details This function is to return mesh network id
  * @remark The returned string should be freed when no longer needed.
index 09b82e2..d3525f7 100644 (file)
@@ -79,6 +79,7 @@ struct mesh_network_s {
        int rssi; /**< RSSI */
        int data_rate; /**< Data rate */
        int security; /**< Security type */
+       mesh_connection_state_e state; /**< Connection state */
 };
 
 struct mesh_station_info_s {
index a96ad6d..2670ef2 100644 (file)
@@ -269,6 +269,23 @@ EXPORT_API int mesh_network_set_data_rate(mesh_network_h network, int data_rate)
        return MESH_ERROR_NONE;
 }
 
+EXPORT_API int mesh_network_get_connection_state(mesh_network_h network,
+               mesh_connection_state_e *state)
+{
+       struct mesh_network_s *net = (struct mesh_network_s *)network;
+
+       CHECK_FEATURE_SUPPORTED(MESH_FEATURE);
+
+       if (network == NULL || state == NULL) {
+               LOGE("Invalid parameter"); //LCOV_EXCL_LINE
+               return MESH_ERROR_INVALID_PARAMETER; //LCOV_EXCL_LINE
+       }
+
+       *state = net->state;
+
+       return MESH_ERROR_NONE;
+}
+
 EXPORT_API int mesh_peer_get_address(mesh_peer_h peer, char **address)
 {
        struct mesh_peer_s *peer_info = (struct mesh_peer_s *)peer;
index 9792e50..31585bf 100644 (file)
@@ -208,11 +208,14 @@ int _mesh_get_scan_result(mesh_h handle)
                                else if (strcasecmp(key, "channel") == 0)  {
                                        network_info->channel = g_variant_get_uint32(val);
                                        LOGD("  Channel [%d]", network_info->channel);
-
-                                       /* Last element */
-                                       _mesh_append_network(network_info);
+                               }
+                               else if (strcasecmp(key, "state") == 0)  {
+                                       network_info->state = g_variant_get_uint32(val);
+                                       LOGD("  State [%d]", network_info->state);
                                }
                        }
+                       _mesh_append_network(network_info);
+
                        g_variant_iter_free(iter_row);
                }
                g_variant_iter_free(iter);
@@ -303,22 +306,25 @@ static void _mesh_signal_handler(GDBusConnection *connection,
                free(ev.data.mesh_enable);
        } else if (0 == g_strcmp0(signal_name, "scan_done")) {
                h->event_handler(MESH_SCAN_DONE_EVENT, NULL);
-       } else if (0 == g_strcmp0(signal_name, "joined_network")) {
-               h->event_handler(MESH_JOIN_NETWORK_EVENT, NULL);
-       } else if (0 == g_strcmp0(signal_name, "left_network")) {
-               h->event_handler(MESH_LEFT_NETWORK_EVENT, NULL);
-       } else if (0 == g_strcmp0(signal_name, "mesh_connected")) {
-               int result = MESH_ERROR_NONE;
+       } else if (0 == g_strcmp0(signal_name, "connection_state")) {
+               char *mesh_id = NULL;
+               char *bssid = NULL;
+               int channel = 0;
+               int state = 0;
                mesh_event_data_s ev;
 
-               g_variant_get(parameters, "(i)", &result);
+               g_variant_get(parameters, "(ssii)", &mesh_id, &bssid, &channel, &state);
 
-               ev.data.connected_result = calloc(1, sizeof(mesh_connected_event_s));
-               RETM_IF(NULL == ev.data.connected_result, "Failed to memory allocation !");
-               ev.data.connected_result->result = __convert_service_error_type(result);
+               ev.data.connection_state = calloc(1, sizeof(mesh_connection_state_event_s));
+               RETM_IF(NULL == ev.data.connection_state, "Failed to memory allocation !");
 
-               h->event_handler(MESH_CONNECTED_EVENT, &ev);
-               free(ev.data.connected_result);
+               g_snprintf(ev.data.connection_state->mesh_id, MAX_MESHID_LEN, "%s", mesh_id);
+               g_snprintf(ev.data.connection_state->bssid, MAX_BSSID_LEN, "%s", bssid);
+               ev.data.connection_state->channel = channel;
+               ev.data.connection_state->state = (mesh_connection_state_e)state;
+
+               h->event_handler(MESH_CONNECTION_STATE_EVENT, &ev);
+               free(ev.data.connection_state);
        } else if (0 == g_strcmp0(signal_name, "sta_joined")) {
                char *bssid = NULL;
                mesh_event_data_s ev;
@@ -345,6 +351,7 @@ static void _mesh_subscribe_event(mesh_h handle)
        unsigned int id;
        struct mesh_handle *h = handle;
 
+       /* mesh_enabled */
        id = g_dbus_connection_signal_subscribe((GDBusConnection *)h->dbus_connection,
                        NULL, MESH_SERVER_NAME, "mesh_enabled", MESH_OBJECT_PATH, NULL,
                        G_DBUS_CALL_FLAGS_NONE, _mesh_signal_handler, h, NULL);
@@ -355,6 +362,7 @@ static void _mesh_subscribe_event(mesh_h handle)
        h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id));
        LOGD("subscribed for mesh_enabled signal %d", id);
 
+       /* scan_done */
        id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, MESH_SERVER_NAME,
                        "scan_done", MESH_OBJECT_PATH, NULL,
                        G_DBUS_CALL_FLAGS_NONE, _mesh_signal_handler, h, NULL);
@@ -365,36 +373,18 @@ static void _mesh_subscribe_event(mesh_h handle)
        h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id));
        LOGD("subscribed for scan_done signal %d", id);
 
+       /* connection_state */
        id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, MESH_SERVER_NAME,
-                       "joined_network", MESH_OBJECT_PATH, NULL,
-                       G_DBUS_CALL_FLAGS_NONE, _mesh_signal_handler, h, NULL);
-       if (0 == id) {
-               LOGE("g_dbus_connection_signal_subscribe(joined_network) Fail(%d)", errno);
-               return;
-       }
-       h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id));
-       LOGD("subscribed for joined_network signal %d", id);
-
-       id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, MESH_SERVER_NAME,
-                       "left_network", MESH_OBJECT_PATH, NULL,
-                       G_DBUS_CALL_FLAGS_NONE, _mesh_signal_handler, h, NULL);
-       if (0 == id) {
-               LOGE("g_dbus_connection_signal_subscribe(left_network) Fail(%d)", errno);
-               return;
-       }
-       h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id));
-       LOGD("subscribed for left_network signal %d", id);
-
-       id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, MESH_SERVER_NAME,
-                       "mesh_connected", MESH_OBJECT_PATH, NULL,
+                       "connection_state", MESH_OBJECT_PATH, NULL,
                        G_DBUS_CALL_FLAGS_NONE, _mesh_signal_handler, h, NULL);
        if (0 == id) {
-               LOGE("g_dbus_connection_signal_subscribe(mesh_connected) Fail(%d)", errno);
+               LOGE("g_dbus_connection_signal_subscribe(connection_state) Fail(%d)", errno);
                return;
        }
        h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id));
-       LOGD("subscribed for mesh_connected signal %d", id);
+       LOGD("subscribed for connection_state signal %d", id);
 
+       /* sta_joined */
        id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, MESH_SERVER_NAME,
                        "sta_joined", MESH_OBJECT_PATH, NULL,
                        G_DBUS_CALL_FLAGS_NONE, _mesh_signal_handler, h, NULL);
@@ -405,6 +395,7 @@ static void _mesh_subscribe_event(mesh_h handle)
        h->dbus_sub_ids = g_list_append(h->dbus_sub_ids, GUINT_TO_POINTER(id));
        LOGD("subscribed for sta_joined signal %d", id);
 
+       /* sta_left */
        id = g_dbus_connection_signal_subscribe(h->dbus_connection, NULL, MESH_SERVER_NAME,
                        "sta_left", MESH_OBJECT_PATH, NULL,
                        G_DBUS_CALL_FLAGS_NONE, _mesh_signal_handler, h, NULL);
@@ -416,6 +407,27 @@ static void _mesh_subscribe_event(mesh_h handle)
        LOGD("subscribed for sta_left signal %d", id);
 }
 
+static void _on_unsubscribe_ids(gpointer data, gpointer user_data)
+{
+       unsigned int id = GPOINTER_TO_UINT(data);
+       struct mesh_handle *h = (struct mesh_handle*)user_data;
+
+       LOGD("[Signal unsubscribe] : %d", id);
+       g_dbus_connection_signal_unsubscribe(
+               (GDBusConnection *)h->dbus_connection, id);
+}
+
+static void _mesh_unsubscribe_event(mesh_h handle)
+{
+       struct mesh_handle *h = handle;
+       RET_IF(NULL == h);
+
+       g_list_foreach(h->dbus_sub_ids, _on_unsubscribe_ids, h);
+
+       g_list_free(h->dbus_sub_ids);
+       h->dbus_sub_ids = NULL;
+}
+
 int _mesh_dbus_start(mesh_h handle)
 {
        struct mesh_handle *h = handle;
@@ -448,6 +460,8 @@ int _mesh_dbus_stop(mesh_h handle)
        if (NULL == h)
                return MESH_ERROR_INVALID_PARAMETER;
 
+       _mesh_unsubscribe_event(handle);
+
        /* Unref all proxies here */
        if (_gproxy_mesh_service) {
                g_object_unref(_gproxy_mesh_service);
@@ -828,9 +842,10 @@ int _mesh_get_joined_mesh_network(mesh_h handle, mesh_network_h* _network)
                char *meshid = NULL;
                char *bssid = NULL;
                int channel = -1;
+               int state = 0;
                int svc_result = 0;
 
-               g_variant_get(variant, "(ssii)", &meshid, &bssid, &channel, &svc_result);
+               g_variant_get(variant, "(ssiii)", &meshid, &bssid, &channel, &state, &svc_result);
                LOGD("get_joined_mesh_network status 0x%x", svc_result);
                result = __convert_service_error_type(svc_result);
 
@@ -841,6 +856,7 @@ int _mesh_get_joined_mesh_network(mesh_h handle, mesh_network_h* _network)
                g_joined_network.rssi = -1;
                g_joined_network.data_rate = 0;
                g_joined_network.security = 0;
+               g_joined_network.state = MESH_CONNECTION_STATE_DISCONNECTED;
 
                if (SERVICE_ERROR_NO_DATA == svc_result) {
                        *_network = NULL;
@@ -860,6 +876,7 @@ int _mesh_get_joined_mesh_network(mesh_h handle, mesh_network_h* _network)
                }
                g_joined_network.channel = channel;
                g_joined_network.rssi = -1;
+               g_joined_network.state = state;
 
                *_network = &g_joined_network;
        } else if (error) {
index a044877..0a91e9f 100644 (file)
@@ -25,6 +25,7 @@ __BEGIN_DECLS
 #define CASE_TO_STR(x) case x: return #x;
 
 const char* mesh_error_to_string(mesh_error_e err);
+const char* _mesh_connection_event_to_string(mesh_connection_state_e e);
 
 __END_DECLS
 
index 06b1be6..7f9de82 100644 (file)
@@ -64,16 +64,27 @@ static const char* _mesh_event_to_string(mesh_event_e e)
        /* CHECK: List all enum values here */
        CASE_TO_STR(MESH_MESH_ENABLED_EVENT)
        CASE_TO_STR(MESH_SCAN_DONE_EVENT)
-       CASE_TO_STR(MESH_JOIN_NETWORK_EVENT)
-       CASE_TO_STR(MESH_LEFT_NETWORK_EVENT)
+       CASE_TO_STR(MESH_CONNECTION_STATE_EVENT)
        CASE_TO_STR(MESH_STATION_JOIN_EVENT)
        CASE_TO_STR(MESH_STATION_LEFT_EVENT)
-       CASE_TO_STR(MESH_CONNECTED_EVENT)
        default :
                return "MESH_EVENT_UNKNOWN";
        }
 }
 
+const char* _mesh_connection_event_to_string(mesh_connection_state_e e)
+{
+       switch (e) {
+       /* CHECK: List all enum values here */
+       CASE_TO_STR(MESH_CONNECTION_STATE_DISCONNECTED)
+       CASE_TO_STR(MESH_CONNECTION_STATE_ASSOCIATION)
+       CASE_TO_STR(MESH_CONNECTION_STATE_CONFIGURATION)
+       CASE_TO_STR(MESH_CONNECTION_STATE_CONNECTED)
+       default :
+               return "Unknown state";
+       }
+}
+
 void event_cb(mesh_event_e event_type, mesh_event_data_s* event)
 {
        msg("");
@@ -86,11 +97,13 @@ void event_cb(mesh_event_e event_type, mesh_event_data_s* event)
                case MESH_SCAN_DONE_EVENT: {
                        msgp("  Mesh Scan Done");
                } break;
-               case MESH_JOIN_NETWORK_EVENT:{
-                       msgp("  Joined Network");
-               } break;
-               case MESH_LEFT_NETWORK_EVENT: {
-                       msgp("  Left Current Network");
+               case MESH_CONNECTION_STATE_EVENT:{
+                       msgp("  Connection state changed [%s]",
+                               _mesh_connection_event_to_string(event->data.connection_state->state));
+                       msgp("  From Mesh ID[%-10s] BSSID[%s] Channel[%d]",
+                               event->data.connection_state->mesh_id,
+                               event->data.connection_state->bssid,
+                               event->data.connection_state->channel);
                } break;
                case MESH_STATION_JOIN_EVENT: {
                        msgp("  New Station Joined = %s", event->data.sta_info->bssid);
@@ -98,10 +111,9 @@ void event_cb(mesh_event_e event_type, mesh_event_data_s* event)
                case MESH_STATION_LEFT_EVENT: {
                        msgp("  A Station Left = %s", event->data.sta_info->bssid);
                } break;
-               case MESH_CONNECTED_EVENT: {
-                       msgp("  Connection sequence finished [%s]",
-                               mesh_error_to_string(event->data.connected_result->result));
-               } break;
+               default:
+                       msgr("  Unexpected event !");
+               break;
        }
 }
 
index 1cc76ec..29bc543 100644 (file)
@@ -238,6 +238,7 @@ static int run_get_joined_mesh_network(MManager *mm, struct menu_data *menu)
        char *_meshid = NULL;
        char *_bssid = NULL;
        int _channel = -1;
+       mesh_connection_state_e _state = MESH_CONNECTION_STATE_DISCONNECTED;
        mesh_network_h network = NULL;
 
        msg("Get Joined Mesh Network Information");
@@ -248,21 +249,24 @@ static int run_get_joined_mesh_network(MManager *mm, struct menu_data *menu)
                        mesh_error_to_string(ret), ret);
                return RET_FAILURE;
        }
-       msg(" - mesh_get_joined_network() ret: [0x%X] [%s]",
-                       ret, mesh_error_to_string(ret));
-       msg("");
 
        if (NULL != network) {
                mesh_network_get_meshid(network, &_meshid);
-               msgp("  Mesh ID = %s", _meshid);
+               msgb("  Mesh ID = %s", _meshid);
                mesh_network_get_bssid(network, &_bssid);
-               msgp("  BSSID   = %s", _bssid);
+               msgb("  BSSID   = %s", _bssid);
                mesh_network_get_channel(network, &_channel);
-               msgp("  Channel = %d", _channel);
+               msgb("  Channel = %d", _channel);
+               mesh_network_get_connection_state(network, &_state);
+               msgb("  State   = %s", _mesh_connection_event_to_string(_state));
 
                if (_meshid) free(_meshid);
                if (_bssid) free(_bssid);
        }
+       msg("");
+       msg(" - mesh_get_joined_network() ret: [0x%X] [%s]",
+                       ret, mesh_error_to_string(ret));
+
        return RET_SUCCESS;
 }