-
Notifications
You must be signed in to change notification settings - Fork 0
/
handlers.accesspoint.go
105 lines (93 loc) · 3.37 KB
/
handlers.accesspoint.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright (C) 2017 Next Thing Co. <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>
package main
import (
"github.com/PopcornComputer/gonnman"
"github.com/gin-gonic/gin"
"net/http"
)
// Endpoint handler for GET /list
func renderAccessPointList(context *gin.Context) {
render(context, gin.H{
"title": "Wifi Onboarding - List of Access Points",
"pipeline": connectionServices},
"list.html")
}
// Endpoint handler for GET /connect?id=<id>
// Wifi path is required!
func renderAccessPointAuthentication(context *gin.Context) {
wifiPath := context.Query("id")
Debug.Println("renderAccessPointAuthentication: Wifi path", wifiPath)
if len(wifiPath) > 0 {
if accessPoint, err := getAccessPointByPath(wifiPath); err == nil {
render(context, gin.H{
"title": "Wifi Onboarding - Connect to " + accessPoint.Name,
"pipeline": accessPoint},
"connect.html")
} else {
Error.Println("renderAccessPointAuthentication: Access point not found")
context.AbortWithError(http.StatusNotFound, err)
}
} else {
Warning.Println("renderAccessPointAuthentication: Missing id query argument in url")
context.Redirect(http.StatusFound, "/ap/status")
}
}
// Helper function use to connect to wifi
func connectToAccessPoint(accessPointPath string, accessPointPassKey string) {
var err error
var serviceToConnect *connman.Service
serviceToConnect = nil
Debug.Printf("connectToAccessPoint: Connecting with key: %s, path: %s", accessPointPassKey, accessPointPath)
// Search for access point and save credentials
if serviceToConnect, err = getAccessPointByPath(accessPointPath); err != nil {
Error.Println("connectToAccessPoint", err)
} else {
setCredentialSSID(serviceToConnect.Name)
}
if serviceToConnect != nil {
if err := serviceToConnect.Connect(accessPointPassKey); err != nil {
Warning.Println("connectToAccessPoint: Failed to Connect", err)
setStatusError(err)
setStatusConnecting(false)
setStatusConnected(false)
setStatusHasCredentials(false)
} else {
Debug.Println("connectToAccessPoint: Connection Successful")
setStatusError(nil)
setStatusConnecting(false)
setStatusConnected(true)
}
}
}
// Endpoint handler for POST /connect
func renderConnectionStatus(context *gin.Context) {
accessPointPassKey := context.PostForm("accessPointPassKey")
accessPointPath := context.PostForm("accessPointPath")
setCredentialPath(accessPointPath)
setCredentialPSK(accessPointPassKey)
setStatusHasCredentials(true)
setStatusConnecting(true)
status := getApplicationStatus()
render(context, gin.H{
"title": "Wifi Onboarding - Attempting to Connect",
"pipeline": status},
"status.html")
Debug.Println("Sending stop signal to credential channel")
creds := getCredentials()
// Push to the credentialsChannel
credentialsChannel <- creds
return
}