|
1 | 1 | #include <unistd.h> |
2 | 2 | #include <stdlib.h> |
3 | 3 | #include <stdio.h> |
| 4 | +#include <getopt.h> |
| 5 | +#include <bluetooth/bluetooth.h> |
| 6 | +#include <bluetooth/sdp.h> |
| 7 | +#include <bluetooth/sdp_lib.h> // for sdp_session_t in start_server |
4 | 8 |
|
5 | 9 | #include "rfcomm-server.h" |
6 | 10 | #include "rfcomm-client.h" |
7 | | -#include "rfcomm-register.h" |
| 11 | +#include "hciscan.h" |
| 12 | +#include "service-register.h" |
| 13 | +#include "main.h" |
| 14 | +#include "service-register.h" |
8 | 15 |
|
9 | 16 | int main(int argc, char *argv[]) |
10 | 17 | { |
11 | | - int flags, opt; |
| 18 | + int opt;//, flags; |
12 | 19 |
|
13 | | - flags = 0; |
14 | | - sdp_session_t *session; |
| 20 | + // Service class variables. Default value is 0xABCD. |
| 21 | + uuid_t service_uuid_short = {SDP_UUID16, {0xABCD} }; |
| 22 | + uuid_t *service_uuid = NULL; |
15 | 23 |
|
16 | | - while ((opt = getopt(argc, argv, "scr")) != -1) { |
| 24 | + // Bluetooth address to connect and send data. Default is 00:00:00:00:00:00. |
| 25 | + bdaddr_t target_bdaddr = {{0, 0, 0, 0, 0, 0}}; |
| 26 | + |
| 27 | + // Two types of program operation. Default is receiver (server). |
| 28 | + typedef enum { |
| 29 | + OPERATION_MODE_SERVER = 1, |
| 30 | + OPERATION_MODE_CLIENT |
| 31 | + } operation_mode_t; |
| 32 | + operation_mode_t operation_mode = OPERATION_MODE_SERVER; |
| 33 | + |
| 34 | + const char *short_opt = "sa:ru:h"; |
| 35 | + struct option long_opt[] = |
| 36 | + { |
| 37 | + {"sender", no_argument, NULL, 's'}, |
| 38 | + {"address", required_argument, NULL, 'a'}, |
| 39 | + {"receiver", no_argument, NULL, 'r'}, |
| 40 | + {"uuid", required_argument, NULL, 'u'}, |
| 41 | + {"help", no_argument, NULL, 'h'}, |
| 42 | + {NULL, 0, NULL, 0 } |
| 43 | + }; |
| 44 | + while ((opt = getopt_long(argc, argv, short_opt, long_opt, NULL)) != -1) { |
17 | 45 | switch (opt) { |
18 | | - case 's': |
19 | | - flags = 1; |
| 46 | + case -1: /* no more arguments */ |
| 47 | + case 0: /* long options toggles */ |
20 | 48 | break; |
21 | | - case 'c': |
22 | | - flags = 2; |
| 49 | + case 's': |
| 50 | + printf("Sender (client) mode switched.\n"); |
| 51 | + operation_mode = OPERATION_MODE_CLIENT; |
| 52 | + break; |
| 53 | + case 'a': |
| 54 | + // Checking and setting address to send data. |
| 55 | + str2ba(optarg, &target_bdaddr); |
23 | 56 | break; |
24 | 57 | case 'r': |
25 | | - flags = 4; |
| 58 | + printf("Receiver (server) mode switched.\n"); |
| 59 | + operation_mode = OPERATION_MODE_SERVER; |
26 | 60 | break; |
27 | | - default: /* ''?'' */ |
28 | | - fprintf(stderr, "Usage: %s [-s] [-c] [-r]\n", |
29 | | - argv[0]); |
30 | | - exit(EXIT_FAILURE); |
31 | | - } |
| 61 | + case 'u': |
| 62 | + sdp_uuid16_create(&service_uuid_short, (uint16_t)strtoul(optarg, NULL, 16)); |
| 63 | + break; |
| 64 | + case 'h': |
| 65 | + printf("Usage: %s [OPTIONS]\n", argv[0]); |
| 66 | + printf(" -s start program as sender (address and service class\n\ |
| 67 | + may be specified)\n"); |
| 68 | + printf(" -a address bluetooth address for sender to connect. if not\n\ |
| 69 | + specified you'll be able to choose from list of\n\ |
| 70 | + available devices.\n"); |
| 71 | + printf(" -r start program as receiver (server)\n"); |
| 72 | + printf(" -u, --uuid specify UUID of service class (default is 0xABCD)\n"); |
| 73 | + printf(" -h, --help print this help and exit\n"); |
| 74 | + printf("\n"); |
| 75 | + exit(EXIT_SUCCESS); |
| 76 | + case ':': |
| 77 | + case '?': |
| 78 | + fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); |
| 79 | + exit(EXIT_FAILURE); |
| 80 | + default: |
| 81 | + fprintf(stderr, "%s: invalid option -- %c\n", argv[0], opt); |
| 82 | + fprintf(stderr, "Try `%s --help' for more information.\n", argv[0]); |
| 83 | + exit(EXIT_FAILURE); |
| 84 | + } |
32 | 85 | } |
33 | 86 |
|
34 | | - printf("flags=%d\n", flags); |
35 | | - |
36 | | - switch (flags) { |
37 | | - case 1: |
38 | | - fprintf(stdout, "Starting server.\n"); |
39 | | - session = register_service(); |
40 | | - rfcomm_server(); |
41 | | - sdp_close( session ); |
42 | | - break; |
43 | | - case 2: |
44 | | - fprintf(stdout, "Starting client.\n"); |
45 | | - rfcomm_client(); |
46 | | - break; |
47 | | - case 4: |
48 | | - fprintf(stdout, "Registering action.\n"); |
49 | | - register_service(); |
50 | | - break; |
| 87 | + // Initialize full length service uuid from short service uuid. |
| 88 | + service_uuid = sdp_uuid_to_uuid128 (&service_uuid_short); |
| 89 | + |
| 90 | + switch( operation_mode ) { |
| 91 | + case OPERATION_MODE_SERVER: |
| 92 | + printf("Operating as receiver (server).\n"); |
| 93 | + show_service_uuid(service_uuid); |
| 94 | + printf("Starting receiver (server)...\n"); |
| 95 | + server_register_and_start(service_uuid); |
| 96 | + break; |
| 97 | + case OPERATION_MODE_CLIENT: |
| 98 | + printf("Operating as sender (client).\n"); |
| 99 | + show_service_uuid(service_uuid); |
| 100 | + show_bdaddr(&target_bdaddr); |
| 101 | + if (bacmp(&target_bdaddr, BDADDR_ANY) == 0) |
| 102 | + { |
| 103 | + printf("Scanning started...\n"); |
| 104 | + hciscan(&target_bdaddr); |
| 105 | + } |
| 106 | + printf("You choose address.\n"); |
| 107 | + show_bdaddr(&target_bdaddr); |
| 108 | + client_start(&target_bdaddr, service_uuid); |
| 109 | + break; |
51 | 110 | default: |
52 | | - fprintf(stderr, "no action\n"); |
| 111 | + printf("Unknown operation mode \"%d\".\n", operation_mode); |
| 112 | + exit(EXIT_FAILURE); |
53 | 113 | } |
54 | | - fprintf(stdout, "Done.\n"); |
55 | 114 |
|
56 | 115 | exit(EXIT_SUCCESS); |
57 | 116 | } |
| 117 | + |
| 118 | +void show_service_uuid(uuid_t *service_uuid) |
| 119 | +{ |
| 120 | + char service_uuid_str[MAX_LEN_UUID_STR]; |
| 121 | + sdp_uuid2strn(service_uuid, service_uuid_str, MAX_LEN_UUID_STR); |
| 122 | + printf("Service class UUID is \"%s\".\n", service_uuid_str); |
| 123 | +} |
| 124 | + |
| 125 | +void show_bdaddr(bdaddr_t *target_bdaddr) |
| 126 | +{ |
| 127 | + char target_addr[32] = {0}; |
| 128 | + ba2str(target_bdaddr, target_addr); |
| 129 | + if (bacmp(target_bdaddr, BDADDR_ANY) == 0) |
| 130 | + printf("Address isn't specified.\n"); |
| 131 | + else |
| 132 | + printf("Address is \"%s\".\n", target_addr); |
| 133 | +} |
| 134 | + |
| 135 | +void server_register_and_start(uuid_t *service_uuid) |
| 136 | +{ |
| 137 | + sdp_session_t *session = 0; |
| 138 | + uint8_t port = 0; // Port of service. |
| 139 | + |
| 140 | + printf("Binding listening server.\n"); |
| 141 | + int s = rfcomm_server(&port); // Open server on socket and get binded port. |
| 142 | + // Register service on binded port. |
| 143 | + printf("Registering service.\n"); |
| 144 | + session = service_register(session, service_uuid, port); |
| 145 | + printf("Accepting connections.\n"); |
| 146 | + accept_connection(s); |
| 147 | + printf("Unregistering service.\n"); |
| 148 | + sdp_close( session ); |
| 149 | + printf("Closing server socket.\n"); |
| 150 | + close(s); // Close socket. |
| 151 | +} |
| 152 | + |
| 153 | +void client_start(bdaddr_t *target, uuid_t *service_uuid) |
| 154 | +{ |
| 155 | + uint8_t port = 0; // Port of service. |
| 156 | + |
| 157 | + port = service_search(target, service_uuid); |
| 158 | + if (port > 0) |
| 159 | + { |
| 160 | + printf("Starting client...\n"); |
| 161 | + rfcomm_client(target, port); |
| 162 | + } |
| 163 | + else |
| 164 | + { |
| 165 | + printf("Service port not found, exiting...\n"); |
| 166 | + } |
| 167 | +} |
0 commit comments