Skip to content

Commit ed04dc7

Browse files
htiboschHein Tiboschalfred2gAniruddhaKanhere
authored
IPv4/Single: Add a SocketID to a socket (#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 <[email protected]> Co-authored-by: alfred gedeon <[email protected]> Co-authored-by: Aniruddha Kanhere <[email protected]>
1 parent b33224b commit ed04dc7

File tree

5 files changed

+146
-0
lines changed

5 files changed

+146
-0
lines changed

.github/lexicon.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,7 @@ egetlinklayeraddress
230230
ehertype
231231
einitialwait
232232
einprogress
233+
einval
233234
einvalidchecksum
234235
einvaliddata
235236
eleasedaddress
@@ -785,6 +786,7 @@ pvparameters
785786
pvportmalloc
786787
pvportmallocsocket
787788
pvsearchid
789+
pvsocketid
788790
pvsource
789791
pxackmessage
790792
pxaddr
@@ -981,6 +983,7 @@ skipnamefield
981983
snd
982984
snprintf
983985
sockaddr
986+
socketid
984987
socketset
985988
sockopt
986989
sof
@@ -1392,8 +1395,10 @@ vrxfaultinjection
13921395
vsocketbind
13931396
vsocketclose
13941397
vsocketclosenexttime
1398+
vsocketgetsocketid
13951399
vsocketlistennexttime
13961400
vsocketselect
1401+
vsocketsetsocketid
13971402
vsocketwakeupuser
13981403
vtasklist
13991404
vtasknotifygivefromisr

source/FreeRTOS_Sockets.c

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4797,6 +4797,49 @@ BaseType_t xSocketValid( const ConstSocket_t xSocket )
47974797
#endif /* ipconfigUSE_TCP */
47984798
/*-----------------------------------------------------------*/
47994799

4800+
/**
4801+
* @brief Set the value of the SocketID of a socket.
4802+
* @param[in] xSocket: The socket whose ID should be set.
4803+
* @param[in] pvSocketID: The new value for the SocketID.
4804+
* @return Zero if the socket was valid, otherwise -EINVAL.
4805+
*/
4806+
BaseType_t xSocketSetSocketID( const Socket_t xSocket,
4807+
void * pvSocketID )
4808+
{
4809+
FreeRTOS_Socket_t * pxSocket = ( FreeRTOS_Socket_t * ) xSocket;
4810+
BaseType_t xReturn = -pdFREERTOS_ERRNO_EINVAL;
4811+
4812+
if( xSocketValid( pxSocket ) )
4813+
{
4814+
xReturn = 0;
4815+
pxSocket->pvSocketID = pvSocketID;
4816+
}
4817+
4818+
return xReturn;
4819+
}
4820+
/*-----------------------------------------------------------*/
4821+
4822+
/**
4823+
* @brief Retrieve the SocketID that is associated with a socket.
4824+
* @param[in] xSocket: The socket whose ID should be returned.
4825+
* @return The current value of pvSocketID, or NULL in case
4826+
* the socket pointer is not valid or when the ID was not
4827+
* yet set.
4828+
*/
4829+
void * pvSocketGetSocketID( const ConstSocket_t xSocket )
4830+
{
4831+
const FreeRTOS_Socket_t * pxSocket = ( const FreeRTOS_Socket_t * ) xSocket;
4832+
void * pvReturn = NULL;
4833+
4834+
if( xSocketValid( pxSocket ) )
4835+
{
4836+
pvReturn = pxSocket->pvSocketID;
4837+
}
4838+
4839+
return pvReturn;
4840+
}
4841+
/*-----------------------------------------------------------*/
4842+
48004843
#if ( ( ipconfigHAS_PRINTF != 0 ) && ( ipconfigUSE_TCP == 1 ) )
48014844

48024845
/**

source/include/FreeRTOS_IP_Private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -680,6 +680,13 @@ typedef struct xSOCKET
680680
EventBits_t xSocketBits; /**< These bits indicate the events which have actually occurred.
681681
* They are maintained by the IP-task */
682682
#endif /* ipconfigSUPPORT_SELECT_FUNCTION */
683+
684+
/* This field is only only by the user, and can be accessed with
685+
* vSocketSetSocketID() / vSocketGetSocketID().
686+
* All fields of a socket will be cleared by memset() in FreeRTOS_socket().
687+
*/
688+
void * pvSocketID;
689+
683690
/* TCP/UDP specific fields: */
684691
/* Before accessing any member of this structure, it should be confirmed */
685692
/* that the protocol corresponds with the type of structure */

source/include/FreeRTOS_Sockets.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,14 @@
366366

367367
void FreeRTOS_netstat( void );
368368

369+
/* This option adds the possibility to have a user-ID attached to a socket.
370+
* The type of this ID is a void *. Both UDP and TCP sockets have
371+
* this ID. It has a default value of NULL.
372+
*/
373+
BaseType_t xSocketSetSocketID( const Socket_t xSocket,
374+
void * pvSocketID );
375+
376+
void * pvSocketGetSocketID( const ConstSocket_t xSocket );
369377

370378
/* End TCP Socket Attributes. */
371379

test/unit-test/FreeRTOS_Sockets/FreeRTOS_Sockets_GenericAPI_utest.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2903,6 +2903,89 @@ void test_FreeRTOS_maywrite_HappyPath( void )
29032903
TEST_ASSERT_EQUAL( 0x3344, xReturn );
29042904
}
29052905

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

0 commit comments

Comments
 (0)