diff options
Diffstat (limited to 'ucs2-afb/ucs_binding.c')
-rw-r--r-- | ucs2-afb/ucs_binding.c | 124 |
1 files changed, 62 insertions, 62 deletions
diff --git a/ucs2-afb/ucs_binding.c b/ucs2-afb/ucs_binding.c index d343ec8..4730967 100644 --- a/ucs2-afb/ucs_binding.c +++ b/ucs2-afb/ucs_binding.c @@ -35,7 +35,7 @@ #include <time.h> #include <assert.h> #include <errno.h> -#include <dirent.h> +#include <dirent.h> #include "ucs_binding.h" #include "ucs_interface.h" @@ -65,7 +65,7 @@ typedef struct { typedef struct { struct afb_event node_event; - + } EventData_t; static ucsContextT *ucsContextS; @@ -77,7 +77,7 @@ PUBLIC void UcsXml_CB_OnError(const char format[], uint16_t vargsCnt, ...) { va_start (args, vargsCnt); vfprintf (stderr, format, args); va_end(args); - + va_list argptr; char outbuf[300]; va_start(argptr, vargsCnt); @@ -199,7 +199,7 @@ void UCSI_CB_OnStop(void *pTag) { void UCSI_CB_OnAmsMessageReceived(void *pTag) { /* If not interested, just ignore this event. - Otherwise UCSI_GetAmsMessage may now be called asynchronous (mainloop) to get the content. + Otherwise UCSI_GetAmsMessage may now be called asynchronous (mainloop) to get the content. Don't forget to call UCSI_ReleaseAmsMessage after that */ } @@ -214,7 +214,7 @@ void UCSI_CB_OnGpioStateChange(void *pTag, uint16_t nodeAddress, uint8_t gpioPin PUBLIC void UCSI_CB_OnMgrReport(void *pTag, Ucs_MgrReport_t code, uint16_t nodeAddress, Ucs_Rm_Node_t *pNode){ bool available; - + if (code == UCS_MGR_REP_AVAILABLE) { available = true; } @@ -225,15 +225,15 @@ PUBLIC void UCSI_CB_OnMgrReport(void *pTag, Ucs_MgrReport_t code, uint16_t nodeA /*untracked event - just exit*/ return; } - + if (eventData) { - + json_object *j_event_info = json_object_new_object(); json_object_object_add(j_event_info, "node", json_object_new_int(nodeAddress)); json_object_object_add(j_event_info, "available", json_object_new_boolean(available)); - + afb_event_push(eventData->node_event, j_event_info); - } + } } bool Cdev_Init(CdevData_t *d, const char *fileName, bool read, bool write) @@ -290,7 +290,7 @@ int onReadCB (sd_event_source* src, int fileFd, uint32_t revents, void* pTag) { STATIC char* GetDefaultConfig(void) { - + char const *data_path = getenv("AFM_APP_INSTALL_DIR"); if (!data_path) { @@ -299,7 +299,7 @@ STATIC char* GetDefaultConfig(void) { else { size_t size; char * config_path; - + AFB_NOTICE("AFM_APP_INSTALL_DIR is: %s", data_path); size = strlen(data_path) + strlen(XML_CONFIG_FOLDER) + strlen(XML_CONFIG_FILE) + 2; config_path = malloc(size); @@ -311,7 +311,7 @@ STATIC char* GetDefaultConfig(void) { } } } - + return NULL; } @@ -358,7 +358,7 @@ PUBLIC int StartConfiguration(const char *filename) { sd_event_source *evtSource; int err; - + /* Read and parse XML file */ ucsConfig = ParseFile(filename); if (NULL == ucsConfig) { @@ -392,7 +392,7 @@ PUBLIC int StartConfiguration(const char *filename) { AFB_ERROR ("Fail to initialize UNICENS"); goto OnErrorExit; } - + return 0; OnErrorExit: @@ -401,17 +401,17 @@ PUBLIC int StartConfiguration(const char *filename) { PUBLIC void ucs2_initialise (struct afb_req request) { const char *filename = afb_req_value(request, "filename"); - + if (!filename) { afb_req_fail_f (request, "filename-missing", "No filename given"); goto OnErrorExit; } - + if (StartConfiguration(filename) != 0) { afb_req_fail_f (request, "load-failed", "Cannot parse file and start UNICENS"); goto OnErrorExit; } - + afb_req_success(request,NULL,"UNICENS-active"); OnErrorExit: @@ -429,22 +429,22 @@ PUBLIC void ucs2_listconfig (struct afb_req request) { queryJ = afb_req_json(request); if (queryJ && json_object_object_get_ex (queryJ, "cfgpath" , &tmpJ)) { dirList = strdup (json_object_get_string(tmpJ)); - } else { - dirList = strdup (UCS2_CFG_PATH); + } else { + dirList = strdup (UCS2_CFG_PATH); AFB_NOTICE ("fgpath:missing uses UCS2_CFG_PATH=%s", UCS2_CFG_PATH); } responseJ = json_object_new_array(); for (dirPath= strtok(dirList, ":"); dirPath && *dirPath; dirPath=strtok(NULL,":")) { struct dirent *dirEnt; - + dirHandle = opendir (dirPath); if (!dirHandle) { AFB_NOTICE ("ucs2_listconfig dir=%s not readable", dirPath); error++; continue; - } - + } + AFB_NOTICE ("ucs2_listconfig scanning: %s", dirPath); while ((dirEnt = readdir(dirHandle)) != NULL) { // Unknown type is accepted to support dump filesystems @@ -456,52 +456,52 @@ PUBLIC void ucs2_listconfig (struct afb_req request) { } } } - + free (dirList); - + if (!error) afb_req_success(request,responseJ,NULL); else { char info[40]; - snprintf (info, sizeof(info), "[%d] where not scanned", error); + snprintf (info, sizeof(info), "[%d] where not scanned", error); afb_req_success(request,responseJ, info); - } - + } + return; } PUBLIC void ucs2_subscribe (struct afb_req request) { - + if (!eventData) { - + eventData = malloc(sizeof(EventData_t)); if (eventData) { eventData->node_event = afb_daemon_make_event ("node-availibility"); } - + if (!eventData || !afb_event_is_valid(eventData->node_event)) { afb_req_fail_f (request, "create-event", "Cannot create or register event"); goto OnExitError; } } - + if (afb_req_subscribe(request, eventData->node_event) != 0) { - + afb_req_fail_f (request, "subscribe-event", "Cannot subscribe to event"); goto OnExitError; } - - afb_req_success(request,NULL,"event subscription successful"); - + + afb_req_success(request,NULL,"event subscription successful"); + OnExitError: return; } STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) { - + if (request_ptr){ afb_req *req = (afb_req *)request_ptr; Ucs_I2c_ResultCode_t *res = (Ucs_I2c_ResultCode_t *)result_ptr; - + if (!res) { afb_req_fail(*req, "processing","busy or lost initialization"); } @@ -511,10 +511,10 @@ STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) { else { afb_req_success(*req, NULL, "success"); } - + afb_req_unref(*req); free(request_ptr); - } + } else { AFB_NOTICE("write_i2c: ambiguous response data"); } @@ -522,32 +522,32 @@ STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) { /* write a single i2c command */ STATIC void ucs2_writei2c_cmd(struct afb_req request, json_object *j_obj) { - + static uint8_t i2c_data[I2C_MAX_DATA_SZ]; uint8_t i2c_data_sz = 0; uint16_t node_addr = 0; struct afb_req *async_req_ptr = NULL; - + node_addr = (uint16_t)json_object_get_int(json_object_object_get(j_obj, "node")); AFB_NOTICE("node_address: 0x%02X", node_addr); - + if (node_addr == 0) { afb_req_fail_f(request, "query-params","params wrong or missing"); goto OnErrorExit; } - + if (json_object_get_type(json_object_object_get(j_obj, "data"))==json_type_array) { int size = json_object_array_length(json_object_object_get(j_obj, "data")); if ((size > 0) && (size <= I2C_MAX_DATA_SZ)) { - + int32_t i; int32_t val; struct json_object *j_elem; struct json_object *j_arr = json_object_object_get(j_obj, "data"); for (i = 0; i < size; i++) { - - + + j_elem = json_object_array_get_idx(j_arr, i); val = json_object_get_int(j_elem); if ((val < 0) && (val > 0xFF)){ @@ -556,20 +556,20 @@ STATIC void ucs2_writei2c_cmd(struct afb_req request, json_object *j_obj) { } i2c_data[i] = (uint8_t)json_object_get_int(j_elem); } - + i2c_data_sz = (uint8_t)i; } } - + if (i2c_data_sz == 0) { AFB_NOTICE("data: invalid or not found"); afb_req_fail_f(request, "query-params","params wrong or missing"); goto OnErrorExit; } - + async_req_ptr = malloc(sizeof(afb_req)); *async_req_ptr = request; - + if (UCSI_I2CWrite( &ucsContextS->ucsiData, /* UCSI_Data_t *pPriv*/ node_addr, /* uint16_t targetAddress*/ false, /* bool isBurst*/ @@ -591,16 +591,16 @@ STATIC void ucs2_writei2c_cmd(struct afb_req request, json_object *j_obj) { async_req_ptr = NULL; goto OnErrorExit; } - + OnErrorExit: return; } /* parse array or single command */ 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"); @@ -612,21 +612,21 @@ PUBLIC void ucs2_writei2c (struct afb_req request) { 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 cnt; int len = json_object_array_length(j_obj); - + if (len != 1) { afb_req_fail_f(request, "query-array","query of multiple commands is not supported"); goto OnErrorExit; } - + for (cnt = 0; cnt < len; cnt++) { - + json_object *j_cmd = json_object_array_get_idx(j_obj, cnt); ucs2_writei2c_cmd(request, j_cmd); } @@ -634,7 +634,7 @@ PUBLIC void ucs2_writei2c (struct afb_req request) { else { ucs2_writei2c_cmd(request, j_obj); } - + OnErrorExit: return; } @@ -643,7 +643,7 @@ PUBLIC int ucs2_initbinding(void) { #ifndef DISABLE_AUTOSTART char *filename = GetDefaultConfig(); if (filename != NULL) { - + AFB_NOTICE("AUTO-LOAD configuration: %s", filename); if (StartConfiguration(filename) == 0) { AFB_NOTICE("AUTO-LOAD successful"); @@ -652,6 +652,6 @@ PUBLIC int ucs2_initbinding(void) { } free(filename); } -#endif +#endif return 0; } |