2 * Copyright (c) 2015 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include <tizen_type.h>
23 static char *door_resource_device_id;
24 static GList *device_id_list;
26 #define DOOR_RESOURCE_TYPE "org.tizen.door"
28 static bool _get_res_type_cb(const char *string, void *user_data)
30 char *resource_uri_path = user_data;
32 DBG("[%s] resource type : %s", resource_uri_path, string);
34 return IOTCON_FUNC_CONTINUE;
37 static int _device_id_compare(const void *a, const void *b)
42 static void _state_changed_cb(iotcon_remote_resource_h resource,
43 iotcon_remote_resource_state_e state, void *user_data)
45 INFO("Resource State is Changed");
48 case IOTCON_REMOTE_RESOURCE_ALIVE:
51 case IOTCON_REMOTE_RESOURCE_LOST_SIGNAL:
52 INFO(" --- LOST_SIGNAL");
59 static void _representation_changed_cb(iotcon_remote_resource_h resource,
60 iotcon_representation_h representation, void *user_data)
66 INFO("Resource is cached");
68 ret = iotcon_representation_get_state(representation, &state);
69 if (IOTCON_ERROR_NONE != ret) {
70 ERR("iotcon_representation_get_state() Fail(%d)", ret);
74 ret = iotcon_state_get_bool(state, "opened", &opened);
75 if (IOTCON_ERROR_NONE != ret) {
76 ERR("iotcon_state_get_bool() Fail(%d)", ret);
82 INFO("door is opened");
85 INFO("door is closed");
92 static void _found_resource(iotcon_remote_resource_h resource, iotcon_error_e result,
97 char *resource_uri_path;
98 char *resource_device_id;
99 int ret, resource_interfaces;
100 iotcon_resource_types_h resource_types;
101 iotcon_remote_resource_h cloned_resource;
103 RETM_IF(IOTCON_ERROR_NONE != result, "Invalid result(%d)", result);
105 if (NULL == resource)
108 INFO("===== resource found =====");
110 /* get the resource URI */
111 ret = iotcon_remote_resource_get_uri_path(resource, &resource_uri_path);
112 if (IOTCON_ERROR_NONE != ret) {
113 ERR("iotcon_remote_resource_get_uri_path() Fail(%d)", ret);
117 /* get the device unique id.
118 * this is unique per-server independent on how it was discovered. */
119 ret = iotcon_remote_resource_get_device_id(resource, &resource_device_id);
120 if (IOTCON_ERROR_NONE != ret) {
121 ERR("iotcon_remote_resource_get_device_id() Fail(%d)", ret);
124 DBG("[%s] resource device id : %s", resource_uri_path, resource_device_id);
126 node = g_list_find_custom(device_id_list, resource_device_id, _device_id_compare);
128 door_resource_device_id = strdup(resource_device_id);
129 if (NULL == door_resource_device_id) {
130 ERR("strdup(door_resource_device_id) Fail");
134 device_id_list = g_list_append(device_id_list, door_resource_device_id);
137 DBG("This device(%s) is already found. skip !", door_resource_device_id);
141 /* get the resource host address */
142 ret = iotcon_remote_resource_get_host_address(resource, &resource_host);
143 if (IOTCON_ERROR_NONE != ret) {
144 ERR("iotcon_remote_resource_get_host_address() Fail(%d)", ret);
145 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
146 free(door_resource_device_id);
149 DBG("[%s] resource host : %s", resource_uri_path, resource_host);
151 /* get the resource interfaces */
152 ret = iotcon_remote_resource_get_interfaces(resource, &resource_interfaces);
153 if (IOTCON_ERROR_NONE != ret) {
154 ERR("iotcon_remote_resource_get_interfaces() Fail(%d)", ret);
155 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
156 free(door_resource_device_id);
159 if (IOTCON_INTERFACE_DEFAULT & resource_interfaces)
160 DBG("[%s] resource interface : DEFAULT_INTERFACE", resource_uri_path);
161 if (IOTCON_INTERFACE_LINK & resource_interfaces)
162 DBG("[%s] resource interface : LINK_INTERFACE", resource_uri_path);
163 if (IOTCON_INTERFACE_BATCH & resource_interfaces)
164 DBG("[%s] resource interface : BATCH_INTERFACE", resource_uri_path);
165 if (IOTCON_INTERFACE_GROUP & resource_interfaces)
166 DBG("[%s] resource interface : GROUP_INTERFACE", resource_uri_path);
168 /* get the resource types */
169 ret = iotcon_remote_resource_get_types(resource, &resource_types);
170 if (IOTCON_ERROR_NONE != ret) {
171 ERR("iotcon_remote_resource_get_types() Fail(%d)", ret);
172 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
173 free(door_resource_device_id);
177 ret = iotcon_resource_types_foreach(resource_types, _get_res_type_cb,
179 if (IOTCON_ERROR_NONE != ret) {
180 ERR("iotcon_resource_types_foreach() Fail(%d)", ret);
181 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
182 free(door_resource_device_id);
186 ret = iotcon_remote_resource_clone(resource, &cloned_resource);
187 if (IOTCON_ERROR_NONE != ret) {
188 ERR("iotcon_remote_resource_clone() Fail(%d)", ret);
189 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
190 free(door_resource_device_id);
194 /* Start Monitoring */
195 ret = iotcon_remote_resource_start_monitoring(cloned_resource, _state_changed_cb,
197 if (IOTCON_ERROR_NONE != ret) {
198 ERR("iotcon_remote_resource_start_monitoring() Fail(%d)", ret);
199 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
200 free(door_resource_device_id);
205 ret = iotcon_remote_resource_start_caching(cloned_resource,
206 _representation_changed_cb, NULL);
207 if (IOTCON_ERROR_NONE != ret) {
208 ERR("iotcon_remote_resource_start_caching() Fail(%d)", ret);
209 iotcon_remote_resource_stop_monitoring(resource);
210 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
211 free(door_resource_device_id);
215 device_id_list = g_list_remove(device_id_list, door_resource_device_id);
216 free(door_resource_device_id);
219 int main(int argc, char **argv)
224 iotcon_remote_resource_h resource;
226 loop = g_main_loop_new(NULL, FALSE);
229 ret = iotcon_connect();
230 if (IOTCON_ERROR_NONE != ret) {
231 ERR("iotcon_connect() Fail(%d)", ret);
235 /* find door typed resources */
236 ret = iotcon_find_resource(IOTCON_MULTICAST_ADDRESS, IOTCON_CONNECTIVITY_IPV4,
237 DOOR_RESOURCE_TYPE, false, _found_resource, &resource);
238 if (IOTCON_ERROR_NONE != ret) {
239 ERR("iotcon_find_resource() Fail(%d)", ret);
244 g_main_loop_run(loop);
245 g_main_loop_unref(loop);
247 g_list_free_full(device_id_list, free);
249 /* disconnect iotcon */