Merge branch 'windows-port'
[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         {
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_CONTINUE:
148                 return "CONTINUE";
149             /* Error status code - START HERE */
150             case OC_STACK_INVALID_URI:
151                 return "INVALID_URI";
152             case OC_STACK_INVALID_QUERY:
153                 return "INVALID_QUERY";
154             case OC_STACK_INVALID_IP:
155                 return "INVALID_IP";
156             case OC_STACK_INVALID_PORT:
157                 return "INVALID_PORT";
158             case OC_STACK_INVALID_CALLBACK:
159                 return "INVALID_CALLBACK";
160             case OC_STACK_INVALID_METHOD:
161                 return "INVALID_METHOD";
162             /** Invalid parameter.*/
163             case OC_STACK_INVALID_PARAM:
164                 return "INVALID_PARAM";
165             case OC_STACK_INVALID_OBSERVE_PARAM:
166                 return "INVALID_OBSERVE_PARAM";
167             case OC_STACK_NO_MEMORY:
168                 return "NO_MEMORY";
169             case OC_STACK_COMM_ERROR:
170                 return "COMM_ERROR";
171             case OC_STACK_TIMEOUT:
172                 return "TIMEOUT";
173             case OC_STACK_ADAPTER_NOT_ENABLED:
174                 return "ADAPTER_NOT_ENABLED";
175             case OC_STACK_NOTIMPL:
176                 return "NOTIMPL";
177             /** Resource not found.*/
178             case OC_STACK_NO_RESOURCE:
179                 return "NO_RESOURCE";
180             /** e.g: not supported method or interface.*/
181             case  OC_STACK_RESOURCE_ERROR:
182                 return "RESOURCE_ERROR";
183             case OC_STACK_SLOW_RESOURCE:
184                 return "SLOW_RESOURCE";
185             case OC_STACK_DUPLICATE_REQUEST:
186                 return "DUPLICATE_REQUEST";
187             /** Resource has no registered observers.*/
188             case OC_STACK_NO_OBSERVERS:
189                 return "NO_OBSERVERS";
190             case OC_STACK_OBSERVER_NOT_FOUND:
191                 return "OBSERVER_NOT_FOUND";
192             case OC_STACK_VIRTUAL_DO_NOT_HANDLE:
193                 return "VIRTUAL_DO_NOT_HANDLE";
194             case OC_STACK_INVALID_OPTION:
195                 return "INVALID_OPTION";
196             /** The remote reply contained malformed data.*/
197             case OC_STACK_MALFORMED_RESPONSE:
198                 return "MALFORMED_RESPONSE";
199             case OC_STACK_PERSISTENT_BUFFER_REQUIRED:
200                 return "PERSISTENT_BUFFER_REQUIRED";
201             case OC_STACK_INVALID_REQUEST_HANDLE:
202                 return "INVALID_REQUEST_HANDLE";
203             case OC_STACK_INVALID_DEVICE_INFO:
204                 return "INVALID_DEVICE_INFO";
205             case OC_STACK_INVALID_JSON:
206                 return "INVALID_JSON";
207             /** Request is not authorized by Resource Server. */
208             case OC_STACK_UNAUTHORIZED_REQ:
209                 return "UNAUTHORIZED_REQ";
210             /** Error code from PDM */
211             case OC_STACK_PDM_IS_NOT_INITIALIZED:
212                 return "PDM_IS_NOT_INITIALIZED";
213             case OC_STACK_DUPLICATE_UUID:
214                 return "DUPLICATE_UUID";
215             case OC_STACK_INCONSISTENT_DB:
216                 return "INCONSISTENT_DB";
217             /** Error code from OTM */
218             case OC_STACK_AUTHENTICATION_FAILURE:
219                 return "AUTHENTICATION_FAILURE";
220             /** Insert all new error codes here!.*/
221 #ifdef WITH_PRESENCE
222             case OC_STACK_PRESENCE_STOPPED:
223                 return "PRESENCE_STOPPED";
224             case OC_STACK_PRESENCE_TIMEOUT:
225                 return "PRESENCE_TIMEOUT";
226             case OC_STACK_PRESENCE_DO_NOT_HANDLE:
227                 return "PRESENCE_DO_NOT_HANDLE";
228 #endif
229             case OC_STACK_ERROR:
230                 return "ERROR";
231
232             case JNI_EXCEPTION:
233                 return "JNI_EXCEPTION";
234             case JNI_NO_NATIVE_POINTER:
235                 return "JNI_NO_NATIVE_POINTER";
236             case JNI_INVALID_VALUE:
237                 return "JNI_INVALID_VALUE";
238             default:
239                 return "";
240         }
241     }
242 };