Skip to content

Commit

Permalink
Add sai_decimal_p2_t for decimal with presion 2
Browse files Browse the repository at this point in the history
Signed-off-by: Jimmy Jin <[email protected]>
  • Loading branch information
oplinksdn committed Nov 14, 2023
1 parent b4331a0 commit 911f289
Show file tree
Hide file tree
Showing 9 changed files with 80 additions and 1 deletion.
4 changes: 4 additions & 0 deletions inc/saitypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ typedef size_t sai_size_t;
typedef uint64_t sai_object_id_t;
typedef void *sai_pointer_t;
typedef uint64_t sai_api_version_t;
typedef double sai_decimal_p2_t;

typedef struct _sai_timespec_t
{
Expand Down Expand Up @@ -1519,6 +1520,9 @@ typedef union _sai_attribute_value_t

/** @validonly meta->attrvaluetype == SAI_ATTR_VALUE_TYPE_PORT_SNR_LIST */
sai_port_snr_list_t portsnrlist;

/** @validonly meta->attrvaluetype == SAI_ATTR_VALUE_TYPE_DECIMAL_P2 */
sai_decimal_p2_t decimalp2;
} sai_attribute_value_t;

/**
Expand Down
1 change: 0 additions & 1 deletion meta/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ WARNINGS = \
-Werror \
-Wextra \
-Wextra \
-Wfloat-equal \
-Wformat=2 \
-Wformat-nonliteral \
-Wformat-security \
Expand Down
16 changes: 16 additions & 0 deletions meta/parse.pl
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@
my $FLAGS = "MANDATORY_ON_CREATE|CREATE_ONLY|CREATE_AND_SET|READ_ONLY|KEY";
my $ENUM_FLAGS_TYPES = "(none|strict|mixed|ranges|free)";

my $DECIMAL_P2_REGEX = '([+-]?([0-9]*[.])?[0-9]{0,2})';

# TAGS HANDLERS

my %ATTR_TAGS = (
Expand Down Expand Up @@ -365,6 +367,8 @@ sub ProcessTagDefault

return $val if $val =~ /^ffff\:ffff\:ffff\:ffff\:ffff\:ffff\:ffff\:ffff$/;

return $val if $val =~ /^$DECIMAL_P2_REGEX$/;

LogError "invalid default tag value '$val' on $type $value";
return undef;
}
Expand Down Expand Up @@ -1492,6 +1496,11 @@ sub ProcessType
return "SAI_ATTR_VALUE_TYPE_INT32_LIST";
}

if ($type eq "sai_decimal_p2_t")
{
return "SAI_ATTR_VALUE_TYPE_DECIMAL_P2";
}

if ($type =~ /^(sai_\w+_t)$/)
{
my $prefix = "SAI_ATTR_VALUE_TYPE";
Expand Down Expand Up @@ -1647,6 +1656,8 @@ sub ProcessDefaultValueType

return "SAI_DEFAULT_VALUE_TYPE_CONST" if $default =~ /^ffff\:ffff\:ffff\:ffff\:ffff\:ffff\:ffff\:ffff$/;

return "SAI_DEFAULT_VALUE_TYPE_CONST" if $default =~ /^$DECIMAL_P2_REGEX$/;

LogError "invalid default value type '$default' on $attr";

return "";
Expand Down Expand Up @@ -1722,6 +1733,10 @@ sub ProcessDefaultValue
{
WriteSource "$val = { .mac = { 0, 0, 0, 0, 0, 0 } };";
}
elsif ($default =~ /^$DECIMAL_P2_REGEX$/ and $type =~ /^sai_decimal_p2_t/)
{
WriteSource "$val = { .$VALUE_TYPES{$type} = $default };";
}
else
{
LogError "invalid default value '$default' on $attr ($type)";
Expand Down Expand Up @@ -4662,6 +4677,7 @@ sub ProcessStructItem
return if $type =~ /^sai_(u?int\d+|ip[46]|mac|cos|vlan_id|queue_index)_t/; # primitives, we could get that from defines
return if $type =~ /^u?int\d+_t/;
return if $type =~ /^sai_[su]\d+_list_t/;
return if $type =~ /^sai_decimal_p2_t/;

if ($type eq "sai_object_id_t" or $type eq "sai_object_list_t")
{
Expand Down
5 changes: 5 additions & 0 deletions meta/saimetadatatypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,11 @@ typedef enum _sai_attr_value_type_t
*/
SAI_ATTR_VALUE_TYPE_INT64,

/**
* @brief Attribute value is decimal p2.
*/
SAI_ATTR_VALUE_TYPE_DECIMAL_P2,

/**
* @brief Attribute value is pointer address.
*/
Expand Down
4 changes: 4 additions & 0 deletions meta/saimetadatautils.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <sai.h>
#include "saimetadatautils.h"
#include "saimetadata.h"
Expand Down Expand Up @@ -376,6 +377,9 @@ static bool sai_metadata_is_condition_value_eq(
case SAI_ATTR_VALUE_TYPE_UINT64:
return cvalue->u64 == value->u64;

case SAI_ATTR_VALUE_TYPE_DECIMAL_P2:
return fabs(cvalue->decimalp2 - value->decimalp2) < 1E-2;

default:

/*
Expand Down
6 changes: 6 additions & 0 deletions meta/saisanitycheck.c
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,7 @@ void check_attr_object_type_provided(
case SAI_ATTR_VALUE_TYPE_VLAN_LIST:
case SAI_ATTR_VALUE_TYPE_UINT32:
case SAI_ATTR_VALUE_TYPE_UINT64:
case SAI_ATTR_VALUE_TYPE_DECIMAL_P2:
case SAI_ATTR_VALUE_TYPE_MAC:
case SAI_ATTR_VALUE_TYPE_POINTER:
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
Expand Down Expand Up @@ -946,6 +947,7 @@ void check_attr_default_required(
case SAI_ATTR_VALUE_TYPE_UINT16:
case SAI_ATTR_VALUE_TYPE_UINT32:
case SAI_ATTR_VALUE_TYPE_UINT64:
case SAI_ATTR_VALUE_TYPE_DECIMAL_P2:
case SAI_ATTR_VALUE_TYPE_MAC:
case SAI_ATTR_VALUE_TYPE_IP_ADDRESS:
case SAI_ATTR_VALUE_TYPE_IP_PREFIX:
Expand Down Expand Up @@ -1415,6 +1417,7 @@ void check_attr_conditions(
case SAI_ATTR_VALUE_TYPE_UINT16:
case SAI_ATTR_VALUE_TYPE_UINT32:
case SAI_ATTR_VALUE_TYPE_UINT64:
case SAI_ATTR_VALUE_TYPE_DECIMAL_P2:

/* number conditions */

Expand Down Expand Up @@ -1622,6 +1625,7 @@ void check_attr_validonly(
case SAI_ATTR_VALUE_TYPE_UINT16:
case SAI_ATTR_VALUE_TYPE_UINT32:
case SAI_ATTR_VALUE_TYPE_UINT64:
case SAI_ATTR_VALUE_TYPE_DECIMAL_P2:
break;

default:
Expand Down Expand Up @@ -2517,6 +2521,7 @@ void check_attr_existing_objects(
case SAI_ATTR_VALUE_TYPE_UINT32:
case SAI_ATTR_VALUE_TYPE_UINT64:
case SAI_ATTR_VALUE_TYPE_UINT8:
case SAI_ATTR_VALUE_TYPE_DECIMAL_P2:

/*
* Primitives we can skip for now, just left as was set by user
Expand Down Expand Up @@ -2774,6 +2779,7 @@ void check_attr_is_primitive(
case SAI_ATTR_VALUE_TYPE_SYSTEM_PORT_CONFIG:
case SAI_ATTR_VALUE_TYPE_FABRIC_PORT_REACHABILITY:
case SAI_ATTR_VALUE_TYPE_LATCH_STATUS:
case SAI_ATTR_VALUE_TYPE_DECIMAL_P2:

if (!md->isprimitive)
{
Expand Down
14 changes: 14 additions & 0 deletions meta/saiserialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -1355,3 +1355,17 @@ int sai_deserialize_attribute(

return (int)(buf - buffer);
}

int sai_serialize_decimal_p2(
_Out_ char *buffer,
_In_ sai_decimal_p2_t decimal)
{
return sprintf(buffer, "%.2lf", decimal);
}

int sai_deserialize_decimal_p2(
_In_ const char *buffer,
_Out_ sai_decimal_p2_t *decimal)
{
return sscanf(buffer, "%lf", decimal);
}
26 changes: 26 additions & 0 deletions meta/saiserialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -877,6 +877,32 @@ void sai_free_attribute(
_In_ const sai_attr_metadata_t *meta,
_In_ const sai_attribute_t *attribute);

/**
* @brief Serialize decimal p2.
*
* @param[out] buffer Output buffer for serialized value.
* @param[in] decimal Decimal_p2 to be serialized.
*
* @return Number of characters written to buffer excluding '\0',
* or #SAI_SERIALIZE_ERROR on error.
*/
int sai_serialize_decimal_p2(
_Out_ char *buffer,
_In_ sai_decimal_p2_t decimal);

/**
* @brief Deserialize decimal p2.
*
* @param[in] buffer Input buffer to be examined.
* @param[out] decimal Deserialized value.
*
* @return Number of characters consumed from the buffer,
* or #SAI_SERIALIZE_ERROR on error.
*/
int sai_deserialize_decimal_p2(
_In_ const char *buffer,
_Out_ sai_decimal_p2_t *decimal);

/**
* @}
*/
Expand Down
5 changes: 5 additions & 0 deletions meta/serialize.pm
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,11 @@ sub GetTypeInfoForSerialize
$TypeInfo{needQuote} = 1;
$TypeInfo{deamp} = "&";
}
elsif ($type =~ m/^sai_decimal_p2_t$/)
{
$TypeInfo{suffix} = "decimal_p2";
$TypeInfo{deamp} = "&";
}
elsif ($type =~ /^sai_pointer_t$/)
{
# need quote since "ptr:" is added on serialize
Expand Down

0 comments on commit 911f289

Please sign in to comment.