sdpcontrol.c (0986ab12e44caea472245845f9a89ced4f137d73) sdpcontrol.c (a4b187fa33f1856b9c0293b900d36b4c797b459d)
1/*
2 * sdpcontrol.c
3 *
4 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 27 unchanged lines hidden (view full) ---

36#include <sdp.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41#include "sdpcontrol.h"
42
43/* Prototypes */
1/*
2 * sdpcontrol.c
3 *
4 * Copyright (c) 2001-2003 Maksim Yevmenkin <m_evmenkin@yahoo.com>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions

--- 27 unchanged lines hidden (view full) ---

36#include <sdp.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <unistd.h>
41#include "sdpcontrol.h"
42
43/* Prototypes */
44static int do_sdp_command (bdaddr_p, int, char **);
44static int do_sdp_command (bdaddr_p, char const *, int,
45 int, char **);
45static struct sdp_command * find_sdp_command (char const *,
46 struct sdp_command *);
47static void print_sdp_command (struct sdp_command *);
48static void usage (void);
49
50/* Main */
51int
52main(int argc, char *argv[])
53{
46static struct sdp_command * find_sdp_command (char const *,
47 struct sdp_command *);
48static void print_sdp_command (struct sdp_command *);
49static void usage (void);
50
51/* Main */
52int
53main(int argc, char *argv[])
54{
54 int n;
55 bdaddr_t bdaddr;
55 char const *control = SDP_LOCAL_PATH;
56 int n, local;
57 bdaddr_t bdaddr;
56
57 memset(&bdaddr, 0, sizeof(bdaddr));
58
59 /* Process command line arguments */
58
59 memset(&bdaddr, 0, sizeof(bdaddr));
60
61 /* Process command line arguments */
60 while ((n = getopt(argc, argv, "a:h")) != -1) {
62 while ((n = getopt(argc, argv, "a:c:lh")) != -1) {
61 switch (n) {
63 switch (n) {
62 case 'a':
64 case 'a': /* bdaddr */
63 if (!bt_aton(optarg, &bdaddr)) {
64 struct hostent *he = NULL;
65
66 if ((he = bt_gethostbyname(optarg)) == NULL)
67 errx(1, "%s: %s", optarg, hstrerror(h_errno));
68
69 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
70 }
71 break;
72
65 if (!bt_aton(optarg, &bdaddr)) {
66 struct hostent *he = NULL;
67
68 if ((he = bt_gethostbyname(optarg)) == NULL)
69 errx(1, "%s: %s", optarg, hstrerror(h_errno));
70
71 memcpy(&bdaddr, he->h_addr, sizeof(bdaddr));
72 }
73 break;
74
75 case 'c': /* control socket */
76 control = optarg;
77 break;
78
79 case 'l': /* local sdpd */
80 local = 1;
81 break;
82
73 case 'h':
74 default:
75 usage();
76 /* NOT REACHED */
77 }
78 }
79
80 argc -= optind;
81 argv += optind;
82
83 if (*argv == NULL)
84 usage();
85
83 case 'h':
84 default:
85 usage();
86 /* NOT REACHED */
87 }
88 }
89
90 argc -= optind;
91 argv += optind;
92
93 if (*argv == NULL)
94 usage();
95
86 return (do_sdp_command(&bdaddr, argc, argv));
96 return (do_sdp_command(&bdaddr, control, local, argc, argv));
87}
88
89/* Execute commands */
90static int
97}
98
99/* Execute commands */
100static int
91do_sdp_command(bdaddr_p bdaddr, int argc, char **argv)
101do_sdp_command(bdaddr_p bdaddr, char const *control, int local,
102 int argc, char **argv)
92{
93 char *cmd = argv[0];
94 struct sdp_command *c = NULL;
95 void *xs = NULL;
96 int e, help;
97
98 help = 0;
99 if (strcasecmp(cmd, "help") == 0) {

--- 15 unchanged lines hidden (view full) ---

115
116 c = find_sdp_command(cmd, sdp_commands);
117 if (c == NULL) {
118 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
119 return (ERROR);
120 }
121
122 if (!help) {
103{
104 char *cmd = argv[0];
105 struct sdp_command *c = NULL;
106 void *xs = NULL;
107 int e, help;
108
109 help = 0;
110 if (strcasecmp(cmd, "help") == 0) {

--- 15 unchanged lines hidden (view full) ---

126
127 c = find_sdp_command(cmd, sdp_commands);
128 if (c == NULL) {
129 fprintf(stdout, "Unknown command: \"%s\"\n", cmd);
130 return (ERROR);
131 }
132
133 if (!help) {
123 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
124 usage();
134 if (!local) {
135 if (memcmp(bdaddr, NG_HCI_BDADDR_ANY, sizeof(*bdaddr)) == 0)
136 usage();
125
137
126 if ((xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr)) == NULL)
127 errx(1, "Could not create SDP session object");
138 xs = sdp_open(NG_HCI_BDADDR_ANY, bdaddr);
139 } else
140 xs = sdp_open_local(control);
128
141
142 if (xs == NULL)
143 errx(1, "Could not create SDP session object");
129 if (sdp_error(xs) == 0)
130 e = (c->handler)(xs, -- argc, ++ argv);
131 else
132 e = ERROR;
133 } else
134 e = USAGE;
135
136 switch (e) {

--- 48 unchanged lines hidden (view full) ---

185 for (c = category; c->command != NULL; c++)
186 fprintf(stdout, "\t%s\n", c->command);
187} /* print_sdp_command */
188
189/* Usage */
190static void
191usage(void)
192{
144 if (sdp_error(xs) == 0)
145 e = (c->handler)(xs, -- argc, ++ argv);
146 else
147 e = ERROR;
148 } else
149 e = USAGE;
150
151 switch (e) {

--- 48 unchanged lines hidden (view full) ---

200 for (c = category; c->command != NULL; c++)
201 fprintf(stdout, "\t%s\n", c->command);
202} /* print_sdp_command */
203
204/* Usage */
205static void
206usage(void)
207{
193 fprintf(stdout, "Usage: sdpcontrol -a BD_ADDR [-h] " \
194 "cmd [p1] [..]]\n");
208 fprintf(stderr,
209"Usage: sdpcontrol options command\n" \
210"Where options are:\n"
211" -a bdaddr specify bdaddr\n" \
212" -c path path to the control socket (default is %s)\n" \
213" -h display usage and quit\n" \
214" -l connect to the local SDP server via control socket\n" \
215" command one of the supported commands\n", SDP_LOCAL_PATH);
195 exit(255);
196} /* usage */
197
216 exit(255);
217} /* usage */
218