revise LOG macros
[platform/core/connectivity/nfc-manager-neard.git] / client / net_nfc_client_tag_jewel.c
1 /*
2  * Copyright (c) 2012, 2013 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 <glib.h>
18 #include <string.h>
19
20 #include "net_nfc_client_tag_jewel.h"
21 #include "net_nfc_client_tag_internal.h"
22
23 #include "net_nfc_typedef_internal.h"
24 #include "net_nfc_debug_internal.h"
25 #include "net_nfc_util_internal.h"
26 #include "net_nfc_target_info.h"
27
28
29 #define JEWEL_CMD_REQA 0x26
30 #define JEWEL_CMD_WUPA 0x52
31 #define JEWEL_CMD_RID 0x78
32 #define JEWEL_CMD_RALL 0x00
33 #define JEWEL_CMD_READ 0x01
34 #define JEWEL_CMD_WRITE_E 0x53
35 #define JEWEL_CMD_WRITE_NE 0x1A
36 #define JEWEL_TAG_KEY   "UID"
37
38 API net_nfc_error_e net_nfc_client_jewel_read_id(net_nfc_target_handle_h handle,
39                 nfc_transceive_data_callback callback, void *user_data)
40 {
41         net_nfc_target_info_s *target_info = NULL;
42
43         data_s rawdata;
44
45         uint8_t send_buffer[9] = {0x00, };
46
47         if(handle == NULL)
48                 return NET_NFC_NULL_PARAMETER;
49
50         if(net_nfc_client_tag_is_connected() == FALSE)
51                 return NET_NFC_OPERATION_FAIL;
52
53         target_info = net_nfc_client_tag_get_client_target_info();
54         if (target_info == NULL)
55                 return NET_NFC_NO_DATA_FOUND;
56
57         if(target_info->devType != NET_NFC_JEWEL_PICC)
58         {
59                 NFC_ERR("only Jewel tag is available(TAG=%d)", target_info->devType);
60                 return NET_NFC_NOT_ALLOWED_OPERATION;
61         }
62
63         send_buffer[0] = JEWEL_CMD_RID;
64
65         net_nfc_util_compute_CRC(CRC_B, send_buffer, 9);
66
67         DEBUG_MSG_PRINT_BUFFER(send_buffer, 9);
68
69         rawdata.buffer = send_buffer;
70         rawdata.length = 9;
71
72         return net_nfc_client_transceive_data(handle,
73                         (data_h)&rawdata,
74                         callback,
75                         user_data);
76 }
77
78 API net_nfc_error_e net_nfc_client_jewel_read_byte(net_nfc_target_handle_h handle,
79                 uint8_t block, uint8_t byte, nfc_transceive_data_callback callback, void *user_data)
80 {
81         net_nfc_target_info_s *target_info = NULL;
82
83         data_s rawdata;
84
85         data_h UID = NULL;
86
87         uint8_t send_buffer[9] = {0x00, };
88
89         if(handle == NULL)
90                 return NET_NFC_NULL_PARAMETER;
91
92         if(block > 0xE || byte > 0x7 )
93                 return NET_NFC_OUT_OF_BOUND;
94
95         if(net_nfc_client_tag_is_connected() == FALSE)
96                 return NET_NFC_OPERATION_FAIL;
97
98         target_info = net_nfc_client_tag_get_client_target_info();
99         if (target_info == NULL)
100                 return NET_NFC_NO_DATA_FOUND;
101
102         if(target_info->devType != NET_NFC_JEWEL_PICC)
103         {
104                 NFC_ERR("only Jewel tag is available(TAG=%d)", target_info->devType);
105                 return NET_NFC_NOT_ALLOWED_OPERATION;
106         }
107
108         if(net_nfc_get_tag_info_value((net_nfc_target_info_h)target_info,
109                                 JEWEL_TAG_KEY,
110                                 &UID) != NET_NFC_OK)
111         {
112                 return NET_NFC_NO_DATA_FOUND;
113         }
114
115
116         if(((data_s*)UID)->length != 4)
117                 return NET_NFC_OUT_OF_BOUND;
118
119         /* command */
120         send_buffer[0] = JEWEL_CMD_READ;
121
122         /* addr */
123         send_buffer[1] = (((block << 3) & 0x78) | (byte & 0x7));
124
125         /* data */
126         send_buffer[2] = 0x00;
127
128         /* UID0 ~ 3 */
129         memcpy(&(send_buffer[3]),
130                         ((data_s*)UID)->buffer,
131                         ((data_s*)UID)->length);
132
133         net_nfc_util_compute_CRC(CRC_B, send_buffer, 9);
134
135         DEBUG_MSG_PRINT_BUFFER(send_buffer, 9);
136
137         rawdata.buffer = send_buffer;
138         rawdata.length = 9;
139
140         return net_nfc_client_transceive_data(handle,
141                         (data_h)&rawdata,
142                         callback,
143                         user_data);
144 }
145
146 API net_nfc_error_e net_nfc_client_jewel_read_all(net_nfc_target_handle_h handle,
147                 nfc_transceive_data_callback callback, void *user_data)
148 {
149         net_nfc_target_info_s *target_info = NULL;
150
151         data_s rawdata;
152
153         data_h UID = NULL;
154
155         uint8_t send_buffer[9] = {0x00, };
156
157         if(handle == NULL )
158                 return NET_NFC_NULL_PARAMETER;
159
160         if(net_nfc_client_tag_is_connected() == FALSE)
161                 return NET_NFC_OPERATION_FAIL;
162
163         target_info = net_nfc_client_tag_get_client_target_info();
164         if (target_info == NULL)
165                 return NET_NFC_NO_DATA_FOUND;
166
167         if(target_info->devType != NET_NFC_JEWEL_PICC)
168         {
169                 NFC_ERR("only Jewel tag is available(TAG=%d)", target_info->devType);
170                 return NET_NFC_NOT_ALLOWED_OPERATION;
171         }
172
173         if(net_nfc_get_tag_info_value((net_nfc_target_info_h)target_info,
174                                 JEWEL_TAG_KEY,
175                                 &UID) != NET_NFC_OK)
176         {
177                 return NET_NFC_NO_DATA_FOUND;
178         }
179
180         if(((data_s*)UID)->length != 4)
181                 return NET_NFC_OUT_OF_BOUND;
182
183         /* command */
184         send_buffer[0] = JEWEL_CMD_RALL;
185
186         /* addr */
187         send_buffer[1] = 0x00;
188
189         /* data */
190         send_buffer[2] = 0x00;
191
192         /* UID0 ~ 3 */
193         memcpy(&(send_buffer[3]),
194                         ((data_s*)UID)->buffer,
195                         ((data_s*)UID)->length);
196
197         net_nfc_util_compute_CRC(CRC_B, send_buffer, 9);
198
199         DEBUG_MSG_PRINT_BUFFER(send_buffer, 9);
200
201         rawdata.buffer = send_buffer;
202         rawdata.length = 9;
203
204         return net_nfc_client_transceive_data(handle,
205                         (data_h)&rawdata,
206                         callback,
207                         user_data);
208 }
209
210 API net_nfc_error_e net_nfc_client_jewel_write_with_erase(
211                 net_nfc_target_handle_h handle,
212                 uint8_t block,
213                 uint8_t byte,
214                 uint8_t data,
215                 nfc_transceive_callback callback,
216                 void *user_data)
217 {
218         net_nfc_target_info_s *target_info = NULL;
219         data_s rawdata;
220         data_h UID = NULL;
221         uint8_t send_buffer[9] = {0x00, };
222
223         if(handle == NULL)
224                 return NET_NFC_NULL_PARAMETER;
225
226         if(block > 0xE || byte > 0x7 )
227                 return NET_NFC_OUT_OF_BOUND;
228
229         if(net_nfc_client_tag_is_connected() == FALSE)
230                 return NET_NFC_OPERATION_FAIL;
231
232
233         target_info = net_nfc_client_tag_get_client_target_info();
234         if (target_info == NULL)
235                 return NET_NFC_NO_DATA_FOUND;
236
237         if(net_nfc_get_tag_info_value((net_nfc_target_info_h)target_info,
238                                 JEWEL_TAG_KEY,
239                                 &UID) != NET_NFC_OK)
240         {
241                 return NET_NFC_NO_DATA_FOUND;
242         }
243
244         if(((data_s*)UID)->length != 4)
245                 return NET_NFC_OUT_OF_BOUND;
246
247         /* command */
248         send_buffer[0] = JEWEL_CMD_WRITE_E;
249
250         /* addr */
251         send_buffer[1] = (((block << 3) & 0x78) | (byte & 0x7));
252
253         /* data */
254         send_buffer[2] = data;
255
256         /* UID0 ~ 3 */
257         memcpy(&(send_buffer[3]),
258                         ((data_s*)UID)->buffer,
259                         ((data_s*)UID)->length);
260
261         net_nfc_util_compute_CRC(CRC_B, send_buffer, 9);
262
263         DEBUG_MSG_PRINT_BUFFER(send_buffer, 9);
264
265         rawdata.buffer = send_buffer;
266         rawdata.length = 9;
267
268         return net_nfc_client_transceive(handle,
269                         (data_h)&rawdata,
270                         callback,
271                         user_data);
272
273 }
274
275 API net_nfc_error_e net_nfc_client_jewel_write_with_no_erase(
276                 net_nfc_target_handle_h handle,
277                 uint8_t block,
278                 uint8_t byte,
279                 uint8_t data,
280                 nfc_transceive_callback callback,
281                 void *user_data)
282 {
283         net_nfc_target_info_s *target_info = NULL;
284
285         data_s rawdata;
286
287         data_h UID = NULL;
288
289         uint8_t send_buffer[9] = {0x00, };
290
291         if(handle == NULL)
292                 return NET_NFC_NULL_PARAMETER;
293
294         if(block > 0xE || byte > 0x7 )
295                 return NET_NFC_OUT_OF_BOUND;
296
297         if(net_nfc_client_tag_is_connected() == FALSE)
298                 return NET_NFC_OPERATION_FAIL;
299
300         target_info = net_nfc_client_tag_get_client_target_info();
301         if (target_info == NULL)
302                 return NET_NFC_NO_DATA_FOUND;
303
304         if(target_info->devType != NET_NFC_JEWEL_PICC)
305         {
306                 NFC_ERR("only Jewel tag is available(TAG=%d)", target_info->devType);
307                 return NET_NFC_NOT_ALLOWED_OPERATION;
308         }
309
310         if(net_nfc_get_tag_info_value((net_nfc_target_info_h)target_info,
311                                 JEWEL_TAG_KEY,
312                                 &UID) != NET_NFC_OK)
313         {
314                 return NET_NFC_NO_DATA_FOUND;
315         }
316
317         if(((data_s*)UID)->length != 4)
318                 return NET_NFC_OUT_OF_BOUND;
319
320         /* command */
321         send_buffer[0] = JEWEL_CMD_WRITE_NE;
322
323         /* addr */
324         send_buffer[1] = (((block << 3) & 0x78) | (byte & 0x7));
325
326         /* data */
327         send_buffer[2] = data;
328
329         /* UID0 ~ 3 */
330         memcpy(&(send_buffer[3]),
331                         ((data_s*)UID)->buffer,
332                         ((data_s*)UID)->length);
333
334         net_nfc_util_compute_CRC(CRC_B, send_buffer, 9);
335
336         DEBUG_MSG_PRINT_BUFFER(send_buffer, 9);
337
338         rawdata.buffer = send_buffer;
339         rawdata.length = 9;
340
341         return net_nfc_client_transceive(handle,
342                         (data_h)&rawdata,
343                         callback,
344                         user_data);
345 }