Search code examples
ccallbacktizen

C passing structure to a callback function (Tizen)


I'm writing an app for Tizen (smart watches). Can't pass a structure to a callback. My typedef:

typedef struct _Health {
    ...;
    const char *data;
} health_s;

Declaring a structure and initializing it:

health_s health = {
    ...,
    .data = "steps"
};

Passing it to a function:

_app_check_and_request_permission("http://tizen.org/privilege/healthinfo", &health);

Getting and checking if the struct address passed well:

void _app_check_and_request_permission(const char *privilege, void *user_data)
{
    health_s *p_health = user_data;
    dlog_print(DLOG_INFO, LOG_TAG, "Health data: %s", p_health->data);
    ...

And here I see in the log "Health data: steps". Passed well. Then I'm trying to pass user_data to a callback function

    ...
    ppm_request_permission(privilege, _app_request_response_cb, p_health);
}

From function description in Tizen docs:

...
* @param[in]   user_data   User specific data which will be passed to
*                          the given callback.
int ppm_request_permission(const char *privilege,
                           ppm_request_response_cb callback,
                           void *user_data);

Callback declaration:

static void _app_request_response_cb(ppm_call_cause_e cause, ppm_request_result_e result,
                             const char *privilege, void *user_data);

And in the callback I'm getting Health data: NULL. Tried to pass p_health->data to the callback and that's ok. The problem is with passing the whole structure. What am I doing wrong? Thanks. Update: Checked the addresses of the passed structure in the _app_check_and_request_permission function and in the callback and they are the same. But the struct in the callback is still empty...


Solution

  • Probably the lifetime of health has ended before _app_request_response_cb is called by the system on completion of the request. This is, for example, the case if the shown definition of health is contained in a function which returns before the request completes. Simple remedy: define health with storage class static.