From 2833af27d80f0e98f1d45e0e2c905c411991d91d Mon Sep 17 00:00:00 2001 From: Dan Mihai Date: Mon, 9 Jan 2017 20:16:39 -0800 Subject: [PATCH] Deprecate OCSetDeviceInfo and registerDeviceInfo Mark OCSetDeviceInfo and registerDeviceInfo as deprecated and replace sample apps' calls to these APIs. Change-Id: I7c318010d893df94bfe2b4599281b02f44dfcf16 Signed-off-by: Dan Mihai Reviewed-on: https://gerrit.iotivity.org/gerrit/16207 Tested-by: jenkins-iotivity Reviewed-by: Mike Fenelon --- .../client/airconditioner/aircon_controlee.cpp | 69 +++++++++--- .../samples/client/thin_light/thin_room_light.cpp | 54 +++++++--- plugins/samples/linux/IotivityandZigbeeServer.c | 37 ++++--- resource/csdk/stack/include/ocstack.h | 2 + .../samples/linux/SimpleClientServer/ocserver.cpp | 116 ++++++++++----------- .../samples/tizen/SimpleClientServer/ocserver.cpp | 60 +++++------ resource/examples/devicediscoveryserver.cpp | 66 +++++++----- resource/examples/fridgeserver.cpp | 41 ++++---- resource/examples/garageserver.cpp | 43 ++++---- resource/examples/lightserver.cpp | 41 ++++---- resource/examples/mediaserver.cpp | 27 +++-- resource/examples/presenceserver.cpp | 43 ++++---- resource/examples/roomserver.cpp | 42 ++++---- resource/examples/simpleserver.cpp | 60 ++++++++--- resource/examples/simpleserverHQ.cpp | 42 ++++---- resource/include/IServerWrapper.h | 1 + resource/include/InProcServerWrapper.h | 1 + resource/include/OCPlatform.h | 2 + resource/include/OCPlatform_impl.h | 2 + 19 files changed, 433 insertions(+), 316 deletions(-) diff --git a/cloud/samples/client/airconditioner/aircon_controlee.cpp b/cloud/samples/client/airconditioner/aircon_controlee.cpp index fc9a1e9..201f89e 100644 --- a/cloud/samples/client/airconditioner/aircon_controlee.cpp +++ b/cloud/samples/client/airconditioner/aircon_controlee.cpp @@ -500,6 +500,37 @@ static FILE *client_open(const char * /*path*/, const char *mode) return fopen("./aircon_controlee.dat", mode); } +OCStackResult SetDeviceInfo() +{ + OCStackResult result = OC_STACK_ERROR; + + OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI); + + if (handle == NULL) + { + cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << endl; + return result; + } + + result = OCBindResourceTypeToResource(handle, "oic.d.airconditioner"); + + if (result != OC_STACK_OK) + { + cout << "Failed to add device type" << endl; + return result; + } + + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, "FAC_2016"); + + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } + + return OC_STACK_OK; +} + int main(int argc, char *argv[]) { if (argc != 4 && argc != 5) @@ -606,6 +637,11 @@ int main(int argc, char *argv[]) , &binarySwitch, placeholders::_1), OC_OBSERVABLE); + if (result != OC_STACK_OK) + { + exit(EXIT_FAILURE); + } + uri = temperature.getResourceUri(); rt = temperature.getResourceType()[0]; itf = temperature.getInterfaces()[0]; @@ -618,26 +654,35 @@ int main(int argc, char *argv[]) , &temperature, placeholders::_1), OC_OBSERVABLE); + if (result != OC_STACK_OK) + { + exit(EXIT_FAILURE); + } + result = airConditioner.addChildResource(&binarySwitch); - result = airConditioner.addChildResource(&temperature); + if (result != OC_STACK_OK) + { + exit(EXIT_FAILURE); + } - cout << "Publishing resources to cloud "; + result = airConditioner.addChildResource(&temperature); + if (result != OC_STACK_OK) + { + exit(EXIT_FAILURE); + } - ResourceHandles resourceHandles; + cout << "Publishing resources to cloud "; - OCDeviceInfo devInfoAirConditioner; - OCStringLL deviceType; + result = SetDeviceInfo(); - deviceType.value = "oic.d.airconditioner"; - deviceType.next = NULL; - devInfoAirConditioner.deviceName = "FAC_2016"; - devInfoAirConditioner.types = &deviceType; - devInfoAirConditioner.specVersion = NULL; - devInfoAirConditioner.dataModelVersions = NULL; + if (result != OC_STACK_OK) + { + exit(EXIT_FAILURE); + } - OCPlatform::registerDeviceInfo(devInfoAirConditioner); + ResourceHandles resourceHandles; result = RDClient::Instance().publishResourceToRD(host, OCConnectivityType::CT_ADAPTER_TCP, resourceHandles, diff --git a/cloud/samples/client/thin_light/thin_room_light.cpp b/cloud/samples/client/thin_light/thin_room_light.cpp index 17cec71..9927362 100644 --- a/cloud/samples/client/thin_light/thin_room_light.cpp +++ b/cloud/samples/client/thin_light/thin_room_light.cpp @@ -34,9 +34,19 @@ #include "ocstack.h" #include "ocpayload.h" #include "rd_client.h" +#include "OCPlatform.h" using namespace std; +#define VERIFY_SUCCESS(op) \ +{ \ + if (op != OC_STACK_OK) \ + { \ + cout << #op << " failed!!" << endl; \ + goto exit; \ + } \ +} + #define DEFAULT_CONTEXT_VALUE 0x99 #define DEFAULT_AUTH_SIGNUP "/oic/account" #define DEFAULT_AUTH_SESSION "/oic/account/session" @@ -447,6 +457,32 @@ OCStackApplicationResult handlePublishCB(void *ctx, OCDoHandle /*handle*/, return OC_STACK_KEEP_TRANSACTION; } +OCStackResult SetDeviceInfo() +{ + OCResourceHandle resourceHandle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI); + if (resourceHandle == NULL) + { + cout << "Device Resource does not exist." << endl; + goto exit; + } + + VERIFY_SUCCESS(OCBindResourceTypeToResource(resourceHandle, "oic.d.light")); + + if (OCGetServerInstanceIDString() == NULL) + { + cout << "Device ID generation failed" << endl; + goto exit; + } + + VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, "Living Room Light")); + + cout << "Device information published successfully." << endl; + return OC_STACK_OK; + +exit: + return OC_STACK_ERROR; +} + void PublishResources(string host) { cout << "Publishing resources..." << endl; @@ -463,23 +499,13 @@ void PublishResources(string host) cbData.context = (void *) DEFAULT_CONTEXT_VALUE; cbData.cd = NULL; - cout << "Publish default resources" << endl; - - OCDeviceInfo devInfoRoomLight; - OCStringLL deviceType; - - deviceType.value = "oic.d.light"; - deviceType.next = NULL; - devInfoRoomLight.deviceName = "Living Room Light"; - devInfoRoomLight.types = &deviceType; - devInfoRoomLight.specVersion = NULL; - devInfoRoomLight.dataModelVersions = NULL; + cout << "Publishing default resources" << endl; - OCStackResult res = OCSetDeviceInfo(devInfoRoomLight); + OCStackResult res = SetDeviceInfo(); if (res != OC_STACK_OK) { - cout << "Setting device info failed" << endl; + cout << "Publishing device info failed" << endl; } res = OCRDPublish(host.c_str(), CT_ADAPTER_TCP, NULL, 0, &cbData, OC_LOW_QOS); @@ -488,7 +514,7 @@ void PublishResources(string host) cout << "Unable to publish default resources to cloud" << endl; } - cout << "Publish user resources" << endl; + cout << "Publishing user resources" << endl; res = OCRDPublish(host.c_str(), CT_ADAPTER_TCP, resourceHandles, 1, &cbData, OC_LOW_QOS); if (res != OC_STACK_OK) diff --git a/plugins/samples/linux/IotivityandZigbeeServer.c b/plugins/samples/linux/IotivityandZigbeeServer.c index 8caa729..da7a793 100644 --- a/plugins/samples/linux/IotivityandZigbeeServer.c +++ b/plugins/samples/linux/IotivityandZigbeeServer.c @@ -27,6 +27,16 @@ #define TAG "IoTivityZigbeeServer" #define defaultComPort "/dev/ttyUSB0" + +#define VERIFY_SUCCESS(op) \ +{ \ + if (op != OC_STACK_OK) \ + { \ + OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); \ + goto exit; \ + } \ +} + int main() { OIC_LOG(INFO, TAG, "Initializing IoTivity..."); @@ -136,18 +146,21 @@ OCStackResult SetPlatformInfo() OCStackResult SetDeviceInfo() { - static OCDeviceInfo deviceInfo = - { - .deviceName = "IoTivity/Zigbee Server Sample", - .specVersion = "IoTivity/Zigbee Device Spec Version", - }; - char *dmv = OICStrdup("IoTivity/Zigbee Data Model Version"); - deviceInfo.dataModelVersions = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL)); - deviceInfo.dataModelVersions->value = dmv; - char *dup = OICStrdup("oic.wk.d"); - deviceInfo.types = (OCStringLL *)OICCalloc(1, sizeof(OCStringLL)); - deviceInfo.types->value = dup; - return OCSetDeviceInfo(deviceInfo); + VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + "IoTivity/Zigbee Server Sample")); + + VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, + "IoTivity/Zigbee Device Spec Version")); + + VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + "IoTivity/Zigbee Data Model Version")); + + OIC_LOG(INFO, TAG, "Device information initialized successfully."); + return OC_STACK_OK; + +exit: + return OC_STACK_ERROR; + } bool processSignal(bool set) diff --git a/resource/csdk/stack/include/ocstack.h b/resource/csdk/stack/include/ocstack.h index 159684f..92d9e0a 100644 --- a/resource/csdk/stack/include/ocstack.h +++ b/resource/csdk/stack/include/ocstack.h @@ -238,6 +238,8 @@ OCStackResult OCSetDefaultDeviceEntityHandler(OCDeviceEntityHandler entityHandle * list. The default Device Type is mandatory and always specified by this Device as the first * Device Type. * + * @deprecated Use OCSetPropertyValue instead. + * * @param deviceInfo Structure passed by the server application containing the device * information. * diff --git a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp index f1b1abe..0e53bdc 100644 --- a/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp +++ b/resource/csdk/stack/samples/linux/SimpleClientServer/ocserver.cpp @@ -44,7 +44,16 @@ #include "common.h" #include "oic_string.h" -//string length of "/a/light/" + std::numeric_limits::digits10 + '\0'" +#define VERIFY_SUCCESS(op) \ +{ \ + if (op != OC_STACK_OK) \ + { \ + OIC_LOG_V(FATAL, TAG, "%s failed!!", #op); \ + goto exit; \ + } \ +} + +// string length of "/a/light/" + std::numeric_limits::digits10 + '\0'" // 9 + 9 + 1 = 19 const int URI_MAXSIZE = 19; @@ -69,10 +78,10 @@ pthread_t threadId_presence; static bool observeThreadStarted = false; #ifdef WITH_PRESENCE -#define numPresenceResources (2) +#define NUM_PRESENCE_RESOURCES 2 #endif -char *gResourceUri= (char *)"/a/light"; +char *gResourceUri = (char *)"/a/light"; const char *dateOfManufacture = "2016-01-15"; const char *deviceName = "myDeviceName"; const char *deviceUUID = "51b55ddc-ccbb-4cb3-a57f-494eeca13a21"; @@ -89,14 +98,9 @@ const char *version = "myVersion"; const char *systemTime = "2015-05-15T11.04"; const char *specVersion = "core.1.1.0"; const char *dataModelVersions = "res.1.1.0,sh.1.1.0"; - -// Entity handler should check for resourceTypeName and ResourceInterface in order to GET -// the existence of a known resource -const char *resourceTypeName = "core.light"; -const char *resourceInterface = OC_RSRVD_INTERFACE_DEFAULT; +const char *deviceType = "oic.d.tv"; OCPlatformInfo platformInfo; -OCDeviceInfo deviceInfo; OCRepPayload* getPayload(const char* uri, int64_t power, bool state) { @@ -178,8 +182,8 @@ bool checkIfQueryForPowerPassed(char * query) int powerRequested = atoi(pointerToOperator + 1); if (Light.power > powerRequested) { - OIC_LOG_V(INFO, TAG, "Current power: %d. Requested: <%d", Light.power - , powerRequested); + OIC_LOG_V(INFO, TAG, "Current power: %d. Requested: <%d", Light.power, + powerRequested); return false; } } @@ -434,7 +438,9 @@ void ProcessObserveDeregister (OCEntityHandlerRequest *ehRequest) } } if (clientStillObserving == false) + { gLightUnderObservation = 0; + } } OCEntityHandlerResult @@ -763,17 +769,17 @@ void *presenceNotificationGenerator(void *param) OIC_LOG_V(INFO, TAG, "Will send out presence in %u seconds", secondsBeforePresence); sleep(secondsBeforePresence); (void)param; - OCDoHandle presenceNotificationHandles[numPresenceResources]; + OCDoHandle presenceNotificationHandles[NUM_PRESENCE_RESOURCES]; OCStackResult res = OC_STACK_OK; - std::array presenceNotificationResources { { + std::array presenceNotificationResources { { std::string("core.fan"), std::string("core.led") } }; - std::array presenceNotificationUris { { + std::array presenceNotificationUris { { std::string("/a/fan"), std::string("/a/led") } }; - for(int i=0; i::digits10 + '\0'" // 9 + 9 + 1 = 19 const int URI_MAXSIZE = 19; @@ -79,6 +88,7 @@ const char *version = "myVersion"; const char *systemTime = "2015-05-15T11.04"; const char *specVersion = "core.1.1.0"; const char *dataModelVersions = "res.1.1.0,sh.1.1.0"; +const char *deviceType = "oic.d.tv"; // Entity handler should check for resourceTypeName and ResourceInterface in order to GET // the existence of a known resource @@ -86,7 +96,6 @@ const char *resourceTypeName = "core.light"; const char *resourceInterface = OC_RSRVD_INTERFACE_DEFAULT; OCPlatformInfo platformInfo; -OCDeviceInfo deviceInfo; OCRepPayload* getPayload(const char* uri, int64_t power, bool state) { @@ -808,13 +817,6 @@ void DeletePlatformInfo() free (platformInfo.systemTime); } -void DeleteDeviceInfo() -{ - free (deviceInfo.deviceName); - free (deviceInfo.specVersion); - OCFreeOCStringLL (deviceInfo.dataModelVersions); -} - bool DuplicateString(char** targetString, const char* sourceString) { if(!sourceString) @@ -916,23 +918,26 @@ OCStackResult SetPlatformInfo(const char* platformID, const char *manufacturerNa return OC_STACK_ERROR; } -OCStackResult SetDeviceInfo(const char* deviceName, const char* specVersion, const char* dataModelVersions) +OCStackResult SetDeviceInfo() { - if(!DuplicateString(&deviceInfo.deviceName, deviceName)) + OCResourceHandle resourceHandle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI); + if (resourceHandle == NULL) { - return OC_STACK_ERROR; - } - if(!DuplicateString(&deviceInfo.specVersion, specVersion)) - { - return OC_STACK_ERROR; - } - OCFreeOCStringLL(deviceInfo.dataModelVersions); - deviceInfo.dataModelVersions = OCCreateOCStringLL(dataModelVersions); - if (!deviceInfo.dataModelVersions) - { - return OC_STACK_ERROR; + OIC_LOG(ERROR, TAG, "Device Resource does not exist."); + goto exit; } + + VERIFY_SUCCESS(OCBindResourceTypeToResource(resourceHandle, deviceType)); + VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName)); + VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion)); + VERIFY_SUCCESS(OCSetPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions)); + + OIC_LOG(INFO, TAG, "Device information initialized successfully."); return OC_STACK_OK; + +exit: + return OC_STACK_ERROR; } static void PrintUsage() @@ -1033,17 +1038,7 @@ int main(int argc, char* argv[]) exit (EXIT_FAILURE); } - registrationResult = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - - if (registrationResult != OC_STACK_OK) - { - cout << "\nDevice info setting failed locally!"; - exit (EXIT_FAILURE); - } - - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv"); - - registrationResult = OCSetDeviceInfo(deviceInfo); + registrationResult = SetDeviceInfo(); if (registrationResult != OC_STACK_OK) { @@ -1080,7 +1075,6 @@ int main(int argc, char* argv[]) cout << "\nEntering ocserver main loop..."; DeletePlatformInfo(); - DeleteDeviceInfo(); signal(SIGINT, handleSigInt); diff --git a/resource/examples/devicediscoveryserver.cpp b/resource/examples/devicediscoveryserver.cpp index 1c3404a..7bb24a2 100644 --- a/resource/examples/devicediscoveryserver.cpp +++ b/resource/examples/devicediscoveryserver.cpp @@ -49,14 +49,14 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "Bill's Battlestar"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0,sh.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0", "sh.1.1.0"}; + +// Device type +std::string deviceType = "oic.d.tv"; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - void DeletePlatformInfo() { delete[] platformInfo.platformID; @@ -72,14 +72,6 @@ void DeletePlatformInfo() delete[] platformInfo.systemTime; } - -void DeleteDeviceInfo() -{ - delete[] deviceInfo.deviceName; - delete[] deviceInfo.specVersion; - OCFreeOCStringLL(deviceInfo.dataModelVersions); -} - void DuplicateString(char ** targetString, std::string sourceString) { *targetString = new char[sourceString.length() + 1]; @@ -106,18 +98,44 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } -OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +OCStackResult SetDeviceInfo() { - DuplicateString(&deviceInfo.deviceName, deviceName); + OCStackResult result = OC_STACK_ERROR; + + OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI); + if (handle == NULL) + { + std::cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << std::endl; + return result; + } - if (!specVersion.empty()) + result = OCBindResourceTypeToResource(handle, deviceType.c_str()); + if (result != OC_STACK_OK) { - DuplicateString(&deviceInfo.specVersion, specVersion); + std::cout << "Failed to add device type" << std::endl; + return result; } - if (!dataModelVersions.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName); + if (result != OC_STACK_OK) { - OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + std::cout << "Failed to set device name" << std::endl; + return result; + } + + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) + { + std::cout << "Failed to set data model versions" << std::endl; + return result; + } + + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + if (result != OC_STACK_OK) + { + std::cout << "Failed to set spec version" << std::endl; + return result; } return OC_STACK_OK; @@ -144,27 +162,21 @@ int main() result = OCPlatform::registerPlatformInfo(platformInfo); - if(result != OC_STACK_OK) + if (result != OC_STACK_OK) { std::cout << "Platform Registration failed\n"; return -1; } + result = SetDeviceInfo(); - result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.d.tv"); - - result = OCPlatform::registerDeviceInfo(deviceInfo); - - if(result != OC_STACK_OK) + if (result != OC_STACK_OK) { std::cout << "Device Registration failed\n"; return -1; } DeletePlatformInfo(); - DeleteDeviceInfo(); // A condition variable will free the mutex it is given, then do a non- // intensive block until 'notify' is called on it. In this case, since we diff --git a/resource/examples/fridgeserver.cpp b/resource/examples/fridgeserver.cpp index 05ed1d8..d1622b0 100644 --- a/resource/examples/fridgeserver.cpp +++ b/resource/examples/fridgeserver.cpp @@ -62,14 +62,11 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "IoTivity Fridge Server"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0"}; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - class Resource { protected: @@ -504,13 +501,6 @@ void DeletePlatformInfo() delete[] platformInfo.systemTime; } -void DeleteDeviceInfo() -{ - delete[] deviceInfo.deviceName; - delete[] deviceInfo.specVersion; - OCFreeOCStringLL(deviceInfo.dataModelVersions); -} - void DuplicateString(char ** targetString, std::string sourceString) { *targetString = new char[sourceString.length() + 1]; @@ -537,18 +527,29 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } -OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +OCStackResult SetDeviceInfo() { - DuplicateString(&deviceInfo.deviceName, deviceName); + OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + deviceName); + if (result != OC_STACK_OK) + { + std::cout << "Failed to set device name" << std::endl; + return result; + } - if (!specVersion.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) { - DuplicateString(&deviceInfo.specVersion, specVersion); + std::cout << "Failed to set data model versions" << std::endl; + return result; } - if (!dataModelVersions.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + if (result != OC_STACK_OK) { - OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + std::cout << "Failed to set spec version" << std::endl; + return result; } return OC_STACK_OK; @@ -582,10 +583,7 @@ int main () return -1; } - result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); - - result = OCPlatform::registerDeviceInfo(deviceInfo); + result = SetDeviceInfo(); if (result != OC_STACK_OK) { @@ -594,7 +592,6 @@ int main () } DeletePlatformInfo(); - DeleteDeviceInfo(); // we will keep the server alive for at most 30 minutes std::this_thread::sleep_for(std::chrono::minutes(30)); return 0; diff --git a/resource/examples/garageserver.cpp b/resource/examples/garageserver.cpp index f176b7f..ab4072d 100644 --- a/resource/examples/garageserver.cpp +++ b/resource/examples/garageserver.cpp @@ -51,14 +51,11 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "IoTivity Garage Server"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0"}; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - // Forward declaring the entityHandler OCEntityHandlerResult entityHandler(std::shared_ptr request); @@ -280,13 +277,6 @@ void DeletePlatformInfo() delete[] platformInfo.systemTime; } -void DeleteDeviceInfo() -{ - delete[] deviceInfo.deviceName; - delete[] deviceInfo.specVersion; - OCFreeOCStringLL(deviceInfo.dataModelVersions); -} - void DuplicateString(char ** targetString, std::string sourceString) { *targetString = new char[sourceString.length() + 1]; @@ -313,18 +303,29 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } -OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +OCStackResult SetDeviceInfo() { - DuplicateString(&deviceInfo.deviceName, deviceName); + OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + deviceName); + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } - if (!specVersion.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) { - DuplicateString(&deviceInfo.specVersion, specVersion); + cout << "Failed to set data model versions" << endl; + return result; } - if (!dataModelVersions.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + if (result != OC_STACK_OK) { - OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + cout << "Failed to set spec version" << endl; + return result; } return OC_STACK_OK; @@ -356,23 +357,19 @@ int main(int /*argc*/, char** /*argv[1]*/) return -1; } - result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); - - result = OCPlatform::registerDeviceInfo(deviceInfo); - + result = SetDeviceInfo(); if (result != OC_STACK_OK) { std::cout << "Device Registration failed\n"; return -1; } + try { // Invoke createResource function of class light. myGarage.createResource(); DeletePlatformInfo(); - DeleteDeviceInfo(); // A condition variable will free the mutex it is given, then do a non- // intensive block until 'notify' is called on it. In this case, since we // don't ever call cv.notify, this should be a non-processor intensive version diff --git a/resource/examples/lightserver.cpp b/resource/examples/lightserver.cpp index 1e9a2c1..55188e9 100644 --- a/resource/examples/lightserver.cpp +++ b/resource/examples/lightserver.cpp @@ -62,14 +62,11 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "IoTivity Light Server"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0"}; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - // Specifies secure or non-secure // false: non-secure resource // true: secure resource @@ -326,13 +323,6 @@ void DeletePlatformInfo() delete[] platformInfo.systemTime; } -void DeleteDeviceInfo() -{ - delete[] deviceInfo.deviceName; - delete[] deviceInfo.specVersion; - OCFreeOCStringLL(deviceInfo.dataModelVersions); -} - void DuplicateString(char ** targetString, std::string sourceString) { *targetString = new char[sourceString.length() + 1]; @@ -360,18 +350,29 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } -OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +OCStackResult SetDeviceInfo() { - DuplicateString(&deviceInfo.deviceName, deviceName); + OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + deviceName); + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } - if (!specVersion.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) { - DuplicateString(&deviceInfo.specVersion, specVersion); + cout << "Failed to set data model versions" << endl; + return result; } - if (!dataModelVersions.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + if (result != OC_STACK_OK) { - OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + cout << "Failed to set spec version" << endl; + return result; } return OC_STACK_OK; @@ -403,10 +404,7 @@ int main(int /*argc*/, char** /*argv[]*/) return -1; } - result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); - - result = OCPlatform::registerDeviceInfo(deviceInfo); + result = SetDeviceInfo(); if (result != OC_STACK_OK) { @@ -427,7 +425,6 @@ int main(int /*argc*/, char** /*argv[]*/) myLight.addInterface(std::string(LINK_INTERFACE)); DeletePlatformInfo(); - DeleteDeviceInfo(); // A condition variable will free the mutex it is given, then do a non- // intensive block until 'notify' is called on it. In this case, since we diff --git a/resource/examples/mediaserver.cpp b/resource/examples/mediaserver.cpp index 849a42c..18f353c 100644 --- a/resource/examples/mediaserver.cpp +++ b/resource/examples/mediaserver.cpp @@ -113,12 +113,7 @@ public: std::string resourceInterface = DEFAULT_INTERFACE; /* Device Information */ - char* deviceName = "IoTivity Media Server"; - char* specVersion = "core.1.1.0"; - OCStringLL types{ nullptr, const_cast(resourceTypeName.c_str()) }; - OCDeviceInfo deviceInfo{ deviceName, &types, specVersion, nullptr }; - - result = OCPlatform::registerDeviceInfo(deviceInfo); + result = SetDeviceInfo(); if (OC_STACK_OK != result) { cout << "Device information registration was unsuccessful\n"; @@ -409,6 +404,26 @@ OCEntityHandlerResult entityHandler(std::shared_ptr request) return ehResult; } +OCStackResult SetDeviceInfo() +{ + OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + "IoTivity Media Server"); + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } + + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, "core.1.1.0"); + if (result != OC_STACK_OK) + { + cout << "Failed to set spec version" << endl; + return result; + } + + return OC_STACK_OK; +} + }; // ChangeMediaRepresentaion is an observation function, diff --git a/resource/examples/presenceserver.cpp b/resource/examples/presenceserver.cpp index c8c7f9c..2ed2359 100644 --- a/resource/examples/presenceserver.cpp +++ b/resource/examples/presenceserver.cpp @@ -62,14 +62,11 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "IoTivity Presence Server"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0"}; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - // Forward declaring the entityHandler OCEntityHandlerResult entityHandler(std::shared_ptr request); @@ -237,13 +234,6 @@ void DeletePlatformInfo() delete[] platformInfo.systemTime; } -void DeleteDeviceInfo() -{ - delete[] deviceInfo.deviceName; - delete[] deviceInfo.specVersion; - OCFreeOCStringLL(deviceInfo.dataModelVersions); -} - void DuplicateString(char ** targetString, std::string sourceString) { *targetString = new char[sourceString.length() + 1]; @@ -271,18 +261,29 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } -OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +OCStackResult SetDeviceInfo() { - DuplicateString(&deviceInfo.deviceName, deviceName); + OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + deviceName); + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } - if (!specVersion.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) { - DuplicateString(&deviceInfo.specVersion, specVersion); + cout << "Failed to set data model versions" << endl; + return result; } - if (!dataModelVersions.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + if (result != OC_STACK_OK) { - OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + cout << "Failed to set spec version" << endl; + return result; } return OC_STACK_OK; @@ -314,16 +315,13 @@ int main() return -1; } - result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); - - result = OCPlatform::registerDeviceInfo(deviceInfo); - + result = SetDeviceInfo(); if (result != OC_STACK_OK) { std::cout << "Device Registration failed\n"; return -1; } + try { using namespace OC::OCPlatform; @@ -363,7 +361,6 @@ int main() createPresenceResources(); DeletePlatformInfo(); - DeleteDeviceInfo(); // A condition variable will free the mutex it is given, then do a non- // intensive block until 'notify' is called on it. In this case, since we diff --git a/resource/examples/roomserver.cpp b/resource/examples/roomserver.cpp index a3091cc..07e8f51 100644 --- a/resource/examples/roomserver.cpp +++ b/resource/examples/roomserver.cpp @@ -59,14 +59,11 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "IoTivity Room Server"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0"}; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - class RoomResource { public: @@ -568,13 +565,6 @@ void DeletePlatformInfo() delete[] platformInfo.systemTime; } -void DeleteDeviceInfo() -{ - delete[] deviceInfo.deviceName; - delete[] deviceInfo.specVersion; - OCFreeOCStringLL(deviceInfo.dataModelVersions); -} - void DuplicateString(char ** targetString, std::string sourceString) { *targetString = new char[sourceString.length() + 1]; @@ -602,18 +592,30 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } -OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +OCStackResult SetDeviceInfo() { - DuplicateString(&deviceInfo.deviceName, deviceName); + OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + deviceName); + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } - if (!specVersion.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) { - DuplicateString(&deviceInfo.specVersion, specVersion); + cout << "Failed to set data model versions" << endl; + return result; } - if (!dataModelVersions.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + + if (result != OC_STACK_OK) { - OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + cout << "Failed to set spec version" << endl; + return result; } return OC_STACK_OK; @@ -665,10 +667,7 @@ int main(int argc, char* argv[]) return -1; } - result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); - - result = OCPlatform::registerDeviceInfo(deviceInfo); + result = SetDeviceInfo(); if (result != OC_STACK_OK) { @@ -682,7 +681,6 @@ int main(int argc, char* argv[]) myRoomResource.createResources(); DeletePlatformInfo(); - DeleteDeviceInfo(); // A condition variable will free the mutex it is given, then do a non- // intensive block until 'notify' is called on it. In this case, since we // don't ever call cv.notify, this should be a non-processor intensive version diff --git a/resource/examples/simpleserver.cpp b/resource/examples/simpleserver.cpp index 24aea45..04c7649 100644 --- a/resource/examples/simpleserver.cpp +++ b/resource/examples/simpleserver.cpp @@ -66,15 +66,13 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "IoTivity Simple Server"; +std::string deviceType = "oic.wk.tv"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0,sh.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0", "sh.1.1.0"}; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - // Specifies where to notify all observers or list of observers // false: notifies all observers // true: notifies list of observers @@ -535,6 +533,49 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } +OCStackResult SetDeviceInfo() +{ + OCStackResult result = OC_STACK_ERROR; + + OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI); + if (handle == NULL) + { + cout << "Failed to find resource " << OC_RSRVD_DEVICE_URI << endl; + return result; + } + + result = OCBindResourceTypeToResource(handle, deviceType.c_str()); + if (result != OC_STACK_OK) + { + cout << "Failed to add device type" << endl; + return result; + } + + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName); + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } + + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) + { + cout << "Failed to set data model versions" << endl; + return result; + } + + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + if (result != OC_STACK_OK) + { + cout << "Failed to set spec version" << endl; + return result; + } + + return OC_STACK_OK; +} + void * handleSlowResponse (void *param, std::shared_ptr pRequest) { // This function handles slow response case @@ -636,16 +677,7 @@ int main(int argc, char* argv[]) return -1; } - result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, deviceName); - result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); - result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, - dataModelVersions); - OCResourceHandle handle = OCGetResourceHandleAtUri(OC_RSRVD_DEVICE_URI); - if (handle) - { - OCBindResourceTypeToResource(handle, "oic.wk.tv"); - } - + result = SetDeviceInfo(); if (result != OC_STACK_OK) { std::cout << "Device Registration failed\n"; diff --git a/resource/examples/simpleserverHQ.cpp b/resource/examples/simpleserverHQ.cpp index ecfc2b4..e4db483 100644 --- a/resource/examples/simpleserverHQ.cpp +++ b/resource/examples/simpleserverHQ.cpp @@ -64,14 +64,11 @@ std::string systemTime = "2016-01-15T11.01"; // Set of strings for each of device info fields std::string deviceName = "IoTivity Simple Server HQ"; std::string specVersion = "core.1.1.0"; -std::string dataModelVersions = "res.1.1.0"; +std::vector dataModelVersions = {"res.1.1.0"}; // OCPlatformInfo Contains all the platform info to be stored OCPlatformInfo platformInfo; -// OCDeviceInfo Contains all the device info to be stored -OCDeviceInfo deviceInfo; - // Specifies where to notify all observers or list of observers // 0 - notifies all observers // 1 - notifies list of observers @@ -451,13 +448,6 @@ void DeletePlatformInfo() delete[] platformInfo.systemTime; } -void DeleteDeviceInfo() -{ - delete[] deviceInfo.deviceName; - delete[] deviceInfo.specVersion; - OCFreeOCStringLL(deviceInfo.dataModelVersions); -} - void DuplicateString(char ** targetString, std::string sourceString) { *targetString = new char[sourceString.length() + 1]; @@ -485,18 +475,29 @@ OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerNa return OC_STACK_OK; } -OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +OCStackResult SetDeviceInfo() { - DuplicateString(&deviceInfo.deviceName, deviceName); + OCStackResult result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DEVICE_NAME, + deviceName); + if (result != OC_STACK_OK) + { + cout << "Failed to set device name" << endl; + return result; + } - if (!specVersion.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_DATA_MODEL_VERSION, + dataModelVersions); + if (result != OC_STACK_OK) { - DuplicateString(&deviceInfo.specVersion, specVersion); + cout << "Failed to set data model versions" << endl; + return result; } - if (!dataModelVersions.empty()) + result = OCPlatform::setPropertyValue(PAYLOAD_TYPE_DEVICE, OC_RSRVD_SPEC_VERSION, specVersion); + if (result != OC_STACK_OK) { - OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + cout << "Failed to set spec version" << endl; + return result; } return OC_STACK_OK; @@ -556,11 +557,7 @@ int main(int argc, char* argv[]) return -1; } - result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); - OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); - - result = OCPlatform::registerDeviceInfo(deviceInfo); - + result = SetDeviceInfo(); if (result != OC_STACK_OK) { std::cout << "Device Registration failed\n"; @@ -580,7 +577,6 @@ int main(int argc, char* argv[]) myLight.addInterface(std::string(LINK_INTERFACE)); DeletePlatformInfo(); - DeleteDeviceInfo(); // A condition variable will free the mutex it is given, then do a non- // intensive block until 'notify' is called on it. In this case, since we diff --git a/resource/include/IServerWrapper.h b/resource/include/IServerWrapper.h index 8aee213..1025a15 100644 --- a/resource/include/IServerWrapper.h +++ b/resource/include/IServerWrapper.h @@ -51,6 +51,7 @@ namespace OC EntityHandler& entityHandler, uint8_t resourceProperty) = 0; + // @deprecated: Use setPropertyValue instead. virtual OCStackResult registerDeviceInfo( const OCDeviceInfo deviceInfo) = 0; diff --git a/resource/include/InProcServerWrapper.h b/resource/include/InProcServerWrapper.h index 98587d3..9762818 100644 --- a/resource/include/InProcServerWrapper.h +++ b/resource/include/InProcServerWrapper.h @@ -44,6 +44,7 @@ namespace OC EntityHandler& entityHandler, uint8_t resourceProperty); + // @deprecated: Use setPropertyValue instead. virtual OCStackResult registerDeviceInfo( const OCDeviceInfo deviceInfo); diff --git a/resource/include/OCPlatform.h b/resource/include/OCPlatform.h index 1242036..f934227 100644 --- a/resource/include/OCPlatform.h +++ b/resource/include/OCPlatform.h @@ -328,6 +328,8 @@ namespace OC /** * Register Device Info * + * @deprecated: Use setPropertyValue instead. + * * @param deviceInfo structure containing all the device specific information * @return Returns ::OC_STACK_OK if no errors and ::OC_STACK_ERROR in case of stack process error */ diff --git a/resource/include/OCPlatform_impl.h b/resource/include/OCPlatform_impl.h index 6e0d9db..4923de0 100644 --- a/resource/include/OCPlatform_impl.h +++ b/resource/include/OCPlatform_impl.h @@ -195,6 +195,8 @@ namespace OC /** * This API registers all the device specific information * + * @deprecated: Use setPropertyValue instead. + * * @param deviceInfo Structure containing all the device related information * * @return Returns ::OC_STACK_OK if success -- 2.7.4