summaryrefslogtreecommitdiff
path: root/src/standards/Tizen/NFC/NFCConverter.cpp
blob: aa67e7a9d471c42291ec1cf4d8a820c57fee9bae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
/*
 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the License);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an AS IS BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */



#include <dpl/log/log.h>
#include <CommonsJavaScript/Validator.h>
#include <CommonsJavaScript/ScopedJSStringRef.h>
#include <CommonsJavaScript/JSUtils.h>
#include "NFCConverter.h"
#include "JSNdefRecord.h"
#include "JSNdefMessage.h"

using namespace std;
using namespace TizenApis::Api::NFC;
using namespace WrtDeviceApis::CommonsJavaScript;
using namespace WrtDeviceApis;


#define NFC_FILTER_ATTRIBUTE_TAG_TYPES "tagTypes"

#define NFC_CHANGED_CALLBACK_ONATTATCH "onAttach"
#define NFC_CHANGED_CALLBACK_ONDETACH "onDetach"

namespace TizenApis {
namespace Tizen1_0 {

vector<std::string> NFCConverter::m_allowedTagFilterProperties;

NFCConverter::NFCConverter(JSContextRef context) : Converter(context)
{
    LogDebug("entered");
    static bool init = initializeAllowedProperties();
    (void) init;
}

NFCConverter::~NFCConverter()
{
    LogDebug("entered");
}

TagFilterPtr NFCConverter::toNFCTagFilter(JSValueRef jsValue)
{
    LogDebug("entered");
    if (JSValueIsNull(m_context, jsValue) ||
        JSValueIsUndefined(m_context, jsValue)) {
        return TagFilterPtr(); //empty filter
    }
    TagFilterPtr filter(new TagFilter());

    Validator validator(m_context);

    if (!JSValueIsUndefined(m_context, jsValue)) {
    	vector<std::string> filter_values(toVectorOfStrings(jsValue));

    	LogDebug("NFCConverter, start setting filter values, tag_types.size() : " << filter_values.size());
    	  for (unsigned int i=0; i<filter_values.size(); i++) {
		nfcTagType filterValue = toNfcTagType(filter_values.at(i));

		filter->addTagTypeValue(filterValue);
    		  LogDebug("NFCConverter,  tag_types.at(i): " << filter_values.at(i));
    	  }
    }
    LogDebug("NFCConverter,  filter size == " << filter->getFilterSize());

    return filter;
}


nfcTagType NFCConverter::toNfcTagType(std::string typeString) {
	if (typeString == "ISO14443_A")
		return NFC_TAGTYPE_ISO14443_A_PICC;
	else if (typeString == "ISO14443_4A")
		return NFC_TAGTYPE_ISO14443_4A_PICC;
	else if (typeString == "ISO14443_3A")
		return NFC_TAGTYPE_ISO14443_3A_PICC;
	else if (typeString == "MIFARE_MINI")
		return NFC_TAGTYPE_MIFARE_MINI_PICC;
	else if (typeString == "MIFARE_1K")
		return NFC_TAGTYPE_MIFARE_1K_PICC;
	else if (typeString == "MIFARE_4K")
		return NFC_TAGTYPE_MIFARE_4K_PICC;
	else if (typeString == "MIFARE_ULTRA")
		return NFC_TAGTYPE_MIFARE_ULTRA_PICC;
	else if (typeString == "MIFARE_DESFIRE")
		return NFC_TAGTYPE_MIFARE_DESFIRE_PICC;
	else if (typeString == "ISO14443_B")
		return NFC_TAGTYPE_ISO14443_B_PICC;
	else if (typeString == "ISO14443_4B")
		return NFC_TAGTYPE_ISO14443_4B_PICC;
	else if (typeString == "ISO14443_BPRIME")
		return NFC_TAGTYPE_ISO14443_BPRIME_PICC;
	else if (typeString == "FELICA")
		return NFC_TAGTYPE_FELICA_PICC;
	else if (typeString == "JEWEL")
		return NFC_TAGTYPE_JEWEL_PICC;
	else if (typeString == "ISO15693")
		return NFC_TAGTYPE_ISO15693_PICC;
	else
		ThrowMsg(Commons::InvalidArgumentException, "No Match Tag Type");
}

std::string NFCConverter::toNfcTagTypeString(nfcTagType type) {
	switch (type) {
		case NFC_TAGTYPE_GENERIC_PICC:
			return "GENERIC_TARGET";
			break;
		case NFC_TAGTYPE_ISO14443_A_PICC:
			return "ISO14443_A";
			break;
		case NFC_TAGTYPE_ISO14443_4A_PICC:
			return "ISO14443_4A";
			break;
		case NFC_TAGTYPE_ISO14443_3A_PICC:
			return "ISO14443_3A";
			break;
		case NFC_TAGTYPE_MIFARE_MINI_PICC:
			return "MIFARE_MINI";
			break;
		case NFC_TAGTYPE_MIFARE_1K_PICC:
			return "MIFARE_1K";
			break;
		case NFC_TAGTYPE_MIFARE_4K_PICC:
			return "MIFARE_4K";
			break;
		case NFC_TAGTYPE_MIFARE_ULTRA_PICC:
			return "MIFARE_ULTRA";
			break;
		case NFC_TAGTYPE_MIFARE_DESFIRE_PICC:
			return "MIFARE_DESFIRE";
			break;
		case NFC_TAGTYPE_ISO14443_B_PICC:
			return "ISO14443_B";
			break;
		case NFC_TAGTYPE_ISO14443_4B_PICC:
			return "ISO14443_4B";
			break;
		case NFC_TAGTYPE_ISO14443_BPRIME_PICC:
			return "ISO14443_BPRIME";
			break;
		case NFC_TAGTYPE_FELICA_PICC:
			return "FELICA";
			break;
		case NFC_TAGTYPE_JEWEL_PICC:
			return "JEWEL";
			break;
		case NFC_TAGTYPE_ISO15693_PICC:
			return "ISO15693";
			break;
		case NFC_TAGTYPE_UNKNOWN_TARGET:
		default:
			return "UNKNOWN_TARGET";
			break;
	}
}

nfcTextEncodeUTF NFCConverter::toNfcTextEncodeUTF(std::string encodeString) {
	if (encodeString == "UTF-8")
		return NFC_TEXT_ENCODE_UTF_8;
	else if (encodeString == "UTF-16")
		return NFC_TEXT_ENCODE_UTF_16;
	else
		Throw(ConversionException);
}

std::string NFCConverter::toNfcTextEncodeUTFString(nfcTextEncodeUTF encode) {
	switch(encode) {
		case NFC_TEXT_ENCODE_UTF_8:
			return "UTF-8";
			break;
		case NFC_TEXT_ENCODE_UTF_16:
			return "UTF-16";
			break;
		default:
			Throw(ConversionException);
	}
}
bool NFCConverter::initializeAllowedProperties()
{
    LogDebug("entered");
    //NFC Filter properties
    m_allowedTagFilterProperties.push_back(NFC_FILTER_ATTRIBUTE_TAG_TYPES);
     return true;
}

JSValueRef NFCConverter::toJSValueRef(const std::vector<unsigned char>& arg) {
    JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);

    if (NULL == jsResult) {
        ThrowMsg(Commons::NullPointerException, "Could not create js array object");
    }

    for (std::size_t i = 0; i < arg.size(); ++i) {
        JSValueRef tmpVal = JSValueMakeNumber(m_context, arg[i]);
        if (!JSSetArrayElement(m_context, jsResult, i, tmpVal)) {
            ThrowMsg(Commons::UnknownException, "Could not insert value into js array");
        }
    }

    return jsResult;
}

JSValueRef NFCConverter::toJSValueRef(NdefRecordProperties props) {
	JSObjectRef propertyRef = JSObjectMake(m_context, NULL, NULL);
	
	ScopedJSStringRef jsTNFString(JSStringCreateWithUTF8CString("tnf"));
	ScopedJSStringRef jsTypeString(JSStringCreateWithUTF8CString("typeName"));
	ScopedJSStringRef jsIdString(JSStringCreateWithUTF8CString("id"));
	
	JSObjectSetProperty(m_context, propertyRef, jsTNFString.get(), toJSValueRef(static_cast<int>(props.tnf)), kJSPropertyAttributeNone, NULL);
	JSObjectSetProperty(m_context, propertyRef, jsTypeString.get(), toJSValueRef(props.typeName), kJSPropertyAttributeNone, NULL);
	JSObjectSetProperty(m_context, propertyRef, jsIdString.get(), toJSValueRef(props.id), kJSPropertyAttributeNone, NULL);

	return propertyRef;
}

JSValueRef NFCConverter::toJSValueRef(std::vector<NFCTagProperties> props) {
	JSObjectRef jsResult = JSCreateArrayObject(m_context, 0, NULL);
	if (NULL == jsResult) {
		ThrowMsg(WrtDeviceApis::Commons::NullPointerException,
		         "Could not create js array object");
	}

	for (std::size_t i = 0; i < props.size(); ++i) {
		JSValueRef tmpVal = toJSValueRef(props[i].value);
		if (!JSSetArrayElementInArrayList(m_context, jsResult, props[i].key.c_str(), tmpVal)) {
			ThrowMsg(WrtDeviceApis::Commons::UnknownException,
			         "Could not insert value into js array");
		}
	}
	return jsResult;
}

NdefRecordProperties NFCConverter::toNdefRecordProperties(JSValueRef value, JSValueRef* exception) {
	if (JSValueIsNull(m_context, value) || JSValueIsUndefined(m_context, value)) {
		ThrowMsg(Commons::InvalidArgumentException,
		         "Message is JS null or JS undefined.");
	}
	JSObjectRef obj = toJSObjectRef(value);
	
	if (!obj) {
		LogError("Object is null");
		ThrowMsg(Commons::NullPointerException, "Private object not initialized");
	}
	
	NdefRecordProperties props;
	JSValueRef tnf_JSProperty = JSUtils::getJSProperty(m_context, value, "tnf", exception);
	JSValueRef type_JSProperty = JSUtils::getJSProperty(m_context, value, "typeName", exception);
	JSValueRef id_JSProperty = JSUtils::getJSProperty(m_context, value, "id", exception);

	if (tnf_JSProperty != NULL && JSValueIsNumber(m_context, tnf_JSProperty)) {
		props.tnf = static_cast<nfcTNF>(toLong(tnf_JSProperty));
	}

	props.typeName= toVectorOfUChars(type_JSProperty);
	props.id= toVectorOfUChars(id_JSProperty);

	return props;
}

std::vector<void *> NFCConverter::toVectorOfRecordHandles(const JSValueRef& arg) {
    if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg)) {
		ThrowMsg(Commons::InvalidArgumentException,
		         "NdefRecordArray is JS null or JS undefined.");
    }

    if (!JSIsArrayValue(m_context, arg)) {
        ThrowMsg(Commons::ConversionException, "Argument is not an JS array.");
    }

    std::vector<void *> result;
    JSObjectRef objArg = toJSObjectRef(arg);
    for (std::size_t i = 0; i < JSGetArrayLength(m_context, objArg); ++i) {
        JSValueRef element = JSGetArrayElement(m_context, objArg, i);
        result.push_back(getRecordHandle(element));
    }
    return result;
}

void *NFCConverter::getRecordHandle(const JSValueRef& arg) {
	if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) ||
		(!JSValueIsObjectOfClass(m_context, arg, JSNdefRecord::getClassRef()))) {
		ThrowMsg(Commons::InvalidArgumentException,
		         "Message is JS null or JS undefined.");
	}
	JSObjectRef obj = toJSObjectRef(arg);
	
	if (!obj) {
		LogError("Object is null");
		ThrowMsg(Commons::NullPointerException, "Private object not initialized");
	}
	NdefRecordPrivObject* privateObject = static_cast<NdefRecordPrivObject*>(JSObjectGetPrivate(obj));
	if (!privateObject) {
		LogError("Private object is not set.");
		ThrowMsg(Commons::NullPointerException, "Private object not initialized");
	}

	INdefRecordPtr record = privateObject->getObject();

	return record->getHandle();
}

void *NFCConverter::getMessageHandle(const JSValueRef& arg) {
	if (JSValueIsNull(m_context, arg) || JSValueIsUndefined(m_context, arg) ||
		(!JSValueIsObjectOfClass(m_context, arg, JSNdefMessage::getClassRef()))) {
		ThrowMsg(Commons::InvalidArgumentException,
		         "Message is JS null or JS undefined.");
	}
	JSObjectRef obj = toJSObjectRef(arg);
	
	if (!obj) {
		LogError("Object is null");
		ThrowMsg(Commons::NullPointerException, "Private object not initialized");
	}
	NdefMessagePrivObject* privateObject = static_cast<NdefMessagePrivObject*>(JSObjectGetPrivate(obj));
	if (!privateObject) {
		LogError("Private object is not set.");
		ThrowMsg(Commons::NullPointerException, "Private object not initialized");
	}

	INdefMessagePtr message = privateObject->getObject();

	return message->getHandle();
}

NFCChangedCallback NFCConverter::toNFCChangedCallback(const JSValueRef& arg) {
	LogDebug("Entered");
    JSObjectRef object = toJSObjectRef(arg);

    NFCChangedCallback result;
    Validator validator(m_context);

    result.onAttach= JSUtils::getJSPropertyOrUndefined(
            m_context, object, NFC_CHANGED_CALLBACK_ONATTATCH
            );
    if (!validator.isNullOrUndefined(result.onAttach) &&
        !validator.isCallback(result.onAttach)) {
        ThrowMsg(Commons::ConversionException, "Not a valid callback.");
    }

    result.onDetach= JSUtils::getJSPropertyOrUndefined(
            m_context, object, NFC_CHANGED_CALLBACK_ONDETACH
            );
    if (!validator.isNullOrUndefined(result.onDetach) &&
        !validator.isCallback(result.onDetach)) {
        ThrowMsg(Commons::ConversionException, "Not a valid callback.");
    }

    return result;
}

}
}