eaf219c25cbda691b2f8701dc78c7d675e579b70
[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         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
956         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
957         RETV_IF(NULL == handle->change, ZIGBEE_ERROR_NO_DATA);
958
959         len = zb_get_data_size(handle->type);
960         data_type = zb_get_analog_or_discret(handle->type);
961         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
962                 memcpy(value->val, handle->change, len);
963                 value->type = handle->type;
964                 value->size = len;
965         } else
966                 return ZIGBEE_ERROR_NOT_SUPPORTED;
967
968         return ZIGBEE_ERROR_NONE;
969 }
970
971 API int zb_set_change_to_report_config_record1(report_config_record_h handle,
972                 zb_value_h value)
973 {
974         int len = -1;
975         int data_type = DATA_TYPE_NONE;
976
977         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
978         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
979
980         if (handle->change)
981                 free(handle->change);
982
983         len = zb_get_data_size(value->type);
984         data_type = zb_get_analog_or_discret(value->type);
985         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
986                 handle->change = calloc(len + 1, sizeof(char));
987                 RETVM_IF(NULL == handle->change, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
988                 memcpy(handle->change, value->val, len);
989                 handle->type = value->type;
990         } else
991                 return ZIGBEE_ERROR_NOT_SUPPORTED;
992
993         return ZIGBEE_ERROR_NONE;
994 }
995
996 API int zb_get_change_from_report_config_record2(report_config_record_h handle,
997                 unsigned char *type, unsigned char **value, int *size)
998 {
999         int len = -1;
1000         int data_type = DATA_TYPE_NONE;
1001         unsigned char *t = NULL;
1002
1003         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1004         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1005         RETV_IF(NULL == handle->change, ZIGBEE_ERROR_NO_DATA);
1006
1007         len = zb_get_data_size(handle->type);
1008         data_type = zb_get_analog_or_discret(handle->type);
1009         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
1010                 t = calloc(len + 1, sizeof(char));
1011                 RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1012                 memcpy(t, handle->change, len);
1013                 *value = t;
1014                 *type = handle->type;
1015                 *size = len;
1016         } else
1017                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1018
1019         return ZIGBEE_ERROR_NONE;
1020 }
1021 API int zb_set_change_to_report_config_record2(
1022                 report_config_record_h handle, unsigned char type, unsigned char *value)
1023 {
1024         int len = -1;
1025         int data_type = DATA_TYPE_NONE;
1026
1027         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1028         if (handle->change)
1029                 free(handle->change);
1030
1031         len = zb_get_data_size(type);
1032         data_type = zb_get_analog_or_discret(type);
1033
1034         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
1035                 handle->change = calloc(len + 1, sizeof(unsigned char));
1036                 RETVM_IF(NULL == handle->change, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1037                 memcpy(handle->change, value, len);
1038                 handle->type = type;
1039         } else
1040                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1041
1042         return ZIGBEE_ERROR_NONE;
1043 }
1044
1045 API int zb_get_change_from_report_config_record3(report_config_record_h handle,
1046                 unsigned char *type, unsigned char *value, int *size)
1047 {
1048         int len = -1;
1049         int data_type = DATA_TYPE_NONE;
1050         unsigned char *t = NULL;
1051
1052         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1053         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1054         RETV_IF(NULL == handle->change, ZIGBEE_ERROR_NO_DATA);
1055
1056         len = zb_get_data_size(handle->type);
1057         data_type = zb_get_analog_or_discret(handle->type);
1058         if (0 < len /* && DATA_TYPE_ANALOG == data_type */) {
1059                 memcpy(value, handle->change, len);
1060                 *type = handle->type;
1061                 *size = len;
1062         } else
1063                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1064
1065         return ZIGBEE_ERROR_NONE;
1066 }
1067
1068 API int zb_create_read_report_config_record(read_report_config_record_h *handle)
1069 {
1070         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1071         read_report_config_record_h t =
1072                 calloc(1, sizeof(struct read_reporting_configuration_record_s));
1073         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1074         *handle = t;
1075         return ZIGBEE_ERROR_NONE;
1076 }
1077
1078 API void zb_destroy_read_report_config_record(read_report_config_record_h handle)
1079 {
1080         free(handle);
1081 }
1082
1083 API int zb_set_dir_to_read_report_config_record(read_report_config_record_h handle,
1084                 unsigned char dir)
1085 {
1086         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1087         if (dir == ZB_ZCL_CLIENT_TO_SERVER)
1088                 handle->dir = 0x00;
1089         else
1090                 handle->dir = 0x01;
1091         return ZIGBEE_ERROR_NONE;
1092 }
1093
1094 API int zb_get_dir_from_read_report_config_record(read_report_config_record_h handle,
1095                 unsigned char *dir)
1096 {
1097         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1098         *dir = handle->dir;
1099         return ZIGBEE_ERROR_NONE;
1100 }
1101
1102 API int zb_set_id_to_read_report_config_record(read_report_config_record_h handle,
1103                 unsigned short id)
1104 {
1105         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1106         handle->id = id;
1107         return ZIGBEE_ERROR_NONE;
1108 }
1109
1110 API int zb_get_id_from_read_report_config_record(read_report_config_record_h handle,
1111                 unsigned short *id)
1112 {
1113         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1114         *id = handle->id;
1115         return ZIGBEE_ERROR_NONE;
1116 }
1117
1118 API int zb_create_report_config_response_record(
1119                 report_config_response_record_h *handle)
1120 {
1121         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1122         report_config_response_record_h t =
1123                 calloc(1, sizeof(struct reporting_configuration_response_record_s));
1124         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1125         *handle = t;
1126         return ZIGBEE_ERROR_NONE;
1127 }
1128
1129 API void zb_destroy_report_config_response_record(
1130                 report_config_response_record_h handle)
1131 {
1132         free(handle);
1133 }
1134
1135 API int zb_get_status_from_report_config_response_record(
1136                 report_config_response_record_h handle, unsigned char *status)
1137 {
1138         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1139         RETV_IF(NULL == status, ZIGBEE_ERROR_INVALID_PARAMETER);
1140         *status = handle->status;
1141         return ZIGBEE_ERROR_NONE;
1142 }
1143
1144 API int zb_set_status_to_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         handle->status = status;
1149         return ZIGBEE_ERROR_NONE;
1150 }
1151
1152 API int zb_get_dir_from_report_config_response_record(
1153                 report_config_response_record_h handle, unsigned char *dir)
1154 {
1155         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1156         RETV_IF(NULL == dir, ZIGBEE_ERROR_INVALID_PARAMETER);
1157         *dir = handle->dir;
1158         return ZIGBEE_ERROR_NONE;
1159 }
1160
1161 API int zb_set_dir_to_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         handle->dir = handle->dir;
1166         return ZIGBEE_ERROR_NONE;
1167 }
1168
1169 API int zb_get_id_from_report_config_response_record(
1170         report_config_response_record_h handle, unsigned short *id)
1171 {
1172         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1173         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1174         *id = handle->id;
1175         return ZIGBEE_ERROR_NONE;
1176 }
1177
1178 API int zb_set_id_to_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         handle->id = id;
1183         return ZIGBEE_ERROR_NONE;
1184 }
1185
1186 API int zb_create_attr_report(attr_report_h *handle)
1187 {
1188         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1189         attr_report_h t = calloc(1, sizeof(struct attribute_report_s));
1190         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1191         *handle = t;
1192         return ZIGBEE_ERROR_NONE;
1193 }
1194
1195 API void zb_destroy_attr_report(attr_report_h handle)
1196 {
1197         if (handle) {
1198                 if (handle->value)
1199                         free(handle->value);
1200                 free(handle);
1201         }
1202 }
1203
1204 API int zb_get_id_from_attr_report(attr_report_h handle, unsigned short *id)
1205 {
1206         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1207         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1208         *id = handle->id;
1209         return ZIGBEE_ERROR_NONE;
1210 }
1211
1212 API int zb_set_id_to_attr_report(attr_report_h handle, unsigned short id)
1213 {
1214         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1215         handle->id = id;
1216         return ZIGBEE_ERROR_NONE;
1217 }
1218
1219 API int zb_get_type_from_attr_report(attr_report_h handle, unsigned char *type)
1220 {
1221         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1222         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
1223         *type = handle->type;
1224         return ZIGBEE_ERROR_NONE;
1225 }
1226
1227 API int zb_set_type_to_attr_report(attr_report_h handle, unsigned char type)
1228 {
1229         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1230         handle->type = type;
1231         return ZIGBEE_ERROR_NONE;
1232 }
1233
1234 API int zb_get_value_from_attr_report(attr_report_h handle, zb_value_h value)
1235 {
1236         int len = -1;
1237
1238         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1239         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1240         RETV_IF(NULL == handle->value, ZIGBEE_ERROR_NO_DATA);
1241
1242         len = zb_get_data_size(handle->type);
1243         if (0 < len) {
1244                 memcpy(value->val, handle->value, len);
1245                 value->size = len;
1246                 value->type = handle->type;
1247         } else if (ZB_ZCL_OCTAT_STRING == handle->type || ZB_ZCL_CHRACTER_STRING == handle->type) {
1248                 if (value->str) {
1249                         if (value->str->v)
1250                                 free(value->str->v);
1251                         free(value->str);
1252                 }
1253                 value->str = calloc(1, sizeof(struct attribute_str_s));
1254                 RETV_IF(NULL == value->str, ZIGBEE_ERROR_OUT_OF_MEMORY);
1255                 value->str->n = *handle->value;
1256                 /* The first 1 byte indicate invalid or length of string */
1257                 if (0xff == value->str->n) {
1258                         free(value->str);
1259                         return ZIGBEE_ERROR_NO_DATA;
1260                 }
1261                 value->str->v = calloc(value->str->n+1, 1);
1262                 if (NULL == value->str->v) {
1263                         free(value->str);
1264                         return ZIGBEE_ERROR_OUT_OF_MEMORY;
1265                 }
1266                 memcpy(value->str->v, handle->value+sizeof(value->str->n), value->str->n);
1267
1268                 value->type = handle->type;
1269                 value->size = value->str->n;
1270         } else if (ZB_ZCL_LONG_OCTAT_STRING == handle->type || ZB_ZCL_LONG_CHRACTER_STRING == handle->type) {
1271                 if (value->wstr) {
1272                         if (value->wstr->v)
1273                                 free(value->wstr->v);
1274                         free(value->wstr);
1275                 }
1276                 value->wstr = calloc(1, sizeof(struct attribute_wstr_s));
1277                 RETV_IF(NULL == value->wstr, ZIGBEE_ERROR_OUT_OF_MEMORY);
1278                 value->wstr->n = *(handle->value+1) << 8 | *handle->value;
1279                 /* The first 2 byte indicate invalid or length of string */
1280                 if (0xffff == value->wstr->n) {
1281                         free(value->wstr);
1282                         return ZIGBEE_ERROR_NO_DATA;
1283                 }
1284                 value->wstr->v = calloc(value->wstr->n+1, 1);
1285                 if (NULL == value->wstr->v) {
1286                         free(value->wstr);
1287                         return ZIGBEE_ERROR_OUT_OF_MEMORY;
1288                 }
1289                 memcpy(value->wstr->v, handle->value+sizeof(value->wstr->n), value->wstr->n);
1290
1291                 value->type = handle->type;
1292                 value->size = value->wstr->n;
1293         } else
1294                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1295
1296         return ZIGBEE_ERROR_NONE;
1297 }
1298
1299 API int zb_set_value_to_attr_report(attr_report_h handle, zb_value_h value)
1300 {
1301         int len = -1;
1302
1303         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1304         RETV_IF(NULL == value, ZIGBEE_ERROR_INVALID_PARAMETER);
1305
1306         if (handle->value)
1307                 free(handle->value);
1308
1309         len = zb_get_data_size(value->type);
1310         if (0 < len) {
1311                 handle->value = calloc(value->size + 1 , sizeof(char));
1312                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1313                 memcpy(handle->value, value->val, value->size);
1314                 handle->type = value->type;
1315         } else if (ZB_ZCL_OCTAT_STRING == handle->type || ZB_ZCL_CHRACTER_STRING == handle->type) {
1316                 RETV_IF(NULL == value->str, ZIGBEE_ERROR_NO_DATA);
1317                 handle->value = calloc(value->str->n + sizeof(value->str->n), sizeof(char));
1318                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1319                 /* The first 1 byte indicate invalid or length of string */
1320                 handle->value[0] = value->str->n & 0xff;
1321                 memcpy(handle->value + sizeof(value->str->n),
1322                         value->str->v + sizeof(value->str->n), value->str->n);
1323         } else if (ZB_ZCL_LONG_OCTAT_STRING == handle->type || ZB_ZCL_LONG_CHRACTER_STRING == handle->type) {
1324                 RETV_IF(NULL == value->wstr, ZIGBEE_ERROR_NO_DATA);
1325                 handle->value = calloc(value->wstr->n + sizeof(value->wstr->n), sizeof(char));
1326                 RETV_IF(NULL == handle->value, ZIGBEE_ERROR_OUT_OF_MEMORY);
1327                 /* The first 2 byte indicate invalid or length of string */
1328                 handle->value[0] = value->wstr->n & 0xff;
1329                 handle->value[1] = (value->wstr->n >> 8) & 0xff ;
1330                 memcpy(handle->value + sizeof(value->wstr->n),
1331                         value->wstr->v + sizeof(value->wstr->n), value->wstr->n);
1332         } else
1333                 return ZIGBEE_ERROR_NOT_SUPPORTED;
1334
1335         return ZIGBEE_ERROR_NONE;
1336 }
1337
1338
1339 /**
1340  * @brief Figure 2.33 Format of the Selector Field
1341  *
1342  * @since_tizen 3.0
1343  */
1344 struct selector_s {
1345         unsigned char indicator; /**< number of index */
1346         unsigned short *index; /**< index list */
1347 };
1348
1349 /**
1350  * @brief stcuture for reading attribute
1351  *
1352  * @since_tizen 3.0
1353  */
1354 struct read_structured_attribute_record_s {
1355         unsigned short id; /**< attribute identifier */
1356         struct selector_s *selector; /**< selector format */
1357 };
1358
1359 /**
1360  * @brief Figure 2.11 Format of the Write Attribute Record Field
1361  *
1362  * @since_tizen 3.0
1363  */
1364 struct write_attribute_structured_status_record_s {
1365         unsigned char status; /**< status */
1366         unsigned short id; /**< identifier */
1367         struct selector_s selector; /**< selector */
1368 };
1369
1370 /**
1371  * @brief Figure 2.32 Format of the Write Attribute Record Field
1372  *
1373  * @since_tizen 3.0
1374  */
1375 struct write_attribute_structured_record_s {
1376         unsigned short id; /**< attribute identifier */
1377         struct selector_s selector;
1378         unsigned char type; /**< attribute data type */
1379         unsigned char *value; /**< data value */; /**< attribute value */
1380 };
1381
1382 API int zb_create_extended_attr_info(extended_attr_info_h *handle)
1383 {
1384         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1385         extended_attr_info_h t = calloc(1, sizeof(struct extended_attribute_infomation_s));
1386         RETVM_IF(NULL == t, ZIGBEE_ERROR_OUT_OF_MEMORY, "calloc() Fail(%d)", errno);
1387         *handle = t;
1388         return ZIGBEE_ERROR_NONE;
1389 }
1390
1391 API void zb_destroy_extended_attr_info(extended_attr_info_h handle)
1392 {
1393         free(handle);
1394 }
1395
1396 API int zb_get_id_from_extended_attr_info(extended_attr_info_h handle,
1397                 unsigned short *id)
1398 {
1399         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1400         RETV_IF(NULL == id, ZIGBEE_ERROR_INVALID_PARAMETER);
1401         *id = handle->id;
1402         return ZIGBEE_ERROR_NONE;
1403 }
1404
1405 API int zb_set_id_to_extended_attr_info(extended_attr_info_h handle,
1406                 unsigned short id)
1407 {
1408         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1409         handle->id = id;
1410         return ZIGBEE_ERROR_NONE;
1411 }
1412
1413 API int zb_get_type_from_extended_attr_info(extended_attr_info_h handle,
1414                 unsigned char *type)
1415 {
1416         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1417         RETV_IF(NULL == type, ZIGBEE_ERROR_INVALID_PARAMETER);
1418         *type = handle->type;
1419         return ZIGBEE_ERROR_NONE;
1420 }
1421
1422 API int zb_set_type_to_extended_attr_info(extended_attr_info_h handle,
1423                 unsigned char type)
1424 {
1425         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1426         handle->type = type;
1427         return ZIGBEE_ERROR_NONE;
1428 }
1429
1430 API int zb_get_acl_from_extended_attr_info(extended_attr_info_h handle,
1431                 unsigned char *acl)
1432 {
1433         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1434         RETV_IF(NULL == acl, ZIGBEE_ERROR_INVALID_PARAMETER);
1435         *acl = handle->acl;
1436         return ZIGBEE_ERROR_NONE;
1437 }
1438
1439 API int zb_set_acl_to_extended_attr_info(extended_attr_info_h handle,
1440                 unsigned char acl)
1441 {
1442         RETV_IF(NULL == handle, ZIGBEE_ERROR_INVALID_PARAMETER);
1443         handle->acl = acl;
1444         return ZIGBEE_ERROR_NONE;
1445 }
1446