-
Notifications
You must be signed in to change notification settings - Fork 37
/
sony.py
344 lines (309 loc) · 10.9 KB
/
sony.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
'''This module extends PTPDevice for Sony devices.
Use it in a master module that determines the vendor and automatically uses its
extension. This is why inheritance is not explicit.
'''
from contextlib import contextmanager
from construct import Container, Struct, Range, Computed, Enum, Array, PrefixedArray, Pass, ExprAdapter
from ..ptp import PTPError
import logging
logger = logging.getLogger(__name__)
__all__ = ('Sony',)
class SonyError(PTPError):
pass
class Sony(object):
'''This class implements Sony's PTP operations.'''
def __init__(self, *args, **kwargs):
logger.debug('Init Sony')
super(Sony, self).__init__(*args, **kwargs)
# TODO: expose the choice to disable automatic Sony extension
self.__raw = False
@contextmanager
def session(self):
'''
Manage Sony session with context manager.
'''
# When raw device, do not perform
if self.__raw:
with super(Sony, self).session():
yield
return
with super(Sony, self).session():
logger.debug('Authentication')
r = []
r.append(self.sdio_connect(1))
r.append(self.sdio_connect(2))
r.append(self.sdio_get_ext_device_info())
r.append(self.sdio_connect(3))
if not all(map(lambda r: r.ResponseCode == 'OK', r)):
raise SonyError('Could not authenticate')
else:
logger.debug('Authentication done')
yield
def _shutdown(self):
logger.debug('Shutdown Sony')
super(Sony, self)._shutdown()
def _PropertyCode(self, **product_properties):
return super(Sony, self)._PropertyCode(
DPCCompensation=0xD200,
DRangeOptimize=0xD201,
SonyImageSize=0xD203,
ShutterSpeed=0xD20D,
ColorTemp=0xD20F,
CCFilter=0xD210,
AspectRatio=0xD211,
FocusFound=0xD213,
ObjectInMemory=0xD215,
ExposeIndex=0xD216,
SonyBatteryLevel=0xD218,
PictureEffect=0xD21B,
ABFilter=0xD21C,
ISO=0xD21E,
AutoFocus=0xD2C1,
Capture=0xD2C2,
Movie=0xD2C8,
StillImage=0xD2C7,
**product_properties
)
def _OperationCode(self, **product_operations):
return super(Sony, self)._OperationCode(
SDIOConnect=0x9201,
SDIOGetExtDeviceInfo=0x9202,
SonyGetDevicePropDesc=0x9203,
SonyGetDevicePropValue=0x9204,
SetControlDeviceA=0x9205,
GetControlDeviceDesc=0x9206,
SetControlDeviceB=0x9207,
GetAllDevicePropData=0x9209,
**product_operations
)
def _ObjectFormatCode(self, **product_object_formats):
return super(Sony, self)._ObjectFormatCode(
RAW=0xb101,
**product_object_formats
)
def _ResponseCode(self, **product_responses):
return super(Sony, self)._ResponseCode(
Sony1=0xa101,
**product_responses
)
def _EventCode(self, **product_events):
return super(Sony, self)._EventCode(
SonyObjectAdded=0xc201,
SonyObjectRemoved=0xc202,
SonyPropertyChanged=0xc203,
**product_events
)
def _FilesystemType(self, **product_filesystem_types):
return super(Sony, self)._FilesystemType(
**product_filesystem_types
)
def _SonyDeviceInfo(self):
return Range(0, 2, self._PTPArray(self._PropertyCode))
def _Visibility(self):
return Enum(
self._UInt8,
Disabled=0x00,
Enabled=0x01,
DisplayOnly=0x02,
)
def _SonyPropDesc(self):
return Struct(
'PropertyCode' / self._PropertyCode,
'DataTypeCode' / self._DataTypeCode,
'SonyGetSet' / self._UInt8,
'GetSet' / Computed(
lambda x: 'GetSet' if x.SonyGetSet & 0x01 else 'Get'
),
'Visibility' / self._Visibility,
'FactoryDefaultValue' / self._DataType,
'CurrentValue' / self._DataType,
'FormFlag' / self._FormFlag,
'Form' / self._Form(self._DataType)
)
def _SonyAllPropDesc(self):
return PrefixedArray(self._UInt64, self._SonyPropDesc)
def _ExposureProgramMode(self):
return Enum(
self._UInt16,
default=Pass,
IntelligentAuto=0x8000,
SuperiorAuto=0x8001,
P=0x2,
A=0x3,
S=0x4,
M=0x1,
MovieP=0x8050,
MovieA=0x8051,
MovieS=0x8052,
MovieM=0x8053,
# Mode=0x8054, # TODO: ??
Panoramic=0x8041,
Portrait=0x7,
SportsAction=0x8011,
Macro=0x8015,
Landscape=0x8014,
Sunset=0x8012,
NightScene=0x8013,
HandheldTwilight=0x8016,
NightPortrait=0x8017,
AntiMotionBlur=0x8018,
)
def _AutoFocus(self):
return Enum(
self._UInt16,
default=Pass,
)
def _PictureEffect(self):
return Enum(
self._UInt16,
default=Pass,
Off=0x8000,
ToyCameraNormal=0x8001,
ToyCameraCool=0x8002,
ToyCameraWarm=0x8003,
ToyCameraGreen=0x8004,
ToyCameraMagenta=0x8005,
Pop=0x8010,
PosterizationBW=0x8020,
PosterizationColor=0x8021,
Retro=0x8030,
SoftHighKey=0x8030,
PartialColorRed=0x8050,
PartialColorGreen=0x8051,
PartialColorBlue=0x8052,
PartialColorYellow=0x8053,
HighContrastMono=0x8060,
SoftFocusLow=0x8070,
SoftFocusMid=0x8071,
SoftFocusHigh=0x8072,
HDRPaintingLow=0x8080,
HDRPaintingMid=0x8081,
HDRPaintingHigh=0x8082,
RichToneMono=0x8090,
MiniatureAuto=0x80a0,
MiniatureTop=0x80a1,
MiniatureMiddleHorizontal=0x80a2,
MiniatureBottom=0x80a3,
MiniatureRight=0x80a4,
MiniatureMiddleVertical=0x80a5,
MiniatureLeft=0x80a6,
Watercolor=0x80b0,
IllustrationLow=0x80c0,
IllustrationMid=0x80c1,
IllustrationHigh=0x80c2,
)
def _StillCaptureMode(self):
'''DriveMode in Sony terminology'''
return Enum(
self._UInt16,
default=Pass,
Single=0x0001,
SelfTimer10s=0x8004,
SelfTimer2s=0x8005,
SelfTimer10sContinuous3Images=0x8008,
SelfTimer10sContinuous5Images=0x8009,
Continuous=0x8013,
ContinuousSpeedPriority=0x8014,
WhiteBalanceBracketLow=0x8018,
WhiteBalanceBracketHigh=0x8028,
DRangeOptimizerBracketLow=0x8019,
DRangoOptimizerBracketHigh=0x8029,
ContinuousBracket1_0EV3Image=0x8311,
ContinuousBracket2_0EV3Image=0x8321,
ContinuousBracket3_0EV3Image=0x8331,
ContinuousBracket0_3EV3Image=0x8337,
ContinuousBracket0_5EV3Image=0x8357,
ContinuousBracket0_7EV3Image=0x8377,
ContinuousBracket0_3EV5Image=0x8537,
ContinuousBracket0_5EV5Image=0x8557,
ContinuousBracket0_7EV5Image=0x8577,
SingleBracket1_0EV3Image=0x8310,
SingleBracket2_0EV3Image=0x8320,
SingleBracket3_0EV3Image=0x8330,
SingleBracket0_3EV3Image=0x8336,
SingleBracket0_5EV3Image=0x8356,
SingleBracket0_7EV3Image=0x8376,
SingleBracket0_3EV5Image=0x8536,
SingleBracket0_5EV5Image=0x8556,
SingleBracket0_7EV5Image=0x8576,
)
def _ExposureBiasCompensation(self):
return ExprAdapter(
self._UInt16,
encode=lambda x: x*1000,
decode=lambda x: x/1000.,
)
def _set_endian(self, endian):
logger.debug('Set Sony endianness')
super(Sony, self)._set_endian(endian)
self._ExposureProgramMode = self._ExposureProgramMode()
self._AutoFocus = self._AutoFocus()
self._PictureEffect = self._PictureEffect()
self._StillCaptureMode = self._StillCaptureMode()
self._Visibility = self._Visibility()
self._SonyPropDesc = self._SonyPropDesc()
self._SonyDeviceInfo = self._SonyDeviceInfo()
self._SonyAllPropDesc = self._SonyAllPropDesc()
def event(self, wait=False):
'''Check Sony or PTP events
If `wait` this function is blocking. Otherwise it may return None.
'''
evt = super(Sony, self).event(wait=wait)
return evt
def sdio_connect(self, step, key1=0, key2=0):
'''Authentication handshake'''
ptp = Container(
OperationCode='SDIOConnect',
SessionID=self._session,
TransactionID=self._transaction,
Parameter=[step, key1, key2]
)
return self.recv(ptp)
def sdio_get_ext_device_info(self, version=0xc8):
'''Sony DeviceInfo'''
ptp = Container(
OperationCode='SDIOGetExtDeviceInfo',
SessionID=self._session,
TransactionID=self._transaction,
Parameter=[version]
)
return self.recv(ptp)
def get_all_device_prop_data(self):
ptp = Container(
OperationCode='GetAllDevicePropData',
SessionID=self._session,
TransactionID=self._transaction,
Parameter=[]
)
response = self.recv(ptp)
return self._parse_if_data(response, self._SonyAllPropDesc)
def set_control_device_A(self, device_property, value_payload):
code = self._code(device_property, self._PropertyCode)
ptp = Container(
OperationCode='SetControlDeviceA',
SessionID=self._session,
TransactionID=self._transaction,
Parameter=[code]
)
response = self.send(ptp, value_payload)
return response
def set_control_device_B(self, device_property, value_payload):
code = self._code(device_property, self._PropertyCode)
ptp = Container(
OperationCode='SetControlDeviceB',
SessionID=self._session,
TransactionID=self._transaction,
Parameter=[code]
)
response = self.send(ptp, value_payload)
return response
def get_control_device_desc(self, device_property):
code = self._code(device_property, self._PropertyCode)
ptp = Container(
OperationCode='GetControlDeviceDesc',
SessionID=self._session,
TransactionID=self._transaction,
Parameter=[code]
)
response = self.recv(ptp)
return response