 Welcome everybody. My name is Mehmet Fatih Karagas and today with my colleague Jevahir we will introduce you any software framework node and how we can use this framework on embedded Linux devices. Let's start with our agenda. We will start with some brief information about this framework node, its advantages and disadvantages. Then we will move to our second section, how we can cross compile node for embedded Linux devices and it's also package manager. Then we will have a look at the development environment and on my final part I will show you some scripting samples that can be usable on embedded devices. Then I will give work to my friend and he will tell the development story of our surveillance application. Okay, let's start with some brief information about this framework. Let's start what is node. This is the official website definition from node's website. You see that node is a platform that's built on Google's V8 JavaScript runtime and it's for building fast scalable network applications. Its main features are it's firstly event driven. It uses an event queue instead of a multi-traded approach. The second one, it uses a non-blocking IO model which enforces us to use asynchronous programming model and none of nodes internal functions doing blocking IO. And it's lightweight. It's around 5 megabyte file size in binaries and it uses about 15 megabytes RAM usage on our sample application which we will show in demo. And it's efficient hardware usage enables us to use it on embedded systems too. Like all frameworks node also have some advantages and some disadvantages. When we look at the advantages we see that it's open source so you don't have to pay for any license fees. As we said previously it's hardware efficiency enables it to use it on embedded systems too. It has a fast learning curve and I will explain in the next slide. The development time is very short especially on network applications when you are doing lots of IO compared to CRC++ and we experienced it in our prototype surveillance application too. And the language is JavaScript. It's JIT compiled just in time compiled language so it's easy to learn it with respect to CRC++ or CRC languages. And you don't need to compile any code so you can fix sandbox immediately and you don't have to create new binaries and push them to your devices. Also the package manager is also very nice. You can find many modules for lots of different purposes and it's very easy to install them with just single line of command. When we look at the disadvantages we see that it's a new framework so it has a small developer pool. It can be hard to find local expert guys but it's an active and growing community. It has very good communities so you can find help on internet. Also it's embedded CPU bound traps. When you do CPU intensive coding you block all the event queue and when you do blocking IO you block also all the event queue so you should be very careful when programming. Keeping in mind that you're doing asynchronous programming. And finally asynchronous debugging is also hard with respect to synchronous debugging. It's hard to trace the memory and finding memory leaks. Next we look at learning curve. Don't use JavaScript. It's comparably easy language to learn and if you know programming you can adapt to it very easily. And most of the web developers are already familiar with the language so this is a plus in my idea. When you are following the tutorials on internet you can start building your basic applications I think in less than an hour and it also includes the installation time too. Okay when we look at the package manager it's name is ampium not packaged modules. It installs modules very easily and it also works on embedded systems too. As you can see just with one line of command ampium install express we are installing the express module it's a module for restful web applications. And it also installs dependent modules too so you don't have to worry about them and it has a global install option. So if you have many applications using nodes and you have common modules you can install them globally and you don't have to install for each of them. And these are some examples of the popular modules on ampium website. Here we use express for our restful web server and socket IO we use for web client and server web socket communication. And also you can find much more on ampium these are just examples. We just mentioned some brief information about node and in this part I will explain you how we can cross compile node and its package manager ampium. Okay firstly the official address of node is nodejs.org and you can get the tar balls from slash download address also you can use git to get the code. Also its license is MIT license which is quite flexible so you can do changes. We built node using classic slash configure and make and make install but when we do it on embedded systems there are some important configuration options that we must choose carefully. Our first option is without ampium option. If you select this option you don't get the ampium package built with it and our second option is SSL option. Normally node comes open SSL built with it but if you don't want SSL support or not you can choose this option. Without SSL option is important because if you want to use ampium you must have SSL support because ampium uses SSL for downloading modules from internet. Our third option is without SSL option. We can't have SSL views when we are cross compiling so if you want to have SSL on your embedded system you either build node on your target or use an emulator system and build on it. And what we lose without Snapshot the handicap is the starting time of node instance. The V8 instance initialization time is shortened with the Snapshot support but when we don't have it we will wait around 1-2 seconds I think when we are just initializing the node. And the other options I think are clear, this CPU and this OS options which are for our case ARM and Linux. After deciding our configuration options now we export our cross compile binaries and then we do configure with our options and we make and make install. I personally extract them to a folder and then copy them to my embedded device and then after these steps I think we are ready to execute the node on our target. Okay let's look at the development environment. Actually you can use any editor which supports JavaScript because it's not a compiled language but some editors have some pluses. I personally use GetIt when I'm working on Ubuntu and I'm using WebStorm working on Mac. The WebStorm has nice features such as code completion and some debugging support also. Also you have a bunch of options these are just examples. For example Cloud9 is cloud based editor you can use traditional IDs such as Eclipse and it also has a plugin. So you have many options and you can decide which ever you are comfortable. Not also internal debug options too and you start your node script using node debug myscript.js example. And you are in debug mode and you have some options here like setting and clearing breakpoints or setting watchers or look at the variable values on runtime. Some others IDs like WebStorm already have an interface for it if you don't want to do it on console. But personally I not used debugging in detail the console logs were enough for me on my experiences. Okay we now have the infrastructure for developing node and where can we use it? We can use it for scripting samples that can be used instead of bash maybe. And we have a development story which my friend Jelire will explain in detail. Now I will show you some scripting samples that we can use by node. Our first sample is letonoffunction. Here we use the exec function of the child process module of node. And here we have a function set leton and it sets the let's value to on and here we use exec and make a system call to the let's brightness. We print an echo one and this is a scene and asynchronous function and when this system call is completed it goes to this function which is a callback function and it prints this string to the console. Also you can of course set echo to zero so you will have a letoff function too. Actually with this function you can write your own factor applications to exec can execute any binary and you can trace their status and you can relaunch them maybe when they die. And second example is the CPU memory usage application. We here use the OS module from the nodes internal models and here we have three functions load average function, free memory function and total memory function. This one returns us an array of three which gives one minute, five minutes and 15 minutes average values of the CPU load and we print it to console with this command. And in our second example we get the free memory amount and the total memory amount of the system and we print them to console with this. So you can do some memory CPU tracing application using these simple functions. My third example is the timers. I think it's very powerful and easy to implement in JavaScript and Node. Here we are doing a lead blinking application which makes lead one second on and one second off and it goes like this. And here we have this timer function set interval and this is the timeout value which is one second and each one second it called this function. This function is defined here and as you remember this code is similar to my first example. Here just we change the value every time it goes into function from zero to one and vice versa. So we have a blinking lead example at the output. My last example is an in a parser example. Here we parsing a sample in a file such as name with version etc. And here we use the power of NPM here we install an in a parser module from NPM with this line of command. And then we require the in a parser such as here. Then we have a function asynchronous function parse function. We give the file name and then when it finished parsing operation it goes to the callback function in the data variable. We will have the values here such as data.version or data.name you can reach the values and if there is an error occurred when doing this job it will go into the error variable. And if there is no error occurred it will be null variable so you can find if there is an error occurred or not. Now my part is finished and I will give work to my colleague and he will explain you the implementation details of our surveillance application. Thank you. Thank you Fatih. Welcome again. I'm J.Y. here and I'm going to talk about example surveillance application. The purpose is to demonstrate usage scenario of Node.js in real-world application by giving more information about network class of Node.js to run complete Node.js application in embedded platform. In the example system there are two parts. The first part is smart IP cameras which can detect motion and second part is server site which is located common control center which monitors cameras gather their alarms in order to show user. This is the deployment model of example system. We have server site common control center and we have fake IP cameras which are iGAP version 2 embedded boards and they are all connected to each other through Ethernet. Embedded Linux is running on our fake camera which is iGAP version 2 boards. We have custom kernel which has version 2637 and then we compiled our Node.js binaries and all other binaries with GCC version 263. This is the component diagram of our example system. We have as I mentioned two parts camera site and server PC. There are two software components at embedded site. The first component is fake video application to simulate alarm messages. Video application sends alarm messages to server through TCP server and then the second part is system manager application which is written using Node.js. This system manager application takes configuration from server site and sends to video application through inter process communication which is TCP connection. And system manager application also sends heartbeat messages to server then our server site listens these UDP heartbeat messages to discover online cameras. And then server application can send configuration to online cameras through TCP connection and our server application source static HTML5 pages and these pages are written in AngularJS framework. There is also socket communication between server application and static HTML files and this is the overview of our system. This is a little complicated diagram. It just also shows the messages of the system. After this point I'm going to talk about coding details of each software component. The first one is fake video application. It is fake to simulate motion detection. In real world it's actually a real video application which can detect motion or you can do any video related jobs. It has connection to camera system manager through TCP. This is the example of inter process communication between Node.js and other binaries which can be written in different languages. It can also produce periodic alarm messages at 10 or 30 seconds depending on its configuration. This is the code snapshot from the video application. This is just a TCP client to connect camera system manager application but its code is not shown in here. After TCP connection to system manager application it creates a new thread to send periodic alarm. Then it tries to read a configuration message from the system manager application. We have two sample configuration messages. One can change video source and the other configuration message can change motion detection mode. We have three options in motion detection. User can turn off the motion detection or user can turn it by setting alarm period to 10 seconds or user can change the alarm period to 30 seconds. After taking any configuration message this application send acknowledgement message to system manager application. This is all about video application just to simulate periodic alarm messages by setting its mode. This is the main application to simulate fake video camera. Camera system manager application is written in Node.js and it manages all other software elements running on camera through inter process communication. This example system takes configuration from server and sends this configuration messages to video application. There are two TCP servers and one UDP client to send heart messages. This is our first TCP server application at camera system manager site. We use NetModule of Node.js to use TCP servers and TCP clients. We create a TCP server by calling create server function of NetModule. As you can see there is a callback function. I'll talk it later. After creating this TCP server we start to listen connections from the given port from the destination address. Whenever a connection is made this callback function is executed and when fake video application connects this server this socket object is created. Then TCP socket object has event listeners. As my friend said Node doesn't have any training. It runs through event listeners and non-blocking functions. In here we have data and close event listeners. Whenever a data is taken through this socket this data event is emitted and the data is received. We can check the length of data by the data objects length member and then we can process the taken data as a byte array. As you can see we check in here opcodes. Fake video application can send acknowledgement messages and alarm messages through this TCP connection. Whenever an alarm is taken from the video application we can take it and try to send to TCP connection of TCP connection. There is another device which is TCP connection between server application which is running at common control center. In here we create second TCP server and then we try to get new connections through the given port. As you can see the host address is omitted so all connections which is direct to any IPv4 address of the host is taken by this server. As you can see we have also data and close event listeners. In here data when the data is taken from server application this code tries to send the given configuration message to our video socket. This is TCP connection between camera's management fake video application and the close event listener is called whenever the connection between server and this application is fully closed. This event listener is omitted also asynchronously. This is the UDP broadcaster to send heartbeat messages to server. We include datagram watchoff node.js to create UDP objects. By calling create socket UDP4 we can create UDP client object. After that we bind a random UDP port in order to send heartbeat messages. In here this is also a synchronous code when this client bind a random UDP port this code is executed and we set broadcast to true to enable broadcasting. Finally we create a new timer to send the heartbeat messages to server. As you can see we sent status of the camera device ID status location and video source and video mall of the video application and time. We just sent with simple method we give message of state of the message and giving UDP port and broadcast address. There is also a callback function in this callback function bytes is the bytes number that are sent successfully. The error object is the error object which you can read if there is error. If there is no error it will be null. Of course we don't check for this mistake here. This is all about our embedded part. Video application and system manager application are running on our IGAP version 2 boards. This camera system manager application takes 15 megabytes from the RAM and nearly 0 CPU power. And second part of my presentation is server application which is running on command control center. This application is connected to all online cameras. It discovers cameras by listening broadcast other messages of the cameras. It configures cameras over TCP connection and it listens other messages from camera. Finally it serves static HTML5 pages to show online cameras and taking other messages to user. This is a little complicated code but actually it's just a function. In this server application we create any UDP object from the datagram module. And you can take UDP messages by implementing a message event list of the UDP object. And this function is called whenever a message is taken from our UDP port. In here message is taken data and our info is the removed address information of the message source. After message is taken we just check if the taken message is from the registered camera or it's a newly connected camera. If it is taken from the already registered camera we just update camera information. If it is a new camera we just create a new device at the devices object. And then we save the information of the camera to newly created object. And then we get IP address of the message source from the info object. And then we create any TCP client object in here to make connection with the camera system manager application. In here we try to connect port to new camera and this is also a callback function. When the connection is made through this newly connected device this function is executed. And then we implement data close and error event listeners of the this new TCP client object. Whenever data is taken from server we just take it and send this data to web clients by calling send alarm by socket. Of course camera can send only alarm messages through this TCP connection. So we just take data, don't control and send to socket. And whenever TCP connection is closed first we send information about this connected camera to web clients and then we remove it from our devices. Server application has also HTTP server and this is a call of the HTTP server of server application. In here it serves static contents which is located in here. And it makes real-time website connection to connect to web clients. We just use express and socket.io modules from which is taken with npm package manager. HTTP module is not just core module. This is the configuration of socket.io code but I'll skip this. Web socket can make us real-time communication between server application and web clients. It has a connection event listener. We implement this connection event listener to get connection from the web clients. Whenever a web client is connected to our server application this callback function is executed. In this callback function we implement any event listener named get device list and other config event listener. Web client can request list of already connected cameras through this event. And it can request to change a setting of any camera through config event. When this config event is emitted this web socket send the configuration parameters of the video application and then it call send devices conf to send this taken data to our TCP client connection. Then we just save this new web socket to our web socket list. Server application also sends data to web clients. For example it can send camera info on alarm. We just call emit function of the web socket and we give event name and data. Taking alarm are sent through this function to all connected web clients. This is just functions used in previous slide. There are other web socket functions called by server application to send data to connected web clients. We send data through new cam, update cam and drop cam events to web clients. These are the data about these events. Web interface is served by server application. It is written in socket event, AngularJS which is AX based HTML5 development environment. Actually this is another story and I'll skip the details. This is an example usage of socket.io at web client site and this example is taken from the website of the socket.io. It just takes data from these event listeners and send data to web server by calling emit function of the socket. This is the socket.io caller our web client site. There are two functions set MD mode and set video source. Web clients can send data to server through these functions. It can change motion detection mode of the camera by calling config and message. There are also event listeners to get data from server like get devices camera info. As mentioned previously, new cam, update cam, drop cam and alarm. Whenever alarm is taken, we just get data of alarm and push this data to alarm list. AngularJS updates the when model is changed. This alarm table is updated at the web browser automatically when we update this list at the model. This is end of our presentation. Thank you for your patience. Now we'll try to make our demo. Let's start our server application. Can we power off these fake cameras? Actually, this server application can also run on this embedded fake application. It starts to listen to connections from the given port. And then we have web interface. Our client is connected but there is no online cameras. We will power on these boards. It takes nearly one minute to boot up. Our first fake camera is found. And we just show its information in here. It's IP, the latest time of the taken message from the UDP port. And camera status, camera location and video mode and video source of fake video application in here. And there is no alarm in here because motion detection mode is opened in turnoff mode. Then we connect the camera. Let's set motion detection mode to 1. And we have alarm message which is taken from our camera. In mode 1, camera sends periodic alarms at each 10 seconds. Let's change its motion detection mode to mode 2. And it just updates it with the new heartbeat message. Let's change the video source mode to thermal of device ID bit 1. Just change second. In here, it sends alarm messages at every 10 seconds. But with the change of motion detection mode, it will send heartbeat messages at 30 seconds. It will take a while for the new message. And the new message is taken. Let's change motion detection mode of the third camera. And we have alarm message from the third camera also in here. You can find the codes of demo from this address. The whole code samples are in here. And thank you for your patience. If you have any questions, we would like to answer. Actually, you can get pre-installed modules. But you have to install NPM to at least one camera and take these modules. And you can use the source of two modules at anywhere. Actually, you can use JavaScript based modules directly copied them to the modules folder. And you can use them, but some modules have C++ bindings. And they can be some problematic when you copy from your desktop PC to embedded system. So you must build them on your embedded device. If it's completely JavaScript based, it's no problem. Because I did it on my experience, it's no problem. If you have C++ coming into the game, then you can have some problems. But you can also compile it at a sample card and then use these binaries at the same configuration. Okay. Yes? I think at least hundreds of megabytes. Hundreds of megabytes, I think at least. It would be problematic in my opinion. I never tried on these lower memory devices. But the device we are working on here, the IGAP has 500 megabytes RAM. And we are using, I think, 300 megabytes for the Linux. And the other part is divided for the DSP part. So we are running on actually 300 megabytes. And we are fine. We are not using much of it. But I think below the 100 megabytes range, you can have some problems maybe. Okay. Thank you for your participation.