static bool write_repeated_emptymsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { EmptyMessage emptymsg = {0}; return pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, EmptyMessage_fields, &emptymsg); }
static bool write_repeated_submsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { SubMessage dummy = {""}; return pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, SubMessage_fields, &dummy) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, SubMessage_fields, &dummy) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, SubMessage_fields, &dummy) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, SubMessage_fields, &dummy) && pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, SubMessage_fields, *arg); }
/* Parse a dictionary in format {'name': value} and encode it directly to protobuf */ static bool encode_dictionary(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { int textlen; char *p = (char*)*arg; if (*p == '{') p++; while (*p != '}') { KeyValuePair pair = KeyValuePair_init_zero; if (*p != '\'') PB_RETURN_ERROR(stream, "invalid key, missing quote"); p++; /* Starting quote of key */ textlen = strchr(p, '\'') - p; strncpy(pair.key, p, textlen); pair.key[textlen] = 0; p += textlen + 2; while (*p == ' ') p++; if (*p == '[') { /* Value is a tree */ pair.treeValue.funcs.encode = encode_tree; pair.treeValue.arg = p; } else if (*p == '\'') { /* Value is a string */ pair.has_stringValue = true; p++; textlen = strchr(p, '\'') - p; strncpy(pair.stringValue, p, textlen); pair.stringValue[textlen] = 0; } else if (*p == '{') { /* Value is a dictionary */ pair.has_dictValue = true; pair.dictValue.dictItem.funcs.encode = encode_dictionary; pair.dictValue.dictItem.arg = p; } else { /* Value is integer */ pair.has_intValue = true; pair.intValue = atoi(p); } p = find_end_of_item(p); if (!pb_encode_tag_for_field(stream, field)) return false; if (!pb_encode_submessage(stream, KeyValuePair_fields, &pair)) return false; } return true; }
bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src) { if (field->ptr == NULL) return false; return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src); }
bool chargesensor_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { SensorReading sensormsg = SensorReading_init_zero; /* Fill in the lucky number */ sensormsg.id = 6; sensormsg.has_id = true; if (ltc.isValid()) { sensormsg.value1 = (int32_t)(ltc.getCharge()*1000); sensormsg.has_value1 = true; } else { sensormsg.error = 1; sensormsg.has_error = true; } /* This encodes the header for the field, based on the constant info from pb_field_t. */ if (!pb_encode_tag_for_field(stream, field)) return false; /* This encodes the data for the field, based on our FileInfo structure. */ if (!pb_encode_submessage(stream, SensorReading_fields, &sensormsg)) return false; return true; }
/* Parse a tree in format [[1 2] 3] and encode it directly to protobuf */ static bool encode_tree(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { TreeNode tree = TreeNode_init_zero; char *p = (char*)*arg; if (*p == '[') { /* This is a tree branch */ p++; tree.left.funcs.encode = encode_tree; tree.left.arg = p; p = find_end_of_item(p); tree.right.funcs.encode = encode_tree; tree.right.arg = p; } else { /* This is a leaf node */ tree.has_leaf = true; tree.leaf = atoi(p); } return pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, TreeNode_fields, &tree); }
static bool checkreturn pb_enc_submessage(pb_ostream_t *stream, const pb_field_t *field, const void *src) { if (field->ptr == NULL) PB_RETURN_ERROR(stream, "invalid field descriptor"); return pb_encode_submessage(stream, (const pb_field_t*)field->ptr, src); }
bool motordataEncoderCallback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { MotorMsg_Data_Param *tuple = ((MotorMsg_Data_Param *) (*arg)); pb_encode_tag_for_field(stream, field); return pb_encode_submessage(stream, MotorMsg_Data_Param_fields, tuple); }
bool encodeMotorMsg(pb_ostream_t *stream, const pb_field_t messagetype[], const void *message) { const pb_field_t *field; for (field = MotorMsg_fields; field->tag != 0; field++) { if (field->ptr == messagetype) { /* This is our field, encode the message using it. */ if (!pb_encode_tag_for_field(stream, field)) return false; return pb_encode_submessage(stream, messagetype, message); } } /* Didn't find the field for messagetype */ return false; }
static bool write_limits(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { Limits limits = {0}; limits.int32_min = INT32_MIN; limits.int32_max = INT32_MAX; limits.uint32_min = 0; limits.uint32_max = UINT32_MAX; limits.int64_min = INT64_MIN; limits.int64_max = INT64_MAX; limits.uint64_min = 0; limits.uint64_max = UINT64_MAX; limits.enum_min = HugeEnum_Negative; limits.enum_max = HugeEnum_Positive; return pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, Limits_fields, &limits); }
/** * Message EPS encode callback function * @param stream * @param field * @param arg * @return */ bool ss_eps_encode(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { EPS eps=EPS_init_zero; log_printf(LOG_VERBOSE,"Encoding EPS SubMessage!\n"); char** arg_eps = (char **)arg[0]; int i; int count=atoi(arg_eps[0]); arg_eps=arg_eps+1; for(i=0;i<count;i++) { arg_eps=arg_eps+i*7; strcpy(eps.profile_id,arg_eps[0]); eps.has_profile_id=true; strcpy(eps.device_id,arg_eps[1]); eps.has_device_id=true; strcpy(eps.zone_type,arg_eps[2]); eps.has_zone_type=true; strcpy(eps.ep,arg_eps[3]); eps.has_ep=true; strcpy(eps.ep_name,arg_eps[4]); eps.has_ep_name=true; eps.rid=atoi(arg_eps[5]); eps.has_rid=true; eps.arm=atoi(arg_eps[6]); eps.has_arm=true; if (!pb_encode_tag_for_field(stream, field)) return false; //This encodes the data for the field, based on our FileInfo structure. if (!pb_encode_submessage(stream, EPS_fields, &eps)) return false; } return true; }
bool switchsensor_callback(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { SensorReading sensormsg = SensorReading_init_zero; /* Fill in the lucky number */ sensormsg.id = 1; sensormsg.has_id = true; sensormsg.value1 = microSwitch.ReadState(); sensormsg.has_value1 = true; /* This encodes the header for the field, based on the constant info from pb_field_t. */ if (!pb_encode_tag_for_field(stream, field)) return false; /* This encodes the data for the field, based on our FileInfo structure. */ if (!pb_encode_submessage(stream, SensorReading_fields, &sensormsg)) return false; return true; }
/* This function is the core of the union encoding process. It handles * the top-level pb_field_t array manually, in order to encode a correct * field tag before the message. The pointer to MsgType_fields array is * used as an unique identifier for the message type. */ bool encode_unionmessage(pb_ostream_t *stream, const pb_msgdesc_t *messagetype, void *message) { pb_field_iter_t iter; if (!pb_field_iter_begin(&iter, UnionMessage_fields, message)) return false; do { if (iter.submsg_desc == messagetype) { /* This is our field, encode the message using it. */ if (!pb_encode_tag_for_field(stream, &iter)) return false; return pb_encode_submessage(stream, messagetype, message); } } while (pb_field_iter_next(&iter)); /* Didn't find the field for messagetype */ return false; }
bool pb_encode_delimited(pb_ostream_t *stream, const pb_field_t fields[], const void *src_struct) { return pb_encode_submessage(stream, fields, src_struct); }
static bool write_submsg(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { return pb_encode_tag_for_field(stream, field) && pb_encode_submessage(stream, SubMessage_fields, *arg); }
/** * Message Dev encode callback function * @param stream * @param field * @param arg * @return */ bool ss_pb_encode_devlist(pb_ostream_t *stream, const pb_field_t *field, void * const *arg) { int i,j; FILE *fd; log_printf(LOG_VERBOSE,"Encoding Devlist SubMessage!\n"); char *args[100]; char temp_str[100][40]; char temp[40]; Dev devlist=Dev_init_zero; struct dirent *ptr=NULL; DIR *dir; dir=opendir("/gl/etc/devlist"); while((ptr=readdir(dir))!=NULL) { //跳过’.'和’..’两个目录 if (ptr->d_name[0] == '.') continue; //printf("%s is ready...\n",ptr->d_name); char path[35]={'\0'}; strcat(path,DEVINFO_DIR); strcat(path, ptr->d_name); fd = fopen(path, "r"); if(fd==NULL)log_printf(LOG_ERROR,"Can't open path file!\n",path); j=0; while (!feof(fd)) { fgets(temp, sizeof(temp), fd); int L; L = strlen(temp); for (i = 0; i <= L; i++) if (temp[i] == '\n') { temp[i] = 0; } strcpy(temp_str[j], temp); args[j]=temp_str[j]; //printf("read test:%s\n",args[j]); j++; } strcpy(devlist.ieee, args[0]); devlist.has_ieee = true; strcpy(devlist.nwk_addr, args[1]); devlist.has_nwk_addr = true; strcpy(devlist.node_name, args[2]); devlist.has_node_name = true; devlist.online = atoi(args[3]); devlist.has_online = true; devlist.node_status = atoi(args[4]); devlist.has_node_status = true; devlist.eps.funcs.encode = &ss_eps_encode; devlist.eps.arg = args + 5; if (!pb_encode_tag_for_field(stream, field)) return false; //This encodes the data for the field, based on our FileInfo structure. if (!pb_encode_submessage(stream, Dev_fields, &devlist)) return false; fclose(fd); } return true; }
bool micropb_encode<Dimension, nullptr>(pb_ostream_t *stream, Dimension* arg) { return pb_encode_submessage(stream, onnx_TensorShapeProto_Dimension_fields, static_cast<void*>(arg)); }
static int pb_encode_proto(pb_ostream_t *stream, const pc_JSON *gprotos, const pc_JSON *protos, const pc_JSON *proto, pc_JSON *value) { pc_JSON *_messages; pc_JSON *_type = pc_JSON_GetObjectItem(proto, "type"); pc_JSON *sub_msg; const char *type = _type->valuestring; const char *str; int int_val; float float_val; double double_val; int length; _messages = pc_JSON_GetObjectItem(protos, "__messages"); switch (pb_get_type(type)) { case PB_uInt32: int_val = value->valueint; if (!pb_encode_varint(stream, int_val)) { return 0; } break; case PB_int32: case PB_sInt32: int_val = value->valueint; if (!pb_encode_svarint(stream, int_val)) { return 0; } break; case PB_float: float_val = (float)value->valuedouble; if (!pb_encode_fixed32(stream, &float_val)) { return 0; } break; case PB_double: double_val = value->valuedouble; if (!pb_encode_fixed64(stream, &double_val)) { return 0; } break; case PB_string: str = value->valuestring; length = strlen(str); if (!pb_encode_string(stream, (const uint8_t *)str, length)) { return 0; } break; default: if (_messages) { sub_msg = pc_JSON_GetObjectItem(_messages, type); if (!sub_msg) { /* check root msg in gprotos */ const char *head = "message "; size_t len = strlen(head) + strlen(type) + 1; char *head_text = (char *)malloc(len); memset(head_text, 0, len); strcpy(head_text, head); strcat(head_text, type); sub_msg = pc_JSON_GetObjectItem(gprotos, head_text); free(head_text); } if (sub_msg) { if (!pb_encode_submessage(stream, gprotos, sub_msg, value)) { return 0; } } else { return 0; } } } return 1; }
static int checkreturn pb_encode_proto(pb_ostream_t *stream, const json_t *gprotos, const json_t *protos, const json_t *proto, json_t *value) { json_t *_messages; json_t *_type = json_object_get(proto, "type"); json_t *sub_msg; const char *type = json_string_value(_type); const char *str; json_int_t int_val; float float_val; double double_val; int length; _messages = json_object_get(protos, "__messages"); switch (pb__get_type(type)) { case PB_uInt32: int_val = json_number_value(value); if (!pb_encode_varint(stream, int_val)) { return 0; } break; case PB_int32: case PB_sInt32: int_val = json_number_value(value); if (!pb_encode_svarint(stream, int_val)) { return 0; } break; case PB_float: float_val = json_number_value(value); if (!pb_encode_fixed32(stream, &float_val)) { return 0; } break; case PB_double: double_val = json_number_value(value); if (!pb_encode_fixed64(stream, &double_val)) { return 0; } break; case PB_string: str = json_string_value(value); length = strlen(str); if (!pb_encode_string(stream, (const uint8_t *)str, length)) { return 0; } break; default: if (_messages) { sub_msg = json_object_get(_messages, type); if (!sub_msg) { // check root msg in gprotos const char *head = "message "; char *head_text = (char *)pc_jsonp_malloc(strlen(head) + strlen(type) + 1); memset(head_text, 0, sizeof(head_text)); strcpy(head_text, head); strcat(head_text, type); sub_msg = json_object_get(gprotos, head_text); pc_jsonp_free(head_text); } if (sub_msg) { if (!pb_encode_submessage(stream, gprotos, sub_msg, value)) { return 0; } } else { return 0; } } } return 1; }