Exemplo n.º 1
0
static int json_get_value_size(struct json_parse_state_s* state) {
  if (json_skip_whitespace(state)) {
    // consumed the whole buffer when we expected a value!
    return 1;
  }

  state->dom_size += sizeof(struct json_value_s);

  switch (state->src[state->offset]) {
  case '"':
    return json_get_string_size(state);
  case '{':
    return json_get_object_size(state);
  case '[':
    return json_get_array_size(state);
  case '-':
  case '0':
  case '1':
  case '2':
  case '3':
  case '4':
  case '5':
  case '6':
  case '7':
  case '8':
  case '9':
    return json_get_number_size(state);
  default:
    if ((state->offset + 4) < state->size &&
      't' == state->src[state->offset + 0] &&
      'r' == state->src[state->offset + 1] &&
      'u' == state->src[state->offset + 2] &&
      'e' == state->src[state->offset + 3]) {
      state->offset += 4;
      return 0;
    } else if ((state->offset + 5) < state->size &&
      'f' == state->src[state->offset + 0] &&
      'a' == state->src[state->offset + 1] &&
      'l' == state->src[state->offset + 2] &&
      's' == state->src[state->offset + 3] &&
      'e' == state->src[state->offset + 4]) {
      state->offset += 5;
      return 0;
    } else if ((state->offset + 4) < state->size &&
      'n' == state->src[state->offset + 0] &&
      'u' == state->src[state->offset + 1] &&
      'l' == state->src[state->offset + 2] &&
      'l' == state->src[state->offset + 3]) {
      state->offset += 4;
      return 0;
    }

    // invalid value!
    return 1;
  }
}
Exemplo n.º 2
0
/*
--> {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState", "lightSwitchState"]}
<-- {"uuid":"42E6E69DAEF6483FBBA412A28AF7CD76","attrSet":["wlanSwitchState"],"wlanSwitchState":{"value":"1","when":"1404443369"}}
*/
static int msdp_get_status(char *params)
{
    int ret = SERVICE_RESULT_ERR;
    char *attrset;
    char *json_out = NULL;
    char params_in[512] = {0};
    int attrset_size, attrset_len = 0;

    //char szPostString[1024] = "{\"uuid\":\"2427287C75CD579897D3DB5854E46A2F\",\"wlanSwitchState\":{\"value\":\"1\",\"when\":\"1404443369\"}}";
    attrset = json_get_value_by_name(params, strlen(params), JSON_KEY_ATTRSET,
                                     &attrset_len, NULL);
    if (attrset) {
        attrset_size = json_get_array_size(attrset, attrset_len);
    }

    if (NULL == attrset || attrset_size == 0) {
        char uuid[MAX_UUID_LEN] = {0};
        int uuid_len = 0;
        char *uuid_ptr = json_get_value_by_name(params, strlen(params), JSON_KEY_UUID,
                                                &uuid_len, NULL);
        PTR_RETURN(uuid_ptr, SERVICE_RESULT_ERR, CALL_FUCTION_FAILED,
                   "json_get_value_by_name(uuid)");
        strncpy(uuid, uuid_ptr, sizeof(uuid) > uuid_len ? uuid_len : sizeof(uuid) - 1);

        char attrset[512] = {0};
        ret = msdp_get_all_attrname(uuid, attrset, sizeof(attrset));
        RET_RETURN(ret, "get all attribute name fail, uuid:%s", uuid);
        log_trace("attrset = %s\n", attrset);

        snprintf(params_in, sizeof(params_in) - 1, GET_DEVICE_ATTR_STRING_FMT, uuid,
                 attrset);
        params = params_in;
    }

    //遍历attrset,并调用厂商注册的接口,返回属性json串
    ret = msdp_get_device_status_handler(params, msdp_get_attr_each_cb,
                                         __get_attribute, &json_out);
    RET_RETURN(ret, CALL_FUCTION_FAILED, "msdp_status_handler");

    log_trace("json_out = %s", json_out ? (json_out) : "NULL");
    //todo: postDeviceData/deviceDataChange
    if (json_out) {
        int str_len, post_ret = SERVICE_RESULT_ERR;
        char devid_str[18] = {0};
        char *dev_id = json_get_value_by_name(params, strlen(params), JSON_KEY_DEVID,
                                              &str_len, NULL);
        if (dev_id != NULL) {
            post_ret = lmns_post_device_data(devid_str, json_out);
            RET_GOTO(post_ret, end, CALL_FUCTION_FAILED, "lmns_post_device_data");
        } else {
            int post_ret = msdp_add_asyncpost_task(json_out);
            RET_GOTO(post_ret, end, "add async post task fail, params:%s", json_out);
            json_out = NULL;
        }
    }

end:
    if (json_out) {
        msdp_free_buff(json_out);
    }

    return ret;
}
Exemplo n.º 3
0
static int json_get_value_size(struct json_parse_state_s *state,
                               int is_global_object) {
  if (is_global_object) {
    state->dom_size += sizeof(struct json_value_s);

    return json_get_object_size(state, /* is_global_object = */ 1);
  } else {
    state->dom_size += sizeof(struct json_value_s);

    if (json_skip_all_skippables(state)) {
      state->error = json_parse_error_premature_end_of_buffer;
      return 1;
    }

    switch (state->src[state->offset]) {
    case '"':
      return json_get_string_size(state);
    case '{':
      return json_get_object_size(state, /* is_global_object = */ 0);
    case '[':
      return json_get_array_size(state);
    case '+':
    case '-':
    case '0':
    case '1':
    case '2':
    case '3':
    case '4':
    case '5':
    case '6':
    case '7':
    case '8':
    case '9':
    case '.':
      return json_get_number_size(state);
    default:
      if ((state->offset + 4) <= state->size &&
          't' == state->src[state->offset + 0] &&
          'r' == state->src[state->offset + 1] &&
          'u' == state->src[state->offset + 2] &&
          'e' == state->src[state->offset + 3]) {
        state->offset += 4;
        return 0;
      } else if ((state->offset + 5) <= state->size &&
                 'f' == state->src[state->offset + 0] &&
                 'a' == state->src[state->offset + 1] &&
                 'l' == state->src[state->offset + 2] &&
                 's' == state->src[state->offset + 3] &&
                 'e' == state->src[state->offset + 4]) {
        state->offset += 5;
        return 0;
      } else if ((state->offset + 4) <= state->size &&
                 'n' == state->src[state->offset + 0] &&
                 'u' == state->src[state->offset + 1] &&
                 'l' == state->src[state->offset + 2] &&
                 'l' == state->src[state->offset + 3]) {
        state->offset += 4;
        return 0;
      }

      // invalid value!
      state->error = json_parse_error_invalid_value;
      return 1;
    }
  }
}