1

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?

YasirA
  • 9,531
  • 2
  • 40
  • 61
capede
  • 945
  • 1
  • 13
  • 26

1 Answers1

2

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

pkot
  • 61
  • 1
  • what a great explanation :D, and im wondering how to set the configuration in variable, not fetching from file, is that a possible thing ?, anyway, is there some manual library API documented for gnokii API ? how good it'll be if there are some... – capede Feb 05 '11 at 14:20
  • The proper API for config variables handling is on TODO list. Probably in 2-3 gnokii versions it will be released. You can look at http://svn.opensync.org/plugins/gnokii-sync/src/gnokii_config.c to see how to handle gnokii config without a config file as of now. There is no proper API documentation. What I can suggest is to read gnokii sources and some usage examples from http://git.savannah.gnu.org/cgit/gnokii/gnokii-extras.git/ – pkot Feb 05 '11 at 17:40
  • i didnt know what you(as developer) expect with `no proper API documentation` regarding for future development of gnokii it self, but im look forward for further release no matter what :D – capede Feb 05 '11 at 17:50