Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
112 | mikeljo | 1 | |
2 | #include <avr/io.h> |
||
3 | #include <inttypes.h> |
||
4 | #include <stdlib.h> |
||
5 | #include <avr/pgmspace.h> |
||
6 | |||
7 | #include "main.h" |
||
8 | #include "rs232.h" |
||
9 | #include "base64.h" |
||
10 | #include "lcd.h" |
||
11 | |||
12 | uint8_t r_buffer[129]; // Dieser Puffer enthält die Rohdaten (kodiert) |
||
13 | uint8_t p_buffer[129]; // Dieser Puffer enthält die Daten im Klartext |
||
14 | //struct str_DebugIn *p_buffer; |
||
15 | |||
16 | |||
17 | uint8_t get_message() // Liest eine komplette Übertragung und dekodiert sie |
||
18 | { |
||
19 | uint8_t index, comm; |
||
20 | |||
21 | timer = 20; // Timer für Timeout |
||
22 | |||
23 | while ((RS232_get() != '#') && (timer > 0)); // Warten auf Start-Zeichen # |
||
24 | |||
25 | if (timer > 0) // Falls kein Timeout auftrat |
||
26 | { |
||
27 | index = 0; // Die Rohdaten in r_buffer einlesen |
||
28 | do |
||
29 | { |
||
30 | comm = RS232_get(); |
||
31 | r_buffer[index++] = comm; |
||
32 | |||
33 | if (index > 127) // Schutz vor Puffer-Überlauf |
||
34 | index = 127; |
||
35 | } |
||
36 | while (comm != 0x0D); // ... bis End-Signal = 0x0D kommt... |
||
37 | base64_decode(index); // Die base64-kodierten Rohdaten umwandeln |
||
38 | return 0; // kein Fehler aufgetreten |
||
39 | } |
||
40 | else |
||
41 | { |
||
42 | return 1; // Fehler aufgetreten |
||
43 | } |
||
44 | } |
||
45 | |||
46 | |||
47 | uint8_t base64_decode(uint8_t number) // Wandelt die base64-Rohdaten in lesbare Daten um |
||
48 | { |
||
49 | uint8_t p,q; |
||
50 | uint8_t a,b,c,d; |
||
51 | |||
52 | p = 2; |
||
53 | q = 0; |
||
54 | |||
55 | while (p < number) |
||
56 | { |
||
57 | a = r_buffer[p + 0] - 61; |
||
58 | b = r_buffer[p + 1] - 61; |
||
59 | c = r_buffer[p + 2] - 61; |
||
60 | d = r_buffer[p + 3] - 61; |
||
61 | |||
62 | p += 4; |
||
63 | |||
64 | p_buffer[q + 0] = (a << 2) | (b >> 4); // gespeichert werden die Daten in p_buffer |
||
65 | p_buffer[q + 1] = ((b & 0x0F) << 4) | (c >> 2); |
||
66 | p_buffer[q + 2] = ((c & 0x03) << 6) | d; |
||
67 | |||
68 | q += 3; |
||
69 | } |
||
70 | return q; // Rückgabe der Anzahl der Datenbytes |
||
71 | } |
||
72 | |||
73 | void base64_send(uint8_t number) // Sendet Daten zur Flight-Control |
||
74 | { // Die Daten werden in p_buffer erwartet, umcodiert und gesendet |
||
75 | uint8_t bpointer,spointer; |
||
76 | uint8_t x,y,z,a; |
||
77 | uint16_t checksum; |
||
78 | |||
79 | r_buffer[0] = p_buffer[0]; |
||
80 | r_buffer[1] = p_buffer[1]; |
||
81 | r_buffer[2] = p_buffer[2]; |
||
82 | |||
83 | bpointer = 3; |
||
84 | spointer = 3; |
||
85 | |||
86 | while (spointer < number) |
||
87 | { |
||
88 | x = p_buffer[spointer]; |
||
89 | y = p_buffer[spointer + 1]; |
||
90 | z = p_buffer[spointer + 2]; |
||
91 | spointer += 3; |
||
92 | |||
93 | r_buffer[bpointer + 0] = (x >> 2) + 61; |
||
94 | r_buffer[bpointer + 1] = (((x & 0x03) << 4) | ((y & 0xF0) >> 4)) + 61; |
||
95 | r_buffer[bpointer + 2] = (((y & 0x0F) << 2) | ((z & 0xC0) >> 6)) + 61; |
||
96 | r_buffer[bpointer + 3] = (z & 0x3F) + 61; |
||
97 | |||
98 | bpointer += 4; |
||
99 | } |
||
100 | |||
101 | checksum = 0; |
||
102 | for(a=0;a<bpointer;a++) |
||
103 | checksum += r_buffer[a]; |
||
104 | |||
105 | checksum %= 4096; |
||
106 | |||
107 | r_buffer[bpointer] = (checksum / 64) + 61; |
||
108 | r_buffer[bpointer + 1] = (checksum % 64) + 61; |
||
109 | r_buffer[bpointer + 2] = 13; |
||
110 | r_buffer[bpointer + 3] = 0; |
||
111 | |||
112 | bpointer += 3; |
||
113 | |||
114 | for(a=0;a<bpointer;a++) |
||
115 | RS232_send(r_buffer[a]); |
||
116 | } |