Skip to content

Commit 78c3db9

Browse files
committed
Minor whitespace- and consistency fixes.
1 parent 42b3cf3 commit 78c3db9

File tree

6 files changed

+114
-111
lines changed

6 files changed

+114
-111
lines changed

examples/await_events.c

+18-18
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ int main(int argc, char **argv)
3333
/* Open and configure each port, and then add its RX event
3434
* to the event set. */
3535
for (int i = 0; i < num_ports; i++) {
36-
3736
printf("Looking for port %s.\n", port_names[i]);
3837
check(sp_get_port_by_name(port_names[i], &ports[i]));
3938

@@ -79,23 +78,24 @@ int check(enum sp_return result)
7978
{
8079
/* For this example we'll just exit on any error by calling abort(). */
8180
char *error_message;
81+
8282
switch (result) {
83-
case SP_ERR_ARG:
84-
printf("Error: Invalid argument.\n");
85-
abort();
86-
case SP_ERR_FAIL:
87-
error_message = sp_last_error_message();
88-
printf("Error: Failed: %s\n", error_message);
89-
sp_free_error_message(error_message);
90-
abort();
91-
case SP_ERR_SUPP:
92-
printf("Error: Not supported.\n");
93-
abort();
94-
case SP_ERR_MEM:
95-
printf("Error: Couldn't allocate memory.\n");
96-
abort();
97-
case SP_OK:
98-
default:
99-
return result;
83+
case SP_ERR_ARG:
84+
printf("Error: Invalid argument.\n");
85+
abort();
86+
case SP_ERR_FAIL:
87+
error_message = sp_last_error_message();
88+
printf("Error: Failed: %s\n", error_message);
89+
sp_free_error_message(error_message);
90+
abort();
91+
case SP_ERR_SUPP:
92+
printf("Error: Not supported.\n");
93+
abort();
94+
case SP_ERR_MEM:
95+
printf("Error: Couldn't allocate memory.\n");
96+
abort();
97+
case SP_OK:
98+
default:
99+
return result;
100100
}
101101
}

examples/handle_errors.c

+56-55
Original file line numberDiff line numberDiff line change
@@ -31,68 +31,69 @@ int check(enum sp_return result)
3131
{
3232
int error_code;
3333
char *error_message;
34+
3435
switch (result) {
3536

3637
/* Handle each of the four negative error codes that can be returned.
3738
*
3839
* In this example, we will end the program on any error, using
3940
* a different return code for each possible class of error. */
4041

41-
case SP_ERR_ARG:
42-
/* When SP_ERR_ARG is returned, there was a problem with one
43-
* or more of the arguments passed to the function, e.g. a null
44-
* pointer or an invalid value. This generally implies a bug in
45-
* the calling code. */
46-
printf("Error: Invalid argument.\n");
47-
end_program(1);
48-
49-
case SP_ERR_FAIL:
50-
/* When SP_ERR_FAIL is returned, there was an error from the OS,
51-
* which we can obtain the error code and message for. These
52-
* calls must be made in the same thread as the call that
53-
* returned SP_ERR_FAIL, and before any other system functions
54-
* are called in that thread, or they may not return the
55-
* correct results. */
56-
error_code = sp_last_error_code();
57-
error_message = sp_last_error_message();
58-
printf("Error: Failed: OS error code: %d, message: '%s'\n",
59-
error_code, error_message);
60-
/* The error message should be freed after use. */
61-
sp_free_error_message(error_message);
62-
end_program(2);
63-
64-
case SP_ERR_SUPP:
65-
/* When SP_ERR_SUPP is returned, the function was asked to do
66-
* something that isn't supported by the current OS or device,
67-
* or that libserialport doesn't know how to do in the current
68-
* version. */
69-
printf("Error: Not supported.\n");
70-
end_program(3);
71-
72-
case SP_ERR_MEM:
73-
/* When SP_ERR_MEM is returned, libserialport wasn't able to
74-
* allocate some memory it needed. Since the library doesn't
75-
* normally use any large data structures, this probably means
76-
* the system is critically low on memory and recovery will
77-
* require very careful handling. The library itself will
78-
* always try to handle any allocation failure safely.
79-
*
80-
* In this example, we'll just try to exit gracefully without
81-
* calling printf, which might need to allocate further memory. */
82-
end_program(4);
83-
84-
case SP_OK:
85-
default:
86-
/* A return value of SP_OK, defined as zero, means that the
87-
* operation succeeded. */
88-
printf("Operation succeeded.\n");
89-
90-
/* Some fuctions can also return a value greater than zero to
91-
* indicate a numeric result, such as the number of bytes read by
92-
* sp_blocking_read(). So when writing an error handling wrapper
93-
* function like this one, it's helpful to return the result so
94-
* that it can be used. */
95-
return result;
42+
case SP_ERR_ARG:
43+
/* When SP_ERR_ARG is returned, there was a problem with one
44+
* or more of the arguments passed to the function, e.g. a null
45+
* pointer or an invalid value. This generally implies a bug in
46+
* the calling code. */
47+
printf("Error: Invalid argument.\n");
48+
end_program(1);
49+
50+
case SP_ERR_FAIL:
51+
/* When SP_ERR_FAIL is returned, there was an error from the OS,
52+
* which we can obtain the error code and message for. These
53+
* calls must be made in the same thread as the call that
54+
* returned SP_ERR_FAIL, and before any other system functions
55+
* are called in that thread, or they may not return the
56+
* correct results. */
57+
error_code = sp_last_error_code();
58+
error_message = sp_last_error_message();
59+
printf("Error: Failed: OS error code: %d, message: '%s'\n",
60+
error_code, error_message);
61+
/* The error message should be freed after use. */
62+
sp_free_error_message(error_message);
63+
end_program(2);
64+
65+
case SP_ERR_SUPP:
66+
/* When SP_ERR_SUPP is returned, the function was asked to do
67+
* something that isn't supported by the current OS or device,
68+
* or that libserialport doesn't know how to do in the current
69+
* version. */
70+
printf("Error: Not supported.\n");
71+
end_program(3);
72+
73+
case SP_ERR_MEM:
74+
/* When SP_ERR_MEM is returned, libserialport wasn't able to
75+
* allocate some memory it needed. Since the library doesn't
76+
* normally use any large data structures, this probably means
77+
* the system is critically low on memory and recovery will
78+
* require very careful handling. The library itself will
79+
* always try to handle any allocation failure safely.
80+
*
81+
* In this example, we'll just try to exit gracefully without
82+
* calling printf, which might need to allocate further memory. */
83+
end_program(4);
84+
85+
case SP_OK:
86+
default:
87+
/* A return value of SP_OK, defined as zero, means that the
88+
* operation succeeded. */
89+
printf("Operation succeeded.\n");
90+
91+
/* Some fuctions can also return a value greater than zero to
92+
* indicate a numeric result, such as the number of bytes read by
93+
* sp_blocking_read(). So when writing an error handling wrapper
94+
* function like this one, it's helpful to return the result so
95+
* that it can be used. */
96+
return result;
9697
}
9798
}
9899

examples/list_ports.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,15 @@ int main(int argc, char **argv)
1717
* pointer will be updated to refer to the array created. */
1818
enum sp_return result = sp_list_ports(&port_list);
1919

20-
if (result != SP_OK)
21-
{
20+
if (result != SP_OK) {
2221
printf("sp_list_ports() failed!\n");
2322
return -1;
2423
}
2524

2625
/* Iterate through the ports. When port_list[i] is NULL
2726
* this indicates the end of the list. */
2827
int i;
29-
for (i = 0; port_list[i] != NULL; i++)
30-
{
28+
for (i = 0; port_list[i] != NULL; i++) {
3129
struct sp_port *port = port_list[i];
3230

3331
/* Get the name of the port. */

examples/port_config.c

+32-24
Original file line numberDiff line numberDiff line change
@@ -130,37 +130,45 @@ int check(enum sp_return result)
130130
{
131131
/* For this example we'll just exit on any error by calling abort(). */
132132
char *error_message;
133+
133134
switch (result) {
134-
case SP_ERR_ARG:
135-
printf("Error: Invalid argument.\n");
136-
abort();
137-
case SP_ERR_FAIL:
138-
error_message = sp_last_error_message();
139-
printf("Error: Failed: %s\n", error_message);
140-
sp_free_error_message(error_message);
141-
abort();
142-
case SP_ERR_SUPP:
143-
printf("Error: Not supported.\n");
144-
abort();
145-
case SP_ERR_MEM:
146-
printf("Error: Couldn't allocate memory.\n");
147-
abort();
148-
case SP_OK:
149-
default:
150-
return result;
135+
case SP_ERR_ARG:
136+
printf("Error: Invalid argument.\n");
137+
abort();
138+
case SP_ERR_FAIL:
139+
error_message = sp_last_error_message();
140+
printf("Error: Failed: %s\n", error_message);
141+
sp_free_error_message(error_message);
142+
abort();
143+
case SP_ERR_SUPP:
144+
printf("Error: Not supported.\n");
145+
abort();
146+
case SP_ERR_MEM:
147+
printf("Error: Couldn't allocate memory.\n");
148+
abort();
149+
case SP_OK:
150+
default:
151+
return result;
151152
}
152153
}
153154

154155
/* Helper function to give a name for each parity mode. */
155156
const char *parity_name(enum sp_parity parity)
156157
{
157158
switch (parity) {
158-
case SP_PARITY_INVALID: return "(Invalid)";
159-
case SP_PARITY_NONE: return "None";
160-
case SP_PARITY_ODD: return "Odd";
161-
case SP_PARITY_EVEN: return "Even";
162-
case SP_PARITY_MARK: return "Mark";
163-
case SP_PARITY_SPACE: return "Space";
164-
default: return NULL;
159+
case SP_PARITY_INVALID:
160+
return "(Invalid)";
161+
case SP_PARITY_NONE:
162+
return "None";
163+
case SP_PARITY_ODD:
164+
return "Odd";
165+
case SP_PARITY_EVEN:
166+
return "Even";
167+
case SP_PARITY_MARK:
168+
return "Mark";
169+
case SP_PARITY_SPACE:
170+
return "Space";
171+
default:
172+
return NULL;
165173
}
166174
}

examples/port_info.c

+4-10
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@ int main(int argc, char **argv)
2424
* pointer will be updated to refer to the port found. */
2525
enum sp_return result = sp_get_port_by_name(port_name, &port);
2626

27-
if (result != SP_OK)
28-
{
27+
if (result != SP_OK) {
2928
printf("sp_get_port_by_name() failed!\n");
3029
return -1;
3130
}
@@ -38,14 +37,11 @@ int main(int argc, char **argv)
3837
* e.g. native port, USB or Bluetooth. */
3938
enum sp_transport transport = sp_get_port_transport(port);
4039

41-
if (transport == SP_TRANSPORT_NATIVE)
42-
{
40+
if (transport == SP_TRANSPORT_NATIVE) {
4341
/* This is a "native" port, usually directly connected
4442
* to the system rather than some external interface. */
4543
printf("Type: Native\n");
46-
}
47-
else if (transport == SP_TRANSPORT_USB)
48-
{
44+
} else if (transport == SP_TRANSPORT_USB) {
4945
/* This is a USB to serial converter of some kind. */
5046
printf("Type: USB\n");
5147

@@ -63,9 +59,7 @@ int main(int argc, char **argv)
6359
int usb_bus, usb_address;
6460
sp_get_port_usb_bus_address(port, &usb_bus, &usb_address);
6561
printf("Bus: %d Address: %d\n", usb_bus, usb_address);
66-
}
67-
else if (transport == SP_TRANSPORT_BLUETOOTH)
68-
{
62+
} else if (transport == SP_TRANSPORT_BLUETOOTH) {
6963
/* This is a Bluetooth serial port. */
7064
printf("Type: Bluetooth\n");
7165

test_timing.c

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ int main(int argc, char *argv[])
1111
struct time a, b, c;
1212
struct timeval tv;
1313
struct timeout to;
14+
1415
printf("Testing arithmetic\n");
1516
time_set_ms(&a, 10050);
1617
time_set_ms(&b, 100);
@@ -63,5 +64,6 @@ int main(int argc, char *argv[])
6364
timeout_update(&to);
6465
assert(timeout_check(&to));
6566
printf("Timeout expired\n");
67+
6668
return 0;
6769
}

0 commit comments

Comments
 (0)