0

I'm trying to mess around with Bluetooth programming on Linux and in C (not C++).

I need to use a struct defined in a header; but every time I try to declare a variable of the particular struct that I need Eclipse complains about being unable to resolve the type.

Why is this? How can I fix it?

My code:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/socket.h>
#include <bluetooth/bluetooth.h>
#include <bluetooth/hci.h> //Header where needed struct is defined
#include <bluetooth/hci_lib.h>

extern struct hci_dev_info; //Just something I tried. Eclipse displays the definition for this struct (the one I need) when moused over. It doesn't seem to know what the struct is within main()

void log_me (FILE * file, char *error_message, int error_code)
{
    fprintf(file, "%s", error_message);
    exit(error_code);
}


int main (void)
{
        FILE *bluetooth_info_file;
        //On line 88 of hci.i the device flags are actually defined

        int devices [16];   //Will store the device numbers
        int device_count;
        int fd;             //File descriptor
        int return_value;
        hci_dev_info *da; //bd_addr (bluetooth address struct)

        bluetooth_info_file = fopen("BluetoothAddresses+Names.txt", "wa");
        if(bluetooth_info_file != NULL)
        {
            fprintf(bluetooth_info_file, "Bluetooth adapters on system:\n");
        } else
        {
            printf("Error opening file!");
            exit(-1);
        }
        device_count = hci_get_devs(devices);

        if (device_count < 0) {
            log_me(bluetooth_info_file, "Unable to get device list!", -1);
        }

        if(device_count == 0)
        {
            log_me(bluetooth_info_file, "No devices exist on the system.", 0);
        }

        for(int i = 0; i < device_count; i++)
        {
            fd = hci_open_id(devices[i]);
            if(fd < 0)
            {
                log_me(bluetooth_info_file, "Unable to open device!", -1);
            }

            return_value = gci_get_attr(fd, &da);

            if(return_value < 0)
            {
                log_me(bluetooth_info_file, "Unable to get device attributes!!", -1);
            }

            fprintf(bluetooth_info_file, "Bluetooth device name: %s\t Address: %s\n", da->name, bda2str(da->bda));


            /*This is the proper spelling and name of the HCI_STATE_UP as mentioned in the sample.
HCI_DEV_UP (and other device events) are defined on line 1474 in hci.h
*/
            if(!(/*Incomplete code*/
                    ))
                log_me(bluetooth_info_file, "Device is down.\n", 0);
            }

            return_value = HCI_WriteScanEnable(fd, 0x02);
            if(return_value)
            {
                log_me("Unable to set scan mode!");
            }
        }


        free( ii );
        close( sock );
        fclose(bluetooth_info_file);
        return 0;
}

Bear in mind this is non-working/incomplete code. The sample I'm using online is over ten years old and no longer relevant but I'm trying to make it work anyway.

  • It's hard to say without seeing the header file. Is it possible that the type you want is `#ifdef`ed out? – Andy Schweig Oct 23 '16 at 00:10
  • I just did a search. Nope. –  Oct 23 '16 at 00:12
  • 1
    In the line `extern struct hci_dev_info;`, you've not specified what you're declaring. Maybe you wanted: `extern struct hci_dev_info dev_info;` or something similar? It's bad practice to be declaring `extern` variables in a source file; there should be a header declaring the variable (and probably some functions and so on) that is included both where the variable is defined and where it is used. That's slightly separate from getting the code to compile, though. – Jonathan Leffler Oct 23 '16 at 00:15
  • Ugh. I'm sorry. This question was entirely pointless. I manage to get it to recognize the struct. All I needed was a struct keyword in front of the hci_dev_info variable. The extern was just an experiment to see if it would work. I'm not entirely sure what extern does but I will some day! Sorry. Sometimes I get hasty and fail to recognize whats staring me in the face >_ –  Oct 23 '16 at 00:18

1 Answers1

0

I found the problem. There was nothing wrong, chalk it up to hastiness and failure to see the obvious.

This line:

hci_dev_info *da

Ought to be:

struct hci_dev_info *da

All struct members are accessible if you do this.

Also remove the line:

extern struct hci_dev_info; //Just something I tried. Eclipse displays the definition for this struct (the one I need) when moused over. It doesn't seem to know what the struct is within main()

As its unnecessary.