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