brlapi_protocol.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * libbrlapi - A library providing access to braille terminals for applications.
  3. *
  4. * Copyright (C) 2002-2020 by
  5. * Samuel Thibault <Samuel.Thibault@ens-lyon.org>
  6. * Sébastien Hinderer <Sebastien.Hinderer@ens-lyon.org>
  7. *
  8. * libbrlapi comes with ABSOLUTELY NO WARRANTY.
  9. *
  10. * This is free software, placed under the terms of the
  11. * GNU Lesser General Public License, as published by the Free Software
  12. * Foundation; either version 2.1 of the License, or (at your option) any
  13. * later version. Please see the file LICENSE-LGPL for details.
  14. *
  15. * Web Page: http://brltty.app/
  16. *
  17. * This software is maintained by Dave Mielke <dave@mielke.cc>.
  18. */
  19. /** \file
  20. * \brief types and constants for \e BrlAPI's protocol
  21. */
  22. #ifndef BRLAPI_INCLUDED_PROTOCOL
  23. #define BRLAPI_INCLUDED_PROTOCOL
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif /* __cplusplus */
  27. #include "brlapi.h"
  28. /* this is for UINT32_MAX */
  29. #include <inttypes.h>
  30. #ifndef UINT32_MAX
  31. #define UINT32_MAX (4294967295U)
  32. #endif /* UINT32_MAX */
  33. /* The type size_t is defined there! */
  34. #include <unistd.h>
  35. /** \defgroup brlapi_protocol BrlAPI's protocol
  36. * \brief Instructions and constants for \e BrlAPI 's protocol
  37. *
  38. * These are defines for the protocol between \e BrlAPI 's server and clients.
  39. * Understanding is not needed to use the \e BrlAPI library, so reading this
  40. * is not needed unless really wanting to connect to \e BrlAPI without
  41. * \e BrlAPI 's library.
  42. *
  43. * @{ */
  44. #define BRLAPI_PROTOCOL_VERSION ((uint32_t) 8) /** Communication protocol version */
  45. /** Maximum packet size for packets exchanged on sockets and with braille
  46. * terminal */
  47. #define BRLAPI_MAXPACKETSIZE 4096
  48. #define BRLAPI_PACKET_VERSION 'v' /**< Version */
  49. #define BRLAPI_PACKET_AUTH 'a' /**< Authorization */
  50. #define BRLAPI_PACKET_GETDRIVERNAME 'n' /**< Ask which driver is used */
  51. #define BRLAPI_PACKET_GETMODELID 'd' /**< Ask which model is used */
  52. #define BRLAPI_PACKET_GETDISPLAYSIZE 's' /**< Dimensions of brl display */
  53. #define BRLAPI_PACKET_ENTERTTYMODE 't' /**< Asks for a specified tty */
  54. #define BRLAPI_PACKET_SETFOCUS 'F' /**< Set current tty focus */
  55. #define BRLAPI_PACKET_LEAVETTYMODE 'L' /**< Release the tty */
  56. #define BRLAPI_PACKET_KEY 'k' /**< Braille key */
  57. #define BRLAPI_PACKET_IGNOREKEYRANGES 'm' /**< Mask key ranges */
  58. #define BRLAPI_PACKET_ACCEPTKEYRANGES 'u' /**< Unmask key ranges */
  59. #define BRLAPI_PACKET_WRITE 'w' /**< Write */
  60. #define BRLAPI_PACKET_ENTERRAWMODE '*' /**< Enter in raw mode */
  61. #define BRLAPI_PACKET_LEAVERAWMODE '#' /**< Leave raw mode */
  62. #define BRLAPI_PACKET_PACKET 'p' /**< Raw packets */
  63. #define BRLAPI_PACKET_ACK 'A' /**< Acknowledgement */
  64. #define BRLAPI_PACKET_ERROR 'e' /**< non-fatal error */
  65. #define BRLAPI_PACKET_EXCEPTION 'E' /**< Exception */
  66. #define BRLAPI_PACKET_SUSPENDDRIVER 'S' /**< Suspend driver */
  67. #define BRLAPI_PACKET_RESUMEDRIVER 'R' /**< Resume driver */
  68. #define BRLAPI_PACKET_PARAM_VALUE (('P'<<8) + 'V') /**< Parameter value */
  69. #define BRLAPI_PACKET_PARAM_REQUEST (('P'<<8) + 'R') /**< Parameter request*/
  70. #define BRLAPI_PACKET_PARAM_UPDATE (('P'<<8) + 'U') /**< Parameter update */
  71. /** Magic number to give when sending a BRLPACKET_ENTERRAWMODE or BRLPACKET_SUSPEND packet */
  72. #define BRLAPI_DEVICE_MAGIC (0xdeadbeefL)
  73. /** Structure of packets headers */
  74. typedef struct {
  75. uint32_t size;
  76. brlapi_packetType_t type;
  77. } brlapi_header_t;
  78. /** Size of packet headers */
  79. #define BRLAPI_HEADERSIZE sizeof(brlapi_header_t)
  80. /** Structure of version packets */
  81. typedef struct {
  82. uint32_t protocolVersion;
  83. } brlapi_versionPacket_t;
  84. /** Structure of authorization packets */
  85. typedef struct {
  86. uint32_t type;
  87. unsigned char key;
  88. } brlapi_authClientPacket_t;
  89. typedef struct {
  90. uint32_t type[1];
  91. } brlapi_authServerPacket_t;
  92. #define BRLAPI_AUTH_NONE 'N' /**< No or implicit authorization */
  93. #define BRLAPI_AUTH_KEY 'K' /**< Key authorization */
  94. #define BRLAPI_AUTH_CRED 'C' /**< Explicit socket credentials authorization */
  95. /** Structure of error packets */
  96. typedef struct {
  97. uint32_t code;
  98. brlapi_packetType_t type;
  99. unsigned char packet;
  100. } brlapi_errorPacket_t;
  101. /** Structure of enterRawMode / suspend packets */
  102. typedef struct {
  103. uint32_t magic;
  104. unsigned char nameLength;
  105. char name;
  106. } brlapi_getDriverSpecificModePacket_t;
  107. /** Flags for writing */
  108. #define BRLAPI_WF_DISPLAYNUMBER 0X01 /**< Display number */
  109. #define BRLAPI_WF_REGION 0X02 /**< Region parameter */
  110. #define BRLAPI_WF_TEXT 0X04 /**< Contains some text */
  111. #define BRLAPI_WF_ATTR_AND 0X08 /**< And attributes */
  112. #define BRLAPI_WF_ATTR_OR 0X10 /**< Or attributes */
  113. #define BRLAPI_WF_CURSOR 0X20 /**< Cursor position */
  114. #define BRLAPI_WF_CHARSET 0X40 /**< Charset */
  115. /** Structure of extended write packets */
  116. typedef struct {
  117. uint32_t flags; /** Flags to tell which fields are present */
  118. unsigned char data; /** Fields in the same order as flag weight */
  119. } brlapi_writeArgumentsPacket_t;
  120. /** Flags for parameter values */
  121. #define BRLAPI_PVF_GLOBAL 0X01 /** Value is the global value */
  122. #define BRLAPI_MAXPARAMSIZE (BRLAPI_MAXPACKETSIZE - (sizeof(uint32_t) + sizeof(brlapi_param_t) + 2*sizeof(uint32_t)))
  123. /** Structure of Parameter value or update */
  124. typedef struct {
  125. uint32_t flags; /** Flags to tell how value was gotten */
  126. brlapi_param_t param; /** Which parameter being transmitted */
  127. uint32_t subparam_hi; /** Which sub-parameter being transmitted, hi 32bits */
  128. uint32_t subparam_lo; /** Which sub-parameter being transmitted, lo 32bits */
  129. unsigned char data[BRLAPI_MAXPARAMSIZE]; /** Content of the parameter */
  130. } brlapi_paramValuePacket_t;
  131. /** Flags for parameter requests */
  132. #define BRLAPI_PARAMF_GET 0X100 /** Get current parameter value */
  133. #define BRLAPI_PARAMF_SUBSCRIBE 0X200 /** Subscribe to parameter updates */
  134. #define BRLAPI_PARAMF_UNSUBSCRIBE 0X400 /** Unsubscribe from parameter updates */
  135. /** Structure of Parameter request */
  136. typedef struct {
  137. uint32_t flags; /** Flags to tell whether/how to get values */
  138. brlapi_param_t param; /** Which parameter to be transmitted */
  139. uint32_t subparam_hi; /** Which sub-parameter being transmitted, hi 32bits */
  140. uint32_t subparam_lo; /** Which sub-parameter being transmitted, lo 32bits */
  141. } brlapi_paramRequestPacket_t;
  142. /** Type for packets. Should be used instead of a mere char[], since it has
  143. * correct alignment requirements. */
  144. typedef union {
  145. unsigned char data[BRLAPI_MAXPACKETSIZE];
  146. brlapi_versionPacket_t version;
  147. brlapi_authClientPacket_t authClient;
  148. brlapi_authServerPacket_t authServer;
  149. brlapi_errorPacket_t error;
  150. brlapi_getDriverSpecificModePacket_t getDriverSpecificMode;
  151. brlapi_writeArgumentsPacket_t writeArguments;
  152. brlapi_paramValuePacket_t paramValue;
  153. brlapi_paramRequestPacket_t paramRequest;
  154. uint32_t uint32;
  155. } brlapi_packet_t;
  156. /* brlapi_writePacket */
  157. /** Send a packet to \e BrlAPI server
  158. *
  159. * This function is for internal use, but one might use it if one really knows
  160. * what one is doing...
  161. *
  162. * \e type should only be one of the above defined BRLPACKET_*.
  163. *
  164. * The syntax is the same as write()'s.
  165. *
  166. * \return 0 on success, -1 on failure.
  167. *
  168. * \sa brlapi_readPacketHeader()
  169. * brlapi_readPacketContent()
  170. * brlapi_readPacket()
  171. */
  172. ssize_t brlapi_writePacket(brlapi_fileDescriptor fd, brlapi_packetType_t type, const void *buf, size_t size);
  173. /* brlapi_readPacketHeader */
  174. /** Read the header (type+size) of a packet from \e BrlAPI server
  175. *
  176. * This function is for internal use, but one might use it if one really knows
  177. * what one is doing...
  178. *
  179. * \e type is where the function will store the packet type; it should always
  180. * be one of the above defined BRLPACKET_* (or else something very nasty must
  181. * have happened :/).
  182. *
  183. * \return packet's size, -2 if \c EOF occurred, -1 on error or signal
  184. * interruption.
  185. *
  186. * \sa brlapi_writePacket()
  187. * brlapi_readPacketContent
  188. * brlapi_readPacket
  189. */
  190. ssize_t brlapi_readPacketHeader(brlapi_fileDescriptor fd, brlapi_packetType_t *packetType);
  191. /* brlapi_readPacketContent */
  192. /** Read the content of a packet from \e BrlAPI server
  193. *
  194. * This function is for internal use, but one might use it if one really knows
  195. * what one is doing...
  196. *
  197. * \e packetSize is the size announced by \e brlapi_readPacketHeader()
  198. *
  199. * \e bufSize is the size of \e buf
  200. *
  201. * \return packetSize, -2 if \c EOF occurred, -1 on error.
  202. *
  203. * If the packet is larger than the supplied buffer, the buffer will be
  204. * filled with the beginning of the packet, the rest of the packet being
  205. * discarded. This follows the semantics of the recv system call when the
  206. * MSG_TRUNC option is given.
  207. *
  208. * \sa brlapi_writePacket()
  209. * brlapi_readPacketHeader()
  210. * brlapi_readPacket()
  211. */
  212. ssize_t brlapi_readPacketContent(brlapi_fileDescriptor fd, size_t packetSize, void *buf, size_t bufSize);
  213. /* brlapi_readPacket */
  214. /** Read a packet from \e BrlAPI server
  215. *
  216. * This function is for internal use, but one might use it if one really knows
  217. * what one is doing...
  218. *
  219. * \e type is where the function will store the packet type; it should always
  220. * be one of the above defined BRLPACKET_* (or else something very nasty must
  221. * have happened :/).
  222. *
  223. * The syntax is the same as read()'s.
  224. *
  225. * \return packet's size, -2 if \c EOF occurred, -1 on error or signal
  226. * interruption.
  227. *
  228. * If the packet is larger than the supplied buffer, the buffer will be
  229. * filled with the beginning of the packet, the rest of the packet being
  230. * discarded. This follows the semantics of the recv system call when the
  231. * MSG_TRUNC option is given.
  232. *
  233. * \sa brlapi_writePacket()
  234. */
  235. ssize_t brlapi_readPacket(brlapi_fileDescriptor fd, brlapi_packetType_t *type, void *buf, size_t size);
  236. /* brlapi_fd_mutex */
  237. /** Mutex for protecting concurrent fd access
  238. *
  239. * In order to regulate concurrent access to the library's file descriptor and
  240. * requests to / answers from \e BrlAPI server, every function of the library
  241. * locks this mutex, namely
  242. *
  243. * - brlapi_openConnection()
  244. * - brlapi_closeConnection()
  245. * - brlapi_enterRawMode()
  246. * - brlapi_leaveRawMode()
  247. * - brlapi_sendRaw()
  248. * - brlapi_recvRaw()
  249. * - brlapi_getDriverName()
  250. * - brlapi_getDisplaySize()
  251. * - brlapi_enterTtyMode()
  252. * - brlapi_enterTtyModeWithPath()
  253. * - brlapi_leaveTtyMode()
  254. * - brlapi_*write*()
  255. * - brlapi_(un)?ignorekey(Range|Set)()
  256. * - brlapi_readKey()
  257. *
  258. * If both these functions and brlapi_writePacket() or brlapi_readPacket() are
  259. * used in a multithreaded application, this mutex must be locked before calling
  260. * brlapi_writePacket() or brlapi_readPacket(), and unlocked afterwards.
  261. */
  262. #ifdef __MINGW32__
  263. #include <windows.h>
  264. extern HANDLE brlapi_fd_mutex;
  265. #else /* __MINGW32__ */
  266. #include <pthread.h>
  267. extern pthread_mutex_t brlapi_fd_mutex;
  268. #endif /* __MINGW32__ */
  269. /* @} */
  270. #ifdef __cplusplus
  271. }
  272. #endif /* __cplusplus */
  273. #endif /* BRLAPI_INCLUDED_PROTOCOL */