Skip to content

Commit

Permalink
applications: serial_lte_modem: UDP server fixes.
Browse files Browse the repository at this point in the history
- Add port to XRECVFROM, so that we can respond.
- Update documentation.

Signed-off-by: Markus Lassila <[email protected]>
  • Loading branch information
MarkusLassila authored and rlubos committed Oct 11, 2023
1 parent f6743c0 commit 7b038c6
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 38 deletions.
7 changes: 4 additions & 3 deletions applications/serial_lte_modem/doc/SOCKET_AT_commands.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1160,20 +1160,21 @@ Response syntax

::

#XRECVFROM: <size>,<ip_addr>
#XRECVFROM: <size>,"<ip_addr>",<port>
<data>

* The ``<data>`` value is a string that contains the data being received.
* The ``<size>`` value is an integer that represents the actual number of bytes received.
* The ``<ip_addr>`` value is a string that represents the IPv4 or IPv6 address of remote peer.
* The ``<ip_addr>`` value is a string that represents the IPv4 or IPv6 address of the remote peer.
* The ``<port>`` value is an integer that represents the UDP port of the remote peer.

Example
~~~~~~~~

::

AT#XRECVFROM=10
#XRECVFROM: 7,"192.168.1.100"
#XRECVFROM: 7,"192.168.1.100",24210
Test OK
OK

Expand Down
54 changes: 22 additions & 32 deletions applications/serial_lte_modem/doc/slm_testing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -279,7 +279,7 @@ UDP client
#XSENDTO: 8
OK
**AT#XRECVFROM=0**
#XRECVFROM: 14
#XRECVFROM: 14,"<*IP address*>",<*port*>
PONG: Test UDP
OK
Expand Down Expand Up @@ -816,22 +816,22 @@ To act as a UDP server, |global_private_address|
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.bind(local)
print("Sending: 'Hello, UDP#1!")
s.sendto("Hello, UDP#1!", host)
s.sendto(b"Hello, UDP#1!", host)
time.sleep(1)
print("Sending: 'Hello, UDP#2!")
s.sendto("Hello, UDP#2!", host)
s.sendto(b"Hello, UDP#2!", host)
data, address = s.recvfrom(1024)
print(data)
print(address)
print("Sending: 'Hello, UDP#3!")
s.sendto("Hello, UDP#3!", host)
s.sendto(b"Hello, UDP#3!", host)
time.sleep(1)
print("Sending: 'Hello, UDP#4!")
s.sendto("Hello, UDP#4!", host)
s.sendto(b"Hello, UDP#4!", host)
time.sleep(1)
print("Sending: 'Hello, UDP#5!")
s.sendto("Hello, UDP#5!", host)
s.sendto(b"Hello, UDP#5!", host)
data, address = s.recvfrom(1024)
print(data)
print(address)
Expand All @@ -848,7 +848,7 @@ To act as a UDP server, |global_private_address|
:class: highlight
**AT#XSOCKET=1,2,1**
#XSOCKET: 2,2,17
#XSOCKET: 0,2,17
OK
**AT#XBIND=**\ *1234*
Expand All @@ -863,12 +863,12 @@ To act as a UDP server, |global_private_address|
:class: highlight
**AT#XRECVFROM=0**
#XRECVFROM: 13
#XRECVFROM: 13,"<*IP address*>",<*port*>
Hello, UDP#1!
OK
**AT#XRECVFROM=0**
#XRECVFROM: 13
#XRECVFROM: 13,"<*IP address*>",<*port*>
Hello, UDP#2!
OK
Expand All @@ -877,47 +877,37 @@ To act as a UDP server, |global_private_address|
OK
**AT#XRECVFROM=0**
#XRECVFROM: 13
#XRECVFROM: 13,"<*IP address*>",<*port*>
Hello, UDP#3!
OK
**AT#XRECVFROM=0**
#XRECVFROM: 13
#XRECVFROM: 13,"<*IP address*>",<*port*>
Hello, UDP#4!
OK
**AT#XRECVFROM=0**
#XRECVFROM: 13
#XRECVFROM: 13,"<*IP address*>",<*port*>
Hello, UDP#5!
OK
**AT#XSENDTO="**\ *example.com*\ **",**\ *1234*\ **,"UDP3/4/5 received"**
#XSENDTO: 17
OK
Note that you will get an error message if a UDP packet is lost.
For example, this error indicates that a packet is lost in the downlink to the nRF91 Series DK:

.. parsed-literal::
:class: highlight
**AT#XRECVFROM=0**
#XSOCKET: -60
ERROR
#. Observe the output of the Python script::

$ python client_udp.py

Sending: 'Hello, UDP#1!
Sending: 'Hello, UDP#2!
UDP1/2 received
('000.000.000.00', 1234)
b'UDP1/2 received'
('000.000.000.00', 1234, 0, 0)
Sending: 'Hello, UDP#3!
Sending: 'Hello, UDP#4!
Sending: 'Hello, UDP#5!
UDP3/4/5 received
('000.000.000.00', 1234)
b'UDP3/4/5 received'
('000.000.000.00', 1234, 0, 0)
Closing connection

#. Close the socket.
Expand All @@ -942,7 +932,7 @@ To act as a UDP server, |global_private_address|
OK
**AT#XUDPSVR=1,**\ *1234*
#XUDPSVR: 2,"started"
#XUDPSVR: 0,"started"
OK
#. Run the :file:`client_udp.py` script to start sending data to the server.
Expand Down Expand Up @@ -978,13 +968,13 @@ To act as a UDP server, |global_private_address|

Sending: 'Hello, UDP#1!
Sending: 'Hello, UDP#2!
UDP1/2 received
('000.000.000.00', 1234)
b'UDP1/2 received'
('000.000.000.00', 1234, 0, 0)
Sending: 'Hello, UDP#3!
Sending: 'Hello, UDP#4!
Sending: 'Hello, UDP#5!
UDP3/4/5 received
('000.000.000.00', 1234)
b'UDP3/4/5 received'
('000.000.000.00', 1234, 0, 0)
Closing connection

#. Close the socket.
Expand All @@ -993,7 +983,7 @@ To act as a UDP server, |global_private_address|
:class: highlight
**AT#XUDPSVR=0**
#XUDPSVR: "stopped"
#XUDPSVR: 0,"stopped"
OK
TLS server
Expand Down
11 changes: 8 additions & 3 deletions applications/serial_lte_modem/src/slm_at_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -872,15 +872,20 @@ static int do_recvfrom(int timeout, int flags)
LOG_WRN("recvfrom() return 0");
} else {
char peer_addr[NET_IPV6_ADDR_LEN] = {0};
uint16_t peer_port = 0;

if (remote.sa_family == AF_INET) {
(void)inet_ntop(AF_INET, &((struct sockaddr_in *)&remote)->sin_addr,
peer_addr, sizeof(peer_addr));
peer_addr, sizeof(peer_addr));
peer_port = ntohs(((struct sockaddr_in *)&remote)->sin_port);

} else if (remote.sa_family == AF_INET6) {
(void)inet_ntop(AF_INET6, &((struct sockaddr_in6 *)&remote)->sin6_addr,
peer_addr, sizeof(peer_addr));
peer_addr, sizeof(peer_addr));
peer_port = ntohs(((struct sockaddr_in6 *)&remote)->sin6_port);
}
rsp_send("\r\n#XRECVFROM: %d,\"%s\"\r\n", ret, peer_addr);

rsp_send("\r\n#XRECVFROM: %d,\"%s\",%d\r\n", ret, peer_addr, peer_port);
data_send(slm_data_buf, ret);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ Serial LTE modem
* ``#XGPSDEL`` AT command to disallow deleting local clock (TCXO) frequency offset data because it is an internal value that should not be deleted when simulating a cold start.
* Socket option ``TLS_DTLS_HANDSHAKE_TIMEO`` to a new name value.
* ``#XTCPSVR`` connection closure status and documentation.
* ``#XRECVFROM`` to include the port of the peer.

* Removed:

Expand Down

0 comments on commit 7b038c6

Please sign in to comment.