int g_Steps = 0;
int isWaiting = 0; //0: none to wait, 1: wait for the response of "getConfigurationValue"
-pthread_mutex_t mutex_lock;
+pthread_mutex_t mutex_lock = PTHREAD_MUTEX_INITIALIZER;
const int SUCCESS_RESPONSE = 0;
g_setCollection = resource;
else
{
+ pthread_mutex_lock(&mutex_lock);
isWaiting = 0;
+ pthread_mutex_unlock(&mutex_lock);
return;
}
}
std::cout << "Exception: " << e.what() << std::endl;
}
+ pthread_mutex_lock(&mutex_lock);
isWaiting = 0;
+ pthread_mutex_unlock(&mutex_lock);
}
int main(int argc, char* argv[])
{
std::string str_steps;
- pthread_mutex_init(&mutex_lock, NULL);
-
- //**************************************************************
- // STEP 0
- PlatformConfig cfg
- { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
-
- OCPlatform::Configure(cfg);
- g_thingsmanager = new ThingsManager();
- //**************************************************************
-
- while (true)
+ try
{
- pthread_mutex_lock(&mutex_lock);
- if (isWaiting > 0)
- {
- pthread_mutex_unlock(&mutex_lock);
- continue;
- }
+ //**************************************************************
+ // STEP 0
+ PlatformConfig cfg
+ { OC::ServiceType::InProc, OC::ModeType::Both, "0.0.0.0", 0, OC::QualityOfService::LowQos };
- isWaiting = 0;
- pthread_mutex_unlock(&mutex_lock);
+ OCPlatform::Configure(cfg);
+ g_thingsmanager = new ThingsManager();
- cout << endl << endl << "(0) Quit" << std::endl;
- cout << "(1) Find all resources(URI: /oic/con, /oic/diag, /factoryset)" << std::endl;
- cout << "(2) Find all groups" << std::endl;
- cout << "(3) Get a Configuration resource" << std::endl;
- cout << "(4) Update a region attribute value" << std::endl;
- cout << "(5) FactoryReset (for the group)" << std::endl;
- cout << "(6) Reboot (for the group)" << std::endl;
- cout << "(10) Show Configuration Units" << std::endl;
+ //**************************************************************
- try
+ while (true)
{
- std::getline (std::cin, str_steps);
-
- if(str_steps == "")
+ pthread_mutex_lock(&mutex_lock);
+ if (isWaiting > 0)
{
+ pthread_mutex_unlock(&mutex_lock);
continue;
}
- else
- {
- g_Steps = std::stoi(str_steps);
- }
- }
- catch(std::invalid_argument&)
- {
- std::cout << "Please put a digit, not string" << std::endl;
- continue;
- }
- if (g_Steps == 0)
- {
- break;
- }
- else if (g_Steps == 1)
- {
- std::vector< std::string > types;
+ isWaiting = 0;
+ pthread_mutex_unlock(&mutex_lock);
- // For Registering a collection resource for configuration resources
- if (configurationCollectionHandle == NULL)
- {
- string resourceURI = "/core/a/configuration/resourceset";
- string resourceTypeName = "core.configuration.resourceset";
- string resourceInterface = BATCH_INTERFACE;
-
- OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
- resourceTypeName, resourceInterface, NULL,
- //&entityHandler, // entityHandler
- OC_DISCOVERABLE);
-
- OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
- OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
- DEFAULT_INTERFACE);
- }
+ cout << endl << endl << "(0) Quit" << std::endl;
+ cout << "(1) Find all resources(URI: /oic/con, /oic/diag, /factoryset)" << std::endl;
+ cout << "(2) Find all groups" << std::endl;
+ cout << "(3) Get a Configuration resource" << std::endl;
+ cout << "(4) Update a region attribute value" << std::endl;
+ cout << "(5) FactoryReset (for the group)" << std::endl;
+ cout << "(6) Reboot (for the group)" << std::endl;
+ cout << "(10) Show Configuration Units" << std::endl;
- // For Registering a collection resource for diagnostics resources
- if (diagnosticsCollectionHandle == NULL)
+ try
{
- string resourceURI = "/core/a/diagnostics/resourceset";
- string resourceTypeName = "core.diagnostics.resourceset";
- string resourceInterface = BATCH_INTERFACE;
+ std::getline (std::cin, str_steps);
- OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
- resourceTypeName, resourceInterface, NULL,
- //&entityHandler, // entityHandler
- OC_DISCOVERABLE);
-
- OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
- OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
+ if(str_steps == "")
+ {
+ continue;
+ }
+ else
+ {
+ g_Steps = std::stoi(str_steps);
+ }
}
-
- // For Registering a collection resource for set resources
- if (setCollectionHandle == NULL)
+ catch(std::invalid_argument&)
{
- string resourceURI = "/core/a/factoryset/resourceset";
- string resourceTypeName = "core.factoryset.resourceset";
- string resourceInterface = BATCH_INTERFACE;
-
- OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
- resourceInterface, NULL,
- //&entityHandler, // entityHandler
- OC_DISCOVERABLE);
+ std::cout << "Please put a digit, not string" << std::endl;
+ continue;
+ }
- OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
- OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
+ if (g_Steps == 0)
+ {
+ break;
}
+ else if (g_Steps == 1)
+ {
+ std::vector< std::string > types;
- types.push_back("oic.con");
- types.push_back("oic.diag");
- types.push_back("factorySet");
+ // For Registering a collection resource for configuration resources
+ if (configurationCollectionHandle == NULL)
+ {
+ string resourceURI = "/core/a/configuration/resourceset";
+ string resourceTypeName = "core.configuration.resourceset";
+ string resourceInterface = BATCH_INTERFACE;
+
+ OCPlatform::registerResource(configurationCollectionHandle, resourceURI,
+ resourceTypeName, resourceInterface, NULL,
+ //&entityHandler, // entityHandler
+ OC_DISCOVERABLE);
+
+ OCPlatform::bindInterfaceToResource(configurationCollectionHandle, GROUP_INTERFACE);
+ OCPlatform::bindInterfaceToResource(configurationCollectionHandle,
+ DEFAULT_INTERFACE);
+ }
- std::cout << "Finding Configuration Resource... " << std::endl;
- std::cout << "Finding Diagnostics Resource... " << std::endl;
- std::cout << "Finding Set Resource... " << std::endl;
+ // For Registering a collection resource for diagnostics resources
+ if (diagnosticsCollectionHandle == NULL)
+ {
+ string resourceURI = "/core/a/diagnostics/resourceset";
+ string resourceTypeName = "core.diagnostics.resourceset";
+ string resourceInterface = BATCH_INTERFACE;
- g_thingsmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
+ OCPlatform::registerResource(diagnosticsCollectionHandle, resourceURI,
+ resourceTypeName, resourceInterface, NULL,
+ //&entityHandler, // entityHandler
+ OC_DISCOVERABLE);
- isWaiting = 1;
+ OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, GROUP_INTERFACE);
+ OCPlatform::bindInterfaceToResource(diagnosticsCollectionHandle, DEFAULT_INTERFACE);
+ }
- thread t(&timeCheck, 5);
- t.join(); // After 5 seconds, isWaiting value will be 0.
- }
- else if (g_Steps == 2) // make a group with found things
- {
- std::vector< std::string > types;
- types.push_back("core.configuration.resourceset");
- types.push_back("core.diagnostics.resourceset");
- types.push_back("core.factoryset.resourceset");
+ // For Registering a collection resource for set resources
+ if (setCollectionHandle == NULL)
+ {
+ string resourceURI = "/core/a/factoryset/resourceset";
+ string resourceTypeName = "core.factoryset.resourceset";
+ string resourceInterface = BATCH_INTERFACE;
- g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
+ OCPlatform::registerResource(setCollectionHandle, resourceURI, resourceTypeName,
+ resourceInterface, NULL,
+ //&entityHandler, // entityHandler
+ OC_DISCOVERABLE);
- std::cout << "Finding Collection resource... " << std::endl;
+ OCPlatform::bindInterfaceToResource(setCollectionHandle, GROUP_INTERFACE);
+ OCPlatform::bindInterfaceToResource(setCollectionHandle, DEFAULT_INTERFACE);
+ }
- isWaiting = 1;
+ types.push_back("oic.con");
+ types.push_back("oic.diag");
+ types.push_back("factorySet");
- thread t(&timeCheck, 5);
- t.join(); // After 5 seconds, isWaiting value will be 0.
- }
- else if (g_Steps == 3)
- {
- // get a value
+ std::cout << "Finding Configuration Resource... " << std::endl;
+ std::cout << "Finding Diagnostics Resource... " << std::endl;
+ std::cout << "Finding Set Resource... " << std::endl;
- ConfigurationName name = "all";
+ g_thingsmanager->findCandidateResources(types, &onFoundCandidateResource, 5);
- std::cout << "For example, get configuration resources's value" << std::endl;
+ pthread_mutex_lock(&mutex_lock);
+ isWaiting = 1;
+ pthread_mutex_unlock(&mutex_lock);
- std::vector< ConfigurationName > configurations;
+ thread t(&timeCheck, 5);
+ t.join(); // After 5 seconds, isWaiting value will be 0.
+ }
+ else if (g_Steps == 2) // make a group with found things
+ {
+ std::vector< std::string > types;
+ types.push_back("core.configuration.resourceset");
+ types.push_back("core.diagnostics.resourceset");
+ types.push_back("core.factoryset.resourceset");
- configurations.push_back(name);
+ g_thingsmanager->findCandidateResources(types, &onFoundCollectionResource, 5);
- if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations, &onGet)
- != OC_STACK_ERROR)
- {
+ std::cout << "Finding Collection resource... " << std::endl;
+
pthread_mutex_lock(&mutex_lock);
- isWaiting = 0;
+ isWaiting = 1;
pthread_mutex_unlock(&mutex_lock);
- }
- }
- else if (g_Steps == 4)
- {
- ConfigurationName name = "r";
- ConfigurationValue value = "U.S.A (new region)";
- if(g_configurationCollection == NULL)
- {
- std::cout<<"Note that you first create a group to use this command." << std::endl;
- continue;
+ thread t(&timeCheck, 5);
+ t.join(); // After 5 seconds, isWaiting value will be 0.
}
+ else if (g_Steps == 3)
+ {
+ // get a value
- std::cout << "For example, change region resource's value" << std::endl;
- std::cout << g_configurationCollection->uri() << std::endl;
+ ConfigurationName name = "all";
- std::map< ConfigurationName, ConfigurationValue > configurations;
+ std::cout << "For example, get configuration resources's value" << std::endl;
- configurations.insert(std::make_pair(name, value));
+ std::vector< ConfigurationName > configurations;
- if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
- &onUpdate) != OC_STACK_ERROR)
- {
- pthread_mutex_lock(&mutex_lock);
- isWaiting = 0;
- pthread_mutex_unlock(&mutex_lock);
+ configurations.push_back(name);
+
+ if (g_thingsmanager->getConfigurations(g_configurationCollection, configurations, &onGet)
+ != OC_STACK_ERROR)
+ {
+ pthread_mutex_lock(&mutex_lock);
+ isWaiting = 0;
+ pthread_mutex_unlock(&mutex_lock);
+ }
}
- }
- else if (g_Steps == 5)
- {
- // factory reset
- if(g_diagnosticsCollection == NULL)
+ else if (g_Steps == 4)
{
- std::cout<<"Note that you first create a group to use this command." << std::endl;
- continue;
- }
+ ConfigurationName name = "r";
+ ConfigurationValue value = "U.S.A (new region)";
- if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
- != OC_STACK_ERROR)
- {
- pthread_mutex_lock(&mutex_lock);
- isWaiting = 0;
- pthread_mutex_unlock(&mutex_lock);
+ if(g_configurationCollection == NULL)
+ {
+ std::cout<<"Note that you first create a group to use this command." << std::endl;
+ continue;
+ }
+
+ std::cout << "For example, change region resource's value" << std::endl;
+ std::cout << g_configurationCollection->uri() << std::endl;
+
+ std::map< ConfigurationName, ConfigurationValue > configurations;
+
+ configurations.insert(std::make_pair(name, value));
+
+ if (g_thingsmanager->updateConfigurations(g_configurationCollection, configurations,
+ &onUpdate) != OC_STACK_ERROR)
+ {
+ pthread_mutex_lock(&mutex_lock);
+ isWaiting = 0;
+ pthread_mutex_unlock(&mutex_lock);
+ }
}
- }
- else if (g_Steps == 6)
- {
- // reboot
- if(g_diagnosticsCollection == NULL)
+ else if (g_Steps == 5)
{
- std::cout<<"Note that you first create a group to use this command." << std::endl;
- continue;
- }
+ // factory reset
+ if(g_diagnosticsCollection == NULL)
+ {
+ std::cout<<"Note that you first create a group to use this command." << std::endl;
+ continue;
+ }
- if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
+ if (g_thingsmanager->factoryReset(g_diagnosticsCollection, &onFactoryReset)
+ != OC_STACK_ERROR)
+ {
+ pthread_mutex_lock(&mutex_lock);
+ isWaiting = 0;
+ pthread_mutex_unlock(&mutex_lock);
+ }
+ }
+ else if (g_Steps == 6)
{
- pthread_mutex_lock(&mutex_lock);
- isWaiting = 0;
- pthread_mutex_unlock(&mutex_lock);
+ // reboot
+ if(g_diagnosticsCollection == NULL)
+ {
+ std::cout<<"Note that you first create a group to use this command." << std::endl;
+ continue;
+ }
+
+ if (g_thingsmanager->reboot(g_diagnosticsCollection, &onReboot) != OC_STACK_ERROR)
+ {
+ pthread_mutex_lock(&mutex_lock);
+ isWaiting = 0;
+ pthread_mutex_unlock(&mutex_lock);
+ }
}
- }
- else if (g_Steps == 10)
- {
- std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;
+ else if (g_Steps == 10)
+ {
+ std::cout << g_thingsmanager->getListOfSupportedConfigurationUnits() << std::endl;
+ }
}
-
+ }catch (OCException e)
+ {
+ std::cout << "Exception in main: " << e.what();
}
return 0;