基础API

更新时间:2018-10-19 11:29:34

linkkit_start

定义描述

函数原型 int linkkit_start(int max_buffered_msg, int get_tsl_from_cloud, linkkit_loglevel_t log_level, linkkit_ops_t ops, linkkit_cloud_domain_type_t domain_type, void user_context)
描述 启动 linkkit 服务,与云端建立连接并安装回调函数

请求参数

参数 类型 必填 描述 备注
max_buffered_msg int 消息队列大小 此参数影响事件消息最大暂存数,频繁业务可适当增大此参数,建议值 [16~32]
get_tsl_from_cloud int 获取 TSL 路径 0: 从本地获取物模型
非0: 从云端获取物模型
log_level linkkit_loglevel_t 设置打印的日志等级 [0~5]: 数字越大, 打印越详细
ops linkkit_ops_t* linkkit 事件回调函数 linkkit 通过回调函数将事件通知给用户
domain_type linkkit_cloud_domain_type_t 云服务器域名 linkkit_cloud_domain_sh: 上海域名
linkkit_cloud_domain_usa: 美国域名
user_context void* 用户上下文环境指针 用户自定义结构体的指针, 回调函数中传出

示例

/* user sample context struct. */
typedef struct _sample_context {
    void* thing;

    int cloud_connected;
    int thing_enabled;

    int service_custom_input_transparency;
    int service_custom_output_contrastratio;
#ifdef SERVICE_OTA_ENABLED
    char ota_buffer[OTA_BUFFER_SIZE];
#endif /* SERVICE_OTA_ENABLED */
} sample_context_t;

static int on_connect(void* ctx)
{
    sample_context_t* sample = ctx;

    sample->cloud_connected = 1;

    LINKKIT_PRINTF("cloud is connected\n");

    return 0;
}

static int on_disconnect(void* ctx)
{
    sample_context_t* sample = ctx;

    sample->cloud_connected = 0;

    LINKKIT_PRINTF("cloud is disconnect\n");

    return 0;
}

static int raw_data_arrived(void* thing_id, void* data, int len, void* ctx)
{
    char raw_data[128] = {0};

    LINKKIT_PRINTF("raw data arrived,len:%d\n", len);

    /* do user's raw data process logical here. */

    /* ............................... */

    /* user's raw data process logical complete */

    snprintf(raw_data, sizeof(raw_data), "test down raw reply data %lld", HAL_UptimeMs());

    linkkit_invoke_raw_service(thing_id, 0, raw_data, strlen(raw_data));

    return 0;
}

static int thing_create(void* thing_id, void* ctx)
{
    //物创建成功,SDK会调用此函数,thing_id为新创建的物的标识符.
    sample_context_t* sample = ctx;

    LINKKIT_PRINTF("new thing@%p created.\n", thing_id);

    sample->thing = thing_id;

    return 0;
}

static int thing_enable(void* thing_id, void* ctx)
{
    sample_context_t* sample = ctx;

    sample->thing_enabled = 1;

    return 0;
}

static int thing_disable(void* thing, void* ctx)
{
    sample_context_t* sample = ctx;

    sample->thing_enabled = 0;

    return 0;
}
#ifdef RRPC_ENABLED
static int handle_service_custom(sample_context_t* sample, void* thing, char* service_identifier, int request_id, int rrpc)
#else
static int handle_service_custom(sample_context_t* sample, void* thing, char* service_identifier, int request_id)
#endif /* RRPC_ENABLED */
{
    char identifier[128] = {0};

    /*
     * get iutput value.
     */
    snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier, "transparency");
    linkkit_get_value(linkkit_method_get_service_input_value, thing, identifier, &sample->service_custom_input_transparency, NULL);

    /*
     * set output value according to user's process result.
     */

    snprintf(identifier, sizeof(identifier), "%s.%s", service_identifier, "Contrastratio");

    sample->service_custom_output_contrastratio = sample->service_custom_input_transparency >= 0 ? sample->service_custom_input_transparency : sample->service_custom_input_transparency * -1;

    linkkit_set_value(linkkit_method_set_service_output_value, thing, identifier, &sample->service_custom_output_contrastratio, NULL);

    /*
     * reponse to service requests
     */
#ifdef RRPC_ENABLED
    linkkit_answer_service(thing, service_identifier, request_id, 200, rrpc);
#else
    linkkit_answer_service(thing, service_identifier, request_id, 200);
#endif /* RRPC_ENABLED */

    return 0;
}
#ifdef RRPC_ENABLED
static int thing_call_service(void* thing_id, char* service, int request_id, int rrpc, void* ctx)
#else
static int thing_call_service(void* thing_id, char* service, int request_id, void* ctx)
#endif /* RRPC_ENABLED */
{
    sample_context_t* sample = ctx;

    LINKKIT_PRINTF("service(%s) requested, id: thing@%p, request id:%d\n",
                   service, thing_id, request_id);

    if (strcmp(service, "Custom") == 0) {
#ifdef RRPC_ENABLED
        handle_service_custom(sample, thing_id, service, request_id, rrpc);
#else
        handle_service_custom(sample, thing_id, service, request_id);
#endif /* RRPC_ENABLED */
    }

    return 0;
}

static int thing_prop_changed(void* thing_id, char* property, void* ctx)
{
    char* value_str = NULL;
    char property_buf[64] = {0};

    /* get new property value */
    if (strstr(property, "HSVColor") != 0) {
        int hue, saturation, value;

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Hue");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &hue, &value_str);

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Saturation");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &saturation, &value_str);

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Value");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &value, &value_str);

        LINKKIT_PRINTF("property(%s), Hue:%d, Saturation:%d, Value:%d\n", property, hue, saturation, value);

        /* XXX: do user's process logical here. */
    } else if (strstr(property, "HSLColor") != 0) {
        int hue, saturation, lightness;

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Hue");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &hue, &value_str);

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Saturation");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &saturation, &value_str);

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Lightness");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &lightness, &value_str);

        LINKKIT_PRINTF("property(%s), Hue:%d, Saturation:%d, Lightness:%d\n", property, hue, saturation, lightness);
        /* XXX: do user's process logical here. */
    }  else if (strstr(property, "RGBColor") != 0) {
        int red, green, blue;

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Red");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &red, &value_str);

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Green");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &green, &value_str);

        snprintf(property_buf, sizeof(property_buf), "%s.%s", property, "Blue");
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property_buf, &blue, &value_str);

        LINKKIT_PRINTF("property(%s), Red:%d, Green:%d, Blue:%d\n", property, red, green, blue);
        /* XXX: do user's process logical here. */
    } else {
        linkkit_get_value(linkkit_method_get_property_value, thing_id, property, NULL, &value_str);

        LINKKIT_PRINTF("#### property(%s) new value set: %s ####\n", property, value_str);
    }

    /* do user's process logical here. */
    linkkit_trigger_event(thing_id, EVENT_PROPERTY_POST_IDENTIFIER, property);

    return 0;
}

static linkkit_ops_t alinkops = {
    .on_connect         = on_connect,             //连云成功回调
    .on_disconnect      = on_disconnect,          //断线回调
    .raw_data_arrived   = raw_data_arrived,       //裸数据到达回调
    .thing_create       = thing_create,           //物创建成功回调
    .thing_enable       = thing_enable,           //物使能回调
    .thing_disable      = thing_disable,          //物禁止回调
    .thing_call_service = thing_call_service,     //服务请求回调
    .thing_prop_changed = thing_prop_changed,     //物属性修改回调
};

int main()
{
    int get_tsl_from_cloud = 1;
    int ret = linkkit_start(16, get_tsl_from_cloud, linkkit_loglevel_debug, &alinkops, linkkit_cloud_domain_sh, sample_ctx);
    ......
}

返回值

详细描述
0 linkkit 服务程序启动成功
-1 linkkit 服务程序启动失败

linkkit_end

定义描述

函数原型 int linkkit_end()
描述 停止 linkkit 服务,与云端断开连接并回收资源

示例

int ret = linkkit_end();

返回值

详细描述
0 linkkit 服务停止成功
-1 linkkit 服务停止失败

linkkit_dispatch

定义描述

函数原型 int linkkit_dispatch()
描述 事件分发函数,触发 linkkit_start 安装的回调

示例

while(1) {

    //调用事件分发函数,长时间未调用此函数会造成事件消息堆积。
    linkkit_dispatch();
    .............
}

返回值

详细描述
0 成功
-1 失败

linkkit_yield

定义描述

函数原型 int linkkit_yield(int timeout_ms)
描述 linkkit 主循环函数,内含了心跳的维持, 服务器下行报文的收取等;如果允许多线程,请不要调用此函数

请求参数

参数 类型 必填 描述 备注
timeout_ms int 超时时间(毫秒) 此参数会影响阻塞在此函数时间,建议设为[100~1000]

示例

while (1) {
    linkkit_dispatch();

    now = uptime_sec();
    if (prev_sec == now) {

    //如果允许多线程,请不要调用linkkit_yield
#ifdef CMP_SUPPORT_MULTI_THREAD
        HAL_SleepMs(100);
#else
        linkkit_yield(100);
#endif
        continue;
    }

    if (exit) break;

    /* after all, this is an sample, give a chance to return... */
    /* modify this value for this sample executaion time period */
    if (now > 60 * execution_time) exit = 1;
        prev_sec = now;
}

返回值

详细描述
0 成功
-1 失败

results matching ""

    No results matching ""