Skip to content

Commit e94069b

Browse files
committed
Update cellular comm interface for simulated interrupt
1 parent 7b15146 commit e94069b

File tree

2 files changed

+5
-237
lines changed

2 files changed

+5
-237
lines changed

FreeRTOS-Plus/Demo/FreeRTOS_Cellular_Interface_Windows_Simulator/Common/MutualAuthMQTTExample.c

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,6 @@
213213
*/
214214
#define mqttexampleTRANSPORT_SEND_RECV_TIMEOUT_MS ( 5000U )
215215

216-
/**
217-
* @brief Transport timeout in milliseconds for transport send and receive.
218-
*/
219-
#define mqttexampleTRANSPORT_SEND_RECV_TIMEOUT_MS ( 200U )
220-
221216
/**
222217
* @brief The length of the outgoing publish records array used by the coreMQTT
223218
* library to track QoS > 0 packet ACKS for outgoing publishes.

FreeRTOS-Plus/Demo/FreeRTOS_Cellular_Interface_Windows_Simulator/Common/comm_if_windows.c

Lines changed: 5 additions & 232 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
#define CELLULAR_COMM_PATH "\\\\.\\"CELLULAR_COMM_INTERFACE_PORT
5252

5353
/* Define the simulated UART interrupt number. */
54-
#define portINTERRUPT_UART ( 2UL )
54+
#define portINTERRUPT_UART portINTERRUPT_USER_DEFINED_START
5555

5656
/* Define the read write buffer size. */
5757
#define COMM_TX_BUFFER_SIZE ( 8192 )
@@ -91,7 +91,6 @@ typedef struct cellularCommContext
9191
HANDLE commFileHandle;
9292
CellularCommInterface_t * pCommInterface;
9393
bool commTaskThreadStarted;
94-
EventGroupHandle_t pCommTaskEvent;
9594
} cellularCommContext_t;
9695

9796
/*-----------------------------------------------------------*/
@@ -126,14 +125,6 @@ static CellularCommInterfaceError_t prvCommIntfReceive( CellularCommInterfaceHan
126125
*/
127126
static CellularCommInterfaceError_t prvCommIntfClose( CellularCommInterfaceHandle_t commInterfaceHandle );
128127

129-
/**
130-
* @brief Get default comm interface context.
131-
*
132-
* @return On success, SOCKETS_ERROR_NONE is returned. If an error occurred, error code defined
133-
* in sockets_wrapper.h is returned.
134-
*/
135-
static cellularCommContext_t * prvGetCellularCommContext( void );
136-
137128
/**
138129
* @brief UART interrupt handler.
139130
*
@@ -170,33 +161,6 @@ static CellularCommInterfaceError_t prvSetupCommTimeout( HANDLE hComm );
170161
*/
171162
static CellularCommInterfaceError_t prvSetupCommSettings( HANDLE hComm );
172163

173-
/**
174-
* @brief Thread routine to generate simulated interrupt.
175-
*
176-
* @param[in] pUserData Pointer to cellularCommContext_t allocated in comm interface open.
177-
*/
178-
static void prvCommTaskThread( void * pUserData );
179-
180-
/**
181-
* @brief Helper function to setup and create commTaskThread.
182-
*
183-
* @param[in] pCellularCommContext Cellular comm interface context allocated in open.
184-
*
185-
* @return On success, IOT_COMM_INTERFACE_SUCCESS is returned. If an error occurred, error code defined
186-
* in CellularCommInterfaceError_t is returned.
187-
*/
188-
static CellularCommInterfaceError_t prvSetupCommTaskThread( cellularCommContext_t * pCellularCommContext );
189-
190-
/**
191-
* @brief Helper function to clean commTaskThread.
192-
*
193-
* @param[in] pCellularCommContext Cellular comm interface context allocated in open.
194-
*
195-
* @return On success, IOT_COMM_INTERFACE_SUCCESS is returned. If an error occurred, error code defined
196-
* in CellularCommInterfaceError_t is returned.
197-
*/
198-
static CellularCommInterfaceError_t prvCleanCommTaskThread( cellularCommContext_t * pCellularCommContext );
199-
200164
/*-----------------------------------------------------------*/
201165

202166
CellularCommInterface_t CellularCommInterface =
@@ -215,28 +179,14 @@ static cellularCommContext_t uxCellularCommContext =
215179
.commFileHandle = NULL,
216180
.pUserData = NULL,
217181
.commStatus = 0U,
218-
.commTaskThreadStarted = false,
219-
.pCommTaskEvent = NULL
182+
.commTaskThreadStarted = false
220183
};
221184

222-
/* Mutex used to protect rxEvent variables that are accessed by multiple threads. */
223-
static void * pvRxEventMutex = NULL;
224-
225-
/* Indicate RX event is received in comm driver. */
226-
static bool rxEvent = false;
227-
228-
/*-----------------------------------------------------------*/
229-
230-
static cellularCommContext_t * prvGetCellularCommContext( void )
231-
{
232-
return &uxCellularCommContext;
233-
}
234-
235185
/*-----------------------------------------------------------*/
236186

237187
static uint32_t prvProcessUartInt( void )
238188
{
239-
cellularCommContext_t * pCellularCommContext = prvGetCellularCommContext();
189+
cellularCommContext_t * pCellularCommContext = &uxCellularCommContext;
240190
CellularCommInterfaceError_t callbackRet = IOT_COMM_INTERFACE_FAILURE;
241191
uint32_t retUartInt = pdTRUE;
242192

@@ -280,9 +230,7 @@ static DWORD WINAPI prvCellularCommReceiveCBThreadFunc( LPVOID pArgument )
280230
{
281231
if( ( dwCommStatus & EV_RXCHAR ) != 0 )
282232
{
283-
WaitForSingleObject( pvRxEventMutex, INFINITE );
284-
rxEvent = true;
285-
ReleaseMutex( pvRxEventMutex );
233+
vPortGenerateSimulatedInterruptFromNative( portINTERRUPT_UART );
286234
}
287235
}
288236
else
@@ -367,154 +315,14 @@ static CellularCommInterfaceError_t prvSetupCommSettings( HANDLE hComm )
367315

368316
/*-----------------------------------------------------------*/
369317

370-
static void prvCommTaskThread( void * pUserData )
371-
{
372-
cellularCommContext_t * pCellularCommContext = ( cellularCommContext_t * ) pUserData;
373-
EventBits_t uxBits = 0;
374-
375-
/* Inform thread ready. */
376-
LogInfo( ( "Cellular commTaskThread started" ) );
377-
378-
if( pCellularCommContext != NULL )
379-
{
380-
( void ) xEventGroupSetBits( pCellularCommContext->pCommTaskEvent,
381-
COMMTASK_EVT_MASK_STARTED );
382-
}
383-
384-
while( true )
385-
{
386-
/* Wait for notification from eventqueue. */
387-
uxBits = xEventGroupWaitBits( ( pCellularCommContext->pCommTaskEvent ),
388-
( ( EventBits_t ) COMMTASK_EVT_MASK_ABORT ),
389-
pdTRUE,
390-
pdFALSE,
391-
pdMS_TO_TICKS( COMMTASK_POLLING_TIME_MS ) );
392-
393-
if( ( uxBits & ( EventBits_t ) COMMTASK_EVT_MASK_ABORT ) != 0U )
394-
{
395-
LogDebug( ( "Abort received, cleaning up!" ) );
396-
break;
397-
}
398-
else
399-
{
400-
/* Polling the global share variable to trigger the interrupt. */
401-
if( rxEvent == true )
402-
{
403-
WaitForSingleObject( pvRxEventMutex, INFINITE );
404-
rxEvent = false;
405-
ReleaseMutex( pvRxEventMutex );
406-
407-
vPortGenerateSimulatedInterrupt( portINTERRUPT_UART );
408-
}
409-
}
410-
}
411-
412-
/* Inform thread ready. */
413-
if( pCellularCommContext != NULL )
414-
{
415-
( void ) xEventGroupSetBits( pCellularCommContext->pCommTaskEvent, COMMTASK_EVT_MASK_ABORTED );
416-
}
417-
418-
LogInfo( ( "Cellular commTaskThread exit" ) );
419-
}
420-
421-
/*-----------------------------------------------------------*/
422-
423-
static CellularCommInterfaceError_t prvSetupCommTaskThread( cellularCommContext_t * pCellularCommContext )
424-
{
425-
BOOL Status = TRUE;
426-
EventBits_t uxBits = 0;
427-
CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS;
428-
429-
pCellularCommContext->pCommTaskEvent = xEventGroupCreate();
430-
431-
if( pCellularCommContext->pCommTaskEvent != NULL )
432-
{
433-
/* Create the FreeRTOS thread to generate the simulated interrupt. */
434-
Status = Platform_CreateDetachedThread( prvCommTaskThread,
435-
( void * ) pCellularCommContext,
436-
COMM_IF_THREAD_DEFAULT_PRIORITY,
437-
COMM_IF_THREAD_DEFAULT_STACK_SIZE );
438-
439-
if( Status != true )
440-
{
441-
commIntRet = IOT_COMM_INTERFACE_FAILURE;
442-
}
443-
}
444-
else
445-
{
446-
commIntRet = IOT_COMM_INTERFACE_FAILURE;
447-
}
448-
449-
if( commIntRet == IOT_COMM_INTERFACE_SUCCESS )
450-
{
451-
uxBits = xEventGroupWaitBits( ( pCellularCommContext->pCommTaskEvent ),
452-
( ( EventBits_t ) COMMTASK_EVT_MASK_STARTED | ( EventBits_t ) COMMTASK_EVT_MASK_ABORTED ),
453-
pdTRUE,
454-
pdFALSE,
455-
portMAX_DELAY );
456-
457-
if( ( uxBits & ( EventBits_t ) COMMTASK_EVT_MASK_STARTED ) == COMMTASK_EVT_MASK_STARTED )
458-
{
459-
pCellularCommContext->commTaskThreadStarted = true;
460-
}
461-
else
462-
{
463-
commIntRet = IOT_COMM_INTERFACE_FAILURE;
464-
pCellularCommContext->commTaskThreadStarted = false;
465-
}
466-
}
467-
468-
return commIntRet;
469-
}
470-
471-
/*-----------------------------------------------------------*/
472-
473-
static CellularCommInterfaceError_t prvCleanCommTaskThread( cellularCommContext_t * pCellularCommContext )
474-
{
475-
EventBits_t uxBits = 0;
476-
CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS;
477-
478-
/* Wait for the commTaskThreadStarted exit. */
479-
if( ( pCellularCommContext->commTaskThreadStarted == true ) && ( pCellularCommContext->pCommTaskEvent != NULL ) )
480-
{
481-
( void ) xEventGroupSetBits( pCellularCommContext->pCommTaskEvent,
482-
COMMTASK_EVT_MASK_ABORT );
483-
uxBits = xEventGroupWaitBits( ( pCellularCommContext->pCommTaskEvent ),
484-
( ( EventBits_t ) COMMTASK_EVT_MASK_ABORTED ),
485-
pdTRUE,
486-
pdFALSE,
487-
portMAX_DELAY );
488-
489-
if( ( uxBits & ( EventBits_t ) COMMTASK_EVT_MASK_ABORTED ) != COMMTASK_EVT_MASK_ABORTED )
490-
{
491-
LogDebug( ( "Cellular close wait commTaskThread fail" ) );
492-
commIntRet = IOT_COMM_INTERFACE_FAILURE;
493-
}
494-
495-
pCellularCommContext->commTaskThreadStarted = false;
496-
}
497-
498-
/* Clean the event group. */
499-
if( pCellularCommContext->pCommTaskEvent != NULL )
500-
{
501-
vEventGroupDelete( pCellularCommContext->pCommTaskEvent );
502-
pCellularCommContext->pCommTaskEvent = NULL;
503-
}
504-
505-
return commIntRet;
506-
}
507-
508-
/*-----------------------------------------------------------*/
509-
510318
static CellularCommInterfaceError_t prvCommIntfOpen( CellularCommInterfaceReceiveCallback_t receiveCallback,
511319
void * pUserData,
512320
CellularCommInterfaceHandle_t * pCommInterfaceHandle )
513321
{
514322
CellularCommInterfaceError_t commIntRet = IOT_COMM_INTERFACE_SUCCESS;
515323
HANDLE hComm = ( HANDLE ) INVALID_HANDLE_VALUE;
516324
BOOL Status = TRUE;
517-
cellularCommContext_t * pCellularCommContext = prvGetCellularCommContext();
325+
cellularCommContext_t * pCellularCommContext = &uxCellularCommContext;
518326
DWORD dwRes = 0;
519327

520328
if( pCellularCommContext == NULL )
@@ -592,25 +400,10 @@ static CellularCommInterfaceError_t prvCommIntfOpen( CellularCommInterfaceReceiv
592400
}
593401
}
594402

595-
/* Create RX event mutex to protect rxEvent. */
596-
if( commIntRet == IOT_COMM_INTERFACE_SUCCESS )
597-
{
598-
pvRxEventMutex = CreateMutex( NULL, FALSE, NULL );
599-
600-
if( pvRxEventMutex == NULL )
601-
{
602-
commIntRet = IOT_COMM_INTERFACE_FAILURE;
603-
}
604-
}
605-
606403
if( commIntRet == IOT_COMM_INTERFACE_SUCCESS )
607404
{
608405
pCellularCommContext->commReceiveCallback = receiveCallback;
609-
commIntRet = prvSetupCommTaskThread( pCellularCommContext );
610-
}
611406

612-
if( commIntRet == IOT_COMM_INTERFACE_SUCCESS )
613-
{
614407
vPortSetInterruptHandler( portINTERRUPT_UART, prvProcessUartInt );
615408
pCellularCommContext->commReceiveCallbackThread =
616409
CreateThread( NULL, 0, prvCellularCommReceiveCBThreadFunc, hComm, 0, NULL );
@@ -653,16 +446,6 @@ static CellularCommInterfaceError_t prvCommIntfOpen( CellularCommInterfaceReceiv
653446
}
654447

655448
pCellularCommContext->commReceiveCallbackThread = NULL;
656-
657-
/* Wait for the commTaskThreadStarted exit. */
658-
( void ) prvCleanCommTaskThread( pCellularCommContext );
659-
660-
/* Clean the rxEvent mutex. */
661-
if( pvRxEventMutex != NULL )
662-
{
663-
CloseHandle( pvRxEventMutex );
664-
pvRxEventMutex = NULL;
665-
}
666449
}
667450

668451
return commIntRet;
@@ -731,16 +514,6 @@ static CellularCommInterfaceError_t prvCommIntfClose( CellularCommInterfaceHandl
731514

732515
pCellularCommContext->commReceiveCallbackThread = NULL;
733516

734-
/* Clean the commTaskThread. */
735-
( void ) prvCleanCommTaskThread( pCellularCommContext );
736-
737-
/* Clean the rxEvent mutex. */
738-
if( pvRxEventMutex != NULL )
739-
{
740-
CloseHandle( pvRxEventMutex );
741-
pvRxEventMutex = NULL;
742-
}
743-
744517
/* clean the data structure. */
745518
pCellularCommContext->commStatus &= ~( CELLULAR_COMM_OPEN_BIT );
746519
}

0 commit comments

Comments
 (0)