Skip to content
Browse files

mtp: add SendData()/GetData() to generically encode/decode data.

  • Loading branch information...
1 parent f39111e commit 6588350ace2ffffefde544b0f6f0a3d822459da8 @hanwen committed Feb 17, 2013
Showing with 54 additions and 93 deletions.
  1. +3 −3 mtp/mtp.go
  2. +42 −90 mtp/ops.go
  3. +9 −0 mtp/print.go
View
6 mtp/mtp.go
@@ -283,8 +283,8 @@ func (d *Device) RunTransaction(req *Container, rep *Container,
_, ok2 := err.(SyncError)
_, ok1 := err.(usb.Error)
if ok1 || ok2 {
- d.Close()
log.Printf("fatal error %v; closing connection.", err)
+ d.Close()
}
}
return err
@@ -361,10 +361,10 @@ func (d *Device) runTransaction(req *Container, rep *Container,
err = d.decodeRep(h, rest, rep)
if d.MTPDebug {
- log.Printf("MTP response %s %v", RC_names[int(rep.Code)], rep.Param)
+ log.Printf("MTP response %s %v", getName(RC_names, int(rep.Code)), rep.Param)
}
if unexpectedData {
- return SyncError(fmt.Sprintf("unexpected data for code %s", RC_names[int(req.Code)]))
+ return SyncError(fmt.Sprintf("unexpected data for code %s", getName(RC_names, int(req.Code))))
}
if err != nil {
View
132 mtp/ops.go
@@ -52,119 +52,92 @@ func (d *Device) CloseSession() error {
return err
}
-func (d *Device) GetDeviceInfo(info *DeviceInfo) error {
- var req, rep Container
-
- req.Code = OC_GetDeviceInfo
+func (d *Device) GetData(req *Container, info interface{}) error {
var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
+ var rep Container
+ err := d.RunTransaction(req, &rep, &buf, nil, 0)
if err != nil {
return err
}
- return Decode(&buf, info)
+ err = Decode(&buf, info)
+ if d.MTPDebug && err == nil {
+ log.Printf("MTP decoded %#v", info)
+ }
+ return err
+}
+
+func (d *Device) GetDeviceInfo(info *DeviceInfo) error {
+ var req Container
+ req.Code = OC_GetDeviceInfo
+ return d.GetData(&req, info)
}
func (d *Device) GetStorageIDs(info *Uint32Array) error {
- var req, rep Container
+ var req Container
req.Code = OC_GetStorageIDs
- var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
- return Decode(&buf, info)
+ return d.GetData(&req, info)
}
func (d *Device) GetObjectPropDesc(objPropCode, objFormatCode uint16, info *ObjectPropDesc) error {
- var req, rep Container
+ var req Container
req.Code = OC_MTP_GetObjectPropDesc
req.Param = []uint32{uint32(objPropCode), uint32(objFormatCode)}
- var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
-
- return Decode(&buf, info)
+ return d.GetData(&req, info)
}
func (d *Device) GetObjectPropValue(objHandle uint32, objPropCode uint16, value interface{}) error {
- var req, rep Container
- var buf bytes.Buffer
-
+ var req Container
req.Code = OC_MTP_GetObjectPropValue
req.Param = []uint32{objHandle, uint32(objPropCode)}
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
- return Decode(&buf, value)
+ return d.GetData(&req, value)
}
func (d *Device) SetObjectPropValue(objHandle uint32, objPropCode uint16, value interface{}) error {
var req, rep Container
req.Code = OC_MTP_SetObjectPropValue
req.Param = []uint32{objHandle, uint32(objPropCode)}
+ return d.SendData(&req, &rep, value)
+}
+
+func (d *Device) SendData(req *Container, rep *Container, value interface{}) error {
var buf bytes.Buffer
err := Encode(&buf, value)
if err != nil {
return err
}
-
- return d.RunTransaction(&req, &rep, nil, &buf, int64(buf.Len()))
+ if d.MTPDebug {
+ log.Printf("MTP encoded %#v", value)
+ }
+ return d.RunTransaction(req, rep, nil, &buf, int64(buf.Len()))
}
func (d *Device) GetObjectPropsSupported(objFormatCode uint16, props *Uint16Array) error {
- var req, rep Container
- var buf bytes.Buffer
+ var req Container
req.Code = OC_MTP_GetObjectPropsSupported
req.Param = []uint32{uint32(objFormatCode)}
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
- return Decode(&buf, props)
+ return d.GetData(&req, props)
}
func (d *Device) GetDevicePropDesc(propCode uint16, info *DevicePropDesc) error {
- var req, rep Container
+ var req Container
req.Code = OC_GetDevicePropDesc
req.Param = append(req.Param, uint32(propCode))
-
- var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
-
- return info.Decode(&buf)
+ return d.GetData(&req, info)
}
func (d *Device) SetDevicePropValue(propCode uint32, src interface{}) error {
var req, rep Container
req.Code = OC_SetDevicePropValue
req.Param = []uint32{propCode}
-
- var buf bytes.Buffer
- err := Encode(&buf, src)
- if err != nil {
- return err
- }
- return d.RunTransaction(&req, &rep, nil, &buf, int64(buf.Len()))
+ return d.SendData(&req, &rep, src)
}
func (d *Device) GetDevicePropValue(propCode uint32, dest interface{}) error {
- var req, rep Container
+ var req Container
req.Code = OC_GetDevicePropValue
req.Param = []uint32{propCode}
-
- var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
- return Decode(&buf, dest)
+ return d.GetData(&req, dest)
}
func (d *Device) ResetDevicePropValue(propCode uint32) error {
@@ -175,39 +148,24 @@ func (d *Device) ResetDevicePropValue(propCode uint32) error {
}
func (d *Device) GetStorageInfo(ID uint32, info *StorageInfo) error {
- var req, rep Container
+ var req Container
req.Code = OC_GetStorageInfo
req.Param = []uint32{ID}
- var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
- return Decode(&buf, info)
+ return d.GetData(&req, info)
}
func (d *Device) GetObjectHandles(storageID, objFormatCode, parent uint32, info *Uint32Array) error {
- var req, rep Container
+ var req Container
req.Code = OC_GetObjectHandles
req.Param = []uint32{storageID, objFormatCode, parent}
- var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
- return Decode(&buf, info)
+ return d.GetData(&req, info)
}
func (d *Device) GetObjectInfo(handle uint32, info *ObjectInfo) error {
- var req, rep Container
+ var req Container
req.Code = OC_GetObjectInfo
req.Param = []uint32{handle}
- var buf bytes.Buffer
- err := d.RunTransaction(&req, &rep, &buf, nil, 0)
- if err != nil {
- return err
- }
- return Decode(&buf, info)
+ return d.GetData(&req, info)
}
func (d *Device) GetNumObjects(storageId uint32, formatCode uint16, parent uint32) (uint32, error) {
@@ -234,13 +192,7 @@ func (d *Device) SendObjectInfo(wantStorageID, wantParent uint32, info *ObjectIn
req.Code = OC_SendObjectInfo
req.Param = []uint32{wantStorageID, wantParent}
- buf := &bytes.Buffer{}
- err = Encode(buf, info)
- if err != nil {
- return
- }
-
- err = d.RunTransaction(&req, &rep, nil, buf, int64(buf.Len()))
+ err = d.SendData(&req, &rep, info)
if err != nil {
return
}
View
9 mtp/print.go
@@ -35,6 +35,15 @@ func hexDump(data []byte) {
}
}
+// extract single name.
+func getName(m map[int]string, val int) string {
+ n, ok := m[val]
+ if !ok {
+ n = fmt.Sprintf("0x%x", val)
+ }
+ return n
+}
+
// Extract names from name map.
func getNames(m map[int]string, vals []uint16) string {
r := []string{}

0 comments on commit 6588350

Please sign in to comment.
Something went wrong with that request. Please try again.