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 final String TAG = "NMResourceHosting : " + this.getClass().getSimpleName();
57 private TextView mLogTextView;
58 private String mLog = "";
60 * To initialize UI Function Setting.
61 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
62 * @see Method method : onCreate</br>
64 protected void onCreate(Bundle savedInstanceState)
66 super.onCreate(savedInstanceState);
67 setContentView(R.layout.activity_main);
68 mLogTextView = (TextView) findViewById(R.id.txtLog);
69 findViewById(R.id.btnStartHosting).setOnClickListener(this);
70 findViewById(R.id.btnStopHosting).setOnClickListener(this);
71 findViewById(R.id.btLogClear).setOnClickListener(this);
73 PlatformConfig platformConfigObj;
75 platformConfigObj = new PlatformConfig(this,ServiceType.IN_PROC,
76 ModeType.CLIENT_SERVER, "0.0.0.0", 0, QualityOfService.LOW);
78 Log.i(TAG, "Before Calling Configure of ocPlatform");
79 OcPlatform.Configure(platformConfigObj);
80 Log.i(TAG, "Configuration done Successfully");
84 * To execute initOICStack for running Resource hosting.
85 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
86 * @see Method method : onStart</br>
89 protected void onStart()
95 * To terminate Resource hosting.
96 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
97 * @see Method method : onStop</br>
100 protected void onStop()
105 protected void onResume()
111 * To execute initOICStack for running Resource hosting.
112 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
113 * @see Method method : onRestart</br>
116 protected void onRestart()
122 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
123 * @see Method method : onDestroy</br>
125 protected void onDestroy()
129 result = OICCoordinatorStop();
130 Log.d(TAG, "OICCoordinatorStop() : "+ result);
134 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
135 * @see Method method : onClick</br>
136 * @param v view to choice
138 public void onClick(View v)
140 int getId = v.getId();
144 case R.id.btnStartHosting:
148 result = OICCoordinatorStart();
149 Log.d(TAG, "OICCoordinatorStart : " + result);
153 Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
154 Log.d(TAG, e.getMessage() + result);
157 case R.id.btnStopHosting:
159 result = OICCoordinatorStop();
160 Log.d(TAG, "OICCoordinatorStop : "+ result);
162 case R.id.btLogClear:
171 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
172 * @see Method method : clearLog</br>
174 private void clearLog()
177 mLogTextView.setText(mLog);
178 Log.i(TAG, "Log textbox is cleared");
182 * recieve the callback log message.
183 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
184 * @see Method method : cbMessage</br>
185 * @param msg callback log message
187 public void cbMessage(String msg)
190 mLogTextView.setText(mLog);
195 * jni function - OicCorrdinatorstart() method.
196 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
197 * @see Method method : OICCoordinatorStart</br>
198 * @see Signature signature : ()V</br>
200 public native int OICCoordinatorStart();
203 * jni function - OICCoordinatorStop() method.
204 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
205 * @see Method method : OICCoordinatorStop</br>
206 * @see signature signature : ()V</br>
208 public native int OICCoordinatorStop();
211 * jni function - ResourceHostingInit() method in order to execute OICCoordinatorStart() method.
212 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
213 * @see Method method : ResourceHostingInit</br>
214 * @param addr ipAddress
215 * @see signature signature : (Ljava/lang/String;)V</br>
217 public native int ResourceHostingInit(String addr);
220 * jni function - ResourceHostingTerminate() method in order to terminate resource hosting
221 * @see Class class : com_example_resourcehostingsampleapp_ResourceHosting</br>
222 * @see Method method : ResourceHostingTerminate</br>
223 * @see signature signature : ()V</br>
225 public native int ResourceHostingTerminate();
229 System.loadLibrary("gnustl_shared");
230 System.loadLibrary("oc_logger");
231 System.loadLibrary("connectivity_abstraction");
232 System.loadLibrary("ca-interface");
233 System.loadLibrary("octbstack");
234 System.loadLibrary("oc");
235 System.loadLibrary("ocstack-jni");
236 System.loadLibrary("NotificationManager");