+
+
+// List Avaliable Configuration Files
+PUBLIC void ucs2_listconfig (struct afb_req request) {
+ struct json_object *queryJ, *tmpJ, *responseJ;
+ DIR *dirHandle;
+ char *dirPath, *dirList;
+ int error=0;
+
+ 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);
+ 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
+ if (dirEnt->d_type == DT_REG || dirEnt->d_type == DT_UNKNOWN) {
+ struct json_object *pathJ = json_object_new_object();
+ json_object_object_add(pathJ, "dirpath", json_object_new_string(dirPath));
+ json_object_object_add(pathJ, "basename", json_object_new_string(dirEnt->d_name));
+ json_object_array_add(responseJ, pathJ);
+ }
+ }
+ }
+
+ free (dirList);
+
+ if (!error) afb_req_success(request,responseJ,NULL);
+ else {
+ char info[40];
+ 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");
+
+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");
+ }
+ else if (*res != UCS_I2C_RES_SUCCESS){
+ afb_req_fail_f(*req, "error-result", "result code: %d", *res);
+ }
+ else {
+ afb_req_success(*req, NULL, "success");
+ }
+
+ afb_req_unref(*req);
+ free(request_ptr);
+ }
+ else {
+ AFB_NOTICE("write_i2c: ambiguous response data");
+ }
+}
+
+/* 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)){
+ i = 0;
+ break;
+ }
+ 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*/
+ 0u, /* block count */
+ 0x2Au, /* i2c slave address */
+ 0x03E8u, /* timeout 1000 milliseconds */
+ i2c_data_sz, /* uint8_t dataLen */
+ &i2c_data[0], /* uint8_t *pData */
+ &ucs2_writei2c_CB, /* callback*/
+ (void*)async_req_ptr /* callback argument */
+ )) {
+ /* asynchronous command is running */
+ afb_req_addref(request);
+ }
+ else {
+ AFB_NOTICE("i2c write: scheduling command failed");
+ afb_req_fail_f(request, "query-command-queue","command queue overload");
+ free(async_req_ptr);
+ 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");
+ 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 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);
+ }
+ }
+ else {
+ ucs2_writei2c_cmd(request, j_obj);
+ }
+
+ OnErrorExit:
+ return;
+}