+
+/**
+ * Check whether persistent storage is valid
+ * @return OC_STACK_OK if valid, other errors otherwise;
+ */
+OCStackResult CheckPersistentStorage(OCPersistentStorage* ps)
+{
+ OIC_LOG_V(INFO, TAG, "In %s", __func__);
+
+ FILE *fp = NULL;
+ OCStackResult ret = OC_STACK_SVR_DB_NOT_EXIST;
+ OicSecDoxm_t* doxm = NULL;
+ OicSecPstat_t* pstat = NULL;
+ uint8_t *data = NULL;
+ size_t size = 0;
+
+ if (NULL == ps)
+ {
+ OIC_LOG(ERROR, TAG, "NULL PersistentStorage Parameter");
+ ret = OC_STACK_INVALID_PARAM;
+ goto exit;
+ }
+
+ // Check whether the DB file exists
+ fp = ps->open(SVR_DB_DAT_FILE_NAME, "r+b");
+ if (NULL == fp)
+ {
+ OIC_LOG(ERROR, TAG, "DB file cannot be opened");
+ ret = OC_STACK_SVR_DB_NOT_EXIST;
+ SetPSStatus(PS_OPEN_FAIL);
+ goto exit;
+ }
+ ps->close(fp);
+
+ OIC_LOG(INFO, TAG, "Checking doxm resource...");
+ //Check DOXM resource
+ ret = GetSecureVirtualDatabaseFromPS2(ps, OIC_JSON_DOXM_NAME, &data, &size);
+ // If database read failed
+ if (OC_STACK_OK != ret)
+ {
+ OIC_LOG (ERROR, TAG, "Can not find the DOXM Resource in SVR DB.");
+ ret = OC_STACK_INCONSISTENT_DB;
+ SetPSStatus(PS_PARSE_FAIL);
+ goto exit;
+ }
+ if (data && 0 < size)
+ {
+ // Read DOXM resource from PS
+ ret = CBORPayloadToDoxm(data, size, &doxm);
+ if (OC_STACK_OK != ret)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to Convert CBOR to Doxm bin : %d", ret);
+ ret = OC_STACK_INCONSISTENT_DB;
+ SetPSStatus(PS_PARSE_FAIL);
+ goto exit;
+ }
+ }
+ else
+ {
+ ret = OC_STACK_INCONSISTENT_DB;
+ SetPSStatus(PS_PARSE_FAIL);
+ goto exit;
+ }
+ if (data)
+ {
+ OICFree(data);
+ data = NULL;
+ }
+
+ OIC_LOG(INFO, TAG, "Checking pstat resource...");
+ //Check PSTAT resource
+ ret = GetSecureVirtualDatabaseFromPS2(ps, OIC_JSON_PSTAT_NAME, &data, &size);
+ // If database read failed
+ if (OC_STACK_OK != ret)
+ {
+ OIC_LOG (ERROR, TAG, "Can not find the PSTAT Resource in SVR DB.");
+ ret = OC_STACK_INCONSISTENT_DB;
+ SetPSStatus(PS_PARSE_FAIL);
+ goto exit;
+ }
+ if (data && 0 < size)
+ {
+ // Read ACL resource from PS
+ ret = CBORPayloadToPstat(data, size, &pstat);
+ if (OC_STACK_OK != ret)
+ {
+ OIC_LOG_V(ERROR, TAG, "Failed to Convert CBOR to PSTAT bin : %d", ret);
+ ret = OC_STACK_INCONSISTENT_DB;
+ SetPSStatus(PS_PARSE_FAIL);
+ goto exit;
+ }
+ }
+ else
+ {
+ ret = OC_STACK_INCONSISTENT_DB;
+ SetPSStatus(PS_PARSE_FAIL);
+ goto exit;
+ }
+ if (data)
+ {
+ OICFree(data);
+ data = NULL;
+ }
+
+ SetPSStatus(PS_NORMAL);
+
+ ret = OC_STACK_OK;
+ OIC_LOG(INFO, TAG, "All Secure Virtual Resources are fine.");
+
+exit:
+ if (data)
+ {
+ OICFree(data);
+ }
+ if (doxm)
+ {
+ DeleteDoxmBinData(doxm);
+ }
+ if (pstat)
+ {
+ DeletePstatBinData(pstat);
+ }
+
+ OIC_LOG_V(INFO, TAG, "Out %s", __func__);
+
+ return ret;
+}
+
+/**
+ * Get the Secure Virtual Resource (SVR) type from the URI.
+ * @param uri [IN] Pointer to URI in question.
+ * @return The OicSecSvrType_t of the URI passed (note: if not a Secure Virtual
+ Resource, e.g. /a/light, will return "NOT_A_SVR_TYPE" enum value)
+ */
+static const char URI_QUERY_CHAR = '?';
+OicSecSvrType_t GetSvrTypeFromUri(const char* uri)
+{
+ if (!uri)
+ {
+ return NOT_A_SVR_RESOURCE;
+ }
+
+ // Remove query from Uri for resource string comparison
+ size_t uriLen = strlen(uri);
+ char *query = strchr (uri, URI_QUERY_CHAR);
+ if (query)
+ {
+ uriLen = query - uri;
+ }
+
+ size_t svrLen = 0;
+
+ svrLen = strlen(OIC_RSRC_ACL_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_ACL_URI, svrLen))
+ {
+ return OIC_R_ACL_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_AMACL_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_AMACL_URI, svrLen))
+ {
+ return OIC_R_AMACL_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_CRED_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_CRED_URI, svrLen))
+ {
+ return OIC_R_CRED_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_CRL_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_CRL_URI, svrLen))
+ {
+ return OIC_R_CRL_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_DOXM_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_DOXM_URI, svrLen))
+ {
+ return OIC_R_DOXM_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_DPAIRING_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_DPAIRING_URI, svrLen))
+ {
+ return OIC_R_DPAIRING_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_PCONF_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_PCONF_URI, svrLen))
+ {
+ return OIC_R_PCONF_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_PSTAT_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_PSTAT_URI, svrLen))
+ {
+ return OIC_R_PSTAT_TYPE;
+ }
+ }
+
+ svrLen = strlen(OIC_RSRC_SACL_URI);
+ if(uriLen == svrLen)
+ {
+ if(0 == strncmp(uri, OIC_RSRC_SACL_URI, svrLen))
+ {
+ return OIC_R_SACL_TYPE;
+ }
+ }
+
+ return NOT_A_SVR_RESOURCE;
+}