replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / base / OcPlatform.java
index da9c25e..954eab2 100644 (file)
@@ -1,28 +1,29 @@
 /*
- * //******************************************************************
- * //
- * // Copyright 2015 Intel Corporation.
- * //
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
- * //
- * // Licensed under the Apache License, Version 2.0 (the "License");
- * // you may not use this file except in compliance with the License.
- * // You may obtain a copy of the License at
- * //
- * //      http://www.apache.org/licenses/LICENSE-2.0
- * //
- * // Unless required by applicable law or agreed to in writing, software
- * // distributed under the License is distributed on an "AS IS" BASIS,
- * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * // See the License for the specific language governing permissions and
- * // limitations under the License.
- * //
- * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *******************************************************************
+ *
+ * Copyright 2015 Intel Corporation.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
  */
 
 package org.iotivity.base;
 
 import org.iotivity.ca.CaInterface;
+import org.iotivity.base.BuildConfig;
 
 import java.util.EnumSet;
 import java.util.Iterator;
@@ -40,6 +41,10 @@ public final class OcPlatform {
         System.loadLibrary("octbstack");
         System.loadLibrary("connectivity_abstraction");
         System.loadLibrary("oc");
+        if (0 != BuildConfig.SECURED)
+        {
+            System.loadLibrary("ocprovision");
+        }
         System.loadLibrary("ocstack-jni");
     }
 
@@ -72,6 +77,8 @@ public final class OcPlatform {
     private static volatile boolean sIsPlatformInitialized = false;
     private static QualityOfService sPlatformQualityOfService = QualityOfService.NA;
 
+    private static volatile boolean sIsStopPlatform = false;
+
     private OcPlatform() {
     }
 
@@ -84,6 +91,12 @@ public final class OcPlatform {
      * @param platformConfig platform configuration
      */
     public synchronized static void Configure(PlatformConfig platformConfig) {
+        if (sIsStopPlatform)
+        {
+            OcPlatform.start();
+            sIsStopPlatform = false;
+        }
+
         if (!sIsPlatformInitialized) {
             CaInterface.initialize(platformConfig.getActivity(), platformConfig.getContext());
 
@@ -95,7 +108,12 @@ public final class OcPlatform {
                     platformConfig.getIpAddress(),
                     platformConfig.getPort(),
                     platformConfig.getQualityOfService().getValue(),
-                    platformConfig.getSvrDbPath()
+                    platformConfig.getSvrDbPath(),
+                    platformConfig.getDBDefaultPath(),
+                    platformConfig.getDBRescuePath(),
+                    platformConfig.getKeySize(),
+                    platformConfig.getKey(),
+                    platformConfig.getAvailableTransportType()
             );
 
             sIsPlatformInitialized = true;
@@ -107,7 +125,33 @@ public final class OcPlatform {
                                          String ipAddress,
                                          int port,
                                          int qualityOfService,
-                                         String dbPath);
+                                         String dbPath,
+                                         String dbPathDefault,
+                                         String dbRescuePath,
+                                         int keySize,
+                                         byte[] key,
+                                         int transport);
+
+    /**
+     * API for stop all process of the OcPlatform.
+     * All of threads and memory will be terminated by this API.
+     * Iotivity Core can be started again through Configure(PlatformConfig platformConfig) API.
+     * Both Configure and Shutdown API is filtering for duplicated calling even while processing.
+     * <p>
+     * Note: This API is for both server and client side.
+     * </p>
+     */
+    public synchronized static void Shutdown() {
+        if (!sIsStopPlatform)
+        {
+            OcPlatform.stop();
+            sIsStopPlatform = true;
+            sIsPlatformInitialized = false;
+        }
+    }
+
+    private static native void stop();
+    private static native void start();
 
     /**
      * API for notifying base that resource's attributes have changed.
@@ -167,6 +211,10 @@ public final class OcPlatform {
             OcResourceResponse ocResourceResponse) throws OcException {
         OcPlatform.initCheck();
 
+        if (ocObservationIdList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
+        }
+
         byte[] idArr = new byte[ocObservationIdList.size()];
         Iterator<Byte> it = ocObservationIdList.iterator();
         int i = 0;
@@ -206,6 +254,10 @@ public final class OcPlatform {
             QualityOfService qualityOfService) throws OcException {
         OcPlatform.initCheck();
 
+        if (ocObservationIdList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocObservationIdList cannot be null");
+        }
+
         byte[] idArr = new byte[ocObservationIdList.size()];
         Iterator<Byte> it = ocObservationIdList.iterator();
         int i = 0;
@@ -536,12 +588,59 @@ public final class OcPlatform {
             OcDeviceInfo ocDeviceInfo) throws OcException {
         OcPlatform.initCheck();
         OcPlatform.registerDeviceInfo0(
-                ocDeviceInfo.getDeviceName()
+                ocDeviceInfo.getDeviceName(),
+                ocDeviceInfo.getDeviceTypes().toArray(
+                        new String[ocDeviceInfo.getDeviceTypes().size()]
+                )
         );
     }
 
     private static native void registerDeviceInfo0(
-            String deviceName
+            String deviceName,
+            String[] deviceTypes
+    ) throws OcException;
+
+    /**
+     * Set param Info
+     *
+     * @param ocDeviceInfo object containing all the device specific information
+     * @throws OcException if failure
+     */
+    public static void setPropertyValue(
+            int path, String propName, String propValue) throws OcException {
+        OcPlatform.initCheck();
+        OcPlatform.setPropertyValue1(path, propName, propValue);
+    }
+
+    public static void setPropertyValue(
+            int path, String propName, List<String> propValue) throws OcException {
+        OcPlatform.initCheck();
+        OcPlatform.setPropertyValue0(path, propName, propValue.toArray(new String[propValue.size()]));
+    }
+
+    public static void getPropertyValue(
+            int path, String propName, String propValue) throws OcException {
+        OcPlatform.initCheck();
+        OcPlatform.getPropertyValue0(path, propName, propValue);
+    }
+
+    private static native void setPropertyValue1(
+            int path,
+            String propName,
+            String propValue
+    ) throws OcException;
+
+
+    private static native void setPropertyValue0(
+            int path,
+            String propName,
+            String[] propValue
+    ) throws OcException;
+
+    private static native void getPropertyValue0(
+            int path,
+            String propName,
+            String propValue
     ) throws OcException;
 
     /**
@@ -622,6 +721,11 @@ public final class OcPlatform {
             OcResourceHandle ocResourceCollectionHandle,
             List<OcResourceHandle> ocResourceHandleList) throws OcException {
         OcPlatform.initCheck();
+
+        if (ocResourceHandleList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
+        }
+
         OcPlatform.bindResources0(
                 ocResourceCollectionHandle,
                 ocResourceHandleList.toArray(
@@ -663,6 +767,11 @@ public final class OcPlatform {
             OcResourceHandle ocResourceCollectionHandle,
             List<OcResourceHandle> ocResourceHandleList) throws OcException {
         OcPlatform.initCheck();
+
+        if (ocResourceHandleList == null) {
+            throw new OcException(ErrorCode.INVALID_PARAM, "ocResourceHandleList cannot be null");
+        }
+
         OcPlatform.unbindResources0(
                 ocResourceCollectionHandle,
                 ocResourceHandleList.toArray(
@@ -825,6 +934,43 @@ public final class OcPlatform {
             OcPresenceHandle ocPresenceHandle) throws OcException;
 
     /**
+     * Subscribes to a server's device presence change events.
+     *
+     * @param host                The IP address/addressable name of the server to subscribe to.
+     * @param di                  Vector which can have the devices id.
+     * @param connectivityTypeSet Set of connectivity types, e.g. IP.
+     * @param onObserveListener   The handler method will be invoked with a map
+     *                            of attribute name and values.
+     * @return a handle object that can be used to identify this subscription request.
+     *         It can be used to unsubscribe from these events in the future.
+     * @throws OcException if failure.
+     */
+    public static OcPresenceHandle subscribeDevicePresence(
+            String host,
+            List<String> di,
+            EnumSet<OcConnectivityType> connectivityTypeSet,
+            OcResource.OnObserveListener onObserveListener) throws OcException {
+        OcPlatform.initCheck();
+        int connTypeInt = 0;
+
+        for (OcConnectivityType connType : OcConnectivityType.values()) {
+            if (connectivityTypeSet.contains(connType))
+                connTypeInt |= connType.getValue();
+        }
+        return OcPlatform.subscribeDevicePresence0(
+                host,
+                di.toArray(new String[di.size()]),
+                connTypeInt,
+                onObserveListener);
+    }
+
+    private static native OcPresenceHandle subscribeDevicePresence0(
+            String host,
+            String[] di,
+            int connectivityType,
+            OcResource.OnObserveListener onObserveListener) throws OcException;
+
+    /**
      * Creates a resource proxy object so that get/put/observe functionality can be used without
      * discovering the object in advance. Note that the consumer of this method needs to provide
      * all of the details required to correctly contact and observe the object. If the consumer
@@ -892,11 +1038,87 @@ public final class OcPlatform {
             throws OcException;
 
     /**
+     *  Method to find all devices which are eligible for direct pairing and return the list.
+     *
+     *  @param timeout timeout for discovering direct pair devices.
+     *  @param FindDirectPairingListener Callback function, which will receive the list of direct
+     *                                  pairable devices.
+     *  @throws OcException
+     */
+   public static native void findDirectPairingDevices(int timeout,
+            FindDirectPairingListener onFindDirectPairingListener) throws OcException;
+
+    /**
+     *  Method to get list of all paired devices for a given device.
+     *
+     *  @param GetDirectPairedListener Callback function, which will receive the list of direct
+     *                                 paired devices.
+     *  @throws OcException
+     */
+    public native void getDirectPairedDevices(GetDirectPairedListener onGetDirectPairedListener)
+        throws OcException;
+
+    /**
+     *  Method to perform direct pairing between two devices.
+     *
+     *  @param peer  Target peer
+     *  @param prmType Pairing Method to be used for Pairing
+     *  @param pin pin
+     *  @param DirectPairingListener Callback function, which will be called after
+     *                                      completion of direct pairing.
+     *  @throws OcException
+     */
+    public static void doDirectPairing(
+            OcDirectPairDevice peer,
+            OcPrmType prmType,
+            String pin,
+            DirectPairingListener onDirectPairingListener) throws OcException {
+
+        OcPlatform.doDirectPairing0(
+                peer,
+                prmType.getValue(),
+                pin,
+                onDirectPairingListener
+                );
+    }
+
+    private static native void doDirectPairing0(OcDirectPairDevice peer,
+            int pmSel, String pinNumber, DirectPairingListener onDirectPairingListener)
+    throws OcException;
+
+    /**
+     * An FindDirectPairingListener can be registered via the OcPlatform.findDirectPairingDevices call.
+     * Event listeners are notified asynchronously
+     */
+    public interface FindDirectPairingListener {
+        public void onFindDirectPairingListener(List<OcDirectPairDevice> ocPairedDeviceList);
+    }
+
+    /**
+     * Listerner to Get List of already Direct Paired devices.
+     * An GetDirectPairedListener can be registered via the OcPlatform.getDirectPairedDevices call.
+     * Event listeners are notified asynchronously
+     */
+    public interface GetDirectPairedListener {
+        public void onGetDirectPairedListener(List<OcDirectPairDevice> ocPairedDeviceList);
+    }
+
+    /**
+     * Listner to get result of doDirectPairing.
+     * An DirectPairingListener can be registered via the OcPlatform.doDirectPairing call.
+     * Event listeners are notified asynchronously
+     */
+    public interface DirectPairingListener {
+        public void onDirectPairingListener(String devId, int result);
+    }
+
+    /**
      * An OnResourceFoundListener can be registered via the OcPlatform.findResource call.
      * Event listeners are notified asynchronously
      */
     public interface OnResourceFoundListener {
         public void onResourceFound(OcResource resource);
+        public void onFindResourceFailed(Throwable ex, String uri);
     }
 
     /**
@@ -926,11 +1148,29 @@ public final class OcPlatform {
     /**
      * An EntityHandler can be registered via the OcPlatform.registerResource call.
      * Event listeners are notified asynchronously
+     *
+     * @note entityhandler callback :
+     * When you set specific return value like EntityHandlerResult.OK, SLOW
+     * and etc in entity handler callback,
+     * ocstack will be not send response automatically to client
+     * except for error return value like EntityHandlerResult.ERROR
+     * If you want to send response to client with specific result,
+     * sendResponse API should be called with the result value.
      */
     public interface EntityHandler {
         public EntityHandlerResult handleEntity(OcResourceRequest ocResourceRequest);
     }
 
+    /**
+     * Listner to get result of findKeepAliveResource and sendKeepAliveRequest.
+     * An PingListener can be registered via the OcPlatform.KeepAliveListener and
+     * OcPlatform.sendKeepAliveRequest call.
+     * Event listeners are notified asynchronously
+     */
+    public interface KeepAliveListener {
+        public void onKeepAliveListener(OcRepresentation ocRepresentation, int result);
+    }
+
     private static void initCheck() {
         if (!sIsPlatformInitialized) {
             throw new IllegalStateException("OcPlatform must be configured by making a call to " +
@@ -947,4 +1187,87 @@ public final class OcPlatform {
         OcPlatform.initCheck();
         return sPlatformQualityOfService;
     }
+
+    /**
+     * Create an account manager object that can be used for doing request to account server.
+     * You can only create this object if OCPlatform was initialized to be a Client or
+     * Client/Server. Otherwise, this will return an empty shared ptr.
+     *
+     * @note For now, OCPlatform SHOULD be initialized to be a Client/Server(Both) for the
+     *       methods of this object to work since device id is not generated on Client mode.
+     *
+     * @param host                Host IP Address of a account server.
+     * @param connectivityTypeSet Set of types of connectivity. Example: CT_ADAPTER_IP
+     * @return new AccountManager object
+     * @throws OcException if failure
+     */
+    public static OcAccountManager constructAccountManagerObject(
+            String host,
+            EnumSet<OcConnectivityType> connectivityTypeSet) throws OcException {
+        OcPlatform.initCheck();
+        int connTypeInt = 0;
+
+        for (OcConnectivityType connType : OcConnectivityType.values()) {
+            if (connectivityTypeSet.contains(connType))
+            {
+                connTypeInt |= connType.getValue();
+            }
+        }
+        return OcPlatform.constructAccountManagerObject0(
+                host,
+                connTypeInt
+                );
+    }
+
+    private static native OcAccountManager constructAccountManagerObject0(
+            String host,
+            int connectivityType) throws OcException;
+    /**
+     * Method to get device Id in byte array.
+     * @return My DeviceId.
+     */
+    public static native byte[] getDeviceId();
+
+    /**
+     * Method to set DeviceId.
+     */
+    public static native void setDeviceId(byte[] deviceId) throws OcException;
+
+    /**
+    * gets OCRepresentation of KeepAlive resource from given host.
+    *
+    * @param host                   Host IP Address of pingResource
+    * @param onKeepAliveFoundListener Function to callback with result code and OCRepresentation.
+    * @throws OcException if failure
+    */
+    public static void findKeepAliveResource(String host,
+                                             KeepAliveListener onKeepAliveFoundListener)
+                                             throws OcException {
+        OcPlatform.initCheck();
+        OcPlatform.findKeepAliveResourceImpl(host, onKeepAliveFoundListener);
+    }
+
+    private static native void findKeepAliveResourceImpl(String host,
+                                                         KeepAliveListener onKeepAliveFoundListener)
+                                                         throws OcException;
+
+    /**
+    * send KeepAlive request to given host.
+    *
+    * @param interval             interval time of expected pong response
+    * @param keepAliveResponseListener handles callback
+    * @throws OcException if failure
+    *
+    */
+    public static void sendKeepAliveRequest(String host, OcRepresentation ocRepresentation,
+                                            KeepAliveListener keepAliveResponseListener)
+                                            throws OcException {
+        OcPlatform.initCheck();
+        OcPlatform.sendKeepAliveRequestImpl(host, ocRepresentation, keepAliveResponseListener);
+    }
+
+    private static native void sendKeepAliveRequestImpl(String host,
+                                                        OcRepresentation ocRepresentation,
+                                                        KeepAliveListener keepAliveResponseListener)
+                                                        throws OcException;
 }