Commit 4e9e5982 authored by Lucas Russo's avatar Lucas Russo

libclient: add libclient

This is a first version of the BPM Client API
that is used to communicate to the BPM server
parent 946b77d0
# Set your cross compile prefix with CROSS_COMPILE variable
CROSS_COMPILE ?=
CMDSEP = ;
CC = $(CROSS_COMPILE)gcc
AR = $(CROSS_COMPILE)ar
LD = $(CROSS_COMPILE)ld
OBJDUMP = $(CROSS_COMPILE)objdump
OBJCOPY = $(CROSS_COMPILE)objcopy
SIZE = $(CROSS_COMPILE)size
MAKE = make
INSTALL_DIR ?= /usr/local/lib
export INSTALL_DIR
# Config variables suitable for creating shared libraries
LIB_VER = 1.0.0
# General C flags
CFLAGS = -std=gnu99 -O2
LOCAL_MSG_DBG ?= n
DBE_DBG ?= n
CFLAGS_DEBUG =
ifeq ($(LOCAL_MSG_DBG),y)
CFLAGS_DEBUG += -DLOCAL_MSG_DBG=1
endif
ifeq ($(DBE_DBG),y)
CFLAGS_DEBUG += -DDBE_DBG=1
endif
# Debug flags -D<flasg_name>=<value>
CFLAGS_DEBUG += -g
# Specific platform Flags
CFLAGS_PLATFORM = -Wall -Wextra -Werror
LDFLAGS_PLATFORM =
# Libraries
LIBS = -lzmq -lczmq -lmdp
# General library flags -L<libdir>
LFLAGS =
# Specific platform objects
OBJS_PLATFORM =
# Library objects
libbpmclient_OBJS_LIB = bpm_client.o bpm_client_codes.o
# Include directories
INCLUDE_DIRS = -I.
# Merge all flags. Optimize for size (-Os)
CFLAGS += $(CFLAGS_PLATFORM) $(CFLAGS_DEBUG)
LDFLAGS = $(LDFLAGS_PLATFORM)
# Output library names
LIBCLIENT = libbpmclient
OUT = $(LIBCLIENT)
.SECONDEXPANSION:
# Objects common for both server and client libraries
common_OBJS = $(OBJS_PLATFORM) $(OBJS_BOARD)
# Objects for each version of library
$(LIBCLIENT)_OBJS = $(common_OBJS) $($(LIBCLIENT)_OBJS_LIB)
$(LIBCLIENT)_HEADERS = $($(LIBCLIENT)_OBJS_LIB:.o=.h)
# Save a git repository description
REVISION = $(shell git describe --dirty --always)
REVISION_NAME = revision
OBJ_REVISION = $(addsuffix .o, $(REVISION_NAME))
OBJS_all = $(common_OBJS) $($(LIBCLIENT)_OBJS_LIB) \
$(OBJ_REVISION)
# Libraries suffixes
LIB_STATIC_SUFFIX = .a
LIB_SHARED_SUFFIX = .so
# Generate suitable names for static libraries
TARGET_STATIC = $(addsuffix $(LIB_STATIC_SUFFIX), $(OUT))
TARGET_SHARED = $(addsuffix $(LIB_SHARED_SUFFIX), $(OUT))
TARGET_SHARED_VER = $(addsuffix $(LIB_SHARED_SUFFIX).$(LIB_VER), $(OUT))
.PHONY: all clean mrproper install uninstall
# Avoid deletion of intermediate files, such as objects
.SECONDARY: $(OBJS_all)
# Makefile rules
all: $(TARGET_STATIC) $(TARGET_SHARED_VER)
# Compile static library
%.a: $$($$*_OBJS) $(OBJ_REVISION)
$(AR) rcs $@ $?
# Compile dynamic library
%.so.$(LIB_VER): $$($$*_OBJS) $(OBJ_REVISION)
$(CC) -shared -fPIC -Wl,-soname,$@ -o $@ $? $(LDFLAGS)
$(REVISION_NAME).o: $(REVISION_NAME).c
$(CC) $(CFLAGS) -DGIT_REVISION=\"$(REVISION)\" -c $<
# Pull in dependency info for *existing* .o files and don't complain if the
# corresponding .d file is not found
-include $(OBJS_all:.o=.d)
# Compile with position-independent objects.
# Autodependencies generatation by Scott McPeak, November 2001,
# from article "Autodependencies with GNU make"
%.o: %.c
$(CC) $(CFLAGS) $(INCLUDE_DIRS) -c -fPIC $*.c -o $@
# create the dependency files "target: pre-requisites"
${CC} -MM $(CFLAGS) $*.c > $*.d
# Workaround to make objects in different folders have
# the correct target path. e.g., "dir/bar.o: dir/bar.c dir/foo.h"
# instead of "bar.o: dir/bar.c dir/foo.h"
@mv -f $*.d $*.d.tmp
@sed -e 's|.*:|$*.o:|' < $*.d.tmp > $*.d
# All prereqs listed will also become command-less,
# prereq-less targets. In this way, the prereq file will be
# treated as changed and the target will be rebuilt
# sed: strip the target (everything before colon)
# sed: remove any continuation backslashes
# fmt -1: list words one per line
# sed: strip leading spaces
# sed: add trailing colons
@sed -e 's/.*://' -e 's/\\$$//' < $*.d.tmp | fmt -1 | \
sed -e 's/^ *//' -e 's/$$/:/' >> $*.d
@rm -f $*.d.tmp
#tests:
# $(MAKE) -C $@ all
install:
$(foreach lib,$(TARGET_SHARED_VER),install -m 755 $(lib) $(INSTALL_DIR) $(CMDSEP))
$(foreach lib,$(TARGET_SHARED),ln -sf $(lib).$(LIB_VER) $(INSTALL_DIR)/$(lib) $(CMDSEP))
$(foreach lib,$(TARGET_STATIC),install -m 755 $(lib) $(INSTALL_DIR) $(CMDSEP))
$(foreach header,$($(LIBCLIENT)_HEADERS),install -m 755 $(header) $(INSTALL_DIR) $(CMDSEP))
uninstall:
$(foreach lib,$(TARGET_SHARED),rm -f $(INSTALL_DIR)/$(lib).$(LIB_VER) $(CMDSEP))
$(foreach lib,$(TARGET_SHARED),rm -f $(INSTALL_DIR)/$(lib) $(CMDSEP))
$(foreach lib,$(TARGET_STATIC),rm -f $(INSTALL_DIR)/$(lib) $(CMDSEP))
$(foreach header,$($(LIBCLIENT)_HEADERS),rm -f $(INSTALL_DIR)/$(header) $(CMDSEP))
clean:
rm -f $(OBJS_all) $(OBJS_all:.o=.d)
mrproper: clean
rm -f *.a *.so.$(LIB_VER)
/*
* Copyright (C) 2014 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU LGPL, version 3 or any later version.
*/
#include "bpm_client.h"
#include "bpm_client_codes.h"
/* Our structure */
struct _bpm_client_t {
mdp_client_t *mdp_client;
};
/* Our API */
bpm_client_t *bpm_client_new (char *broker_endp, int verbose)
{
bpm_client_t *self = zmalloc (sizeof *self);
self->mdp_client = mdp_client_new (broker_endp, verbose);
return self;
}
void bpm_client_destroy (bpm_client_t **self_p)
{
assert (self_p);
if (*self_p) {
bpm_client_t *self = *self_p;
mdp_client_destroy (&self->mdp_client);
*self_p = NULL;
}
}
bpm_cli_recode_t bpm_blink_leds (bpm_client_t *self, char *service, uint32_t leds)
{
uint32_t operation = BPM_CLI_OPCODE_LEDS;
zmsg_t *request = zmsg_new ();
zmsg_addmem (request, &operation, sizeof (operation));
zmsg_addmem (request, &leds, sizeof (leds));
mdp_client_send (self->mdp_client, service, &request);
return BPM_CLI_RECODE_OK;
}
/*
* Copyright (C) 2014 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU LGPL, version 3 or any later version.
*/
#ifndef _BPM_CLIENT_H_
#define _BPM_CLIENT_H_
#include <inttypes.h>
#include <mdp.h>
#include "bpm_client_codes.h"
typedef struct _bpm_client_t bpm_client_t;
/* Our API */
/* Create an instance of the BPM client. This must be called
* before any operation involving communicating with the BPM
* server. Return an instance of the bpm client */
bpm_client_t *bpm_client_new (char *broker_endp, int verbose);
/* Destroy an instance of the BPM client. This must be called
* after all operations involving the communication with the BPM
* server */
void bpm_client_destroy (bpm_client_t **self_p);
/* Blink the FMC Leds. This is only used for debug and for demostration
* purposes. Return BPM_CLI_RECODE_OK if ok and BPM_CLI_RECODE_ERR if
* error */
bpm_cli_recode_t bpm_blink_leds (bpm_client_t *self, char *service, uint32_t leds);
#endif
/*
* Copyright (C) 2014 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU LGPL, version 3 or any later version.
*/
#include "bpm_client_codes.h"
const BPM_CLI_OPCODE_TYPE cli_opcodes [BPM_CLI_OPCODE_END] = {
[0] = BPM_CLI_OPCODE_LEDS,
};
const BPM_CLI_RECODE_TYPE cli_recodes [BPM_CLI_RECODE_END] = {
[0] = BPM_CLI_RECODE_OK,
[1] = BPM_CLI_RECODE_ERR
};
/*
* Copyright (C) 2014 LNLS (www.lnls.br)
* Author: Lucas Russo <lucas.russo@lnls.br>
*
* Released according to the GNU LGPL, version 3 or any later version.
*/
#ifndef _BPM_CLIENT_CODES_H_
#define _BPM_CLIENT_CODES_H_
#include <inttypes.h>
/* Messaging OPCODES */
#define BPM_CLI_OPCODE_LEDS_SIZE (sizeof (uint32_t))
#define BPM_CLI_OPCODE_TYPE uint32_t
#define BPM_CLI_OPCODE_LEDS 0
#define BPM_CLI_OPCODE_END 1
typedef BPM_CLI_OPCODE_TYPE bpm_cli_opcode_t;
extern const bpm_cli_opcode_t bpm_cli_opcodes [BPM_CLI_OPCODE_END];
/* Messaging Reply CODES */
#define BPM_CLI_RECODE_SIZE (sizeof(uint32_t))
#define BPM_CLI_RECODE_TYPE uint32_t
#define BPM_CLI_RECODE_OK 0
#define BPM_CLI_RECODE_ERR 1
#define BPM_CLI_RECODE_END 2
typedef BPM_CLI_RECODE_TYPE bpm_cli_recode_t;
extern const bpm_cli_recode_t bpm_cli_recodes [BPM_CLI_RECODE_END];
#endif
const char *build_revision = GIT_REVISION;
const char *build_date = __DATE__ " " __TIME__;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment