From 27dcf7b10e139f0e145368ac28191c5aa3bcdc07 Mon Sep 17 00:00:00 2001 From: Alexey Andreyev Date: Tue, 26 May 2015 21:47:05 +0300 Subject: [PATCH 1/7] restructuring project dirs and files structuring project files and dirs --- .gitignore | 3 + Makefile | 45 +++++---- README.md | 5 +- coap.c => libraries/microcoap/microcoap.c | 2 +- coap.h => libraries/microcoap/microcoap.h | 6 +- microcoap-example/connections.h | 12 +++ endpoints.c => microcoap-example/endpoints.c | 10 +- microcoap-example/endpoints.h | 15 +++ microcoap-example/microcoap-example.ino | 100 +++++++++++++++++++ 9 files changed, 170 insertions(+), 28 deletions(-) rename coap.c => libraries/microcoap/microcoap.c (99%) rename coap.h => libraries/microcoap/microcoap.h (98%) create mode 100644 microcoap-example/connections.h rename endpoints.c => microcoap-example/endpoints.c (95%) create mode 100644 microcoap-example/endpoints.h create mode 100644 microcoap-example/microcoap-example.ino diff --git a/.gitignore b/.gitignore index 12c2f74..26064bc 100644 --- a/.gitignore +++ b/.gitignore @@ -33,3 +33,6 @@ coap *.i*86 *.x86_64 *.hex + +# OS system files +.directory \ No newline at end of file diff --git a/Makefile b/Makefile index 19175d2..09ba2f4 100644 --- a/Makefile +++ b/Makefile @@ -1,22 +1,33 @@ -CFLAGS += -Wall -DDEBUG +# make -> compile microcoap app +# make clean -> remove the app file and all object files (.o) +# make all -> clean and compile +APPNAME = microcoap +SRC = main-posix.c \ + libraries/microcoap/microcoap.c \ + microcoap-example/endpoints.c +# compilation options +CFLAGS = -Wall -DDEBUG +INC_DIR = libraries/microcoap \ + microcoap-example # -DIPV6 -SRC = $(wildcard *.c) -OBJ = $(SRC:%.c=%.o) -DEPS = $(SRC:%.c=%.d) -EXEC = coap +# linking options +# LDFLAGS = -shared -Wl,-soname,$(APPNAME) -all: $(EXEC) +# how to compile individual object files +OBJS = $(SRC:.c=.o) +.c.o: + $(CC) $(CFLAGS) -I$(INC_DIR) -c $< -o $@ --include $(DEPS) - -$(EXEC): $(OBJ) - @$(CC) $(CFLAGS) -o $@ $^ - -%.o: %.c %.d - @$(CC) -c $(CFLAGS) -o $@ $< - -%.d: %.c - @$(CC) -MM $(CFLAGS) $< > $@ +.PHONY: all clean +# library compilation +$(APPNAME): $(OBJS) $(SRC) + $(CC) $(OBJS) -o $(APPNAME) +#$(LDFLAGS) + +# cleaning rule clean: - @$(RM) $(EXEC) $(OBJ) $(DEPS) + rm -f $(OBJS) $(APPNAME) *~ + +# additional rule +all: clean $(APPNAME) \ No newline at end of file diff --git a/README.md b/README.md index 12c5f6b..6477aa4 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,12 @@ Endpoint handlers are defined in endpoints.c For linux/OSX make - ./coap + ./micocoap For Arduino - open microcoap.ino + copy libraries and microcoap-example directories to the Arduino IDE sketches folder + open: File -> sketches folder -> microcoap-example To test, use libcoap diff --git a/coap.c b/libraries/microcoap/microcoap.c similarity index 99% rename from coap.c rename to libraries/microcoap/microcoap.c index 4d036f5..4788602 100644 --- a/coap.c +++ b/libraries/microcoap/microcoap.c @@ -4,7 +4,7 @@ #include #include #include -#include "coap.h" +#include "microcoap.h" extern void endpoint_setup(void); extern const coap_endpoint_t endpoints[]; diff --git a/coap.h b/libraries/microcoap/microcoap.h similarity index 98% rename from coap.h rename to libraries/microcoap/microcoap.h index 9090285..288aea3 100644 --- a/coap.h +++ b/libraries/microcoap/microcoap.h @@ -1,8 +1,8 @@ -#ifndef COAP_H -#define COAP_H 1 +#ifndef MICROCOAP_H +#define MICROCOAP_H 1 #ifdef __cplusplus -extern "C" { +extern "C" { #endif #include diff --git a/microcoap-example/connections.h b/microcoap-example/connections.h new file mode 100644 index 0000000..908eeea --- /dev/null +++ b/microcoap-example/connections.h @@ -0,0 +1,12 @@ +#ifndef CONNECTIONS_H +#define CONNECTIONS 1 +#ifdef __cplusplus +extern "C" { +#endif + +#define LED 13 + +#ifdef __cplusplus +} +#endif +#endif // CONNECTIONS \ No newline at end of file diff --git a/endpoints.c b/microcoap-example/endpoints.c similarity index 95% rename from endpoints.c rename to microcoap-example/endpoints.c index ccc961b..775810e 100644 --- a/endpoints.c +++ b/microcoap-example/endpoints.c @@ -1,6 +1,7 @@ #include #include -#include "coap.h" +#include "microcoap.h" +#include "connections.h" static char light = '0'; @@ -10,10 +11,9 @@ void build_rsp(void); #ifdef ARDUINO #include "Arduino.h" -static int led = 6; void endpoint_setup(void) { - pinMode(led, OUTPUT); + pinMode(LED, OUTPUT); build_rsp(); } #else @@ -44,7 +44,7 @@ static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpk { light = '1'; #ifdef ARDUINO - digitalWrite(led, HIGH); + digitalWrite(LED, HIGH); #else printf("ON\n"); #endif @@ -54,7 +54,7 @@ static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpk { light = '0'; #ifdef ARDUINO - digitalWrite(led, LOW); + digitalWrite(LED, LOW); #else printf("OFF\n"); #endif diff --git a/microcoap-example/endpoints.h b/microcoap-example/endpoints.h new file mode 100644 index 0000000..323e01f --- /dev/null +++ b/microcoap-example/endpoints.h @@ -0,0 +1,15 @@ +#ifndef ENDPOINTS_H +#define ENDPOINTS_H 1 +#ifdef __cplusplus +extern "C" { +#endif + +#include "microcoap.h" + +static int handle_get_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo); +static int handle_put_light(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo); + +#ifdef __cplusplus +} +#endif +#endif // ENDPOINTS_H diff --git a/microcoap-example/microcoap-example.ino b/microcoap-example/microcoap-example.ino new file mode 100644 index 0000000..6b9cf5c --- /dev/null +++ b/microcoap-example/microcoap-example.ino @@ -0,0 +1,100 @@ +/* +* WARNING - UDP_TX_PACKET_MAX_SIZE is hardcoded by Arduino to 24 bytes +* This limits the size of possible outbound UDP packets +*/ + +#include +#include +#include +#include +#include "endpoints.h" +#include "microcoap.h" + +#define PORT 5683 +static uint8_t mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; + +EthernetClient client; +EthernetUDP udp; +uint8_t packetbuf[256]; +static uint8_t scratch_raw[32]; +static coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)}; + +void setup() +{ + int i; + Serial.begin(9600); + while (!Serial) + { + ; // wait for serial port to connect. Needed for Leonardo only + } + + // start the Ethernet connection: + if (Ethernet.begin(mac) == 0) + { + Serial.println("Failed to configure Ethernet using DHCP"); + while(1); + } + Serial.print("My IP address: "); + for (i=0;i<4;i++) + { + Serial.print(Ethernet.localIP()[i], DEC); + Serial.print("."); + } + Serial.println(); + udp.begin(PORT); + + coap_setup(); + endpoint_setup(); +} + +void udp_send(const uint8_t *buf, int buflen) +{ + udp.beginPacket(udp.remoteIP(), udp.remotePort()); + while(buflen--) + udp.write(*buf++); + udp.endPacket(); +} + +void loop() +{ + int sz; + int rc; + coap_packet_t pkt; + int i; + + if ((sz = udp.parsePacket()) > 0) + { + udp.read(packetbuf, sizeof(packetbuf)); + + for (i=0;i -#include -#include -#include -#include "coap.h" - -#define PORT 5683 -static uint8_t mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; - -EthernetClient client; -EthernetUDP udp; -uint8_t packetbuf[256]; -static uint8_t scratch_raw[32]; -static coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)}; - -void setup() -{ - int i; - Serial.begin(9600); - while (!Serial) - { - ; // wait for serial port to connect. Needed for Leonardo only - } - - // start the Ethernet connection: - if (Ethernet.begin(mac) == 0) - { - Serial.println("Failed to configure Ethernet using DHCP"); - while(1); - } - Serial.print("My IP address: "); - for (i=0;i<4;i++) - { - Serial.print(Ethernet.localIP()[i], DEC); - Serial.print("."); - } - Serial.println(); - udp.begin(PORT); - - coap_setup(); - endpoint_setup(); -} - -void udp_send(const uint8_t *buf, int buflen) -{ - udp.beginPacket(udp.remoteIP(), udp.remotePort()); - while(buflen--) - udp.write(*buf++); - udp.endPacket(); -} - -void loop() -{ - int sz; - int rc; - coap_packet_t pkt; - int i; - - if ((sz = udp.parsePacket()) > 0) - { - udp.read(packetbuf, sizeof(packetbuf)); - - for (i=0;i Date: Tue, 26 Apr 2016 11:29:24 +0300 Subject: [PATCH 7/7] working on microcoap --- src-posix/Makefile | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src-posix/Makefile b/src-posix/Makefile index 94d79ac..455376b 100644 --- a/src-posix/Makefile +++ b/src-posix/Makefile @@ -3,12 +3,12 @@ # make all -> clean and compile APPNAME = microcoap SRC = main-posix.c \ - libraries/microcoap/microcoap.c \ - microcoap-example/endpoints.c + ../microcoap.c \ + ../microcoap-example/endpoints.c # compilation options CFLAGS = -Wall -DDEBUG -INC_DIR = libraries/microcoap \ - microcoap-example +INC_DIR = ../ \ + ../microcoap-example # -DIPV6 # how to compile individual object files @@ -27,4 +27,4 @@ clean: rm -f $(OBJS) $(APPNAME) *~ # additional rule -all: clean $(APPNAME) \ No newline at end of file +all: clean $(APPNAME)