[IoTivity Simulator] Handling resource interfaces.
[platform/upstream/iotivity.git] / service / simulator / src / server / simulator_single_resource_impl.h
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 #ifndef SIMULATOR_SINGLE_RESOURCE_IMPL_H_
22 #define SIMULATOR_SINGLE_RESOURCE_IMPL_H_
23
24 #include "simulator_single_resource.h"
25 #include "resource_update_automation_mngr.h"
26 #include "RamlParser.h"
27
28 class SimulatorResourceFactory;
29 class SimulatorSingleResourceImpl : public SimulatorSingleResource
30 {
31     public:
32         friend class SimulatorResourceFactory;
33
34         std::string getName() const;
35         SimulatorResource::Type getType() const;
36         std::string getURI() const;
37         std::string getResourceType() const;
38         std::vector<std::string> getInterface() const;
39         void setInterface(const std::vector<std::string> &interfaces);
40         void setName(const std::string &name);
41         void setURI(const std::string &uri);
42         void setResourceType(const std::string &resourceType);
43         void addInterface(const std::string &interfaceType);
44         void removeInterface(const std::string &interfaceType);
45         void addInterface(const std::vector<std::string> &interfaceList);
46         void removeInterface(const std::vector<std::string> &interfaceList);
47         void setObservable(bool state);
48         void setObserverCallback(ObserverCallback callback);
49         bool isObservable();
50         bool isStarted();
51         void start();
52         void stop();
53         std::vector<ObserverInfo> getObserversList();
54         void notify(int id);
55         void notifyAll();
56         void notify(int id, SimulatorResourceModel &resModel);
57         void notifyAll(SimulatorResourceModel &resModel);
58
59         bool getAttribute(const std::string &attrName,
60                           SimulatorResourceModel::Attribute &attribute);
61         void addAttribute(const SimulatorResourceModel::Attribute &attribute, bool notify = true);
62         bool getAttributeProperty(const std::string &attrName,
63                                   SimulatorResourceModel::AttributeProperty &property);
64         bool setAttributeProperty(const std::string &attrName,
65                                   const SimulatorResourceModel::AttributeProperty &property);
66         bool updateAttributeValue(const SimulatorResourceModel::Attribute &attribute,
67                                   bool notify = true);
68         bool removeAttribute(const std::string &attrName, bool notify = true);
69         SimulatorResourceModel getResourceModel();
70         void setModelChangeCallback(ResourceModelChangedCallback callback);
71         int startResourceUpdation(AutomationType type, int updateInterval,
72                                   updateCompleteCallback callback);
73         int startAttributeUpdation(const std::string &attrName, AutomationType type,
74                                    int updateInterval, updateCompleteCallback callback);
75         std::vector<int> getResourceUpdationIds();
76         std::vector<int> getAttributeUpdationIds();
77         void stopUpdation(const int id);
78         void setResourceModel(const SimulatorResourceModel &resModel);
79         void setPutErrorResponseModel(const SimulatorResourceModel &resModel);
80         void setPostErrorResponseModel(const SimulatorResourceModel &resModel);
81         void setActionType(std::map<RAML::ActionType, RAML::ActionPtr> &actionType);
82         RAML::ActionType getActionType(std::string requestType);
83         void notifyApp();
84         void notifyApp(SimulatorResourceModel &resModel);
85
86     private:
87         SimulatorSingleResourceImpl();
88         OCEntityHandlerResult handleRequests(std::shared_ptr<OC::OCResourceRequest> request);
89         std::shared_ptr<OC::OCResourceResponse> handleReadRequest(
90             std::shared_ptr<OC::OCResourceRequest> request, const std::string &interfaceType);
91         std::shared_ptr<OC::OCResourceResponse> handleWriteRequest(
92             std::shared_ptr<OC::OCResourceRequest> request, const std::string &interfaceType);
93         OCEntityHandlerResult sendErrorResponse(std::shared_ptr<OC::OCResourceRequest> request,
94             const int errCode, OCEntityHandlerResult responseCode);
95         void setCommonProperties(SimulatorResourceModel &resModel);
96         bool updateResourceModel(OC::OCRepresentation &ocRep, SimulatorResourceModel &resModel);
97         void addObserver(OC::ObservationInfo ocObserverInfo);
98         void removeObserver(OC::ObservationInfo ocObserverInfo);
99         void removeAllObservers();
100
101         SimulatorResource::Type m_type;
102         std::string m_name;
103         std::string m_uri;
104         std::string m_resourceType;
105         std::vector<std::string> m_interfaces;
106         std::map<std::string, std::vector<std::string>> m_requestTypes;
107
108         std::recursive_mutex m_objectLock;
109         std::mutex m_modelLock;
110         SimulatorResourceModel m_resModel;
111         SimulatorResourceModel m_putErrorResModel;
112         SimulatorResourceModel m_postErrorResModel;
113         ResourceModelChangedCallback m_modelCallback;
114         ObserverCallback m_observeCallback;
115         UpdateAutomationMngr m_updateAutomationMgr;
116         std::vector<ObserverInfo> m_observersList;
117         std::map<RAML::ActionType , RAML::ActionPtr> m_actionTypes;
118
119         OCResourceProperty m_property;
120         OCResourceHandle m_resourceHandle;
121 };
122
123 typedef std::shared_ptr<SimulatorSingleResourceImpl> SimulatorSingleResourceImplSP;
124
125 #endif