To add connection manager for Tizen BLE Server
[platform/upstream/iotivity.git] / resource / csdk / connectivity / util / src / camanager / tizen / caleconnectionmanager.c
1 /* ****************************************************************
2  *
3  * Copyright 2016 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 #include <bluetooth.h>
22 #include <bluetooth_type.h>
23 #include <bluetooth_internal.h>
24
25 #include "camanagerleinterface.h"
26 #include "cacommon.h"
27 #include "caleserver.h"
28 #include "cagattservice.h"
29 #include "logger.h"
30
31 #define TAG "OIC_CA_MANAGER_TZ_LE"
32
33 static CAAdapterStateChangedCB g_adapterStateCB = NULL;
34 static CAConnectionStateChangedCB g_connStateCB = NULL;
35
36 static void CAManagerAdapterMonitorHandler(const CAEndpoint_t *info, CANetworkStatus_t status);
37 static void CAManagerConnectionMonitorHandler(CATransportAdapter_t adapter,
38                                               const char *remoteAddress, bool connected);
39
40 void CASetLENetworkMonitorCallbacks(CAAdapterStateChangedCB adapterStateCB,
41                                     CAConnectionStateChangedCB connStateCB)
42 {
43     OIC_LOG(DEBUG, TAG, "CASetLENetworkMonitorCallbacks");
44
45     g_adapterStateCB = adapterStateCB;
46     CASetNetworkMonitorCallback(CAManagerAdapterMonitorHandler);
47
48     g_connStateCB = connStateCB;
49     CASetLEConnectionStateChangedCallback(CAManagerConnectionMonitorHandler);
50 }
51
52 void CAStartServerLEAdvertising()
53 {
54     OIC_LOG(DEBUG, TAG, "CAStartServerLEAdvertising");
55
56     CAResult_t res = CALEStartAdvertise(CA_GATT_SERVICE_UUID);
57     if (CA_STATUS_OK != res)
58     {
59         OIC_LOG_V(ERROR, TAG, "Failed to start le advertising [%d]", res);
60         return;
61     }
62 }
63
64 void CAStopServerLEAdvertising()
65 {
66     OIC_LOG(DEBUG, TAG, "CAStopServerLEAdvertising");
67
68     CAResult_t res = CALEStopAdvertise();
69     if (CA_STATUS_OK != res)
70     {
71         OIC_LOG_V(ERROR, TAG, "Failed to stop le advertising [%d]", res);
72         return;
73     }
74 }
75
76 CAResult_t CASetLEClientAutoConnectionDeviceInfo(const char * address)
77 {
78     OIC_LOG(DEBUG, TAG, "CASetLEClientAutoConnectionDeviceInfo");
79     return CA_NOT_SUPPORTED;
80 }
81
82 CAResult_t CAUnsetLEClientAutoConnectionDeviceInfo(const char * address)
83 {
84     OIC_LOG(DEBUG, TAG, "CAUnsetLEClientAutoConnectionDeviceInfo");
85     return CA_NOT_SUPPORTED;
86 }
87
88 static void CAManagerAdapterMonitorHandler(const CAEndpoint_t *info, CANetworkStatus_t status)
89 {
90     (void)info;
91     (void)status;
92
93     if (CA_INTERFACE_DOWN == status)
94     {
95         g_adapterStateCB(info->adapter, false);
96         OIC_LOG(DEBUG, TAG, "Pass the disabled adapter state to upper layer");
97     }
98     else if (CA_INTERFACE_UP == status)
99     {
100         g_adapterStateCB(info->adapter, true);
101         OIC_LOG(DEBUG, TAG, "Pass the enabled adapter state to upper layer");
102     }
103 }
104
105 static void CAManagerConnectionMonitorHandler(CATransportAdapter_t adapter,
106                                               const char *remoteAddress, bool connected)
107 {
108     if (!remoteAddress)
109     {
110         OIC_LOG(ERROR, TAG, "remoteAddress is NULL");
111         return;
112     }
113
114     if (connected)
115     {
116         if (g_connStateCB)
117         {
118             g_connStateCB(CA_ADAPTER_GATT_BTLE, remoteAddress, true);
119             OIC_LOG(DEBUG, TAG, "Pass the connected device info to upper layer");
120
121             // stop le advertising
122             CAStopServerLEAdvertising();
123         }
124     }
125     else
126     {
127         if (g_connStateCB)
128         {
129             g_connStateCB(CA_ADAPTER_GATT_BTLE, remoteAddress, false);
130             OIC_LOG(DEBUG, TAG, "Pass the disconnected device info to upper layer");
131
132             // start le advertising to receive new connection request.
133             CAStartServerLEAdvertising();
134         }
135     }
136 }