Merge "Partial Implementation of US1574:"
[platform/upstream/iotivity.git] / csdk / android / OCLib / src / com / oc / OCDiscovery.java
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
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
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
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.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 package com.oc;
22
23 import java.util.HashMap;
24
25 public class OCDiscovery {
26   static private native void jniFindResource(String host, HashMap<String, String> filter,
27       OCResourceResultHandler handler);
28
29   /**
30    * Internal API for Service and Resource Discovery
31    * 
32    * @param host - Host IP Address of a service to direct resource discovery query. If null or
33    *        empty, performs service discovery OR resource discovery query to ALL group members.
34    * @param filter<String FilterType, String Value> - Filter Type - Can be one of two of the link
35    *        format equivalent attribute types declared in table 7 of the
36    *        OC-Lite-Product-Specification-V1 Document. Either "n" (i.e. Resource Name) or "rt"
37    *        (i.e. Resource Type). Value - Can be any valid String that's defined to work with CoRE
38    *        Link Attributes as defined in the RFC6690.
39    * @param handler - Handles callbacks, success states and failure states.
40    * 
41    *        Four modes of discovery defined as follows: 
42    *        (NULL/Empty, NULL/Empty) - Performs ALL service discovery AND ALL resource discovery. 
43    *        (NULL/Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
44    *                                  from ALL services. 
45    *        (Not Empty, NULL/Empty) - Performs ALL resource discovery on a particular service. 
46    *        (Not Empty, Not Empty) - Performs query for a filtered/scoped/particular resource(s) 
47    *                                  from a particular service.
48    * 
49    *        Thread-Safety: TODO Determine if it is going to be safe to call in a UI thread
50    */
51   private void findResource(String host, HashMap<String, String> filter,
52       OCResourceResultHandler handler) {
53     /*** Have to make sure all values are valid so the pass down to JNI runs smoothly. ***/
54     String tempHost = host;
55     HashMap<String, String> tempFilter = filter;
56     if (host == null) {
57       tempHost = "";
58     }
59     if (filter == null) {
60       tempFilter = new HashMap<String, String>();
61       tempFilter.put("", ""); // TODO: NOt sure if putting an empty key is necessary if the hashmap is empty.
62     }
63     // Todo: Throw exception if handler is invalid (i.e. null).
64     /*** Done ensuring all values are valid.  ***/
65     jniFindResource(tempHost, tempFilter, handler);
66   }
67
68   /**
69    * Queries the network for available resources.
70    * 
71    * NOTE: This gets posted to a new thread so it doesn't hold the UI or network thread.
72    * 
73    * @param serviceURL - The URL to the service. NULL for a broadcast/multicast.
74    * @param resourceType - The resource type filter. NULL for match any.
75    * 
76    * @throws This may throw if the service is not started, network is bad, etc.
77    * 
78    *         Thread-Safety: TODO Determine if it is going to be safe to call in a UI thread
79    */
80   public void findResourceByType(String serviceURL, String resourceType,
81       OCResourceResultHandler handler) {
82     HashMap<String, String> filteredMap = new HashMap<String, String>();
83     filteredMap.put("rt", resourceType);
84     findResource(serviceURL, filteredMap, handler);
85   }
86 }