// limitations under the License.
//
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
+
#ifdef WITH_ARDUINO
#define __STDC_LIMIT_MACROS
#endif
#include <stdlib.h>
#include <string.h>
-#include "ocstack.h"
+
+#include "cainterface.h"
#include "logger.h"
-#include "oic_malloc.h"
#include "ocpayload.h"
#include "ocpayloadcbor.h"
+#include "ocstack.h"
+#include "oic_malloc.h"
#include "payload_logging.h"
-#include "cJSON.h"
-#include "cainterface.h"
-#include "secureresourcemanager.h"
#include "resourcemanager.h"
+#include "secureresourcemanager.h"
#include "srmresourcestrings.h"
#include "srmutility.h"
const size_t DB_FILE_SIZE_BLOCK = 1023;
/**
- * Gets the Secure Virtual Database size.
+ * Gets the Secure Virtual Database size
*
- * @param ps pointer of OCPersistentStorage for the SVR name ("acl", "cred", "pstat" etc).
+ * @param ps - pointer of OCPersistentStorage for the Secure Virtual Resource(s)
*
- * @return total size of the SVR database.
+ * @return size_t - total size of the SVR database
*/
-static size_t GetSVRDatabaseSize(const OCPersistentStorage* ps)
+static size_t GetSVRDatabaseSize(const OCPersistentStorage *ps)
{
- size_t size = 0;
if (!ps)
{
- return size;
+ return 0;
}
- size_t bytesRead = 0;
- char buffer[DB_FILE_SIZE_BLOCK];
- FILE* fp = ps->open(SVR_DB_DAT_FILE_NAME, "r");
+ size_t size = 0;
+ char buffer[DB_FILE_SIZE_BLOCK]; // can not initialize with declaration
+ // but maybe not needed to initialize
+ FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
if (fp)
{
+ size_t bytesRead = 0;
do
{
bytesRead = ps->read(buffer, 1, DB_FILE_SIZE_BLOCK, fp);
size += bytesRead;
- } while (bytesRead > 0);
+ } while (bytesRead);
ps->close(fp);
}
return size;
}
-char* GetSVRDatabase()
-{
- char * jsonStr = NULL;
- FILE * fp = NULL;
- OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
- int size = GetSVRDatabaseSize(ps);
- if (0 == size)
- {
- OIC_LOG (ERROR, TAG, "FindSVRDatabaseSize failed");
- return NULL;
- }
-
- if (ps && ps->open)
- {
- // Open default SRM database file. An app could change the path for its server.
- fp = ps->open(SVR_DB_FILE_NAME, "r");
- if (fp)
- {
- jsonStr = (char*)OICMalloc(size + 1);
- VERIFY_NON_NULL(TAG, jsonStr, FATAL);
- size_t bytesRead = ps->read(jsonStr, 1, size, fp);
- jsonStr[bytesRead] = '\0';
-
- OIC_LOG_V(DEBUG, TAG, "Read %zu bytes from SVR database file", bytesRead);
- ps->close(fp);
- fp = NULL;
- }
- else
- {
- OIC_LOG (ERROR, TAG, "Unable to open SVR database file!!");
- }
- }
-
-exit:
- if (ps && fp)
- {
- ps->close(fp);
- }
- return jsonStr;
-}
-
-OCStackResult UpdateSVRDatabase(const char* rsrcName, cJSON* jsonObj)
-{
- OCStackResult ret = OC_STACK_ERROR;
- cJSON *jsonSVRDb = NULL;
- OCPersistentStorage* ps = NULL;
-
- // Read SVR database from PS
- char* jsonSVRDbStr = GetSVRDatabase();
- VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
-
- // Use cJSON_Parse to parse the existing SVR database
- jsonSVRDb = cJSON_Parse(jsonSVRDbStr);
- VERIFY_NON_NULL(TAG,jsonSVRDb, ERROR);
-
- OICFree(jsonSVRDbStr);
- jsonSVRDbStr = NULL;
-
- //If Cred resource gets updated with empty list then delete the Cred
- //object from database.
- if(NULL == jsonObj && (0 == strcmp(rsrcName, OIC_JSON_CRED_NAME)))
- {
- cJSON_DeleteItemFromObject(jsonSVRDb, rsrcName);
- }
- else if (jsonObj->child )
- {
- // Create a duplicate of the JSON object which was passed.
- cJSON* jsonDuplicateObj = cJSON_Duplicate(jsonObj, 1);
- VERIFY_NON_NULL(TAG,jsonDuplicateObj, ERROR);
-
- cJSON* jsonObj = cJSON_GetObjectItem(jsonSVRDb, rsrcName);
-
- /*
- ACL, PStat & Doxm resources at least have default entries in the database but
- Cred resource may have no entries. The first cred resource entry (for provisioning tool)
- is created when the device is owned by provisioning tool and it's ownerpsk is generated.*/
- if((strcmp(rsrcName, OIC_JSON_CRED_NAME) == 0 ||
- strcmp(rsrcName, OIC_JSON_CRL_NAME) == 0 ||
- strcmp(rsrcName, OIC_JSON_PCONF_NAME) == 0 ||
- strcmp(rsrcName, OIC_JSON_DPAIRING_NAME) == 0) && (!jsonObj))
- {
- // Add the fist cred object in existing SVR database json
- cJSON_AddItemToObject(jsonSVRDb, rsrcName, jsonDuplicateObj->child);
- }
- else
- {
- VERIFY_NON_NULL(TAG,jsonObj, ERROR);
-
- // Replace the modified json object in existing SVR database json
- cJSON_ReplaceItemInObject(jsonSVRDb, rsrcName, jsonDuplicateObj->child);
- }
- }
-
- // Generate string representation of updated SVR database json object
- jsonSVRDbStr = cJSON_PrintUnformatted(jsonSVRDb);
- VERIFY_NON_NULL(TAG,jsonSVRDbStr, ERROR);
-
- // Update the persistent storage with new SVR database
- ps = SRMGetPersistentStorageHandler();
- if (ps && ps->open)
- {
- FILE* fp = ps->open(SVR_DB_FILE_NAME, "w");
- if (fp)
- {
- size_t bytesWritten = ps->write(jsonSVRDbStr, 1, strlen(jsonSVRDbStr), fp);
- if (bytesWritten == strlen(jsonSVRDbStr))
- {
- ret = OC_STACK_OK;
- }
- OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", bytesWritten);
- ps->close(fp);
- fp = NULL;
- }
- else
- {
- OIC_LOG (ERROR, TAG, "Unable to open SVR database file!! ");
- }
- }
-
-exit:
- OICFree(jsonSVRDbStr);
- cJSON_Delete(jsonSVRDb);
-
- return ret;
-}
-
+/**
+ * Gets the Secure Virtual Database from the Persistent Storage
+ *
+ * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
+ * @param data - pointer of the returned Secure Virtual Resource(s)
+ * @param size - pointer of the returned size of Secure Virtual Resource(s)
+ *
+ * @return OCStackResult - result of getting Secure Virtual Resource(s)
+ */
OCStackResult GetSecureVirtualDatabaseFromPS(const char *rsrcName, uint8_t **data, size_t *size)
{
- if (!data || !size)
+ OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS IN");
+ if (!data || *data || !size)
{
return OC_STACK_INVALID_PARAM;
}
- OCStackResult ret = OC_STACK_ERROR;
- *data = NULL;
- FILE *fp = NULL;
+ FILE *fp = NULL;
+ uint8_t *fsData = NULL;
size_t fileSize = 0;
+ OCStackResult ret = OC_STACK_ERROR;
OCPersistentStorage *ps = SRMGetPersistentStorageHandler();
VERIFY_NON_NULL(TAG, ps, ERROR);
fileSize = GetSVRDatabaseSize(ps);
- if (fileSize != 0)
+ OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
+ if (fileSize)
{
- OIC_LOG_V(DEBUG, TAG, "File Read Size: %zu", fileSize);
- uint8_t *fsData = (uint8_t *)OICCalloc(1, fileSize);
+ fsData = (uint8_t *) OICCalloc(1, fileSize);
VERIFY_NON_NULL(TAG, fsData, ERROR);
- FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "r");
+ fp = ps->open(SVR_DB_DAT_FILE_NAME, "rb");
VERIFY_NON_NULL(TAG, fp, ERROR);
- size_t itemsRead = ps->read(fsData, 1, fileSize, fp);
- if (itemsRead == fileSize)
+ if (ps->read(fsData, 1, fileSize, fp) == fileSize)
{
- VERIFY_NON_NULL(TAG, fsData, ERROR);
- if (rsrcName != NULL)
+ if (rsrcName)
{
- CborParser parser = { .end = NULL, .flags = 0 };
- CborValue cbor = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
+ CborParser parser; // will be initialized in |cbor_parser_init|
+ CborValue cbor; // will be initialized in |cbor_parser_init|
cbor_parser_init(fsData, fileSize, 0, &parser, &cbor);
- CborValue cborValue = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
- CborError cborFindResult = cbor_value_enter_container(&cbor, &cborValue);
-
- while (cbor_value_is_valid(&cborValue))
+ CborValue cborValue = {0};
+ CborError cborFindResult = cbor_value_map_find_value(&cbor, rsrcName, &cborValue);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&cborValue))
{
- char *name = NULL;
- size_t len = 0;
- cborFindResult = cbor_value_dup_text_string(&cborValue, &name, &len, NULL);
- VERIFY_SUCCESS(TAG, cborFindResult == CborNoError, ERROR);
- cborFindResult = cbor_value_advance(&cborValue);
- VERIFY_SUCCESS(TAG, cborFindResult == CborNoError, ERROR);
- if (strcmp(name, rsrcName) == 0)
- {
- cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
- VERIFY_SUCCESS(TAG, cborFindResult == CborNoError, ERROR);
- ret = OC_STACK_OK;
- OICFree(fsData);
- OICFree(name);
- goto exit;
- }
- OICFree(name);
+ cborFindResult = cbor_value_dup_byte_string(&cborValue, data, size, NULL);
+ VERIFY_SUCCESS(TAG, CborNoError==cborFindResult, ERROR);
+ ret = OC_STACK_OK;
}
+ // in case of |else (...)|, svr_data not found
}
// return everything in case rsrcName is NULL
else
{
- *data = fsData;
*size = fileSize;
+ *data = (uint8_t *) OICCalloc(1, fileSize);
+ VERIFY_NON_NULL(TAG, *data, ERROR);
+ memcpy(*data, fsData, fileSize);
+ ret = OC_STACK_OK;
}
}
}
- else
- {
- OIC_LOG (ERROR, TAG, "Unable to open SVR database to read!! ");
- }
+ OIC_LOG(DEBUG, TAG, "GetSecureVirtualDatabaseFromPS OUT");
exit:
- if (ps && fp)
+ if (fp)
{
ps->close(fp);
}
+ OICFree(fsData);
return ret;
}
-OCStackResult UpdateSecureResourceInPS(const char* rsrcName, const uint8_t* psPayload, size_t psSize)
+/**
+ * Updates the Secure Virtual Resource(s) into the Persistent Storage.
+ * This function stores cbor-payload of each resource by appending resource name,
+ * and empty payload implies deleting the value
+ *
+ * @param rsrcName - pointer of character string for the SVR name (e.g. "acl")
+ * @param psPayload - pointer of the updated Secure Virtual Resource(s)
+ * @param psSize - the updated size of Secure Virtual Resource(s)
+ *
+ * @return OCStackResult - result of updating Secure Virtual Resource(s)
+ */
+OCStackResult UpdateSecureResourceInPS(const char *rsrcName, const uint8_t *psPayload, size_t psSize)
{
- /*
- * This function stores cbor payload of each resource by appending resource name.
- */
- if (!rsrcName || !*psPayload)
+ OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS IN");
+ if (!rsrcName)
{
return OC_STACK_INVALID_PARAM;
}
- OCStackResult ret = OC_STACK_ERROR;
- size_t cborSize = 0;
+ size_t dbSize = 0;
+ size_t outSize = 0;
uint8_t *dbData = NULL;
uint8_t *outPayload = NULL;
- size_t dbSize = 0;
- ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
- if (dbData && dbSize != 0)
+ uint8_t *aclCbor = NULL;
+ uint8_t *pstatCbor = NULL;
+ uint8_t *doxmCbor = NULL;
+ uint8_t *amaclCbor = NULL;
+ uint8_t *svcCbor = NULL;
+ uint8_t *credCbor = NULL;
+ uint8_t *pconfCbor = NULL;
+
+ int64_t cborEncoderResult = CborNoError;
+ OCStackResult ret = GetSecureVirtualDatabaseFromPS(NULL, &dbData, &dbSize);
+ if (dbData && dbSize)
{
- uint8_t size = dbSize + psSize;
-
- outPayload = (uint8_t *)OICCalloc(1, size);
- VERIFY_NON_NULL(TAG, outPayload, ERROR);
-
- CborEncoder encoder = { { .ptr = NULL }, .end = NULL, .added = 0, .flags = 0};
- cbor_encoder_init(&encoder, outPayload, size, 0);
+ size_t aclCborLen = 0;
+ size_t pstatCborLen = 0;
+ size_t doxmCborLen = 0;
+ size_t amaclCborLen = 0;
+ size_t svcCborLen = 0;
+ size_t credCborLen = 0;
+ size_t pconfCborLen = 0;
+
+ // Gets each secure virtual resource from persistent storage
+ // this local scoping intended, for destroying large cbor instances after use
{
- CborEncoder map = { {.ptr = NULL }, .end = 0, .added = 0, .flags = 0};
- CborError cborEncoderResult = cbor_encoder_create_map(&encoder, &map, CborIndefiniteLength);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Creating PS Interface Map.");
+ CborParser parser; // will be initialized in |cbor_parser_init|
+ CborValue cbor; // will be initialized in |cbor_parser_init|
+ cbor_parser_init(dbData, dbSize, 0, &parser, &cbor);
+ CborValue curVal = {0};
+ CborError cborFindResult = CborNoError;
+
+ cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_ACL_NAME, &curVal);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
{
- bool found = false;
- CborValue cbor = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
- CborParser parser = { .end = NULL, .flags = 0 };
- cbor_parser_init(dbData, size, 0, &parser, &cbor);
-
- CborValue cborValue = { .parser = NULL, .ptr = NULL, .remaining = 0, .extra = 0, .type = 0, .flags = 0 };
- CborError cborFindResult = CborNoError;
-
- if (cbor_value_is_container(&cbor))
- {
- cborFindResult = cbor_value_enter_container(&cbor, &cborValue);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Entering PS Interface Map.");
- }
-
- while (cbor_value_is_valid(&cborValue))
- {
- char *name = NULL;
- size_t len = 0;
- cborFindResult = cbor_value_dup_text_string(&cborValue, &name, &len, NULL);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Duplicating Value.");
- cborFindResult = cbor_value_advance(&cborValue);
- VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Advancing Value.");
-
- cborEncoderResult = cbor_encode_text_string(&map, name, strlen(name));
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Copying Text Str Value.");
-
- if (strcmp(name, rsrcName) == 0)
- {
- cborEncoderResult = cbor_encode_byte_string(&map, psPayload, psSize);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Encoding Byte String.");
- found = true;
- }
- else
- {
- uint8_t *byteString = NULL;
- size_t byteLen = 0;
- cborFindResult = cbor_value_dup_byte_string(&cborValue, &byteString, &byteLen, NULL);
- VERIFY_SUCCESS(TAG, cborFindResult == CborNoError, ERROR);
- if (byteString)
- {
- cborEncoderResult = cbor_encode_byte_string(&map, byteString, byteLen);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding value.");
- }
- OICFree(byteString);
- }
- OICFree(name);
- cbor_value_advance(&cborValue);
- }
+ cborFindResult = cbor_value_dup_byte_string(&curVal, &aclCbor, &aclCborLen, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding ACL Name Value.");
+ }
+ cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PSTAT_NAME, &curVal);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
+ {
+ cborFindResult = cbor_value_dup_byte_string(&curVal, &pstatCbor, &pstatCborLen, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PSTAT Name Value.");
+ }
+ cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_DOXM_NAME, &curVal);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
+ {
+ cborFindResult = cbor_value_dup_byte_string(&curVal, &doxmCbor, &doxmCborLen, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding DOXM Name Value.");
+ }
+ cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_AMACL_NAME, &curVal);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
+ {
+ cborFindResult = cbor_value_dup_byte_string(&curVal, &amaclCbor, &amaclCborLen, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding AMACL Name Value.");
+ }
+ cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_SVC_NAME, &curVal);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
+ {
+ cborFindResult = cbor_value_dup_byte_string(&curVal, &svcCbor, &svcCborLen, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding SVC Name Value.");
+ }
+ cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_CRED_NAME, &curVal);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
+ {
+ cborFindResult = cbor_value_dup_byte_string(&curVal, &credCbor, &credCborLen, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding CRED Name Value.");
+ }
+ cborFindResult = cbor_value_map_find_value(&cbor, OIC_JSON_PCONF_NAME, &curVal);
+ if (CborNoError == cborFindResult && cbor_value_is_byte_string(&curVal))
+ {
+ cborFindResult = cbor_value_dup_byte_string(&curVal, &pconfCbor, &pconfCborLen, NULL);
+ VERIFY_CBOR_SUCCESS(TAG, cborFindResult, "Failed Finding PCONF Name Value.");
+ }
+ }
- // This is an exception when the value is not stored in the database.
- if (!found)
- {
- cborEncoderResult = cbor_encode_text_string(&map, rsrcName, strlen(rsrcName));
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed adding value.");
- cborEncoderResult = cbor_encode_byte_string(&map, psPayload, psSize);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed entering byte string.");
- }
+ // Updates the added |psPayload| with the existing secure virtual resource(s)
+ // this local scoping intended, for destroying large cbor instances after use
+ {
+ size_t size = aclCborLen + pstatCborLen + doxmCborLen + amaclCborLen
+ + svcCborLen + credCborLen + pconfCborLen + psSize + 255;
+ // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
+
+ outPayload = (uint8_t *) OICCalloc(1, size);
+ VERIFY_NON_NULL(TAG, outPayload, ERROR);
+ CborEncoder encoder; // will be initialized in |cbor_parser_init|
+ cbor_encoder_init(&encoder, outPayload, size, 0);
+ CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
+ cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
+
+ if (psPayload && psSize)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
}
- cborEncoderResult = cbor_encoder_close_container(&encoder, &map);
- VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed closing container.");
+ if (strcmp(OIC_JSON_ACL_NAME, rsrcName) && aclCborLen)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_ACL_NAME, strlen(OIC_JSON_ACL_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Name.");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, aclCbor, aclCborLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding ACL Value.");
+ }
+ if (strcmp(OIC_JSON_PSTAT_NAME, rsrcName) && pstatCborLen)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PSTAT_NAME, strlen(OIC_JSON_PSTAT_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Name.");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pstatCbor, pstatCborLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PSTAT Value.");
+ }
+ if (strcmp(OIC_JSON_DOXM_NAME, rsrcName) && doxmCborLen)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_DOXM_NAME, strlen(OIC_JSON_DOXM_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Name.");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, doxmCbor, doxmCborLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Doxm Value.");
+ }
+ if (strcmp(OIC_JSON_AMACL_NAME, rsrcName) && amaclCborLen)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_AMACL_NAME, strlen(OIC_JSON_AMACL_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Name.");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, amaclCbor, amaclCborLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Amacl Value.");
+ }
+ if (strcmp(OIC_JSON_SVC_NAME, rsrcName) && svcCborLen)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_SVC_NAME, strlen(OIC_JSON_SVC_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Name.");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, svcCbor, svcCborLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding SVC Value.");
+ }
+ if (strcmp(OIC_JSON_CRED_NAME, rsrcName) && credCborLen)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_CRED_NAME, strlen(OIC_JSON_CRED_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Name.");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, credCbor, credCborLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Cred Value.");
+ }
+ if (strcmp(OIC_JSON_PCONF_NAME, rsrcName) && pconfCborLen)
+ {
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, OIC_JSON_PCONF_NAME, strlen(OIC_JSON_PCONF_NAME));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Name.");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, pconfCbor, pconfCborLen);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Pconf Value.");
+ }
+
+ cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
+ outSize = encoder.ptr - outPayload;
}
- cborSize = encoder.ptr - outPayload;
}
+ else if (psPayload && psSize)
+ {
+ size_t size = psSize + 255;
+ // This added '255' is arbitrary value that is added to cover the name of the resource, map addition and ending
+ outPayload = (uint8_t *) OICCalloc(1, size);
+ VERIFY_NON_NULL(TAG, outPayload, ERROR);
+ CborEncoder encoder; // will be initialized in |cbor_parser_init|
+ cbor_encoder_init(&encoder, outPayload, size, 0);
+ CborEncoder secRsrc; // will be initialized in |cbor_encoder_create_map|
+ cborEncoderResult |= cbor_encoder_create_map(&encoder, &secRsrc, CborIndefiniteLength);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding PS Map.");
+
+ cborEncoderResult |= cbor_encode_text_string(&secRsrc, rsrcName, strlen(rsrcName));
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value Tag");
+ cborEncoderResult |= cbor_encode_byte_string(&secRsrc, psPayload, psSize);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Adding Value.");
+
+ cborEncoderResult |= cbor_encoder_close_container(&encoder, &secRsrc);
+ VERIFY_CBOR_SUCCESS(TAG, cborEncoderResult, "Failed Closing Array.");
+ outSize = encoder.ptr - outPayload;
+ }
+
+ if (outPayload && outSize)
{
+ OIC_LOG_V(DEBUG, TAG, "Writting in the file: %zu", outSize);
OCPersistentStorage* ps = SRMGetPersistentStorageHandler();
if (ps)
{
- FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "w+");
+ FILE *fp = ps->open(SVR_DB_DAT_FILE_NAME, "wb");
if (fp)
{
- size_t numberItems = ps->write(outPayload, 1, cborSize, fp);
- if (cborSize == numberItems)
+ size_t numberItems = ps->write(outPayload, 1, outSize, fp);
+ if (outSize == numberItems)
{
- OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", cborSize);
+ OIC_LOG_V(DEBUG, TAG, "Written %zu bytes into SVR database file", outSize);
ret = OC_STACK_OK;
}
else
{
OIC_LOG(ERROR, TAG, "File open failed.");
}
-
}
}
+ OIC_LOG(DEBUG, TAG, "UpdateSecureResourceInPS OUT");
exit:
- if (dbData)
- {
- OICFree(dbData);
- }
- if (outPayload)
- {
- OICFree(outPayload);
- }
-
+ OICFree(dbData);
+ OICFree(outPayload);
+ OICFree(aclCbor);
+ OICFree(pstatCbor);
+ OICFree(doxmCbor);
+ OICFree(amaclCbor);
+ OICFree(svcCbor);
+ OICFree(credCbor);
+ OICFree(pconfCbor);
return ret;
}