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