Add uuid module 38/181238/8
authorjunkyu han <junkyu.han@samsung.com>
Mon, 11 Jun 2018 07:28:30 +0000 (16:28 +0900)
committerjunkyu han <junkyu.han@samsung.com>
Thu, 14 Jun 2018 09:58:07 +0000 (18:58 +0900)
Change-Id: If0b401c3c88ef071852e4fdd314c48d1c1d937ee

daemon/CMakeLists.txt
daemon/include/ttd-deviceid.h [new file with mode: 0644]
daemon/include/ttd-wifimgr.h [new file with mode: 0644]
daemon/res/ttd.conf
daemon/src/ttd-deviceid.c [new file with mode: 0644]
daemon/src/ttd-wifimgr.c [new file with mode: 0644]
packaging/tizen-things-daemon.spec

index 5088899..fb381a3 100644 (file)
@@ -15,6 +15,8 @@ pkg_check_modules(DAEMON_PKGS REQUIRED
        capi-appfw-app-control
        vconf
        capi-network-softap
+       capi-network-wifi-manager
+       uuid
 )
 
 FOREACH (flag ${DAEMON_PKGS_CFLAGS})
diff --git a/daemon/include/ttd-deviceid.h b/daemon/include/ttd-deviceid.h
new file mode 100644 (file)
index 0000000..8af8aaa
--- /dev/null
@@ -0,0 +1,22 @@
+/*
+ * 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.
+ */
+
+typedef enum {
+       DEVICEID_TYPE_ONLY_MAC = 0,
+       DEVICEID_TYPE_MAC_TIME,
+} deviceid_type_e;
+
+char *ttd_deviceid_get_device_id(deviceid_type_e type);
diff --git a/daemon/include/ttd-wifimgr.h b/daemon/include/ttd-wifimgr.h
new file mode 100644 (file)
index 0000000..04176ae
--- /dev/null
@@ -0,0 +1,17 @@
+/*
+ * 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.
+ */
+
+int ttd_wifimgr_get_mac_address(char **mac_addr);
index f2c3868..4bcbdd1 100644 (file)
@@ -4,3 +4,5 @@ server=http://apitest.showiot.xyz/api/cmd?&target=test-page-device&owner=test-pa
 appid=
 [setting]
 network_disconnected_limit=600
+[identifier]
+deviceid=
diff --git a/daemon/src/ttd-deviceid.c b/daemon/src/ttd-deviceid.c
new file mode 100644 (file)
index 0000000..c9b4d6a
--- /dev/null
@@ -0,0 +1,153 @@
+/*
+ * 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 <uuid/uuid.h>
+#include <glib.h>
+#include <glib/gprintf.h>
+
+#include "ttd-log.h"
+#include "ttd-deviceid.h"
+#include "ttd-config.h"
+#include "ttd-wifimgr.h"
+
+#define TTD_IDENTIFIER "identifier"
+#define TTD_IDENTIFIER_DEVICEID "deviceid"
+#define MAC_BUF_SIZE 20
+#define SHA256_LEN 32
+#define MAX_DEVICEID_SIZE 37
+#define UUID_HYPHEN_COUNT 4
+#define UUID_STRING_LENGTH (SHA256_LEN + UUID_HYPHEN_COUNT)
+
+static int __get_mac_addr(char **mac_buf)
+{
+       char *mac_addr = NULL;
+       char **temp_addr;
+
+       if (ttd_wifimgr_get_mac_address(&mac_addr) < 0) {
+               _E("Failed to get mac address");
+               return -1;
+       }
+
+       temp_addr = g_strsplit(mac_addr, ":", 6);
+       g_free(mac_addr);
+
+       *mac_buf = g_strjoinv(NULL, temp_addr);
+       g_strfreev(temp_addr);
+
+       return 0;
+}
+
+static char *__convert_str_to_uuid(char *uuid)
+{
+       char uuid_buf[UUID_STRING_LENGTH + 1] = { '\0', };
+
+       if (!uuid)
+               return NULL;
+
+       /* We discard character after SHA256_DIGEST_LENGTH according to the SmartThings */
+       int j = 0;
+       for (int i = 0; i < SHA256_LEN && j <= UUID_STRING_LENGTH; i++)
+       {
+               if (i == 8 || i == 12 || i == 16 || i == 20)
+                       uuid_buf[j++] = '-';
+
+               uuid_buf[j++] = uuid[i];
+       }
+
+       g_free(uuid);
+
+       return g_strdup(uuid_buf);
+}
+
+static char *__generate_deviceid_with_mac(void)
+{
+       char *mac_addr = NULL;
+       char *hashed_addr = NULL;
+       char *uuid = NULL;
+
+       if (__get_mac_addr(&mac_addr) < 0) {
+               _E("Failed to get MAC Address to make device ID");
+               return NULL;
+       }
+
+       hashed_addr = g_compute_checksum_for_string(G_CHECKSUM_SHA256, (const gchar *)mac_addr, -1);
+
+       uuid = __convert_str_to_uuid(hashed_addr);
+
+       return uuid;
+}
+
+static char *__generate_deviceid_with_mac_time(void)
+{
+       uuid_t uuid;
+       char uuid_str[MAX_DEVICEID_SIZE];
+
+       uuid_generate_time_safe(uuid);
+
+       uuid_unparse(uuid, uuid_str);
+       _D("UUID for DeviceID[%s]", uuid_str);
+
+       uuid_clear(uuid);
+
+       return g_strdup(uuid_str);
+}
+
+static char *__generate_device_id(deviceid_type_e type)
+{
+       char *uuid = NULL;
+       switch (type)
+       {
+       case DEVICEID_TYPE_ONLY_MAC:
+               uuid = __generate_deviceid_with_mac();
+               break;
+       case DEVICEID_TYPE_MAC_TIME:
+               uuid = __generate_deviceid_with_mac_time();
+               break;
+       default:
+               _E("Something Strange");
+               return NULL;
+       }
+
+       if (!uuid)
+               return NULL;
+
+       if (ttd_config_write_string(TTD_IDENTIFIER, TTD_IDENTIFIER_DEVICEID, uuid) < 0) {
+               _E("Failed to store Device ID");
+               g_free(uuid);
+               return NULL;
+       }
+
+       return uuid;
+}
+
+char *ttd_deviceid_get_device_id(deviceid_type_e type)
+{
+       char *d_id = NULL;
+       /*
+        * Get deviceID from any where...as a string
+        */
+       if (ttd_config_read_string(TTD_IDENTIFIER, TTD_IDENTIFIER_DEVICEID, &d_id) < 0) {
+               _E("Failed to get Device ID");
+               return NULL;
+       }
+
+       if (!strlen(d_id)) {
+               g_free(d_id);
+               return __generate_device_id(type);
+       }
+       else
+               return d_id;
+}
diff --git a/daemon/src/ttd-wifimgr.c b/daemon/src/ttd-wifimgr.c
new file mode 100644 (file)
index 0000000..5e33f54
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * 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 <wifi-manager.h>
+
+#include "ttd-log.h"
+#include "ttd-wifimgr.h"
+
+wifi_manager_h wifi_h;
+
+int ttd_wifimgr_init(void)
+{
+       if (wifi_manager_initialize(&wifi_h) != WIFI_MANAGER_ERROR_NONE) {
+               _E("Failed to initialize wifi manager");
+               return -1;
+       }
+
+       return 0;
+}
+
+void ttd_wifimgr_fini(void)
+{
+       if (wifi_h) {
+               wifi_manager_deinitialize(wifi_h);
+               wifi_h = 0;
+       }
+}
+
+int ttd_wifimgr_get_mac_address(char **mac_addr)
+{
+       char *addr = NULL;
+
+       if (!wifi_h) {
+               if (ttd_wifimgr_init() < 0)
+                       return -1;
+       }
+
+       if (wifi_manager_get_mac_address(wifi_h, &addr) != WIFI_MANAGER_ERROR_NONE) {
+               _E("Failed to get MAC Address from WIFI manager");
+               return -1;
+       }
+
+       *mac_addr = addr;
+
+       return 0;
+}
index 9eb8068..b5efd74 100644 (file)
@@ -21,7 +21,9 @@ BuildRequires:  pkgconfig(openssl)
 BuildRequires:  pkgconfig(capi-appfw-app-control)
 BuildRequires:  pkgconfig(capi-appfw-app-common)
 BuildRequires:  pkgconfig(vconf)
+BuildRequires:  pkgconfig(uuid)
 BuildRequires:  pkgconfig(capi-network-softap)
+BuildRequires:  pkgconfig(capi-network-wifi-manager)
 
 %description
 Tizen Things daemon