Merge test folder of test branch
[iotivity.git] / test / src / tc / ns / gtest / csdk / src / helper / NSProviderHelper.cpp
1 /******************************************************************
2  *
3  * Copyright 2016 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  *      LICENSE-2.0" target="_blank">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 #include "NSProviderHelper.h"
23
24 NSProviderHelper* NSProviderHelper::s_nsHelperInstance = NULL;
25 std::mutex NSProviderHelper::s_mutex;
26 NSConsumer* NSProviderHelper::s_pConsumer = nullptr;
27 string NSProviderHelper::s_consumerID = "";
28 const string NOTIFICATION_INFO = "OCF_NOTIFICATION";
29 bool NSProviderHelper::s_isOnSubscribeRequest = false;
30 bool NSProviderHelper::s_isOnProviderSyncInfo = false;
31
32 NSProviderHelper::NSProviderHelper()
33 {
34     PlatformConfig config
35     { ServiceType::InProc, ModeType::Both, "0.0.0.0", 0, QualityOfService::LowQos };
36     OCPlatform::Configure(config);
37     IOTIVITYTEST_LOG(INFO, "Platform Configuration Done!!");
38
39     try
40     {
41         OCPlatform::stopPresence();
42         IOTIVITYTEST_LOG(INFO, "Successfully stopPresense() called !!");
43     }
44     catch (...)
45     {
46         IOTIVITYTEST_LOG(WARNING, "Can't stop presense..!!!");
47     }
48 }
49
50 NSProviderHelper::~NSProviderHelper()
51 {
52     NSProviderHelper::s_pConsumer = nullptr;
53 }
54
55 NSProviderHelper* NSProviderHelper::getInstance(void)
56 {
57     if (s_nsHelperInstance == NULL)
58     {
59         s_mutex.lock();
60
61         s_nsHelperInstance = new NSProviderHelper();
62
63         s_mutex.unlock();
64     }
65
66     s_pConsumer = nullptr;
67     s_consumerID = "";
68
69     return s_nsHelperInstance;
70 }
71
72 void NSProviderHelper::onProviderSyncInfo(NSSyncInfo* syncInfo)
73 {
74     IOTIVITYTEST_LOG(INFO, "onProviderSyncInfoCallback() called !!");
75     s_isOnProviderSyncInfo = true;
76 }
77
78 void NSProviderHelper::onSubscribeRequest(NSConsumer* consumer)
79 {
80     IOTIVITYTEST_LOG(INFO, "onSubscribeRequestCallback() called with consumerID %s !!",
81             string(consumer->consumerId).c_str());
82
83     NSProviderHelper::s_pConsumer = consumer;
84     NSProviderHelper::s_consumerID = consumer->consumerId;
85     s_isOnSubscribeRequest = true;
86 }
87
88 NSProviderConfig NSProviderHelper::getProviderConfig(bool policy)
89 {
90     NSProviderConfig config;
91
92     config.subRequestCallback = &NSProviderHelper::onSubscribeRequest;
93     config.syncInfoCallback = &NSProviderHelper::onProviderSyncInfo;
94     config.subControllability = policy;
95     config.userInfo = OICStrdup(NOTIFICATION_INFO.c_str());
96
97     return config;
98 }
99
100 NSConsumer* NSProviderHelper::getConsumer()
101 {
102     waitForConsumer(WAIT_TIME_MAX);
103
104     return NSProviderHelper::s_pConsumer;
105 }
106
107 string NSProviderHelper::getConsumerID()
108 {
109     waitForConsumer(WAIT_TIME_MAX);
110
111     return NSProviderHelper::s_consumerID;
112 }
113
114 void NSProviderHelper::waitForConsumer(int time)
115 {
116     IOTIVITYTEST_LOG(INFO, "Waiting for consumer........");
117
118     while (time--)
119     {
120         if (NSProviderHelper::s_pConsumer != nullptr)
121         {
122             return;
123         }
124
125         CommonUtil::waitInSecond(WAIT_TIME_MIN);
126     }
127 }
128
129 bool NSProviderHelper::printProviderTopicList(NSTopicLL *topics)
130 {
131     bool isTopicFound = false;
132     if (topics)
133     {
134         NSTopicLL *topicIter = topics;
135         while (topicIter)
136         {
137             IOTIVITYTEST_LOG(INFO, "Topic Name: %s \t Topic State: %d", topicIter->topicName,
138                     topicIter->state);
139             if (strcmp(topicIter->topicName, TOPIC_NAME_PROVIDER) == 0)
140             {
141                 isTopicFound = true;
142             }
143             topicIter = topicIter->next;
144         }
145     }
146
147     return isTopicFound;
148 }