FreeCalypso > hg > freecalypso-tools
annotate rfcal/cmu200/session.c @ 203:f2dde45ba378
target-utils/Makefile: flash-boot-wa added
author | Mychaela Falconia <falcon@freecalypso.org> |
---|---|
date | Mon, 01 May 2017 21:21:13 +0000 |
parents | 47d56330609d |
children |
rev | line source |
---|---|
195
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
1 /* |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
2 * This module contains the code that handles a single local socket |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
3 * connection session. |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
4 */ |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
5 |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
6 #include <ctype.h> |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
7 #include <stdio.h> |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
8 #include <stdlib.h> |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
9 #include <string.h> |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
10 #include <strings.h> |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
11 |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
12 extern int activesock; |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
13 |
196
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
14 #define MAX_FIELDS 10 |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
15 |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
16 char client_cmd[256], *client_cmd_fields[MAX_FIELDS+1]; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
17 int client_cmd_nfields; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
18 |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
19 parse_cmd_into_fields() |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
20 { |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
21 char *cp; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
22 |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
23 client_cmd_nfields = 0; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
24 for (cp = client_cmd; ; ) { |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
25 while (isspace(*cp)) |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
26 cp++; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
27 if (*cp == '\0') |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
28 break; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
29 if (client_cmd_nfields >= MAX_FIELDS) { |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
30 send_socket_response("-Command has too many fields\n"); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
31 return(1); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
32 } |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
33 client_cmd_fields[client_cmd_nfields++] = cp; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
34 while (*cp && !isspace(*cp)) |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
35 cp++; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
36 if (*cp) |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
37 *cp++ = '\0'; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
38 } |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
39 client_cmd_fields[client_cmd_nfields] = 0; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
40 return(0); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
41 } |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
42 |
195
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
43 handle_command() |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
44 { |
196
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
45 char readbuf[256]; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
46 int cc, pos, rc; |
195
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
47 |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
48 for (pos = 0; ; ) { |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
49 cc = read(activesock, readbuf, sizeof readbuf); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
50 if (cc <= 0) { |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
51 printf("Client program closed connection\n"); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
52 return(1); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
53 } |
196
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
54 if (pos + cc > sizeof client_cmd) { |
195
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
55 send_socket_response("-Command too long\n"); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
56 return(1); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
57 } |
196
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
58 bcopy(readbuf, client_cmd + pos, cc); |
195
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
59 pos += cc; |
196
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
60 if (client_cmd[pos-1] == '\n') |
195
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
61 break; |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
62 } |
196
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
63 client_cmd[pos-1] = '\0'; |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
64 printf("Client command: %s\n", client_cmd); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
65 rc = parse_cmd_into_fields(); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
66 if (rc) |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
67 return(0); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
68 if (!client_cmd_nfields) { |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
69 send_socket_response("+Empty command OK\n"); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
70 return(0); |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
71 } |
47d56330609d
fc-cmu200d: skeleton complete, ready to start adding meat
Mychaela Falconia <falcon@freecalypso.org>
parents:
195
diff
changeset
|
72 return dispatch_client_command(); |
195
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
73 } |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
74 |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
75 handle_session() |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
76 { |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
77 int rc; |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
78 |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
79 send_socket_response("+CMU200 interface daemon ready\n"); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
80 for (;;) { |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
81 rc = handle_command(); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
82 if (rc) |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
83 break; |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
84 } |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
85 close(activesock); |
db9ee7745cdd
fc-cmu200d: socket handling skeleton added
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff
changeset
|
86 } |