9 Replies Latest reply on Nov 20, 2014 11:09 AM by deium

    Modifying Setup Webpage

    jblackston

      Is there a way to modify the setup webpage so I can change what is allowed to be edited and the color and the look of the page? Where are these files found on the Edison?

        • 1. Re: Modifying Setup Webpage
          Intel_Peter

          Hello jblackston,

           

          You can find the file if you follow this path:

          /usr/lib/edison_config_tools/edison-config-server.js

          You can edit the file there or you can use the next command to copy it to the SD Card:

          cp /usr/lib/edison_config_tools/edison-config-server.js /media/sdcard/edison-config-server.js

          Now you can see it on the SD Card and analyze it in your computer to see what changes you want to make.

          If you have any more doubts don't hesitate to ask.

           

           

          Regards,

                      Peter

          • 2. Re: Modifying Setup Webpage
            jblackston

            I did a check on my Edison and there is no directory /usr/lib/edison_config_tools

            • 3. Re: Modifying Setup Webpage
              Intel_Peter

              Hello jblackston,

               

              It is really strange that your Edison doesn't have a directory in /usr/lib/ called edison_config_tools because the file edison-config-server.js is stored in that directory. Make sure you have flashed the latest Yocto image into your Edison, and that you have a Wi-Fi connection using the command configure_edison --wifi .

              After you have entered your password something like this should be shown.

              captureWIFI.PNG

              And if you type that IP address in your browser this will be shown.

              c2.PNG

              Are you able to see this with your Edison?

              edisonconfigtools.PNG

              As you can see on the picture above there is a directory called /usr/lib/edison_config_tools

              Within that directory there is a file called edison-config-server.js

              edjs.PNG

              And if you extract that file to your SD Card this is the code you will see.

               

              var
                http = require('http'),
                fs = require('fs'),
                qs = require('querystring'),
                exec = require('child_process').exec,
                url = require('url');
              
              
              var site = __dirname + '/public';
              var payload, urlobj;
              var injectStatusAfter = '<h1>Edison One-time Setup</h1></a>';
              var injectPasswordSectionAfter = 'onsubmit="saveFields()">';
              var supportedExtensions = {
                "css"   : "text/css",
                "xml"   : "text/xml",
                "htm"   : "text/html",
                "html"  : "text/html",
                "js"    : "application/javascript",
                "json"  : "application/json",
                "txt"   : "text/plain",
                "bmp"   : "image/bmp",
                "gif"   : "image/gif",
                "jpeg"  : "image/jpeg",
                "jpg"   : "image/jpeg",
                "png"   : "image/png"
              }
              var STATE_DIR = '/var/lib/edison_config_tools';
              
              
              function getContentType(filename) {
                var i = filename.lastIndexOf('.');
                if (i < 0) {
                  return 'application/octet-stream';
                }
                return supportedExtensions[filename.substr(i+1).toLowerCase()] || 'application/octet-stream';
              }
              
              
              function injectStatus(in_text, statusmsg, iserr) {
                var injectStatusAt = in_text.indexOf(injectStatusAfter) + injectStatusAfter.length;
                var status = "";
                if (statusmsg) {
                  if (iserr)
                    status = '<div class="status errmsg">' + statusmsg + '</div>';
                  else
                    status = '<div class="status">' + statusmsg + '</div>';
                }
                return in_text.substring(0, injectStatusAt) + status + in_text.substring(injectStatusAt, in_text.length);
              }
              
              
              function inject(my_text, after_string, in_text) {
                var at = in_text.indexOf(after_string) + after_string.length;
                return in_text.substring(0, at) + my_text + in_text.substring(at, in_text.length);
              }
              
              
              function pageNotFound(res) {
                res.statusCode = 404;
                res.end("The page at " + urlobj.pathname + " was not found.");
              }
              
              
              // --- end utility functions
              
              
              function getStateBasedIndexPage() {
                if (!fs.existsSync(STATE_DIR + '/password-setup.done')) {
                  return inject(fs.readFileSync(site + '/password-section.html', {encoding: 'utf8'}),
                    injectPasswordSectionAfter,
                    fs.readFileSync(site + '/index.html', {encoding: 'utf8'}));
                }
                return fs.readFileSync(site + '/index.html', {encoding: 'utf8'});
              }
              
              
              function setHost(params) {
                if (!params.name) {
                  return {cmd: ""};
                }
              
              
                if (params.name.length < 5) {
                  return {failure: "The name is too short. It must be at least 5 characters long."};
                }
                return {cmd: "configure_edison --changeName " + params.name};
              }
              
              
              function setPass(params) {
                if (fs.existsSync(STATE_DIR + '/password-setup.done')) {
                  return {cmd: ""};
                }
                if (params.pass1 === params.pass2) {
                  if (params.pass1.length < 8 || params.pass1.length > 63) {
                    return {failure: "Passwords must be between 8 and 63 characters long. Please try again."};
                  }
                  return {cmd: "configure_edison --changePassword " + params.pass1};
                }
                return {failure: "Passwords do not match. Please try again."};
              }
              
              
              function setWiFi(params) {
                var exec_cmd = null, errmsg = "Unknown error occurred.";
                if (!params.ssid) {
                  return {cmd: ""};
                } else if (!params.protocol) {
                  errmsg = "Please specify the network protocol (Open, WEP, etc.)";
                } else if (params.protocol === "OPEN") {
                  exec_cmd = "configure_edison --changeWiFi OPEN '" + params.ssid + "'";
                } else if (params.protocol === "WEP") {
                  if (params.netpass.length == 5 || params.netpass.length == 13)
                    exec_cmd = "configure_edison --changeWiFi WEP '" + params.ssid + "' '" + params.netpass + "'";
                  else
                    errmsg = "The supplied password must be 5 or 13 characters long.";
                } else if (params.protocol === "WPA-PSK") {
                    if (params.netpass && params.netpass.length >= 8 && params.netpass.length <= 63) {
                      exec_cmd = "configure_edison --changeWiFi WPA-PSK '" + params.ssid + "' '" + params.netpass + "'";
                    } else {
                      errmsg = "Password must be between 8 and 63 characters long.";
                    }
                } else if (params.protocol === "WPA-EAP") {
                    if (params.netuser && params.netpass)
                      exec_cmd = "configure_edison --changeWiFi WPA-EAP '" + params.ssid + "' '" + params.netuser + "' '"
                        + params.netpass + "'";
                    else
                      errmsg = "Please specify both the username and the password.";
                } else {
                  errmsg = "The specified network protocol is not supported."
                }
              
              
                if (exec_cmd) {
                  return {cmd: exec_cmd};
                }
                return {failure: errmsg};
              }
              
              
              function submitForm(params, res, req) {
                var calls = [setPass, setHost, setWiFi];
                var result = null, commands = ['sleep 5'];
              
              
                // check for errors and respond as soon as we find one
                for (var i = 0; i < calls.length; ++i) {
                  result = calls[i](params, req);
                  if (result.failure) {
                    res.end(injectStatus(getStateBasedIndexPage(), result.failure, true));
                    return;
                  }
                  commands.push(result.cmd);
                }
              
              
                // no errors occurred. Do success response.
                exec ('configure_edison --showNames', function (error, stdout, stderr) {
                  var nameobj = {hostname: "unknown", ssid: "unknown"};
                  try {
                    nameobj = JSON.parse(stdout);
                  } catch (ex) {
                    console.log("Could not parse output of configure_edison --showNames (may not be valid JSON)");
                    console.log(ex);
                  }
              
              
                  var hostname = nameobj.hostname;
                  var res_str;
                  var device_ap_ssid = nameobj.ssid;
              
              
                  if (params.name) {
                    hostname = params.name;
                    device_ap_ssid = params.name;
                  }
              
              
                  if (params.ssid) { // WiFi is being configured
                    res_str = fs.readFileSync(site + '/exit.html', {encoding: 'utf8'})
                  } else {
                    res_str = fs.readFileSync(site + '/exiting-without-wifi.html', {encoding: 'utf8'})
                  }
              
              
                  res_str = res_str.replace(/params_ssid/g, params.ssid); // leaves exiting-without-wifi.html unchanged
                  res_str = res_str.replace(/params_hostname/g, hostname + ".local");
                  res_str = res_str.replace(/params_ap/g, device_ap_ssid);
                  res.end(res_str);
              
              
                  // Now execute commands
                  commands.push("configure_edison --disableOneTimeSetup");
                  for (var i = 0; i < commands.length; ++i) {
                    if (!commands[i]) {
                      continue;
                    }
                    console.log("Executing command: " + commands[i]);
                    exec(commands[i], function(error, stdout, stderr) {
                      if (error) {
                        console.log("Error occurred:");
                        console.log(stderr);
                      }
                      console.log(stdout);
                    });
                  }
                });
              }
              
              
              function handlePostRequest(req, res) {
                var params = qs.parse(payload);
                if (urlobj.pathname === '/submitForm') {
                  submitForm(params, res, req);
                } else {
                  pageNotFound(res);
                }
              }
              
              
              // main request handler. GET requests are handled here.
              // POST requests are handled in handlePostRequest()
              function requestHandler(req, res) {
              
              
                urlobj = url.parse(req.url, true);
                payload = "";
              
              
                // POST request. Get payload.
                if (req.method === 'POST') {
                  req.on('data', function (data) {
                    payload += data;
                  });
                  req.on('end', function () {
                    handlePostRequest(req, res);
                  });
                  return;
                }
              
              
                // GET request
                if (!urlobj.pathname || urlobj.pathname === '/' || urlobj.pathname === '/index.html') {
                  if (fs.existsSync(STATE_DIR + '/one-time-setup.done')) {
                    var res_str = fs.readFileSync(site + '/status.html', {encoding: 'utf8'});
                    var myhostname, myipaddr;
                    var cmd = 'configure_edison --showWiFiIP';
                    console.log("Executing: " + cmd);
                    exec(cmd, function (error, stdout, stderr) {
                      if (error) {
                        console.log("Error occurred:");
                        console.log(stderr);
                        myipaddr = "unknown";
                      } else {
                        myipaddr = stdout;
                      }
                      console.log(stdout);
              
              
                      cmd = 'hostname';
                      console.log("Executing: " + cmd);
                      exec(cmd, function (error, stdout, stderr) {
                        if (error) {
                          console.log("Error occurred:");
                          console.log(stderr);
                          myhostname = "unknown";
                        } else {
                          myhostname = stdout;
                        }
                        console.log(stdout);
              
              
                        res_str = res_str.replace(/params_ip/g, myipaddr);
                        res_str = res_str.replace(/params_hostname/g, myhostname);
                        res.end(res_str);
                      });
                    });
                  } else {
                    res.end(getStateBasedIndexPage());
                  }
                } else { // for files like .css and images.
                  if (!fs.existsSync(site + urlobj.pathname)) {
                    pageNotFound(res);
                    return;
                  }
                  fs.readFile(site + urlobj.pathname, function (err, data) {
                    if (err)
                      throw err;
                    res.setHeader('content-type', getContentType(urlobj.pathname));
                    res.end(data);
                  });
                }
              }
              
              
              http.createServer(requestHandler).listen(80);
              

               

              If you have any more doubts, don't hesitate to ask.

               

              Regards,

                          Peter

              • 4. Re: Modifying Setup Webpage
                jblackston

                Thanks that did the trick.

                 

                One other question. When you set the access point password is also sets the root password to that. Is there any way to set different passwords so that in production the access point isn't the root password to the Linux box?

                • 5. Re: Modifying Setup Webpage
                  Intel_Peter

                  Hello jblackston,

                   

                  If you type configure_edison --setup, the Linux console will ask you to change the name of your Edison, after you have entered it the Edison will ask you to enter a new root password and confirm it. Once this process is done, the Edison will try to setup a Wi-Fi connection, it will something like the first picture of my last post. You are free to set these passwords as you please, the root password on the Edison and of course the Wi-Fi password that you set in your WLAN.

                   

                  If you have any more doubts don't hesitate to ask.

                   

                  Regards,

                              Peter

                  • 6. Re: Modifying Setup Webpage
                    jblackston

                    When I do a "configure_edison --changePassword somestuff" it not only sets that password as the root login password but also the password for the AP. I of course want two different passwords. I'm trying to do this as a simple script to help with installs on production. I'm assuming the configure_edison --setup does the same thing as --changePassword.

                    • 7. Re: Modifying Setup Webpage
                      Intel_Peter

                      Hello jblacksotn,

                       

                      I think you're right it does set the password for both the root and APM, I've been reading a little and as far as I understand there isn't a way to separate the APM password from the root password, but I'll research a little more and if I find a way to do it, I will let you know. However if you manage to do it I invite you to share your method in the community.

                      If you have any more doubts don't hesitate to ask.

                       

                      Regards,

                                  Peter

                      • 8. Re: Modifying Setup Webpage
                        jblackston

                        Does the Edison support the passwd command? I don't have the ability to try it right now so I'm just asking. Then you could configure the edison and then perform the passwd to reset just the root password after you had set the AP password.

                        • 9. Re: Modifying Setup Webpage
                          deium

                          jblackston

                          The beautiful thing about the Intel Edison is that it harnesses the power of the Intel Atom on a Linux platform (the supported Yocto flavour by default) and the world of electronics (SPI, I2S, I2C, GPIO, UART) in such a small form factor.  As a development platform, the fact that we have access to source code, an onboard compiler, a provided IDE, multiple language support (C, C++, Python, JavaScript - and I personally program in my favorite Kylix 3, a flavour of pascal), -- everything becomes possible (except that which is hard coded, hard-wired, and not exposed via an API).  It is through our "development" research, reading, coding, experimenting, failures and successes that we as Makers Make things.  We are only limited by our imagination if we are willing to devote the time and effort to those research items, and willing to risk the experiment to test our code against our assumptions to achieve our dreams.  One trick and not a defined path to a dream is to realize that there are many pathways to get there - how you choose to get there is what makes your project unique.  Some call it coding, others call it a hack.

                           

                          trying the passwd command - reveals that it is supported.  Researching Yocto Linux would show it should be included.

                          You could also add a new user to your Edison so that you separate root superuser functions from enduser access (of which you define).

                           

                          adduser newuser

                          will then prompt you for a password for the newuser and create a user directory in the /home/newuser folder.

                          logins for newuser are limited to the functions that the root has permitted that newuser (as much or as little as you wish)

                          You can have several user types all with different logins and different accesses.

                          You will highly need to brush up on the Yocto Linux for this, but it is very powerful.

                           

                          The pathways are endless, limited only by the amount of research and experimenting you have done.

                          Only testing will give you which of the several paths you have tried are a better choice for your project.

                           

                          As for the original question, Intel_Peter has answered and graphically demonstrated how to get there.

                          > Is there a way to modify the setup webpage so I can change what is allowed to be edited and the color and the look of the page? Where are these files found on the Edison?