f101b32f02b21d9feea148fb2beb283c2b29085e
[platform/core/connectivity/bluetooth-agent.git] / hid-agent / bluetooth-hid-manager.c
1 /*
2  * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include "bluetooth-hid-agent.h"
19
20 static char *sender;
21 static bt_hid_state_t hid_state;
22
23 static const char *__bt_hid_state2str(bt_hid_state_t state)
24 {
25         switch (state) {
26         case BT_HID_STATE_UNAVAILABLE:
27                 return "UNAVAILABLE";
28         case BT_HID_STATE_DISCONNECTED:
29                 return "DISCONNECTED";
30         case BT_HID_STATE_CONNECTING:
31                 return "CONNECTING";
32         case BT_HID_STATE_CONNECTED:
33                 return "CONNECTED";
34         case BT_HID_STATE_DISCONNECTING:
35                 return "DISCONNECTING";
36         }
37
38         return NULL;
39 }
40
41 #if 0
42 static gboolean __bt_hid_check_for_callpath(const char *call_sender)
43 {
44         GSList *s_list = app_list;
45         char *sender;
46
47         DBG_SECURE("sender is  = %s\n", call_sender);
48
49         if (call_sender == NULL) {
50
51                 ERR("Invalid Parameters");
52                 return FALSE;
53         }
54
55         /*check if the call is already registered*/
56         DBG("Checking if the call is already registered");
57         while (s_list != NULL) {
58                 sender = s_list->data;
59
60                 if (sender == NULL)
61                         break;
62
63                 if (g_strcmp0(sender, call_sender) == 0) {
64                         DBG("sender path and call path match... so return true");
65                         return TRUE;
66                 }
67
68                 s_list = s_list->next;
69         }
70
71         ERR("Sender [%s] is not registered", call_sender);
72         return FALSE;
73 }
74
75 bt_hid_agent_error_t _bt_hid_register_application(gboolean register_flag,
76                 const char *sender)
77 {
78         char *sender_name;
79
80         if (sender == NULL)
81                 return BT_HID_AGENT_ERROR_INVALID_PARAM;
82
83         DBG(" Requesting [%s]", register_flag ? "Register" : "Unregister");
84         DBG(" sender = %s", sender);
85
86         if (register_flag) {
87                 if (__bt_hid_check_for_callpath(sender))
88                         return BT_HID_AGENT_ERROR_ALREADY_EXIST;
89
90                 /* add call path to the senders list*/
91                 sender_name = g_strdup(sender);
92                 app_list = g_slist_append(app_list, sender_name);
93
94                 return BT_HID_AGENT_ERROR_NONE;
95         } else {
96                 /*remove the call from senders list */
97                 GSList *s_list = app_list;
98
99                 while (s_list != NULL) {
100                         sender_name = s_list->data;
101
102                         if (sender_name == NULL)
103                                 return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
104
105                         if (g_strcmp0(sender_name, sender) == 0) {
106                                 app_list = g_slist_remove(app_list, sender_name);
107                                 g_free(sender_name);
108
109                                 if (app_list == NULL && hid_state == BT_HID_STATE_CONNECTED)
110                                         __bt_hid_disconnect_profile();
111
112                                 return BT_HID_AGENT_ERROR_NONE;
113                         }
114                         s_list = s_list->next;
115                 }
116
117                 return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
118         }
119 }
120
121 #else
122 bt_hid_agent_error_t _bt_hid_register_application(gboolean register_flag,
123                 const char *sender_name)
124 {
125         if (sender_name == NULL)
126                 return BT_HID_AGENT_ERROR_INVALID_PARAM;
127
128         if (register_flag == TRUE) {
129                 if (sender != NULL) {
130                         ERR("Already registered [sender:%s]", sender_name);
131                         return BT_HID_AGENT_ERROR_ALREADY_EXIST;
132                 }
133
134                 /* set the call path to the sender */
135                 sender = g_strdup(sender_name);
136                 INFO("Registered [sender:%s]", sender_name);
137         } else {
138                 if (sender == NULL)
139                         return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
140                 if (strcasecmp(sender, sender_name) != 0)
141                         return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
142
143                 /* unset the call path to the sender */
144                 g_free(sender);
145                 sender = NULL;
146                 if (hid_state == BT_HID_STATE_CONNECTED)
147                         __bt_hid_disconnect_profile();
148                 INFO("Unregistered [sender:%s]", sender_name);
149         }
150
151         return BT_HID_AGENT_ERROR_NONE;
152 }
153 #endif
154
155 void _bt_hid_set_profile_state(bt_hid_state_t new_state)
156 {
157         INFO_C("state changed [%s] =>[%s]", __bt_hid_state2str(hid_state),
158                                         __bt_hid_state2str(new_state));
159         hid_state = new_state;
160
161         switch (new_state) {
162         case BT_HID_STATE_UNAVAILABLE:
163         case BT_HID_STATE_DISCONNECTED:
164                 break;
165         case BT_HID_STATE_CONNECTED:
166                 break;
167         case BT_HID_STATE_CONNECTING:
168         case BT_HID_STATE_DISCONNECTING:
169         default:
170                 break;
171         }
172 }
173
174 bt_hid_state_t _bt_hid_get_profile_state(void)
175 {
176         return hid_state;
177 }