replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / android / android_api / base / src / main / java / org / iotivity / ca / CaLeServerInterface.java
1 /******************************************************************
2  *
3  * Copyright 2014 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 package org.iotivity.ca;
22
23 import android.bluetooth.BluetoothDevice;
24 import android.bluetooth.BluetoothGattCharacteristic;
25 import android.bluetooth.BluetoothGattDescriptor;
26 import android.bluetooth.BluetoothGattServerCallback;
27 import android.bluetooth.BluetoothGattService;
28 import android.bluetooth.le.AdvertiseCallback;
29 import android.bluetooth.le.AdvertiseSettings;
30
31 public class CaLeServerInterface {
32
33     private CaLeServerInterface() {
34
35         caLeRegisterGattServerCallback(mGattServerCallback);
36         caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
37     }
38
39     public static void getLeGattServerCallback() {
40         caLeRegisterGattServerCallback(mGattServerCallback);
41     }
42
43     public static void getBluetoothLeAdvertiseCallback() {
44         caLeRegisterBluetoothLeAdvertiseCallback(mAdvertiseCallback);
45     }
46
47     private native static void caLeRegisterGattServerCallback(BluetoothGattServerCallback callback);
48
49     private native static void caLeRegisterBluetoothLeAdvertiseCallback(AdvertiseCallback callback);
50
51     // BluetoothGattServerCallback
52     private native static void caLeGattServerConnectionStateChangeCallback(
53             BluetoothDevice device, int status, int newState);
54
55     private native static void caLeGattServerNWConnectionStateChangeCallback(
56             BluetoothDevice device, int status, int newState);
57
58     private native static void caLeGattServerServiceAddedCallback(int status,
59                                                                   BluetoothGattService service);
60
61     private native static void caLeGattServerCharacteristicReadRequestCallback(
62             BluetoothDevice device, byte[] data);
63
64     private native static void caLeGattServerCharacteristicWriteRequestCallback(
65             BluetoothDevice device, byte[] data, int id, int offset, byte[] value);
66
67     private native static void caLeGattServerNotificationSentCallback(BluetoothDevice device,
68                                                                      int status);
69
70     private native static void caLeGattServerMtuChangedCallback(BluetoothDevice gatt, int mtu);
71
72     // AdvertiseCallback
73     private native static void caLeAdvertiseStartSuccessCallback(
74             AdvertiseSettings settingsInEffect);
75
76     private native static void caLeAdvertiseStartFailureCallback(int errorCode);
77
78     private static final BluetoothGattServerCallback mGattServerCallback =
79                          new BluetoothGattServerCallback() {
80
81         @Override
82         public void onConnectionStateChange(BluetoothDevice device, int status,
83                 int newState) {
84             super.onConnectionStateChange(device, status, newState);
85
86             caLeGattServerConnectionStateChangeCallback(device, status, newState);
87             caLeGattServerNWConnectionStateChangeCallback(device, status, newState);
88         }
89
90         @Override
91         public void onServiceAdded(int status, BluetoothGattService service) {
92             super.onServiceAdded(status, service);
93
94             caLeGattServerServiceAddedCallback(status, service);
95         }
96
97         @Override
98         public void onCharacteristicReadRequest(
99                 BluetoothDevice device, int requestId, int offset,
100                 BluetoothGattCharacteristic characteristic) {
101             super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
102
103             caLeGattServerCharacteristicReadRequestCallback(device, characteristic.getValue());
104         }
105
106         @Override
107         public void onCharacteristicWriteRequest(
108                 BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
109                 boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
110             super.onCharacteristicWriteRequest(device, requestId, characteristic,
111                     preparedWrite, responseNeeded, offset, value);
112
113             caLeGattServerCharacteristicWriteRequestCallback(device, value, requestId,
114                                                              offset, value);
115         }
116
117         @Override
118         public void onDescriptorReadRequest(
119                 BluetoothDevice device,
120                 int requestId, int offset, BluetoothGattDescriptor descriptor) {
121             super.onDescriptorReadRequest(device, requestId, offset, descriptor);
122         }
123
124         @Override
125         public void onDescriptorWriteRequest(
126                 BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
127                 boolean preparedWrite, boolean responseNeeded, int offset,
128                 byte[] value) {
129             super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
130                                            responseNeeded, offset, value);
131         }
132
133         @Override
134         public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
135             super.onExecuteWrite(device, requestId, execute);
136         }
137
138         @Override
139         public void onNotificationSent(BluetoothDevice device, int status) {
140             super.onNotificationSent(device, status);
141
142             caLeGattServerNotificationSentCallback(device, status);
143         }
144
145         @Override
146         public void onMtuChanged(BluetoothDevice device, int mtu) {
147             super.onMtuChanged(device, mtu);
148
149             caLeGattServerMtuChangedCallback(device, mtu);
150         }
151     };
152
153     private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
154
155         @Override
156         public void onStartSuccess(AdvertiseSettings settingsInEffect) {
157             super.onStartSuccess(settingsInEffect);
158
159             caLeAdvertiseStartSuccessCallback(settingsInEffect);
160         }
161
162         @Override
163         public void onStartFailure(int errorCode) {
164             super.onStartFailure(errorCode);
165
166             caLeAdvertiseStartFailureCallback(errorCode);
167         }
168     };
169 }
170