Tizen, Android BT , BLE optimization - making similar to wifi, eth adapters
[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    cabtserver.c
23  * @brief   This    file provides the APIs to start and stop RFCOMM server.
24  */
25
26
27 #include <string.h>
28 #include <bluetooth.h>
29 #include "caedrinterface.h"
30
31 #include "caadapterutils.h"
32 #include "caedrutils.h"
33 #include "logger.h"
34 #include "umutex.h"
35 #include "cacommon.h"
36 #include "caedrdevicelist.h"
37
38 static int32_t gMaxPendingConnections = 10;
39
40 CAResult_t CAEDRServerStart(const char *serviceUUID, int32_t *serverFD)
41 {
42     OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
43
44     bt_error_e err = BT_ERROR_NONE;
45     bool isRunning = false;
46     int32_t socketFD;
47
48     VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
49     VERIFY_NON_NULL(serverFD, EDR_ADAPTER_TAG, "Server fd holder is null");
50
51     if (0 >= strlen(serviceUUID))
52     {
53         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid!");
54         return CA_STATUS_INVALID_PARAM;
55     }
56
57     if (BT_ERROR_NONE != bt_adapter_is_service_used(serviceUUID, &isRunning))
58     {
59         OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG,
60                   "Unable to find whether service is already running or not!");
61         return CA_STATUS_FAILED;
62     }
63
64     if (true == isRunning)
65     {
66         OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "Service is already running with this UUID!");
67         return CA_SERVER_STARTED_ALREADY;
68     }
69
70     // Registers a rfcomm socket with a specific service_uuid .
71     if (BT_ERROR_NONE != (err = bt_socket_create_rfcomm(serviceUUID, &socketFD)))
72     {
73         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to create rfcomm socket!, error num [%x]",
74                   err);
75         return CA_STATUS_FAILED;
76     }
77
78     // Start listening and accepting
79     if (BT_ERROR_NONE != (err = bt_socket_listen_and_accept_rfcomm(socketFD,
80                                 gMaxPendingConnections)))
81     {
82         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed in listen rfcomm socket!, error num [%x]",
83                   err);
84
85         bt_socket_destroy_rfcomm(socketFD);
86         return CA_STATUS_FAILED;
87     }
88
89     *serverFD = socketFD;
90
91     OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
92     return CA_STATUS_OK;
93 }
94
95 CAResult_t CAEDRServerStop(const int32_t serverFD)
96 {
97     OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
98
99     bt_error_e err = BT_ERROR_NONE;
100     if (BT_ERROR_NONE != (err = bt_socket_destroy_rfcomm(serverFD)))
101     {
102         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close server socket!, error num [%x]",
103                   err);
104         return CA_STATUS_FAILED;
105     }
106
107     OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
108     return CA_STATUS_OK;
109 }
110
111 CAResult_t CAEDRManagerReadData(void)
112 {
113     OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "IN");
114     OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG, "OUT");
115     return CA_NOT_SUPPORTED;
116 }