1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20 package com.example.resourcehostingsampleapp;
22 import java.net.Inet4Address;
23 import java.net.InetAddress;
24 import java.net.NetworkInterface;
25 import java.net.SocketException;
26 import java.util.Enumeration;
28 import org.iotivity.base.ModeType;
29 import org.iotivity.base.OcPlatform;
30 import org.iotivity.base.OcResourceHandle;
31 import org.iotivity.base.PlatformConfig;
32 import org.iotivity.base.QualityOfService;
33 import org.iotivity.base.ServiceType;
35 import android.app.Activity;
36 import android.os.Bundle;
37 import android.util.Log;
38 import android.view.View;
39 import android.view.View.OnClickListener;
40 import android.widget.TextView;
41 import android.widget.Toast;
44 * To execute resource hosting function for android sample application .
45 * @author Copyright 2015 Samsung Electronics All Rights Reserved.
46 * @see className class : ResourceHosting</br>
50 public class ResourceHosting extends Activity implements OnClickListener
52 private final int OCSTACK_OK = 0;
53 private final int OCSTACK_ERROR = 255;
54 private final int RESOURCEHOSTING_DO_NOT_THREADRUNNING = -2;
56 private String TAG = "ResourceHosting";
57 private OcResourceHandle mResourceHandle;
58 private String mIpAddress;
59 private TextView mLogTextView;
60 private String mLog = "";
62 * To initialize UI Function Setting.
63 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
64 * @see Method method : onCreate</br>
66 protected void onCreate(Bundle savedInstanceState)
68 super.onCreate(savedInstanceState);
69 setContentView(R.layout.activity_main);
70 mLogTextView = (TextView) findViewById(R.id.txtLog);
71 findViewById(R.id.btnStartHosting).setOnClickListener(this);
72 findViewById(R.id.btnStopHosting).setOnClickListener(this);
73 findViewById(R.id.btLogClear).setOnClickListener(this);
75 PlatformConfig platformConfigObj;
77 platformConfigObj = new PlatformConfig(this,ServiceType.IN_PROC,
78 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
80 Log.i(TAG, "Before Calling Configure of ocPlatform");
81 OcPlatform.Configure(platformConfigObj);
82 Log.i(TAG, "Configuration done Successfully");
86 * To execute initOICStack for running Resource hosting.
87 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
88 * @see Method method : onStart</br>
91 protected void onStart()
98 * To terminate Resource hosting.
99 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
100 * @see Method method : onStop</br>
103 protected void onStop()
107 result = ResourceHostingTerminate();
108 Log.d(TAG, "ResourceHostingTerminate : "+ result);
111 protected void onResume()
117 * To execute initOICStack for running Resource hosting.
118 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
119 * @see Method method : onRestart</br>
122 protected void onRestart()
129 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
130 * @see Method method : onDestroy</br>
132 protected void onDestroy()
138 * get IpAddress and execute resourceHostingInit() method.
139 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
140 * @see Method method : initOICStack</br>
142 private void initOICStack()
146 mIpAddress = getIpAddress();
148 result = ResourceHostingInit(mIpAddress);
149 Log.d(TAG, "ResourceHostingInit : " + result);
158 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
159 * @see Method method : getIpAddress</br>
161 private String getIpAddress()
165 for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces();
166 en.hasMoreElements();)
168 NetworkInterface intf = (NetworkInterface) en.nextElement();
169 for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();)
171 InetAddress inetAddress = (InetAddress) enumIpAddr.nextElement();
172 if (!inetAddress.isLoopbackAddress())
174 if (inetAddress instanceof Inet4Address)
175 return inetAddress.getHostAddress().toString();
180 catch (SocketException e)
188 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
189 * @see Method method : onClick</br>
190 * @param v view to choice
192 public void onClick(View v)
194 int getId = v.getId();
198 case R.id.btnStartHosting:
202 result = OICCoordinatorStart();
203 Log.d(TAG, "OICCoordinatorStart : " + result);
207 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
210 case R.id.btnStopHosting:
212 result = OICCoordinatorStop();
213 Log.d(TAG, "OICCoordinatorStop : "+ result);
215 case R.id.btLogClear:
224 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
225 * @see Method method : clearLog</br>
227 private void clearLog()
230 mLogTextView.setText(mLog);
234 * recieve the callback log message.
235 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
236 * @see Method method : cbMessage</br>
237 * @param msg callback log message
239 public void cbMessage(String msg)
242 mLogTextView.setText(mLog);
246 * jni function - OicCorrdinatorstart() method.
247 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
248 * @see Method method : OICCoordinatorStart</br>
249 * @see Signature signature : ()V</br>
251 public native int OICCoordinatorStart();
254 * jni function - OICCoordinatorStop() method.
255 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
256 * @see Method method : OICCoordinatorStop</br>
257 * @see signature signature : ()V</br>
259 public native int OICCoordinatorStop();
262 * jni function - ResourceHostingInit() method in order to execute OICCoordinatorStart() method.
263 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
264 * @see Method method : ResourceHostingInit</br>
265 * @param addr ipAddress
266 * @see signature signature : (Ljava/lang/String;)V</br>
268 public native int ResourceHostingInit(String addr);
271 * jni function - ResourceHostingTerminate() method in order to terminate resource hosting
272 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
273 * @see Method method : ResourceHostingTerminate</br>
274 * @see signature signature : ()V</br>
276 public native int ResourceHostingTerminate();
280 System.loadLibrary("gnustl_shared");
281 System.loadLibrary("oc_logger");
282 System.loadLibrary("connectivity_abstraction");
283 System.loadLibrary("ca-interface");
284 System.loadLibrary("octbstack");
285 System.loadLibrary("oc");
286 System.loadLibrary("ocstack-jni");
287 System.loadLibrary("NotificationManager");