0

这是代码人员:

#include <stdio.h>
#include <stdlib.h>
#include <gnokii.h>
#include <signal.h>
/*
 * 
 */

#define _(x) x

struct gn_statemachine *state = NULL;

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

void businit(void) {
    gn_error    error;

    atexit(busterminate);

    error = gn_lib_phoneprofile_load(NULL, &state);
    if (GN_ERR_NONE == error) {
        error = gn_lib_phone_open(state);
    }

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

void signal_handler(int signal) {
    (void)signal;
    exit(-2);
}

int main(int argc, char *argv[]) {
    gn_data     *data;
        gn_sms          sms;
    gn_error    error;


    businit();

    signal(SIGINT, signal_handler);

    gn_data_clear(data);

        sprintf(sms.remote.number,"%s","+628571641111");
        sprintf(sms.user_data[0].u.text,"%s","tesss");

        data->message_center    = calloc(1, sizeof(gn_sms_message_center));
        data->message_center->id= 1;

    error = gn_sm_functions(GN_OP_GetSMSCenter, data, state);
    if(error == GN_ERR_NONE)
    {
        snprintf(sms.smsc.number,sizeof(sms.smsc.number),"%s",data->sms->smsc.number); // set to sms.smsc.number from data.sms.smsc.number
        sms.smsc.type = data->message_center->smsc.type;
        //g_slice_free(gn_sms_message_center,data->message_center); // free the ram
        free(data->message_center);
    }
    if(!sms.smsc.number[0])
    {
        printf("failed once getting sms center number\n");

    }
    if(!sms.smsc.type)
    {
        sms.smsc.type = GN_GSM_NUMBER_Unknown;
    }

     data->sms = &sms;

    //send the message
    error = gn_sms_send(data,state);

    if(error == GN_ERR_NONE)
    {
        if(sms.parts > 1)
        {
            int j;
            printf("sms sent with : %d parts, and reference number is : ", sms.parts);

            for(j=0; j < sms.parts; j++)
            {
                printf("%d\n",sms.reference[j]);
            }
        }
        else
        {
            printf("one sms sent with reference number : %d\n",sms.reference[0]);
        }
    }
    else
    {
        printf("libgnokii error : %s\n",gn_error_print(error));
    }

    free(sms.reference);

    return 0;
}

我要向 +628571641111 发送一条短信,文本为“tesss”,但不幸的是操作系统说它是分段错误,因此,我的错在哪里?

$ gnokii --identify
GNOKII Version 0.6.29
IMEI         : 3556XXXXX509XXX
Manufacturer : ZTE INCORPORATED
Model        : MF627
Product name : MF627
Revision     : BD_3GHAP673A4V1.0.0
$ gdb -q ./gnokii_send_sms 
Reading symbols from /root/gnokii_send_sms...(no debugging symbols found)...done.
(gdb) r
Starting program: /root/gnokii_send_sms 
[Thread debugging using libthread_db enabled]

Program received signal SIGSEGV, Segmentation fault.
0x00317334 in ?? () from /lib/libc.so.6
(gdb) 
4

1 回答 1

1

您正在向 gn_data_clear 传递一个尚未初始化的指针。在你的主要功能的开始,你需要有

gn_data     data;

不是

gn_data     *data;

下面是函数实现:

GNOKII_API void gn_data_clear(gn_data *data)
{
        memset(data, 0, sizeof(gn_data));
}
于 2011-03-17T19:44:47.700 回答