1717#include <zephyr/net/ethernet.h>
1818#include <zephyr/net/net_if.h>
1919#include <zephyr/net/ppp.h>
20+ #include <zephyr/posix/sys/eventfd.h>
2021#include <zephyr/posix/sys/socket.h>
2122#include <zephyr/random/random.h>
2223#include <assert.h>
@@ -88,11 +89,13 @@ static unsigned int ppp_pdn_cid;
8889enum {
8990 ZEPHYR_FD_IDX , /* Raw Zephyr socket to pass data to/from the PPP link. */
9091 MODEM_FD_IDX , /* Raw modem socket to pass data to/from the LTE link. */
92+ EVENT_FD_IDX , /* Eventfd to signal the PPP thread. */
9193 PPP_FDS_COUNT
9294};
9395const char * const ppp_socket_names [PPP_FDS_COUNT ] = {
9496 [ZEPHYR_FD_IDX ] = "Zephyr ",
95- [MODEM_FD_IDX ] = "modem "
97+ [MODEM_FD_IDX ] = "modem ",
98+ [EVENT_FD_IDX ] = "eventfd "
9699};
97100static int ppp_fds [PPP_FDS_COUNT ] = { -1 , -1 };
98101
@@ -117,7 +120,7 @@ static bool open_ppp_sockets(void)
117120 ppp_fds [ZEPHYR_FD_IDX ] = zsock_socket (AF_PACKET , SOCK_DGRAM | SOCK_NATIVE ,
118121 htons (ETH_P_ALL ));
119122 if (ppp_fds [ZEPHYR_FD_IDX ] < 0 ) {
120- LOG_ERR ("Zephyr socket creation failed (%d)." , errno );
123+ LOG_ERR ("Zephyr socket creation failed (%d)." , - errno );
121124 return false;
122125 }
123126
@@ -129,13 +132,13 @@ static bool open_ppp_sockets(void)
129132 ret = zsock_bind (ppp_fds [ZEPHYR_FD_IDX ],
130133 (const struct sockaddr * )& ppp_zephyr_dst_addr , sizeof (ppp_zephyr_dst_addr ));
131134 if (ret < 0 ) {
132- LOG_ERR ("Failed to bind Zephyr socket (%d)." , errno );
135+ LOG_ERR ("Failed to bind Zephyr socket (%d)." , - errno );
133136 return false;
134137 }
135138
136139 ppp_fds [MODEM_FD_IDX ] = zsock_socket (AF_PACKET , SOCK_RAW , 0 );
137140 if (ppp_fds [MODEM_FD_IDX ] < 0 ) {
138- LOG_ERR ("Modem socket creation failed (%d)." , errno );
141+ LOG_ERR ("Modem socket creation failed (%d)." , - errno );
139142 return false;
140143 }
141144
@@ -151,6 +154,12 @@ static bool open_ppp_sockets(void)
151154 return false;
152155 }
153156
157+ ppp_fds [EVENT_FD_IDX ] = eventfd (0 , 0 );
158+ if (ppp_fds [EVENT_FD_IDX ] < 0 ) {
159+ LOG_ERR ("Eventfd creation failed (%d)." , - errno );
160+ return false;
161+ }
162+
154163 return true;
155164}
156165
@@ -162,7 +171,7 @@ static void close_ppp_sockets(void)
162171 }
163172 if (zsock_close (ppp_fds [i ])) {
164173 LOG_WRN ("Failed to close %s socket (%d)." ,
165- ppp_socket_names [i ], errno );
174+ ppp_socket_names [i ], - errno );
166175 }
167176 ppp_fds [i ] = -1 ;
168177 }
@@ -218,7 +227,7 @@ static void delegate_ppp_event(enum ppp_action action, enum ppp_reason reason)
218227 LOG_DBG ("PPP %s, reason: %d" , ppp_action_str (event .action ), event .reason );
219228
220229 if (k_msgq_put (& ppp_work .queue , & event , K_NO_WAIT )) {
221- LOG_ERR ("Failed to queue PPP event (%d)." , errno );
230+ LOG_ERR ("Failed to queue PPP event." );
222231 }
223232
224233 k_work_submit_to_queue (& slm_work_q , & ppp_work .work );
@@ -371,10 +380,12 @@ static int ppp_stop(enum ppp_reason reason)
371380
372381 net_if_carrier_off (ppp_iface );
373382
374- close_ppp_sockets ();
375-
383+ /* Close the thread. */
384+ eventfd_write ( ppp_fds [ EVENT_FD_IDX ], 1 );
376385 k_thread_join (& ppp_data_passing_thread_id , K_SECONDS (1 ));
377386
387+ close_ppp_sockets ();
388+
378389 ppp_state = PPP_STATE_STOPPED ;
379390 send_status_notification ();
380391
@@ -667,7 +678,7 @@ static void ppp_data_passing_thread(void*, void*, void*)
667678 const int poll_ret = zsock_poll (fds , ARRAY_SIZE (fds ), -1 );
668679
669680 if (poll_ret <= 0 ) {
670- LOG_ERR ("Sockets polling failed (%d, %d)." , poll_ret , errno );
681+ LOG_ERR ("Sockets polling failed (%d, %d). Restart. " , poll_ret , - errno );
671682 delegate_ppp_event (PPP_RESTART , PPP_REASON_DEFAULT );
672683 return ;
673684 }
@@ -678,15 +689,20 @@ static void ppp_data_passing_thread(void*, void*, void*)
678689 if (!revents ) {
679690 continue ;
680691 }
692+
693+ if (src == EVENT_FD_IDX ) {
694+ LOG_DBG ("Exit thread." );
695+ return ;
696+ }
697+
681698 if (!(revents & ZSOCK_POLLIN )) {
682- /* ZSOCK_POLLERR/ZSOCK_POLLNVAL happen when the sockets are closed
683- * or when the connection goes down.
684- */
685- if ((revents ^ ZSOCK_POLLERR ) && (revents ^ ZSOCK_POLLNVAL )) {
686- LOG_WRN ("Unexpected event 0x%x on %s socket." ,
699+ /* ZSOCK_POLLERR comes when the connection goes down (AT+CFUN=0). */
700+ if (revents ^ ZSOCK_POLLERR ) {
701+ LOG_WRN ("Unexpected event 0x%x on %s socket. Stop." ,
687702 revents , ppp_socket_names [src ]);
703+ } else {
704+ LOG_DBG ("Connection down. Stop." );
688705 }
689- LOG_DBG ("Socket closed or connection down." );
690706 delegate_ppp_event (PPP_STOP , PPP_REASON_DEFAULT );
691707 return ;
692708 }
@@ -696,7 +712,7 @@ static void ppp_data_passing_thread(void*, void*, void*)
696712 if (len <= 0 ) {
697713 if (len != -1 || (errno != EAGAIN && errno != EWOULDBLOCK )) {
698714 LOG_ERR ("Failed to receive data from %s socket (%d, %d)." ,
699- ppp_socket_names [src ], len , errno );
715+ ppp_socket_names [src ], len , - errno );
700716 }
701717 continue ;
702718 }
@@ -722,7 +738,7 @@ static void ppp_data_passing_thread(void*, void*, void*)
722738 zsock_sendto (fds [dst ].fd , ppp_data_buf , len , 0 , dst_addr , addrlen );
723739 if (send_ret == -1 ) {
724740 LOG_ERR ("Failed to send %zd bytes to %s socket (%d)." ,
725- len , ppp_socket_names [dst ], errno );
741+ len , ppp_socket_names [dst ], - errno );
726742 } else if (send_ret != len ) {
727743 LOG_ERR ("Only sent %zd out of %zd bytes to %s socket." ,
728744 send_ret , len , ppp_socket_names [dst ]);
0 commit comments