Merge branch 'connectivity-abstraction' to master
[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
28 #include <string.h>
29 #include <bluetooth.h>
30 #include "caedrinterface.h"
31
32 #include "caadapterutils.h"
33 #include "caedrutils.h"
34 #include "logger.h"
35 #include "umutex.h"
36 #include "cacommon.h"
37 #include "caedrdevicelist.h"
38
39 static int32_t g_maxPendingConnections = 10;
40
41 CAResult_t CAEDRServerStart(const char *serviceUUID, int *serverFD, u_thread_pool_t handle)
42 {
43     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
44
45     VERIFY_NON_NULL(serviceUUID, EDR_ADAPTER_TAG, "Service UUID is null");
46     VERIFY_NON_NULL(serverFD, EDR_ADAPTER_TAG, "Server fd holder is null");
47
48     if (!serviceUUID[0])
49     {
50         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Invalid input: Empty service uuid!");
51         return CA_STATUS_INVALID_PARAM;
52     }
53
54     bool isRunning = false;
55     bt_error_e err = bt_adapter_is_service_used(serviceUUID, &isRunning);
56     if (BT_ERROR_NONE != err)
57     {
58         OIC_LOG_V(DEBUG, EDR_ADAPTER_TAG,
59                   "Unable to find whether service is already running or not! erorr num[%x]", err);
60         return CA_STATUS_FAILED;
61     }
62
63     if (isRunning)
64     {
65         OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service is already running with this UUID!");
66         return CA_SERVER_STARTED_ALREADY;
67     }
68
69     int socketFD = 0;
70     // Registers a rfcomm socket with a specific service_uuid.
71     err = bt_socket_create_rfcomm(serviceUUID, &socketFD);
72     if (BT_ERROR_NONE != err)
73     {
74         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed to create rfcomm socket!, error num [%x]",
75                   err);
76         return CA_STATUS_FAILED;
77     }
78
79     // Start listening and accepting
80     err = bt_socket_listen_and_accept_rfcomm(socketFD,
81                                 g_maxPendingConnections);
82     if (BT_ERROR_NONE != err)
83     {
84         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed in listen rfcomm socket!, error num [%x]",
85                   err);
86
87         bt_socket_destroy_rfcomm(socketFD);
88         return CA_STATUS_FAILED;
89     }
90
91     *serverFD = socketFD;
92
93     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
94     return CA_STATUS_OK;
95 }
96
97 CAResult_t CAEDRServerStop(int serverFD)
98 {
99     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
100
101     bt_error_e err = bt_socket_destroy_rfcomm(serverFD);
102     if (BT_ERROR_NONE != err)
103     {
104         OIC_LOG_V(ERROR, EDR_ADAPTER_TAG, "Failed close server socket!, error num [%x]",
105                   err);
106         return CA_STATUS_FAILED;
107     }
108
109     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
110     return CA_STATUS_OK;
111 }
112
113 void CAEDRServerTerminate()
114 {
115     // This is just a dummy
116     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "CAEDRServerTerminate");
117 }
118
119 CAResult_t CAEDRManagerReadData(void)
120 {
121     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
122     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
123     return CA_NOT_SUPPORTED;
124 }
125