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()
184 void ServiceBookConfig::Dump(std::ostream
&os
) const
186 os
<< " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format
<< "\n";
188 // cycle through the type table
189 for( const FieldLink
<ServiceBookConfig
> *b
= ServiceBookConfigFieldLinks
;
190 b
->type
!= SBFCC_END
;
194 const std::string
&s
= this->*(b
->strMember
);
196 os
<< " " << b
->name
<< ": " << s
<< "\n";
198 else if( b
->timeMember
) {
199 time_t t
= this->*(b
->timeMember
);
201 os
<< " " << b
->name
<< ": " << ctime(&t
);
205 // print any unknowns
207 os
<< " ------------------- End of Config Field\n";
211 ///////////////////////////////////////////////////////////////////////////////
214 // service book field codes
215 #define SBFC_OLD_NAME 0x01
216 #define SBFC_HIDDEN_NAME 0x02
217 #define SBFC_NAME 0x03
218 #define SBFC_OLD_UNIQUE_ID 0x06
219 #define SBFC_UNIQUE_ID 0x07
220 #define SBFC_CONTENT_ID 0x08
221 #define SBFC_CONFIG 0x09
222 #define SBFC_OLD_DESC 0x32
223 #define SBFC_DESCRIPTION 0x0f
224 #define SBFC_DSID 0xa1
225 #define SBFC_BES_DOMAIN 0xa2
226 #define SBFC_USER_ID 0xa3
227 #define SBFC_END 0xffff
229 // private data class, containing internal structures
230 class ServiceBookData
233 FieldLink
<ServiceBook
> *m_typeSet
;
234 ServiceBookData(FieldLink
<ServiceBook
> *typeSet
) : m_typeSet(typeSet
) {}
237 // The Old/New tables contain the same fields, but use different
238 // type codes. Keeping them separate yet linked makes it possible
239 // to convert between old and new type codes, while hopefully keeping
241 static FieldLink
<ServiceBook
> ServiceBookOldFieldLinks
[] = {
242 { SBFC_OLD_NAME
, "Old Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
243 { SBFC_OLD_DESC
, "Old Desc", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
244 { SBFC_OLD_UNIQUE_ID
, "Old UniqueId", 0, 0, &ServiceBook::UniqueId
, 0, 0, 0, 0, false },
245 { SBFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
248 static FieldLink
<ServiceBook
> ServiceBookNewFieldLinks
[] = {
249 { SBFC_NAME
, "Name", 0, 0, &ServiceBook::Name
, 0, 0, 0, 0, true },
250 { SBFC_DESCRIPTION
, "Description", 0, 0, &ServiceBook::Description
, 0, 0, 0, 0, true },
251 { SBFC_UNIQUE_ID
, "UniqueId", 0, 0, &ServiceBook::UniqueId
, 0, 0, 0, 0, false },
252 { SBFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
255 // This table holds all
256 static FieldLink
<ServiceBook
> ServiceBookFieldLinks
[] = {
257 { SBFC_HIDDEN_NAME
, "Hidden Name",0, 0, &ServiceBook::HiddenName
, 0, 0, 0, 0, true },
258 { SBFC_DSID
, "DSID", 0, 0, &ServiceBook::DSID
, 0, 0, 0, 0, false },
259 { SBFC_CONTENT_ID
, "ContentId", 0, 0, &ServiceBook::ContentId
, 0, 0, 0, 0, false },
260 { SBFC_BES_DOMAIN
, "BES Domain", 0, 0, &ServiceBook::BesDomain
, 0, 0, 0, 0, false },
261 { SBFC_END
, "End of List",0, 0, 0, 0, 0, 0, 0, false }
264 // Array of conflicting tables only
265 static FieldLink
<ServiceBook
> *ServiceBookLinkTable
[] = {
266 ServiceBookOldFieldLinks
,
267 ServiceBookNewFieldLinks
,
271 #define FIELDLINK_END 0xffff
273 template <class RecordT
>
274 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
275 const CommonField
*field
,
276 const IConverter
*ic
,
277 FieldLink
<RecordT
> *links
)
279 // cycle through the type table
280 for( FieldLink
<RecordT
> *b
= links
; b
->type
!= FIELDLINK_END
; b
++ ) {
281 if( b
->type
== field
->type
) {
283 std::string
&s
= rec
->*(b
->strMember
);
285 dout(RecordT::GetDBName() << ": field '" << b
->name
<< "' already has data (" << s
<< "). Overwriting.");
287 s
= ParseFieldString(field
);
288 if( b
->iconvNeeded
&& ic
)
292 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
293 time_t &t
= rec
->*(b
->timeMember
);
294 t
= min2time(field
->u
.min1900
);
302 template <class RecordT
>
303 FieldLink
<RecordT
>* ParseFieldByTable(RecordT
*rec
,
304 const CommonField
*field
,
305 const IConverter
*ic
,
306 FieldLink
<RecordT
> **b
)
309 FieldLink
<RecordT
> *link
=
310 ParseFieldByTable
<RecordT
>(rec
, field
, ic
, *b
);
317 ServiceBook::ServiceBook()
318 : m_data( new ServiceBookData(ServiceBookOldFieldLinks
) )
324 ServiceBook::~ServiceBook()
328 const unsigned char* ServiceBook::ParseField(const unsigned char *begin
,
329 const unsigned char *end
,
330 const IConverter
*ic
)
332 const CommonField
*field
= (const CommonField
*) begin
;
334 // advance and check size
335 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
336 if( begin
> end
) // if begin==end, we are ok
339 if( !btohs(field
->size
) ) // if field has no size, something's up
342 // cycle through the type tables
343 FieldLink
<ServiceBook
> *typeSet
=
344 ParseFieldByTable(this, field
, ic
, ServiceBookLinkTable
);
346 if( m_data
->m_typeSet
&& m_data
->m_typeSet
!= typeSet
) {
347 dout("ServiceBook record has a mix of old and new field types.");
349 m_data
->m_typeSet
= typeSet
;
353 if( ParseFieldByTable(this, field
, ic
, ServiceBookFieldLinks
) )
354 return begin
; // done!
357 // handle special cases
358 switch( field
->type
)
362 Data
config((const void *)field
->u
.raw
, btohs(field
->size
));
364 Config
.ParseHeader(config
, offset
);
365 Config
.ParseFields(config
, offset
);
366 return begin
; // success
368 catch( BadPackedFormat
& ) {
369 // break here so unprocessed raw packet is still
375 // if still not handled, add to the Unknowns list
377 uf
.type
= field
->type
;
378 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
379 Unknowns
.push_back(uf
);
381 // return new pointer for next field
385 void ServiceBook::ParseHeader(const Data
&data
, size_t &offset
)
387 // no header in this record (?)
390 void ServiceBook::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
392 const unsigned char *finish
= ParseCommonFields(*this,
393 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
394 offset
+= finish
- (data
.GetData() + offset
);
397 void ServiceBook::BuildHeader(Data
&data
, size_t &offset
) const
399 // no header in this record (?)
405 /// Build fields part of record
407 void ServiceBook::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
409 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
412 void ServiceBook::Clear()
414 m_data
->m_typeSet
= ServiceBookOldFieldLinks
;
419 inline void FormatStr(std::ostream
&os
, const char *name
, const std::string
&str
)
422 os
<< " " << setw(20) << name
;
423 os
<< ": " << str
<< "\n";
427 void ServiceBook::Dump(std::ostream
&os
) const
429 ios::fmtflags oldflags
= os
.setf(ios::left
);
430 char fill
= os
.fill(' ');
432 os
<< "ServiceBook entry: 0x" << setbase(16) << RecordId
433 << " (" << (unsigned int)RecType
<< ")\n";
435 FormatStr(os
, "Name", Name
);
436 FormatStr(os
, "Hidden Name", HiddenName
);
437 FormatStr(os
, "Description", Description
);
438 FormatStr(os
, "DSID", DSID
);
439 FormatStr(os
, "Unique ID", UniqueId
);
440 FormatStr(os
, "Content ID", ContentId
);
441 FormatStr(os
, "(BES) Domain", BesDomain
);
445 // print any unknowns
448 // cleanup the stream
453 bool ServiceBook::operator<(const ServiceBook
&other
) const
455 int cmp
= BesDomain
.compare(other
.BesDomain
);
457 cmp
= DSID
.compare(other
.DSID
);
459 cmp
= Name
.compare(other
.Name
);
461 cmp
= UniqueId
.compare(other
.UniqueId
);