74e48e6dc0f22eeeee9ea134cf851ea2c8aba81a
[iotivity.git] / java / 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         {
52             case 0:
53                 return OC::ServiceType::InProc;
54             case 1:
55                 return OC::ServiceType::OutOfProc;
56             default:
57                 ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected service type");
58                 return OC::ServiceType::OutOfProc;
59         };
60     }
61
62     static OC::ModeType getModeType(JNIEnv *env, int type)
63     {
64         switch (type)
65         {
66             case 0:
67                 return OC::ModeType::Server;
68             case 1:
69                 return OC::ModeType::Client;
70             case 2:
71                 return OC::ModeType::Both;
72             case 3:
73                 return OC::ModeType::Gateway;
74             default:
75                 ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected mode type");
76                 return OC::ModeType::Both;
77         };
78     }
79
80     static OC::QualityOfService getQOS(JNIEnv *env, int type)
81     {
82         switch (type)
83         {
84             case 0:
85                 return OC::QualityOfService::LowQos;
86             case 1:
87                 return OC::QualityOfService::MidQos;
88             case 2:
89                 return OC::QualityOfService::HighQos;
90             case 3:
91                 return OC::QualityOfService::NaQos;
92             default:
93                 ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected quality of service");
94                 return OC::QualityOfService::NaQos;
95         };
96     }
97
98     static OC::ObserveType getObserveType(JNIEnv *env, int type)
99     {
100         switch (type)
101         {
102             case 0:
103                 return OC::ObserveType::Observe;
104             case 1:
105                 return OC::ObserveType::ObserveAll;
106             default:
107                 ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected observe type");
108                 return OC::ObserveType::ObserveAll;
109         };
110     }
111
112     static OCEntityHandlerResult getOCEntityHandlerResult(JNIEnv *env, int type)
113     {
114         switch (type)
115         {
116             case 0:
117                 return OCEntityHandlerResult::OC_EH_OK;
118             case 1:
119                 return OCEntityHandlerResult::OC_EH_ERROR;
120             case 2:
121                 return OCEntityHandlerResult::OC_EH_RESOURCE_CREATED;
122             case 3:
123                 return OCEntityHandlerResult::OC_EH_RESOURCE_DELETED;
124             case 4:
125                 return OCEntityHandlerResult::OC_EH_SLOW;
126             case 5:
127                 return OCEntityHandlerResult::OC_EH_FORBIDDEN;
128             case 6:
129                 return OCEntityHandlerResult::OC_EH_RESOURCE_NOT_FOUND;
130             default:
131                 ThrowOcException(OC_STACK_INVALID_PARAM, "Unexpected OCEntityHandlerResult");
132                 return OCEntityHandlerResult::OC_EH_ERROR;
133         };
134     }
135
136     static std::string stackResultToStr(const int result)
137     {
138         switch (result)
139         {
140             /** Success status code - START HERE.*/
141             case OC_STACK_OK:
142                 return "OK";
143             case OC_STACK_RESOURCE_CREATED:
144                 return "RESOURCE_CREATED";
145             case OC_STACK_RESOURCE_DELETED:
146                 return "RESOURCE_DELETED";
147             case OC_STACK_RESOURCE_CHANGED:
148                 return "RESOURCE_CHANGED";
149             case OC_STACK_CONTINUE:
150                 return "CONTINUE";
151             /* Error status code - START HERE */
152             case OC_STACK_INVALID_URI:
153                 return "INVALID_URI";
154             case OC_STACK_INVALID_QUERY:
155                 return "INVALID_QUERY";
156             case OC_STACK_INVALID_IP:
157                 return "INVALID_IP";
158             case OC_STACK_INVALID_PORT:
159                 return "INVALID_PORT";
160             case OC_STACK_INVALID_CALLBACK:
161                 return "INVALID_CALLBACK";
162             case OC_STACK_INVALID_METHOD:
163                 return "INVALID_METHOD";
164             /** Invalid parameter.*/
165             case OC_STACK_INVALID_PARAM:
166                 return "INVALID_PARAM";
167             case OC_STACK_INVALID_OBSERVE_PARAM:
168                 return "INVALID_OBSERVE_PARAM";
169             case OC_STACK_NO_MEMORY:
170                 return "NO_MEMORY";
171             case OC_STACK_COMM_ERROR:
172                 return "COMM_ERROR";
173             case OC_STACK_TIMEOUT:
174                 return "TIMEOUT";
175             case OC_STACK_ADAPTER_NOT_ENABLED:
176                 return "ADAPTER_NOT_ENABLED";
177             case OC_STACK_NOTIMPL:
178                 return "NOTIMPL";
179             /** Resource not found.*/
180             case OC_STACK_NO_RESOURCE:
181                 return "NO_RESOURCE";
182             /** e.g: not supported method or interface.*/
183             case  OC_STACK_RESOURCE_ERROR:
184                 return "RESOURCE_ERROR";
185             case OC_STACK_SLOW_RESOURCE:
186                 return "SLOW_RESOURCE";
187             case OC_STACK_DUPLICATE_REQUEST:
188                 return "DUPLICATE_REQUEST";
189             /** Resource has no registered observers.*/
190             case OC_STACK_NO_OBSERVERS:
191                 return "NO_OBSERVERS";
192             case OC_STACK_OBSERVER_NOT_FOUND:
193                 return "OBSERVER_NOT_FOUND";
194             case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
195                 return "VIRTUAL_DO_NOT_HANDLE";
196             case OC_STACK_INVALID_OPTION:
197                 return "INVALID_OPTION";
198             /** The remote reply contained malformed data.*/
199             case OC_STACK_MALFORMED_RESPONSE:
200                 return "MALFORMED_RESPONSE";
201             case OC_STACK_PERSISTENT_BUFFER_REQUIRED:
202                 return "PERSISTENT_BUFFER_REQUIRED";
203             case OC_STACK_INVALID_REQUEST_HANDLE:
204                 return "INVALID_REQUEST_HANDLE";
205             case OC_STACK_INVALID_DEVICE_INFO:
206                 return "INVALID_DEVICE_INFO";
207             case OC_STACK_INVALID_JSON:
208                 return "INVALID_JSON";
209             /** Request is not authorized by Resource Server. */
210             case OC_STACK_UNAUTHORIZED_REQ:
211                 return "UNAUTHORIZED_REQ";
212             /** Error code from PDM */
213             case OC_STACK_PDM_IS_NOT_INITIALIZED:
214                 return "PDM_IS_NOT_INITIALIZED";
215             case OC_STACK_DUPLICATE_UUID:
216                 return "DUPLICATE_UUID";
217             case OC_STACK_INCONSISTENT_DB:
218                 return "INCONSISTENT_DB";
219             /** Error code from OTM */
220             case OC_STACK_AUTHENTICATION_FAILURE:
221                 return "AUTHENTICATION_FAILURE";
222             /** Insert all new error codes here!.*/
223 #ifdef WITH_PRESENCE
224             case OC_STACK_PRESENCE_STOPPED:
225                 return "PRESENCE_STOPPED";
226             case OC_STACK_PRESENCE_TIMEOUT:
227                 return "PRESENCE_TIMEOUT";
228             case OC_STACK_PRESENCE_DO_NOT_HANDLE:
229                 return "PRESENCE_DO_NOT_HANDLE";
230 #endif
231             case OC_STACK_ERROR:
232                 return "ERROR";
233
234             case JNI_EXCEPTION:
235                 return "JNI_EXCEPTION";
236             case JNI_NO_NATIVE_POINTER:
237                 return "JNI_NO_NATIVE_POINTER";
238             case JNI_INVALID_VALUE:
239                 return "JNI_INVALID_VALUE";
240             default:
241                 return "";
242         }
243     }
244 };