3 /// Blackberry database record parser class for Handheld Agent records
7 Copyright (C) 2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "r_hhagent.h"
23 #include "record-internal.h"
24 #include "protostructs.h"
31 #include "ios_state.h"
33 #define __DEBUG_MODE__
37 using namespace Barry::Protocol
;
42 ///////////////////////////////////////////////////////////////////////////////
43 // HandheldAgent class
46 #define HHAFC_END 0xffff
48 // Field codes for record 3000000
49 #define HHAFC3_MODEL 0x03
50 #define HHAFC3_BANDS 0x07
51 #define HHAFC3_VERSION 0x08
52 #define HHAFC3_NETWORK 0x0f
53 #define HHAFC3_PIN 0x10
54 #define HHAFC3_MEID 0x11
56 // Field codes for record 7000000
57 #define HHAFC7_FIRMWARE 0x13
58 #define HHAFC7_MANUFACTURER 0x14
59 #define HHAFC7_MODEL 0x15
60 #define HHAFC7_PLATFORM 0x17
62 // These fields are only valid for RecordId 0x3000000
63 static FieldLink
<HandheldAgent
> HandheldAgentFieldLinks_3000000
[] = {
64 { HHAFC3_MODEL
, "Model", 0, 0, &HandheldAgent::Model
, 0, 0, 0, 0, true },
65 { HHAFC3_NETWORK
, "Network", 0, 0, &HandheldAgent::Network
, 0, 0, 0, 0, true },
66 { HHAFC3_BANDS
, "Bands", 0, 0, &HandheldAgent::Bands
, 0, 0, 0, 0, true },
67 { HHAFC3_MEID
, "MEID/ESN", 0, 0, &HandheldAgent::MEID
, 0, 0, 0, 0, true },
68 { HHAFC3_PIN
, "PIN", 0, 0, &HandheldAgent::Pin
, 0, 0, 0, 0, true },
69 { HHAFC3_VERSION
, "Version",0, 0, &HandheldAgent::Version
, 0, 0, 0, 0, true },
70 { HHAFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
73 // These fields are only for RecordId 0x4000000
74 static FieldLink
<HandheldAgent
> HandheldAgentFieldLinks_4000000
[] = {
75 { HHAFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
78 // These fields are only for RecordId 0x5000000
79 static FieldLink
<HandheldAgent
> HandheldAgentFieldLinks_5000000
[] = {
80 { HHAFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
83 // These fields are only for RecordId 0x7000000
84 static FieldLink
<HandheldAgent
> HandheldAgentFieldLinks_7000000
[] = {
85 { HHAFC7_MODEL
, "Model", 0, 0, &HandheldAgent::Model
, 0, 0, 0, 0, true },
86 { HHAFC7_MANUFACTURER
,"Manufacturer",0,0,&HandheldAgent::Manufacturer
,0, 0, 0, 0, true },
87 { HHAFC7_FIRMWARE
, "Firmware", 0, 0, &HandheldAgent::Version
, 0, 0, 0, 0, true },
88 { HHAFC7_PLATFORM
, "Platform", 0, 0, &HandheldAgent::PlatformVersion
, 0, 0, 0, 0, true },
89 { HHAFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
92 // Use this table for default application style records
93 static FieldLink
<HandheldAgent
> HandheldAgentFieldLinks_Default
[] = {
94 { HHAFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
97 // Use this for display / Dump() etc... includes all fields
98 static FieldLink
<HandheldAgent
> HandheldAgentFieldLinks_All
[] = {
99 { 0, "Model", 0, 0, &HandheldAgent::Model
, 0, 0, 0, 0, true },
100 { 0, "Network", 0, 0, &HandheldAgent::Network
, 0, 0, 0, 0, true },
101 { 0, "Manufacturer",0,0, &HandheldAgent::Manufacturer
,0, 0, 0, 0, true },
102 { 0, "Bands", 0, 0, &HandheldAgent::Bands
, 0, 0, 0, 0, true },
103 { 0, "MEID/ESN", 0, 0, &HandheldAgent::MEID
, 0, 0, 0, 0, true },
104 { 0, "PIN", 0, 0, &HandheldAgent::Pin
, 0, 0, 0, 0, true },
105 { 0, "Version", 0, 0, &HandheldAgent::Version
, 0, 0, 0, 0, true },
106 { 0, "Platform", 0, 0, &HandheldAgent::PlatformVersion
, 0, 0, 0, 0, true },
107 { HHAFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
110 HandheldAgent::HandheldAgent()
115 HandheldAgent::~HandheldAgent()
119 const unsigned char* HandheldAgent::ParseField(const unsigned char *begin
,
120 const unsigned char *end
,
121 const IConverter
*ic
)
123 const CommonField
*field
= (const CommonField
*) begin
;
125 // advance and check size
126 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
127 if( begin
> end
) // if begin==end, we are ok
130 if( !btohs(field
->size
) ) // if field has no size, something's up
133 // cycle through the type table
134 FieldLink
<HandheldAgent
> *b
= HandheldAgentFieldLinks_Default
;
135 if( RecordId
== 0 ) {
136 // internal consistency check... all parsing code should
137 // call SetIds() first, and HandheldAgent relies on this,
138 // so double check, and throw if not
139 throw std::logic_error("HandheldAgent requires SetIds() to be called before ParseField()");
141 else if( RecordId
== GetMEIDRecordId() ) {
142 b
= HandheldAgentFieldLinks_3000000
;
144 else if( RecordId
== GetUnknown1RecordId() ) {
145 b
= HandheldAgentFieldLinks_4000000
;
147 else if( RecordId
== GetUnknown2RecordId() ) {
148 b
= HandheldAgentFieldLinks_5000000
;
150 else if( RecordId
== GetUnknown3RecordId() ) {
151 b
= HandheldAgentFieldLinks_7000000
;
154 for( ; b
->type
!= HHAFC_END
; b
++ ) {
155 if( b
->type
== field
->type
) {
157 std::string
&s
= this->*(b
->strMember
);
158 s
= ParseFieldString(field
);
159 if( b
->iconvNeeded
&& ic
)
161 return begin
; // done!
163 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
164 time_t &t
= this->*(b
->timeMember
);
165 dout("min1900: " << field
->u
.min1900
);
166 t
= min2time(field
->u
.min1900
);
169 else if( b
->addrMember
) {
171 // parse email address
172 // get dual addr+name string first
173 // Note: this is a different format than
174 // used in r_message*.cc
176 std::string
dual((const char*)field
->u
.raw
, btohs(field
->size
));
180 // assign first string, using null terminator
181 // letting std::string add it for us if it
183 a
.Email
= dual
.c_str();
185 // assign second string, using first size
187 a
.Name
= dual
.c_str() + a
.Email
.size() + 1;
189 // if the address is non-empty, add to list
191 // i18n convert if needed
192 if( b
->iconvNeeded
&& ic
) {
193 a
.Name
= ic
->FromBB(a
.Name
);
194 a
.Email
= ic
->FromBB(a
.Email
);
197 EmailAddressList
&al
= this->*(b
->addrMember
);
206 // handle special cases
207 // switch( field->type )
211 // if still not handled, add to the Unknowns list
213 uf
.type
= field
->type
;
214 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
215 Unknowns
.push_back(uf
);
217 // return new pointer for next field
221 void HandheldAgent::ParseHeader(const Data
&data
, size_t &offset
)
223 // no header in HandheldAgent records
226 void HandheldAgent::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
228 const unsigned char *finish
= ParseCommonFields(*this,
229 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
230 offset
+= finish
- (data
.GetData() + offset
);
233 void HandheldAgent::BuildHeader(Data
&data
, size_t &offset
) const
235 // no header in HandheldAgent records
241 /// Build fields part of record.
243 void HandheldAgent::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
247 void HandheldAgent::Clear()
250 RecType
= GetDefaultRecType();
258 PlatformVersion
.clear();
259 Manufacturer
.clear();
265 std::string
HandheldAgent::GetDescription() const
268 oss
<< "Handheld Agent: 0x" << hex
<< RecordId
;
272 void HandheldAgent::Dump(std::ostream
&os
) const
274 ios_format_state
state(os
);
276 os
<< "HandheldAgent entry: 0x" << hex
<< RecordId
277 << " (" << (unsigned int)RecType
<< ")\n";
279 // cycle through the type table
280 for( const FieldLink
<HandheldAgent
> *b
= HandheldAgentFieldLinks_All
;
281 b
->type
!= HHAFC_END
;
285 const std::string
&s
= this->*(b
->strMember
);
287 os
<< " " << b
->name
<< ": " << s
<< "\n";
289 else if( b
->timeMember
) {
290 time_t t
= this->*(b
->timeMember
);
292 os
<< " " << b
->name
<< ": " << ctime(&t
);
294 os
<< " " << b
->name
<< ": disabled\n";
296 else if( b
->addrMember
) {
297 const EmailAddressList
&al
= this->*(b
->addrMember
);
298 EmailAddressList::const_iterator lb
= al
.begin(), le
= al
.end();
300 for( ; lb
!= le
; ++lb
) {
304 os
<< " " << b
->name
<< ": " << *lb
<< "\n";
309 // print any unknowns
313 bool HandheldAgent::operator<(const HandheldAgent
&other
) const
315 return RecordId
< other
.RecordId
;
318 bool HandheldAgent::IsSpecial(uint32_t record_id
)
321 record_id
== GetMEIDRecordId() ||
322 record_id
== GetUnknown1RecordId() ||
323 record_id
== GetUnknown2RecordId() ||
324 record_id
== GetUnknown3RecordId();
328 // The ESN number is in two parts. When in decimal, the first 3
329 // characters are one number, then the rest. In hex, the first 2
330 // digits are the same number, then the rest. Both are padded
333 // For example, hex: 4c070068 dec: 07600458856
334 // hex: [4c]070068 dec: [076]00458856
336 // Returns an empty string on error.
339 bool HandheldAgent::IsESNHex(const std::string
&esn
)
341 const char *hex
= "0123456789ABCDEFabcdef";
342 size_t npos
= string::npos
;
344 if( esn
.size() == 8 && esn
.find_first_not_of(hex
) == npos
) {
351 bool HandheldAgent::IsESNDec(const std::string
&esn
)
353 const char *dec
= "0123456789";
354 size_t npos
= string::npos
;
356 if( esn
.size() == 11 && esn
.find_first_not_of(dec
) == npos
) {
363 std::string
HandheldAgent::ESNDec2Hex(const std::string
&esn
)
367 if( esn
.size() != 11 )
370 unsigned int part1
, part2
;
371 istringstream
iss(esn
.substr(0, 3));
375 iss
.str(esn
.substr(3));
382 oss
<< setfill('0') << setw(2) << hex
<< part1
;
383 oss
<< setfill('0') << setw(6) << hex
<< part2
;
389 std::string
HandheldAgent::ESNHex2Dec(const std::string
&esn
)
393 if( esn
.size() != 8 )
396 unsigned int part1
, part2
;
397 istringstream
iss(esn
.substr(0, 2));
401 iss
.str(esn
.substr(2));
408 oss
<< setfill('0') << setw(3) << dec
<< part1
;
409 oss
<< setfill('0') << setw(8) << dec
<< part2
;