1eb4879263a56654f2f7815298ef3c5a087d6d75
[platform/core/api/zigbee.git] / test / zcl_cluster_scene.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 <zigbee.h>
27
28 #include "main.h"
29 #include "menu.h"
30
31 extern zigbee_h  handle;
32
33 extern nwk_addr  dest_addr16;
34 extern simple_desc_s sj_ep1;
35
36 static char data_group_id[MENU_DATA_SIZE + 1] = "2";
37 static char data_scene_id[MENU_DATA_SIZE + 1] = "1";
38 static char data_trans_time[MENU_DATA_SIZE + 1] = "0";
39 static char data_scene_name[MENU_DATA_SIZE + 1] = "6scnone";
40
41 static void zigbee_zcl_scene_add_scene_rsp(nwk_addr addr16, unsigned char ep,
42         unsigned char status, unsigned short group_id, unsigned char scene_id, void *user_data)
43 {
44         msg("");
45         msgp("zigbee_zcl_scene_add_scene_rsp()");
46
47         msg("  Network Address = 0x%04X", addr16);
48         msg("  End-Point = 0x%02X", ep);
49         msg("  Status    = 0x%02X (%s)", status, zb_get_zcl_error_message(status));
50         msg("  Group ID  = 0x%04X", group_id);
51         msg("  Scene ID  = 0x%02X", scene_id);
52 }
53
54 static void zigbee_zcl_scene_view_scene_rsp(nwk_addr addr16, unsigned char ep,
55         unsigned char status, unsigned short group_id, unsigned char scene_id,
56         unsigned short transition_time, const char *scene_name,
57         const char *extension_field_set, unsigned short ext_len, void *user_data)
58 {
59         int i;
60         msg("");
61         msgp("zigbee_zcl_scene_view_scene_rsp()");
62
63         msg("  Network Address = 0x%04X", addr16);
64         msg("  End-Point       = 0x%02X", ep);
65         msg("  Status          = 0x%02X (%s)", status, zb_get_zcl_error_message(status));
66         msg("  Group ID        = 0x%04X", group_id);
67         msg("  Scene ID        = 0x%02X", scene_id);
68         msg("  Transition Time = 0x%04X", transition_time);
69         msg("  Scene Name      = %s\n", scene_name);
70         for (i = 0; i < ext_len; i++)
71                 msg("    extension_field_set[%d] = 0x%02X", i, extension_field_set[i]);
72 }
73
74 static void zigbee_zcl_scene_remove_scene_rsp(nwk_addr addr16, unsigned char ep,
75         unsigned char status, unsigned short group_id, unsigned char scene_id, void *user_data)
76 {
77         msg("");
78         msgp("zigbee_zcl_scene_remove_scene_rsp()");
79
80         msg("  Network Address = 0x%04X", addr16);
81         msg("  End-Point = 0x%02X", ep);
82         msg("  Status    = 0x%02X (%s)", status, zb_get_zcl_error_message(status));
83         msg("  Group ID  = 0x%04X", group_id);
84         msg("  Scene ID  = 0x%02X", scene_id);
85 }
86
87 static void zigbee_zcl_scene_remove_all_scene_rsp(nwk_addr addr16, unsigned char ep,
88         unsigned char status, unsigned short group_id, void *user_data)
89 {
90         msg("");
91         msgp("zigbee_zcl_scene_remove_all_scene_rsp()");
92
93         msg("  Network Address = 0x%04X", addr16);
94         msg("  End-Point = 0x%02X", ep);
95         msg("  Status    = 0x%02X (%s)", status, zb_get_zcl_error_message(status));
96         msg("  Group ID  = 0x%04X", group_id);
97 }
98
99 static void zigbee_zcl_scene_store_scene_rsp(nwk_addr addr16, unsigned char ep,
100         unsigned char status, unsigned short group_id, unsigned char scene_id, void *user_data)
101 {
102         msg("");
103         msgp("zigbee_zcl_scene_store_scene_rsp()");
104
105         msg("  Network Address = 0x%04X", addr16);
106         msg("  End-Point = 0x%02X", ep);
107         msg("  Status    = 0x%02X (%s)", status, zb_get_zcl_error_message(status));
108         msg("  Group ID  = 0x%04X", group_id);
109         msg("  Scene ID  = 0x%02X", scene_id);
110 }
111
112 static void zigbee_zcl_scene_get_scene_membership_rsp(nwk_addr addr16,
113         unsigned char ep, unsigned char status, unsigned char capacity, unsigned short group_id,
114         unsigned char scene_count, unsigned char *scene_list, void *user_data)
115 {
116         int i;
117
118         msg("");
119         msgp("zigbee_zcl_scene_get_scene_membership_rsp()");
120
121         msg("  Network Address = 0x%04X", addr16);
122         msg("  End-Point = 0x%02X", ep);
123         msg("  Status    = 0x%02X (%s)", status, zb_get_zcl_error_message(status));
124         msg("  Capacity  = 0x%02X", capacity);
125         msg("  Group ID  = 0x%02X", group_id);
126         for (i = 0; i < scene_count && ZB_ZCL_SUCCESS == status; i++)
127                 msg("    scene_list[%d] = 0x%02X", i, scene_list[i]);
128 }
129
130 static int run_scene_add_scene(MManager *mm, struct menu_data *menu)
131 {
132         int ret = ZIGBEE_ERROR_NONE;
133         unsigned short group_id = 0x0002;
134         unsigned char scene_id = 0x01;
135         unsigned short trans_time = 0x0000;
136         const char ext_field_set[] = {0x00, 0x06, 0x01, 0x00};
137         unsigned short ext_len = sizeof(ext_field_set) / sizeof(ext_field_set[0]);
138
139         if (strlen(data_group_id))
140                 group_id = (unsigned short)strtol(data_group_id, NULL, 10);
141         if (strlen(data_scene_id))
142                 scene_id = (unsigned char)strtol(data_scene_id, NULL, 10);
143
144         ret = zb_zcl_scene_add_scene(handle, dest_addr16,
145                         sj_ep1.ep, group_id, scene_id, trans_time, data_scene_name,
146                         ext_len, ext_field_set, zigbee_zcl_scene_add_scene_rsp, NULL);
147         if (ZIGBEE_ERROR_NONE != ret) {
148                 msg("zb_zcl_scene_add_scene(0x%X) - FAILED!!! [%s]", ret, zigbee_error_to_string(ret));
149                 return RET_FAILURE;
150         }
151
152         msg(" - zb_zcl_scene_add_scene() ret: [0x%X] [%s]", ret, zigbee_error_to_string(ret));
153         msg("");
154
155         return RET_SUCCESS;
156 }
157
158 static int run_scene_view_scene(MManager *mm, struct menu_data *menu)
159 {
160         int ret = ZIGBEE_ERROR_NONE;
161         unsigned short group_id = 0x0002;
162         unsigned char scene_id = 0x01;
163
164         if (strlen(data_group_id))
165                 group_id = (unsigned short)strtol(data_group_id, NULL, 10);
166         if (strlen(data_scene_id))
167                 scene_id = (unsigned char)strtol(data_scene_id, NULL, 10);
168
169         ret = zb_zcl_scene_view_scene(handle, dest_addr16, sj_ep1.ep, group_id,
170                         scene_id, zigbee_zcl_scene_view_scene_rsp, NULL);
171         if (ZIGBEE_ERROR_NONE != ret) {
172                 msg("zb_zcl_scene_view_scene(0x%X) - FAILED!!! [%s]", ret, zigbee_error_to_string(ret));
173                 return RET_FAILURE;
174         }
175
176         msg(" - zb_zcl_scene_view_scene() ret: [0x%X] [%s]", ret, zigbee_error_to_string(ret));
177         msg("");
178
179         return RET_SUCCESS;
180 }
181
182 static int run_scene_get_scene_membership(MManager *mm, struct menu_data *menu)
183 {
184         int ret = ZIGBEE_ERROR_NONE;
185         unsigned short group_id = 0x0002;
186
187         if (strlen(data_group_id))
188                 group_id = (unsigned short)strtol(data_group_id, NULL, 10);
189
190         ret = zb_zcl_scene_get_scene_membership(handle, dest_addr16, sj_ep1.ep,
191                         group_id, zigbee_zcl_scene_get_scene_membership_rsp, NULL);
192         if (ZIGBEE_ERROR_NONE != ret) {
193                 msg("zb_zcl_scene_get_scene_membership(0x%X) - FAILED!!! [%s]", ret, zigbee_error_to_string(ret));
194                 return RET_FAILURE;
195         }
196
197         msg(" - zb_zcl_scene_get_scene_membership() ret: [0x%X] [%s]", ret, zigbee_error_to_string(ret));
198         msg("");
199
200         return RET_SUCCESS;
201 }
202
203 static int run_scene_remove_scene(MManager *mm, struct menu_data *menu)
204 {
205         int ret = ZIGBEE_ERROR_NONE;
206         unsigned short group_id = 0x0002;
207         unsigned char scene_id = 0x01;
208
209         if (strlen(data_group_id))
210                 group_id = (unsigned short)strtol(data_group_id, NULL, 10);
211         if (strlen(data_scene_id))
212                 scene_id = (unsigned char)strtol(data_scene_id, NULL, 10);
213
214         ret = zb_zcl_scene_remove_scene(handle, dest_addr16, sj_ep1.ep, group_id,
215                         scene_id, zigbee_zcl_scene_remove_scene_rsp, NULL);
216         if (ZIGBEE_ERROR_NONE != ret) {
217                 msg("zb_zcl_scene_remove_scene(0x%X) - FAILED!!! [%s]", ret, zigbee_error_to_string(ret));
218                 return RET_FAILURE;
219         }
220
221         msg(" - zb_zcl_scene_remove_scene() ret: [0x%X] [%s]", ret, zigbee_error_to_string(ret));
222         msg("");
223
224         return RET_SUCCESS;
225 }
226
227 static int run_scene_remove_all_scene(MManager *mm, struct menu_data *menu)
228 {
229         int ret = ZIGBEE_ERROR_NONE;
230         unsigned short group_id = 0x0002;
231
232         if (strlen(data_group_id))
233                 group_id = (unsigned short)strtol(data_group_id, NULL, 10);
234
235         ret = zb_zcl_scene_remove_all_scene(handle, dest_addr16, sj_ep1.ep, group_id,
236                         zigbee_zcl_scene_remove_all_scene_rsp, NULL);
237         if (ZIGBEE_ERROR_NONE != ret) {
238                 msg("zb_zcl_scene_remove_all_scene(0x%X) - FAILED!!! [%s]", ret, zigbee_error_to_string(ret));
239                 return RET_FAILURE;
240         }
241
242         msg(" - zb_zcl_scene_remove_all_scene() ret: [0x%X] [%s]", ret, zigbee_error_to_string(ret));
243         msg("");
244
245         return RET_SUCCESS;
246 }
247
248 static int run_scene_store_scene(MManager *mm, struct menu_data *menu)
249 {
250         int ret = ZIGBEE_ERROR_NONE;
251         unsigned short group_id = 0x0002;
252         unsigned char scene_id = 0x01;
253
254         if (strlen(data_group_id))
255                 group_id = (unsigned short)strtol(data_group_id, NULL, 10);
256         if (strlen(data_scene_id))
257                 scene_id = (unsigned char)strtol(data_scene_id, NULL, 10);
258
259         ret = zb_zcl_scene_store_scene(handle, dest_addr16, sj_ep1.ep, group_id,
260                         scene_id, zigbee_zcl_scene_store_scene_rsp, NULL);
261         if (ZIGBEE_ERROR_NONE != ret) {
262                 msg("zb_zcl_scene_store_scene(0x%X) - FAILED!!! [%s]", ret, zigbee_error_to_string(ret));
263                 return RET_FAILURE;
264         }
265
266         msg(" - zb_zcl_scene_store_scene() ret: [0x%X] [%s]", ret, zigbee_error_to_string(ret));
267         msg("");
268
269         return RET_SUCCESS;
270 }
271
272 static int run_scene_recall_scene(MManager *mm, struct menu_data *menu)
273 {
274         int ret = ZIGBEE_ERROR_NONE;
275         unsigned short group_id = 0x0002;
276         unsigned char scene_id = 0x01;
277
278         if (strlen(data_group_id))
279                 group_id = (unsigned short)strtol(data_group_id, NULL, 10);
280         if (strlen(data_scene_id))
281                 scene_id = (unsigned char)strtol(data_scene_id, NULL, 10);
282
283         ret = zb_zcl_scene_recall_scene(handle, dest_addr16, sj_ep1.ep,
284                         group_id, scene_id);
285         if (ZIGBEE_ERROR_NONE != ret) {
286                 msg("zb_zcl_scene_recall_scene(0x%X) - FAILED!!! [%s]", ret, zigbee_error_to_string(ret));
287                 return RET_FAILURE;
288         }
289
290         msg(" - zb_zcl_scene_recall_scene() ret: [0x%X] [%s]", ret, zigbee_error_to_string(ret));
291         msg("");
292
293         return RET_SUCCESS;
294 }
295
296 static struct menu_data _menu_add_scene[] = {
297         { "1", "Group ID", NULL, NULL, data_group_id },
298         { "2", "Scene ID", NULL, NULL, data_scene_id },
299         { "3", "Transition time", NULL, NULL, data_trans_time },
300         { "4", "Scene name", NULL, NULL, data_scene_name },
301         { "5", "Run", NULL, run_scene_add_scene, NULL },
302         { NULL, NULL, },
303 };
304
305 static struct menu_data _menu_view_scene[] = {
306         { "1", "Group ID", NULL, NULL, data_group_id },
307         { "2", "Scene ID", NULL, NULL, data_scene_id },
308         { "3", "Run", NULL, run_scene_view_scene, NULL },
309         { NULL, NULL, },
310 };
311
312 static struct menu_data _menu_get_scene_membership[] = {
313         { "1", "Group ID", NULL, NULL, data_group_id },
314         { "2", "Run", NULL, run_scene_get_scene_membership, NULL },
315         { NULL, NULL, },
316 };
317
318 static struct menu_data _menu_remove_scene[] = {
319         { "1", "Group ID", NULL, NULL, data_group_id },
320         { "2", "Scene ID", NULL, NULL, data_scene_id },
321         { "3", "Run", NULL, run_scene_remove_scene, NULL },
322         { NULL, NULL, },
323 };
324
325 static struct menu_data _menu_remove_all_scene[] = {
326         { "1", "Group ID", NULL, NULL, data_group_id },
327         { "2", "Run", NULL, run_scene_remove_all_scene, NULL },
328         { NULL, NULL, },
329 };
330
331 static struct menu_data _menu_store_scene[] = {
332         { "1", "Group ID", NULL, NULL, data_group_id },
333         { "2", "Scene ID", NULL, NULL, data_scene_id },
334         { "3", "Run", NULL, run_scene_store_scene, NULL },
335         { NULL, NULL, },
336 };
337
338 static struct menu_data _menu_recall_scene[] = {
339         { "1", "Group ID", NULL, NULL, data_group_id },
340         { "2", "Scene ID", NULL, NULL, data_scene_id },
341         { "3", "Run", NULL, run_scene_recall_scene, NULL },
342         { NULL, NULL, },
343 };
344
345 struct menu_data menu_zcl_cluster_scene[] = {
346         { "1", "zb_zcl_scene_add_scene",
347                         _menu_add_scene, NULL, NULL },
348         { "2", "zb_zcl_scene_view_scene",
349                         _menu_view_scene, NULL, NULL },
350         { "3", "zb_zcl_scene_get_scene_membership",
351                         _menu_get_scene_membership, NULL, NULL },
352         { "4", "zb_zcl_scene_remove_scene",
353                         _menu_remove_scene, NULL, NULL },
354         { "5", "zb_zcl_scene_remove_all_scene",
355                         _menu_remove_all_scene, NULL, NULL },
356         { "6", "zb_zcl_scene_store_scene",
357                         _menu_store_scene, NULL, NULL },
358         { "7", "zb_zcl_scene_recall_scene",
359                         _menu_recall_scene, NULL, NULL },
360         { NULL, NULL, },
361 };