-
Notifications
You must be signed in to change notification settings - Fork 2
/
bipheaders.py
176 lines (118 loc) · 4.15 KB
/
bipheaders.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# Copyright (c) 2018 Kannan Subramani <[email protected]>
# SPDX-License-Identifier: GPL-3.0
# -*- coding: utf-8 -*-.
"""Implementation of BIP related headers"""
from PyOBEX.headers import *
# IMAGEPULL_UUID = "8EE9B3D0-4608-11D5-841A-0002A5325B4E"
COVERART_UUID = "7163DD54-4A7E-11E2-B47C-0050C2490048"
#########################
# #
# User Defined Headers #
# #
#########################
# code | length | data
# 30 | 00 13 | 00310030003000300030003000340000
class Img_Handle(UnicodeHeader):
code = 0x30
class Img_Descriptor(DataHeader):
code = 0x71
header_dict.update({
0x71: Img_Descriptor,
0x30: Img_Handle
})
#############################################
# #
# Application Parameters Header Properties #
# #
#############################################
class AppParamProperty(object):
def __init__(self, data, encoded=False):
self.data = data if encoded else self.encode(data)
def encode(self, data):
return struct.pack(">BB", self.tagid, struct.calcsize(self.fmt)) + data
def decode(self):
return struct.unpack(">BB", self.data[:2]), self.data[2:]
class OneByteProperty(AppParamProperty):
fmt = ">B"
def encode(self, data):
return super(OneByteProperty, self).encode(struct.pack(self.fmt, data))
def decode(self):
headers, data = super(OneByteProperty, self).decode()
return struct.unpack(self.fmt, data)[0]
class TwoByteProperty(AppParamProperty):
fmt = ">H"
def encode(self, data):
return super(TwoByteProperty, self).encode(struct.pack(self.fmt, data))
def decode(self):
headers, data = super(TwoByteProperty, self).decode()
return struct.unpack(self.fmt, data)[0]
class FourByteProperty(AppParamProperty):
fmt = ">I"
def encode(self, data):
return super(FourByteProperty, self).encode(struct.pack(self.fmt, data))
def decode(self):
headers, data = super(FourByteProperty, self).decode()
return struct.unpack(self.fmt, data)[0]
class SixteenByteProperty(AppParamProperty):
fmt = ">16B"
def encode(self, data):
return super(SixteenByteProperty, self).encode(struct.pack(self.fmt, data))
def decode(self):
headers, data = super(SixteenByteProperty, self).decode()
return struct.unpack(self.fmt, data)[0]
class NbReturnedHandles(TwoByteProperty):
tagid = 0x01
class ListStartOffset(TwoByteProperty):
tagid = 0x02
class LatestCapturedImages(OneByteProperty):
tagid = 0x03
class PartialFileLength(FourByteProperty):
tagid = 0x04
class PartialFileStartOffset(FourByteProperty):
tagid = 0x05
class TotalFileSize(FourByteProperty):
tagid = 0x06
class EndFlag(OneByteProperty):
tagid = 0x07
class RemoteDisplay(OneByteProperty):
tagid = 0x08
class ServiceID(SixteenByteProperty):
tagid = 0x09
class StoreFlag(OneByteProperty):
tagid = 0x0A
app_parameters_dict = {
0x01: NbReturnedHandles,
0x02: ListStartOffset,
0x03: LatestCapturedImages,
0x04: PartialFileLength,
0x05: PartialFileStartOffset,
0x06: TotalFileSize,
0x07: EndFlag,
0x08: RemoteDisplay,
0x09: ServiceID,
0x0A: StoreFlag
}
# Sample App Parameters data
# code | length | data
# 4c | 00 0e | 0202000101020002030101
def extended_decode(self):
"""Decodes the App_Parameters header data into AppParamProperties dict"""
# size of tagid = 1 byte
# size of length = 1 byte
data = self.data
res_dict = {}
while data:
tagid = ord(data[0])
length = ord(data[1])
app_param_class = app_parameters_dict[tagid]
res_dict[app_param_class.__name__] = app_param_class(data[:length + 2], encoded=True)
data = data[length + 2:]
return res_dict
def extended_encode(self, data_dict):
"""Encodes the AppParamProperties dict + super().encode"""
data = ""
for item in data_dict.values():
data += item.data
return struct.pack(">BH", self.code, len(data) + 3) + data
App_Parameters.decode = extended_decode
App_Parameters.encode = extended_encode