Fixed ACR issue.
[platform/core/api/zigbee.git] / test / main.c
1 /*
2  * Copyright (c) 2016 Samsung Electronics Co., Ltd. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <sys/time.h>
22 #include <unistd.h>
23 #include <glib.h>
24 #include <gio/gio.h>
25
26 #include "main.h"
27 #include "menu.h"
28
29 zb_zigbee_h handle;
30
31 extern struct menu_data menu_zdo_binding[];
32 extern struct menu_data menu_zdo_device[];
33 extern struct menu_data menu_zdo_network[];
34
35 extern struct menu_data menu_zcl_global[];
36 extern struct menu_data menu_zcl_cluster[];
37 extern struct menu_data menu_custom[];
38
39 static char data_remove_children[MENU_DATA_SIZE + 1] = "1";
40 static char data_endpoint[MENU_DATA_SIZE + 1] = "1";
41 static char data_rejoin[MENU_DATA_SIZE + 1] = "0";
42 static char data_pj_timeout[MENU_DATA_SIZE + 1] = "90";
43 static char data_choose_target[MENU_DATA_SIZE + 1] = "0";
44 static char data_dest_addr16[MENU_DATA_SIZE + 1] = "";
45
46 zb_nwk_addr panid = 0x615F;
47 zb_ieee_addr co_addr64 = { 0xD0, 0x52, 0xA8, 0xA9, 0x8F, 0xB7, 0x00, 0x01 };
48
49 zb_nwk_addr  st_addr16 = 0x708B;
50 zb_ieee_addr st_addr64 = { 0x00, 0x0D, 0x6F, 0x00, 0x04, 0x2B, 0xBE, 0x14 };
51
52 zb_nwk_addr  sj_addr16 = 0x4756;
53 zb_ieee_addr sj_addr64 = { 0x24, 0xFD, 0x5B, 0x00, 0x01, 0x01, 0x86, 0xB6 };
54
55 zb_nwk_addr  dest_addr16 = 0x1D1A;
56 zb_ieee_addr dest_addr64 = { 0x00, 0x12, 0x4B, 0x00, 0x02, 0xF2, 0x58, 0x5B };
57
58 simple_desc_s st_ep1, st_ep2, st_ep3, st_ep4, sj_ep1;
59 simple_desc_s dimmer_ep1, s_thermostat_ep1, huebulb_ep1;
60
61 zb_end_dev_info_h *target_list = NULL;
62 int target_list_count;
63
64 static const char* zb_event_str[] = {
65         /* 0x00 */
66         "ZB_ZDP_ENABLE_EVENT",
67         "ZB_ZDP_FORM_NETWORK_DONE",
68         "ZB_ZDP_JOIN_EVENT",
69         "ZB_ZDP_REJOIN_EVENT",
70         "ZB_ZDP_CHILD_LEFT",
71         "ZB_ZDP_LEAVE_DONE_EVENT",
72         "ZB_ZDP_UPDATED_NETWORK_NOTIFICATION_EVENT",
73         "ZB_ZDP_BIND_RSP_EVENT",
74         "ZB_ZDP_UNBIND_RSP_EVENT",
75         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
76
77         /* 0x10 */
78         "ZB_ZCL_GLOBAL_DEFAULT_RSP_EVENT",
79         "ZB_ZCL_GLOBAL_ATTRIBUTE_REPORT_EVENT",
80         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
81         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
82
83         /* 0x20 */
84         "ZB_ZCL_ALARM_EVENT",
85         "ZB_ZCL_IAS_ZONE_ENROLL_REQUEST_EVENT",
86         "ZB_ZCL_IAS_ZONE_STATUS_CHANGE_NOTIFICATION_EVENT",
87         "ZB_ZCL_IAS_ZONE_STATUS_CHANGE_EXTENDED_NOTIFICATION_EVENT",
88         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
89         NULL, NULL, NULL, NULL, NULL,
90
91         /* 0x30 */
92         "ZB_ZCL_LEVEL_CONTROL_CURRENT_LEVEL_ATTRIBUTE_REPORT_EVENT",
93         "ZB_ZCL_COLOR_CONTROL_CURRENT_X_ATTRIBUTE_REPORT_EVENT",
94         "ZB_ZCL_COLOR_CONTROL_CURRENT_Y_ATTRIBUTE_REPORT_EVENT",
95         "ZB_ZCL_COLOR_CONTROL_CURRENT_HUE_ATTRIBUTE_REPORT_EVENT",
96         "ZB_ZCL_COLOR_CONTROL_CURRENT_SATURATION_ATTRIBUTE_REPORT_EVENT",
97         "ZB_ZCL_COLOR_CONTROL_CURRENT_TEMPERATURE_ATTRIBUTE_REPORT_EVENT",
98         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
99         NULL, NULL, NULL,
100
101         /* 0x40 */
102         "ZB_ZCL_DOOR_LOCK_OPERATIONAL_NOTIFICATION_EVENT",
103         "ZB_ZCL_DOOR_LOCK_PROGRAMING_NOTIFICATION_EVENT",
104         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
105         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
106
107         /* 0x50 */
108         "ZB_ZCL_SIMPLE_METERING_REMOVE_MIRROR_EVENT",
109         "ZB_ZCL_SIMPLE_METERING_REQUEST_MIRROR_EVENT",
110         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
111         NULL, NULL, NULL, NULL, NULL, NULL, NULL,
112
113         /* 0x60 */
114         NULL,
115 };
116
117 const char* zigbee_error_to_string(zb_error_e err)
118 {
119         switch (err) {
120         /* CHECK: List all enum values here */
121         CASE_TO_STR(ZIGBEE_ERROR_NONE)
122         CASE_TO_STR(ZIGBEE_ERROR_INVALID_PARAMETER)
123         CASE_TO_STR(ZIGBEE_ERROR_PARAMETER_OUT_OF_RANGE)
124         CASE_TO_STR(ZIGBEE_ERROR_OUT_OF_MEMORY)
125         CASE_TO_STR(ZIGBEE_ERROR_IO_ERROR)
126         CASE_TO_STR(ZIGBEE_ERROR_NO_DATA)
127         CASE_TO_STR(ZIGBEE_ERROR_NOT_SUPPORTED)
128         CASE_TO_STR(ZIGBEE_ERROR_PERMISSION_DENIED)
129         CASE_TO_STR(ZIGBEE_ERROR_INVALID_ENDPOINT)
130         CASE_TO_STR(ZIGBEE_ERROR_INVALID_ADDRESS)
131         CASE_TO_STR(ZIGBEE_ERROR_OPERATION_FAILED)
132         default :
133                 return "ZIGBEE_ERROR_UNKNOWN";
134         }
135 }
136
137 static void _init_endpoints()
138 {
139         /* Samjin power outlet EP1 */
140         sj_ep1.ep = 1;
141         sj_ep1.profileid = 0x0104;
142         sj_ep1.deviceid = 0x0002;
143         sj_ep1.num_of_in_clusters = 7;
144         sj_ep1.num_of_out_clusters = 1;
145         /* In clusters : 0000 0003 0004 0005 0006 0009 0B04 0B05 */
146         sj_ep1.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
147         sj_ep1.in_clusters[1] = ZB_ZCL_CLUSTER_IDENTIFY;
148         sj_ep1.in_clusters[2] = ZB_ZCL_CLUSTER_GROUPS;
149         sj_ep1.in_clusters[3] = ZB_ZCL_CLUSTER_SCENES;
150         sj_ep1.in_clusters[4] = ZB_ZCL_CLUSTER_ON_OFF;
151         sj_ep1.in_clusters[5] = ZB_ZCL_CLUSTER_ALARM;
152         sj_ep1.in_clusters[6] = ZB_ZCL_CLUSTER_DIAGNOSTICS;
153         /* Out clusters : 0019 */
154         /*unsigned short sj_out_clusters[] = { ZB_ZCL_CLUSTER_ID_OTA_BOOTLOAD };*/
155
156         /* SmartThings open/close sensor EP1 */
157         st_ep1.ep = 1;
158         st_ep1.profileid = 0x0104;
159         st_ep1.deviceid = 0x0402;
160         st_ep1.num_of_in_clusters = 7;
161         st_ep1.num_of_out_clusters = 1;
162         st_ep1.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
163         st_ep1.in_clusters[1] = ZB_ZCL_CLUSTER_POWER_CONFIG;
164         st_ep1.in_clusters[2] = ZB_ZCL_CLUSTER_IDENTIFY;
165         st_ep1.in_clusters[3] = ZB_ZCL_CLUSTER_TEMP_MEASUREMENT;
166         st_ep1.in_clusters[4] = ZB_ZCL_CLUSTER_IAS_ZONE;
167         st_ep1.in_clusters[5] = ZB_ZCL_CLUSTER_POLL_CONTROL;
168         st_ep1.in_clusters[6] = ZB_ZCL_CLUSTER_DIAGNOSTICS;
169         st_ep1.out_clusters[0] = ZB_ZCL_CLUSTER_OTA_BOOTLOAD;
170
171         /* SmartThings open/close sensor EP2 */
172         st_ep2.ep = 2;
173         st_ep2.profileid = 0xC2DF;
174         st_ep2.deviceid = 0x000C;
175         st_ep2.num_of_in_clusters = 4;
176         st_ep2.num_of_out_clusters = 1;
177         st_ep2.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
178         st_ep2.in_clusters[1] = ZB_ZCL_CLUSTER_IDENTIFY;
179         st_ep2.in_clusters[2] = ZB_ZCL_CLUSTER_SCENES;
180         st_ep2.in_clusters[3] = 0xFC0F;
181         st_ep2.out_clusters[0] = ZB_ZCL_CLUSTER_IDENTIFY;
182
183         /* SmartThings Motion Sensor EP1 */
184         st_ep3.ep = 1;
185         st_ep3.profileid = 0x0104;
186         st_ep3.deviceid = 0x0402;
187         st_ep3.num_of_in_clusters = 7;
188         st_ep3.num_of_out_clusters = 1;
189         st_ep3.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
190         st_ep3.in_clusters[1] = ZB_ZCL_CLUSTER_POWER_CONFIG;
191         st_ep3.in_clusters[2] = ZB_ZCL_CLUSTER_IDENTIFY;
192         st_ep3.in_clusters[3] = ZB_ZCL_CLUSTER_POLL_CONTROL;
193         st_ep3.in_clusters[4] = ZB_ZCL_CLUSTER_TEMP_MEASUREMENT;
194         st_ep3.in_clusters[5] = ZB_ZCL_CLUSTER_IAS_ZONE;
195         st_ep3.in_clusters[6] = ZB_ZCL_CLUSTER_DIAGNOSTICS;
196         st_ep3.out_clusters[0] = ZB_ZCL_CLUSTER_OTA_BOOTLOAD;
197
198         /* SmartThings Motion Sensor EP2 */
199         st_ep4.ep = 2;
200         st_ep4.profileid = 0xC2DF;
201         st_ep4.deviceid = 0x0107;
202         st_ep4.num_of_in_clusters = 5;
203         st_ep4.num_of_out_clusters = 1;
204         st_ep4.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
205         st_ep4.in_clusters[1] = ZB_ZCL_CLUSTER_POWER_CONFIG;
206         st_ep4.in_clusters[2] = ZB_ZCL_CLUSTER_IDENTIFY;
207         st_ep4.in_clusters[3] = ZB_ZCL_CLUSTER_DIAGNOSTICS;
208         st_ep4.in_clusters[4] = 0xFC46;
209         st_ep4.out_clusters[0] = ZB_ZCL_CLUSTER_OTA_BOOTLOAD;
210
211         /* Things Simulator - GE_ZigBeeDimmer */
212         dimmer_ep1.ep = 1;
213         dimmer_ep1.profileid = 0x0104;
214         dimmer_ep1.deviceid = 0xffff;
215         dimmer_ep1.num_of_in_clusters = 7;
216         dimmer_ep1.num_of_out_clusters = 1;
217         dimmer_ep1.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
218         dimmer_ep1.in_clusters[1] = ZB_ZCL_CLUSTER_IDENTIFY;
219         dimmer_ep1.in_clusters[2] = ZB_ZCL_CLUSTER_GROUPS;
220         dimmer_ep1.in_clusters[3] = ZB_ZCL_CLUSTER_SCENES;
221         dimmer_ep1.in_clusters[4] = ZB_ZCL_CLUSTER_ON_OFF;
222         dimmer_ep1.in_clusters[5] = ZB_ZCL_CLUSTER_LEVEL_CONTROL;
223         dimmer_ep1.in_clusters[6] = ZB_ZCL_CLUSTER_DIAGNOSTICS;
224         /*dimmer_ep1.in_clusters[7] = ZB_ZCL_CLUSTER_ID_SIMPLE_METERING;*/
225         dimmer_ep1.out_clusters[0] = ZB_ZCL_CLUSTER_TIME;
226         /*dimmer_ep1.out_clusters[1] = ZB_ZCL_CLUSTER_ID_OTA_BOOTLOAD;*/
227
228         /* Things Simulator - CentraLite_Thermostat */
229         s_thermostat_ep1.ep = 1;
230         s_thermostat_ep1.profileid = 0x0104;
231         s_thermostat_ep1.deviceid = 0xffff;
232         s_thermostat_ep1.num_of_in_clusters = 8;
233         s_thermostat_ep1.num_of_out_clusters = 2;
234         s_thermostat_ep1.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
235         s_thermostat_ep1.in_clusters[1] = ZB_ZCL_CLUSTER_POWER_CONFIG;
236         s_thermostat_ep1.in_clusters[2] = ZB_ZCL_CLUSTER_IDENTIFY;
237         s_thermostat_ep1.in_clusters[3] = ZB_ZCL_CLUSTER_POLL_CONTROL;
238         s_thermostat_ep1.in_clusters[4] = ZB_ZCL_CLUSTER_THERMOSTAT;
239         s_thermostat_ep1.in_clusters[5] = ZB_ZCL_CLUSTER_FAN_CONTROL;
240         s_thermostat_ep1.in_clusters[6] = ZB_ZCL_CLUSTER_THERMOSTAT_UI_CONFIG;
241         s_thermostat_ep1.in_clusters[7] = ZB_ZCL_CLUSTER_DIAGNOSTICS;
242         s_thermostat_ep1.out_clusters[0] = ZB_ZCL_CLUSTER_TIME;
243         s_thermostat_ep1.out_clusters[1] = ZB_ZCL_CLUSTER_OTA_BOOTLOAD;
244
245         /* Things Simulator - Zigbee_HueBulb */
246         huebulb_ep1.ep = 1;
247         huebulb_ep1.profileid = 0x0104;
248         huebulb_ep1.deviceid = 0xffff;
249         huebulb_ep1.num_of_in_clusters = 8;
250         huebulb_ep1.num_of_out_clusters = 1;
251         huebulb_ep1.in_clusters[0] = ZB_ZCL_CLUSTER_BASIC;
252         huebulb_ep1.in_clusters[1] = ZB_ZCL_CLUSTER_IDENTIFY;
253         huebulb_ep1.in_clusters[2] = ZB_ZCL_CLUSTER_GROUPS;
254         huebulb_ep1.in_clusters[3] = ZB_ZCL_CLUSTER_SCENES;
255         huebulb_ep1.in_clusters[4] = ZB_ZCL_CLUSTER_ON_OFF;
256         huebulb_ep1.in_clusters[5] = ZB_ZCL_CLUSTER_LEVEL_CONTROL;
257         huebulb_ep1.in_clusters[6] = ZB_ZCL_CLUSTER_COLOR_CONTROL;
258         huebulb_ep1.in_clusters[7] = ZB_ZCL_CLUSTER_ZLL_COMMISSIONING;
259         huebulb_ep1.out_clusters[0] = ZB_ZCL_CLUSTER_OTA_BOOTLOAD;
260 }
261
262 static void _zb_form_network_done_cb(zb_nwk_addr panid, void *user_data)
263 {
264         msgp("\nform_network_done received PANID = 0x%04X\n", panid);
265 }
266
267 static void _zb_disable_network_done_cb(unsigned char ret, void *user_data)
268 {
269         msgp("\ndisable_network result received = 0x%02X\n", ret);
270 }
271
272 static void _zb_event_cb(zb_nwk_addr addr16, zb_ieee_addr addr64, zb_event_e e,
273         zb_event_data_h ev, void* user_data)
274 {
275         int event_type = e;
276         zb_error_e status;
277         unsigned char count;
278
279         zb_end_point ep;
280         zb_cluster_id clusterid;
281         zb_command_id commandid;
282
283         msg("");
284         msg("");
285         msgp("Event received = %s [0x%02X]", zb_event_str[e], e);
286
287         switch (event_type) {
288         case ZB_ZDP_ENABLE_EVENT:
289         {
290                 zb_ev_get_enable_status(
291                         ev,
292                         &status
293                         );
294                 msg("  zigbee state = %d [%s]", status, (status ? "Enabled" : "Disabled"));
295                 break;
296         }
297         case ZB_ZDP_FORM_NETWORK_DONE:
298         {
299                 zb_nwk_addr pan_id = 0;
300                 zb_ev_get_form_network_panid(ev, &pan_id);
301                 msg("0x%d  form network done", pan_id);
302                 break;
303         }
304         case ZB_ZDP_JOIN_EVENT:
305         {
306                 zb_end_point *ep = NULL;
307                 zb_ev_get_join_count(
308                         ev,
309                         &count
310                         );
311                 zb_ev_get_join_ep(
312                         ev,
313                         &ep
314                         );
315
316                 msg("  %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X joined",
317                         addr64[0], addr64[1], addr64[2], addr64[3],
318                         addr64[4], addr64[5], addr64[6], addr64[7]);
319                 msg("end-point count = %d", count);
320                 for (int i = 0; i < count; i++)
321                         msg("[%d]", ep[i]);
322
323                 if (ep) {
324                         free(ep);
325                         ep = NULL;
326                 }
327
328                 break;
329         }
330         case ZB_ZDP_REJOIN_EVENT:
331         {
332                 msg("  %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X rejoined",
333                         addr64[0], addr64[1], addr64[2], addr64[3],
334                         addr64[4], addr64[5], addr64[6], addr64[7]);
335                 break;
336         }
337         case ZB_ZDP_CHILD_LEFT: {
338                 zb_error_e status = 0;
339                 zb_ev_get_child_left_status(ev, &status);
340                 msg("  %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X left Status Code = 0x%02X",
341                         addr64[0], addr64[1], addr64[2], addr64[3],
342                         addr64[4], addr64[5], addr64[6], addr64[7], status);
343                 break;
344         }
345         case ZB_ZDP_LEAVE_DONE_EVENT: {
346                 msg("  0x%04X leave done\n", addr16);
347                 break;
348         }
349         case ZB_ZDP_UPDATED_NETWORK_NOTIFICATION_EVENT: {
350                 msg("  Update network notification received\n");
351                 break;
352         }
353         case ZB_ZDP_BIND_RSP_EVENT:
354         {
355                 /* zb_parse_bind_cb(ev, &status); */
356                 /* msg("binding %04X, result=%0X\n", addr16, status); */
357                 msg("  Not implemented");
358                 break;
359         }
360         case ZB_ZDP_UNBIND_RSP_EVENT:
361         {
362                 /* zb_parse_unbind_cb(ev, &status); */
363                 /* msg("unbinding %04X, result=%0X\n", addr16, status); */
364                 msg("  Not implemented");
365                 break;
366         }
367         case ZB_ZCL_GLOBAL_DEFAULT_RSP_EVENT:
368         {
369                 zb_zcl_status_e zcl_status;
370                 zb_ev_get_global_default_rsp_ep(
371                         ev,
372                         &ep
373                         );
374                 zb_ev_get_global_default_rsp_cluster_id(
375                         ev,
376                         &clusterid
377                         );
378                 zb_ev_get_global_default_rsp_command_id(
379                         ev,
380                         &commandid
381                         );
382                 zb_ev_get_global_default_rsp_status(
383                         ev,
384                         &zcl_status
385                         );
386
387                 msg("  addr16     = 0x%04X", addr16);
388                 msg("  endpoint   = 0x%02X", ep);
389                 msg("  cluster_id = 0x%04X (%s)", clusterid, zb_zcl_get_cluster_id_str(clusterid));
390                 msg("  commandid = 0x%02X", commandid);
391                 msg("  status     = 0x%02X (%s)",
392                         zcl_status, zb_zcl_get_status_str(zcl_status));
393                 break;
394         }
395         case ZB_ZCL_GLOBAL_ATTRIBUTE_REPORT_EVENT:
396         {
397                 int ret;
398                 int i, j, count;
399                 zb_attribute_id id;
400                 zb_zcl_data_type_e type;
401                 unsigned char rec_len = 0;
402                 unsigned char *buf = NULL;
403
404                 zb_zcl_attr_report_h *records = NULL;
405
406                 ret = zb_ev_get_global_attr_report_records(
407                         ev,
408                         records,
409                         &rec_len
410                         );
411                 if (ZIGBEE_ERROR_NONE != ret) {
412                         msg("zb_ev_get_global_attr_report_records(0x%X) - FAILED!!! [%s]",
413                                 ret, zigbee_error_to_string(ret));
414                         return;
415                 }
416                 if (NULL == records) {
417                         msg("records is NULL");
418                         return;
419                 }
420
421                 msg("Global Attribute Report Event Received from = 0x%04X record_len=%d",
422                         addr16, rec_len);
423                 for (j = 0; j < rec_len; j++) {
424                         zb_attr_report_get_id(
425                                 records[j],
426                                 &id
427                                 );
428                         msg("  id   = [0x%04X]", id);
429                         zb_attr_report_get_type(
430                                 records[j],
431                                 &type
432                                 );
433                         msg("  type = [0x%02X][%s]", type, zb_zcl_get_data_type_str(type));
434                         zb_attr_report_get_value(
435                                 records[j],
436                                 &type,
437                                 &buf,
438                                 &count
439                                 );
440                         if (ZIGBEE_ERROR_NONE == ret) {
441                                 for (i = 0; i < count; i++)
442                                         msg("    0x%02X ", buf[i]);
443                         }
444                         msg("");
445                         if (buf) {
446                                 free(buf);
447                                 buf = NULL;
448                         }
449                 }
450                 break;
451         }
452         case ZB_ZCL_ALARM_EVENT:
453         {
454                 unsigned short count = 0;
455                 zb_ev_get_alarm_count(ev, &count);
456                 msg("Alarm Event Received = %d\n", count);
457                 break;
458         }
459         case ZB_ZCL_IAS_ZONE_ENROLL_REQUEST_EVENT:
460         {
461                 /*zb_nwk_addr addr16;*/
462                 zb_zcl_ias_zone_type_e zone_type;
463                 unsigned char ep, mfg_code;
464                 msg("IAS Zone Request Received\n");
465
466                 zb_ev_get_enroll_request_src_ep(
467                         ev,
468                         &ep
469                         );
470                 zb_ev_get_enroll_request_zone_type(
471                         ev,
472                         &zone_type
473                         );
474                 zb_ev_get_enroll_request_mfg_code(
475                         ev,
476                         &mfg_code
477                         );
478
479                 /*msg("Network Address from 0x%04X\n", addr16);*/
480                 msg("  End-Point from 0x%02X", ep);
481                 msg("  Zone Type = 0x%04X", zone_type);
482                 msg("  Manufacturer Code = 0x%02X", mfg_code);
483                 zb_zcl_zone_enroll_response(
484                         handle,
485                         addr16,
486                         ep,
487                         ZB_ZCL_IAS_ZONE_ENROLL_SUCCESS,
488                         0x0001
489                         );
490                 break;
491         }
492         case ZB_ZCL_IAS_ZONE_STATUS_CHANGE_NOTIFICATION_EVENT:
493         {
494                 unsigned short zone_status;
495
496                 zb_ev_get_ias_noti_src_ep(
497                         ev,
498                         &ep
499                         );
500                 zb_ev_get_ias_noti_zone_status(
501                         ev,
502                         &zone_status
503                         );
504
505                 msg("  addr16      = 0x%04X", addr16);
506                 msg("  endpoint    = 0x%02X", ep);
507                 msg("  zone_status = 0x%04X", zone_status);
508
509                 break;
510         }
511         case ZB_ZCL_IAS_ZONE_STATUS_CHANGE_EXTENDED_NOTIFICATION_EVENT:
512         {
513                 unsigned char status, zone_id;
514                 unsigned short zone_status, delay;
515
516                 zb_ev_get_ias_ext_noti_src_ep(
517                         ev,
518                         &ep
519                         );
520                 zb_ev_get_ias_ext_noti_status(
521                         ev,
522                         &status
523                         );
524                 zb_ev_get_ias_ext_noti_zone_status(
525                         ev,
526                         &zone_status
527                         );
528                 zb_ev_get_ias_ext_noti_zone_id(
529                         ev,
530                         &zone_id
531                         );
532                 zb_ev_get_ias_ext_noti_delay(
533                         ev,
534                         &delay
535                         );
536
537                 msg("  addr16          = 0x%04X", addr16);
538                 msg("  endpoint        = 0x%02X", ep);
539                 msg("  status          = 0x%02X", status);
540                 msg("  zone_status     = 0x%04X", zone_status);
541                 msg("  zone_id         = 0x%04X", zone_id);
542                 msg("  delay           = 0x%04X", delay);
543
544                 /* Closed or not alarmed */
545                 if (0x01 == (zone_status & 0x01))
546                         zb_zcl_on_off_control(
547                                 handle,
548                                 dest_addr16,
549                                 1,
550                                 ZB_ZCL_OFF
551                                 );
552                 /* Opened or alarmed */
553                 else if (0x00 == (zone_status & 0x01))
554                         zb_zcl_on_off_control(
555                                 handle,
556                                 dest_addr16,
557                                 1,
558                                 ZB_ZCL_ON
559                                 );
560                 break;
561         }
562         case ZB_ZCL_LEVEL_CONTROL_CURRENT_LEVEL_ATTRIBUTE_REPORT_EVENT:
563                 break;
564         case ZB_ZCL_COLOR_CONTROL_CURRENT_X_ATTRIBUTE_REPORT_EVENT:
565                 break;
566         case ZB_ZCL_COLOR_CONTROL_CURRENT_Y_ATTRIBUTE_REPORT_EVENT:
567                 break;
568         case ZB_ZCL_COLOR_CONTROL_CURRENT_HUE_ATTRIBUTE_REPORT_EVENT:
569                 break;
570         case ZB_ZCL_COLOR_CONTROL_CURRENT_SATURATION_ATTRIBUTE_REPORT_EVENT:
571                 break;
572         case ZB_ZCL_COLOR_CONTROL_CURRENT_TEMPERATURE_ATTRIBUTE_REPORT_EVENT:
573                 break;
574         case ZB_ZCL_DOOR_LOCK_OPERATIONAL_NOTIFICATION_EVENT:
575                 break;
576         case ZB_ZCL_DOOR_LOCK_PROGRAMING_NOTIFICATION_EVENT:
577                 break;
578         case ZB_ZCL_SIMPLE_METERING_REMOVE_MIRROR_EVENT:
579                 break;
580         case ZB_ZCL_SIMPLE_METERING_REQUEST_MIRROR_EVENT:
581                 break;
582         default:
583                 msgp("Unhandled Event received = [0x%02X]", e);
584                 break;
585         }
586         msg("");
587 }
588
589 static int run_create(MManager *mm, struct menu_data *menu)
590 {
591         int ret = ZIGBEE_ERROR_NONE;
592         if (handle) {
593                 msg("Already handle exist.");
594                 return RET_FAILURE;
595         }
596
597         ret = zb_create(
598                 &handle
599                 );
600         if (ZIGBEE_ERROR_NONE != ret) {
601                 msg("zb_create(0x%X) - FAILED!!! [%s]",
602                         ret, zigbee_error_to_string(ret));
603                 return RET_FAILURE;
604         }
605
606         return RET_SUCCESS;
607 }
608
609 static int run_destroy(MManager *mm, struct menu_data *menu)
610 {
611         if (NULL == handle) {
612                 msg("Already destroyed or not created.");
613                 return RET_FAILURE;
614         }
615
616         zb_destroy(
617                 handle
618                 );
619         handle = NULL;
620
621         return RET_SUCCESS;
622 }
623
624 static int run_enable(MManager *mm, struct menu_data *menu)
625 {
626         int ret = ZIGBEE_ERROR_NONE;
627
628         /* Set event callback */
629         ret = zb_set_event_cb(
630                 handle,
631                 _zb_event_cb
632                 );
633         if (ZIGBEE_ERROR_NONE != ret) {
634                 msg("zb_set_event_cb(0x%X) - FAILED!!! [%s]",
635                         ret, zigbee_error_to_string(ret));
636                 return RET_FAILURE;
637         }
638
639         msg(" - zb_set_event_cb() ret: [0x%X] [%s]",
640                 ret, zigbee_error_to_string(ret));
641
642         /* Enable */
643         ret = zb_enable(
644                         handle
645                         );
646         if (ZIGBEE_ERROR_NONE != ret) {
647                 msg("zb_enable(0x%X) - FAILED!!! [%s]",
648                         ret, zigbee_error_to_string(ret));
649                 return RET_FAILURE;
650         }
651
652         msg(" - zb_enable() ret: [0x%X] [%s]",
653                 ret, zigbee_error_to_string(ret));
654
655         return RET_SUCCESS;
656 }
657
658 static int run_disable(MManager *mm, struct menu_data *menu)
659 {
660         /* De-register event callback */
661         int ret = ZIGBEE_ERROR_NONE;
662
663         ret = zb_disable(
664                         handle
665                         );
666         if (ZIGBEE_ERROR_NONE != ret) {
667                 msg("zb_disable(0x%X) - FAILED!!! [%s]",
668                         ret, zigbee_error_to_string(ret));
669                 return RET_FAILURE;
670         }
671
672         msg(" - zb_disable() ret: [0x%X] [%s]",
673                 ret, zigbee_error_to_string(ret));
674
675         return RET_SUCCESS;
676 }
677
678 static int run_hw_reset(MManager *mm, struct menu_data *menu)
679 {
680         int ret = ZIGBEE_ERROR_NONE;
681
682         ret = zb_hw_reset(
683                         handle
684                         );
685         if (ZIGBEE_ERROR_NONE != ret) {
686                 msg("zb_hw_reset(0x%X) - FAILED!!! [%s]",
687                         ret, zigbee_error_to_string(ret));
688                 return RET_FAILURE;
689         }
690
691         msg(" - zb_hw_reset() ret: [0x%X] [%s]",
692                 ret, zigbee_error_to_string(ret));
693
694         return RET_SUCCESS;
695 }
696
697 static int run_coex_start(MManager *mm, struct menu_data *menu)
698 {
699         int ret = ZIGBEE_ERROR_NONE;
700         unsigned char channel = 11;
701
702         ret = zb_coex_start(
703                         handle,
704                         channel
705                         );
706         if (ZIGBEE_ERROR_NONE != ret) {
707                 msg("zb_coex_start(0x%X) - FAILED!!! [%s]",
708                         ret, zigbee_error_to_string(ret));
709                 return RET_FAILURE;
710         }
711
712         msg(" - zb_coex_start() ret: [0x%X] [%s]",
713                 ret, zigbee_error_to_string(ret));
714
715         return RET_SUCCESS;
716 }
717
718 static int run_coex_stop(MManager *mm, struct menu_data *menu)
719 {
720         int ret = ZIGBEE_ERROR_NONE;
721
722         ret = zb_coex_stop(
723                         handle
724                         );
725         if (ZIGBEE_ERROR_NONE != ret) {
726                 msg("zb_coex_stop(0x%X) - FAILED!!! [%s]",
727                         ret, zigbee_error_to_string(ret));
728                 return RET_FAILURE;
729         }
730
731         msg(" - zb_coex_stop() ret: [0x%X] [%s]",
732                 ret, zigbee_error_to_string(ret));
733
734         return RET_SUCCESS;
735 }
736
737 static int run_get_network_info(MManager *mm, struct menu_data *menu)
738 {
739         int ret = ZIGBEE_ERROR_NONE;
740         zb_nwk_addr nodeid;
741         zb_nwk_addr panid;
742         unsigned char channel;
743         unsigned char tx_power;
744
745         ret = zb_get_network_info(
746                         handle,
747                         co_addr64,
748                         &nodeid,
749                         &panid,
750                         &channel,
751                         &tx_power
752                         );
753         if (ZIGBEE_ERROR_NONE != ret) {
754                 msg("zb_get_network_info(0x%X) - FAILED!!! [%s]",
755                         ret, zigbee_error_to_string(ret));
756                 return RET_FAILURE;
757         }
758
759         msg(" - zb_get_network_info() ret: [0x%X] [%s]",
760                 ret, zigbee_error_to_string(ret));
761         msg("  IEEE ADDR : %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
762                         co_addr64[0], co_addr64[1], co_addr64[2],
763                         co_addr64[3], co_addr64[4], co_addr64[5],
764                         co_addr64[6], co_addr64[7]);
765         msg("  nodeID [0x%02X]", nodeid);
766         msg("  PanID [0x%02X]", panid);
767         msg("  Channel [%d] Tx Power [%d]", channel, tx_power);
768
769         return RET_SUCCESS;
770 }
771
772 static int run_get_controller_mac_address(MManager *mm, struct menu_data *menu)
773 {
774         int ret = ZIGBEE_ERROR_NONE;
775
776         ret = zb_get_controller_mac_address(handle, co_addr64);
777         if (ZIGBEE_ERROR_NONE != ret) {
778                 msg("zb_get_controller_mac_address(%d) - FAILED!!! [%s]",
779                         ret, zigbee_error_to_string(ret));
780                 return RET_FAILURE;
781         }
782
783         msg(" - zb_get_controller_mac_address() ret: [0x%X] [%s]",
784                 ret, zigbee_error_to_string(ret));
785         msg("  IEEE ADDR : %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
786                 co_addr64[0], co_addr64[1], co_addr64[2], co_addr64[3],
787                 co_addr64[4], co_addr64[5], co_addr64[6], co_addr64[7]);
788
789         return RET_SUCCESS;
790 }
791
792 static int run_get_end_dev_info_list(MManager *mm, struct menu_data *menu)
793 {
794         int ret = ZIGBEE_ERROR_NONE;
795         int i, j;
796         int count;
797         zb_nwk_addr addr16;
798         zb_ieee_addr addr64;
799         unsigned char num_of_ep;
800         unsigned char *ep_list = NULL;
801         zb_end_dev_info_h *list = NULL;
802
803         msg("");
804         ret = zb_end_dev_info_get_list(
805                 handle,
806                 &count,
807                 &list
808                 );
809         for (i = 0; i < count && list; i++) {
810                 zb_end_dev_info_get_network_address(
811                         list[i],
812                         &addr16
813                         );
814                 zb_end_dev_info_get_ieee_address(
815                         (list)[i],
816                         addr64
817                         );
818
819                 msgb("[# %02d]", (i+1));
820                 if (!strncmp((char *)sj_addr64, (char *)addr64, sizeof(addr64))) {
821                         sj_addr16 = addr16;
822                         msgb("   Found [Samjin Power Outlet] !!");
823                 }
824                 if (!strncmp((char *)st_addr64, (char *)addr64, sizeof(addr64))) {
825                         st_addr16 = addr16;
826                         msgb("   Found [ST Open/Close Sensor] !!");
827                 }
828                 msg("   network address = 0x%02X", addr16);
829                 msg("      IEEE address = %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
830                         addr64[0], addr64[1], addr64[2], addr64[3],
831                         addr64[4], addr64[5], addr64[6], addr64[7]);
832
833                 zb_end_dev_info_get_num_of_ep(
834                         (list)[i],
835                         &num_of_ep
836                         );
837                 msg("   number of end-points = 0x%04X", num_of_ep);
838
839                 zb_end_dev_info_get_ep_list(
840                         (list)[i],
841                         &ep_list
842                         );
843                 msgn("     ");
844
845                 if (NULL == ep_list)
846                         continue;
847
848                 for (j = 0; j < num_of_ep; j++)
849                         msgn("%04x ", ep_list[j]);
850                 msg("\n");
851
852                 free(ep_list);
853         }
854         zb_end_dev_info_list_free(
855                 list
856                 );
857
858         msg(" - zb_end_dev_info_get_list() ret: [0x%X] [%s]",
859                 ret, zigbee_error_to_string(ret));
860
861         return RET_SUCCESS;
862 }
863
864 static int run_get_endpoint_list(MManager *mm, struct menu_data *menu)
865 {
866         int ret = ZIGBEE_ERROR_NONE;
867         int i = 0;
868         unsigned char count = 0;
869         zb_end_point *ep_list = NULL;
870
871         ret = zb_end_dev_get_endpoint_list(
872                         handle,
873                         dest_addr64,
874                         &count,
875                         &ep_list);
876         if (ZIGBEE_ERROR_NONE != ret) {
877                 msg("zb_end_dev_get_endpoint_list(%d) - FAILED!!! [%s]",
878                         ret, zigbee_error_to_string(ret));
879                 return RET_FAILURE;
880         }
881
882         msg(" - zb_end_dev_get_endpoint_list() ret: [0x%X] [%s]",
883                 ret, zigbee_error_to_string(ret));
884         msgb("Endpoints count [%d] : ", count);
885         for (i = 0; i < count; i++)
886                 msgn("  %04d", ep_list[i]);
887         msg("\n");
888
889         if (ep_list) {
890                 free(ep_list);
891                 ep_list = NULL;
892         }
893
894         return RET_SUCCESS;
895 }
896
897 static int run_get_cluster_list(MManager *mm, struct menu_data *menu)
898 {
899         int ret = ZIGBEE_ERROR_NONE;
900         int i = 0;
901         unsigned char ep = 1;
902         unsigned char in_count = 0;
903         unsigned char out_count = 0;
904         zb_cluster_id *in_cluster_list = NULL;
905         zb_cluster_id *out_cluster_list = NULL;
906
907         if (strlen(data_endpoint) > 0)
908                 ep = (unsigned char)strtol(data_endpoint, NULL, 10);
909
910         ret = zb_end_dev_info_get_cluster_list(
911                         handle,
912                         dest_addr64,
913                         ep,
914                         &in_count,
915                         &in_cluster_list,
916                         &out_count,
917                         &out_cluster_list);
918         if (ZIGBEE_ERROR_NONE != ret) {
919                 msg("zb_end_dev_info_get_cluster_list (%d) - FAILED!!! [%s]",
920                         ret, zigbee_error_to_string(ret));
921                 return RET_FAILURE;
922         }
923
924         msg(" - zb_end_dev_info_get_cluster_list() ret: [0x%X] [%s]",
925                 ret, zigbee_error_to_string(ret));
926         msgb("In Clusters count [%d] : ", in_count);
927         for (i = 0; i < in_count; i++)
928                 msgn(" %04X (%s)\n", in_cluster_list[i], zb_zcl_get_cluster_id_str(in_cluster_list[i]));
929         msg("\n");
930
931         if (in_cluster_list) {
932                 free(in_cluster_list);
933                 in_cluster_list = NULL;
934         }
935
936         msgb("Out Clusters count [%d] : ", out_count);
937         for (i = 0; i < out_count; i++)
938                 msgn(" %04X (%s)\n",
939                         out_cluster_list[i], zb_zcl_get_cluster_id_str(out_cluster_list[i]));
940         msg("\n");
941
942         if (out_cluster_list) {
943                 free(out_cluster_list);
944                 out_cluster_list = NULL;
945         }
946
947         return RET_SUCCESS;
948 }
949
950 static int run_choose_end_device_list(MManager *mm, struct menu_data *menu)
951 {
952         int ret = ZIGBEE_ERROR_NONE;
953         int i, j;
954         zb_nwk_addr addr16;
955         zb_ieee_addr addr64;
956         unsigned char num_of_ep;
957         zb_end_point *ep_list = NULL;
958
959         if (NULL != target_list)
960                 zb_end_dev_info_list_free(target_list);
961
962         ret = zb_end_dev_info_get_list(
963                         handle,
964                         &target_list_count,
965                         &target_list
966                         );
967
968         msg(" - zb_end_dev_info_get_list() ret: [0x%X] [%s]",
969                 ret, zigbee_error_to_string(ret));
970         msg("");
971
972         for (i = 0; i < target_list_count && target_list; i++) {
973                 zb_end_dev_info_get_network_address(
974                         target_list[i],
975                         &addr16
976                         );
977                 zb_end_dev_info_get_ieee_address(
978                         (target_list)[i],
979                         addr64
980                         );
981
982                 msgb("[# %02d] End device", (i+1));
983                 if (!strncmp((char *)sj_addr64, (char *)addr64, sizeof(addr64))) {
984                         sj_addr16 = addr16;
985                         msgb("   Found [Samjin Power Outlet] !!");
986                 }
987                 if (!strncmp((char *)st_addr64, (char *)addr64, sizeof(addr64))) {
988                         st_addr16 = addr16;
989                         msgb("   Found [ST Open/Close Sensor] !!");
990                 }
991                 msg("   network address = 0x%02X", addr16);
992                 msg("      IEEE address = %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
993                         addr64[0], addr64[1], addr64[2], addr64[3],
994                         addr64[4], addr64[5], addr64[6], addr64[7]);
995
996                 zb_end_dev_info_get_num_of_ep(
997                         (target_list)[i],
998                         &num_of_ep
999                         );
1000                 msg("   number of end-points = 0x%04X", num_of_ep);
1001
1002                 zb_end_dev_info_get_ep_list(
1003                         (target_list)[i],
1004                         &ep_list
1005                         );
1006                 msgn("      ");
1007
1008                 if (NULL == ep_list)
1009                         continue;
1010
1011                 for (j = 0; j < num_of_ep; j++)
1012                         msgn("%04x ", ep_list[j]);
1013                 msg("\n");
1014
1015                 free(ep_list);
1016         }
1017
1018         return ret;
1019 }
1020
1021 static int run_select_device(MManager *mm, struct menu_data *menu)
1022 {
1023         int i;
1024         int selected = (int)strtol(data_choose_target, NULL, 10);
1025         zb_nwk_addr addr16;
1026
1027         /* Apply manual address first */
1028         if (strlen(data_dest_addr16) > 0) {
1029                 dest_addr16 = (unsigned short)strtol(data_dest_addr16, NULL, 16);
1030                 msg("  network addr [0x%04X] selected.", dest_addr16);
1031                 memset(data_dest_addr16, '\0', MENU_DATA_SIZE + 1);
1032                 return RET_SUCCESS;
1033         }
1034
1035         if (NULL == target_list) {
1036                 msg("Please select device list first !");
1037                 return RET_FAILURE;
1038         }
1039
1040         if (selected <= 0) {
1041                 msg("Please input valid device index !");
1042                 return RET_FAILURE;
1043         }
1044
1045         for (i = 0; i < target_list_count && target_list; i++) {
1046                 if (selected == (i+1)) {
1047                         zb_end_dev_info_get_network_address(
1048                                 target_list[i],
1049                                 &addr16
1050                                 );
1051                         zb_end_dev_info_get_ieee_address(
1052                                 (target_list)[i],
1053                                 dest_addr64
1054                                 );
1055                         dest_addr16 = addr16;
1056                         msg("  network addr [0x%04X] selected.", dest_addr16);
1057                         msg("  ieee address = %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
1058                                 dest_addr64[0], dest_addr64[1], dest_addr64[2], dest_addr64[3],
1059                                 dest_addr64[4], dest_addr64[5], dest_addr64[6], dest_addr64[7]);
1060                         return RET_SUCCESS;
1061                 }
1062         }
1063
1064         msg("  Index [%d] was not found from end device list", selected);
1065
1066         return RET_FAILURE;
1067 }
1068
1069 static int run_form_network(MManager *mm, struct menu_data *menu)
1070 {
1071         int ret = ZIGBEE_ERROR_NONE;
1072
1073         ret = zb_form_network(
1074                         handle,
1075                         _zb_form_network_done_cb,
1076                         NULL
1077                         );
1078         if (ZIGBEE_ERROR_NONE != ret) {
1079                 msg("zb_form_network(0x%X) - FAILED!!! [%s]",
1080                         ret, zigbee_error_to_string(ret));
1081                 return RET_FAILURE;
1082         }
1083
1084         msg(" - zb_form_network() ret: [0x%X] [%s]",
1085                 ret, zigbee_error_to_string(ret));
1086
1087         return RET_SUCCESS;
1088 }
1089
1090 static int run_disable_network(MManager *mm, struct menu_data *menu)
1091 {
1092         int ret = ZIGBEE_ERROR_NONE;
1093
1094         ret = zb_disable_network(
1095                         handle,
1096                         _zb_disable_network_done_cb,
1097                         NULL
1098                         );
1099         if (ZIGBEE_ERROR_NONE != ret) {
1100                 msg("zb_disable_network(0x%X) - FAILED!!! [%s]",
1101                         ret, zigbee_error_to_string(ret));
1102                 return RET_FAILURE;
1103         }
1104
1105         msg(" - zb_disable_network() ret: [0x%X] [%s]",
1106                 ret, zigbee_error_to_string(ret));
1107
1108         return RET_SUCCESS;
1109 }
1110
1111 static int run_leave_device(MManager *mm, struct menu_data *menu)
1112 {
1113         int ret = ZIGBEE_ERROR_NONE;
1114         bool remove_children = true;
1115         bool rejoin = false;
1116         unsigned char var = 0;
1117
1118         if (strlen(data_remove_children)) {
1119                 var = (unsigned char)strtol(data_remove_children, NULL, 10);
1120                 remove_children = (var == 0) ? false : true;
1121         }
1122         if (strlen(data_rejoin)) {
1123                 var = (unsigned char)strtol(data_rejoin, NULL, 10);
1124                 rejoin = (var == 0) ? false : true;
1125         }
1126
1127         ret = zb_leave_device(
1128                         handle,
1129                         dest_addr64,
1130                         remove_children,
1131                         rejoin
1132                         );
1133         if (ret != ZIGBEE_ERROR_NONE) {
1134                 msg("zb_leave_device(0x%X) - FAILED!!! [%s]",
1135                         ret, zigbee_error_to_string(ret));
1136                 return RET_FAILURE;
1137         }
1138
1139         msg(" - zb_leave_device() ret: [0x%X] [%s]",
1140                 ret, zigbee_error_to_string(ret));
1141
1142         return RET_SUCCESS;
1143 }
1144
1145 static int run_permit_join(MManager *mm, struct menu_data *menu)
1146 {
1147         int ret = ZIGBEE_ERROR_NONE;
1148         unsigned char timeout = 90;
1149
1150         if (strlen(data_pj_timeout))
1151                 timeout = (unsigned char)strtol(data_pj_timeout, NULL, 10);
1152
1153         ret = zb_permit_join(
1154                         handle,
1155                         timeout
1156                         );
1157         if (ZIGBEE_ERROR_NONE != ret) {
1158                 msg("zb_permit_join(0x%X) - FAILED!!! [%s]",
1159                         ret, zigbee_error_to_string(ret));
1160                 return RET_FAILURE;
1161         }
1162
1163         msg(" - zb_permit_join() ret: [0x%X] [%s]",
1164                 ret, zigbee_error_to_string(ret));
1165
1166         return RET_SUCCESS;
1167 }
1168
1169 struct menu_data menu_sel_device[] = {
1170         { "1", "Device list", NULL, run_choose_end_device_list, data_choose_target },
1171         { "2", "Input network addr manually (hex)", NULL, NULL, data_dest_addr16 },
1172         { "3", "Apply", NULL, run_select_device, NULL },
1173         { NULL, NULL, },
1174 };
1175
1176 static struct menu_data _menu_get_cluster_list[] = {
1177         { "1", "Endpoint", NULL, NULL, data_endpoint },
1178         { "2", "Run", NULL, run_get_cluster_list, NULL },
1179         { NULL, NULL, },
1180 };
1181
1182 static struct menu_data menu_device[] = {
1183         { "1", "zb_create", NULL, run_create, NULL },
1184         { "2", "zb_destroy", NULL, run_destroy, NULL },
1185         { "3", "zb_enable", NULL, run_enable, NULL },
1186         { "4", "zb_disable", NULL, run_disable, NULL },
1187         { "5", "zb_hw_reset", NULL, run_hw_reset, NULL },
1188         { "6", "zb_coex_start", NULL, run_coex_start, NULL },
1189         { "7", "zb_coex_stop", NULL, run_coex_stop, NULL },
1190         { "8", "zb_end_dev_info_get_list", NULL, run_get_end_dev_info_list, NULL },
1191         { "9", "zb_get_endpoint_list", NULL, run_get_endpoint_list, NULL },
1192         { "10", "zb_end_dev_info_get_cluster_list ", _menu_get_cluster_list, NULL, NULL },
1193         { NULL, NULL, },
1194 };
1195
1196 static struct menu_data _menu_leave_device[] = {
1197         { "1", "Remove children", NULL, NULL, data_remove_children },
1198         { "2", "Rejoin", NULL, NULL, data_rejoin },
1199         { "3", "Run", NULL, run_leave_device, NULL },
1200         { NULL, NULL, },
1201 };
1202
1203 static struct menu_data _menu_permit_join[] = {
1204         { "1", "Timeout", NULL, NULL, data_pj_timeout },
1205         { "2", "Run", NULL, run_permit_join, NULL },
1206         { NULL, NULL, },
1207 };
1208
1209 static struct menu_data menu_network[] = {
1210         { "1", "zb_get_network_info", NULL, run_get_network_info, NULL },
1211         { "2", "zb_get_controller_mac_address", NULL, run_get_controller_mac_address, NULL },
1212         { "3", "zb_form_network", NULL, run_form_network, NULL },
1213         { "4", "zb_disable_network", NULL, run_disable_network, NULL },
1214         { "5", "zb_leave_device", _menu_leave_device, NULL, NULL },
1215         { "6", "zb_permit_join", _menu_permit_join, NULL, NULL },
1216         { NULL, NULL, },
1217 };
1218
1219 static struct menu_data menu_zcl[] = {
1220         { "1", "Global Commands", menu_zcl_global, NULL, NULL },
1221         { "2", "Cluster Commands", menu_zcl_cluster, NULL, NULL },
1222         { NULL, NULL, },
1223 };
1224
1225 static struct menu_data menu_zdo[] = {
1226         { "1", "Binding Manager", menu_zdo_binding, NULL, NULL },
1227         { "2", "Device Discovery", menu_zdo_device, NULL, NULL },
1228         { "3", "Network Manager", menu_zdo_network, NULL, NULL },
1229         { NULL, NULL, },
1230 };
1231
1232 static struct menu_data menu_main[] = {
1233         { "1", "Device", menu_device, NULL, NULL },
1234         { "2", "Network", menu_network, NULL, NULL },
1235         { "3", "Cluster Library", menu_zcl, NULL, NULL },
1236         { "4", "Device Object", menu_zdo, NULL, NULL },
1237         { "5", "Custom", menu_custom, NULL, NULL },
1238         { NULL, NULL, },
1239 };
1240
1241 static int __select_handle_register_event(MManager *mm, struct menu_data *menu)
1242 {
1243         int ret = -1;
1244
1245         if (NULL != handle) {
1246                 zb_destroy(handle);
1247                 handle = NULL;
1248         }
1249         _init_endpoints();
1250
1251         ret = zb_create(
1252                         &handle
1253                         );
1254
1255         msg("");
1256
1257         if (ZIGBEE_ERROR_NONE != ret) {
1258                 msg("zb_create(0x%X) - FAILED!!! [%s]",
1259                         ret, zigbee_error_to_string(ret));
1260                 return RET_FAILURE;
1261         } else {
1262                 msg("zb_create(0x%X) - SUCCESS!!!. handle [%p]", ret, handle);
1263         }
1264
1265         /* Set event callback */
1266         ret = zb_set_event_cb(
1267                 handle,
1268                 _zb_event_cb
1269                 );
1270         if (ZIGBEE_ERROR_NONE != ret) {
1271                 msg("zb_set_event_cb(0x%X) - FAILED!!!", ret);
1272                 return RET_FAILURE;
1273         }
1274
1275         msg(" - zb_set_event_cb() ret: [0x%X] [%s]",
1276                 ret, zigbee_error_to_string(ret));
1277
1278         /* Enable */
1279         ret = zb_enable(
1280                 handle
1281                 );
1282         if (ZIGBEE_ERROR_PERMISSION_DENIED == ret) {
1283                 /* Admin permission is not set, but we can do normal operations */
1284                 msg("zb_enable(0x%X) - FAILED!!!", ret);
1285         } else if (ZIGBEE_ERROR_NONE != ret) {
1286                 msg("zb_enable(0x%X) - FAILED!!! [%s]",
1287                         ret, zigbee_error_to_string(ret));
1288                 return RET_FAILURE;
1289         }
1290
1291         msg(" - zb_enable() ret: [0x%X] [%s]",
1292                 ret, zigbee_error_to_string(ret));
1293
1294         /* Set coordinator address */
1295         ret = zb_get_controller_mac_address(
1296                         handle,
1297                         co_addr64
1298                         );
1299         if (ZIGBEE_ERROR_NONE != ret)
1300                 msg("zb_get_controller_mac_address(%d) - FAILED!!! [%s]",
1301                         ret, zigbee_error_to_string(ret));
1302         else
1303                 msg("   Coordinator Address:");
1304                 msg("     %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X",
1305                         co_addr64[0], co_addr64[1], co_addr64[2], co_addr64[3],
1306                         co_addr64[4], co_addr64[5], co_addr64[6], co_addr64[7]);
1307
1308         menu_manager_set_user_data(mm, handle);
1309
1310         return RET_SUCCESS;
1311 }
1312
1313 static gboolean __create_init_menu(struct menu_data init_menu[1])
1314 {
1315         init_menu[0].key = "1";
1316         init_menu[0].title = "Init";
1317         init_menu[0].sub_menu = menu_main;
1318         init_menu[0].callback = __select_handle_register_event;
1319         init_menu[0].data = NULL;
1320
1321         return TRUE;
1322 }
1323
1324 int main(int arg, char **argv)
1325 {
1326         GMainLoop *mainloop = NULL;
1327         GIOChannel *channel = g_io_channel_unix_new(STDIN_FILENO);
1328         MManager *manager;
1329         struct menu_data init_menu[1+1] = { {NULL, NULL, } };
1330
1331 #if !GLIB_CHECK_VERSION(2, 35, 0)
1332         g_type_init();
1333 #endif
1334 #if !GLIB_CHECK_VERSION(2, 31, 0)
1335         g_thread_init(NULL);
1336 #endif
1337         mainloop = g_main_loop_new(NULL, FALSE);
1338
1339         msg("");
1340         msg("* ZigBee Test application ");
1341         msg("* Build On: %s  %s", __DATE__, __TIME__);
1342
1343         if (__create_init_menu(init_menu) == FALSE)
1344                 goto OUT;
1345
1346         manager = menu_manager_new(init_menu, mainloop);
1347         if (!manager)
1348                 goto OUT;
1349
1350         menu_manager_run(manager);
1351
1352         g_io_add_watch(channel, (G_IO_IN | G_IO_ERR | G_IO_HUP | G_IO_NVAL),
1353                                 on_menu_manager_keyboard, manager);
1354         g_main_loop_run(mainloop);
1355
1356 OUT:
1357         if (NULL != target_list)
1358                 zb_end_dev_info_list_free(
1359                         target_list
1360                         );
1361
1362         if (handle)
1363                 zb_disable(
1364                         handle
1365                         );
1366
1367         g_main_loop_unref(mainloop);
1368         msg("******* Bye bye *******");
1369
1370         return 0;
1371 }