-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig.bgs
More file actions
196 lines (165 loc) · 6.27 KB
/
Copy pathconfig.bgs
File metadata and controls
196 lines (165 loc) · 6.27 KB
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
export const FLASH_PS_KEY_SERIAL_NUMBER = $8001
export const FLASH_PS_KEY_PASSWORD = $8002
export const FLASH_PS_KEY_APP_MODE = $8100
export const FLASH_PS_KEY_CLIENT_MODE_ENDPOINT = $8101
export const FLASH_PS_KEY_SETUP_SUCCESS = $8102
const API_STORE_MODE = 0
const API_STORE_SSID = 1
const API_STORE_PASSWORD = 2
const API_STORE_ENDPOINT = 3
const CONFIG_DOMAIN() = "flapos.setup"
const CONFIG_DOMAIN_LEN = 12
const RESOURCE_SSIDS() = "/api/ssids"
const RESOURCE_SSIDS_LEN = 10
const RESOURCE_CONF() = "/api/conf"
const RESOURCE_CONF_LEN = 9
const STORE_MODE_PATH() = "/mode"
const STORE_MODE_PATH_LEN = 5
const STORE_SSID_PATH() = "/ssid"
const STORE_SSID_PATH_LEN = 5
const STORE_PASSWORD_PATH() = "/password"
const STORE_PASSWORD_PATH_LEN = 9
const STORE_ENDPOINT_PATH() = "/endpoint"
const STORE_ENDPOINT_PATH_LEN = 9
const base_ssid_name() = "FlapOS_"
dim ssid_name(17)
dim scan_result_index
dim selected_mode
dim while_counter
dim ssid_request
dim handled
dim should_reset
export procedure start_conf_mode()
call flash_ps_load(FLASH_PS_KEY_SERIAL_NUMBER)(result, flash_result_len, flash_result_data(0:flash_result_len))
call tcpip_configure(192.168.1.1, 255.255.255.0, 192.168.1.1, 0)
call system_set_max_power_saving_state(0)
memcpy(ssid_name(0), base_ssid_name(0), 7)
memcpy(ssid_name(7), flash_result_data(5), flash_result_len - 5)
call sme_start_ap_mode(1, 0, 7 + flash_result_len - 5, ssid_name(:))
end
procedure method_not_allowed(allowed_method)
response(0:33) = "HTTP/1.1 405 Method Not Allowed\r\n"
if allowed_method = HTTP_POST then
response(33:15) = "Allow: POST\r\n\r\n"
response_len = 48
else
response(33:14) = "Allow: GET\r\n\r\n"
response_len = 47
end if
end
procedure handle_ssids_request(request)
response(0:52) = "HTTP/1.1 200 OK\r\nContent-type: application/json\r\n\r\n["
response_len = 52
call https_api_response(request, response_len, response(0:response_len))
ssid_request = request
scan_result_index = 0
call sme_scan_results_sort_rssi(10)
end
procedure handle_conf_request(request, path_len, path_data())
if path_len = STORE_MODE_PATH_LEN && memcmp(path_data(0), STORE_MODE_PATH(0), path_len) then
call util_atoi(1, request_data(0:1))(selected_mode)
call flash_ps_save(FLASH_PS_KEY_APP_MODE, 1, selected_mode)(result)
handled = 1
should_reset = 1
end if
if path_len = STORE_SSID_PATH_LEN && memcmp(path_data(0), STORE_SSID_PATH(0), path_len) then
call flash_ps_save(FLASH_PS_KEY_CLIENT_SSID, request_data_len, request_data(0:request_data_len))
handled = 1
end if
if path_len = STORE_PASSWORD_PATH_LEN && memcmp(path_data(0), STORE_PASSWORD_PATH(0), path_len) then
if request_data_len > 0 then
call flash_ps_save(FLASH_PS_KEY_CLIENT_PW, request_data_len, request_data(0:request_data_len))
else
call flash_ps_erase(FLASH_PS_KEY_CLIENT_PW)
end if
handled = 1
end if
if path_len = STORE_ENDPOINT_PATH_LEN && memcmp(path_data(0), STORE_ENDPOINT_PATH(0), path_len) then
call flash_ps_save(FLASH_PS_KEY_CLIENT_MODE_ENDPOINT, request_data_len, request_data(0:request_data_len))
handled = 1
end if
if handled = 0 then
return
end if
response(0:19) = "HTTP/1.1 200 OK\r\n\r\n"
response_len = 19
end
# event sme_ap_mode_started
export procedure config_sme_ap_mode_started(hw_interface)
call https_add_path(0, 1, "/")
call https_add_path(1, 5, "/api/")
call flash_ps_save(FLASH_PS_KEY_DNSS_URL, CONFIG_DOMAIN_LEN, CONFIG_DOMAIN(0:CONFIG_DOMAIN_LEN))
call flash_ps_save(FLASH_PS_KEY_DNSS_ANY_URL, 1, 1)
call https_enable(1, 1, 1)
call endpoint_send(0, 22, "[DBG]AP mode started\r\n")
call start_status_notification(LED_ORANGE, INTERVAL_CONNECTED)
end
# event https_api_request_finished
export procedure config_https_api_request_finished(request)
handled = 0
should_reset = 0
if request_resource_len = RESOURCE_SSIDS_LEN && memcmp(request_resource(0), RESOURCE_SSIDS(0), RESOURCE_SSIDS_LEN) then
if request_method = HTTP_GET then
call handle_ssids_request(request)
return
else
call method_not_allowed(HTTP_POST)
end if
handled = 1
end if
if request_resource_len > RESOURCE_CONF_LEN && memcmp(request_resource(0), RESOURCE_CONF(0), RESOURCE_CONF_LEN) then
if request_method = HTTP_POST then
call handle_conf_request(request, request_resource_len - RESOURCE_CONF_LEN, request_resource(RESOURCE_CONF_LEN:request_resource_len - RESOURCE_CONF_LEN))
else
call method_not_allowed(HTTP_GET)
handled = 1
end if
end if
if handled = 0 then
response(0:26) = "HTTP/1.1 404 Not Found\r\n\r\n"
response_len = 26
end if
call https_api_response(request, response_len, response(0:response_len))
call https_api_response_finish()
if should_reset = 1 then
call flash_ps_save(FLASH_PS_KEY_SETUP_SUCCESS, 1, 0)
call system_reset(0)
end if
end
# event sme_scanned
export procedure config_sme_scanned(status)
call start_conf_mode()
end
# event sme_scan_sort_result
export procedure config_sme_scan_sort_result(channel, rssi, snr, secure, ssid_len, ssid_data())
# filtering empty SSID
if ssid_len = 0 then
return
end if
# filtering SSID with invalid characters
while_counter = 0
while while_counter < ssid_len
if ssid_data(while_counter:1) < 32 then
return
end if
while_counter = while_counter + 1
end while
response_len = 0
if scan_result_index > 0 then
response(response_len:1) = ","
response_len = response_len + 1
end if
response(response_len:1) = "\x22"
response_len = response_len + 1
memcpy(response(response_len), ssid_data(0), ssid_len)
response_len = response_len + ssid_len
response(response_len:1) = "\x22"
response_len = response_len + 1
call https_api_response(ssid_request, response_len, response(0:response_len))
scan_result_index = scan_result_index + 1
end
# event sme_scan_sort_finished
export procedure config_sme_scan_sort_finished()
call https_api_response(ssid_request, 1, "]")
call https_api_response_finish()
end