1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 // ============================================================================
9 // ============================================================================
10 #include "ocmodelimpl.h"
19 // ============================================================================
21 // ============================================================================
25 static const char TAG[] = "OCModelImpl";
27 // Global variable used by friend async callback function to access the OCModelImpl instance
28 OCModelImpl *pModelImpl = 0;
31 * Asynchronous callback friend function invoked by the OC Stack upon service discovery
33 * @param result - OC stack result
34 * @param representation - handle to the representation of the resource
36 void asyncDoResourcesCallback(OCStackResult result, OCRepresentationHandle representation) {
37 OC_LOG(INFO, TAG, "Entering asyncDoResourcesCallback");
39 if (representation && (result == OC_STACK_OK)) {
40 OCResource *resource = (OCResource *)representation;
41 OC_LOG_V(INFO, TAG, "URI = %s", resource->uri);
43 // If the pointer is valid, make a call to the OCModelImpl instance method
45 pModelImpl->addService(resource->uri);
50 // ============================================================================
51 // OCDiscoverServicesResult Implementation Class
52 // ============================================================================
53 class OCDiscoverServicesSuccess : public OCDiscoverServicesResult {
55 virtual ~OCDiscoverServicesSuccess() {}
57 // ============================================================
59 // ============================================================
61 virtual OCQueryResultType getResult() const {
65 virtual const std::list<std::string>& getServiceList() const {
70 std::list<std::string> serviceList;
74 // ============================================================================
76 // ============================================================================
77 OCModel::SharedPtr OCModel::createModel() {
78 OC_LOG(INFO, TAG, "Entering OCModel::createModel");
79 return OCModelImpl::createModel();
82 OCModelImpl::SharedPtr OCModelImpl::createModel() {
83 // Get singleton of ModelImpl.
84 OC_LOG(INFO, TAG, "Entering OCModelImpl::createModel");
85 static SharedPtr instance = std::make_shared<OCModelImpl>();
89 // ============================================================================
90 // Constructors & Destructors
91 // ============================================================================
92 OCModelImpl::OCModelImpl() {
93 OC_LOG(INFO, TAG, "Entering OCModel::OCModelImpl");
99 OCModelImpl::~OCModelImpl() {
100 OC_LOG(INFO, TAG, "Entering OCModel::~OCModelImpl");
101 // Call OCStack layer
102 if (OCStop() != OC_STACK_OK) {
103 OC_LOG(ERROR, TAG, "Stack shutdown error");
106 discoverCallback = 0;
110 // ============================================================================
112 // ============================================================================
114 * Start the OC Stack.
117 * IP Address of host device
119 * Port of host device*
121 * Host device is client, server, or client-server
124 * true - successfully started the OC Stack
125 * false - error starting the OC Stack
127 bool OCModelImpl::start(const std::string ipAddr, int16_t port, OCStackMode mode) {
128 OC_LOG_V(INFO, TAG, "Entering OCModelImpl::start, IP = %s, port = %d", ipAddr.c_str(), (int)port);
130 // Call OCStack layer - initialize stack
131 if (OCInit(ipAddr.c_str(), port, (OCMode)mode) != OC_STACK_OK) {
132 OC_LOG(ERROR, TAG, "Stack initialization error");
136 // Call OCStack layer - enable device discovery and provide a callback
137 if (OCDoResource(OC_REST_GET, OC_EXPLICIT_DEVICE_DISCOVERY_URI, 0, 0, asyncDoResourcesCallback) != OC_STACK_OK) {
138 OC_LOG(ERROR, TAG, "Stack discovery error");
145 * Get all services that have been discovered at time of call
147 * @param asyncReturnFunc - asynchronous callback function that is invoked
148 * by the OCModelImpl when service discovery
149 * is complete. The callback will include
150 * status and a list of all discovered services
152 void OCModelImpl::discoverServices(OCDiscoverServicesFunction& asyncReturnFunc) {
153 OC_LOG(INFO, TAG, "Entering OCModelImpl::discoverServices");
155 mutex_lock_guard lock(serviceListMutex);
158 discoverCallback = asyncReturnFunc;
160 // Invoke the callback immediately
161 invokeDiscoverCallback();
165 * Add a service to the OCModelImpl
167 * @param url - URL of the service
168 * @return Total number of services in the OCModelImpl
170 uint16_t OCModelImpl::addService(const std::string url) {
171 OC_LOG_V(INFO, TAG, "Entering OCModelImpl::addService, service = %s", url.c_str());
172 serviceList.push_back(url);
174 invokeDiscoverCallback();
175 return (uint16_t)serviceList.size();
179 * Internal methods that invokes the client callback (if it was previously set via discoverServices)
180 * Called when a new service is discovered.
182 void OCModelImpl::invokeDiscoverCallback() {
183 OC_LOG(INFO, TAG, "Entering OCModelImpl::invokeDiscoverCallback");
184 // Only invoke the callback if it has been set and and at least one service has been added
185 // to the service list
186 if (discoverCallback && (serviceList.size() > 0)) {
187 OC_LOG(INFO, TAG, "Invoking callback");
189 OCDiscoverServicesSuccess result;
190 // Insert from the model device list to the result device list
191 result.serviceList.insert(result.serviceList.end(), serviceList.begin(), serviceList.end());
193 // Invoke the callback
194 discoverCallback(result);