Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
158 | KeyOz | 1 | /*************************************************************************** |
2 | * Copyright (C) 2008 by Manuel Schrape * |
||
3 | * manuel.schrape@gmx.de * |
||
4 | * * |
||
5 | * This program is free software; you can redistribute it and/or modify * |
||
6 | * it under the terms of the GNU General Public License as published by * |
||
7 | * the Free Software Foundation; either version 2 of the License. * |
||
8 | * * |
||
9 | * This program is distributed in the hope that it will be useful, * |
||
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
||
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
||
12 | * GNU General Public License for more details. * |
||
13 | * * |
||
14 | * You should have received a copy of the GNU General Public License * |
||
15 | * along with this program; if not, write to the * |
||
16 | * Free Software Foundation, Inc., * |
||
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
||
18 | ***************************************************************************/ |
||
19 | |||
20 | #include "ToolBox.h" |
||
21 | |||
22 | ToolBox::ToolBox() |
||
23 | { |
||
24 | } |
||
25 | |||
26 | bool ToolBox::Decode64(sRxData &RX, bool Long) |
||
27 | { |
||
28 | unsigned char a,b,c,d; |
||
29 | unsigned char ptr = 0; |
||
30 | unsigned char x,y,z; |
||
31 | int ptrOut[150]; |
||
32 | |||
33 | int ptrIn = 3; |
||
34 | int max = RX.String.length(); |
||
35 | int len = RX.String.length(); |
||
36 | int DecLen = 0; |
||
37 | |||
38 | if (RX.Input[ptrIn] == 0) |
||
39 | { |
||
40 | qDebug("QString to Char ERROR...!!!!"); |
||
41 | return false; |
||
42 | } |
||
43 | |||
44 | while(len != 0) |
||
45 | { |
||
46 | a = RX.Input[ptrIn++] - '='; |
||
47 | b = RX.Input[ptrIn++] - '='; |
||
48 | c = RX.Input[ptrIn++] - '='; |
||
49 | d = RX.Input[ptrIn++] - '='; |
||
50 | |||
51 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
||
52 | |||
53 | x = (a << 2) | (b >> 4); |
||
54 | y = ((b & 0x0f) << 4) | (c >> 2); |
||
55 | z = ((c & 0x03) << 6) | d; |
||
56 | |||
57 | if(len--) ptrOut[ptr++] = x; else break; |
||
58 | if(len--) ptrOut[ptr++] = y; else break; |
||
59 | if(len--) ptrOut[ptr++] = z; else break; |
||
60 | } |
||
61 | |||
62 | for (int a=0; a<ptr; a++) |
||
63 | { |
||
64 | if (Long == false) |
||
65 | { |
||
66 | int b1, b2, b3; |
||
67 | |||
68 | b1 = ptrOut[a++]; |
||
69 | b2 = ptrOut[a]; |
||
70 | |||
71 | b3 = (b2 << 8) | b1; |
||
72 | |||
73 | if (b3 > 32767) |
||
74 | b3 = b3 - 65536; |
||
75 | |||
76 | RX.Decode[DecLen] = b3; |
||
77 | DecLen++; |
||
78 | } |
||
79 | else |
||
80 | { |
||
81 | RX.Decode[DecLen] = ptrOut[a]; |
||
82 | DecLen++; |
||
83 | } |
||
84 | } |
||
85 | return true; |
||
86 | } |
||
87 | |||
88 | int ToolBox::Data2Int(int *Data , int Start, bool is_signed) |
||
89 | { |
||
90 | int Out = (Data[Start+1]<<8) | (Data[Start+0]); |
||
91 | |||
92 | if ((Out > 32767) && (is_signed)) |
||
93 | Out = Out - 65536; |
||
94 | |||
95 | return Out; |
||
96 | |||
97 | } |
||
98 | |||
99 | long ToolBox::Data2Long(int *Data , int Start, bool is_signed) |
||
100 | { |
||
101 | long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]); |
||
102 | |||
103 | return Out; |
||
104 | } |
||
105 | |||
159 | KeyOz | 106 | QString ToolBox::Data2QString(int Data[150], int Start, int End) |
107 | { |
||
108 | char String[150]; |
||
109 | for (int a = Start; a < End; a++) |
||
110 | { |
||
111 | String[a - Start] = Data[a]; |
||
112 | } |
||
113 | String[End - Start] = '\0'; |
||
114 | |||
115 | return QString(String); |
||
116 | } |
||
117 | |||
158 | KeyOz | 118 | bool ToolBox::check_CRC(QString RXString) |
119 | { |
||
120 | int CRC = 0; |
||
121 | char *RX; |
||
122 | |||
123 | int Length = RXString.length(); |
||
124 | |||
125 | RX = RXString.toLatin1().data(); |
||
126 | |||
127 | if (RX[1] == 127) |
||
128 | { |
||
129 | RX[1] = 0; |
||
130 | } |
||
131 | |||
132 | for(int i=0; i < Length-2; i++) |
||
133 | { |
||
134 | CRC+=RX[i]; |
||
135 | } |
||
136 | |||
137 | CRC = CRC % 4096; |
||
138 | |||
139 | if(RX[Length - 2] != ('=' + (CRC / 64))) |
||
140 | { |
||
141 | return false; |
||
142 | } |
||
143 | |||
144 | if(RX[Length - 1] != ('=' + CRC % 64)) |
||
145 | { |
||
146 | return false; |
||
147 | } |
||
148 | |||
149 | return true; |
||
150 | } |
||
151 | |||
152 | QString ToolBox::add_CRC(QString TXString) |
||
153 | { |
||
154 | unsigned int tmpCRC = 0; |
||
155 | |||
156 | char *TXBuff; |
||
157 | char CRC[2]; |
||
158 | |||
159 | TXBuff = TXString.toLatin1().data(); |
||
160 | |||
161 | for(int i = 0; i < TXString.length(); i++) |
||
162 | { |
||
163 | tmpCRC += TXBuff[i]; |
||
164 | } |
||
165 | |||
166 | tmpCRC %= 4096; |
||
167 | |||
168 | CRC[0] = '=' + tmpCRC / 64; |
||
169 | CRC[1] = '=' + tmpCRC % 64; |
||
170 | CRC[2] = '\0'; |
||
171 | |||
172 | QString Return = TXString + QString(CRC); |
||
173 | |||
174 | return Return; |
||
175 | } |
||
176 | |||
177 | QString ToolBox::Encode64(char Data[150],unsigned int Length) |
||
178 | { |
||
179 | unsigned int pt = 0; |
||
180 | unsigned char a,b,c; |
||
181 | unsigned char ptr = 0; |
||
182 | |||
183 | char TX_Buff[150]; |
||
184 | |||
185 | while(Length > 0) |
||
186 | { |
||
187 | if(Length) { a = Data[ptr++]; Length--;} else a = 0; |
||
188 | if(Length) { b = Data[ptr++]; Length--;} else b = 0; |
||
189 | if(Length) { c = Data[ptr++]; Length--;} else c = 0; |
||
190 | |||
191 | TX_Buff[pt++] = '=' + (a >> 2); |
||
192 | TX_Buff[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
||
193 | TX_Buff[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
||
194 | TX_Buff[pt++] = '=' + ( c & 0x3f); |
||
195 | } |
||
196 | TX_Buff[pt] = 0; |
||
197 | |||
198 | return QString(TX_Buff); |
||
199 | } |
||
200 | |||
201 | QString ToolBox::DataToString(int Data[150]) |
||
202 | { |
||
203 | char String[100]; |
||
204 | for (int a = 0; a < 100; a++) |
||
205 | { |
||
206 | String[a] = Data[a]; |
||
207 | } |
||
208 | |||
209 | return QString(String).trimmed(); |
||
210 | } |