Merge "Partial Implementation of US1574:"
[platform/upstream/iotivity.git] / csdk / ubstack / src / ocmodelimpl.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
22 #ifndef OCMODELIMPL_H_
23 #define OCMODELIMPL_H_
24
25 // ============================================================================
26 // Includes
27 // ============================================================================
28 #include <memory>
29 #include <map>
30 #include <stdint.h>
31
32 #include "ocinternalapi.h"
33
34 extern "C" {
35     #include "ocstack.h"
36 }
37 // ============================================================================
38 // Namespace
39 // ============================================================================
40 namespace oc {
41 namespace ub {
42
43 class OCModelImpl : public OCModel {
44 public:
45         typedef std::shared_ptr<OCModelImpl> SharedPtr;
46         typedef std::weak_ptr<OCModelImpl> WeakPtr;
47
48 public:
49         /**
50          * Factory pattern to create a OCModel singleton
51          * @return Shared pointer to the OCModel
52          */
53         static SharedPtr createModel();
54
55 public:
56         /**
57          * Constructor
58          */
59         OCModelImpl();
60
61         /**
62          * Destructor
63          */
64         virtual ~OCModelImpl();
65
66     /**
67      * Start the OC Stack.
68      *
69      * @param ipAddr
70      *     IP Address of host device
71      * @param port
72      *     Port of host device*
73      * @param mode
74      *     Host device is client, server, or client-server
75      *
76      * @return
77      *     true  - successfully started the OC Stack
78      *     false - error starting the OC Stack
79      */
80     virtual bool start(const std::string ipAddr, int16_t port, OCStackMode mode);
81
82     /**
83      * Get all services that have been discovered at time of call
84      *
85      * @param asyncReturnFunc - asynchronous callback function that is invoked
86      *                          by the OCModelImpl when service discovery
87      *                          is complete.  The callback will include
88      *                          status and a list of all discovered services
89      */
90         virtual void discoverServices(OCDiscoverServicesFunction& asyncReturnFunc);
91
92     /**
93      * Add a service to the OCModelImpl
94      *
95      * @param url - URL of the service
96      * @return Total number of services in the OCModelImpl
97      */
98         virtual uint16_t addService(const std::string url);
99
100 private:
101     typedef std::list<std::string> ServiceList;
102
103 private:
104     /**
105      * Internal methods that invokes the client callback (if it was previously set via discoverServices)
106      * Called when a new service is discovered.
107      */
108     virtual void invokeDiscoverCallback();
109
110     /**
111      * Asynchronous callback friend function invoked by the OC Stack upon service discovery
112      *
113      * @param result         - OC stack result
114      * @param representation - handle to the representation of the resource
115      */
116     friend void asyncDoResourcesCallback(OCStackResult result, OCRepresentationHandle representation);
117 private:
118     ServiceList serviceList;
119     mutex serviceListMutex;
120     OCDiscoverServicesFunction discoverCallback;
121 };
122
123 }
124 }
125 #endif /* OCMODELIMPL_H_ */