Nick Pelly | bc21fde | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 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 | #define LOG_TAG "NdefRecord" |
| 18 | |
Nick Pelly | 1af97f0 | 2010-10-22 06:59:54 -0700 | [diff] [blame] | 19 | #include <stdlib.h> |
| 20 | |
Nick Pelly | bc21fde | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 21 | #include "jni.h" |
| 22 | #include "JNIHelp.h" |
| 23 | |
| 24 | #include "android_nfc.h" |
| 25 | |
| 26 | #include <utils/Log.h> |
| 27 | |
| 28 | namespace android { |
| 29 | |
| 30 | static jbyteArray android_nfc_NdefRecord_generate( |
| 31 | JNIEnv *e, jobject o, jshort flags, jshort tnf, jbyteArray type, |
| 32 | jbyteArray id, jbyteArray payload) |
| 33 | { |
| 34 | uint32_t status; |
| 35 | phFriNfc_NdefRecord_t record; |
| 36 | uint32_t buf_size; |
| 37 | uint32_t record_size; |
| 38 | uint8_t *buf = NULL; |
| 39 | jbyteArray result = NULL; |
| 40 | |
| 41 | /* Prepare NDEF record structure */ |
| 42 | record.Flags = (uint8_t)flags; |
| 43 | record.Tnf = (uint8_t)tnf; |
| 44 | record.TypeLength = (uint32_t)e->GetArrayLength(type); |
| 45 | record.Type = (uint8_t *)e->GetByteArrayElements(type, NULL); |
| 46 | record.IdLength = (uint32_t)e->GetArrayLength(id); |
| 47 | record.Id = (uint8_t *)e->GetByteArrayElements(id, NULL); |
| 48 | record.PayloadLength = (uint32_t)e->GetArrayLength(payload); |
| 49 | record.PayloadData = (uint8_t *)e->GetByteArrayElements(payload, NULL); |
| 50 | |
| 51 | buf_size = record.PayloadLength + record.IdLength + record.TypeLength + 8; |
| 52 | |
| 53 | buf = (uint8_t*)malloc(buf_size); |
| 54 | if (buf == NULL) |
| 55 | goto end; |
| 56 | |
Jeff Hamilton | 5629d51 | 2010-11-08 11:36:21 -0600 | [diff] [blame] | 57 | TRACE("phFriNfc_NdefRecord_Generate()"); |
Nick Pelly | bc21fde | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 58 | |
| 59 | status = phFriNfc_NdefRecord_Generate(&record, buf, buf_size, |
| 60 | &record_size); |
| 61 | |
| 62 | if (status) { |
| 63 | LOGE("phFriNfc_NdefRecord_Generate() returned 0x%04x", status); |
| 64 | goto end; |
| 65 | } |
Jeff Hamilton | 5629d51 | 2010-11-08 11:36:21 -0600 | [diff] [blame] | 66 | TRACE("phFriNfc_NdefRecord_Generate() returned 0x%04x", status); |
Nick Pelly | bc21fde | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 67 | |
| 68 | result = e->NewByteArray(record_size); |
| 69 | if (result == NULL) |
| 70 | goto end; |
| 71 | |
| 72 | e->SetByteArrayRegion(result, 0, record_size, (jbyte *)buf); |
| 73 | |
| 74 | end: |
| 75 | e->ReleaseByteArrayElements(type, (jbyte *)record.Type, JNI_ABORT); |
| 76 | e->ReleaseByteArrayElements(id, (jbyte *)record.Id, JNI_ABORT); |
| 77 | e->ReleaseByteArrayElements(payload, (jbyte *)record.PayloadData, JNI_ABORT); |
| 78 | |
| 79 | if(buf) |
| 80 | free(buf); |
| 81 | |
| 82 | return result; |
| 83 | } |
| 84 | |
Sylvain Fonteneau | dd7341f | 2010-10-17 15:32:33 -0700 | [diff] [blame] | 85 | static jint android_nfc_NdefRecord_parseNdefRecord(JNIEnv *e, jobject o, |
| 86 | jbyteArray array) |
| 87 | { |
| 88 | uint16_t status; |
| 89 | jbyte *raw_record; |
| 90 | jsize raw_record_size; |
| 91 | jint ret = -1; |
| 92 | phFriNfc_NdefRecord_t record; |
| 93 | |
Martijn Coenen | 8bede17 | 2011-04-08 16:42:22 +0200 | [diff] [blame] | 94 | jfieldID mType, mId, mPayload, mTnf, mFlags; |
Sylvain Fonteneau | dd7341f | 2010-10-17 15:32:33 -0700 | [diff] [blame] | 95 | jbyteArray type = NULL; |
| 96 | jbyteArray id = NULL; |
| 97 | jbyteArray payload = NULL; |
| 98 | |
| 99 | jclass record_cls = e->GetObjectClass(o); |
| 100 | |
| 101 | raw_record_size = e->GetArrayLength(array); |
| 102 | raw_record = e->GetByteArrayElements(array, NULL); |
| 103 | if (raw_record == NULL) { |
| 104 | goto clean_and_return; |
| 105 | } |
| 106 | |
Jeff Hamilton | 5629d51 | 2010-11-08 11:36:21 -0600 | [diff] [blame] | 107 | TRACE("phFriNfc_NdefRecord_Parse()"); |
Sylvain Fonteneau | dd7341f | 2010-10-17 15:32:33 -0700 | [diff] [blame] | 108 | status = phFriNfc_NdefRecord_Parse(&record, (uint8_t *)raw_record); |
| 109 | if (status) { |
| 110 | LOGE("phFriNfc_NdefRecord_Parse() returned 0x%04x", status); |
| 111 | goto clean_and_return; |
| 112 | } |
Jeff Hamilton | 5629d51 | 2010-11-08 11:36:21 -0600 | [diff] [blame] | 113 | TRACE("phFriNfc_NdefRecord_Parse() returned 0x%04x", status); |
Sylvain Fonteneau | dd7341f | 2010-10-17 15:32:33 -0700 | [diff] [blame] | 114 | |
| 115 | /* Set TNF field */ |
| 116 | mTnf = e->GetFieldID(record_cls, "mTnf", "S"); |
| 117 | e->SetShortField(o, mTnf, record.Tnf); |
| 118 | |
| 119 | /* Set type field */ |
| 120 | mType = e->GetFieldID(record_cls, "mType", "[B"); |
| 121 | type = e->NewByteArray(record.TypeLength); |
| 122 | if (type == NULL) { |
| 123 | goto clean_and_return; |
| 124 | } |
| 125 | e->SetByteArrayRegion(type, 0, record.TypeLength, |
| 126 | (jbyte *)record.Type); |
| 127 | e->SetObjectField(o, mType, type); |
| 128 | |
| 129 | /* Set id field */ |
| 130 | mId = e->GetFieldID(record_cls, "mId", "[B"); |
| 131 | id = e->NewByteArray(record.IdLength); |
| 132 | if (id == NULL) { |
| 133 | goto clean_and_return; |
| 134 | } |
| 135 | e->SetByteArrayRegion(id, 0, record.IdLength, |
| 136 | (jbyte *)record.Id); |
| 137 | e->SetObjectField(o, mId, id); |
| 138 | |
| 139 | /* Set payload field */ |
| 140 | mPayload = e->GetFieldID(record_cls, "mPayload", "[B"); |
| 141 | payload = e->NewByteArray(record.PayloadLength); |
| 142 | if (payload == NULL) { |
| 143 | goto clean_and_return; |
| 144 | } |
Martijn Coenen | 8bede17 | 2011-04-08 16:42:22 +0200 | [diff] [blame] | 145 | |
Sylvain Fonteneau | dd7341f | 2010-10-17 15:32:33 -0700 | [diff] [blame] | 146 | e->SetByteArrayRegion(payload, 0, record.PayloadLength, |
| 147 | (jbyte *)record.PayloadData); |
| 148 | e->SetObjectField(o, mPayload, payload); |
| 149 | |
Martijn Coenen | 8bede17 | 2011-04-08 16:42:22 +0200 | [diff] [blame] | 150 | /* Set flags field */ |
| 151 | mFlags = e->GetFieldID(record_cls, "mFlags", "B"); |
Martijn Coenen | ef565c4 | 2011-11-10 15:20:14 -0800 | [diff] [blame] | 152 | e->SetByteField(o, mFlags, record.Flags); |
Martijn Coenen | 8bede17 | 2011-04-08 16:42:22 +0200 | [diff] [blame] | 153 | |
Sylvain Fonteneau | dd7341f | 2010-10-17 15:32:33 -0700 | [diff] [blame] | 154 | ret = 0; |
| 155 | |
| 156 | clean_and_return: |
| 157 | if (type != NULL) { |
| 158 | e->DeleteLocalRef(type); |
| 159 | } |
| 160 | if (id != NULL) { |
| 161 | e->DeleteLocalRef(id); |
| 162 | } |
| 163 | if (payload != NULL) { |
| 164 | e->DeleteLocalRef(payload); |
| 165 | } |
| 166 | if (raw_record != NULL) { |
| 167 | e->ReleaseByteArrayElements(array, raw_record, JNI_ABORT); |
| 168 | } |
| 169 | |
| 170 | return ret; |
| 171 | } |
| 172 | |
Nick Pelly | bc21fde | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 173 | static JNINativeMethod gMethods[] = { |
| 174 | {"generate", "(SS[B[B[B)[B", (void *)android_nfc_NdefRecord_generate}, |
Sylvain Fonteneau | dd7341f | 2010-10-17 15:32:33 -0700 | [diff] [blame] | 175 | {"parseNdefRecord", "([B)I", (void *)android_nfc_NdefRecord_parseNdefRecord}, |
Nick Pelly | bc21fde | 2010-10-13 17:25:24 -0700 | [diff] [blame] | 176 | }; |
| 177 | |
| 178 | int register_android_nfc_NdefRecord(JNIEnv *e) |
| 179 | { |
| 180 | return jniRegisterNativeMethods(e, "android/nfc/NdefRecord", gMethods, NELEM(gMethods)); |
| 181 | } |
| 182 | |
| 183 | } // namespace android |