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 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
|
/*
*
* OBEX Server
*
* Copyright (C) 2009-2010 Intel Corporation
* Copyright (C) 2007-2010 Marcel Holtmann <marcel@holtmann.org>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <glib.h>
#include <stdlib.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <inttypes.h>
#include "gobex/gobex.h"
#include "gobex/gobex-apparam.h"
#include "obexd/src/obexd.h"
#include "obexd/src/plugin.h"
#include "obexd/src/log.h"
#include "obexd/src/obex.h"
#include "obexd/src/service.h"
#include "obexd/src/manager.h"
#include "obexd/src/mimetype.h"
#include "phonebook.h"
#include "filesystem.h"
#define PHONEBOOK_TYPE "x-bt/phonebook"
#define VCARDLISTING_TYPE "x-bt/vcard-listing"
#define VCARDENTRY_TYPE "x-bt/vcard"
#define ORDER_TAG 0x01
#define SEARCHVALUE_TAG 0x02
#define SEARCHATTRIB_TAG 0x03
#define MAXLISTCOUNT_TAG 0x04
#define LISTSTARTOFFSET_TAG 0x05
#define FILTER_TAG 0x06
#define FORMAT_TAG 0X07
#define PHONEBOOKSIZE_TAG 0X08
#define NEWMISSEDCALLS_TAG 0X09
struct cache {
gboolean valid;
uint32_t index;
GSList *entries;
};
struct cache_entry {
uint32_t handle;
char *id;
char *name;
char *sound;
char *tel;
};
struct pbap_session {
struct apparam_field *params;
char *folder;
uint32_t find_handle;
struct cache cache;
struct pbap_object *obj;
};
struct pbap_object {
GString *buffer;
GObexApparam *apparam;
gboolean firstpacket;
gboolean lastpart;
struct pbap_session *session;
void *request;
};
static const uint8_t PBAP_TARGET[TARGET_SIZE] = {
0x79, 0x61, 0x35, 0xF0, 0xF0, 0xC5, 0x11, 0xD8,
0x09, 0x66, 0x08, 0x00, 0x20, 0x0C, 0x9A, 0x66 };
typedef int (*cache_entry_find_f) (const struct cache_entry *entry,
const char *value);
static void cache_entry_free(void *data)
{
struct cache_entry *entry = data;
g_free(entry->id);
g_free(entry->name);
g_free(entry->sound);
g_free(entry->tel);
g_free(entry);
}
static gboolean entry_name_find(const struct cache_entry *entry,
const char *value)
{
char *name;
gboolean ret;
if (!entry->name)
return FALSE;
if (strlen(value) == 0)
return TRUE;
name = g_utf8_strdown(entry->name, -1);
ret = (g_strstr_len(name, -1, value) ? TRUE : FALSE);
g_free(name);
return ret;
}
static gboolean entry_sound_find(const struct cache_entry *entry,
const char *value)
{
if (!entry->sound)
return FALSE;
return (g_strstr_len(entry->sound, -1, value) ? TRUE : FALSE);
}
static gboolean entry_tel_find(const struct cache_entry *entry,
const char *value)
{
if (!entry->tel)
return FALSE;
return (g_strstr_len(entry->tel, -1, value) ? TRUE : FALSE);
}
static const char *cache_find(struct cache *cache, uint32_t handle)
{
GSList *l;
for (l = cache->entries; l; l = l->next) {
struct cache_entry *entry = l->data;
if (entry->handle == handle)
return entry->id;
}
return NULL;
}
static void cache_clear(struct cache *cache)
{
g_slist_free_full(cache->entries, cache_entry_free);
cache->entries = NULL;
}
static void phonebook_size_result(const char *buffer, size_t bufsize,
int vcards, int missed,
gboolean lastpart, void *user_data)
{
struct pbap_session *pbap = user_data;
uint16_t phonebooksize;
if (pbap->obj->request) {
phonebook_req_finalize(pbap->obj->request);
pbap->obj->request = NULL;
}
if (vcards < 0)
vcards = 0;
DBG("vcards %d", vcards);
phonebooksize = vcards;
pbap->obj->apparam = g_obex_apparam_set_uint16(NULL, PHONEBOOKSIZE_TAG,
phonebooksize);
pbap->obj->firstpacket = TRUE;
if (missed > 0) {
DBG("missed %d", missed);
pbap->obj->apparam = g_obex_apparam_set_uint16(
pbap->obj->apparam,
NEWMISSEDCALLS_TAG,
missed);
}
obex_object_set_io_flags(pbap->obj, G_IO_IN, 0);
}
static void query_result(const char *buffer, size_t bufsize, int vcards,
int missed, gboolean lastpart, void *user_data)
{
struct pbap_session *pbap = user_data;
DBG("");
if (pbap->obj->request && lastpart) {
phonebook_req_finalize(pbap->obj->request);
pbap->obj->request = NULL;
}
pbap->obj->lastpart = lastpart;
if (vcards < 0) {
obex_object_set_io_flags(pbap->obj, G_IO_ERR, -ENOENT);
return;
}
if (!pbap->obj->buffer)
pbap->obj->buffer = g_string_new_len(buffer, bufsize);
else
pbap->obj->buffer = g_string_append_len(pbap->obj->buffer,
buffer, bufsize);
if (missed > 0) {
DBG("missed %d", missed);
pbap->obj->firstpacket = TRUE;
pbap->obj->apparam = g_obex_apparam_set_uint16(
pbap->obj->apparam,
NEWMISSEDCALLS_TAG,
missed);
}
obex_object_set_io_flags(pbap->obj, G_IO_IN, 0);
}
static void cache_entry_notify(const char *id, uint32_t handle,
const char *name, const char *sound,
const char *tel, void *user_data)
{
struct pbap_session *pbap = user_data;
struct cache_entry *entry = g_new0(struct cache_entry, 1);
struct cache *cache = &pbap->cache;
if (handle != PHONEBOOK_INVALID_HANDLE)
entry->handle = handle;
else
entry->handle = ++pbap->cache.index;
entry->id = g_strdup(id);
entry->name = g_strdup(name);
entry->sound = g_strdup(sound);
entry->tel = g_strdup(tel);
cache->entries = g_slist_append(cache->entries, entry);
}
static int alpha_sort(gconstpointer a, gconstpointer b)
{
const struct cache_entry *e1 = a;
const struct cache_entry *e2 = b;
return g_strcmp0(e1->name, e2->name);
}
static int indexed_sort(gconstpointer a, gconstpointer b)
{
const struct cache_entry *e1 = a;
const struct cache_entry *e2 = b;
return (e1->handle - e2->handle);
}
static int phonetical_sort(gconstpointer a, gconstpointer b)
{
const struct cache_entry *e1 = a;
const struct cache_entry *e2 = b;
/* SOUND attribute is optional. Use Indexed sort if not present. */
if (!e1->sound || !e2->sound)
return indexed_sort(a, b);
return g_strcmp0(e1->sound, e2->sound);
}
static GSList *sort_entries(GSList *l, uint8_t order, uint8_t search_attrib,
const char *value)
{
GSList *sorted = NULL;
cache_entry_find_f find;
GCompareFunc sort;
char *searchval;
/*
* Default sorter is "Indexed". Some backends doesn't inform the index,
* for this case a sequential internal index is assigned.
* 0x00 = indexed
* 0x01 = alphanumeric
* 0x02 = phonetic
*/
switch (order) {
case 0x01:
sort = alpha_sort;
break;
case 0x02:
sort = phonetical_sort;
break;
default:
sort = indexed_sort;
break;
}
/*
* This implementation checks if the given field CONTAINS the
* search value(case insensitive). Name is the default field
* when the attribute is not provided.
*/
switch (search_attrib) {
/* Number */
case 1:
find = entry_tel_find;
break;
/* Sound */
case 2:
find = entry_sound_find;
break;
default:
find = entry_name_find;
break;
}
searchval = value ? g_utf8_strdown(value, -1) : NULL;
for (; l; l = l->next) {
struct cache_entry *entry = l->data;
if (searchval && !find(entry, (const char *) searchval))
continue;
sorted = g_slist_insert_sorted(sorted, entry, sort);
}
g_free(searchval);
return sorted;
}
static int generate_response(void *user_data)
{
struct pbap_session *pbap = user_data;
GSList *sorted;
GSList *l;
uint16_t max = pbap->params->maxlistcount;
DBG("");
if (max == 0) {
/* Ignore all other parameter and return PhoneBookSize */
uint16_t size = g_slist_length(pbap->cache.entries);
pbap->obj->apparam = g_obex_apparam_set_uint16(
pbap->obj->apparam,
PHONEBOOKSIZE_TAG,
size);
return 0;
}
/*
* Don't free the sorted list content: this list contains
* only the reference for the "real" cache entry.
*/
sorted = sort_entries(pbap->cache.entries, pbap->params->order,
pbap->params->searchattrib,
(const char *) pbap->params->searchval);
/* Computing offset considering first entry of the phonebook */
l = g_slist_nth(sorted, pbap->params->liststartoffset);
pbap->obj->buffer = g_string_new(VCARD_LISTING_BEGIN);
for (; l && max; l = l->next, max--) {
const struct cache_entry *entry = l->data;
char *escaped_name = g_markup_escape_text(entry->name, -1);
g_string_append_printf(pbap->obj->buffer,
VCARD_LISTING_ELEMENT, entry->handle, escaped_name);
g_free(escaped_name);
}
pbap->obj->buffer = g_string_append(pbap->obj->buffer,
VCARD_LISTING_END);
g_slist_free(sorted);
return 0;
}
static void cache_ready_notify(void *user_data)
{
struct pbap_session *pbap = user_data;
DBG("");
phonebook_req_finalize(pbap->obj->request);
pbap->obj->request = NULL;
pbap->cache.valid = TRUE;
generate_response(pbap);
obex_object_set_io_flags(pbap->obj, G_IO_IN, 0);
}
static void cache_entry_done(void *user_data)
{
struct pbap_session *pbap = user_data;
const char *id;
int ret;
DBG("");
pbap->cache.valid = TRUE;
id = cache_find(&pbap->cache, pbap->find_handle);
if (id == NULL) {
DBG("Entry %d not found on cache", pbap->find_handle);
obex_object_set_io_flags(pbap->obj, G_IO_ERR, -ENOENT);
return;
}
phonebook_req_finalize(pbap->obj->request);
pbap->obj->request = phonebook_get_entry(pbap->folder, id,
pbap->params, query_result, pbap, &ret);
if (ret < 0)
obex_object_set_io_flags(pbap->obj, G_IO_ERR, ret);
}
static struct apparam_field *parse_aparam(const uint8_t *buffer, uint32_t hlen)
{
GObexApparam *apparam;
struct apparam_field *param;
apparam = g_obex_apparam_decode(buffer, hlen);
if (apparam == NULL)
return NULL;
param = g_new0(struct apparam_field, 1);
/*
* As per spec when client doesn't include MAXLISTCOUNT_TAG then it
* should be assume as Maximum value in vcardlisting 65535
*/
param->maxlistcount = UINT16_MAX;
g_obex_apparam_get_uint8(apparam, ORDER_TAG, ¶m->order);
g_obex_apparam_get_uint8(apparam, SEARCHATTRIB_TAG,
¶m->searchattrib);
g_obex_apparam_get_uint8(apparam, FORMAT_TAG, ¶m->format);
g_obex_apparam_get_uint16(apparam, MAXLISTCOUNT_TAG,
¶m->maxlistcount);
g_obex_apparam_get_uint16(apparam, LISTSTARTOFFSET_TAG,
¶m->liststartoffset);
g_obex_apparam_get_uint64(apparam, FILTER_TAG, ¶m->filter);
param->searchval = g_obex_apparam_get_string(apparam, SEARCHVALUE_TAG);
DBG("o %x sa %x sv %s fil %" G_GINT64_MODIFIER "x for %x max %x off %x",
param->order, param->searchattrib, param->searchval,
param->filter, param->format, param->maxlistcount,
param->liststartoffset);
g_obex_apparam_free(apparam);
return param;
}
static void *pbap_connect(struct obex_session *os, int *err)
{
struct pbap_session *pbap;
manager_register_session(os);
pbap = g_new0(struct pbap_session, 1);
pbap->folder = g_strdup("/");
pbap->find_handle = PHONEBOOK_INVALID_HANDLE;
if (err)
*err = 0;
return pbap;
}
static int pbap_get(struct obex_session *os, void *user_data)
{
struct pbap_session *pbap = user_data;
const char *type = obex_get_type(os);
const char *name = obex_get_name(os);
struct apparam_field *params;
const uint8_t *buffer;
char *path;
ssize_t rsize;
int ret;
DBG("name %s type %s pbap %p", name, type, pbap);
if (type == NULL)
return -EBADR;
rsize = obex_get_apparam(os, &buffer);
if (rsize < 0) {
if (g_ascii_strcasecmp(type, VCARDENTRY_TYPE) != 0)
return -EBADR;
rsize = 0;
}
params = parse_aparam(buffer, rsize);
if (params == NULL)
return -EBADR;
if (pbap->params) {
g_free(pbap->params->searchval);
g_free(pbap->params);
}
pbap->params = params;
if (g_ascii_strcasecmp(type, PHONEBOOK_TYPE) == 0) {
/* Always contains the absolute path */
if (g_path_is_absolute(name))
path = g_strdup(name);
else
path = g_build_filename("/", name, NULL);
} else if (g_ascii_strcasecmp(type, VCARDLISTING_TYPE) == 0) {
/* Always relative */
if (!name || strlen(name) == 0) {
/* Current folder */
path = g_strdup(pbap->folder);
} else {
/* Current folder + relative path */
path = g_build_filename(pbap->folder, name, NULL);
/* clear cache */
pbap->cache.valid = FALSE;
pbap->cache.index = 0;
cache_clear(&pbap->cache);
}
} else if (g_ascii_strcasecmp(type, VCARDENTRY_TYPE) == 0) {
/* File name only */
path = g_strdup(name);
} else
return -EBADR;
if (path == NULL)
return -EBADR;
ret = obex_get_stream_start(os, path);
g_free(path);
return ret;
}
static int pbap_setpath(struct obex_session *os, void *user_data)
{
struct pbap_session *pbap = user_data;
const char *name;
const uint8_t *nonhdr;
char *fullname;
int err;
if (obex_get_non_header_data(os, &nonhdr) != 2) {
error("Set path failed: flag and constants not found!");
return -EBADMSG;
}
name = obex_get_name(os);
DBG("name %s folder %s nonhdr 0x%x%x", name, pbap->folder,
nonhdr[0], nonhdr[1]);
fullname = phonebook_set_folder(pbap->folder, name, nonhdr[0], &err);
if (err < 0)
return err;
g_free(pbap->folder);
pbap->folder = fullname;
/*
* FIXME: Define a criteria to mark the cache as invalid
*/
pbap->cache.valid = FALSE;
pbap->cache.index = 0;
cache_clear(&pbap->cache);
return 0;
}
static void pbap_disconnect(struct obex_session *os, void *user_data)
{
struct pbap_session *pbap = user_data;
manager_unregister_session(os);
if (pbap->obj)
pbap->obj->session = NULL;
if (pbap->params) {
g_free(pbap->params->searchval);
g_free(pbap->params);
}
cache_clear(&pbap->cache);
g_free(pbap->folder);
g_free(pbap);
}
static int pbap_chkput(struct obex_session *os, void *user_data)
{
/* Rejects all PUTs */
return -EBADR;
}
static struct obex_service_driver pbap = {
.name = "Phonebook Access server",
.service = OBEX_PBAP,
.target = PBAP_TARGET,
.target_size = TARGET_SIZE,
.connect = pbap_connect,
.get = pbap_get,
.setpath = pbap_setpath,
.disconnect = pbap_disconnect,
.chkput = pbap_chkput
};
static struct pbap_object *vobject_create(struct pbap_session *pbap,
void *request)
{
struct pbap_object *obj;
obj = g_new0(struct pbap_object, 1);
obj->session = pbap;
pbap->obj = obj;
obj->request = request;
return obj;
}
static void *vobject_pull_open(const char *name, int oflag, mode_t mode,
void *context, size_t *size, int *err)
{
struct pbap_session *pbap = context;
phonebook_cb cb;
int ret;
void *request;
DBG("name %s context %p maxlistcount %d", name, context,
pbap->params->maxlistcount);
if (oflag != O_RDONLY) {
ret = -EPERM;
goto fail;
}
if (name == NULL) {
ret = -EBADR;
goto fail;
}
if (pbap->params->maxlistcount == 0)
cb = phonebook_size_result;
else
cb = query_result;
request = phonebook_pull(name, pbap->params, cb, pbap, &ret);
if (ret < 0)
goto fail;
/* reading first part of results from backend */
ret = phonebook_pull_read(request);
if (ret < 0)
goto fail;
if (err)
*err = 0;
return vobject_create(pbap, request);
fail:
if (err)
*err = ret;
return NULL;
}
static int vobject_close(void *object)
{
struct pbap_object *obj = object;
DBG("");
if (obj->session)
obj->session->obj = NULL;
if (obj->buffer)
g_string_free(obj->buffer, TRUE);
if (obj->apparam)
g_obex_apparam_free(obj->apparam);
if (obj->request)
phonebook_req_finalize(obj->request);
g_free(obj);
return 0;
}
static void *vobject_list_open(const char *name, int oflag, mode_t mode,
void *context, size_t *size, int *err)
{
struct pbap_session *pbap = context;
struct pbap_object *obj = NULL;
int ret;
void *request;
if (name == NULL) {
ret = -EBADR;
goto fail;
}
DBG("name %s context %p valid %d", name, context, pbap->cache.valid);
if (oflag != O_RDONLY) {
ret = -EPERM;
goto fail;
}
/* PullvCardListing always get the contacts from the cache */
if (pbap->cache.valid) {
obj = vobject_create(pbap, NULL);
ret = generate_response(pbap);
} else {
request = phonebook_create_cache(name, cache_entry_notify,
cache_ready_notify, pbap, &ret);
if (ret == 0)
obj = vobject_create(pbap, request);
}
if (ret < 0)
goto fail;
if (err)
*err = 0;
return obj;
fail:
if (obj)
vobject_close(obj);
if (err)
*err = ret;
return NULL;
}
static void *vobject_vcard_open(const char *name, int oflag, mode_t mode,
void *context, size_t *size, int *err)
{
struct pbap_session *pbap = context;
const char *id;
uint32_t handle;
int ret;
void *request;
DBG("name %s context %p valid %d", name, context, pbap->cache.valid);
if (oflag != O_RDONLY) {
ret = -EPERM;
goto fail;
}
if (name == NULL || sscanf(name, "%u.vcf", &handle) != 1) {
ret = -EBADR;
goto fail;
}
if (pbap->cache.valid == FALSE) {
pbap->find_handle = handle;
request = phonebook_create_cache(pbap->folder,
cache_entry_notify, cache_entry_done, pbap, &ret);
goto done;
}
id = cache_find(&pbap->cache, handle);
if (!id) {
ret = -ENOENT;
goto fail;
}
request = phonebook_get_entry(pbap->folder, id, pbap->params,
query_result, pbap, &ret);
done:
if (ret < 0)
goto fail;
if (err)
*err = 0;
return vobject_create(pbap, request);
fail:
if (err)
*err = ret;
return NULL;
}
static ssize_t vobject_pull_get_next_header(void *object, void *buf, size_t mtu,
uint8_t *hi)
{
struct pbap_object *obj = object;
if (!obj->buffer && !obj->apparam)
return -EAGAIN;
*hi = G_OBEX_HDR_APPARAM;
if (obj->firstpacket) {
obj->firstpacket = FALSE;
return g_obex_apparam_encode(obj->apparam, buf, mtu);
}
return 0;
}
static ssize_t vobject_pull_read(void *object, void *buf, size_t count)
{
struct pbap_object *obj = object;
struct pbap_session *pbap = obj->session;
int len, ret;
DBG("buffer %p maxlistcount %d", obj->buffer,
pbap->params->maxlistcount);
if (!obj->buffer) {
if (pbap->params->maxlistcount == 0)
return -ENOSTR;
return -EAGAIN;
}
len = string_read(obj->buffer, buf, count);
if (len == 0 && !obj->lastpart) {
/* in case when buffer is empty and we know that more
* data is still available in backend, requesting new
* data part via phonebook_pull_read and returning
* -EAGAIN to suspend request for now */
ret = phonebook_pull_read(obj->request);
if (ret)
return -EPERM;
return -EAGAIN;
}
return len;
}
static ssize_t vobject_list_get_next_header(void *object, void *buf, size_t mtu,
uint8_t *hi)
{
struct pbap_object *obj = object;
struct pbap_session *pbap = obj->session;
/* Backend still busy reading contacts */
if (!pbap->cache.valid)
return -EAGAIN;
*hi = G_OBEX_HDR_APPARAM;
if (pbap->params->maxlistcount == 0)
return g_obex_apparam_encode(obj->apparam, buf, mtu);
return 0;
}
static ssize_t vobject_list_read(void *object, void *buf, size_t count)
{
struct pbap_object *obj = object;
struct pbap_session *pbap = obj->session;
DBG("valid %d maxlistcount %d", pbap->cache.valid,
pbap->params->maxlistcount);
if (pbap->params->maxlistcount == 0)
return -ENOSTR;
return string_read(obj->buffer, buf, count);
}
static ssize_t vobject_vcard_read(void *object, void *buf, size_t count)
{
struct pbap_object *obj = object;
DBG("buffer %p", obj->buffer);
if (!obj->buffer)
return -EAGAIN;
return string_read(obj->buffer, buf, count);
}
static struct obex_mime_type_driver mime_pull = {
.target = PBAP_TARGET,
.target_size = TARGET_SIZE,
.mimetype = "x-bt/phonebook",
.open = vobject_pull_open,
.close = vobject_close,
.read = vobject_pull_read,
.get_next_header = vobject_pull_get_next_header,
};
static struct obex_mime_type_driver mime_list = {
.target = PBAP_TARGET,
.target_size = TARGET_SIZE,
.mimetype = "x-bt/vcard-listing",
.open = vobject_list_open,
.close = vobject_close,
.read = vobject_list_read,
.get_next_header = vobject_list_get_next_header,
};
static struct obex_mime_type_driver mime_vcard = {
.target = PBAP_TARGET,
.target_size = TARGET_SIZE,
.mimetype = "x-bt/vcard",
.open = vobject_vcard_open,
.close = vobject_close,
.read = vobject_vcard_read,
};
static int pbap_init(void)
{
int err;
err = phonebook_init();
if (err < 0)
return err;
err = obex_mime_type_driver_register(&mime_pull);
if (err < 0)
goto fail_mime_pull;
err = obex_mime_type_driver_register(&mime_list);
if (err < 0)
goto fail_mime_list;
err = obex_mime_type_driver_register(&mime_vcard);
if (err < 0)
goto fail_mime_vcard;
err = obex_service_driver_register(&pbap);
if (err < 0)
goto fail_pbap_reg;
return 0;
fail_pbap_reg:
obex_mime_type_driver_unregister(&mime_vcard);
fail_mime_vcard:
obex_mime_type_driver_unregister(&mime_list);
fail_mime_list:
obex_mime_type_driver_unregister(&mime_pull);
fail_mime_pull:
phonebook_exit();
return err;
}
static void pbap_exit(void)
{
obex_service_driver_unregister(&pbap);
obex_mime_type_driver_unregister(&mime_pull);
obex_mime_type_driver_unregister(&mime_list);
obex_mime_type_driver_unregister(&mime_vcard);
phonebook_exit();
}
OBEX_PLUGIN_DEFINE(pbap, pbap_init, pbap_exit)
|