summaryrefslogtreecommitdiff
path: root/r2d2.c
blob: 07fcf6e386022a38596d888ce8b615b797f29b3b (plain)
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
/*
    r2d2 - interact with Sphero R2-D2
    Copyright (C) 2020 Ian Cowburn

    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 3 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, see <http://www.gnu.org/licenses/>.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h>
#include <bluetooth/hci_lib.h>
#include <bluetooth/l2cap.h>

typedef struct
{
    const char	*name;
    int		command[20];
} Command;

static Command plain_commands[] =
{
    {"laugh", {0x0A,0x18,0x00,0x1F,0x00,0x32,0x00,0x00,0x00,0x00,0x00,-1}},
    {"yes", {0x0A,0x17,0x05,0x41,0x00,0x0F,-1}},
    {"no", {0x0A,0x17,0x05,0x3F,0x00,0x10,-1}},
    {"alarm", {0x0A,0x17,0x05,0x17,0x00,0x07,-1}},
    {"angry", {0x0A,0x17,0x05,0x18,0x00,0x08,-1}},
    {"annoyed", {0x0A,0x17,0x05,0x19,0x00,0x09,-1}},
    {"ionblast", {0x0A,0x17,0x05,0x1A,0x00,0x0E,-1}},
    {"sad", {0x0A,0x17,0x05,0x1C,0x00,0x11,-1}},
    {"scared", {0x0A,0x17,0x05,0x1D,0x00,0x13,-1}},
    {"chatty", {0x0A,0x17,0x05,0x17,0x00,0x0A,-1}},
    {"confident", {0x0A,0x17,0x05,0x18,0x00,0x12,-1}},
    {"excited", {0x0A,0x17,0x05,0x19,0x00,0x0C,-1}},
    {"happy", {0x0A,0x17,0x05,0x1A,0x00,0x0D,-1}},
    {"laugh", {0x0A,0x17,0x05,0x1B,0x00,0x0F,-1}},
    {"surprise", {0x0A,0x17,0x05,0x1C,0x00,0x18,-1}},
    {"tripod", {0x0A,0x17,0x0D,0x1D,0x01,-1}},
    {"bipod", {0x0A,0x17,0x0D,0x1C,0x02,-1}},
    {NULL, {0}}
};

static unsigned char force_command[] =
{
    0x75,0x73,0x65,0x74,0x68,0x65,0x66,0x6F,0x72,0x63,0x65,0x2E,
    0x2E,0x2E,0x62,0x61,0x6E,0x64
};

static Command wake_command =
{
    "wakeup",
    {
    	0x0A,0x13,0x0D,0x00,-1
    }
};

static void Fatal(const char *call)
{
    perror(call);
    exit(EXIT_FAILURE);
}

static unsigned char Checksum(const char *p, int len)
{
    unsigned char c;
    int f;

    c = 0;

    for(f = 0; f < len; f++)
    {
    	c += p[f];
    }

    c = ~c;

    return c;
}

static int Write(int sock, unsigned char *buff, int len)
{
    int wr = 0;
    int tot = 0;

    while(tot < len)
    {
    	wr = write(sock, buff + tot, len - tot);

	if (wr < 1)
	{
	    return -1;
	}

	tot += wr;
    }

    return len;
}

static void CharWriteHandle(int sock, int handle,
			    const unsigned char *cmd, int len)
{
    unsigned char buff[1024];
    unsigned short *us;

    memcpy(buff + 3, cmd, len);
    len += 3;

    buff[0] = 0x12;

    us = (unsigned short *)(buff + 1);
    *us = htobs(handle);

    if (Write(sock, buff, len) == -1)
    {
    	Fatal("write");
    }
}

static void SendCommand(int sock, int handle, const Command *cmd)
{
    unsigned char buff[1024];
    unsigned short *us;
    int len;
    int f;

    for(f = 0; cmd->command[f] != -1; f++)
    {
    	buff[f] = (unsigned char)cmd->command[f];
    }

    len = f;

    buff[len] = Checksum(buff, len);
    len++;

    memmove(buff + 1, buff, len++);
    buff[0] = 0x8du;
    buff[++len] = 0xd8u;

    CharWriteHandle(sock, handle, buff, len);
}

int main(int argc, char *argv[])
{
    char address[32] = {0};
    struct sockaddr_l2 src_addr = {0};
    struct sockaddr_l2 dest_addr = {0};
    int base;
    int f;
    int sock;

    /* Get address
    */
    if (getenv("R2D2"))
    {
    	snprintf(address, sizeof address, "%s", getenv("R2D2"));
    }

    if (argc > 2 && strcmp(argv[1], "-a") == 0)
    {
    	snprintf(address, sizeof address, "%s", argv[2]);
	base = 3;
    }
    else
    {
    	base = 1;
    }

    /* Check address and arguments supplied
    */
    if (!address[0])
    {
    	fprintf(stderr, "%s: pass address or set R2D2 variable\n", argv[0]);
	return EXIT_FAILURE;
    }

    if (argc <= base)
    {
	fprintf(stderr, "%s: usage %s [-a address] command [..command]\n",
			    argv[0], argv[0]);
    	return EXIT_FAILURE;
    }

    /* Open connection to R2
    */
    sock = socket(AF_BLUETOOTH, SOCK_SEQPACKET, BTPROTO_L2CAP);

    if (sock == -1)
    {
    	Fatal("socket");
    }

    src_addr.l2_family = AF_BLUETOOTH;
    src_addr.l2_cid = htobs(4);
    bacpy(&src_addr.l2_bdaddr, BDADDR_ANY);
    src_addr.l2_bdaddr_type = BDADDR_LE_RANDOM;

    if (bind(sock, (void *)&src_addr, sizeof(src_addr)) == -1)
    {
    	Fatal("bind");
    }

    dest_addr.l2_family = AF_BLUETOOTH;
    dest_addr.l2_cid = htobs(4);
    str2ba(address, &dest_addr.l2_bdaddr);
    dest_addr.l2_bdaddr_type = BDADDR_LE_RANDOM;

    if (connect(sock, (void *)&dest_addr, sizeof(dest_addr)) == -1)
    {
    	Fatal("connect");
    }

    CharWriteHandle(sock, 0x15, force_command, sizeof force_command);
    SendCommand(sock, 0x1c,  &wake_command);

    for(f = base; f < argc; f++)
    {
	int n;

    	Command *cmd = NULL;

	for(n = 0; plain_commands[n].name && !cmd; n++)
	{
	    if (strcmp(argv[f], plain_commands[n].name) == 0)
	    {
	    	cmd = plain_commands + n;
	    }
	}

	if (cmd == NULL)
	{
	    fprintf(stderr, "%s: unknown command %s\n", argv[0], argv[f]);
	}
	else
	{
	    SendCommand(sock, 0x1c,  cmd);
	}
    }

    /* All done
    */
    if (close(sock) == -1)
    {
    	Fatal("close");
    }

    return EXIT_SUCCESS;
}