2 * Copyright (c) 2018 Samsung Electronics Co., Ltd.
4 * Licensed under the Flora License, Version 1.1 (the License);
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://floralicense.org/license/
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an AS IS BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 #include <uuid/uuid.h>
19 #include <glib/gprintf.h>
23 #include "ttd-deviceid.h"
24 #include "ttd-config.h"
25 #include "ttd-wifimgr.h"
27 #define TTD_IDENTIFIER "identifier"
28 #define TTD_IDENTIFIER_DEVICEID "deviceid"
29 #define MAC_BUF_SIZE 20
31 #define MAX_DEVICEID_SIZE 37
32 #define UUID_HYPHEN_COUNT 4
33 #define UUID_STRING_LENGTH (SHA256_LEN + UUID_HYPHEN_COUNT)
35 static int __get_mac_addr(char **mac_buf)
37 char *mac_addr = NULL;
40 if (ttd_wifimgr_get_mac_address(&mac_addr) < 0) {
41 _E("Failed to get mac address");
45 temp_addr = g_strsplit(mac_addr, ":", 6);
48 *mac_buf = g_strjoinv(NULL, temp_addr);
49 g_strfreev(temp_addr);
54 static char *__convert_str_to_uuid(char *uuid)
56 char uuid_buf[UUID_STRING_LENGTH + 1] = { '\0', };
61 /* We discard character after SHA256_DIGEST_LENGTH according to the SmartThings */
63 for (int i = 0; i < SHA256_LEN && j <= UUID_STRING_LENGTH; i++)
65 if (i == 8 || i == 12 || i == 16 || i == 20)
68 uuid_buf[j++] = uuid[i];
73 return g_strdup(uuid_buf);
76 static char *__generate_deviceid_with_mac(void)
78 char *mac_addr = NULL;
79 char *hashed_addr = NULL;
82 if (__get_mac_addr(&mac_addr) < 0) {
83 _E("Failed to get MAC Address to make device ID");
87 hashed_addr = g_compute_checksum_for_string(G_CHECKSUM_SHA256, (const gchar *)mac_addr, -1);
89 uuid = __convert_str_to_uuid(hashed_addr);
94 static char *__generate_deviceid_with_mac_time(void)
97 char uuid_str[MAX_DEVICEID_SIZE];
99 uuid_generate_time_safe(uuid);
101 uuid_unparse(uuid, uuid_str);
102 _D("UUID for DeviceID[%s]", uuid_str);
106 return g_strdup(uuid_str);
109 static char *__generate_device_id(deviceid_type_e type)
114 case DEVICEID_TYPE_ONLY_MAC:
115 uuid = __generate_deviceid_with_mac();
117 case DEVICEID_TYPE_MAC_TIME:
118 uuid = __generate_deviceid_with_mac_time();
121 _E("Something Strange");
128 if (ttd_config_write_string(TTD_IDENTIFIER, TTD_IDENTIFIER_DEVICEID, uuid) < 0) {
129 _E("Failed to store Device ID");
137 char *ttd_deviceid_get_device_id(deviceid_type_e type)
141 * Get deviceID from any where...as a string
143 if (ttd_config_read_string(TTD_IDENTIFIER, TTD_IDENTIFIER_DEVICEID, &d_id) < 0) {
144 _E("Failed to get Device ID");
150 return __generate_device_id(type);