Remove unnecessary define and files for SSL
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniOcProvisioning.cpp
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Samsung Electronics All Rights Reserved.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
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
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
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.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include "JniOcProvisioning.h"
24 #include "JniPinCheckListener.h"
25 #include "JniDisplayPinListener.h"
26
27 using namespace OC;
28 namespace PH = std::placeholders;
29
30 static JniPinCheckListener *jniPinListener = nullptr;
31 static JniDisplayPinListener *jniDisplayPinListener = nullptr;
32
33 void Callback(char *buf, size_t size)
34 {
35     if (jniPinListener)
36     {
37         jniPinListener->PinCallback(buf, size);
38     }
39     else
40     {
41         LOGE("jniPinListener is null");
42     }
43 }
44
45 void displayPinCB(char *pinBuf, size_t pinSize)
46 {
47     if (jniDisplayPinListener)
48     {
49         jniDisplayPinListener->displayPinCallback(pinBuf, pinSize);
50     }
51     else
52     {
53         LOGE("DisplayPinListener is null");
54     }
55 }
56
57 /*
58  * Class:     org_iotivity_base_OcProvisioning
59  * Method:    ownershipTransferCDdata
60  * Signature: (I)V
61  */
62 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_ownershipTransferCBdata
63   (JNIEnv *env, jobject thiz, jint OxmType, jobject jListener)
64 {
65     LOGD("OcProvisioning_ownershipTransferCBdata");
66     OCStackResult result = OC_STACK_ERROR;
67
68     try
69     {
70         OTMCallbackData_t CBData = {0};
71         if (OIC_JUST_WORKS == (OicSecOxm_t)OxmType)
72         {
73             CBData.loadSecretCB = LoadSecretJustWorksCallback;
74             CBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
75             CBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
76             CBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
77
78             result = OCSecure::setOwnerTransferCallbackData((OicSecOxm_t)OxmType,
79                     &CBData, NULL);
80         }
81         if (OIC_RANDOM_DEVICE_PIN == (OicSecOxm_t)OxmType)
82         {
83             if (jListener)
84             {
85                 delete jniPinListener;
86                 jniPinListener = new JniPinCheckListener(env, jListener);
87                 CBData.loadSecretCB = InputPinCodeCallback;
88                 CBData.createSecureSessionCB = CreateSecureSessionRandomPinCallback;
89                 CBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
90                 CBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
91                 result = OCSecure::setOwnerTransferCallbackData((OicSecOxm_t)OxmType,
92                         &CBData, Callback);
93             }
94             else
95             {
96                 result = OC_STACK_INVALID_CALLBACK;
97             }
98         }
99
100         if (OC_STACK_OK != result)
101         {
102             ThrowOcException(result, "OcProvisioning_ownershipTransferCDdata");
103             return;
104         }
105     }
106     catch (OCException& e)
107     {
108         LOGE("%s", e.reason().c_str());
109         ThrowOcException(e.code(), e.reason().c_str());
110     }
111 }
112
113 /*
114 * Class:     org_iotivity_base_OcProvisioning
115 * Method:    discoverUnownedDevices
116 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
117 */
118 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverUnownedDevices1
119   (JNIEnv *env, jclass clazz, jint timeout)
120 {
121     LOGI("OcProvisioning_discoverUnownedDevices");
122     DeviceList_t list;
123
124     try
125     {
126         if (timeout < 0)
127         {
128             ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
129             return nullptr;
130         }
131         OCStackResult result = OCSecure::discoverUnownedDevices((unsigned short)timeout, list);
132
133         if (OC_STACK_OK != result)
134         {
135             ThrowOcException(result, "Failed to discover Unowned devices");
136             return nullptr;
137         }
138
139         return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
140     }
141     catch (OCException& e)
142     {
143         LOGE("%s", e.reason().c_str());
144         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
145         return nullptr;
146     }
147 }
148
149 /*
150  * Class:     org_iotivity_base_OcProvisioning
151  * Method:    provisionInit
152  * Signature: (Ljava/lang/String;)V
153  */
154 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_provisionInit
155   (JNIEnv *env, jclass calzz, jstring jdbPath)
156 {
157     LOGI("OcProvisioning_provisionInit");
158     char *dbpath;
159
160     if (!jdbPath)
161     {
162         ThrowOcException(OC_STACK_INVALID_PARAM, "SVR db path cannot be null");
163         return;
164     }
165
166     try
167     {
168         dbpath = (char*)env->GetStringUTFChars(jdbPath, NULL);
169         OCStackResult result = OCSecure::provisionInit(env->GetStringUTFChars(jdbPath, NULL));
170
171         if (OC_STACK_OK != result)
172         {
173             env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
174             ThrowOcException(result, "Failed to Init Provisioning Manager");
175             return;
176         }
177         env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
178     }
179     catch (OCException& e)
180     {
181         LOGE("%s", e.reason().c_str());
182         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
183     }
184 }
185
186 /*
187  * Class:     org_iotivity_base_OcProvisioning
188  * Method:    discoverOwnedDevices
189  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
190  */
191 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverOwnedDevices1
192   (JNIEnv *env, jclass clazz , jint timeout)
193 {
194     LOGI("OcProvisioning_discoverOwnedDevices");
195     DeviceList_t list;
196
197     try
198     {
199         if (timeout < 0)
200         {
201             ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
202             return nullptr;
203         }
204         OCStackResult result = OCSecure::discoverOwnedDevices((unsigned short)timeout, list);
205         if (OC_STACK_OK != result)
206         {
207             ThrowOcException(result, "Failed to discover Owned devices");
208             return nullptr;
209         }
210
211         return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
212     }
213     catch (OCException& e)
214     {
215         LOGE("%s", e.reason().c_str());
216         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
217         return nullptr;
218     }
219 }
220
221 /*
222  * Class:     org_iotivity_base_OcProvisioning
223  * Method:    getDevicestatusLists
224  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
225  */
226 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceStatusList1
227   (JNIEnv *env, jclass clazz, jint timeout)
228 {
229     LOGI("OcProvisioning_getDeviceStatusList");
230     DeviceList_t  ownedDevList, unownedDevList;
231
232     try
233     {
234         if (timeout < 0)
235         {
236             ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
237             return nullptr;
238         }
239         OCStackResult result = OCSecure::getDevInfoFromNetwork((unsigned short)timeout,
240                 ownedDevList, unownedDevList);
241         if (OC_STACK_OK != result)
242         {
243             ThrowOcException(result, "Failed to get Device Status List");
244             return nullptr;
245         }
246         ownedDevList.insert(ownedDevList.end(), unownedDevList.begin(), unownedDevList.end());
247         return JniSecureUtils::convertDeviceVectorToJavaArray(env, ownedDevList);
248     }
249     catch (OCException& e)
250     {
251         LOGE("%s", e.reason().c_str());
252         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
253         return nullptr;
254     }
255 }
256
257 /*
258  * Class:     org_iotivity_base_OcProvisioning
259  * Method:    setDisplayPinListener
260  * Signature: (Lorg/iotivity/base/OcProvisioning/DisplayPinListener;)V
261  */
262 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_setDisplayPinListener
263   (JNIEnv *env, jclass thiz, jobject jListener)
264 {
265
266     LOGI("OcProvisioning_setDisplayPinListener");
267
268     if (!jListener)
269     {
270         ThrowOcException(OC_STACK_INVALID_CALLBACK, "displayPinListener can't be null");
271         return;
272     }
273     delete jniDisplayPinListener;
274     jniDisplayPinListener = new JniDisplayPinListener(env, jListener);
275
276     try
277     {
278         OCStackResult result = OCSecure::setDisplayPinCB(displayPinCB);
279         if (OC_STACK_OK != result)
280         {
281             ThrowOcException(result, "Failed to set displayPinListener");
282             return;
283         }
284     }
285     catch (OCException& e)
286     {
287         LOGE("%s", e.reason().c_str());
288         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
289     }
290 }
291 /*
292  * Class:     org_iotivity_base_OcProvisioning
293  * Method:    saveTrustCertChain1
294  * Signature: (Lorg/iotivity/base/OcProvisioning/provisionTrustCertChain1;)V
295  */
296     JNIEXPORT jint JNICALL Java_org_iotivity_base_OcProvisioning_saveTrustCertChain1
297 (JNIEnv *env, jobject thiz, jbyteArray trustCertChain, jint encodingType)
298 {
299     LOGD("OcProvisioning_saveTrustCertChain1");
300 #if defined(__WITH_DTLS__) || defined(__WITH_TLS__)
301     jbyte* trustCertChainBytes = env->GetByteArrayElements(trustCertChain, 0);
302     jsize arrayLength = env->GetArrayLength(trustCertChain);
303     uint16_t credId;
304     unsigned char* trustedCertChar = new unsigned char[arrayLength];
305     try
306     {
307         env->GetByteArrayRegion (trustCertChain, 0, arrayLength, reinterpret_cast<jbyte*>(trustedCertChar));
308         OCStackResult result = OCSecure::saveTrustCertChain((uint8_t*)trustedCertChar, arrayLength,
309                 (OicEncodingType_t)encodingType, &credId);
310         if (OC_STACK_OK != result)
311         {
312             ThrowOcException(result, "OcProvisioning_saveTrustCertChain1");
313             return -1;
314         }
315     }
316     catch (OCException& e)
317     {
318         LOGE("%s", e.reason().c_str());
319         ThrowOcException(e.code(), e.reason().c_str());
320     }
321     return (jint)credId;
322 #else
323     ThrowOcException(OC_STACK_INVALID_PARAM, "WITH_TLS not enabled");
324     return -1;
325 #endif // __WITH_DTLS__ || __WITH_TLS__
326 }