Merge "Partial Implementation of US1574:"
[platform/upstream/iotivity.git] / csdk / controller / core / src / ModelImpl.h
1 //******************************************************************
2 //
3 // Copyright 2014 Intel Mobile Communications GmbH 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 MODELIMPL_H_
22 #define MODELIMPL_H_
23
24 // ============================================================================
25 // Includes
26 // ============================================================================
27 #include <memory>
28 #include <map>
29 #include <list>
30 #include <stdint.h>
31
32 #include "Protocol.hpp"
33 #include "UUIDLess.hpp"
34 #include "InternalApi.h"
35 #include "Core.h"
36
37
38 // ============================================================================
39 // Namespace
40 // ============================================================================
41 namespace Intel {
42 namespace CCFL {
43 namespace API {
44
45 class ModelImpl : public Model
46 {
47 public:
48         typedef std::shared_ptr<ModelImpl> SharedPtr;
49         typedef std::weak_ptr<ModelImpl> WeakPtr;
50
51 public:
52         // Factory
53         static SharedPtr createModel();
54
55 public:
56         // Constructor & Destructor
57         ModelImpl();
58         virtual ~ModelImpl();
59
60         // Get the device list
61         virtual void getDevices(GetDevicesFunction& asyncReturnFunc);
62
63         // Add/remove device observer
64         virtual void removeDeviceObserver(DeviceObserverHandle observerHandle);
65         virtual void setDeviceObserver(DeviceEventFunction& asyncEventFunction);
66
67         // Register/unregister a protocol
68         virtual const Protocols::Protocol::Handle registerProtocol(const Protocols::Protocol::SharedPtr& protocol);
69         virtual bool unregisterProtocol(const Protocols::Protocol::Handle protocolHandle);
70         virtual Device::SharedPtr getDevice(const UUID_t& deviceId);
71         virtual void signalDeviceChange(const UUID_t& deviceId, DeviceEvent::DeviceChange deviceEvent);
72
73 private:
74         typedef std::map<Protocols::Protocol::Handle, Protocols::Protocol::WeakPtr> ProtocolMap;
75         typedef std::list<Device::SharedPtr> DeviceList;
76         typedef std::map<DeviceObserverHandle, DeviceEventFunction> DeviceObserverMap;
77         typedef std::list<UUID_t> DeviceIdList;
78 private:
79         const Protocols::Protocol::Handle generateProtocolHandle();
80         bool isHandleInUse(Protocols::Protocol::Handle handle);
81         Device::SharedPtr addDeviceToList(const UUID_t& deviceId);
82
83         DeviceObserverHandle generateDeviceObserverHandle();
84   bool isDeviceObserverHandleInUse(DeviceObserverHandle observerHandle);
85   DeviceIdList getDeviceIdList();
86   void notifyDeviceObservers(DeviceEvent::DeviceChange event, const DeviceIdList& deviceIdList);
87
88 private:
89         ProtocolMap protocolMap_;
90         DeviceList deviceList_;
91         DeviceObserverMap deviceObserverMap_;
92
93         mutex protocolMapMutex_;
94         mutex deviceListMutex_;
95         mutex deviceObserverMapMutex_;
96 };
97
98 }
99 }
100 }
101 #endif /* MODELIMPL_H_ */