1 /******************************************************************
3 * Copyright 2016 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 ******************************************************************/
21 package org.iotivity.test.es.tc.helper;
23 import org.iotivity.base.OcConnectivityType;
24 import org.iotivity.base.OcResource;
25 import org.iotivity.base.OcPlatform;
26 import org.iotivity.base.OcException;
27 import org.iotivity.base.PlatformConfig;
28 import org.iotivity.base.ServiceType;
29 import org.iotivity.service.easysetup.mediator.ESConstants;
30 import org.iotivity.service.easysetup.mediator.EasySetup;
31 import org.iotivity.service.easysetup.mediator.RemoteEnrollee;
32 import org.iotivity.base.ModeType;
33 import org.iotivity.base.QualityOfService;
34 import org.iotivity.base.EntityHandlerResult;
35 import org.iotivity.base.OcResourceRequest;
36 import org.iotivity.base.ResourceProperty;
37 import org.iotivity.base.OcResourceHandle;
39 import android.util.Log;
40 import java.util.EnumSet;
41 import java.util.HashMap;
44 import android.content.Context;
46 public class ESEnrolleeHelper {
47 private static final String ALL_INTERFACE_TYPE = "0.0.0.0";
48 private static final String EMPTY_STRING = "";
49 private static final String FOUND_RESOURCE_IS_INVALID = "Found resource is invalid";
50 private static final String FAIL_TO_FIND_RESOURCE = "Fail to find resource";
51 private static final String FAIL_TO_REGISTER_ENROLLEE_RESOURCE = "Fail to register enrollee resource";
52 private static final String INTERFACE_TYPE_OF_THE_RESOURCE = "Interface Type of the resource : ";
53 private static final String RESOURCE_TYPE_OF_THE_RESOURCE = "Resource Type of the resource : ";
54 private static final String HOST_ADDRESS_OF_THE_RESOURCE = "Host address of the resource: ";
55 private static final String URI_OF_THE_RESOURCE = "URI of the resource: ";
56 private static final String ENROLLEEOUND = "Enrolleeound";
57 private static final String RT = "?rt=";
58 Map<String, String> queryParamsMap = new HashMap<String, String>();
59 private static final String TAG = "Enrollee Resource Creation: ";
60 private boolean isResourceFound = false;
61 private OcResource enrolleeResource;
62 private static final String RESOURCE_NAME = "/a/light";
63 private static final String BATCH_INTERFACE_TYPE = OcPlatform.BATCH_INTERFACE;
64 public static final boolean callbackcalled = false;
66 public ESEnrolleeHelper() {
67 enrolleeResource = null;
68 isResourceFound = false;
71 private boolean configurePlatform(Context context) {
72 PlatformConfig cfg = new PlatformConfig(context, ServiceType.IN_PROC,
73 ModeType.CLIENT_SERVER, ALL_INTERFACE_TYPE, 0,
74 QualityOfService.LOW);
75 OcPlatform.Configure(cfg);
79 private boolean registerEnrolleeResource(String resourceName,
80 String resourceType, String interfaceType) {
82 OcPlatform.EntityHandler entityHandler = new OcPlatform.EntityHandler() {
84 public EntityHandlerResult handleEntity(
85 OcResourceRequest ocResourceRequest) {
86 return EntityHandlerResult.OK;
90 OcResourceHandle handler = null;
93 handler = OcPlatform.registerResource(resourceName, resourceType,
94 interfaceType, entityHandler,
95 EnumSet.of(ResourceProperty.DISCOVERABLE));
96 } catch (OcException ex) {
97 Log.d(TAG, ex.getMessage());
100 if (handler == null) {
101 Log.d(TAG, FAIL_TO_REGISTER_ENROLLEE_RESOURCE);
108 OcPlatform.OnResourceFoundListener listener = new OcPlatform.OnResourceFoundListener() {
111 public void onFindResourceFailed(Throwable throwable, String s) {
112 Log.d(TAG, FAIL_TO_FIND_RESOURCE);
113 isResourceFound = true;
117 public void onResourceFound(OcResource ocResource) {
118 isResourceFound = true;
119 if (ocResource == null) {
120 Log.d(TAG, FOUND_RESOURCE_IS_INVALID);
122 enrolleeResource = ocResource;
123 Log.d(TAG, ENROLLEEOUND);
124 Log.d(TAG, URI_OF_THE_RESOURCE + ocResource.getUri());
125 Log.d(TAG, HOST_ADDRESS_OF_THE_RESOURCE + ocResource.getHost());
126 Log.d(TAG, RESOURCE_TYPE_OF_THE_RESOURCE
127 + ocResource.getResourceTypes().get(0));
128 Log.d(TAG, INTERFACE_TYPE_OF_THE_RESOURCE
129 + ocResource.getResourceInterfaces().get(1));
135 private OcResource findEnrolleeResource(String resourceType) {
137 String requestUri = OcPlatform.WELL_KNOWN_QUERY + RT + resourceType;
140 OcPlatform.findResource(EMPTY_STRING, requestUri,
141 EnumSet.of(OcConnectivityType.CT_DEFAULT), listener);
142 ESUtility.waitUntilEnrolleeResponse(isResourceFound);
143 } catch (OcException ex) {
144 Log.d(TAG, ex.getMessage());
149 public OcResource findEnrolleeResource(Context context) {
150 String requestUri = OcPlatform.WELL_KNOWN_QUERY + RT
151 + ESConstants.OC_RSRVD_ES_RES_TYPE_EASYSETUP;
152 configurePlatform(context);
154 OcPlatform.findResource(EMPTY_STRING, requestUri,
155 EnumSet.of(OcConnectivityType.CT_DEFAULT), listener);
156 ESUtility.waitUntilEnrolleeResponse(isResourceFound);
157 } catch (OcException ex) {
158 Log.d(TAG, ex.getMessage());
160 return enrolleeResource;
163 public OcResource createEnrolleeResource(Context context,
164 String resourceName, String resourceType, String interfaceType) {
166 if (configurePlatform(context) && registerEnrolleeResource(resourceName,
167 resourceType, interfaceType)) {
168 findEnrolleeResource(resourceType);
171 return enrolleeResource;
174 public RemoteEnrollee createRemoteEnrollee(Context context) {
175 OcResource ocResource = createEnrolleeResource(context, RESOURCE_NAME,
176 ESConstants.OC_RSRVD_ES_RES_TYPE_EASYSETUP, BATCH_INTERFACE_TYPE);
177 if (ocResource == null) {
180 return EasySetup.getInstance(context).createRemoteEnrollee(ocResource);