OSDN Git Service

emulator: Add hook only if there aren't other with same type and opcode
authorEder Ruiz Maria <eder.ruiz@openbossa.org>
Sun, 21 Jul 2013 00:17:14 +0000 (20:17 -0400)
committerJohan Hedberg <johan.hedberg@intel.com>
Wed, 24 Jul 2013 16:03:02 +0000 (09:03 -0700)
emulator/btdev.c

index 52b58ce..30022d1 100644 (file)
@@ -115,6 +115,23 @@ struct btdev {
 
 static struct btdev *btdev_list[MAX_BTDEV_ENTRIES] = { };
 
+static int get_hook_index(struct btdev *btdev, enum btdev_hook_type type,
+                                                               uint16_t opcode)
+{
+       int i;
+
+       for (i = 0; i < MAX_HOOK_ENTRIES; i++) {
+               if (btdev->hook_list[i] == NULL)
+                       continue;
+
+               if (btdev->hook_list[i]->type == type &&
+                                       btdev->hook_list[i]->opcode == opcode)
+                       return i;
+       }
+
+       return -1;
+}
+
 static inline int add_btdev(struct btdev *btdev)
 {
        int i, index = -1;
@@ -1710,6 +1727,9 @@ int btdev_add_hook(struct btdev *btdev, enum btdev_hook_type type,
        if (!btdev)
                return -1;
 
+       if (get_hook_index(btdev, type, opcode) > 0)
+               return -1;
+
        for (i = 0; i < MAX_HOOK_ENTRIES; i++) {
                if (btdev->hook_list[i] == NULL) {
                        btdev->hook_list[i] = malloc(sizeof(struct hook));