Blob: ccp.c

Blob id: 8aa537b044e13b6d6a773645b420161bedca13ef

Size: 4.9 KB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
// SPDX-License-Identifier: GPL-2.0-or-later
/*
 *
 *  BlueZ - Bluetooth protocol stack for Linux
 *
 *  Copyright (C) 2024  Intel Corporation. All rights reserved.
 *
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _GNU_SOURCE

#include <ctype.h>
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>

#include <glib.h>

#include "gdbus/gdbus.h"

#include "bluetooth/bluetooth.h"
#include "bluetooth/hci.h"
#include "bluetooth/sdp.h"
#include "bluetooth/uuid.h"

#include "src/dbus-common.h"
#include "src/shared/util.h"
#include "src/shared/att.h"
#include "src/shared/queue.h"
#include "src/shared/gatt-db.h"
#include "src/shared/gatt-client.h"
#include "src/shared/gatt-server.h"
#include "src/shared/ccp.h"

#include "btio/btio.h"
#include "src/plugin.h"
#include "src/adapter.h"
#include "src/gatt-database.h"
#include "src/device.h"
#include "src/profile.h"
#include "src/service.h"
#include "src/log.h"
#include "src/error.h"

#define GTBS_UUID_STR "0000184C-0000-1000-8000-00805f9b34fb"

struct ccp_data {
	struct btd_device *device;
	struct btd_service *service;
	struct bt_ccp *ccp;
	unsigned int state_id;
};

static void ccp_debug(const char *str, void *user_data)
{
	DBG_IDX(0xffff, "%s", str);
}

static struct ccp_data *ccp_data_new(struct btd_device *device)
{
	struct ccp_data *data;

	data = new0(struct ccp_data, 1);
	data->device = device;

	return data;
}

static int ccp_probe(struct btd_service *service)
{
	struct btd_device *device = btd_service_get_device(service);
	struct btd_adapter *adapter = device_get_adapter(device);
	struct btd_gatt_database *database = btd_adapter_get_database(adapter);
	struct ccp_data *data = btd_service_get_user_data(service);
	char addr[18];

	ba2str(device_get_address(device), addr);
	DBG("%s", addr);

	if (data) {
		error("Profile probed twice for the same device!");
		return -EINVAL;
	}

	data = ccp_data_new(device);
	data->service = service;

	data->ccp = bt_ccp_new(btd_gatt_database_get_db(database),
			       btd_device_get_gatt_db(device));

	bt_ccp_set_debug(data->ccp, ccp_debug, NULL, NULL);
	btd_service_set_user_data(service, data);

	return 0;
}

static void ccp_data_free(struct ccp_data *data)
{
	if (data->service) {
		btd_service_set_user_data(data->service, NULL);
		bt_ccp_set_user_data(data->ccp, NULL);
	}

	bt_ccp_unref(data->ccp);
	free(data);
}

static void ccp_data_remove(struct ccp_data *data)
{
	DBG("data %p", data);

	ccp_data_free(data);
}

static void ccp_remove(struct btd_service *service)
{
	struct btd_device *device = btd_service_get_device(service);
	struct ccp_data *data;
	char addr[18];

	ba2str(device_get_address(device), addr);
	DBG("%s", addr);

	data = btd_service_get_user_data(service);
	if (!data) {
		error("CCP service not handled by profile");
		return;
	}

	ccp_data_remove(data);
}

static int ccp_accept(struct btd_service *service)
{
	struct btd_device *device = btd_service_get_device(service);
	struct bt_gatt_client *client = btd_device_get_gatt_client(device);
	struct ccp_data *data = btd_service_get_user_data(service);
	char addr[18];

	ba2str(device_get_address(device), addr);
	DBG("%s", addr);

	if (!bt_ccp_attach(data->ccp, client)) {
		error("CCP unable to attach");
		return -EINVAL;
	}

	/* TODO: register telephony operations here */

	btd_service_connecting_complete(service, 0);

	return 0;
}

static int ccp_connect(struct btd_service *service)
{
	struct btd_device *device = btd_service_get_device(service);
	char addr[18];

	ba2str(device_get_address(device), addr);
	DBG("%s", addr);

	return 0;
}

static int ccp_disconnect(struct btd_service *service)
{
	struct btd_device *device = btd_service_get_device(service);
	struct ccp_data *data = btd_service_get_user_data(service);
	char addr[18];

	ba2str(device_get_address(device), addr);
	DBG("%s", addr);

	bt_ccp_detach(data->ccp);

	btd_service_disconnecting_complete(service, 0);

	return 0;
}

static int ccp_server_probe(struct btd_profile *p, struct btd_adapter *adapter)
{
	struct btd_gatt_database *database = btd_adapter_get_database(adapter);

	bt_ccp_register(btd_gatt_database_get_db(database));

	return 0;
}

static void
ccp_server_remove(struct btd_profile *p,
		  struct btd_adapter *adapter)
{
	DBG("CCP remove adapter");
}

static struct btd_profile ccp_profile = {
	.name		= "ccp",
	.priority	= BTD_PROFILE_PRIORITY_MEDIUM,
	.remote_uuid	= GTBS_UUID_STR,
	.device_probe	= ccp_probe,
	.device_remove	= ccp_remove,
	.accept		= ccp_accept,
	.connect	= ccp_connect,
	.disconnect	= ccp_disconnect,
	.adapter_probe	= ccp_server_probe,
	.adapter_remove = ccp_server_remove,
	.testing	= true,
};

static int ccp_init(void)
{
	return btd_profile_register(&ccp_profile);
}

static void ccp_exit(void)
{
	btd_profile_unregister(&ccp_profile);
}

BLUETOOTH_PLUGIN_DEFINE(ccp, VERSION, BLUETOOTH_PLUGIN_PRIORITY_DEFAULT,
			ccp_init, ccp_exit)