IOT-1060 API should return invalid param for negative timeout value
authorSunil Kumar K r <sunil.k14@samsung.com>
Mon, 4 Apr 2016 06:47:20 +0000 (12:17 +0530)
committerRandeep Singh <randeep.s@samsung.com>
Wed, 6 Apr 2016 07:05:58 +0000 (07:05 +0000)
timeout validation added. function names changed from json to cbor in provisioningclient

code reformated as per comments.
Change-Id: I20e254e7f50abde179a61b52e78c6fb92fcd3129
Signed-off-by: Sunil Kumar K R <sunil.k14@samsung.com>
Reviewed-on: https://gerrit.iotivity.org/gerrit/7565
Reviewed-by: Ashwini Kumar <k.ashwini@samsung.com>
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
Reviewed-by: Randeep Singh <randeep.s@samsung.com>
android/android_api/base/jni/JniOcProvisioning.cpp
android/android_api/base/jni/JniOcSecureResource.cpp
android/examples/provisioningclient/src/main/java/org/iotivity/base/examples/provisioningclient/ProvisioningClient.java
android/examples/provisioningclient/src/main/java/org/iotivity/base/examples/provisioningclient/StringConstants.java

index 5b2155a4ed014e13e88fea58c52194e1e61d121c..52d5bc9cd53067e5ef5da72d802b134d708639ba 100644 (file)
@@ -123,6 +123,11 @@ JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverUno
 
     try
     {
+        if (timeout < 0)
+        {
+            ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
+            return nullptr;
+        }
         OCStackResult result = OCSecure::discoverUnownedDevices((unsigned short)timeout, list);
 
         if (OC_STACK_OK != result)
@@ -191,6 +196,11 @@ JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_discoverOwn
 
     try
     {
+        if (timeout < 0)
+        {
+            ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
+            return nullptr;
+        }
         OCStackResult result = OCSecure::discoverOwnedDevices((unsigned short)timeout, list);
         if (OC_STACK_OK != result)
         {
@@ -221,6 +231,11 @@ JNIEXPORT jobjectArray JNICALL Java_org_iotivity_base_OcProvisioning_getDeviceSt
 
     try
     {
+        if (timeout < 0)
+        {
+            ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
+            return nullptr;
+        }
         OCStackResult result = OCSecure::getDevInfoFromNetwork((unsigned short)timeout,
                 ownedDevList, unownedDevList);
         if (OC_STACK_OK != result)
index 29e2ce8411c1f890e31c12af7d747a3402bb808a..77c8dc6df78d4a0364766ec6a23442e67c9059b7 100644 (file)
@@ -356,6 +356,12 @@ JNIEXPORT void JNICALL Java_org_iotivity_base_OcSecureResource_removeDevice
 (JNIEnv *env, jobject thiz, jint timeout, jobject jListener)
 {
     LOGD("OcSecureResource_removeDevice");
+    if (timeout < 0)
+    {
+        ThrowOcException(OC_STACK_INVALID_PARAM, "Timeout value cannot be negative");
+        return;
+    }
+
     if (!jListener)
     {
         ThrowOcException(OC_STACK_INVALID_PARAM, "provisionResultListener cannot be null");
index bc590b0524a3c23cd1c9d83b0aca35b9c3217b4a..fbd8eda01a68e04b937c8dc3b5464c4c955c3c6e 100644 (file)
@@ -14,20 +14,22 @@ import android.util.Log;
 import android.view.Gravity;
 import android.widget.LinearLayout;
 import android.widget.TextView;
+
+import org.iotivity.base.CredType;
+import org.iotivity.base.DeviceStatus;
+import org.iotivity.base.KeySize;
 import org.iotivity.base.ModeType;
 import org.iotivity.base.OcException;
 import org.iotivity.base.OcPlatform;
-import org.iotivity.base.PlatformConfig;
-import org.iotivity.base.QualityOfService;
-import org.iotivity.base.ServiceType;
 import org.iotivity.base.OcProvisioning;
 import org.iotivity.base.OcSecureResource;
-import org.iotivity.base.ProvisionResult;
-import org.iotivity.base.OxmType;
 import org.iotivity.base.OicSecAcl;
-import org.iotivity.base.CredType;
-import org.iotivity.base.KeySize;
-import org.iotivity.base.DeviceStatus;
+import org.iotivity.base.OxmType;
+import org.iotivity.base.PlatformConfig;
+import org.iotivity.base.ProvisionResult;
+import org.iotivity.base.QualityOfService;
+import org.iotivity.base.ServiceType;
+
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.FileOutputStream;
@@ -39,41 +41,11 @@ import java.util.EnumSet;
 import java.util.List;
 
 public class ProvisioningClient extends Activity implements
-    OcSecureResource.DoOwnershipTransferListener,OcSecureResource.ProvisionPairwiseDevicesListener {
+        OcSecureResource.DoOwnershipTransferListener, OcSecureResource.ProvisionPairwiseDevicesListener {
 
     private static final String TAG = "Provisioning Client: ";
     private static final int BUFFER_SIZE = 1024;
     int unownedDevCount = StringConstants.NUMBER_ZERO;
-    private String filePath = "";
-    private OcSecureResource newSecureResource;
-    private List<OcSecureResource> deviceList;
-    private List<OcSecureResource> ownedDeviceList;
-    private TextView mEventsTextView;
-
-    @Override
-    protected void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.activity_secure_provision_client);
-        mEventsTextView = new TextView(this);
-        mEventsTextView.setGravity(Gravity.BOTTOM);
-        mEventsTextView.setMovementMethod(new ScrollingMovementMethod());
-        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
-        layout.addView(mEventsTextView, new LinearLayout.LayoutParams(
-                        LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)
-        );
-        filePath = getFilesDir().getPath() + "/"; //  data/data/<package>/files/
-        //copy json when application runs first time
-        SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
-        boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
-        if (isFirstRun) {
-            copyJsonFromAsset();
-            SharedPreferences.Editor editor = wmbPreference.edit();
-            editor.putBoolean("FIRSTRUN", false);
-            editor.commit();
-        }
-        initOICStack();
-    }
-
     OcProvisioning.PinCallbackListener pinCallbackListener =
             new OcProvisioning.PinCallbackListener() {
                 @Override
@@ -82,7 +54,11 @@ public class ProvisioningClient extends Activity implements
                     return "";
                 }
             };
-
+    private String filePath = "";
+    private OcSecureResource newSecureResource;
+    private List<OcSecureResource> deviceList;
+    private List<OcSecureResource> ownedDeviceList;
+    private TextView mEventsTextView;
     OcSecureResource.ProvisionAclListener provisionAclListener =
             new OcSecureResource.ProvisionAclListener() {
                 @Override
@@ -97,7 +73,6 @@ public class ProvisioningClient extends Activity implements
                     }
                 }
             };
-
     OcSecureResource.ProvisionCredentialsListener provisionCredentialsListener =
             new OcSecureResource.ProvisionCredentialsListener() {
                 @Override
@@ -112,7 +87,6 @@ public class ProvisioningClient extends Activity implements
                     }
                 }
             };
-
     OcSecureResource.UnlinkDevicesListener unlinkDevicesListener =
             new OcSecureResource.UnlinkDevicesListener() {
                 @Override
@@ -127,7 +101,6 @@ public class ProvisioningClient extends Activity implements
                     }
                 }
             };
-
     OcSecureResource.RemoveDeviceListener removeDeviceListener =
             new OcSecureResource.RemoveDeviceListener() {
                 @Override
@@ -141,6 +114,30 @@ public class ProvisioningClient extends Activity implements
                 }
             };
 
+    @Override
+    protected void onCreate(Bundle savedInstanceState) {
+        super.onCreate(savedInstanceState);
+        setContentView(R.layout.activity_secure_provision_client);
+        mEventsTextView = new TextView(this);
+        mEventsTextView.setGravity(Gravity.BOTTOM);
+        mEventsTextView.setMovementMethod(new ScrollingMovementMethod());
+        LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
+        layout.addView(mEventsTextView, new LinearLayout.LayoutParams(
+                        LinearLayout.LayoutParams.MATCH_PARENT, 0, 1f)
+        );
+        filePath = getFilesDir().getPath() + "/"; //  data/data/<package>/files/
+        //copy CBOR file when application runs first time
+        SharedPreferences wmbPreference = PreferenceManager.getDefaultSharedPreferences(this);
+        boolean isFirstRun = wmbPreference.getBoolean("FIRSTRUN", true);
+        if (isFirstRun) {
+            copyCborFromAsset();
+            SharedPreferences.Editor editor = wmbPreference.edit();
+            editor.putBoolean("FIRSTRUN", false);
+            editor.commit();
+        }
+        initOICStack();
+    }
+
     /**
      * configure OIC platform and call findResource
      */
@@ -152,7 +149,7 @@ public class ProvisioningClient extends Activity implements
                 ModeType.CLIENT_SERVER,
                 "0.0.0.0", // bind to all available interfaces
                 0,
-                QualityOfService.LOW, filePath + StringConstants.OIC_CLIENT_JSON_DB_FILE);
+                QualityOfService.LOW, filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE);
         OcPlatform.Configure(cfg);
         try {
             /*
@@ -200,15 +197,13 @@ public class ProvisioningClient extends Activity implements
             List<String> resources = new ArrayList<String>();
             List<String> periods = new ArrayList<String>();
             List<String> recurrences = new ArrayList<String>();
-            String rownerID= "61646d69-6e44-6576-6963-655555494430";
-            recurrences.add("Daily");
-            resources.add("*");
-            //owners.add("adminDeviceUUID0");
-            periods.add("01-01-15");
+            recurrences.add(StringConstants.DEFAULT_RECURRENCES);
+            resources.add(StringConstants.DEFAULT_RESOURCES);
+            periods.add(StringConstants.DEFAULT_PERIOD);
             OicSecAcl acl1 = new OicSecAcl(newSecureResource.getDeviceID(), recurrences, periods,
-                    31, resources, rownerID);
+                    StringConstants.DEFAULT_PERMISSION, resources, StringConstants.DEFAULT_ROWNER_ID);
             OicSecAcl acl2 = new OicSecAcl(newSecureResource2.getDeviceID(), recurrences, periods,
-                    31, resources, rownerID);
+                    StringConstants.DEFAULT_PERMISSION, resources, StringConstants.DEFAULT_ROWNER_ID);
             newSecureResource.provisionPairwiseDevices(EnumSet.of(CredType.SYMMETRIC_PAIR_WISE_KEY),
                     KeySize.OWNER_PSK_LENGTH_128, acl1, newSecureResource2, acl2, this);
         } catch (Exception e) {
@@ -233,21 +228,21 @@ public class ProvisioningClient extends Activity implements
     }
 
     /**
-     * Copy svr db json file from assets folder to app data files dir
+     * Copy svr db CBOR dat file from assets folder to app data files dir
      */
-    private void copyJsonFromAsset() {
+    private void copyCborFromAsset() {
         InputStream inputStream = null;
         OutputStream outputStream = null;
         int length;
         byte[] buffer = new byte[BUFFER_SIZE];
         try {
-            inputStream = getAssets().open(StringConstants.OIC_CLIENT_JSON_DB_FILE);
+            inputStream = getAssets().open(StringConstants.OIC_CLIENT_CBOR_DB_FILE);
             File file = new File(filePath);
             //check files directory exists
             if (!(file.exists() && file.isDirectory())) {
                 file.mkdirs();
             }
-            outputStream = new FileOutputStream(filePath + StringConstants.OIC_CLIENT_JSON_DB_FILE);
+            outputStream = new FileOutputStream(filePath + StringConstants.OIC_CLIENT_CBOR_DB_FILE);
             while ((length = inputStream.read(buffer)) != -1) {
                 outputStream.write(buffer, 0, length);
             }
@@ -255,10 +250,10 @@ public class ProvisioningClient extends Activity implements
             logMessage(TAG + "Null pointer exception " + e.getMessage());
             Log.e(TAG, e.getMessage());
         } catch (FileNotFoundException e) {
-            logMessage(TAG + "Json svr db file not found " + e.getMessage());
+            logMessage(TAG + "CBOR svr db file not found " + e.getMessage());
             Log.e(TAG, e.getMessage());
         } catch (IOException e) {
-            logMessage(TAG + StringConstants.OIC_CLIENT_JSON_DB_FILE + " file copy failed");
+            logMessage(TAG + StringConstants.OIC_CLIENT_CBOR_DB_FILE + " file copy failed");
             Log.e(TAG, e.getMessage());
         } finally {
             if (inputStream != null) {
@@ -369,18 +364,14 @@ public class ProvisioningClient extends Activity implements
                     OcSecureResource ocSecureResourceDest = ownedDeviceList.get(1);
                     publishProgress(TAG + "ACL Provision for " + ocSecureResource.getDeviceID());
                     List<String> resources = new ArrayList<String>();
-                    
                     List<String> periods = new ArrayList<String>();
                     List<String> recurrences = new ArrayList<String>();
-
-                    String rownerID="61646d69-6e44-6576-6963-655555494430";
-
-                    recurrences.add("Daily");
-                    resources.add("*");
-                    
-                    periods.add("01-01-15");
+                    recurrences.add(StringConstants.DEFAULT_RECURRENCES);
+                    resources.add(StringConstants.DEFAULT_RESOURCES);
+                    periods.add(StringConstants.DEFAULT_PERIOD);
                     OicSecAcl aclObject = new OicSecAcl(ocSecureResourceDest.getDeviceID(),
-                            recurrences, periods, 31, resources, rownerID);
+                            recurrences, periods, StringConstants.DEFAULT_PERMISSION, resources,
+                            StringConstants.DEFAULT_ROWNER_ID);
                     ocSecureResource.provisionACL(aclObject, provisionAclListener);
                 } else {
                     publishProgress(TAG + "No Owned devices present");
@@ -447,7 +438,7 @@ public class ProvisioningClient extends Activity implements
                     List<String> linkedDevices = ocSecureResource.getLinkedDevices();
                     if (linkedDevices.size() > 0) {
                         for (int i = 0; i < linkedDevices.size(); i++) {
-                            publishProgress(TAG + "Linked Devices "+
+                            publishProgress(TAG + "Linked Devices " +
                                     (i + 1) + "= " + linkedDevices.get(i));
                         }
                     } else {
@@ -549,7 +540,7 @@ public class ProvisioningClient extends Activity implements
             try {
                 publishProgress(TAG + "Initiate Owned device Discovery");
                 ownedDeviceList = OcProvisioning.discoverOwnedDevices
-                    (StringConstants.DISCOVERY_TIMEOUT_10);
+                        (StringConstants.DISCOVERY_TIMEOUT_10);
                 if (ownedDeviceList.size() > 0) {
                     for (int i = 0; i < ownedDeviceList.size(); i++) {
                         publishProgress(TAG + "Owned Discovered Device " + (i + 1) + "= " +
index 7199913a5e965ec64062830768c3fa0224ce033e..eb3a1892c62b15a66e064af505817cf1f6da9711 100644 (file)
@@ -6,7 +6,12 @@ public interface StringConstants {
     public static final int DISCOVERY_TIMEOUT_10 =  10;
     public static final int DISCOVERY_TIMEOUT_20 =  20;
     public static final int ERROR_CODE =  1;
-    public static final String OIC_CLIENT_JSON_DB_FILE =  "oic_svr_db_client.dat";
+    public static final String DEFAULT_ROWNER_ID =  "61646d69-6e44-6576-6963-655555494430";
+    public static final String DEFAULT_RESOURCES =  "*";
+    public static final String DEFAULT_RECURRENCES =  "Daily";
+    public static final String DEFAULT_PERIOD =  "12-12-16";
+    public static final int DEFAULT_PERMISSION = 31;
+    public static final String OIC_CLIENT_CBOR_DB_FILE =  "oic_svr_db_client.dat";
     public static final String MESSAGE = "message";
     public static final String OIC_SQL_DB_FILE =  "Pdm.db";
     public static final int  CREDENTIAL_TYPE=1;