Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add "Put" Actuator Data to turn on/off LED #3

Open
wants to merge 25 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
c67078b
Add "Put" Actuator Data to turn on/off LED
seojeongmoon Oct 28, 2019
92314ad
Update saul_coap.c
seojeongmoon Oct 28, 2019
213bf65
Merge pull request #1 from seojeongmoon/seojeongmoon-patch-2
seojeongmoon Oct 28, 2019
7c9fc11
Indent saul_coap.c
rosetree Oct 28, 2019
822549c
Merge branch 'master' into sj-patch-1
rosetree Oct 28, 2019
6c3c622
Merge pull request #2 from rosetree/sj-patch-1
seojeongmoon Oct 28, 2019
595197a
Update README.md
seojeongmoon Nov 18, 2019
ff1ea4d
Update saul_coap.c
seojeongmoon Nov 25, 2019
1f93d2f
Update saul_coap.c
seojeongmoon Nov 25, 2019
8fa1d2a
Update saul_coap.c
seojeongmoon Nov 29, 2019
c443565
Update saul_coap.c
seojeongmoon Nov 29, 2019
37eb3c4
Update Makefile
seojeongmoon Nov 29, 2019
f88ce1e
Update saul_coap.c
seojeongmoon Dec 1, 2019
ab49362
Update saul_coap.c
seojeongmoon Dec 1, 2019
d605861
added resource for winch
seojeongmoon Dec 1, 2019
bad8f35
Update README.md
seojeongmoon Dec 2, 2019
7245414
Update README on testing with dtls
seojeongmoon Dec 2, 2019
7092915
added cbor.h, TO DO comments
seojeongmoon Dec 2, 2019
80f8d5c
separate winch from saul_coap, error corrections
seojeongmoon Dec 2, 2019
0ea1f32
Create winch.c
seojeongmoon Dec 2, 2019
842fd36
Rename winch.c to winch.txt
seojeongmoon Dec 2, 2019
8e05a98
error removed
seojeongmoon Dec 2, 2019
93d1c1a
specify directory
seojeongmoon Dec 9, 2019
6a6e086
Update winch.txt
seojeongmoon Dec 9, 2019
1b1f02d
Merge remote-tracking branch 'rosetree/master' into seojeongmoon-patch-1
seojeongmoon Dec 16, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,11 @@ DEVELHELP ?= 1
# Change this to 0 show compiler invocation lines by default:
QUIET ?= 1

# Include tinycbor for data representation
USEPKG += tinycbor
INCLUDE += $(RIOTPKG)/tinycbor/cbor.h

CFLAGS += -DGNRC_IPV6_NIB_CONF_SLAAC=1

# Include RIOT Base makefile
include $(RIOTBASE)/Makefile.include
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,21 @@ The idea of these resources is, to offer similar functionality as the


## Build and Execute

Directory: X/riot-saul-coap/RIOT/examples/gcoap/
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a small statement when this directory should be used? We only need it for the GET requests, but still have to run the make command in the project directory. Also you could change X/riot-saul-coap to . (which would mean: in this project directory).


Enter shell with board command (Phytec)

SERIAL=... BOARD=pba-d-01-kw2x BUILD_IN_DOCKER=1 make all flash term

To distinguish multiple boards using SERIAL number

make list-ttys

To test get command

coap get <ip address> 5683 <resource>

With DTLS, port number is 5684
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea to include the different port! Thanks :)



175 changes: 173 additions & 2 deletions saul_coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
* @file
* @brief CoAP endpoint for the SAUL registry
*
* @author Micha Rosenbaum <[email protected]>
* @author Seojeong Moon <[email protected]>
@author Micha Rosenbaum <[email protected]>
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, that you included yourself. I should have done this earlier.

Could need some code style improvements, like the * at the beginning and the spaces between the @author tag and the name.

*
* @}
*/
Expand All @@ -24,30 +25,41 @@
#include "saul_reg.h"
#include "fmt.h"
#include "net/gcoap.h"
#include "cbor.h"
#include "phydat.h"
//#include "winch.h"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What’s that? Work in progress?


extern char *make_msg(char *, ...);

static ssize_t _saul_cnt_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _saul_dev_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _saul_sensortype_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _sense_type_responder(coap_pkt_t* pdu, uint8_t *buf, size_t len, uint8_t type);
static ssize_t _saul_atr_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _atr_type_responder(coap_pkt_t* pdu, uint8_t *buf, size_t len, uint8_t type);

/* specific sense type handlers, shortcut via enum in saul.h */
static ssize_t _sense_temp_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _sense_hum_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _sense_servo_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _sense_press_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
static ssize_t _sense_voltage_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);
/* winch handler */
//static ssize_t _saul_winch_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx);

/* CoAP resources. Must be sorted by path (ASCII order). */
static const coap_resource_t _resources[] = {
{ "/hum", COAP_GET, _sense_hum_handler, NULL },
{ "/press", COAP_GET, _sense_press_handler, NULL },
{ "/saul/cnt", COAP_GET, _saul_cnt_handler, NULL },
{ "/saul/dev", COAP_POST, _saul_dev_handler, NULL },
{"/saul/atr", COAP_PUT, _saul_atr_handler, NULL},
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What does atr stand for? Attribute? Another coding style related request: could you use a space after { and before }, like in the surrounding lines.

{ "/sensor", COAP_GET, _saul_sensortype_handler, NULL },
{ "/servo", COAP_GET, _sense_servo_handler, NULL },
{ "/temp", COAP_GET, _sense_temp_handler, NULL },
{ "/voltage", COAP_GET, _sense_voltage_handler, NULL },
//{ "/winch", COAP_PUT, _saul_winch_handler, NULL},

};

static gcoap_listener_t _listener = {
Expand Down Expand Up @@ -159,7 +171,7 @@ static ssize_t _sense_type_responder(coap_pkt_t* pdu, uint8_t *buf, size_t len,
{
saul_reg_t *dev = saul_reg_find_type(type);
phydat_t res;
int dim;
int dim = 0;
size_t resp_len;

gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
Expand Down Expand Up @@ -234,3 +246,162 @@ void saul_coap_init(void)
{
gcoap_register_listener(&_listener);
}

static ssize_t _saul_atr_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
{
uint8_t type;

(void)ctx;

//payload length has to be bigger - edit according to expected size
if (pdu->payload_len <= 5) {
char req_payl[6] = { 0 };
memcpy(req_payl, (char *)pdu->payload, pdu->payload_len);
type = atoi(req_payl);
}
else {
return gcoap_response(pdu, buf, len, COAP_CODE_BAD_REQUEST);
}

return _atr_type_responder(pdu, buf, len, type);
}

static ssize_t _atr_type_responder(coap_pkt_t* pdu, uint8_t *buf, size_t len, uint8_t type)
{
saul_reg_t *dev = saul_reg_find_type(type);
phydat_t res;
int dim = 0;
size_t resp_len;
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
coap_opt_add_format(pdu, COAP_FORMAT_TEXT);
resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);

if (dev == NULL) {
char *err = "device not found";
if (pdu->payload_len >= strlen(err)) {
memcpy(pdu->payload, err, strlen(err));
resp_len += gcoap_response(pdu, buf, len, COAP_CODE_404);
return resp_len;
}
else {
phydat_dump(&res, type);//phydat.h
dim = saul_reg_write(dev, &res);
}
}
if (dim <= 0) {
char *err = "no values found";
if (pdu->payload_len >= strlen(err)) {
memcpy(pdu->payload, err, strlen(err));
resp_len += gcoap_response(pdu, buf, len, COAP_CODE_404);
return resp_len;
}
else {
return gcoap_response(pdu, buf, len, COAP_CODE_404);
}
}
/* write the response buffer with the request device value */
resp_len += fmt_u16_dec((char *)pdu->payload, res.val[0]);
return resp_len;
}


CborError export_cbor_to_phydat(CborParser *parser, uint8_t *cbor_buf, size_t buf_len, phydat_t data, int dim)
{
//CborParser parser;
CborValue value, r;
bool resultbool;
CborError err = CborNoError;

//initialize Cbor parser
err = cbor_parser_init(cbor_buf, buf_len, 0, parser, &value);
if (err != CborNoError) {
return err;
}

//check if map exists
if(!cbor_value_is_map(&value) || cbor_value_is_null(&value)){
return 0;
}

//enter container if map exists
//CborError cbor_value_enter_container ( const CborValue * it,CborValue * recursed )
err = cbor_value_enter_container(&value, &r) ;
if (err != CborNoError) {
return err;
}

//check for values
if(!cbor_value_is_text_string(&r)){
return 0;
}

cbor_value_text_string_equals(&r, "values", &resultbool);

if(!resultbool){
return 0;
}
cbor_value_advance(&r);

if(!cbor_value_is_array(&r)){
return 0;
}
/***************************************** check ******************************/
for (uint8_t i = 0; i < dim; i++) {
int16_t *p;
p = data.val;
int temp = *p++;
err = cbor_value_get_int_checked(&r, &temp);
if (err != CborNoError) {
return err;
}
}

//check for unit

if(!cbor_value_is_text_string(&r)){
return 0;
}

cbor_value_text_string_equals(&r, "unit", &resultbool);

if(!resultbool){
return 0;
}
cbor_value_advance(&r);
if(!cbor_value_is_array(&r)){
return 0;
}

/* uint8_t unit; */
int unit = data.unit;
err = cbor_value_get_int_checked(&r, &unit);
if (err != CborNoError) {
return err;
}

//check for scale

if(!cbor_value_is_text_string(&r)){
return 0;
}

cbor_value_text_string_equals(&r, "scale", &resultbool);

if(!resultbool){
return 0;
}
cbor_value_advance(&r);
if(!cbor_value_is_array(&r)){
return 0;
}

// int8_t scale;
int scale = data.scale;
err = cbor_value_get_int_checked(&r, &scale);
if (err != CborNoError) {
return err;
}

return CborNoError;
}

78 changes: 78 additions & 0 deletions winch.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this file be merged?


static int set(const void *dev, phydat_t *data)
{
const winch_saul_driver *w = (const _saul_driver *) dev;
winch_set();
return 1;
}

const saul_driver_t winch_saul_driver = {
.read = saul_notsup,
.write = set,
.type = SAUL_ACT_SERVO
};

static ssize_t _saul_winch_handler(coap_pkt_t* pdu, uint8_t *buf, size_t len, void *ctx)
{
uint8_t type;

(void)ctx;

//change payload size - according to expected parameters etc.
if (pdu->payload_len <= 5) {
char req_payl[6] = { 0 };
memcpy(req_payl, (char *)pdu->payload, pdu->payload_len);
type = atoi(req_payl);
}
else {
return gcoap_response(pdu, buf, len, COAP_CODE_BAD_REQUEST);
}

return _servo_type_responder(pdu, buf, len, type);
}

static ssize_t _winch_type_responder(coap_pkt_t* pdu, uint8_t *buf, size_t len, uint8_t type)
{
saul_reg_t *dev = saul_reg_find_type(type);
phydat_t res;
phydat_t data;
int dim;
size_t resp_len;
gcoap_resp_init(pdu, buf, len, COAP_CODE_CONTENT);
/* option of text and cbor format*/
coap_opt_add_format(pdu, COAP_FORMAT_TEXT);
//coap_opt_add_format(pdu, COAP_FORMAT_CBOR);
resp_len = coap_opt_finish(pdu, COAP_OPT_FINISH_PAYLOAD);

if (dev == NULL) {
char *err = "device not found";
if (pdu->payload_len >= strlen(err)) {
memcpy(pdu->payload, err, strlen(err));
resp_len += gcoap_response(pdu, buf, len, COAP_CODE_404);
return resp_len;
}
else {
//parse!***
//parse whatever to phydat
//CborError export_cbor_to_phydat(CborParser *parser, uint8_t *cbor_buf, size_t buf_len, phydat_t data, int dim)
//functions to set phydat_t data to winch actuator: winch_set(dev, &data);
//functions to execute : winch_control (winch_t *winch, int l_ges)
return gcoap_response(pdu, buf, len, COAP_CODE_404);
}
}
if (dim <= 0) {
char *err = "no values found";
if (pdu->payload_len >= strlen(err)) {
memcpy(pdu->payload, err, strlen(err));
resp_len += gcoap_response(pdu, buf, len, COAP_CODE_404);
return resp_len;
}
else {
return gcoap_response(pdu, buf, len, COAP_CODE_404);
}
}
/* write the response buffer with the request device value */
resp_len += fmt_u16_dec((char *)pdu->payload, res.val[0]);
return resp_len;
}