2 /// \file r_servicebook.cc
3 /// Blackberry database record parser class for
4 /// Service Book records.
8 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include "r_servicebook.h"
24 #include "record-internal.h"
26 #include "protostructs.h"
36 #include "ios_state.h"
38 #define __DEBUG_MODE__
42 using namespace Barry::Protocol
;
46 ///////////////////////////////////////////////////////////////////////////////
47 // ServiceBookConfig class
49 // service book packed field codes
50 #define SBFCC_END 0xffff
52 static FieldLink
<ServiceBookConfig
> ServiceBookConfigFieldLinks
[] = {
53 // { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0 },
54 { SBFCC_END
, "End of List",0, 0, 0, 0, 0 }
57 ServiceBookConfig::ServiceBookConfig()
63 ServiceBookConfig::~ServiceBookConfig()
67 const unsigned char* ServiceBookConfig::ParseField(const unsigned char *begin
,
68 const unsigned char *end
,
79 const PackedField_02
*field
= (const PackedField_02
*) begin
;
83 begin
+= PACKED_FIELD_02_HEADER_SIZE
+ size
;
89 const PackedField_10
*field
= (const PackedField_10
*) begin
;
93 begin
+= PACKED_FIELD_10_HEADER_SIZE
+ size
;
98 eout("------> Unknown packed field format: 0x" << std::hex
<<
99 (unsigned int) Format
);
100 throw BadPackedFormat(Format
);
106 if( begin
> end
) // if begin==end, we are ok
109 if( !size
) // if field has no size, something's up
112 // cycle through the type table
113 for( FieldLink
<ServiceBookConfig
> *b
= ServiceBookConfigFieldLinks
;
114 b
->type
!= SBFCC_END
;
117 if( b
->type
== type
) {
119 std::string
&s
= this->*(b
->strMember
);
120 s
= ParseFieldString(raw
, size
-1);
121 return begin
; // done!
127 // handle special cases
133 // if still not handled, add to the Unknowns list
136 uf
.data
.assign((const char*)raw
, size
);
137 Unknowns
.push_back(uf
);
139 // return new pointer for next field
143 void ServiceBookConfig::ParseHeader(const Data
&data
, size_t &offset
)
145 MAKE_RECORD(const Barry::Protocol::ServiceBookConfigField
, sbc
, data
, offset
);
146 offset
+= SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
;
147 if( data
.GetSize() >= offset
) { // size check!
148 Format
= sbc
->format
;
152 void ServiceBookConfig::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
154 const unsigned char *finish
= ParseCommonFields(*this,
155 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
156 offset
+= finish
- (data
.GetData() + offset
);
159 void ServiceBookConfig::BuildHeader(Data
&data
, size_t &offset
) const
161 // make sure there is enough space
162 data
.GetBuffer(offset
+ SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
);
164 MAKE_RECORD(Barry::Protocol::ServiceBookConfigField
, sbc
, data
, offset
);
165 sbc
->format
= Format
;
167 offset
+= SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
;
173 /// Build fields part of record
175 void ServiceBookConfig::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
177 throw std::logic_error("ServiceBookConfig::Build not yet implemented");
180 void ServiceBookConfig::Clear()
186 void ServiceBookConfig::Dump(std::ostream
&os
) const
188 ios_format_state
state(os
);
190 os
<< " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format
<< "\n";
192 // cycle through the type table
193 for( const FieldLink
<ServiceBookConfig
> *b
= ServiceBookConfigFieldLinks
;
194 b
->type
!= SBFCC_END
;
198 const std::string
&s
= this->*(b
->strMember
);
200 os
<< " " << b
->name
<< ": " << s
<< "\n";
202 else if( b
->timeMember
) {
203 time_t t
= this->*(b
->timeMember
);
205 os
<< " " << b
->name
<< ": " << ctime(&t
);
209 // print any unknowns
211 os
<< " ------------------- End of Config Field\n";
215 ///////////////////////////////////////////////////////////////////////////////
218 // service book field codes
219 #define SBFC_OLD_NAME 0x01
220 #define SBFC_HIDDEN_NAME 0x02
221 #define SBFC_NAME 0x03
222 #define SBFC_OLD_UNIQUE_ID 0x06
223 #define SBFC_UNIQUE_ID 0x07
224 #define SBFC_CONTENT_ID 0x08
225 #define SBFC_CONFIG 0x09
226 #define SBFC_OLD_DESC 0x32
227 #define SBFC_DESCRIPTION 0x0f
228 #define SBFC_DSID 0xa1
229 #define SBFC_BES_DOMAIN 0xa2
230 #define SBFC_USER_ID 0xa3
231 #define SBFC_END 0xffff
233 // private data class, containing internal structures
234 class ServiceBookData
237 FieldLink
<ServiceBook
> *m_typeSet
;
238 ServiceBookData(FieldLink
<ServiceBook
> *typeSet
) : m_typeSet(typeSet
) {}
241 // The Old/New tables contain the same fields, but use different
242 // type codes. Keeping them separate yet linked makes it possible
243 // to convert between old and new type codes, while hopefully keeping
245 static FieldLink
<ServiceBook
> ServiceBookOldFieldLinks
[] = {
246 { SBFC_OLD_NAME
, "Old Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
247 { SBFC_OLD_DESC
, "Old Desc", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
248 { SBFC_OLD_UNIQUE_ID
, "Old UniqueId", 0, 0, &ServiceBook::UniqueId
, 0, 0, 0, 0, false },
249 { SBFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
252 static FieldLink
<ServiceBook
> ServiceBookNewFieldLinks
[] = {
253 { SBFC_NAME
, "Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
254 { SBFC_DESCRIPTION
, "Description", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
255 { SBFC_UNIQUE_ID
, "UniqueId", 0, 0, &ServiceBook::UniqueId
, 0, 0, 0, 0, false },
256 { SBFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
259 // This table holds all
260 static FieldLink
<ServiceBook
> ServiceBookFieldLinks
[] = {
261 { SBFC_HIDDEN_NAME
, "Hidden Name",0, 0, &ServiceBook::HiddenName
, 0, 0, 0, 0, true },
262 { SBFC_DSID
, "DSID", 0, 0, &ServiceBook::DSID
, 0, 0, 0, 0, false },
263 { SBFC_CONTENT_ID
, "ContentId", 0, 0, &ServiceBook::ContentId
, 0, 0, 0, 0, false },
264 { SBFC_BES_DOMAIN
, "BES Domain", 0, 0, &ServiceBook::BesDomain
, 0, 0, 0, 0, false },
265 { SBFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
268 // Array of conflicting tables only
269 static FieldLink
<ServiceBook
> *ServiceBookLinkTable
[] = {
270 ServiceBookOldFieldLinks
,
271 ServiceBookNewFieldLinks
,
275 #define FIELDLINK_END 0xffff
277 template <class RecordT
>
278 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
279 const CommonField
*field
,
280 const IConverter
*ic
,
281 FieldLink
<RecordT
> *links
)
283 // cycle through the type table
284 for( FieldLink
<RecordT
> *b
= links
; b
->type
!= FIELDLINK_END
; b
++ ) {
285 if( b
->type
== field
->type
) {
287 std::string
&s
= rec
->*(b
->strMember
);
289 dout(RecordT::GetDBName() << ": field '" << b
->name
<< "' already has data (" << s
<< "). Overwriting.");
291 s
= ParseFieldString(field
);
292 if( b
->iconvNeeded
&& ic
)
296 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
297 time_t &t
= rec
->*(b
->timeMember
);
298 t
= min2time(field
->u
.min1900
);
306 template <class RecordT
>
307 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
308 const CommonField
*field
,
309 const IConverter
*ic
,
310 FieldLink
<RecordT
> **b
)
313 FieldLink
<RecordT
> *link
=
314 ParseFieldByTable
<RecordT
>(rec
, field
, ic
, *b
);
321 ServiceBook::ServiceBook()
322 : m_data( new ServiceBookData(ServiceBookOldFieldLinks
) )
328 ServiceBook::~ServiceBook()
332 const unsigned char* ServiceBook::ParseField(const unsigned char *begin
,
333 const unsigned char *end
,
334 const IConverter
*ic
)
336 const CommonField
*field
= (const CommonField
*) begin
;
338 // advance and check size
339 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
340 if( begin
> end
) // if begin==end, we are ok
343 if( !btohs(field
->size
) ) // if field has no size, something's up
346 // cycle through the type tables
347 FieldLink
<ServiceBook
> *typeSet
=
348 ParseFieldByTable(this, field
, ic
, ServiceBookLinkTable
);
350 if( m_data
->m_typeSet
&& m_data
->m_typeSet
!= typeSet
) {
351 dout("ServiceBook record has a mix of old and new field types.");
353 m_data
->m_typeSet
= typeSet
;
357 if( ParseFieldByTable(this, field
, ic
, ServiceBookFieldLinks
) )
358 return begin
; // done!
361 // handle special cases
362 switch( field
->type
)
366 Data
config((const void *)field
->u
.raw
, btohs(field
->size
));
368 Config
.ParseHeader(config
, offset
);
369 Config
.ParseFields(config
, offset
);
370 return begin
; // success
372 catch( BadPackedFormat
& ) {
373 // break here so unprocessed raw packet is still
379 // if still not handled, add to the Unknowns list
381 uf
.type
= field
->type
;
382 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
383 Unknowns
.push_back(uf
);
385 // return new pointer for next field
389 void ServiceBook::ParseHeader(const Data
&data
, size_t &offset
)
391 // no header in this record (?)
394 void ServiceBook::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
396 const unsigned char *finish
= ParseCommonFields(*this,
397 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
398 offset
+= finish
- (data
.GetData() + offset
);
401 void ServiceBook::BuildHeader(Data
&data
, size_t &offset
) const
403 // no header in this record (?)
409 /// Build fields part of record
411 void ServiceBook::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
413 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
416 void ServiceBook::Clear()
418 m_data
->m_typeSet
= ServiceBookOldFieldLinks
;
423 std::string
ServiceBook::GetDescription() const
428 inline void FormatStr(std::ostream
&os
, const char *name
, const std::string
&str
)
430 ios_format_state
state(os
);
433 os
<< " " << setw(20) << name
;
434 os
<< ": " << str
<< "\n";
438 void ServiceBook::Dump(std::ostream
&os
) const
440 ios_format_state
state(os
);
445 os
<< "ServiceBook entry: 0x" << setbase(16) << RecordId
446 << " (" << (unsigned int)RecType
<< ")\n";
448 FormatStr(os
, "Name", Name
);
449 FormatStr(os
, "Hidden Name", HiddenName
);
450 FormatStr(os
, "Description", Description
);
451 FormatStr(os
, "DSID", DSID
);
452 FormatStr(os
, "Unique ID", UniqueId
);
453 FormatStr(os
, "Content ID", ContentId
);
454 FormatStr(os
, "(BES) Domain", BesDomain
);
458 // print any unknowns
462 bool ServiceBook::operator<(const ServiceBook
&other
) const
464 int cmp
= BesDomain
.compare(other
.BesDomain
);
466 cmp
= DSID
.compare(other
.DSID
);
468 cmp
= Name
.compare(other
.Name
);
470 cmp
= UniqueId
.compare(other
.UniqueId
);