summaryrefslogtreecommitdiff
path: root/libjack/uuid.c
blob: 9b86f5f2f779cc006958fca8e7a27b25fc9ee062 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
    Copyright (C) 2013 Paul Davis

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 2.1 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 */

#include <stdio.h>
#include <stdint.h>

#include <jack/types.h>
#include <jack/uuid.h>

#include "internal.h"

static pthread_mutex_t uuid_lock = PTHREAD_MUTEX_INITIALIZER;
static uint32_t uuid_cnt = 0;

enum JackUUIDType {
	JackUUIDPort = 0x1,
	JackUUIDClient = 0x2
};

jack_uuid_t
jack_client_uuid_generate ()
{
	jack_uuid_t uuid = JackUUIDClient;

	pthread_mutex_lock (&uuid_lock);
	uuid = (uuid << 32) | ++uuid_cnt;
	pthread_mutex_unlock (&uuid_lock);
	return uuid;
}

jack_uuid_t
jack_port_uuid_generate (uint32_t port_id)
{
	jack_uuid_t uuid = JackUUIDPort;

	uuid = (uuid << 32) | (port_id + 1);
	return uuid;
}

uint32_t
jack_uuid_to_index (jack_uuid_t u)
{
	return (u & 0xffff) - 1;
}

int
jack_uuid_empty (jack_uuid_t u)
{
	return u == 0;
}

int
jack_uuid_compare (jack_uuid_t a, jack_uuid_t b)
{
	if (a == b) {
		return 0;
	}

	if (a < b) {
		return -1;
	}

	return 1;
}

void
jack_uuid_copy (jack_uuid_t* dst, jack_uuid_t src)
{
	*dst = src;
}

void
jack_uuid_clear (jack_uuid_t* u)
{
	*u = 0;
}

void
jack_uuid_unparse (jack_uuid_t u, char b[JACK_UUID_STRING_SIZE])
{
	snprintf (b, JACK_UUID_STRING_SIZE, "%" PRIu64, u);
}

int
jack_uuid_parse (const char *b, jack_uuid_t* u)
{
	if (sscanf (b, "%" PRIu64, u) == 1) {

		if (*u < (0x1LL << 32)) {
			/* has not type bits set - not legal */
			return -1;
		}

		return 0;
	}

	return -1;
}