2 * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "bluetooth-hid-agent.h"
21 static bt_hid_state_t hid_state;
23 static const char *__bt_hid_state2str(bt_hid_state_t state)
26 case BT_HID_STATE_UNAVAILABLE:
28 case BT_HID_STATE_DISCONNECTED:
29 return "DISCONNECTED";
30 case BT_HID_STATE_CONNECTING:
32 case BT_HID_STATE_CONNECTED:
34 case BT_HID_STATE_DISCONNECTING:
35 return "DISCONNECTING";
42 static gboolean __bt_hid_check_for_callpath(const char *call_sender)
44 GSList *s_list = app_list;
47 DBG_SECURE("sender is = %s\n", call_sender);
49 if (call_sender == NULL) {
51 ERR("Invalid Parameters");
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;
63 if (g_strcmp0(sender, call_sender) == 0) {
64 DBG("sender path and call path match... so return true");
68 s_list = s_list->next;
71 ERR("Sender [%s] is not registered", call_sender);
75 bt_hid_agent_error_t _bt_hid_register_application(gboolean register_flag,
81 return BT_HID_AGENT_ERROR_INVALID_PARAM;
83 DBG(" Requesting [%s]", register_flag ? "Register" : "Unregister");
84 DBG(" sender = %s", sender);
87 if (__bt_hid_check_for_callpath(sender))
88 return BT_HID_AGENT_ERROR_ALREADY_EXIST;
90 /* add call path to the senders list*/
91 sender_name = g_strdup(sender);
92 app_list = g_slist_append(app_list, sender_name);
94 return BT_HID_AGENT_ERROR_NONE;
96 /*remove the call from senders list */
97 GSList *s_list = app_list;
99 while (s_list != NULL) {
100 sender_name = s_list->data;
102 if (sender_name == NULL)
103 return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
105 if (g_strcmp0(sender_name, sender) == 0) {
106 app_list = g_slist_remove(app_list, sender_name);
109 if (app_list == NULL && hid_state == BT_HID_STATE_CONNECTED)
110 __bt_hid_disconnect_profile();
112 return BT_HID_AGENT_ERROR_NONE;
114 s_list = s_list->next;
117 return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
122 bt_hid_agent_error_t _bt_hid_register_application(gboolean register_flag,
123 const char *sender_name)
125 if (sender_name == NULL)
126 return BT_HID_AGENT_ERROR_INVALID_PARAM;
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;
134 /* set the call path to the sender */
135 sender = g_strdup(sender_name);
136 INFO("Registered [sender:%s]", sender_name);
139 return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
140 if (strcasecmp(sender, sender_name) != 0)
141 return BT_HID_AGENT_ERROR_NOT_AVAILABLE;
143 /* unset the call path to the sender */
146 if (hid_state == BT_HID_STATE_CONNECTED)
147 __bt_hid_disconnect_profile();
148 INFO("Unregistered [sender:%s]", sender_name);
151 return BT_HID_AGENT_ERROR_NONE;
155 void _bt_hid_set_profile_state(bt_hid_state_t new_state)
157 INFO_C("state changed [%s] =>[%s]", __bt_hid_state2str(hid_state),
158 __bt_hid_state2str(new_state));
159 hid_state = new_state;
162 case BT_HID_STATE_UNAVAILABLE:
163 case BT_HID_STATE_DISCONNECTED:
165 case BT_HID_STATE_CONNECTED:
167 case BT_HID_STATE_CONNECTING:
168 case BT_HID_STATE_DISCONNECTING:
174 bt_hid_state_t _bt_hid_get_profile_state(void)