三木社区

 找回密码
 立即注册
搜索
热搜: 活动 交友 discuz
查看: 547|回复: 0
打印 上一主题 下一主题

user_service_request_event_handler

[复制链接]

1657

主题

1684

帖子

5684

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
5684
跳转到指定楼层
楼主
发表于 2019-6-17 15:44:58 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
用户服务请求事件处理程序
  1. static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
  2.         const char *request, const int request_len,
  3.         char **response, int *response_len)
  4. {
  5.     int contrastratio = 0, to_cloud = 0;
  6.     cJSON *root = NULL, *item_transparency = NULL, *item_from_cloud = NULL;
  7.     EXAMPLE_TRACE("Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len,
  8.                   serviceid,
  9.                   request);

  10.     /* Parse Root */
  11.     root = cJSON_Parse(request);
  12.     if (root == NULL || !cJSON_IsObject(root)) {
  13.         EXAMPLE_TRACE("JSON Parse Error");
  14.         return -1;
  15.     }

  16.     if (strlen("Custom") == serviceid_len && memcmp("Custom", serviceid, serviceid_len) == 0) {
  17.         /* Parse Item */
  18.         const char *response_fmt = "{"Contrastratio":%d}";
  19.         item_transparency = cJSON_GetObjectItem(root, "transparency");
  20.         if (item_transparency == NULL || !cJSON_IsNumber(item_transparency)) {
  21.             cJSON_Delete(root);
  22.             return -1;
  23.         }
  24.         EXAMPLE_TRACE("transparency: %d", item_transparency->valueint);
  25.         contrastratio = item_transparency->valueint + 1;

  26.         /* Send Service Response To Cloud */
  27.         *response_len = strlen(response_fmt) + 10 + 1;
  28.         *response = (char *)HAL_Malloc(*response_len);
  29.         if (*response == NULL) {
  30.             EXAMPLE_TRACE("Memory Not Enough");
  31.             return -1;
  32.         }
  33.         memset(*response, 0, *response_len);
  34.         HAL_Snprintf(*response, *response_len, response_fmt, contrastratio);
  35.         *response_len = strlen(*response);
  36.     } else if (strlen("SyncService") == serviceid_len && memcmp("SyncService", serviceid, serviceid_len) == 0) {
  37.         /* Parse Item */
  38.         const char *response_fmt = "{"ToCloud":%d}";
  39.         item_from_cloud = cJSON_GetObjectItem(root, "FromCloud");
  40.         if (item_from_cloud == NULL || !cJSON_IsNumber(item_from_cloud)) {
  41.             cJSON_Delete(root);
  42.             return -1;
  43.         }
  44.         EXAMPLE_TRACE("FromCloud: %d", item_from_cloud->valueint);
  45.         to_cloud = item_from_cloud->valueint + 1;

  46.         /* Send Service Response To Cloud */
  47.         *response_len = strlen(response_fmt) + 10 + 1;
  48.         *response = (char *)HAL_Malloc(*response_len);
  49.         if (*response == NULL) {
  50.             EXAMPLE_TRACE("Memory Not Enough");
  51.             return -1;
  52.         }
  53.         memset(*response, 0, *response_len);
  54.         HAL_Snprintf(*response, *response_len, response_fmt, to_cloud);
  55.         *response_len = strlen(*response);
  56.     }
  57.     cJSON_Delete(root);

  58.     return 0;
  59. }
复制代码



回复

使用道具 举报

Archiver|手机版|小黑屋|三木电子社区 ( 辽ICP备11000133号-4 )

辽公网安备 21021702000620号

GMT+8, 2026-3-19 00:42 , Processed in 0.022655 second(s), 22 queries .

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表