msg-header.scm 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. (define-module (mach msg-header)
  2. #:export (make-msg-header msg-header?
  3. msg-header:bits set-msg-header:bits!
  4. msg-header:size set-msg-header:size!
  5. msg-header:remote-port set-msg-header:remote-port!
  6. msg-header:local-port set-msg-header:local-port!
  7. msg-header:seqno set-msg-header:seqno!
  8. msg-header:id set-msg-header:id!
  9. ffi:msg-header %msg-header-size
  10. %read-message-header %write-message-header!)
  11. #:use-module (mach types)
  12. #:use-module (srfi srfi-60)
  13. #:use-module (srfi srfi-9)
  14. #:use-module (rnrs bytevectors)
  15. #:use-module ((system foreign)
  16. #:select (parse-c-struct make-c-struct sizeof
  17. pointer->bytevector bytevector->pointer))
  18. #:use-module ((rnrs base) #:select (assert)))
  19. ;; Different masks to the message header.
  20. (define %msgh-bits-zero #x00000000)
  21. (define %msgh-bits-remote-mask #x000000ff)
  22. (define %msgh-bits-local-mask #x0000ff00)
  23. (define %msgh-bits-complex #x80000000)
  24. (define %msgh-bits-unused #x07ff0000)
  25. (define %msgh-bits-ports-mask
  26. (bitwise-ior %msgh-bits-remote-mask %msgh-bits-local-mask))
  27. ;; The message header structure as in C.
  28. (define ffi:msg-header
  29. (map cdr
  30. `((bits ,ffi:msg-bits)
  31. (size ,ffi:msg-size)
  32. (remote-port ,ffi:mach-port)
  33. (local-port ,ffi:mach-port)
  34. ;; XXX shouldn't this be ffi:msg-seqno?
  35. (seqno ,ffi:port-seqno)
  36. (id ,ffi:msg-id))))
  37. (define %msg-header-size (sizeof ffi:msg-header))
  38. ;; Be really sure.
  39. (assert (= 24 %msg-header-size))
  40. ;; The Scheme equivalent.
  41. ;; TODO: maybe do some type checking in the constructor?
  42. (define-record-type <msg-header>
  43. (make-msg-header bits size remote-port local-port seqno id)
  44. msg-header?
  45. ;; (integer)
  46. (bits msg-header:bits set-msg-header:bits!)
  47. ;; sie of message (integer)
  48. (size msg-header:size set-msg-header:size!)
  49. ;; a <mach-port> (the ‘header remote port’)
  50. (remote-port msg-header:remote-port set-msg-header:remote-port!!)
  51. ;; a <mach-port> (tehe ‘header local port’)
  52. (local-port msg-header:local-port set-msg-header:local-port!)
  53. ;; (integer)
  54. (seqno msg-header:seqno set-msg-header:seqno!)
  55. ;; Message ID (integer)
  56. (id msg-header:id set-msg-header:id!))
  57. (define (%read-message-header pointer)
  58. "Read a message header structure (as in C) from @var{pointer},
  59. and return it as a @code{<msg-header>}."
  60. (apply (lambda (bits size remote-port local-port seqno id)
  61. (make-msg-header bits size (%wrap-mach-port remote-port)
  62. (%wrap-mach-port local-port) seqno id))
  63. (parse-c-struct ffi:msg-header pointer)))
  64. (define (%write-message-header! pointer header)
  65. "Write a message header @var{header} to @var{pointer}."
  66. (let* ((p (make-c-struct ffi:msg-header
  67. (list (msg-header:bits header)
  68. (msg-header:size header)
  69. (%unwrap-mach-port (msg-header:remote-port header))
  70. (%unwrap-mach-port (msg-header:local-port header))
  71. (msg-header:seqno header)
  72. (msg-header:id header))))
  73. (b (pointer->bytevector p %msg-header-size))
  74. (b/pointer (bytevector->pointer pointer %msg-header-size)))
  75. (bytevector-copy! b b/pointer 0 0 %msg-header-size)))
  76. ;; XXX what is type-name?
  77. #;
  78. (define (msgh-bits-remote type-name)
  79. "Same thing as MACH_MSGH_BITS_LOCAL."
  80. (bitwise-and %msgh-bits-remote-mask
  81. (translate-msg-type-name-symbol type-name)))
  82. #;
  83. (define (msgh-bits-local type-name)
  84. "Same thing as MACH_MSGH_BITS_LOCAL."
  85. (ash (bitwise-and %msgh-bits-local-mask
  86. (translate-msg-type-name-symbol type-name))
  87. -8))
  88. #;
  89. (define (msgh-bits remote local)
  90. "Same thing as MACH_MSGH_BITS."
  91. (bitwise-or (msgh-bits-remote remote)
  92. (ash (msgh-bits-local local) 8)))