72df325fcfa2729e90e7ebc706cd72b3ff012c43
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_edr_adapter / tizen / caedrutils.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 helper functions for EDR adapter.
25  */
26
27 #include "caedrutils.h"
28
29 #include <bluetooth.h>
30
31 #include "logger.h"
32 #include "uarraylist.h"
33
34 /**
35  * Pending devices list for data transfer.
36  */
37 static u_arraylist_t *g_deviceStateList = NULL;
38
39 bool CAEDRIsServiceSupported(const char **serviceUUID, int32_t serviceCount,
40                                  const char *matchService)
41 {
42     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "IN");
43
44     if (NULL == serviceUUID || 0 == serviceCount || NULL == matchService)
45     {
46         OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Invalid input");
47         return false;
48     }
49
50     for (int i = 0; i < serviceCount; i++)
51     {
52         if (!strcasecmp(serviceUUID[i], matchService))
53         {
54             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "Service found !");
55             return true;
56         }
57     }
58
59     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "OUT");
60     return false;
61 }
62
63 CAConnectedDeviceInfo_t *CAEDRGetDeviceInfoFromAddress(const char *remoteAddress)
64 {
65     OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "CAEDRGetDeviceInfoFromAddress");
66
67     if (!remoteAddress)
68     {
69         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "remoteAddress is null");
70         return NULL;
71     }
72
73     uint32_t length = u_arraylist_length(g_deviceStateList);
74     for (uint32_t index = 0; index < length; index++)
75     {
76         CAConnectedDeviceInfo_t* deviceInfo =
77                 (CAConnectedDeviceInfo_t*) u_arraylist_get(g_deviceStateList, index);
78         if (!deviceInfo)
79         {
80             OIC_LOG(DEBUG, EDR_ADAPTER_TAG, "deviceInfo object is null");
81             continue;
82         }
83
84         if (!strcmp((const char*) deviceInfo->address, remoteAddress))
85         {
86             return deviceInfo;
87         }
88     }
89     return NULL;
90 }
91
92 CAResult_t CAEDRAddDeviceInfoToList(const char *remoteAddress, CAConnectedDeviceInfo_t *deviceInfo)
93 {
94     if (!remoteAddress)
95     {
96         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "remoteAddress is null");
97         return CA_STATUS_INVALID_PARAM;
98     }
99
100     if (!deviceInfo)
101     {
102         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "deviceInfo is null");
103         return CA_STATUS_INVALID_PARAM;
104     }
105
106     for (int i = 0; i < CA_MACADDR_SIZE; i++)
107     {
108         deviceInfo->address[i] = (uint8_t) remoteAddress[i];
109     }
110
111     if (!g_deviceStateList)
112     {
113         g_deviceStateList = u_arraylist_create();
114         if (!g_deviceStateList)
115         {
116             OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to create Device list");
117             return CA_STATUS_FAILED;
118         }
119     }
120
121     if (!u_arraylist_add(g_deviceStateList, (void *) deviceInfo))
122     {
123         OIC_LOG(ERROR, EDR_ADAPTER_TAG, "Failed to Add Device to list");
124         return CA_STATUS_FAILED;
125     }
126
127     return CA_STATUS_OK;
128 }