Project

General

Profile

Revision 1183

Added individual hardware component files

View differences:

comm_server.c
1 1
#include "comm_server.h"
2
#include "global.h"
3 2

  
4 3
#include <avr/pgmspace.h>
5 4

  
5
#include "global.h"
6

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

  
11
#define comm_server_debug
12

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

  
6 17
#define buffer_size 120
7 18

  
8 19

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

  
9 24
void comm_server_init ()
10 25
{
11 26
	usb_init ();
12
	
13 27
}
14 28

  
15
void server_send_finished ()
16
{
17
	usb_puts ("finished");
18
}
19 29

  
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";
30
// ########################
31
// ## Message processing ##
32
// ########################
23 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

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

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

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

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

  
37
//	if ((equal_len || (longer && space after)) 
38
//&& strncmp_P
39
//            (serial_recieve_buffer, text, text_len)==0)
40 65

  
41
	
42 66

  
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
//    }
67

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

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

  
58 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

  
59 86
static void handle_message (uint8_t *message, uint8_t size)
60 87
{
61
	usb_puts ("Received a message, size ");
88
#ifdef comm_server_debug
89
	usb_puts ("# Received a message, size ");
62 90
	usb_puti(size);
63
	usb_puts (": ");
64

  
65
	for (uint8_t i=0; i<size; ++i)
66
	{
67
		usb_putc (message[i]);
68
	}
91
	usb_puts (": [");
92
	usb_puts ((char *)message);
93
	usb_puts ("]" NL);
94
#endif
69 95
	
70
	usb_puts (NL);
96
	if (serial_match (command_ping, message, size)) usb_puts ("pong" NL);
71 97
}
72 98

  
73 99

  
74 100

  
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 101

  
87

  
88

  
89 102
void byte_transmission_test (void)
90 103
{
91 104
	uint8_t c;
......
113 126
	{
114 127
		// Do nothing until we receive a byte (this function will return 0 when a character has been read)
115 128
		// (Yes, we could use the blocking function for now, but we may need the non-blocking later)
116
		while (usb_getc_nb (&c));
129
		while (usb_getc_nb ((char *)&c));
117 130
		
118 131
		if (c=='\r' || c=='\n')
119 132
		{

Also available in: Unified diff