2 /// \file r_servicebook.cc
3 /// Blackberry database record parser class for
4 /// Service Book records.
8 Copyright (C) 2005-2010, 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"
37 #define __DEBUG_MODE__
41 using namespace Barry::Protocol
;
45 ///////////////////////////////////////////////////////////////////////////////
46 // ServiceBookConfig class
48 // service book packed field codes
49 #define SBFCC_END 0xffff
51 static FieldLink
<ServiceBookConfig
> ServiceBookConfigFieldLinks
[] = {
52 // { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0 },
53 { SBFCC_END
, "End of List",0, 0, 0, 0, 0 }
56 ServiceBookConfig::ServiceBookConfig()
62 ServiceBookConfig::~ServiceBookConfig()
66 const unsigned char* ServiceBookConfig::ParseField(const unsigned char *begin
,
67 const unsigned char *end
,
78 const PackedField_02
*field
= (const PackedField_02
*) begin
;
82 begin
+= PACKED_FIELD_02_HEADER_SIZE
+ size
;
88 const PackedField_10
*field
= (const PackedField_10
*) begin
;
92 begin
+= PACKED_FIELD_10_HEADER_SIZE
+ size
;
97 eout("------> Unknown packed field format: 0x" << std::hex
<<
98 (unsigned int) Format
);
99 throw BadPackedFormat(Format
);
105 if( begin
> end
) // if begin==end, we are ok
108 if( !size
) // if field has no size, something's up
111 // cycle through the type table
112 for( FieldLink
<ServiceBookConfig
> *b
= ServiceBookConfigFieldLinks
;
113 b
->type
!= SBFCC_END
;
116 if( b
->type
== type
) {
118 std::string
&s
= this->*(b
->strMember
);
119 s
= ParseFieldString(raw
, size
-1);
120 return begin
; // done!
126 // handle special cases
132 // if still not handled, add to the Unknowns list
135 uf
.data
.assign((const char*)raw
, size
);
136 Unknowns
.push_back(uf
);
138 // return new pointer for next field
142 void ServiceBookConfig::ParseHeader(const Data
&data
, size_t &offset
)
144 MAKE_RECORD(const Barry::Protocol::ServiceBookConfigField
, sbc
, data
, offset
);
145 offset
+= SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
;
146 if( data
.GetSize() >= offset
) { // size check!
147 Format
= sbc
->format
;
151 void ServiceBookConfig::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
153 const unsigned char *finish
= ParseCommonFields(*this,
154 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
155 offset
+= finish
- (data
.GetData() + offset
);
158 void ServiceBookConfig::BuildHeader(Data
&data
, size_t &offset
) const
160 // make sure there is enough space
161 data
.GetBuffer(offset
+ SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
);
163 MAKE_RECORD(Barry::Protocol::ServiceBookConfigField
, sbc
, data
, offset
);
164 sbc
->format
= Format
;
166 offset
+= SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE
;
172 /// Build fields part of record
174 void ServiceBookConfig::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
176 throw std::logic_error("ServiceBookConfig::Build not yet implemented");
179 void ServiceBookConfig::Clear()
185 void ServiceBookConfig::Dump(std::ostream
&os
) const
187 os
<< " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format
<< "\n";
189 // cycle through the type table
190 for( const FieldLink
<ServiceBookConfig
> *b
= ServiceBookConfigFieldLinks
;
191 b
->type
!= SBFCC_END
;
195 const std::string
&s
= this->*(b
->strMember
);
197 os
<< " " << b
->name
<< ": " << s
<< "\n";
199 else if( b
->timeMember
) {
200 time_t t
= this->*(b
->timeMember
);
202 os
<< " " << b
->name
<< ": " << ctime(&t
);
206 // print any unknowns
208 os
<< " ------------------- End of Config Field\n";
212 ///////////////////////////////////////////////////////////////////////////////
215 // service book field codes
216 #define SBFC_OLD_NAME 0x01
217 #define SBFC_HIDDEN_NAME 0x02
218 #define SBFC_NAME 0x03
219 #define SBFC_OLD_UNIQUE_ID 0x06
220 #define SBFC_UNIQUE_ID 0x07
221 #define SBFC_CONTENT_ID 0x08
222 #define SBFC_CONFIG 0x09
223 #define SBFC_OLD_DESC 0x32
224 #define SBFC_DESCRIPTION 0x0f
225 #define SBFC_DSID 0xa1
226 #define SBFC_BES_DOMAIN 0xa2
227 #define SBFC_USER_ID 0xa3
228 #define SBFC_END 0xffff
230 // private data class, containing internal structures
231 class ServiceBookData
234 FieldLink
<ServiceBook
> *m_typeSet
;
235 ServiceBookData(FieldLink
<ServiceBook
> *typeSet
) : m_typeSet(typeSet
) {}
238 // The Old/New tables contain the same fields, but use different
239 // type codes. Keeping them separate yet linked makes it possible
240 // to convert between old and new type codes, while hopefully keeping
242 static FieldLink
<ServiceBook
> ServiceBookOldFieldLinks
[] = {
243 { SBFC_OLD_NAME
, "Old Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
244 { SBFC_OLD_DESC
, "Old Desc", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
245 { SBFC_OLD_UNIQUE_ID
, "Old UniqueId", 0, 0, &ServiceBook::UniqueId
, 0, 0, 0, 0, false },
246 { SBFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
249 static FieldLink
<ServiceBook
> ServiceBookNewFieldLinks
[] = {
250 { SBFC_NAME
, "Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
251 { SBFC_DESCRIPTION
, "Description", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
252 { SBFC_UNIQUE_ID
, "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 // This table holds all
257 static FieldLink
<ServiceBook
> ServiceBookFieldLinks
[] = {
258 { SBFC_HIDDEN_NAME
, "Hidden Name",0, 0, &ServiceBook::HiddenName
, 0, 0, 0, 0, true },
259 { SBFC_DSID
, "DSID", 0, 0, &ServiceBook::DSID
, 0, 0, 0, 0, false },
260 { SBFC_CONTENT_ID
, "ContentId", 0, 0, &ServiceBook::ContentId
, 0, 0, 0, 0, false },
261 { SBFC_BES_DOMAIN
, "BES Domain", 0, 0, &ServiceBook::BesDomain
, 0, 0, 0, 0, false },
262 { SBFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
265 // Array of conflicting tables only
266 static FieldLink
<ServiceBook
> *ServiceBookLinkTable
[] = {
267 ServiceBookOldFieldLinks
,
268 ServiceBookNewFieldLinks
,
272 #define FIELDLINK_END 0xffff
274 template <class RecordT
>
275 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
276 const CommonField
*field
,
277 const IConverter
*ic
,
278 FieldLink
<RecordT
> *links
)
280 // cycle through the type table
281 for( FieldLink
<RecordT
> *b
= links
; b
->type
!= FIELDLINK_END
; b
++ ) {
282 if( b
->type
== field
->type
) {
284 std::string
&s
= rec
->*(b
->strMember
);
286 dout(RecordT::GetDBName() << ": field '" << b
->name
<< "' already has data (" << s
<< "). Overwriting.");
288 s
= ParseFieldString(field
);
289 if( b
->iconvNeeded
&& ic
)
293 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
294 time_t &t
= rec
->*(b
->timeMember
);
295 t
= min2time(field
->u
.min1900
);
303 template <class RecordT
>
304 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
305 const CommonField
*field
,
306 const IConverter
*ic
,
307 FieldLink
<RecordT
> **b
)
310 FieldLink
<RecordT
> *link
=
311 ParseFieldByTable
<RecordT
>(rec
, field
, ic
, *b
);
318 ServiceBook::ServiceBook()
319 : m_data( new ServiceBookData(ServiceBookOldFieldLinks
) )
325 ServiceBook::~ServiceBook()
329 const unsigned char* ServiceBook::ParseField(const unsigned char *begin
,
330 const unsigned char *end
,
331 const IConverter
*ic
)
333 const CommonField
*field
= (const CommonField
*) begin
;
335 // advance and check size
336 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
337 if( begin
> end
) // if begin==end, we are ok
340 if( !btohs(field
->size
) ) // if field has no size, something's up
343 // cycle through the type tables
344 FieldLink
<ServiceBook
> *typeSet
=
345 ParseFieldByTable(this, field
, ic
, ServiceBookLinkTable
);
347 if( m_data
->m_typeSet
&& m_data
->m_typeSet
!= typeSet
) {
348 dout("ServiceBook record has a mix of old and new field types.");
350 m_data
->m_typeSet
= typeSet
;
354 if( ParseFieldByTable(this, field
, ic
, ServiceBookFieldLinks
) )
355 return begin
; // done!
358 // handle special cases
359 switch( field
->type
)
363 Data
config((const void *)field
->u
.raw
, btohs(field
->size
));
365 Config
.ParseHeader(config
, offset
);
366 Config
.ParseFields(config
, offset
);
367 return begin
; // success
369 catch( BadPackedFormat
& ) {
370 // break here so unprocessed raw packet is still
376 // if still not handled, add to the Unknowns list
378 uf
.type
= field
->type
;
379 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
380 Unknowns
.push_back(uf
);
382 // return new pointer for next field
386 void ServiceBook::ParseHeader(const Data
&data
, size_t &offset
)
388 // no header in this record (?)
391 void ServiceBook::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
393 const unsigned char *finish
= ParseCommonFields(*this,
394 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
395 offset
+= finish
- (data
.GetData() + offset
);
398 void ServiceBook::BuildHeader(Data
&data
, size_t &offset
) const
400 // no header in this record (?)
406 /// Build fields part of record
408 void ServiceBook::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
410 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
413 void ServiceBook::Clear()
415 m_data
->m_typeSet
= ServiceBookOldFieldLinks
;
420 inline void FormatStr(std::ostream
&os
, const char *name
, const std::string
&str
)
423 os
<< " " << setw(20) << name
;
424 os
<< ": " << str
<< "\n";
428 void ServiceBook::Dump(std::ostream
&os
) const
430 ios::fmtflags oldflags
= os
.setf(ios::left
);
431 char fill
= os
.fill(' ');
433 os
<< "ServiceBook entry: 0x" << setbase(16) << RecordId
434 << " (" << (unsigned int)RecType
<< ")\n";
436 FormatStr(os
, "Name", Name
);
437 FormatStr(os
, "Hidden Name", HiddenName
);
438 FormatStr(os
, "Description", Description
);
439 FormatStr(os
, "DSID", DSID
);
440 FormatStr(os
, "Unique ID", UniqueId
);
441 FormatStr(os
, "Content ID", ContentId
);
442 FormatStr(os
, "(BES) Domain", BesDomain
);
446 // print any unknowns
449 // cleanup the stream
454 bool ServiceBook::operator<(const ServiceBook
&other
) const
456 int cmp
= BesDomain
.compare(other
.BesDomain
);
458 cmp
= DSID
.compare(other
.DSID
);
460 cmp
= Name
.compare(other
.Name
);
462 cmp
= UniqueId
.compare(other
.UniqueId
);