2 * This file is part of buxton.
4 * Copyright (C) 2013 Intel Corporation
6 * buxton is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1
9 * of the License, or (at your option) any later version.
13 * \file lbuxton.c Buxton library implementation
25 #include <sys/socket.h>
27 #include <sys/types.h>
34 #include "buxtonclient.h"
35 #include "buxtonkey.h"
36 #include "buxtonresponse.h"
37 #include "buxtonstring.h"
38 #include "configurator.h"
45 int buxton_set_conf_file(char *path)
54 if (st.st_mode & S_IFDIR) {
59 buxton_add_cmd_line(CONFIG_CONF_FILE, path);
64 int buxton_open(BuxtonClient *client)
66 _BuxtonClient **c = (_BuxtonClient **)client;
67 _BuxtonClient *cl = NULL;
69 struct sockaddr_un remote;
72 if ((bx_socket = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
76 remote.sun_family = AF_UNIX;
77 sock_name_len = strlen(buxton_socket()) + 1;
78 if (sock_name_len >= sizeof(remote.sun_path)) {
79 buxton_log("Provided socket name: %s is too long, maximum allowed length is %d bytes\n",
80 buxton_socket(), sizeof(remote.sun_path));
84 strncpy(remote.sun_path, buxton_socket(), sock_name_len);
85 r = connect(bx_socket, (struct sockaddr *)&remote, sizeof(remote));
91 if (fcntl(bx_socket, F_SETFL, O_NONBLOCK)) {
96 if (!setup_callbacks()) {
101 cl = malloc0(sizeof(_BuxtonClient));
113 void buxton_close(BuxtonClient client)
120 c = (_BuxtonClient *)client;
129 int buxton_get_value(BuxtonClient client,
131 BuxtonCallback callback,
137 _BuxtonKey *k = (_BuxtonKey *)key;
139 if (!k || !(k->group.value) || !(k->name.value) ||
140 k->type <= BUXTON_TYPE_MIN || k->type >= BUXTON_TYPE_MAX) {
144 r = buxton_wire_get_value((_BuxtonClient *)client, k, callback, data);
150 ret = buxton_wire_get_response(client);
161 int buxton_register_notification(BuxtonClient client,
163 BuxtonCallback callback,
169 _BuxtonKey *k = (_BuxtonKey *)key;
171 if (!k || !k->group.value || !k->name.value ||
172 k->type <= BUXTON_TYPE_MIN || k->type >= BUXTON_TYPE_MAX) {
176 r = buxton_wire_register_notification((_BuxtonClient *)client, k,
183 ret = buxton_wire_get_response(client);
194 int buxton_unregister_notification(BuxtonClient client,
196 BuxtonCallback callback,
202 _BuxtonKey *k = (_BuxtonKey *)key;
204 if (!k || !k->group.value || !k->name.value ||
205 k->type <= BUXTON_TYPE_MIN || k->type >= BUXTON_TYPE_MAX) {
209 r = buxton_wire_unregister_notification((_BuxtonClient *)client, k,
216 ret = buxton_wire_get_response(client);
227 int buxton_set_value(BuxtonClient client,
230 BuxtonCallback callback,
236 _BuxtonKey *k = (_BuxtonKey *)key;
238 if (!k || !k->group.value || !k->name.value || !k->layer.value ||
239 k->type <= BUXTON_TYPE_MIN || k->type >= BUXTON_TYPE_MAX || !value) {
243 r = buxton_wire_set_value((_BuxtonClient *)client, k, value, callback,
250 ret = buxton_wire_get_response(client);
261 int buxton_set_label(BuxtonClient client,
264 BuxtonCallback callback,
271 _BuxtonKey *k = (_BuxtonKey *)key;
273 if (!k || !k->group.value || !k->layer.value || !value) {
278 v = buxton_string_pack(value);
280 r = buxton_wire_set_label((_BuxtonClient *)client, k, &v, callback,
287 ret = buxton_wire_get_response(client);
298 int buxton_create_group(BuxtonClient client,
300 BuxtonCallback callback,
306 _BuxtonKey *k = (_BuxtonKey *)key;
308 /* We require the key name to be NULL, since it is not used for groups */
309 if (!k || !k->group.value || k->name.value || !k->layer.value) {
314 r = buxton_wire_create_group((_BuxtonClient *)client, k, callback, data);
320 ret = buxton_wire_get_response(client);
331 int buxton_remove_group(BuxtonClient client,
333 BuxtonCallback callback,
339 _BuxtonKey *k = (_BuxtonKey *)key;
341 /* We require the key name to be NULL, since it is not used for groups */
342 if (!k || !k->group.value || k->name.value || !k->layer.value) {
347 r = buxton_wire_remove_group((_BuxtonClient *)client, k, callback, data);
353 ret = buxton_wire_get_response(client);
364 int buxton_client_list_keys(BuxtonClient client,
366 BuxtonCallback callback,
378 l = buxton_string_pack(layer_name);
380 r = buxton_wire_list_keys((_BuxtonClient *)client, &l, callback, data);
386 ret = buxton_wire_get_response(client);
397 int buxton_unset_value(BuxtonClient client,
399 BuxtonCallback callback,
405 _BuxtonKey *k = (_BuxtonKey *)key;
407 if (!k || !k->group.value || !k->name.value || !k->layer.value ||
408 k->type <= BUXTON_TYPE_MIN || k->type >= BUXTON_TYPE_MAX) {
412 r = buxton_wire_unset_value((_BuxtonClient *)client, k, callback, data);
418 ret = buxton_wire_get_response(client);
429 BuxtonKey buxton_key_create(char *group, char *name, char *layer,
432 _BuxtonKey *key = NULL;
441 if (type <= BUXTON_TYPE_MIN || type >= BUXTON_TYPE_MAX) {
464 key = malloc0(sizeof(_BuxtonKey));
469 key->group.value = g;
470 key->group.length = (uint32_t)strlen(g) + 1;
473 key->name.length = (uint32_t)strlen(n) + 1;
475 key->name.value = NULL;
476 key->name.length = 0;
479 key->layer.value = l;
480 key->layer.length = (uint32_t)strlen(l) + 1;
482 key->layer.value = NULL;
483 key->layer.length = 0;
487 return (BuxtonKey)key;
496 char *buxton_key_get_group(BuxtonKey key)
498 _BuxtonKey *k = (_BuxtonKey *)key;
507 char *buxton_key_get_name(BuxtonKey key)
509 _BuxtonKey *k = (_BuxtonKey *)key;
518 char *buxton_key_get_layer(BuxtonKey key)
520 _BuxtonKey *k = (_BuxtonKey *)key;
529 BuxtonDataType buxton_key_get_type(BuxtonKey key)
531 _BuxtonKey *k = (_BuxtonKey *)key;
540 void buxton_key_free(BuxtonKey key)
542 _BuxtonKey *k = (_BuxtonKey *)key;
548 free(k->group.value);
550 free(k->layer.value);
554 ssize_t buxton_client_handle_response(BuxtonClient client)
556 return buxton_wire_handle_response((_BuxtonClient *)client);
559 BuxtonControlMessage buxton_response_type(BuxtonResponse response)
561 _BuxtonResponse *r = (_BuxtonResponse *)response;
570 int32_t buxton_response_status(BuxtonResponse response)
573 _BuxtonResponse *r = (_BuxtonResponse *)response;
579 if (buxton_response_type(response) == BUXTON_CONTROL_CHANGED) {
583 d = buxton_array_get(r->data, 0);
586 return d->store.d_int32;
592 BuxtonKey buxton_response_key(BuxtonResponse response)
594 _BuxtonKey *key = NULL;
595 _BuxtonResponse *r = (_BuxtonResponse *)response;
601 if (buxton_response_type(response) == BUXTON_CONTROL_LIST) {
605 key = malloc0(sizeof(_BuxtonKey));
610 if (!buxton_key_copy(r->key, key)) {
615 return (BuxtonKey)key;
618 void *buxton_response_value(BuxtonResponse response)
621 BuxtonData *d = NULL;
622 _BuxtonResponse *r = (_BuxtonResponse *)response;
623 BuxtonControlMessage type;
629 type = buxton_response_type(response);
630 if (type == BUXTON_CONTROL_GET) {
631 d = buxton_array_get(r->data, 1);
632 } else if (type == BUXTON_CONTROL_CHANGED) {
634 d = buxton_array_get(r->data, 0);
646 return strdup(d->store.d_string.value);
648 p = malloc0(sizeof(int32_t));
652 *(int32_t *)p = (int32_t)d->store.d_int32;
655 p = malloc0(sizeof(uint32_t));
659 *(uint32_t *)p = (uint32_t)d->store.d_uint32;
662 p = malloc0(sizeof(int64_t));
666 *(int64_t *)p = (int64_t)d->store.d_int64;
669 p = malloc0(sizeof(uint64_t));
673 *(uint64_t *)p = (uint64_t)d->store.d_uint64;
676 p = malloc0(sizeof(float));
680 *(float *)p = (float)d->store.d_float;
683 p = malloc0(sizeof(double));
687 *(double *)p = (double)d->store.d_double;
690 p = malloc0(sizeof(bool));
694 *(bool *)p = (bool)d->store.d_boolean;
705 * Editor modelines - http://www.wireshark.org/tools/modelines.html
710 * indent-tabs-mode: t
713 * vi: set shiftwidth=8 tabstop=8 noexpandtab:
714 * :indentSize=8:tabSize=8:noTabs=false: