@@ -31,68 +31,69 @@ int check(enum sp_return result)
31
31
{
32
32
int error_code ;
33
33
char * error_message ;
34
+
34
35
switch (result ) {
35
36
36
37
/* Handle each of the four negative error codes that can be returned.
37
38
*
38
39
* In this example, we will end the program on any error, using
39
40
* a different return code for each possible class of error. */
40
41
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 ;
96
97
}
97
98
}
98
99
0 commit comments