Makefile.am: Fix udev rule name in EXTRA_DIST
[libjaylink.git] / libjaylink / strutil.c
blob913a5a54c774d42a347bebdcb1faf1155b995674
1 /*
2 * This file is part of the libjaylink project.
4 * Copyright (C) 2016 Marc Schink <jaylink-dev@marcschink.de>
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <errno.h>
24 #include "libjaylink.h"
26 /**
27 * @file
29 * String utility functions.
32 /**
33 * Convert a string representation of a serial number to an integer.
35 * The string representation of the serial number must be in decimal form.
37 * @param[in] str String representation to convert.
38 * @param[out] serial_number Serial number on success, and undefined on
39 * failure.
41 * @retval JAYLINK_OK Success.
42 * @retval JAYLINK_ERR_ARG Invalid arguments.
43 * @retval JAYLINK_ERR Conversion error. Serial number is invalid or string
44 * representation contains invalid character(s).
46 * @since 0.1.0
48 JAYLINK_API int jaylink_parse_serial_number(const char *str,
49 uint32_t *serial_number)
51 char *end_ptr;
52 unsigned long long tmp;
54 if (!str || !serial_number)
55 return JAYLINK_ERR_ARG;
57 errno = 0;
58 tmp = strtoull(str, &end_ptr, 10);
60 if (*end_ptr != '\0' || errno != 0 || tmp > UINT32_MAX)
61 return JAYLINK_ERR;
63 *serial_number = tmp;
65 return JAYLINK_OK;
68 /**
69 * Get the string representation of a hardware type.
71 * @param[in] type Hardware type.
73 * @return The string representation of the given hardware type, or NULL
74 * for an unknown type.
76 * @since 0.3.0
78 JAYLINK_API const char *jaylink_hardware_type_string(
79 enum jaylink_hardware_type type)
81 switch (type) {
82 case JAYLINK_HW_TYPE_JLINK:
83 return "J-Link";
84 case JAYLINK_HW_TYPE_FLASHER:
85 return "Flasher";
86 case JAYLINK_HW_TYPE_JLINK_PRO:
87 return "J-Link PRO";
88 default:
89 break;
92 return NULL;
95 /**
96 * Get the string representation of a target interface.
98 * @param[in] iface Target interface.
100 * @return The string representation of the given target interface, or NULL
101 * for an unknown interface.
103 * @since 0.3.0
105 JAYLINK_API const char *jaylink_target_interface_string(
106 enum jaylink_target_interface iface)
108 switch (iface) {
109 case JAYLINK_TIF_JTAG:
110 return "JTAG";
111 case JAYLINK_TIF_SWD:
112 return "SWD";
113 case JAYLINK_TIF_BDM3:
114 return "BDM3";
115 case JAYLINK_TIF_FINE:
116 return "FINE";
117 case JAYLINK_TIF_2W_JTAG_PIC32:
118 return "2-wire JTAG for PIC32";
119 case JAYLINK_TIF_SPI:
120 return "SPI";
121 case JAYLINK_TIF_C2:
122 return "C2";
123 case JAYLINK_TIF_CJTAG:
124 return "cJTAG";
125 default:
126 break;
129 return NULL;