2 * //******************************************************************
4 * // Copyright 2015 Samsung Electronics All Rights Reserved.
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
12 * // http://www.apache.org/licenses/LICENSE-2.0
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
23 #include "JniSecureUtils.h"
24 #include "JniOcSecureResource.h"
25 #include "srmutility.h"
28 jobject JniSecureUtils::convertProvisionresultVectorToJavaList(JNIEnv *env, const OC::PMResultList_t *result)
30 jobject jResultList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
36 for (size_t i = 0; i < result->size(); ++i)
38 jstring jStr = env->NewStringUTF((convertUUIDtoStr(result->at(i).deviceId).c_str()));
43 jobject jresult = env->NewObject(
44 g_cls_OcProvisionResult,
45 g_mid_OcProvisionResult_ctor,
47 static_cast<jint>(result->at(i).res)
54 env->CallBooleanMethod(jResultList, g_mid_LinkedList_add_object, jresult);
55 if (env->ExceptionCheck())
59 env->DeleteLocalRef(jresult);
60 env->DeleteLocalRef(jStr);
65 jobjectArray JniSecureUtils::convertDeviceVectorToJavaArray(JNIEnv *env,
66 std::vector<std::shared_ptr<OC::OCSecureResource>> &deviceListVector)
68 jsize len = static_cast<jsize>(deviceListVector.size());
69 jobjectArray devArr = env->NewObjectArray(len, g_cls_OcSecureResource, NULL);
76 for (jsize i = 0; i < len; ++i)
78 JniOcSecureResource *device = new JniOcSecureResource(deviceListVector[i]);
79 jobject jDevice = env->NewObject(g_cls_OcSecureResource, g_mid_OcSecureResource_ctor);
81 SetHandle<JniOcSecureResource>(env, jDevice, device);
87 env->SetObjectArrayElement(devArr, i, jDevice);
88 if (env->ExceptionCheck())
92 env->DeleteLocalRef(jDevice);
97 std::string JniSecureUtils::convertUUIDtoStr(OicUuid_t uuid)
99 std::ostringstream deviceId("");
100 char base64Buff[B64ENCODE_OUT_SAFESIZE(sizeof(((OicUuid_t*)0)->id)) + 1] = {0,};
102 B64Result b64Ret = B64_OK;
104 b64Ret = b64Encode(uuid.id, sizeof(uuid.id), base64Buff,
105 sizeof(base64Buff), &outLen);
107 deviceId << base64Buff;
108 return deviceId.str();
111 jobject JniSecureUtils::convertUUIDVectorToJavaStrList(JNIEnv *env, UuidList_t &vector)
113 jobject jList = env->NewObject(g_cls_LinkedList, g_mid_LinkedList_ctor);
118 for (size_t i = 0; i < vector.size(); ++i)
120 jstring jStr = env->NewStringUTF((convertUUIDtoStr(vector[i])).c_str());
125 env->CallBooleanMethod(jList, g_mid_LinkedList_add_object, jStr);
126 if (env->ExceptionCheck())
130 env->DeleteLocalRef(jStr);
135 OCStackResult JniSecureUtils::convertJavaACLToOCAcl(JNIEnv *env, jobject in, OicSecAcl_t *acl)
140 jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_subject);
141 if (!jData || env->ExceptionCheck())
143 return OC_STACK_ERROR;
146 char *str = (char*) env->GetStringUTFChars(jData, 0);
147 if (OC_STACK_OK == ConvertStrToUuid(str, &acl->subject))
149 env->ReleaseStringUTFChars(jData, str);
153 return OC_STACK_ERROR;
156 jint jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_resources_cnt);
157 if (!jCount || env->ExceptionCheck())
159 return OC_STACK_ERROR;
162 acl->resourcesLen = jCount;
163 acl->resources = new char*[jCount];
164 for (jint i = 0; i < jCount; ++i)
167 jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_resources, args);
168 if (!jData || env->ExceptionCheck())
170 return OC_STACK_ERROR;
173 acl->resources[i] = (char*) env->GetStringUTFChars(jData, 0);
176 jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_permission);
177 if (env->ExceptionCheck())
179 return OC_STACK_ERROR;
182 acl->permission = jCount;
183 jCount = (jint) env->CallIntMethod(in, g_mid_OcOicSecAcl_get_periods_cnt);
184 if (env->ExceptionCheck())
186 return OC_STACK_ERROR;
189 acl->prdRecrLen = jCount;
190 acl->periods = new char*[jCount];
191 for (jint i = 0; i < jCount; ++i)
194 jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_periods, args);
195 if (!jData || env->ExceptionCheck())
197 return OC_STACK_ERROR;
200 acl->periods[i] = (char*) env->GetStringUTFChars(jData, 0);
203 acl->recurrences = new char*[jCount]; //TODO:Period Len and Reccurence len is same
204 for (jint i = 0; i < jCount; ++i)
207 jData = (jstring) env->CallObjectMethodA(in, g_mid_OcOicSecAcl_get_recurrences, args);
208 if (!jData || env->ExceptionCheck())
210 return OC_STACK_ERROR;
213 acl->recurrences[i] = (char*) env->GetStringUTFChars(jData, 0);
216 jData = (jstring) env->CallObjectMethod(in, g_mid_OcOicSecAcl_get_rownerID);
217 if (!jData || env->ExceptionCheck())
219 return OC_STACK_ERROR;
222 str = (char*) env->GetStringUTFChars(jData, 0);
224 if (OC_STACK_OK == ConvertStrToUuid(str, &acl->rownerID))
226 env->ReleaseStringUTFChars(jData, str);
230 return OC_STACK_ERROR;