]> git.sur5r.net Git - freertos/blob - FreeRTOS-Labs/Demo/FreeRTOS_IoT_Libraries/tools/aws_config_offline/js/generator.js
Add the Labs projects provided in the V10.2.1_191129 zip file.
[freertos] / FreeRTOS-Labs / Demo / FreeRTOS_IoT_Libraries / tools / aws_config_offline / js / generator.js
1 \r
2 // Check for the various File API support.\r
3 if (window.File && window.FileReader && window.FileList && window.Blob) {\r
4   // Success! All the File APIs are supported.\r
5 } else {\r
6   alert('Please use a web browser that supports HTML5 file APIs.')\r
7 }\r
8 \r
9 function formatCredentialTextForHeader (credentialText) {\r
10   // Replace any CR/LF pairs with a newline character.\r
11   credentialText = credentialText.replace(/\r\n/g, '\n')\r
12 \r
13   // Add line endings for C-language variable declaration.\r
14   credentialText = credentialText.replace(/\n/g, '\\n" \\\n    "')\r
15 \r
16   // Remove '\n"' from the last line of the declaration and add a semicolon.\r
17   credentialText = credentialText.slice(0, -9) + '"\n'\r
18   return credentialText\r
19 }\r
20 \r
21 function generateCertificateConfigurationHeader () {\r
22   var pemCertificateText = ''\r
23   var pemPrivateKeyText = ''\r
24   var filename = 'aws_iot_demo_profile.h'\r
25   var outputText = ''\r
26 \r
27   var readerCertificate = new FileReader()\r
28   var readerPrivateKey = new FileReader()\r
29 \r
30   // Start certificate read\r
31   readerCertificate.readAsText(pemInputFileCertificate.files[0])\r
32 \r
33   // Define a handler to create appropriate client certificate file text.\r
34   readerCertificate.onload = function (e) {\r
35     pemCertificateText = e.target.result\r
36 \r
37     // Add C-language variable declaration plus EOL formatting.\r
38     pemCertificateText = '    "' + formatCredentialTextForHeader(pemCertificateText)\r
39 \r
40     // Because this is async, read next file inline.\r
41     readerPrivateKey.readAsText(pemInputFilePrivateKey.files[0])\r
42   }\r
43 \r
44   // Define a handler to create appropriate private key file text.\r
45   readerPrivateKey.onload = function (e) {\r
46     pemPrivateKeyText = e.target.result\r
47 \r
48     // Add C-language variable declaration plus EOL formatting.\r
49     pemPrivateKeyText = '    "' + formatCredentialTextForHeader(pemPrivateKeyText)\r
50 \r
51     outputText = awsIotProfileTemplate\r
52     outputText = outputText.replace('<IOTEndpoint>', '"' + document.getElementById('AWSEndpoint').value + '"')\r
53     outputText = outputText.replace('<IOTThingName>', '"' + document.getElementById('thingName').value + '"')\r
54     outputText = outputText.replace('<ClientCertificatePEM>', pemCertificateText)\r
55     outputText = outputText.replace('<ClientPrivateKeyPEM>', pemPrivateKeyText)\r
56 \r
57     // Because this is async, handle download generation inline.\r
58     var downloadBlob = new Blob([outputText], { type: 'text/plain' })\r
59     if (window.navigator.msSaveOrOpenBlob) {\r
60       window.navigator.msSaveBlob(downloadBlob, filename)\r
61     } else {\r
62       var downloadLink = document.createElement('a')\r
63       downloadLink.href = window.URL.createObjectURL(downloadBlob)\r
64       downloadLink.download = filename\r
65       document.body.appendChild(downloadLink)\r
66       downloadLink.click()\r
67       document.body.removeChild(downloadLink)\r
68     }\r
69   }\r
70 }\r