2 /// \file r_servicebook.cc
3 /// Blackberry database record parser class for
4 /// Service Book records.
8 Copyright (C) 2005-2012, 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 dout("------> 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::Validate() const
163 void ServiceBookConfig::BuildHeader(Data
&data
, size_t &offset
) const
165 // make sure there is enough space
166 data
.GetBuffer(offset
+ SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
);
168 MAKE_RECORD(Barry::Protocol::ServiceBookConfigField
, sbc
, data
, offset
);
169 sbc
->format
= Format
;
171 offset
+= SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
;
177 /// Build fields part of record
179 void ServiceBookConfig::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
181 throw std::logic_error("ServiceBookConfig::Build not yet implemented");
184 void ServiceBookConfig::Clear()
190 void ServiceBookConfig::Dump(std::ostream
&os
) const
192 ios_format_state
state(os
);
194 os
<< " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format
<< "\n";
196 // cycle through the type table
197 for( const FieldLink
<ServiceBookConfig
> *b
= ServiceBookConfigFieldLinks
;
198 b
->type
!= SBFCC_END
;
202 const std::string
&s
= this->*(b
->strMember
);
204 os
<< " " << b
->name
<< ": " << s
<< "\n";
206 else if( b
->timeMember
) {
207 TimeT t
= this->*(b
->timeMember
);
209 os
<< " " << b
->name
<< ": " << t
<< "\n";
213 // print any unknowns
215 os
<< " ------------------- End of Config Field\n";
219 ///////////////////////////////////////////////////////////////////////////////
222 // service book field codes
223 #define SBFC_OLD_NAME 0x01
224 #define SBFC_HIDDEN_NAME 0x02
225 #define SBFC_NAME 0x03
226 #define SBFC_OLD_UNIQUE_ID 0x06
227 #define SBFC_UNIQUE_ID 0x07
228 #define SBFC_CONTENT_ID 0x08
229 #define SBFC_CONFIG 0x09
230 #define SBFC_OLD_DESC 0x32
231 #define SBFC_DESCRIPTION 0x0f
232 #define SBFC_DSID 0xa1
233 #define SBFC_BES_DOMAIN 0xa2
234 #define SBFC_USER_ID 0xa3
235 #define SBFC_END 0xffff
237 // private data class, containing internal structures
238 class ServiceBookData
241 FieldLink
<ServiceBook
> *m_typeSet
;
242 ServiceBookData(FieldLink
<ServiceBook
> *typeSet
) : m_typeSet(typeSet
) {}
245 // The Old/New tables contain the same fields, but use different
246 // type codes. Keeping them separate yet linked makes it possible
247 // to convert between old and new type codes, while hopefully keeping
249 static FieldLink
<ServiceBook
> ServiceBookOldFieldLinks
[] = {
250 { SBFC_OLD_NAME
, "Old Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
251 { SBFC_OLD_DESC
, "Old Desc", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
252 { SBFC_OLD_UNIQUE_ID
, "Old UniqueId", 0, 0, &ServiceBook::UniqueId
, 0, 0, 0, 0, false },
253 { SBFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
256 static FieldLink
<ServiceBook
> ServiceBookNewFieldLinks
[] = {
257 { SBFC_NAME
, "Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
258 { SBFC_DESCRIPTION
, "Description", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
259 { SBFC_UNIQUE_ID
, "UniqueId", 0, 0, &ServiceBook::UniqueId
, 0, 0, 0, 0, false },
260 { SBFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
263 // This table holds all
264 static FieldLink
<ServiceBook
> ServiceBookFieldLinks
[] = {
265 { SBFC_HIDDEN_NAME
, "Hidden Name",0, 0, &ServiceBook::HiddenName
, 0, 0, 0, 0, true },
266 { SBFC_DSID
, "DSID", 0, 0, &ServiceBook::DSID
, 0, 0, 0, 0, false },
267 { SBFC_CONTENT_ID
, "ContentId", 0, 0, &ServiceBook::ContentId
, 0, 0, 0, 0, false },
268 { SBFC_BES_DOMAIN
, "BES Domain", 0, 0, &ServiceBook::BesDomain
, 0, 0, 0, 0, false },
269 { SBFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
272 // Array of conflicting tables only
273 static FieldLink
<ServiceBook
> *ServiceBookLinkTable
[] = {
274 ServiceBookOldFieldLinks
,
275 ServiceBookNewFieldLinks
,
279 #define FIELDLINK_END 0xffff
281 template <class RecordT
>
282 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
283 const CommonField
*field
,
284 const IConverter
*ic
,
285 FieldLink
<RecordT
> *links
)
287 // cycle through the type table
288 for( FieldLink
<RecordT
> *b
= links
; b
->type
!= FIELDLINK_END
; b
++ ) {
289 if( b
->type
== field
->type
) {
291 std::string
&s
= rec
->*(b
->strMember
);
293 dout(RecordT::GetDBName() << ": field '" << b
->name
<< "' already has data (" << s
<< "). Overwriting.");
295 s
= ParseFieldString(field
);
296 if( b
->iconvNeeded
&& ic
)
300 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
301 TimeT
&t
= rec
->*(b
->timeMember
);
302 t
.Time
= min2time(field
->u
.min1900
);
310 template <class RecordT
>
311 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
312 const CommonField
*field
,
313 const IConverter
*ic
,
314 FieldLink
<RecordT
> **b
)
317 FieldLink
<RecordT
> *link
=
318 ParseFieldByTable
<RecordT
>(rec
, field
, ic
, *b
);
325 ServiceBook::ServiceBook()
326 : m_data( new ServiceBookData(ServiceBookOldFieldLinks
) )
332 ServiceBook::~ServiceBook()
336 const unsigned char* ServiceBook::ParseField(const unsigned char *begin
,
337 const unsigned char *end
,
338 const IConverter
*ic
)
340 const CommonField
*field
= (const CommonField
*) begin
;
342 // advance and check size
343 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
344 if( begin
> end
) // if begin==end, we are ok
347 if( !btohs(field
->size
) ) // if field has no size, something's up
350 // cycle through the type tables
351 FieldLink
<ServiceBook
> *typeSet
=
352 ParseFieldByTable(this, field
, ic
, ServiceBookLinkTable
);
354 if( m_data
->m_typeSet
&& m_data
->m_typeSet
!= typeSet
) {
355 dout("ServiceBook record has a mix of old and new field types.");
357 m_data
->m_typeSet
= typeSet
;
361 if( ParseFieldByTable(this, field
, ic
, ServiceBookFieldLinks
) )
362 return begin
; // done!
365 // handle special cases
366 switch( field
->type
)
370 Data
config((const void *)field
->u
.raw
, btohs(field
->size
));
372 Config
.ParseHeader(config
, offset
);
373 Config
.ParseFields(config
, offset
);
374 return begin
; // success
376 catch( BadPackedFormat
& ) {
377 // break here so unprocessed raw packet is still
383 // if still not handled, add to the Unknowns list
385 uf
.type
= field
->type
;
386 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
387 Unknowns
.push_back(uf
);
389 // return new pointer for next field
393 void ServiceBook::ParseHeader(const Data
&data
, size_t &offset
)
395 // no header in this record (?)
398 void ServiceBook::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
400 const unsigned char *finish
= ParseCommonFields(*this,
401 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
402 offset
+= finish
- (data
.GetData() + offset
);
405 void ServiceBook::Validate() const
410 void ServiceBook::BuildHeader(Data
&data
, size_t &offset
) const
412 // no header in this record (?)
418 /// Build fields part of record
420 void ServiceBook::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
422 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
425 void ServiceBook::Clear()
427 m_data
->m_typeSet
= ServiceBookOldFieldLinks
;
432 const FieldHandle
<ServiceBook
>::ListT
& ServiceBook::GetFieldHandles()
434 static FieldHandle
<ServiceBook
>::ListT fhv
;
439 #undef CONTAINER_OBJECT_NAME
440 #define CONTAINER_OBJECT_NAME fhv
442 #undef RECORD_CLASS_NAME
443 #define RECORD_CLASS_NAME ServiceBook
445 FHP(RecType
, "Record Type Code");
446 FHP(RecordId
, "Unique Record ID");
449 FHP(HiddenName
, "Hidden Name");
450 FHP(Description
, "Description");
452 FHP(BesDomain
, "BES Domain");
453 FHP(UniqueId
, "Unique ID");
454 FHP(ContentId
, "Content ID");
456 // FIXME - this config is not yet implmented fully... when it is,
457 // will need to add this as a field to FieldHandle<>, and maybe
458 // implement a ServiceBookConfig::GetFieldHandles()
459 //ServiceBookConfig Config;
461 FHP(Unknowns
, "Unknown Fields");
466 std::string
ServiceBook::GetDescription() const
471 inline void FormatStr(std::ostream
&os
, const char *name
, const std::string
&str
)
473 ios_format_state
state(os
);
476 os
<< " " << setw(20) << name
;
477 os
<< ": " << str
<< "\n";
481 void ServiceBook::Dump(std::ostream
&os
) const
483 ios_format_state
state(os
);
488 os
<< "ServiceBook entry: 0x" << setbase(16) << RecordId
489 << " (" << (unsigned int)RecType
<< ")\n";
491 FormatStr(os
, "Name", Name
);
492 FormatStr(os
, "Hidden Name", HiddenName
);
493 FormatStr(os
, "Description", Description
);
494 FormatStr(os
, "DSID", DSID
);
495 FormatStr(os
, "Unique ID", UniqueId
);
496 FormatStr(os
, "Content ID", ContentId
);
497 FormatStr(os
, "(BES) Domain", BesDomain
);
501 // print any unknowns
505 bool ServiceBook::operator<(const ServiceBook
&other
) const
507 int cmp
= BesDomain
.compare(other
.BesDomain
);
509 cmp
= DSID
.compare(other
.DSID
);
511 cmp
= Name
.compare(other
.Name
);
513 cmp
= UniqueId
.compare(other
.UniqueId
);