Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:26

0001 /* SPDX-License-Identifier: BSD-2-Clause */
0002 
0003 /**
0004  * @file
0005  *
0006  * @ingroup RTEMSScoreObject
0007  *
0008  * @brief This source file contains the implementation of
0009  *   _Objects_Name_to_string() and _Objects_Get_name_as_string().
0010  */
0011 
0012 /*
0013  *  COPYRIGHT (c) 1989-2008.
0014  *  On-Line Applications Research Corporation (OAR).
0015  *
0016  * Redistribution and use in source and binary forms, with or without
0017  * modification, are permitted provided that the following conditions
0018  * are met:
0019  * 1. Redistributions of source code must retain the above copyright
0020  *    notice, this list of conditions and the following disclaimer.
0021  * 2. Redistributions in binary form must reproduce the above copyright
0022  *    notice, this list of conditions and the following disclaimer in the
0023  *    documentation and/or other materials provided with the distribution.
0024  *
0025  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
0026  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
0027  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
0028  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
0029  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0030  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0031  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
0032  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
0033  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
0034  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
0035  * POSSIBILITY OF SUCH DAMAGE.
0036  */
0037 
0038 #ifdef HAVE_CONFIG_H
0039 #include "config.h"
0040 #endif
0041 
0042 #include <rtems/score/threadimpl.h>
0043 
0044 /*
0045  * Do not use isprint() from <ctypes.h> since this depends on the heavy weight
0046  * C locale support of Newlib.
0047  */
0048 static bool _Objects_Name_char_is_printable( char c )
0049 {
0050   unsigned char uc;
0051 
0052   uc = (unsigned char) c;
0053   return uc >= ' ' && uc <= '~';
0054 }
0055 
0056 size_t _Objects_Name_to_string(
0057   Objects_Name  name,
0058   bool          is_string,
0059   char         *buffer,
0060   size_t        buffer_size
0061 )
0062 {
0063   char        lname[ 5 ];
0064   const char *s;
0065   char       *d;
0066   size_t      i;
0067 
0068   if ( is_string ) {
0069     s = name.name_p;
0070   } else {
0071     lname[ 0 ] = (name.name_u32 >> 24) & 0xff;
0072     lname[ 1 ] = (name.name_u32 >> 16) & 0xff;
0073     lname[ 2 ] = (name.name_u32 >>  8) & 0xff;
0074     lname[ 3 ] = (name.name_u32 >>  0) & 0xff;
0075     lname[ 4 ] = '\0';
0076     s = lname;
0077   }
0078 
0079   d = buffer;
0080   i = 1;
0081 
0082   if ( s != NULL ) {
0083     while ( *s != '\0' ) {
0084       if ( i < buffer_size ) {
0085         *d = _Objects_Name_char_is_printable(*s) ? *s : '*';
0086         ++d;
0087       }
0088 
0089       ++s;
0090       ++i;
0091     }
0092   }
0093 
0094   if ( buffer_size > 0 ) {
0095     *d = '\0';
0096   }
0097 
0098   return i - 1;
0099 }
0100 
0101 /*
0102  *  This method objects the name of an object and returns its name
0103  *  in the form of a C string.  It attempts to be careful about
0104  *  overflowing the user's string and about returning unprintable characters.
0105  */
0106 
0107 char *_Objects_Get_name_as_string(
0108   Objects_Id        id,
0109   size_t            length,
0110   char             *name
0111 )
0112 {
0113   const Objects_Information *information;
0114   const Objects_Control     *the_object;
0115   ISR_lock_Context           lock_context;
0116   Objects_Id                 tmpId;
0117 
0118   if ( length == 0 )
0119     return NULL;
0120 
0121   if ( name == NULL )
0122     return NULL;
0123 
0124   tmpId = (id == OBJECTS_ID_OF_SELF) ? _Thread_Get_executing()->Object.id : id;
0125 
0126   information = _Objects_Get_information_id( tmpId );
0127   if ( !information )
0128     return NULL;
0129 
0130   the_object = _Objects_Get( tmpId, &lock_context, information );
0131   if ( the_object == NULL ) {
0132     return NULL;
0133   }
0134 
0135   _Objects_Name_to_string(
0136     the_object->name,
0137     _Objects_Has_string_name( information ),
0138     name,
0139     length
0140   );
0141 
0142   _ISR_lock_ISR_enable( &lock_context );
0143   return name;
0144 }