05b33832a76fd7a6906a9f6bc255ab37f8363e22
[platform/upstream/iotivity.git] / service / simulator / java / jni / simulator_platform_info_jni.cpp
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 #include "simulator_platform_info_jni.h"
22 #include "simulator_utils_jni.h"
23 #include "jni_string.h"
24
25 extern SimulatorClassRefs gSimulatorClassRefs;
26
27 jobject JniPlatformInfo::toJava(PlatformInfo &platformInfo)
28 {
29     if (!m_env)
30         return nullptr;
31
32     static jmethodID platformInfoCtor = m_env->GetMethodID(gSimulatorClassRefs.platformInfoCls,
33                                         "<init>", "()V");
34     jobject jPlatformInfo = (jobject) m_env->NewObject(gSimulatorClassRefs.platformInfoCls,
35                             platformInfoCtor);
36     setFieldValue(jPlatformInfo, "mPlatformId", platformInfo.getPlatformID());
37     setFieldValue(jPlatformInfo, "mManufacturerName", platformInfo.getManufacturerName());
38     setFieldValue(jPlatformInfo, "mManufacturerUrl", platformInfo.getManufacturerUrl());
39     setFieldValue(jPlatformInfo, "mModelNumber", platformInfo.getModelNumber());
40     setFieldValue(jPlatformInfo, "mDateOfManufacture", platformInfo.getDateOfManfacture());
41     setFieldValue(jPlatformInfo, "mPlatformVersion", platformInfo.getPlatformVersion());
42     setFieldValue(jPlatformInfo, "mOperationSystemVersion", platformInfo.getOSVersion());
43     setFieldValue(jPlatformInfo, "mHardwareVersion", platformInfo.getHardwareVersion());
44     setFieldValue(jPlatformInfo, "mFirmwareVersion", platformInfo.getFirmwareVersion());
45     setFieldValue(jPlatformInfo, "mSupportUrl", platformInfo.getSupportUrl());
46     setFieldValue(jPlatformInfo, "mSystemTime", platformInfo.getSystemTime());
47
48     return jPlatformInfo;
49 }
50
51 PlatformInfo JniPlatformInfo::toCpp(jobject jPlatformInfo)
52 {
53     PlatformInfo platformInfo;
54     if (!m_env || !jPlatformInfo)
55         return platformInfo;
56
57     platformInfo.setPlatformID(getFieldValue(jPlatformInfo, "mPlatformId"));
58     platformInfo.setManufacturerName(getFieldValue(jPlatformInfo, "mManufacturerName"));
59     platformInfo.setManufacturerUrl(getFieldValue(jPlatformInfo, "mManufacturerUrl"));
60     platformInfo.setModelNumber(getFieldValue(jPlatformInfo, "mModelNumber"));
61     platformInfo.setDateOfManfacture(getFieldValue(jPlatformInfo, "mDateOfManufacture"));
62     platformInfo.setPlatformVersion(getFieldValue(jPlatformInfo, "mPlatformVersion"));
63     platformInfo.setOSVersion(getFieldValue(jPlatformInfo, "mOperationSystemVersion"));
64     platformInfo.setHardwareVersion(getFieldValue(jPlatformInfo, "mHardwareVersion"));
65     platformInfo.setFirmwareVersion(getFieldValue(jPlatformInfo, "mFirmwareVersion"));
66     platformInfo.setSupportUrl(getFieldValue(jPlatformInfo, "mSupportUrl"));
67     platformInfo.setSystemTime(getFieldValue(jPlatformInfo, "mSystemTime"));
68
69     return std::move(platformInfo);
70 }
71
72 void JniPlatformInfo::setFieldValue(jobject jPlatformInfo, const std::string &fieldName,
73                                     const std::string &value)
74 {
75     jfieldID fieldID = m_env->GetFieldID(m_env->GetObjectClass(jPlatformInfo), fieldName.c_str(),
76                                          "Ljava/lang/String;");
77     jstring valueStr = m_env->NewStringUTF(value.c_str());
78     m_env->SetObjectField(jPlatformInfo, fieldID, valueStr);
79 }
80
81 std::string JniPlatformInfo::getFieldValue(jobject jPlatformInfo, const std::string &fieldName)
82 {
83     jfieldID fieldID = m_env->GetFieldID(m_env->GetObjectClass(jPlatformInfo), fieldName.c_str(),
84                                          "Ljava/lang/String;");
85     jstring jvalue = (jstring) m_env->GetObjectField(jPlatformInfo, fieldID);
86     JniString value(m_env, jvalue);
87     return value.get();
88 }