Imported Upstream version 0.9.2
[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 caLeGattServerServiceAddedCallback(int status,
56                                                                   BluetoothGattService service);
57
58     private native static void caLeGattServerCharacteristicReadRequestCallback(
59             BluetoothDevice device,
60             int requestId, int offset, BluetoothGattCharacteristic characteristic, byte[] data);
61
62     private native static void caLeGattServerCharacteristicWriteRequestCallback(
63             BluetoothDevice device, int requestId,
64             BluetoothGattCharacteristic characteristic, byte[] data, boolean preparedWrite,
65             boolean responseNeeded, int offset, byte[] value);
66
67     private native static void caLeGattServerDescriptorReadRequestCallback(
68             BluetoothDevice device, int requestId, int offset, BluetoothGattDescriptor descriptor);
69
70     public native static void caLeGattServerDescriptorWriteRequestCallback(
71             BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
72             boolean preparedWrite, boolean responseNeeded, int offset, byte[] value);
73
74     private native static void caLeGattServerExecuteWriteCallback(BluetoothDevice device,
75                                                                  int requestId, boolean execute);
76
77     private native static void caLeGattServerNotificationSentCallback(BluetoothDevice device,
78                                                                      int status);
79
80     // AdvertiseCallback
81     private native static void caLeAdvertiseStartSuccessCallback(
82             AdvertiseSettings settingsInEffect);
83
84     private native static void caLeAdvertiseStartFailureCallback(int errorCode);
85
86     private static final BluetoothGattServerCallback mGattServerCallback =
87                          new BluetoothGattServerCallback() {
88
89         @Override
90         public void onConnectionStateChange(BluetoothDevice device, int status,
91                 int newState) {
92             super.onConnectionStateChange(device, status, newState);
93
94             caLeGattServerConnectionStateChangeCallback(device, status, newState);
95         }
96
97         @Override
98         public void onServiceAdded(int status, BluetoothGattService service) {
99             super.onServiceAdded(status, service);
100
101             caLeGattServerServiceAddedCallback(status, service);
102         }
103
104         @Override
105         public void onCharacteristicReadRequest(
106                 BluetoothDevice device, int requestId, int offset,
107                 BluetoothGattCharacteristic characteristic) {
108             super.onCharacteristicReadRequest(device, requestId, offset, characteristic);
109
110             caLeGattServerCharacteristicReadRequestCallback(device, requestId, offset,
111                                                             characteristic,
112                                                             characteristic.getValue());
113         }
114
115         @Override
116         public void onCharacteristicWriteRequest(
117                 BluetoothDevice device, int requestId, BluetoothGattCharacteristic characteristic,
118                 boolean preparedWrite, boolean responseNeeded, int offset, byte[] value) {
119             super.onCharacteristicWriteRequest(device, requestId, characteristic,
120                     preparedWrite, responseNeeded, offset, value);
121
122             caLeGattServerCharacteristicWriteRequestCallback(device, requestId, characteristic,
123                                                              value, preparedWrite, responseNeeded,
124                                                              offset, value);
125         }
126
127         @Override
128         public void onDescriptorReadRequest(
129                 BluetoothDevice device,
130                 int requestId, int offset, BluetoothGattDescriptor descriptor) {
131             super.onDescriptorReadRequest(device, requestId, offset, descriptor);
132
133             caLeGattServerDescriptorReadRequestCallback(device, requestId, offset, descriptor);
134         }
135
136         @Override
137         public void onDescriptorWriteRequest(
138                 BluetoothDevice device, int requestId, BluetoothGattDescriptor descriptor,
139                 boolean preparedWrite, boolean responseNeeded, int offset,
140                 byte[] value) {
141             super.onDescriptorWriteRequest(device, requestId, descriptor, preparedWrite,
142                                            responseNeeded, offset, value);
143
144             caLeGattServerDescriptorWriteRequestCallback(device, requestId, descriptor,
145                                                          preparedWrite, responseNeeded, offset,
146                                                          value);
147         }
148
149         @Override
150         public void onExecuteWrite(BluetoothDevice device, int requestId, boolean execute) {
151             super.onExecuteWrite(device, requestId, execute);
152
153             caLeGattServerExecuteWriteCallback(device, requestId, execute);
154         }
155
156         @Override
157         public void onNotificationSent(BluetoothDevice device, int status) {
158             super.onNotificationSent(device, status);
159
160             caLeGattServerNotificationSentCallback(device, status);
161         }
162     };
163
164     private static final AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() {
165
166         @Override
167         public void onStartSuccess(AdvertiseSettings settingsInEffect) {
168             super.onStartSuccess(settingsInEffect);
169
170             caLeAdvertiseStartSuccessCallback(settingsInEffect);
171         }
172
173         @Override
174         public void onStartFailure(int errorCode) {
175             super.onStartFailure(errorCode);
176
177             caLeAdvertiseStartFailureCallback(errorCode);
178         }
179     };
180 }
181