Fix for wrong statement
[platform/core/api/nfc.git] / src / nfc_common.c
1 /*
2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
3 *
4 * Licensed under the Apache License, Version 2.0 (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://www.apache.org/licenses/LICENSE-2.0
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 "nfc_common.h"
18
19 _nfc_context_s gdbus_nfc_context;
20
21 /* LCOV_EXCL_START */
22 bool nfc_common_get_login_user(uid_t *uid)
23 {
24         int i, ret;
25         uid_t *uids;
26         int uid_count;
27
28         uid_count = sd_get_uids(&uids);
29
30         if (uid_count <= 0) {
31                 LOGD("sd_get_uids failed [%d]", uid_count);
32                 return false;
33         }
34
35         for (i = 0; i < uid_count ; i++) {
36                 char *state = NULL;
37
38                 ret = sd_uid_get_state(uids[i], &state);
39
40                 if (ret < 0) {
41                         LOGD("sd_uid_get_state failed [%d]", ret);
42                 } else {
43                         if (!strncmp(state, "online", 6)) {
44                                 *uid = uids[i];
45                                 free(state);
46                                 free(uids);
47                                 return true;
48                         }
49                 }
50
51                 free(state);
52         }
53
54         LOGD("not exist login user");
55
56         free(uids);
57         return false;
58 }
59
60 int _iter_func(const aul_app_info *info, void *data)
61 {
62         uid_t uid = 0;
63         int *pid = (int *)data;
64         int status;
65
66         if (nfc_common_get_login_user(&uid) == false) {
67                 LOGD("net_nfc_util_get_login_user is failed");
68                 return 0;
69         }
70
71         status = aul_app_get_status_bypid_for_uid(info->pid, uid);
72
73         LOGD("login user is %d, pid is %d, status is %d", (int)uid, info->pid, status);
74
75         if (status == STATUS_VISIBLE || status == STATUS_FOCUS) {
76                 *pid = info->pid;
77                 return -1;
78         }
79         return 0;
80 }
81
82 pid_t nfc_common_get_focus_app_pid()
83 {
84         int ret;
85         uid_t uid = 0;
86         pid_t pid = 0;
87
88         if (nfc_common_get_login_user(&uid) == false) {
89                 LOGD("nfc_common_get_login_user is failed");
90                 return -1;
91         }
92
93         ret = aul_app_get_all_running_app_info_for_uid(_iter_func, &pid, uid);
94
95         if (ret == AUL_R_OK) {
96                 return pid;
97         } else {
98                 LOGD("aul_app_get_all_running_app_info_for_uid is failed");
99                 return -1;
100         }
101 }
102
103 char * nfc_common_get_bt_address_string(data_h data)
104 {
105         uint8_t *buffer;
106         uint32_t length;
107
108         if (data == NULL)
109                 return NULL;
110
111         buffer = net_nfc_get_data_buffer(data);
112         length = net_nfc_get_data_length(data);
113
114         if (buffer == NULL || length < 6)
115                 return NULL;
116
117         return g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
118                 buffer[0],
119                 buffer[1],
120                 buffer[2],
121                 buffer[3],
122                 buffer[4],
123                 buffer[5]);
124 }
125
126 bool nfc_common_check_app_permission()
127 {
128         pid_t focus_app_pid, current_app_pid;
129
130         focus_app_pid = nfc_common_get_focus_app_pid();
131         current_app_pid = getpgid(getpid());
132
133         LOGD("[check app permission] focus_app_pid [%d], current_app_pid [%d]", focus_app_pid,
134                 current_app_pid);
135
136         return (focus_app_pid == current_app_pid) ? true : false;
137 }
138
139 int nfc_common_convert_error_code(const char *func, int native_error_code)
140 {
141         int error_code = NFC_ERROR_NONE;
142         char *errorstr = NULL;
143
144         switch (native_error_code) {
145         case NET_NFC_OK:
146                 error_code = NFC_ERROR_NONE;
147                 errorstr = "ERROR_NONE";
148                 break;
149
150         case NET_NFC_ALLOC_FAIL:
151                 error_code = NFC_ERROR_OUT_OF_MEMORY;
152                 errorstr = "OUT_OF_MEMORY";
153                 break;
154
155         case NET_NFC_NOT_CONNECTED:
156                 error_code = NFC_ERROR_NO_DEVICE;
157                 errorstr = "NO_DEVICE";
158                 break;
159
160         case NET_NFC_UNKNOWN_ERROR:
161         case NET_NFC_THREAD_CREATE_FAIL:
162         case NET_NFC_IPC_FAIL:
163         case NET_NFC_BUFFER_TOO_SMALL:
164         case NET_NFC_COMMUNICATE_WITH_CONTROLLER_FAILED:
165         case NET_NFC_RF_ERROR:
166         case NET_NFC_NOT_SUPPORTED:
167         case NET_NFC_TAG_READ_FAILED:
168         case NET_NFC_TAG_WRITE_FAILED:
169         case NET_NFC_OPERATION_FAIL:
170         case NET_NFC_INSUFFICIENT_STORAGE:
171         case NET_NFC_NOT_INITIALIZED:
172         case NET_NFC_NOT_REGISTERED:
173         case NET_NFC_NO_DATA_FOUND:
174         case NET_NFC_NOT_ALLOWED_OPERATION:
175                 error_code = NFC_ERROR_OPERATION_FAILED;
176                 errorstr = "OPERATION_FAILED";
177                 break;
178
179         case NET_NFC_SECURITY_FAIL:
180                 error_code = NFC_ERROR_SECURITY_RESTRICTED;
181                 errorstr = "SECURITY_RESTRICTED";
182                 break;
183
184         case NET_NFC_INVALID_STATE:
185                 error_code = NFC_ERROR_ILLEGAL_STATE;
186                 errorstr = "ILLEGAL_STATE";
187                 break;
188
189         case NET_NFC_OUT_OF_BOUND:
190         case NET_NFC_NULL_PARAMETER:
191         case NET_NFC_LLCP_INVALID_SOCKET:
192                 error_code = NFC_ERROR_INVALID_PARAMETER;
193                 errorstr = "INVALID_PARAMETER";
194                 break;
195         case NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE:
196                 error_code = NFC_ERROR_INVALID_RECORD_TYPE;
197                 errorstr = "INVALID_RECORD_TYPE";
198                 break;
199
200         case NET_NFC_ALREADY_INITIALIZED:
201         case NET_NFC_ALREADY_REGISTERED:
202                 error_code = NFC_ERROR_NONE;
203                 errorstr = "ERROR_NONE";
204                 break;
205
206         case NET_NFC_RF_TIMEOUT:
207                 error_code = NFC_ERROR_TIMED_OUT;
208                 errorstr = "TIMED_OUT";
209                 break;
210         case NET_NFC_INVALID_FORMAT:
211         case NET_NFC_NDEF_TYPE_LENGTH_IS_NOT_OK:
212         case NET_NFC_NDEF_ID_LENGTH_IS_NOT_OK:
213         case NET_NFC_NDEF_BUF_END_WITHOUT_ME:
214                 error_code = NFC_ERROR_INVALID_NDEF_MESSAGE;
215                 errorstr = "INVALID_NDEF_MESSAGE";
216                 break;
217         case NET_NFC_NO_NDEF_MESSAGE:
218                 error_code = NFC_ERROR_NO_NDEF_MESSAGE;
219                 errorstr = "NO_NDEF_MESSAGE";
220                 break;
221         case NET_NFC_BUSY:
222                 error_code = NFC_ERROR_DEVICE_BUSY;
223                 errorstr = "DEVICE_BUSY";
224                 break;
225         case NET_NFC_NO_NDEF_SUPPORT:
226                 error_code = NFC_ERROR_NOT_NDEF_FORMAT;
227                 errorstr = "NOT_SUPPORTED";
228                 break;
229         case NET_NFC_PERMISSION_DENIED:
230                 error_code = NFC_ERROR_PERMISSION_DENIED;
231                 errorstr = "PERMISSION_DENIED";
232                 break;
233         case NET_NFC_NOT_ACTIVATED:
234                 error_code = NFC_ERROR_NOT_ACTIVATED;
235                 errorstr = "NOT_ACTIVATED";
236                 break;
237         case NET_NFC_DATA_CONFLICTED:
238                 error_code = NFC_ERROR_DATA_CONFLICTED;
239                 errorstr = "DATA_CONFLICTED";
240                 break;
241         default:
242                 error_code = NFC_ERROR_OPERATION_FAILED;
243                 errorstr = "OPERATION_FAILED";
244         }
245
246         if (error_code != NFC_ERROR_NONE)
247                 LOGE("NFC func : %s, %s(0x%08x)", func, errorstr, error_code);
248
249         return error_code;
250 }
251
252 bool nfc_common_is_initialized()
253 {
254         return gdbus_nfc_context.initialized;
255 }
256
257 bool nfc_common_is_supported(char *str)
258 {
259         int ret;
260         bool is_supported;
261
262         ret = system_info_get_platform_bool(str, &is_supported);
263
264         if (ret != 0)
265                 return false;
266
267         return is_supported;
268 }
269
270 int nfc_common_get_rawdata_size(nfc_ndef_message_h ndef_message,
271         unsigned int *byte_size)
272 {
273         int ret;
274
275         LOG_BEGIN();
276
277         CHECK_SUPPORTED(NFC_FEATURE);
278         CHECK_INIT();
279         CHECK_INVALID(ndef_message == NULL);
280         CHECK_INVALID(byte_size == NULL);
281
282         ret = net_nfc_get_ndef_message_byte_length(ndef_message,
283                 (unsigned int *)byte_size);
284
285         return nfc_common_convert_error_code(__func__, ret);
286 }
287 /* LCOV_EXCL_STOP */
288