Add message protocol buffer

This commit is contained in:
Ryan Alder 2020-02-17 14:17:23 -08:00
parent 597601e7d0
commit 19985b04c2
4 changed files with 65 additions and 6 deletions

View File

@ -2,16 +2,27 @@
# Single lora testing app # Single lora testing app
CC=g++ CC=g++
CFLAGS=-c -Wall CFLAGS=-c -Wall `pkg-config --cflags 'libprotobuf-c >= 1.0.0'`
LIBS=-lwiringPi LIBS=-lwiringPi -lprotobuf-c
all: dragino_lora_app all: dragino_lora_app
dragino_lora_app: main.o dragino_lora_app: main.o protobuf.o message.pb-c.o
$(CC) main.o $(LIBS) -o dragino_lora_app $(CC) main.o protobuf.o message.pb-c.o $(LIBS) -o dragino_lora_app
main.o: main.c main.o: main.c protobuf.h
$(CC) $(CFLAGS) main.c $(CC) $(CFLAGS) main.c
protobuf.o: message.pb-c.h protobuf.c protobuf.h
$(CC) $(CFLAGS) protobuf.c -o protobuf.o
protobuf.h: message.pb-c.h
message.pb-c.o: message.pb-c.h message.pb-c.c
$(CC) $(CFLAGS) message.pb-c.c -o message.pb-c.o
message.pb-c.h message.pb-c.c: message.proto
protoc-c --c_out=. message.proto
clean: clean:
rm *.o dragino_lora_app rm *.o dragino_lora_app message.pb-c.c message.pb-c.h

12
message.proto Normal file
View File

@ -0,0 +1,12 @@
syntax = "proto2";
package Fenceless;
message Coordinate {
required double x = 1;
required double y = 2;
}
message CollarResponse {
required Coordinate loc = 1;
}

32
protobuf.c Normal file
View File

@ -0,0 +1,32 @@
#include "message.pb-c.h"
#include "protobuf.h"
#include <stdlib.h>
void* _allocator_alloc(void*, size_t count) {
return malloc(count);
}
void _allocator_free(void*, void* data) {
free(data);
}
static ProtobufCAllocator malloc_allocator = {
_allocator_alloc, _allocator_free, NULL
};
void encode_update(double x, double y, uint8_t* buffer) {
Fenceless__Coordinate coord;
Fenceless__CollarResponse response;
fenceless__coordinate__init(&coord);
coord.x = x;
coord.y = y;
fenceless__collar_response__init(&response);
response.loc = &coord;
fenceless__collar_response__pack(&response, buffer);
}
Fenceless__CollarResponse* decode_update(size_t len, uint8_t* buffer) {
return fenceless__collar_response__unpack(&malloc_allocator, len, buffer);
}

4
protobuf.h Normal file
View File

@ -0,0 +1,4 @@
#include "message.pb-c.h"
void encode_update(double x, double y, uint8_t* buffer);
Fenceless__CollarResponse* decode_update(size_t len, uint8_t* buffer);