X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=resource%2Fexamples%2Fsimpleserver.cpp;h=e610629c341ea7030cb0c44f1f125f47a07ade62;hb=8229635f6d207516ccbbdf23b13be164e0fc1787;hp=e98be52e6d1539d90e7139640298addde99385b9;hpb=aea100b2578a777b34dc03fdc3b6451d1d60cfc5;p=platform%2Fupstream%2Fiotivity.git diff --git a/resource/examples/simpleserver.cpp b/resource/examples/simpleserver.cpp index e98be52..e610629 100644 --- a/resource/examples/simpleserver.cpp +++ b/resource/examples/simpleserver.cpp @@ -22,24 +22,59 @@ /// This sample provides steps to define an interface for a resource /// (properties and methods) and host this resource on the server. /// +#include "iotivity_config.h" #include - +#ifdef HAVE_UNISTD_H +#include +#endif +#ifdef HAVE_PTHREAD_H #include +#endif #include #include #include "OCPlatform.h" #include "OCApi.h" +#ifdef HAVE_WINDOWS_H +#include +#endif + +#include "ocpayload.h" using namespace OC; using namespace std; namespace PH = std::placeholders; +static const char* SVR_DB_FILE_NAME = "./oic_svr_db_server.dat"; int gObservation = 0; void * ChangeLightRepresentation (void *param); void * handleSlowResponse (void *param, std::shared_ptr pRequest); +// Set of strings for each of platform Info fields +std::string platformId = "0A3E0D6F-DBF5-404E-8719-D6880042463A"; +std::string manufacturerName = "OCF"; +std::string manufacturerLink = "https://www.iotivity.org"; +std::string modelNumber = "myModelNumber"; +std::string dateOfManufacture = "2016-01-15"; +std::string platformVersion = "myPlatformVersion"; +std::string operatingSystemVersion = "myOS"; +std::string hardwareVersion = "myHardwareVersion"; +std::string firmwareVersion = "1.0"; +std::string supportLink = "https://www.iotivity.org"; +std::string systemTime = "2016-01-15T11.01"; + +// Set of strings for each of device info fields +std::string deviceName = "IoTivity Simple Server"; +std::string specVersion = "core.1.1.0"; +std::string 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 @@ -240,9 +275,9 @@ public: } } - void addInterface(const std::string& interface) const + void addInterface(const std::string& iface) const { - OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, interface); + OCStackResult result = OCPlatform::bindInterfaceToResource(m_resourceHandle, iface); if (OC_STACK_OK != result) { cout << "Binding TypeName to Resource was unsuccessful\n"; @@ -371,7 +406,12 @@ OCEntityHandlerResult entityHandler(std::shared_ptr request) m_interestedObservers.end()); } +#if defined(_WIN32) + DWORD threadId = 0; + HANDLE threadHandle = INVALID_HANDLE_VALUE; +#else pthread_t threadId; +#endif cout << "\t\trequestFlag : Observer\n"; gObservation = 1; @@ -381,7 +421,11 @@ OCEntityHandlerResult entityHandler(std::shared_ptr request) // If we have not created the thread already, we will create one here. if(!startedThread) { +#if defined(_WIN32) + threadHandle = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)ChangeLightRepresentation, (void*)this, 0, &threadId); +#else pthread_create (&threadId, NULL, ChangeLightRepresentation, (void *)this); +#endif startedThread = 1; } ehResult = OC_EH_OK; @@ -450,6 +494,72 @@ void * ChangeLightRepresentation (void *param) return NULL; } +void DeletePlatformInfo() +{ + delete[] platformInfo.platformID; + delete[] platformInfo.manufacturerName; + delete[] platformInfo.manufacturerUrl; + delete[] platformInfo.modelNumber; + delete[] platformInfo.dateOfManufacture; + delete[] platformInfo.platformVersion; + delete[] platformInfo.operatingSystemVersion; + delete[] platformInfo.hardwareVersion; + delete[] platformInfo.firmwareVersion; + delete[] platformInfo.supportUrl; + 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]; + strncpy(*targetString, sourceString.c_str(), (sourceString.length() + 1)); +} + +OCStackResult SetPlatformInfo(std::string platformID, std::string manufacturerName, + std::string manufacturerUrl, std::string modelNumber, std::string dateOfManufacture, + std::string platformVersion, std::string operatingSystemVersion, + std::string hardwareVersion, std::string firmwareVersion, std::string supportUrl, + std::string systemTime) +{ + DuplicateString(&platformInfo.platformID, platformID); + DuplicateString(&platformInfo.manufacturerName, manufacturerName); + DuplicateString(&platformInfo.manufacturerUrl, manufacturerUrl); + DuplicateString(&platformInfo.modelNumber, modelNumber); + DuplicateString(&platformInfo.dateOfManufacture, dateOfManufacture); + DuplicateString(&platformInfo.platformVersion, platformVersion); + DuplicateString(&platformInfo.operatingSystemVersion, operatingSystemVersion); + DuplicateString(&platformInfo.hardwareVersion, hardwareVersion); + DuplicateString(&platformInfo.firmwareVersion, firmwareVersion); + DuplicateString(&platformInfo.supportUrl, supportUrl); + DuplicateString(&platformInfo.systemTime, systemTime); + + return OC_STACK_OK; +} + +OCStackResult SetDeviceInfo(std::string deviceName, std::string specVersion, std::string dataModelVersions) +{ + DuplicateString(&deviceInfo.deviceName, deviceName); + + if (!specVersion.empty()) + { + DuplicateString(&deviceInfo.specVersion, specVersion); + } + + if (!dataModelVersions.empty()) + { + OCResourcePayloadAddStringLL(&deviceInfo.dataModelVersions, dataModelVersions.c_str()); + } + + return OC_STACK_OK; +} + void * handleSlowResponse (void *param, std::shared_ptr pRequest) { // This function handles slow response case @@ -484,13 +594,13 @@ void PrintUsage() static FILE* client_open(const char* /*path*/, const char *mode) { - return fopen("./oic_svr_db_server.json", mode); + return fopen(SVR_DB_FILE_NAME, mode); } int main(int argc, char* argv[]) { PrintUsage(); - OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink }; + OCPersistentStorage ps {client_open, fread, fwrite, fclose, unlink, NULL, NULL}; if (argc == 1) { @@ -530,13 +640,37 @@ int main(int argc, char* argv[]) PlatformConfig cfg { OC::ServiceType::InProc, OC::ModeType::Server, - "0.0.0.0", // By setting to "0.0.0.0", it binds to all available interfaces - 0, // Uses randomly available port + (OCTransportAdapter)(OCTransportAdapter::OC_ADAPTER_IP|OCTransportAdapter::OC_ADAPTER_TCP), OC::QualityOfService::LowQos, &ps }; OCPlatform::Configure(cfg); + std::cout << "Starting server & setting platform info\n"; + + OCStackResult result = SetPlatformInfo(platformId, manufacturerName, manufacturerLink, + modelNumber, dateOfManufacture, platformVersion, operatingSystemVersion, + hardwareVersion, firmwareVersion, supportLink, systemTime); + + result = OCPlatform::registerPlatformInfo(platformInfo); + + if (result != OC_STACK_OK) + { + std::cout << "Platform Registration failed\n"; + return -1; + } + + result = SetDeviceInfo(deviceName, specVersion, dataModelVersions); + OCResourcePayloadAddStringLL(&deviceInfo.types, "oic.wk.d"); + + result = OCPlatform::registerDeviceInfo(deviceInfo); + + if (result != OC_STACK_OK) + { + std::cout << "Device Registration failed\n"; + return -1; + } + try { // Create the instance of the resource class @@ -551,6 +685,8 @@ int main(int argc, char* argv[]) myLight.addInterface(std::string(LINK_INTERFACE)); std::cout << "Added Interface and Type" << std::endl; + 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