phonet.txt 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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 traditionally established between two endpoints by a
  115. "third party" application. This means that both endpoints are passive.
  116. As of Linux kernel version 2.6.39, it is also possible to connect
  117. two endpoints directly, using connect() on the active side. This is
  118. intended to support the newer Nokia Wireless Modem API, as found in
  119. e.g. the Nokia Slim Modem in the ST-Ericsson U8500 platform:
  120. struct sockaddr_spn spn;
  121. int fd;
  122. fd = socket(PF_PHONET, SOCK_SEQPACKET, PN_PROTO_PIPE);
  123. memset(&spn, 0, sizeof(spn));
  124. spn.spn_family = AF_PHONET;
  125. spn.spn_obj = ...;
  126. spn.spn_dev = ...;
  127. spn.spn_resource = 0xD9;
  128. connect(fd, (struct sockaddr *)&spn, sizeof(spn));
  129. /* normal I/O here ... */
  130. close(fd);
  131. WARNING:
  132. When polling a connected pipe socket for writability, there is an
  133. intrinsic race condition whereby writability might be lost between the
  134. polling and the writing system calls. In this case, the socket will
  135. block until write becomes possible again, unless non-blocking mode
  136. is enabled.
  137. The pipe protocol provides two socket options at the SOL_PNPIPE level:
  138. PNPIPE_ENCAP accepts one integer value (int) of:
  139. PNPIPE_ENCAP_NONE: The socket operates normally (default).
  140. PNPIPE_ENCAP_IP: The socket is used as a backend for a virtual IP
  141. interface. This requires CAP_NET_ADMIN capability. GPRS data
  142. support on Nokia modems can use this. Note that the socket cannot
  143. be reliably poll()'d or read() from while in this mode.
  144. PNPIPE_IFINDEX is a read-only integer value. It contains the
  145. interface index of the network interface created by PNPIPE_ENCAP,
  146. or zero if encapsulation is off.
  147. PNPIPE_HANDLE is a read-only integer value. It contains the underlying
  148. identifier ("pipe handle") of the pipe. This is only defined for
  149. socket descriptors that are already connected or being connected.
  150. Authors
  151. -------
  152. Linux Phonet was initially written by Sakari Ailus.
  153. Other contributors include Mikä Liljeberg, Andras Domokos,
  154. Carlos Chinea and Rémi Denis-Courmont.
  155. Copyright (C) 2008 Nokia Corporation.