13 Replies Latest reply on Oct 5, 2016 10:07 PM by hvaughan3

    C++ Application for BlueZ Protocol Stack - Compile Error

    stuart.h

      I am new to Edison and would be grateful for some help launching my exploration of the platform's capabilities.

       

      (1)  First, to verify instantiation of my development environment, I attempted to compile what looks like well-tested code from http://people.csail.mit.edu/albert/bluez-intro/c404.html ("BlueZ Intro") in a stand-alone program called "Sketch.cpp" (included below).  The compiler cannot resolve "inquiry_info" or "bdaddr" terms, both of which are defined in the "hci_lib.h" header file.  I suspect that the compiler can't find an implementation file for hci_lib.  Probably a newbie oversight. 

       

      Any ideas?

       

      I downloaded the BlueZ archive but could not find an option to give Eclipse a path to the BlueZ library directory.  I was reluctant to run the BlueZ installation, so as a workaround, I tried importing the included files directly into the Eclipse project work space, but I could not find anything like hci_lib.c in the BlueZ archive.

       

      (2)  What would be the best way to pass the "rfkill unblock bluetooth" command to the Edison board during runtime?  It works fine from the connman connection manager, but eventually I would want to unblock Bluetooth automatically on power up.

       

      (3)  While I'm here, any update on when Curie might be available?  It might ultimately be a better platform for my needs, and I intend to evaluate it too.

       

      Apologies if this seems elementary, if I missed some obvious documentation, or if I am in the wrong forum. 
      My primary training is biomedical engineering.  Programming is not my longest and strongest suit -- but working on it!

       

      Sketch.cpp below

      ******************************

       

      /* Sketch for Bluetooth inquiry

         * Using BlueZ-5.31

         * Compiler cannot resolve "inquiry_info" and"bdaddr" terms

         */

       

      #include <stdio.h>

      #include <stdlib.h>

      #include <unistd.h>

      #include <sys/socket.h>

      #include "bluetooth.h"

      #include "hci.h"

      #include "hci_lib.h"

      using namespace std;

       

      int main(int argc, char **argv)                                                                 // All below from BlueZ Intro

      {

          inquiry_info *ii = NULL;      

          int max_rsp, num_rsp;

          int dev_id, sock, len, flags;

          int i;

          char addr[19] = { 0 };

          char name[248] = { 0 };

       

          dev_id = hci_get_route(NULL);

          sock = hci_open_dev( dev_id );

          if (dev_id < 0 || sock < 0) {

              perror("opening socket");

              exit(1);

          }

       

          len  = 8;

          max_rsp = 255;

          flags = IREQ_CACHE_FLUSH;

          ii = (inquiry_info*)malloc(max_rsp * sizeof(inquiry_info));                      //  Compiler cannot resolve "inquiry_info"

       

          num_rsp = hci_inquiry(dev_id, len, max_rsp, NULL, &ii, flags);

          if( num_rsp < 0 ) perror("hci_inquiry");

       

          for (i = 0; i < num_rsp; i++) {

              ba2str(&(ii+i)->bdaddr, addr);                                                              // Compiler cannot resolve "bdaddr"

              memset(name, 0, sizeof(name));

              if (hci_read_remote_name(sock, &(ii+i)->bdaddr, sizeof(name),

                  name, 0) < 0)

              strcpy(name, "[unknown]");

              printf("%s  %s\n", addr, name);

          }

       

          free( ii );

          close( sock );

          return 0;

      }

        • 1. Re: C++ Application for BlueZ Protocol Stack - Compile Error
          Intel_Alvarado

          Hi,

          1. I managed to compile the code posted below by editing the first includes such as:
          /* Sketch for Bluetooth inquiry
            *Using BlueZ-5.31
            * Compiler cannot resolve "inquiry_info" and"bdaddr" terms
            */
          
          #include <stdio.h>
          #include <stdlib.h>
          #include <unistd.h>
          #include <sys/socket.h>
          #include "bluetooth/bluetooth.h"
          #include "bluetooth/hci.h"
          #include "bluetooth/hci_lib.h"
          using namespace std;
          

          I compiled the code with gcc -o outest sketch.cpp –lbluetooth

           

               2. To automatically have the board run rfkill unblock Bluetooth at boot you could create a service. This service will allow you to run scripts at startup. Take a look at Automatic Scripting at Boot-Up | Musings from Stephanie

           

              3.Unfortunately not. No updates on the Curie yet. All the information of Curie can be found in Intel® Curie™ Module: Unleashing Wearable Device Innovation . When updates are released, will be published in this website.

           

          Let me know if this was useful.

           

          Sergio

          • 2. Re: C++ Application for BlueZ Protocol Stack - Compile Error
            stuart.h

            Hi Sergio,

             

            Thank you for the kind help.  The boot-up suggestion was great.  I like it!

             

            As to the compiler, mixed joy.

             

            (a) PROGRESS! (Of sorts.)  Eclipse compiled Sketch_1 when I manually imported 3 bluetooth header and 2 implementation files directly into the Eclipse, Sketch_1 src folder.  (See attached screen grabs of successful configuration.)  I obtained those files by downloading and unpacking BlueZ from BlueZ.org, then manually importing the five files into the Sketch_1 project using Eclipse's file-import function.  In the Sketch_1.cpp #include statements, I referenced the three bluetooth header files as shown in the attached screen grabs without the "bluetooth/" prefix. I assume Eclipse takes all five files from the src folder. (In the meantime I also updated the IOT libraries using "opkg update" and "opkg upgrade".) 

             

            (b) BUT .... No joy when I include the header files into Sketch_1.cpp using the "bluetooth/" prefix as per your suggestion (e.g., "#include bluetooth/hci.c).  The compiler still throws "undefined reference" and other (probably cascaded) errors for the hci-related functions, regardless of whether I import the five bluetooth files directly into the src folder.  I assume that if I do not reference the files directly in the Sketch_1 src folder, Eclipse is looking outside the src folder, but not finding the files for some reason.  Because you compiled with your edits on your system, I suspect there is something peculiar about my instantiation of Eclipse.  Either it needs a tweak, or my understanding of how Eclipse searches for header files does -- or both.  Should I, like, reinstall Eclipse as a next step?


            (By the way, I do not grasp the significance of your comment about gcc -o outest sketch.cpp –lbluetooth, probably due to my inexperience.)

             

            Any enlightenment would be welcome.

             

            Gratefully,

             

            Stuart

             

            P.s,: How can I include nice screen grabs like you do? 

            • 3. Re: C++ Application for BlueZ Protocol Stack - Compile Error
              Intel_Alvarado

              The reason why you’re getting the undefined reference warning is most likely because Eclipse is looking for the library files and is not able to find them. The libraries are added in the /usr/include file. In your “includes” screenshot you don’t seem to have the Bluetooth library. There is a thread that you might find useful that describes how to add libraries in Eclipse. Re: Adding C++ sensor library in Eclipse and some other questions

              The command gcc I used to compile the code to see if I received the same errors as you and if the compilation was successful. You don’t need to use this command.

              As for the screen grabs, when you are typing your reply after clicking the “Reply” button, there is an option that looks like a camera called insert image. Was this what you were referring to?

               

              Sergio

              • 4. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                stuart.h

                Thanks again, Sergio for your continuing help.

                 

                Status update: no joy when I added a path to ".../usr/include/bluetooth" separately from ".../usr/include" as shown in the attached screen shots.  Eclipse still gives "undefined reference" errors to hci-related functions.  I have referenced the header files in the .cpp file with and without the "bluetooth/" prefix, e.g., as both "bluetooth/hci.h" and "hci.h".  I infer that Eclipse is finding the bluetooth-related header files, because it is not giving "unresolved inclusion" errors on the #include lines of the .cpp file. I can make Eclipse throw those errors by addying a typo to a filename, like "blootooth.h", but Eclipse is not giving those kinds of errors.
                Project.tiff

                Sketch_1.tiff

                Just working from first principles, if Eclipse is finding the header files, is it possible that it either cannot generate or cannot find implementation functions, such as it would otherwise compile from a corresponding .c implementation file?  The program executes if I import " .h" and " .c" files into the project src folder from the tarball I downloaded from BlueZ.org. Perhaps Eclipse can't find whatever corresponds to a ".c" or binary file in the Eclipse native libraries.  Do they live in the Eclipse environment or down on the Edison board?

                 

                Lacking other ideas, I re-installed Eclipse and updated the libraries again.  No change in behavior.  I'll continue poking around, but I'm down to brute-force, try-something-and-see-what-happens ideas.

                 

                Apologies for taking up your time.  I appreciate your help and would be grateful for any enlightenment.

                 

                Stuart

                • 5. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                  Intel_Alvarado

                  Have you made any progress trying to include the library in Eclipse? Can you include the complete errors you are receiving? Normally these “undefined errors” contain references to different functions. Maybe this will provide a clue of where the error lies. Make sure that the path column of the error displays the full path.

                   

                  I also found a thread with a similar issue as yours, take a look at Help on using the iotkit-comm library in Eclipse and follow the suggestions provided.

                   

                  Sergio

                  • 6. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                    mhahn

                    when adding to Eclipse you should add into "devkit-x86\sysroots\i586-poky-linux\usr\include" folder.

                    • 7. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                      mhahn

                      i.e. in your case: *\usr\include\bluetooth

                      • 9. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                        stuart.h

                        Hi Sergio and "m" of mHann,

                         

                        Thanks for checking in.

                         

                        No, I still don't have my version of Eclipse set up for the Bluetooth libraries.  I've been using the workaround of copying BlueZ source files into the project workspace, but I would like to configure Eclipse properly.  Note: my goal is to use the native libraries that downloaded with Eclipse.  I have only been using the BlueZ files as a workaround, and my intent is to cease use of those other BlueZ files.

                         

                        The following narrative describes my latest status with reference to attached screen grabs.  The "main" routine remains unchanged from my initial post.

                         

                        (1) The Sketch_1.cpp file references Bluetooth-related header files using a format similar to: "#include hci.h" (See "Sketch_1.cpp.tiff".)  Using a format like "#include bluetooth/hci.h" produces no change in behavior.

                        Sketch_1.cpp.tiff

                         

                        (2)  The project "include" folder seems appropriately directed to the bluetooth folder path: ".../i586-pokey-linux/user/include/bluetooth" (see "Sketch_1 Project").  I understand this resulted from my adding the /bluetooth folder as an option under: project settings; C/C++ General; Paths and Symbols. (See Sketch_1 C/C++ General Paths.)  

                        Sketch_1 Project.tiff

                        Sketch_1 C:C++ General Paths.tiff

                         

                        (3)  The command that I presume launches the compiler seems to include an option for the /bluetooth folder (See"Sketch_1 Build Settings G++ Compiler"), but I also suspect this presumption is faulty because of item (4) below.

                        Sketch_1 Build Settings G++ Compiler.tiff

                         

                        (4) When building the project, the Eclipse console reports four errors of the format: "undefined reference to []" where [] is a Bluetooth HCI command, such as "hci_get_route" (see "Sketch_1 Compile Errors"). The console also reports what appears to be an echo of the build invocation, which lists an option  to "-lmraa", but option for any Bluetooth library. 

                        Sketch_1 Compile Errors.tiff

                         

                        (5)  In case it might be relevant, I noticed that the preferences for the linker coincidentally include an option "lmraa" but no reference to any bluetooth library.  (See "Sketch_1 Linker.)

                        Sketch_1 Linker.tiff

                         

                        Hopefully this provides enough information for you to spot my oversight.   Thanks for your assistance and, as always, any enlightenment would be appreciated.

                         

                        Stuart

                        • 10. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                          Intel_Alvarado

                          Try using these steps to add the bluez-5.31 library to Eclipse and build a project:

                          1. Download and extract the bluez.tar.xz file http://www.bluez.org/release-of-bluez-5-31/
                          2. Copy and paste the extracted folder (named something like bluez-5.31) to the usr\include folder. The path to usr\include folder may look something like this C:\Intel\iotdk-ide-win\devkit-x86\sysroots\i586-poky-linux\usr\include
                          3. Now on Eclipse (assuming your project is open) add the linker flags for your project. On Eclipse's menus select Project>Properties>C/C++ Build>Settings>Tool Settings>Cross GCC Linker>Miscellaneous, add the flag "-bluez-3.1" and click OK.

                          Note: This flag is basically a "-" character concatenated with the name of the bluez folder.

                             4. Edit the header calls on your main file; add the path to where the header files needed are. In this case bluetooth.h, hci.h and hci_lib.h are inside bluez 5.31, header calls should look something like these:

                          #include "bluez-5.31/bluetooth.h"

                          #include "bluez-5.31/hci.h"

                          #include "bluez-5.31/hci_lib.h"

                             5. Proceed to save (Ctrl + S) and Build Project.

                           

                          With these steps I was able to build and compile your code within Eclipse.

                           

                          Please let me know if this was useful.

                           

                          Sergio

                          • 11. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                            stuart.h

                            Thank you Sergio,

                             

                            I was able to compile the sketch in Eclipse using a slight modification to your instructions.  In step 3, I added the character "l" (as in "link", lower case) to the flag in the linker's "Miscellaneous" menu between the hyphen ("-") and the folder name, e.g., : "-lBluez-5.31" rather than "-Bluez-5.31".

                             

                            Just for reference, in case someone else reads this thread in the future, that modification also allowed me to compile the sketch using the native, Bluetooth libraries that downloaded with Eclipse (i.e., using instructions from Intel's Edison page for getting started with Edison).  Minimum modifications that seemed to work were:

                             

                            (1)  In the c++ sketch itself, include the Bluetooth files using the format: "#include bluetooth/hci.h".

                            (2)  Add the linker flag "lbluetooth" in the linker menu as you discussed above in item 3.

                             

                            In retrospect, it would seem that the version of Eclipse I downloaded from the Intel site (Eclipse 4.4.2 - Luna) is pre-configured to link to the mraa libraries but not to the Bluetooth libraries.  Just as an observation, this version of Eclipse also appears not to be pre-configured to link to the upm libraries either.  Another newbie might need to make this same modification to use those libraries (or you might pre-configure Eclipse to link to all three).


                            Have a great new year.  If you (or any of the Edison team) find yourself in Hawaii, hit me up for a beer.

                             

                            Aloha,

                             

                            Stuart

                            • 12. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                              Intel_Alvarado

                              Thank you for sharing the additional modifications you used to compile. Your post will help other users have a better experience using Eclipse.

                              We wish you a great new year as well.

                               

                              Sergio

                              • 13. Re: C++ Application for BlueZ Protocol Stack - Compile Error
                                hvaughan3

                                Sergio,

                                 

                                Really appreciate all of your help with this. I am trying to include the Azure IoT Library into an espressif project from within Eclipse and was directed to this thread as an example of how to do it.

                                 

                                Going through your instructions, I make it to step #3 and do not see any kind of 'Tools Menu' and am wondering if I am using a different version of Eclipse or something. Currently it says I am running version 4.5.2. Would that make a difference?

                                 

                                Below is a screenshot of what I see once I get to the 'C/C++ Build>Settings' page:

                                EclipseMenuOptionMissing.png