2 * This file is part of the libjaylink project.
4 * Copyright (C) 2014-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/>.
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
32 #include "libjaylink.h"
33 #include "libjaylink-internal.h"
38 * Device enumeration and handling.
42 #define CMD_GET_VERSION 0x01
43 #define CMD_GET_HW_STATUS 0x07
44 #define CMD_REGISTER 0x09
45 #define CMD_GET_HW_INFO 0xc1
46 #define CMD_GET_FREE_MEMORY 0xd4
47 #define CMD_GET_CAPS 0xe8
48 #define CMD_GET_EXT_CAPS 0xed
49 #define CMD_GET_HW_VERSION 0xf0
50 #define CMD_READ_CONFIG 0xf2
51 #define CMD_WRITE_CONFIG 0xf3
53 #define REG_CMD_REGISTER 0x64
54 #define REG_CMD_UNREGISTER 0x65
56 /** Size of the registration header in bytes. */
57 #define REG_HEADER_SIZE 8
58 /** Minimum registration information size in bytes. */
59 #define REG_MIN_SIZE 0x4c
60 /** Maximum registration information size in bytes. */
61 #define REG_MAX_SIZE 0x200
62 /** Size of a connection entry in bytes. */
63 #define REG_CONN_INFO_SIZE 16
67 JAYLINK_PRIV
struct jaylink_device
*device_allocate(
68 struct jaylink_context
*ctx
)
70 struct jaylink_device
*dev
;
73 dev
= malloc(sizeof(struct jaylink_device
));
78 list
= list_prepend(ctx
->devs
, dev
);
94 static struct jaylink_device
**allocate_device_list(size_t length
)
96 struct jaylink_device
**list
;
98 list
= malloc(sizeof(struct jaylink_device
*) * (length
+ 1));
109 * Get available devices.
111 * @param[in,out] ctx libjaylink context.
112 * @param[out] devices Newly allocated array which contains instances of
113 * available devices on success, and undefined on failure.
114 * The array is NULL-terminated and must be free'd by the
115 * caller with jaylink_free_devices().
116 * @param[out] count Number of available devices on success, and undefined on
117 * failure. Can be NULL.
119 * @retval JAYLINK_OK Success.
120 * @retval JAYLINK_ERR_ARG Invalid arguments.
121 * @retval JAYLINK_ERR_MALLOC Memory allocation error.
122 * @retval JAYLINK_ERR Other error conditions.
124 * @see jaylink_discovery_scan()
128 JAYLINK_API
int jaylink_get_devices(struct jaylink_context
*ctx
,
129 struct jaylink_device
***devices
, size_t *count
)
133 struct jaylink_device
**devs
;
134 struct jaylink_device
*dev
;
137 if (!ctx
|| !devices
)
138 return JAYLINK_ERR_ARG
;
140 num
= list_length(ctx
->discovered_devs
);
141 devs
= allocate_device_list(num
);
144 log_err(ctx
, "Failed to allocate device list.");
145 return JAYLINK_ERR_MALLOC
;
148 item
= ctx
->discovered_devs
;
150 for (i
= 0; i
< num
; i
++) {
151 dev
= (struct jaylink_device
*)item
->data
;
152 devs
[i
] = jaylink_ref_device(dev
);
167 * @param[in,out] devices Array of device instances. Must be NULL-terminated.
168 * @param[in] unref Determines whether the device instances should be
171 * @see jaylink_get_devices()
175 JAYLINK_API
void jaylink_free_devices(struct jaylink_device
**devices
,
184 for (i
= 0; devices
[i
]; i
++)
185 jaylink_unref_device(devices
[i
]);
192 * Get the host interface of a device.
194 * @param[in] dev Device instance.
195 * @param[out] iface Host interface of the device on success, and undefined on
198 * @retval JAYLINK_OK Success.
199 * @retval JAYLINK_ERR_ARG Invalid arguments.
203 JAYLINK_API
int jaylink_device_get_host_interface(
204 const struct jaylink_device
*dev
,
205 enum jaylink_host_interface
*iface
)
208 return JAYLINK_ERR_ARG
;
210 *iface
= dev
->interface
;
216 * Get the serial number of a device.
218 * @note This serial number is for enumeration purpose only and might differ
219 * from the real serial number of the device.
221 * @param[in] dev Device instance.
222 * @param[out] serial_number Serial number of the device on success, and
223 * undefined on failure.
225 * @retval JAYLINK_OK Success.
226 * @retval JAYLINK_ERR_ARG Invalid arguments.
227 * @retval JAYLINK_ERR_NOT_AVAILABLE Serial number is not available.
231 JAYLINK_API
int jaylink_device_get_serial_number(
232 const struct jaylink_device
*dev
, uint32_t *serial_number
)
234 if (!dev
|| !serial_number
)
235 return JAYLINK_ERR_ARG
;
237 if (!dev
->valid_serial_number
)
238 return JAYLINK_ERR_NOT_AVAILABLE
;
240 *serial_number
= dev
->serial_number
;
246 * Get the USB address of a device.
248 * @note Identification of a device with the USB address is deprecated and the
249 * serial number should be used instead.
251 * @param[in] dev Device instance.
252 * @param[out] address USB address of the device on success, and undefined on
255 * @retval JAYLINK_OK Success.
256 * @retval JAYLINK_ERR_ARG Invalid arguments.
257 * @retval JAYLINK_ERR_NOT_SUPPORTED Operation not supported.
259 * @see jaylink_device_get_serial_number()
263 JAYLINK_API
int jaylink_device_get_usb_address(
264 const struct jaylink_device
*dev
,
265 enum jaylink_usb_address
*address
)
267 if (!dev
|| !address
)
268 return JAYLINK_ERR_ARG
;
270 if (dev
->interface
!= JAYLINK_HIF_USB
)
271 return JAYLINK_ERR_NOT_SUPPORTED
;
273 *address
= dev
->usb_address
;
279 * Increment the reference count of a device.
281 * @param[in,out] dev Device instance.
283 * @return The given device instance on success, or NULL on invalid argument.
287 JAYLINK_API
struct jaylink_device
*jaylink_ref_device(
288 struct jaylink_device
*dev
)
299 * Decrement the reference count of a device.
301 * @param[in,out] dev Device instance.
305 JAYLINK_API
void jaylink_unref_device(struct jaylink_device
*dev
)
307 struct jaylink_context
*ctx
;
314 if (!dev
->ref_count
) {
317 log_dbg(ctx
, "Device destroyed (bus:address = %03u:%03u).",
318 libusb_get_bus_number(dev
->usb_dev
),
319 libusb_get_device_address(dev
->usb_dev
));
321 ctx
->devs
= list_remove(dev
->ctx
->devs
, dev
);
324 libusb_unref_device(dev
->usb_dev
);
330 static struct jaylink_device_handle
*allocate_device_handle(
331 struct jaylink_device
*dev
)
333 struct jaylink_device_handle
*devh
;
335 devh
= malloc(sizeof(struct jaylink_device_handle
));
340 devh
->dev
= jaylink_ref_device(dev
);
345 static void free_device_handle(struct jaylink_device_handle
*devh
)
347 jaylink_unref_device(devh
->dev
);
354 * @param[in,out] dev Device instance.
355 * @param[out] devh Newly allocated handle for the opened device on success,
356 * and undefined on failure.
358 * @retval JAYLINK_OK Success.
359 * @retval JAYLINK_ERR_ARG Invalid arguments.
360 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
361 * @retval JAYLINK_ERR_MALLOC Memory allocation error.
362 * @retval JAYLINK_ERR_IO Input/output error.
363 * @retval JAYLINK_ERR Other error conditions.
367 JAYLINK_API
int jaylink_open(struct jaylink_device
*dev
,
368 struct jaylink_device_handle
**devh
)
371 struct jaylink_device_handle
*handle
;
374 return JAYLINK_ERR_ARG
;
376 handle
= allocate_device_handle(dev
);
379 log_err(dev
->ctx
, "Device handle malloc failed.");
380 return JAYLINK_ERR_MALLOC
;
383 ret
= transport_open(handle
);
385 if (ret
!= JAYLINK_OK
) {
386 free_device_handle(handle
);
398 * @param[in,out] devh Device instance.
400 * @retval JAYLINK_OK Success.
401 * @retval JAYLINK_ERR_ARG Invalid arguments.
402 * @retval JAYLINK_ERR Other error conditions.
406 JAYLINK_API
int jaylink_close(struct jaylink_device_handle
*devh
)
411 return JAYLINK_ERR_ARG
;
413 ret
= transport_close(devh
);
414 free_device_handle(devh
);
420 * Get the device instance from a device handle.
422 * @note The reference count of the device instance is not increased.
424 * @param[in] devh Device handle.
426 * @return The device instance on success, or NULL on invalid argument.
430 JAYLINK_API
struct jaylink_device
*jaylink_get_device(
431 struct jaylink_device_handle
*devh
)
440 * Retrieve the firmware version of a device.
442 * @param[in,out] devh Device handle.
443 * @param[out] version Newly allocated string which contains the firmware
444 * version on success, and undefined if @p length is zero
445 * or on failure. The string is null-terminated and must be
446 * free'd by the caller.
447 * @param[out] length Length of the firmware version string including trailing
448 * null-terminator on success, and undefined on failure.
449 * Zero if no firmware version string is available.
451 * @retval JAYLINK_OK Success.
452 * @retval JAYLINK_ERR_ARG Invalid arguments.
453 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
454 * @retval JAYLINK_ERR_MALLOC Memory allocation error.
455 * @retval JAYLINK_ERR_IO Input/output error.
456 * @retval JAYLINK_ERR Other error conditions.
460 JAYLINK_API
int jaylink_get_firmware_version(
461 struct jaylink_device_handle
*devh
, char **version
,
465 struct jaylink_context
*ctx
;
470 if (!devh
|| !version
|| !length
)
471 return JAYLINK_ERR_ARG
;
473 ctx
= devh
->dev
->ctx
;
474 ret
= transport_start_write_read(devh
, 1, 2, true);
476 if (ret
!= JAYLINK_OK
) {
477 log_err(ctx
, "transport_start_write_read() failed: %s.",
478 jaylink_strerror(ret
));
482 buf
[0] = CMD_GET_VERSION
;
484 ret
= transport_write(devh
, buf
, 1);
486 if (ret
!= JAYLINK_OK
) {
487 log_err(ctx
, "transport_write() failed: %s.",
488 jaylink_strerror(ret
));
492 ret
= transport_read(devh
, buf
, 2);
494 if (ret
!= JAYLINK_OK
) {
495 log_err(ctx
, "transport_read() failed: %s.",
496 jaylink_strerror(ret
));
500 dummy
= buffer_get_u16(buf
, 0);
506 ret
= transport_start_read(devh
, dummy
);
508 if (ret
!= JAYLINK_OK
) {
509 log_err(ctx
, "transport_start_read() failed: %s.",
510 jaylink_strerror(ret
));
517 log_err(ctx
, "Firmware version string malloc failed.");
518 return JAYLINK_ERR_MALLOC
;
521 ret
= transport_read(devh
, (uint8_t *)tmp
, dummy
);
523 if (ret
!= JAYLINK_OK
) {
524 log_err(ctx
, "transport_read() failed: %s.",
525 jaylink_strerror(ret
));
530 /* Last byte is reserved for null-terminator. */
538 * Retrieve the hardware information of a device.
540 * @note This function must only be used if the device has the
541 * #JAYLINK_DEV_CAP_GET_HW_INFO capability.
543 * @param[in,out] devh Device handle.
544 * @param[in] mask A bit field where each set bit represents hardware
545 * information to request. See #jaylink_hardware_info for a
546 * description of the hardware information and their bit
548 * @param[out] info Array to store the hardware information on success. Its
549 * content is undefined on failure. The array must be large
550 * enough to contain at least as many elements as bits set in
553 * @retval JAYLINK_OK Success.
554 * @retval JAYLINK_ERR_ARG Invalid arguments.
555 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
556 * @retval JAYLINK_ERR_IO Input/output error.
557 * @retval JAYLINK_ERR Other error conditions.
561 JAYLINK_API
int jaylink_get_hardware_info(struct jaylink_device_handle
*devh
,
562 uint32_t mask
, uint32_t *info
)
565 struct jaylink_context
*ctx
;
571 if (!devh
|| !mask
|| !info
)
572 return JAYLINK_ERR_ARG
;
574 ctx
= devh
->dev
->ctx
;
577 for (i
= 0; i
< 32; i
++) {
582 length
= num
* sizeof(uint32_t);
584 ret
= transport_start_write_read(devh
, 5, length
, true);
586 if (ret
!= JAYLINK_OK
) {
587 log_err(ctx
, "transport_start_write_read() failed: %s.",
588 jaylink_strerror(ret
));
592 buf
[0] = CMD_GET_HW_INFO
;
593 buffer_set_u32(buf
, mask
, 1);
595 ret
= transport_write(devh
, buf
, 5);
597 if (ret
!= JAYLINK_OK
) {
598 log_err(ctx
, "transport_write() failed: %s.",
599 jaylink_strerror(ret
));
603 ret
= transport_read(devh
, (uint8_t *)info
, length
);
605 if (ret
!= JAYLINK_OK
) {
606 log_err(ctx
, "transport_read() failed: %s.",
607 jaylink_strerror(ret
));
611 for (i
= 0; i
< num
; i
++)
612 info
[i
] = buffer_get_u32((uint8_t *)info
,
613 i
* sizeof(uint32_t));
619 * Retrieve the hardware version of a device.
621 * @note This function must only be used if the device has the
622 * #JAYLINK_DEV_CAP_GET_HW_VERSION capability.
624 * @warning This function may return a value for @p version where
625 * #jaylink_hardware_version::type is not covered by
626 * #jaylink_hardware_type.
628 * @param[in,out] devh Device handle.
629 * @param[out] version Hardware version on success, and undefined on failure.
631 * @retval JAYLINK_OK Success.
632 * @retval JAYLINK_ERR_ARG Invalid arguments.
633 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
634 * @retval JAYLINK_ERR_IO Input/output error.
635 * @retval JAYLINK_ERR Other error conditions.
639 JAYLINK_API
int jaylink_get_hardware_version(
640 struct jaylink_device_handle
*devh
,
641 struct jaylink_hardware_version
*version
)
644 struct jaylink_context
*ctx
;
648 if (!devh
|| !version
)
649 return JAYLINK_ERR_ARG
;
651 ctx
= devh
->dev
->ctx
;
652 ret
= transport_start_write_read(devh
, 1, 4, true);
654 if (ret
!= JAYLINK_OK
) {
655 log_err(ctx
, "transport_start_write_read() failed: %s.",
656 jaylink_strerror(ret
));
660 buf
[0] = CMD_GET_HW_VERSION
;
662 ret
= transport_write(devh
, buf
, 1);
664 if (ret
!= JAYLINK_OK
) {
665 log_err(ctx
, "transport_write() failed: %s.",
666 jaylink_strerror(ret
));
670 ret
= transport_read(devh
, buf
, 4);
672 if (ret
!= JAYLINK_OK
) {
673 log_err(ctx
, "transport_read() failed: %s.",
674 jaylink_strerror(ret
));
678 tmp
= buffer_get_u32(buf
, 0);
680 version
->type
= (tmp
/ 1000000) % 100;
681 version
->major
= (tmp
/ 10000) % 100;
682 version
->minor
= (tmp
/ 100) % 100;
683 version
->revision
= tmp
% 100;
689 * Retrieve the hardware status of a device.
691 * @param[in,out] devh Device handle.
692 * @param[out] status Hardware status on success, and undefined on failure.
694 * @retval JAYLINK_OK Success.
695 * @retval JAYLINK_ERR_ARG Invalid arguments.
696 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
697 * @retval JAYLINK_ERR_IO Input/output error.
698 * @retval JAYLINK_ERR Other error conditions.
702 JAYLINK_API
int jaylink_get_hardware_status(struct jaylink_device_handle
*devh
,
703 struct jaylink_hardware_status
*status
)
706 struct jaylink_context
*ctx
;
709 if (!devh
|| !status
)
710 return JAYLINK_ERR_ARG
;
712 ctx
= devh
->dev
->ctx
;
713 ret
= transport_start_write_read(devh
, 1, 8, true);
715 if (ret
!= JAYLINK_OK
) {
716 log_err(ctx
, "transport_start_write_read() failed: %s.",
717 jaylink_strerror(ret
));
721 buf
[0] = CMD_GET_HW_STATUS
;
723 ret
= transport_write(devh
, buf
, 1);
725 if (ret
!= JAYLINK_OK
) {
726 log_err(ctx
, "transport_write() failed: %s.",
727 jaylink_strerror(ret
));
731 ret
= transport_read(devh
, buf
, 8);
733 if (ret
!= JAYLINK_OK
) {
734 log_err(ctx
, "transport_read() failed: %s.",
735 jaylink_strerror(ret
));
739 status
->target_voltage
= buffer_get_u16(buf
, 0);
740 status
->tck
= buf
[2];
741 status
->tdi
= buf
[3];
742 status
->tdo
= buf
[4];
743 status
->tms
= buf
[5];
744 status
->tres
= buf
[6];
745 status
->trst
= buf
[7];
751 * Retrieve the capabilities of a device.
753 * The capabilities are stored in a 32-bit bit array consisting of
754 * #JAYLINK_DEV_CAPS_SIZE bytes where each individual bit represents a
755 * capability. The first bit of this array is the least significant bit of the
756 * first byte and the following bits are sequentially numbered in order of
757 * increasing bit significance and byte index. A set bit indicates a supported
758 * capability. See #jaylink_device_capability for a description of the
759 * capabilities and their bit positions.
761 * @param[in,out] devh Device handle.
762 * @param[out] caps Buffer to store capabilities on success. Its content is
763 * undefined on failure. The buffer must be large enough to
764 * contain at least #JAYLINK_DEV_CAPS_SIZE bytes.
766 * @retval JAYLINK_OK Success.
767 * @retval JAYLINK_ERR_ARG Invalid arguments.
768 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
769 * @retval JAYLINK_ERR_IO Input/output error.
770 * @retval JAYLINK_ERR Other error conditions.
772 * @see jaylink_get_extended_caps()
773 * @see jaylink_has_cap()
777 JAYLINK_API
int jaylink_get_caps(struct jaylink_device_handle
*devh
,
781 struct jaylink_context
*ctx
;
785 return JAYLINK_ERR_ARG
;
787 ctx
= devh
->dev
->ctx
;
788 ret
= transport_start_write_read(devh
, 1, JAYLINK_DEV_CAPS_SIZE
, true);
790 if (ret
!= JAYLINK_OK
) {
791 log_err(ctx
, "transport_start_write_read() failed: %s.",
792 jaylink_strerror(ret
));
796 buf
[0] = CMD_GET_CAPS
;
798 ret
= transport_write(devh
, buf
, 1);
800 if (ret
!= JAYLINK_OK
) {
801 log_err(ctx
, "transport_write() failed: %s.",
802 jaylink_strerror(ret
));
806 ret
= transport_read(devh
, caps
, JAYLINK_DEV_CAPS_SIZE
);
808 if (ret
!= JAYLINK_OK
) {
809 log_err(ctx
, "transport_read() failed: %s.",
810 jaylink_strerror(ret
));
818 * Retrieve the extended capabilities of a device.
820 * The extended capabilities are stored in a 256-bit bit array consisting of
821 * #JAYLINK_DEV_EXT_CAPS_SIZE bytes. See jaylink_get_caps() for a further
822 * description of how the capabilities are represented in this bit array. For a
823 * description of the capabilities and their bit positions, see
824 * #jaylink_device_capability.
826 * @note This function must only be used if the device has the
827 * #JAYLINK_DEV_CAP_GET_EXT_CAPS capability.
829 * @param[in,out] devh Device handle.
830 * @param[out] caps Buffer to store capabilities on success. Its content is
831 * undefined on failure. The buffer must be large enough to
832 * contain at least #JAYLINK_DEV_EXT_CAPS_SIZE bytes.
834 * @retval JAYLINK_OK Success.
835 * @retval JAYLINK_ERR_ARG Invalid arguments.
836 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
837 * @retval JAYLINK_ERR_IO Input/output error.
838 * @retval JAYLINK_ERR Other error conditions.
840 * @see jaylink_get_caps()
844 JAYLINK_API
int jaylink_get_extended_caps(struct jaylink_device_handle
*devh
,
848 struct jaylink_context
*ctx
;
852 return JAYLINK_ERR_ARG
;
854 ctx
= devh
->dev
->ctx
;
855 ret
= transport_start_write_read(devh
, 1, JAYLINK_DEV_EXT_CAPS_SIZE
,
858 if (ret
!= JAYLINK_OK
) {
859 log_err(ctx
, "transport_start_write_read() failed: %s.",
860 jaylink_strerror(ret
));
864 buf
[0] = CMD_GET_EXT_CAPS
;
866 ret
= transport_write(devh
, buf
, 1);
868 if (ret
!= JAYLINK_OK
) {
869 log_err(ctx
, "transport_write() failed: %s.",
870 jaylink_strerror(ret
));
874 ret
= transport_read(devh
, caps
, JAYLINK_DEV_EXT_CAPS_SIZE
);
876 if (ret
!= JAYLINK_OK
) {
877 log_err(ctx
, "transport_read() failed: %s.",
878 jaylink_strerror(ret
));
886 * Retrieve the size of free memory of a device.
888 * @note This function must only be used if the device has the
889 * #JAYLINK_DEV_CAP_GET_FREE_MEMORY capability.
891 * @param[in,out] devh Device handle.
892 * @param[out] size Size of free memory in bytes on success, and undefined on
895 * @retval JAYLINK_OK Success.
896 * @retval JAYLINK_ERR_ARG Invalid arguments.
897 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
898 * @retval JAYLINK_ERR_IO Input/output error.
899 * @retval JAYLINK_ERR Other error conditions.
903 JAYLINK_API
int jaylink_get_free_memory(struct jaylink_device_handle
*devh
,
907 struct jaylink_context
*ctx
;
911 return JAYLINK_ERR_ARG
;
913 ctx
= devh
->dev
->ctx
;
914 ret
= transport_start_write_read(devh
, 1, 4, true);
916 if (ret
!= JAYLINK_OK
) {
917 log_err(ctx
, "transport_start_write_read() failed: %s.",
918 jaylink_strerror(ret
));
922 buf
[0] = CMD_GET_FREE_MEMORY
;
924 ret
= transport_write(devh
, buf
, 1);
926 if (ret
!= JAYLINK_OK
) {
927 log_err(ctx
, "transport_write() failed: %s.",
928 jaylink_strerror(ret
));
932 ret
= transport_read(devh
, buf
, 4);
934 if (ret
!= JAYLINK_OK
) {
935 log_err(ctx
, "transport_read() failed: %s.",
936 jaylink_strerror(ret
));
940 *size
= buffer_get_u32(buf
, 0);
946 * Read the raw configuration data of a device.
948 * @note This function must only be used if the device has the
949 * #JAYLINK_DEV_CAP_READ_CONFIG capability.
951 * @param[in,out] devh Device handle.
952 * @param[out] config Buffer to store configuration data on success. Its
953 * content is undefined on failure. The buffer must be large
954 * enough to contain at least
955 * #JAYLINK_DEV_CONFIG_SIZE bytes.
957 * @retval JAYLINK_OK Success.
958 * @retval JAYLINK_ERR_ARG Invalid arguments.
959 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
960 * @retval JAYLINK_ERR_IO Input/output error.
961 * @retval JAYLINK_ERR Other error conditions.
965 JAYLINK_API
int jaylink_read_raw_config(struct jaylink_device_handle
*devh
,
969 struct jaylink_context
*ctx
;
972 if (!devh
|| !config
)
973 return JAYLINK_ERR_ARG
;
975 ctx
= devh
->dev
->ctx
;
976 ret
= transport_start_write_read(devh
, 1, JAYLINK_DEV_CONFIG_SIZE
,
979 if (ret
!= JAYLINK_OK
) {
980 log_err(ctx
, "transport_start_write_read() failed: %s.",
981 jaylink_strerror(ret
));
985 buf
[0] = CMD_READ_CONFIG
;
987 ret
= transport_write(devh
, buf
, 1);
989 if (ret
!= JAYLINK_OK
) {
990 log_err(ctx
, "transport_write() failed: %s.",
991 jaylink_strerror(ret
));
995 ret
= transport_read(devh
, config
, JAYLINK_DEV_CONFIG_SIZE
);
997 if (ret
!= JAYLINK_OK
) {
998 log_err(ctx
, "transport_read() failed: %s.",
999 jaylink_strerror(ret
));
1007 * Write the raw configuration data of a device.
1009 * @note This function must only be used if the device has the
1010 * #JAYLINK_DEV_CAP_WRITE_CONFIG capability.
1012 * @param[in,out] devh Device handle.
1013 * @param[in] config Buffer to write configuration data from. The size of the
1014 * configuration data is expected to be
1015 * #JAYLINK_DEV_CONFIG_SIZE bytes.
1017 * @retval JAYLINK_OK Success.
1018 * @retval JAYLINK_ERR_ARG Invalid arguments.
1019 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
1020 * @retval JAYLINK_ERR_IO Input/output error.
1021 * @retval JAYLINK_ERR Other error conditions.
1025 JAYLINK_API
int jaylink_write_raw_config(struct jaylink_device_handle
*devh
,
1026 const uint8_t *config
)
1029 struct jaylink_context
*ctx
;
1032 if (!devh
|| !config
)
1033 return JAYLINK_ERR_ARG
;
1035 ctx
= devh
->dev
->ctx
;
1036 ret
= transport_start_write(devh
, 1 + JAYLINK_DEV_CONFIG_SIZE
, true);
1038 if (ret
!= JAYLINK_OK
) {
1039 log_err(ctx
, "transport_start_write() failed: %s.",
1040 jaylink_strerror(ret
));
1044 buf
[0] = CMD_WRITE_CONFIG
;
1046 ret
= transport_write(devh
, buf
, 1);
1048 if (ret
!= JAYLINK_OK
) {
1049 log_err(ctx
, "transport_write() failed: %s.",
1050 jaylink_strerror(ret
));
1054 ret
= transport_write(devh
, config
, JAYLINK_DEV_CONFIG_SIZE
);
1056 if (ret
!= JAYLINK_OK
) {
1057 log_err(ctx
, "transport_write() failed: %s.",
1058 jaylink_strerror(ret
));
1065 static void parse_conntable(struct jaylink_connection
*conns
,
1066 const uint8_t *buffer
, uint16_t num
, uint16_t entry_size
)
1074 for (i
= 0; i
< num
; i
++) {
1075 conns
[i
].pid
= buffer_get_u32(buffer
, offset
);
1077 in
.s_addr
= buffer_get_u32(buffer
, offset
+ 4);
1079 * Use inet_ntoa() instead of inet_ntop() because the latter
1080 * requires at least Windows Vista.
1082 strcpy(conns
[i
].hid
, inet_ntoa(in
));
1084 conns
[i
].iid
= buffer
[offset
+ 8];
1085 conns
[i
].cid
= buffer
[offset
+ 9];
1086 conns
[i
].handle
= buffer_get_u16(buffer
, offset
+ 10);
1087 conns
[i
].timestamp
= buffer_get_u32(buffer
, offset
+ 12);
1088 offset
= offset
+ entry_size
;
1092 static bool _inet_pton(const char *str
, struct in_addr
*in
)
1096 struct sockaddr_in sock_in
;
1099 length
= sizeof(sock_in
);
1102 * Use WSAStringToAddress() instead of inet_pton() because the latter
1103 * requires at least Windows Vista.
1105 ret
= WSAStringToAddress((LPTSTR
)str
, AF_INET
, NULL
,
1106 (LPSOCKADDR
)&sock_in
, &length
);
1111 *in
= sock_in
.sin_addr
;
1113 if (inet_pton(AF_INET
, str
, in
) != 1)
1121 * Register a connection on a device.
1123 * A connection can be registered by using 0 as handle. Additional information
1124 * about the connection can be attached whereby the timestamp is a read-only
1125 * value and therefore ignored for registration. On success, a new handle
1126 * greater than 0 is obtained from the device.
1128 * However, if an obtained handle does not appear in the list of device
1129 * connections, the connection was not registered because the maximum number of
1130 * connections on the device is reached.
1132 * @note This function must only be used if the device has the
1133 * #JAYLINK_DEV_CAP_REGISTER capability.
1137 * static bool register_connection(struct jaylink_device_handle *devh,
1138 * struct jaylink_connection *conn)
1141 * struct jaylink_connection conns[JAYLINK_MAX_CONNECTIONS];
1142 * bool found_handle;
1148 * strcpy(conn->hid, "0.0.0.0");
1152 * ret = jaylink_register(devh, conn, conns, &count);
1154 * if (ret != JAYLINK_OK) {
1155 * printf("jaylink_register() failed: %s.\n",
1156 * jaylink_strerror(ret));
1160 * found_handle = false;
1162 * for (i = 0; i < count; i++) {
1163 * if (conns[i].handle == conn->handle) {
1164 * found_handle = true;
1169 * if (!found_handle) {
1170 * printf("Maximum number of connections reached.\n");
1174 * printf("Connection successfully registered.\n");
1180 * @param[in,out] devh Device handle.
1181 * @param[in,out] connection Connection to register on the device.
1182 * @param[out] connections Array to store device connections on success.
1183 * Its content is undefined on failure. The array must
1184 * be large enough to contain at least
1185 * #JAYLINK_MAX_CONNECTIONS elements.
1186 * @param[out] count Number of device connections on success, and undefined on
1189 * @retval JAYLINK_OK Success.
1190 * @retval JAYLINK_ERR_ARG Invalid arguments.
1191 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
1192 * @retval JAYLINK_ERR_PROTO Protocol violation.
1193 * @retval JAYLINK_ERR_IO Input/output error.
1194 * @retval JAYLINK_ERR Other error conditions.
1196 * @see jaylink_unregister()
1200 JAYLINK_API
int jaylink_register(struct jaylink_device_handle
*devh
,
1201 struct jaylink_connection
*connection
,
1202 struct jaylink_connection
*connections
, size_t *count
)
1205 struct jaylink_context
*ctx
;
1206 uint8_t buf
[REG_MAX_SIZE
];
1209 uint16_t entry_size
;
1211 uint32_t table_size
;
1212 uint16_t addinfo_size
;
1215 if (!devh
|| !connection
|| !connections
|| !count
)
1216 return JAYLINK_ERR_ARG
;
1218 ctx
= devh
->dev
->ctx
;
1220 buf
[0] = CMD_REGISTER
;
1221 buf
[1] = REG_CMD_REGISTER
;
1222 buffer_set_u32(buf
, connection
->pid
, 2);
1224 if (!_inet_pton(connection
->hid
, &in
))
1225 return JAYLINK_ERR_ARG
;
1227 buffer_set_u32(buf
, in
.s_addr
, 6);
1229 buf
[10] = connection
->iid
;
1230 buf
[11] = connection
->cid
;
1231 buffer_set_u16(buf
, connection
->handle
, 12);
1233 ret
= transport_start_write_read(devh
, 14, REG_MIN_SIZE
, true);
1235 if (ret
!= JAYLINK_OK
) {
1236 log_err(ctx
, "transport_start_write_read() failed: %s.",
1237 jaylink_strerror(ret
));
1241 ret
= transport_write(devh
, buf
, 14);
1243 if (ret
!= JAYLINK_OK
) {
1244 log_err(ctx
, "transport_write() failed: %s.",
1245 jaylink_strerror(ret
));
1249 ret
= transport_read(devh
, buf
, REG_MIN_SIZE
);
1251 if (ret
!= JAYLINK_OK
) {
1252 log_err(ctx
, "transport_read() failed: %s.",
1253 jaylink_strerror(ret
));
1257 handle
= buffer_get_u16(buf
, 0);
1258 num
= buffer_get_u16(buf
, 2);
1259 entry_size
= buffer_get_u16(buf
, 4);
1260 addinfo_size
= buffer_get_u16(buf
, 6);
1262 if (num
> JAYLINK_MAX_CONNECTIONS
) {
1263 log_err(ctx
, "Maximum number of device connections exceeded: "
1265 return JAYLINK_ERR_PROTO
;
1268 if (entry_size
!= REG_CONN_INFO_SIZE
) {
1269 log_err(ctx
, "Invalid connection entry size: %u bytes.",
1271 return JAYLINK_ERR_PROTO
;
1274 table_size
= num
* entry_size
;
1275 size
= REG_HEADER_SIZE
+ table_size
+ addinfo_size
;
1277 if (size
> REG_MAX_SIZE
) {
1278 log_err(ctx
, "Maximum registration information size exceeded: "
1280 return JAYLINK_ERR_PROTO
;
1283 if (size
> REG_MIN_SIZE
) {
1284 ret
= transport_start_read(devh
, size
- REG_MIN_SIZE
);
1286 if (ret
!= JAYLINK_OK
) {
1287 log_err(ctx
, "transport_start_read() failed: %s.",
1288 jaylink_strerror(ret
));
1292 ret
= transport_read(devh
, buf
+ REG_MIN_SIZE
,
1293 size
- REG_MIN_SIZE
);
1295 if (ret
!= JAYLINK_OK
) {
1296 log_err(ctx
, "transport_read() failed: %s.",
1297 jaylink_strerror(ret
));
1303 log_err(ctx
, "Obtained invalid connection handle.");
1304 return JAYLINK_ERR_PROTO
;
1307 connection
->handle
= handle
;
1308 parse_conntable(connections
, buf
+ REG_HEADER_SIZE
, num
, entry_size
);
1316 * Unregister a connection from a device.
1318 * @note This function must only be used if the device has the
1319 * #JAYLINK_DEV_CAP_REGISTER capability.
1321 * @param[in,out] devh Device handle.
1322 * @param[in,out] connection Connection to unregister from the device.
1323 * @param[out] connections Array to store device connections on success.
1324 * Its content is undefined on failure. The array must
1325 * be large enough to contain at least
1326 * #JAYLINK_MAX_CONNECTIONS elements.
1327 * @param[out] count Number of device connections on success, and undefined on
1330 * @retval JAYLINK_OK Success.
1331 * @retval JAYLINK_ERR_ARG Invalid arguments.
1332 * @retval JAYLINK_ERR_TIMEOUT A timeout occurred.
1333 * @retval JAYLINK_ERR_PROTO Protocol violation.
1334 * @retval JAYLINK_ERR_IO Input/output error.
1335 * @retval JAYLINK_ERR Other error conditions.
1337 * @see jaylink_register()
1341 JAYLINK_API
int jaylink_unregister(struct jaylink_device_handle
*devh
,
1342 const struct jaylink_connection
*connection
,
1343 struct jaylink_connection
*connections
, size_t *count
)
1346 struct jaylink_context
*ctx
;
1347 uint8_t buf
[REG_MAX_SIZE
];
1349 uint16_t entry_size
;
1351 uint32_t table_size
;
1352 uint16_t addinfo_size
;
1355 if (!devh
|| !connection
|| !connections
|| !count
)
1356 return JAYLINK_ERR_ARG
;
1358 ctx
= devh
->dev
->ctx
;
1360 buf
[0] = CMD_REGISTER
;
1361 buf
[1] = REG_CMD_UNREGISTER
;
1362 buffer_set_u32(buf
, connection
->pid
, 2);
1364 if (!_inet_pton(connection
->hid
, &in
))
1365 return JAYLINK_ERR_ARG
;
1367 buffer_set_u32(buf
, in
.s_addr
, 6);
1369 buf
[10] = connection
->iid
;
1370 buf
[11] = connection
->cid
;
1371 buffer_set_u16(buf
, connection
->handle
, 12);
1373 ret
= transport_start_write_read(devh
, 14, REG_MIN_SIZE
, true);
1375 if (ret
!= JAYLINK_OK
) {
1376 log_err(ctx
, "transport_start_write_read() failed: %s.",
1377 jaylink_strerror(ret
));
1381 ret
= transport_write(devh
, buf
, 14);
1383 if (ret
!= JAYLINK_OK
) {
1384 log_err(ctx
, "transport_write() failed: %s.",
1385 jaylink_strerror(ret
));
1389 ret
= transport_read(devh
, buf
, REG_MIN_SIZE
);
1391 if (ret
!= JAYLINK_OK
) {
1392 log_err(ctx
, "transport_read() failed: %s.",
1393 jaylink_strerror(ret
));
1397 num
= buffer_get_u16(buf
, 2);
1398 entry_size
= buffer_get_u16(buf
, 4);
1399 addinfo_size
= buffer_get_u16(buf
, 6);
1401 if (num
> JAYLINK_MAX_CONNECTIONS
) {
1402 log_err(ctx
, "Maximum number of device connections exceeded: "
1404 return JAYLINK_ERR_PROTO
;
1407 if (entry_size
!= REG_CONN_INFO_SIZE
) {
1408 log_err(ctx
, "Invalid connection entry size: %u bytes.",
1410 return JAYLINK_ERR_PROTO
;
1413 table_size
= num
* entry_size
;
1414 size
= REG_HEADER_SIZE
+ table_size
+ addinfo_size
;
1416 if (size
> REG_MAX_SIZE
) {
1417 log_err(ctx
, "Maximum registration information size exceeded: "
1419 return JAYLINK_ERR_PROTO
;
1422 if (size
> REG_MIN_SIZE
) {
1423 ret
= transport_start_read(devh
, size
- REG_MIN_SIZE
);
1425 if (ret
!= JAYLINK_OK
) {
1426 log_err(ctx
, "transport_start_read() failed: %s.",
1427 jaylink_strerror(ret
));
1431 ret
= transport_read(devh
, buf
+ REG_MIN_SIZE
,
1432 size
- REG_MIN_SIZE
);
1434 if (ret
!= JAYLINK_OK
) {
1435 log_err(ctx
, "transport_read() failed: %s.",
1436 jaylink_strerror(ret
));
1441 parse_conntable(connections
, buf
+ REG_HEADER_SIZE
, num
, entry_size
);