-
Notifications
You must be signed in to change notification settings - Fork 45
/
Copy pathmain.cpp
executable file
·2148 lines (1698 loc) · 79.2 KB
/
main.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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) AC SOFTWARE SP. Z O.O.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
/*
https://docs.oracle.com/javase/7/docs/technotes/guides/jni/spec/types.html
Type Signature | Java Type
Z | boolean
B | byte
C | char
S | short
I | int
J | long
F | float
D | double
*/
#include <android/log.h>
#include <jni.h>
#include <stdlib.h>
#include <string.h>
#include "actions.h"
#include "channel_config.h"
#include "device_config.h"
#include "proto.h"
#include "srpc.h"
#include "supla-client.h"
#include "supla.h"
// __android_log_write(ANDROID_LOG_DEBUG, log_tag, "XXXX");
#define JNI_FUNCTION_V(jni_function_suffix, supla_client_function) \
extern "C" JNIEXPORT jboolean JNICALL \
Java_org_supla_android_lib_SuplaClient_##jni_function_suffix( \
JNIEnv *env, jobject thiz, jlong _asc) { \
void *supla_client = supla_client_ptr(_asc); \
if (supla_client) { \
return supla_client_function(supla_client) ? JNI_TRUE : JNI_FALSE; \
} \
return JNI_FALSE; \
}
#define JNI_FUNCTION_I(jni_function_suffix, supla_client_function) \
extern "C" JNIEXPORT jboolean JNICALL \
Java_org_supla_android_lib_SuplaClient_##jni_function_suffix( \
JNIEnv *env, jobject thiz, jlong _asc, jint i1) { \
void *supla_client = supla_client_ptr(_asc); \
if (supla_client) { \
return supla_client_function(supla_client, i1) ? JNI_TRUE : JNI_FALSE; \
} \
return JNI_FALSE; \
}
#define JNI_FUNCTION_II(jni_function_suffix, supla_client_function) \
extern "C" JNIEXPORT jboolean JNICALL \
Java_org_supla_android_lib_SuplaClient_##jni_function_suffix( \
JNIEnv *env, jobject thiz, jlong _asc, jint i1, jint i2) { \
void *supla_client = supla_client_ptr(_asc); \
if (supla_client) { \
return supla_client_function(supla_client, i1, i2) ? JNI_TRUE \
: JNI_FALSE; \
} \
return JNI_FALSE; \
}
#define JNI_FUNCTION_IString(jni_function_suffix, supla_client_function) \
extern "C" JNIEXPORT jboolean JNICALL \
Java_org_supla_android_lib_SuplaClient_##jni_function_suffix( \
JNIEnv *env, jobject thiz, jlong _asc, jint i1, jstring s1) { \
jboolean result = JNI_FALSE; \
const char *str = env->GetStringUTFChars(s1, 0); \
if (str) { \
void *supla_client = supla_client_ptr(_asc); \
if (supla_client) { \
result = supla_client_function(supla_client, i1, str) ? JNI_TRUE \
: JNI_FALSE; \
} \
env->ReleaseStringUTFChars(s1, str); \
} \
return result; \
}
#define JNI_CALLBACK_I(jni_function_cb) \
void supla_cb_##jni_function_cb(void *_suplaclient, void *user_data, \
_supla_int_t i1) { \
ASC_VAR_DECLARATION(); \
ENV_VAR_DECLARATION(); \
if (asc->j_mid_##jni_function_cb == NULL) { \
return; \
} \
env->CallVoidMethod(asc->j_obj, asc->j_mid_##jni_function_cb, i1); \
}
#define JNI_CALLBACK_STRING(jni_function_cb) \
void supla_cb_##jni_function_cb(void *_suplaclient, void *user_data, \
char *str) { \
ASC_VAR_DECLARATION(); \
ENV_VAR_DECLARATION(); \
if (asc->j_mid_##jni_function_cb == NULL) { \
return; \
} \
env->CallVoidMethod(asc->j_obj, asc->j_mid_##jni_function_cb, \
str ? new_string_utf(env, str) : NULL); \
}
jfieldID supla_client_GetFieldID(JNIEnv *env, jclass c, const char *name,
const char *type) {
jfieldID fid;
fid = env->GetFieldID(c, name, type);
if (fid == NULL) {
__android_log_print(ANDROID_LOG_ERROR, log_tag,
"Unknown field name: %s type: %s", name, type);
}
return fid;
}
void supla_android_client(TAndroidSuplaClient *asc, jmethodID mid,
jobject obj) {
ENV_VAR_DECLARATION();
if (obj) {
env->CallVoidMethod(asc->j_obj, mid, obj);
} else {
env->CallVoidMethod(asc->j_obj, mid);
}
}
void supla_stringobj2buffer(JNIEnv *env, jobject cfg, jclass jcs,
const char *name, char *buff, int size) {
jfieldID fid = supla_client_GetFieldID(env, jcs, name, "Ljava/lang/String;");
jstring jstr = (jstring)env->GetObjectField(cfg, fid);
supla_GetStringUtfChars(env, jstr, buff, size);
}
void supla_bytearrobj2buffer(JNIEnv *env, jobject cfg, jclass jcs,
const char *name, char *buff, int size) {
jfieldID fid = supla_client_GetFieldID(env, jcs, name, "[B");
jbyteArray barr = (jbyteArray)env->GetObjectField(cfg, fid);
supla_GetByteArrayElements(env, barr, buff, size);
}
unsigned char supla_shortobj2jshort(JNIEnv *env, jobject in, jshort *out) {
if (env == NULL || in == NULL || out == NULL) {
return 0;
}
jclass in_cls = env->GetObjectClass(in);
jmethodID m_get_short_val = env->GetMethodID(in_cls, "shortValue", "()S");
if (m_get_short_val == NULL) {
return 0;
}
*out = env->CallShortMethod(in, m_get_short_val);
return 1;
}
jobject supla_jshort2shortobj(JNIEnv *env, jshort in) {
jclass cls = env->FindClass("java/lang/Short");
jmethodID m_init = env->GetMethodID(cls, "<init>", "(S)V");
if (NULL == m_init) {
return NULL;
};
return env->NewObject(cls, m_init, in);
}
jobject supla_jint2integerobj(JNIEnv *env, jint in) {
jclass cls = env->FindClass("java/lang/Integer");
jmethodID m_init = env->GetMethodID(cls, "<init>", "(I)V");
if (NULL == m_init) {
return NULL;
};
return env->NewObject(cls, m_init, in);
}
void supla_cb_on_versionerror(void *_suplaclient, void *user_data, int version,
int remote_version_min, int remote_version) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_on_versionerror) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaVersionError");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject rerr = env->NewObject(cls, methodID);
jclass crerr = env->GetObjectClass(rerr);
fid = supla_client_GetFieldID(env, crerr, "Version", "I");
env->SetIntField(rerr, fid, version);
fid = supla_client_GetFieldID(env, crerr, "RemoteVersionMin", "I");
env->SetIntField(rerr, fid, remote_version_min);
fid = supla_client_GetFieldID(env, crerr, "RemoteVersion", "I");
env->SetIntField(rerr, fid, remote_version);
supla_android_client(asc, asc->j_mid_on_versionerror, rerr);
}
}
void supla_cb_on_connected(void *_suplaclient, void *user_data) {
ASC_VAR_DECLARATION();
if (asc->j_mid_on_connected)
supla_android_client(asc, asc->j_mid_on_connected, NULL);
}
void supla_cb_on_connerror(void *_suplaclient, void *user_data, int code) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_on_connerror) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaConnError");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject cerr = env->NewObject(cls, methodID);
jclass ccerr = env->GetObjectClass(cerr);
fid = supla_client_GetFieldID(env, ccerr, "Code", "I");
env->SetIntField(cerr, fid, code);
supla_android_client(asc, asc->j_mid_on_connerror, cerr);
}
}
void supla_cb_on_disconnected(void *_suplaclient, void *user_data) {
ASC_VAR_DECLARATION();
if (asc->j_mid_on_disconnected)
supla_android_client(asc, asc->j_mid_on_disconnected, NULL);
}
void supla_cb_on_registering(void *_suplaclient, void *user_data) {
ASC_VAR_DECLARATION();
if (asc->j_mid_on_registering)
supla_android_client(asc, asc->j_mid_on_registering, NULL);
}
void supla_cb_on_registered(void *_suplaclient, void *user_data,
TSC_SuplaRegisterClientResult_D *result) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_on_registered) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaRegisterResult");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject reg = env->NewObject(cls, methodID);
jclass creg = env->GetObjectClass(reg);
fid = supla_client_GetFieldID(env, creg, "ResultCode", "I");
env->SetIntField(reg, fid, result->result_code);
fid = supla_client_GetFieldID(env, creg, "ClientID", "I");
env->SetIntField(reg, fid, result->ClientID);
fid = supla_client_GetFieldID(env, creg, "LocationCount", "I");
env->SetIntField(reg, fid, result->LocationCount);
fid = supla_client_GetFieldID(env, creg, "ChannelCount", "I");
env->SetIntField(reg, fid, result->ChannelCount);
fid = supla_client_GetFieldID(env, creg, "ChannelGroupCount", "I");
env->SetIntField(reg, fid, result->ChannelGroupCount);
fid = supla_client_GetFieldID(env, creg, "SceneCount", "I");
env->SetIntField(reg, fid, result->SceneCount);
fid = supla_client_GetFieldID(env, creg, "Flags", "I");
env->SetIntField(reg, fid, result->Flags);
fid = supla_client_GetFieldID(env, creg, "ActivityTimeout", "I");
env->SetIntField(reg, fid, result->activity_timeout);
fid = supla_client_GetFieldID(env, creg, "Version", "I");
env->SetIntField(reg, fid, result->version);
fid = supla_client_GetFieldID(env, creg, "VersionMin", "I");
env->SetIntField(reg, fid, result->version_min);
supla_android_client(asc, asc->j_mid_on_registered, reg);
}
}
void supla_cb_on_registererror(void *_suplaclient, void *user_data, int code) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_on_registererror) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaRegisterError");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject rerr = env->NewObject(cls, methodID);
jclass crerr = env->GetObjectClass(rerr);
fid = supla_client_GetFieldID(env, crerr, "ResultCode", "I");
env->SetIntField(rerr, fid, code);
supla_android_client(asc, asc->j_mid_on_registererror, rerr);
}
}
void supla_cb_location_update(void *_suplaclient, void *user_data,
TSC_SuplaLocation *location) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_location_update) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaLocation");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject loc = env->NewObject(cls, methodID);
jclass cloc = env->GetObjectClass(loc);
fid = supla_client_GetFieldID(env, cloc, "EOL", "Z");
env->SetBooleanField(loc, fid, location->EOL == 1 ? JNI_TRUE : JNI_FALSE);
fid = supla_client_GetFieldID(env, cloc, "Id", "I");
env->SetIntField(loc, fid, location->Id);
fid = supla_client_GetFieldID(env, cloc, "Caption", "Ljava/lang/String;");
env->SetObjectField(loc, fid, new_string_utf(env, location->Caption));
supla_android_client(asc, asc->j_mid_location_update, loc);
}
}
jobject supla_channelvalue_to_jobject(void *_suplaclient, void *user_data,
TSuplaChannelValue_B *channel_value) {
ASC_VAR_DECLARATION(NULL);
ENV_VAR_DECLARATION(NULL);
jfieldID fid;
jclass cls = env->FindClass("org/supla/android/lib/SuplaChannelValue");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject val = env->NewObject(cls, methodID);
jclass cval = env->GetObjectClass(val);
jbyteArray arr = env->NewByteArray(SUPLA_CHANNELVALUE_SIZE);
env->SetByteArrayRegion(arr, 0, SUPLA_CHANNELVALUE_SIZE,
(const jbyte *)channel_value->value);
fid = supla_client_GetFieldID(env, cval, "Value", "[B");
env->SetObjectField(val, fid, arr);
arr = env->NewByteArray(SUPLA_CHANNELVALUE_SIZE);
env->SetByteArrayRegion(arr, 0, SUPLA_CHANNELVALUE_SIZE,
(const jbyte *)channel_value->sub_value);
fid = supla_client_GetFieldID(env, cval, "SubValue", "[B");
env->SetObjectField(val, fid, arr);
fid = supla_client_GetFieldID(env, cval, "SubValueType", "S");
env->SetShortField(val, fid, channel_value->sub_value_type);
return val;
}
void supla_cb_channel_update(void *_suplaclient, void *user_data,
TSC_SuplaChannel_E *channel) {
// int a;
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
// for(a=0;a<SUPLA_CHANNELVALUE_SIZE;a++)
// __android_log_print(ANDROID_LOG_ERROR, log_tag, "Channel %i, Value[%i]=%i",
// channel->Id, a, channel->value.value[a]);
if (asc->j_mid_channel_update) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaChannel");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject ch = env->NewObject(cls, methodID);
jclass cch = env->GetObjectClass(ch);
fid = supla_client_GetFieldID(env, cch, "EOL", "Z");
env->SetBooleanField(ch, fid, channel->EOL == 1 ? JNI_TRUE : JNI_FALSE);
fid = supla_client_GetFieldID(env, cch, "Id", "I");
env->SetIntField(ch, fid, channel->Id);
fid = supla_client_GetFieldID(env, cch, "DeviceID", "I");
env->SetIntField(ch, fid, channel->DeviceID);
fid = supla_client_GetFieldID(env, cch, "LocationID", "I");
env->SetIntField(ch, fid, channel->LocationID);
fid = supla_client_GetFieldID(env, cch, "Type", "I");
env->SetIntField(ch, fid, channel->Type);
fid = supla_client_GetFieldID(env, cch, "Func", "I");
env->SetIntField(ch, fid, channel->Func);
fid = supla_client_GetFieldID(env, cch, "ManufacturerID", "S");
env->SetShortField(ch, fid, channel->ManufacturerID);
fid = supla_client_GetFieldID(env, cch, "ProductID", "S");
env->SetShortField(ch, fid, channel->ProductID);
fid = supla_client_GetFieldID(env, cch, "OnLine", "Z");
env->SetBooleanField(ch, fid, channel->online == 1 ? JNI_TRUE : JNI_FALSE);
fid = supla_client_GetFieldID(env, cch, "Value",
"Lorg/supla/android/lib/SuplaChannelValue;");
jobject chv =
supla_channelvalue_to_jobject(_suplaclient, user_data, &channel->value);
env->SetObjectField(ch, fid, chv);
fid = supla_client_GetFieldID(env, cch, "Caption", "Ljava/lang/String;");
env->SetObjectField(ch, fid, new_string_utf(env, channel->Caption));
fid = supla_client_GetFieldID(env, cch, "AltIcon", "I");
env->SetIntField(ch, fid, channel->AltIcon);
fid = supla_client_GetFieldID(env, cch, "UserIcon", "I");
env->SetIntField(ch, fid, channel->UserIcon);
fid = supla_client_GetFieldID(env, cch, "Flags", "J");
env->SetLongField(ch, fid, channel->Flags);
fid = supla_client_GetFieldID(env, cch, "ProtocolVersion", "I");
env->SetIntField(ch, fid, channel->ProtocolVersion);
fid = supla_client_GetFieldID(env, cch, "DefaultConfigCRC32", "J");
env->SetLongField(ch, fid, channel->DefaultConfigCRC32);
supla_android_client(asc, asc->j_mid_channel_update, ch);
}
}
void supla_cb_channel_value_update(void *_suplaclient, void *user_data,
TSC_SuplaChannelValue_B *channel_value) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_channel_value_update) {
jclass cls =
env->FindClass("org/supla/android/lib/SuplaChannelValueUpdate");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject val = env->NewObject(cls, methodID);
jclass cval = env->GetObjectClass(val);
fid = supla_client_GetFieldID(env, cval, "EOL", "Z");
env->SetBooleanField(val, fid,
channel_value->EOL == 1 ? JNI_TRUE : JNI_FALSE);
fid = supla_client_GetFieldID(env, cval, "Id", "I");
env->SetIntField(val, fid, channel_value->Id);
fid = supla_client_GetFieldID(env, cval, "OnLine", "Z");
env->SetBooleanField(val, fid,
channel_value->online == 1 ? JNI_TRUE : JNI_FALSE);
fid = supla_client_GetFieldID(env, cval, "Value",
"Lorg/supla/android/lib/SuplaChannelValue;");
jobject chv = supla_channelvalue_to_jobject(_suplaclient, user_data,
&channel_value->value);
env->SetObjectField(val, fid, chv);
supla_android_client(asc, asc->j_mid_channel_value_update, val);
}
}
void supla_channel_em_addsummary(TAndroidSuplaClient *asc, JNIEnv *env,
jobject parent, jclass parent_cls,
TElectricityMeter_ExtendedValue_V3 *em_ev,
jint phase) {
jclass cls = env->FindClass(
"org/supla/android/lib/SuplaChannelElectricityMeterValue$Summary");
jmethodID methodID = env->GetMethodID(
cls, "<init>",
"(Lorg/supla/android/lib/SuplaChannelElectricityMeterValue;JJJJ)V");
jlong fae = em_ev->total_forward_active_energy[phase];
jlong rae = em_ev->total_reverse_active_energy[phase];
jlong fre = em_ev->total_forward_reactive_energy[phase];
jlong rre = em_ev->total_reverse_reactive_energy[phase];
jobject sum_obj = env->NewObject(cls, methodID, parent, fae, rae, fre, rre);
jmethodID add_summary_mid = env->GetMethodID(
parent_cls, "addSummary",
"(ILorg/supla/android/lib/SuplaChannelElectricityMeterValue$Summary;)V");
phase += 1;
env->CallVoidMethod(parent, add_summary_mid, phase, sum_obj);
}
void supla_channel_em_addmeasurement(TAndroidSuplaClient *asc, JNIEnv *env,
jobject parent, jclass parent_cls,
TElectricityMeter_ExtendedValue_V3 *em_ev,
jint phase, int midx) {
jclass cls = env->FindClass(
"org/supla/android/lib/SuplaChannelElectricityMeterValue$Measurement");
jmethodID methodID = env->GetMethodID(
cls, "<init>",
"(Lorg/supla/android/lib/SuplaChannelElectricityMeterValue;IIIIIIII)V");
jobject m_obj = env->NewObject(
cls, methodID, parent, em_ev->m[midx].freq, em_ev->m[midx].voltage[phase],
em_ev->m[midx].current[phase], em_ev->m[midx].power_active[phase],
em_ev->m[midx].power_reactive[phase],
em_ev->m[midx].power_apparent[phase], em_ev->m[midx].power_factor[phase],
em_ev->m[midx].phase_angle[phase]);
jmethodID add_m_mid =
env->GetMethodID(parent_cls, "addMeasurement",
"(ILorg/supla/android/lib/"
"SuplaChannelElectricityMeterValue$Measurement;)V");
phase += 1;
env->CallVoidMethod(parent, add_m_mid, phase, m_obj);
}
jobject supla_channelelectricitymetervalue_to_jobject(
TAndroidSuplaClient *asc, JNIEnv *env,
TElectricityMeter_ExtendedValue_V3 *em_ev) {
int a = 0;
int b = 0;
char currency[4];
memcpy(currency, em_ev->currency, 3);
currency[sizeof(currency) - 1] = 0;
jlong fae_b = em_ev->total_forward_active_energy_balanced;
jlong rae_b = em_ev->total_reverse_active_energy_balanced;
jint voltage_phase_angle_12 = em_ev->voltage_phase_angle_12;
jint voltage_phase_angle_13 = em_ev->voltage_phase_angle_13;
jint phase_sequence = em_ev->phase_sequence;
jclass cls =
env->FindClass("org/supla/android/lib/SuplaChannelElectricityMeterValue");
jmethodID methodID =
env->GetMethodID(cls, "<init>", "(IIIILjava/lang/String;JJ)V");
jobject val = env->NewObject(
cls, methodID, em_ev->measured_values, voltage_phase_angle_12,
voltage_phase_angle_13, phase_sequence, em_ev->period, em_ev->total_cost,
em_ev->price_per_unit, new_string_utf(env, currency), fae_b, rae_b);
for (a = 0; a < 3; a++) {
supla_channel_em_addsummary(asc, env, val, cls, em_ev, a);
for (b = 0; b < em_ev->m_count; b++) {
supla_channel_em_addmeasurement(asc, env, val, cls, em_ev, a, b);
}
}
return val;
}
jobject supla_impulsecountervalue_to_jobject(
TAndroidSuplaClient *asc, JNIEnv *env,
TSC_ImpulseCounter_ExtendedValue *ic_ev) {
jclass cls =
env->FindClass("org/supla/android/lib/SuplaChannelImpulseCounterValue");
jmethodID methodID = env->GetMethodID(
cls, "<init>", "(IJJIILjava/lang/String;Ljava/lang/String;)V");
char currency[4];
memcpy(currency, ic_ev->currency, 3);
currency[sizeof(currency) - 1] = 0;
ic_ev->custom_unit[sizeof(ic_ev->custom_unit) - 1] = 0;
jlong counter = ic_ev->counter;
jlong calculated_value = ic_ev->calculated_value;
return env->NewObject(cls, methodID, ic_ev->impulses_per_unit, counter,
calculated_value, ic_ev->total_cost,
ic_ev->price_per_unit, new_string_utf(env, currency),
new_string_utf(env, ic_ev->custom_unit));
}
jobject supla_thermostatvalue_to_jobject(TAndroidSuplaClient *asc, JNIEnv *env,
TThermostat_ExtendedValue *th_ev) {
jclass cls =
env->FindClass("org/supla/android/lib/SuplaChannelThermostatValue");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject m_obj = env->NewObject(cls, methodID);
int a;
if (th_ev->Fields & THERMOSTAT_FIELD_MeasuredTemperatures) {
jmethodID set_measured_temperature_m_mid =
env->GetMethodID(cls, "setMeasuredTemperature", "(ID)Z");
for (a = 0; a < sizeof(th_ev->MeasuredTemperature) / sizeof(_supla_int16_t);
a++) {
env->CallBooleanMethod(m_obj, set_measured_temperature_m_mid, a,
th_ev->MeasuredTemperature[a] / 100.0);
}
}
if (th_ev->Fields & THERMOSTAT_FIELD_PresetTemperatures) {
jmethodID set_preset_temperature_m_mid =
env->GetMethodID(cls, "setPresetTemperature", "(ID)Z");
for (a = 0; a < sizeof(th_ev->PresetTemperature) / sizeof(_supla_int16_t);
a++) {
env->CallBooleanMethod(m_obj, set_preset_temperature_m_mid, a,
th_ev->PresetTemperature[a] / 100.0);
}
}
if (th_ev->Fields & THERMOSTAT_FIELD_Flags) {
jmethodID set_flags_m_mid = env->GetMethodID(cls, "setFlags", "(II)Z");
for (a = 0; a < sizeof(th_ev->Flags) / sizeof(_supla_int16_t); a++) {
env->CallBooleanMethod(m_obj, set_flags_m_mid, a, th_ev->Flags[a]);
}
}
if (th_ev->Fields & THERMOSTAT_FIELD_Values) {
jmethodID set_values_m_mid = env->GetMethodID(cls, "setValues", "(II)Z");
for (a = 0; a < sizeof(th_ev->Values) / sizeof(_supla_int16_t); a++) {
env->CallBooleanMethod(m_obj, set_values_m_mid, a, th_ev->Values[a]);
}
}
if (th_ev->Fields & THERMOSTAT_FIELD_Time) {
jmethodID set_time_m_mid = env->GetMethodID(cls, "setTime", "(BBBB)V");
for (a = 0; a < sizeof(th_ev->Values) / sizeof(_supla_int16_t); a++) {
env->CallVoidMethod(m_obj, set_time_m_mid, (jbyte)th_ev->Time.sec,
(jbyte)th_ev->Time.min, (jbyte)th_ev->Time.hour,
(jbyte)th_ev->Time.dayOfWeek);
}
}
if (th_ev->Fields & THERMOSTAT_FIELD_Schedule) {
jmethodID set_schedule_vtype_m_mid =
env->GetMethodID(cls, "setScheduleValueType", "(B)V");
env->CallVoidMethod(m_obj, set_schedule_vtype_m_mid,
th_ev->Schedule.ValueType);
jmethodID set_schedule_value_m_mid =
env->GetMethodID(cls, "setScheduleValue", "(BBB)V");
for (a = 0; a < 7; a++) {
for (int b = 0; b < 24; b++) {
env->CallVoidMethod(m_obj, set_schedule_value_m_mid, (jbyte)a, (jbyte)b,
(jbyte)th_ev->Schedule.HourValue[a][b]);
}
}
}
return m_obj;
}
jobject supla_channelstate_to_jobject(TAndroidSuplaClient *asc, JNIEnv *env,
TDSC_ChannelState *state) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaChannelState");
jmethodID methodID = env->GetMethodID(cls, "<init>", "(IIII[BBBBBBBIIBBII)V");
jbyteArray mac = env->NewByteArray(6);
env->SetByteArrayRegion(mac, 0, 6, (const jbyte *)state->MAC);
return env->NewObject(
cls, methodID, (jint)state->ChannelID, (jint)state->Fields,
(jint)state->defaultIconField, (jint)state->IPv4, mac,
(jbyte)state->BatteryLevel, (jbyte)state->BatteryPowered,
(jbyte)state->WiFiRSSI, (jbyte)state->WiFiSignalStrength,
(jbyte)state->BridgeNodeOnline, (jbyte)state->BridgeNodeSignalStrength,
(jint)state->Uptime, (jint)state->ConnectionUptime,
(jbyte)state->BatteryHealth, (jbyte)state->LastConnectionResetCause,
(jint)state->LightSourceLifespan,
state->Fields & SUPLA_CHANNELSTATE_FIELD_LIGHTSOURCEOPERATINGTIME
? (jint)state->LightSourceOperatingTime
: (jshort)state->LightSourceLifespanLeft);
}
jobject supla_timerstate_to_jobject(TAndroidSuplaClient *asc, JNIEnv *env,
TTimerState_ExtendedValue *state) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaTimerState");
jmethodID methodID =
env->GetMethodID(cls, "<init>", "(J[BILjava/lang/String;)V");
supla_client_set_str(state->SenderName, &state->SenderNameSize,
SUPLA_SENDER_NAME_MAXSIZE);
jbyteArray arr = env->NewByteArray(SUPLA_CHANNELVALUE_SIZE);
env->SetByteArrayRegion(arr, 0, SUPLA_CHANNELVALUE_SIZE,
(const jbyte *)state->TargetValue);
jlong countdownEndsAt = (jlong)state->CountdownEndsAt +
supla_client_get_time_diff(asc->_supla_client);
return env->NewObject(cls, methodID, countdownEndsAt, arr, state->SenderID,
new_string_utf(env, state->SenderName));
}
void supla_channelextendedvalue_set_object(
void *_suplaclient, void *user_data,
TSuplaChannelExtendedValue *channel_extendedvalue, jobject obj) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
jclass cls = env->GetObjectClass(obj);
TChannelState_ExtendedValue *channel_state = NULL;
TTimerState_ExtendedValue *timer_state = NULL;
if (channel_extendedvalue->type == EV_TYPE_ELECTRICITY_METER_MEASUREMENT_V2 ||
channel_extendedvalue->type == EV_TYPE_ELECTRICITY_METER_MEASUREMENT_V3) {
TElectricityMeter_ExtendedValue_V3 em_ev = {};
if (srpc_evtool_extended2emextended_latest(channel_extendedvalue, &em_ev)) {
fid = supla_client_GetFieldID(
env, cls, "ElectricityMeterValue",
"Lorg/supla/android/lib/SuplaChannelElectricityMeterValue;");
jobject chv =
supla_channelelectricitymetervalue_to_jobject(asc, env, &em_ev);
env->SetObjectField(obj, fid, chv);
}
} else if (channel_extendedvalue->type ==
EV_TYPE_IMPULSE_COUNTER_DETAILS_V1) {
TSC_ImpulseCounter_ExtendedValue ic_ev;
if (srpc_evtool_v1_extended2icextended(channel_extendedvalue, &ic_ev) ==
1) {
fid = supla_client_GetFieldID(
env, cls, "ImpulseCounterValue",
"Lorg/supla/android/lib/SuplaChannelImpulseCounterValue;");
jobject chv = supla_impulsecountervalue_to_jobject(asc, env, &ic_ev);
env->SetObjectField(obj, fid, chv);
}
} else if (channel_extendedvalue->type == EV_TYPE_THERMOSTAT_DETAILS_V1) {
TThermostat_ExtendedValue th_ev;
if (srpc_evtool_v1_extended2thermostatextended(channel_extendedvalue,
&th_ev) == 1) {
fid = supla_client_GetFieldID(
env, cls, "ThermostatValue",
"Lorg/supla/android/lib/SuplaChannelThermostatValue;");
jobject chv = supla_thermostatvalue_to_jobject(asc, env, &th_ev);
env->SetObjectField(obj, fid, chv);
}
} else if (channel_extendedvalue->type == EV_TYPE_CHANNEL_STATE_V1 &&
channel_extendedvalue->size ==
sizeof(TChannelState_ExtendedValue)) {
channel_state = (TChannelState_ExtendedValue *)channel_extendedvalue->value;
} else if ((channel_extendedvalue->type == EV_TYPE_TIMER_STATE_V1 ||
channel_extendedvalue->type == EV_TYPE_TIMER_STATE_V1_SEC) &&
channel_extendedvalue->size <= sizeof(TTimerState_ExtendedValue) &&
channel_extendedvalue->size >= sizeof(TTimerState_ExtendedValue) -
SUPLA_SENDER_NAME_MAXSIZE) {
timer_state = (TTimerState_ExtendedValue *)channel_extendedvalue->value;
} else if (channel_extendedvalue->type ==
EV_TYPE_CHANNEL_AND_TIMER_STATE_V1 &&
channel_extendedvalue->size <=
sizeof(TChannelAndTimerState_ExtendedValue) &&
channel_extendedvalue->size >=
sizeof(TChannelAndTimerState_ExtendedValue) -
SUPLA_SENDER_NAME_MAXSIZE) {
channel_state =
&((TChannelAndTimerState_ExtendedValue *)channel_extendedvalue->value)
->Channel;
timer_state =
&((TChannelAndTimerState_ExtendedValue *)channel_extendedvalue->value)
->Timer;
}
if (channel_state) {
fid = supla_client_GetFieldID(env, cls, "ChannelStateValue",
"Lorg/supla/android/lib/SuplaChannelState;");
// TChannelState_ExtendedValue is equal to TDSC_ChannelState
jobject channel_state_obj = supla_channelstate_to_jobject(
asc, env, (TDSC_ChannelState *)channel_state);
env->SetObjectField(obj, fid, channel_state_obj);
}
if (timer_state) {
fid = supla_client_GetFieldID(env, cls, "TimerStateValue",
"Lorg/supla/android/lib/SuplaTimerState;");
jobject timer_state_obj = supla_timerstate_to_jobject(
asc, env, (TTimerState_ExtendedValue *)timer_state);
env->SetObjectField(obj, fid, timer_state_obj);
}
}
jobject supla_channelextendedvalue_to_jobject(
void *_suplaclient, void *user_data,
TSuplaChannelExtendedValue *channel_extendedvalue) {
ASC_VAR_DECLARATION(NULL);
ENV_VAR_DECLARATION(NULL);
jclass cls =
env->FindClass("org/supla/android/lib/SuplaChannelExtendedValue");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject obj = env->NewObject(cls, methodID);
int index = 0;
TSuplaChannelExtendedValue ev = {};
while (srpc_evtool_value_get(channel_extendedvalue, index, &ev)) {
index++;
supla_channelextendedvalue_set_object(_suplaclient, user_data, &ev, obj);
}
return obj;
}
void supla_cb_channel_extendedvalue_update(
void *_suplaclient, void *user_data,
TSC_SuplaChannelExtendedValue *channel_extendedvalue) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_channel_extendedvalue_update) {
jclass cls =
env->FindClass("org/supla/android/lib/SuplaChannelExtendedValueUpdate");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject val = env->NewObject(cls, methodID);
jclass cval = env->GetObjectClass(val);
fid = supla_client_GetFieldID(env, cval, "Id", "I");
env->SetIntField(val, fid, channel_extendedvalue->Id);
fid = supla_client_GetFieldID(
env, cval, "Value",
"Lorg/supla/android/lib/SuplaChannelExtendedValue;");
jobject chv = supla_channelextendedvalue_to_jobject(
_suplaclient, user_data, &channel_extendedvalue->value);
env->SetObjectField(val, fid, chv);
supla_android_client(asc, asc->j_mid_channel_extendedvalue_update, val);
}
}
void supla_cb_channelgroup_update(void *_suplaclient, void *user_data,
TSC_SuplaChannelGroup_B *channel_group) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_channelgroup_update) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaChannelGroup");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject chg = env->NewObject(cls, methodID);
jclass cchg = env->GetObjectClass(chg);
fid = supla_client_GetFieldID(env, cchg, "EOL", "Z");
env->SetBooleanField(chg, fid,
channel_group->EOL == 1 ? JNI_TRUE : JNI_FALSE);
fid = supla_client_GetFieldID(env, cchg, "Id", "I");
env->SetIntField(chg, fid, channel_group->Id);
fid = supla_client_GetFieldID(env, cchg, "LocationID", "I");
env->SetIntField(chg, fid, channel_group->LocationID);
fid = supla_client_GetFieldID(env, cchg, "Func", "I");
env->SetIntField(chg, fid, channel_group->Func);
fid = supla_client_GetFieldID(env, cchg, "AltIcon", "I");
env->SetIntField(chg, fid, channel_group->AltIcon);
fid = supla_client_GetFieldID(env, cchg, "UserIcon", "I");
env->SetIntField(chg, fid, channel_group->UserIcon);
fid = supla_client_GetFieldID(env, cchg, "Flags", "J");
env->SetLongField(chg, fid, channel_group->Flags);
fid = supla_client_GetFieldID(env, cchg, "Caption", "Ljava/lang/String;");
env->SetObjectField(chg, fid, new_string_utf(env, channel_group->Caption));
supla_android_client(asc, asc->j_mid_channelgroup_update, chg);
}
}
void supla_cb_channelgroup_relation_update(
void *_suplaclient, void *user_data,
TSC_SuplaChannelGroupRelation *channelgroup_relation) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
jfieldID fid;
if (asc->j_mid_channelgroup_update) {
jclass cls =
env->FindClass("org/supla/android/lib/SuplaChannelGroupRelation");
jmethodID methodID = env->GetMethodID(cls, "<init>", "()V");
jobject chg = env->NewObject(cls, methodID);
jclass cchg = env->GetObjectClass(chg);
fid = supla_client_GetFieldID(env, cchg, "EOL", "Z");
env->SetBooleanField(
chg, fid, channelgroup_relation->EOL == 1 ? JNI_TRUE : JNI_FALSE);
fid = supla_client_GetFieldID(env, cchg, "ChannelGroupID", "I");
env->SetIntField(chg, fid, channelgroup_relation->ChannelGroupID);
fid = supla_client_GetFieldID(env, cchg, "ChannelID", "I");
env->SetIntField(chg, fid, channelgroup_relation->ChannelID);
supla_android_client(asc, asc->j_mid_channelgroup_relation_update, chg);
}
}
void supla_cb_channel_relation_update(
void *_suplaclient, void *user_data,
TSC_SuplaChannelRelation *channel_relation) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
if (asc->j_mid_channel_update) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaChannelRelation");
jmethodID methodID = env->GetMethodID(cls, "<init>", "(IISB)V");
jobject rel = env->NewObject(cls, methodID, channel_relation->Id,
channel_relation->ParentId,
channel_relation->Type, channel_relation->EOL);
supla_android_client(asc, asc->j_mid_channel_relation_update, rel);
}
}
void supla_cb_scene_update(void *_suplaclient, void *user_data,
TSC_SuplaScene *scene) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
if (asc->j_mid_scene_update) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaScene");
jmethodID methodID =
env->GetMethodID(cls, "<init>", "(IIIILjava/lang/String;Z)V");
jobject scene_obj = env->NewObject(
cls, methodID, scene->Id, scene->LocationId, scene->AltIcon,
scene->UserIcon, new_string_utf(env, scene->Caption),
scene->EOL == 1 ? JNI_TRUE : JNI_FALSE);
supla_android_client(asc, asc->j_mid_scene_update, scene_obj);
}
}
void supla_cb_scene_state_update(void *_suplaclient, void *user_data,
TSC_SuplaSceneState *state) {
ASC_VAR_DECLARATION();
ENV_VAR_DECLARATION();
if (asc->j_mid_scene_state_update) {
jclass cls = env->FindClass("org/supla/android/lib/SuplaSceneState");
jmethodID methodID =
env->GetMethodID(cls, "<init>", "(IJJILjava/lang/String;Z)V");
jlong millisFromStart = state->MillisecondsFromStart;
jlong millisLeft = state->MillisecondsLeft;
jobject state_obj = env->NewObject(
cls, methodID, state->SceneId, millisFromStart, millisLeft,
state->InitiatorId, new_string_utf(env, state->InitiatorName),
state->EOL == 1 ? JNI_TRUE : JNI_FALSE);