1const { spawnSync } = require('child_process');
2const md5File = require('md5-file');
3const https = require('https');
4
5function OpenFileHandler() {
6  console.log('Will open a dialog box ...');
7  const options = {
8    title: 'Open a file or folder',
9  };
10  let x = dialog.showOpenDialogSync(options) + '';  // Convert to string
11  console.log('file name: ' + x)
12
13  // Determine file type
14  let is_asio_log = false;
15  const data = fs.readFileSync(x, {encoding: 'utf-8'});
16  let lines = data.split('\n');
17  for (let i = 0; i < lines.length; i++) {
18    if (lines[i].indexOf('@asio') == 0) {
19      is_asio_log = true;
20      break;
21    }
22  }
23
24  if (is_asio_log) {
25    ShowBlocker('Loading Boost ASIO handler tracking log');
26    console.log('This file is a Boost Asio handler tracking log');
27    ParseBoostHandlerTimeline(data);
28    OnGroupByConditionChanged_ASIO();
29    if (boost_asio_handler_timeline_view.IsEmpty() == false) {
30      boost_asio_handler_timeline_view.CurrentFileName = x;
31      HideWelcomeScreen();
32      ShowASIOTimeline();
33      ShowNavigation();
34      UpdateFileNamesString();
35    }
36    HideBlocker();
37    return;
38  }
39
40  OpenDBusPcapFile(x);
41
42  UpdateLayout();
43}
44
45// The file may be either DBus dump or Boost Asio handler log
46document.getElementById('btn_open_file')
47    .addEventListener('click', OpenFileHandler);
48document.getElementById('btn_open_file2')
49    .addEventListener('click', OpenFileHandler);
50
51document.getElementById('bah1').addEventListener(
52    'click', OnGroupByConditionChanged_ASIO);
53document.getElementById('bah2').addEventListener(
54    'click', OnGroupByConditionChanged_ASIO);
55document.getElementById('bah3').addEventListener(
56    'click', OnGroupByConditionChanged_ASIO);
57
58// UI elements
59var g_group_by_dbus = document.getElementById('span_group_by_dbus');
60var g_group_by_ipmi = document.getElementById('span_group_by_ipmi');
61var g_group_by_asio =
62    document.getElementById('span_group_by_boost_asio_handler')
63var g_canvas_ipmi = document.getElementById('my_canvas_ipmi');
64var g_canvas_dbus = document.getElementById('my_canvas_dbus');
65var g_canvas_asio = document.getElementById('my_canvas_boost_asio_handler');
66
67var g_dbus_pcap_status_content = document.getElementById('dbus_pcap_status_content');
68var g_dbus_pcap_error_content = document.getElementById('dbus_pcap_error_content');
69var g_btn_download_dbus_pcap = document.getElementById('btn_download_dbus_pcap');
70var g_welcome_screen_content = document.getElementById('welcome_screen_content');
71var g_scapy_error_content = document.getElementById('scapy_error_content');
72
73var g_btn_zoom_reset = document.getElementById('btn_zoom_reset');
74
75function DownloadDbusPcap() {
76  const url = 'https://raw.githubusercontent.com/openbmc/openbmc-tools/08ce0a5bad2b5c970af567c2e9888d444afe3946/dbus-pcap/dbus-pcap';
77
78  https.get(url, (res) => {
79    const path = 'dbus-pcap';
80    const file_path = fs.createWriteStream(path);
81    res.pipe(file_path);
82    file_path.on('finish', () => {
83      file_path.close();
84      alert("dbus-pcap download complete!");
85      CheckDbusPcapPresence();
86    });
87  });
88}
89
90g_btn_download_dbus_pcap.addEventListener(
91  'click', DownloadDbusPcap);
92
93function ShowDBusTimeline() {
94  g_canvas_dbus.style.display = 'block';
95  g_group_by_dbus.style.display = 'block';
96}
97function ShowIPMITimeline() {
98  g_canvas_ipmi.style.display = 'block';
99  g_group_by_ipmi.style.display = 'block';
100}
101function ShowASIOTimeline() {
102  g_canvas_asio.style.display = 'block';
103  g_group_by_asio.style.display = 'block';
104}
105
106// Make sure the user has scapy.all installed
107function IsPythonInstallationOK() {
108  const x = spawnSync('python3', ['-m', 'scapy.all']);
109  return (x.status == 0);
110}
111
112function IsDbusPcapPresent() {
113  // This should exist if the openbmc-tools repository
114  // is checked out as a whole
115  const dbus_pcap = '../dbus-pcap/dbus-pcap';
116
117  if (fs.existsSync('dbus-pcap')) {
118    return true;
119  } else if (fs.existsSync(dbus_pcap)) { // Create symlink
120    fs.symlinkSync(dbus_pcap, './dbus-pcap');
121    return true;
122  } else {
123    return false;
124  }
125}
126
127function CheckDependencies() {
128  g_dbus_pcap_status_content.style.display = 'none';
129  g_dbus_pcap_error_content.style.display = 'none';
130  g_scapy_error_content.style.display = 'none';
131  g_welcome_screen_content.style.display = 'none';
132
133  const dbus_pcap_ok = IsDbusPcapPresent();
134  if (!dbus_pcap_ok) {
135    g_dbus_pcap_error_content.style.display = 'block';
136  }
137
138  const scapy_ok = IsPythonInstallationOK();
139  if (!scapy_ok) {
140    g_scapy_error_content.style.display = 'block';
141  }
142
143  let msg = "";
144  if (dbus_pcap_ok) {
145    msg += 'dbus-pcap found, md5sum: ' +
146      md5File.sync('dbus-pcap');
147    g_dbus_pcap_status_content.style.display = 'block';
148    g_dbus_pcap_status_content.textContent = msg;
149  }
150
151  if (dbus_pcap_ok && scapy_ok) {
152    g_welcome_screen_content.style.display = 'block';
153  }
154}
155
156function Init() {
157  console.log('[Init] Initialization');
158  ipmi_timeline_view.Canvas = document.getElementById('my_canvas_ipmi');
159  dbus_timeline_view.Canvas = document.getElementById('my_canvas_dbus');
160  boost_asio_handler_timeline_view.Canvas =
161      document.getElementById('my_canvas_boost_asio_handler');
162
163  // Hide all canvases until the user loads files
164  ipmi_timeline_view.Canvas.style.display = 'none';
165  dbus_timeline_view.Canvas.style.display = 'none';
166  boost_asio_handler_timeline_view.Canvas.style.display = 'none';
167
168  let v1 = ipmi_timeline_view;
169  let v2 = dbus_timeline_view;
170  let v3 = boost_asio_handler_timeline_view;
171
172  // Link views
173  v1.linked_views = [v2, v3];
174  v2.linked_views = [v1, v3];
175  v3.linked_views = [v1, v2];
176
177  // Set accent color
178  v1.AccentColor = 'rgba(0,224,224,0.5)';
179  v2.AccentColor = 'rgba(0,128,0,  0.5)';
180  v3.AccentColor = '#E22';
181
182  CheckDependencies();
183}
184
185var g_WelcomeScreen = document.getElementById('welcome_screen');
186function HideWelcomeScreen() {
187  g_WelcomeScreen.style.display = 'none';
188}
189
190var g_Blocker = document.getElementById('blocker');
191var g_BlockerCaption = document.getElementById('blocker_caption');
192function HideBlocker() {
193  g_Blocker.style.display = 'none';
194}
195function ShowBlocker(msg) {
196  g_Blocker.style.display = 'block';
197  g_BlockerCaption.textContent = msg;
198}
199
200var g_Navigation = document.getElementById('div_navi_and_replay');
201function ShowNavigation() {
202  g_Navigation.style.display = 'block';
203}
204
205function UpdateFileNamesString() {
206  let tmp = [];
207  if (ipmi_timeline_view.CurrentFileName != '') {
208    tmp.push('IPMI timeline: ' + ipmi_timeline_view.CurrentFileName)
209  }
210  if (dbus_timeline_view.CurrentFileName != '') {
211    tmp.push('DBus timeline: ' + dbus_timeline_view.CurrentFileName)
212  }
213  if (boost_asio_handler_timeline_view.CurrentFileName != '') {
214    tmp.push(
215        'ASIO timeline: ' + boost_asio_handler_timeline_view.CurrentFileName);
216  }
217  let s = tmp.join('<br/>');
218  document.getElementById('capture_info').innerHTML = s;
219}
220
221var g_cb_debug_info = document.getElementById('cb_debuginfo');
222
223
224Init();
225