Friday Aug 09, 2013

Nescide: An IDE for Hacking the TinyOS Platform

nesC (network embedded systems C), pronounced "NES-see", is a component-based, event-driven programming language used to build applications for the TinyOS platform. TinyOS is an operating environment designed to run on embedded devices used in distributed wireless sensor networks. nesC is built as an extension to the C programming language with components "wired" together to run applications on TinyOS.

I may sound like I know a lot, because I know the above. But I only know the above because I googled (and then copied and pasted) for "nesc" and found this on Wikipedia: http://en.wikipedia.org/wiki/NesC. And I googled for that because of this question by Aditya Devisetty on the NetBeans dev@platform.netbeans.org mailing list:

There is a Nesc language plugin for eclipse namely Yeti2 with all modern IDE features, we got language description files from them which uses CUP parser tool and JFLEX lexical tool. Now we want to develop a Nesc language plugin for NetBeans IDE by using those language description files.

We've used the C/C++ plugin Project Template as a Nesc Language Project Template. Is it possible to add additional actions to our Nesc Project Template? If it is possible, how can I add a "New File" action when the user right-clicks the project folder and chooses "New", with a "Nesc" sub option, i.e., New=>Nesc=>nesc files.

Right now, as I type these words, I still know nothing more than the above. What is Yeti2? What is CUP? What is JFLEX? My understanding from the context is that Yeti2 is the name of the Nesc plugin from Eclipse and that CUP and JFLEX are, together, something like ANTLR or JavaCC.

Here's my thinking from this scant knowledge, which I'm only sharing so that others at this kind of stage of NetBeans plugin development may follow a similar thinking pattern. Or may reject this thinking pattern completely. But at least be aware that this approach exists.

  1. Google "Yeti2", "CUP", "JFLEX". Just to get a clue what those mean outside my own head. OK, a couple of seconds later I know that Yeti 2 (thanks to link here) is "the TinyOS 2.x Plugin for Eclipse". Oh, wait. A new term "TinyOS". TinyOS is an open source, BSD-licensed operating system designed for low-power wireless devices, such as those used in sensor networks, ubiquitous computing, personal area networks, smart buildings, and smart meters. (Thanks to this link.) Right now I wonder what the relationship is between TinyOS and Nesc. Could Nesc be the programming language used on TinyOS? Oh, yes, that's what I copied and pasted at the start of this blog entry, serves me right for pasting it and not really reading it.

  2. The key phrase from a tooling point of view, in the opening paragraph above, now that I've read it, is "nesC is built as an extension to the C programming language". Therefore, a NetBeans plugin for Nesc should be based on a distribution of NetBeans IDE that includes the C/C++ support. Wait, maybe a NetBeans plugin exists already for either "TinyOS" or "Nesc". Two seconds later I find this: http://code.google.com/p/nesc-netbeans-plugin. OK, so it is dated. So what, it's a starting point. Certainly worth investigating. Now let's google some more, maybe there are other NetBeans plugins that relate somehow. Wow, another one to investigate, 5 seconds of googling later: http://ubicomp.in/category/tags-ubicomp/netbeans. About another 5 seconds of googling later, it would seem to me that, at least the 'tools' folder of https://github.com/tyll/tinyos-2.x-contrib/tree/master/cotsbots has some interesting content that is related to NetBeans IDE yet also appears to be related to TinyOS.

  3. OK. Let's go a step further. Let's go to the NetBeans Downloads page. I wonder if I need to download the "All" distribution and end up with a bunch of stuff I'll never need in the context of my  NetBeans-based TinyOS development environment. Hurray. There's a C/C++ distribution of NetBeans IDE. Hence, great. I can download that and, since NetBeans IDE is inherently extensible, I'll be able to extend the downloaded NetBeans IDE to include Nesc (whatever that is, since I still haven't looked at the syntax or anything else related to Nesc) and support for TinyOS, preferably by installing one of the plugins I've already identified or another one. It's now 16.54 on Friday afternoon. Let's see how long it will take me to download the C/C++ distribution of NetBeans IDE. It's now 16.55 on the same day and 51.6 MB have been downloaded, i.e., the C/C++ distribution of NetBeans IDE.

  4. Let's now install the download, which is "netbeans-7.3.1-cpp-linux.sh", since I'm on Ubuntu Linux. It installs in a few seconds. This is literally the first time I've downloaded the C/C++ distribution of NetBeans IDE. I like the Nimbus Look and Feel, so will just pop into the etc/netbeans.conf file to add the "--laf Nimbus" switch, which won't be necessary in 7.4, since there a Look and Feel switcher is built into the Options window. Oh, wait, since I already had 7.3.1 installed, the C/C++ bits are added to the existing NetBeans IDE installation. OK. Well, I'm now going to uninstall NetBeans IDE and then install it afresh via "netbeans-7.3.1-cpp-linux.sh", since that provides the minimum set of modules needed for my TinyOS/Nesc toolset based on NetBeans IDE. OK, 7.3.1 no longer exists. OK, now reinstalling the C/C++ distribution of NetBeans IDE 7.3.1. Installed it. From scratch. So let's go back to that netbeans.conf file and add the "--laf Nimbus" switch. OK, now I start up NetBeans IDE 7.3.1 and the New Project window clearly shows that all I have is C/C++ support in my NetBeans IDE:



  5. Like most things in life, the above is good news and bad news at the same time. NetBeans IDE, and the NetBeans Platform beneath it, are both Java applications. Even the C/C++ IDE that you see above is created in Java. If we want to create a new C/C++ oriented application on top of the above, i.e., if we want to extend the above, we need to know Java. Would be cool if we could use our (well "our" is an odd word here, since the "me" part of "our" doesn't know any C++ at all) to extend the above application. However, before worrying about coding in Java to extend the above, let's see what happens when we install some of the Nesc plugins identified in step 2 above into our application. Hmmm. Not very convincing plugins. Either empty repos or other weirdness. Still, always worth investigating a bit all the plugins that are out there, no point in reinventing the wheel. Let's just create our own.

  6. Creating a plugin is going to be interesting, since we need to use Java even though the application shown in the screenshot above doesn't support Java programming. So we can't do our development there. Let's take a different approach. Let's treat the above application as the basis of our application and develop it further in a separate instance of NetBeans IDE. That's a lot simpler than it sounds. First, we'll go here and download "netbeans-7.3.1-201306052037-cpp.zip". In other words, that will be the platform, or basis, of our Nesc IDE. For the sake of argument and to give it a name, we'll call it "nescide" from now onwards. Then, again, uninstall NetBeans IDE 7.3.1, i.e., uninstall the C/C++ distribution of NetBeans IDE. Then download and install the "Java SE" distribution of NetBeans IDE. That will give us all the tools for using Java for programming, while we'll be using the  "netbeans-7.3.1-201306052037-cpp.zip" as our application basis. No worries if that doesn't make sense, it will do so in the next step.

  7. So now I've installed "netbeans-7.3.1-javase-linux.sh". Now let's create a new application that uses the "netbeans-7.3.1-201306052037-cpp.zip" we downloaded above as its basis.

    In NetBeans IDE 7.3.1, create a new NetBeans Platform application:

    Name the application "Nescide" and click Manage:

    Now browse to the folder where you unzipped  "netbeans-7.3.1-201306052037-cpp.zip". And then look through the list of modules that are now available to your Nescide, including a long list of C/C++ modules:

    Click Close, click Finish, and then right-click the newly created project, choose Project Properties, and in the Libraries tab include all the clusters of modules made available by the platform that's the basis of your application:

    Run the application and now you have the start of Nescide, based on the C/C++ distribution of NetBeans IDE:

    Even more interestingly, we can now add new modules to extend the functionality of our Nescide:

  8. Let's now create a new project template, which is clearly what Aditya Devisetty did, if you go all the way back to the blockquote with which this blog entry started. Firstly, run Nescide from NetBeans IDE 7.3.1. That is, right-click the "Nescide" project node that you see above and choose Run. Nescide starts up in development mode. Go to File | New Project and then C/C++ | C/C++ Application. Click Next. Type "SimpleNesc" in Project Name and then click Finish. Now Nescide shows the following project template as a starting point for your application:



  9. Before we work creating a module that bundles the above project template into Nescide, so that it will be available to anyone using Nescide, let's add something to it that will make the project template actually useful to Nesc programmers. Hmmm. I know absolutely nothing about nesC, not even its file extension, not even whether it has its own file extension, so excuse me while I google a few seconds. OK, three seconds later: "nesC uses the filename extension '.nc' for all source files -- interfaces, modules, and configurations." (Thanks to TinyOS Tutorial Lesson 1.) I also wonder whether there's a MIME type that is generally recognized as being applicable to nesC files. Doesn't seem to be one. Let's use "text/x-nc". What about a unique icon we can use? None that I can find but I shrunk the TinyOS logo down to this squishy 16x16 image:



  10. So, we'll create MIME type support for nesC files, in a new NetBeans module (project name 'NescFileSupport', with code name base "org.nescide.module.nescfilesupport"), in our Nescide application. When we do so, following the related tutorial, a new file template is created in the module, here "NescTemplate.nc", among other things, as explained in the tutorial. I copied and pasted some nesC code into the "NescTemplate.nc" template, so that the user has some code to get started with and not just a blank file.



    When we deploy the application again from NetBeans IDE 7.3.1 and reopen the SimpleNesc project template, which should still be somewhere on disk since you created it in an earlier step above, we can create a new Nesc file in it, from the New File wizard in the category (by default "Other") specified in the "package-info.java" class above. Now I have the following in Nescide, i.e., I have created a file and it has the icon I provided above and the file has the content I provided above:



  11. Now that I've created a NescTemplate.nc file, I can easily create more. When I right-click the project template, the most recently used file is immediately available at the top of the list:



    However, the question with which this blog entry started is as follows: "Is it possible to add additional actions to our Nesc Project Template? If it is possible, how can I add a 'New File' action when the user right-clicks the project folder and chooses 'New', with a 'Nesc' sub option, i.e., New=>Nesc=>nesc files."

    Fine. Let's do that. In your module, create some additional files similar to "NescTemplate.nc". In each of them, add some code as a starting point for something the user will want to do. (For example, from here.) To register the templates into the application, open the "package-info.java" file and change to something like the below, i.e., you can have multiple @TemplateRegistrations in a single file:
    @TemplateRegistrations({
        @TemplateRegistration(folder = "Nesc", content = "EmptyTemplate.nc"),
        @TemplateRegistration(folder = "Nesc", content = "SimpleTemplate.nc")
    })
    package org.nescide.module.nescfilesupport;
    
    import org.netbeans.api.templates.TemplateRegistration;
    import org.netbeans.api.templates.TemplateRegistrations;

    Then extend the Lookup of the project type to define the privileged templates, i.e., the templates that will be available when the user right-clicks a project and chooses New:

    import org.netbeans.spi.project.ProjectServiceProvider;
    import org.netbeans.spi.project.ui.PrivilegedTemplates;
    
    @ProjectServiceProvider(projectType = "org-netbeans-modules-cnd-makeproject",
        service = PrivilegedTemplates.class)
    public class NescPrivilegedTemplates implements PrivilegedTemplates {
    
        private static final String[] PRIVILEGED_NAMES = new String[]{
            "Templates/Nesc/EmptyTemplate.nc",
            "Templates/Nesc/SimpleTemplate.nc",
        };
    
        @Override
        public String[] getPrivilegedTemplates() {
            return PRIVILEGED_NAMES;
        }
        
    }

    The above requires you to add dependencies on the Project API and the Project UI API.

    When you run Nescide, and open or create a project, you'll see when you right-click and choose New the templates you set to be privileged:

  12. Now create a new module and use the New Project template wizard from the Module Development category in the New File dialog to wrap the application into a module and register it in Nescide. To do this, you'll need to install the C/C++ support into NetBeans IDE 7.3.1, otherwise your project will not be recognized and you'll not be able to use the Project Template wizard.

That answers the basic question anyway. Another time more, e.g., syntax coloring for nesC files.

About

Geertjan Wielenga (@geertjanw) is a Principal Product Manager in the Oracle Developer Tools group living & working in Amsterdam. He is a Java technology enthusiast, evangelist, trainer, speaker, and writer. He blogs here daily.

The focus of this blog is mostly on NetBeans (a development tool primarily for Java programmers), with an occasional reference to NetBeans, and sometimes diverging to topics relating to NetBeans. And then there are days when NetBeans is mentioned, just for a change.

Search

Archives
« August 2013 »
SunMonTueWedThuFriSat
    
1
24
       
Today