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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
"""
Copyright 2023 Suchinton Chakravarty
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import os
import platform
import can
from configparser import ConfigParser
import logging
python_version = f"python{'.'.join(platform.python_version_tuple()[:2])}"
def check_paths(*paths):
return {path: os.path.exists(path) for path in paths}
USER_CONFIG_PATH = os.path.join(os.path.expanduser("~"),
".local/share/agl-demo-control-panel/config.ini")
CONFIG_PATHS = check_paths(
USER_CONFIG_PATH,
"/etc/agl-demo-control-panel/config.ini",
"/etc/agl-demo-control-panel.ini",
os.path.abspath(os.path.join(os.path.dirname(__file__), 'config.ini'))
)
CA_PATHS = check_paths(
"/etc/kuksa-val/CA.pem",
f"/usr/lib/{python_version}/site-packages/kuksa_client/kuksa_server_certificates/CA.pem",
os.path.abspath(os.path.join(os.path.dirname(
__file__), "../assets/cert/CA.pem"))
)
WS_TOKEN_PATHS = check_paths(
f"/usr/lib/{python_version}/site-packages/kuksa_client/kuksa_server_certificates/jwt/super-admin.json.token",
os.path.join(os.path.expanduser(
"~"), f".local/lib/{python_version}/site-packages/kuksa_client/kuksa_server_certificates/jwt/super-admin.json.token")
)
GRPC_TOKEN_PATHS = check_paths(
os.path.abspath(os.path.join(os.path.dirname(__file__),
"../assets/token/grpc/actuate-provide-all.token"))
)
config = ConfigParser()
config_path = next((path for path, exists in CONFIG_PATHS.items() if exists), None)
if config_path:
config.read(config_path)
PLAYBACK_FILE_PATHS = check_paths(
config.get('default', 'file-playback-path', fallback=None),
"/home/agl-driver/can_messages.txt",
"/etc/agl-demo-control-panel/can_messages.txt",
os.path.abspath(os.path.join(os.path.dirname(__file__), "../assets/can_messages.txt"))
)
DBC_FILE_PATHS = check_paths(
config.get('default', 'dbc-file-path', fallback=None),
"/etc/kuksa-dbc-feeder/agl-vcar.dbc",
os.path.abspath(os.path.join(os.path.dirname(__file__), "../Scripts/agl-vcar.dbc"))
)
CA_PATH = next((path for path, exists in CA_PATHS.items() if exists), None)
WS_TOKEN = next((path for path, exists in WS_TOKEN_PATHS.items() if exists), None)
GRPC_TOKEN = next((path for path, exists in GRPC_TOKEN_PATHS.items() if exists), None)
PLAYBACK_FILE = next((path for path, exists in PLAYBACK_FILE_PATHS.items() if exists), None)
KUKSA_CONFIG = {}
KUKSA_TOKEN = None
def load_config():
"""
Initializes configuration from the config.ini file.
Args:
Returns:
Tuple[str, Dict[Union[str, bool]], str]: A tuple containing a dictionary containing
the configuration option and the token to use for the selected protocol.
"""
global KUKSA_CONFIG, KUKSA_TOKEN
KUKSA_CONFIG.clear()
if config.has_section('vss-server'):
KUKSA_CONFIG['ip'] = config['vss-server']['ip']
KUKSA_CONFIG['port'] = config['vss-server']['port']
KUKSA_CONFIG['protocol'] = config['vss-server']['protocol']
KUKSA_CONFIG['insecure'] = False if config['vss-server']['insecure'] == 'False' else True
if config.has_option('vss-server', 'cacert'):
KUKSA_CONFIG['cacertificate'] = config['vss-server']['cacert'] if os.path.exists(
config['vss-server']['cacert']) else CA_PATH
else:
KUKSA_CONFIG['cacertificate'] = None
KUKSA_CONFIG['tls_server_name'] = config['vss-server']['tls_server_name']
if config.has_option('vss-server', 'token'):
if config['vss-server']['token'] == 'default':
KUKSA_TOKEN = get_default_token(KUKSA_CONFIG['protocol'])
elif os.path.exists(config['vss-server']['token']):
KUKSA_TOKEN = config['vss-server']['token']
else:
ValueError(
f"Token file {config['vss-server']['token']} not found")
else:
raise ValueError(
f"Config section {'vss-server'} not found in config.ini")
return KUKSA_CONFIG, KUKSA_TOKEN
def get_default_token(protocol):
if protocol == 'grpc':
return GRPC_TOKEN
else:
return WS_TOKEN
def save_config(new_config, auth_token, CA_File=None):
"""
save values to config.ini under [vss-server]
"""
config.set('vss-server', 'ip', str(new_config['ip']))
config.set('vss-server', 'port', str(new_config['port']))
config.set('vss-server', 'protocol', str(new_config['protocol']))
config.set('vss-server', 'insecure', str(new_config['insecure']))
config.set('vss-server', 'tls_server_name',
str(new_config['tls_server_name']))
if auth_token in WS_TOKEN_PATHS or auth_token in GRPC_TOKEN_PATHS or auth_token == 'default':
config.set('vss-server', 'token', 'default')
else:
config.set('vss-server', 'token', str(auth_token))
if CA_File in CA_PATHS or CA_File == 'default':
config.set('vss-server', 'cacert', 'default')
else:
config.set('vss-server', 'cacert', str(CA_File))
# Always save to the user config
config_path = USER_CONFIG_PATH
os.makedirs(os.path.dirname(config_path), exist_ok=True)
with open(config_path, 'w') as configfile:
config.write(configfile)
# check the keypad-features wanted
def keypad_enabled():
return config.getboolean('keypad-feature', 'enabled', fallback=False)
def keypad_only():
return config.getboolean('keypad-feature', 'keypad-only', fallback=False)
def get_keypad_config():
# return the ip and port of the keypad if specified, else return localhost and 8080
ip = config.get('keypad-feature', 'ip', fallback=False)
port = config.get('keypad-feature', 'port', fallback=False)
return ip if ip else "localhost", port if port else "8080"
def get_keypad_keys_to_hide():
# return the keys to hide if specified, else return an empty
keys = config.get('keypad-feature', 'keys-to-hide', fallback=False)
return keys.split(',') if keys else []
def fullscreen_mode():
return config.getboolean('default', 'fullscreen-mode', fallback=False)
def hvac_enabled():
return config.getboolean('default', 'hvac-enabled', fallback=True)
def steering_wheel_enabled():
return config.getboolean('default', 'steering-wheel-enabled', fallback=True)
def file_playback_enabled():
return config.getboolean('default', 'file-playback-enabled', fallback=True)
def get_playback_file():
if PLAYBACK_FILE is not None:
return PLAYBACK_FILE
else:
# save file in project baseDir/Scripts/can_messages.txt
with open(os.path.join(os.path.dirname(__file__), "can_messages.txt"), "w") as file:
file.write("")
# update config.ini
config.set('default', 'file-playback-path', os.path.join(os.path.dirname(__file__), "can_messages.txt"))
with open(USER_CONFIG_PATH, 'w') as configfile:
config.write(configfile)
return os.path.join(os.path.dirname(__file__), "can_messages.txt")
# Function to get the dbc file path from config.ini
def get_dbc_file():
if DBC_FILE_PATHS:
return DBC_FILE_PATHS
else:
raise ValueError("DBC file path not found")
# Function to get the can interface name from config.ini
def get_can_interface():
print(config.get('default', 'can-interface', fallback=None))
return config.get('default', 'can-interface', fallback=None)
# function to check if can interface is enabled or not
def can_interface_enabled():
can_interface = get_can_interface()
if can_interface is not None:
try:
# check if an interface by the name exists
can.interface.Bus(interface='socketcan', channel=can_interface)
return True
except Exception as e:
logging.error(f"Error: {e}")
return False
return False
if not config.has_section('vss-server'):
config.add_section('vss-server')
temp = {
'ip': "localhost",
'port': "55555",
'protocol': "grpc",
'insecure': "",
'cacert': "",
'token': "",
'tls_server_name': "",
}
save_config(temp, 'default', 'default')
|