Browse Source

lowlevel: Add match variants of detect functions

Luke Dashjr 12 years ago
parent
commit
2ef593a6d3
2 changed files with 30 additions and 18 deletions
  1. 24 18
      lowlevel.c
  2. 6 0
      lowlevel.h

+ 24 - 18
lowlevel.c

@@ -103,6 +103,27 @@ struct lowlevel_device_info *lowlevel_scan()
 	return devinfo_list;
 	return devinfo_list;
 }
 }
 
 
+bool _lowlevel_match_product(const struct lowlevel_device_info * const info, const char ** const needles)
+{
+	if (!info->product)
+		return false;
+	for (int i = 0; needles[i]; ++i)
+		if (!strstr(info->product, needles[i]))
+			return false;
+	return true;
+}
+
+bool lowlevel_match_id(const struct lowlevel_device_info * const info, const struct lowlevel_driver * const lowl, const int32_t vid, const int32_t pid)
+{
+	if (info->lowl != lowl)
+		return false;
+	if (vid != -1 && vid != info->vid)
+		return false;
+	if (pid != -1 && pid != info->pid)
+		return false;
+	return true;
+}
+
 #define DETECT_BEGIN  \
 #define DETECT_BEGIN  \
 	struct lowlevel_device_info *info, *tmp;  \
 	struct lowlevel_device_info *info, *tmp;  \
 	int found = 0;  \
 	int found = 0;  \
@@ -111,44 +132,29 @@ struct lowlevel_device_info *lowlevel_scan()
 	{  \
 	{  \
 // END DETECT_BEGIN
 // END DETECT_BEGIN
 
 
-#define DETECT_PREEND  \
+#define DETECT_END  \
 		if (!cb(info, userp))  \
 		if (!cb(info, userp))  \
 			continue;  \
 			continue;  \
 		LL_DELETE(devinfo_list, info);  \
 		LL_DELETE(devinfo_list, info);  \
 		++found;  \
 		++found;  \
-// END DETECT_PREEND
-
-#define DETECT_END  \
 	}  \
 	}  \
 	return found;  \
 	return found;  \
 // END DETECT_END
 // END DETECT_END
 
 
 int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles, void * const userp)
 int _lowlevel_detect(lowl_found_devinfo_func_t cb, const char *serial, const char **product_needles, void * const userp)
 {
 {
-	int i;
-	
 	DETECT_BEGIN
 	DETECT_BEGIN
 		if (serial && ((!info->serial) || strcmp(serial, info->serial)))
 		if (serial && ((!info->serial) || strcmp(serial, info->serial)))
 			continue;
 			continue;
-		if (product_needles[0] && !info->product)
+		if (product_needles[0] && !_lowlevel_match_product(info, product_needles))
 			continue;
 			continue;
-		for (i = 0; product_needles[i]; ++i)
-			if (!strstr(info->product, product_needles[i]))
-				goto next;
-	DETECT_PREEND
-next: ;
 	DETECT_END
 	DETECT_END
 }
 }
 
 
 int lowlevel_detect_id(const lowl_found_devinfo_func_t cb, void * const userp, const struct lowlevel_driver * const lowl, const int32_t vid, const int32_t pid)
 int lowlevel_detect_id(const lowl_found_devinfo_func_t cb, void * const userp, const struct lowlevel_driver * const lowl, const int32_t vid, const int32_t pid)
 {
 {
 	DETECT_BEGIN
 	DETECT_BEGIN
-		if (info->lowl != lowl)
-			continue;
-		if (vid != -1 && vid != info->vid)
-			continue;
-		if (pid != -1 && pid != info->pid)
+		if (!lowlevel_match_id(info, lowl, vid, pid))
 			continue;
 			continue;
-	DETECT_PREEND
 	DETECT_END
 	DETECT_END
 }
 }

+ 6 - 0
lowlevel.h

@@ -35,6 +35,12 @@ struct lowlevel_device_info {
 };
 };
 
 
 extern struct lowlevel_device_info *lowlevel_scan();
 extern struct lowlevel_device_info *lowlevel_scan();
+extern bool _lowlevel_match_product(const struct lowlevel_device_info *, const char **);
+#define lowlevel_match_product(info, ...)  \
+	_lowlevel_match_product(info, (const char *[]){__VA_ARGS__, NULL})
+#define lowlevel_match_lowlproduct(info, matchlowl, ...)  \
+	(matchlowl == info->lowl && _lowlevel_match_product(info, (const char *[]){__VA_ARGS__, NULL}))
+extern bool lowlevel_match_id(const struct lowlevel_device_info *, const struct lowlevel_driver *, int32_t vid, int32_t pid);
 extern int _lowlevel_detect(lowl_found_devinfo_func_t, const char *serial, const char **product_needles, void *);
 extern int _lowlevel_detect(lowl_found_devinfo_func_t, const char *serial, const char **product_needles, void *);
 #define lowlevel_detect(func, ...)  _lowlevel_detect(func, NULL, (const char *[]){__VA_ARGS__, NULL}, NULL)
 #define lowlevel_detect(func, ...)  _lowlevel_detect(func, NULL, (const char *[]){__VA_ARGS__, NULL}, NULL)
 #define lowlevel_detect_serial(func, serial)  _lowlevel_detect(func, serial, (const char *[]){NULL}, NULL)
 #define lowlevel_detect_serial(func, serial)  _lowlevel_detect(func, serial, (const char *[]){NULL}, NULL)