4501b78d10f1db5295882dd9c73072e8c9dfd734
[platform/core/api/zigbee.git] / lib / zbl-zcl.c
1 #include <glib.h>
2 #include <error.h>
3 #include <stdlib.h>
4
5 #include <zb-type.h>
6 #include <zb-error.h>
7 #include <zcl/zb-zcl-type.h>
8 #include <zcl/zb-zcl-attribute-type.h>
9
10 #include "zbl.h"
11 #include "zb-log.h"
12 #include "zb-common.h"
13 #include "zcl/zb-zcl-type.h"
14
15 API const char* zb_get_cluster_name(int id)
16 {
17         const char* clust_name = NULL;
18
19         switch (id) {
20         case ZB_ZCL_BASIC_CLUSTER_ID:
21                 clust_name = "ZB_ZCL_BASIC_CLUSTER_ID : 0x0000";
22                 break;
23         case ZB_ZCL_POWER_CONFIG_CLUSTER_ID:
24                 clust_name = "ZB_ZCL_POWER_CONFIG_CLUSTER_ID : 0x0001";
25                 break;
26         case ZB_ZCL_DEVICE_TEMP_CLUSTER_ID:
27                 clust_name = "ZB_ZCL_DEVICE_TEMP_CLUSTER_ID : 0x0002";
28                 break;
29         case ZB_ZCL_IDENTIFY_CLUSTER_ID:
30                 clust_name = "ZB_ZCL_IDENTIFY_CLUSTER_ID : 0x0003";
31                 break;
32         case ZB_ZCL_GROUPS_CLUSTER_ID:
33                 clust_name = "ZB_ZCL_GROUPS_CLUSTER_ID : 0x0004";
34                 break;
35         case ZB_ZCL_SCENES_CLUSTER_ID:
36                 clust_name = "ZB_ZCL_SCENES_CLUSTER_ID : 0x0005";
37                 break;
38         case ZB_ZCL_ON_OFF_CLUSTER_ID:
39                 clust_name = "ZB_ZCL_ON_OFF_CLUSTER_ID : 0x0006";
40                 break;
41         case ZB_ZCL_ON_OFF_SWITCH_CONFIG_CLUSTER_ID:
42                 clust_name = "ZB_ZCL_ON_OFF_SWITCH_CONFIG_CLUSTER_ID : 0x0007";
43                 break;
44         case ZB_ZCL_LEVEL_CONTROL_CLUSTER_ID:
45                 clust_name = "ZB_ZCL_LEVEL_CONTROL_CLUSTER_ID : 0x0008";
46                 break;
47         case ZB_ZCL_ALARM_CLUSTER_ID:
48                 clust_name = "ZB_ZCL_ALARM_CLUSTER_ID : 0x0009";
49                 break;
50         case ZB_ZCL_TIME_CLUSTER_ID:
51                 clust_name = "ZB_ZCL_TIME_CLUSTER_ID : 0x000A";
52                 break;
53         case ZB_ZCL_RSSI_LOCATION_CLUSTER_ID:
54                 clust_name = "ZB_ZCL_RSSI_LOCATION_CLUSTER_ID : 0x000B";
55                 break;
56         case ZB_ZCL_BINARY_INPUT_BASIC_CLUSTER_ID:
57                 clust_name = "ZB_ZCL_BINARY_INPUT_BASIC_CLUSTER_ID : 0x000F";
58                 break;
59         case ZB_ZCL_COMMISSIONING_CLUSTER_ID:
60                 clust_name = "ZB_ZCL_COMMISSIONING_CLUSTER_ID : 0x0015";
61                 break;
62         case ZB_ZCL_PARTITION_CLUSTER_ID:
63                 clust_name = "ZB_ZCL_PARTITION_CLUSTER_ID : 0x0016";
64                 break;
65         case ZB_ZCL_OTA_BOOTLOAD_CLUSTER_ID:
66                 clust_name = "ZB_ZCL_OTA_BOOTLOAD_CLUSTER_ID : 0x0019";
67                 break;
68         case ZB_ZCL_POWER_PROFILE_CLUSTER_ID:
69                 clust_name = "ZB_ZCL_POWER_PROFILE_CLUSTER_ID : 0x001A";
70                 break;
71         case ZB_ZCL_APPLIANCE_CONTROL_CLUSTER_ID:
72                 clust_name = "ZB_ZCL_APPLIANCE_CONTROL_CLUSTER_ID : 0x001B";
73                 break;
74         case ZB_ZCL_POLL_CONTROL_CLUSTER_ID:
75                 clust_name = "ZB_ZCL_POLL_CONTROL_CLUSTER_ID : 0x0020";
76                 break;
77         case ZB_ZCL_SHADE_CONFIG_CLUSTER_ID:
78                 clust_name = "ZB_ZCL_SHADE_CONFIG_CLUSTER_ID : 0x0100";
79                 break;
80         case ZB_ZCL_DOOR_LOCK_CLUSTER_ID:
81                 clust_name = "ZB_ZCL_DOOR_LOCK_CLUSTER_ID : 0x0101";
82                 break;
83         case ZB_ZCL_WINDOW_COVERING_CLUSTER_ID:
84                 clust_name = "ZB_ZCL_WINDOW_COVERING_CLUSTER_ID : 0x0102";
85                 break;
86         case ZB_ZCL_PUMP_CONFIG_CONTROL_CLUSTER_ID:
87                 clust_name = "ZB_ZCL_PUMP_CONFIG_CONTROL_CLUSTER_ID : 0x0200";
88                 break;
89         case ZB_ZCL_THERMOSTAT_CLUSTER_ID:
90                 clust_name = "ZB_ZCL_THERMOSTAT_CLUSTER_ID : 0x0201";
91                 break;
92         case ZB_ZCL_FAN_CONTROL_CLUSTER_ID:
93                 clust_name = "ZB_ZCL_FAN_CONTROL_CLUSTER_ID : 0x0202";
94                 break;
95         case ZB_ZCL_DEHUMID_CONTROL_CLUSTER_ID:
96                 clust_name = "ZB_ZCL_DEHUMID_CONTROL_CLUSTER_ID : 0x0203";
97                 break;
98         case ZB_ZCL_THERMOSTAT_UI_CONFIG_CLUSTER_ID:
99                 clust_name = "ZB_ZCL_THERMOSTAT_UI_CONFIG_CLUSTER_ID : 0x0204";
100                 break;
101         case ZB_ZCL_COLOR_CONTROL_CLUSTER_ID:
102                 clust_name = "ZB_ZCL_COLOR_CONTROL_CLUSTER_ID : 0x0300";
103                 break;
104         case ZB_ZCL_BALLAST_CONFIGURATION_CLUSTER_ID:
105                 clust_name = "ZB_ZCL_BALLAST_CONFIGURATION_CLUSTER_ID : 0x0301";
106                 break;
107         case ZB_ZCL_ILLUM_MEASUREMENT_CLUSTER_ID:
108                 clust_name = "ZB_ZCL_ILLUM_MEASUREMENT_CLUSTER_ID : 0x0400";
109                 break;
110         case ZB_ZCL_ILLUM_LEVEL_SENSING_CLUSTER_ID:
111                 clust_name = "ZB_ZCL_ILLUM_LEVEL_SENSING_CLUSTER_ID : 0x0401";
112                 break;
113         case ZB_ZCL_TEMP_MEASUREMENT_CLUSTER_ID:
114                 clust_name = "ZB_ZCL_TEMP_MEASUREMENT_CLUSTER_ID : 0x0402";
115                 break;
116         case ZB_ZCL_PRESSURE_MEASUREMENT_CLUSTER_ID:
117                 clust_name = "ZB_ZCL_PRESSURE_MEASUREMENT_CLUSTER_ID : 0x0403";
118                 break;
119         case ZB_ZCL_FLOW_MEASUREMENT_CLUSTER_ID:
120                 clust_name = "ZB_ZCL_FLOW_MEASUREMENT_CLUSTER_ID : 0x0404";
121                 break;
122         case ZB_ZCL_RELATIVE_HUMIDITY_MEASUREMENT_CLUSTER_ID:
123                 clust_name = "ZB_ZCL_RELATIVE_HUMIDITY_MEASUREMENT_CLUSTER_ID : 0x0405";
124                 break;
125         case ZB_ZCL_OCCUPANCY_SENSING_CLUSTER_ID:
126                 clust_name = "ZB_ZCL_OCCUPANCY_SENSING_CLUSTER_ID : 0x0406";
127                 break;
128         case ZB_ZCL_IAS_ZONE_CLUSTER_ID:
129                 clust_name = "ZB_ZCL_IAS_ZONE_CLUSTER_ID : 0x0500";
130                 break;
131         case ZB_ZCL_IAS_ACE_CLUSTER_ID:
132                 clust_name = "ZB_ZCL_IAS_ACE_CLUSTER_ID : 0x0501";
133                 break;
134         case ZB_ZCL_IAS_WD_CLUSTER_ID:
135                 clust_name = "ZB_ZCL_IAS_WD_CLUSTER_ID : 0x0502";
136                 break;
137         case ZB_ZCL_GENERIC_TUNNEL_CLUSTER_ID:
138                 clust_name = "ZB_ZCL_GENERIC_TUNNEL_CLUSTER_ID : 0x0600";
139                 break;
140         case ZB_ZCL_BACNET_PROTOCOL_TUNNEL_CLUSTER_ID:
141                 clust_name = "ZB_ZCL_BACNET_PROTOCOL_TUNNEL_CLUSTER_ID : 0x0601";
142                 break;
143         case ZB_ZCL_11073_PROTOCOL_TUNNEL_CLUSTER_ID:
144                 clust_name = "ZB_ZCL_11073_PROTOCOL_TUNNEL_CLUSTER_ID : 0x0614";
145                 break;
146         case ZB_ZCL_ISO7816_PROTOCOL_TUNNEL_CLUSTER_ID:
147                 clust_name = "ZB_ZCL_ISO7816_PROTOCOL_TUNNEL_CLUSTER_ID : 0x0615";
148                 break;
149         case ZB_ZCL_PRICE_CLUSTER_ID:
150                 clust_name = "ZB_ZCL_PRICE_CLUSTER_ID : 0x0700";
151                 break;
152         case ZB_ZCL_DEMAND_RESPONSE_LOAD_CONTROL_CLUSTER_ID:
153                 clust_name = "ZB_ZCL_DEMAND_RESPONSE_LOAD_CONTROL_CLUSTER_ID : 0x0701";
154                 break;
155         case ZB_ZCL_SIMPLE_METERING_CLUSTER_ID:
156                 clust_name = "ZB_ZCL_SIMPLE_METERING_CLUSTER_ID : 0x0702";
157                 break;
158         case ZB_ZCL_MESSAGING_CLUSTER_ID:
159                 clust_name = "ZB_ZCL_MESSAGING_CLUSTER_ID : 0x0703";
160                 break;
161         case ZB_ZCL_TUNNELING_CLUSTER_ID:
162                 clust_name = "ZB_ZCL_TUNNELING_CLUSTER_ID : 0x0704";
163                 break;
164         case ZB_ZCL_PREPAYMENT_CLUSTER_ID:
165                 clust_name = "ZB_ZCL_PREPAYMENT_CLUSTER_ID : 0x0705";
166                 break;
167         case ZB_ZCL_ENERGY_MANAGEMENT_CLUSTER_ID:
168                 clust_name = "ZB_ZCL_ENERGY_MANAGEMENT_CLUSTER_ID : 0x0706";
169                 break;
170         case ZB_ZCL_TOU_CALENDAR_CLUSTER_ID:
171                 clust_name = "ZB_ZCL_TOU_CALENDAR_CLUSTER_ID : 0x0707";
172                 break;
173         case ZB_ZCL_DEVICE_MANAGEMENT_CLUSTER_ID:
174                 clust_name = "ZB_ZCL_DEVICE_MANAGEMENT_CLUSTER_ID : 0x0708";
175                 break;
176         case ZB_ZCL_EVENTS_CLUSTER_ID:
177                 clust_name = "ZB_ZCL_EVENTS_CLUSTER_ID : 0x0709";
178                 break;
179         case ZB_ZCL_MDU_PAIRING_CLUSTER_ID:
180                 clust_name = "ZB_ZCL_MDU_PAIRING_CLUSTER_ID : 0x070A";
181                 break;
182         case ZB_ZCL_KEY_ESTABLISHMENT_CLUSTER_ID:
183                 clust_name = "ZB_ZCL_KEY_ESTABLISHMENT_CLUSTER_ID : 0x0800";
184                 break;
185         case ZB_ZCL_INFORMATION_CLUSTER_ID:
186                 clust_name = "ZB_ZCL_INFORMATION_CLUSTER_ID : 0x0900";
187                 break;
188         case ZB_ZCL_DATA_SHARING_CLUSTER_ID:
189                 clust_name = "ZB_ZCL_DATA_SHARING_CLUSTER_ID : 0x0901";
190                 break;
191         case ZB_ZCL_GAMING_CLUSTER_ID:
192                 clust_name = "ZB_ZCL_GAMING_CLUSTER_ID : 0x0902";
193                 break;
194         case ZB_ZCL_DATA_RATE_CONTROL_CLUSTER_ID:
195                 clust_name = "ZB_ZCL_DATA_RATE_CONTROL_CLUSTER_ID : 0x0903";
196                 break;
197         case ZB_ZCL_VOICE_OVER_ZIGBEE_CLUSTER_ID:
198                 clust_name = "ZB_ZCL_VOICE_OVER_ZIGBEE_CLUSTER_ID : 0x0904";
199                 break;
200         case ZB_ZCL_CHATTING_CLUSTER_ID:
201                 clust_name = "ZB_ZCL_CHATTING_CLUSTER_ID : 0x0905";
202                 break;
203         case ZB_ZCL_PAYMENT_CLUSTER_ID:
204                 clust_name = "ZB_ZCL_PAYMENT_CLUSTER_ID : 0x0A01";
205                 break;
206         case ZB_ZCL_BILLING_CLUSTER_ID:
207                 clust_name = "ZB_ZCL_BILLING_CLUSTER_ID : 0x0A02";
208                 break;
209         case ZB_ZCL_APPLIANCE_IDENTIFICATION_CLUSTER_ID:
210                 clust_name = "ZB_ZCL_APPLIANCE_IDENTIFICATION_CLUSTER_ID : 0x0B00";
211                 break;
212         case ZB_ZCL_METER_IDENTIFICATION_CLUSTER_ID:
213                 clust_name = "ZB_ZCL_DATA_RATE_CONTROL_CLUSTER_ID : 0x0B01";
214                 break;
215         case ZB_ZCL_APPLIANCE_EVENTS_AND_ALERT_CLUSTER_ID:
216                 clust_name = "ZB_ZCL_APPLIANCE_EVENTS_AND_ALERT_CLUSTER_ID : 0x0B02";
217                 break;
218         case ZB_ZCL_APPLIANCE_STATISTICS_CLUSTER_ID:
219                 clust_name = "ZB_ZCL_APPLIANCE_STATISTICS_CLUSTER_ID : 0x0905";
220                 break;
221         case ZB_ZCL_ELECTRICAL_MEASUREMENT_CLUSTER_ID:
222                 clust_name = "ZB_ZCL_ELECTRICAL_MEASUREMENT_CLUSTER_ID : 0x0B04";
223                 break;
224         case ZB_ZCL_DIAGNOSTICS_CLUSTER_ID:
225                 clust_name = "ZB_ZCL_DIAGNOSTICS_CLUSTER_ID : 0x0B05";
226                 break;
227         case ZB_ZCL_ZLL_COMMISSIONING_CLUSTER_ID:
228                 clust_name = "ZB_ZCL_ZLL_COMMISSIONING_CLUSTER_ID : 0x1000";
229                 break;
230         default:
231                 clust_name = "UNDEFINED_CLUSTER";
232                 break;
233         }
234
235         return clust_name;
236 }
237
238 typedef struct _attr_type_size {
239         unsigned char type;
240         unsigned char size;
241 } AttrTypeSize;
242
243 static AttrTypeSize attribute_sizes[] = {
244         {ZB_ZCL_NO_DATA_ATTRIBUTE_TYPE, 0},
245         {ZB_ZCL_8_BIT_DATA, 1},
246         {ZB_ZCL_16_BIT_DATA, 2},
247         {ZB_ZCL_24_BIT_DATA, 3},
248         {ZB_ZCL_32_BIT_DATA, 4},
249         {ZB_ZCL_40_BIT_DATA, 5},
250         {ZB_ZCL_48_BIT_DATA, 6},
251         {ZB_ZCL_56_BIT_DATA, 7},
252         {ZB_ZCL_64_BIT_DATA, 8},
253         {ZB_ZCL_BOOLEAN, 1},
254         {ZB_ZCL_8_BIT_BITMAP, 1},
255         {ZB_ZCL_16_BIT_BITMAP, 2},
256         {ZB_ZCL_24_BIT_BITMAP, 3},
257         {ZB_ZCL_32_BIT_BITMAP, 4},
258         {ZB_ZCL_40_BIT_BITMAP, 5},
259         {ZB_ZCL_48_BIT_BITMAP, 6},
260         {ZB_ZCL_56_BIT_BITMAP, 7},
261         {ZB_ZCL_64_BIT_BITMAP, 8},
262         {ZB_ZCL_UNSIGNED_8_BIT_INTEGER, 1},
263         {ZB_ZCL_UNSIGNED_16_BIT_INTEGER, 2},
264         {ZB_ZCL_UNSIGNED_24_BIT_INTEGER, 3},
265         {ZB_ZCL_UNSIGNED_32_BIT_INTEGER, 4},
266         {ZB_ZCL_UNSIGNED_40_BIT_INTEGER, 5},
267         {ZB_ZCL_UNSIGNED_48_BIT_INTEGER, 6},
268         {ZB_ZCL_UNSIGNED_56_BIT_INTEGER, 7},
269         {ZB_ZCL_UNSIGNED_64_BIT_INTEGER, 8},
270         {ZB_ZCL_SIGNED_8_BIT_INTEGER, 1},
271         {ZB_ZCL_SIGNED_16_BIT_INTEGER, 2},
272         {ZB_ZCL_SIGNED_24_BIT_INTEGER, 3},
273         {ZB_ZCL_SIGNED_32_BIT_INTEGER, 4},
274         {ZB_ZCL_SIGNED_40_BIT_INTEGER, 5},
275         {ZB_ZCL_SIGNED_48_BIT_INTEGER, 6},
276         {ZB_ZCL_SIGNED_56_BIT_INTEGER, 7},
277         {ZB_ZCL_SIGNED_64_BIT_INTEGER, 8},
278         {ZB_ZCL_8_BIT_ENUMERATION, 1},
279         {ZB_ZCL_16_BIT_ENUMERATION, 2},
280         {ZB_ZCL_SEMI_PRECISION, 2},
281         {ZB_ZCL_SINGLE_PRECISION, 4},
282         {ZB_ZCL_DOUBLE_PRECISION, 8},
283         {ZB_ZCL_TIME_OF_DAY, 4},
284         {ZB_ZCL_DATE, 4},
285         {ZB_ZCL_UTC_TIME, 4},
286         {ZB_ZCL_CLUSTER_ID, 2},
287         {ZB_ZCL_ATTRIBUTE_ID, 2},
288         {ZB_ZCL_BACNET_OID, 4},
289         {ZB_ZCL_IEEE_ADDRESS, 8},
290         {ZB_ZCL_128_BIT_SECURITY_KEY, 16},
291 };
292
293 static unsigned char analog_discrete_thresholds[] = {
294         0x07, DATA_TYPE_NONE,
295         0x1F, DATA_TYPE_DISCRETE,
296         0x2F, DATA_TYPE_ANALOG,
297         0x37, DATA_TYPE_DISCRETE,
298         0x3F, DATA_TYPE_ANALOG,
299         0x57, DATA_TYPE_DISCRETE,
300         0xDF, DATA_TYPE_NONE,
301         0xE7, DATA_TYPE_ANALOG,
302         0xFF, DATA_TYPE_NONE
303 };
304
305 API int zb_get_analog_or_discret(unsigned char type)
306 {
307         int index = 0;
308
309         while (analog_discrete_thresholds[index] < type)
310                 index += 2;
311
312         return analog_discrete_thresholds[index+1];
313 }
314
315 API int zb_get_data_size(unsigned char type)
316 {
317         int i;
318         int count = (int)(sizeof(attribute_sizes) / 2);
319         for (i = 0; i < count; i++) {
320                 if (attribute_sizes[i].type == type)
321                         return attribute_sizes[i].size;
322         }
323         return -1;
324 }
325
326 API int zb_create_read_attr_status_record(read_attr_status_record_h *handle)
327 {
328         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
329         read_attr_status_record_h simple = calloc(1, sizeof(struct read_attribute_status_record_s));
330         RETVM_IF(NULL == simple, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
331         *handle = simple;
332         return ZIGBEE_ERROR_NONE;
333 }
334
335 API void zb_destroy_read_attr_status_record(read_attr_status_record_h handle)
336 {
337         RET_IF(NULL == handle);
338         if (handle->value)
339                 free(handle->value);
340         free(handle);
341 }
342
343 API int zb_get_id_from_read_attr_status_record(read_attr_status_record_h handle,
344                 unsigned short* id)
345 {
346         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
347         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
348         *id = handle->id;
349         return ZIGBEE_ERROR_NONE;
350 }
351
352 API int zb_set_id_to_read_attr_status_record(read_attr_status_record_h handle,
353                 unsigned short id)
354 {
355         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
356         handle->id = id;
357         return ZIGBEE_ERROR_NONE;
358 }
359
360 API int zb_get_status_from_read_attr_status_record(read_attr_status_record_h handle,
361                 unsigned char* status)
362 {
363         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
364         RETV_IF(NULL == status, ZIGBEE_ERROR_INVALID_PARAMETER);
365         *status = handle->status;
366         return ZIGBEE_ERROR_NONE;
367 }
368
369 API int zb_set_status_to_read_attr_status_record(read_attr_status_record_h handle,
370                 unsigned char status)
371 {
372         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
373         handle->status = status;
374         return ZIGBEE_ERROR_NONE;
375 }
376
377 API int zb_get_type_from_read_attr_status_record(read_attr_status_record_h handle,
378                 unsigned char *type)
379 {
380         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
381         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
382         *type = handle->type;
383         return ZIGBEE_ERROR_NONE;
384 }
385
386 API int zb_set_type_to_read_attr_status_record(read_attr_status_record_h handle,
387                 unsigned char type)
388 {
389         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
390         handle->type = type;
391         return ZIGBEE_ERROR_NONE;
392 }
393
394 API int zb_get_value_from_read_attr_status_record(read_attr_status_record_h handle,
395                 unsigned char type, zb_value_h value)
396 {
397         int len = -1;
398
399         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
400         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
401         RETV_IF(NULL == handle->value, ZIGBEE_ERROR_NO_DATA);
402
403         len = zb_get_data_size(type);
404         if (0 < len) {
405                 memcpy(value->val, handle->value, len);
406                 value->size = len;
407                 value->type = type;
408         } else if (ZB_ZCL_OCTAT_STRING == type || ZB_ZCL_CHRACTER_STRING == type) {
409                 if (value->str) {
410                         if (value->str->v)
411                                 free(value->str->v);
412                         free(value->str);
413                 }
414                 value->str = calloc(1, sizeof(struct attribute_str_s));
415                 RETV_IF(NULL == value->str, ZIGBEE_ERROR_OUT_OF_MEMORY);
416                 value->str->n = *handle->value;
417                 /* The first 1 byte indicate invalid or length of string */
418                 if (0xff == value->str->n) {
419                         free(value->str);
420                         return ZIGBEE_ERROR_NO_DATA;
421                 }
422                 value->str->v = calloc(value->str->n+1, 1);
423                 if (NULL == value->str->v) {
424                         free(value->str);
425                         return ZIGBEE_ERROR_OUT_OF_MEMORY;
426                 }
427                 memcpy(value->str->v, handle->value+sizeof(value->str->n), value->str->n);
428
429                 value->type = type;
430                 value->size = value->str->n;
431         } else if (ZB_ZCL_LONG_OCTAT_STRING == type || ZB_ZCL_LONG_CHRACTER_STRING == type) {
432                 if (value->wstr) {
433                         if (value->wstr->v)
434                                 free(value->wstr->v);
435                         free(value->wstr);
436                 }
437                 value->wstr = calloc(1, sizeof(struct attribute_wstr_s));
438                 RETV_IF(NULL == value->wstr, ZIGBEE_ERROR_OUT_OF_MEMORY);
439                 value->wstr->n = *(handle->value+1) << 8 | *handle->value;
440                 /* The first 2 byte indicate invalid or length of string */
441                 if (0xffff == value->wstr->n) {
442                         free(value->wstr);
443                         return ZIGBEE_ERROR_NO_DATA;
444                 }
445                 value->wstr->v = calloc(value->wstr->n+1, 1);
446                 if (NULL == value->wstr->v) {
447                         free(value->wstr);
448                         return ZIGBEE_ERROR_OUT_OF_MEMORY;
449                 }
450                 memcpy(value->wstr->v, handle->value+sizeof(value->wstr->n), value->wstr->n);
451
452                 value->type = type;
453                 value->size = value->wstr->n;
454         } else
455                 return ZIGBEE_ERROR_NOT_SUPPORTED;
456
457         return ZIGBEE_ERROR_NONE;
458 }
459
460 API int zb_set_value_to_read_attr_status_record(read_attr_status_record_h handle,
461                 unsigned char type, zb_value_h value)
462 {
463         int len = -1;
464
465         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
466         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
467
468         if (handle->value)
469                 free(handle->value);
470
471         len = zb_get_data_size(type);
472         if (0 < len) {
473                 handle->value = calloc(len, sizeof(char));
474                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
475                 memcpy(handle->value, value->val, len);
476         } else if (ZB_ZCL_OCTAT_STRING == type || ZB_ZCL_CHRACTER_STRING == type) {
477                 RETV_IF(NULL == value->str, ZIGBEE_ERROR_NO_DATA);
478                 handle->value = calloc(value->str->n + sizeof(value->str->n), sizeof(char));
479                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
480                 handle->value[0] = value->str->n;
481                 memcpy(handle->value + sizeof(value->str->n),
482                         value->str->v+sizeof(value->str->n), value->str->n);
483         } else if (ZB_ZCL_LONG_OCTAT_STRING == type || ZB_ZCL_LONG_CHRACTER_STRING == type) {
484                 RETV_IF(NULL == value->wstr, ZIGBEE_ERROR_NO_DATA);
485                 handle->value = calloc(value->wstr->n + sizeof(value->wstr->n), sizeof(char));
486                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
487                 handle->value[0] = value->wstr->n & 0xff;
488                 handle->value[1] = (value->wstr->n >> 8) & 0xff ;
489                 memcpy(handle->value + sizeof(value->wstr->n),
490                         value->wstr->v+sizeof(value->wstr->n), value->wstr->n);
491         } else
492                 return ZIGBEE_ERROR_NOT_SUPPORTED;
493
494         return ZIGBEE_ERROR_NONE;
495 }
496
497 API int zb_create_value(zb_value_h *handle)
498 {
499         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
500         zb_value_h simple = calloc(1, sizeof(struct zb_value_s));
501         RETVM_IF(NULL == simple, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
502         *handle = simple;
503         return ZIGBEE_ERROR_NONE;
504 }
505
506 API void zb_destroy_value(zb_value_h handle)
507 {
508         RET_IF(NULL == handle);
509         if (handle->str) {
510                 if (handle->str->v)
511                         free(handle->str->v);
512                 free(handle->str);
513         }
514         if (handle->wstr) {
515                 if (handle->wstr->v)
516                         free(handle->wstr->v);
517                 free(handle->wstr);
518         }
519         free(handle);
520 }
521
522 API int zb_set_value(zb_value_h handle, unsigned char type, unsigned char *value,
523                 int count)
524 {
525         int len = -1;
526
527         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
528         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
529
530         len = zb_get_data_size(type);
531         if (0 < len) {
532                 memcpy(handle->val, value, len);
533                 handle->size = len;
534                 handle->type = type;
535         } else if (ZB_ZCL_OCTAT_STRING == type || ZB_ZCL_CHRACTER_STRING == type) {
536                 if (handle->str) {
537                         if (handle->str->v)
538                                 free(handle->str->v);
539                         free(handle->str);
540                 }
541                 /* string size exception case */
542                 RETV_IF(0xff - 1 < handle->str->n, ZIGBEE_ERROR_PARAMETER_OUT_OF_RANGE);
543                 handle->str = malloc(sizeof(struct attribute_str_s));
544                 RETV_IF(NULL == handle->str, ZIGBEE_ERROR_OUT_OF_MEMORY);
545                 handle->str->n = count;
546                 /* The first 1 byte indicate invalid or length of string */
547                 handle->str->v = calloc(sizeof(unsigned char), handle->str->n + sizeof(handle->str->n));
548                 RETV_IF(NULL == handle->str->v, ZIGBEE_ERROR_OUT_OF_MEMORY);
549                 memcpy(handle->str->v, value+sizeof(handle->str->n), handle->str->n);
550
551                 handle->type = type;
552                 handle->size = count;
553         } else if (ZB_ZCL_LONG_OCTAT_STRING == type || ZB_ZCL_LONG_CHRACTER_STRING == type) {
554                 if (handle->wstr) {
555                         if (handle->wstr->v)
556                                 free(handle->wstr->v);
557                         free(handle->wstr);
558                 }
559                 /* 2 byte string size exception case */
560                 RETV_IF(0xffff - 1 < handle->wstr->n, ZIGBEE_ERROR_PARAMETER_OUT_OF_RANGE);
561                 handle->wstr = malloc(sizeof(struct attribute_wstr_s));
562                 RETV_IF(NULL == handle->wstr, ZIGBEE_ERROR_OUT_OF_MEMORY);
563                 handle->wstr->n = count;
564                 /* The first 2 byte indicate invalid or length of string */
565                 handle->wstr->v = calloc(sizeof(unsigned char), handle->wstr->n + sizeof(handle->wstr->n));
566                 RETV_IF(NULL == handle->wstr->v, ZIGBEE_ERROR_OUT_OF_MEMORY);
567                 memcpy(handle->wstr->v, value+sizeof(handle->wstr->n), handle->wstr->n);
568
569                 handle->type = type;
570                 handle->size = count;
571         } else
572                 return ZIGBEE_ERROR_NOT_SUPPORTED;
573
574         return ZIGBEE_ERROR_NONE;
575 }
576
577 API int zb_get_value(zb_value_h handle, unsigned char *type, unsigned char **value,
578                 int *count)
579 {
580         int len = -1;
581         unsigned char *s = NULL;
582
583         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
584         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
585
586         len = zb_get_data_size(handle->type);
587         if (0 < len) {
588                 s = calloc(handle->size + 1 , sizeof(char));
589                 memcpy(s, handle->val, len);
590                 *count = len;
591         } else if (ZB_ZCL_OCTAT_STRING == handle->type || ZB_ZCL_CHRACTER_STRING == handle->type) {
592                 RETV_IF(NULL == handle->str, ZIGBEE_ERROR_NO_DATA);
593                 s = calloc(sizeof(unsigned char), handle->str->n);
594                 RETV_IF(NULL == s, ZIGBEE_ERROR_OUT_OF_MEMORY);
595                 memcpy(s, handle->str->v+sizeof(handle->str->n), handle->str->n);
596                 *count = handle->str->n;
597         } else if (ZB_ZCL_OCTAT_STRING == handle->type || ZB_ZCL_CHRACTER_STRING == handle->type) {
598                 RETV_IF(NULL == handle->wstr, ZIGBEE_ERROR_NO_DATA);
599                 s = calloc(sizeof(unsigned char), handle->wstr->n);
600                 RETV_IF(NULL == s, ZIGBEE_ERROR_OUT_OF_MEMORY);
601                 memcpy(s, handle->wstr->v+sizeof(handle->wstr->n), handle->wstr->n);
602                 *count = handle->wstr->n;
603         } else
604                 return ZIGBEE_ERROR_NOT_SUPPORTED;
605
606         *type = handle->type;
607         *value = s;
608
609         return ZIGBEE_ERROR_NONE;
610 }
611
612 API int zb_create_discover_attr_info(discover_attr_info_record_h *handle)
613 {
614         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
615         discover_attr_info_record_h simple = calloc(1,
616                 sizeof(struct discover_attribute_info_record_s));
617         RETVM_IF(NULL == simple, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
618         *handle = simple;
619         return ZIGBEE_ERROR_NONE;
620 }
621
622 API void zb_destroy_discover_attr_info(discover_attr_info_record_h handle)
623 {
624         free(handle);
625 }
626
627 API int zb_get_id_from_discover_attr_info(
628                 discover_attr_info_record_h handle,
629                 unsigned short *id)
630 {
631         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
632         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
633         *id = handle->id;
634         return ZIGBEE_ERROR_NONE;
635 }
636
637 API int zb_set_id_to_discover_attr_info(
638                 discover_attr_info_record_h handle,
639                 unsigned short id)
640 {
641         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
642         handle->id = id;
643         return ZIGBEE_ERROR_NONE;
644 }
645
646 API int zb_get_type_from_discover_attr_info(
647                 discover_attr_info_record_h handle,
648                 unsigned char *type)
649 {
650         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
651         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
652         *type = handle->type;
653         return ZIGBEE_ERROR_NONE;
654 }
655
656 API int zb_set_type_to_discover_attr_info(
657                 discover_attr_info_record_h handle,
658                 unsigned char type)
659 {
660         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
661         handle->type = type;
662         return ZIGBEE_ERROR_NONE;
663 }
664
665 API int zb_create_write_attr_record(
666                 write_attr_record_h *handle)
667 {
668         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
669         write_attr_record_h t = calloc(1, sizeof(struct write_attribute_record_s));
670         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
671         *handle = t;
672         return ZIGBEE_ERROR_NONE;
673 }
674
675 API void zb_destroy_write_attr_record(write_attr_record_h handle)
676 {
677         RET_IF(NULL == handle);
678         if (handle->value)
679                 free(handle->value);
680         free(handle);
681 }
682
683 API int zb_set_id_to_write_attr_record(write_attr_record_h handle,
684                 unsigned short id)
685 {
686         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
687         handle->id = id;
688         return ZIGBEE_ERROR_NONE;
689 }
690
691 API int zb_set_type_to_write_attr_record(write_attr_record_h handle,
692                 unsigned char type)
693 {
694         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
695         handle->type = type;
696         return ZIGBEE_ERROR_NONE;
697 }
698
699 API int zb_set_buf_to_write_attr_record(write_attr_record_h handle,
700         unsigned char type, unsigned char *value, int count)
701 {
702         int len = -1;
703         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
704
705         if (handle->value)
706                 free(handle->value);
707
708         len = zb_get_data_size(type);
709         if (0 < len) {
710                 handle->value = calloc(count + 1 , sizeof(char));
711                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
712                 memcpy(handle->value, value, count);
713         } else if (ZB_ZCL_OCTAT_STRING == type || ZB_ZCL_CHRACTER_STRING == type) {
714                 handle->value = calloc(count + sizeof(unsigned char) + 1, sizeof(char));
715                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
716                 /* The first 1 byte indicate invalid or length of string */
717                 handle->value[0] = count & 0xff;
718                 memcpy(handle->value + sizeof(unsigned char), value + sizeof(unsigned char), count);
719         } else if (ZB_ZCL_LONG_OCTAT_STRING == handle->type || ZB_ZCL_LONG_CHRACTER_STRING == handle->type) {
720                 handle->value = calloc(count + sizeof(unsigned short)  + 1, sizeof(char));
721                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
722                 /* The first 2 byte indicate invalid or length of string */
723                 handle->value[0] = count & 0xff;
724                 handle->value[1] = (count >> 8) & 0xff ;
725                 memcpy(handle->value + sizeof(unsigned short), value + sizeof(unsigned short), count);
726         } else
727                 return ZIGBEE_ERROR_NOT_SUPPORTED;
728
729         return ZIGBEE_ERROR_NONE;
730 }
731
732 API int zb_set_value_to_write_attr_record(write_attr_record_h handle, zb_value_h value)
733 {
734         int len = -1;
735
736         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
737         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
738
739         if (handle->value)
740                 free(handle->value);
741
742         len = zb_get_data_size(value->type);
743         if (0 < len) {
744                 handle->value = calloc(value->size + 1 , sizeof(char));
745                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
746                 memcpy(handle->value, value->val, value->size);
747                 handle->type = value->type;
748         } else if (ZB_ZCL_OCTAT_STRING == handle->type || ZB_ZCL_CHRACTER_STRING == handle->type) {
749                 RETV_IF(NULL == value->str, ZIGBEE_ERROR_NO_DATA);
750                 /* 1 byte string size exception case */
751                 RETV_IF(0xff - 1 < value->str->n, ZIGBEE_ERROR_PARAMETER_OUT_OF_RANGE);
752                 handle->value = calloc(value->str->n + sizeof(value->str->n), sizeof(char));
753                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
754                 /* The first 1 byte indicate invalid or length of string */
755                 handle->value[0] = value->str->n & 0xff;
756                 memcpy(handle->value + sizeof(value->str->n),
757                         value->str->v + sizeof(value->str->n), value->str->n);
758         } else if (ZB_ZCL_LONG_OCTAT_STRING == handle->type || ZB_ZCL_LONG_CHRACTER_STRING == handle->type) {
759                 RETV_IF(NULL == value->wstr, ZIGBEE_ERROR_NO_DATA);
760                 /* 2 byte string size exception case */
761                 RETV_IF(0xffff - 1 < value->wstr->n, ZIGBEE_ERROR_PARAMETER_OUT_OF_RANGE);
762                 handle->value = calloc(value->wstr->n + sizeof(value->wstr->n), sizeof(char));
763                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
764                 /* The first 2 byte indicate invalid or length of string */
765                 handle->value[0] = value->wstr->n & 0xff;
766                 handle->value[1] = (value->wstr->n >> 8) & 0xff ;
767                 memcpy(handle->value + sizeof(value->wstr->n),
768                         value->wstr->v + sizeof(value->wstr->n), value->wstr->n);
769         } else
770                 return ZIGBEE_ERROR_NOT_SUPPORTED;
771
772         return ZIGBEE_ERROR_NONE;
773 }
774
775 API int zb_create_write_attr_status(write_attr_status_record_h *handle)
776 {
777         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
778         write_attr_status_record_h simple = calloc(1,
779                 sizeof(struct write_attribute_status_record_s));
780         RETVM_IF(NULL == simple, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
781         *handle = simple;
782         return ZIGBEE_ERROR_NONE;
783 }
784
785 API void zb_destroy_write_attr_status(write_attr_status_record_h handle)
786 {
787         free(handle);
788 }
789
790 API int zb_get_status_from_write_attr_status(write_attr_status_record_h handle,
791                 unsigned char *status)
792 {
793         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
794         RETV_IF(NULL == status, ZIGBEE_ERROR_INVALID_PARAMETER);
795         *status = handle->status;
796         return ZIGBEE_ERROR_NONE;
797 }
798
799 API int zb_set_status_to_write_attr_status(write_attr_status_record_h handle,
800                 unsigned char status)
801 {
802         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
803         handle->status = status;
804         return ZIGBEE_ERROR_NONE;
805 }
806
807 API int zb_get_id_from_write_attr_status(write_attr_status_record_h handle,
808                 unsigned short *id)
809 {
810         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
811         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
812         *id = handle->id;
813         return ZIGBEE_ERROR_NONE;
814 }
815
816 API int zb_set_id_to_write_attr_status(write_attr_status_record_h handle,
817                 unsigned short id)
818 {
819         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
820         handle->id = id;
821         return ZIGBEE_ERROR_NONE;
822 }
823
824 API int zb_create_report_config_record(report_config_record_h *handle)
825 {
826         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
827         report_config_record_h t;
828         t = calloc(1, sizeof(struct reporting_configuration_record_s));
829         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
830         *handle = t;
831         return ZIGBEE_ERROR_NONE;
832 }
833
834 API void zb_destroy_report_config_record(report_config_record_h handle)
835 {
836         RET_IF(NULL == handle);
837         if (handle->change)
838                 free(handle->change);
839         free(handle);
840 }
841
842 API int zb_get_dir_from_report_config_record(report_config_record_h handle,
843                 unsigned char *dir)
844 {
845         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
846         RETV_IF(NULL == dir, ZIGBEE_ERROR_INVALID_PARAMETER);
847         *dir = handle->dir;
848         return ZIGBEE_ERROR_NONE;
849 }
850
851 API int zb_set_dir_to_report_config_record(report_config_record_h handle,
852                 unsigned char dir)
853 {
854         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
855         if (dir == ZB_ZCL_CLIENT_TO_SERVER)
856                 handle->dir = 0x00 ;
857         else
858                 handle->dir = 0x01;
859         return ZIGBEE_ERROR_NONE;
860 }
861
862 API int zb_get_id_from_report_config_record(report_config_record_h handle,
863                 unsigned short *id)
864 {
865         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
866         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
867         *id = handle->id;
868         return ZIGBEE_ERROR_NONE;
869 }
870
871 API int zb_set_id_to_report_config_record(report_config_record_h handle,
872                 unsigned short id)
873 {
874         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
875         handle->id = id ;
876         return ZIGBEE_ERROR_NONE;
877 }
878
879
880 API int zb_get_type_from_report_config_record(report_config_record_h handle,
881                 unsigned char *type)
882 {
883         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
884         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
885         *type = handle->type;
886         return ZIGBEE_ERROR_NONE;
887 }
888
889 API int zb_set_type_to_report_config_record(report_config_record_h handle,
890                 unsigned char type)
891 {
892         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
893         handle->type = type;
894         return ZIGBEE_ERROR_NONE;
895 }
896
897
898 API int zb_get_min_i_from_report_config_record(report_config_record_h handle,
899                 unsigned short *min_i)
900 {
901         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
902         RETV_IF(NULL == min_i, ZIGBEE_ERROR_INVALID_PARAMETER);
903         *min_i = handle->min_i;
904         return ZIGBEE_ERROR_NONE;
905 }
906
907 API int zb_set_min_i_to_report_config_record(report_config_record_h handle,
908                 unsigned short min_i)
909 {
910         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
911         handle->min_i = min_i;
912         return ZIGBEE_ERROR_NONE;
913 }
914
915 API int zb_get_max_i_from_report_config_record(report_config_record_h handle,
916                 unsigned short *max_i)
917 {
918         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
919         RETV_IF(NULL == max_i, ZIGBEE_ERROR_INVALID_PARAMETER);
920         *max_i = handle->max_i;
921         return ZIGBEE_ERROR_NONE;
922 }
923
924 API int zb_set_max_i_to_report_config_record(report_config_record_h handle,
925                 unsigned short max_i)
926 {
927         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
928         handle->max_i = max_i;
929         return ZIGBEE_ERROR_NONE;
930 }
931
932 API int zb_get_timeout_from_report_config_record(report_config_record_h handle,
933                 unsigned short *to)
934 {
935         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
936         RETV_IF(NULL == to, ZIGBEE_ERROR_INVALID_PARAMETER);
937         *to = handle->to;
938         return ZIGBEE_ERROR_NONE;
939 }
940
941 API int zb_set_timeout_to_report_config_record(report_config_record_h handle,
942                 unsigned short to)
943 {
944         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
945         handle->to = to;
946         return ZIGBEE_ERROR_NONE;
947 }
948
949 API int zb_get_change_from_report_config_record1(report_config_record_h handle,
950                 zb_value_h value)
951 {
952         int len = -1;
953         int data_type = DATA_TYPE_NONE;
954
955         NOT_USED(data_type);
956
957         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
958         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
959         RETV_IF(NULL == handle->change, ZIGBEE_ERROR_NO_DATA);
960
961         len = zb_get_data_size(handle->type);
962         data_type = zb_get_analog_or_discret(handle->type);
963         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
964                 memcpy(value->val, handle->change, len);
965                 value->type = handle->type;
966                 value->size = len;
967         } else
968                 return ZIGBEE_ERROR_NOT_SUPPORTED;
969
970         return ZIGBEE_ERROR_NONE;
971 }
972
973 API int zb_set_change_to_report_config_record1(report_config_record_h handle,
974                 zb_value_h value)
975 {
976         int len = -1;
977         int data_type = DATA_TYPE_NONE;
978
979         NOT_USED(data_type);
980
981         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
982         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
983
984         if (handle->change)
985                 free(handle->change);
986
987         len = zb_get_data_size(value->type);
988         data_type = zb_get_analog_or_discret(value->type);
989         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
990                 handle->change = calloc(len + 1, sizeof(char));
991                 RETVM_IF(NULL == handle->change, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
992                 memcpy(handle->change, value->val, len);
993                 handle->type = value->type;
994         } else
995                 return ZIGBEE_ERROR_NOT_SUPPORTED;
996
997         return ZIGBEE_ERROR_NONE;
998 }
999
1000 API int zb_get_change_from_report_config_record2(report_config_record_h handle,
1001                 unsigned char *type, unsigned char **value, int *size)
1002 {
1003         int len = -1;
1004         int data_type = DATA_TYPE_NONE;
1005         unsigned char *t = NULL;
1006
1007         NOT_USED(data_type);
1008
1009         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1010         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1011         RETV_IF(NULL == handle->change, ZIGBEE_ERROR_NO_DATA);
1012
1013         len = zb_get_data_size(handle->type);
1014         data_type = zb_get_analog_or_discret(handle->type);
1015         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
1016                 t = calloc(len + 1, sizeof(char));
1017                 RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1018                 memcpy(t, handle->change, len);
1019                 *value = t;
1020                 *type = handle->type;
1021                 *size = len;
1022         } else
1023                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1024
1025         return ZIGBEE_ERROR_NONE;
1026 }
1027 API int zb_set_change_to_report_config_record2(
1028                 report_config_record_h handle, unsigned char type, unsigned char *value)
1029 {
1030         int len = -1;
1031         int data_type = DATA_TYPE_NONE;
1032
1033         NOT_USED(data_type);
1034
1035         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1036         if (handle->change)
1037                 free(handle->change);
1038
1039         len = zb_get_data_size(type);
1040         data_type = zb_get_analog_or_discret(type);
1041
1042         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
1043                 handle->change = calloc(len + 1, sizeof(unsigned char));
1044                 RETVM_IF(NULL == handle->change, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1045                 memcpy(handle->change, value, len);
1046                 handle->type = type;
1047         } else
1048                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1049
1050         return ZIGBEE_ERROR_NONE;
1051 }
1052
1053 API int zb_get_change_from_report_config_record3(report_config_record_h handle,
1054                 unsigned char *type, unsigned char *value, int *size)
1055 {
1056         int len = -1;
1057         int data_type = DATA_TYPE_NONE;
1058
1059         NOT_USED(data_type);
1060
1061         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1062         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1063         RETV_IF(NULL == handle->change, ZIGBEE_ERROR_NO_DATA);
1064
1065         len = zb_get_data_size(handle->type);
1066         data_type = zb_get_analog_or_discret(handle->type);
1067         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
1068                 memcpy(value, handle->change, len);
1069                 *type = handle->type;
1070                 *size = len;
1071         } else
1072                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1073
1074         return ZIGBEE_ERROR_NONE;
1075 }
1076
1077 API int zb_create_read_report_config_record(read_report_config_record_h *handle)
1078 {
1079         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1080         read_report_config_record_h t =
1081                 calloc(1, sizeof(struct read_reporting_configuration_record_s));
1082         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1083         *handle = t;
1084         return ZIGBEE_ERROR_NONE;
1085 }
1086
1087 API void zb_destroy_read_report_config_record(read_report_config_record_h handle)
1088 {
1089         free(handle);
1090 }
1091
1092 API int zb_set_dir_to_read_report_config_record(read_report_config_record_h handle,
1093                 unsigned char dir)
1094 {
1095         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1096         if (dir == ZB_ZCL_CLIENT_TO_SERVER)
1097                 handle->dir = 0x00;
1098         else
1099                 handle->dir = 0x01;
1100         return ZIGBEE_ERROR_NONE;
1101 }
1102
1103 API int zb_get_dir_from_read_report_config_record(read_report_config_record_h handle,
1104                 unsigned char *dir)
1105 {
1106         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1107         *dir = handle->dir;
1108         return ZIGBEE_ERROR_NONE;
1109 }
1110
1111 API int zb_set_id_to_read_report_config_record(read_report_config_record_h handle,
1112                 unsigned short id)
1113 {
1114         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1115         handle->id = id;
1116         return ZIGBEE_ERROR_NONE;
1117 }
1118
1119 API int zb_get_id_from_read_report_config_record(read_report_config_record_h handle,
1120                 unsigned short *id)
1121 {
1122         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1123         *id = handle->id;
1124         return ZIGBEE_ERROR_NONE;
1125 }
1126
1127 API int zb_create_report_config_response_record(
1128                 report_config_response_record_h *handle)
1129 {
1130         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1131         report_config_response_record_h t =
1132                 calloc(1, sizeof(struct reporting_configuration_response_record_s));
1133         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1134         *handle = t;
1135         return ZIGBEE_ERROR_NONE;
1136 }
1137
1138 API void zb_destroy_report_config_response_record(
1139                 report_config_response_record_h handle)
1140 {
1141         free(handle);
1142 }
1143
1144 API int zb_get_status_from_report_config_response_record(
1145                 report_config_response_record_h handle, unsigned char *status)
1146 {
1147         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1148         RETV_IF(NULL == status, ZIGBEE_ERROR_INVALID_PARAMETER);
1149         *status = handle->status;
1150         return ZIGBEE_ERROR_NONE;
1151 }
1152
1153 API int zb_set_status_to_report_config_response_record(
1154                 report_config_response_record_h handle, unsigned char status)
1155 {
1156         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1157         handle->status = status;
1158         return ZIGBEE_ERROR_NONE;
1159 }
1160
1161 API int zb_get_dir_from_report_config_response_record(
1162                 report_config_response_record_h handle, unsigned char *dir)
1163 {
1164         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1165         RETV_IF(NULL == dir, ZIGBEE_ERROR_INVALID_PARAMETER);
1166         *dir = handle->dir;
1167         return ZIGBEE_ERROR_NONE;
1168 }
1169
1170 API int zb_set_dir_to_report_config_response_record(
1171                 report_config_response_record_h handle, unsigned char dir)
1172 {
1173         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1174         handle->dir = handle->dir;
1175         return ZIGBEE_ERROR_NONE;
1176 }
1177
1178 API int zb_get_id_from_report_config_response_record(
1179         report_config_response_record_h handle, unsigned short *id)
1180 {
1181         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1182         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1183         *id = handle->id;
1184         return ZIGBEE_ERROR_NONE;
1185 }
1186
1187 API int zb_set_id_to_report_config_response_record(
1188         report_config_response_record_h handle, unsigned short id)
1189 {
1190         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1191         handle->id = id;
1192         return ZIGBEE_ERROR_NONE;
1193 }
1194
1195 API int zb_create_attr_report(attr_report_h *handle)
1196 {
1197         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1198         attr_report_h t = calloc(1, sizeof(struct attribute_report_s));
1199         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1200         *handle = t;
1201         return ZIGBEE_ERROR_NONE;
1202 }
1203
1204 API void zb_destroy_attr_report(attr_report_h handle)
1205 {
1206         if (handle) {
1207                 if (handle->value)
1208                         free(handle->value);
1209                 free(handle);
1210         }
1211 }
1212
1213 API int zb_get_id_from_attr_report(attr_report_h handle, unsigned short *id)
1214 {
1215         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1216         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1217         *id = handle->id;
1218         return ZIGBEE_ERROR_NONE;
1219 }
1220
1221 API int zb_set_id_to_attr_report(attr_report_h handle, unsigned short id)
1222 {
1223         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1224         handle->id = id;
1225         return ZIGBEE_ERROR_NONE;
1226 }
1227
1228 API int zb_get_type_from_attr_report(attr_report_h handle, unsigned char *type)
1229 {
1230         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1231         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
1232         *type = handle->type;
1233         return ZIGBEE_ERROR_NONE;
1234 }
1235
1236 API int zb_set_type_to_attr_report(attr_report_h handle, unsigned char type)
1237 {
1238         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1239         handle->type = type;
1240         return ZIGBEE_ERROR_NONE;
1241 }
1242
1243 API int zb_get_value_from_attr_report(attr_report_h handle, zb_value_h value)
1244 {
1245         int len = -1;
1246
1247         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1248         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1249         RETV_IF(NULL == handle->value, ZIGBEE_ERROR_NO_DATA);
1250
1251         len = zb_get_data_size(handle->type);
1252         if (0 < len) {
1253                 memcpy(value->val, handle->value, len);
1254                 value->size = len;
1255                 value->type = handle->type;
1256         } else if (ZB_ZCL_OCTAT_STRING == handle->type || ZB_ZCL_CHRACTER_STRING == handle->type) {
1257                 if (value->str) {
1258                         if (value->str->v)
1259                                 free(value->str->v);
1260                         free(value->str);
1261                 }
1262                 value->str = calloc(1, sizeof(struct attribute_str_s));
1263                 RETV_IF(NULL == value->str, ZIGBEE_ERROR_OUT_OF_MEMORY);
1264                 value->str->n = *handle->value;
1265                 /* The first 1 byte indicate invalid or length of string */
1266                 if (0xff == value->str->n) {
1267                         free(value->str);
1268                         return ZIGBEE_ERROR_NO_DATA;
1269                 }
1270                 value->str->v = calloc(value->str->n+1, 1);
1271                 if (NULL == value->str->v) {
1272                         free(value->str);
1273                         return ZIGBEE_ERROR_OUT_OF_MEMORY;
1274                 }
1275                 memcpy(value->str->v, handle->value+sizeof(value->str->n), value->str->n);
1276
1277                 value->type = handle->type;
1278                 value->size = value->str->n;
1279         } else if (ZB_ZCL_LONG_OCTAT_STRING == handle->type || ZB_ZCL_LONG_CHRACTER_STRING == handle->type) {
1280                 if (value->wstr) {
1281                         if (value->wstr->v)
1282                                 free(value->wstr->v);
1283                         free(value->wstr);
1284                 }
1285                 value->wstr = calloc(1, sizeof(struct attribute_wstr_s));
1286                 RETV_IF(NULL == value->wstr, ZIGBEE_ERROR_OUT_OF_MEMORY);
1287                 value->wstr->n = *(handle->value+1) << 8 | *handle->value;
1288                 /* The first 2 byte indicate invalid or length of string */
1289                 if (0xffff == value->wstr->n) {
1290                         free(value->wstr);
1291                         return ZIGBEE_ERROR_NO_DATA;
1292                 }
1293                 value->wstr->v = calloc(value->wstr->n+1, 1);
1294                 if (NULL == value->wstr->v) {
1295                         free(value->wstr);
1296                         return ZIGBEE_ERROR_OUT_OF_MEMORY;
1297                 }
1298                 memcpy(value->wstr->v, handle->value+sizeof(value->wstr->n), value->wstr->n);
1299
1300                 value->type = handle->type;
1301                 value->size = value->wstr->n;
1302         } else
1303                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1304
1305         return ZIGBEE_ERROR_NONE;
1306 }
1307
1308 API int zb_set_value_to_attr_report(attr_report_h handle, zb_value_h value)
1309 {
1310         int len = -1;
1311
1312         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1313         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1314
1315         if (handle->value)
1316                 free(handle->value);
1317
1318         len = zb_get_data_size(value->type);
1319         if (0 < len) {
1320                 handle->value = calloc(value->size + 1 , sizeof(char));
1321                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1322                 memcpy(handle->value, value->val, value->size);
1323                 handle->type = value->type;
1324         } else if (ZB_ZCL_OCTAT_STRING == handle->type || ZB_ZCL_CHRACTER_STRING == handle->type) {
1325                 RETV_IF(NULL == value->str, ZIGBEE_ERROR_NO_DATA);
1326                 handle->value = calloc(value->str->n + sizeof(value->str->n), sizeof(char));
1327                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1328                 /* The first 1 byte indicate invalid or length of string */
1329                 handle->value[0] = value->str->n & 0xff;
1330                 memcpy(handle->value + sizeof(value->str->n),
1331                         value->str->v + sizeof(value->str->n), value->str->n);
1332         } else if (ZB_ZCL_LONG_OCTAT_STRING == handle->type || ZB_ZCL_LONG_CHRACTER_STRING == handle->type) {
1333                 RETV_IF(NULL == value->wstr, ZIGBEE_ERROR_NO_DATA);
1334                 handle->value = calloc(value->wstr->n + sizeof(value->wstr->n), sizeof(char));
1335                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1336                 /* The first 2 byte indicate invalid or length of string */
1337                 handle->value[0] = value->wstr->n & 0xff;
1338                 handle->value[1] = (value->wstr->n >> 8) & 0xff ;
1339                 memcpy(handle->value + sizeof(value->wstr->n),
1340                         value->wstr->v + sizeof(value->wstr->n), value->wstr->n);
1341         } else
1342                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1343
1344         return ZIGBEE_ERROR_NONE;
1345 }
1346
1347
1348 /**
1349  * @brief Figure 2.33 Format of the Selector Field
1350  *
1351  * @since_tizen 3.0
1352  */
1353 struct selector_s {
1354         unsigned char indicator; /**< number of index */
1355         unsigned short *index; /**< index list */
1356 };
1357
1358 /**
1359  * @brief stcuture for reading attribute
1360  *
1361  * @since_tizen 3.0
1362  */
1363 struct read_structured_attribute_record_s {
1364         unsigned short id; /**< attribute identifier */
1365         struct selector_s *selector; /**< selector format */
1366 };
1367
1368 /**
1369  * @brief Figure 2.11 Format of the Write Attribute Record Field
1370  *
1371  * @since_tizen 3.0
1372  */
1373 struct write_attribute_structured_status_record_s {
1374         unsigned char status; /**< status */
1375         unsigned short id; /**< identifier */
1376         struct selector_s selector; /**< selector */
1377 };
1378
1379 /**
1380  * @brief Figure 2.32 Format of the Write Attribute Record Field
1381  *
1382  * @since_tizen 3.0
1383  */
1384 struct write_attribute_structured_record_s {
1385         unsigned short id; /**< attribute identifier */
1386         struct selector_s selector;
1387         unsigned char type; /**< attribute data type */
1388         unsigned char *value; /**< data value */; /**< attribute value */
1389 };
1390
1391 API int zb_create_extended_attr_info(extended_attr_info_h *handle)
1392 {
1393         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1394         extended_attr_info_h t = calloc(1, sizeof(struct extended_attribute_infomation_s));
1395         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1396         *handle = t;
1397         return ZIGBEE_ERROR_NONE;
1398 }
1399
1400 API void zb_destroy_extended_attr_info(extended_attr_info_h handle)
1401 {
1402         free(handle);
1403 }
1404
1405 API int zb_get_id_from_extended_attr_info(extended_attr_info_h handle,
1406                 unsigned short *id)
1407 {
1408         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1409         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1410         *id = handle->id;
1411         return ZIGBEE_ERROR_NONE;
1412 }
1413
1414 API int zb_set_id_to_extended_attr_info(extended_attr_info_h handle,
1415                 unsigned short id)
1416 {
1417         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1418         handle->id = id;
1419         return ZIGBEE_ERROR_NONE;
1420 }
1421
1422 API int zb_get_type_from_extended_attr_info(extended_attr_info_h handle,
1423                 unsigned char *type)
1424 {
1425         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1426         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
1427         *type = handle->type;
1428         return ZIGBEE_ERROR_NONE;
1429 }
1430
1431 API int zb_set_type_to_extended_attr_info(extended_attr_info_h handle,
1432                 unsigned char type)
1433 {
1434         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1435         handle->type = type;
1436         return ZIGBEE_ERROR_NONE;
1437 }
1438
1439 API int zb_get_acl_from_extended_attr_info(extended_attr_info_h handle,
1440                 unsigned char *acl)
1441 {
1442         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1443         RETV_IF(NULL == acl, ZIGBEE_ERROR_INVALID_PARAMETER);
1444         *acl = handle->acl;
1445         return ZIGBEE_ERROR_NONE;
1446 }
1447
1448 API int zb_set_acl_to_extended_attr_info(extended_attr_info_h handle,
1449                 unsigned char acl)
1450 {
1451         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1452         handle->acl = acl;
1453         return ZIGBEE_ERROR_NONE;
1454 }
1455