[IoTivity Simulator] Handling resource interfaces.
[platform/upstream/iotivity.git] / service / simulator / src / common / simulator_utils.h
1 /******************************************************************
2  *
3  * Copyright 2015 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  * @file simulator_utils.h
23  *
24  * @brief This file provides utility methods used by both client and servers.
25  */
26
27 #ifndef SIMULATOR_UTILS_H_
28 #define SIMULATOR_UTILS_H_
29
30 #include "simulator_exceptions.h"
31 #include "octypes.h"
32 #include "OCException.h"
33
34 #include <map>
35
36 /**
37  * Utilities for Invocation of OC platfrom level APIs.
38  */
39 template <typename FnT, typename... ArgsT>
40 typename std::enable_if<std::is_same<OCStackResult, decltype(std::declval<FnT>()(std::declval<ArgsT>()...))>::value>::type
41 invokeocplatform(FnT fn, ArgsT &&...args)
42 {
43     try
44     {
45         OCStackResult ocResult = fn(std::forward<ArgsT>(args)...);
46         if (OC_STACK_OK != ocResult)
47         {
48             // Throw SimulatorException converting error codes
49             throw SimulatorException(static_cast<SimulatorResult>(ocResult), OC::OCException::reason(ocResult));
50         }
51     }
52     catch (OC::OCException &e)
53     {
54         // Throw SimulatorException converting error codes
55         throw SimulatorException(static_cast<SimulatorResult>(e.code()), e.reason());
56     }
57 }
58
59 namespace OC
60 {
61     // Read-Only interface
62     const std::string READ_INTERFACE = "oic.if.r";
63
64     // Read-Write Interface
65     const std::string READWRITE_INTERFACE = "oic.if.rw";
66
67     // Actuator Interface
68     const std::string ACTUATOR_INTERFACE = "oic.if.a";
69
70     // Sensor Interface
71     const std::string SENSOR_INTERFACE = "oic.if.s";
72
73     class OCRepresentation;
74 }
75
76 std::string getPayloadString(const OC::OCRepresentation &);
77 std::string getPayloadTypeString(OCPayloadType type);
78 std::string getRequestString(const std::map<std::string, std::string> &queryParams,
79                              const OC::OCRepresentation &rep);
80 std::string getRequestString(const std::map<std::string, std::string> &queryParams);
81
82
83 #define VALIDATE_INPUT(CONDITION, MSG) if (CONDITION) {throw InvalidArgsException(SIMULATOR_INVALID_PARAM, MSG);}
84 #define VALIDATE_CALLBACK(CALLBACK) if (!CALLBACK){throw InvalidArgsException(SIMULATOR_INVALID_CALLBACK, "Invalid callback!");}
85
86 #endif