Subversion Repositories Projects

Rev

Rev 440 | Rev 450 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 440 Rev 449
Line 1... Line 1...
1
#include <Parser.h>
1
#include <Parser.h>
Line -... Line 2...
-
 
2
 
-
 
3
/**
-
 
4
 * create a frame that can be send to the MK using the
-
 
5
 * connection class.
-
 
6
 * see http://www.mikrokopter.com/ucwiki/en/SerialProtocol
-
 
7
 * how the protocol is encoded and
-
 
8
 * see http://www.mikrokopter.com/ucwiki/en/SerialCommands
-
 
9
 * to look at the possible commands that are already coded
-
 
10
 * in data
-
 
11
 */
-
 
12
void Parser::create_frame(char cmd, int address, char * data, unsigned int length) {
-
 
13
        //if # is cmd we do not touch anything, because
-
 
14
        // the command is already encoded in data
-
 
15
        if (cmd != '#') {
-
 
16
            /*
-
 
17
            //calculate buffer length
-
 
18
            //(4 Bytes for 1-byte '#', 1-byte address, and 2-byte crc)
-
 
19
            //(the rest for the data length - see encode how this is calculated)
-
 
20
            int buff_len = 4+(length/3 + (length%3==0?0:1) )*4;
-
 
21
 
-
 
22
            //allociate memory for the data we want to send
-
 
23
            char * send_data = (char *)malloc(buf_len);
-
 
24
            */
-
 
25
            char send_data[150];
-
 
26
            send_data[0]='#';
-
 
27
            send_data[1]=(char)address;
-
 
28
            send_data[2]=cmd;
-
 
29
            for (int i = 0; i < length; i++)
-
 
30
                send_data[i+3] = data[i];
-
 
31
            //TODO: abgleich mit MKCommunication::send_command
-
 
32
            Parser::encode64(send_data, length);
-
 
33
            address = 'a' + address;
-
 
34
           
-
 
35
        }
-
 
36
}
-
 
37
 
-
 
38
 
2
 
39
/**
3
// Base64 Decoder
40
 * Base64 Decoder
-
 
41
 * see Parser.h for details about sRxData
-
 
42
 * data = data that will be decoded
-
 
43
 * len = length of data
4
// see Parser.h for details about sRxData
44
 * ptrOut = pointer to decoded data
-
 
45
 * offset = offset in data
-
 
46
 */
5
bool Parser::decode64(sRxData &rx)
47
int Parser::decode64(char * data, int len, unsigned char *ptrOut, int offset)
6
{
-
 
7
    int length = rx.str.size();
48
{
8
    unsigned char a,b,c,d;
49
    unsigned char a,b,c,d;
9
    unsigned char ptr = 0;
50
    unsigned char ptr = 0;
10
    unsigned char x,y,z;
-
 
Line 11... Line -...
11
    int ptrOut[150];
-
 
12
 
-
 
13
    int ptrIn = 3;
-
 
14
    int max = length;
51
    unsigned char x,y,z;
15
    int len = length;
52
 
-
 
53
    int decLen = 0;
16
    int decLen = 0;
54
/*
17
 
55
    //FIXME: dies wieder einklammern!
18
    if (rx.input[ptrIn] == 0) {
56
    if (data[ptrIn] == 0) {
19
        return false;
57
        return -1;
20
        //TODO: catch error to show that something went wrong during the decode process
58
        //TODO: catch error to show that something went wrong during the decode process
-
 
59
        //throw "Nothing received";
-
 
60
    }
-
 
61
*/
-
 
62
    //decode data
-
 
63
    while(len) {
-
 
64
        a = data[offset++] - '=';
-
 
65
        b = data[offset++] - '=';
Line 21... Line -...
21
        //throw "Nothing received";
-
 
22
    }
-
 
23
 
-
 
24
    while(len != 0) {
-
 
25
        a = rx.input[ptrIn++] - '=';
-
 
26
        b = rx.input[ptrIn++] - '=';
-
 
27
        c = rx.input[ptrIn++] - '=';
66
        c = data[offset++] - '=';
Line 28... Line 67...
28
        d = rx.input[ptrIn++] - '=';
67
        d = data[offset++] - '=';
29
 
68
 
30
        if(ptrIn > max - 2) break;
69
        //if(offset > max - 2) break;
Line 31... Line 70...
31
 
70
 
32
        x = (a << 2) | (b >> 4);
71
        x = (a << 2) | (b >> 4);
33
        y = ((b & 0x0f) << 4) | (c >> 2);
72
        y = ((b & 0x0f) << 4) | (c >> 2);
34
        z = ((c & 0x03) << 6) | d;
73
        z = ((c & 0x03) << 6) | d;
35
 
-
 
-
 
74
 
-
 
75
        if(len--) ptrOut[ptr++] = x; else break;
36
        if(len--) ptrOut[ptr++] = x; else break;
76
        if(len--) ptrOut[ptr++] = y; else break;
37
        if(len--) ptrOut[ptr++] = y; else break;
77
        if(len--) ptrOut[ptr++] = z; else break;
-
 
78
    }
-
 
79
    //decoded data
-
 
80
    unsigned char * decData;
38
        if(len--) ptrOut[ptr++] = z; else break;
81
    for (int a=0; a<ptr; a++) {
Line 39... Line 82...
39
    }
82
        if (len) {
40
 
83
            decData[decLen] = ptrOut[a];
Line 41... Line 84...
41
    for (int a=0; a<ptr; a++) {
84
            decLen++;
Line 42... Line 85...
42
        if (length == false) {
85
        } else {
43
            int b1, b2, b3;
86
            int b1, b2, b3;
Line 44... Line 87...
44
 
87
 
45
            b1 = ptrOut[a++];
-
 
46
            b2 = ptrOut[a];
-
 
47
 
-
 
48
            b3 = (b2 << 8) | b1;
88
            b1 = ptrOut[a++];
49
 
89
            b2 = ptrOut[a];
50
            if (b3 > 32767)
-
 
51
                b3 = b3 - 65536;
90
 
-
 
91
            b3 = (b2 << 8) | b1;
52
 
92
 
53
            rx.decode[decLen] = b3;
93
            if (b3 > 32767)
Line -... Line 94...
-
 
94
                b3 = b3 - 65536;
54
            decLen++;
95
 
-
 
96
            decData[decLen] = b3;
55
        } else {
97
            decLen++;
56
            rx.decode[decLen] = ptrOut[a];
98
        }
57
            decLen++;
99
    }
58
        }
100
    ptrOut = decData;
59
        rx.decLen = decLen;
101
    return decLen;
Line 81... Line 123...
81
        tx_buff[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
123
        tx_buff[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6));
82
        tx_buff[pt++] = '=' + ( c & 0x3f);
124
        tx_buff[pt++] = '=' + ( c & 0x3f);
83
    }
125
    }
84
    tx_buff[pt] = 0;
126
    tx_buff[pt] = 0;
Line -... Line 127...
-
 
127
 
85
 
128
    //move pointer of tx_buff to data
86
    return string(tx_buff);
129
    data = tx_buff;
Line 87... Line 130...
87
}
130
}
88
 
131
 
89
// Datensatz nach 8bit Integer
132
// Datensatz nach 8bit Integer
Line 94... Line 137...
94
      out = out - 256;
137
      out = out - 256;
Line 95... Line 138...
95
 
138
 
96
    return out;
139
    return out;
Line -... Line 140...
-
 
140
}
97
}
141
 
-
 
142
/**
98
 
143
 * received char to 8 bit integer
99
// received char to 8 bit integer
144
 */
100
int Parser::charToData(int data) {
145
int Parser::charToData(int data) {
101
    if (data < 0)
146
    if (data < 0)
102
        return data + 256;
147
        return data + 256;
Line -... Line 148...
-
 
148
    return data;
103
    return data;
149
}
-
 
150
 
104
}
151
/**
105
 
152
 * convert data to 16bit Integer
106
// convert data to 16bit Integer
153
 */
Line 107... Line 154...
107
int Parser::dataToInt(int *Data , int Start, bool is_signed)
154
int Parser::dataToInt(int *Data , int Start, bool is_signed)
Line 113... Line 160...
113
 
160
 
Line 114... Line 161...
114
    return Out;
161
    return Out;
Line -... Line 162...
-
 
162
 
115
 
163
}
-
 
164
 
116
}
165
/**
117
 
166
 * convert data to 32bit Long
118
// convert data to 32bit Long
167
 */
Line 119... Line 168...
119
long Parser::dataToLong(int *Data , int Start, bool is_signed)
168
long Parser::dataToLong(int *Data , int Start, bool is_signed)
Line 133... Line 182...
133
    float temp = value;
182
    float temp = value;
Line 134... Line 183...
134
 
183
 
135
    return temp / num;
184
    return temp / num;
Line 136... Line -...
136
}
-
 
137
 
185
}
138
string Parser::dataToString(int Data[150], int Start, int End)
-
 
139
{
-
 
140
    char String[150];
186
 
141
    for (int a = Start; a < End; a++)
-
 
142
    {
187
/**
143
        String[a - Start] = Data[a];
-
 
144
    }
-
 
145
    String[End - Start] = '\0';
-
 
146
 
-
 
147
    return string(String);
-
 
148
}
-
 
149
 
188
 * check CRC
150
// check CRC
189
 */
151
bool Parser::check_CRC(string RXstr)
-
 
152
{
190
bool Parser::check_CRC(char * rx, int length)
153
    int length = RXstr.size();
-
 
Line 154... Line 191...
154
    int CRC = 0;
191
{
155
    char *RX = (char *)RXstr.c_str();
192
    int CRC = 0;
156
 
193
 
157
    if (RX[1] == 127)
194
    if (rx[1] == 127)
Line 158... Line 195...
158
    {
195
    {
159
        RX[1] = 0;
196
        rx[1] = 0;
160
    }
197
    }
161
 
198
 
Line 162... Line 199...
162
    for(int i=0; i < length-2; i++)
199
    for(int i=0; i < length-2; i++)
Line 163... Line 200...
163
    {
200
    {
164
            CRC+=RX[i];
201
            CRC+=rx[i];
165
    }
202
    }
166
 
203
 
Line 167... Line 204...
167
    CRC = CRC % 4096;
204
    CRC = CRC % 4096;
168
 
205
 
169
    if(RX[length - 2] != ('=' + (CRC / 64)))
206
    if(rx[length - 2] != ('=' + (CRC / 64)))
170
    {
207
    {
Line 171... Line 208...
171
        return false;
208
        return false;
172
    }
209
    }
Line -... Line 210...
-
 
210
 
-
 
211
    if(rx[length - 1] != ('=' + CRC % 64))
173
 
212
    {
174
    if(RX[length - 1] != ('=' + CRC % 64))
213
        return false;
175
    {
214
    }
176
        return false;
-
 
177
    }
215
 
Line 178... Line -...
178
 
-
 
179
    return true;
-
 
180
}
216
    return true;
181
 
217
}
182
// add CRC
218
 
183
string Parser::add_CRC(string TX)
219
/**
Line 184... Line 220...
184
{
220
 * create CRC and add it to tx
Line 185... Line 221...
185
    int length = TX.size();
221
 */
186
    unsigned int tmpCRC = 0;
222
void Parser::add_CRC(char * tx, int length)
187
 
223
{
188
    char CRC[2];
-
 
189
 
-
 
190
    for(int i = 0; i < length; i++)
224
    unsigned int tmpCRC = 0;