Rev 358 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 358 | Rev 391 | ||
---|---|---|---|
Line 17... | Line 17... | ||
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
17 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * |
18 | ***************************************************************************/ |
18 | ***************************************************************************/ |
Line 19... | Line 19... | ||
19 | 19 | ||
Line 20... | Line -... | ||
20 | #include "ToolBox.h" |
- | |
21 | 20 | #include "ToolBox.h" |
|
- | 21 | ||
22 | ToolBox::ToolBox() |
22 | /** |
Line -... | Line 23... | ||
- | 23 | * specific data for QT |
|
23 | { |
24 | */ |
24 | } |
25 | |
25 | 26 | //FIXME: search for something else than sleep |
|
26 | void ToolBox::Wait(int Time) |
27 | void ToolBox::wait(int Time) |
27 | { |
28 | { |
- | 29 | #ifndef _WIN32_ |
|
- | 30 | usleep(Time); |
|
28 | #ifndef _WIN32_ |
31 | #else |
29 | usleep(Time); |
32 | //FIXME: implement sleep for win_32 |
30 | #else |
33 | //(or kick this function completely) |
Line 31... | Line 34... | ||
31 | // sleep(Time); |
34 | // sleep(Time); |
Line 38... | Line 41... | ||
38 | 41 | ||
Line 39... | Line 42... | ||
39 | s_Wert = QString("%1").arg(Wert); |
42 | s_Wert = QString("%1").arg(Wert); |
Line 40... | Line -... | ||
40 | - | ||
41 | Temp = s_Wert.left(s_Wert.length() - Count) + QString(".") + s_Wert.right(Count); |
- | |
42 | - | ||
43 | /* |
- | |
44 | if (Wert > 0) |
- | |
45 | Temp = ""; |
- | |
46 | else |
- | |
47 | Temp = "-"; |
- | |
48 | 43 | ||
49 | Temp = Temp + QString("%1").arg(Wert / Count) + "." + QString("%1").arg(Wert % Count); |
44 | Temp = s_Wert.left(s_Wert.length() - Count) + QString(".") + s_Wert.right(Count); |
Line 50... | Line -... | ||
50 | */ |
- | |
51 | return Temp; |
- | |
52 | } |
- | |
53 | - | ||
54 | - | ||
55 | // Base64 Decoder |
- | |
56 | bool ToolBox::Decode64(sRxData &RX, bool Long) |
- | |
57 | { |
- | |
58 | unsigned char a,b,c,d; |
- | |
59 | unsigned char ptr = 0; |
- | |
60 | unsigned char x,y,z; |
- | |
61 | int ptrOut[150]; |
- | |
62 | - | ||
63 | int ptrIn = 3; |
- | |
64 | int max = RX.String.length(); |
- | |
65 | int len = RX.String.length(); |
- | |
66 | int DecLen = 0; |
- | |
67 | - | ||
68 | if (RX.Input[ptrIn] == 0) |
- | |
69 | { |
- | |
70 | // qDebug("QString to Char ERROR...!!!!"); |
- | |
71 | return false; |
- | |
72 | } |
- | |
73 | - | ||
74 | while(len != 0) |
- | |
75 | { |
- | |
76 | a = RX.Input[ptrIn++] - '='; |
- | |
77 | b = RX.Input[ptrIn++] - '='; |
- | |
78 | c = RX.Input[ptrIn++] - '='; |
- | |
79 | d = RX.Input[ptrIn++] - '='; |
- | |
80 | - | ||
81 | if(ptrIn > max - 2) break; // nicht mehr Daten verarbeiten, als empfangen wurden |
- | |
82 | - | ||
83 | x = (a << 2) | (b >> 4); |
- | |
84 | y = ((b & 0x0f) << 4) | (c >> 2); |
- | |
85 | z = ((c & 0x03) << 6) | d; |
- | |
86 | - | ||
87 | if(len--) ptrOut[ptr++] = x; else break; |
- | |
88 | if(len--) ptrOut[ptr++] = y; else break; |
- | |
89 | if(len--) ptrOut[ptr++] = z; else break; |
- | |
90 | } |
- | |
91 | - | ||
92 | for (int a=0; a<ptr; a++) |
- | |
93 | { |
- | |
94 | if (Long == false) |
- | |
95 | { |
- | |
96 | int b1, b2, b3; |
- | |
97 | - | ||
98 | b1 = ptrOut[a++]; |
- | |
99 | b2 = ptrOut[a]; |
- | |
100 | - | ||
101 | b3 = (b2 << 8) | b1; |
- | |
102 | - | ||
103 | if (b3 > 32767) |
- | |
104 | b3 = b3 - 65536; |
- | |
105 | - | ||
106 | RX.Decode[DecLen] = b3; |
- | |
107 | DecLen++; |
- | |
108 | } |
- | |
109 | else |
- | |
110 | { |
- | |
111 | RX.Decode[DecLen] = ptrOut[a]; |
- | |
112 | DecLen++; |
- | |
113 | } |
- | |
114 | - | ||
115 | RX.DecLen = DecLen; |
- | |
116 | } |
- | |
117 | return true; |
- | |
118 | } |
- | |
119 | - | ||
120 | // Base64 Encoder |
- | |
121 | QString ToolBox::Encode64(char Data[150],unsigned int Length) |
- | |
122 | { |
- | |
123 | unsigned int pt = 0; |
- | |
124 | unsigned char a,b,c; |
- | |
125 | unsigned char ptr = 0; |
- | |
126 | - | ||
127 | char TX_Buff[150]; |
- | |
128 | - | ||
129 | while(Length > 0) |
- | |
130 | { |
- | |
131 | if(Length) { a = Data[ptr++]; Length--;} else a = 0; |
- | |
132 | if(Length) { b = Data[ptr++]; Length--;} else b = 0; |
- | |
133 | if(Length) { c = Data[ptr++]; Length--;} else c = 0; |
- | |
134 | - | ||
135 | TX_Buff[pt++] = '=' + (a >> 2); |
- | |
136 | TX_Buff[pt++] = '=' + (((a & 0x03) << 4) | ((b & 0xf0) >> 4)); |
- | |
137 | TX_Buff[pt++] = '=' + (((b & 0x0f) << 2) | ((c & 0xc0) >> 6)); |
- | |
138 | TX_Buff[pt++] = '=' + ( c & 0x3f); |
- | |
139 | } |
- | |
140 | TX_Buff[pt] = 0; |
- | |
141 | - | ||
142 | return QString(TX_Buff); |
- | |
143 | } |
- | |
144 | - | ||
145 | // Datensatz nach 8bit Integer |
- | |
146 | int ToolBox::Data2Char(int *Data , int Start, bool is_signed) |
- | |
147 | { |
- | |
148 | int Out = (Data[Start]); |
- | |
149 | - | ||
150 | if ((Out > 128) && (is_signed)) |
- | |
151 | Out = Out - 256; |
- | |
152 | - | ||
153 | return Out; |
- | |
154 | - | ||
155 | } |
- | |
156 | - | ||
157 | // Datensatz nach 8bit Integer |
- | |
158 | int ToolBox::Char2Data(int Data) |
- | |
159 | { |
- | |
160 | if (Data < 0) |
- | |
161 | { |
- | |
162 | return Data + 256; |
- | |
163 | } |
- | |
164 | return Data; |
- | |
165 | } |
- | |
166 | - | ||
167 | - | ||
168 | // Datensatz nach 16bit Integer |
- | |
169 | int ToolBox::Data2Int(int *Data , int Start, bool is_signed) |
- | |
170 | { |
- | |
171 | int Out = (Data[Start+1]<<8) | (Data[Start+0]); |
- | |
172 | - | ||
173 | if ((Out > 32767) && (is_signed)) |
- | |
174 | Out = Out - 65536; |
- | |
175 | - | ||
176 | return Out; |
- | |
177 | - | ||
178 | } |
- | |
179 | - | ||
180 | // Datensatz nach 32bit Long |
- | |
181 | long ToolBox::Data2Long(int *Data , int Start, bool is_signed) |
- | |
182 | { |
- | |
183 | long Out = (Data[Start+3]<<24) | (Data[Start+2]<<16) | (Data[Start+1]<<8) | (Data[Start+0]); |
- | |
184 | - | ||
185 | if ((Out > 32767) && (is_signed)) |
- | |
186 | Out = Out; |
- | |
187 | 45 | ||
188 | return Out; |
46 | return Temp; |
189 | } |
- | |
190 | - | ||
191 | // Datensatz nach QString |
- | |
192 | QString ToolBox::Data2QString(int Data[150], int Start, int End) |
- | |
193 | { |
- | |
194 | char String[150]; |
- | |
195 | for (int a = Start; a < End; a++) |
- | |
196 | { |
- | |
197 | String[a - Start] = Data[a]; |
- | |
198 | } |
- | |
199 | String[End - Start] = '\0'; |
- | |
200 | - | ||
201 | return QString(String); |
- | |
202 | } |
47 | } |
203 | - | ||
204 | // Datensatz-CRC prüfen |
- | |
205 | bool ToolBox::check_CRC(QString RXString) |
- | |
206 | { |
- | |
207 | int CRC = 0; |
- | |
208 | char *RX; |
- | |
209 | - | ||
210 | int Length = RXString.length(); |
- | |
211 | - | ||
212 | RX = RXString.toLatin1().data(); |
- | |
213 | - | ||
214 | if (RX[1] == 127) |
- | |
215 | { |
- | |
216 | RX[1] = 0; |
- | |
217 | } |
- | |
218 | - | ||
219 | for(int i=0; i < Length-2; i++) |
- | |
220 | { |
- | |
221 | CRC+=RX[i]; |
- | |
222 | } |
- | |
223 | - | ||
224 | CRC = CRC % 4096; |
- | |
225 | - | ||
226 | if(RX[Length - 2] != ('=' + (CRC / 64))) |
- | |
227 | { |
- | |
228 | return false; |
- | |
229 | } |
- | |
230 | - | ||
231 | if(RX[Length - 1] != ('=' + CRC % 64)) |
- | |
232 | { |
- | |
233 | return false; |
- | |
234 | } |
- | |
235 | - | ||
236 | return true; |
- | |
237 | } |
- | |
238 | - | ||
239 | // Datensatz-CRC hinzufügen |
- | |
240 | QString ToolBox::add_CRC(QString TXString) |
- | |
241 | { |
- | |
242 | unsigned int tmpCRC = 0; |
- | |
243 | - | ||
244 | char *TXBuff; |
- | |
245 | char CRC[2]; |
- | |
246 | - | ||
247 | TXBuff = TXString.toLatin1().data(); |
- | |
248 | - | ||
249 | for(int i = 0; i < TXString.length(); i++) |
- | |
250 | { |
- | |
251 | tmpCRC += TXBuff[i]; |
- | |
252 | } |
- | |
253 | - | ||
254 | tmpCRC %= 4096; |
- | |
255 | - | ||
256 | CRC[0] = '=' + tmpCRC / 64; |
48 | |
257 | CRC[1] = '=' + tmpCRC % 64; |
- | |
258 | CRC[2] = '\0'; |
- | |
259 | 49 | // Datensatz nach QString |
|
Line 260... | Line 50... | ||
260 | QString Return = TXString + QString(CRC); |
50 | QString ToolBox::dataToQString(int Data[150], int Start, int End) |
261 | 51 | { |
|
262 | return Return; |
52 | return QString(Parser::dataToString(Data, Start, End)); |