aboutsummaryrefslogtreecommitdiffstats
path: root/ucs2-afb
diff options
context:
space:
mode:
authorTobias Jahnke <tobias.jahnke@microchip.com>2017-12-12 18:33:35 +0100
committerTobias Jahnke <tobias.jahnke@microchip.com>2017-12-12 18:33:35 +0100
commit5b19a1a8e03704ff178c9f605530c3e3f8fa1134 (patch)
treed46764ac1c7d6756241736af27fa39fab155d86b /ucs2-afb
parentb59d76ac913c15e739f9b65177b6b893a3462db4 (diff)
ucs_binding.c: Add control message transmission
Bug-AGL: SPEC-1177 Support control message transmission to other network devices via json api. Change-Id: I22af427471a379f819e626ea4d746f78f3e37a6a Signed-off-by: Tobias Jahnke <tobias.jahnke@microchip.com>
Diffstat (limited to 'ucs2-afb')
-rw-r--r--ucs2-afb/ucs_apidef.h18
-rw-r--r--ucs2-afb/ucs_apidef.json45
-rw-r--r--ucs2-afb/ucs_binding.c141
3 files changed, 179 insertions, 25 deletions
diff --git a/ucs2-afb/ucs_apidef.h b/ucs2-afb/ucs_apidef.h
index 1dac82a..d3dfd3c 100644
--- a/ucs2-afb/ucs_apidef.h
+++ b/ucs2-afb/ucs_apidef.h
@@ -43,7 +43,15 @@ static const char _afb_description_v2_UNICENS[] =
"\"type\":\"integer\",\"format\":\"int32\"}},{\"in\":\"query\",\"name\":\""
"data\",\"required\":true,\"schema\":{\"type\":\"array\",\"format\":\"int"
"32\"},\"style\":\"simple\"}],\"responses\":{\"200\":{\"$ref\":\"#/compon"
- "ents/responses/200\"}}}}}}"
+ "ents/responses/200\"}}}},\"/sendmessage\":{\"description\":\"Transmits a"
+ " control message to a node.\",\"get\":{\"x-permissions\":{\"$ref\":\"#/c"
+ "omponents/x-permissions/monitor\"},\"parameters\":[{\"in\":\"query\",\"n"
+ "ame\":\"node\",\"required\":true,\"schema\":{\"type\":\"integer\",\"form"
+ "at\":\"int32\"}},{\"in\":\"query\",\"name\":\"msgid\",\"required\":true,"
+ "\"schema\":{\"type\":\"integer\",\"format\":\"int32\"}},{\"in\":\"query\""
+ ",\"name\":\"data\",\"required\":true,\"schema\":{\"type\":\"array\",\"fo"
+ "rmat\":\"int32\"},\"style\":\"simple\"}],\"responses\":{\"200\":{\"$ref\""
+ ":\"#/components/responses/200\"}}}}}}"
;
static const struct afb_auth _afb_auths_v2_UNICENS[] = {
@@ -55,6 +63,7 @@ static const struct afb_auth _afb_auths_v2_UNICENS[] = {
void ucs2_initialise(struct afb_req req);
void ucs2_subscribe(struct afb_req req);
void ucs2_writei2c(struct afb_req req);
+ void ucs2_sendmessage(struct afb_req req);
static const struct afb_verb_v2 _afb_verbs_v2_UNICENS[] = {
{
@@ -86,6 +95,13 @@ static const struct afb_verb_v2 _afb_verbs_v2_UNICENS[] = {
.session = AFB_SESSION_NONE_V2
},
{
+ .verb = "sendmessage",
+ .callback = ucs2_sendmessage,
+ .auth = &_afb_auths_v2_UNICENS[1],
+ .info = "Transmits a control message to a node.",
+ .session = AFB_SESSION_NONE_V2
+ },
+ {
.verb = NULL,
.callback = NULL,
.auth = NULL,
diff --git a/ucs2-afb/ucs_apidef.json b/ucs2-afb/ucs_apidef.json
index 1a00a97..a150fb8 100644
--- a/ucs2-afb/ucs_apidef.json
+++ b/ucs2-afb/ucs_apidef.json
@@ -163,7 +163,7 @@
"in": "query",
"name": "node",
"required": true,
- "schema": {
+ "schema": {
"type": "integer",
"format": "int32"
}
@@ -172,7 +172,48 @@
"in": "query",
"name": "data",
"required": true,
- "schema": {
+ "schema": {
+ "type": "array",
+ "format": "int32"
+ },
+ "style": "simple"
+ }
+ ],
+ "responses": {
+ "200": {"$ref": "#/components/responses/200"}
+ }
+ }
+ },
+ "/sendmessage": {
+ "description": "Transmits a control message to a node.",
+ "get": {
+ "x-permissions": {
+ "$ref": "#/components/x-permissions/monitor"
+ },
+ "parameters": [
+ {
+ "in": "query",
+ "name": "node",
+ "required": true,
+ "schema": {
+ "type": "integer",
+ "format": "int32"
+ }
+ },
+ {
+ "in": "query",
+ "name": "msgid",
+ "required": true,
+ "schema": {
+ "type": "integer",
+ "format": "int32"
+ }
+ },
+ {
+ "in": "query",
+ "name": "data",
+ "required": true,
+ "schema": {
"type": "array",
"format": "int32"
},
diff --git a/ucs2-afb/ucs_binding.c b/ucs2-afb/ucs_binding.c
index b8a1ba8..ca1f756 100644
--- a/ucs2-afb/ucs_binding.c
+++ b/ucs2-afb/ucs_binding.c
@@ -24,6 +24,8 @@
#define BUFFER_FRAME_COUNT 10 /* max frames in buffer */
#define WAIT_TIMER_US 1000000 /* default waiting timer 1s */
#define I2C_MAX_DATA_SZ 32 /* max. number of bytes to be written to i2c */
+#define CTRL_MAX_DATA_SZ 45 /* max. number of bytes to be written to control
+ * channel */
#include <systemd/sd-event.h>
#include <sys/types.h>
@@ -190,6 +192,7 @@ STATIC int OnStopCB (sd_event_source *source, uint64_t usec, void *pTag) {
UcsXml_FreeVal(ucsContextS->ucsConfig);
ucsContextS->ucsConfig = NULL;
}
+ return 0;
}
/**
@@ -508,6 +511,48 @@ OnExitError:
return;
}
+static json_object * ucs2_validate_command (struct afb_req request,
+ const char* func_name) {
+
+ struct json_object *j_obj = NULL;
+
+ if (!ucsContextS) { /* check UNICENS is initialized */
+ afb_req_fail_f(request, "unicens-init",
+ "Load a configuration before calling %s.",
+ func_name);
+ goto OnErrorExit;
+ }
+
+ j_obj = afb_req_json(request);
+ if (!j_obj) {
+ afb_req_fail_f(request,
+ "query-notjson","query=%s not a valid json entry",
+ afb_req_value(request,""));
+ goto OnErrorExit;
+ }
+
+ AFB_DEBUG("request: %s", json_object_to_json_string(j_obj));
+
+ if (json_object_get_type(j_obj)==json_type_array) {
+ int len = json_object_array_length(j_obj);
+
+ if (len == 1) { /* only support 1 command in array */
+ j_obj = json_object_array_get_idx(j_obj, 0);
+ }
+ else {
+ afb_req_fail_f(request,
+ "query-array",
+ "query of multiple %s commands is not supported",
+ func_name);
+ j_obj = NULL;
+ goto OnErrorExit;
+ }
+ }
+
+ OnErrorExit:
+ return j_obj;
+}
+
STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) {
if (request_ptr){
@@ -613,44 +658,96 @@ PUBLIC void ucs2_writei2c (struct afb_req request) {
struct json_object *j_obj;
- /* check UNICENS is initialised */
- if (!ucsContextS) {
- afb_req_fail_f(request, "unicens-init","Should Load Config before using setvol");
- goto OnErrorExit;
+ j_obj = ucs2_validate_command(request, "writei2c");
+
+ if (j_obj) {
+ ucs2_writei2c_cmd(request, j_obj);
}
+}
- j_obj = afb_req_json(request);
- if (!j_obj) {
- afb_req_fail_f(request, "query-notjson","query=%s not a valid json entry", afb_req_value(request,""));
- goto OnErrorExit;
- };
+/* write a single control message */
+STATIC void ucs2_sendmessage_cmd(struct afb_req request, json_object *j_obj) {
- AFB_DEBUG("request: %s", json_object_to_json_string(j_obj));
+ static uint8_t ctrl_data[CTRL_MAX_DATA_SZ];
+ uint8_t ctrl_data_sz = 0;
+ uint16_t node_addr = 0;
+ uint16_t msg_id = 0;
+ json_object *j_tmp;
- if (json_object_get_type(j_obj)==json_type_array) {
+ if (json_object_object_get_ex(j_obj, "node", &j_tmp)) {
+ node_addr = (uint16_t)json_object_get_int(j_tmp);
+ AFB_NOTICE("node_address: 0x%02X", node_addr);
+ }
+ else {
+ afb_req_fail_f(request, "query-params","param node missing");
+ goto OnErrorExit;
+ }
- int cnt;
- int len = json_object_array_length(j_obj);
+ if (json_object_object_get_ex(j_obj, "msgid", &j_tmp)) {
+ msg_id = (uint16_t)json_object_get_int(j_tmp);
+ AFB_NOTICE("msgid: 0x%02X", msg_id);
+ }
+ else {
+ afb_req_fail_f(request, "query-params","param msgid missing");
+ goto OnErrorExit;
+ }
- if (len != 1) {
- afb_req_fail_f(request, "query-array","query of multiple commands is not supported");
- goto OnErrorExit;
- }
+ if (json_object_object_get_ex(j_obj, "data", &j_tmp)) {
+ if (json_object_get_type(j_tmp)==json_type_array) {
+ int size = json_object_array_length(j_tmp);
+ if ((size > 0) && (size <= CTRL_MAX_DATA_SZ)) {
+ int32_t i;
+ int32_t val;
+ struct json_object *j_elem;
+
+ for (i = 0; i < size; i++) {
+ j_elem = json_object_array_get_idx(j_tmp, i);
+ val = json_object_get_int(j_elem);
+ if ((val < 0) && (val > 0xFF)){
+ i = 0;
+ break;
+ }
+ ctrl_data[i] = (uint8_t)json_object_get_int(j_elem);
+ }
- for (cnt = 0; cnt < len; cnt++) {
+ if (i != size) { /* check if size matches */
+ afb_req_fail_f(request, "query-params",
+ "parameter data is ambiguous");
+ goto OnErrorExit;
+ }
- json_object *j_cmd = json_object_array_get_idx(j_obj, cnt);
- ucs2_writei2c_cmd(request, j_cmd);
+ ctrl_data_sz = (uint8_t)i;
+ }
}
}
+
+ if (UCSI_SendAmsMessage(&ucsContextS->ucsiData, /* UCSI_Data_t *pPriv*/
+ msg_id,
+ node_addr,
+ &ctrl_data[0],
+ ctrl_data_sz)) {
+ afb_req_success(request, NULL, "sendmessage started successful");
+ }
else {
- ucs2_writei2c_cmd(request, j_obj);
+ AFB_NOTICE("sendmessage: scheduling command failed");
+ afb_req_fail_f(request, "query-command-queue","command queue overload");
+ goto OnErrorExit;
}
- OnErrorExit:
+OnErrorExit:
return;
}
+PUBLIC void ucs2_sendmessage(struct afb_req request) {
+ struct json_object *j_obj;
+
+ j_obj = ucs2_validate_command(request, "sendmessage");
+
+ if (j_obj) {
+ ucs2_sendmessage_cmd(request, j_obj);
+ }
+}
+
PUBLIC int ucs2_initbinding(void) {
#ifndef DISABLE_AUTOSTART
char *filename = GetDefaultConfig();