3 /// Record parsing class for the phone call logs database.
7 Copyright (C) 2008-2009, Nicolas VIVIEN
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_calllog.h"
24 #include "record-internal.h"
25 #include "protostructs.h"
31 #include "ios_state.h"
34 using namespace Barry::Protocol
;
38 #define MILLISECONDS_IN_A_SECOND 1000
40 time_t CallLog::GetTime() const
42 return (time_t)(Timestamp
/ MILLISECONDS_IN_A_SECOND
);
45 CallLog::DirectionFlagType
CallLog::DirectionProto2Rec(uint8_t s
)
47 return (DirectionFlagType
)s
;
50 uint8_t CallLog::DirectionRec2Proto(DirectionFlagType s
)
55 CallLog::PhoneTypeFlagType
CallLog::PhoneTypeProto2Rec(uint8_t s
)
57 return (PhoneTypeFlagType
)s
;
60 uint8_t CallLog::PhoneTypeRec2Proto(PhoneTypeFlagType s
)
66 ///////////////////////////////////////////////////////////////////////////////
69 // CallLog Field Codes
70 #define CLLFC_CALLLOG_TYPE 0x01
71 #define CLLFC_DIRECTION 0x02
72 #define CLLFC_DURATION 0x03
73 #define CLLFC_TIMESTAMP 0x04
74 #define CLLFC_STATUS 0x06
75 #define CLLFC_UNIQUEID 0x07
76 #define CLLFC_PHONE_TYPE 0x0b
77 #define CLLFC_PHONE_NUMBER 0x0c
78 #define CLLFC_PHONE_INFO 0x0d
79 #define CLLFC_CONTACT_NAME 0x1f
80 #define CLLFC_END 0xffff
82 static FieldLink
<CallLog
> CallLogFieldLinks
[] = {
83 { CLLFC_PHONE_NUMBER
, "Phone number", 0, 0, &CallLog::PhoneNumber
, 0, 0, 0, 0, true },
84 { CLLFC_CONTACT_NAME
, "Contact name", 0, 0, &CallLog::ContactName
, 0, 0, 0, 0, true },
85 { CLLFC_END
, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
97 const unsigned char* CallLog::ParseField(const unsigned char *begin
,
98 const unsigned char *end
,
101 const CommonField
*field
= (const CommonField
*) begin
;
103 // advance and check size
104 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
105 if( begin
> end
) // if begin==end, we are ok
108 if( !btohs(field
->size
) ) // if field has no size, something's up
111 if( field
->type
== CLLFC_CALLLOG_TYPE
) {
112 if( field
->u
.raw
[0] != 'p' ) {
113 throw Error( "CallLog::ParseField: CallLogType is not 'p'" );
118 // this is always the same as the RecordID from the lower level
119 // protocol, so we throw this away for now
120 if( field
->type
== CLLFC_UNIQUEID
)
123 // cycle through the type table
124 for( FieldLink
<CallLog
> *b
= CallLogFieldLinks
;
125 b
->type
!= CLLFC_END
;
128 if( b
->type
== field
->type
) {
130 std::string
&s
= this->*(b
->strMember
);
131 s
= ParseFieldString(field
);
132 if( b
->iconvNeeded
&& ic
)
134 return begin
; // done!
136 else if( b
->timeMember
&& btohs(field
->size
) == 4 ) {
137 time_t &t
= this->*(b
->timeMember
);
138 t
= min2time(field
->u
.min1900
);
144 // handle special cases
145 switch( field
->type
)
148 // single byte... size check above checks for non-zero already
149 switch (field
->u
.raw
[0]) {
151 StatusFlag
= Barry::CallLog::OK
;
154 StatusFlag
= Barry::CallLog::Busy
;
157 StatusFlag
= Barry::CallLog::NetError
;
160 StatusFlag
= Barry::CallLog::Unknown
;
164 case CLLFC_DIRECTION
:
165 if( field
->u
.raw
[0] > CLL_DIRECTION_RANGE_HIGH
) {
166 throw Error( "CallLog::ParseField: direction field out of bounds" );
169 DirectionFlag
= DirectionProto2Rec(field
->u
.raw
[0]);
173 case CLLFC_PHONE_TYPE
:
174 if( field
->u
.raw
[0] > CLL_PHONETYPE_RANGE_HIGH
) {
175 PhoneTypeFlag
= Barry::CallLog::TypeUnknown
;
178 PhoneTypeFlag
= PhoneTypeProto2Rec(field
->u
.raw
[0]);
182 case CLLFC_PHONE_INFO
:
183 switch (field
->u
.raw
[0]) {
185 PhoneInfoFlag
= Barry::CallLog::InfoKnown
;
188 PhoneInfoFlag
= Barry::CallLog::InfoUnknown
;
191 PhoneInfoFlag
= Barry::CallLog::InfoPrivate
;
194 PhoneInfoFlag
= Barry::CallLog::InfoUndefined
;
199 if( btohs(field
->size
) >= sizeof(field
->u
.uint32
) ) {
200 Duration
= btohl(field
->u
.uint32
);
205 case CLLFC_TIMESTAMP
:
206 if( btohs(field
->size
) >= sizeof(field
->u
.timestamp
) ) {
207 Timestamp
= btohll(field
->u
.timestamp
);
213 // if still not handled, add to the Unknowns list
215 uf
.type
= field
->type
;
216 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
217 Unknowns
.push_back(uf
);
219 // return new pointer for next field
223 void CallLog::ParseHeader(const Data
&data
, size_t &offset
)
225 // no header in CallLog records
228 void CallLog::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
230 const unsigned char *finish
= ParseCommonFields(*this,
231 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
232 offset
+= finish
- (data
.GetData() + offset
);
235 void CallLog::BuildHeader(Data
&data
, size_t &offset
) const
237 // not yet implemented
240 void CallLog::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
242 // not yet implemented
245 void CallLog::Dump(std::ostream
&os
) const
247 ios_format_state
state(os
);
249 uint32_t timestamp
= Duration
;
250 int32_t days
, hours
, minutes
, secondes
;
252 static const char *DirectionName
[] = { "Received", "Sent", "Call Missing (Messagerie)", "Call Missing" };
253 static const char *StatusName
[] = { "OK", "Busy", "Error", "Not supported by Barry" };
254 static const char *PhoneInfoName
[] = { "Undefined", "Known phone number", "Unknown phone number", "Private phone number" };
255 static const char *PhoneTypeName
[] = { "Unknown", "Office", "Home", "Mobile", "Not supported by Barry" };
257 os
<< "CallLog entry: 0x" << setbase(16) << RecordId
258 << " (" << (unsigned int)RecType
<< ")\n";
260 time_t t
= GetTime();
261 os
<< " Timestamp: " << ctime(&t
);
262 os
<< " Direction: " << DirectionName
[DirectionFlag
] << "\n";
263 os
<< " Status: " << StatusName
[StatusFlag
] << "\n";
264 os
<< " Phone info: " << PhoneInfoName
[PhoneInfoFlag
] << "\n";
265 os
<< " Phone type: " << PhoneTypeName
[PhoneTypeFlag
] << "\n";
270 days
= (int) (timestamp
/ (60 * 60 * 24));
271 timestamp
= timestamp
- (days
* (60 * 60 * 24));
273 hours
= (int) (timestamp
/ (60 * 60));
274 timestamp
= timestamp
- (hours
* (60 * 60));
276 minutes
= (int) (timestamp
/ 60);
277 timestamp
= timestamp
- (minutes
* 60);
279 secondes
= timestamp
;
282 os
<< setbase(10) << days
<< " days ";
284 os
<< setbase(10) << days
<< " day ";
286 os
<< setfill ('0') << setw(2) << setbase(10) << hours
;
288 os
<< setfill ('0') << setw(2) << setbase(10) << minutes
;
290 os
<< setfill ('0') << setw(2) << setbase(10) << secondes
;
293 // cycle through the type table
294 for( const FieldLink
<CallLog
> *b
= CallLogFieldLinks
;
295 b
->type
!= CLLFC_END
;
299 const std::string
&s
= this->*(b
->strMember
);
301 os
<< " " << b
->name
<< ": " << s
<< "\n";
303 else if( b
->timeMember
) {
304 time_t t
= this->*(b
->timeMember
);
306 os
<< " " << b
->name
<< ": " << ctime(&t
);
308 os
<< " " << b
->name
<< ": unknown\n";
317 void CallLog::Clear()
319 RecType
= GetDefaultRecType();
328 DirectionFlag
= Barry::CallLog::Receiver
;
329 StatusFlag
= Barry::CallLog::Unknown
;
330 PhoneTypeFlag
= Barry::CallLog::TypeUnknown
;
331 PhoneInfoFlag
= Barry::CallLog::InfoUndefined
;
336 std::string
CallLog::GetDescription() const