Handling errors returned from the library.
Handling errors returned from the library.
#include <stdio.h>
#include <stdlib.h>
void end_program(int return_code)
{
if (port_list != NULL)
if (config != NULL)
if (port != NULL)
exit(return_code);
}
{
int error_code;
char *error_message;
switch (result) {
printf("Error: Invalid argument.\n");
end_program(1);
printf("Error: Failed: OS error code: %d, message: '%s'\n",
error_code, error_message);
end_program(2);
printf("Error: Not supported.\n");
end_program(3);
end_program(4);
default:
printf("Operation succeeded.\n");
return result;
}
}
int main(int argc, char **argv)
{
printf("Getting list of ports.\n");
printf("Creating a new port configuration.\n");
printf("Trying to find a port that doesn't exist.\n");
end_program(0);
}
@ SP_ERR_SUPP
The requested operation is not supported by this system or device.
@ SP_ERR_FAIL
A system error occurred while executing the operation.
@ SP_ERR_MEM
A memory allocation failed while executing the operation.
@ SP_OK
Operation completed successfully.
@ SP_ERR_ARG
Invalid arguments were passed to the function.
void sp_free_port(struct sp_port *port)
Free a port structure obtained from sp_get_port_by_name() or sp_copy_port().
enum sp_return sp_get_port_by_name(const char *portname, struct sp_port **port_ptr)
Obtain a pointer to a new sp_port structure representing the named port.
enum sp_return sp_list_ports(struct sp_port ***list_ptr)
List the serial ports available on the system.
void sp_free_port_list(struct sp_port **ports)
Free a port list obtained from sp_list_ports().
void sp_free_config(struct sp_port_config *config)
Free a port configuration structure.
enum sp_return sp_new_config(struct sp_port_config **config_ptr)
Allocate a port configuration structure.
char * sp_last_error_message(void)
Get the error message for a failed operation.
int sp_last_error_code(void)
Get the error code for a failed operation.
void sp_free_error_message(char *message)
Free an error message returned by sp_last_error_message().
An opaque structure representing a serial port.
An opaque structure representing the configuration for a serial port.