Start with protobuf integration
This commit is contained in:
parent
813d6c2dbd
commit
fd42eea160
19
Makefile
19
Makefile
@ -1,11 +1,11 @@
|
||||
CC = gcc
|
||||
CXX = g++
|
||||
CFLAGS = -std=c99 -I ./ -I ./include -I /opt/zeromq/include -DUA_NO_AMALGAMATION # put pre-processor settings (-I, -D, etc) here
|
||||
CFLAGS = -std=c99 -I ./ -I ./include -I /opt/zeromq/include -I /opt/protobuf-c/include -DUA_NO_AMALGAMATION # put pre-processor settings (-I, -D, etc) here
|
||||
LDFLAGS = # put linker settings here
|
||||
LIBS = ./lib/libopen62541-static.a
|
||||
|
||||
.PHONY: all
|
||||
all: myServer server_method server_folders myMqttTestModelServer myZeromqTestModelServer myZeromqTemperatureServer myZeromqTemperatureSniffer
|
||||
all: myServer server_method server_folders myMqttTestModelServer myZeromqTestModelServer myZeromqTemperatureServer myZeromqTemperatureSniffer myZeromqProtobufSniffer
|
||||
|
||||
myServer: myServer.o
|
||||
$(CC) -o $@ $(CFLAGS) $(LDFLAGS) $^ $(LIBS)
|
||||
@ -37,10 +37,23 @@ myZeromqTemperatureServer: myZeromqTemperatureServer.o
|
||||
myZeromqTemperatureSniffer: myZeromqTemperatureSniffer.o
|
||||
$(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $^ $(LIBS) /opt/zeromq/lib/libzmq-static.a -lpthread
|
||||
|
||||
processvalue.pb-c.c: processvalue.proto
|
||||
/opt/protobuf-c/bin/protoc-c --c_out=. $^
|
||||
|
||||
processvalue.pb-c.h: processvalue.pb-c.c
|
||||
|
||||
processvalue_pb2.py: processvalue.proto
|
||||
/opt/protobuf/bin/protoc --python_out=. $^
|
||||
|
||||
myZeromqProtobufSniffer.o: myZeromqProtobufSniffer.c processvalue.pb-c.h
|
||||
|
||||
myZeromqProtobufSniffer: myZeromqProtobufSniffer.o processvalue.pb-c.o
|
||||
$(CXX) -o $@ $(CFLAGS) $(LDFLAGS) $^ $(LIBS) /opt/zeromq/lib/libzmq-static.a /opt/protobuf-c/lib/libprotobuf-c.a -lpthread
|
||||
|
||||
.c.o:
|
||||
$(CC) $(CFLAGS) -c $<
|
||||
|
||||
.PHONY: clean
|
||||
clean:
|
||||
-rm -f *.o myServer server_method server_folders myMqttTestModelServer myZeromqTestModelServer myZeromqTemperatureServer myZeromqTemperatureSniffer
|
||||
-rm -f *.o myServer server_method server_folders myMqttTestModelServer myZeromqTestModelServer myZeromqTemperatureServer myZeromqTemperatureSniffer myZeromqProtobufSniffer
|
||||
|
||||
|
46
myZeromqProtobufSniffer.c
Normal file
46
myZeromqProtobufSniffer.c
Normal file
@ -0,0 +1,46 @@
|
||||
#include <stdlib.h>
|
||||
#include <assert.h>
|
||||
#include <signal.h>
|
||||
#include <zmq.h>
|
||||
|
||||
#include "processvalue.pb-c.h"
|
||||
|
||||
|
||||
#define BUFSIZE 1024
|
||||
|
||||
int running = 1;
|
||||
static void stopHandler(int sign) {
|
||||
printf("received ctrl-c\n");
|
||||
running = 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
signal(SIGINT, stopHandler); /* catches ctrl-c */
|
||||
|
||||
/* prepare zmq context, connect to server and subscribe with empty filter */
|
||||
void *zmqContext = zmq_ctx_new();
|
||||
void *zmqSubscriber = zmq_socket(zmqContext, ZMQ_SUB);
|
||||
int rc = zmq_connect(zmqSubscriber, "tcp://127.0.0.1:12346");
|
||||
assert(rc == 0);
|
||||
rc = zmq_setsockopt(zmqSubscriber, ZMQ_SUBSCRIBE, 0, 0);
|
||||
assert(rc == 0);
|
||||
|
||||
while (running) {
|
||||
char buf[BUFSIZE];
|
||||
int rcLen = zmq_recv(zmqSubscriber, buf, BUFSIZE, 0);
|
||||
assert(rcLen != -1);
|
||||
|
||||
Krohne__ProcessValue *processValue = krohne__process_value__unpack(NULL, rcLen, buf);
|
||||
assert(processValue != NULL);
|
||||
printf("value: %d\n", processValue->value);
|
||||
printf("unit: %i\n", processValue->unit);
|
||||
printf("status: %i\n", processValue->status);
|
||||
krohne__process_value__free_unpacked(processValue, NULL);
|
||||
}
|
||||
|
||||
printf("Terminating ...\n");
|
||||
zmq_close(zmqSubscriber);
|
||||
zmq_ctx_destroy(zmqContext);
|
||||
}
|
10
myZeromqTemperatureSender.py
Normal file
10
myZeromqTemperatureSender.py
Normal file
@ -0,0 +1,10 @@
|
||||
import zmq
|
||||
import time
|
||||
|
||||
context = zmq.Context()
|
||||
publisher = context.socket(zmq.PUB)
|
||||
publisher.bind("tcp://127.0.0.1:12346")
|
||||
|
||||
while True:
|
||||
publisher.send("abc")
|
||||
time.sleep(5)
|
144
processvalue.pb-c.c
Normal file
144
processvalue.pb-c.c
Normal file
@ -0,0 +1,144 @@
|
||||
/* Generated by the protocol buffer compiler. DO NOT EDIT! */
|
||||
/* Generated from: processvalue.proto */
|
||||
|
||||
/* Do not generate deprecated warnings for self */
|
||||
#ifndef PROTOBUF_C__NO_DEPRECATED
|
||||
#define PROTOBUF_C__NO_DEPRECATED
|
||||
#endif
|
||||
|
||||
#include "processvalue.pb-c.h"
|
||||
void krohne__process_value__init
|
||||
(Krohne__ProcessValue *message)
|
||||
{
|
||||
static Krohne__ProcessValue init_value = KROHNE__PROCESS_VALUE__INIT;
|
||||
*message = init_value;
|
||||
}
|
||||
size_t krohne__process_value__get_packed_size
|
||||
(const Krohne__ProcessValue *message)
|
||||
{
|
||||
assert(message->base.descriptor == &krohne__process_value__descriptor);
|
||||
return protobuf_c_message_get_packed_size ((const ProtobufCMessage*)(message));
|
||||
}
|
||||
size_t krohne__process_value__pack
|
||||
(const Krohne__ProcessValue *message,
|
||||
uint8_t *out)
|
||||
{
|
||||
assert(message->base.descriptor == &krohne__process_value__descriptor);
|
||||
return protobuf_c_message_pack ((const ProtobufCMessage*)message, out);
|
||||
}
|
||||
size_t krohne__process_value__pack_to_buffer
|
||||
(const Krohne__ProcessValue *message,
|
||||
ProtobufCBuffer *buffer)
|
||||
{
|
||||
assert(message->base.descriptor == &krohne__process_value__descriptor);
|
||||
return protobuf_c_message_pack_to_buffer ((const ProtobufCMessage*)message, buffer);
|
||||
}
|
||||
Krohne__ProcessValue *
|
||||
krohne__process_value__unpack
|
||||
(ProtobufCAllocator *allocator,
|
||||
size_t len,
|
||||
const uint8_t *data)
|
||||
{
|
||||
return (Krohne__ProcessValue *)
|
||||
protobuf_c_message_unpack (&krohne__process_value__descriptor,
|
||||
allocator, len, data);
|
||||
}
|
||||
void krohne__process_value__free_unpacked
|
||||
(Krohne__ProcessValue *message,
|
||||
ProtobufCAllocator *allocator)
|
||||
{
|
||||
assert(message->base.descriptor == &krohne__process_value__descriptor);
|
||||
protobuf_c_message_free_unpacked ((ProtobufCMessage*)message, allocator);
|
||||
}
|
||||
static const ProtobufCEnumValue krohne__process_value__status_type__enum_values_by_number[2] =
|
||||
{
|
||||
{ "valid", "KROHNE__PROCESS_VALUE__STATUS_TYPE__valid", 0 },
|
||||
{ "invalid", "KROHNE__PROCESS_VALUE__STATUS_TYPE__invalid", 1 },
|
||||
};
|
||||
static const ProtobufCIntRange krohne__process_value__status_type__value_ranges[] = {
|
||||
{0, 0},{0, 2}
|
||||
};
|
||||
static const ProtobufCEnumValueIndex krohne__process_value__status_type__enum_values_by_name[2] =
|
||||
{
|
||||
{ "invalid", 1 },
|
||||
{ "valid", 0 },
|
||||
};
|
||||
const ProtobufCEnumDescriptor krohne__process_value__status_type__descriptor =
|
||||
{
|
||||
PROTOBUF_C__ENUM_DESCRIPTOR_MAGIC,
|
||||
"Krohne.processValue.statusType",
|
||||
"statusType",
|
||||
"Krohne__ProcessValue__StatusType",
|
||||
"Krohne",
|
||||
2,
|
||||
krohne__process_value__status_type__enum_values_by_number,
|
||||
2,
|
||||
krohne__process_value__status_type__enum_values_by_name,
|
||||
1,
|
||||
krohne__process_value__status_type__value_ranges,
|
||||
NULL,NULL,NULL,NULL /* reserved[1234] */
|
||||
};
|
||||
static const ProtobufCFieldDescriptor krohne__process_value__field_descriptors[3] =
|
||||
{
|
||||
{
|
||||
"value",
|
||||
1,
|
||||
PROTOBUF_C_LABEL_REQUIRED,
|
||||
PROTOBUF_C_TYPE_DOUBLE,
|
||||
0, /* quantifier_offset */
|
||||
offsetof(Krohne__ProcessValue, value),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"unit",
|
||||
2,
|
||||
PROTOBUF_C_LABEL_REQUIRED,
|
||||
PROTOBUF_C_TYPE_INT32,
|
||||
0, /* quantifier_offset */
|
||||
offsetof(Krohne__ProcessValue, unit),
|
||||
NULL,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
{
|
||||
"status",
|
||||
3,
|
||||
PROTOBUF_C_LABEL_REQUIRED,
|
||||
PROTOBUF_C_TYPE_ENUM,
|
||||
0, /* quantifier_offset */
|
||||
offsetof(Krohne__ProcessValue, status),
|
||||
&krohne__process_value__status_type__descriptor,
|
||||
NULL,
|
||||
0, /* flags */
|
||||
0,NULL,NULL /* reserved1,reserved2, etc */
|
||||
},
|
||||
};
|
||||
static const unsigned krohne__process_value__field_indices_by_name[] = {
|
||||
2, /* field[2] = status */
|
||||
1, /* field[1] = unit */
|
||||
0, /* field[0] = value */
|
||||
};
|
||||
static const ProtobufCIntRange krohne__process_value__number_ranges[1 + 1] =
|
||||
{
|
||||
{ 1, 0 },
|
||||
{ 0, 3 }
|
||||
};
|
||||
const ProtobufCMessageDescriptor krohne__process_value__descriptor =
|
||||
{
|
||||
PROTOBUF_C__MESSAGE_DESCRIPTOR_MAGIC,
|
||||
"Krohne.processValue",
|
||||
"ProcessValue",
|
||||
"Krohne__ProcessValue",
|
||||
"Krohne",
|
||||
sizeof(Krohne__ProcessValue),
|
||||
3,
|
||||
krohne__process_value__field_descriptors,
|
||||
krohne__process_value__field_indices_by_name,
|
||||
1, krohne__process_value__number_ranges,
|
||||
(ProtobufCMessageInit) krohne__process_value__init,
|
||||
NULL,NULL,NULL /* reserved[123] */
|
||||
};
|
79
processvalue.pb-c.h
Normal file
79
processvalue.pb-c.h
Normal file
@ -0,0 +1,79 @@
|
||||
/* Generated by the protocol buffer compiler. DO NOT EDIT! */
|
||||
/* Generated from: processvalue.proto */
|
||||
|
||||
#ifndef PROTOBUF_C_processvalue_2eproto__INCLUDED
|
||||
#define PROTOBUF_C_processvalue_2eproto__INCLUDED
|
||||
|
||||
#include <protobuf-c/protobuf-c.h>
|
||||
|
||||
PROTOBUF_C__BEGIN_DECLS
|
||||
|
||||
#if PROTOBUF_C_VERSION_NUMBER < 1000000
|
||||
# error This file was generated by a newer version of protoc-c which is incompatible with your libprotobuf-c headers. Please update your headers.
|
||||
#elif 1002001 < PROTOBUF_C_MIN_COMPILER_VERSION
|
||||
# error This file was generated by an older version of protoc-c which is incompatible with your libprotobuf-c headers. Please regenerate this file with a newer version of protoc-c.
|
||||
#endif
|
||||
|
||||
|
||||
typedef struct _Krohne__ProcessValue Krohne__ProcessValue;
|
||||
|
||||
|
||||
/* --- enums --- */
|
||||
|
||||
typedef enum _Krohne__ProcessValue__StatusType {
|
||||
KROHNE__PROCESS_VALUE__STATUS_TYPE__valid = 0,
|
||||
KROHNE__PROCESS_VALUE__STATUS_TYPE__invalid = 1
|
||||
PROTOBUF_C__FORCE_ENUM_TO_BE_INT_SIZE(KROHNE__PROCESS_VALUE__STATUS_TYPE)
|
||||
} Krohne__ProcessValue__StatusType;
|
||||
|
||||
/* --- messages --- */
|
||||
|
||||
struct _Krohne__ProcessValue
|
||||
{
|
||||
ProtobufCMessage base;
|
||||
double value;
|
||||
int32_t unit;
|
||||
Krohne__ProcessValue__StatusType status;
|
||||
};
|
||||
#define KROHNE__PROCESS_VALUE__INIT \
|
||||
{ PROTOBUF_C_MESSAGE_INIT (&krohne__process_value__descriptor) \
|
||||
, 0, 0, 0 }
|
||||
|
||||
|
||||
/* Krohne__ProcessValue methods */
|
||||
void krohne__process_value__init
|
||||
(Krohne__ProcessValue *message);
|
||||
size_t krohne__process_value__get_packed_size
|
||||
(const Krohne__ProcessValue *message);
|
||||
size_t krohne__process_value__pack
|
||||
(const Krohne__ProcessValue *message,
|
||||
uint8_t *out);
|
||||
size_t krohne__process_value__pack_to_buffer
|
||||
(const Krohne__ProcessValue *message,
|
||||
ProtobufCBuffer *buffer);
|
||||
Krohne__ProcessValue *
|
||||
krohne__process_value__unpack
|
||||
(ProtobufCAllocator *allocator,
|
||||
size_t len,
|
||||
const uint8_t *data);
|
||||
void krohne__process_value__free_unpacked
|
||||
(Krohne__ProcessValue *message,
|
||||
ProtobufCAllocator *allocator);
|
||||
/* --- per-message closures --- */
|
||||
|
||||
typedef void (*Krohne__ProcessValue_Closure)
|
||||
(const Krohne__ProcessValue *message,
|
||||
void *closure_data);
|
||||
|
||||
/* --- services --- */
|
||||
|
||||
|
||||
/* --- descriptors --- */
|
||||
|
||||
extern const ProtobufCMessageDescriptor krohne__process_value__descriptor;
|
||||
extern const ProtobufCEnumDescriptor krohne__process_value__status_type__descriptor;
|
||||
|
||||
PROTOBUF_C__END_DECLS
|
||||
|
||||
|
||||
#endif /* PROTOBUF_C_processvalue_2eproto__INCLUDED */
|
13
processvalue.proto
Normal file
13
processvalue.proto
Normal file
@ -0,0 +1,13 @@
|
||||
package Krohne;
|
||||
|
||||
message processValue {
|
||||
required double value = 1;
|
||||
required int32 unit = 2;
|
||||
|
||||
enum statusType {
|
||||
valid = 0;
|
||||
invalid = 1;
|
||||
}
|
||||
|
||||
required statusType status = 3;
|
||||
}
|
106
processvalue_pb2.py
Normal file
106
processvalue_pb2.py
Normal file
@ -0,0 +1,106 @@
|
||||
# Generated by the protocol buffer compiler. DO NOT EDIT!
|
||||
# source: processvalue.proto
|
||||
|
||||
import sys
|
||||
_b=sys.version_info[0]<3 and (lambda x:x) or (lambda x:x.encode('latin1'))
|
||||
from google.protobuf import descriptor as _descriptor
|
||||
from google.protobuf import message as _message
|
||||
from google.protobuf import reflection as _reflection
|
||||
from google.protobuf import symbol_database as _symbol_database
|
||||
from google.protobuf import descriptor_pb2
|
||||
# @@protoc_insertion_point(imports)
|
||||
|
||||
_sym_db = _symbol_database.Default()
|
||||
|
||||
|
||||
|
||||
|
||||
DESCRIPTOR = _descriptor.FileDescriptor(
|
||||
name='processvalue.proto',
|
||||
package='Krohne',
|
||||
serialized_pb=_b('\n\x12processvalue.proto\x12\x06Krohne\"\x82\x01\n\x0cprocessValue\x12\r\n\x05value\x18\x01 \x02(\x01\x12\x0c\n\x04unit\x18\x02 \x02(\x05\x12/\n\x06status\x18\x03 \x02(\x0e\x32\x1f.Krohne.processValue.statusType\"$\n\nstatusType\x12\t\n\x05valid\x10\x00\x12\x0b\n\x07invalid\x10\x01')
|
||||
)
|
||||
_sym_db.RegisterFileDescriptor(DESCRIPTOR)
|
||||
|
||||
|
||||
|
||||
_PROCESSVALUE_STATUSTYPE = _descriptor.EnumDescriptor(
|
||||
name='statusType',
|
||||
full_name='Krohne.processValue.statusType',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
values=[
|
||||
_descriptor.EnumValueDescriptor(
|
||||
name='valid', index=0, number=0,
|
||||
options=None,
|
||||
type=None),
|
||||
_descriptor.EnumValueDescriptor(
|
||||
name='invalid', index=1, number=1,
|
||||
options=None,
|
||||
type=None),
|
||||
],
|
||||
containing_type=None,
|
||||
options=None,
|
||||
serialized_start=125,
|
||||
serialized_end=161,
|
||||
)
|
||||
_sym_db.RegisterEnumDescriptor(_PROCESSVALUE_STATUSTYPE)
|
||||
|
||||
|
||||
_PROCESSVALUE = _descriptor.Descriptor(
|
||||
name='processValue',
|
||||
full_name='Krohne.processValue',
|
||||
filename=None,
|
||||
file=DESCRIPTOR,
|
||||
containing_type=None,
|
||||
fields=[
|
||||
_descriptor.FieldDescriptor(
|
||||
name='value', full_name='Krohne.processValue.value', index=0,
|
||||
number=1, type=1, cpp_type=5, label=2,
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='unit', full_name='Krohne.processValue.unit', index=1,
|
||||
number=2, type=5, cpp_type=1, label=2,
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
_descriptor.FieldDescriptor(
|
||||
name='status', full_name='Krohne.processValue.status', index=2,
|
||||
number=3, type=14, cpp_type=8, label=2,
|
||||
has_default_value=False, default_value=0,
|
||||
message_type=None, enum_type=None, containing_type=None,
|
||||
is_extension=False, extension_scope=None,
|
||||
options=None),
|
||||
],
|
||||
extensions=[
|
||||
],
|
||||
nested_types=[],
|
||||
enum_types=[
|
||||
_PROCESSVALUE_STATUSTYPE,
|
||||
],
|
||||
options=None,
|
||||
is_extendable=False,
|
||||
extension_ranges=[],
|
||||
oneofs=[
|
||||
],
|
||||
serialized_start=31,
|
||||
serialized_end=161,
|
||||
)
|
||||
|
||||
_PROCESSVALUE.fields_by_name['status'].enum_type = _PROCESSVALUE_STATUSTYPE
|
||||
_PROCESSVALUE_STATUSTYPE.containing_type = _PROCESSVALUE
|
||||
DESCRIPTOR.message_types_by_name['processValue'] = _PROCESSVALUE
|
||||
|
||||
processValue = _reflection.GeneratedProtocolMessageType('processValue', (_message.Message,), dict(
|
||||
DESCRIPTOR = _PROCESSVALUE,
|
||||
__module__ = 'processvalue_pb2'
|
||||
# @@protoc_insertion_point(class_scope:Krohne.processValue)
|
||||
))
|
||||
_sym_db.RegisterMessage(processValue)
|
||||
|
||||
|
||||
# @@protoc_insertion_point(module_scope)
|
Loading…
x
Reference in New Issue
Block a user