static int read_vtl_pkt(calc_handle * handle, uint8_t cmd, prime_vtl_pkt ** pkt, int packet_contains_header) { int res; (void)packet_contains_header; *pkt = prime_vtl_pkt_new(0); if (*pkt != NULL) { (*pkt)->cmd = cmd; res = prime_recv_data(handle, *pkt); if (res == ERR_SUCCESS) { if ((*pkt)->size > 0) { if ((*pkt)->data[0] == (*pkt)->cmd) { hpcalcs_debug("%s: command matches returned data", __FUNCTION__); } else { hpcalcs_debug("%s: command does not match returned data", __FUNCTION__); // It's not necessarily an error. } } else { hpcalcs_info("%s: empty packet", __FUNCTION__); } } else { prime_vtl_pkt_del(*pkt); *pkt = NULL; } } else { res = ERR_MALLOC; hpcalcs_error("%s: couldn't create packet", __FUNCTION__); } return res; }
HPEXPORT cable_model HPCALL hpcables_get_model(cable_handle * handle) { cable_model model = CABLE_NUL; if (handle != NULL) { model = handle->model; } else { hpcalcs_error("%s: handle is NULL", __FUNCTION__); } return model; }
HPEXPORT int HPCALL calc_prime_r_get_infos (calc_handle * handle, calc_infos * infos) { int res; if (handle != NULL) { prime_vtl_pkt * pkt; res = read_vtl_pkt(handle, CMD_PRIME_GET_INFOS, &pkt, 1); if (res == ERR_SUCCESS && pkt != NULL) { if (infos != NULL) { infos->size = pkt->size; infos->data = pkt->data; // Transfer ownership of the memory block to the caller. pkt->data = NULL; // Detach it from virtual packet. } // else do nothing. res is already ERR_SUCCESS. prime_vtl_pkt_del(pkt); } else { hpcalcs_error("%s: failed to read packet", __FUNCTION__); } } else { res = ERR_INVALID_HANDLE; hpcalcs_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL calc_prime_s_get_infos (calc_handle * handle) { int res; if (handle != NULL) { prime_vtl_pkt * pkt = prime_vtl_pkt_new(1); if (pkt != NULL) { uint8_t * ptr; pkt->cmd = CMD_PRIME_GET_INFOS; ptr = pkt->data; *ptr++ = CMD_PRIME_GET_INFOS; res = write_vtl_pkt(handle, pkt); prime_vtl_pkt_del(pkt); } else { res = ERR_MALLOC; hpcalcs_error("%s: couldn't create packet", __FUNCTION__); } } else { res = ERR_INVALID_HANDLE; hpcalcs_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL calc_prime_r_check_ready(calc_handle * handle, uint8_t ** out_data, uint32_t * out_size) { int res; if (handle != NULL) { prime_vtl_pkt * pkt; res = read_vtl_pkt(handle, CMD_PRIME_CHECK_READY, &pkt, 0); if (res == ERR_SUCCESS && pkt != NULL) { if (out_data != NULL && out_size != NULL) { *out_size = pkt->size; *out_data = pkt->data; // Transfer ownership of the memory block to the caller. pkt->data = NULL; // Detach it from virtual packet. } // else do nothing. res is already ERR_SUCCESS. prime_vtl_pkt_del(pkt); } else { hpcalcs_error("%s: failed to read packet", __FUNCTION__); } } else { res = ERR_INVALID_HANDLE; hpcalcs_error("%s: handle is NULL", __FUNCTION__); } return res; }
HPEXPORT int HPCALL hpcalcs_error_get(int number, char **message) { int ret = number; //hpcalcs_debug("%s: entering", __FUNCTION__); if (message != NULL) { if (number >= ERR_CALC_FIRST && number <= ERR_CALC_LAST) { switch (number) { case ERR_CALC_NO_CABLE: *message = strdup(_("No cable attached")); break; case ERR_CALC_CABLE_NOT_OPEN: *message = strdup(_("Cable is not open")); break; case ERR_CALC_BUSY: *message = strdup(_("Calc is busy")); break; case ERR_CALC_INVALID_FNCTS: *message = strdup(_("Invalid cable functions")); break; case ERR_CALC_PACKET_FORMAT: *message = strdup(_("Unhandled packet format")); break; case ERR_CALC_SPLIT_TIMESTAMP: *message = strdup(_("Unable to get time constituents")); break; case ERR_CALC_PROBE_FAILED: *message = strdup(_("Calc probing failed")); break; default: *message = strdup(_("<Unknown error code>")); break; } ret = 0; } else { *message = NULL; } } else { hpcalcs_error("%s: message is NULL", __FUNCTION__); } //hpcalcs_debug("%s: exiting %d", __FUNCTION__, ret); return ret; }
HPEXPORT int HPCALL hplibs_error_get(int number, char **message) { int err = number; char *s = NULL; if (message != NULL) { // Skip ERR_SUCCESS. if (number > ERR_HPLIBS_GENERIC_FIRST && number <= ERR_HPLIBS_GENERIC_LAST) { switch (number) { case ERR_MALLOC: *message = strdup(_("Failed to allocate memory")); break; case ERR_INVALID_HANDLE: *message = strdup(_("Invalid handle pointer")); break; case ERR_INVALID_PARAMETER: *message = strdup(_("Invalid function parameter")); break; case ERR_INVALID_MODEL: *message = strdup(_("Invalid model")); break; case ERR_LIBRARY_INIT: *message = strdup(_("Problem initializing the library")); break; default: *message = strdup(_("<Unknown error code>")); break; } return 0; } else { // Retrieve the error message. err = hpfiles_error_get(err, &s); if (err) { free(s); err = hpcables_error_get(err, &s); if (err) { free(s); err = hpcalcs_error_get(err, &s); if (err) { free(s); err = hpopers_error_get(err, &s); if (err) { // next level: not a libhp* error. free(s); s = NULL; } else { hpopers_info("%s\n", s); } } else { hpcalcs_info("%s\n", s); } } else { hpcables_info("%s\n", s); } } else { hpfiles_info("%s\n", s); } *message = s; } } else { hpcalcs_error("%s: message is NULL", __FUNCTION__); return number; } return number; }