Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 234 lines (196 sloc) 7.324 kB
e1a50b2 @yifanlu Able to initialize the Vita
authored
1 //
2 // vitamtp.c
3 // VitaMTP
4 //
5 // Created by Yifan Lu on 2/19/12.
6 // Copyright (c) 2012 __MyCompanyName__. All rights reserved.
7 //
8
52a8757 @yifanlu Merged and modified changes from dridri85, implementing: SendStorageS…
authored
9 #define _FILE_OFFSET_BITS 64
10
e1a50b2 @yifanlu Able to initialize the Vita
authored
11 #include <stdio.h>
12 #include <stdlib.h>
13 #include <string.h>
14 #include "vitamtp.h"
15
16 /**
17 * Get the first (as in "first in the list of") connected Vita MTP device.
18 * @return a device pointer. NULL if error, no connected device, or no connected Vita
19 * @see LIBMTP_Get_Connected_Devices()
20 */
21 LIBMTP_mtpdevice_t *LIBVitaMTP_Get_First_Vita(void)
22 {
23 LIBMTP_mtpdevice_t *first_device = NULL;
24 LIBMTP_raw_device_t *devices;
25 int numdevs;
26 int i;
27 LIBMTP_error_number_t ret;
28
29 ret = LIBMTP_Detect_Raw_Devices(&devices, &numdevs);
30 if (ret != LIBMTP_ERROR_NONE) {
31 return NULL;
32 }
33
34 if (devices == NULL || numdevs == 0) {
35 return NULL;
36 }
37
38 for (i = 0; i < numdevs; i++)
39 {
40 if(devices[i].device_entry.vendor_id == VITA_VID &&
41 devices[i].device_entry.product_id == VITA_PID)
42 {
43 first_device = LIBMTP_Open_Raw_Device(&devices[i]);
44 break;
45 }
46 LIBMTP_Release_Device(first_device);
47 }
48
49
50 free(devices);
51 return first_device; // NULL if no vita
52 }
53
52a8757 @yifanlu Merged and modified changes from dridri85, implementing: SendStorageS…
authored
54 uint16_t VitaMTP_SendData(LIBMTP_mtpdevice_t *device, uint32_t event_id, uint32_t code, void* data, unsigned int len){
55 PTPParams *params = (PTPParams*)device->params;
56 PTPContainer ptp;
57
58 PTP_CNT_INIT(ptp);
59 ptp.Code = code;
60 ptp.Nparam = 1;
61 ptp.Param1 = event_id;
62
63 uint16_t ret = ptp_transaction(params, &ptp, PTP_DP_SENDDATA, len, (unsigned char**)&data, 0);
64 return ret;
65 }
66
67 uint16_t VitaMTP_GetData(LIBMTP_mtpdevice_t *device, uint32_t event_id, uint32_t code, void* data, unsigned int* len){
68 PTPParams *params = (PTPParams*)device->params;
69 PTPContainer ptp;
70
71 PTP_CNT_INIT(ptp);
72 ptp.Code = code;
73 ptp.Nparam = 1;
74 ptp.Param1 = event_id;
75
76 uint16_t ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, (unsigned char**)&data, len);
77
78 return ret;
79 }
80
81 uint16_t VitaMTP_SendInt32(LIBMTP_mtpdevice_t *device, uint32_t event_id, uint32_t code, uint32_t value){
82 PTPParams *params = (PTPParams*)device->params;
83 PTPContainer ptp;
84
85 PTP_CNT_INIT(ptp);
86 ptp.Code = code;
87 ptp.Nparam = 2;
88 ptp.Param1 = event_id;
89 ptp.Param2 = value;
90
91 return ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, 0);
92 }
93
e1a50b2 @yifanlu Able to initialize the Vita
authored
94 uint16_t VitaMTP_GetVitaInfo(LIBMTP_mtpdevice_t *device, vita_info_t *info)
95 {
96 PTPParams *params = (PTPParams*)device->params;
97 PTPContainer ptp;
98 int ret;
99
100 PTP_CNT_INIT(ptp);
101 ptp.Code = PTP_OC_VITA_GetVitaInfo;
102 ptp.Nparam = 0;
103 ret = ptp_transaction(params, &ptp, PTP_DP_GETDATA, 0, &info->raw_xml, 0);
104 if(ret != 0)
105 {
106 return ret;
107 }
108 // TODO: Parse XML
109 int len;
110 memcpy(&len, info->raw_xml, sizeof(int));
111 return 0;
112 }
113
114 uint16_t VitaMTP_SendInitiatorInfo(LIBMTP_mtpdevice_t *device, initiator_info_t *info)
115 {
116 // TODO: Parse XML
117 PTPParams *params = (PTPParams*)device->params;
118 PTPContainer ptp;
119
120 PTP_CNT_INIT(ptp);
121 ptp.Code = PTP_OC_VITA_SendInitiatorInfo;
122 ptp.Nparam = 0;
123 return ptp_transaction(params, &ptp, PTP_DP_SENDDATA, info->raw_len, &info->raw_xml, 0);
124 }
125
d8d966b @yifanlu Changed to strict types for MTP commands
authored
126 uint16_t VitaMTP_SendHostStatus(LIBMTP_mtpdevice_t *device, uint32_t status)
e1a50b2 @yifanlu Able to initialize the Vita
authored
127 {
128 PTPParams *params = (PTPParams*)device->params;
129 PTPContainer ptp;
130
131 PTP_CNT_INIT(ptp);
132 ptp.Code = PTP_OC_VITA_SendHostStatus;
133 ptp.Nparam = 1;
134 ptp.Param1 = status;
135 return ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, 0);
136 }
137
d8d966b @yifanlu Changed to strict types for MTP commands
authored
138 uint16_t VitaMTP_GetSettingInfo(LIBMTP_mtpdevice_t *device, uint32_t event_id, settings_info_t *info)
e1a50b2 @yifanlu Able to initialize the Vita
authored
139 {
52a8757 @yifanlu Merged and modified changes from dridri85, implementing: SendStorageS…
authored
140 unsigned int len;
141 return VitaMTP_GetData(device, event_id, PTP_OC_VITA_GetSettingInfo, &info->raw_xml, &len);
e1a50b2 @yifanlu Able to initialize the Vita
authored
142 // TODO: Parse XML
143 }
d8d966b @yifanlu Changed to strict types for MTP commands
authored
144
145 uint16_t VitaMTP_ReportResult(LIBMTP_mtpdevice_t *device, uint32_t event_id, uint16_t result)
146 {
147 PTPParams *params = (PTPParams*)device->params;
148 PTPContainer ptp;
149
150 PTP_CNT_INIT(ptp);
151 ptp.Code = PTP_OC_VITA_ReportResult;
152 ptp.Nparam = 2;
153 ptp.Param1 = event_id;
154 ptp.Param2 = result;
155 return ptp_transaction(params, &ptp, PTP_DP_NODATA, 0, NULL, 0);
156 }
27531e7 @yifanlu Added commands for update checking. (Still no XML parsing yet)
authored
157
158 uint16_t VitaMTP_GetUrl(LIBMTP_mtpdevice_t *device, uint32_t event_id, unsigned char *data)
159 {
52a8757 @yifanlu Merged and modified changes from dridri85, implementing: SendStorageS…
authored
160 unsigned int len = 0;
161 return VitaMTP_GetData(device, event_id, PTP_OC_VITA_GetUrl, &data, &len);
27531e7 @yifanlu Added commands for update checking. (Still no XML parsing yet)
authored
162 // TODO: Parse XML
163 }
164
165 uint16_t VitaMTP_SendHttpObjectFromURL(LIBMTP_mtpdevice_t *device, uint32_t event_id, int len, unsigned char *data)
166 {
52a8757 @yifanlu Merged and modified changes from dridri85, implementing: SendStorageS…
authored
167 return VitaMTP_SendData(device, event_id, PTP_OC_VITA_SendHttpObjectFromURL, data, len);
168 }
169
170 uint16_t VitaMTP_SendStorageSize(LIBMTP_mtpdevice_t *device, uint32_t event_id, uint64_t storage_size, uint64_t available_size){
171 static const unsigned char padding[] = {0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
172 static const int len = 25;
173 uint16_t ret;
174 uint64_t* data = malloc(len);
175 data[0] = storage_size;
176 data[1] = available_size;
177 memcpy(&data[2], padding, sizeof(padding));
178 ret = VitaMTP_SendData(device, event_id, PTP_OC_VITA_SendStorageSize, data, len);
179 free(data);
180 return ret;
181 }
182
183 uint16_t VitaMTP_SendNumOfObject(LIBMTP_mtpdevice_t *device, uint32_t event_id, uint32_t num){
184 return VitaMTP_SendInt32(device, event_id, PTP_OC_VITA_SendNumOfObject, num);
185 }
186
187 uint16_t VitaMTP_GetBrowseInfo(LIBMTP_mtpdevice_t *device, uint32_t event_id, browse_info_t* info){
188 unsigned int len = 0; // unused for now
189 return VitaMTP_GetData(device, event_id, PTP_OC_VITA_GetBrowseInfo, &info, &len);
190 }
191
192 uint16_t VitaMTP_SendObjectMetadata(LIBMTP_mtpdevice_t *device, uint32_t event_id, metadata_t* metas){
193 // TODO: Create XML
194 uint16_t ret = VitaMTP_SendData(device, event_id, PTP_OC_VITA_SendObjectMetadata, metas->raw_xml, metas->raw_len);
195 return ret;
196 }
197
198 uint16_t VitaMTP_SendObjectThumb(LIBMTP_mtpdevice_t *device, uint32_t event_id, thumbnail_t* thumb){
199 // TODO: Create XML
200 uint16_t ret = VitaMTP_SendData(device, event_id, PTP_OC_VITA_SendObjectThumb, thumb->raw_xml, thumb->raw_len);
201 return ret;
202 }
203
204 uint16_t VitaMTP_SendObjectStatus(LIBMTP_mtpdevice_t *device, uint32_t event_id, object_status_t* status){
205 int* data;
206 unsigned int len = 0;
207 uint16_t ret = VitaMTP_GetData(device, event_id, PTP_OC_VITA_SendObjectStatus, &data, &len);
208 status->p_data = data;
209 status->type = data[0];
210 status->size = data[1];
211 status->file = (char*)&data[2];
212 return ret;
213 }
214
215 uint16_t VitaMTP_SendPartOfObjectInit(LIBMTP_mtpdevice_t *device, uint32_t event_id, send_part_init_t* send_init){
216 return VitaMTP_GetData(device, event_id, PTP_OC_VITA_SendPartOfObjectInit, &send_init, NULL);
217 }
218
219 uint16_t VitaMTP_SendPartOfObject(LIBMTP_mtpdevice_t *device, uint32_t event_id, send_part_init_t* init, metadata_t* meta){
220 FILE* fp = fopen(meta->path, "rb");
221 if(init->size > UINT32_MAX) // Because of libptp's length limits, we cannot be bigger than an int
222 return PTP_RC_OperationNotSupported;
223 if(!fp)
224 return PTP_RC_AccessDenied;
225 uint8_t* data = (uint8_t*)malloc((uint32_t)init->size + 8);
226 fseeko(fp, init->offset, SEEK_SET);
227 fread(&data[8], sizeof(char), init->size, fp);
228 ((uint64_t*)data)[0] = init->size;
229 fclose(fp);
230 uint16_t ret = VitaMTP_SendData(device, event_id, PTP_OC_VITA_SendPartOfObject, data, (uint32_t)init->size + 8);
231 free(data);
232 return ret;
27531e7 @yifanlu Added commands for update checking. (Still no XML parsing yet)
authored
233 }
Something went wrong with that request. Please try again.