Various fixes for the Android resource container extension
[platform/upstream/iotivity.git] / service / resource-container / src / BundleResource.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 "BundleResource.h"
22
23 #include <list>
24 #include <string.h>
25
26 #include "InternalTypes.h"
27
28 namespace OIC
29 {
30     namespace Service
31     {
32         BundleResource::BundleResource()
33         {
34             m_pNotiReceiver = nullptr;
35         }
36
37         BundleResource::~BundleResource()
38         {
39             m_pNotiReceiver = nullptr;
40         }
41
42         void BundleResource::registerObserver(NotificationReceiver *pNotiReceiver)
43         {
44             m_pNotiReceiver = pNotiReceiver;
45         }
46
47         std::list< std::string > BundleResource::getAttributeNames()
48         {
49             std::list< std::string > ret;
50             for (RCSResourceAttributes::iterator it = m_resourceAttributes.begin();
51                  it != m_resourceAttributes.end(); ++it)
52             {
53                 ret.push_back(it->key());
54             }
55             return ret;
56         }
57
58         RCSResourceAttributes &BundleResource::getAttributes()
59         {
60             return m_resourceAttributes;
61         }
62
63         void BundleResource::setAttributes(RCSResourceAttributes &attrs, bool notify)
64         {
65             for (RCSResourceAttributes::iterator it = attrs.begin(); it != attrs.end(); ++it)
66             {
67                 OC_LOG_V(INFO, "BUNDLE_RESOUCE", "set attribute \(%s)'",
68                          std::string(it->key() + "\', with " + it->value().toString()).c_str());
69
70                 m_resourceAttributes[it->key()] = it->value();
71             }
72
73             if (notify && m_pNotiReceiver){
74                 OC_LOG_V(INFO, "BUNDLE_RESOUCE", "Notifying receiver");
75                 m_pNotiReceiver->onNotificationReceived(m_uri);
76             }
77         }
78
79         void BundleResource::setAttributes(RCSResourceAttributes &attrs)
80         {
81             setAttributes(attrs, false);
82         }
83
84         void BundleResource::setAttribute(const std::string &key,
85                                           RCSResourceAttributes::Value &&value, bool notify)
86         {
87             OC_LOG_V(INFO, "BUNDLE_RESOUCE", "set attribute \(%s)'", std::string(key + "\', with " +
88                      value.toString()).c_str());
89
90             m_resourceAttributes[key] = value;
91
92             if (notify && m_pNotiReceiver)
93                 m_pNotiReceiver->onNotificationReceived(m_uri);
94         }
95
96         void BundleResource::setAttribute(const std::string &key, RCSResourceAttributes::Value &&value)
97         {
98             setAttribute(key, std::move(value), true);
99         }
100
101         RCSResourceAttributes::Value BundleResource::getAttribute(const std::string &key)
102         {
103             OC_LOG_V(INFO, "BUNDLE_RESOUCE", "get attribute \'(%s)" , std::string(key + "\'").c_str());
104
105             return m_resourceAttributes.at(key);
106         }
107     }
108 }