Fixed ACR issue.
[platform/core/api/zigbee.git] / lib / zbl-zcl.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <glib.h>
19 #include <error.h>
20 #include <stdlib.h>
21
22 #include "zbl.h"
23 #include "zbl-dbus.h"
24 #include "zb-common.h"
25
26 #define ZB_GUARD_CHAR 1
27 #define ZB_ZCL_OCTET_SIZE 1
28 #define ZB_ZCL_LONG_OCTET_SIZE 2
29
30 API int zb_read_attr_status_record_create(
31         zb_zcl_read_attr_status_record_h *handle)
32 {
33         zb_zcl_read_attr_status_record_h simple = NULL;
34
35         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
36         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
37         CHECK_ZIGBEE_PRIVILEGE();
38
39         simple = calloc(1, sizeof(struct read_attribute_status_record_s));
40         RETVM_IF(NULL == simple, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
41
42         *handle = simple;
43
44         return ZIGBEE_ERROR_NONE;
45 }
46
47 API int zb_read_attr_status_record_clone(zb_zcl_read_attr_status_record_h src,
48         zb_zcl_read_attr_status_record_h *dst)
49 {
50         struct read_attribute_status_record_s *srcs = src;
51         struct read_attribute_status_record_s *desc = NULL;
52
53         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
54         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
55         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
56
57         desc = calloc(1, sizeof(struct read_attribute_status_record_s));
58         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
59
60         memcpy(desc, src, sizeof(struct read_attribute_status_record_s));
61         desc->value = calloc(1, zb_zcl_get_data_size(srcs->type));
62         RETVM_IF(NULL == desc->value, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
63         memcpy(desc->value, srcs->value, zb_zcl_get_data_size(srcs->type));
64         *dst = desc;
65
66         return ZIGBEE_ERROR_NONE;
67 }
68
69
70 API int zb_read_attr_status_record_destroy(
71         zb_zcl_read_attr_status_record_h handle)
72 {
73         struct read_attribute_status_record_s* h = handle;
74
75         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
76         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
77
78         if (h->value)
79                 free(h->value);
80         free(h);
81
82         return ZIGBEE_ERROR_NONE;
83 }
84
85 API int zb_read_attr_status_record_get_id(
86         zb_zcl_read_attr_status_record_h handle, unsigned short* id)
87 {
88         struct read_attribute_status_record_s* h = handle;
89         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
90         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
91         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
92
93         *id = h->id;
94         return ZIGBEE_ERROR_NONE;
95 }
96
97 API int zb_read_attr_status_record_set_id(
98         zb_zcl_read_attr_status_record_h handle, unsigned short id)
99 {
100         struct read_attribute_status_record_s* h = handle;
101         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
102         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
103
104         h->id = id;
105         return ZIGBEE_ERROR_NONE;
106 }
107
108 API int zb_read_attr_status_record_get_status(
109         zb_zcl_read_attr_status_record_h handle, zb_zcl_status_e* status)
110 {
111         struct read_attribute_status_record_s* h = handle;
112         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
113         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
114         RETV_IF(NULL == status, ZIGBEE_ERROR_INVALID_PARAMETER);
115
116         *status = h->status;
117         return ZIGBEE_ERROR_NONE;
118 }
119
120 API int zb_read_attr_status_record_set_status(
121         zb_zcl_read_attr_status_record_h handle, zb_zcl_status_e status)
122 {
123         struct read_attribute_status_record_s* h = handle;
124         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
125         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
126
127         h->status = status;
128         return ZIGBEE_ERROR_NONE;
129 }
130
131 API int zb_read_attr_status_record_get_type(
132         zb_zcl_read_attr_status_record_h handle, zb_zcl_data_type_e *type)
133 {
134         struct read_attribute_status_record_s* h = handle;
135         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
136         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
137         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
138
139         *type = h->type;
140         return ZIGBEE_ERROR_NONE;
141 }
142
143 API int zb_read_attr_status_record_set_type(
144         zb_zcl_read_attr_status_record_h handle, zb_zcl_data_type_e type)
145 {
146         struct read_attribute_status_record_s* h = handle;
147         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
148         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
149         RETV_IF(0 >= zb_zcl_get_data_size(type), ZIGBEE_ERROR_INVALID_PARAMETER);
150
151         h->type = type;
152         return ZIGBEE_ERROR_NONE;
153 }
154
155 API int zb_read_attr_status_record_get_value(
156         zb_zcl_read_attr_status_record_h handle, zb_zcl_data_type_e* type,
157         unsigned char** value, int* count)
158 {
159         int len = -1;
160         struct read_attribute_status_record_s* h = handle;
161
162         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
163         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
164         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
165         RETV_IF(NULL == h->value, ZIGBEE_ERROR_NO_DATA);
166
167         len = zb_zcl_get_data_size(h->type);
168         if (0 < len) {
169
170                 *value = calloc(len + ZB_GUARD_CHAR, sizeof(char));
171                 RETV_IF(NULL == *value, ZIGBEE_ERROR_OUT_OF_MEMORY);
172                 memcpy(*value, h->value, len);
173                 *type = h->type;
174                 *count = len;
175
176         } else if (ZB_ZCL_OCTET_STRING == h->type || ZB_ZCL_CHARACTER_STRING == h->type) {
177
178                 len = h->value[0];
179
180                 *value = calloc(len + ZB_GUARD_CHAR, sizeof(char));
181                 RETV_IF(NULL == *value, ZIGBEE_ERROR_OUT_OF_MEMORY);
182                 memcpy(*value, h->value + ZB_ZCL_OCTET_SIZE, len);
183
184                 *type = h->type;
185                 *count = len;
186
187         } else if (ZB_ZCL_LONG_OCTET_STRING == h->type || ZB_ZCL_LONG_CHARACTER_STRING == h->type) {
188
189                 len = h->value[0] & 0xff;
190                 len |= (h->value[1] << 8) & 0xff00 ;
191
192                 *value = calloc(len + ZB_GUARD_CHAR, sizeof(char));
193                 RETV_IF(NULL == *value, ZIGBEE_ERROR_OUT_OF_MEMORY);
194                 memcpy(*value, h->value + ZB_ZCL_LONG_OCTET_SIZE, len);
195
196                 *type = h->type;
197                 *count = len;
198
199         } else {
200                 return ZIGBEE_ERROR_NOT_SUPPORTED;
201         }
202
203         return ZIGBEE_ERROR_NONE;
204 }
205
206 API int zb_read_attr_status_record_set_value(
207         zb_zcl_read_attr_status_record_h handle, zb_zcl_data_type_e type,
208         unsigned char* value, int count)
209 {
210         int len = -1;
211         struct read_attribute_status_record_s* h = handle;
212
213         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
214         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
215         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
216
217         len = zb_zcl_get_data_size(type);
218         if (0 < len) {
219
220                 h->value = calloc(len + ZB_GUARD_CHAR, sizeof(char));
221                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
222                 memcpy(h->value, value, len);
223                 h->type = type;
224
225         } else if (ZB_ZCL_OCTET_STRING == type || ZB_ZCL_CHARACTER_STRING == type) {
226
227                 h->value = calloc(count + ZB_ZCL_OCTET_SIZE + ZB_GUARD_CHAR, sizeof(char));
228                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
229
230                 h->value[0] = count;
231                 memcpy(h->value + ZB_ZCL_OCTET_SIZE, value + ZB_ZCL_OCTET_SIZE, count);
232
233         } else if (ZB_ZCL_LONG_OCTET_STRING == type || ZB_ZCL_LONG_CHARACTER_STRING == type) {
234
235                 h->value = calloc(count + ZB_ZCL_LONG_OCTET_SIZE + ZB_GUARD_CHAR, sizeof(char));
236                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
237
238                 h->value[0] = count & 0xff;
239                 h->value[1] = (count >> 8) & 0xff ;
240                 memcpy(h->value + ZB_ZCL_LONG_OCTET_SIZE, value, len);
241
242         } else {
243                 return ZIGBEE_ERROR_NOT_SUPPORTED;
244         }
245
246         return ZIGBEE_ERROR_NONE;
247 }
248
249 API int zb_discover_attr_info_create(zb_zcl_discover_attr_info_record_h *handle)
250 {
251         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
252
253         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
254         zb_zcl_discover_attr_info_record_h simple = NULL;
255
256         CHECK_ZIGBEE_PRIVILEGE();
257
258         simple = calloc(1, sizeof(struct discover_attribute_info_record_s));
259         RETVM_IF(NULL == simple, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
260         *handle = simple;
261         return ZIGBEE_ERROR_NONE;
262 }
263
264 API int zb_discover_attr_info_clone(zb_zcl_discover_attr_info_record_h src,
265         zb_zcl_discover_attr_info_record_h *dst)
266 {
267         struct discover_attribute_info_record_s *srcs = src;
268         struct discover_attribute_info_record_s *desc = NULL;
269
270         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
271         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
272         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
273
274         desc = calloc(1, sizeof(struct discover_attribute_info_record_s));
275         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
276
277         memcpy(desc, srcs, sizeof(struct discover_attribute_info_record_s));
278
279         *dst = desc;
280
281         return ZIGBEE_ERROR_NONE;
282 }
283
284 API int zb_discover_attr_info_destroy(zb_zcl_discover_attr_info_record_h handle)
285 {
286         struct discover_attribute_info_record_s *h = handle;
287
288         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
289         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
290
291         free(h);
292
293         return ZIGBEE_ERROR_NONE;
294 }
295
296 API int zb_discover_attr_info_get_id(
297         zb_zcl_discover_attr_info_record_h handle, unsigned short *id)
298 {
299         struct discover_attribute_info_record_s *h = handle;
300         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
301         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
302         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
303
304         *id = h->id;
305         return ZIGBEE_ERROR_NONE;
306 }
307
308 API int zb_discover_attr_info_set_id(
309         zb_zcl_discover_attr_info_record_h handle, unsigned short id)
310 {
311         struct discover_attribute_info_record_s *h = handle;
312         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
313         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
314
315         h->id = id;
316         return ZIGBEE_ERROR_NONE;
317 }
318
319 API int zb_discover_attr_info_get_type(
320         zb_zcl_discover_attr_info_record_h handle, zb_zcl_data_type_e *type)
321 {
322         struct discover_attribute_info_record_s *h = handle;
323         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
324         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
325         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
326
327         *type = h->type;
328         return ZIGBEE_ERROR_NONE;
329 }
330
331 API int zb_discover_attr_info_set_type(
332         zb_zcl_discover_attr_info_record_h handle, zb_zcl_data_type_e type)
333 {
334         struct discover_attribute_info_record_s *h = handle;
335         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
336         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
337         RETV_IF(0 >= zb_zcl_get_data_size(type), ZIGBEE_ERROR_INVALID_PARAMETER);
338
339         h->type = type;
340         return ZIGBEE_ERROR_NONE;
341 }
342
343 API int zb_write_attr_record_create(zb_zcl_write_attr_record_h *handle)
344 {
345         zb_zcl_write_attr_record_h t = NULL;
346
347         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
348         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
349         CHECK_ZIGBEE_PRIVILEGE();
350
351         t = calloc(1, sizeof(struct write_attribute_record_s));
352         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
353
354         *handle = t;
355         return ZIGBEE_ERROR_NONE;
356 }
357
358 API int zb_write_attr_record_clone(zb_zcl_write_attr_record_h src,
359         zb_zcl_write_attr_record_h *dst)
360 {
361         struct write_attribute_record_s *srcs = src;
362         struct write_attribute_record_s *desc = NULL;
363
364         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
365         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
366         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
367
368         desc = calloc(1, sizeof(struct write_attribute_record_s));
369         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
370
371         memcpy(desc, src, sizeof(struct write_attribute_record_s));
372
373         desc->value = calloc(1, zb_zcl_get_data_size(srcs->type));
374         RETVM_IF(NULL == desc->value, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
375
376         memcpy(desc->value, srcs->value, zb_zcl_get_data_size(srcs->type));
377         *dst = desc;
378
379         return ZIGBEE_ERROR_NONE;
380 }
381
382 API int zb_write_attr_record_destroy(zb_zcl_write_attr_record_h handle)
383 {
384         struct write_attribute_record_s* h = handle;
385
386         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
387         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
388
389         if (h->value) {
390                 free(h->value);
391                 h->value = NULL;
392         }
393         free(h);
394         h = NULL;
395
396         return ZIGBEE_ERROR_NONE;
397 }
398
399 API int zb_write_attr_record_set_id(zb_zcl_write_attr_record_h handle,
400         unsigned short id)
401 {
402         struct write_attribute_record_s* h = handle;
403
404         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
405         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
406
407         h->id = id;
408         return ZIGBEE_ERROR_NONE;
409 }
410
411 API int zb_write_attr_record_set_type(zb_zcl_write_attr_record_h handle,
412         zb_zcl_data_type_e type)
413 {
414         struct write_attribute_record_s* h = handle;
415
416         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
417         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
418         RETV_IF(0 >= zb_zcl_get_data_size(type), ZIGBEE_ERROR_INVALID_PARAMETER);
419
420         h->type = type;
421         return ZIGBEE_ERROR_NONE;
422 }
423
424 API int zb_write_attr_record_set_value(zb_zcl_write_attr_record_h handle,
425         zb_zcl_data_type_e type, unsigned char* value, int count)
426 {
427         int len = -1;
428
429         struct write_attribute_record_s* h = handle;
430
431         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
432         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
433         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
434
435         if (h->value) {
436                 free(h->value);
437                 h->value = NULL;
438         }
439
440         len = zb_zcl_get_data_size(type);
441         if (0 < len) {
442
443                 h->value = calloc(len + ZB_GUARD_CHAR , sizeof(char));
444                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
445                 memcpy(h->value, value, count);
446                 h->type = type;
447
448         } else if (ZB_ZCL_OCTET_STRING == type || ZB_ZCL_CHARACTER_STRING == type) {
449
450                 h->value = calloc(count + ZB_ZCL_OCTET_SIZE + ZB_GUARD_CHAR, sizeof(char));
451                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
452                 /* The first 1 byte indicate invalid or length of string */
453                 h->value[0] = value[0] & 0xff;
454                 memcpy(h->value + ZB_ZCL_OCTET_SIZE, value, count);
455
456         } else if (ZB_ZCL_LONG_OCTET_STRING == type || ZB_ZCL_LONG_CHARACTER_STRING == type) {
457
458                 h->value = calloc(count + ZB_ZCL_LONG_OCTET_SIZE + ZB_GUARD_CHAR, sizeof(char));
459                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
460
461                 /* The first 2 byte indicate invalid or length of string */
462                 h->value[0] = count & 0xff;
463                 h->value[1] = (count >> 8) & 0xff ;
464
465                 memcpy(h->value + ZB_ZCL_LONG_OCTET_SIZE, value, count);
466
467         } else {
468                 return ZIGBEE_ERROR_NOT_SUPPORTED;
469         }
470
471         return ZIGBEE_ERROR_NONE;
472 }
473
474 API int zb_write_attr_status_create(zb_zcl_write_attr_status_record_h *handle)
475 {
476         zb_zcl_write_attr_status_record_h simple = NULL;
477
478         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
479         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
480         CHECK_ZIGBEE_PRIVILEGE();
481
482         simple = calloc(1, sizeof(struct write_attribute_status_record_s));
483         RETVM_IF(NULL == simple, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
484         *handle = simple;
485
486         return ZIGBEE_ERROR_NONE;
487 }
488
489 API int zb_write_attr_status_clone(zb_zcl_write_attr_status_record_h src,
490         zb_zcl_write_attr_status_record_h *dst)
491 {
492         struct write_attribute_status_record_s *srcs = src;
493         struct write_attribute_status_record_s *desc = NULL;
494
495         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
496         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
497         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
498
499         desc = calloc(1, sizeof(struct write_attribute_status_record_s));
500         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
501
502         memcpy(desc, srcs, sizeof(struct write_attribute_status_record_s));
503
504         *dst = desc;
505
506         return ZIGBEE_ERROR_NONE;
507 }
508
509 API int zb_write_attr_status_destroy(zb_zcl_write_attr_status_record_h handle)
510 {
511         struct write_attribute_status_record_s *h = handle;
512
513         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
514         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
515
516         free(h);
517         return ZIGBEE_ERROR_NONE;
518 }
519
520 API int zb_write_attr_status_get_status(
521         zb_zcl_write_attr_status_record_h handle, zb_zcl_status_e *status)
522 {
523         struct write_attribute_status_record_s *h = handle;
524         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
525         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
526         RETV_IF(NULL == status, ZIGBEE_ERROR_INVALID_PARAMETER);
527
528         *status = h->status;
529         return ZIGBEE_ERROR_NONE;
530 }
531
532 API int zb_write_attr_status_set_status(zb_zcl_write_attr_status_record_h handle,
533         zb_zcl_status_e status)
534 {
535         struct write_attribute_status_record_s *h = handle;
536         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
537         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
538
539         h->status = status;
540         return ZIGBEE_ERROR_NONE;
541 }
542
543 API int zb_write_attr_status_get_id(zb_zcl_write_attr_status_record_h handle,
544         unsigned short *id)
545 {
546         struct write_attribute_status_record_s *h = handle;
547         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
548         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
549         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
550
551         *id = h->id;
552         return ZIGBEE_ERROR_NONE;
553 }
554
555 API int zb_write_attr_status_set_id(zb_zcl_write_attr_status_record_h handle,
556         unsigned short id)
557 {
558         struct write_attribute_status_record_s *h = handle;
559         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
560         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
561
562         h->id = id;
563         return ZIGBEE_ERROR_NONE;
564 }
565
566 API int zb_report_config_record_create(zb_zcl_reporting_config_record_h *handle)
567 {
568         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
569         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
570
571         zb_zcl_reporting_config_record_h t = NULL;
572
573         CHECK_ZIGBEE_PRIVILEGE();
574
575         t = calloc(1, sizeof(struct reporting_configuration_record_s));
576         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
577         *handle = t;
578         return ZIGBEE_ERROR_NONE;
579 }
580
581 API int zb_report_config_record_clone(zb_zcl_reporting_config_record_h src,
582         zb_zcl_reporting_config_record_h *dst)
583 {
584         struct reporting_configuration_record_s *srcs = src;
585         struct reporting_configuration_record_s *desc = NULL;
586
587         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
588         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
589         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
590
591         desc = calloc(1, sizeof(struct reporting_configuration_record_s));
592         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
593
594         memcpy(desc, src, sizeof(struct reporting_configuration_record_s));
595
596         desc->change = calloc(1, zb_zcl_get_data_size(srcs->type));
597         RETVM_IF(NULL == desc->change, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
598
599         memcpy(desc->change, srcs->change, zb_zcl_get_data_size(srcs->type));
600         *dst = desc;
601
602         return ZIGBEE_ERROR_NONE;
603 }
604
605 API int zb_report_config_record_destroy(zb_zcl_reporting_config_record_h handle)
606 {
607         struct reporting_configuration_record_s *h = handle;
608
609         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
610         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
611
612         if (h->change)
613                 free(h->change);
614         free(h);
615
616         return ZIGBEE_ERROR_NONE;
617 }
618
619 API int zb_report_config_record_get_dir(zb_zcl_reporting_config_record_h handle,
620         zb_zcl_fc_direction_e *dir)
621 {
622         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
623
624         struct reporting_configuration_record_s *h = handle;
625         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
626         RETV_IF(NULL == dir, ZIGBEE_ERROR_INVALID_PARAMETER);
627         *dir = h->dir;
628         return ZIGBEE_ERROR_NONE;
629 }
630
631 API int zb_report_config_record_set_dir(zb_zcl_reporting_config_record_h handle,
632         zb_zcl_fc_direction_e dir)
633 {
634         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
635
636         struct reporting_configuration_record_s *h = handle;
637         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
638         if (dir == ZB_ZCL_CLIENT_TO_SERVER)
639                 h->dir = 0x00 ;
640         else
641                 h->dir = 0x01;
642         return ZIGBEE_ERROR_NONE;
643 }
644
645 API int zb_report_config_record_get_id(zb_zcl_reporting_config_record_h handle,
646         unsigned short *id)
647 {
648         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
649
650         struct reporting_configuration_record_s *h = handle;
651         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
652         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
653         *id = h->id;
654         return ZIGBEE_ERROR_NONE;
655 }
656
657 API int zb_report_config_record_set_id(zb_zcl_reporting_config_record_h handle,
658         unsigned short id)
659 {
660         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
661
662         struct reporting_configuration_record_s *h = handle;
663         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
664         h->id = id ;
665         return ZIGBEE_ERROR_NONE;
666 }
667
668
669 API int zb_report_config_record_get_type(
670         zb_zcl_reporting_config_record_h handle, zb_zcl_data_type_e *type)
671 {
672         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
673
674         struct reporting_configuration_record_s *h = handle;
675         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
676         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
677         *type = h->type;
678         return ZIGBEE_ERROR_NONE;
679 }
680
681 API int zb_report_config_record_get_min_interval(
682         zb_zcl_reporting_config_record_h handle, unsigned short *min_i)
683 {
684         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
685
686         struct reporting_configuration_record_s *h = handle;
687         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
688         RETV_IF(NULL == min_i, ZIGBEE_ERROR_INVALID_PARAMETER);
689         *min_i = h->min_i;
690         return ZIGBEE_ERROR_NONE;
691 }
692
693 API int zb_report_config_record_set_min_interval(
694         zb_zcl_reporting_config_record_h handle, unsigned short min_i)
695 {
696         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
697
698         struct reporting_configuration_record_s *h = handle;
699         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
700         h->min_i = min_i;
701         return ZIGBEE_ERROR_NONE;
702 }
703
704 API int zb_report_config_record_get_max_interval(
705         zb_zcl_reporting_config_record_h handle, unsigned short *max_i)
706 {
707         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
708
709         struct reporting_configuration_record_s *h = handle;
710         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
711         RETV_IF(NULL == max_i, ZIGBEE_ERROR_INVALID_PARAMETER);
712         *max_i = h->max_i;
713         return ZIGBEE_ERROR_NONE;
714 }
715
716 API int zb_report_config_record_set_max_interval(
717         zb_zcl_reporting_config_record_h handle, unsigned short max_i)
718 {
719         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
720
721         struct reporting_configuration_record_s *h = handle;
722         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
723         h->max_i = max_i;
724         return ZIGBEE_ERROR_NONE;
725 }
726
727 API int zb_report_config_record_get_timeout(
728         zb_zcl_reporting_config_record_h handle, unsigned short *to)
729 {
730         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
731
732         struct reporting_configuration_record_s *h = handle;
733         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
734         RETV_IF(NULL == to, ZIGBEE_ERROR_INVALID_PARAMETER);
735         *to = h->to;
736         return ZIGBEE_ERROR_NONE;
737 }
738
739 API int zb_report_config_record_set_timeout(
740         zb_zcl_reporting_config_record_h handle, unsigned short to)
741 {
742         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
743
744         struct reporting_configuration_record_s *h = handle;
745         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
746         h->to = to;
747         return ZIGBEE_ERROR_NONE;
748 }
749
750 API int zb_report_config_record_get_change(
751         zb_zcl_reporting_config_record_h handle, zb_zcl_data_type_e *type,
752         unsigned char **value, int *count)
753 {
754         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
755
756         int len = -1;
757         unsigned char *temp = NULL;
758         int data_type = ZB_ZCL_DATA_TYPE_NONE;
759
760         struct reporting_configuration_record_s *h = handle;
761
762         NOT_USED(data_type);
763
764         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
765         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
766         RETV_IF(NULL == h->change, ZIGBEE_ERROR_NO_DATA);
767
768         len = zb_zcl_get_data_size(h->type);
769         data_type = zb_zcl_get_analog_or_discret(h->type);
770         if (0 < len /* && ZB_ZCL_DATA_TYPE_ANALOG == data_type */) {
771
772                 temp = calloc(len, sizeof(char));
773                 RETV_IF(NULL == temp, ZIGBEE_ERROR_OUT_OF_MEMORY);
774                 memcpy(temp, h->change, len);
775                 *type = h->type;
776                 *count = len;
777                 *value = temp;
778
779         } else {
780                 return ZIGBEE_ERROR_NOT_SUPPORTED;
781         }
782
783         return ZIGBEE_ERROR_NONE;
784 }
785
786 API int zb_report_config_record_set_change(
787         zb_zcl_reporting_config_record_h handle, zb_zcl_data_type_e type, unsigned char *value)
788 {
789         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
790
791         int len = -1;
792         int data_type = ZB_ZCL_DATA_TYPE_NONE;
793
794         struct reporting_configuration_record_s *h = handle;
795
796         NOT_USED(data_type);
797
798         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
799         if (h->change)
800                 free(h->change);
801
802         len = zb_zcl_get_data_size(type);
803         data_type = zb_zcl_get_analog_or_discret(type);
804
805         if (0 < len /* && ZB_ZCL_DATA_TYPE_ANALOG == data_type */) {
806                 h->change = calloc(len + 1, sizeof(unsigned char));
807                 RETV_IF(NULL == h->change, ZIGBEE_ERROR_OUT_OF_MEMORY);
808                 memcpy(h->change, value, len);
809                 h->type = type;
810         } else {
811                 return ZIGBEE_ERROR_NOT_SUPPORTED;
812         }
813
814         return ZIGBEE_ERROR_NONE;
815 }
816
817 API int zb_read_report_config_record_create(
818         zb_zcl_read_report_config_record_h *handle)
819 {
820         zb_zcl_read_report_config_record_h t = NULL;
821
822         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
823         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
824         CHECK_ZIGBEE_PRIVILEGE();
825
826         t = calloc(1, sizeof(struct read_reporting_configuration_record_s));
827         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
828         *handle = t;
829
830         return ZIGBEE_ERROR_NONE;
831 }
832
833 API int zb_read_report_config_record_clone(
834         zb_zcl_read_report_config_record_h src, zb_zcl_read_report_config_record_h *dst)
835 {
836         struct read_reporting_configuration_record_s *srcs = src;
837         struct read_reporting_configuration_record_s *desc = NULL;
838
839         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
840         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
841         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
842
843         desc = calloc(1, sizeof(struct read_reporting_configuration_record_s));
844         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
845
846         memcpy(desc, srcs, sizeof(struct read_reporting_configuration_record_s));
847         *dst = desc;
848
849         return ZIGBEE_ERROR_NONE;
850 }
851
852 API int zb_read_report_config_record_destroy(
853         zb_zcl_read_report_config_record_h handle)
854 {
855         struct read_reporting_configuration_record_s *h = handle;
856
857         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
858         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
859
860         free(h);
861
862         return ZIGBEE_ERROR_NONE;
863 }
864
865 API int zb_read_report_config_record_set_dir(
866         zb_zcl_read_report_config_record_h handle, zb_zcl_fc_direction_e dir)
867 {
868         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
869
870         struct read_reporting_configuration_record_s *h = handle;
871         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
872
873         if (dir == ZB_ZCL_CLIENT_TO_SERVER)
874                 h->dir = 0x00;
875         else
876                 h->dir = 0x01;
877         return ZIGBEE_ERROR_NONE;
878 }
879
880 API int zb_read_report_config_record_get_dir(
881         zb_zcl_read_report_config_record_h handle, zb_zcl_fc_direction_e *dir)
882 {
883         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
884
885         struct read_reporting_configuration_record_s *h = handle;
886         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
887
888         *dir = h->dir;
889         return ZIGBEE_ERROR_NONE;
890 }
891
892 API int zb_read_report_config_record_set_id(
893         zb_zcl_read_report_config_record_h handle, unsigned short id)
894 {
895         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
896
897         struct read_reporting_configuration_record_s *h = handle;
898         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
899
900         h->id = id;
901         return ZIGBEE_ERROR_NONE;
902 }
903
904 API int zb_read_report_config_record_get_id(
905         zb_zcl_read_report_config_record_h handle, unsigned short *id)
906 {
907         struct read_reporting_configuration_record_s *h = handle;
908         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
909         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
910         *id = h->id;
911         return ZIGBEE_ERROR_NONE;
912 }
913
914 API int zb_report_config_response_record_create(
915         zb_zcl_report_config_response_record_h *handle)
916 {
917         zb_zcl_report_config_response_record_h t = NULL;
918
919         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
920         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
921         CHECK_ZIGBEE_PRIVILEGE();
922
923         t = calloc(1, sizeof(struct reporting_configuration_response_record_s));
924         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
925         *handle = t;
926
927         return ZIGBEE_ERROR_NONE;
928 }
929
930 API int zb_report_config_response_record_clone(
931         zb_zcl_report_config_response_record_h src,
932         zb_zcl_report_config_response_record_h *dst)
933 {
934         struct reporting_configuration_response_record_s *srcs = src;
935         struct reporting_configuration_response_record_s *desc = NULL;
936
937         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
938         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
939         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
940
941         desc = calloc(1, sizeof(struct reporting_configuration_response_record_s));
942         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
943
944         memcpy(desc, srcs, sizeof(struct reporting_configuration_response_record_s));
945         *dst = desc;
946
947         return ZIGBEE_ERROR_NONE;
948 }
949
950 API int zb_report_config_response_record_destroy(
951         zb_zcl_report_config_response_record_h handle)
952 {
953         struct reporting_configuration_response_record_s *h = handle;
954
955         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
956         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
957
958         free(h);
959
960         return ZIGBEE_ERROR_NONE;
961 }
962
963 API int zb_report_config_response_record_get_status(
964         zb_zcl_report_config_response_record_h handle, zb_zcl_status_e *status)
965 {
966         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
967
968         struct reporting_configuration_response_record_s *h = handle;
969         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
970         RETV_IF(NULL == status, ZIGBEE_ERROR_INVALID_PARAMETER);
971
972         *status = h->status;
973         return ZIGBEE_ERROR_NONE;
974 }
975
976 API int zb_report_config_response_record_set_status(
977         zb_zcl_report_config_response_record_h handle, zb_zcl_status_e status)
978 {
979         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
980
981         struct reporting_configuration_response_record_s *h = handle;
982         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
983
984         h->status = status;
985         return ZIGBEE_ERROR_NONE;
986 }
987
988 API int zb_report_config_response_record_get_dir(
989         zb_zcl_report_config_response_record_h handle, zb_zcl_fc_direction_e *dir)
990 {
991         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
992
993         struct reporting_configuration_response_record_s *h = handle;
994         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
995         RETV_IF(NULL == dir, ZIGBEE_ERROR_INVALID_PARAMETER);
996
997         *dir = h->dir;
998         return ZIGBEE_ERROR_NONE;
999 }
1000
1001 API int zb_report_config_response_record_set_dir(
1002         zb_zcl_report_config_response_record_h handle, zb_zcl_fc_direction_e dir)
1003 {
1004         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1005
1006         struct reporting_configuration_response_record_s *h = handle;
1007         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1008
1009         h->dir = dir;
1010         return ZIGBEE_ERROR_NONE;
1011 }
1012
1013 API int zb_report_config_response_record_get_id(
1014         zb_zcl_report_config_response_record_h handle, unsigned short *id)
1015 {
1016         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1017
1018         struct reporting_configuration_response_record_s *h = handle;
1019         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1020         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1021
1022         *id = h->id;
1023         return ZIGBEE_ERROR_NONE;
1024 }
1025
1026 API int zb_report_config_response_record_set_id(
1027         zb_zcl_report_config_response_record_h handle, unsigned short id)
1028 {
1029         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1030
1031         struct reporting_configuration_response_record_s *h = handle;
1032         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1033
1034         h->id = id;
1035         return ZIGBEE_ERROR_NONE;
1036 }
1037
1038 API int zb_attr_report_create(zb_zcl_attr_report_h *handle)
1039 {
1040         zb_zcl_attr_report_h t = NULL;
1041
1042         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1043         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1044         CHECK_ZIGBEE_PRIVILEGE();
1045
1046         t = calloc(1, sizeof(struct attribute_report_s));
1047         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1048         *handle = t;
1049
1050         return ZIGBEE_ERROR_NONE;
1051 }
1052
1053 API int zb_attr_report_clone(zb_zcl_attr_report_h src, zb_zcl_attr_report_h *dst)
1054 {
1055         struct attribute_report_s *srcs = src;
1056         struct attribute_report_s *desc = NULL;
1057
1058         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1059         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
1060         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
1061
1062         desc = calloc(1, sizeof(struct attribute_report_s));
1063         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1064
1065         memcpy(desc, srcs, sizeof(struct attribute_report_s));
1066
1067         desc->value = calloc(1, zb_zcl_get_data_size(srcs->type));
1068         RETVM_IF(NULL == desc->value, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1069
1070         memcpy(desc->value, srcs->value, zb_zcl_get_data_size(srcs->type));
1071         *dst = desc;
1072
1073         return ZIGBEE_ERROR_NONE;
1074 }
1075
1076 API int zb_attr_report_destroy(zb_zcl_attr_report_h handle)
1077 {
1078         struct attribute_report_s *h = handle;
1079
1080         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1081         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1082
1083         if (h->value)
1084                 free(h->value);
1085         free(h);
1086
1087         return ZIGBEE_ERROR_NONE;
1088 }
1089
1090 API int zb_attr_report_get_id(zb_zcl_attr_report_h handle, unsigned short *id)
1091 {
1092         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1093
1094         struct attribute_report_s *h = handle;
1095         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1096         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1097
1098         *id = h->id;
1099         return ZIGBEE_ERROR_NONE;
1100 }
1101
1102 API int zb_attr_report_set_id(zb_zcl_attr_report_h handle, unsigned short id)
1103 {
1104         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1105
1106         struct attribute_report_s *h = handle;
1107         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1108
1109         h->id = id;
1110         return ZIGBEE_ERROR_NONE;
1111 }
1112
1113 API int zb_attr_report_get_type(zb_zcl_attr_report_h handle,
1114         zb_zcl_data_type_e *type)
1115 {
1116         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1117
1118         struct attribute_report_s *h = handle;
1119         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1120         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
1121
1122         *type = h->type;
1123         return ZIGBEE_ERROR_NONE;
1124 }
1125
1126 API int zb_attr_report_set_type(zb_zcl_attr_report_h handle,
1127         zb_zcl_data_type_e type)
1128 {
1129         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1130
1131         struct attribute_report_s *h = handle;
1132         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1133
1134         h->type = type;
1135         return ZIGBEE_ERROR_NONE;
1136 }
1137
1138 API int zb_attr_report_get_value(zb_zcl_attr_report_h handle,
1139         zb_zcl_data_type_e* type, unsigned char** value, int* count)
1140 {
1141         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1142
1143         int len = -1;
1144         unsigned char *temp = NULL;
1145         struct attribute_report_s *h = handle;
1146
1147         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1148         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1149         RETV_IF(NULL == h->value, ZIGBEE_ERROR_NO_DATA);
1150
1151         len = zb_zcl_get_data_size(h->type);
1152         if (0 < len) {
1153
1154                 temp = calloc(len, sizeof(unsigned char));
1155                 RETV_IF(NULL == temp, ZIGBEE_ERROR_OUT_OF_MEMORY);
1156                 memcpy(temp, h->value, len);
1157                 *type = h->type;
1158                 *count = len;
1159                 *value = temp;
1160
1161         } else if (ZB_ZCL_OCTET_STRING == h->type || ZB_ZCL_CHARACTER_STRING == h->type) {
1162
1163                 len = h->value[0];
1164                 temp = calloc(len + ZB_GUARD_CHAR, sizeof(char));
1165                 RETV_IF(NULL == temp, ZIGBEE_ERROR_OUT_OF_MEMORY);
1166
1167                 memcpy(temp, h->value + ZB_ZCL_OCTET_SIZE, len);
1168                 *type = h->type;
1169                 *count = len;
1170                 *value = temp;
1171
1172         } else if (ZB_ZCL_LONG_OCTET_STRING == h->type || ZB_ZCL_LONG_CHARACTER_STRING == h->type) {
1173
1174                 len = h->value[0] & 0xff;
1175                 len |= (h->value[1] << 8) & 0xff00 ;
1176
1177                 temp = calloc(len + ZB_GUARD_CHAR, sizeof(char));
1178                 RETV_IF(NULL == temp, ZIGBEE_ERROR_OUT_OF_MEMORY);
1179                 memcpy(*value, h->value + ZB_ZCL_LONG_OCTET_SIZE, len);
1180
1181                 *type = h->type;
1182                 *count = len;
1183                 *value = temp;
1184
1185         } else {
1186                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1187         }
1188
1189         return ZIGBEE_ERROR_NONE;
1190 }
1191
1192 API int zb_attr_report_set_value(zb_zcl_attr_report_h handle,
1193         zb_zcl_data_type_e type, unsigned char* value, int count)
1194 {
1195         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1196
1197         int len = -1;
1198         struct attribute_report_s *h = handle;
1199
1200         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1201         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1202
1203         if (h->value) {
1204                 free(h->value);
1205                 h->value = NULL;
1206         }
1207
1208         len = zb_zcl_get_data_size(type);
1209         if (0 < len) {
1210
1211                 h->value = calloc(len + ZB_GUARD_CHAR , sizeof(char));
1212                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1213                 memcpy(h->value, value, count);
1214                 h->type = type;
1215
1216         } else if (ZB_ZCL_OCTET_STRING == type || ZB_ZCL_CHARACTER_STRING == type) {
1217
1218                 h->value = calloc(count + ZB_ZCL_OCTET_SIZE + ZB_GUARD_CHAR, sizeof(char));
1219                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1220                 /* The first 1 byte indicate invalid or length of string */
1221                 h->value[0] = value[0] & 0xff;
1222                 memcpy(h->value + ZB_ZCL_OCTET_SIZE, value, count);
1223
1224         } else if (ZB_ZCL_LONG_OCTET_STRING == type || ZB_ZCL_LONG_CHARACTER_STRING == type) {
1225
1226                 h->value = calloc(count + ZB_ZCL_LONG_OCTET_SIZE + ZB_GUARD_CHAR, sizeof(char));
1227                 RETV_IF(NULL == h->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1228
1229                 /* The first 2 byte indicate invalid or length of string */
1230                 h->value[0] = count & 0xff;
1231                 h->value[1] = (count >> 8) & 0xff ;
1232
1233                 memcpy(h->value + ZB_ZCL_LONG_OCTET_SIZE, value, count);
1234
1235         } else {
1236                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1237         }
1238
1239         return ZIGBEE_ERROR_NONE;
1240 }
1241
1242
1243 /**
1244  * @brief Format of the Selector Field
1245  *
1246  * @since_tizen 4.0
1247  */
1248 struct selector_s {
1249         unsigned char indicator; /**< number of index */
1250         unsigned short *index; /**< index list */
1251 };
1252
1253 /**
1254  * @brief Stcuture for reading attribute
1255  *
1256  * @since_tizen 4.0
1257  */
1258 struct read_structured_attribute_record_s {
1259         unsigned short id; /**< attribute identifier */
1260         struct selector_s *selector; /**< selector format */
1261 };
1262
1263 /**
1264  * @brief Format of the Write Attribute Record Field
1265  *
1266  * @since_tizen 4.0
1267  */
1268 struct write_attribute_structured_status_record_s {
1269         unsigned char status; /**< status */
1270         unsigned short id; /**< identifier */
1271         struct selector_s selector; /**< selector */
1272 };
1273
1274 /**
1275  * @brief Format of the Write Attribute Record Field
1276  *
1277  * @since_tizen 4.0
1278  */
1279 struct write_attribute_structured_record_s {
1280         unsigned short id; /**< attribute identifier */
1281         struct selector_s selector;
1282         unsigned char type; /**< attribute data type */
1283         unsigned char *value; /**< data value */; /**< attribute value */
1284 };
1285
1286 API int zb_extended_attr_info_create(zb_zcl_extended_attr_info_h *handle)
1287 {
1288         zb_zcl_extended_attr_info_h t = NULL;
1289
1290         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1291         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1292         CHECK_ZIGBEE_PRIVILEGE();
1293
1294         t = calloc(1, sizeof(struct extended_attribute_infomation_s));
1295         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1296         *handle = t;
1297
1298         return ZIGBEE_ERROR_NONE;
1299 }
1300
1301 API int zb_extended_attr_info_clone(zb_zcl_extended_attr_info_h src,
1302         zb_zcl_extended_attr_info_h *dst)
1303 {
1304         struct extended_attribute_infomation_s *srcs = src;
1305         struct extended_attribute_infomation_s *desc = NULL;
1306
1307         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1308         RETV_IF(NULL == src, ZIGBEE_ERROR_INVALID_PARAMETER);
1309         RETV_IF(NULL == dst, ZIGBEE_ERROR_INVALID_PARAMETER);
1310
1311         desc = calloc(1, sizeof(struct extended_attribute_infomation_s));
1312         RETVM_IF(NULL == desc, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1313
1314         memcpy(desc, srcs, sizeof(struct extended_attribute_infomation_s));
1315
1316         *dst = desc;
1317
1318         return ZIGBEE_ERROR_NONE;
1319 }
1320
1321 API int zb_extended_attr_info_destroy(zb_zcl_extended_attr_info_h handle)
1322 {
1323         struct extended_attribute_infomation_s *h = handle;
1324
1325         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1326         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1327
1328         free(h);
1329
1330         return ZIGBEE_ERROR_NONE;
1331 }
1332
1333 API int zb_extended_attr_info_get_id(zb_zcl_extended_attr_info_h handle,
1334         unsigned short *id)
1335 {
1336         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1337
1338         struct extended_attribute_infomation_s *h = handle;
1339         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1340         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1341
1342         *id = h->id;
1343         return ZIGBEE_ERROR_NONE;
1344 }
1345
1346 API int zb_extended_attr_info_set_id(zb_zcl_extended_attr_info_h handle,
1347         unsigned short id)
1348 {
1349         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1350
1351         struct extended_attribute_infomation_s *h = handle;
1352         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1353
1354         h->id = id;
1355         return ZIGBEE_ERROR_NONE;
1356 }
1357
1358 API int zb_extended_attr_info_get_type(zb_zcl_extended_attr_info_h handle,
1359         zb_zcl_data_type_e *type)
1360 {
1361         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1362
1363         struct extended_attribute_infomation_s *h = handle;
1364         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1365         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
1366
1367         *type = h->type;
1368         return ZIGBEE_ERROR_NONE;
1369 }
1370
1371 API int zb_extended_attr_info_set_type(zb_zcl_extended_attr_info_h handle,
1372         zb_zcl_data_type_e type)
1373 {
1374         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1375
1376         struct extended_attribute_infomation_s *h = handle;
1377         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1378         RETV_IF(0 >= zb_zcl_get_data_size(type), ZIGBEE_ERROR_INVALID_PARAMETER);
1379
1380         h->type = type;
1381         return ZIGBEE_ERROR_NONE;
1382 }
1383
1384 API int zb_extended_attr_info_get_acl(zb_zcl_extended_attr_info_h handle,
1385         zb_zcl_acl_type_e *acl)
1386 {
1387         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1388
1389         struct extended_attribute_infomation_s *h = handle;
1390         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1391         RETV_IF(NULL == acl, ZIGBEE_ERROR_INVALID_PARAMETER);
1392
1393         *acl = h->acl;
1394         return ZIGBEE_ERROR_NONE;
1395 }
1396
1397 API int zb_extended_attr_info_set_acl(zb_zcl_extended_attr_info_h handle,
1398         zb_zcl_acl_type_e acl)
1399 {
1400         CHECK_FEATURE_SUPPORTED(ZIGBEE_FEATURE);
1401
1402         struct extended_attribute_infomation_s *h = handle;
1403         RETV_IF(NULL == h, ZIGBEE_ERROR_INVALID_PARAMETER);
1404
1405         h->acl = acl;
1406         return ZIGBEE_ERROR_NONE;
1407 }
1408