spp.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2003-2010 Marcel Holtmann <marcel@holtmann.org>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #include <stdio.h>
  14. #include <errno.h>
  15. #include <unistd.h>
  16. #include <signal.h>
  17. #include <sys/socket.h>
  18. #include "lib/bluetooth.h"
  19. #include "lib/rfcomm.h"
  20. #include "lib/sdp.h"
  21. #include "lib/sdp_lib.h"
  22. #include "cups.h"
  23. int spp_print(bdaddr_t *src, bdaddr_t *dst, uint8_t channel, int fd, int copies, const char *cups_class)
  24. {
  25. struct sockaddr_rc addr;
  26. unsigned char buf[2048];
  27. int i, sk, err, len;
  28. if ((sk = socket(PF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM)) < 0) {
  29. perror("ERROR: Can't create socket");
  30. if (cups_class)
  31. return CUPS_BACKEND_FAILED;
  32. else
  33. return CUPS_BACKEND_RETRY;
  34. }
  35. addr.rc_family = AF_BLUETOOTH;
  36. bacpy(&addr.rc_bdaddr, src);
  37. addr.rc_channel = 0;
  38. if (bind(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  39. perror("ERROR: Can't bind socket");
  40. close(sk);
  41. if (cups_class)
  42. return CUPS_BACKEND_FAILED;
  43. else
  44. return CUPS_BACKEND_RETRY;
  45. }
  46. addr.rc_family = AF_BLUETOOTH;
  47. bacpy(&addr.rc_bdaddr, dst);
  48. addr.rc_channel = channel;
  49. if (connect(sk, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
  50. perror("ERROR: Can't connect to device");
  51. close(sk);
  52. if (cups_class)
  53. return CUPS_BACKEND_FAILED;
  54. else
  55. return CUPS_BACKEND_RETRY;
  56. }
  57. fputs("STATE: -connecting-to-device\n", stderr);
  58. /* Ignore SIGTERM signals if printing from stdin */
  59. if (fd == 0) {
  60. #ifdef HAVE_SIGSET
  61. sigset(SIGTERM, SIG_IGN);
  62. #elif defined(HAVE_SIGACTION)
  63. memset(&action, 0, sizeof(action));
  64. sigemptyset(&action.sa_mask);
  65. action.sa_handler = SIG_IGN;
  66. sigaction(SIGTERM, &action, NULL);
  67. #else
  68. signal(SIGTERM, SIG_IGN);
  69. #endif /* HAVE_SIGSET */
  70. }
  71. for (i = 0; i < copies; i++) {
  72. if (fd != 0) {
  73. fprintf(stderr, "PAGE: 1 1\n");
  74. lseek(fd, 0, SEEK_SET);
  75. }
  76. while ((len = read(fd, buf, sizeof(buf))) > 0) {
  77. err = write(sk, buf, len);
  78. if (err < 0) {
  79. perror("ERROR: Error writing to device");
  80. close(sk);
  81. return CUPS_BACKEND_FAILED;
  82. }
  83. }
  84. }
  85. close(sk);
  86. return CUPS_BACKEND_OK;
  87. }