Feedback
Feedback

If you are having issues with the exercises, please create a ticket on DevZone: devzone.nordicsemi.com
Click or drag files to this area to upload. You can upload up to 2 files.

Network programming

Any firmware running on the application core of the nRF9160 SiP that wants to utilize the modem core must interface with it. And before we cover how to interface with the modem, it is useful to have a basic understanding of network programming using Internet sockets, or better-explained socket programming.

Sockets

Definition

Socket: A socket is a software structure that serves as an endpoint for sending and receiving data across a network.

A common scenario: A server runs on a computer and has a socket that is bound to a specific port number. The server listens to the socket, waiting for a client to make a connection request. On the client-side, it knows the hostname of the computer and the aforementioned port number and it will try to make a connection request by binding to a local port number that it will use during the connection. If the server accepts the connection, it will get a new socket bound to the same local port, with its remote endpoint set to the address and port of the client. Then it will keep listening to the original socket for new connection requests while handling the connection. A socket is also created on the client-side, and the client and server can communicate by writing to or reading from their respective sockets.

The structure of a socket is determined by the API for the networking architecture. Internet sockets are commonly based on the Berkeley sockets API, also known as BSD sockets.

More on this

We highly recommend Beej’s Guide to Network Programming for a basic, and easy-to-understand introduction to sockets.

Transport layer

The nRF9160 gives you the freedom to choose between a number of data delivery options. You can choose to use IP packets or non-IP packets. In addition, when choosing IP packets, you can further specify the complexity level by choosing either TCP or UDP.

Recalling the nRF9160 communication stack from Lesson 1, these two are what make up the transport layer, see below.

nRF9160 modem stack

The nRF9160 also supports non-IP data delivery (NIDD) but this is not recommended and is beyond the scope of this course.

TCP and UDP

The two communication protocols that make up the transport layer in the nRF9160 communication stack is the Transmission Control Protocol (TCP) and the User Datagram Protocol (UDP).

TCP is a connection-oriented protocol that uses a three-way handshake to establish a connection between server and client before beginning to send data. It provides reliable, ordered and error-checked delivery of a stream of bytes.

UDP is a connectionless protocol that does not require an established connection, but rather transmits packets (called datagrams) directly to the receiver. As opposed to TCP, this does not provide ordered or error-checked delivery.

The two protocols have a number of advantages and disadvantages that become relevant when choosing which one to use in your application. The following table summarizes the main advantages and disadvantages of both TCP and UDP.

AdvantagesDisadvantages
TCPData retransmission
Congestion control
In-order delivery
Error detection
Slow handshake
Larger overhead
Repetition if data is not received
Not suited for NB-IoT
UDPNo handshake is needed
Better suited for low-power devices
Not guaranteed delivery (can be enforced by the application layer)
Not all major cloud vendors support UDP yet

Being a simpler protocol, UDP has fewer overhead bits and no handshake. This creates a number of advantages such as the lower power consumption associated with the absence of handshakes. However, this can lead to a higher data loss and packets can arrive out of order but can be solved by the application protocol.

On the other hand, TCP is a more complex protocol. It offers packet awareness, which means the transceivers are aware of the order of packets being sent so any lost packet can be detected and re-transmitted and any disruption in the packet order can be corrected. Of course, this results in increased overhead and power consumption.

Definition

Handshake: An initial negotiation between client and server that establishes the parameters of their transactions.

Transport layer security

To secure the transport layer, the nRF9160 modem firmware offers transport layer security protocols that provide confidentiality, integrity, and authenticity to the communication.

  • Confidentiality: The communication cannot be read by third parties along the network path.
  • Integrity: The communication cannot be altered by third parties along the network path
  • Authenticity: Verifies the identity of both sides of the communication

On top of the transport layer in the nRF9160 communication stack, we find the layer for the transport layer security protocols, TLS and DTLS.

TLS and DTLS

The two protocols that make up this layer are the Transport Layer Security (TLS) and Datagram Transport Layer Security (DTLS), which provide security for TCP and UDP respectively.

TLS is an encryption protocol designed to provide security over a TCP connection. When establishing a connection between the server and client, the client requests a TLS connection. One of the main ways of achieving this is to use a different port number for TLS connections, as we will see in lesson 4.

DTLS is an implementation of TLS that is specifically designed to be used over datagram-based communication and is intended to provide similar security guarantees. To do this, DTLS requires a handshake prior to transmitting messages, where each handshake packet has a specific sequence number to solve the fact that UDP does not provide ordered delivery.

For the application, the difference between using TLS and DTLS is insignificant, as both protocols provide the same type of security.

Register an account
Already have an account? Log in
(All fields are required unless specified optional)

  • 8 or more characters
  • Upper and lower case letters
  • At least one number or special character

Forgot your password?
Enter the email associated with your account, and we will send you a link to reset your password.