--- /dev/null
+/*
+ * 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);
--- /dev/null
+/*
+ * 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);
--- /dev/null
+/*
+ * 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;
+}
--- /dev/null
+/*
+ * 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;
+}