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