17
17
#include <zephyr/net/ethernet.h>
18
18
#include <zephyr/net/net_if.h>
19
19
#include <zephyr/net/ppp.h>
20
+ #include <zephyr/posix/sys/eventfd.h>
20
21
#include <zephyr/posix/sys/socket.h>
21
22
#include <zephyr/random/random.h>
22
23
#include <assert.h>
@@ -88,11 +89,13 @@ static unsigned int ppp_pdn_cid;
88
89
enum {
89
90
ZEPHYR_FD_IDX , /* Raw Zephyr socket to pass data to/from the PPP link. */
90
91
MODEM_FD_IDX , /* Raw modem socket to pass data to/from the LTE link. */
92
+ EVENT_FD_IDX , /* Eventfd to signal the PPP thread. */
91
93
PPP_FDS_COUNT
92
94
};
93
95
const char * const ppp_socket_names [PPP_FDS_COUNT ] = {
94
96
[ZEPHYR_FD_IDX ] = "Zephyr ",
95
- [MODEM_FD_IDX ] = "modem "
97
+ [MODEM_FD_IDX ] = "modem ",
98
+ [EVENT_FD_IDX ] = "eventfd "
96
99
};
97
100
static int ppp_fds [PPP_FDS_COUNT ] = { -1 , -1 };
98
101
@@ -117,7 +120,7 @@ static bool open_ppp_sockets(void)
117
120
ppp_fds [ZEPHYR_FD_IDX ] = zsock_socket (AF_PACKET , SOCK_DGRAM | SOCK_NATIVE ,
118
121
htons (ETH_P_ALL ));
119
122
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 );
121
124
return false;
122
125
}
123
126
@@ -129,13 +132,13 @@ static bool open_ppp_sockets(void)
129
132
ret = zsock_bind (ppp_fds [ZEPHYR_FD_IDX ],
130
133
(const struct sockaddr * )& ppp_zephyr_dst_addr , sizeof (ppp_zephyr_dst_addr ));
131
134
if (ret < 0 ) {
132
- LOG_ERR ("Failed to bind Zephyr socket (%d)." , errno );
135
+ LOG_ERR ("Failed to bind Zephyr socket (%d)." , - errno );
133
136
return false;
134
137
}
135
138
136
139
ppp_fds [MODEM_FD_IDX ] = zsock_socket (AF_PACKET , SOCK_RAW , 0 );
137
140
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 );
139
142
return false;
140
143
}
141
144
@@ -151,6 +154,12 @@ static bool open_ppp_sockets(void)
151
154
return false;
152
155
}
153
156
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
+
154
163
return true;
155
164
}
156
165
@@ -162,7 +171,7 @@ static void close_ppp_sockets(void)
162
171
}
163
172
if (zsock_close (ppp_fds [i ])) {
164
173
LOG_WRN ("Failed to close %s socket (%d)." ,
165
- ppp_socket_names [i ], errno );
174
+ ppp_socket_names [i ], - errno );
166
175
}
167
176
ppp_fds [i ] = -1 ;
168
177
}
@@ -218,7 +227,7 @@ static void delegate_ppp_event(enum ppp_action action, enum ppp_reason reason)
218
227
LOG_DBG ("PPP %s, reason: %d" , ppp_action_str (event .action ), event .reason );
219
228
220
229
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." );
222
231
}
223
232
224
233
k_work_submit_to_queue (& slm_work_q , & ppp_work .work );
@@ -371,10 +380,12 @@ static int ppp_stop(enum ppp_reason reason)
371
380
372
381
net_if_carrier_off (ppp_iface );
373
382
374
- close_ppp_sockets ();
375
-
383
+ /* Close the thread. */
384
+ eventfd_write ( ppp_fds [ EVENT_FD_IDX ], 1 );
376
385
k_thread_join (& ppp_data_passing_thread_id , K_SECONDS (1 ));
377
386
387
+ close_ppp_sockets ();
388
+
378
389
ppp_state = PPP_STATE_STOPPED ;
379
390
send_status_notification ();
380
391
@@ -667,7 +678,7 @@ static void ppp_data_passing_thread(void*, void*, void*)
667
678
const int poll_ret = zsock_poll (fds , ARRAY_SIZE (fds ), -1 );
668
679
669
680
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 );
671
682
delegate_ppp_event (PPP_RESTART , PPP_REASON_DEFAULT );
672
683
return ;
673
684
}
@@ -678,15 +689,20 @@ static void ppp_data_passing_thread(void*, void*, void*)
678
689
if (!revents ) {
679
690
continue ;
680
691
}
692
+
693
+ if (src == EVENT_FD_IDX ) {
694
+ LOG_DBG ("Exit thread." );
695
+ return ;
696
+ }
697
+
681
698
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." ,
687
702
revents , ppp_socket_names [src ]);
703
+ } else {
704
+ LOG_DBG ("Connection down. Stop." );
688
705
}
689
- LOG_DBG ("Socket closed or connection down." );
690
706
delegate_ppp_event (PPP_STOP , PPP_REASON_DEFAULT );
691
707
return ;
692
708
}
@@ -696,7 +712,7 @@ static void ppp_data_passing_thread(void*, void*, void*)
696
712
if (len <= 0 ) {
697
713
if (len != -1 || (errno != EAGAIN && errno != EWOULDBLOCK )) {
698
714
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 );
700
716
}
701
717
continue ;
702
718
}
@@ -722,7 +738,7 @@ static void ppp_data_passing_thread(void*, void*, void*)
722
738
zsock_sendto (fds [dst ].fd , ppp_data_buf , len , 0 , dst_addr , addrlen );
723
739
if (send_ret == -1 ) {
724
740
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 );
726
742
} else if (send_ret != len ) {
727
743
LOG_ERR ("Only sent %zd out of %zd bytes to %s socket." ,
728
744
send_ret , len , ppp_socket_names [dst ]);
0 commit comments