2011-02-05 19 views
1

我有下面的代碼的問題:gnokii:API錯誤?

#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; 
} 

我與gcc -o main main.c -lgnokii編譯它,但是當它執行它尋找配置文件時產生的錯誤:

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

,因爲我包含在配置文件在主輸出的一個文件夾內:

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

什麼錯誤呢?

回答

2

對於初學者來說,下面的會導致一些問題:

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

state變量未初始化這裏。這將導致隨機指針被傳遞,最有可能的是段錯誤。

接下來,gn_lib_phoneprofile_load()的第一個參數不是配置文件名,而是配置中提供連接詳細信息的電話部分。假設你通過config因爲這個參數你需要:

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

,但放置在標準gnokii的配置文件的位置。要使用不同的位置使用:

​​

第二個參數是手機節的名稱。如果爲NULL,那麼將使用[global]

此外gn_lib_phoneprofile_load()只是讀取配置文件。您需要運行gn_lib_phone_open()來初始化連接。

最後,還有一個類似的代碼已經寫好,沒有必要重新發明輪子:http://git.savannah.gnu.org/cgit/gnokii/gnokii-extras.git/tree/snippets/sms/sms_status.c

+0

什麼很好的解釋:d,並即時知道如何設置可變的配置,而不是從文件中提取,是一個可能的事情?無論如何,是否有一些手動庫API的gnokii API記錄?如果有一些,它將會有多好... – capede 2011-02-05 14:20:26