1 /* *****************************************************************
3 * Copyright 2015 Samsung Electronics All Rights Reserved.
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 ******************************************************************/
23 * This file contains class which provides functions
24 * related to Group Synchronization.
27 package org.iotivity.service.tm;
30 import java.util.Vector;
32 import org.iotivity.base.OcException;
33 import org.iotivity.base.OcResource;
34 import org.iotivity.base.OcResourceHandle;
36 import android.util.Log;
39 * This class provides a set of APIs relating to Group Synchronization.
41 public class GroupSynchronization {
43 private IFindGroupListener groupListener;
44 private final String LOG_TAG = this.getClass()
47 private static GroupSynchronization s_groupSynchronizationInstance;
50 private static native int nativeFindGroup(
51 Vector<String> collectionResourceTypes);
53 private static native int nativeCreateGroup(String collectionResourceType);
55 private static native int nativeJoinGroupString(
56 String collectionResourceType, OcResourceHandle resourceHandle);
58 private static native int nativeJoinGroupObject(OcResource resource,
59 OcResourceHandle resourceHandle);
61 private static native int nativeLeaveGroup(String collectionResourceType,
62 OcResourceHandle resourceHandle);
64 private static native int nativeLeaveGroupForResource(OcResource resource,
65 String collectionResourceType, OcResourceHandle resourceHandle);
67 private static native void nativeDeleteGroup(String collectionResourceType);
69 private static native Map<String, OcResourceHandle> nativeGetGroupList();
72 System.loadLibrary("gnustl_shared");
73 System.loadLibrary("oc_logger");
74 System.loadLibrary("connectivity_abstraction");
75 System.loadLibrary("ca-interface");
76 System.loadLibrary("octbstack");
77 System.loadLibrary("oc");
78 System.loadLibrary("TGMSDKLibrary");
79 System.loadLibrary("ocstack-jni");
80 System.loadLibrary("things-manager-jni");
84 * Function for Getting instance of GroupSynchronization object.
86 * @return GroupSynchronization instance.
88 public static GroupSynchronization getInstance() {
89 if (null == s_groupSynchronizationInstance) {
90 s_groupSynchronizationInstance = new GroupSynchronization();
92 return s_groupSynchronizationInstance;
96 * Provides interface for getting group discovery status.
98 public interface IFindGroupListener {
100 * This callback method will be called to notify whether group is found
106 public void onGroupFindCallback(OcResource resource);
110 * Set listener for receiving notification on whether the requested group is
114 * IFindGroupListener to receive group discovery status.
116 public void setGroupListener(IFindGroupListener listener) {
117 groupListener = listener;
121 * API for finding a specific remote group when a resource tries to join a
124 * IFindGroupListener::onGroupFindCallback will be called to notify whether
125 * requested group found or not.
128 * @param resourceTypes
129 * resource types of a group to find and join
131 * @return OCStackResult - return value of this API. It returns OC_STACK_OK
134 public OCStackResult findGroup(Vector<String> resourceTypes) {
135 OCStackResult result;
136 if (null == groupListener) {
137 result = OCStackResult.OC_STACK_ERROR;
139 int ordinal = nativeFindGroup(resourceTypes);
140 result = OCStackResult.conversion(ordinal);
146 * API for creating a new group.
148 * @param resourceType
149 * resource type of a group to create
151 * @return OCStackResult - return value of this API. It returns OC_STACK_OK
154 public OCStackResult createGroup(String resourceType) {
155 OCStackResult result;
156 int ordinal = nativeCreateGroup(resourceType);
157 result = OCStackResult.conversion(ordinal);
162 * API for joining a group. This API is used when a resource that has a
163 * group tries to find a specific remote resource and makes it join a group.
165 * @param resourceType
166 * resource type of a group to join.
167 * @param resourceHandle
168 * resource handle to join a group.
170 * @return OCStackResult - return value of this API. It returns OC_STACK_OK
173 public OCStackResult joinGroup(String resourceType,
174 OcResourceHandle resourceHandle) {
175 OCStackResult result;
177 int ordinal = nativeJoinGroupString(resourceType, resourceHandle);
178 result = OCStackResult.conversion(ordinal);
184 * API for joining a group. This API is used when a resource that doesn't
185 * have a group tries to find and join a specific remote group.
188 * group resource to join.
189 * @param resourceHandle
190 * resource handle to join a group.
192 * @return OCStackResult - return value of this API. It returns OC_STACK_OK
195 public OCStackResult joinGroup(OcResource resource,
196 OcResourceHandle resourceHandle) throws OcException {
198 OCStackResult result;
199 int ordinal = nativeJoinGroupObject(resource, resourceHandle);
200 result = OCStackResult.conversion(ordinal);
206 * API for leaving a joined group.
208 * @param resourceType
209 * resource type of a group to leave.
210 * @param resourceHandle
211 * resource handle to leave a group.
213 * @return OCStackResult - return value of this API. It returns OC_STACK_OK
216 public OCStackResult leaveGroup(String resourceType,
217 OcResourceHandle resourceHandle) {
219 OCStackResult result;
220 int ordinal = nativeLeaveGroup(resourceType, resourceHandle);
221 result = OCStackResult.conversion(ordinal);
227 * API for leaving a joined group.
230 * resource of a group to leave.
231 * @param resourceType
232 * resource type of a group to leave.
233 * @param resourceHandle
234 * resource handle to leave a group.
236 * @return OCStackResult - return value of this API. It returns OC_STACK_OK
239 public OCStackResult leaveGroup(OcResource resource, String resourceType,
240 OcResourceHandle resourceHandle) {
242 OCStackResult result;
243 int ordinal = nativeLeaveGroupForResource(resource, resourceType,
245 result = OCStackResult.conversion(ordinal);
251 * API for deleting a group.
253 * @param collectionResourceType
254 * resource type of a group to delete.
256 public void deleteGroup(String collectionResourceType) {
257 nativeDeleteGroup(collectionResourceType);
261 * API for getting a list of joined groups.
263 * @return Returns group resource type and group resource handle as a map
266 public Map<String, OcResourceHandle> getGroupList() {
267 return nativeGetGroupList();
270 // ******** JNI UTILITY FUNCTION **********//
273 * This callback method is called from JNI to notify whether group is found
276 public void onGroupFindCallback(OcResource resource) {
277 if (null != groupListener) {
278 Log.i("ThingsManagerCallback : onGroupFindCallback",
279 "Received Callback from JNI");
280 groupListener.onGroupFindCallback(resource);