Merge branch 'security-summit' into 'master'
[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
26 using namespace OC;
27 namespace PH = std::placeholders;
28
29 static JniPinCheckListener *PinListener = nullptr;
30
31 void Callback(char *buf, size_t size)
32 {
33     if (PinListener)
34     {
35         PinListener->PinCallback(buf, size);
36     }
37     else
38     {
39         LOGE("PinListener is null");
40     }
41 }
42
43 /*
44  * Class:     org_iotivity_base_OcProvisioning
45  * Method:    ownershipTransferCDdata
46  * Signature: (I)V
47  */
48 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_ownershipTransferCBdata
49   (JNIEnv *env, jobject thiz, jint OxmType, jobject jListener)
50 {
51     LOGD("OcProvisioning_ownershipTransferCBdata");
52     OCStackResult result = OC_STACK_ERROR;
53
54     try
55     {
56         OTMCallbackData_t CBData = {0};
57         if (OIC_JUST_WORKS == (OicSecOxm_t)OxmType)
58         {
59             CBData.loadSecretCB = LoadSecretJustWorksCallback;
60             CBData.createSecureSessionCB = CreateSecureSessionJustWorksCallback;
61             CBData.createSelectOxmPayloadCB = CreateJustWorksSelectOxmPayload;
62             CBData.createOwnerTransferPayloadCB = CreateJustWorksOwnerTransferPayload;
63
64             result = OCSecure::setOwnerTransferCallbackData((OicSecOxm_t)OxmType,
65                     &CBData, NULL);
66         }
67         if (OIC_RANDOM_DEVICE_PIN == (OicSecOxm_t)OxmType)
68         {
69             if (jListener)
70             {
71                 delete PinListener;
72                 PinListener = new JniPinCheckListener(env, jListener);
73                 CBData.loadSecretCB = InputPinCodeCallback;
74                 CBData.createSecureSessionCB = CreateSecureSessionRandomPinCallbak;
75                 CBData.createSelectOxmPayloadCB = CreatePinBasedSelectOxmPayload;
76                 CBData.createOwnerTransferPayloadCB = CreatePinBasedOwnerTransferPayload;
77                 result = OCSecure::setOwnerTransferCallbackData((OicSecOxm_t)OxmType,
78                         &CBData, Callback);
79             }
80             else
81             {
82                 result = OC_STACK_ERROR;
83             }
84         }
85
86         if (OC_STACK_OK != result)
87         {
88             ThrowOcException(result, "OcProvisioning_ownershipTransferCDdata");
89             return;
90         }
91     }
92     catch (OCException& e)
93     {
94         LOGE("%s", e.reason().c_str());
95         ThrowOcException(e.code(), e.reason().c_str());
96     }
97 }
98
99 /*
100 * Class:     org_iotivity_base_OcProvisioning
101 * Method:    discoverUnownedDevices
102 * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
103 */
104 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverUnownedDevices1
105   (JNIEnv *env, jclass clazz, jint timeout)
106 {
107     LOGI("OcProvisioning_discoverUnownedDevices");
108     DeviceList_t list;
109
110     try
111     {
112         OCStackResult result = OCSecure::discoverUnownedDevices((unsigned short)timeout, list);
113
114         if (OC_STACK_OK != result)
115         {
116             ThrowOcException(result, "Failed to discover Unowned devices");
117             return nullptr;
118         }
119
120         return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
121     }
122     catch (OCException& e)
123     {
124         LOGE("%s", e.reason().c_str());
125         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
126     }
127 }
128
129 /*
130  * Class:     org_iotivity_base_OcProvisioning
131  * Method:    provisionInit
132  * Signature: (Ljava/lang/String;)V
133  */
134 JNIEXPORT void JNICALL Java_org_iotivity_base_OcProvisioning_provisionInit
135   (JNIEnv *env, jclass calzz, jstring jdbPath)
136 {
137     LOGI("OcProvisioning_provisionInit");
138     char *dbpath;
139
140     if (!jdbPath)
141     {
142         ThrowOcException(OC_STACK_INVALID_PARAM, "SVR db path cannot be null");
143         return;
144     }
145
146     try
147     {
148         dbpath = (char*)env->GetStringUTFChars(jdbPath, NULL);
149         OCStackResult result = OCSecure::provisionInit(env->GetStringUTFChars(jdbPath, NULL));
150
151         if (OC_STACK_OK != result)
152         {
153             env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
154             ThrowOcException(result, "Failed to Init Provisioning Manager");
155             return;
156         }
157         env->ReleaseStringUTFChars(jdbPath, (const char*)dbpath);
158     }
159     catch (OCException& e)
160     {
161         LOGE("%s", e.reason().c_str());
162         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
163     }
164 }
165
166 /*
167  * Class:     org_iotivity_base_OcProvisioning
168  * Method:    discoverOwnedDevices
169  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
170  */
171 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverOwnedDevices1
172   (JNIEnv *env, jclass clazz , jint timeout)
173 {
174     LOGI("OcProvisioning_discoverOwnedDevices");
175     DeviceList_t list;
176
177     try
178     {
179         OCStackResult result = OCSecure::discoverOwnedDevices((unsigned short)timeout, list);
180         if (OC_STACK_OK != result)
181         {
182             ThrowOcException(result, "Failed to discover Owned devices");
183             return nullptr;
184         }
185
186         return JniSecureUtils::convertDeviceVectorToJavaArray(env, list);
187     }
188     catch (OCException& e)
189     {
190         LOGE("%s", e.reason().c_str());
191         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
192     }
193 }
194
195 /*
196  * Class:     org_iotivity_base_OcProvisioning
197  * Method:    getDevicestatusLists
198  * Signature: (I)[Lorg/iotivity/base/OcSecureResource;
199  */
200 JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceStatusList1
201   (JNIEnv *env, jclass clazz, jint timeout)
202 {
203     LOGI("OcProvisioning_getDeviceStatusList");
204     DeviceList_t  ownedDevList, unownedDevList;
205
206     try
207     {
208         OCStackResult result = OCSecure::getDevInfoFromNetwork((unsigned short)timeout,
209                 ownedDevList, unownedDevList);
210         if (OC_STACK_OK != result)
211         {
212             ThrowOcException(result, "Failed to get Device Status List");
213             return nullptr;
214         }
215         ownedDevList.insert(ownedDevList.end(), unownedDevList.begin(), unownedDevList.end());
216         return JniSecureUtils::convertDeviceVectorToJavaArray(env, ownedDevList);
217     }
218     catch (OCException& e)
219     {
220         LOGE("%s", e.reason().c_str());
221         ThrowOcException(OC_STACK_ERROR, e.reason().c_str());
222     }
223 }