Project

General

Profile

Revision 1185

Server communication: basic message processing

View differences:

comm_server.c
3 3
#include <avr/pgmspace.h>
4 4

  
5 5
#include "global.h"
6
#include "tests.h"
6 7

  
7 8
// ##############
8 9
// ## Settings ##
......
42 43
//    }
43 44
//}
44 45

  
45

  
46

  
47 46
/** Check if the buffer starts with the given text, followed by a space */
48 47
// This function is copied from XNS
49 48
static bool serial_match (PGM_P text, uint8_t *buffer, uint8_t size)
......
54 53
	if (size<text_len) return false;
55 54

  
56 55
	// 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;
56
	if (size>text_len && buffer[text_len]!=' ') return false;
58 57

  
59 58
	// Test for match
60 59
	if (strncmp_P ((char *)buffer, text, text_len)==0) return true;
......
65 64

  
66 65

  
67 66

  
67

  
68

  
68 69
// ########################
69 70
// ## Messages (sending) ##
70 71
// ########################
......
75 76
}
76 77

  
77 78

  
79
// ######################
80
// ## Message handlers ##
81
// ######################
82

  
83
static void handle_message_ping (uint8_t *buffer, uint8_t size)
84
{
85
	usb_puts ("pong" NL);
86
	orbs_set (255, 127, 0, 0, 255, 0);
87
	delay_ms (400);
88
	orbs_set (0, 255, 0, 255, 127, 0);
89
}
90

  
91
static void handle_message_start_test (uint8_t *buffer, uint8_t size)
92
{
93
	// FIXME parameter handling
94
	// Parameters: all/rangefinder/motor/encoder all/<num>
95
	test_all ();
96
}
97

  
98

  
78 99
// ##########################
79 100
// ## Messages (receiving) ##
80 101
// ##########################
81 102

  
103

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

  
86 109
static void handle_message (uint8_t *message, uint8_t size)
87 110
{
88 111
#ifdef comm_server_debug
112
	// Output: "# Received a message, size <size>: [<message>]"
89 113
	usb_puts ("# Received a message, size ");
90 114
	usb_puti(size);
91 115
	usb_puts (": [");
......
93 117
	usb_puts ("]" NL);
94 118
#endif
95 119
	
96
	if (serial_match (command_ping, message, size)) usb_puts ("pong" NL);
120
	bool handled=false;
121
	if (serial_match (command_ping      , message, size)) { handle_message_ping       (message, size); handled=true; }
122
	if (serial_match (command_start_test, message, size)) { handle_message_start_test (message, size); handled=true; }
123
	// More messages go here
124
	
125
	if (!handled)
126
	{
127
		usb_puts ("# Warning: unhandled message: [");
128
		usb_puts ((char *)message);
129
		usb_puts ("]" NL);
130
	}
97 131
}
98 132

  
99 133

  
134
// ###############
135
// ## Debugging ##
136
// ###############
100 137

  
101

  
102 138
void byte_transmission_test (void)
103 139
{
104 140
	uint8_t c;
......
113 149
}
114 150

  
115 151

  
152
// ###############
153
// ## Main loop ##
154
// ###############
155

  
116 156
void server_main (void)
117 157
{
118 158
	uint8_t buffer[buffer_size];
......
121 161

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

  
164
	usb_puts (NL);
165
	usb_puts ("# Diagnostic station server mode" NL);
124 166
	
125 167
	while (1)
126 168
	{
......
158 200
	
159 201
	}
160 202
}
161

  
162

  
163

  

Also available in: Unified diff