Cloud communication integration 52/189952/6
authorMichal Kolodziejski <m.kolodziejs@samsung.com>
Mon, 24 Sep 2018 14:21:26 +0000 (16:21 +0200)
committerMichal Kolodziejski <m.kolodziejs@samsung.com>
Thu, 4 Oct 2018 08:22:52 +0000 (10:22 +0200)
Change-Id: Ib8c863c5e903bf55890b79ee3792fec06c700423
Signed-off-by: Michal Kolodziejski <m.kolodziejs@samsung.com>
CMakeLists.txt
inc/cloud/cloud_communication.h [new file with mode: 0644]
src/app.c
src/cloud/cloud_communication.c [new file with mode: 0644]

index 6c5128b..0b5dd3c 100644 (file)
@@ -50,6 +50,7 @@ ADD_EXECUTABLE(${PROJECT_NAME}
        ${PROJECT_ROOT_DIR}/src/cloud/cloud_request.c
        ${PROJECT_ROOT_DIR}/src/cloud/http_request.c
        ${PROJECT_ROOT_DIR}/src/net-util.c
+       ${PROJECT_ROOT_DIR}/src/cloud/cloud_communication.c
 )
 
 TARGET_LINK_LIBRARIES(${PROJECT_NAME} ${pkgs_LDFLAGS} -lm)
diff --git a/inc/cloud/cloud_communication.h b/inc/cloud/cloud_communication.h
new file mode 100644 (file)
index 0000000..44135d1
--- /dev/null
@@ -0,0 +1,37 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * @brief Initializes cloud communication.
+ * @return 0 on success, other value on failure.
+*/
+int cloud_communication_init();
+
+/**
+ * @brief Starts sending POST request with car data to the cloud.
+ * @param[in] interval Interval in seconds between POST requests.
+*/
+void cloud_communication_start(int interval);
+
+/**
+ * @brief Stops sending POST request with car data to the cloud.
+*/
+void cloud_communication_stop();
+
+/**
+ * @brief Finalize all resources used by cloud communication.
+*/
+void cloud_communication_fini();
\ No newline at end of file
index fa9977a..608ad0a 100644 (file)
--- a/src/app.c
+++ b/src/app.c
 #include "message.h"
 #include "connection_manager.h"
 #include "net-util.h"
+#include "config.h"
+#include "cloud/cloud_communication.h"
 
 #define ENABLE_MOTOR 1
 
+#define CONFIG_GRP_CAR "Car"
+#define CONFIG_KEY_ID "Id"
+#define CONFIG_KEY_NAME "Name"
+#define CLOUD_REQUESTS_FREQUENCY 15
+
 enum {
        DIR_STATE_S,
        DIR_STATE_F,
@@ -44,6 +51,9 @@ typedef struct app_data_s {
        guint idle_h;
 } app_data;
 
+static void _initialize_components(app_data *ad);
+static void _initialize_config();
+
 static void service_app_lang_changed(app_event_info_h event_info, void *user_data)
 {
        return;
@@ -199,6 +209,41 @@ static void __conn_state_changed_cb(connection_state_e state,
        return;
 }
 
+static void _initialize_config()
+{
+       config_init();
+       char *id = NULL;
+       char *name = NULL;
+       gboolean modified = false;
+       if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, &id) != 0) {
+               char *uuid = g_uuid_string_random();
+               config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_ID, uuid);
+               g_free(uuid);
+               modified = true;
+       }
+       if (config_get_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, &id) != 0) {
+               config_set_string(CONFIG_GRP_CAR, CONFIG_KEY_NAME, "Passerati");
+               modified = true;
+       }
+       if (modified == true) {
+               config_save();
+       }
+       free(id);
+       free(name);
+}
+
+static void _initialize_components(app_data *ad)
+{
+       receiver_init(RECEIVER_TYPE_UDP);
+       receiver_set_state_changed_cb(RECEIVER_TYPE_UDP, __recv_state_change, ad);
+
+       connection_manager_init();
+       connection_manager_set_state_changed_cb(__conn_state_changed_cb, ad);
+       net_util_init();
+       _initialize_config();
+       cloud_communication_init();
+}
+
 static bool service_app_create(void *data)
 {
        int ret = 0;
@@ -222,12 +267,8 @@ static bool service_app_create(void *data)
        }
 #endif
 
-       receiver_init(RECEIVER_TYPE_UDP);
-       receiver_set_state_changed_cb(RECEIVER_TYPE_UDP, __recv_state_change, ad);
-
-       connection_manager_init();
-       connection_manager_set_state_changed_cb(__conn_state_changed_cb, ad);
-       net_util_init();
+       _initialize_components(ad);
+       cloud_communication_start(CLOUD_REQUESTS_FREQUENCY);
 
        message_queue_new();
 
@@ -256,9 +297,13 @@ static void service_app_terminate(void *data)
 
 
        connection_manager_fini();
-       net_util_fini();
        receiver_fini(RECEIVER_TYPE_UDP);
 
+       cloud_communication_stop();
+       cloud_communication_fini();
+       config_shutdown();
+       net_util_fini();
+
        resource_close_all();
        log_file_close();
 
diff --git a/src/cloud/cloud_communication.c b/src/cloud/cloud_communication.c
new file mode 100644 (file)
index 0000000..59703f3
--- /dev/null
@@ -0,0 +1,189 @@
+/*
+ * Copyright (c) 2018 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Flora License, Version 1.1 (the License);
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://floralicense.org/license/
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an AS IS BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "cloud/cloud_communication.h"
+#include <glib.h>
+#include <wifi-manager.h>
+#include <stdlib.h>
+#include "cloud/car_info.h"
+#include "cloud/cloud_request.h"
+#include "log.h"
+#include "config.h"
+#include "connection_manager.h"
+#include "net-util.h"
+
+typedef struct communication_data_ {
+    gboolean is_initialized;
+    gboolean is_running;
+    car_info_t *car_info;
+    guint source_id;
+} communication_data_t;
+
+static communication_data_t _communication;
+
+static void post_response_cb(request_result_e result, void *user_data);
+static gboolean post_timer_cb(gpointer data);
+static void wifi_changed_cb(const char *ap_mac, const char *ap_ssid, char *ip_addr, void *user_data);
+
+static int set_car_id();
+static int set_car_ip();
+static int set_car_name();
+static int set_ap_mac();
+static int set_ap_ssid();
+
+int cloud_communication_init()
+{
+    retvm_if(_communication.is_initialized, -1, "Cloud communication is already initialized");
+    _communication.car_info = car_info_create();
+
+    if (set_car_id() != 0) {
+        return -1;
+    }
+    if (set_car_ip() != 0) {
+        return -1;
+    }
+    if (set_car_name() != 0) {
+        return -1;
+    }
+    if (set_ap_mac() != 0) {
+        return -1;
+    }
+    if (set_ap_ssid() != 0) {
+        return -1;
+    }
+
+    net_util_set_wifi_connection_changed_cb(wifi_changed_cb, NULL);
+
+    _communication.is_initialized = true;
+    return 0;
+}
+
+void cloud_communication_start(int interval)
+{
+    retm_if(!_communication.is_initialized, "Cloud communication is not initialized");
+    retm_if(_communication.is_running, "Cloud communication is already running");
+
+    _communication.source_id = g_timeout_add_seconds(interval, post_timer_cb, _communication.car_info);
+    _communication.is_running = TRUE;
+}
+
+void cloud_communication_stop()
+{
+    retm_if(!_communication.is_initialized, "Cloud communication is not initialized");
+    retm_if(_communication.is_running, "Cloud communication is already stopped");
+
+    g_source_remove(_communication.source_id);
+    _communication.is_running = FALSE;
+}
+
+void cloud_communication_fini()
+{
+    retm_if(!_communication.is_initialized, "Cloud communication is already finalized");
+
+    cloud_communication_stop();
+    car_info_destroy(_communication.car_info);
+}
+
+static void post_response_cb(request_result_e result, void *user_data)
+{
+    if (result == SUCCESS) {
+        _I("POST SUCCESS");
+    }
+    else {
+        _I("POST FAILURE");
+    }
+}
+
+static gboolean post_timer_cb(gpointer data)
+{
+    retv_if(!data, FALSE);
+    car_info_t *car = (car_info_t *)data;
+    cloud_request_api_racing_post(car, post_response_cb, NULL);
+    return TRUE;
+}
+
+static int set_car_id()
+{
+    char *id = NULL;
+    int ret = 0;
+    ret = config_get_string("Car", "Id", &id);
+    if (ret != 0) {
+        _E("Getting car ID from config failed!");
+        return -1;
+    }
+
+    car_info_set_car_id(_communication.car_info, id);
+    g_free(id);
+    return 0;
+}
+
+static int set_car_ip()
+{
+    char *ip;
+    int ret = net_util_get_ip_addr(&ip);
+    if (ret != 0) {
+        return -1;
+    }
+    car_info_set_car_ip(_communication.car_info, ip);
+    g_free(ip);
+    return 0;
+}
+
+static int set_car_name()
+{
+    char *name;
+    int ret = 0;
+
+    ret = config_get_string("Car", "Name", &name);
+    if (ret != 0) {
+        _E("Getting car name from config failed!");
+        return -1;
+    }
+    car_info_set_car_name(_communication.car_info, name);
+    g_free(name);
+    return 0;
+}
+
+static int set_ap_mac()
+{
+    char *mac;
+    int ret = net_util_get_ap_mac(&mac);
+    if (ret != 0) {
+        return -1;
+    }
+    car_info_set_car_ap_mac(_communication.car_info, mac);
+    g_free(mac);
+    return 0;
+}
+
+static int set_ap_ssid()
+{
+    char *ssid;
+    int ret = net_util_get_ap_ssid(&ssid);
+    if (ret != 0) {
+        return -1;
+    }
+    car_info_set_ap_ssid(_communication.car_info, ssid);
+    g_free(ssid);
+    return 0;
+}
+
+static void wifi_changed_cb(const char *ap_mac, const char *ap_ssid, char *ip_addr, void *user_data)
+{
+    car_info_set_car_ap_mac(_communication.car_info, ap_mac);
+    car_info_set_ap_ssid(_communication.car_info, ap_ssid);
+    car_info_set_car_ip(_communication.car_info, ip_addr);
+}
\ No newline at end of file