Imported Upstream version 1.0.1
[platform/upstream/iotivity.git] / service / resource-container / examples / BMISensorBundle / src / BMISensorBundleActivator.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "BMISensorBundleActivator.h"
22
23 #include <algorithm>
24 #include <sstream>
25 #include "BMISensorResource.h"
26
27
28 BMISensorBundleActivator *g_bundleBMI;
29
30 BMISensorBundleActivator::BMISensorBundleActivator()
31 {
32     m_pResourceContainer = nullptr;
33 }
34
35 BMISensorBundleActivator::~BMISensorBundleActivator()
36 {
37     m_pResourceContainer = nullptr;
38 }
39
40 void BMISensorBundleActivator::activateBundle(ResourceContainerBundleAPI *resourceContainer,
41         std::string bundleId)
42 {
43     m_pResourceContainer = resourceContainer;
44     m_bundleId = bundleId;
45
46     std::vector<resourceInfo> resourceConfig;
47
48     resourceContainer->getResourceConfiguration(m_bundleId, &resourceConfig);
49
50     for (vector<resourceInfo>::iterator itor = resourceConfig.begin();
51          itor != resourceConfig.end(); itor++)
52     {
53         createResource(*itor);
54     }
55 }
56
57 void BMISensorBundleActivator::deactivateBundle()
58 {
59     std::vector< BundleResource::Ptr >::iterator itor;
60     for (itor = m_vecResources.begin(); itor != m_vecResources.end();)
61     {
62         destroyResource(*itor);
63     }
64 }
65
66 void BMISensorBundleActivator::createResource(resourceInfo resourceInfo)
67 {
68     if (resourceInfo.resourceType == "oic.r.sensor")
69     {
70         static int BMISensorCount = 1;
71
72         // create BMISensor resource
73         BundleResource::Ptr newResource = std::make_shared< BMISensorResource >();
74
75         newResource->m_bundleId = m_bundleId;
76         std::string indexCount;//string which will contain the indexCount
77         stringstream convert; // stringstream used for the conversion
78         convert << BMISensorCount++;//add the value of Number to the characters in the stream
79         indexCount = convert.str();//set indexCount to the content of the stream
80
81         newResource->m_uri = "/softsensor/BMIsensor/" + indexCount;
82         newResource->m_resourceType = resourceInfo.resourceType;
83         newResource->m_mapResourceProperty = resourceInfo.resourceProperty;
84
85         newResource->initAttributes();
86
87         m_pResourceContainer->registerResource(newResource);
88         m_vecResources.push_back(newResource);
89     }
90 }
91
92 void BMISensorBundleActivator::destroyResource(BundleResource::Ptr resource)
93 {
94     std::vector< BundleResource::Ptr >::iterator itor;
95
96     itor = std::find(m_vecResources.begin(), m_vecResources.end(), resource);
97
98     if (itor != m_vecResources.end())
99     {
100         m_pResourceContainer->unregisterResource(resource);
101         m_vecResources.erase(itor);
102     }
103 }
104
105 extern "C" void bmisensor_externalActivateBundle(ResourceContainerBundleAPI *resourceContainer,
106         std::string bundleId)
107 {
108     g_bundleBMI = new BMISensorBundleActivator();
109     g_bundleBMI->activateBundle(resourceContainer, bundleId);
110 }
111
112 extern "C" void bmisensor_externalDeactivateBundle()
113 {
114     g_bundleBMI->deactivateBundle();
115     delete g_bundleBMI;
116 }
117
118 extern "C" void bmisensor_externalCreateResource(resourceInfo resourceInfo)
119 {
120     g_bundleBMI->createResource(resourceInfo);
121 }
122
123 extern "C" void bmisensor_externalDestroyResource(BundleResource::Ptr pBundleResource)
124 {
125     g_bundleBMI->destroyResource(pBundleResource);
126 }