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