forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMyUserPrompter-JNI.cpp
393 lines (338 loc) · 16.3 KB
/
MyUserPrompter-JNI.cpp
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
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
*
* Copyright (c) 2022 Project CHIP Authors
*
* 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 "MyUserPrompter-JNI.h"
#include <jni.h>
#include <lib/core/CHIPError.h>
#include <lib/support/CHIPJNIError.h>
#include <lib/support/JniReferences.h>
#include <lib/support/JniTypeWrappers.h>
using namespace chip;
JNIMyUserPrompter::JNIMyUserPrompter(jobject provider)
{
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrReturn(env != nullptr, ChipLogError(Zcl, "Failed to GetEnvForCurrentThread for JNIMyUserPrompter"));
VerifyOrReturn(mJNIMyUserPrompterObject.Init(provider) == CHIP_NO_ERROR,
ChipLogError(Zcl, "Failed to init mJNIMyUserPrompterObject"));
jclass JNIMyUserPrompterClass = env->GetObjectClass(provider);
VerifyOrReturn(JNIMyUserPrompterClass != nullptr, ChipLogError(Zcl, "Failed to get JNIMyUserPrompter Java class"));
mPromptForCommissionOKPermissionMethod =
env->GetMethodID(JNIMyUserPrompterClass, "promptForCommissionOkPermission", "(IILjava/lang/String;)V");
if (mPromptForCommissionOKPermissionMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access JNIMyUserPrompter 'promptForCommissionOkPermission' method");
env->ExceptionClear();
}
mPromptForCommissionPincodeMethod =
env->GetMethodID(JNIMyUserPrompterClass, "promptForCommissionPinCode", "(IILjava/lang/String;)V");
if (mPromptForCommissionPincodeMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access JNIMyUserPrompter 'promptForCommissionPinCode' method");
env->ExceptionClear();
}
mHidePromptsOnCancelMethod = env->GetMethodID(JNIMyUserPrompterClass, "hidePromptsOnCancel", "(IILjava/lang/String;)V");
if (mHidePromptsOnCancelMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access JNIMyUserPrompter 'hidePromptsOnCancel' method");
env->ExceptionClear();
}
mPromptWithCommissionerPasscodeMethod =
env->GetMethodID(JNIMyUserPrompterClass, "promptWithCommissionerPasscode", "(IILjava/lang/String;JILjava/lang/String;)V");
if (mPromptWithCommissionerPasscodeMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access JNIMyUserPrompter 'promptWithCommissionerPasscode' method");
env->ExceptionClear();
}
mPromptCommissioningStartedMethod =
env->GetMethodID(JNIMyUserPrompterClass, "promptCommissioningStarted", "(IILjava/lang/String;)V");
if (mPromptCommissioningStartedMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access JNIMyUserPrompter 'promptCommissioningStarted' method");
env->ExceptionClear();
}
mPromptCommissioningSucceededMethod =
env->GetMethodID(JNIMyUserPrompterClass, "promptCommissioningSucceeded", "(IILjava/lang/String;)V");
if (mPromptCommissioningSucceededMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access JNIMyUserPrompter 'promptCommissioningSucceeded' method");
env->ExceptionClear();
}
mPromptCommissioningFailedMethod =
env->GetMethodID(JNIMyUserPrompterClass, "promptCommissioningFailed", "(Ljava/lang/String;Ljava/lang/String;)V");
if (mPromptCommissioningFailedMethod == nullptr)
{
ChipLogError(Zcl, "Failed to access JNIMyUserPrompter 'promptCommissioningFailed' method");
env->ExceptionClear();
}
}
/*
* Called to prompt the user for consent to allow the given commissioneeName/vendorId/productId to be commissioned.
* For example "[commissioneeName] is requesting permission to cast to this TV, approve?"
*
* If user responds with OK then implementor calls UserPrompterResolver.OnPromptAccepted;
* If user responds with Cancel then implementor calls calls UserPrompterResolver.OnPromptDeclined();
*
*/
void JNIMyUserPrompter::PromptForCommissionOKPermission(uint16_t vendorId, uint16_t productId, const char * commissioneeName)
{
DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(mJNIMyUserPrompterObject.HasValidObjectRef(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mPromptForCommissionOKPermissionMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV);
{
jstring jcommissioneeName = env->NewStringUTF(commissioneeName);
VerifyOrExit(jcommissioneeName != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
env->ExceptionClear();
env->CallVoidMethod(mJNIMyUserPrompterObject.ObjectRef(), mPromptForCommissionOKPermissionMethod,
static_cast<jint>(vendorId), static_cast<jint>(productId), jcommissioneeName);
if (env->ExceptionCheck())
{
ChipLogError(DeviceLayer, "Java exception in PromptForCommissionOKPermission");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_ERROR_INCORRECT_STATE;
goto exit;
}
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "PromptForCommissionOKPermission error: %s", err.AsString());
}
}
/*
* Called to prompt the user to enter the setup pincode displayed by the given commissioneeName/vendorId/productId to be
* commissioned. For example "Please enter pin displayed in casting app."
*
* If user responds with OK then implementor calls UserPrompterResolver.OnPinCodeEntered();
* If user responds with Cancel then implementor calls UserPrompterResolver.OnPinCodeDeclined();
*
*/
void JNIMyUserPrompter::PromptForCommissionPasscode(uint16_t vendorId, uint16_t productId, const char * commissioneeName,
uint16_t pairingHint, const char * pairingInstruction)
{
DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(mJNIMyUserPrompterObject.HasValidObjectRef(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mPromptForCommissionPincodeMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV);
{
jstring jcommissioneeName = env->NewStringUTF(commissioneeName);
VerifyOrExit(jcommissioneeName != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
env->ExceptionClear();
env->CallVoidMethod(mJNIMyUserPrompterObject.ObjectRef(), mPromptForCommissionPincodeMethod, static_cast<jint>(vendorId),
static_cast<jint>(productId), jcommissioneeName);
if (env->ExceptionCheck())
{
ChipLogError(Zcl, "Java exception in PromptForCommissionPincode");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_ERROR_INCORRECT_STATE;
goto exit;
}
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "PromptForCommissionPincode error: %s", err.AsString());
}
}
/**
* Called to when CancelCommissioning is received via UDC.
* Indicates that commissioner can stop showing the passcode entry or display dialog.
* For example, can show text such as "Commissioning cancelled by client" before hiding dialog.
*/
void JNIMyUserPrompter::HidePromptsOnCancel(uint16_t vendorId, uint16_t productId, const char * commissioneeName)
{
ChipLogError(Zcl, "JNIMyUserPrompter::HidePromptsOnCancel");
DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(mJNIMyUserPrompterObject.HasValidObjectRef(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mPromptForCommissionOKPermissionMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV);
{
jstring jcommissioneeName = env->NewStringUTF(commissioneeName);
VerifyOrExit(jcommissioneeName != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
env->ExceptionClear();
env->CallVoidMethod(mJNIMyUserPrompterObject.ObjectRef(), mHidePromptsOnCancelMethod, static_cast<jint>(vendorId),
static_cast<jint>(productId), jcommissioneeName);
if (env->ExceptionCheck())
{
ChipLogError(DeviceLayer, "Java exception in HidePromptsOnCancel");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_ERROR_INCORRECT_STATE;
goto exit;
}
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "HidePromptsOnCancel error: %s", err.AsString());
}
}
/**
* Return true if this UserPrompter displays QR code along with passcode
* When PromptWithCommissionerPasscode is called during Commissioner Passcode functionality.
*/
bool JNIMyUserPrompter::DisplaysPasscodeAndQRCode()
{
ChipLogError(Zcl, "JNIMyUserPrompter::DisplaysPasscodeAndQRCode Needs Implementation");
return false;
}
/**
* Called to display the given setup passcode to the user,
* for commissioning the given commissioneeName with the given vendorId and productId,
* and provide instructions for where to enter it in the commissionee (when pairingHint and pairingInstruction are provided).
* For example "Casting Passcode: [passcode]. For more instructions, click here."
*/
void JNIMyUserPrompter::PromptWithCommissionerPasscode(uint16_t vendorId, uint16_t productId, const char * commissioneeName,
uint32_t passcode, uint16_t pairingHint, const char * pairingInstruction)
{
ChipLogError(Zcl, "JNIMyUserPrompter::PromptWithCommissionerPasscode");
DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(mJNIMyUserPrompterObject.HasValidObjectRef(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mPromptForCommissionOKPermissionMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV);
{
jstring jcommissioneeName = env->NewStringUTF(commissioneeName);
VerifyOrExit(jcommissioneeName != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
jstring jpairingInstruction = env->NewStringUTF(pairingInstruction);
VerifyOrExit(jpairingInstruction != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
env->ExceptionClear();
env->CallVoidMethod(mJNIMyUserPrompterObject.ObjectRef(), mPromptWithCommissionerPasscodeMethod,
static_cast<jint>(vendorId), static_cast<jint>(productId), jcommissioneeName,
static_cast<jlong>(passcode), static_cast<jint>(pairingHint), jpairingInstruction);
if (env->ExceptionCheck())
{
ChipLogError(DeviceLayer, "Java exception in PromptWithCommissionerPasscode");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_ERROR_INCORRECT_STATE;
goto exit;
}
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "PromptWithCommissionerPasscode error: %s", err.AsString());
}
}
/**
* Called to alert the user that commissioning has begun."
*/
void JNIMyUserPrompter::PromptCommissioningStarted(uint16_t vendorId, uint16_t productId, const char * commissioneeName)
{
ChipLogError(Zcl, "JNIMyUserPrompter::PromptCommissioningStarted");
DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(mJNIMyUserPrompterObject.HasValidObjectRef(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mPromptForCommissionOKPermissionMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV);
{
jstring jcommissioneeName = env->NewStringUTF(commissioneeName);
VerifyOrExit(jcommissioneeName != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
env->ExceptionClear();
env->CallVoidMethod(mJNIMyUserPrompterObject.ObjectRef(), mPromptCommissioningStartedMethod, static_cast<jint>(vendorId),
static_cast<jint>(productId), jcommissioneeName);
if (env->ExceptionCheck())
{
ChipLogError(DeviceLayer, "Java exception in PromptCommissioningStarted");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_ERROR_INCORRECT_STATE;
goto exit;
}
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "PromptCommissioningStarted error: %s", err.AsString());
}
}
/*
* Called to notify the user that commissioning succeeded. It can be in form of UI Notification.
*/
void JNIMyUserPrompter::PromptCommissioningSucceeded(uint16_t vendorId, uint16_t productId, const char * commissioneeName)
{
DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(mJNIMyUserPrompterObject.HasValidObjectRef(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mPromptCommissioningSucceededMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV);
{
jstring jcommissioneeName = env->NewStringUTF(commissioneeName);
VerifyOrExit(jcommissioneeName != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
env->ExceptionClear();
env->CallVoidMethod(mJNIMyUserPrompterObject.ObjectRef(), mPromptCommissioningSucceededMethod, static_cast<jint>(vendorId),
static_cast<jint>(productId), jcommissioneeName);
if (env->ExceptionCheck())
{
ChipLogError(Zcl, "Java exception in PromptCommissioningSucceeded");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_ERROR_INCORRECT_STATE;
goto exit;
}
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "PromptCommissioningSucceeded error: %s", err.AsString());
}
}
/*
* Called to notify the user that commissioning failed. It can be in form of UI Notification.
*/
void JNIMyUserPrompter::PromptCommissioningFailed(const char * commissioneeName, CHIP_ERROR error)
{
DeviceLayer::StackUnlock unlock;
CHIP_ERROR err = CHIP_NO_ERROR;
JNIEnv * env = JniReferences::GetInstance().GetEnvForCurrentThread();
VerifyOrExit(mJNIMyUserPrompterObject.HasValidObjectRef(), err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(mPromptCommissioningFailedMethod != nullptr, err = CHIP_ERROR_INCORRECT_STATE);
VerifyOrExit(env != nullptr, err = CHIP_JNI_ERROR_NO_ENV);
{
jstring jcommissioneeError = env->NewStringUTF(error.AsString());
VerifyOrExit(jcommissioneeError != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
jstring jcommissioneeName = env->NewStringUTF(commissioneeName);
VerifyOrExit(jcommissioneeName != nullptr, ChipLogError(Zcl, "Could not create jstring"); err = CHIP_ERROR_INTERNAL);
env->ExceptionClear();
env->CallVoidMethod(mJNIMyUserPrompterObject.ObjectRef(), mPromptCommissioningFailedMethod, jcommissioneeName,
jcommissioneeError);
if (env->ExceptionCheck())
{
ChipLogError(Zcl, "Java exception in PromptCommissioningFailed");
env->ExceptionDescribe();
env->ExceptionClear();
err = CHIP_ERROR_INCORRECT_STATE;
goto exit;
}
}
exit:
if (err != CHIP_NO_ERROR)
{
ChipLogError(Zcl, "PromptCommissioningFailed error: %s", err.AsString());
}
}