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
6 alert('Please use a web browser that supports HTML5 file APIs.')
\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
13 // Add line endings for C-language variable declaration.
\r
14 credentialText = credentialText.replace(/\n/g, '\\n" \\\n "')
\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
21 function generateCertificateConfigurationHeader () {
\r
22 var pemCertificateText = ''
\r
23 var pemPrivateKeyText = ''
\r
24 var filename = 'aws_iot_demo_profile.h'
\r
27 var readerCertificate = new FileReader()
\r
28 var readerPrivateKey = new FileReader()
\r
30 // Start certificate read
\r
31 readerCertificate.readAsText(pemInputFileCertificate.files[0])
\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
37 // Add C-language variable declaration plus EOL formatting.
\r
38 pemCertificateText = ' "' + formatCredentialTextForHeader(pemCertificateText)
\r
40 // Because this is async, read next file inline.
\r
41 readerPrivateKey.readAsText(pemInputFilePrivateKey.files[0])
\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
48 // Add C-language variable declaration plus EOL formatting.
\r
49 pemPrivateKeyText = ' "' + formatCredentialTextForHeader(pemPrivateKeyText)
\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
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
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