replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaInterface.java
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 package org.iotivity.ca;
24
25 import android.content.Context;
26 import android.app.Activity;
27 import android.bluetooth.BluetoothDevice;
28 import org.iotivity.base.OcException;
29 import org.iotivity.base.OcConnectivityType;
30
31 import java.util.EnumSet;
32
33 public class CaInterface {
34     static {
35         System.loadLibrary("connectivity_abstraction");
36         System.loadLibrary("ca-interface");
37     }
38     private static volatile boolean isConnectionManagerInitialized = false;
39     private static volatile boolean isBtPairingInitialized = false;
40
41     public static native void initialize(Activity activity, Context context);
42
43     /**
44      *  Method start connection manager service.
45      *  this method has to be called before other API call.
46      *  @param context                                application context
47      *  @param onConnectionManagerStateListener       connection state callback listener
48      */
49     public synchronized static void startManagerService(Context context,
50             OnConnectionManagerStateListener onConnectionManagerStateListener) {
51         if (!isConnectionManagerInitialized) {
52             CaInterface.caManagerInitialize(context, onConnectionManagerStateListener);
53             isConnectionManagerInitialized = true;
54         }
55     }
56
57     /**
58      *  Method stop connection manager service.
59      *  this method must be called, when Application is destroied.
60      */
61     public synchronized static void stopManagerService() {
62         if (isConnectionManagerInitialized) {
63             CaInterface.caManagerTerminate();
64             isConnectionManagerInitialized = false;
65         }
66     }
67
68     /**
69      *  Method set device information for Auto-Connection.
70      *  this method has to be called before FindResource is called.
71      *  @param address                      LE address of scanned bluetooth device.
72      */
73     public synchronized static void setAutoConnectionDevice(String address)
74             throws OcException {
75         CaInterface.initCheckForConnectionManager();
76         CaInterface.caManagerSetAutoConnectionDeviceInfo(address);
77     }
78
79     /**
80      *  Method unset device information for Auto-Connection.
81      *  @param address                      LE address of scanned bluetooth device.
82      */
83     public synchronized static void unsetAutoConnectionDevice(String address)
84             throws OcException {
85         CaInterface.initCheckForConnectionManager();
86         CaInterface.caManagerUnsetAutoConnectionDeviceInfo(address);
87     }
88
89     /**
90      *  Interface for connection manager state listener.
91      *  Event listeners are notified asynchronously.
92      */
93     public interface OnConnectionManagerStateListener {
94         public void onAdapterStateChanged(OcConnectivityType type, boolean enabled);
95         public void onConnectionStateChanged(OcConnectivityType type, String address,
96                 boolean connected);
97     }
98
99     private static void initCheckForConnectionManager() {
100         if (!isConnectionManagerInitialized) {
101             throw new IllegalStateException("ConnectionManager must be started by making "
102                     + "a call to CaInterface.startManagerService before any other API "
103                     + "calls are permitted");
104         }
105     }
106
107     private static native void caManagerInitialize(Context context,
108             OnConnectionManagerStateListener onConnectionManagerStateListener);
109     private static native void caManagerTerminate();
110     private static native void caManagerSetAutoConnectionDeviceInfo(String address);
111     private static native void caManagerUnsetAutoConnectionDeviceInfo(String address);
112
113     /**
114      *  start bluetooth pairing service.
115      *  @param context                      application context
116      */
117     public synchronized static void startBtPairingService(Context context,
118             OnBtDeviceFoundListener listener) {
119         if (!isBtPairingInitialized) {
120             CaInterface.caBtPairingInitialize(context, listener);
121
122             isBtPairingInitialized = true;
123         }
124     }
125
126     /**
127      *  stop bluetooth pairing service.
128      */
129     public synchronized static void stopBtPairingService() {
130         if (isBtPairingInitialized) {
131             CaInterface.caBtPairingTerminate();
132
133             isBtPairingInitialized = false;
134         }
135     }
136
137     /**
138      *  start bluetooth device scan.
139      */
140     public synchronized static void startScan()
141             throws OcException {
142         CaInterface.initCheckForBtPairingUtil();
143         CaInterface.caBtPairingStartScan();
144     }
145
146     /**
147      *  stop bluetooth device scan.
148      */
149     public synchronized static void stopScan()
150             throws OcException {
151         CaInterface.initCheckForBtPairingUtil();
152         CaInterface.caBtPairingStopScan();
153     }
154
155     /**
156      *  create bond
157      */
158     public synchronized static void createBond(BluetoothDevice device)
159             throws OcException {
160         CaInterface.initCheckForBtPairingUtil();
161         CaInterface.caBtPairingCreateBond(device);
162     }
163
164     public interface OnBtDeviceFoundListener {
165         public void onBtDeviceFound(BluetoothDevice device) throws OcException;
166     }
167
168     private static void initCheckForBtPairingUtil() {
169         if (!isBtPairingInitialized) {
170             throw new IllegalStateException("BT pairing Util must be started by making "
171                     + "a call to CaInterface.startBtPairingService before any other API "
172                     + "calls are permitted");
173         }
174     }
175
176     private static native void caBtPairingInitialize(Context context,
177             OnBtDeviceFoundListener listener);
178     private static native void caBtPairingTerminate();
179     private static native void caBtPairingStartScan();
180     private static native void caBtPairingStopScan();
181     private static native void caBtPairingCreateBond(BluetoothDevice device);
182
183     /**
184      *  set BLE scan interval time and working count.
185      *  scanning logic (start scan -> stop scan) will be worked repeatly for workingCount.
186      *  and if you choose '0' value for workingCount parameter,
187      *  scanning will be worked continually as interval time.
188      *  @param intervalTime                  interval time(Seconds).
189      *  @param workingCount                  working count with interval time.
190      */
191     public synchronized static void setLeScanIntervalTime(int intervalTime, int workingCount){
192         CaInterface.setLeScanIntervalTimeImpl(intervalTime, workingCount);
193     }
194     private static native void setLeScanIntervalTimeImpl(int intervalTime, int workingCount);
195
196     /**
197      *  stop BLE scan.
198      *  if you want to start scan, it can be triggered by setLeScanIntervalTime or
199      *  other request API like findResource.
200      */
201     public synchronized static void stopLeScan(){
202         CaInterface.stopLeScanImpl();
203     }
204     private static native void stopLeScanImpl();
205
206     /**
207      *  start BLE Advertising.
208      */
209     public synchronized static void startLeAdvertising(){
210         CaInterface.startLeAdvertisingImpl();
211     }
212     private static native void startLeAdvertisingImpl();
213
214     /**
215      *  stop BLE Advertising.
216      */
217     public synchronized static void stopLeAdvertising(){
218         CaInterface.stopLeAdvertisingImpl();
219     }
220     private static native void stopLeAdvertisingImpl();
221
222     /**
223      *  set BT configure
224      */
225     public synchronized static void setBTConfigure(int flag){
226         CaInterface.setBTConfigureImpl(flag);
227     }
228     private static native void setBTConfigureImpl(int flag);
229
230     public synchronized static int setCipherSuite(OicCipher cipher, OcConnectivityType connType){
231         return CaInterface.setCipherSuiteImpl(cipher.getValue(), connType.getValue());
232     }
233     private static native int setCipherSuiteImpl(int cipher, int adapter);
234
235     /**
236     *  Disconnect TCP session.
237     *  It will disconnect the current TCP session.
238     *
239     *  @param address host address [IP address].
240     *  @param port Port number.
241     *  @param transportFlags Set of Transport flags.
242     *
243     *  @return Result of the API call.
244     *
245     *  @see CaTransportFlags
246     */
247     public synchronized static int disconnectTCPSession(String address, int port,
248                                      EnumSet<CaTransportFlags> transportFlags) {
249         int transPortFlagsInt = 0;
250         for (CaTransportFlags flag : CaTransportFlags.values()) {
251             if (transportFlags.contains(flag)) {
252                 transPortFlagsInt |= flag.getValue();
253             }
254         }
255
256         return CaInterface.disconnectTCPSessionImpl(address, port, transPortFlagsInt);
257     }
258     private static native int disconnectTCPSessionImpl(String address, int port, int flags);
259 }