|
|
用户服务请求事件处理程序
- static int user_service_request_event_handler(const int devid, const char *serviceid, const int serviceid_len,
- const char *request, const int request_len,
- char **response, int *response_len)
- {
- int contrastratio = 0, to_cloud = 0;
- cJSON *root = NULL, *item_transparency = NULL, *item_from_cloud = NULL;
- EXAMPLE_TRACE("Service Request Received, Devid: %d, Service ID: %.*s, Payload: %s", devid, serviceid_len,
- serviceid,
- request);
- /* Parse Root */
- root = cJSON_Parse(request);
- if (root == NULL || !cJSON_IsObject(root)) {
- EXAMPLE_TRACE("JSON Parse Error");
- return -1;
- }
- if (strlen("Custom") == serviceid_len && memcmp("Custom", serviceid, serviceid_len) == 0) {
- /* Parse Item */
- const char *response_fmt = "{"Contrastratio":%d}";
- item_transparency = cJSON_GetObjectItem(root, "transparency");
- if (item_transparency == NULL || !cJSON_IsNumber(item_transparency)) {
- cJSON_Delete(root);
- return -1;
- }
- EXAMPLE_TRACE("transparency: %d", item_transparency->valueint);
- contrastratio = item_transparency->valueint + 1;
- /* Send Service Response To Cloud */
- *response_len = strlen(response_fmt) + 10 + 1;
- *response = (char *)HAL_Malloc(*response_len);
- if (*response == NULL) {
- EXAMPLE_TRACE("Memory Not Enough");
- return -1;
- }
- memset(*response, 0, *response_len);
- HAL_Snprintf(*response, *response_len, response_fmt, contrastratio);
- *response_len = strlen(*response);
- } else if (strlen("SyncService") == serviceid_len && memcmp("SyncService", serviceid, serviceid_len) == 0) {
- /* Parse Item */
- const char *response_fmt = "{"ToCloud":%d}";
- item_from_cloud = cJSON_GetObjectItem(root, "FromCloud");
- if (item_from_cloud == NULL || !cJSON_IsNumber(item_from_cloud)) {
- cJSON_Delete(root);
- return -1;
- }
- EXAMPLE_TRACE("FromCloud: %d", item_from_cloud->valueint);
- to_cloud = item_from_cloud->valueint + 1;
- /* Send Service Response To Cloud */
- *response_len = strlen(response_fmt) + 10 + 1;
- *response = (char *)HAL_Malloc(*response_len);
- if (*response == NULL) {
- EXAMPLE_TRACE("Memory Not Enough");
- return -1;
- }
- memset(*response, 0, *response_len);
- HAL_Snprintf(*response, *response_len, response_fmt, to_cloud);
- *response_len = strlen(*response);
- }
- cJSON_Delete(root);
- return 0;
- }
复制代码
|
|