Skip to content

Commit

Permalink
Fix for reliability of PPP disconnect.
Browse files Browse the repository at this point in the history
  • Loading branch information
RobMeades committed Mar 11, 2024
1 parent 99995bd commit 19da622
Showing 1 changed file with 19 additions and 21 deletions.
40 changes: 19 additions & 21 deletions port/platform/linux/src/u_port_ppp.c
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,11 @@ typedef struct {

/** Root of the linked list of PPP entities.
*/
static uLinkedList_t *gpPppInterfaceList = NULL; /**< A linked list of uPortPppInterface_t. */
static uLinkedList_t *gpPppInterfaceList = NULL; /**< A list of uPortPppInterface_t. */

/** Root of linked list of local device names.
*/
static uLinkedList_t *gpPppLocalDeviceNameList =
NULL; /**< A linked list of uPortPppLocalDevice_t. */
static uLinkedList_t *gpPppLocalDeviceNameList = NULL; /**< A list of uPortPppLocalDevice_t. */

/** Mutex to protect the linked list of PPP entities.
*/
Expand All @@ -199,7 +198,7 @@ static const char gLcpTerminateReqPacket[] = {0x7e, 0xff, 0x7d, 0x23, 0xc0, 0x21
};

/** The bytes that represent an LCP Terminate-Ack for
* gLcpTerminateReqPacket.
* gLcpTerminateReqPacket[].
*/
static const char gLcpTerminateAckPacket[] = {0x7e, 0xff, 0x7d, 0x23, 0xc0, 0x21, 0x7d, 0x26,
0x7d, 0x22, 0x7d, 0x20, 0x7d, 0x24, 0x94, 0x7d,
Expand Down Expand Up @@ -378,19 +377,17 @@ static void terminateLink(uPortPppInterface_t *pPppInterface)
startTimeMs = uPortGetTickTimeMs();
while ((pPppInterface->waitingForModuleDisconnect || pppdConnected) &&
(uPortGetTickTimeMs() - startTimeMs < U_PORT_PPP_CONNECT_TIMEOUT_SECONDS * 1000)) {
if (pppdConnected) {
// Wait for data to arrive on the connected socket
if (socketSelect(pPppInterface->connectedSocket, U_CFG_OS_YIELD_MS)) {
// Read the data
dataSize = read(pPppInterface->connectedSocket, buffer, sizeof(buffer));
if (dataSize > 0) {
if (bufferContains(&(pPppInterface->fromPppdBufferCache),
buffer, dataSize,
gLcpTerminateAckPacket,
sizeof(gLcpTerminateAckPacket))) {
pppdConnected = false;
}
}
// Wait for data to arrive on the connected socket
if (pppdConnected &&
socketSelect(pPppInterface->connectedSocket, U_CFG_OS_YIELD_MS)) {
// Read the data
dataSize = read(pPppInterface->connectedSocket, buffer, sizeof(buffer));
if ((dataSize > 0) &&
bufferContains(&(pPppInterface->fromPppdBufferCache),
buffer, dataSize,
gLcpTerminateAckPacket,
sizeof(gLcpTerminateAckPacket))) {
pppdConnected = false;
}
}
uPortTaskBlock(250);
Expand Down Expand Up @@ -462,7 +459,8 @@ static void socketTask(void *pParameters)
uPortLog("U_PORT_PPP: pppd connected.\n");
while (!pPppInterface->socketTaskExit) {
// Wait for data to arrive on the connected socket
if (socketSelect(pPppInterface->connectedSocket, U_CFG_OS_YIELD_MS)) {
if (socketSelect(pPppInterface->connectedSocket, U_CFG_OS_YIELD_MS) &&
!pPppInterface->dataTransferSuspended) {
// Read the data
dataSize = read(pPppInterface->connectedSocket, buffer, sizeof(buffer));
if ((dataSize > 0) &&
Expand Down Expand Up @@ -531,7 +529,7 @@ static int32_t startSocketTask(uPortPppInterface_t *pPppInterface,
pPppInterface->listeningSocket = socket(AF_INET, SOCK_STREAM, 0);
if (pPppInterface->listeningSocket >= 0) {
if (setsockopt(pPppInterface->listeningSocket, SOL_SOCKET, SO_REUSEADDR,
(const char*) &reuse, sizeof(reuse)) < 0) {
(const char *) &reuse, sizeof(reuse)) < 0) {
// This is not fatal, it just means that the OS might prevent
// us binding to the same address again if we come back into
// here too quickly
Expand All @@ -540,7 +538,7 @@ static int32_t startSocketTask(uPortPppInterface_t *pPppInterface,
}
#ifdef SO_REUSEPORT
if (setsockopt(pPppInterface->listeningSocket, SOL_SOCKET, SO_REUSEPORT,
(const char*) &reuse, sizeof(reuse)) < 0) {
(const char *) &reuse, sizeof(reuse)) < 0) {
// This is not fatal, it just means that the OS might prevent
// us binding to the same address again if we come back into
// here too quickly
Expand Down Expand Up @@ -573,7 +571,7 @@ static int32_t startSocketTask(uPortPppInterface_t *pPppInterface,
U_PORT_PPP_SOCKET_TASK_PRIORITY,
&(pPppInterface->socketTaskHandle));
if (errorCode == 0) {
uPortLog("U_PORT_PPP: listening for pppd on \"%s\".\n", pAddressString);
uPortLog("U_PORT_PPP: listening for pppd on %s.\n", pAddressString);
} else {
uPortMutexDelete(pPppInterface->socketTaskMutex);
close(pPppInterface->listeningSocket);
Expand Down

0 comments on commit 19da622

Please sign in to comment.