network.scm 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (gnu installer newt network)
  19. #:use-module (gnu installer connman)
  20. #:use-module (gnu installer steps)
  21. #:use-module (gnu installer utils)
  22. #:use-module (gnu installer newt ethernet)
  23. #:use-module (gnu installer newt page)
  24. #:use-module (gnu installer newt wifi)
  25. #:use-module (guix i18n)
  26. #:use-module (srfi srfi-1)
  27. #:use-module (srfi srfi-11)
  28. #:use-module (srfi srfi-34)
  29. #:use-module (srfi srfi-35)
  30. #:use-module (newt)
  31. #:export (run-network-page))
  32. ;; Maximum length of a technology name.
  33. (define technology-name-max-length (make-parameter 20))
  34. (define (technology->text technology)
  35. "Return a string describing the given TECHNOLOGY."
  36. (let* ((name (technology-name technology))
  37. (padded-name (string-pad-right name
  38. (technology-name-max-length))))
  39. (format #f "~a~%" padded-name)))
  40. (define (run-technology-page)
  41. "Run a page to ask the user which technology shall be used to access
  42. Internet and return the selected technology. For now, only technologies with
  43. \"ethernet\" or \"wifi\" types are supported."
  44. (define (technology-items)
  45. (filter (lambda (technology)
  46. (let ((type (technology-type technology)))
  47. (or
  48. (string=? type "ethernet")
  49. (string=? type "wifi"))))
  50. (connman-technologies)))
  51. (let ((items (technology-items)))
  52. (if (null? items)
  53. (case (choice-window
  54. (G_ "Internet access")
  55. (G_ "Continue")
  56. (G_ "Exit")
  57. (G_ "The install process requires an internet access, but no \
  58. network device were found. Do you want to continue anyway?"))
  59. ((1) (raise
  60. (condition
  61. (&installer-step-break))))
  62. ((2) (raise
  63. (condition
  64. (&installer-step-abort)))))
  65. (run-listbox-selection-page
  66. #:info-text (G_ "The install process requires an internet access.\
  67. Please select a network device.")
  68. #:title (G_ "Internet access")
  69. #:listbox-items items
  70. #:listbox-item->text technology->text
  71. #:button-text (G_ "Exit")
  72. #:button-callback-procedure
  73. (lambda _
  74. (raise
  75. (condition
  76. (&installer-step-abort))))))))
  77. (define (find-technology-by-type technologies type)
  78. "Find and return a technology with the given TYPE in TECHNOLOGIES list."
  79. (find (lambda (technology)
  80. (string=? (technology-type technology)
  81. type))
  82. technologies))
  83. (define (wait-technology-powered technology)
  84. "Wait and display a progress bar until the given TECHNOLOGY is powered."
  85. (let ((name (technology-name technology))
  86. (full-value 5))
  87. (run-scale-page
  88. #:title (G_ "Powering technology")
  89. #:info-text (format #f "Waiting for technology ~a to be powered." name)
  90. #:scale-full-value full-value
  91. #:scale-update-proc
  92. (lambda (value)
  93. (let* ((technologies (connman-technologies))
  94. (type (technology-type technology))
  95. (updated-technology
  96. (find-technology-by-type technologies type))
  97. (technology-powered? updated-technology))
  98. (sleep 1)
  99. (if technology-powered?
  100. full-value
  101. (+ value 1)))))))
  102. (define (wait-service-online)
  103. "Display a newt scale until connman detects an Internet access. Do
  104. FULL-VALUE tentatives, spaced by 1 second."
  105. (let* ((full-value 5))
  106. (run-scale-page
  107. #:title (G_ "Checking connectivity")
  108. #:info-text (G_ "Waiting internet access is established.")
  109. #:scale-full-value full-value
  110. #:scale-update-proc
  111. (lambda (value)
  112. (sleep 1)
  113. (if (connman-online?)
  114. full-value
  115. (+ value 1))))
  116. (unless (connman-online?)
  117. (run-error-page
  118. (G_ "The selected network does not provide an Internet \
  119. access, please try again.")
  120. (G_ "Connection error"))
  121. (raise
  122. (condition
  123. (&installer-step-abort))))))
  124. (define (run-network-page)
  125. "Run a page to allow the user to configure connman so that it can access the
  126. Internet."
  127. (define network-steps
  128. (list
  129. ;; Ask the user to choose between ethernet and wifi technologies.
  130. (installer-step
  131. (id 'select-technology)
  132. (compute
  133. (lambda _
  134. (run-technology-page))))
  135. ;; Enable the previously selected technology.
  136. (installer-step
  137. (id 'power-technology)
  138. (compute
  139. (lambda (result _)
  140. (let ((technology (result-step result 'select-technology)))
  141. (connman-enable-technology technology)
  142. (wait-technology-powered technology)))))
  143. ;; Propose the user to connect to one of the service available for the
  144. ;; previously selected technology.
  145. (installer-step
  146. (id 'connect-service)
  147. (compute
  148. (lambda (result _)
  149. (let* ((technology (result-step result 'select-technology))
  150. (type (technology-type technology)))
  151. (cond
  152. ((string=? "wifi" type)
  153. (run-wifi-page))
  154. ((string=? "ethernet" type)
  155. (run-ethernet-page)))))))
  156. ;; Wait for connman status to switch to 'online, which means it can
  157. ;; access Internet.
  158. (installer-step
  159. (id 'wait-online)
  160. (compute (lambda _
  161. (wait-service-online))))))
  162. (run-installer-steps
  163. #:steps network-steps
  164. #:rewind-strategy 'start))