phonet.txt 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. Linux Phonet protocol family
  2. ============================
  3. Introduction
  4. ------------
  5. Phonet is a packet protocol used by Nokia cellular modems for both IPC
  6. and RPC. With the Linux Phonet socket family, Linux host processes can
  7. receive and send messages from/to the modem, or any other external
  8. device attached to the modem. The modem takes care of routing.
  9. Phonet packets can be exchanged through various hardware connections
  10. depending on the device, such as:
  11. - USB with the CDC Phonet interface,
  12. - infrared,
  13. - Bluetooth,
  14. - an RS232 serial port (with a dedicated "FBUS" line discipline),
  15. - the SSI bus with some TI OMAP processors.
  16. Packets format
  17. --------------
  18. Phonet packets have a common header as follows:
  19. struct phonethdr {
  20. uint8_t pn_media; /* Media type (link-layer identifier) */
  21. uint8_t pn_rdev; /* Receiver device ID */
  22. uint8_t pn_sdev; /* Sender device ID */
  23. uint8_t pn_res; /* Resource ID or function */
  24. uint16_t pn_length; /* Big-endian message byte length (minus 6) */
  25. uint8_t pn_robj; /* Receiver object ID */
  26. uint8_t pn_sobj; /* Sender object ID */
  27. };
  28. On Linux, the link-layer header includes the pn_media byte (see below).
  29. The next 7 bytes are part of the network-layer header.
  30. The device ID is split: the 6 higher-order bits constitute the device
  31. address, while the 2 lower-order bits are used for multiplexing, as are
  32. the 8-bit object identifiers. As such, Phonet can be considered as a
  33. network layer with 6 bits of address space and 10 bits for transport
  34. protocol (much like port numbers in IP world).
  35. The modem always has address number zero. All other device have a their
  36. own 6-bit address.
  37. Link layer
  38. ----------
  39. Phonet links are always point-to-point links. The link layer header
  40. consists of a single Phonet media type byte. It uniquely identifies the
  41. link through which the packet is transmitted, from the modem's
  42. perspective. Each Phonet network device shall prepend and set the media
  43. type byte as appropriate. For convenience, a common phonet_header_ops
  44. link-layer header operations structure is provided. It sets the
  45. media type according to the network device hardware address.
  46. Linux Phonet network interfaces support a dedicated link layer packets
  47. type (ETH_P_PHONET) which is out of the Ethernet type range. They can
  48. only send and receive Phonet packets.
  49. The virtual TUN tunnel device driver can also be used for Phonet. This
  50. requires IFF_TUN mode, _without_ the IFF_NO_PI flag. In this case,
  51. there is no link-layer header, so there is no Phonet media type byte.
  52. Note that Phonet interfaces are not allowed to re-order packets, so
  53. only the (default) Linux FIFO qdisc should be used with them.
  54. Network layer
  55. -------------
  56. The Phonet socket address family maps the Phonet packet header:
  57. struct sockaddr_pn {
  58. sa_family_t spn_family; /* AF_PHONET */
  59. uint8_t spn_obj; /* Object ID */
  60. uint8_t spn_dev; /* Device ID */
  61. uint8_t spn_resource; /* Resource or function */
  62. uint8_t spn_zero[...]; /* Padding */
  63. };
  64. The resource field is only used when sending and receiving;
  65. It is ignored by bind() and getsockname().
  66. Low-level datagram protocol
  67. ---------------------------
  68. Applications can send Phonet messages using the Phonet datagram socket
  69. protocol from the PF_PHONET family. Each socket is bound to one of the
  70. 2^10 object IDs available, and can send and receive packets with any
  71. other peer.
  72. struct sockaddr_pn addr = { .spn_family = AF_PHONET, };
  73. ssize_t len;
  74. socklen_t addrlen = sizeof(addr);
  75. int fd;
  76. fd = socket(PF_PHONET, SOCK_DGRAM, 0);
  77. bind(fd, (struct sockaddr *)&addr, sizeof(addr));
  78. /* ... */
  79. sendto(fd, msg, msglen, 0, (struct sockaddr *)&addr, sizeof(addr));
  80. len = recvfrom(fd, buf, sizeof(buf), 0,
  81. (struct sockaddr *)&addr, &addrlen);
  82. This protocol follows the SOCK_DGRAM connection-less semantics.
  83. However, connect() and getpeername() are not supported, as they did
  84. not seem useful with Phonet usages (could be added easily).
  85. Resource subscription
  86. ---------------------
  87. A Phonet datagram socket can be subscribed to any number of 8-bits
  88. Phonet resources, as follow:
  89. uint32_t res = 0xXX;
  90. ioctl(fd, SIOCPNADDRESOURCE, &res);
  91. Subscription is similarly cancelled using the SIOCPNDELRESOURCE I/O
  92. control request, or when the socket is closed.
  93. Note that no more than one socket can be subcribed to any given
  94. resource at a time. If not, ioctl() will return EBUSY.
  95. Phonet Pipe protocol
  96. --------------------
  97. The Phonet Pipe protocol is a simple sequenced packets protocol
  98. with end-to-end congestion control. It uses the passive listening
  99. socket paradigm. The listening socket is bound to an unique free object
  100. ID. Each listening socket can handle up to 255 simultaneous
  101. connections, one per accept()'d socket.
  102. int lfd, cfd;
  103. lfd = socket(PF_PHONET, SOCK_SEQPACKET, PN_PROTO_PIPE);
  104. listen (lfd, INT_MAX);
  105. /* ... */
  106. cfd = accept(lfd, NULL, NULL);
  107. for (;;)
  108. {
  109. char buf[...];
  110. ssize_t len = read(cfd, buf, sizeof(buf));
  111. /* ... */
  112. write(cfd, msg, msglen);
  113. }
  114. Connections are established between two endpoints by a "third party"
  115. application. This means that both endpoints are passive; so connect()
  116. is not possible.
  117. WARNING:
  118. When polling a connected pipe socket for writability, there is an
  119. intrinsic race condition whereby writability might be lost between the
  120. polling and the writing system calls. In this case, the socket will
  121. block until write becomes possible again, unless non-blocking mode
  122. is enabled.
  123. The pipe protocol provides two socket options at the SOL_PNPIPE level:
  124. PNPIPE_ENCAP accepts one integer value (int) of:
  125. PNPIPE_ENCAP_NONE: The socket operates normally (default).
  126. PNPIPE_ENCAP_IP: The socket is used as a backend for a virtual IP
  127. interface. This requires CAP_NET_ADMIN capability. GPRS data
  128. support on Nokia modems can use this. Note that the socket cannot
  129. be reliably poll()'d or read() from while in this mode.
  130. PNPIPE_IFINDEX is a read-only integer value. It contains the
  131. interface index of the network interface created by PNPIPE_ENCAP,
  132. or zero if encapsulation is off.
  133. Phonet Pipe-controller Implementation
  134. -------------------------------------
  135. Phonet Pipe-controller is enabled by selecting the CONFIG_PHONET_PIPECTRLR Kconfig
  136. option. It is useful when communicating with those Nokia Modems which do not
  137. implement Pipe controller in them e.g. Nokia Slim Modem used in ST-Ericsson
  138. U8500 platform.
  139. The implementation is based on the Data Connection Establishment Sequence
  140. depicted in 'Nokia Wireless Modem API - Wireless_modem_user_guide.pdf'
  141. document.
  142. It allows a phonet sequenced socket (host-pep) to initiate a Pipe connection
  143. between itself and a remote pipe-end point (e.g. modem).
  144. The implementation adds socket options at SOL_PNPIPE level:
  145. PNPIPE_PIPE_HANDLE
  146. It accepts an integer argument for setting value of pipe handle.
  147. PNPIPE_ENABLE accepts one integer value (int). If set to zero, the pipe
  148. is disabled. If the value is non-zero, the pipe is enabled. If the pipe
  149. is not (yet) connected, ENOTCONN is error is returned.
  150. The implementation also adds socket 'connect'. On calling the 'connect', pipe
  151. will be created between the source socket and the destination, and the pipe
  152. state will be set to PIPE_DISABLED.
  153. After a pipe has been created and enabled successfully, the Pipe data can be
  154. exchanged between the host-pep and remote-pep (modem).
  155. User-space would typically follow below sequence with Pipe controller:-
  156. -socket
  157. -bind
  158. -setsockopt for PNPIPE_PIPE_HANDLE
  159. -connect
  160. -setsockopt for PNPIPE_ENCAP_IP
  161. -setsockopt for PNPIPE_ENABLE
  162. Authors
  163. -------
  164. Linux Phonet was initially written by Sakari Ailus.
  165. Other contributors include Mikä Liljeberg, Andras Domokos,
  166. Carlos Chinea and Rémi Denis-Courmont.
  167. Copyright (C) 2008 Nokia Corporation.