This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgowpd_test.go
128 lines (108 loc) · 3.34 KB
/
gowpd_test.go
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
package gowpd_test
import (
"fmt"
"github.com/rlj1202/go-wpd"
"log"
"testing"
)
func TestAll(t *testing.T) {
gowpd.Initialize()
pPortableDeviceManager, err := gowpd.CreatePortableDeviceManager()
if err != nil {
panic(err)
}
deviceIDs, err := pPortableDeviceManager.GetDevices()
if err != nil {
panic(err)
}
pClientInfo, err := gowpd.CreatePortableDeviceValues()
if err != nil {
panic(err)
}
pClientInfo.SetStringValue(gowpd.WPD_CLIENT_NAME, "libgowpd")
pClientInfo.SetUnsignedIntegerValue(gowpd.WPD_CLIENT_MAJOR_VERSION, 1)
pClientInfo.SetUnsignedIntegerValue(gowpd.WPD_CLIENT_MINOR_VERSION, 0)
pClientInfo.SetUnsignedIntegerValue(gowpd.WPD_CLIENT_REVISION, 2)
for i, id := range deviceIDs {
friendlyName, err := pPortableDeviceManager.GetDeviceFriendlyName(id)
if err != nil {
panic(err)
}
manufacturer, err := pPortableDeviceManager.GetDeviceManufacturer(id)
if err != nil {
panic(err)
}
description, err := pPortableDeviceManager.GetDeviceDescription(id)
if err != nil {
panic(err)
}
fmt.Printf("[%d]\n", i)
fmt.Printf("\tdeviceName: %s\n", friendlyName)
fmt.Printf("\tmanufacturer: %s\n", manufacturer)
fmt.Printf("\tdescription: %s\n", description)
if friendlyName != "SANDISK" {
gowpd.FreeDeviceID(id)
continue
}
pPortableDevice, err := gowpd.CreatePortableDevice()
if err != nil {
panic(err)
}
// Establish a connection
err = pPortableDevice.Open(id, pClientInfo)
if err != nil {
panic(err)
}
// objectId where the file will be transferred under.
objectID := "F:\\"
// path to selected file to transfer to device.
filePath := "E:\\RedLaboratory\\Media\\Picture\\result.png"
filePath = "E:\\test.md"
// open file as IStream.
pFileStream, err := gowpd.SHCreateStreamOnFile(filePath, 0)
if err != nil {
panic(err)
}
// acquire properties needed to transfer file to device
pObjectProperties, err := gowpd.GetRequiredPropertiesForContentType(gowpd.WPD_CONTENT_TYPE_IMAGE, objectID, filePath, pFileStream)
if err != nil {
panic(err)
}
// get stream to device
content, err := pPortableDevice.Content()
if err != nil {
panic(err)
}
pTempStream, cbTransferSize, err := content.CreateObjectWithPropertiesAndData(pObjectProperties)
if err != nil {
panic(err)
}
// convert pTempStream to PortableDeviceDataStream to use more method e.g newly created object id.
_pFinalObjectDataStream, err := pTempStream.QueryInterface(gowpd.IID_IPortableDeviceDataStream)
if err != nil {
panic(err)
}
pFinalObjectDataStream := (*gowpd.IPortableDeviceDataStream)(_pFinalObjectDataStream)
// copy data from pFileStream to pFinalObjectDataStream
cbBytesWritten, err := gowpd.StreamCopy((*gowpd.IStream)(_pFinalObjectDataStream), pFileStream, cbTransferSize)
if err != nil {
panic(err)
}
// call commit method to notice device that transferring data is finished.
err = pFinalObjectDataStream.Commit(0)
if err != nil {
panic(err)
}
newlyCreatedObjectID, err := pFinalObjectDataStream.GetObjectID()
if err != nil {
panic(err)
}
log.Printf("\"%s\" has been transferred to device successfully: %d\n", newlyCreatedObjectID, cbBytesWritten)
// transferring is finished. release the deviceID.
gowpd.FreeDeviceID(id)
// release device interface too.
pPortableDevice.Release()
}
pPortableDeviceManager.Release()
gowpd.Uninitialize()
}