Minor whitespace- and consistency fixes.

This commit is contained in:
Uwe Hermann 2020-01-26 21:18:49 +01:00
parent 42b3cf3b98
commit 78c3db9bfb
6 changed files with 109 additions and 106 deletions

View File

@ -33,7 +33,6 @@ int main(int argc, char **argv)
/* Open and configure each port, and then add its RX event
* to the event set. */
for (int i = 0; i < num_ports; i++) {
printf("Looking for port %s.\n", port_names[i]);
check(sp_get_port_by_name(port_names[i], &ports[i]));
@ -79,23 +78,24 @@ int check(enum sp_return result)
{
/* For this example we'll just exit on any error by calling abort(). */
char *error_message;
switch (result) {
case SP_ERR_ARG:
printf("Error: Invalid argument.\n");
abort();
case SP_ERR_FAIL:
error_message = sp_last_error_message();
printf("Error: Failed: %s\n", error_message);
sp_free_error_message(error_message);
abort();
case SP_ERR_SUPP:
printf("Error: Not supported.\n");
abort();
case SP_ERR_MEM:
printf("Error: Couldn't allocate memory.\n");
abort();
case SP_OK:
default:
return result;
case SP_ERR_ARG:
printf("Error: Invalid argument.\n");
abort();
case SP_ERR_FAIL:
error_message = sp_last_error_message();
printf("Error: Failed: %s\n", error_message);
sp_free_error_message(error_message);
abort();
case SP_ERR_SUPP:
printf("Error: Not supported.\n");
abort();
case SP_ERR_MEM:
printf("Error: Couldn't allocate memory.\n");
abort();
case SP_OK:
default:
return result;
}
}

View File

@ -31,6 +31,7 @@ int check(enum sp_return result)
{
int error_code;
char *error_message;
switch (result) {
/* Handle each of the four negative error codes that can be returned.
@ -38,61 +39,61 @@ int check(enum sp_return result)
* In this example, we will end the program on any error, using
* a different return code for each possible class of error. */
case SP_ERR_ARG:
/* When SP_ERR_ARG is returned, there was a problem with one
* or more of the arguments passed to the function, e.g. a null
* pointer or an invalid value. This generally implies a bug in
* the calling code. */
printf("Error: Invalid argument.\n");
end_program(1);
case SP_ERR_ARG:
/* When SP_ERR_ARG is returned, there was a problem with one
* or more of the arguments passed to the function, e.g. a null
* pointer or an invalid value. This generally implies a bug in
* the calling code. */
printf("Error: Invalid argument.\n");
end_program(1);
case SP_ERR_FAIL:
/* When SP_ERR_FAIL is returned, there was an error from the OS,
* which we can obtain the error code and message for. These
* calls must be made in the same thread as the call that
* returned SP_ERR_FAIL, and before any other system functions
* are called in that thread, or they may not return the
* correct results. */
error_code = sp_last_error_code();
error_message = sp_last_error_message();
printf("Error: Failed: OS error code: %d, message: '%s'\n",
error_code, error_message);
/* The error message should be freed after use. */
sp_free_error_message(error_message);
end_program(2);
case SP_ERR_FAIL:
/* When SP_ERR_FAIL is returned, there was an error from the OS,
* which we can obtain the error code and message for. These
* calls must be made in the same thread as the call that
* returned SP_ERR_FAIL, and before any other system functions
* are called in that thread, or they may not return the
* correct results. */
error_code = sp_last_error_code();
error_message = sp_last_error_message();
printf("Error: Failed: OS error code: %d, message: '%s'\n",
error_code, error_message);
/* The error message should be freed after use. */
sp_free_error_message(error_message);
end_program(2);
case SP_ERR_SUPP:
/* When SP_ERR_SUPP is returned, the function was asked to do
* something that isn't supported by the current OS or device,
* or that libserialport doesn't know how to do in the current
* version. */
printf("Error: Not supported.\n");
end_program(3);
case SP_ERR_SUPP:
/* When SP_ERR_SUPP is returned, the function was asked to do
* something that isn't supported by the current OS or device,
* or that libserialport doesn't know how to do in the current
* version. */
printf("Error: Not supported.\n");
end_program(3);
case SP_ERR_MEM:
/* When SP_ERR_MEM is returned, libserialport wasn't able to
* allocate some memory it needed. Since the library doesn't
* normally use any large data structures, this probably means
* the system is critically low on memory and recovery will
* require very careful handling. The library itself will
* always try to handle any allocation failure safely.
*
* In this example, we'll just try to exit gracefully without
* calling printf, which might need to allocate further memory. */
end_program(4);
case SP_ERR_MEM:
/* When SP_ERR_MEM is returned, libserialport wasn't able to
* allocate some memory it needed. Since the library doesn't
* normally use any large data structures, this probably means
* the system is critically low on memory and recovery will
* require very careful handling. The library itself will
* always try to handle any allocation failure safely.
*
* In this example, we'll just try to exit gracefully without
* calling printf, which might need to allocate further memory. */
end_program(4);
case SP_OK:
default:
/* A return value of SP_OK, defined as zero, means that the
* operation succeeded. */
printf("Operation succeeded.\n");
case SP_OK:
default:
/* A return value of SP_OK, defined as zero, means that the
* operation succeeded. */
printf("Operation succeeded.\n");
/* Some fuctions can also return a value greater than zero to
* indicate a numeric result, such as the number of bytes read by
* sp_blocking_read(). So when writing an error handling wrapper
* function like this one, it's helpful to return the result so
* that it can be used. */
return result;
/* Some fuctions can also return a value greater than zero to
* indicate a numeric result, such as the number of bytes read by
* sp_blocking_read(). So when writing an error handling wrapper
* function like this one, it's helpful to return the result so
* that it can be used. */
return result;
}
}

View File

@ -17,8 +17,7 @@ int main(int argc, char **argv)
* pointer will be updated to refer to the array created. */
enum sp_return result = sp_list_ports(&port_list);
if (result != SP_OK)
{
if (result != SP_OK) {
printf("sp_list_ports() failed!\n");
return -1;
}
@ -26,8 +25,7 @@ int main(int argc, char **argv)
/* Iterate through the ports. When port_list[i] is NULL
* this indicates the end of the list. */
int i;
for (i = 0; port_list[i] != NULL; i++)
{
for (i = 0; port_list[i] != NULL; i++) {
struct sp_port *port = port_list[i];
/* Get the name of the port. */

View File

@ -130,24 +130,25 @@ int check(enum sp_return result)
{
/* For this example we'll just exit on any error by calling abort(). */
char *error_message;
switch (result) {
case SP_ERR_ARG:
printf("Error: Invalid argument.\n");
abort();
case SP_ERR_FAIL:
error_message = sp_last_error_message();
printf("Error: Failed: %s\n", error_message);
sp_free_error_message(error_message);
abort();
case SP_ERR_SUPP:
printf("Error: Not supported.\n");
abort();
case SP_ERR_MEM:
printf("Error: Couldn't allocate memory.\n");
abort();
case SP_OK:
default:
return result;
case SP_ERR_ARG:
printf("Error: Invalid argument.\n");
abort();
case SP_ERR_FAIL:
error_message = sp_last_error_message();
printf("Error: Failed: %s\n", error_message);
sp_free_error_message(error_message);
abort();
case SP_ERR_SUPP:
printf("Error: Not supported.\n");
abort();
case SP_ERR_MEM:
printf("Error: Couldn't allocate memory.\n");
abort();
case SP_OK:
default:
return result;
}
}
@ -155,12 +156,19 @@ int check(enum sp_return result)
const char *parity_name(enum sp_parity parity)
{
switch (parity) {
case SP_PARITY_INVALID: return "(Invalid)";
case SP_PARITY_NONE: return "None";
case SP_PARITY_ODD: return "Odd";
case SP_PARITY_EVEN: return "Even";
case SP_PARITY_MARK: return "Mark";
case SP_PARITY_SPACE: return "Space";
default: return NULL;
case SP_PARITY_INVALID:
return "(Invalid)";
case SP_PARITY_NONE:
return "None";
case SP_PARITY_ODD:
return "Odd";
case SP_PARITY_EVEN:
return "Even";
case SP_PARITY_MARK:
return "Mark";
case SP_PARITY_SPACE:
return "Space";
default:
return NULL;
}
}

View File

@ -24,8 +24,7 @@ int main(int argc, char **argv)
* pointer will be updated to refer to the port found. */
enum sp_return result = sp_get_port_by_name(port_name, &port);
if (result != SP_OK)
{
if (result != SP_OK) {
printf("sp_get_port_by_name() failed!\n");
return -1;
}
@ -38,14 +37,11 @@ int main(int argc, char **argv)
* e.g. native port, USB or Bluetooth. */
enum sp_transport transport = sp_get_port_transport(port);
if (transport == SP_TRANSPORT_NATIVE)
{
if (transport == SP_TRANSPORT_NATIVE) {
/* This is a "native" port, usually directly connected
* to the system rather than some external interface. */
printf("Type: Native\n");
}
else if (transport == SP_TRANSPORT_USB)
{
} else if (transport == SP_TRANSPORT_USB) {
/* This is a USB to serial converter of some kind. */
printf("Type: USB\n");
@ -63,9 +59,7 @@ int main(int argc, char **argv)
int usb_bus, usb_address;
sp_get_port_usb_bus_address(port, &usb_bus, &usb_address);
printf("Bus: %d Address: %d\n", usb_bus, usb_address);
}
else if (transport == SP_TRANSPORT_BLUETOOTH)
{
} else if (transport == SP_TRANSPORT_BLUETOOTH) {
/* This is a Bluetooth serial port. */
printf("Type: Bluetooth\n");

View File

@ -11,6 +11,7 @@ int main(int argc, char *argv[])
struct time a, b, c;
struct timeval tv;
struct timeout to;
printf("Testing arithmetic\n");
time_set_ms(&a, 10050);
time_set_ms(&b, 100);
@ -63,5 +64,6 @@ int main(int argc, char *argv[])
timeout_update(&to);
assert(timeout_check(&to));
printf("Timeout expired\n");
return 0;
}