Skip to content

Commit c99f8fc

Browse files
htiboschHein Tiboschalfred2gAniruddhaKanhere
authored andcommitted
IPv4/Single: Add a SocketID to a socket (FreeRTOS#546)
* IPv4/Single: Add a SocketID to a socket * Change in comment * Applied uncrustify to format the source code * Added a few entries to lexicon.txt * Removed the 'ipconfigUSE_SetSocketID' option * Change to lexicon.txt * Add unit tests for the newly added API Co-authored-by: Hein Tibosch <hein@htibosch.net> Co-authored-by: alfred gedeon <28123637+alfred2g@users.noreply.github.com> Co-authored-by: Aniruddha Kanhere <60444055+AniruddhaKanhere@users.noreply.github.com>
1 parent c05a087 commit c99f8fc

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

.github/lexicon.txt

+5
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ ehertype
250250
eigrp
251251
einitialwait
252252
einprogress
253+
einval
253254
einvalidchecksum
254255
einvaliddata
255256
eleasedaddress
@@ -888,6 +889,7 @@ pvportmalloc
888889
pvportmallocsocket
889890
pvptr
890891
pvsearchid
892+
pvsocketid
891893
pvsource
892894
pxackmessage
893895
pxaddr
@@ -1127,6 +1129,7 @@ snd
11271129
snprintf
11281130
sntptimestamp
11291131
sockaddr
1132+
socketid
11301133
socketset
11311134
sockopt
11321135
sof
@@ -1645,8 +1648,10 @@ vrxfaultinjection
16451648
vsocketbind
16461649
vsocketclose
16471650
vsocketclosenexttime
1651+
vsocketgetsocketid
16481652
vsocketlistennexttime
16491653
vsocketselect
1654+
vsocketsetsocketid
16501655
vsocketwakeupuser
16511656
vstartntptask
16521657
vtaskdelay

source/FreeRTOS_Sockets.c

+43
Original file line numberDiff line numberDiff line change
@@ -5455,6 +5455,49 @@ BaseType_t xSocketValid( const ConstSocket_t xSocket )
54555455
#endif /* ipconfigUSE_TCP */
54565456
/*-----------------------------------------------------------*/
54575457

5458+
/**
5459+
* @brief Set the value of the SocketID of a socket.
5460+
* @param[in] xSocket: The socket whose ID should be set.
5461+
* @param[in] pvSocketID: The new value for the SocketID.
5462+
* @return Zero if the socket was valid, otherwise -EINVAL.
5463+
*/
5464+
BaseType_t xSocketSetSocketID( const Socket_t xSocket,
5465+
void * pvSocketID )
5466+
{
5467+
FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
5468+
BaseType_t xReturn = -pdFREERTOS_ERRNO_EINVAL;
5469+
5470+
if( xSocketValid( pxSocket ) )
5471+
{
5472+
xReturn = 0;
5473+
pxSocket->pvSocketID = pvSocketID;
5474+
}
5475+
5476+
return xReturn;
5477+
}
5478+
/*-----------------------------------------------------------*/
5479+
5480+
/**
5481+
* @brief Retrieve the SocketID that is associated with a socket.
5482+
* @param[in] xSocket: The socket whose ID should be returned.
5483+
* @return The current value of pvSocketID, or NULL in case
5484+
* the socket pointer is not valid or when the ID was not
5485+
* yet set.
5486+
*/
5487+
void * pvSocketGetSocketID( const ConstSocket_t xSocket )
5488+
{
5489+
const FreeRTOS_Socket_t * pxSocket = ( const FreeRTOS_Socket_t * ) xSocket;
5490+
void * pvReturn = NULL;
5491+
5492+
if( xSocketValid( pxSocket ) )
5493+
{
5494+
pvReturn = pxSocket->pvSocketID;
5495+
}
5496+
5497+
return pvReturn;
5498+
}
5499+
/*-----------------------------------------------------------*/
5500+
54585501
#if ( ( ipconfigHAS_PRINTF != 0 ) && ( ipconfigUSE_TCP == 1 ) )
54595502

54605503
/**

source/include/FreeRTOS_IP_Private.h

+7
Original file line numberDiff line numberDiff line change
@@ -701,6 +701,13 @@ struct xSOCKET
701701
* They are maintained by the IP-task */
702702
#endif /* ipconfigSUPPORT_SELECT_FUNCTION */
703703
struct xNetworkEndPoint * pxEndPoint; /**< The end-point to which the socket is bound. */
704+
705+
/* This field is only only by the user, and can be accessed with
706+
* vSocketSetSocketID() / vSocketGetSocketID().
707+
* All fields of a socket will be cleared by memset() in FreeRTOS_socket().
708+
*/
709+
void * pvSocketID;
710+
704711
/* TCP/UDP specific fields: */
705712
/* Before accessing any member of this structure, it should be confirmed */
706713
/* that the protocol corresponds with the type of structure */

source/include/FreeRTOS_Sockets.h

+8
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,14 @@
379379

380380
void FreeRTOS_netstat( void );
381381

382+
/* This option adds the possibility to have a user-ID attached to a socket.
383+
* The type of this ID is a void *. Both UDP and TCP sockets have
384+
* this ID. It has a default value of NULL.
385+
*/
386+
BaseType_t xSocketSetSocketID( const Socket_t xSocket,
387+
void * pvSocketID );
388+
389+
void * pvSocketGetSocketID( const ConstSocket_t xSocket );
382390

383391
/* End TCP Socket Attributes. */
384392

test/unit-test/FreeRTOS_Sockets/FreeRTOS_Sockets_GenericAPI_utest.c

+83
Original file line numberDiff line numberDiff line change
@@ -2902,6 +2902,89 @@ void test_FreeRTOS_maywrite_HappyPath( void )
29022902
TEST_ASSERT_EQUAL( 0x3344, xReturn );
29032903
}
29042904

2905+
/*
2906+
* @brief Test setting socket ID when the socket is NULL.
2907+
*/
2908+
void test_xSocketSetSocketID_NULLSocket( void )
2909+
{
2910+
BaseType_t xReturn;
2911+
2912+
xReturn = xSocketSetSocketID( NULL, NULL );
2913+
2914+
TEST_ASSERT_EQUAL( -pdFREERTOS_ERRNO_EINVAL, xReturn );
2915+
}
2916+
2917+
/*
2918+
* @brief Test setting socket ID when the socket is invalid.
2919+
*/
2920+
void test_xSocketSetSocketID_InvalidSocket( void )
2921+
{
2922+
BaseType_t xReturn;
2923+
2924+
xReturn = xSocketSetSocketID( FREERTOS_INVALID_SOCKET, NULL );
2925+
2926+
TEST_ASSERT_EQUAL( -pdFREERTOS_ERRNO_EINVAL, xReturn );
2927+
}
2928+
2929+
/*
2930+
* @brief Test setting socket ID when the socket is Valid.
2931+
*/
2932+
void test_xSocketSetSocketID_ValidSocket( void )
2933+
{
2934+
BaseType_t xReturn;
2935+
FreeRTOS_Socket_t xSocket;
2936+
BaseType_t AnchorVariable;
2937+
2938+
memset( &xSocket, 0, sizeof( xSocket ) );
2939+
2940+
xReturn = xSocketSetSocketID( &xSocket, &AnchorVariable );
2941+
2942+
TEST_ASSERT_EQUAL( 0, xReturn );
2943+
TEST_ASSERT_EQUAL( &AnchorVariable, xSocket.pvSocketID );
2944+
}
2945+
2946+
/*
2947+
* @brief Test setting socket ID when the socket is NULL.
2948+
*/
2949+
void test_pvSocketGetSocketID_NULLSocket( void )
2950+
{
2951+
void * pvReturn;
2952+
2953+
pvReturn = pvSocketGetSocketID( NULL );
2954+
2955+
TEST_ASSERT_EQUAL( NULL, pvReturn );
2956+
}
2957+
2958+
/*
2959+
* @brief Test setting socket ID when the socket is invalid.
2960+
*/
2961+
void test_pvSocketGetSocketID_InvalidSocket( void )
2962+
{
2963+
void * pvReturn;
2964+
2965+
pvReturn = pvSocketGetSocketID( FREERTOS_INVALID_SOCKET );
2966+
2967+
TEST_ASSERT_EQUAL( NULL, pvReturn );
2968+
}
2969+
2970+
/*
2971+
* @brief Test setting socket ID when the socket is Valid.
2972+
*/
2973+
void test_pvSocketGetSocketID_ValidSocket( void )
2974+
{
2975+
BaseType_t pvReturn;
2976+
FreeRTOS_Socket_t xSocket;
2977+
BaseType_t AnchorVariable;
2978+
2979+
memset( &xSocket, 0, sizeof( xSocket ) );
2980+
2981+
xSocket.pvSocketID = &AnchorVariable;
2982+
2983+
pvReturn = pvSocketGetSocketID( &xSocket );
2984+
2985+
TEST_ASSERT_EQUAL( &AnchorVariable, pvReturn );
2986+
}
2987+
29052988
/*
29062989
* @brief This function just prints out some data. It is expected to make calls to the
29072990
* below functions when IP stack is not initialised.

0 commit comments

Comments
 (0)