4040#include < androidfw/KeyCharacterMap.h>
4141#include < androidfw/VirtualKeyMap.h>
4242
43+ #include < sha1.h>
4344#include < string.h>
4445#include < stdint.h>
4546#include < dirent.h>
@@ -78,6 +79,20 @@ static inline const char* toString(bool value) {
7879 return value ? " true" : " false" ;
7980}
8081
82+ static String8 sha1 (const String8& in) {
83+ SHA1_CTX ctx;
84+ SHA1Init (&ctx);
85+ SHA1Update (&ctx, reinterpret_cast <const u_char*>(in.string ()), in.size ());
86+ u_char digest[SHA1_DIGEST_LENGTH];
87+ SHA1Final (digest, &ctx);
88+
89+ String8 out;
90+ for (size_t i = 0 ; i < SHA1_DIGEST_LENGTH; i++) {
91+ out.appendFormat (" %02x" , digest[i]);
92+ }
93+ return out;
94+ }
95+
8196// --- Global Functions ---
8297
8398uint32_t getAbsAxisUsage (int32_t axis, uint32_t deviceClasses) {
@@ -209,11 +224,11 @@ EventHub::~EventHub(void) {
209224 release_wake_lock (WAKE_LOCK_ID);
210225}
211226
212- String8 EventHub::getDeviceName (int32_t deviceId) const {
227+ InputDeviceIdentifier EventHub::getDeviceIdentifier (int32_t deviceId) const {
213228 AutoMutex _l (mLock );
214229 Device* device = getDeviceLocked (deviceId);
215- if (device == NULL ) return String8 ();
216- return device->identifier . name ;
230+ if (device == NULL ) return InputDeviceIdentifier ();
231+ return device->identifier ;
217232}
218233
219234uint32_t EventHub::getDeviceClasses (int32_t deviceId) const {
@@ -893,6 +908,30 @@ status_t EventHub::openDeviceLocked(const char *devicePath) {
893908 identifier.uniqueId .setTo (buffer);
894909 }
895910
911+ // Compute a device descriptor that uniquely identifies the device.
912+ // The descriptor is assumed to be a stable identifier. Its value should not
913+ // change between reboots, reconnections, firmware updates or new releases of Android.
914+ // Ideally, we also want the descriptor to be short and relatively opaque.
915+ String8 rawDescriptor;
916+ rawDescriptor.appendFormat (" :%04x:%04x:" , identifier.vendor , identifier.product );
917+ if (!identifier.uniqueId .isEmpty ()) {
918+ rawDescriptor.append (" uniqueId:" );
919+ rawDescriptor.append (identifier.uniqueId );
920+ } if (identifier.vendor == 0 && identifier.product == 0 ) {
921+ // If we don't know the vendor and product id, then the device is probably
922+ // built-in so we need to rely on other information to uniquely identify
923+ // the input device. Usually we try to avoid relying on the device name or
924+ // location but for built-in input device, they are unlikely to ever change.
925+ if (!identifier.name .isEmpty ()) {
926+ rawDescriptor.append (" name:" );
927+ rawDescriptor.append (identifier.name );
928+ } else if (!identifier.location .isEmpty ()) {
929+ rawDescriptor.append (" location:" );
930+ rawDescriptor.append (identifier.location );
931+ }
932+ }
933+ identifier.descriptor = sha1 (rawDescriptor);
934+
896935 // Make file descriptor non-blocking for use with poll().
897936 if (fcntl (fd, F_SETFL, O_NONBLOCK)) {
898937 ALOGE (" Error %d making device file descriptor non-blocking." , errno);
@@ -904,19 +943,18 @@ status_t EventHub::openDeviceLocked(const char *devicePath) {
904943 int32_t deviceId = mNextDeviceId ++;
905944 Device* device = new Device (fd, deviceId, String8 (devicePath), identifier);
906945
907- #if 0
908- ALOGI("add device %d: %s\n", deviceId, devicePath);
909- ALOGI(" bus: %04x\n"
910- " vendor %04x\n"
911- " product %04x\n"
912- " version %04x\n",
946+ ALOGV (" add device %d: %s\n " , deviceId, devicePath);
947+ ALOGV (" bus: %04x\n "
948+ " vendor %04x\n "
949+ " product %04x\n "
950+ " version %04x\n " ,
913951 identifier.bus , identifier.vendor , identifier.product , identifier.version );
914- ALOGI(" name: \"%s\"\n", identifier.name.string());
915- ALOGI(" location: \"%s\"\n", identifier.location.string());
916- ALOGI(" unique id: \"%s\"\n", identifier.uniqueId.string());
917- ALOGI(" driver: v%d.%d.%d\n",
952+ ALOGV (" name: \" %s\"\n " , identifier.name .string ());
953+ ALOGV (" location: \" %s\"\n " , identifier.location .string ());
954+ ALOGV (" unique id: \" %s\"\n " , identifier.uniqueId .string ());
955+ ALOGV (" descriptor: \" %s\" (%s)\n " , identifier.descriptor .string (), rawDescriptor.string ());
956+ ALOGV (" driver: v%d.%d.%d\n " ,
918957 driverVersion >> 16 , (driverVersion >> 8 ) & 0xff , driverVersion & 0xff );
919- #endif
920958
921959 // Load the configuration file for the device.
922960 loadConfigurationLocked (device);
@@ -1303,6 +1341,7 @@ void EventHub::dump(String8& dump) {
13031341 }
13041342 dump.appendFormat (INDENT3 " Classes: 0x%08x\n " , device->classes );
13051343 dump.appendFormat (INDENT3 " Path: %s\n " , device->path .string ());
1344+ dump.appendFormat (INDENT3 " Descriptor: %s\n " , device->identifier .descriptor .string ());
13061345 dump.appendFormat (INDENT3 " Location: %s\n " , device->identifier .location .string ());
13071346 dump.appendFormat (INDENT3 " UniqueId: %s\n " , device->identifier .uniqueId .string ());
13081347 dump.appendFormat (INDENT3 " Identifier: bus=0x%04x, vendor=0x%04x, "
0 commit comments