Fix doxygen
[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 pid_t nfc_common_get_focus_app_pid()
23 {
24         int ret;
25         pid_t pid = 0;
26
27         ret = aul_window_get_focused_pid(&pid);
28
29         if (ret == AUL_R_OK) {
30                 return pid;
31         } else {
32                 LOGD("aul_window_get_focused_pid is failed");
33                 return -1;
34         }
35 }
36
37 char * nfc_common_get_bt_address_string(data_h data)
38 {
39         uint8_t *buffer;
40         uint32_t length;
41
42         if (data == NULL)
43                 return NULL;
44
45         buffer = net_nfc_get_data_buffer(data);
46         length = net_nfc_get_data_length(data);
47
48         if (buffer == NULL || length < 6)
49                 return NULL;
50
51         return g_strdup_printf("%02X:%02X:%02X:%02X:%02X:%02X",
52                 buffer[0],
53                 buffer[1],
54                 buffer[2],
55                 buffer[3],
56                 buffer[4],
57                 buffer[5]);
58 }
59
60 bool nfc_common_check_app_permission()
61 {
62         pid_t focus_app_pid, current_app_pid;
63
64         focus_app_pid = nfc_common_get_focus_app_pid();
65         current_app_pid = getpgid(getpid());
66
67         LOGD("[check app permission] focus_app_pid [%d], current_app_pid [%d]", focus_app_pid,
68                 current_app_pid);
69
70         return (focus_app_pid == current_app_pid) ? true : false;
71 }
72
73 int nfc_common_convert_error_code(const char *func, int native_error_code)
74 {
75         int error_code = NFC_ERROR_NONE;
76         char *errorstr = NULL;
77
78         switch (native_error_code) {
79         case NET_NFC_OK:
80                 error_code = NFC_ERROR_NONE;
81                 errorstr = "ERROR_NONE";
82                 break;
83
84         case NET_NFC_ALLOC_FAIL:
85                 error_code = NFC_ERROR_OUT_OF_MEMORY;
86                 errorstr = "OUT_OF_MEMORY";
87                 break;
88
89         case NET_NFC_NOT_CONNECTED:
90                 error_code = NFC_ERROR_NO_DEVICE;
91                 errorstr = "NO_DEVICE";
92                 break;
93
94         case NET_NFC_UNKNOWN_ERROR:
95         case NET_NFC_THREAD_CREATE_FAIL:
96         case NET_NFC_IPC_FAIL:
97         case NET_NFC_BUFFER_TOO_SMALL:
98         case NET_NFC_COMMUNICATE_WITH_CONTROLLER_FAILED:
99         case NET_NFC_RF_ERROR:
100         case NET_NFC_NOT_SUPPORTED:
101         case NET_NFC_TAG_READ_FAILED:
102         case NET_NFC_TAG_WRITE_FAILED:
103         case NET_NFC_OPERATION_FAIL:
104         case NET_NFC_INSUFFICIENT_STORAGE:
105         case NET_NFC_NOT_INITIALIZED:
106         case NET_NFC_NOT_REGISTERED:
107         case NET_NFC_NO_DATA_FOUND:
108         case NET_NFC_NOT_ALLOWED_OPERATION:
109                 error_code = NFC_ERROR_OPERATION_FAILED;
110                 errorstr = "OPERATION_FAILED";
111                 break;
112
113         case NET_NFC_SECURITY_FAIL:
114                 error_code = NFC_ERROR_SECURITY_RESTRICTED;
115                 errorstr = "SECURITY_RESTRICTED";
116                 break;
117
118         case NET_NFC_INVALID_STATE:
119                 error_code = NFC_ERROR_ILLEGAL_STATE;
120                 errorstr = "ILLEGAL_STATE";
121                 break;
122
123         case NET_NFC_OUT_OF_BOUND:
124         case NET_NFC_NULL_PARAMETER:
125         case NET_NFC_LLCP_INVALID_SOCKET:
126                 error_code = NFC_ERROR_INVALID_PARAMETER;
127                 errorstr = "INVALID_PARAMETER";
128                 break;
129         case NET_NFC_NDEF_RECORD_IS_NOT_EXPECTED_TYPE:
130                 error_code = NFC_ERROR_INVALID_RECORD_TYPE;
131                 errorstr = "INVALID_RECORD_TYPE";
132                 break;
133
134         case NET_NFC_ALREADY_INITIALIZED:
135         case NET_NFC_ALREADY_REGISTERED:
136                 error_code = NFC_ERROR_NONE;
137                 errorstr = "ERROR_NONE";
138                 break;
139
140         case NET_NFC_RF_TIMEOUT:
141                 error_code = NFC_ERROR_TIMED_OUT;
142                 errorstr = "TIMED_OUT";
143                 break;
144         case NET_NFC_INVALID_FORMAT:
145         case NET_NFC_NDEF_TYPE_LENGTH_IS_NOT_OK:
146         case NET_NFC_NDEF_ID_LENGTH_IS_NOT_OK:
147         case NET_NFC_NDEF_BUF_END_WITHOUT_ME:
148                 error_code = NFC_ERROR_INVALID_NDEF_MESSAGE;
149                 errorstr = "INVALID_NDEF_MESSAGE";
150                 break;
151         case NET_NFC_NO_NDEF_MESSAGE:
152                 error_code = NFC_ERROR_NO_NDEF_MESSAGE;
153                 errorstr = "NO_NDEF_MESSAGE";
154                 break;
155         case NET_NFC_BUSY:
156                 error_code = NFC_ERROR_DEVICE_BUSY;
157                 errorstr = "DEVICE_BUSY";
158                 break;
159         case NET_NFC_NO_NDEF_SUPPORT:
160                 error_code = NFC_ERROR_NOT_NDEF_FORMAT;
161                 errorstr = "NOT_SUPPORTED";
162                 break;
163         case NET_NFC_PERMISSION_DENIED:
164                 error_code = NFC_ERROR_PERMISSION_DENIED;
165                 errorstr = "PERMISSION_DENIED";
166                 break;
167         case NET_NFC_NOT_ACTIVATED:
168                 error_code = NFC_ERROR_NOT_ACTIVATED;
169                 errorstr = "NOT_ACTIVATED";
170                 break;
171         case NET_NFC_DATA_CONFLICTED:
172                 error_code = NFC_ERROR_DATA_CONFLICTED;
173                 errorstr = "DATA_CONFLICTED";
174                 break;
175         default:
176                 error_code = NFC_ERROR_OPERATION_FAILED;
177                 errorstr = "OPERATION_FAILED";
178         }
179
180         if (error_code != NFC_ERROR_NONE)
181                 LOGE("NFC func : %s, %s(0x%08x)", func, errorstr, error_code);
182
183         return error_code;
184 }
185
186 bool nfc_common_is_initialized()
187 {
188         return gdbus_nfc_context.initialized;
189 }
190
191 bool nfc_common_is_supported(char *str)
192 {
193         int ret;
194         bool is_supported;
195
196         ret = system_info_get_platform_bool(str, &is_supported);
197
198         if (ret != 0)
199                 return false;
200
201         return is_supported;
202 }
203
204 int nfc_common_get_rawdata_size(nfc_ndef_message_h ndef_message,
205         unsigned int *byte_size)
206 {
207         int ret;
208
209         LOG_BEGIN();
210
211         CHECK_SUPPORTED(NFC_FEATURE);
212         CHECK_INIT();
213         CHECK_INVALID(ndef_message == NULL);
214         CHECK_INVALID(byte_size == NULL);
215
216         ret = net_nfc_get_ndef_message_byte_length(ndef_message,
217                 (unsigned int *)byte_size);
218
219         return nfc_common_convert_error_code(__func__, ret);
220 }
221 /* LCOV_EXCL_STOP */
222