Merge test folder of test branch
[iotivity.git] / test / src / tc / ri / gtest / csdk / include / RICsdkHelper.h
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  *      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 RI_CSDK_HELPER_H_
23 #define RI_CSDK_HELPER_H_
24
25 #include <string.h>
26 #ifdef HAVE_UNISTD_H
27 #include <unistd.h>
28 #endif
29 #ifdef HAVE_PTHREAD_H
30 #include <pthread.h>
31 #endif
32 #include <iostream>
33 #include <map>
34 #include <utility>
35 #include <vector>
36 #include <mutex>
37 #include <algorithm>
38 #include <gtest/gtest.h>
39 #include <OCApi.h>
40 #include <oic_malloc.h>
41 #include "CommonUtil.h"
42 #include "CommonTestUtil.h"
43 #include "IotivityTest_Logger.h"
44 #include "OCPlatform.h"
45 #include "ocstack.h"
46 #include "ocpayload.h"
47
48 using namespace OC;
49 using namespace std;
50 namespace PH = std::placeholders;
51
52 #define EMPTY_STRING ""
53 #define INT_ZERO 0
54 #define DATABASE_FILE "oic_svr_db_server.dat"
55
56 //timeouts for callback
57 #define CALLBACK_WAIT_MIN 1
58 #define CALLBACK_WAIT_DEFAULT 5
59 #define CALLBACK_WAIT_MAX 5
60
61 //Set of strings for each of deviceInfo fields
62 #define MANUFACTURE_DATE "myDateOfManufacture"
63 #define FIRMWARE_VERSION "my.Firmware.Version"
64 #define MANUFACTURER_NAME "myManufacturer"
65 #define OS_VERSION "myOS"
66 #define HW_VERSION "myHardwareVersion"
67 #define PLATFORM_ID "0A3E0D6F-DBF5-404E-8719-D6880042463A"
68 #define MANUFACTURER_URL "www.myurl.com"
69 #define MODEL_NO "myModelNumber"
70 #define PLATFORM_VERSION "platformVersion"
71 #define SUPPORT_URL "https://www.iotivity.org"
72 #define SYSTEM_TIME "2015-05-15T11.04"
73
74 //Set of strings for each of platform info fields
75 #define DEVICE_NAME "TestName"
76 #define SPEC_VERSION "myDeviceSpecVersion"
77 #define DATA_MODEL_VERSION "myDeviceModelVersions"
78 #define TEMPERATURE_TYPE "oic.r.temperature"
79 #define TEMPERATURE_URI "/test/ri/csdk/temperature"
80 #define RESOURCE_TYPE "%s/oic/res?rt="
81 //Context value set
82 #define DEFAULT_CONTEXT_VALUE 0x99
83
84 //query for doResource
85 constexpr char DEVICE_DISCOVERY_QUERY[]
86 { "%s/oic/d" };
87 constexpr char PLATFORM_DISCOVERY_QUERY[]
88 { "%s/oic/p" };
89 constexpr char ALL_RESOURCE_DISCOVERY_QUERY[]
90 { "%s/oic/res" };
91 #ifdef __WINDOWS__
92 constexpr char TEMPERATURE_RESOURCE_DISCOVERY_QUERY[]
93 { RESOURCE_TYPE TEMPERATURE_TYPE };
94 #endif
95 #ifndef __WINDOWS__
96 constexpr char TEMPERATURE_RESOURCE_DISCOVERY_QUERY[]
97 { "%s/oic/res?rt="TEMPERATURE_TYPE };
98 #endif
99 constexpr char LIGHT_RESOURCE_DISCOVERY_QUERY[]
100 { "%s/oic/res?rt=core.light" };
101 constexpr char INTERFACE_DISCOVERY_QUERY[]
102 { "%s/oic/res?if=oic.if.new" };
103
104 //resource types
105 constexpr char RESOURCE_TYPE_LIGHT[]
106 { "core.light" };
107 constexpr char RESOURCE_TYPE_FAN[]
108 { "core.fan" };
109 constexpr char RESOURCE_TYPE_TEMPERATURE[]
110 { "oic.r.temperature" };
111 constexpr char RESOURCE_TYPE_PLATFORM[]
112 { "oic.wk.p" };
113 constexpr char RESOURCE_TYPE_DEVICE[]
114 { "oic.wk.d" };
115 constexpr char RESOURCE_URI_LIGHT[]
116 { "/a/light" };
117 constexpr char RESOURCE_URI_FAN[]
118 { "/a/fan" };
119 constexpr char RESOURCE_URI_TEMPERATURE[]
120 { TEMPERATURE_URI };
121 constexpr char RESOURCE_INTERFACE_DEFAULT[]
122 { "oc.if.a" };
123 constexpr char RESOURCE_INTERFACE_RW[]
124 { "core.rw" };
125
126 constexpr char KEY_TEMPERATURE[]
127 { "temperature" };
128 constexpr char KEY_UNITS[]
129 { "units" };
130 constexpr char KEY_HOUR[]
131 { "x.samsung.hour" };
132
133 constexpr char KEY_DEVICE_NAME[]
134 { "n" };
135 constexpr char KEY_SPEC_VERSION[]
136 { "lcv" };
137
138 constexpr char KEY_PLATFORM_ID[]
139 { "pi" };
140 constexpr char KEY_MANUFACTURER_NAME[]
141 { "mnmn" };
142 constexpr char KEY_MANUFACTURER_URL[]
143 { "mnml" };
144 constexpr char KEY_MODEL_NUMBER[]
145 { "mnmo" };
146 constexpr char KEY_DATE_OF_MANUFACTURE[]
147 { "mndt" };
148 constexpr char KEY_PLATFORM_VERSION[]
149 { "mnpv" };
150 constexpr char KEY_OPERATING_SYSTEM[]
151 { "mnos" };
152 constexpr char KEY_HARDWARE_VERSION[]
153 { "mnhw" };
154 constexpr char KEY_FIRMWARE_VERSION[]
155 { "mnfv" };
156 constexpr char KEY_SUPPORT_URL[]
157 { "mnsl" };
158 constexpr char KEY_SYSTEM_TIME[]
159 { "st" };
160
161 class RICsdkHelper
162 {
163 private:
164     static RICsdkHelper* s_riCsdkHelperInstance;
165     static std::mutex s_mutex;
166     const uint8_t m_ResourceProperty = OC_DISCOVERABLE | OC_OBSERVABLE;
167     const std::string m_ResourceTypeName = "core.res";
168     uint16_t m_ServerPort = 0;
169     const char* m_ServerIp = nullptr;
170     OCResourceHandle m_handle;
171     string m_failureMsg;
172     pthread_t m_p_thread;
173
174     //client variables
175     uint16_t m_ClientPort = 0;
176     const char* m_ClientIp = nullptr;
177     OCCallbackData m_cbData;
178     OCPayload* m_targetPayload;
179
180 public:
181     static bool s_quitFlag;
182     static bool s_isCallback;
183     static bool s_isPayloadCorrect;
184     static bool s_isServerResponse;
185     static string s_failureMsg;
186     static bool s_isResourceFound;
187     static OCEntityHandlerResult s_responseErrorCode;
188     static int s_unicastDiscovery;
189
190     static char s_szQueryUri[100];
191     static char s_discoveryAddr[100];
192     static string s_coapQuery;
193     static OCDevAddr s_remoteAddr;
194     static OCResourceHandle s_handle;
195     static int64_t s_temp;
196     static char* s_units;
197     static int64_t s_hour;
198
199     static bool s_isBtc;
200     OCStackResult m_result;
201
202 public:
203     RICsdkHelper();
204     virtual ~RICsdkHelper();
205     static RICsdkHelper* getInstance(void);
206
207     //server functions
208     OCStackResult initServer();
209     void duplicateString(char ** targetString, std::string sourceString);
210     OCPlatformInfo getPlatformInfo();bool OCResourcePayloadAddStringLL(OCStringLL **stringLL,
211             std::string value);
212     OCDeviceInfo getDeviceInfo();
213     static OCEntityHandlerResult OCEntityHandlerCb(OCEntityHandlerFlag flag,
214             OCEntityHandlerRequest *entityHandlerRequest, void* /*callback*/);
215     OCResourceHandle createResource(const char *resourceTypeName, const char *resourceInterfaceName,
216             const char *uri);
217     //OCResourceHandle RICsdkHelper::createTemperatureResource();
218
219     //client functions
220     OCStackResult initClient();
221     OCDoHandle doResource(OCMethod method, const char *requestUri, OCQualityOfService qos);
222     static OCStackApplicationResult PlatformDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
223             OCClientResponse * clientResponse);
224     static OCStackApplicationResult DeviceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
225             OCClientResponse * clientResponse);
226     static OCStackApplicationResult ResourceDiscoveryReqCB(void* ctx, OCDoHandle /*handle*/,
227             OCClientResponse * clientResponse);
228     static OCStackApplicationResult getReqCB(void* ctx, OCDoHandle /*handle*/,
229             OCClientResponse * clientResponse);
230     static OCStackApplicationResult putReqCB(void* ctx, OCDoHandle /*handle*/,
231             OCClientResponse * clientResponse);
232     static OCStackApplicationResult postReqCB(void* ctx, OCDoHandle /*handle*/,
233             OCClientResponse * clientResponse);
234     static OCStackApplicationResult deleteReqCB(void* ctx, OCDoHandle /*handle*/,
235             OCClientResponse * clientResponse);
236     static OCStackApplicationResult obsReqCB(void* ctx, OCDoHandle /*handle*/,
237             OCClientResponse * clientResponse);
238     static OCStackApplicationResult allReqCB(void* ctx, OCDoHandle /*handle*/,
239                 OCClientResponse * clientResponse);
240
241     //stc functions
242     OCStackResult initClientServer();
243
244     void startOCProcess();
245     static void runThreadForOCProcess();
246
247     static OCEntityHandlerResult ProcessGetRequest(OCEntityHandlerRequest *ehRequest,
248             OCRepPayload **payload);
249     static OCEntityHandlerResult ProcessPutRequest(OCEntityHandlerRequest *ehRequest,
250             OCRepPayload** payload);
251     static OCEntityHandlerResult ProcessPostRequest(OCEntityHandlerRequest *ehRequest,
252             OCRepPayload** payload);
253     static OCEntityHandlerResult ProcessDeleteRequest(OCEntityHandlerRequest *ehRequest);
254
255     //payload functions
256     static OCRepPayload* getPayload(int64_t temp, char* units, int64_t hour);
257     static OCRepPayload* constructResponse(OCEntityHandlerRequest *ehRequest);
258     static void getPayloadData(OCClientResponse * clientResponse);
259     static bool payloadLogDiscovery(OCDiscoveryPayload* payload);
260     static bool payloadLogDevice(OCRepPayload* payload);
261     static bool payloadLogPlatform(OCRepPayload* payload);
262     static bool payloadLogRepResource(OCRepPayload* payload);
263
264     //failure message get
265     string getFailureMessage();
266 };
267
268 #endif
269