Imported Upstream version 0.9.2
[platform/upstream/iotivity.git] / android / android_api / base / jni / JniUtils.h
1 /*
2 * //******************************************************************
3 * //
4 * // Copyright 2015 Intel Corporation.
5 * //
6 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7 * //
8 * // Licensed under the Apache License, Version 2.0 (the "License");
9 * // you may not use this file except in compliance with the License.
10 * // You may obtain a copy of the License at
11 * //
12 * //      http://www.apache.org/licenses/LICENSE-2.0
13 * //
14 * // Unless required by applicable law or agreed to in writing, software
15 * // distributed under the License is distributed on an "AS IS" BASIS,
16 * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * // See the License for the specific language governing permissions and
18 * // limitations under the License.
19 * //
20 * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 */
22
23 #include "JniOcStack.h"
24 #include "OCRepresentation.h"
25
26 class JniUtils
27 {
28 public:
29     static void convertJavaMapToQueryParamsMap(JNIEnv *env, jobject hashMap,
30         OC::QueryParamsMap &map);
31     static jobject convertQueryParamsMapToJavaMap(JNIEnv *env, const OC::QueryParamsMap &map);
32
33     static jobject convertStrVectorToJavaStrList(JNIEnv *env, std::vector<std::string> &vector);
34     static void convertJavaStrArrToStrVector(JNIEnv *env, jobjectArray jStrArr,
35         std::vector<std::string> &vector);
36
37     static void convertJavaHeaderOptionsArrToVector(JNIEnv *env, jobjectArray jHeaderOptions,
38         OC::HeaderOptions& headerOptions);
39     static jobject convertHeaderOptionsVectorToJavaList(JNIEnv *env,
40         const OC::HeaderOptions& headerOptions);
41
42     static void convertJavaRepresentationArrToVector(JNIEnv *env,
43         jobjectArray jRepresentationArray,
44         std::vector<OC::OCRepresentation>& representationVector);
45     static jobjectArray convertRepresentationVectorToJavaArray(JNIEnv *env,
46         const std::vector<OC::OCRepresentation>& representationVector);
47
48     static OC::ServiceType getServiceType(JNIEnv *env, int type)
49     {
50         switch (type) {
51         case 0:
52             return OC::ServiceType::InProc;
53         case 1:
54             return OC::ServiceType::OutOfProc;
55         default:
56             ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected service type");
57             return OC::ServiceType::OutOfProc;
58         };
59     }
60
61     static OC::ModeType getModeType(JNIEnv *env, int type)
62     {
63         switch (type) {
64         case 0:
65             return OC::ModeType::Server;
66         case 1:
67             return OC::ModeType::Client;
68         case 2:
69             return OC::ModeType::Both;
70         default:
71             ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected mode type");
72             return OC::ModeType::Both;
73         };
74     }
75
76     static OC::QualityOfService getQOS(JNIEnv *env, int type)
77     {
78         switch (type) {
79         case 0:
80             return OC::QualityOfService::LowQos;
81         case 1:
82             return OC::QualityOfService::MidQos;
83         case 2:
84             return OC::QualityOfService::HighQos;
85         case 3:
86             return OC::QualityOfService::NaQos;
87         default:
88             ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected quality of service");
89             return OC::QualityOfService::NaQos;
90         };
91     }
92
93     static OC::ObserveType getObserveType(JNIEnv *env, int type)
94     {
95         switch (type) {
96         case 0:
97             return OC::ObserveType::Observe;
98         case 1:
99             return OC::ObserveType::ObserveAll;
100         default:
101             ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected observe type");
102             return OC::ObserveType::ObserveAll;
103         };
104     }
105
106     static OCEntityHandlerResult getOCEntityHandlerResult(JNIEnv *env, int type)
107     {
108         switch (type) {
109         case 0:
110             return OCEntityHandlerResult::OC_EH_OK;
111         case 1:
112             return OCEntityHandlerResult::OC_EH_ERROR;
113         case 2:
114             return OCEntityHandlerResult::OC_EH_RESOURCE_CREATED;
115         case 3:
116             return OCEntityHandlerResult::OC_EH_RESOURCE_DELETED;
117         case 4:
118             return OCEntityHandlerResult::OC_EH_SLOW;
119         case 5:
120             return OCEntityHandlerResult::OC_EH_FORBIDDEN;
121         default:
122             ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected OCEntityHandlerResult");
123             return OCEntityHandlerResult::OC_EH_ERROR;
124         };
125     }
126
127     static std::string stackResultToStr(const int result)
128     {
129         switch (result)
130         {
131         case OC_STACK_OK:
132             return "OK";
133         case OC_STACK_RESOURCE_CREATED:
134             return "RESOURCE_CREATED";
135         case OC_STACK_RESOURCE_DELETED:
136             return "RESOURCE_DELETED";
137         case OC_STACK_CONTINUE:
138             return "CONTINUE";
139             /* Success status code - END HERE */
140             /* Error status code - START HERE */
141         case OC_STACK_INVALID_URI:
142             return "INVALID_URI";
143         case OC_STACK_INVALID_QUERY:
144             return "INVALID_QUERY";
145         case OC_STACK_INVALID_IP:
146             return "INVALID_IP";
147
148         case OC_STACK_INVALID_PORT:
149             return "INVALID_PORT";
150         case OC_STACK_INVALID_CALLBACK:
151             return "INVALID_CALLBACK";
152         case OC_STACK_INVALID_METHOD:
153             return "INVALID_METHOD";
154         case OC_STACK_INVALID_PARAM:
155             return "INVALID_PARAM";
156         case OC_STACK_INVALID_OBSERVE_PARAM:
157             return "INVALID_OBSERVE_PARAM";
158         case OC_STACK_NO_MEMORY:
159             return "NO_MEMORY";
160         case OC_STACK_COMM_ERROR:
161             return "COMM_ERROR";
162         case OC_STACK_NOTIMPL:
163             return "NOTIMPL";
164         case OC_STACK_NO_RESOURCE:
165             return "NO_RESOURCE";
166         case  OC_STACK_RESOURCE_ERROR:
167             return "RESOURCE_ERROR";
168         case OC_STACK_SLOW_RESOURCE:
169             return "SLOW_RESOURCE";
170             //case OC_STACK_DUPLICATE_REQUEST:
171             //    return "DUPLICATE_REQUEST";
172         case OC_STACK_NO_OBSERVERS:
173             return "NO_OBSERVERS";
174         case OC_STACK_OBSERVER_NOT_FOUND:
175             return "OBSERVER_NOT_FOUND";
176         case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
177             return "VIRTUAL_DO_NOT_HANDLE";
178         case OC_STACK_INVALID_OPTION:
179             return "INVALID_OPTION";
180         case OC_STACK_MALFORMED_RESPONSE:
181             return "MALFORMED_RESPONSE";
182         case OC_STACK_PERSISTENT_BUFFER_REQUIRED:
183             return "PERSISTENT_BUFFER_REQUIRED";
184         case OC_STACK_INVALID_REQUEST_HANDLE:
185             return "INVALID_REQUEST_HANDLE";
186         case OC_STACK_INVALID_DEVICE_INFO:
187             return "INVALID_DEVICE_INFO";
188             //case OC_STACK_INVALID_JSON:
189             //    return "INVALID_JSON";
190
191         case OC_STACK_PRESENCE_STOPPED:
192             return "PRESENCE_STOPPED";
193         case OC_STACK_PRESENCE_TIMEOUT:
194             return "PRESENCE_TIMEOUT";
195         case OC_STACK_PRESENCE_DO_NOT_HANDLE:
196             return "PRESENCE_DO_NOT_HANDLE";
197
198         case OC_STACK_ERROR:
199             return "ERROR";
200
201         case JNI_EXCEPTION:
202             return "JNI_EXCEPTION";
203         case JNI_NO_NATIVE_POINTER:
204             return "JNI_NO_NATIVE_POINTER";
205         case JNI_INVALID_VALUE:
206             return "JNI_INVALID_VALUE";
207         default:
208             return "";
209         }
210     }
211 };