-
Notifications
You must be signed in to change notification settings - Fork 6
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
base: master
Are you sure you want to change the base?
Changes from 24 commits
c67078b
92314ad
213bf65
7c9fc11
822549c
6c3c622
595197a
ff1ea4d
1f93d2f
8fa1d2a
c443565
37eb3c4
f88ce1e
ab49362
d605861
bad8f35
7245414
7092915
80f8d5c
0ea1f32
842fd36
8e05a98
93d1c1a
6a6e086
1b1f02d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/ | ||
|
||
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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea to include the different port! Thanks :) |
||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
* | ||
* @} | ||
*/ | ||
|
@@ -24,30 +25,41 @@ | |
#include "saul_reg.h" | ||
#include "fmt.h" | ||
#include "net/gcoap.h" | ||
#include "cbor.h" | ||
#include "phydat.h" | ||
//#include "winch.h" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What does |
||
{ "/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 = { | ||
|
@@ -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); | ||
|
@@ -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; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
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 themake
command in the project directory. Also you could changeX/riot-saul-coap
to.
(which would mean: in this project directory).