replace : iotivity -> iotivity-sec
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / tizen / caedrserver.c
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 /**
22  * @file
23  *
24  * This file provides the APIs to start and stop RFCOMM server.
25  */
26
27 #include <string.h>
28 #include <bluetooth.h>
29
30 #include "caedrinterface.h"
31 #include "caadapterutils.h"
32 #include "caedrutils.h"
33 #include "logger.h"
34 #include "octhread.h"
35 #include "cacommon.h"
36 #include "caedrdevicelist.h"
37
38 static int32_t g_maxPendingConnections = 10;
39
40 /**
41  * Storing RfcommserverUUID
42  */
43 static int g_serverFD = -1;
44
45 CAResult_t CAEDRServerStart()
46 {
47     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
48
49     if(-1 != g_serverFD)
50     {
51         OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "%s Already running", __func__);
52         return CA_STATUS_OK;
53     }
54
55     bool isRunning = false;
56     bt_error_e err = bt_adapter_is_service_used(OIC_EDR_SERVICE_ID, &isRunning);
57     if (BT_ERROR_NONE != err)
58     {
59         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG,
60                   "Unable to find whether service is already running or not! error num[%x]", err);
61         return CA_STATUS_FAILED;
62     }
63
64     if (isRunning)
65     {
66         OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already running with this UUID!");
67         return CA_SERVER_STARTED_ALREADY;
68     }
69
70     int socketFD = 0;
71     // Registers a rfcomm socket with a specific service_uuid.
72     err = bt_socket_create_rfcomm(OIC_EDR_SERVICE_ID, &socketFD);
73     if (BT_ERROR_NONE != err)
74     {
75         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to create rfcomm socket!, error num [%x]", err);
76         return CA_STATUS_FAILED;
77     }
78
79     // Start listening and accepting
80     err = bt_socket_listen_and_accept_rfcomm(socketFD, g_maxPendingConnections);
81     if (BT_ERROR_NONE != err)
82     {
83         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed in listen rfcomm socket!, error num [%x]", err);
84
85         bt_socket_destroy_rfcomm(socketFD);
86         return CA_STATUS_FAILED;
87     }
88
89     g_serverFD = socketFD;
90
91     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
92     return CA_STATUS_OK;
93 }
94
95 CAResult_t CAEDRServerStop()
96 {
97     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
98
99     if (-1 < g_serverFD)
100     {
101         bt_error_e err = bt_socket_destroy_rfcomm(g_serverFD);
102
103         if (BT_ERROR_NONE != err)
104         {
105             OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close server socket!, error num [%x]", err);
106             return CA_STATUS_FAILED;
107         }
108         g_serverFD = -1;
109     }
110
111     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
112     return CA_STATUS_OK;
113 }
114
115 CAResult_t CAEDRServerInitialize(ca_thread_pool_t handle)
116 {
117     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "CAEDRServerInitialize");
118     return CA_STATUS_OK;
119 }
120
121 void CAEDRServerTerminate()
122 {
123     // This is just a dummy
124     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "CAEDRServerTerminate");
125 }
126
127 CAResult_t CAEDRManagerReadData(void)
128 {
129     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
130     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
131     return CA_NOT_SUPPORTED;
132 }