Nokia M2M Platform M2M System Protocol 2 Socket Interface: User Manual
Nokia M2M Platform M2M System Protocol 2 Socket Interface: User Manual
0 9312023
SOCKET INTERFACE
USER MANUAL
Contents
Acronym/term Description
AM Application Module
ANSI American National Standards Institute
API Application Programming Interface
CHAP Challenge Handshake Authentication Protocol
DLL Data Link Layer
IP Internet Protocol
KB Kilobyte (equivalent to 1024 bytes)
M2M Machine-to-Machine (Man-to-Machine, Machine-to-
Man)
NLL Network Link Layer
OS Operating System
OSI Open Systems Interconnection
Platform Operating system and communication services
PPP Point-to-Point Protocol
1/46
DEFINITIONS
Term Description
Socket API M2M System Protocol 2 API for socket operations
M2M System M2M System Protocol 2 API for serial link control
Protocol 2 Control
API
Wireless Link Control M2M System Protocol 2 API for GSM link control
API
Software wrapper Used between software elements to isolate platform-
specific features
2/46
1. ABOUT THIS DOCUMENT
This document describes, from application software point of view, how to
integrate and use the M2M System Protocol 2. In addition to providing an
overview of the M2M System Protocol 2 software, the document includes
instructions on how to integrate the M2M System Protocol 2 with application
software and how to use the APIs of the M2M System Protocol 2 socket
interface. The document also includes an example of the M2M System Protocol
2 socket usage.
3/46
2. INTRODUCTION
The M2M System Protocol 2 software provides a socket-programming interface
for application software. The socket interface may be used
Because the IP protocol stack placed at the disposal of the socket interface is
located on the Nokia M2M module, application software must have a
connection to the Nokia M2M module services. As illustrated in Figure 1, the
M2M System Protocol 2 software is used to tunnel IP traffic between the AM
and the Nokia M2M module via a serial connection.
The serial connection is a physical connection between the AM and Nokia M2M
module. It is connected between a serial port of the AM and the M2M System
Connector of the Nokia M2M module. The serial connection defines the lowest
layer transmission format using a point-to-point type of serial mode
transmission as described in the Nokia M2M Platform M2M System Protocol 2
Specification.
4/46
The wireless connectivity makes it possible to connect the Nokia M2M module
to a remote peer over a GSM network. Thus application software can be
connected to sockets located on remote machines. For more information on the
wireless connectivity solutions available via the Nokia M2M Platform, see the
Nokia M2M Platform Software Developer’s Guide.
5/46
3. THE M2M SYSTEM PROTOCOL 2 SOFTWARE
DESCRIPTION
The M2M System Protocol 2 is based on the Open Systems Interconnection
(OSI) Reference Model. It is a set of layers organized in a hierarchical structure,
where each layer uses the layer below and provides services for the layer
above. When data is sent from application software to a network, it flows
downward the stack layer by layer until it has passed through the whole stack.
When the data reaches the bottom layer it is passed to a network. The data
received from the network flows the stack in an opposite direction. The software
structure of the M2M System Protocol 2 is illustrated in Figure 2.
Application software
Socket interface
Network Link Layer Operating Operating
system system
Data Link Layer wrapper services
Serial communication wrapper
Serial communication services
6/46
3.4 PLATFORM WRAPPERS
The M2M System Protocol 2 is designed to operate in heterogeneous
environments where a variety of operating systems and different types of serial
communication hardware are used. The operating system and serial
communication wrappers separate platform-specific functionality from the
protocol stack in order to enhance the portability of the M2M System Protocol 2
implementation to different environments. For more information on the M2M
System Protocol 2 platform wrapping, see Chapter 4.
7/46
4. INTEGRATING THE M2M SYSTEM PROTOCOL 2 WITH
APPLICATION SOFTWARE
This chapter describes how the M2M System Protocol 2 is integrated as a part
of application software.
Application software uses the M2M System Protocol 2 functions via the socket
interface. An application that uses the M2M System Protocol 2 functionality
must include the header file sp2_socket.h.
The protocol stack provides the core functionality of the M2M System Protocol
2. It is recommended that application software is designed not to use the
protocol stack directly.
The platform wrappers contain the platform-specific implementation of the
operating system and serial communication services for the M2M System
Protocol 2. The platform wrappers must be configured separately for each AM
platform.
8/46
Note: The M2M System Protocol 2 file names and external identifiers (except
‘system’ wrappers) are prefixed with ‘sp2’ to avoid name conflicts.
The M2M System Protocol 2 software building process includes the following
steps:
1. Select platform wrapper source files for the M2M System Protocol 2.
It is up to the application software developer to implement the AM platform
wrappers if they are not available. For more information on porting the M2M
System Protocol 2, see Chapter 4.3.
2. Compile all the M2M System Protocol 2 source files.
Compile the selected platform wrappers and all the socket interface/protocol
stack source files listed in Table 1. The socket interface and protocol stack
source files should compile without modifications.
3. Link the compiled files to application software
Once the M2M System Protocol source files are compiled (and optionally
linked as a library), they are linked with application software to produce the
final binary image.
Tip: Chapter 6.2.2 includes a sample makefile that can be used to get started with
building the M2M System Protocol 2 software.
9/46
When the M2M System Protocol 2 is started it will create the SenderThread,
ReceiverThread, and MsgQueueThread. The SenderThread and
ReceiverThread are high priority serial drivers used for packet sending and
receiving, respectively. The MsgQueueThread takes care of serial message
delivery to/from the M2M System Protocol protocol stack and its priority is
application dependent.
The M2M System Protocol 2 memory requirements are approx. 64 KB ROM
(without system libraries) and approx. 4 KB RAM. In addition, some memory is
needed for dynamic memory and thread stacks. Dynamic memory size ranges
from a few kilobytes to a maximum of 30 kilobytes; depending mainly on serial
buffer sizes. The memory requirement for each thread stack is a few kilobytes.
Note: The Nokia M2M Platform M2M System Protocol 2 Specification also defines
a minimum implementation for resource-constrained AM devices. The Nokia M2M
module supports the minimum implementation.
10/46
5. THE M2M SYSTEM PROTOCOL 2 SOCKET INTERFACE
The socket interface is divided into the following APIs: Socket API, M2M
System Protocol 2 Control API, and Wireless Link Control API. This chapter
describes the APIs and lists the related methods, parameters, and return
values. In addition, the chapter provides message sequence diagrams, which
illustrate message exchange caused by API calls between the AM and Nokia
M2M module.
For more detailed description of the messages, refer to the Nokia M2M Platform
M2M System Protocol 2 Specification.
Note: The M2M System Protocol 2 must be started before making any other M2M
System Protocol 2 API calls. For more information on how to start the M2M
System Protocol 2, see Chapter 5.2.1.
sp2_socket()
Create a socket
status()
11/46
Parameter Description
Type Stream or datagram
Protocol TCP or UDP
socket = sp2_socket(addrFormat,type,protocol);
if (socket != SP2_SOCKET_ERROR)
sp2_bind()
Bind the
socket
status()
12/46
Parameter Description
Address The address of the socket to be bound
Address length The length of the socket address in bytes
13/46
Application software M2M System Protocol 2 Nokia M2M module software
sp2_listen()
SOCKET()
Create a socket
SOCKET_RESP()
* err_status()
BIND()
Start listening
to the socket
LISTEN_RESP()
status()
14/46
Return value Description
SP2_SOCKET_ERROR Connection failure
status = sp2_listen(socketID,queueLen);
if (status == SP2_SOCKET_OK)
sp2_accept()
Listening to incoming
Wait for an accept connection requests
notification
ACCEPT_NOTIFICATION()
ACCEPT_NOTIFICATION_RESP()
status()
15/46
Return value Description
Socket ID A handle to an incoming socket
SP2_SOCKET_ERROR Acceptance failure
sp2_connect()
SOCKET()
Create a socket
SOCKET_RESP()
* err_status()
CONNECT()
CONNECT_RESP()
status()
16/46
Parameter Description
Socket ID A handle to a socket to be connected
Address pointer The address of a socket to which the existing socket is
connected
Address length The length of the socket address in bytes
sp2_send / sp2_sendto()
SEND / SENDTO()
17/46
Figure 8. Sending data to a socket
The parameters of the sp2_send function call are described in Table 13.
The parameters of the sp2_sendto function call are described in Table 14.
The return values of both the sp2_send and sp2_sendto function calls are
described in Table 15.
Table 15. The sp2_send and sp2_sendto function call return values
Return value Description
Bytes written Number of bytes written successfully
SP2_SOCKET_ERROR Failure to write data
bytesWritten = sp2_send(socketID,buff,nbytes,flags);
if (bytesWritten != SP2_SOCKET_ERROR)
18/46
5.1.7 Receiving data from a socket (sp2_receive)
The sp2_receive function instructs the Nokia M2M module to read data from
a socket. The sp2_receive will block and wait until data is available. The
function call is illustrated in Figure 9.
sp2_receive()
RECEIVE()
RECEIVE_RESP()
* err_status()
Wait until data is available
RECEIVE_NOTIFICATION()
RECEIVE_NOTIFICATION_RESP()
status / data()
19/46
DWORD socketID; // a handle to the socket
BYTE buff[DATA_LENGTH]; // buffer to store received data
DWORD nbytes = DATA_LENGTH; // number of bytes to be read
DWORD flags = 0; // no flags used
DWORD bytesRead; // number of bytes read successfully
bytesRead = sp2_receive(socketID,buff,nbytes,flags);
if (bytesRead != SP2_SOCKET_ERROR)
sp2_close()
CLOSE()
20/46
DWORD socketID; // a handle to the socket
DWORD status; // a return value from the function
status = sp2_close(socketID);
if (status == SP2_SOCKET_OK)
sp2_gethostbyname()
GETHOSTBYNAME_REQ()
host = sp2_gethostbyname(hostname);
if (host != NULL)
21/46
{
// use the host information and then release allocated memory
free(host->h_name); // name string must be released by caller
free(host); // host struct must be released by caller finally
}
Convert a 32-bit value from host byte order to network byte order:
Convert a 16-bit value from host byte order to network byte order:
Convert a 32-bit value from network byte order to host byte order:
Convert a 16-bit value from network byte order to host byte order:
Convert an IPv4 address from a dotted decimal string to 32-bit network byte
ordered value:
Convert a 32-bit network byte ordered IPv4 address to dotted decimal string:
22/46
Note: The sp2_start function must be executed and the
sp2_hLinkReadyEvent must be received before any other M2M System
Protocol 2 API call can be made.
sp2_start()
sp2_reset()
RESET()
* Clear packet sequence numbers, abort pending transactions, and close all sockets.
23/46
Note: Typically only the port and baud rate parameters are modified. The
recommended values for the N1, N3, and T3 parameters are 255, 10, and 5,
respectively. For more information on modifying the N1, N3, and T3 parameter
values, see the Nokia M2M Platform M2M System Protocol 2 Specification.
status = sp2_start(n1,n3,t3,port,baudrate);
if (status == SP2_OK)
Note: After this function is called and the stop event is received, the M2M
System Protocol 2 resources are freed, and the protocol must be restarted before
issuing any further API requests.
24/46
Application software M2M System Protocol 2 Nokia M2M module software
sp2_stop()
RESET()
status = sp2_stop();
if (status == SP2_OK)
25/46
Application software M2M System Protocol 2 Nokia M2M module software
sp2_link_open()
LINK_OPEN()
LINK_OPEN_RESP()
* err_status() Open the link
LINK_NOTIFICATION()
LINK_NOTIFICATION_RESP()
status()
linkID = sp2_link_open(connectionID);
if (linkID != SP2_LINK_ERROR)
26/46
5.3.2 Closing a wireless link (sp2_link_close)
The sp2_link_close function is used to close a wireless link over a GSM
network via the Nokia M2M module. The function call is depicted in Figure 15.
Note: All remote sockets must be closed before closing the wireless link.
sp2_link_close()
LINK_CLOSE()
LINK_CLOSE_RESP()
* err_status / status()
Close the link
LINK_NOTIFICATION()
LINK_NOTIFICATION_RESP()
status()
Note: If the link is already closed, the sp2_link_close call will return immediately,
and LINK_NOTIFICATION is not used.
27/46
DWORD linkID; // a handle to the link
if (sp2_link_close(linkID) == SP2_LINK_OK)
28/46
6. EXAMPLE OF M2M SYSTEM PROTOCOL 2 SOCKET
USAGE
1. Open a serial link to the Nokia M2M module via the M2M System Protocol 2
Control API.
2. Reset the protocol via the M2M System Protocol 2 Control API.
3. Open a wireless GSM link via the Wireless Link Control API.
5. Close the wireless link via the Wireless Link Control API
6. Close the serial link to the Nokia M2M module via the M2M System Protocol 2
Control API.
A typical client socket message sequence is illustrated in Figure 16.
29/46
Socket client M2M System Protocol 2
Create a socket()
** Connect the socket()
Data sending and receiving()
Close the socket()
* Close the wireless link()
* Wireless link closed notification()
Note: In case the wireless link breaks all new and pending socket functions using
the link in question will fail. To execute the socket operations that were
interrupted, the wireless link has to be re-established and the sockets have to be
closed and recreated.
Typically application software that uses a M2M System Protocol 2 server socket
has to:
1. Open a serial link to the Nokia M2M module via the M2M System Protocol 2
Control API.
2. Reset the M2M System Protocol 2 via the M2M System Protocol 2 Control
API.
30/46
4. Communicate with the client via the Socket API.
5. Close the serial link to the Nokia M2M module via the M2M System Protocol 2
Control API.
A typical server socket message sequence is illustrated in Figure 17.
Note: In case the serial link between the AM and Nokia M2M module is re-
established after being broken, the M2M System Protocol 2 will reset the protocol;
all pending operations are interrupted, and all sockets are closed.
31/46
HelloWorld Client M2M System Protocol 2 HelloWorld Server
32/46
Note: An example configuration (for the Nokia 12 GSM module) is presented in
Appendix: Nokia 12 GSM module configuration for the Hello World application.
When executing the Hello World application, the server must be initialized
first; it must be ready to accept incoming socket connections from the client.
The Hello World application can be executed, for example, from the
command prompt.
When the Hello World application is executed without errors, the client will
display the text “Hello World successful!” on the screen. If an error occurs, the
client will display an error message with a failure explanation.
#include <stdio.h>
// Size of buffer that holds text messages: "SAY" and "HELLO WORLD!"
#define messageBufferLen 80
static const WORD TcpPortThatServerIsListening = 0x5555;
33/46
// Client functionality
static BOOL HelloWorldClient(void);
// Server functionality
static BOOL HelloWorldServer(void);
// The function for writing a '\0' char terminated text string to a socket
static BOOL sendMessage(DWORD socket, const char *msg);
// The function for reading a '\0' char terminated string from a socket
static BOOL receiveMessage(DWORD socket, char *messageBuffer, int size);
// Cleanup and exit Hello World (just a convenience function)
static void cleanExit(int statusId, char *statusName);
if (argc != 3)
{
cleanExit(0,"Usage: helloworld client/server <com port>");
}
if (strcmp(argv[1],"client") == 0)
{
isClient = TRUE;
} else
{
isClient = FALSE; // i.e. isServer
}
COMPort = (BYTE)atoi(argv[2]);
if (isClient)
{
printf("HelloWorld Client at COM port %d\r\n",COMPort);
} else
{
printf("HelloWorld Server at COM port %d\r\n",COMPort);
}
34/46
{
cleanExit(40,"ERROR: hLinkReadyEvent not OK");
}
// Please note that wireless link is not needed for local sockets
if ((linkID = sp2_link_open(connectionID)) == SP2_LINK_ERROR)
{
printf("sp2_link_open failed\n");
return FALSE;
}
35/46
socket = sp2_socket(SP2_AF_INET, SP2_SOCK_STREAM, SP2_IPPROTO_TCP);
if(socket != SP2_SOCKET_ERROR)
{
socketAddress.sin_family = SP2_AF_INET;
socketAddress.sin_port = sp2_htons(TcpPortThatServerIsListening);
socketAddress.sin_addr = sp2_inet_addr("10.0.0.1");
if(sp2_connect(socket,(sp2_sockaddr*)&socketAddress,
sizeof(socketAddress)) != SP2_SOCKET_ERROR)
{
const char *sayString = "SAY";
printf("Client: send message: %s\n", sayString);
if (sendMessage(socket, sayString))
{
char messageBuffer[messageBufferLen];
if (receiveMessage(socket, messageBuffer, messageBufferLen))
{
printf("Client: got message: %s\n ", messageBuffer);
if (strcmp(messageBuffer,"HELLO WORLD!") == 0)
{
success = TRUE;
}
} else
{
printf ("ERROR: Client receiveMessage\n");
}
} else
{
printf ("ERROR: Client sp2_send\n");
}
} else
{
printf ("ERROR: Client sp2_connect\n");
}
if (sp2_close(socket) == SP2_SOCKET_ERROR)
{
printf ("ERROR: Client sp2_close\n");
}
} else
{
printf ("ERROR: Client sp2_socket\n");
}
return success;
}
if(serverSocket != SP2_SOCKET_ERROR)
{
socketAddress.sin_family = SP2_AF_INET;
36/46
socketAddress.sin_port = sp2_htons(TcpPortThatServerIsListening);
socketAddress.sin_addr = 0;
if (sp2_close(serverSocket) == SP2_SOCKET_ERROR)
{
printf ("ERROR: Server socket sp2_close\n");
return FALSE;
}
37/46
return TRUE;
}
// The function for writing a '\0' char terminated text string to a socket
BOOL sendMessage(DWORD socket, const char *msg)
{
int bytesSent = 0;
int size = strlen(msg)+1;
while (bytesSent != size)
{
// Send data to the M2M System Protocol 2.
// Hello World messages are very short and thus probably sent
// in one request. However, the number of bytes written is checked
// and sending is looped until all the data has been sent.
int n = sp2_send(socket, msg+bytesSent, size-bytesSent, 0x00);
if (n == SP2_SOCKET_ERROR)
{
printf("ERROR: sp2_send\n");
return FALSE;
}
bytesSent += n;
Yield(); // Just to let other threads run
}
return TRUE;
}
// The function for reading a '\0' char terminated string from a socket
BOOL receiveMessage(DWORD socket, char *messageBuffer, int size)
{
int bytesReceived = 0;
BOOL isMessageReceived = FALSE;
if (n == SP2_SOCKET_ERROR)
{
printf("ERROR: sp2_receive\n");
return isMessageReceived;
} else
{ // Check whether the '\0' char was received.
int i;
for (i=bytesReceived; i<bytesReceived+n; i++)
{
if (messageBuffer[i] == 0)
{
isMessageReceived = TRUE;
}
}
bytesReceived += n;
}
Yield(); // Let other threads run
}
return isMessageReceived;
}
38/46
printf("%s\n",statusName);
switch (statusId)
{
// Successful case!
case 0:
break;
// Error cases:
case -10:
// flow-thru
case -20:
CloseHandle(sp2_hLinkReadyEvent);
CloseHandle(sp2_hStopEvent);
// flow-thru
case -30:
break;
case 60:
// flow-thru
case 50:
// flow-thru
case 40:
// flow-thru
if (sp2_stop())
{
system_event_wait(sp2_hStopEvent,10*1000);
}
// flow-thru
case 30:
system_event_destroy(sp2_hStopEvent);
// flow-thru
case 20:
system_event_destroy(sp2_hLinkReadyEvent);
// flow-thru
case 10:
break;
default:
printf("Unknown ERROR at cleanExit: %d\n",statusId);
break;
}
To use the M2M System Protocol 2 functions from an application the M2M
System Protocol 2 objects must be included within application software. Listing
2 shows a makefile template that lists the Hello World and M2M System
Protocol 2 object files.
Listing 2. A sample makefile for the M2M System Protocol 2
# Makefile template for the M2M System Protocol 2 (Win32 port)
39/46
# of HelloWorld.o in application module software makefile
all: $(SP2_OBJS) HelloWorld.o
Tip: The Hello World makefile is a good starting point when integrating the M2M
System Protocol 2 with application software.
40/46
APPENDIX: NOKIA 12 GSM MODULE CONFIGURATION FOR THE
HELLO WORLD APPLICATION
This chapter provides an example of how to use the Nokia 12 Configurator to
configure two Nokia 12 GSM modules as a client and a server. The
configuration presented in this chapter has to be carried out in order for the
Hello World client to establish a connection to the Hello World server.
Figure 19. The Hello World server configuration for client authentication
The connection of the server must be configured so that the server is ready to
accept incoming requests from the Hello World client. In this example the client
will be configured so that it makes a TCP CSD call and that is why the wireless
bearer of the server is configured to use TCP CSD.
41/46
4. Choose TCP CSD as the wireless bearer.
An example configuration is shown in Figure 20.
42/46
Figure 21. Hello World server IP address configuration
43/46
Figure 22. The Hello World client default connection configuration
A wireless connection (from the client to the server) has to be configured for the
client. The outgoing CHAP configuration of the client must match with the
incoming CHAP configuration of the server in order for the server to make a
client authentication. In addition, the phone number of the server is defined in
the ‘Gateway number’ field because the client calls the Gateway number to
establish a data connection to the server.
44/46
Figure 23. Hello World client configuration for a wireless connection
Note: In this example the data call is dropped if there is a 300 second pause in
the data traffic (connection timeout is set to 300 s).
And finally, you also have to configure the IP address of the client. The IP
address that is configured to the client can be any valid host IP address, for
example 10.0.0.2.
45/46
Figure 24. Hello World client IP address configuration
46/46