configure.ac: Add 'check-news' Automake option
[libjaylink.git] / libjaylink / device.c
blob1b2a75e2ceb89e8c556540994758c35a5aeb33f9
1 /*
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/>.
20 #include <stdlib.h>
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <string.h>
24 #ifdef _WIN32
25 #include <winsock2.h>
26 #else
27 #include <sys/socket.h>
28 #include <arpa/inet.h>
29 #endif
30 #include <libusb.h>
32 #include "libjaylink.h"
33 #include "libjaylink-internal.h"
35 /**
36 * @file
38 * Device enumeration and handling.
41 /** @cond PRIVATE */
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
64 /** @endcond */
66 /** @private */
67 JAYLINK_PRIV struct jaylink_device *device_allocate(
68 struct jaylink_context *ctx)
70 struct jaylink_device *dev;
71 struct list *list;
73 dev = malloc(sizeof(struct jaylink_device));
75 if (!dev)
76 return NULL;
78 list = list_prepend(ctx->devs, dev);
80 if (!list) {
81 free(dev);
82 return NULL;
85 ctx->devs = list;
87 dev->ctx = ctx;
88 dev->ref_count = 1;
89 dev->usb_dev = NULL;
91 return 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));
100 if (!list)
101 return NULL;
103 list[length] = NULL;
105 return list;
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()
126 * @since 0.1.0
128 JAYLINK_API int jaylink_get_devices(struct jaylink_context *ctx,
129 struct jaylink_device ***devices, size_t *count)
131 size_t num;
132 struct list *item;
133 struct jaylink_device **devs;
134 struct jaylink_device *dev;
135 size_t i;
137 if (!ctx || !devices)
138 return JAYLINK_ERR_ARG;
140 num = list_length(ctx->discovered_devs);
141 devs = allocate_device_list(num);
143 if (!devs) {
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);
153 item = item->next;
156 if (count)
157 *count = num;
159 *devices = devs;
161 return JAYLINK_OK;
165 * Free devices.
167 * @param[in,out] devices Array of device instances. Must be NULL-terminated.
168 * @param[in] unref Determines whether the device instances should be
169 * unreferenced.
171 * @see jaylink_get_devices()
173 * @since 0.1.0
175 JAYLINK_API void jaylink_free_devices(struct jaylink_device **devices,
176 bool unref)
178 size_t i;
180 if (!devices)
181 return;
183 if (unref) {
184 for (i = 0; devices[i]; i++)
185 jaylink_unref_device(devices[i]);
188 free(devices);
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
196 * failure.
198 * @retval JAYLINK_OK Success.
199 * @retval JAYLINK_ERR_ARG Invalid arguments.
201 * @since 0.1.0
203 JAYLINK_API int jaylink_device_get_host_interface(
204 const struct jaylink_device *dev,
205 enum jaylink_host_interface *iface)
207 if (!dev || !iface)
208 return JAYLINK_ERR_ARG;
210 *iface = dev->interface;
212 return JAYLINK_OK;
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.
229 * @since 0.1.0
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;
242 return JAYLINK_OK;
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
253 * failure.
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()
261 * @since 0.1.0
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;
275 return JAYLINK_OK;
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.
285 * @since 0.1.0
287 JAYLINK_API struct jaylink_device *jaylink_ref_device(
288 struct jaylink_device *dev)
290 if (!dev)
291 return NULL;
293 dev->ref_count++;
295 return dev;
299 * Decrement the reference count of a device.
301 * @param[in,out] dev Device instance.
303 * @since 0.1.0
305 JAYLINK_API void jaylink_unref_device(struct jaylink_device *dev)
307 struct jaylink_context *ctx;
309 if (!dev)
310 return;
312 dev->ref_count--;
314 if (!dev->ref_count) {
315 ctx = dev->ctx;
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);
323 if (dev->usb_dev)
324 libusb_unref_device(dev->usb_dev);
326 free(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));
337 if (!devh)
338 return NULL;
340 devh->dev = jaylink_ref_device(dev);
342 return devh;
345 static void free_device_handle(struct jaylink_device_handle *devh)
347 jaylink_unref_device(devh->dev);
348 free(devh);
352 * Open a device.
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.
365 * @since 0.1.0
367 JAYLINK_API int jaylink_open(struct jaylink_device *dev,
368 struct jaylink_device_handle **devh)
370 int ret;
371 struct jaylink_device_handle *handle;
373 if (!dev || !devh)
374 return JAYLINK_ERR_ARG;
376 handle = allocate_device_handle(dev);
378 if (!handle) {
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);
387 return ret;
390 *devh = handle;
392 return JAYLINK_OK;
396 * Close a device.
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.
404 * @since 0.1.0
406 JAYLINK_API int jaylink_close(struct jaylink_device_handle *devh)
408 int ret;
410 if (!devh)
411 return JAYLINK_ERR_ARG;
413 ret = transport_close(devh);
414 free_device_handle(devh);
416 return ret;
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.
428 * @since 0.1.0
430 JAYLINK_API struct jaylink_device *jaylink_get_device(
431 struct jaylink_device_handle *devh)
433 if (!devh)
434 return NULL;
436 return devh->dev;
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.
458 * @since 0.1.0
460 JAYLINK_API int jaylink_get_firmware_version(
461 struct jaylink_device_handle *devh, char **version,
462 size_t *length)
464 int ret;
465 struct jaylink_context *ctx;
466 uint8_t buf[2];
467 uint16_t dummy;
468 char *tmp;
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));
479 return 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));
489 return 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));
497 return ret;
500 dummy = buffer_get_u16(buf, 0);
501 *length = dummy;
503 if (!dummy)
504 return JAYLINK_OK;
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));
511 return ret;
514 tmp = malloc(dummy);
516 if (!tmp) {
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));
526 free(tmp);
527 return ret;
530 /* Last byte is reserved for null-terminator. */
531 tmp[dummy - 1] = 0;
532 *version = tmp;
534 return JAYLINK_OK;
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
547 * positions.
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
551 * @a mask.
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.
559 * @since 0.1.0
561 JAYLINK_API int jaylink_get_hardware_info(struct jaylink_device_handle *devh,
562 uint32_t mask, uint32_t *info)
564 int ret;
565 struct jaylink_context *ctx;
566 uint8_t buf[5];
567 unsigned int i;
568 unsigned int num;
569 unsigned int length;
571 if (!devh || !mask || !info)
572 return JAYLINK_ERR_ARG;
574 ctx = devh->dev->ctx;
575 num = 0;
577 for (i = 0; i < 32; i++) {
578 if (mask & (1 << i))
579 num++;
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));
589 return 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));
600 return 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));
608 return ret;
611 for (i = 0; i < num; i++)
612 info[i] = buffer_get_u32((uint8_t *)info,
613 i * sizeof(uint32_t));
615 return JAYLINK_OK;
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.
637 * @since 0.1.0
639 JAYLINK_API int jaylink_get_hardware_version(
640 struct jaylink_device_handle *devh,
641 struct jaylink_hardware_version *version)
643 int ret;
644 struct jaylink_context *ctx;
645 uint8_t buf[4];
646 uint32_t tmp;
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));
657 return 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));
667 return 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));
675 return 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;
685 return JAYLINK_OK;
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.
700 * @since 0.1.0
702 JAYLINK_API int jaylink_get_hardware_status(struct jaylink_device_handle *devh,
703 struct jaylink_hardware_status *status)
705 int ret;
706 struct jaylink_context *ctx;
707 uint8_t buf[8];
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));
718 return 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));
728 return 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));
736 return 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];
747 return JAYLINK_OK;
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()
775 * @since 0.1.0
777 JAYLINK_API int jaylink_get_caps(struct jaylink_device_handle *devh,
778 uint8_t *caps)
780 int ret;
781 struct jaylink_context *ctx;
782 uint8_t buf[1];
784 if (!devh || !caps)
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));
793 return 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));
803 return 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));
811 return ret;
814 return JAYLINK_OK;
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()
842 * @since 0.1.0
844 JAYLINK_API int jaylink_get_extended_caps(struct jaylink_device_handle *devh,
845 uint8_t *caps)
847 int ret;
848 struct jaylink_context *ctx;
849 uint8_t buf[1];
851 if (!devh || !caps)
852 return JAYLINK_ERR_ARG;
854 ctx = devh->dev->ctx;
855 ret = transport_start_write_read(devh, 1, JAYLINK_DEV_EXT_CAPS_SIZE,
856 true);
858 if (ret != JAYLINK_OK) {
859 log_err(ctx, "transport_start_write_read() failed: %s.",
860 jaylink_strerror(ret));
861 return 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));
871 return 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));
879 return ret;
882 return JAYLINK_OK;
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
893 * failure.
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.
901 * @since 0.1.0
903 JAYLINK_API int jaylink_get_free_memory(struct jaylink_device_handle *devh,
904 uint32_t *size)
906 int ret;
907 struct jaylink_context *ctx;
908 uint8_t buf[4];
910 if (!devh || !size)
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));
919 return 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));
929 return 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));
937 return ret;
940 *size = buffer_get_u32(buf, 0);
942 return JAYLINK_OK;
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.
963 * @since 0.1.0
965 JAYLINK_API int jaylink_read_raw_config(struct jaylink_device_handle *devh,
966 uint8_t *config)
968 int ret;
969 struct jaylink_context *ctx;
970 uint8_t buf[1];
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,
977 true);
979 if (ret != JAYLINK_OK) {
980 log_err(ctx, "transport_start_write_read() failed: %s.",
981 jaylink_strerror(ret));
982 return 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));
992 return 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));
1000 return ret;
1003 return JAYLINK_OK;
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.
1023 * @since 0.1.0
1025 JAYLINK_API int jaylink_write_raw_config(struct jaylink_device_handle *devh,
1026 const uint8_t *config)
1028 int ret;
1029 struct jaylink_context *ctx;
1030 uint8_t buf[1];
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));
1041 return 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));
1051 return 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));
1059 return ret;
1062 return JAYLINK_OK;
1065 static void parse_conntable(struct jaylink_connection *conns,
1066 const uint8_t *buffer, uint16_t num, uint16_t entry_size)
1068 unsigned int i;
1069 size_t offset;
1070 struct in_addr in;
1072 offset = 0;
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)
1094 #ifdef _WIN32
1095 int ret;
1096 struct sockaddr_in sock_in;
1097 int length;
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);
1108 if (ret != 0)
1109 return false;
1111 *in = sock_in.sin_addr;
1112 #else
1113 if (inet_pton(AF_INET, str, in) != 1)
1114 return false;
1115 #endif
1117 return true;
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.
1135 * Example code:
1136 * @code{.c}
1137 * static bool register_connection(struct jaylink_device_handle *devh,
1138 * struct jaylink_connection *conn)
1140 * int ret;
1141 * struct jaylink_connection conns[JAYLINK_MAX_CONNECTIONS];
1142 * bool found_handle;
1143 * size_t count;
1144 * size_t i;
1146 * conn->handle = 0;
1147 * conn->pid = 0;
1148 * strcpy(conn->hid, "0.0.0.0");
1149 * conn->iid = 0;
1150 * conn->cid = 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));
1157 * return false;
1160 * found_handle = false;
1162 * for (i = 0; i < count; i++) {
1163 * if (conns[i].handle == conn->handle) {
1164 * found_handle = true;
1165 * break;
1169 * if (!found_handle) {
1170 * printf("Maximum number of connections reached.\n");
1171 * return false;
1174 * printf("Connection successfully registered.\n");
1176 * return true;
1178 * @endcode
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
1187 * failure.
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()
1198 * @since 0.1.0
1200 JAYLINK_API int jaylink_register(struct jaylink_device_handle *devh,
1201 struct jaylink_connection *connection,
1202 struct jaylink_connection *connections, size_t *count)
1204 int ret;
1205 struct jaylink_context *ctx;
1206 uint8_t buf[REG_MAX_SIZE];
1207 uint16_t handle;
1208 uint16_t num;
1209 uint16_t entry_size;
1210 uint32_t size;
1211 uint32_t table_size;
1212 uint16_t addinfo_size;
1213 struct in_addr in;
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));
1238 return 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));
1246 return 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));
1254 return 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: "
1264 "%u.", num);
1265 return JAYLINK_ERR_PROTO;
1268 if (entry_size != REG_CONN_INFO_SIZE) {
1269 log_err(ctx, "Invalid connection entry size: %u bytes.",
1270 entry_size);
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: "
1279 "%u bytes.", size);
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));
1289 return JAYLINK_ERR;
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));
1298 return JAYLINK_ERR;
1302 if (!handle) {
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);
1310 *count = num;
1312 return JAYLINK_OK;
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
1328 * failure.
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()
1339 * @since 0.1.0
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)
1345 int ret;
1346 struct jaylink_context *ctx;
1347 uint8_t buf[REG_MAX_SIZE];
1348 uint16_t num;
1349 uint16_t entry_size;
1350 uint32_t size;
1351 uint32_t table_size;
1352 uint16_t addinfo_size;
1353 struct in_addr in;
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));
1378 return 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));
1386 return 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));
1394 return 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: "
1403 "%u.", num);
1404 return JAYLINK_ERR_PROTO;
1407 if (entry_size != REG_CONN_INFO_SIZE) {
1408 log_err(ctx, "Invalid connection entry size: %u bytes.",
1409 entry_size);
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: "
1418 "%u bytes.", size);
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));
1428 return JAYLINK_ERR;
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));
1437 return JAYLINK_ERR;
1441 parse_conntable(connections, buf + REG_HEADER_SIZE, num, entry_size);
1443 *count = num;
1445 return JAYLINK_OK;