featuremgmt/featuremgr/tools/datfilehelpers/FeatureFlag.pm
changeset 0 08ec8eefde2f
equal deleted inserted replaced
-1:000000000000 0:08ec8eefde2f
       
     1 #!perl -w
       
     2 # Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     3 # All rights reserved.
       
     4 # This component and the accompanying materials are made available
       
     5 # under the terms of "Eclipse Public License v1.0"
       
     6 # which accompanies this distribution, and is available
       
     7 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 #
       
     9 # Initial Contributors:
       
    10 # Nokia Corporation - initial contribution.
       
    11 #
       
    12 # Contributors:
       
    13 #
       
    14 # Description:
       
    15 #
       
    16 
       
    17 use strict;
       
    18 
       
    19 #
       
    20 # A simple class to manage feature flags for a feature set data file.
       
    21 #
       
    22 package FeatureFlag;
       
    23 
       
    24 # Create a feature flag object.
       
    25 sub new
       
    26 {
       
    27 	my $arg = shift;
       
    28 	my $class = ref($arg) || $arg;
       
    29 	my($uid, $sf, $ud) = @_;
       
    30 	die "You must specify a UID when creating a FeatureFlag object"
       
    31 	    unless(defined($uid));
       
    32 	$sf = 0 unless(defined($sf));
       
    33 	$ud = 0 unless(defined($ud));
       
    34 
       
    35 	my $self = {
       
    36 			     uid => $uid,
       
    37 			     statusflags => $sf,
       
    38 			     userdata => $ud,
       
    39 				 endian => "LE",
       
    40 			   };
       
    41 	bless $self, $class;
       
    42 	return $self;
       
    43 }
       
    44 
       
    45 sub Endian
       
    46 {
       
    47 	my $self = shift;
       
    48 	return undef unless(ref($self));
       
    49 	my $arg = shift;
       
    50 	return $self->{endian} unless(defined($arg) and $arg =~ m/(^BE$|^LE$)/i);
       
    51 	$self->{endian} = lc($1);
       
    52 	return $self->{endian};
       
    53 }
       
    54 
       
    55 # Return a twelve byte string 'feature flag' information.
       
    56 sub BinaryContent
       
    57 {
       
    58 	my $self = shift;
       
    59 	return undef unless(ref($self));
       
    60 	my @arr = ( $self->UID(), $self->StatusFlags(), $self->UserData() );
       
    61 
       
    62 	# Decide whether we want big or little endian output.
       
    63 	# According to the documentation, 'V', 'N' are GUARANTEED to be 32-bit.
       
    64 	my $packstring = "V3"; # Little endian.
       
    65 	   $packstring = "N3" if($self->Endian() eq "BE");
       
    66 
       
    67 	my $string = pack $packstring, @arr;
       
    68 	return $string;
       
    69 }
       
    70 
       
    71 # A single 32-bit number.
       
    72 sub UID
       
    73 {
       
    74 	my $self = shift;
       
    75 	return undef unless(ref($self));
       
    76 	my $uid = shift;
       
    77 	return $self->{uid} unless(defined($uid));
       
    78 	$uid = int($uid);
       
    79 	$self->{uid} = $uid;
       
    80 	return $uid;
       
    81 }
       
    82 
       
    83 # A single 32-bit number.
       
    84 sub StatusFlags
       
    85 {
       
    86 	my $self = shift;
       
    87 	return undef unless(ref($self));
       
    88 	my $sf = shift;
       
    89 	return $self->{statusflags} unless(defined($sf));
       
    90 	$sf = int($sf);
       
    91 	$self->{statusflags} = $sf;
       
    92 	return $sf;
       
    93 }
       
    94 
       
    95 # A single 32-bit number.
       
    96 sub UserData
       
    97 {
       
    98 	my $self = shift;
       
    99 	return undef unless(ref($self));
       
   100 	my $ud = shift;
       
   101 	return $self->{userdata} unless(defined($ud));
       
   102 	$ud = int($ud);
       
   103 	$self->{userdata} = $ud;
       
   104 	return $ud;
       
   105 }
       
   106 
       
   107 # Display the content of the feature flag in english.
       
   108 sub Show
       
   109 {
       
   110 	my $self = shift;
       
   111 	return undef unless(ref($self));
       
   112 	my $fd = shift;
       
   113 	$fd = *STDOUT unless(defined($fd));
       
   114 	printf $fd "UID 0x%08x\n", $self->UID();
       
   115 	printf $fd "Status Flags 0x%08x\n", $self->StatusFlags();
       
   116 
       
   117 	# Supported?
       
   118 	print "\t";
       
   119 	print "Not " unless($self->Supported);
       
   120 	print "Supported\n";
       
   121 
       
   122 	# Upgradable?
       
   123 	print "\t";
       
   124 	print "Not " unless($self->Upgradable);
       
   125 	print "Upgradable\n";
       
   126 
       
   127 	# Modifiable?
       
   128 	print "\t";
       
   129 	print "Not " unless($self->Modifiable);
       
   130 	print "Modifiable\n";
       
   131 	                              
       
   132 	# BlackListed?
       
   133 	print "\t";
       
   134 	print "Not " unless($self->BlackListed);
       
   135 	print "BlackListed\n";
       
   136 
       
   137 	# Uninitialised?
       
   138 	print "\t";
       
   139 	print "Not " unless($self->Uninitialised); # Double negative.
       
   140 	print "Uninitialised\n";
       
   141 
       
   142 	# Persisted?
       
   143 	print "\t";
       
   144 	print "Not " unless($self->Persisted);
       
   145 	print "Persisted\n";
       
   146 
       
   147 	printf $fd "User Data 0x%08x\n\n", $self->UserData();
       
   148 	return 1;
       
   149 }
       
   150 
       
   151 1;
       
   152 # ###########################################################################
       
   153 
       
   154 # The following methods operate on the 'StatusFlags' member, just setting
       
   155 # or clearing bits as required.
       
   156 #
       
   157 # Bits 6 through 31 are currently reserved, 23/7/07.
       
   158 #
       
   159 
       
   160 sub Supported
       
   161 {
       
   162 	my $self = shift;
       
   163 	return undef unless(ref($self));
       
   164 	my $arg = shift;
       
   165    	my $sp = $self->StatusFlags;
       
   166 	if(defined($arg))
       
   167 	{
       
   168 	    $arg = 0 if( $arg =~ m/EXCLUDE/i );
       
   169     	$arg = 1 if( $arg =~ m/FEATURE/i );
       
   170     	if($arg) { $sp |= 1; } else { $sp &= ~1; };
       
   171     	$self->StatusFlags($sp);
       
   172 		return $arg;
       
   173 	}
       
   174 	else
       
   175 	{
       
   176 		return ($sp & 1);
       
   177 	}
       
   178 }
       
   179 
       
   180 sub Upgradable
       
   181 {
       
   182 	my $self = shift;
       
   183 	return undef unless(ref($self));
       
   184 	my $arg = shift;
       
   185     my $sp = $self->StatusFlags;
       
   186 	if(defined($arg))
       
   187 	{
       
   188     	if($arg) { $sp |= 2; } else { $sp &= ~2; };
       
   189     	$self->StatusFlags($sp);
       
   190 		return $arg;
       
   191 	}
       
   192 	else
       
   193 	{
       
   194 		return ($sp & 2);
       
   195 	}
       
   196 }
       
   197 
       
   198 sub Modifiable
       
   199 {
       
   200 	my $self = shift;
       
   201 	return undef unless(ref($self));
       
   202 	my $arg = shift;
       
   203     my $sp = $self->StatusFlags;
       
   204 	if(defined($arg))
       
   205 	{
       
   206     	if($arg) { $sp |= 4; } else { $sp &= ~4; };
       
   207     	$self->StatusFlags($sp);
       
   208 		return $arg;
       
   209 	}
       
   210 	else
       
   211 	{
       
   212 		return ($sp & 4);
       
   213 	}
       
   214 }
       
   215 
       
   216 sub BlackListed
       
   217 {
       
   218 	my $self = shift;
       
   219 	return undef unless(ref($self));
       
   220 	my $arg = shift;
       
   221    	my $sp = $self->StatusFlags;
       
   222 	if(defined($arg))
       
   223 	{
       
   224     	if($arg) { $sp |= 8; } else { $sp &= ~8; };
       
   225     	$self->StatusFlags($sp);
       
   226 		return($arg);
       
   227 	}
       
   228 	else
       
   229 	{
       
   230 		return ($sp & 8);
       
   231 	}
       
   232 }
       
   233 
       
   234 sub Uninitialised
       
   235 {
       
   236 	my $self = shift;
       
   237 	return undef unless(ref($self));
       
   238 	my $arg = shift;
       
   239    	my $sp = $self->StatusFlags;
       
   240 	if(defined($arg))
       
   241 	{
       
   242     	if($arg) { $sp |= 16; } else { $sp &= ~16; };
       
   243     	$self->StatusFlags($sp);
       
   244 		return($arg);
       
   245 	}
       
   246 	else
       
   247 	{
       
   248 		return ($sp & 16);
       
   249 	}
       
   250 }
       
   251 
       
   252 sub Uninitialized
       
   253 {
       
   254 	return Uninitialised(@_);
       
   255 }
       
   256 
       
   257 sub Persisted
       
   258 {
       
   259 	my $self = shift;
       
   260 	return undef unless(ref($self));
       
   261 	my $arg = shift;
       
   262     my $sp = $self->StatusFlags;
       
   263 	if(defined($arg))
       
   264 	{
       
   265     	if($arg) { $sp |= 32; } else { $sp &= ~32; };
       
   266     	$self->StatusFlags($sp);
       
   267 		return($arg);
       
   268 	}
       
   269 	else
       
   270 	{
       
   271 		return ($sp & 32);
       
   272 	}
       
   273 }
       
   274 
       
   275 
       
   276 # ###########################################################################
       
   277 
       
   278 1;
       
   279