void adm_add_datadef(char *name, uint8_t *mid_str, int num_parms, adm_string_fn to_string, adm_size_fn get_size) { uint32_t size = 0; uint32_t used = 0; adm_datadef_t *new_entry = NULL; DTNMP_DEBUG_ENTRY("adm_add_datadef","(%llx, %llx, %d, %llx, %llx)", name, mid_str, num_parms, to_string, get_size); /* Step 0 - Sanity Checks. */ if((name == NULL) || (mid_str == NULL)) { DTNMP_DEBUG_ERR("adm_add_datadef","Bad Args.", NULL); DTNMP_DEBUG_EXIT("adm_add_datadef","->.", NULL); return; } if(gAdmData == NULL) { DTNMP_DEBUG_ERR("adm_add_datadef","Global data list not initialized.", NULL); DTNMP_DEBUG_EXIT("adm_add_datadef","->.", NULL); return; } /* Step 1 - Check name length. */ if(strlen(name) > ADM_MAX_NAME) { DTNMP_DEBUG_WARN("adm_add_datadef","Trunc. %s to %d bytes.", name, ADM_MAX_NAME) } /* Step 2 - Allocate a Data Definition. */ if((new_entry = (adm_datadef_t *) MTAKE(sizeof(adm_datadef_t))) == NULL) { DTNMP_DEBUG_ERR("adm_add_datadef","Can't allocate new entry of size %d.", sizeof(adm_datadef_t)); DTNMP_DEBUG_EXIT("adm_add_datadef","->.", NULL); return; } /* Step 3 - Copy the ADM information. */ strncpy((char *)new_entry->name, name, ADM_MAX_NAME); new_entry->mid = mid_deserialize(mid_str, ADM_MID_ALLOC, &used); new_entry->num_parms = num_parms; new_entry->collect = NULL; new_entry->to_string = (to_string == NULL) ? adm_print_uvast : to_string; new_entry->get_size = (get_size == NULL) ? adm_size_uvast : get_size; /* Step 4 - Add the new entry. */ lyst_insert_last(gAdmData, new_entry); DTNMP_DEBUG_EXIT("adm_add_datadef","->.", NULL); return; }
void adm_add_ctrl(char *name, uint8_t *mid_str, int num_parms) { uint8_t *tmp = NULL; uint32_t size = 0; uint32_t used = 0; adm_ctrl_t *new_entry = NULL; DTNMP_DEBUG_ENTRY("adm_add_ctrl","(%#llx, %#llx, %d)", name, mid_str, num_parms); /* Step 0 - Sanity Checks. */ if((name == NULL) || (mid_str == NULL)) { DTNMP_DEBUG_ERR("adm_add_ctrl","Bad Args.", NULL); DTNMP_DEBUG_EXIT("adm_add_ctrl","->.", NULL); return; } if(gAdmCtrls == NULL) { DTNMP_DEBUG_ERR("adm_add_ctrl","Global Controls list not initialized.", NULL); DTNMP_DEBUG_EXIT("adm_add_ctrl","->.", NULL); return; } /* Step 1 - Check name length. */ if(strlen(name) > ADM_MAX_NAME) { DTNMP_DEBUG_WARN("adm_add_ctrl","Trunc. %s to %d bytes.", name, ADM_MAX_NAME) } /* Step 2 - Allocate a Data Definition. */ if((new_entry = (adm_ctrl_t *) MTAKE(sizeof(adm_ctrl_t))) == NULL) { DTNMP_DEBUG_ERR("adm_add_ctrl","Can't allocate new entry of size %d.", sizeof(adm_datadef_t)); DTNMP_DEBUG_EXIT("adm_add_ctrl","->.", NULL); return; } /* Step 3 - Copy the ADM information. */ strncpy((char *)new_entry->name, name, ADM_MAX_NAME); new_entry->mid = mid_deserialize(mid_str, ADM_MID_ALLOC, &used); //new_entry->mid_len = size; new_entry->num_parms = num_parms; new_entry->run = NULL; /* Step 4 - Add the new entry. */ lyst_insert_last(gAdmCtrls, new_entry); DTNMP_DEBUG_EXIT("adm_add_ctrl","->.", NULL); return; }
/****************************************************************************** * * \par Function Name: adm_add_datadef_collect * * \par Registers a collection function to a data definition. * * \param[in] mid_str serialized MID value * \param[in] collect The data collection function. * * \par Notes: * 1. When working with parameterized OIDs, the given MID should * be all information excluding the parameterized portion of the OID. * 2. ADM names will be truncated after ADM_MAX_NAME bytes. * * Modification History: * MM/DD/YY AUTHOR DESCRIPTION * -------- ------------ --------------------------------------------- * 11/25/12 E. Birrane Initial implementation. * 07/27/13 E. BIrrane Updated ADM to use Lysts. *****************************************************************************/ void adm_add_datadef_collect(uint8_t *mid_str, adm_data_collect_fn collect) { uint32_t used = 0; mid_t *mid = NULL; adm_datadef_t *entry = NULL; DTNMP_DEBUG_ENTRY("adm_add_datadef_collect","(%lld, %lld)", mid_str, collect); if((mid_str == NULL) || (collect == NULL)) { DTNMP_DEBUG_ERR("adm_add_datadef_collect","Bad Args.", NULL); DTNMP_DEBUG_EXIT("adm_add_datadef_collect","->.", NULL); return; } if((mid = mid_deserialize(mid_str, ADM_MID_ALLOC, &used)) == NULL) { char *tmp = utils_hex_to_string(mid_str, ADM_MID_ALLOC); DTNMP_DEBUG_ERR("adm_add_datadef_collect","Can't deserialize MID str %s.",tmp); MRELEASE(tmp); DTNMP_DEBUG_EXIT("adm_add_datadef_collect","->.", NULL); return; } if((entry = adm_find_datadef(mid)) == NULL) { char *tmp = mid_to_string(mid); DTNMP_DEBUG_ERR("adm_add_datadef_collect","Can't find data for MID %s.", tmp); MRELEASE(tmp); } else { entry->collect = collect; } mid_release(mid); DTNMP_DEBUG_EXIT("adm_add_datadef_collect","->.", NULL); }
void adm_add_ctrl_run(uint8_t *mid_str, adm_ctrl_fn run) { uint32_t used = 0; mid_t *mid = NULL; adm_ctrl_t *entry = NULL; DTNMP_DEBUG_ENTRY("adm_add_ctrl_run","(%lld, %lld)", mid_str, run); if((mid_str == NULL) || (run == NULL)) { DTNMP_DEBUG_ERR("adm_add_ctrl_run","Bad Args.", NULL); DTNMP_DEBUG_EXIT("adm_add_ctrl_run","->.",NULL); return; } if((mid = mid_deserialize(mid_str, ADM_MID_ALLOC, &used)) == NULL) { char *tmp = utils_hex_to_string(mid_str, ADM_MID_ALLOC); DTNMP_DEBUG_ERR("adm_add_ctrl_run","Can't deserialized MID %s", tmp); MRELEASE(tmp); DTNMP_DEBUG_EXIT("adm_add_ctrl_run","->.",NULL); return; } if((entry = adm_find_ctrl(mid)) == NULL) { char *tmp = mid_to_string(mid); DTNMP_DEBUG_ERR("adm_add_ctrl_run","Can't find control for MID %s", tmp); MRELEASE(tmp); } else { entry->run = run; } mid_release(mid); DTNMP_DEBUG_EXIT("adm_add_ctrl_run","->.",NULL); }
/****************************************************************************** * * \par Function Name: midcol_deserialize * * \par Purpose: Deserialize a MID collection into a Lyst. * * \retval NULL - Failure * !NULL - The created Lyst. * * \param[in] buffer The buffer holding the MC. * \param[in] buffer_size The size of the buffer, in bytes. * \param[out] bytes_used The # bytes consumed in the deserialization. * * \par Notes: * 1. The created Lyst must be freed when done. * 2. Reminder: A Lyst is a pointer to a LystStruct. * * Modification History: * MM/DD/YY AUTHOR DESCRIPTION * -------- ------------ --------------------------------------------- * 11/14/12 E. Birrane Initial implementation, * 06/17/13 E. Birrane Updated to ION 3.1.3, moved to uvast *****************************************************************************/ Lyst midcol_deserialize(unsigned char *buffer, uint32_t buffer_size, uint32_t *bytes_used) { unsigned char *cursor = NULL; Lyst result = NULL; uint32_t bytes = 0; uvast num = 0; mid_t *cur_mid = NULL; uint32_t i = 0; DTNMP_DEBUG_ENTRY("midcol_deserialize","(%#llx,%d,%#llx)", (unsigned long) buffer, buffer_size, (unsigned long) bytes_used); /* Step 0: Sanity Check. */ if((buffer == NULL) || (buffer_size == 0) || (bytes_used == NULL)) { DTNMP_DEBUG_ERR("mid_deserialize_mc","Bad Args", NULL); DTNMP_DEBUG_EXIT("mid_deserialize_mc","->NULL",NULL); return NULL; } *bytes_used = 0; cursor = buffer; /* Step 1: Create the Lyst. */ if((result = lyst_create()) == NULL) { DTNMP_DEBUG_ERR("midcol_deserialize","Can't create lyst.", NULL); DTNMP_DEBUG_EXIT("midcol_deserialize","->NULL",NULL); return NULL; } /* Step 2: Grab # MIDs in the collection. */ if((bytes = utils_grab_sdnv(cursor, buffer_size, &num)) == 0) { DTNMP_DEBUG_ERR("midcol_deserialize","Can't parse SDNV.", NULL); lyst_destroy(result); DTNMP_DEBUG_EXIT("midcol_deserialize","->NULL",NULL); return NULL; } else { cursor += bytes; buffer_size -= bytes; *bytes_used += bytes; } /* Step 3: Grab Mids. */ for(i = 0; i < num; i++) { /* Deserialize ith MID. */ if((cur_mid = mid_deserialize(cursor, buffer_size, &bytes)) == NULL) { DTNMP_DEBUG_ERR("mid_deserialize_mc","Can't grab MID #%d.", i); midcol_destroy(&result); DTNMP_DEBUG_EXIT("mid_deserialize_mc","->NULL",NULL); return NULL; } else { cursor += bytes; buffer_size -= bytes; *bytes_used += bytes; } /* Drop it in the lyst in order. */ lyst_insert_last(result, cur_mid); } DTNMP_DEBUG_EXIT("midcol_deserialize","->%#llx",(unsigned long)result); return result; }
adm_stat_msg_t *msg_deserialize_stat_msg(uint8_t *cursor, uint32_t size, uint32_t *bytes_used) { adm_stat_msg_t *result = NULL; uint32_t bytes = 0; DTNMP_DEBUG_ENTRY("msg_deserialize_stat_msg","(0x%x, %d, 0x%x)", (unsigned long)cursor, size, (unsigned long) bytes_used); /* Step 0: Sanity Checks. */ if((cursor == NULL) || (bytes_used == 0)) { DTNMP_DEBUG_ERR("msg_deserialize_stat_msg","Bad Args.",NULL); DTNMP_DEBUG_EXIT("msg_deserialize_stat_msg","->NULL",NULL); return NULL; } /* Step 1: Allocate the new message structure. */ if((result = (adm_stat_msg_t*)MTAKE(sizeof(adm_stat_msg_t))) == NULL) { DTNMP_DEBUG_ERR("msg_deserialize_stat_msg","Can't Alloc %d Bytes.", sizeof(adm_stat_msg_t)); *bytes_used = 0; DTNMP_DEBUG_EXIT("msg_deserialize_stat_msg","->NULL",NULL); return NULL; } else { memset(result,0,sizeof(adm_stat_msg_t)); } /* Step 2: Deserialize the message. */ /* Grab the mask */ if((result->code = mid_deserialize(cursor,size,&bytes)) == NULL) { DTNMP_DEBUG_ERR("msg_deserialize_stat_msg","Can't get code MID.",NULL); *bytes_used = 0; msg_release_stat_msg(result); DTNMP_DEBUG_EXIT("msg_deserialize_stat_msg","->NULL",NULL); return NULL; } else { cursor += bytes; size -= bytes; *bytes_used += bytes; } /* Grab the timestamp */ uvast val = 0; if((bytes = utils_grab_sdnv(cursor, size, &val)) == 0) { DTNMP_DEBUG_ERR("msg_deserialize_stat_msg","Can't get timestamp.",NULL); *bytes_used = 0; msg_release_stat_msg(result); DTNMP_DEBUG_EXIT("msg_deserialize_stat_msg","->NULL",NULL); return NULL; } else { cursor += bytes; size -= bytes; *bytes_used += bytes; result->time = val; } /* Grab the Lyst. */ if((result->generators = midcol_deserialize(cursor,size,&bytes)) == NULL) { DTNMP_DEBUG_ERR("msg_deserialize_stat_msg","Can't get generators.",NULL); *bytes_used = 0; msg_release_stat_msg(result); DTNMP_DEBUG_EXIT("msg_deserialize_stat_msg","->NULL",NULL); return NULL; } DTNMP_DEBUG_EXIT("msg_deserialize_stat_msg","->0x%x", (unsigned long)result); return result; }
/****************************************************************************** * * \par Function Name: ctrl_deserialize * * \par Construct a control instance from an incoming message byte stream. * * \retval NULL - Error in deserializing the control. * !NULL - The control instance * * \param[in] cursor Pointer to the start of the serialized control. * \param[in] size The size of the remaining serialized data * \param[out] bytes_used The number of bytes consumed in processing this ctrl. * * Modification History: * MM/DD/YY AUTHOR DESCRIPTION * -------- ------------ --------------------------------------------- * 01/10/13 E. Birrane Initial implementation. (JHU/APL) * 05/17/15 E. Birrane Moved to ctrl.c, updated to DTNMP V0.1 (Secure DTN - NASA: NNX14CS58P) *****************************************************************************/ ctrl_exec_t *ctrl_deserialize(uint8_t *cursor, uint32_t size, uint32_t *bytes_used) { ctrl_exec_t *result = NULL; uint32_t bytes = 0; uvast val = 0; AMP_DEBUG_ENTRY("ctrl_deserialize","(0x%x, %d, 0x%x)", (unsigned long)cursor, size, (unsigned long) bytes_used); /* Step 0: Sanity Checks. */ if((cursor == NULL) || (bytes_used == 0)) { AMP_DEBUG_ERR("ctrl_deserialize","Bad Args.",NULL); AMP_DEBUG_EXIT("ctrl_deserialize","->NULL",NULL); return NULL; } /* Step 1: Allocate the new message structure. */ if((result = (ctrl_exec_t*)STAKE(sizeof(ctrl_exec_t))) == NULL) { AMP_DEBUG_ERR("ctrl_deserialize","Can't Alloc %d Bytes.", sizeof(ctrl_exec_t)); *bytes_used = 0; AMP_DEBUG_EXIT("ctrl_deserialize","->NULL",NULL); return NULL; } else { memset(result,0,sizeof(ctrl_exec_t)); } bytes = decodeSdnv(&val, cursor); result->time = val; cursor += bytes; size -= bytes; *bytes_used += bytes; bytes = decodeSdnv(&val,cursor); result->status = val; cursor += bytes; size -= bytes; *bytes_used += bytes; memcpy(&(result->sender), cursor, sizeof(eid_t)); cursor += sizeof(eid_t); size -= sizeof(eid_t); *bytes_used += sizeof(eid_t); if((result->mid = mid_deserialize(cursor, size, &bytes)) == NULL) { AMP_DEBUG_ERR("ctrl_deserialize","Can't grab contents.",NULL); *bytes_used = 0; SRELEASE(result); AMP_DEBUG_EXIT("ctrl_deserialize","->NULL",NULL); return NULL; } else { cursor += bytes; size -= bytes; *bytes_used += bytes; } AMP_DEBUG_EXIT("ctrl_deserialize","->0x%x", (unsigned long)result); return result; }
void ui_print_val(uint8_t type, uint8_t *data, uint32_t length) { uint32_t bytes = 0; if(data == NULL) { printf("NULL"); return; } switch(type) { case AMP_TYPE_VAR: { var_t *cd = var_deserialize(data, length, &bytes); char *str = var_to_string(cd); printf("%s", str); SRELEASE(str); var_release(cd); } break; case AMP_TYPE_INT: printf("%d", utils_deserialize_int(data, length, &bytes)); break; case AMP_TYPE_TS: case AMP_TYPE_UINT: printf("%d", utils_deserialize_uint(data, length, &bytes)); break; case AMP_TYPE_VAST: printf(VAST_FIELDSPEC, utils_deserialize_vast(data, length, &bytes)); break; case AMP_TYPE_SDNV: case AMP_TYPE_UVAST: printf(UVAST_FIELDSPEC, utils_deserialize_uvast(data, length, &bytes)); break; case AMP_TYPE_REAL32: printf("%f", utils_deserialize_real32(data, length, &bytes)); break; case AMP_TYPE_REAL64: printf("%f", utils_deserialize_real64(data, length, &bytes)); break; case AMP_TYPE_STRING: { char* tmp = NULL; tmp = utils_deserialize_string(data, length, &bytes); printf("%s", tmp); SRELEASE(tmp); } break; case AMP_TYPE_BLOB: { blob_t *blob = blob_deserialize(data, length, &bytes); char *str = blob_to_str(blob); printf("%s", str); SRELEASE(str); SRELEASE(blob); } break; case AMP_TYPE_DC: { uint32_t bytes = 0; Lyst dc = dc_deserialize(data, length, &bytes); ui_print_dc(dc); dc_destroy(&dc); } break; case AMP_TYPE_MID: { uint32_t bytes = 0; mid_t *mid = mid_deserialize(data, length, &bytes); ui_print_mid(mid); mid_release(mid); } break; case AMP_TYPE_MC: { uint32_t bytes = 0; Lyst mc = midcol_deserialize(data, length, &bytes); ui_print_mc(mc); midcol_destroy(&mc); } break; // \todo: Expression has no priority. Need to re-think priority. case AMP_TYPE_EXPR: { uint32_t bytes = 0; expr_t *expr = expr_deserialize(data, length, &bytes); ui_print_expr(expr); expr_release(expr); } break; /* case DTNMP_TYPE_DEF: { uint32_t bytes = 0; def_gen_t *def = def_deserialize_gen(data, length, &bytes); ui_print_def(def); def_release_gen(def); } break; */ case AMP_TYPE_TRL: { uint32_t bytes = 0; trl_t *trl = trl_deserialize(data, length, &bytes); ui_print_trl(trl); trl_release(trl); } break; case AMP_TYPE_TABLE: { uint32_t bytes = 0; table_t *table = table_deserialize(data, length, &bytes); ui_print_table(table); table_destroy(table, 1); } break; case AMP_TYPE_SRL: { uint32_t bytes = 0; srl_t *srl = srl_deserialize(data, length, &bytes); ui_print_srl(srl); srl_release(srl); } break; default: printf("Unknown."); } }