-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathknx_iot_virtual_pb.c
2074 lines (1904 loc) · 67.3 KB
/
knx_iot_virtual_pb.c
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) 2022-2023 Cascoda Ltd
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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.
-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
*/
/**
* @file
*
* KNX virtual Push Button
* ## Application Design
*
* support functions:
*
* - app_init
* initializes the stack values.
* - register_resources
* function that registers all endpoints,
* e.g. sets the GET/PUT/POST/DELETE
* handlers for each end point
*
* - main
* starts the stack, with the registered resources.
* can be compiled out with NO_MAIN
*
* handlers for the implemented methods (get/put):
* - get_[path]
* function that is being called when a GET is called on [path]
* set the global variables in the output
* - put_[path]
* function that is being called when a PUT is called on [path]
* if input data is of the correct type
* updates the global variables
*
* ## stack specific defines
* - __linux__
* build for Linux
* - WIN32
* build for Windows
* - OC_OSCORE
* oscore is enabled as compile flag
* ## File specific defines
* - NO_MAIN
* compile out the function main()
* - INCLUDE_EXTERNAL
* includes header file "external_header.h", so that other tools/dependencies
* can be included without changing this code
* - KNX_GUI
* build the GUI with console option, so that all
* logging can be seen in the command window
*/
#include "oc_api.h"
#include "oc_core_res.h"
#include "oc_rep.h"
#include "oc_helpers.h"
#include "api/oc_knx_fp.h"
#include "port/oc_clock.h"
#include <signal.h>
/* test purpose only; commandline reset */
#include "api/oc_knx_dev.h"
#ifdef OC_SPAKE
#include "security/oc_spake2plus.h"
#endif
#ifdef INCLUDE_EXTERNAL
/* import external definitions from header file*/
/* this file should be externally supplied */
#include "external_header.h"
#endif
#include "knx_iot_virtual_pb.h"
#include <stdlib.h>
#include <ctype.h>
#ifdef __linux__
/** linux specific code */
#include <pthread.h>
#ifndef NO_MAIN
static pthread_mutex_t mutex;
static pthread_cond_t cv;
static struct timespec ts;
#endif /* NO_MAIN */
#endif
#include <stdio.h> /* defines FILENAME_MAX */
#define MY_NAME "KNX virtual Push Button" /**< The name of the application */
#define APP_MAX_STRING 30
#ifdef WIN32
/** windows specific code */
#include <windows.h>
static CONDITION_VARIABLE cv; /**< event loop variable */
static CRITICAL_SECTION cs; /**< event loop variable */
#include <direct.h>
#define GetCurrentDir _getcwd
#else
#include <unistd.h>
#define GetCurrentDir getcwd
#endif
#define btoa(x) ((x) ? "true" : "false")
volatile int quit = 0; /**< stop variable, used by handle_signal */
bool g_reset = false; /**< reset variable, set by commandline arguments */
char g_serial_number[20] = "00FA10010400";
volatile bool g_OnOff_1; /**< global variable for OnOff_1 */
volatile bool g_InfoOnOff_1; /**< global variable for InfoOnOff_1 */
volatile bool g_OnOff_2; /**< global variable for OnOff_2 */
volatile bool g_InfoOnOff_2; /**< global variable for InfoOnOff_2 */
volatile bool g_OnOff_3; /**< global variable for OnOff_3 */
volatile bool g_InfoOnOff_3; /**< global variable for InfoOnOff_3 */
volatile bool g_OnOff_4; /**< global variable for OnOff_4 */
volatile bool g_InfoOnOff_4; /**< global variable for InfoOnOff_4 */
volatile bool g_fault_InfoOnOff_1; /**< global variable for fault InfoOnOff_1 */
volatile bool g_fault_InfoOnOff_2; /**< global variable for fault InfoOnOff_2 */
volatile bool g_fault_InfoOnOff_3; /**< global variable for fault InfoOnOff_3 */
volatile bool g_fault_InfoOnOff_4; /**< global variable for fault InfoOnOff_4 */
// BOOLEAN code
/**
* @brief function to check if the url is represented by a boolean
*
* @param true = url value is a boolean
* @param false = url is not a boolean
*/
bool app_is_bool_url(char* url)
{
if ( strcmp(url, URL_ONOFF_1) == 0) {
return true; /**< OnOff_1 is a boolean */
}
if ( strcmp(url, URL_INFOONOFF_1) == 0) {
return true; /**< InfoOnOff_1 is a boolean */
}
if ( strcmp(url, URL_ONOFF_2) == 0) {
return true; /**< OnOff_2 is a boolean */
}
if ( strcmp(url, URL_INFOONOFF_2) == 0) {
return true; /**< InfoOnOff_2 is a boolean */
}
if ( strcmp(url, URL_ONOFF_3) == 0) {
return true; /**< OnOff_3 is a boolean */
}
if ( strcmp(url, URL_INFOONOFF_3) == 0) {
return true; /**< InfoOnOff_3 is a boolean */
}
if ( strcmp(url, URL_ONOFF_4) == 0) {
return true; /**< OnOff_4 is a boolean */
}
if ( strcmp(url, URL_INFOONOFF_4) == 0) {
return true; /**< InfoOnOff_4 is a boolean */
}
return false;
}
/**
* @brief sets the global boolean variable at the url
*
* @param url the url indicating the global variable
* @param value the value to be set
*/
void app_set_bool_variable(char* url, bool value)
{
if ( strcmp(url, URL_ONOFF_1) == 0) {
g_OnOff_1 = value; /**< global variable for OnOff_1 */
return;
}
if ( strcmp(url, URL_INFOONOFF_1) == 0) {
g_InfoOnOff_1 = value; /**< global variable for InfoOnOff_1 */
return;
}
if ( strcmp(url, URL_ONOFF_2) == 0) {
g_OnOff_2 = value; /**< global variable for OnOff_2 */
return;
}
if ( strcmp(url, URL_INFOONOFF_2) == 0) {
g_InfoOnOff_2 = value; /**< global variable for InfoOnOff_2 */
return;
}
if ( strcmp(url, URL_ONOFF_3) == 0) {
g_OnOff_3 = value; /**< global variable for OnOff_3 */
return;
}
if ( strcmp(url, URL_INFOONOFF_3) == 0) {
g_InfoOnOff_3 = value; /**< global variable for InfoOnOff_3 */
return;
}
if ( strcmp(url, URL_ONOFF_4) == 0) {
g_OnOff_4 = value; /**< global variable for OnOff_4 */
return;
}
if ( strcmp(url, URL_INFOONOFF_4) == 0) {
g_InfoOnOff_4 = value; /**< global variable for InfoOnOff_4 */
return;
}
}
/**
* @brief retrieve the global boolean variable at the url
*
* @param url the url indicating the global variable
* @return the value of the variable
*/
bool app_retrieve_bool_variable(char* url)
{
if ( strcmp(url, URL_ONOFF_1) == 0) {
return g_OnOff_1; /**< global variable for OnOff_1 */
}
if ( strcmp(url, URL_INFOONOFF_1) == 0) {
return g_InfoOnOff_1; /**< global variable for InfoOnOff_1 */
}
if ( strcmp(url, URL_ONOFF_2) == 0) {
return g_OnOff_2; /**< global variable for OnOff_2 */
}
if ( strcmp(url, URL_INFOONOFF_2) == 0) {
return g_InfoOnOff_2; /**< global variable for InfoOnOff_2 */
}
if ( strcmp(url, URL_ONOFF_3) == 0) {
return g_OnOff_3; /**< global variable for OnOff_3 */
}
if ( strcmp(url, URL_INFOONOFF_3) == 0) {
return g_InfoOnOff_3; /**< global variable for InfoOnOff_3 */
}
if ( strcmp(url, URL_ONOFF_4) == 0) {
return g_OnOff_4; /**< global variable for OnOff_4 */
}
if ( strcmp(url, URL_INFOONOFF_4) == 0) {
return g_InfoOnOff_4; /**< global variable for InfoOnOff_4 */
}
return false;
}
// FAULT code
/**
* @brief set the fault (boolean) variable at the url
*
* @param url the url indicating the fault variable
* @param value the value of the fault variable
*/
void app_set_fault_variable(char* url, bool value)
{
}
/**
* @brief retrieve the fault (boolean) variable at the url
*
* @param url the url indicating the fault variable
* @return the value of the fault variable
*/
bool app_retrieve_fault_variable(char* url)
{
if ( strcmp(url, URL_INFOONOFF_1) == 0) {
return g_fault_InfoOnOff_1; /**< global variable for InfoOnOff_1 */
}
if ( strcmp(url, URL_INFOONOFF_2) == 0) {
return g_fault_InfoOnOff_2; /**< global variable for InfoOnOff_2 */
}
if ( strcmp(url, URL_INFOONOFF_3) == 0) {
return g_fault_InfoOnOff_3; /**< global variable for InfoOnOff_3 */
}
if ( strcmp(url, URL_INFOONOFF_4) == 0) {
return g_fault_InfoOnOff_4; /**< global variable for InfoOnOff_4 */
}
return false;
}
// PARAMETER code
bool app_is_url_parameter(char* url)
{
return false;
}
char* app_get_parameter_url(int index)
{
return NULL;
}
char* app_get_parameter_name(int index)
{
return NULL;
}
bool app_is_secure()
{
#ifdef OC_OSCORE
return true;
#else
return false;
#endif /* OC_OSCORE */
}
static oc_put_struct_t app_put = { NULL };
void
app_set_put_cb(oc_put_cb_t cb)
{
app_put.cb = cb;
}
oc_put_struct_t *
oc_get_put_cb(void)
{
return &app_put;
}
void do_put_cb(char* url)
{
oc_put_struct_t *my_cb = oc_get_put_cb();
if (my_cb && my_cb->cb) {
my_cb->cb(url);
}
}
#ifdef __cplusplus
extern "C" {
#endif
int app_initialize_stack();
void signal_event_loop(void);
void register_resources(void);
int app_init(void);
#ifdef __cplusplus
}
#endif
// DEVBOARD code
/**
* @brief devboard button toggle callback
*
*/
// we no longer have app_set_bool_variable
//void dev_btn_toggle_cb(char *url)
//{
// PRINT_APP("Handling %s\n", url);
// bool val = app_retrieve_bool_variable(url);
// if (val == true)
// {
// val = false;
// }
// else
// {
// val = true;
// }
// app_set_bool_variable(url, val);
// oc_do_s_mode_with_scope(5, url, "w");
//}
/**
* @brief s-mode response callback
* will be called when a response is received on an s-mode read request
*
* @param url the url
* @param rep the full response
* @param rep_value the parsed value of the response
*/
void
oc_add_s_mode_response_cb(char *url, oc_rep_t *rep, oc_rep_t *rep_value)
{
(void)rep;
(void)rep_value;
PRINT("oc_add_s_mode_response_cb %s\n", url);
}
/**
* @brief function to set the input string to upper case
*
* @param str the string to make upper case
*
*/
void app_str_to_upper(char *str){
while (*str != '\0')
{
*str = toupper(*str);
str++;
}
}
/**
* @brief function to set up the device.
*
* sets the:
* - manufacturer : cascoda
* - serial number : 00FA10010400
* - base path
* - knx spec version
* - hardware version : [0, 7, 0]
* - firmware version : [0, 7, 0]
* - hardware type : 000000000001
* - device model : KNX virtual - PB
*
*/
int
app_init(void)
{
int ret = oc_init_platform("cascoda", NULL, NULL);
char serial_number_uppercase[20];
/* set the application name, version, base url, device serial number */
ret |= oc_add_device(MY_NAME, "1.0.0", "//", g_serial_number, NULL, NULL);
oc_device_info_t *device = oc_core_get_device_info(0);
/* set the hardware version 0.7.0 */
oc_core_set_device_hwv(0, 0, 7, 0);
/* set the firmware version 0.7.0 */
oc_core_set_device_fwv(0, 0, 7, 0);
char mid[5];
strncpy(mid, g_serial_number, 5); // mid = first 4 digits of sn
mid[4] = '\0';
long int mid_num = strtol(mid, NULL, 16);
/* manufactorer id */
oc_core_set_device_mid(0, (uint32_t)mid_num);
/* set the hardware type*/
// 123456789012
oc_core_set_device_hwt(0, "000000000002");
/* set the model */
oc_core_set_device_model(0, "KNX virtual - PB");
oc_set_s_mode_response_cb(oc_add_s_mode_response_cb);
#define PASSWORD "ABY8B77J50YXMUDW3DG4"
#ifdef OC_SPAKE
if (strlen(oc_spake_get_password()) == 0)
oc_spake_set_password(PASSWORD);
strncpy(serial_number_uppercase, oc_string(device->serialnumber), 19);
app_str_to_upper(serial_number_uppercase);
printf("\n === QR Code: KNX:S:%s;P:%s ===\n", serial_number_uppercase, oc_spake_get_password());
#endif
return ret;
}
/**
* @brief returns the password
*/
char* app_get_password()
{
return PASSWORD;
}
// data point (objects) handling
/**
* @brief CoAP GET method for data point "OnOff_1" resource at url URL_ONOFF_1 ("/p/o_1_1").
* resource types: ['urn:knx:dpa.421.61']
* function is called to initialize the return values of the GET method.
* initialization of the returned values are done from the global property
* values.
*
* @param request the request representation.
* @param interfaces the interface used for this call
* @param user_data the user data.
*/
void
get_OnOff_1(oc_request_t *request, oc_interface_mask_t interfaces,
void *user_data)
{
(void)user_data; /* variable not used */
/* MANUFACTORER: SENSOR add here the code to talk to the HW if one implements a
sensor. the call to the HW needs to fill in the global variable before it
returns to this function here. alternative is to have a callback from the
hardware that sets the global variables.
*/
bool error_state = false; /* the error state, the generated code */
PRINT("-- Begin get_OnOff_1 %s \n", URL_ONOFF_1);
/* check if the accept header is CBOR */
if (oc_check_accept_header(request, APPLICATION_CBOR) == false) {
oc_send_response(request, OC_STATUS_BAD_OPTION);
return;
}
// check the query parameter m with the various values
char *m;
char* m_key;
size_t m_key_len;
size_t m_len = (int)oc_get_query_value(request, "m", &m);
if (m_len != -1) {
PRINT(" Query param: %.*s",(int)m_len, m);
oc_init_query_iterator();
size_t device_index = request->resource->device;
oc_device_info_t *device = oc_core_get_device_info(device_index);
if (device != NULL) {
oc_rep_begin_root_object();
while (oc_iterate_query(request, &m_key, &m_key_len, &m, &m_len) != -1) {
// unique identifier
if ((strncmp(m, "id", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
char mystring[100];
snprintf(mystring,99,"urn:knx:sn:%s%s",oc_string(device->serialnumber),
oc_string(request->resource->uri));
oc_rep_i_set_text_string(root, 0, mystring);
}
// resource types
if ((strncmp(m, "rt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, rt, "urn:knx:dpa.421.61");
}
// interfaces
if ((strncmp(m, "if", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, if, "if.s");
}
if ((strncmp(m, "dpt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, dpt, oc_string(request->resource->dpt));
}
// ga
if ((strncmp(m, "ga", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
int index = oc_core_find_group_object_table_url(oc_string(request->resource->uri));
if (index > -1) {
oc_group_object_table_t* got_table_entry = oc_core_get_group_object_table_entry(index);
if (got_table_entry) {
oc_rep_set_int_array(root, ga, got_table_entry->ga, got_table_entry->ga_len);
}
}
}
if ((strncmp(m, "desc", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, desc, "On/Off push button 1");
}
} /* query iterator */
oc_rep_end_root_object();
} else {
/* device is NULL */
oc_send_cbor_response(request, OC_STATUS_BAD_OPTION);
}
oc_send_cbor_response(request, OC_STATUS_OK);
return;
}
oc_rep_begin_root_object();
oc_rep_i_set_boolean(root, 1, g_OnOff_1);
oc_rep_end_root_object();
if (g_err) {
error_state = true;
}
PRINT("CBOR encoder size %d\n", oc_rep_get_encoded_payload_size());
if (error_state == false) {
oc_send_cbor_response(request, OC_STATUS_OK);
} else {
oc_send_response(request, OC_STATUS_BAD_OPTION);
}
PRINT("-- End get_OnOff_1\n");
}
/**
* @brief CoAP GET method for data point "InfoOnOff_1" resource at url URL_INFOONOFF_1 ("/p/o_2_2").
* resource types: ['urn:knx:dpa.421.51']
* function is called to initialize the return values of the GET method.
* initialization of the returned values are done from the global property
* values.
*
* @param request the request representation.
* @param interfaces the interface used for this call
* @param user_data the user data.
*/
void
get_InfoOnOff_1(oc_request_t *request, oc_interface_mask_t interfaces,
void *user_data)
{
(void)user_data; /* variable not used */
/* MANUFACTORER: SENSOR add here the code to talk to the HW if one implements a
sensor. the call to the HW needs to fill in the global variable before it
returns to this function here. alternative is to have a callback from the
hardware that sets the global variables.
*/
bool error_state = false; /* the error state, the generated code */
PRINT("-- Begin get_InfoOnOff_1 %s \n", URL_INFOONOFF_1);
/* check if the accept header is CBOR */
if (oc_check_accept_header(request, APPLICATION_CBOR) == false) {
oc_send_response(request, OC_STATUS_BAD_OPTION);
return;
}
// check the query parameter m with the various values
char *m;
char* m_key;
size_t m_key_len;
size_t m_len = (int)oc_get_query_value(request, "m", &m);
if (m_len != -1) {
PRINT(" Query param: %.*s",(int)m_len, m);
oc_init_query_iterator();
size_t device_index = request->resource->device;
oc_device_info_t *device = oc_core_get_device_info(device_index);
if (device != NULL) {
oc_rep_begin_root_object();
while (oc_iterate_query(request, &m_key, &m_key_len, &m, &m_len) != -1) {
// unique identifier
if ((strncmp(m, "id", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
char mystring[100];
snprintf(mystring,99,"urn:knx:sn:%s%s",oc_string(device->serialnumber),
oc_string(request->resource->uri));
oc_rep_i_set_text_string(root, 0, mystring);
}
// resource types
if ((strncmp(m, "rt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, rt, "urn:knx:dpa.421.51");
}
// interfaces
if ((strncmp(m, "if", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, if, "if.a");
}
if ((strncmp(m, "dpt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, dpt, oc_string(request->resource->dpt));
}
// ga
if ((strncmp(m, "ga", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
int index = oc_core_find_group_object_table_url(oc_string(request->resource->uri));
if (index > -1) {
oc_group_object_table_t* got_table_entry = oc_core_get_group_object_table_entry(index);
if (got_table_entry) {
oc_rep_set_int_array(root, ga, got_table_entry->ga, got_table_entry->ga_len);
}
}
}
if ((strncmp(m, "desc", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, desc, "Feedback 1");
}
} /* query iterator */
oc_rep_end_root_object();
} else {
/* device is NULL */
oc_send_cbor_response(request, OC_STATUS_BAD_OPTION);
}
oc_send_cbor_response(request, OC_STATUS_OK);
return;
}
oc_rep_begin_root_object();
oc_rep_i_set_boolean(root, 1, g_InfoOnOff_1);
oc_rep_end_root_object();
if (g_err) {
error_state = true;
}
PRINT("CBOR encoder size %d\n", oc_rep_get_encoded_payload_size());
if (error_state == false) {
oc_send_cbor_response(request, OC_STATUS_OK);
} else {
oc_send_response(request, OC_STATUS_BAD_OPTION);
}
PRINT("-- End get_InfoOnOff_1\n");
}
/**
* @brief CoAP PUT method for data point "InfoOnOff_1" resource at url "/p/o_2_2".
* resource types: ['urn:knx:dpa.421.51']
* The function has as input the request body, which are the input values of the
* PUT method.
* The input values (as a set) are checked if all supplied values are correct.
* If the input values are correct, they will be assigned to the global property
* values.
*
* @param request the request representation.
* @param interfaces the used interfaces during the request.
* @param user_data the supplied user data.
*/
void
put_InfoOnOff_1(oc_request_t *request, oc_interface_mask_t interfaces,
void *user_data)
{
(void)interfaces;
(void)user_data;
bool error_state = true;
PRINT("-- Begin put_InfoOnOff_1:\n");
oc_rep_t *rep = NULL;
/* handle the different requests e.g. via s-mode or normal CoAP call*/
if (oc_is_redirected_request(request)) {
PRINT(" redirected request..\n");
}
rep = request->request_payload;
/* loop over all the entries in the request */
/* handle the type of payload correctly. */
error_state = true;
while (rep != NULL) {
/* handle the type of payload correctly. */
if ((rep->iname == 1) && (rep->type == OC_REP_BOOL)) {
PRINT(" put_InfoOnOff_1 received : %d\n", rep->value.boolean);
g_InfoOnOff_1 = rep->value.boolean;
error_state = false;
break;
}
}
if (error_state == false){
oc_send_cbor_response(request, OC_STATUS_CHANGED);
do_put_cb(URL_INFOONOFF_1);
PRINT("-- End put_InfoOnOff_1\n");
return;
}
/* request data was not recognized, so it was a bad request */
oc_send_response(request, OC_STATUS_BAD_REQUEST);
PRINT("-- End put_InfoOnOff_1\n");
}
/**
* @brief CoAP GET method for data point "OnOff_2" resource at url URL_ONOFF_2 ("/p/o_3_3").
* resource types: ['urn:knx:dpa.421.61']
* function is called to initialize the return values of the GET method.
* initialization of the returned values are done from the global property
* values.
*
* @param request the request representation.
* @param interfaces the interface used for this call
* @param user_data the user data.
*/
void
get_OnOff_2(oc_request_t *request, oc_interface_mask_t interfaces,
void *user_data)
{
(void)user_data; /* variable not used */
/* MANUFACTORER: SENSOR add here the code to talk to the HW if one implements a
sensor. the call to the HW needs to fill in the global variable before it
returns to this function here. alternative is to have a callback from the
hardware that sets the global variables.
*/
bool error_state = false; /* the error state, the generated code */
PRINT("-- Begin get_OnOff_2 %s \n", URL_ONOFF_2);
/* check if the accept header is CBOR */
if (oc_check_accept_header(request, APPLICATION_CBOR) == false) {
oc_send_response(request, OC_STATUS_BAD_OPTION);
return;
}
// check the query parameter m with the various values
char *m;
char* m_key;
size_t m_key_len;
size_t m_len = (int)oc_get_query_value(request, "m", &m);
if (m_len != -1) {
PRINT(" Query param: %.*s",(int)m_len, m);
oc_init_query_iterator();
size_t device_index = request->resource->device;
oc_device_info_t *device = oc_core_get_device_info(device_index);
if (device != NULL) {
oc_rep_begin_root_object();
while (oc_iterate_query(request, &m_key, &m_key_len, &m, &m_len) != -1) {
// unique identifier
if ((strncmp(m, "id", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
char mystring[100];
snprintf(mystring,99,"urn:knx:sn:%s%s",oc_string(device->serialnumber),
oc_string(request->resource->uri));
oc_rep_i_set_text_string(root, 0, mystring);
}
// resource types
if ((strncmp(m, "rt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, rt, "urn:knx:dpa.421.61");
}
// interfaces
if ((strncmp(m, "if", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, if, "if.s");
}
if ((strncmp(m, "dpt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, dpt, oc_string(request->resource->dpt));
}
// ga
if ((strncmp(m, "ga", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
int index = oc_core_find_group_object_table_url(oc_string(request->resource->uri));
if (index > -1) {
oc_group_object_table_t* got_table_entry = oc_core_get_group_object_table_entry(index);
if (got_table_entry) {
oc_rep_set_int_array(root, ga, got_table_entry->ga, got_table_entry->ga_len);
}
}
}
if ((strncmp(m, "desc", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, desc, "On/Off push button 2");
}
} /* query iterator */
oc_rep_end_root_object();
} else {
/* device is NULL */
oc_send_cbor_response(request, OC_STATUS_BAD_OPTION);
}
oc_send_cbor_response(request, OC_STATUS_OK);
return;
}
oc_rep_begin_root_object();
oc_rep_i_set_boolean(root, 1, g_OnOff_2);
oc_rep_end_root_object();
if (g_err) {
error_state = true;
}
PRINT("CBOR encoder size %d\n", oc_rep_get_encoded_payload_size());
if (error_state == false) {
oc_send_cbor_response(request, OC_STATUS_OK);
} else {
oc_send_response(request, OC_STATUS_BAD_OPTION);
}
PRINT("-- End get_OnOff_2\n");
}
/**
* @brief CoAP GET method for data point "InfoOnOff_2" resource at url URL_INFOONOFF_2 ("/p/o_4_4").
* resource types: ['urn:knx:dpa.421.51']
* function is called to initialize the return values of the GET method.
* initialization of the returned values are done from the global property
* values.
*
* @param request the request representation.
* @param interfaces the interface used for this call
* @param user_data the user data.
*/
void
get_InfoOnOff_2(oc_request_t *request, oc_interface_mask_t interfaces,
void *user_data)
{
(void)user_data; /* variable not used */
/* MANUFACTORER: SENSOR add here the code to talk to the HW if one implements a
sensor. the call to the HW needs to fill in the global variable before it
returns to this function here. alternative is to have a callback from the
hardware that sets the global variables.
*/
bool error_state = false; /* the error state, the generated code */
PRINT("-- Begin get_InfoOnOff_2 %s \n", URL_INFOONOFF_2);
/* check if the accept header is CBOR */
if (oc_check_accept_header(request, APPLICATION_CBOR) == false) {
oc_send_response(request, OC_STATUS_BAD_OPTION);
return;
}
// check the query parameter m with the various values
char *m;
char* m_key;
size_t m_key_len;
size_t m_len = (int)oc_get_query_value(request, "m", &m);
if (m_len != -1) {
PRINT(" Query param: %.*s",(int)m_len, m);
oc_init_query_iterator();
size_t device_index = request->resource->device;
oc_device_info_t *device = oc_core_get_device_info(device_index);
if (device != NULL) {
oc_rep_begin_root_object();
while (oc_iterate_query(request, &m_key, &m_key_len, &m, &m_len) != -1) {
// unique identifier
if ((strncmp(m, "id", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
char mystring[100];
snprintf(mystring,99,"urn:knx:sn:%s%s",oc_string(device->serialnumber),
oc_string(request->resource->uri));
oc_rep_i_set_text_string(root, 0, mystring);
}
// resource types
if ((strncmp(m, "rt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, rt, "urn:knx:dpa.421.51");
}
// interfaces
if ((strncmp(m, "if", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, if, "if.a");
}
if ((strncmp(m, "dpt", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, dpt, oc_string(request->resource->dpt));
}
// ga
if ((strncmp(m, "ga", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
int index = oc_core_find_group_object_table_url(oc_string(request->resource->uri));
if (index > -1) {
oc_group_object_table_t* got_table_entry = oc_core_get_group_object_table_entry(index);
if (got_table_entry) {
oc_rep_set_int_array(root, ga, got_table_entry->ga, got_table_entry->ga_len);
}
}
}
if ((strncmp(m, "desc", m_len) == 0) |
(strncmp(m, "*", m_len) == 0) ) {
oc_rep_set_text_string(root, desc, "Feedback 2");
}
} /* query iterator */
oc_rep_end_root_object();
} else {
/* device is NULL */
oc_send_cbor_response(request, OC_STATUS_BAD_OPTION);
}
oc_send_cbor_response(request, OC_STATUS_OK);
return;
}
oc_rep_begin_root_object();
oc_rep_i_set_boolean(root, 1, g_InfoOnOff_2);
oc_rep_end_root_object();
if (g_err) {
error_state = true;
}
PRINT("CBOR encoder size %d\n", oc_rep_get_encoded_payload_size());
if (error_state == false) {
oc_send_cbor_response(request, OC_STATUS_OK);
} else {
oc_send_response(request, OC_STATUS_BAD_OPTION);
}
PRINT("-- End get_InfoOnOff_2\n");
}
/**
* @brief CoAP PUT method for data point "InfoOnOff_2" resource at url "/p/o_4_4".
* resource types: ['urn:knx:dpa.421.51']
* The function has as input the request body, which are the input values of the
* PUT method.
* The input values (as a set) are checked if all supplied values are correct.
* If the input values are correct, they will be assigned to the global property
* values.
*
* @param request the request representation.
* @param interfaces the used interfaces during the request.
* @param user_data the supplied user data.
*/
void
put_InfoOnOff_2(oc_request_t *request, oc_interface_mask_t interfaces,
void *user_data)
{
(void)interfaces;
(void)user_data;
bool error_state = true;
PRINT("-- Begin put_InfoOnOff_2:\n");
oc_rep_t *rep = NULL;
/* handle the different requests e.g. via s-mode or normal CoAP call*/
if (oc_is_redirected_request(request)) {
PRINT(" redirected request..\n");
}
rep = request->request_payload;
/* loop over all the entries in the request */
/* handle the type of payload correctly. */
error_state = true;
while (rep != NULL) {
/* handle the type of payload correctly. */
if ((rep->iname == 1) && (rep->type == OC_REP_BOOL)) {
PRINT(" put_InfoOnOff_2 received : %d\n", rep->value.boolean);
g_InfoOnOff_2 = rep->value.boolean;
error_state = false;
break;
}
}
if (error_state == false){