diff --git a/.gitignore b/.gitignore index 12c2f74..07d1385 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,5 @@ # Executable -coap +microcoap # Object files *.o @@ -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 deleted file mode 100644 index 19175d2..0000000 --- a/Makefile +++ /dev/null @@ -1,22 +0,0 @@ -CFLAGS += -Wall -DDEBUG -# -DIPV6 -SRC = $(wildcard *.c) -OBJ = $(SRC:%.c=%.o) -DEPS = $(SRC:%.c=%.d) -EXEC = coap - -all: $(EXEC) - --include $(DEPS) - -$(EXEC): $(OBJ) - @$(CC) $(CFLAGS) -o $@ $^ - -%.o: %.c %.d - @$(CC) -c $(CFLAGS) -o $@ $< - -%.d: %.c - @$(CC) -MM $(CFLAGS) $< > $@ - -clean: - @$(RM) $(EXEC) $(OBJ) $(DEPS) diff --git a/README.md b/README.md index 12c5f6b..38e181b 100644 --- a/README.md +++ b/README.md @@ -16,11 +16,14 @@ Endpoint handlers are defined in endpoints.c For linux/OSX make - ./coap + ./microcoap For Arduino - open microcoap.ino + copy libraries and microcoap-example directories to the Arduino IDE sketches folder (or just change the sketches folder to the microcoap dir) + open: File -> sketches folder -> microcoap-example + +Arduino LED pin defined at `microcoap-example\connections.h` and set to `13` (default embedded led for some Arduino boards) To test, use libcoap @@ -32,6 +35,12 @@ To test, use libcoap Or use copper (Firefox plugin) coap://127.0.0.1 + +How to add new coap links +====================== + +Use `microcoap-example` dir and edit it or prepare similar folder. +Edit `endpoints.h` and `endpoints.c` similar to light entry. Arduino problem =============== 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.ino b/microcoap-example/microcoap-example.ino similarity index 97% rename from microcoap.ino rename to microcoap-example/microcoap-example.ino index 148b6ce..6b9cf5c 100644 --- a/microcoap.ino +++ b/microcoap-example/microcoap-example.ino @@ -7,7 +7,8 @@ #include #include #include -#include "coap.h" +#include "endpoints.h" +#include "microcoap.h" #define PORT 5683 static uint8_t mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02}; diff --git a/coap.c b/microcoap.c similarity index 99% rename from coap.c rename to microcoap.c index 4d036f5..cf7e3a6 100644 --- a/coap.c +++ b/microcoap.c @@ -4,9 +4,10 @@ #include #include #include -#include "coap.h" +#include "microcoap.h" extern void endpoint_setup(void); +extern void increment_tick(void); extern const coap_endpoint_t endpoints[]; #ifdef DEBUG diff --git a/coap.h b/microcoap.h similarity index 98% rename from coap.h rename to microcoap.h index 9090285..9941946 100644 --- a/coap.h +++ b/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 @@ -165,6 +165,7 @@ int coap_handle_req(coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_ void coap_option_nibble(uint32_t value, uint8_t *nibble); void coap_setup(void); void endpoint_setup(void); +void increment_tick(void); // FIXME: create cpp wrapper #ifdef __cplusplus } diff --git a/src-posix/Makefile b/src-posix/Makefile new file mode 100644 index 0000000..455376b --- /dev/null +++ b/src-posix/Makefile @@ -0,0 +1,30 @@ +# 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 \ + ../microcoap.c \ + ../microcoap-example/endpoints.c +# compilation options +CFLAGS = -Wall -DDEBUG +INC_DIR = ../ \ + ../microcoap-example +# -DIPV6 + +# how to compile individual object files +OBJS = $(SRC:.c=.o) +.c.o: + $(CC) $(CFLAGS) -I$(INC_DIR) -c $< -o $@ + +.PHONY: all clean + +# app compilation +$(APPNAME): $(OBJS) $(SRC) + $(CC) $(OBJS) -o $(APPNAME) + +# cleaning rule +clean: + rm -f $(OBJS) $(APPNAME) *~ + +# additional rule +all: clean $(APPNAME) diff --git a/main-posix.c b/src-posix/main-posix.c similarity index 98% rename from main-posix.c rename to src-posix/main-posix.c index 3711b02..12d1670 100644 --- a/main-posix.c +++ b/src-posix/main-posix.c @@ -4,7 +4,7 @@ #include #include -#include "coap.h" +#include "microcoap.h" #define PORT 5683