network.scm 6.4 KB

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