-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPTPDevicePropDesc.py
167 lines (144 loc) · 6.88 KB
/
PTPDevicePropDesc.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
# /* -LICENSE-START-
# ** Copyright (c) 2020 Blackmagic Design
# **
# ** Permission is hereby granted, free of charge, to any person or organization
# ** obtaining a copy of the software and accompanying documentation covered by
# ** this license (the "Software") to use, reproduce, display, distribute,
# ** execute, and transmit the Software, and to prepare derivative works of the
# ** Software, and to permit third-parties to whom the Software is furnished to
# ** do so, all subject to the following:
# **
# ** The copyright notices in the Software and this entire statement, including
# ** the above license grant, this restriction and the following disclaimer,
# ** must be included in all copies of the Software, in whole or in part, and
# ** all derivative works of the Software, unless such copies or derivative
# ** works are solely in the form of machine-executable object code generated by
# ** a source language processor.
# **
# ** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# ** IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# ** FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
# ** SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
# ** FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
# ** ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
# ** DEALINGS IN THE SOFTWARE.
# ** -LICENSE-END-
# */
import fixedint
from LogFunctions import *
from MemoryLayout import *
from PTPTypes import *
from PTPDeserialiser import *
class RangedDevicePropValues:
def __init__(self, minimum, maximum, step):
self.m_minimum = minimum
self.m_maximum = maximum
self.m_step = step
class PTPDevicePropDesc:
def __init__(self):
self.m_devicePropCode = DevicePropCode.Undefined
self.m_dataTypeCode = DataTypeCode.Undefined
self.m_permission = DevicePropPermissions.ReadOnly
self.m_defaultValue = bytearray(0)
self.m_currentValue = bytearray(0)
self.m_propertyForm = DevicePropForm.NONE
self.m_minimumValue = bytearray(0)
self.m_maximumValue = bytearray(0)
self.m_stepValue = bytearray(0)
self.m_enums = [bytearray(0)]
def GetMemorySize(self, dataTypeCode):
match dataTypeCode:
case DataTypeCode.Int8: return MemoryLayout_Int8__size
case DataTypeCode.UInt8: return MemoryLayout_UInt8__size
case DataTypeCode.Int16: return MemoryLayout_Int16__size
case DataTypeCode.UInt16: return MemoryLayout_UInt16__size
case DataTypeCode.Int32: return MemoryLayout_Int32__size
case DataTypeCode.UInt32: return MemoryLayout_UInt32__size
case DataTypeCode.Int64: return MemoryLayout_Int64__size
case DataTypeCode.UInt64: return MemoryLayout_UInt64__size
case _: return 0
def GetMemoryObject(self, dataTypeCode, data):
if dataTypeCode != DataTypeCode.String:
if len(data) > self.GetMemorySize(dataTypeCode):
return None
signed = False
match dataTypeCode:
case DataTypeCode.Int8: size, signed = 1, True
case DataTypeCode.UInt8: size, signed = 1, False
case DataTypeCode.Int16: size, signed = 2, True
case DataTypeCode.UInt16: size, signed = 2, False
case DataTypeCode.Int32: size, signed = 4, True
case DataTypeCode.UInt32: size, signed = 4, False
case DataTypeCode.Int64: size, signed = 8, True
case DataTypeCode.UInt64: size, signed = 8, False
case DataTypeCode.String: return bytes(data).decode(encoding="ascii", errors="replace")
case _: return None
return int.from_bytes(data[0:size], byteorder='little', signed=signed)
def deserializeFromPayload(self, data):
deserialiser = PTPDeserialiser(data)
try:
self.m_devicePropCode = deserialiser.deserialise(self.m_devicePropCode)
self.m_dataTypeCode = deserialiser.deserialise(self.m_dataTypeCode)
self.m_permission = deserialiser.deserialise(self.m_permission)
elemSize = self.GetMemorySize(self.m_dataTypeCode)
if (self.m_dataTypeCode == DataTypeCode.String):
# Special case
defaultValue = deserialiser.deserialiseString()
currentValue = deserialiser.deserialiseString()
self.m_defaultValue = bytes(defaultValue).decode(encoding="ascii", errors="replace")
self.m_currentValue = bytes(currentValue).decode(encoding="ascii", errors="replace")
else:
self.m_defaultValue = deserialiser.deserialiseBySizeKeepByteArray(elemSize)
self.m_currentValue = deserialiser.deserialiseBySizeKeepByteArray(elemSize)
self.m_propertyForm = deserialiser.deserialise(self.m_propertyForm)
if (self.m_propertyForm == DevicePropForm.Range):
self.m_minimumValue = deserialiser.deserialiseBySizeKeepByteArray(elemSize)
self.m_maximumValue = deserialiser.deserialiseBySizeKeepByteArray(elemSize)
self.m_stepValue = deserialiser.deserialiseBySizeKeepByteArray(elemSize)
elif (self.m_propertyForm == DevicePropForm.Enum):
if (self.m_dataTypeCode == DataTypeCode.String):
stringEnums = deserialiser.deserialiseDataArray()
for item in stringEnums:
self.m_enums.append(bytes(item).decode(encoding="ascii", errors="replace"))
else:
self.m_enums = deserialiser.deserialiseDataArrayBySize(elemSize)
if len(self.m_enums) > 0:
self.m_minimumValue = self.m_enums[0]
self.m_maximumValue = self.m_enums[-1]
return True
except:
# Failed to deserialise PTP device info
return False
def getDefaultValue(self):
return self.GetMemoryObject(self.m_dataTypeCode, self.m_defaultValue)
def getCurrentValue(self):
return self.GetMemoryObject(self.m_dataTypeCode, self.m_currentValue)
def getMinimumValue(self):
return self.GetMemoryObject(self.m_dataTypeCode, self.m_minimumValue)
def getMaximumValue(self):
return self.GetMemoryObject(self.m_dataTypeCode, self.m_maximumValue)
def getStepValue(self):
return self.GetMemoryObject(self.m_dataTypeCode, self.m_stepValue)
def getRangeValues(self):
ret = (self.getMinimumValue(), self.getMaximumValue(), self.getStepValue())
return ret
def getEnumValues(self):
ret = []
for data in self.m_enums:
ret.append(self.GetMemoryObject(self.m_dataTypeCode, data))
return ret
def printDebug(self):
Logger.LogWithInfo("DevicePropDesc:")
Logger.LogWithInfo("m_devicePropCode: {}".format(self.m_devicePropCode))
Logger.LogWithInfo("m_permission: {}".format(self.m_permission))
Logger.LogWithInfo("m_defaultValue: {}".format(self.getDefaultValue()))
Logger.LogWithInfo("m_currentValue: {}".format(self.getCurrentValue()))
Logger.LogWithInfo("m_propertyForm: {}".format(self.m_propertyForm))
if (self.m_propertyForm == DevicePropForm.Range):
Logger.LogWithInfo("Range:")
Logger.LogWithInfo("\t Minimum: {}".format(self.getMinimumValue()))
Logger.LogWithInfo("\t Maximum: {}".format(self.getMaximumValue()))
Logger.LogWithInfo("\t Step: {}".format(self.getStepValue()))
elif (self.m_propertyForm == DevicePropForm.Enum):
Logger.LogWithInfo("Enums:")
Logger.LogWithInfo(self.getEnumValues())