1 //******************************************************************
3 // Copyright 2014 Intel Corporation All Rights Reserved.
4 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 #include "ocinternalapi.h"
11 #include "ocstackinternal.h"
14 #include "gtest/gtest.h"
15 #include <sys/types.h>
29 static const char TAG[] = "TestHarness";
30 static const std::string SERVICE_URI_1 = "coap://127.0.0.1:5683/";
31 static const std::string SERVICE_URI_2 = "coap://127.0.0.2:5683/";
32 static const std::string SERVICE_URI_3 = "coap://127.0.0.3:5683/";
34 void discoverServicesAsync(const oc::ub::OCDiscoverServicesResult& result) {
35 OC_LOG(INFO, TAG, "Entering discoverServicesAsync");
37 EXPECT_EQ(oc::ub::OCQueryResultType::SUCCESS, result.getResult());
39 if (result.getResult() == oc::ub::OCQueryResultType::SUCCESS) {
40 OC_LOG(INFO, TAG, "discoverServicesAsync, OCQueryResultType::SUCCESS");
42 std::list<std::string> serviceList = result.getServiceList();
43 EXPECT_GE((int)serviceList.size(), 1);
45 if (serviceList.size() > 0) {
46 for (auto iter = serviceList.begin(); iter != serviceList.end(); ++iter) {
47 OC_LOG_V(INFO, TAG, "discoverServicesAsync, Service = %s", iter->c_str());
52 //-----------------------------------------------------------------------------
54 //-----------------------------------------------------------------------------
55 TEST(UBStackTest, ServiceDiscovery) {
56 OC_LOG(INFO, TAG, "Running ServiceDiscovery");
58 oc::ub::OCModel::WeakPtr modelWeak = oc::ub::OCModel::createModel();
59 oc::ub::OCModel::SharedPtr modelShared = modelWeak.lock();
60 EXPECT_TRUE((modelShared != 0));
63 EXPECT_TRUE(modelShared->start("127.0.0.1", 5683, oc::ub::OC_CLIENT));
65 // Create an async discovery function
66 oc::ub::OCDiscoverServicesFunction serviceFunction = discoverServicesAsync;
69 modelShared->discoverServices(serviceFunction);
72 EXPECT_EQ(1, (int)modelShared->addService(SERVICE_URI_1));
75 EXPECT_EQ(2, (int)modelShared->addService(SERVICE_URI_2));
77 // Add service via OC Stack
78 OCUpdateResources(SERVICE_URI_3.c_str());