Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / diagnostic_station / station / comm_server.c @ 1183

History | View | Annotate | Download (3.39 KB)

1
#include "comm_server.h"
2

    
3
#include <avr/pgmspace.h>
4

    
5
#include "global.h"
6

    
7
// ##############
8
// ## Settings ##
9
// ##############
10

    
11
#define comm_server_debug
12

    
13
// ###############
14
// ## Constants ##
15
// ###############
16

    
17
#define buffer_size 120
18

    
19

    
20
// ####################
21
// ## Initialization ##
22
// ####################
23

    
24
void comm_server_init ()
25
{
26
        usb_init ();
27
}
28

    
29

    
30
// ########################
31
// ## Message processing ##
32
// ########################
33

    
34
// This function is copied from XNS
35
//static void serial_send_string_P (const char *s)/*{{{*/
36
//{
37
//    char buf;
38
//    while (memcpy_P (&buf, s, sizeof (char)), buf!=0)
39
//    {
40
//        serial_transmit (buf);
41
//        s++;
42
//    }
43
//}
44

    
45

    
46

    
47
/** Check if the buffer starts with the given text, followed by a space */
48
// This function is copied from XNS
49
static bool serial_match (PGM_P text, uint8_t *buffer, uint8_t size)
50
{
51
        uint8_t text_len=strlen_P (text);
52

    
53
        // If the buffer is shorter than the text, there is no match
54
        if (size<text_len) return false;
55

    
56
        // If the buffer is longer than the text, it must have a space at position after the text
57
        if (size>text_len && text[text_len]!=' ') return false;
58

    
59
        // Test for match
60
        if (strncmp_P ((char *)buffer, text, text_len)==0) return true;
61
        
62
        return false;
63
}
64

    
65

    
66

    
67

    
68
// ########################
69
// ## Messages (sending) ##
70
// ########################
71

    
72
void server_send_finished ()
73
{
74
        usb_puts ("finished");
75
}
76

    
77

    
78
// ##########################
79
// ## Messages (receiving) ##
80
// ##########################
81

    
82
// Put all symbolic string constants into the program memory because else all of them would be copied into the RAM (see
83
// the avr-libc documentation, chapter "Data in Program Space").
84
const char command_ping          [] PROGMEM = "ping";
85

    
86
static void handle_message (uint8_t *message, uint8_t size)
87
{
88
#ifdef comm_server_debug
89
        usb_puts ("# Received a message, size ");
90
        usb_puti(size);
91
        usb_puts (": [");
92
        usb_puts ((char *)message);
93
        usb_puts ("]" NL);
94
#endif
95
        
96
        if (serial_match (command_ping, message, size)) usb_puts ("pong" NL);
97
}
98

    
99

    
100

    
101

    
102
void byte_transmission_test (void)
103
{
104
        uint8_t c;
105
        
106
        while (1)
107
        {
108
                c=usb_getc ();
109
                usb_puts ("[");
110
                usb_puti (c);
111
                usb_puts ("] ");
112
        }
113
}
114

    
115

    
116
void server_main (void)
117
{
118
        uint8_t buffer[buffer_size];
119
        uint8_t c;
120
        uint8_t buffer_fill=0;
121

    
122
        //byte_transmission_test (); // Does not return
123

    
124
        
125
        while (1)
126
        {
127
                // Do nothing until we receive a byte (this function will return 0 when a character has been read)
128
                // (Yes, we could use the blocking function for now, but we may need the non-blocking later)
129
                while (usb_getc_nb ((char *)&c));
130
                
131
                if (c=='\r' || c=='\n')
132
                {
133
                        // A newline character was received. This terminates the message.
134
                        
135
                        // Empty lines are ignored.
136
                        if (buffer_fill>0)
137
                        {
138
                                // Add a terminating 0 to the buffer so the string functions won't try to read outside the buffer.
139
                                buffer[buffer_fill]=0;
140
                                
141
                                // Handle the message.
142
                                handle_message (buffer, buffer_fill);
143
                        
144
                                // Reset the buffer
145
                                buffer_fill=0;
146
                        }
147
                }
148
                else
149
                {
150
                        // If there is enough space left in the buffer, add the character we just received. Leave one byte for a
151
                        // terminating 0. If there is not enough space in the buffer, the rest of the message is ignored.
152
                        if (buffer_fill<buffer_size-1)
153
                        {
154
                                buffer[buffer_fill]=c;
155
                                ++buffer_fill;
156
                        }
157
                }
158
        
159
        }
160
}
161

    
162

    
163