It adds support to add device resource type via API.
Jira Issue: IOT-921
Signed-off-by: Habib Virji <habib.virji@samsung.com>
Change-Id: Ib47f637ff343b5bc8dbce5d5f0cc2b5f1eab46ab
Reviewed-on: https://gerrit.iotivity.org/gerrit/5747
Tested-by: jenkins-iotivity <jenkins-iotivity@opendaylight.org>
#include <signal.h>
#include <ocstack.h>
#include <logger.h>
+#include "oic_string.h"
+#include "oic_malloc.h"
#define TAG "IoTivityZigbeeServer"
#define defaultComPort "/dev/ttyUSB0"
OCStackResult SetDeviceInfo()
{
- static const OCDeviceInfo deviceInfo =
+ static OCDeviceInfo deviceInfo =
{
- .deviceName = "IoTivity/Zigbee Server Sample"
+ .deviceName = "IoTivity/Zigbee Server Sample",
};
-
+ char *dup = OICStrdup("oic.wk.d");
+ deviceInfo.types = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ deviceInfo.types->value = dup;
+ OICFree(dup);
return OCSetDeviceInfo(deviceInfo);
}
processSignal(true);
}
}
-
// Device Payload
OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
- const char* specVer, const char* dmVer);
+ const OCStringLL *types, const char* specVer, const char* dmVer);
void OCDevicePayloadDestroy(OCDevicePayload* payload);
// Platform Payload
#define OC_MASK_MODS (0x0FF0)
#define OC_MASK_FAMS (OC_IP_USE_V6|OC_IP_USE_V4)
+typedef struct OCStringLL
+{
+ struct OCStringLL *next;
+ char* value;
+} OCStringLL;
+
/**
* End point identity.
*/
{
/** Pointer to the device name.*/
char *deviceName;
-
+ /** Pointer to the types.*/
+ OCStringLL *types;
} OCDeviceInfo;
#ifdef RA_ADAPTER
} OCRepPayloadValue;
-typedef struct OCStringLL
-{
- struct OCStringLL *next;
- char* value;
-} OCStringLL;
-
// used for get/set/put/observe/etc representations
typedef struct OCRepPayload
{
typedef struct
{
OCPayload base;
- char* sid;
+ char *sid;
+ OCStringLL *types;
char* deviceName;
char* specVersion;
char* dataModelVersion;
* Callback function definition of direct-pairing
*
* @param[OUT] peer - pairing device info.
- * @param[OUT} result - It's returned with 'OC_STACK_XXX'. It will return 'OC_STACK_OK'
+ * @param[OUT} result - It's returned with 'OC_STACK_XXX'. It will return 'OC_STACK_OK'
* if D2D pairing is success without error
*/
typedef void (*OCDirectPairingCB)(OCDPDev_t *peer, OCStackResult result);
OIC_LOG_V(level, PL_TAG, "\tDevice Name:%s", payload->deviceName);
OIC_LOG_V(level, PL_TAG, "\tSpec Version%s", payload->specVersion);
OIC_LOG_V(level, PL_TAG, "\tData Model Version:%s", payload->dataModelVersion);
+ if (payload->types)
+ {
+ OIC_LOG(level, PL_TAG, "\tResource Type:");
+ for (OCStringLL *strll = payload->types; strll; strll = strll->next)
+ {
+ OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
+ }
+ }
}
static inline void OCPayloadLogPlatform(LogLevel level, OCPlatformPayload* payload)
exit (EXIT_FAILURE);
}
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
+
registrationResult = OCSetDeviceInfo(deviceInfo);
if (registrationResult != OC_STACK_OK)
exit (EXIT_FAILURE);
}
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
+
registrationResult = OCSetDeviceInfo(deviceInfo);
if (registrationResult != OC_STACK_OK)
}
OCDevicePayload* OCDevicePayloadCreate(const char* sid, const char* dname,
- const char* specVer, const char* dmVer)
+ const OCStringLL *types, const char* specVer, const char* dmVer)
{
OCDevicePayload* payload = (OCDevicePayload*)OICCalloc(1, sizeof(OCDevicePayload));
goto exit;
}
+ payload->types = CloneOCStringLL((OCStringLL *)types);
+ if (types && !payload->types)
+ {
+ goto exit;
+ }
+
return payload;
exit:
OICFree(payload->deviceName);
OICFree(payload->specVersion);
OICFree(payload->dataModelVersion);
+ OCFreeOCStringLL(payload->types);
OICFree(payload);
}
err |= cbor_encoder_create_map(&encoder, &repMap, CborIndefiniteLength);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed creating device map");
+ // Resource Type
+ if (payload->types)
+ {
+ OIC_LOG(INFO, TAG, "Payload has types");
+ err |= cbor_encode_text_string(&repMap, OC_RSRVD_RESOURCE_TYPE,
+ sizeof(OC_RSRVD_RESOURCE_TYPE) - 1);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type tag");
+ char *joinedTypes = OCStringLLJoin(payload->types);
+ printf(" JOINED TYPES : %s %zd \n", joinedTypes, strlen(joinedTypes));
+ VERIFY_PARAM_NON_NULL(TAG, joinedTypes, "Failed creating joined string");
+ err |= cbor_encode_text_string(&repMap, joinedTypes, strlen(joinedTypes));
+ OICFree(joinedTypes);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding rep resource type value");
+ }
+
// Device ID
err |= AddTextStringToMap(&repMap, OC_RSRVD_DEVICE_ID, sizeof(OC_RSRVD_DEVICE_ID) - 1 , payload->sid);
VERIFY_CBOR_SUCCESS(TAG, err, "Failed adding device id");
if (cbor_value_is_map(rootValue))
{
+ CborValue curVal;
+ // Resource Type
+ err = cbor_value_map_find_value(rootValue, OC_RSRVD_RESOURCE_TYPE, &curVal);
+ if (cbor_value_is_valid(&curVal))
+ {
+ err = OCParseStringLL(rootValue, OC_RSRVD_RESOURCE_TYPE, &out->types);
+ VERIFY_CBOR_SUCCESS(TAG, err, "Failed to find rt type tag/value");
+ }
// Device ID
size_t len = 0;
- CborValue curVal;
err = cbor_value_map_find_value(rootValue, OC_RSRVD_DEVICE_ID, &curVal);
if (cbor_value_is_valid(&curVal))
{
else
{
payload = (OCPayload*) OCDevicePayloadCreate(deviceId, savedDeviceInfo.deviceName,
- OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
+ savedDeviceInfo.types, OC_SPEC_VERSION, OC_DATA_MODEL_VERSION);
if (!payload)
{
discoveryResult = OC_STACK_NO_MEMORY;
OIC_LOG(INFO, TAG, "Deleting device info.");
OICFree(savedDeviceInfo.deviceName);
+ OCFreeOCStringLL(savedDeviceInfo.types);
savedDeviceInfo.deviceName = NULL;
+
}
static OCStackResult DeepCopyDeviceInfo(OCDeviceInfo info)
return OC_STACK_NO_MEMORY;
}
+ if (info.types)
+ {
+ savedDeviceInfo.types = CloneOCStringLL(info.types);
+ if(!savedDeviceInfo.types && info.types)
+ {
+ DeleteDeviceInfo();
+ return OC_STACK_NO_MEMORY;
+ }
+ }
return OC_STACK_OK;
}
#include "OCPlatform.h"
#include "OCApi.h"
+#include "ocpayload.h"
using namespace OC;
result = SetDeviceInfo(deviceName);
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
result = OCPlatform::registerDeviceInfo(deviceInfo);
return 0;
}
-
-
-
-
rep[OC_RSRVD_DATA_MODEL_VERSION] = payload->dataModelVersion ?
std::string(payload->dataModelVersion) :
std::string();
+ for (OCStringLL *strll = payload->types; strll; strll = strll->next)
+ {
+ rep.addResourceType(strll->value);
+ }
m_reps.push_back(std::move(rep));
}
#include <OCPlatform.h>
#include <OCApi.h>
+#include <oic_malloc.h>
#include <gtest/gtest.h>
namespace OCPlatformTest
}
//Helper methods
+ void DeleteStringLL(OCStringLL* ll)
+ {
+ if (!ll)
+ {
+ return;
+ }
+
+ DeleteStringLL(ll->next);
+ delete[] ll->value;
+ OICFree(ll);
+ }
+
void DeleteDeviceInfo(OCDeviceInfo deviceInfo)
{
delete[] deviceInfo.deviceName;
+ DeleteStringLL(deviceInfo.types);
}
strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1));
}
+ bool OCResourcePayloadAddStringLL(OCStringLL **stringLL, std::string value)
+ {
+ char *dup = NULL;
+ DuplicateString(&dup, value);
+ if (!*stringLL)
+ {
+ *stringLL = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ (*stringLL)->value = dup;
+ return true;
+ }
+ else
+ {
+ OCStringLL *temp = *stringLL;
+ while(temp->next)
+ {
+ temp = temp->next;
+ }
+ temp->next = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL));
+ temp->next->value = dup;
+ return true;
+ }
+ return false;
+ }
+
OCResourceHandle RegisterResource(std::string uri, std::string type, std::string iface)
{
PlatformConfig cfg
TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithValidParameters)
{
OCDeviceInfo deviceInfo;
-
DuplicateString(&deviceInfo.deviceName, "myDeviceName");
-// TODO: FIX THIS, IT IS FAILING DUE to GetDoxmId not working.
-// EXPECT_EQ(OC_STACK_OK, OCPlatform::registerDeviceInfo(deviceInfo));
+ deviceInfo.types = NULL;
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d");
+ OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv");
+ EXPECT_EQ(OC_STACK_OK, OCPlatform::registerDeviceInfo(deviceInfo));
EXPECT_NO_THROW(DeleteDeviceInfo(deviceInfo));
}
TEST(RegisterDeviceInfoTest, RegisterDeviceInfoWithEmptyObject)
{
- OCDeviceInfo di = {0};
+ OCDeviceInfo di = {0, 0};
EXPECT_ANY_THROW(OCPlatform::registerDeviceInfo(di));
}
static const char devicename1[] = "device name";
static const char specver1[] = "spec version";
static const char dmver1[] = "data model version";
+ static OCStringLL *types = NULL;
// Device Payloads
TEST(DeviceDiscoveryEncoding, Normal)
{
+ OCResourcePayloadAddStringLL(&types, "oic.wk.d");
+ OCResourcePayloadAddStringLL(&types, "oic.d.tv");
+
OCDevicePayload* device = OCDevicePayloadCreate(
sid1,
devicename1,
+ types,
specver1,
dmver1);
EXPECT_STREQ(sid1, device->sid);
EXPECT_STREQ(specver1, device->specVersion);
EXPECT_STREQ(dmver1, device->dataModelVersion);
EXPECT_EQ(PAYLOAD_TYPE_DEVICE, ((OCPayload*)device)->type);
+ EXPECT_STREQ("oic.wk.d", device->types->value);
+ EXPECT_STREQ("oic.d.tv", device->types->next->value);
uint8_t* cborData;
size_t cborSize;
EXPECT_STREQ(device->deviceName, ((OCDevicePayload*)parsedDevice)->deviceName);
EXPECT_STREQ(device->specVersion, ((OCDevicePayload*)parsedDevice)->specVersion);
EXPECT_STREQ(device->dataModelVersion, ((OCDevicePayload*)parsedDevice)->dataModelVersion);
+ EXPECT_STREQ("oic.wk.d", ((OCDevicePayload*)parsedDevice)->types->value);
+ EXPECT_STREQ("oic.d.tv", ((OCDevicePayload*)parsedDevice)->types->next->value);
EXPECT_EQ(device->base.type, ((OCDevicePayload*)parsedDevice)->base.type);
OCPayloadDestroy((OCPayload*)device);