replace value to macro for HTTP OK
[apps/native/tizen-things-daemon.git] / daemon / src / ttd-deviceid.c
1 /*
2  * Copyright (c) 2018 Samsung Electronics Co., Ltd.
3  *
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
7  *
8  * http://floralicense.org/license/
9  *
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.
15  */
16
17 #include <uuid/uuid.h>
18 #include <glib.h>
19 #include <glib/gprintf.h>
20 #include <string.h>
21
22 #include "ttd-log.h"
23 #include "ttd-deviceid.h"
24 #include "ttd-config.h"
25 #include "ttd-wifimgr.h"
26
27 #define TTD_IDENTIFIER "identifier"
28 #define TTD_IDENTIFIER_DEVICEID "deviceid"
29 #define MAC_BUF_SIZE 20
30 #define SHA256_LEN 32
31 #define MAX_DEVICEID_SIZE 37
32 #define UUID_HYPHEN_COUNT 4
33 #define UUID_STRING_LENGTH (SHA256_LEN + UUID_HYPHEN_COUNT)
34
35 static int __get_mac_addr(char **mac_buf)
36 {
37         char *mac_addr = NULL;
38         char **temp_addr;
39
40         if (ttd_wifimgr_get_mac_address(&mac_addr) < 0) {
41                 _E("Failed to get mac address");
42                 return -1;
43         }
44
45         temp_addr = g_strsplit(mac_addr, ":", 6);
46         g_free(mac_addr);
47
48         *mac_buf = g_strjoinv(NULL, temp_addr);
49         g_strfreev(temp_addr);
50
51         return 0;
52 }
53
54 static char *__convert_str_to_uuid(char *uuid)
55 {
56         char uuid_buf[UUID_STRING_LENGTH + 1] = { '\0', };
57
58         if (!uuid)
59                 return NULL;
60
61         /* We discard character after SHA256_DIGEST_LENGTH according to the SmartThings */
62         int j = 0;
63         for (int i = 0; i < SHA256_LEN && j <= UUID_STRING_LENGTH; i++)
64         {
65                 if (i == 8 || i == 12 || i == 16 || i == 20)
66                         uuid_buf[j++] = '-';
67
68                 uuid_buf[j++] = uuid[i];
69         }
70
71         g_free(uuid);
72
73         return g_strdup(uuid_buf);
74 }
75
76 static char *__generate_deviceid_with_mac(void)
77 {
78         char *mac_addr = NULL;
79         char *hashed_addr = NULL;
80         char *uuid = NULL;
81
82         if (__get_mac_addr(&mac_addr) < 0) {
83                 _E("Failed to get MAC Address to make device ID");
84                 return NULL;
85         }
86
87         hashed_addr = g_compute_checksum_for_string(G_CHECKSUM_SHA256, (const gchar *)mac_addr, -1);
88
89         uuid = __convert_str_to_uuid(hashed_addr);
90
91         return uuid;
92 }
93
94 static char *__generate_deviceid_with_mac_time(void)
95 {
96         uuid_t uuid;
97         char uuid_str[MAX_DEVICEID_SIZE];
98
99         uuid_generate_time_safe(uuid);
100
101         uuid_unparse(uuid, uuid_str);
102         _D("UUID for DeviceID[%s]", uuid_str);
103
104         uuid_clear(uuid);
105
106         return g_strdup(uuid_str);
107 }
108
109 static char *__generate_device_id(deviceid_type_e type)
110 {
111         char *uuid = NULL;
112         switch (type)
113         {
114         case DEVICEID_TYPE_ONLY_MAC:
115                 uuid = __generate_deviceid_with_mac();
116                 break;
117         case DEVICEID_TYPE_MAC_TIME:
118                 uuid = __generate_deviceid_with_mac_time();
119                 break;
120         default:
121                 _E("Something Strange");
122                 return NULL;
123         }
124
125         if (!uuid)
126                 return NULL;
127
128         if (ttd_config_write_string(TTD_IDENTIFIER, TTD_IDENTIFIER_DEVICEID, uuid) < 0) {
129                 _E("Failed to store Device ID");
130                 g_free(uuid);
131                 return NULL;
132         }
133
134         return uuid;
135 }
136
137 char *ttd_deviceid_get_device_id(deviceid_type_e type)
138 {
139         char *d_id = NULL;
140         /*
141          * Get deviceID from any where...as a string
142          */
143         if (ttd_config_read_string(TTD_IDENTIFIER, TTD_IDENTIFIER_DEVICEID, &d_id) < 0) {
144                 _E("Failed to get Device ID");
145                 return NULL;
146         }
147
148         if (!strlen(d_id)) {
149                 g_free(d_id);
150                 return __generate_device_id(type);
151         }
152         else
153                 return d_id;
154 }