[e2b9a993] | 1 | /*
|
---|
| 2 | * Copyright (c) 2010 Lenka Trochtova
|
---|
| 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup devman
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
[38b3baf] | 32 |
|
---|
[c47e1a8] | 33 | #include <str.h>
|
---|
[e2b9a993] | 34 |
|
---|
| 35 | #include "devman.h"
|
---|
| 36 |
|
---|
[cc70d57] | 37 | /** Compute compound score of driver and device.
|
---|
| 38 | *
|
---|
| 39 | * @param driver Match id of the driver.
|
---|
| 40 | * @param device Match id of the device.
|
---|
| 41 | * @return Compound score.
|
---|
| 42 | * @retval 0 No match at all.
|
---|
| 43 | */
|
---|
| 44 | static int compute_match_score(match_id_t *driver, match_id_t *device)
|
---|
| 45 | {
|
---|
| 46 | if (str_cmp(driver->id, device->id) == 0) {
|
---|
| 47 | /*
|
---|
[4006447] | 48 | * The strings match, return the product of their scores.
|
---|
[cc70d57] | 49 | */
|
---|
| 50 | return driver->score * device->score;
|
---|
| 51 | } else {
|
---|
| 52 | /*
|
---|
| 53 | * Different strings, return zero.
|
---|
| 54 | */
|
---|
| 55 | return 0;
|
---|
| 56 | }
|
---|
| 57 | }
|
---|
| 58 |
|
---|
[ba38f72c] | 59 | int get_match_score(driver_t *drv, dev_node_t *dev)
|
---|
[38b3baf] | 60 | {
|
---|
[b72efe8] | 61 | link_t *drv_head = &drv->match_ids.ids.head;
|
---|
| 62 | link_t *dev_head = &dev->pfun->match_ids.ids.head;
|
---|
[e2b9a993] | 63 |
|
---|
[b72efe8] | 64 | if (list_empty(&drv->match_ids.ids) ||
|
---|
| 65 | list_empty(&dev->pfun->match_ids.ids)) {
|
---|
[e2b9a993] | 66 | return 0;
|
---|
[b72efe8] | 67 | }
|
---|
[e2b9a993] | 68 |
|
---|
[cc70d57] | 69 | /*
|
---|
[4006447] | 70 | * Go through all pairs, return the highest score obtained.
|
---|
[cc70d57] | 71 | */
|
---|
| 72 | int highest_score = 0;
|
---|
[38b3baf] | 73 |
|
---|
[b72efe8] | 74 | link_t *drv_link = drv->match_ids.ids.head.next;
|
---|
[cc70d57] | 75 | while (drv_link != drv_head) {
|
---|
| 76 | link_t *dev_link = dev_head->next;
|
---|
| 77 | while (dev_link != dev_head) {
|
---|
| 78 | match_id_t *drv_id = list_get_instance(drv_link, match_id_t, link);
|
---|
| 79 | match_id_t *dev_id = list_get_instance(dev_link, match_id_t, link);
|
---|
| 80 |
|
---|
| 81 | int score = compute_match_score(drv_id, dev_id);
|
---|
| 82 | if (score > highest_score) {
|
---|
| 83 | highest_score = score;
|
---|
| 84 | }
|
---|
| 85 |
|
---|
[e2b9a993] | 86 | dev_link = dev_link->next;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[cc70d57] | 89 | drv_link = drv_link->next;
|
---|
| 90 | }
|
---|
[e2b9a993] | 91 |
|
---|
[cc70d57] | 92 | return highest_score;
|
---|
[e2b9a993] | 93 | }
|
---|
| 94 |
|
---|
[38b3baf] | 95 | /** @}
|
---|
| 96 | */
|
---|