2 * Copyright (c) 2012, 2013 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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 "nfc_common.h"
19 _nfc_context_s gdbus_nfc_context;
22 bool nfc_common_get_login_user(uid_t *uid)
28 uid_count = sd_get_uids(&uids);
31 LOGD("sd_get_uids failed [%d]", uid_count);
35 for (i = 0; i < uid_count ; i++) {
38 ret = sd_uid_get_state(uids[i], &state);
41 LOGD("sd_uid_get_state failed [%d]", ret);
43 if (!strncmp(state, "online", 6)) {
54 LOGD("not exist login user");
60 int _iter_func(const aul_app_info *info, void *data)
63 int *pid = (int *)data;
66 if (nfc_common_get_login_user(&uid) == false) {
67 LOGD("net_nfc_util_get_login_user is failed");
71 status = aul_app_get_status_bypid_for_uid(info->pid, uid);
73 LOGD("login user is %d, pid is %d, status is %d", (int)uid, info->pid, status);
75 if (status == STATUS_VISIBLE || status == STATUS_FOCUS) {
82 pid_t nfc_common_get_focus_app_pid()
88 if (nfc_common_get_login_user(&uid) == false) {
89 LOGD("nfc_common_get_login_user is failed");
93 ret = aul_app_get_all_running_app_info_for_uid(_iter_func, &pid, uid);
95 if (ret == AUL_R_OK) {
98 LOGD("aul_app_get_all_running_app_info_for_uid is failed");
103 char * nfc_common_get_bt_address_string(data_h data)
111 buffer = net_nfc_get_data_buffer(data);
112 length = net_nfc_get_data_length(data);
114 if (buffer == NULL || length < 6)
117 return g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
126 bool nfc_common_check_app_permission()
128 pid_t focus_app_pid, current_app_pid;
130 focus_app_pid = nfc_common_get_focus_app_pid();
131 current_app_pid = getpgid(getpid());
133 LOGD("[check app permission] focus_app_pid [%d], current_app_pid [%d]", focus_app_pid,
136 return (focus_app_pid == current_app_pid) ? true : false;
139 int nfc_common_convert_error_code(const char *func, int native_error_code)
141 int error_code = NFC_ERROR_NONE;
142 char *errorstr = NULL;
144 switch (native_error_code) {
146 error_code = NFC_ERROR_NONE;
147 errorstr = "ERROR_NONE";
150 case NET_NFC_ALLOC_FAIL:
151 error_code = NFC_ERROR_OUT_OF_MEMORY;
152 errorstr = "OUT_OF_MEMORY";
155 case NET_NFC_NOT_CONNECTED:
156 error_code = NFC_ERROR_NO_DEVICE;
157 errorstr = "NO_DEVICE";
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";
179 case NET_NFC_SECURITY_FAIL:
180 error_code = NFC_ERROR_SECURITY_RESTRICTED;
181 errorstr = "SECURITY_RESTRICTED";
184 case NET_NFC_INVALID_STATE:
185 error_code = NFC_ERROR_ILLEGAL_STATE;
186 errorstr = "ILLEGAL_STATE";
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";
195 case NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE:
196 error_code = NFC_ERROR_INVALID_RECORD_TYPE;
197 errorstr = "INVALID_RECORD_TYPE";
200 case NET_NFC_ALREADY_INITIALIZED:
201 case NET_NFC_ALREADY_REGISTERED:
202 error_code = NFC_ERROR_NONE;
203 errorstr = "ERROR_NONE";
206 case NET_NFC_RF_TIMEOUT:
207 error_code = NFC_ERROR_TIMED_OUT;
208 errorstr = "TIMED_OUT";
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";
217 case NET_NFC_NO_NDEF_MESSAGE:
218 error_code = NFC_ERROR_NO_NDEF_MESSAGE;
219 errorstr = "NO_NDEF_MESSAGE";
222 error_code = NFC_ERROR_DEVICE_BUSY;
223 errorstr = "DEVICE_BUSY";
225 case NET_NFC_NO_NDEF_SUPPORT:
226 error_code = NFC_ERROR_NOT_NDEF_FORMAT;
227 errorstr = "NOT_SUPPORTED";
229 case NET_NFC_PERMISSION_DENIED:
230 error_code = NFC_ERROR_PERMISSION_DENIED;
231 errorstr = "PERMISSION_DENIED";
233 case NET_NFC_NOT_ACTIVATED:
234 error_code = NFC_ERROR_NOT_ACTIVATED;
235 errorstr = "NOT_ACTIVATED";
237 case NET_NFC_DATA_CONFLICTED:
238 error_code = NFC_ERROR_DATA_CONFLICTED;
239 errorstr = "DATA_CONFLICTED";
242 error_code = NFC_ERROR_OPERATION_FAILED;
243 errorstr = "OPERATION_FAILED";
246 if (error_code != NFC_ERROR_NONE)
247 LOGE("NFC func : %s, %s(0x%08x)", func, errorstr, error_code);
252 bool nfc_common_is_initialized()
254 return gdbus_nfc_context.initialized;
257 bool nfc_common_is_supported(char *str)
262 ret = system_info_get_platform_bool(str, &is_supported);
270 int nfc_common_get_rawdata_size(nfc_ndef_message_h ndef_message,
271 unsigned int *byte_size)
277 CHECK_SUPPORTED(NFC_FEATURE);
279 CHECK_INVALID(ndef_message == NULL);
280 CHECK_INVALID(byte_size == NULL);
282 ret = net_nfc_get_ndef_message_byte_length(ndef_message,
283 (unsigned int *)byte_size);
285 return nfc_common_convert_error_code(__func__, ret);