summaryrefslogblamecommitdiffstats
path: root/mtp/ffs/mtp_MtpDatabase.cpp
blob: 30076982b097eb29cee10f68b864265cef4d37eb (plain) (tree)
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850

















































































































































































































































































































































































































































































































































































































































































































































































































































































                                                                                                                                                        
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *	  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * Copyright (C) 2014 TeamWin - bigbiff and Dees_Troy mtp database conversion to C++
 */

#include <utils/Log.h>

#include <assert.h>
#include <cutils/properties.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <libgen.h>
#include <limits.h>
#include <stdio.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <map>
#include <string>

#include "MtpDataPacket.h"
#include "MtpDatabase.h"
#include "MtpDebug.h"
#include "MtpObjectInfo.h"
#include "MtpProperty.h"
#include "MtpStorage.h"
#include "MtpStringBuffer.h"
#include "MtpUtils.h"
#include "mtp.h"
#include "mtp_MtpDatabase.hpp"

IMtpDatabase::IMtpDatabase() {
  storagenum = 0;
  count = -1;
}

IMtpDatabase::~IMtpDatabase() {
  std::map<int, MtpStorage*>::iterator i;
  for (i = storagemap.begin(); i != storagemap.end(); i++) {
	delete i->second;
  }
}

int IMtpDatabase::DEVICE_PROPERTIES[3] = { MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER,
										   MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME,
										   MTP_DEVICE_PROPERTY_IMAGE_SIZE };

int IMtpDatabase::FILE_PROPERTIES[10] = {
  // NOTE must match beginning of AUDIO_PROPERTIES, VIDEO_PROPERTIES
  // and IMAGE_PROPERTIES below
  MTP_PROPERTY_STORAGE_ID, MTP_PROPERTY_OBJECT_FORMAT, MTP_PROPERTY_PROTECTION_STATUS,
  MTP_PROPERTY_OBJECT_SIZE, MTP_PROPERTY_OBJECT_FILE_NAME, MTP_PROPERTY_DATE_MODIFIED,
  MTP_PROPERTY_PARENT_OBJECT, MTP_PROPERTY_PERSISTENT_UID, MTP_PROPERTY_NAME,
  // TODO: why is DISPLAY_NAME not here?
  MTP_PROPERTY_DATE_ADDED
};

int IMtpDatabase::AUDIO_PROPERTIES[19] = {
  // NOTE must match FILE_PROPERTIES above
  MTP_PROPERTY_STORAGE_ID, MTP_PROPERTY_OBJECT_FORMAT, MTP_PROPERTY_PROTECTION_STATUS,
  MTP_PROPERTY_OBJECT_SIZE, MTP_PROPERTY_OBJECT_FILE_NAME, MTP_PROPERTY_DATE_MODIFIED,
  MTP_PROPERTY_PARENT_OBJECT, MTP_PROPERTY_PERSISTENT_UID, MTP_PROPERTY_NAME,
  MTP_PROPERTY_DISPLAY_NAME, MTP_PROPERTY_DATE_ADDED,

  // audio specific properties
  MTP_PROPERTY_ARTIST, MTP_PROPERTY_ALBUM_NAME, MTP_PROPERTY_ALBUM_ARTIST, MTP_PROPERTY_TRACK,
  MTP_PROPERTY_ORIGINAL_RELEASE_DATE, MTP_PROPERTY_DURATION, MTP_PROPERTY_GENRE,
  MTP_PROPERTY_COMPOSER
};

int IMtpDatabase::VIDEO_PROPERTIES[15] = {
  // NOTE must match FILE_PROPERTIES above
  MTP_PROPERTY_STORAGE_ID, MTP_PROPERTY_OBJECT_FORMAT, MTP_PROPERTY_PROTECTION_STATUS,
  MTP_PROPERTY_OBJECT_SIZE, MTP_PROPERTY_OBJECT_FILE_NAME, MTP_PROPERTY_DATE_MODIFIED,
  MTP_PROPERTY_PARENT_OBJECT, MTP_PROPERTY_PERSISTENT_UID, MTP_PROPERTY_NAME,
  MTP_PROPERTY_DISPLAY_NAME, MTP_PROPERTY_DATE_ADDED,

  // video specific properties
  MTP_PROPERTY_ARTIST, MTP_PROPERTY_ALBUM_NAME, MTP_PROPERTY_DURATION, MTP_PROPERTY_DESCRIPTION
};

int IMtpDatabase::IMAGE_PROPERTIES[12] = {
  // NOTE must match FILE_PROPERTIES above
  MTP_PROPERTY_STORAGE_ID, MTP_PROPERTY_OBJECT_FORMAT, MTP_PROPERTY_PROTECTION_STATUS,
  MTP_PROPERTY_OBJECT_SIZE, MTP_PROPERTY_OBJECT_FILE_NAME, MTP_PROPERTY_DATE_MODIFIED,
  MTP_PROPERTY_PARENT_OBJECT, MTP_PROPERTY_PERSISTENT_UID, MTP_PROPERTY_NAME,
  MTP_PROPERTY_DISPLAY_NAME, MTP_PROPERTY_DATE_ADDED,

  // image specific properties
  MTP_PROPERTY_DESCRIPTION
};

int IMtpDatabase::ALL_PROPERTIES[25] = {
  // NOTE must match FILE_PROPERTIES above
  MTP_PROPERTY_STORAGE_ID, MTP_PROPERTY_OBJECT_FORMAT, MTP_PROPERTY_PROTECTION_STATUS,
  MTP_PROPERTY_OBJECT_SIZE, MTP_PROPERTY_OBJECT_FILE_NAME, MTP_PROPERTY_DATE_MODIFIED,
  MTP_PROPERTY_PARENT_OBJECT, MTP_PROPERTY_PERSISTENT_UID, MTP_PROPERTY_NAME,
  MTP_PROPERTY_DISPLAY_NAME, MTP_PROPERTY_DATE_ADDED,

  // image specific properties
  MTP_PROPERTY_DESCRIPTION,

  // audio specific properties
  MTP_PROPERTY_ARTIST, MTP_PROPERTY_ALBUM_NAME, MTP_PROPERTY_ALBUM_ARTIST, MTP_PROPERTY_TRACK,
  MTP_PROPERTY_ORIGINAL_RELEASE_DATE, MTP_PROPERTY_DURATION, MTP_PROPERTY_GENRE,
  MTP_PROPERTY_COMPOSER,

  // video specific properties
  MTP_PROPERTY_ARTIST, MTP_PROPERTY_ALBUM_NAME, MTP_PROPERTY_DURATION, MTP_PROPERTY_DESCRIPTION,

  // image specific properties
  MTP_PROPERTY_DESCRIPTION
};

int IMtpDatabase::SUPPORTED_PLAYBACK_FORMATS[26] = { SUPPORTED_PLAYBACK_FORMAT_UNDEFINED,
													 SUPPORTED_PLAYBACK_FORMAT_ASSOCIATION,
													 SUPPORTED_PLAYBACK_FORMAT_TEXT,
													 SUPPORTED_PLAYBACK_FORMAT_HTML,
													 SUPPORTED_PLAYBACK_FORMAT_WAV,
													 SUPPORTED_PLAYBACK_FORMAT_MP3,
													 SUPPORTED_PLAYBACK_FORMAT_MPEG,
													 SUPPORTED_PLAYBACK_FORMAT_EXIF_JPEG,
													 SUPPORTED_PLAYBACK_FORMAT_TIFF_EP,
													 SUPPORTED_PLAYBACK_FORMAT_BMP,
													 SUPPORTED_PLAYBACK_FORMAT_GIF,
													 SUPPORTED_PLAYBACK_FORMAT_JFIF,
													 SUPPORTED_PLAYBACK_FORMAT_PNG,
													 SUPPORTED_PLAYBACK_FORMAT_TIFF,
													 SUPPORTED_PLAYBACK_FORMAT_WMA,
													 SUPPORTED_PLAYBACK_FORMAT_OGG,
													 SUPPORTED_PLAYBACK_FORMAT_AAC,
													 SUPPORTED_PLAYBACK_FORMAT_MP4_CONTAINER,
													 SUPPORTED_PLAYBACK_FORMAT_MP2,
													 SUPPORTED_PLAYBACK_FORMAT_3GP_CONTAINER,
													 SUPPORTED_PLAYBACK_FORMAT_ABSTRACT_AV_PLAYLIST,
													 SUPPORTED_PLAYBACK_FORMAT_WPL_PLAYLIST,
													 SUPPORTED_PLAYBACK_FORMAT_M3U_PLAYLIST,
													 SUPPORTED_PLAYBACK_FORMAT_PLS_PLAYLIST,
													 SUPPORTED_PLAYBACK_FORMAT_XML_DOCUMENT,
													 SUPPORTED_PLAYBACK_FORMAT_FLAC };

MtpObjectHandle IMtpDatabase::beginSendObject(const char* path, MtpObjectFormat format,
											  MtpObjectHandle parent, MtpStorageID storageID,
											  uint64_t size, time_t modified) {
  if (storagemap.find(storageID) == storagemap.end()) return kInvalidObjectHandle;
  return storagemap[storageID]->beginSendObject(path, format, parent, size, modified);
}

void IMtpDatabase::endSendObject(const char* path, MtpObjectHandle handle, MtpObjectFormat format,
								 bool succeeded) {
  MTPD("endSendObject() %s\n", path);
  if (!succeeded) {
	MTPE("endSendObject() failed, unlinking %s\n", path);
	unlink(path);
  }
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++)
	storit->second->endSendObject(path, handle, format, succeeded);
}

void IMtpDatabase::createDB(MtpStorage* storage, MtpStorageID storageID) {
  storagemap[storageID] = storage;
  storage->createDB();
}

void IMtpDatabase::destroyDB(MtpStorageID storageID) {
  MtpStorage* storage = storagemap[storageID];
  storagemap.erase(storageID);
  delete storage;
}

MtpObjectHandleList* IMtpDatabase::getObjectList(MtpStorageID storageID,
												 __attribute__((unused)) MtpObjectFormat format,
												 MtpObjectHandle parent) {
  MTPD("IMtpDatabase::getObjectList::storageID: %d\n", storageID);
  MtpObjectHandleList* list = storagemap[storageID]->getObjectList(storageID, parent);
  MTPD("IMtpDatabase::getObjectList::list size: %d\n", list->size());
  return list;
}

int IMtpDatabase::getNumObjects(MtpStorageID storageID,
								__attribute__((unused)) MtpObjectFormat format,
								MtpObjectHandle parent) {
  MtpObjectHandleList* list = storagemap[storageID]->getObjectList(storageID, parent);
  int size = list->size();
  delete list;
  return size;
}

MtpObjectFormatList* IMtpDatabase::getSupportedPlaybackFormats() {
  // This function tells the host PC which file formats the device supports
  MtpObjectFormatList* list = new MtpObjectFormatList();
  int length = sizeof(SUPPORTED_PLAYBACK_FORMATS) / sizeof(SUPPORTED_PLAYBACK_FORMATS[0]);
  MTPD("IMtpDatabase::getSupportedPlaybackFormats length: %i\n", length);
  for (int i = 0; i < length; i++) {
	MTPD("supported playback format: %x\n", SUPPORTED_PLAYBACK_FORMATS[i]);
	list->push_back(SUPPORTED_PLAYBACK_FORMATS[i]);
  }
  return list;
}

MtpObjectFormatList* IMtpDatabase::getSupportedCaptureFormats() {
  // Android OS implementation of this function returns NULL
  // so we are not implementing this function either.
  MTPD(
	  "IMtpDatabase::getSupportedCaptureFormats returning NULL (This is what Android does as "
	  "well).\n");
  return NULL;
}

MtpObjectPropertyList* IMtpDatabase::getSupportedObjectProperties(MtpObjectFormat format) {
  int* properties;
  MtpObjectPropertyList* list = new MtpObjectPropertyList();
  int length = 0;
  switch (format) {
	case MTP_FORMAT_MP3:
	case MTP_FORMAT_WAV:
	case MTP_FORMAT_WMA:
	case MTP_FORMAT_OGG:
	case MTP_FORMAT_AAC:
	  properties = AUDIO_PROPERTIES;
	  length = sizeof(AUDIO_PROPERTIES) / sizeof(AUDIO_PROPERTIES[0]);
	  break;
	case MTP_FORMAT_MPEG:
	case MTP_FORMAT_3GP_CONTAINER:
	case MTP_FORMAT_WMV:
	  properties = VIDEO_PROPERTIES;
	  length = sizeof(VIDEO_PROPERTIES) / sizeof(VIDEO_PROPERTIES[0]);
	  break;
	case MTP_FORMAT_EXIF_JPEG:
	case MTP_FORMAT_GIF:
	case MTP_FORMAT_PNG:
	case MTP_FORMAT_BMP:
	  properties = IMAGE_PROPERTIES;
	  length = sizeof(IMAGE_PROPERTIES) / sizeof(IMAGE_PROPERTIES[0]);
	  break;
	case 0:
	  properties = ALL_PROPERTIES;
	  length = sizeof(ALL_PROPERTIES) / sizeof(ALL_PROPERTIES[0]);
	  break;
	default:
	  properties = FILE_PROPERTIES;
	  length = sizeof(FILE_PROPERTIES) / sizeof(FILE_PROPERTIES[0]);
  }
  MTPD("IMtpDatabase::getSupportedObjectProperties length is: %i, format: %x", length, format);
  for (int i = 0; i < length; i++) {
	MTPD("supported object property: %x\n", properties[i]);
	list->push_back(properties[i]);
  }
  return list;
}

MtpDevicePropertyList* IMtpDatabase::getSupportedDeviceProperties() {
  MtpDevicePropertyList* list = new MtpDevicePropertyList();
  int length = sizeof(DEVICE_PROPERTIES) / sizeof(DEVICE_PROPERTIES[0]);
  MTPD("IMtpDatabase::getSupportedDeviceProperties length was: %i\n", length);
  for (int i = 0; i < length; i++) list->push_back(DEVICE_PROPERTIES[i]);
  return list;
}

MtpResponseCode IMtpDatabase::getObjectPropertyValue(MtpObjectHandle handle,
													 MtpObjectProperty property,
													 MtpDataPacket& packet) {
  MTPD("IMtpDatabase::getObjectPropertyValue mtpid: %u, property: %x\n", handle, property);
  int type;
  MtpResponseCode result = MTP_RESPONSE_INVALID_OBJECT_HANDLE;
  MtpStorage::PropEntry prop;
  if (!getObjectPropertyInfo(property, type)) {
	MTPE("IMtpDatabase::getObjectPropertyValue returning MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED\n");
	return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
  }
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
	if (storit->second->getObjectPropertyValue(handle, property, prop) == 0) {
	  result = MTP_RESPONSE_OK;
	  break;
	}
  }

  if (result != MTP_RESPONSE_OK) {
	MTPE("IMtpDatabase::getObjectPropertyValue unable to locate handle: %u\n", handle);
	return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
  }

  uint64_t longValue = prop.intvalue;
  // special case date properties, which are strings to MTP
  // but stored internally as a uint64
  if (property == MTP_PROPERTY_DATE_MODIFIED || property == MTP_PROPERTY_DATE_ADDED) {
	char date[20];
	formatDateTime(longValue, date, sizeof(date));
	packet.putString(date);
	goto out;
  }

  switch (type) {
	case MTP_TYPE_INT8:
	  packet.putInt8(longValue);
	  break;
	case MTP_TYPE_UINT8:
	  packet.putUInt8(longValue);
	  break;
	case MTP_TYPE_INT16:
	  packet.putInt16(longValue);
	  break;
	case MTP_TYPE_UINT16:
	  packet.putUInt16(longValue);
	  break;
	case MTP_TYPE_INT32:
	  packet.putInt32(longValue);
	  break;
	case MTP_TYPE_UINT32:
	  packet.putUInt32(longValue);
	  break;
	case MTP_TYPE_INT64:
	  packet.putInt64(longValue);
	  break;
	case MTP_TYPE_UINT64:
	  packet.putUInt64(longValue);
	  break;
	case MTP_TYPE_INT128:
	  packet.putInt128(longValue);
	  break;
	case MTP_TYPE_UINT128:
	  packet.putUInt128(longValue);
	  break;
	case MTP_TYPE_STR: {
	  /*std::string stringValue = (string)stringValuesArray[0];
	  if (stringValue) {
		const char* str = stringValue.c_str();
		if (str == NULL) {
		  return MTP_RESPONSE_GENERAL_ERROR;
		}
		packet.putString(str);
	  } else {
		packet.putEmptyString();
	  }*/
	  packet.putString(prop.strvalue.c_str());
	  MTPD("MTP_TYPE_STR: %x = %s\n", prop.property, prop.strvalue.c_str());
	  // MTPE("STRING unsupported type in getObjectPropertyValue\n");
	  // result = MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
	  break;
	}
	default:
	  MTPE("unsupported type in getObjectPropertyValue\n");
	  result = MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
  }
out:
  return result;
}

MtpResponseCode IMtpDatabase::setObjectPropertyValue(MtpObjectHandle handle,
													 MtpObjectProperty property,
													 MtpDataPacket& packet) {
  int type;
  MTPD("IMtpDatabase::setObjectPropertyValue start\n");
  if (!getObjectPropertyInfo(property, type)) {
	MTPE("IMtpDatabase::setObjectPropertyValue returning MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED\n");
	return MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
  }
  MTPD("IMtpDatabase::setObjectPropertyValue continuing\n");

  int8_t int8_t_value;
  uint8_t uint8_t_value;
  int16_t int16_t_value;
  uint16_t uint16_t_value;
  int32_t int32_t_value;
  uint32_t uint32_t_value;
  int64_t int64_t_value;
  uint64_t uint64_t_value;
  std::string stringValue;

  switch (type) {
	case MTP_TYPE_INT8:
	  MTPD("int8\n");
	  packet.getInt8(int8_t_value);
	  break;
	case MTP_TYPE_UINT8:
	  MTPD("uint8\n");
	  packet.getUInt8(uint8_t_value);
	  break;
	case MTP_TYPE_INT16:
	  MTPD("int16\n");
	  packet.getInt16(int16_t_value);
	  break;
	case MTP_TYPE_UINT16:
	  MTPD("uint16\n");
	  packet.getUInt16(uint16_t_value);
	  break;
	case MTP_TYPE_INT32:
	  MTPD("int32\n");
	  packet.getInt32(int32_t_value);
	  break;
	case MTP_TYPE_UINT32:
	  MTPD("uint32\n");
	  packet.getUInt32(uint32_t_value);
	  break;
	case MTP_TYPE_INT64:
	  MTPD("int64\n");
	  packet.getInt64(int64_t_value);
	  break;
	case MTP_TYPE_UINT64:
	  MTPD("uint64\n");
	  packet.getUInt64(uint64_t_value);
	  break;
	case MTP_TYPE_STR: {
	  MTPD("string\n");
	  MtpStringBuffer buffer;
	  packet.getString(buffer);
	  stringValue = buffer;
	  break;
	}
	default:
	  MTPE("IMtpDatabase::setObjectPropertyValue unsupported type %i in getObjectPropertyValue\n",
		   type);
	  return MTP_RESPONSE_INVALID_OBJECT_PROP_FORMAT;
  }

  int result = MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;

  switch (property) {
	case MTP_PROPERTY_OBJECT_FILE_NAME: {
	  MTPD("IMtpDatabase::setObjectPropertyValue renaming file, handle: %d, new name: '%s'\n",
		   handle, stringValue.c_str());
	  std::map<int, MtpStorage*>::iterator storit;
	  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
		if (storit->second->renameObject(handle, stringValue) == 0) {
		  MTPD("MTP_RESPONSE_OK\n");
		  result = MTP_RESPONSE_OK;
		  break;
		}
	  }
	} break;

	default:
	  MTPE("IMtpDatabase::setObjectPropertyValue property %x not supported.\n", property);
	  result = MTP_RESPONSE_OBJECT_PROP_NOT_SUPPORTED;
  }
  MTPD("IMtpDatabase::setObjectPropertyValue returning %d\n", result);
  return result;
}

MtpResponseCode IMtpDatabase::getDevicePropertyValue(MtpDeviceProperty property,
													 MtpDataPacket& packet) {
  int type, result = 0;
  char prop_value[PROPERTY_VALUE_MAX];
  MTPD("property %s\n", MtpDebug::getDevicePropCodeName(property));
  if (!getDevicePropertyInfo(property, type)) {
	MTPE("IMtpDatabase::getDevicePropertyValue MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED\n");
	return MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
  }
  MTPD("property %s\n", MtpDebug::getDevicePropCodeName(property));
  MTPD("property %x\n", property);
  MTPD("MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME %x\n", MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME);
  switch (property) {
	case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
	case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
	  result = MTP_RESPONSE_OK;
	  break;
	default: {
	  MTPE("IMtpDatabase::getDevicePropertyValue property %x not supported\n", property);
	  result = MTP_RESPONSE_DEVICE_PROP_NOT_SUPPORTED;
	  break;
	}
  }

  if (result != MTP_RESPONSE_OK) {
	MTPD("MTP_REPONSE_OK NOT OK\n");
	return result;
  }

  long longValue = 0;
  property_get("ro.build.product", prop_value, "unknown manufacturer");
  switch (type) {
	case MTP_TYPE_INT8: {
	  MTPD("MTP_TYPE_INT8\n");
	  packet.putInt8(longValue);
	  break;
	}
	case MTP_TYPE_UINT8: {
	  MTPD("MTP_TYPE_UINT8\n");
	  packet.putUInt8(longValue);
	  break;
	}
	case MTP_TYPE_INT16: {
	  MTPD("MTP_TYPE_INT16\n");
	  packet.putInt16(longValue);
	  break;
	}
	case MTP_TYPE_UINT16: {
	  MTPD("MTP_TYPE_UINT16\n");
	  packet.putUInt16(longValue);
	  break;
	}
	case MTP_TYPE_INT32: {
	  MTPD("MTP_TYPE_INT32\n");
	  packet.putInt32(longValue);
	  break;
	}
	case MTP_TYPE_UINT32: {
	  MTPD("MTP_TYPE_UINT32\n");
	  packet.putUInt32(longValue);
	  break;
	}
	case MTP_TYPE_INT64: {
	  MTPD("MTP_TYPE_INT64\n");
	  packet.putInt64(longValue);
	  break;
	}
	case MTP_TYPE_UINT64: {
	  MTPD("MTP_TYPE_UINT64\n");
	  packet.putUInt64(longValue);
	  break;
	}
	case MTP_TYPE_INT128: {
	  MTPD("MTP_TYPE_INT128\n");
	  packet.putInt128(longValue);
	  break;
	}
	case MTP_TYPE_UINT128: {
	  MTPD("MTP_TYPE_UINT128\n");
	  packet.putInt128(longValue);
	  break;
	}
	case MTP_TYPE_STR: {
	  MTPD("MTP_TYPE_STR\n");
	  char* str = prop_value;
	  packet.putString(str);
	  break;
	}
	default:
	  MTPE("IMtpDatabase::getDevicePropertyValue unsupported type %i in getDevicePropertyValue\n",
		   type);
	  return MTP_RESPONSE_INVALID_DEVICE_PROP_FORMAT;
  }

  return MTP_RESPONSE_OK;
}

MtpResponseCode IMtpDatabase::setDevicePropertyValue(__attribute__((unused))
													 MtpDeviceProperty property,
													 __attribute__((unused))
													 MtpDataPacket& packet) {
  MTPE("IMtpDatabase::setDevicePropertyValue not implemented, returning 0\n");
  return 0;
}

MtpResponseCode IMtpDatabase::resetDeviceProperty(__attribute__((unused))
												  MtpDeviceProperty property) {
  MTPE("IMtpDatabase::resetDeviceProperty not implemented, returning -1\n");
  return -1;
}

MtpResponseCode IMtpDatabase::getObjectPropertyList(MtpObjectHandle handle, uint32_t format,
													uint32_t property, int groupCode, int depth,
													MtpDataPacket& packet) {
  MTPD("getObjectPropertyList()\n");
  MTPD("property: %x\n", property);
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
	MTPD("IMtpDatabase::getObjectPropertyList calling getObjectPropertyList\n");
	if (storit->second->getObjectPropertyList(handle, format, property, groupCode, depth, packet) ==
		0) {
	  MTPD("MTP_RESPONSE_OK\n");
	  return MTP_RESPONSE_OK;
	}
  }
  MTPE("IMtpDatabase::getObjectPropertyList MTP_RESPOSNE_INVALID_OBJECT_HANDLE %i\n", handle);
  return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
}

MtpResponseCode IMtpDatabase::getObjectInfo(MtpObjectHandle handle, MtpObjectInfo& info) {
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
	if (storit->second->getObjectInfo(handle, info) == 0) {
	  MTPD("MTP_RESPONSE_OK\n");
	  return MTP_RESPONSE_OK;
	}
  }
  MTPE("IMtpDatabase::getObjectInfo MTP_RESPONSE_INVALID_OBJECT_HANDLE %i\n", handle);
  return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
}

void* IMtpDatabase::getThumbnail(__attribute__((unused)) MtpObjectHandle handle,
								 __attribute__((unused)) size_t& outThumbSize) {
  MTPE("IMtpDatabase::getThumbnail not implemented, returning 0\n");
  return 0;
}

MtpResponseCode IMtpDatabase::getObjectFilePath(MtpObjectHandle handle,
												MtpStringBuffer& outFilePath,
												int64_t& outFileLength,
												MtpObjectFormat& outFormat) {
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
	MTPD("IMtpDatabase::getObjectFilePath calling getObjectFilePath\n");
	if (storit->second->getObjectFilePath(handle, outFilePath, outFileLength, outFormat) == 0) {
	  MTPD("MTP_RESPONSE_OK\n");
	  return MTP_RESPONSE_OK;
	}
  }
  MTPE("IMtpDatabase::getObjectFilePath MTP_RESPOSNE_INVALID_OBJECT_HANDLE %i\n", handle);
  return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
}

// MtpResponseCode IMtpDatabase::deleteFile(MtpObjectHandle handle) {
//	MTPD("IMtpDatabase::deleteFile\n");
//	std::map<int, MtpStorage*>::iterator storit;
//	for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
//		if (storit->second->deleteFile(handle) == 0) {
//			MTPD("MTP_RESPONSE_OK\n");
//			return MTP_RESPONSE_OK;
//		}
//	}
//	MTPE("IMtpDatabase::deleteFile MTP_RESPONSE_INVALID_OBJECT_HANDLE %i\n", handle);
//	return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
// }

struct PropertyTableEntry {
  MtpObjectProperty property;
  int type;
};

static const PropertyTableEntry kObjectPropertyTable[] = {
  { MTP_PROPERTY_STORAGE_ID, MTP_TYPE_UINT32 },
  { MTP_PROPERTY_OBJECT_FORMAT, MTP_TYPE_UINT16 },
  { MTP_PROPERTY_PROTECTION_STATUS, MTP_TYPE_UINT16 },
  { MTP_PROPERTY_OBJECT_SIZE, MTP_TYPE_UINT64 },
  { MTP_PROPERTY_OBJECT_FILE_NAME, MTP_TYPE_STR },
  { MTP_PROPERTY_DATE_MODIFIED, MTP_TYPE_STR },
  { MTP_PROPERTY_PARENT_OBJECT, MTP_TYPE_UINT32 },
  { MTP_PROPERTY_PERSISTENT_UID, MTP_TYPE_UINT128 },
  { MTP_PROPERTY_NAME, MTP_TYPE_STR },
  { MTP_PROPERTY_DISPLAY_NAME, MTP_TYPE_STR },
  { MTP_PROPERTY_DATE_ADDED, MTP_TYPE_STR },
  { MTP_PROPERTY_ARTIST, MTP_TYPE_STR },
  { MTP_PROPERTY_ALBUM_NAME, MTP_TYPE_STR },
  { MTP_PROPERTY_ALBUM_ARTIST, MTP_TYPE_STR },
  { MTP_PROPERTY_TRACK, MTP_TYPE_UINT16 },
  { MTP_PROPERTY_ORIGINAL_RELEASE_DATE, MTP_TYPE_STR },
  { MTP_PROPERTY_GENRE, MTP_TYPE_STR },
  { MTP_PROPERTY_COMPOSER, MTP_TYPE_STR },
  { MTP_PROPERTY_DURATION, MTP_TYPE_UINT32 },
  { MTP_PROPERTY_DESCRIPTION, MTP_TYPE_STR },
};

static const PropertyTableEntry kDevicePropertyTable[] = {
  { MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER, MTP_TYPE_STR },
  { MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME, MTP_TYPE_STR },
  { MTP_DEVICE_PROPERTY_IMAGE_SIZE, MTP_TYPE_STR },
};

bool IMtpDatabase::getObjectPropertyInfo(MtpObjectProperty property, int& type) {
  int count = sizeof(kObjectPropertyTable) / sizeof(kObjectPropertyTable[0]);
  const PropertyTableEntry* entry = kObjectPropertyTable;
  MTPD("IMtpDatabase::getObjectPropertyInfo size is: %i\n", count);
  for (int i = 0; i < count; i++, entry++) {
	if (entry->property == property) {
	  type = entry->type;
	  return true;
	}
  }
  return false;
}

bool IMtpDatabase::getDevicePropertyInfo(MtpDeviceProperty property, int& type) {
  int count = sizeof(kDevicePropertyTable) / sizeof(kDevicePropertyTable[0]);
  const PropertyTableEntry* entry = kDevicePropertyTable;
  MTPD("IMtpDatabase::getDevicePropertyInfo count is: %i\n", count);
  for (int i = 0; i < count; i++, entry++) {
	if (entry->property == property) {
	  type = entry->type;
	  MTPD("type: %x\n", type);
	  return true;
	}
  }
  return false;
}

MtpObjectHandleList* IMtpDatabase::getObjectReferences(MtpObjectHandle handle) {
  // call function and place files with associated handles into int array
  MTPD(
	  "IMtpDatabase::getObjectReferences returning null, this seems to be what Android always "
	  "does.\n");
  MTPD("handle: %d\n", handle);
  // Windows + Android seems to always return a NULL in this function, c == null path
  // The way that this is handled in Android then is to do this:
  return NULL;
}

MtpResponseCode IMtpDatabase::setObjectReferences(__attribute__((unused)) MtpObjectHandle handle,
												  __attribute__((unused))
												  MtpObjectHandleList* references) {
  MTPE("IMtpDatabase::setObjectReferences not implemented, returning 0\n");
  return 0;
}

MtpProperty* IMtpDatabase::getObjectPropertyDesc(MtpObjectProperty property,
												 MtpObjectFormat format) {
  MTPD("IMtpDatabase::getObjectPropertyDesc start\n");
  MtpProperty* result = NULL;
  switch (property) {
	case MTP_PROPERTY_OBJECT_FORMAT:
	  // use format as default value
	  result = new MtpProperty(property, MTP_TYPE_UINT16, false, format);
	  break;
	case MTP_PROPERTY_PROTECTION_STATUS:
	case MTP_PROPERTY_TRACK:
	  result = new MtpProperty(property, MTP_TYPE_UINT16);
	  break;
	case MTP_PROPERTY_STORAGE_ID:
	case MTP_PROPERTY_PARENT_OBJECT:
	case MTP_PROPERTY_DURATION:
	  result = new MtpProperty(property, MTP_TYPE_UINT32);
	  break;
	case MTP_PROPERTY_OBJECT_SIZE:
	  result = new MtpProperty(property, MTP_TYPE_UINT64);
	  break;
	case MTP_PROPERTY_PERSISTENT_UID:
	  result = new MtpProperty(property, MTP_TYPE_UINT128);
	  break;
	case MTP_PROPERTY_NAME:
	case MTP_PROPERTY_DISPLAY_NAME:
	case MTP_PROPERTY_ARTIST:
	case MTP_PROPERTY_ALBUM_NAME:
	case MTP_PROPERTY_ALBUM_ARTIST:
	case MTP_PROPERTY_GENRE:
	case MTP_PROPERTY_COMPOSER:
	case MTP_PROPERTY_DESCRIPTION:
	  result = new MtpProperty(property, MTP_TYPE_STR);
	  break;
	case MTP_PROPERTY_DATE_MODIFIED:
	case MTP_PROPERTY_DATE_ADDED:
	case MTP_PROPERTY_ORIGINAL_RELEASE_DATE:
	  result = new MtpProperty(property, MTP_TYPE_STR);
	  result->setFormDateTime();
	  break;
	case MTP_PROPERTY_OBJECT_FILE_NAME:
	  // We allow renaming files and folders
	  result = new MtpProperty(property, MTP_TYPE_STR, true);
	  break;
  }
  return result;
}

MtpProperty* IMtpDatabase::getDevicePropertyDesc(MtpDeviceProperty property) {
  MtpProperty* result = NULL;
  bool writable = false;
  switch (property) {
	case MTP_DEVICE_PROPERTY_SYNCHRONIZATION_PARTNER:
	case MTP_DEVICE_PROPERTY_DEVICE_FRIENDLY_NAME:
	  writable = true;
	  // fall through
	case MTP_DEVICE_PROPERTY_IMAGE_SIZE:
	  result = new MtpProperty(property, MTP_TYPE_STR, writable);

	  // get current value
	  // TODO: add actual values
	  result->setCurrentValue(0);
	  result->setDefaultValue(0);
	  break;
  }

  return result;
}

void IMtpDatabase::sessionStarted() {
  MTPD("IMtpDatabase::sessionStarted not implemented or does nothing, returning\n");
  return;
}

void IMtpDatabase::sessionEnded() {
  MTPD("IMtpDatabase::sessionEnded not implemented or does nothing, returning\n");
  return;
}

// ----------------------------------------------------------------------------

void IMtpDatabase::lockMutex(void) {
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
	storit->second->lockMutex(0);
  }
}

void IMtpDatabase::unlockMutex(void) {
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
	storit->second->unlockMutex(0);
  }
}

MtpResponseCode IMtpDatabase::beginDeleteObject(MtpObjectHandle handle) {
  MTPD("IMtoDatabase::beginDeleteObject handle: %u\n", handle);
  std::map<int, MtpStorage*>::iterator storit;
  for (storit = storagemap.begin(); storit != storagemap.end(); storit++) {
	if (storit->second->deleteFile(handle) == 0) {
	  MTPD("IMtpDatabase::beginDeleteObject::MTP_RESPONSE_OK\n");
	  return MTP_RESPONSE_OK;
	}
  }
  return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
}

void IMtpDatabase::endDeleteObject(MtpObjectHandle handle __unused, bool succeeded __unused) {
  MTPD("IMtpDatabase::endDeleteObject not implemented yet\n");
}

void IMtpDatabase::rescanFile(const char* path __unused, MtpObjectHandle handle __unused,
							  MtpObjectFormat format __unused) {
  MTPD("IMtpDatabase::rescanFile not implemented yet\n");
}

MtpResponseCode IMtpDatabase::beginMoveObject(MtpObjectHandle handle __unused,
											  MtpObjectHandle newParent __unused,
											  MtpStorageID newStorage __unused) {
  MTPD("IMtpDatabase::beginMoveObject not implemented yet\n");
  return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
}

void IMtpDatabase::endMoveObject(MtpObjectHandle oldParent __unused,
								 MtpObjectHandle newParent __unused,
								 MtpStorageID oldStorage __unused, MtpStorageID newStorage __unused,
								 MtpObjectHandle handle __unused, bool succeeded __unused) {
  MTPD("IMtpDatabase::endMoveObject not implemented yet\n");
}

MtpResponseCode IMtpDatabase::beginCopyObject(MtpObjectHandle handle __unused,
											  MtpObjectHandle newParent __unused,
											  MtpStorageID newStorage __unused) {
  MTPD("IMtpDatabase::beginCopyObject not implemented yet\n");
  return MTP_RESPONSE_INVALID_OBJECT_HANDLE;
}

void IMtpDatabase::endCopyObject(MtpObjectHandle handle __unused, bool succeeded __unused) {
  MTPD("IMtpDatabase::endCopyObject not implemented yet\n");
}