root / trunk / code / projects / diagnostic_station / station / comm_server.c @ 1182
History | View | Annotate | Download (3.14 KB)
1 |
#include "comm_server.h" |
---|---|
2 |
#include "global.h" |
3 |
|
4 |
#include <avr/pgmspace.h> |
5 |
|
6 |
#define buffer_size 120 |
7 |
|
8 |
|
9 |
void comm_server_init ()
|
10 |
{ |
11 |
usb_init (); |
12 |
|
13 |
} |
14 |
|
15 |
void server_send_finished ()
|
16 |
{ |
17 |
usb_puts ("finished");
|
18 |
} |
19 |
|
20 |
// Put all symbolic string constants into the program memory because else they would be copied into the RAM (see the
|
21 |
// avrlibc documentation).
|
22 |
const char command_ping [] PROGMEM = "ping"; |
23 |
|
24 |
/** Check if the buffer starts with the given text, followed by a space */
|
25 |
static bool serial_match (PGM_P text, uint8_t *buffer, uint8_t size) |
26 |
{ |
27 |
return false; // FIXME implement |
28 |
// uint8_t text_len=strlen_P (text);
|
29 |
|
30 |
// If the buffer is shorter than the text, there is no match
|
31 |
// if (size<text_len) return false;
|
32 |
|
33 |
// If the buffer is longer than the text, it must have a space at position after the text
|
34 |
// if (size>text_len && text[text_len]
|
35 |
|
36 |
|
37 |
// if ((equal_len || (longer && space after))
|
38 |
//&& strncmp_P
|
39 |
// (serial_recieve_buffer, text, text_len)==0)
|
40 |
|
41 |
|
42 |
|
43 |
// if ((size==text_len ||
|
44 |
// (serial_recieve_buffer_len>text_len &&
|
45 |
// serial_recieve_buffer[text_len]==' ')) && strncmp_P
|
46 |
// (serial_recieve_buffer, text, text_len)==0)
|
47 |
// {
|
48 |
// parameter_start=0;
|
49 |
// parameter_length=text_len;
|
50 |
// return true;
|
51 |
// }
|
52 |
// else
|
53 |
// {
|
54 |
// return false;
|
55 |
// }
|
56 |
} |
57 |
|
58 |
|
59 |
static void handle_message (uint8_t *message, uint8_t size) |
60 |
{ |
61 |
usb_puts ("Received a message, size ");
|
62 |
usb_puti(size); |
63 |
usb_puts (": ");
|
64 |
|
65 |
for (uint8_t i=0; i<size; ++i) |
66 |
{ |
67 |
usb_putc (message[i]); |
68 |
} |
69 |
|
70 |
usb_puts (NL); |
71 |
} |
72 |
|
73 |
|
74 |
|
75 |
// From XNS
|
76 |
//
|
77 |
//static void serial_send_string_P (const char *s)/*{{{*/
|
78 |
//{
|
79 |
// char buf;
|
80 |
// while (memcpy_P (&buf, s, sizeof (char)), buf!=0)
|
81 |
// {
|
82 |
// serial_transmit (buf);
|
83 |
// s++;
|
84 |
// }
|
85 |
//}
|
86 |
|
87 |
|
88 |
|
89 |
void byte_transmission_test (void) |
90 |
{ |
91 |
uint8_t c; |
92 |
|
93 |
while (1) |
94 |
{ |
95 |
c=usb_getc (); |
96 |
usb_puts ("[");
|
97 |
usb_puti (c); |
98 |
usb_puts ("] ");
|
99 |
} |
100 |
} |
101 |
|
102 |
|
103 |
void server_main (void) |
104 |
{ |
105 |
uint8_t buffer[buffer_size]; |
106 |
uint8_t c; |
107 |
uint8_t buffer_fill=0;
|
108 |
|
109 |
//byte_transmission_test (); // Does not return
|
110 |
|
111 |
|
112 |
while (1) |
113 |
{ |
114 |
// Do nothing until we receive a byte (this function will return 0 when a character has been read)
|
115 |
// (Yes, we could use the blocking function for now, but we may need the non-blocking later)
|
116 |
while (usb_getc_nb (&c));
|
117 |
|
118 |
if (c=='\r' || c=='\n') |
119 |
{ |
120 |
// A newline character was received. This terminates the message.
|
121 |
|
122 |
// Empty lines are ignored.
|
123 |
if (buffer_fill>0) |
124 |
{ |
125 |
// Add a terminating 0 to the buffer so the string functions won't try to read outside the buffer.
|
126 |
buffer[buffer_fill]=0;
|
127 |
|
128 |
// Handle the message.
|
129 |
handle_message (buffer, buffer_fill); |
130 |
|
131 |
// Reset the buffer
|
132 |
buffer_fill=0;
|
133 |
} |
134 |
} |
135 |
else
|
136 |
{ |
137 |
// If there is enough space left in the buffer, add the character we just received. Leave one byte for a
|
138 |
// terminating 0. If there is not enough space in the buffer, the rest of the message is ignored.
|
139 |
if (buffer_fill<buffer_size-1) |
140 |
{ |
141 |
buffer[buffer_fill]=c; |
142 |
++buffer_fill; |
143 |
} |
144 |
} |
145 |
|
146 |
} |
147 |
} |
148 |
|
149 |
|
150 |
|