Changeset 111

Show
Ignore:
Timestamp:
03/04/06 03:02:57 (3 years ago)
Author:
abauer
Message:

Improved the obex transport.

Fixed several memory leaks

Fixed several bugs

Improved the test cases

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/development-branch/libsyncml/objects/sml_auth.c

    r104 r111  
    5555        unsigned int buffersize = 0; 
    5656         
    57         if (!cred && auth->enabled && auth->state != SML_AUTH_ACCEPTED) { 
     57        if (!cred && !auth->enabled) { 
     58                smlTrace(TRACE_INTERNAL, "Auth is disabled and no cred given"); 
     59                auth->state = SML_NO_ERROR; 
     60        } else if (!cred && auth->enabled && auth->state != SML_AUTH_ACCEPTED) { 
     61                smlTrace(TRACE_INTERNAL, "Auth is required"); 
    5862                auth->state = SML_ERROR_AUTH_REQUIRED; 
    59         } else if (cred && !auth->enabled) { 
     63        } else if ((!cred && auth->enabled && auth->state == SML_AUTH_ACCEPTED) || \ 
     64                (cred && !auth->enabled)) { 
     65                smlTrace(TRACE_INTERNAL, "Auth is already accepted %i", auth->enabled); 
    6066                auth->state = SML_AUTH_ACCEPTED; 
    61         } else if (!auth->enabled) { 
    62                 auth->state = SML_NO_ERROR; 
    6367        } else { 
    6468                smlTrace(TRACE_INTERNAL, "Input is \"%s\"", cred->data); 
  • branches/development-branch/libsyncml/sml_queue.c

    r108 r111  
    400400        g_source_attach(source, context); 
    401401        queue->context = context; 
     402        if (context) 
     403                g_main_context_ref(context); 
    402404         
    403405        smlTrace(TRACE_EXIT, "%s", __func__); 
     
    417419         
    418420        queue->source = NULL; 
    419         queue->context = NULL; 
     421         
     422        if (queue->context) { 
     423                g_main_context_unref(queue->context); 
     424                queue->context = NULL; 
     425        } 
    420426         
    421427        smlTrace(TRACE_EXIT, "%s", __func__); 
  • branches/development-branch/libsyncml/sml_support.c

    r108 r111  
    179179        if (!chan) { 
    180180                printf("unable to open %s for writing: %s\n", logfile, error->message); 
     181                g_free(logfile); 
     182                g_free(logmessage); 
    181183                return; 
    182184        } 
  • branches/development-branch/libsyncml/sml_transport.c

    r108 r111  
    188188        tsp->thread = NULL; 
    189189         
    190         g_main_context_unref(tsp->context); 
    191         tsp->context = NULL; 
    192          
    193190        smlTrace(TRACE_EXIT, "%s", __func__); 
    194191} 
     
    226223         
    227224        cmd->type = SML_TRANSPORT_CMD_DISCONNECT; 
    228          
    229         //Call the connect function 
     225        if (link) { 
     226                cmd->link = link; 
     227                smlLinkRef(cmd->link); 
     228        } 
     229         
     230        //Call the disconnect function 
    230231        smlQueueSend(tsp->command_queue, cmd); 
    231232         
     
    295296        smlTrace(TRACE_EXIT, "%s: Freed", __func__); 
    296297} 
    297          
    298 void smlTransportSetState(SmlTransport *tsp, SmlTransportState state, SmlError **error) 
    299 { 
    300         smlTrace(TRACE_ENTRY, "%s(%p, %i, %p(%p))", __func__, tsp, state, error, error ? *error : NULL); 
    301         smlAssert(tsp); 
    302          
    303         tsp->state = state; 
    304         if (error && *error) { 
    305                 tsp->error = *error; 
    306                 smlErrorRef(error); 
    307         } 
    308          
    309         smlTrace(TRACE_EXIT, "%s", __func__); 
    310 } 
    311298 
    312299void smlTransportSetError(SmlTransport *tsp, SmlLink *link, SmlError **error) 
     
    339326SmlLink *smlLinkNew(SmlTransport *tsp, void *link_data, SmlError **error) 
    340327{ 
    341         smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, tsp, link_data, error); 
     328        smlTrace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, tsp, link_data, error); 
    342329         
    343330        SmlLink *link = smlTryMalloc0(sizeof(SmlLink), error); 
  • branches/development-branch/libsyncml/sml_transport_internals.h

    r101 r111  
    5151         
    5252        SmlTransportState state; 
    53         SmlError *error; 
    5453         
    5554        SmlTransportType type; 
  • branches/development-branch/libsyncml/transports/obex_client.c

    r102 r111  
    6868                         
    6969                        if (obex_rsp != OBEX_RSP_SUCCESS) { 
     70                                env->error = TRUE; 
    7071                                smlErrorSet(&error, SML_ERROR_GENERIC, "Request not successfull: %i", obex_rsp); 
    7172                                goto error; 
     
    9899                                        if (!env->connection_id) { 
    99100                                                smlErrorSet(&error, SML_ERROR_GENERIC, "Missing connection id"); 
     101                                                g_free(who); 
    100102                                                goto error; 
    101103                                        } 
     
    103105                                        if (!who || strcmp(who, "SYNCML-SYNC")) { 
    104106                                                smlErrorSet(&error, SML_ERROR_GENERIC, "Missing or wrong who response"); 
     107                                                g_free(who); 
    105108                                                goto error; 
    106109                                        } 
     110                                        g_free(who); 
    107111                                         
    108112                                        smlTransportReceiveEvent(env->tsp, NULL, SML_TRANSPORT_EVENT_CONNECT_DONE, NULL, NULL); 
     
    129133                                                                } 
    130134                                                                 
     135                                                                if (!length) { 
     136                                                                        smlErrorSet(&error, SML_ERROR_GENERIC, "Got zero length!"); 
     137                                                                        goto error; 
     138                                                                } 
     139                                                                 
    131140                                                                body = smlTryMalloc0(length, &error); 
    132141                                                                if (!body) 
     
    244253        OBEX_Cleanup(env->obexhandle); 
    245254error_free_env: 
     255        g_free(env->path); 
    246256        g_free(env); 
    247257error: 
     
    257267         
    258268        smlAssert(env->tsp); 
     269        g_free(env->path); 
     270        g_free(env->stream_chunk); 
     271         
     272        OBEX_Cleanup(env->obexhandle); 
    259273         
    260274        g_free(env); 
     
    295309                smlTrace(TRACE_INTERNAL, "socket %i", fd); 
    296310                 
    297                 smlTrace(TRACE_INTERNAL, "peer addr = %d %s %i", hostinfo->h_addr_list[0], inet_ntoa(addr.sin_addr), env->port); 
    298                 //%d.%d.%d.%d", addr.sin_addr.s_addr[0], addr.sin_addr.addr[1], addr.sin_addr.addr[2], addr.sin_addr.addr[3]); 
    299          
     311                const char *addrstr = inet_ntoa(addr.sin_addr); 
     312                smlTrace(TRACE_INTERNAL, "peer addr = %d %s %i", hostinfo->h_addr_list[0], addrstr, env->port); 
     313 
    300314                if (connect(fd, (struct sockaddr *)&addr, sizeof(struct sockaddr_in)) < 0) { 
    301315                        smlErrorSet(&error, SML_ERROR_GENERIC, "Cannot connect socket: %s", strerror(errno)); 
     
    458472static void smlTransportObexClientSend(void *userdata, void *link, SmlTransportData *data, SmlError *error) 
    459473{ 
    460         smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %d, %i, %p)", __func__, userdata, link, data, error); 
     474        smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, userdata, link, data, error); 
    461475        smlAssert(data); 
    462476        smlAssert(userdata); 
     
    467481                goto error; 
    468482         
     483        env->error = FALSE; 
    469484        env->mimetype = data->type; 
    470485         
     
    503518         
    504519        /* Now convert to unicode. It requires 2 bytes per char + end*/ 
    505         unsigned char *unicode = g_malloc0(2 * strlen(target) + 2); 
     520        /*unsigned char *unicode = g_malloc0(2 * strlen(target) + 2); 
    506521 
    507522        unsigned int unicodesize = OBEX_CharToUnicode(unicode, (const unsigned char *)target, 2 * strlen(target) + 2); 
     
    509524                smlErrorSet(&error, SML_ERROR_GENERIC, "unable to convert to unicode"); 
    510525                goto error_free_obj; 
    511         } 
     526        }*/ 
    512527         
    513528        /* Now add the target mime type */ 
     
    539554        } 
    540555         
    541         if (env->tsp->error != NULL) { 
     556        if (env->error) { 
    542557                smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to send put request. Bailing out"); 
    543558                goto error; 
     
    566581         
    567582        /* Now add the target mime type */ 
    568         header.bs = (unsigned char *)unicode; 
    569         OBEX_ObjectAddHeader(env->obexhandle, obj, OBEX_HDR_TYPE, header, unicodesize, 0); 
     583        //header.bs = (unsigned char *)unicode; 
     584        //OBEX_ObjectAddHeader(env->obexhandle, obj, OBEX_HDR_TYPE, header, unicodesize, 0); 
     585        header.bs = (unsigned char *)target; 
     586        OBEX_ObjectAddHeader(env->obexhandle, obj, OBEX_HDR_TYPE, header, strlen(target) + 1, 0); 
    570587         
    571588        env->busy = TRUE; 
  • branches/development-branch/libsyncml/transports/obex_client_internals.h

    r101 r111  
    3636         
    3737        SmlBool busy; 
     38        SmlBool error; 
    3839        SmlMimeType mimetype; 
    3940} SmlTransportObexClientEnv; 
  • branches/development-branch/libsyncml/transports/obex_server.c

    r101 r111  
    3636#include <netdb.h> 
    3737 
    38 static uint32_t smlTransportObexServerGenerateId(SmlTransportObexServerEnv *env) 
    39 
    40         env->lastConId++; 
    41         return env->lastConId; 
    42 
    43  
    44 gboolean _dispatch_obex(gpointer data) 
    45 
    46         obex_t *obexhandle = data; 
    47          
    48         if (OBEX_HandleInput(obexhandle, 0) < 0) 
    49                 smlTrace(TRACE_INTERNAL, "Unable to handle input"); 
    50  
     38static gboolean _dispatch_obex(gpointer data) 
     39
     40        SmlLinkObexServerEnv *linkenv = data; 
     41         
     42        if (OBEX_HandleInput(linkenv->handle, 0) < 0) { 
     43                SmlError *error = NULL; 
     44                smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to handle input"); 
     45                smlTransportReceiveEvent(linkenv->env->tsp, linkenv->link, SML_TRANSPORT_EVENT_ERROR, NULL, error); 
     46                smlTrace(TRACE_ERROR, "%s: %s", __func__, smlErrorPrint(&error)); 
     47                smlErrorDeref(&error); 
     48                return FALSE; 
     49        } 
     50         
     51        if (linkenv->disconnect) { 
     52                smlTrace(TRACE_INTERNAL, "disconnecting link"); 
     53                OBEX_TransportDisconnect(linkenv->handle); 
     54                close(linkenv->fd); 
     55                linkenv->destroy = TRUE; 
     56        } 
     57         
     58         
     59        if (linkenv->destroy) { 
     60                smlTrace(TRACE_INTERNAL, "Destroying link %p %p", linkenv, linkenv->link); 
     61                smlTransportReceiveEvent(linkenv->env->tsp, linkenv->link, SML_TRANSPORT_EVENT_DISCONNECT_DONE, NULL, NULL); 
     62                smlLinkDeref(linkenv->link); 
     63                 
     64                OBEX_Cleanup(linkenv->handle); 
     65                g_free(linkenv); 
     66                return FALSE; 
     67        } 
    5168        return TRUE; 
    5269} 
    53  
    54 static gboolean _fd_prepare(GSource *source, gint *timeout_) 
    55 { 
    56         *timeout_ = 100; 
    57         return FALSE; 
    58 } 
    59  
    60 static gboolean _fd_check(GSource *source) 
    61 { 
    62         SmlTransportObexServerEnv *env = *((SmlTransportObexServerEnv **)(source + 1)); 
    63         fd_set rfds; 
    64         FD_ZERO(&rfds); 
    65         FD_SET(env->fd, &rfds); 
    66             
    67         struct timeval tv; 
    68         tv.tv_sec = 0; 
    69         tv.tv_usec = 1; 
    70  
    71         int retval = select(env->fd + 1, &rfds, NULL, NULL, &tv); 
    72          
    73         if (retval == -1) 
    74                 smlTrace(TRACE_ERROR, "%s: Unable to select()", __func__); 
    75         else if (retval) 
    76         return TRUE; 
    77          
    78         return FALSE; 
    79 } 
    80  
    81 /** 
    82  * @defgroup GroupIDPrivate Group Description Internals 
    83  * @ingroup ParentGroupID 
    84  * @brief The private part 
    85  *  
    86  */ 
    87 /*@{*/ 
    8870 
    8971static void _smlObexEvent(obex_t *handle, obex_object_t *object, int mode, int event, int obex_cmd, int obex_rsp) 
     
    9274        SmlLinkObexServerEnv *linkenv = OBEX_GetUserData(handle); 
    9375        SmlError *error = NULL; 
    94         SmlLink *link = NULL; 
    9576        obex_headerdata_t header; 
    9677        uint8_t headertype = 0; 
     
    9980        SmlMimeType mimetype = SML_MIMETYPE_UNKNOWN; 
    10081         
     82        if (linkenv->disconnect) { 
     83                OBEX_ObjectSetRsp(object, OBEX_RSP_FORBIDDEN, OBEX_RSP_FORBIDDEN); 
     84                smlTrace(TRACE_EXIT, "%s: This link was disconnected", __func__); 
     85                return; 
     86        } 
     87         
    10188        switch (event)  { 
    10289                case OBEX_EV_PROGRESS: 
     
    10592                case OBEX_EV_REQDONE: 
    10693                        smlTrace(TRACE_INTERNAL, "Done"); 
     94                        if (obex_cmd == OBEX_CMD_DISCONNECT) { 
     95                                linkenv->destroy = TRUE; 
     96                        } 
    10797                        break; 
    10898        case OBEX_EV_REQHINT: 
     
    111101                        switch (obex_cmd) { 
    112102                                case OBEX_CMD_GET: 
    113                                         while (!linkenv->send_data) { 
    114                                                 smlQueueDispatch(linkenv->env->tsp->command_queue); 
     103                                        /* At this point we received a get request. If we dont have any data 
     104                                         * to send yet, we iterate the main context until the data arrived. */ 
     105                                        while (!linkenv->send_data && !linkenv->disconnect) { 
     106                                                if (!g_main_context_pending(linkenv->env->tsp->context)) { 
     107                                                        usleep(100); 
     108                                                        continue; 
     109                                                } 
     110                                                smlTrace(TRACE_INTERNAL, "Dispatching command queue since we dont have data to send"); 
     111                                                g_main_context_iteration(linkenv->env->tsp->context, TRUE); 
     112                                        } 
     113                                         
     114                                        /* We received a disconnect event so we reply with an error */ 
     115                                        if (linkenv->disconnect) { 
     116                                                OBEX_ObjectSetRsp(object, OBEX_RSP_FORBIDDEN, OBEX_RSP_FORBIDDEN); 
     117                                                smlTrace(TRACE_INTERNAL, "Get was aborted"); 
     118                                                break; 
    115119                                        } 
    116120                                case OBEX_CMD_PUT: 
     
    143147                                                                break; 
    144148                                                        case OBEX_HDR_TYPE:; 
    145                                                                 char *mimetypestr = g_malloc0(len / 2 + 1); 
     149                                                                char *mimetypestr = g_strndup((char *)header.bs, len); 
     150                                                                /*char *mimetypestr = g_malloc0(len);// / 2 + 1); 
    146151                                                                if (OBEX_UnicodeToChar((unsigned char *)mimetypestr, header.bs, len / 2 + 1) == -1) { 
    147152                                                                        g_free(mimetypestr); 
    148153                                                                        smlErrorSet(&error, SML_ERROR_GENERIC, "unable to convert from unicode"); 
    149154                                                                        goto error; 
    150                                                                 } 
     155                                                                }*/ 
    151156                                                                 
    152157                                                                smlTrace(TRACE_INTERNAL, "Found type: %s", mimetypestr); 
     
    212217                                        } 
    213218                                         
    214                                         link = smlLinkFind(linkenv->env->tsp, linkenv); 
    215                                         if (!link) { 
    216                                                 smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to find link"); 
    217                                                 goto error; 
    218                                         } 
    219                                          
    220                                         smlLinkRef(link); 
    221                                          
    222219                                        SmlTransportData *tspdata = smlTransportDataNew(body, length, mimetype, TRUE, &error); 
    223220                                        if (!tspdata) 
    224221                                                goto error; 
    225222                                         
    226                                         smlTransportReceiveEvent(linkenv->env->tsp, link, SML_TRANSPORT_EVENT_DATA, tspdata, NULL); 
     223                                        smlTransportReceiveEvent(linkenv->env->tsp, linkenv->link, SML_TRANSPORT_EVENT_DATA, tspdata, NULL); 
     224                                         
     225                                        smlTransportDataDeref(tspdata); 
    227226                                         
    228227                                        OBEX_ObjectSetRsp(object, OBEX_RSP_CONTINUE, OBEX_RSP_SUCCESS); 
     
    239238                                                                break; 
    240239                                                        case OBEX_HDR_TYPE:; 
    241                                                                 char *mimetypestr = g_malloc0(len / 2 + 1); 
     240                                                                char *mimetypestr = g_strndup((char *)header.bs, len); 
     241                                                                /*char *mimetypestr = g_malloc0(len / 2 + 1); 
    242242                                                                if (OBEX_UnicodeToChar((unsigned char *)mimetypestr, header.bs, len / 2 + 1) == -1) { 
    243243                                                                        g_free(mimetypestr); 
    244244                                                                        smlErrorSet(&error, SML_ERROR_GENERIC, "unable to convert from unicode"); 
    245245                                                                        goto error; 
    246                                                                 } 
     246                                                                }*/ 
    247247                                                                 
    248248                                                                smlTrace(TRACE_INTERNAL, "Found type: %s", mimetypestr); 
     
    282282                                        } 
    283283                                         
    284                                         link = smlLinkFind(linkenv->env->tsp, linkenv); 
    285                                         if (!link) { 
    286                                                 smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to find link"); 
    287                                                 goto error; 
    288                                         } 
    289                                          
    290                                         smlLinkRef(link); 
     284                                        if (!linkenv->send_data) { 
     285                                                smlErrorSet(&error, SML_ERROR_GENERIC, "No data to send"); 
     286                                                goto error; 
     287                                        } 
     288                                         
     289                                        if (mimetype != linkenv->send_data->type) { 
     290                                                smlErrorSet(&error, SML_ERROR_GENERIC, "Wrong mimetype requested"); 
     291                                                goto error; 
     292                                        } 
    291293                                         
    292294                                        /* Now the data and size */ 
     
    332334  
    333335                                         
    334                                         link = smlLinkNew(linkenv->env->tsp, linkenv, &error); 
    335                                         if (!link
    336                                                 goto error; 
    337                                          
    338                                         smlTransportReceiveEvent(linkenv->env->tsp, link, SML_TRANSPORT_EVENT_CONNECT_DONE, NULL, NULL); 
     336                                        linkenv->link = smlLinkNew(linkenv->env->tsp, linkenv, &error); 
     337                                        if (!linkenv->link
     338                                                goto error; 
     339                                         
     340                                        smlTransportReceiveEvent(linkenv->env->tsp, linkenv->link, SML_TRANSPORT_EVENT_CONNECT_DONE, NULL, NULL); 
    339341                                         
    340342                                        OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS); 
     
    344346                                        smlTrace(TRACE_INTERNAL, "Got DISCONNECT command"); 
    345347                                        OBEX_ObjectSetRsp(object, OBEX_RSP_SUCCESS, OBEX_RSP_SUCCESS); 
    346                                          
    347                                         link = smlLinkFind(linkenv->env->tsp, linkenv); 
    348                                         if (!link) 
    349                                                 goto error; 
    350                                          
    351                                         smlLinkRef(link); 
    352                                          
    353                                         smlTransportReceiveEvent(linkenv->env->tsp, link, SML_TRANSPORT_EVENT_DISCONNECT_DONE, NULL, NULL); 
    354                                          
    355348                                        break; 
    356349                                default: 
     
    374367error: 
    375368        OBEX_ObjectSetRsp(object, OBEX_RSP_BAD_REQUEST, OBEX_RSP_BAD_REQUEST); 
    376         if (link)                                        
    377                 smlTransportReceiveEvent(linkenv->env->tsp, link, SML_TRANSPORT_EVENT_ERROR, NULL, error); 
     369        smlTransportReceiveEvent(linkenv->env->tsp, linkenv->link, SML_TRANSPORT_EVENT_ERROR, NULL, error); 
    378370        smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(&error)); 
    379371        smlErrorDeref(&error); 
    380372} 
    381373 
     374/* These function supervise the file descriptor where we are listening for incoming 
     375 * connections */ 
     376static gboolean _fd_prepare(GSource *source, gint *timeout_) 
     377{ 
     378        /* There is not need to dispatch this too fast since 
     379         * it does not influence the transmission of data, only 
     380         * the connection */ 
     381        *timeout_ = 50; 
     382        return FALSE; 
     383} 
     384 
     385static gboolean _fd_check(GSource *source) 
     386{ 
     387        SmlTransportObexServerEnv *env = *((SmlTransportObexServerEnv **)(source + 1)); 
     388        fd_set rfds; 
     389        FD_ZERO(&rfds); 
     390        FD_SET(env->fd, &rfds); 
     391            
     392        struct timeval tv; 
     393        tv.tv_sec = 0; 
     394        tv.tv_usec = 1; 
     395 
     396        int retval = select(env->fd + 1, &rfds, NULL, NULL, &tv); 
     397         
     398        if (retval == -1) 
     399                smlTrace(TRACE_ERROR, "%s: Unable to select()", __func__); 
     400        else if (retval) 
     401        return TRUE; 
     402         
     403        return FALSE; 
     404} 
     405 
    382406static gboolean _fd_dispatch(GSource *source, GSourceFunc callback, gpointer user_data) 
    383407{ 
     408        smlTrace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, source, callback, user_data); 
    384409        SmlTransportObexServerEnv *env = user_data; 
    385410        SmlError *error = NULL; 
    386         obex_t *obexhandle = NULL; 
    387411         
    388412        struct sockaddr_in addr; 
    389413        memset(&addr, 0, sizeof(addr)); 
    390414        socklen_t addrlen = sizeof(addr); 
    391                         
     415         
    392416        smlTrace(TRACE_INTERNAL, "dispatch %i", env->fd); 
    393         int fd = accept(env->fd, (struct sockaddr *)&addr, &addrlen); 
    394         smlTrace(TRACE_INTERNAL, "New fs %i", fd); 
    395          
    396         if (fd == -1) 
    397                 smlTrace(TRACE_INTERNAL, "error: %s, %i %i", strerror(errno), errno, EINVAL); 
    398         else { 
    399                 obexhandle = OBEX_Init(OBEX_TRANS_FD, _smlObexEvent, 0); 
    400                 if (!obexhandle) { 
    401                         smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to create new handle"); 
    402                         goto error_close_fd; 
    403                 } 
    404                          
    405                 /* Start the obex transport */ 
    406                 if (FdOBEX_TransportSetup(obexhandle, fd, fd, 4096) < 0) { 
    407                         smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to setup"); 
    408                         goto error_clean_obex; 
    409                 } 
     417         
     418        /* Create the env that handles this link */ 
     419        SmlLinkObexServerEnv *linkenv = smlTryMalloc0(sizeof(SmlLinkObexServerEnv), &error); 
     420        if (!linkenv) 
     421                goto error; 
     422        linkenv->env = env; 
     423         
     424        linkenv->fd = accept(env->fd, (struct sockaddr *)&addr, &addrlen); 
     425        if (linkenv->fd == -1) { 
     426                smlErrorSet(&error, SML_ERROR_GENERIC, "error: %s, %i", strerror(errno), errno); 
     427                goto error_free_env; 
     428        } 
     429        smlTrace(TRACE_INTERNAL, "New fs %i", linkenv->fd); 
     430         
     431         
     432        linkenv->handle = OBEX_Init(OBEX_TRANS_FD, _smlObexEvent, 0); 
     433        if (!linkenv->handle) { 
     434                smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to create new handle"); 
     435                goto error_close_fd; 
     436        } 
    410437                 
    411                 /* Now we create a source that watches the new obex connection */ 
    412                 GSource *idle = g_idle_source_new(); 
    413                 g_source_set_callback(idle, _dispatch_obex, obexhandle, NULL); 
    414                 g_source_attach(idle, env->tsp->context); 
    415                  
    416                 SmlLinkObexServerEnv *linkenv = smlTryMalloc0(sizeof(SmlLinkObexServerEnv), &error); 
    417                 if (!linkenv) 
    418                         goto error_clean_obex; 
    419                                          
    420                                 /* We first have to add the connection id which we generate for this transport */ 
    421                 linkenv->conid = smlTransportObexServerGenerateId(env); 
    422                 linkenv->handle = obexhandle; 
    423                 linkenv->env = env; 
    424                  
    425                 OBEX_SetUserData(obexhandle, linkenv); 
    426  
    427         // Create a buffer that will hold the stream chunks 
    428             /*env->stream_chunk = smlTryMalloc0(STREAM_CHUNK, error); 
    429             if (!env->stream_chunk) 
    430         goto error_close_handle;*/ 
    431         } 
    432          
     438        /* Start the obex transport */ 
     439        if (FdOBEX_TransportSetup(linkenv->handle, linkenv->fd, linkenv->fd, 4096) < 0) { 
     440                smlErrorSet(&error, SML_ERROR_GENERIC, "Unable to setup"); 
     441                goto error_clean_obex; 
     442        } 
     443         
     444        /* Now we create a source that watches the new obex connection */ 
     445        linkenv->source = g_idle_source_new(); 
     446        g_source_set_callback(linkenv->source, _dispatch_obex, linkenv, NULL); 
     447        g_source_attach(linkenv->source, env->tsp->context); 
     448         
     449        /* We first have to add the connection id which we generate for this transport */ 
     450        env->lastConId++; 
     451        linkenv->conid = env->lastConId; 
     452 
     453        OBEX_SetUserData(linkenv->handle, linkenv); 
     454         
     455        smlTrace(TRACE_EXIT, "%s", __func__); 
    433456        return TRUE; 
    434457 
    435458error_clean_obex: 
    436         OBEX_Cleanup(obexhandle); 
     459        OBEX_Cleanup(linkenv->handle); 
    437460error_close_fd: 
    438         close(fd); 
    439         smlTrace(TRACE_ERROR, "%s: %s", __func__, smlErrorPrint(&error)); 
    440         smlErrorDeref(&error); 
     461        close(linkenv->fd); 
     462error_free_env: 
     463        g_free(linkenv); 
     464error: 
     465        smlTransportReceiveEvent(env->tsp, NULL, SML_TRANSPORT_EVENT_ERROR, NULL, error); 
     466        smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(&error)); 
    441467        return TRUE; 
    442468} 
     
    516542         
    517543        /* Now we create a source that watches for incoming connection requests */ 
    518         GSourceFuncs *functions = g_malloc0(sizeof(GSourceFuncs)); 
    519         functions->prepare = _fd_prepare; 
    520         functions->check = _fd_check; 
    521         functions->dispatch = _fd_dispatch; 
    522         functions->finalize = NULL; 
    523  
    524         GSource *source = g_source_new(functions, sizeof(GSource) + sizeof(SmlTransportObexServerEnv *)); 
    525         SmlTransportObexServerEnv **envptr = (SmlTransportObexServerEnv **)(source + 1); 
     544        env->functions = smlTryMalloc0(sizeof(GSourceFuncs), error); 
     545        if (!env->functions) 
     546                goto error_close_handle; 
     547        env->functions->prepare = _fd_prepare; 
     548        env->functions->check = _fd_check; 
     549        env->functions->dispatch = _fd_dispatch; 
     550        env->functions->finalize = NULL; 
     551 
     552        env->source = g_source_new(env->functions, sizeof(GSource) + sizeof(SmlTransportObexServerEnv *)); 
     553        SmlTransportObexServerEnv **envptr = (SmlTransportObexServerEnv **)(env->source + 1); 
    526554        *envptr = env; 
    527         g_source_set_callback(source, NULL, env, NULL); 
    528         env->source = source; 
    529         env->functions = functions; 
    530         g_source_attach(source, tsp->context); 
     555        g_source_set_callback(env->source, NULL, env, NULL); 
     556        g_source_attach(env->source, tsp->context); 
    531557 
    532558     
     
    535561 
    536562error_close_handle: 
    537 //     OBEX_Cleanup(env->obexhandle); 
     563       close(env->fd); 
    538564error_free_env: 
     565        g_free(env->path); 
    539566        g_free(env); 
    540567error: 
     
    551578        smlAssert(env->tsp); 
    552579         
     580        close(env->fd); 
     581        g_free(env->functions); 
     582        g_source_destroy(env->source); 
     583        g_free(env->path); 
     584         
    553585        g_free(env); 
    554586         
     
    559591static void smlTransportObexServerSend(void *userdata, void *link, SmlTransportData *data, SmlError *error) 
    560592{ 
    561         smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %d, %i, %p)", __func__, userdata, link, data, error); 
     593        smlTrace(TRACE_ENTRY, "%s(%p, %p, %p, %p)", __func__, userdata, link, data, error); 
    562594        smlAssert(data); 
    563595        smlAssert(userdata); 
    564         SmlTransportObexServerEnv *env = userdata; 
    565596        SmlLinkObexServerEnv *linkenv = link; 
    566         smlAssert(env); 
    567         smlAssert(linkenv); 
    568597         
    569598        if (error) 
    570599                goto error; 
    571  
     600         
    572601        if (linkenv->send_data) { 
    573602                smlErrorSet(&error, SML_ERROR_GENERIC, "We already have waiting data"); 
    574603                goto error; 
    575604        } 
    576  
     605         
    577606        linkenv->send_data = data; 
    578  
     607        smlTransportDataRef(linkenv->send_data); 
     608         
    579609        smlTrace(TRACE_EXIT, "%s", __func__); 
    580610        return; 
     
    583613        smlErrorDeref(&error); 
    584614        smlTrace(TRACE_EXIT_ERROR, "%s: %s", __func__, smlErrorPrint(&error)); 
     615        return; 
     616} 
     617 
     618static void smlTransportObexServerDisconnect(void *data, void *linkdata) 
     619{ 
     620        smlTrace(TRACE_ENTRY, "%s(%p, %p)", __func__, data, linkdata); 
     621        smlAssert(data); 
     622        smlAssert(linkdata); 
     623        SmlLinkObexServerEnv *linkenv = linkdata; 
     624         
     625        linkenv->disconnect = TRUE; 
     626         
     627        smlTrace(TRACE_EXIT, "%s", __func__); 
    585628        return; 
    586629} 
     
    597640        tsp->functions.finalize = smlTransportObexServerFinalize; 
    598641        tsp->functions.send = smlTransportObexServerSend; 
     642        tsp->functions.disconnect = smlTransportObexServerDisconnect; 
    599643        return TRUE; 
    600644} 
  • branches/development-branch/libsyncml/transports/obex_server_internals.h

    r101 r111  
    3939 
    4040typedef struct SmlLinkObexServerEnv { 
     41        SmlLink *link; 
    4142        SmlTransportObexServerEnv *env; 
    4243        uint32_t conid; 
    4344        obex_t *handle; 
    4445        uint8_t *stream_chunk; 
    45          
    46         SmlBool busy; 
    47         SmlErrorType result; 
    48         char *message; 
    49          
     46        int fd; 
     47        SmlBool destroy; 
     48        SmlBool disconnect; 
    5049        GSource *source; 
    51         GSourceFuncs *functions; 
    5250         
    5351        SmlTransportData *send_data; 
  • branches/development-branch/tests/check_obex.c

    r101 r111  
    9898END_TEST 
    9999 
    100 #if 0 
     100int client_connect_done = 0; 
     101int client_disconnect_done = 0; 
     102int client_receives = 0; 
     103int client_errors = 0; 
     104 
     105void _recv_client_event(SmlTransport *tsp, SmlLink *link, SmlTransportEventType type, SmlTransportData *data, SmlError *error, void *userdata) 
     106
     107        smlTrace(TRACE_ENTRY, "%s()", __func__); 
     108         
     109        fail_unless(GPOINTER_TO_INT(userdata) == 1); 
     110         
     111        switch (type) { 
     112                case SML_TRANSPORT_EVENT_CONNECT_DONE: 
     113