Beta merge 2
[profile/ivi/wrt-plugins-tizen.git] / src / platform / Tizen / NFC / NFCUtil.cpp
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
18
19 #include <Commons/Exception.h>
20
21 #include "NFCUtil.h"
22 #include <dpl/log/log.h>
23 #include <nfc.h>
24
25
26 namespace TizenApis {
27 namespace Platform {
28 namespace NFC {
29
30 using namespace Api::NFC;
31
32 std::vector<unsigned char> NFCUtil::toVector(const unsigned char *ch, const int size) {
33         std::vector<unsigned char> vec;
34         if (ch && (size > 0)) {
35                 int i;
36                 
37                 for (i = 0; i < size; i++)
38                         vec.push_back(ch[i]);
39         }
40         LogDebug("result:" << byteToString(ch, size));
41         return vec;
42 }
43
44 unsigned char *NFCUtil::toCharPtr(std::vector<unsigned char> vec) {
45         if (vec.size() > 0) {
46                 unsigned char * chr = (unsigned char *) malloc(vec.size() * sizeof(unsigned char));
47                 for (int i = 0; i < static_cast<int>(vec.size()); i++)
48                         chr[i] = vec.at(i);
49
50                 return chr;
51         }
52
53         return NULL;
54 }
55
56 char *NFCUtil::byteToString(std::vector<unsigned char> buffer){
57         static char localbuffer[255];
58         memset(localbuffer, 0, 255);
59         memcpy(localbuffer, toCharPtr(buffer), buffer.size());
60         return localbuffer;     
61 }
62
63 char *NFCUtil::byteToString(const unsigned char* buffer, const int size){
64         static char localbuffer[255];
65         memset(localbuffer, 0, 255);
66         memcpy(localbuffer, buffer, size);
67         return localbuffer;     
68 }
69
70 nfcTagType NFCUtil::convertTonfcTagType(const unsigned short type) {
71         switch (static_cast<nfc_tag_type_e>(type)) {
72         case NFC_GENERIC_PICC:
73                 return NFC_TAGTYPE_GENERIC_PICC;
74         case NFC_ISO14443_A_PICC:
75                 return NFC_TAGTYPE_ISO14443_A_PICC;
76         case NFC_ISO14443_4A_PICC:
77                 return NFC_TAGTYPE_ISO14443_4A_PICC;
78         case NFC_ISO14443_3A_PICC:
79                 return NFC_TAGTYPE_ISO14443_3A_PICC;
80         case NFC_MIFARE_MINI_PICC:
81                 return NFC_TAGTYPE_MIFARE_MINI_PICC;
82         case NFC_MIFARE_1K_PICC:
83                 return NFC_TAGTYPE_MIFARE_1K_PICC;
84         case NFC_MIFARE_4K_PICC:
85                 return NFC_TAGTYPE_MIFARE_4K_PICC;
86         case NFC_MIFARE_ULTRA_PICC:
87                 return NFC_TAGTYPE_MIFARE_ULTRA_PICC;
88         case NFC_MIFARE_DESFIRE_PICC:
89                 return NFC_TAGTYPE_MIFARE_DESFIRE_PICC;
90         case NFC_ISO14443_B_PICC:
91                 return NFC_TAGTYPE_ISO14443_B_PICC;
92         case NFC_ISO14443_4B_PICC:
93                 return NFC_TAGTYPE_ISO14443_4B_PICC;
94         case NFC_ISO14443_BPRIME_PICC:
95                 return NFC_TAGTYPE_ISO14443_BPRIME_PICC;
96         case NFC_FELICA_PICC:
97                 return NFC_TAGTYPE_FELICA_PICC;
98         case NFC_JEWEL_PICC:
99                 return NFC_TAGTYPE_JEWEL_PICC;
100         case NFC_ISO15693_PICC:
101                 return NFC_TAGTYPE_ISO15693_PICC;
102         case NFC_UNKNOWN_TARGET:
103         default :
104                 return NFC_TAGTYPE_UNKNOWN_TARGET;
105         }
106 }
107
108 unsigned short NFCUtil::convertToTNF(nfcTNF tnf) {
109         switch (tnf) {
110                 case NFC_TNF_WELL_KNOWN:
111                         return static_cast<unsigned short>(NFC_RECORD_TNF_WELL_KNOWN);
112                 case NFC_TNF_MIME_MEDIA:
113                         return static_cast<unsigned short>(NFC_RECORD_TNF_MIME_MEDIA);
114                 case NFC_TNF_URI:
115                         return static_cast<unsigned short>(NFC_RECORD_TNF_URI);
116                 case NFC_TNF_EXTERNAL_RTD:
117                         return static_cast<unsigned short>(NFC_RECORD_TNF_EXTERNAL_RTD);
118                 case NFC_TNF_UNKNOWN:
119                         return static_cast<unsigned short>(NFC_RECORD_TNF_UNKNOWN);
120                 case NFC_TNF_UNCHAGNED:
121                         return static_cast<unsigned short>(NFC_RECORD_TNF_UNCHAGNED);
122                 case NFC_TNF_EMPTY:
123                 default:
124                         return static_cast<unsigned short>(NFC_RECORD_TNF_EMPTY);
125         }
126 }
127
128 nfcTNF NFCUtil::convertTonfcTNF(unsigned short tnf) {
129         switch (static_cast<nfc_record_tnf_e>(tnf)) {
130                 case NFC_RECORD_TNF_WELL_KNOWN:
131                         return NFC_TNF_WELL_KNOWN;
132                 case NFC_RECORD_TNF_MIME_MEDIA:
133                         return NFC_TNF_MIME_MEDIA;
134                 case NFC_RECORD_TNF_URI:
135                         return NFC_TNF_URI;
136                 case NFC_RECORD_TNF_EXTERNAL_RTD:
137                         return NFC_TNF_EXTERNAL_RTD;
138                 case NFC_RECORD_TNF_UNKNOWN:
139                         return NFC_TNF_UNKNOWN;
140                 case NFC_RECORD_TNF_UNCHAGNED:
141                         return NFC_TNF_UNCHAGNED;
142                 case NFC_RECORD_TNF_EMPTY:
143                 default:
144                         return NFC_TNF_EMPTY;
145         }
146 }
147
148 }
149 }
150 }