network.scm 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2018 Mathieu Othacehe <m.othacehe@gmail.com>
  3. ;;; Copyright © 2019 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) 10)
  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. (let* ((full-value 5))
  115. (run-scale-page
  116. #:title (G_ "Checking connectivity")
  117. #:info-text (G_ "Waiting for Internet access establishment...")
  118. #:scale-full-value full-value
  119. #:scale-update-proc
  120. (lambda (value)
  121. (sleep 1)
  122. (if (connman-online?)
  123. full-value
  124. (+ value 1))))
  125. (unless (connman-online?)
  126. (run-error-page
  127. (G_ "The selected network does not provide access to the \
  128. Internet, please try again.")
  129. (G_ "Connection error"))
  130. (raise
  131. (condition
  132. (&installer-step-abort))))))
  133. (define (run-network-page)
  134. "Run a page to allow the user to configure connman so that it can access the
  135. Internet."
  136. (define network-steps
  137. (list
  138. ;; Ask the user to choose between ethernet and wifi technologies.
  139. (installer-step
  140. (id 'select-technology)
  141. (compute
  142. (lambda _
  143. (run-technology-page))))
  144. ;; Enable the previously selected technology.
  145. (installer-step
  146. (id 'power-technology)
  147. (compute
  148. (lambda (result _)
  149. (let ((technology (result-step result 'select-technology)))
  150. (connman-enable-technology technology)
  151. (wait-technology-powered technology)))))
  152. ;; Propose the user to connect to one of the service available for the
  153. ;; previously selected technology.
  154. (installer-step
  155. (id 'connect-service)
  156. (compute
  157. (lambda (result _)
  158. (let* ((technology (result-step result 'select-technology))
  159. (type (technology-type technology)))
  160. (cond
  161. ((string=? "wifi" type)
  162. (run-wifi-page))
  163. ((string=? "ethernet" type)
  164. (run-ethernet-page)))))))
  165. ;; Wait for connman status to switch to 'online, which means it can
  166. ;; access Internet.
  167. (installer-step
  168. (id 'wait-online)
  169. (compute (lambda _
  170. (wait-service-online))))))
  171. (run-installer-steps
  172. #:steps network-steps
  173. #:rewind-strategy 'start))