Просмотр исходного кода

qcacmn: Add qdf api which converts string to array

Added api which parses given input string to 8-bit unsigned array.

Change-Id: I2d6800f550b37970b9a0d48508a49e6354a94f9a
CRs-Fixed: 2306890
Karthik Kantamneni 6 лет назад
Родитель
Сommit
4113fb7d13
2 измененных файлов с 97 добавлено и 14 удалено
  1. 15 0
      qdf/inc/qdf_types.h
  2. 82 14
      qdf/src/qdf_types.c

+ 15 - 0
qdf/inc/qdf_types.h

@@ -887,6 +887,21 @@ struct qdf_ipv6_addr {
  */
 QDF_STATUS qdf_ipv6_parse(const char *ipv6_str, struct qdf_ipv6_addr *out_addr);
 
+/**
+ * qdf_uint8_array_parse() - parse the given string as uint8 array
+ * @in_str: the input string to parse
+ * @out_array: the output uint8 array, populated on success
+ * @array_size: size of the array
+ * @out_size: size of the populated array
+ *
+ * This API is called to convert string (each byte separated by
+ * a comma) into an u8 array
+ *
+ * Return: QDF_STATUS
+ */
+QDF_STATUS qdf_uint8_array_parse(const char *in_str, uint8_t *out_array,
+				 qdf_size_t array_size, qdf_size_t *out_size);
+
 #define QDF_MAX_NUM_CHAN   (128)
 
 #define QDF_BCAST_MAC_ADDR (0xFF)

+ 82 - 14
qdf/src/qdf_types.c

@@ -177,7 +177,7 @@ static QDF_STATUS qdf_consume_radix(const char **str, uint8_t *out_radix)
 }
 
 static QDF_STATUS
-qdf_int_parse(const char *int_str, uint64_t *out_int, bool *out_negate)
+__qdf_int_parse_lazy(const char **int_str, uint64_t *out_int, bool *out_negate)
 {
 	QDF_STATUS status;
 	bool negate = false;
@@ -185,28 +185,21 @@ qdf_int_parse(const char *int_str, uint64_t *out_int, bool *out_negate)
 	uint8_t digit;
 	uint64_t value = 0;
 	uint64_t next_value;
+	const char *str = *int_str;
 
-	QDF_BUG(int_str);
-	if (!int_str)
-		return QDF_STATUS_E_INVAL;
-
-	QDF_BUG(out_int);
-	if (!out_int)
-		return QDF_STATUS_E_INVAL;
-
-	int_str = qdf_str_left_trim(int_str);
+	str = qdf_str_left_trim(str);
 
-	status = qdf_consume_char(&int_str, '-');
+	status = qdf_consume_char(&str, '-');
 	if (QDF_IS_STATUS_SUCCESS(status))
 		negate = true;
 	else
-		qdf_consume_char(&int_str, '+');
+		qdf_consume_char(&str, '+');
 
-	status = qdf_consume_radix(&int_str, &radix);
+	status = qdf_consume_radix(&str, &radix);
 	if (QDF_IS_STATUS_ERROR(status))
 		return status;
 
-	while (QDF_IS_STATUS_SUCCESS(qdf_consume_hex(&int_str, &digit))) {
+	while (QDF_IS_STATUS_SUCCESS(qdf_consume_hex(&str, &digit))) {
 		if (digit >= radix)
 			return QDF_STATUS_E_FAILURE;
 
@@ -217,6 +210,32 @@ qdf_int_parse(const char *int_str, uint64_t *out_int, bool *out_negate)
 		value = next_value;
 	}
 
+	*int_str = str;
+	*out_negate = negate;
+	*out_int = value;
+
+	return QDF_STATUS_SUCCESS;
+}
+
+static QDF_STATUS
+qdf_int_parse(const char *int_str, uint64_t *out_int, bool *out_negate)
+{
+	QDF_STATUS status;
+	bool negate;
+	uint64_t value;
+
+	QDF_BUG(int_str);
+	if (!int_str)
+		return QDF_STATUS_E_INVAL;
+
+	QDF_BUG(out_int);
+	if (!out_int)
+		return QDF_STATUS_E_INVAL;
+
+	status = __qdf_int_parse_lazy(&int_str, &value, &negate);
+	if (QDF_IS_STATUS_ERROR(status))
+		return status;
+
 	int_str = qdf_str_left_trim(int_str);
 	if (int_str[0] != '\0')
 		return QDF_STATUS_E_FAILURE;
@@ -524,3 +543,52 @@ QDF_STATUS qdf_ipv6_parse(const char *ipv6_str, struct qdf_ipv6_addr *out_addr)
 	return QDF_STATUS_SUCCESS;
 }
 qdf_export_symbol(qdf_ipv6_parse);
+
+QDF_STATUS qdf_uint8_array_parse(const char *in_str, uint8_t *out_array,
+				 qdf_size_t array_size, qdf_size_t *out_size)
+{
+	QDF_STATUS status;
+	bool negate;
+	qdf_size_t size = 0;
+	uint64_t value;
+
+	QDF_BUG(in_str);
+	if (!in_str)
+		return QDF_STATUS_E_INVAL;
+
+	QDF_BUG(out_array);
+	if (!out_array)
+		return QDF_STATUS_E_INVAL;
+
+	QDF_BUG(out_size);
+	if (!out_size)
+		return QDF_STATUS_E_INVAL;
+
+	while (size < array_size) {
+		status = __qdf_int_parse_lazy(&in_str, &value, &negate);
+		if (QDF_IS_STATUS_ERROR(status))
+			return status;
+
+		if ((uint8_t)value != value || negate)
+			return QDF_STATUS_E_RANGE;
+
+		in_str = qdf_str_left_trim(in_str);
+
+		switch (in_str[0]) {
+		case ',':
+			out_array[size++] = value;
+			in_str++;
+			break;
+		case '\0':
+			out_array[size++] = value;
+			*out_size = size;
+			return QDF_STATUS_SUCCESS;
+		default:
+			return QDF_STATUS_E_FAILURE;
+		}
+	}
+
+	return QDF_STATUS_E_FAILURE;
+}
+
+qdf_export_symbol(qdf_uint8_array_parse);