gnokii: API error?

278 views Asked by At

I have issues with the following code:

#include <stdio.h>
#include <stdlib.h>

#include <string.h>
#include <gnokii.h>

#define CONFIG_FILE "config"

struct gn_statemachine  *state;

void terminate(void) {
    gn_lib_phone_close(state);
    gn_lib_phoneprofile_free(&state);
    gn_lib_library_free();
}


int main() {
    gn_data data;
    gn_error error;    
    gn_sms_folder_list folderlist;

    atexit(terminate);

    if((error = gn_lib_phoneprofile_load(CONFIG_FILE,&state)) 
       != GN_ERR_NONE)
    {
        fprintf(stderr,"%s\n",gn_error_print(error));
        exit(1);
    }

    memset(&folderlist,0,sizeof(gn_sms_folder_list));
    gn_data_clear(&data);
    data.sms_folder_list = &folderlist;

    error = gn_sm_functions(GN_OP_GetSMSFolders, &data, state);

    printf("ada %d sms dun\n",folderlist.number);

    return 0;
}

I'm compiling it with gcc -o main main.c -lgnokii, but when it's executed it generates errors when looking for config file:

# ./gnokiitest 
No phone_config section in the config file.
Either global or given phone section cannot be found.
Segmentation fault

because I included the config file within one folder of main output:

$ cat config 
[global]
  connection = bluetooth
  port = 24:22:AB:AB:C1:F8
  model = AT
  rfcomm_channel = 2

Whats wrong then?

1

There are 1 answers

3
pkot On BEST ANSWER

For starters, the following will cause issues:

if((error = gn_lib_phoneprofile_load(CONFIG_FILE,&state))

state variable is not initialized here. That will cause random pointer being passed and most likely segfault.

Next, the first argument to gn_lib_phoneprofile_load() is not the config file name, but the phone section in the config where the connection details are provided. Given that you pass config as this parameter you'd need:

[phone_config]
connection = bluetooth
port = 24:22:AB:AB:C1:F8
model = AT
rfcomm_channel = 2

but placed in the standard gnokii config file location. To use different location use:

gn_lib_phoneprofile_load_from_file(CONFIG_FILE, NULL, &state);

Second argument is the phone section name. If NULL, then [global] would be used.

Additionally gn_lib_phoneprofile_load() just reads the config file. You need to run gn_lib_phone_open() to initialize the connection.

Finally, there is similar code already written, no need to reinvent the wheel: http://git.savannah.gnu.org/cgit/gnokii/gnokii-extras.git/tree/snippets/sms/sms_status.c