n_gsm.rst 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ==============================
  2. GSM 0710 tty multiplexor HOWTO
  3. ==============================
  4. .. contents:: :local:
  5. This line discipline implements the GSM 07.10 multiplexing protocol
  6. detailed in the following 3GPP document:
  7. https://www.3gpp.org/ftp/Specs/archive/07_series/07.10/0710-720.zip
  8. This document give some hints on how to use this driver with GPRS and 3G
  9. modems connected to a physical serial port.
  10. How to use it
  11. =============
  12. Config Initiator
  13. ----------------
  14. #. Initialize the modem in 0710 mux mode (usually ``AT+CMUX=`` command) through
  15. its serial port. Depending on the modem used, you can pass more or less
  16. parameters to this command.
  17. #. Switch the serial line to using the n_gsm line discipline by using
  18. ``TIOCSETD`` ioctl.
  19. #. Configure the mux using ``GSMIOC_GETCONF_EXT``/``GSMIOC_SETCONF_EXT`` ioctl if needed.
  20. #. Configure the mux using ``GSMIOC_GETCONF``/``GSMIOC_SETCONF`` ioctl.
  21. #. Configure DLCs using ``GSMIOC_GETCONF_DLCI``/``GSMIOC_SETCONF_DLCI`` ioctl for non-defaults.
  22. #. Obtain base gsmtty number for the used serial port.
  23. Major parts of the initialization program
  24. (a good starting point is util-linux-ng/sys-utils/ldattach.c)::
  25. #include <stdio.h>
  26. #include <stdint.h>
  27. #include <linux/gsmmux.h>
  28. #include <linux/tty.h>
  29. #define DEFAULT_SPEED B115200
  30. #define SERIAL_PORT /dev/ttyS0
  31. int ldisc = N_GSM0710;
  32. struct gsm_config c;
  33. struct gsm_config_ext ce;
  34. struct gsm_dlci_config dc;
  35. struct termios configuration;
  36. uint32_t first;
  37. /* open the serial port connected to the modem */
  38. fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
  39. /* configure the serial port : speed, flow control ... */
  40. /* send the AT commands to switch the modem to CMUX mode
  41. and check that it's successful (should return OK) */
  42. write(fd, "AT+CMUX=0\r", 10);
  43. /* experience showed that some modems need some time before
  44. being able to answer to the first MUX packet so a delay
  45. may be needed here in some case */
  46. sleep(3);
  47. /* use n_gsm line discipline */
  48. ioctl(fd, TIOCSETD, &ldisc);
  49. /* get n_gsm extended configuration */
  50. ioctl(fd, GSMIOC_GETCONF_EXT, &ce);
  51. /* use keep-alive once every 5s for modem connection supervision */
  52. ce.keep_alive = 500;
  53. /* set the new extended configuration */
  54. ioctl(fd, GSMIOC_SETCONF_EXT, &ce);
  55. /* get n_gsm configuration */
  56. ioctl(fd, GSMIOC_GETCONF, &c);
  57. /* we are initiator and need encoding 0 (basic) */
  58. c.initiator = 1;
  59. c.encapsulation = 0;
  60. /* our modem defaults to a maximum size of 127 bytes */
  61. c.mru = 127;
  62. c.mtu = 127;
  63. /* set the new configuration */
  64. ioctl(fd, GSMIOC_SETCONF, &c);
  65. /* get DLC 1 configuration */
  66. dc.channel = 1;
  67. ioctl(fd, GSMIOC_GETCONF_DLCI, &dc);
  68. /* the first user channel gets a higher priority */
  69. dc.priority = 1;
  70. /* set the new DLC 1 specific configuration */
  71. ioctl(fd, GSMIOC_SETCONF_DLCI, &dc);
  72. /* get first gsmtty device node */
  73. ioctl(fd, GSMIOC_GETFIRST, &first);
  74. printf("first muxed line: /dev/gsmtty%i\n", first);
  75. /* and wait for ever to keep the line discipline enabled */
  76. daemon(0,0);
  77. pause();
  78. #. Use these devices as plain serial ports.
  79. For example, it's possible:
  80. - to use *gnokii* to send / receive SMS on ``ttygsm1``
  81. - to use *ppp* to establish a datalink on ``ttygsm2``
  82. #. First close all virtual ports before closing the physical port.
  83. Note that after closing the physical port the modem is still in multiplexing
  84. mode. This may prevent a successful re-opening of the port later. To avoid
  85. this situation either reset the modem if your hardware allows that or send
  86. a disconnect command frame manually before initializing the multiplexing mode
  87. for the second time. The byte sequence for the disconnect command frame is::
  88. 0xf9, 0x03, 0xef, 0x03, 0xc3, 0x16, 0xf9
  89. Config Requester
  90. ----------------
  91. #. Receive ``AT+CMUX=`` command through its serial port, initialize mux mode
  92. config.
  93. #. Switch the serial line to using the *n_gsm* line discipline by using
  94. ``TIOCSETD`` ioctl.
  95. #. Configure the mux using ``GSMIOC_GETCONF_EXT``/``GSMIOC_SETCONF_EXT``
  96. ioctl if needed.
  97. #. Configure the mux using ``GSMIOC_GETCONF``/``GSMIOC_SETCONF`` ioctl.
  98. #. Configure DLCs using ``GSMIOC_GETCONF_DLCI``/``GSMIOC_SETCONF_DLCI`` ioctl for non-defaults.
  99. #. Obtain base gsmtty number for the used serial port::
  100. #include <stdio.h>
  101. #include <stdint.h>
  102. #include <linux/gsmmux.h>
  103. #include <linux/tty.h>
  104. #define DEFAULT_SPEED B115200
  105. #define SERIAL_PORT /dev/ttyS0
  106. int ldisc = N_GSM0710;
  107. struct gsm_config c;
  108. struct gsm_config_ext ce;
  109. struct gsm_dlci_config dc;
  110. struct termios configuration;
  111. uint32_t first;
  112. /* open the serial port */
  113. fd = open(SERIAL_PORT, O_RDWR | O_NOCTTY | O_NDELAY);
  114. /* configure the serial port : speed, flow control ... */
  115. /* get serial data and check "AT+CMUX=command" parameter ... */
  116. /* use n_gsm line discipline */
  117. ioctl(fd, TIOCSETD, &ldisc);
  118. /* get n_gsm extended configuration */
  119. ioctl(fd, GSMIOC_GETCONF_EXT, &ce);
  120. /* use keep-alive once every 5s for peer connection supervision */
  121. ce.keep_alive = 500;
  122. /* set the new extended configuration */
  123. ioctl(fd, GSMIOC_SETCONF_EXT, &ce);
  124. /* get n_gsm configuration */
  125. ioctl(fd, GSMIOC_GETCONF, &c);
  126. /* we are requester and need encoding 0 (basic) */
  127. c.initiator = 0;
  128. c.encapsulation = 0;
  129. /* our modem defaults to a maximum size of 127 bytes */
  130. c.mru = 127;
  131. c.mtu = 127;
  132. /* set the new configuration */
  133. ioctl(fd, GSMIOC_SETCONF, &c);
  134. /* get DLC 1 configuration */
  135. dc.channel = 1;
  136. ioctl(fd, GSMIOC_GETCONF_DLCI, &dc);
  137. /* the first user channel gets a higher priority */
  138. dc.priority = 1;
  139. /* set the new DLC 1 specific configuration */
  140. ioctl(fd, GSMIOC_SETCONF_DLCI, &dc);
  141. /* get first gsmtty device node */
  142. ioctl(fd, GSMIOC_GETFIRST, &first);
  143. printf("first muxed line: /dev/gsmtty%i\n", first);
  144. /* and wait for ever to keep the line discipline enabled */
  145. daemon(0,0);
  146. pause();
  147. 11-03-08 - Eric Bénard - <eric@eukrea.com>