UcsXml component use libxml2 instead of libmxml
[apps/agl-service-unicens.git] / ucs2-afb / ucs_binding.c
1 /*
2  * Copyright (C) 2016 "IoT.bzh"
3  * Author Fulup Ar Foll <fulup@iot.bzh>
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *   http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * references:
18  *   https://gist.github.com/ghedo/963382
19  *   http://alsa-utils.sourcearchive.com/documentation/1.0.15/aplay_8c-source.html
20  */
21
22 #define _GNU_SOURCE
23
24 #define BUFFER_FRAME_COUNT 10 /* max frames in buffer */
25 #define WAIT_TIMER_US 1000000 /* default waiting timer 1s */
26 #define I2C_MAX_DATA_SZ    32 /* max. number of bytes to be written to i2c */
27
28 #include <systemd/sd-event.h>
29 #include <sys/types.h>
30 #include <sys/stat.h>
31 #include <stdio.h>
32 #include <fcntl.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <time.h>
36 #include <assert.h>
37 #include <errno.h>
38 #include <dirent.h>
39
40 #include "ucs_binding.h"
41 #include "ucs_interface.h"
42
43 #define MAX_FILENAME_LEN (100)
44 #define RX_BUFFER (64)
45 #define XML_CONFIG_FOLDER "/data/"
46 #define XML_CONFIG_FILE "config_multichannel_audio_kit.xml"
47
48 /** Internal structure, enabling multiple instances of this component.
49  * \note Do not access any of this variables.
50  *  */
51 typedef struct {
52     int fileHandle;
53     int fileFlags;
54     char fileName[MAX_FILENAME_LEN];
55     uint8_t rxBuffer[RX_BUFFER];
56     uint32_t rxLen;
57 } CdevData_t;
58
59
60 typedef struct {
61   CdevData_t rx;
62   CdevData_t tx;
63   UCSI_Data_t ucsiData;
64   UcsXmlVal_t* ucsConfig;
65 } ucsContextT;
66
67 typedef struct {
68     struct afb_event node_event;
69
70 } EventData_t;
71
72 static ucsContextT *ucsContextS = NULL;
73 static EventData_t *eventData = NULL;
74
75 PUBLIC void UcsXml_CB_OnError(const char format[], uint16_t vargsCnt, ...) {
76     /*AFB_DEBUG (afbIface, format, args); */
77     va_list args;
78     va_start (args, vargsCnt);
79     vfprintf (stderr, format, args);
80     va_end(args);
81
82     va_list argptr;
83     char outbuf[300];
84     va_start(argptr, vargsCnt);
85     vsprintf(outbuf, format, argptr);
86     va_end(argptr);
87     AFB_WARNING ("%s", outbuf);
88 }
89
90 PUBLIC uint16_t UCSI_CB_OnGetTime(void *pTag) {
91     struct timespec currentTime;
92     uint16_t timer;
93     pTag = pTag;
94
95     if (clock_gettime(CLOCK_MONOTONIC_RAW, &currentTime))   {
96         assert(false);
97         return 0;
98     }
99
100     timer = (uint16_t) ((currentTime.tv_sec * 1000 ) + ( currentTime.tv_nsec / 1000000 ));
101     return(timer);
102 }
103
104 STATIC int onTimerCB (sd_event_source* source,uint64_t timer, void* pTag) {
105     ucsContextT *ucsContext = (ucsContextT*) pTag;
106
107     sd_event_source_unref(source);
108     UCSI_Timeout(&ucsContext->ucsiData);
109
110     return 0;
111 }
112
113 void UCSI_CB_OnNetworkState(void *pTag, bool isAvailable, uint16_t packetBandwidth, uint8_t amountOfNodes)
114 {
115     AFB_NOTICE ("Network is available=%d, bw=%d, nodeCnt=%d", isAvailable, packetBandwidth, amountOfNodes);
116 }
117
118 /* UCS2 Interface Timer Callback */
119 PUBLIC void UCSI_CB_OnSetServiceTimer(void *pTag, uint16_t timeout) {
120   uint64_t usec;
121   /* set a timer with  250ms accuracy */
122   sd_event_now(afb_daemon_get_event_loop(), CLOCK_BOOTTIME, &usec);
123   sd_event_add_time(afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, usec + (timeout*1000), 250, onTimerCB, pTag);
124
125 }
126
127 /**
128  * \brief Callback when ever an Unicens forms a human readable message.
129  *        This can be error events or when enabled also debug messages.
130  * \note This function must be implemented by the integrator
131  * \param pTag - Pointer given by the integrator by UCSI_Init
132  * \param format - Zero terminated format string (following printf rules)
133  * \param vargsCnt - Amount of parameters stored in "..."
134  */
135 void UCSI_CB_OnUserMessage(void *pTag, bool isError, const char format[],
136     uint16_t vargsCnt, ...) {
137     va_list argptr;
138     char outbuf[300];
139     pTag = pTag;
140     va_start(argptr, vargsCnt);
141     vsprintf(outbuf, format, argptr);
142     va_end(argptr);
143     AFB_NOTICE ("%s",outbuf);
144 }
145
146 /** UCSI_Service cannot be called directly within UNICENS context, need to service stack through mainloop */
147 STATIC int OnServiceRequiredCB (sd_event_source *source, uint64_t usec, void *pTag) {
148     ucsContextT *ucsContext = (ucsContextT*) pTag;
149
150     sd_event_source_unref(source);
151     UCSI_Service(&ucsContext->ucsiData);
152     return (0);
153 }
154
155 /* UCS Callback fire when ever UNICENS needs to be serviced */
156 PUBLIC void UCSI_CB_OnServiceRequired(void *pTag) {
157
158    /* push an asynchronous request for loopback to call UCSI_Service */
159    sd_event_add_time(afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, 0, 0, OnServiceRequiredCB, pTag);
160 }
161
162 /* Callback when ever this UNICENS wants to send a message to INIC. */
163 PUBLIC void UCSI_CB_OnTxRequest(void *pTag, const uint8_t *pData, uint32_t len) {
164     ucsContextT *ucsContext = (ucsContextT*) pTag;
165     CdevData_t *cdevTx = &ucsContext->tx;
166     uint32_t total = 0;
167
168     if (NULL == pData || 0 == len) return;
169
170     if (O_RDONLY == cdevTx->fileFlags) return;
171     if (-1 == cdevTx->fileHandle)
172         cdevTx->fileHandle = open(cdevTx->fileName, cdevTx->fileFlags);
173     if (-1 == cdevTx->fileHandle)
174         return;
175
176     while(total < len) {
177         ssize_t written = write(cdevTx->fileHandle, &pData[total], (len - total));
178         if (0 >= written)
179         {
180             /* Silently ignore write error (only occur in non-blocking mode) */
181             break;
182         }
183         total += (uint32_t) written;
184     }
185 }
186
187 /** UcsXml_FreeVal be called directly within UNICENS context, need to service stack through mainloop */
188 STATIC int OnStopCB (sd_event_source *source, uint64_t usec, void *pTag) {
189     if (NULL != ucsContextS && NULL != ucsContextS->ucsConfig) {
190         UcsXml_FreeVal(ucsContextS->ucsConfig);
191         ucsContextS->ucsConfig = NULL;
192     }
193 }
194
195 /**
196  * \brief Callback when UNICENS instance has been stopped.
197  * \note This event can be used to free memory holding the resources
198  *       passed with UCSI_NewConfig
199  * \note This function must be implemented by the integrator
200  * \param pTag - Pointer given by the integrator by UCSI_Init
201  */
202 void UCSI_CB_OnStop(void *pTag) {
203    AFB_NOTICE ("UNICENS stopped");
204    /* push an asynchronous request for loopback to call UcsXml_FreeVal */
205    sd_event_add_time(afb_daemon_get_event_loop(), NULL, CLOCK_MONOTONIC, 0, 0, OnStopCB, pTag);
206 }
207
208 /** This callback will be raised, when ever an applicative message on the control channel arrived */
209 void UCSI_CB_OnAmsMessageReceived(void *pTag)
210 {
211         /* If not interested, just ignore this event.
212            Otherwise UCSI_GetAmsMessage may now be called asynchronous (mainloop) to get the content.
213            Don't forget to call UCSI_ReleaseAmsMessage after that */
214 }
215
216 void UCSI_CB_OnRouteResult(void *pTag, uint16_t routeId, bool isActive, uint16_t connectionLabel)
217 {
218     AFB_NOTICE ("Route 0x%X is active=%d, connection label=0x%X", routeId, isActive, connectionLabel);
219 }
220
221 void UCSI_CB_OnGpioStateChange(void *pTag, uint16_t nodeAddress, uint8_t gpioPinId, bool isHighState)
222 {
223     AFB_NOTICE ("GPIO state of node 0x%X changed, pin=%d isHigh=%d", nodeAddress, gpioPinId, isHighState);
224 }
225
226 PUBLIC void UCSI_CB_OnMgrReport(void *pTag, Ucs_MgrReport_t code, uint16_t nodeAddress, Ucs_Rm_Node_t *pNode){
227
228     bool available;
229
230     if (code == UCS_MGR_REP_AVAILABLE) {
231         available = true;
232     }
233     else if (code == UCS_MGR_REP_NOT_AVAILABLE) {
234         available = false;
235     }
236     else {
237         /*untracked event - just exit*/
238         return;
239     }
240
241     if (eventData) {
242
243         json_object *j_event_info = json_object_new_object();
244         json_object_object_add(j_event_info, "node", json_object_new_int(nodeAddress));
245         json_object_object_add(j_event_info, "available", json_object_new_boolean(available));
246
247         afb_event_push(eventData->node_event, j_event_info);
248     }
249 }
250
251 bool Cdev_Init(CdevData_t *d, const char *fileName, bool read, bool write)
252 {
253     if (NULL == d || NULL == fileName)  goto OnErrorExit;
254
255     memset(d, 0, sizeof(CdevData_t));
256     strncpy(d->fileName, fileName, MAX_FILENAME_LEN);
257     d->fileHandle = -1;
258
259     if (read && write)
260         d->fileFlags = O_RDWR | O_NONBLOCK;
261     else if (read)
262         d->fileFlags = O_RDONLY | O_NONBLOCK;
263     else if (write)
264         d->fileFlags = O_WRONLY | O_NONBLOCK;
265
266     /* open file to enable event loop */
267     d->fileHandle = open(d->fileName, d->fileFlags);
268     if (d->fileHandle  <= 0) goto OnErrorExit;
269
270     return true;
271
272  OnErrorExit:
273     return false;
274 }
275
276 static bool InitializeCdevs(ucsContextT *ucsContext)
277 {
278     if(!Cdev_Init(&ucsContext->tx, CONTROL_CDEV_TX, false, true))
279         return false;
280     if(!Cdev_Init(&ucsContext->rx, CONTROL_CDEV_RX, true, false))
281         return false;
282     return true;
283 }
284
285 /* Callback fire when something is avaliable on MOST cdev */
286 int onReadCB (sd_event_source* src, int fileFd, uint32_t revents, void* pTag) {
287     ucsContextT *ucsContext =( ucsContextT*) pTag;
288     ssize_t len;
289     uint8_t pBuffer[RX_BUFFER];
290     int ok;
291
292     len = read (ucsContext->rx.fileHandle, &pBuffer, sizeof(pBuffer));
293     if (0 == len)
294         return 0;
295     ok= UCSI_ProcessRxData(&ucsContext->ucsiData, pBuffer, (uint16_t)len);
296     if (!ok) {
297         AFB_DEBUG ("Buffer overrun (not handle)");
298         /* Buffer overrun could replay pBuffer */
299     }
300     return 0;
301 }
302
303
304 STATIC char* GetDefaultConfig(void) {
305
306     char const *data_path = getenv("AFM_APP_INSTALL_DIR");
307
308     if (!data_path) {
309         AFB_ERROR("AFM_APP_INSTALL_DIR is not defined");
310     }
311     else {
312         size_t size;
313         char * config_path;
314
315         AFB_NOTICE("AFM_APP_INSTALL_DIR is: %s", data_path);
316         size = strlen(data_path) + strlen(XML_CONFIG_FOLDER) + strlen(XML_CONFIG_FILE) + 2;
317         config_path = malloc(size);
318         if (config_path != NULL) {
319             snprintf(config_path, size, "%s%s%s", data_path, XML_CONFIG_FOLDER, XML_CONFIG_FILE);
320             if(access(config_path, R_OK ) == 0) {
321                 AFB_NOTICE("Default configuration: %s", config_path);
322                 return config_path;
323             }
324         }
325     }
326
327     return NULL;
328 }
329
330 STATIC UcsXmlVal_t* ParseFile(const char *filename) {
331     char *xmlBuffer;
332     ssize_t readSize;
333     int fdHandle ;
334     struct stat fdStat;
335     UcsXmlVal_t *ucsConfig = NULL;
336
337     fdHandle = open(filename, O_RDONLY);
338     if (fdHandle <= 0) {
339         AFB_ERROR("File not accessible: '%s' err=%s", filename, strerror(fdHandle));
340         goto OnErrorExit;
341     }
342
343     /* read file into buffer as a \0 terminated string */
344     fstat(fdHandle, &fdStat);
345     xmlBuffer = (char*)alloca(fdStat.st_size + 1);
346     readSize = read(fdHandle, xmlBuffer, fdStat.st_size);
347     close(fdHandle);
348     xmlBuffer[readSize] = '\0'; /* In any case, terminate it. */
349
350     if (readSize != fdStat.st_size)  {
351         AFB_ERROR("File to read fullfile '%s' size(%d!=%d)", filename, (int)readSize, (int)fdStat.st_size);
352         goto OnErrorExit;
353     }
354
355     ucsConfig = UcsXml_Parse(xmlBuffer);
356     if (!ucsConfig)  {
357         AFB_ERROR("File XML invalid: '%s'", filename);
358         goto OnErrorExit;
359     }
360     AFB_NOTICE ("Parsing result: %d Nodes, %d Scripts, Ethernet Bandwith %d bytes = %.2f MBit/s", ucsConfig->nodSize, ucsConfig->routesSize, ucsConfig->packetBw, (48 * 8 * ucsConfig->packetBw / 1000.0));
361
362     return (ucsConfig);
363
364  OnErrorExit:
365     return NULL;
366 }
367
368 PUBLIC int StartConfiguration(const char *filename) {
369     static ucsContextT ucsContext = { 0 };
370
371     sd_event_source *evtSource;
372     int err;
373
374     /* Read and parse XML file */
375     ucsContext.ucsConfig = ParseFile(filename);
376     if (NULL == ucsContext.ucsConfig) {
377         AFB_ERROR ("Cannot access or load file: '%s'", filename);
378         goto OnErrorExit;
379     }
380
381     /* When ucsContextS is set, do not initalize UNICENS, CDEVs or system hooks, just load new XML */
382     if (!ucsContextS)
383     {
384         if (!ucsContextS && !InitializeCdevs(&ucsContext))  {
385             AFB_ERROR ("Fail to initialise device [rx=%s tx=%s]", CONTROL_CDEV_RX, CONTROL_CDEV_TX);
386             goto OnErrorExit;
387         }
388
389         /* Initialise UNICENS Config Data Structure */
390         UCSI_Init(&ucsContext.ucsiData, &ucsContext);
391
392         /* register aplayHandle file fd into binder mainloop */
393         err = sd_event_add_io(afb_daemon_get_event_loop(), &evtSource, ucsContext.rx.fileHandle, EPOLLIN, onReadCB, &ucsContext);
394         if (err < 0) {
395             AFB_ERROR ("Cannot hook events to mainloop");
396             goto OnErrorExit;
397         }
398
399         /* save this in a statical variable until ucs2vol move to C */
400         ucsContextS = &ucsContext;
401     }
402     /* Initialise UNICENS with parsed config */
403     if (!UCSI_NewConfig(&ucsContext.ucsiData, ucsContext.ucsConfig))   {
404         AFB_ERROR ("Fail to initialize UNICENS");
405         goto OnErrorExit;
406     }
407
408     return 0;
409
410  OnErrorExit:
411     return -1;
412 }
413
414 PUBLIC void ucs2_initialise (struct afb_req request) {
415     const char *filename = afb_req_value(request, "filename");
416
417     if (!filename) {
418         afb_req_fail_f (request, "filename-missing", "No filename given");
419         goto OnErrorExit;
420     }
421
422     if (StartConfiguration(filename) != 0) {
423         afb_req_fail_f (request, "load-failed", "Cannot parse file and start UNICENS");
424         goto OnErrorExit;
425     }
426
427     afb_req_success(request,NULL,"UNICENS-active");
428
429  OnErrorExit:
430     return;
431 }
432
433
434 // List Avaliable Configuration Files
435 PUBLIC void ucs2_listconfig (struct afb_req request) {
436     struct json_object *queryJ, *tmpJ, *responseJ;
437     DIR  *dirHandle;
438     char *dirPath, *dirList;
439     int error=0;
440
441     queryJ = afb_req_json(request);
442     if (queryJ && json_object_object_get_ex (queryJ, "cfgpath" , &tmpJ)) {
443         dirList = strdup (json_object_get_string(tmpJ));
444     } else {
445         dirList = strdup (UCS2_CFG_PATH);
446         AFB_NOTICE ("fgpath:missing uses UCS2_CFG_PATH=%s", UCS2_CFG_PATH);
447     }
448
449     responseJ = json_object_new_array();
450     for (dirPath= strtok(dirList, ":"); dirPath && *dirPath; dirPath=strtok(NULL,":")) {
451          struct dirent *dirEnt;
452
453         dirHandle = opendir (dirPath);
454         if (!dirHandle) {
455             AFB_NOTICE ("ucs2_listconfig dir=%s not readable", dirPath);
456             error++;
457             continue;
458         }
459
460         AFB_NOTICE ("ucs2_listconfig scanning: %s", dirPath);
461         while ((dirEnt = readdir(dirHandle)) != NULL) {
462             // Unknown type is accepted to support dump filesystems
463             if (dirEnt->d_type == DT_REG || dirEnt->d_type == DT_UNKNOWN) {
464                 struct json_object *pathJ = json_object_new_object();
465                 json_object_object_add(pathJ, "dirpath", json_object_new_string(dirPath));
466                 json_object_object_add(pathJ, "basename", json_object_new_string(dirEnt->d_name));
467                 json_object_array_add(responseJ, pathJ);
468             }
469         }
470     }
471
472     free (dirList);
473
474     if (!error)  afb_req_success(request,responseJ,NULL);
475     else {
476         char info[40];
477         snprintf (info, sizeof(info), "[%d] where not scanned", error);
478          afb_req_success(request,responseJ, info);
479     }
480
481     return;
482 }
483
484 PUBLIC void ucs2_subscribe (struct afb_req request) {
485
486     if (!eventData) {
487
488         eventData = malloc(sizeof(EventData_t));
489         if (eventData) {
490             eventData->node_event = afb_daemon_make_event ("node-availibility");
491         }
492
493         if (!eventData || !afb_event_is_valid(eventData->node_event)) {
494             afb_req_fail_f (request, "create-event", "Cannot create or register event");
495             goto OnExitError;
496         }
497     }
498
499     if (afb_req_subscribe(request, eventData->node_event) != 0) {
500
501         afb_req_fail_f (request, "subscribe-event", "Cannot subscribe to event");
502         goto OnExitError;
503     }
504
505     afb_req_success(request,NULL,"event subscription successful");
506
507 OnExitError:
508     return;
509 }
510
511 STATIC void ucs2_writei2c_CB (void *result_ptr, void *request_ptr) {
512
513     if (request_ptr){
514         afb_req *req = (afb_req *)request_ptr;
515         Ucs_I2c_ResultCode_t *res = (Ucs_I2c_ResultCode_t *)result_ptr;
516
517         if (!res) {
518             afb_req_fail(*req, "processing","busy or lost initialization");
519         }
520         else if (*res != UCS_I2C_RES_SUCCESS){
521             afb_req_fail_f(*req, "error-result", "result code: %d", *res);
522         }
523         else {
524             afb_req_success(*req, NULL, "success");
525         }
526
527         afb_req_unref(*req);
528         free(request_ptr);
529     }
530     else {
531         AFB_NOTICE("write_i2c: ambiguous response data");
532     }
533 }
534
535 /* write a single i2c command */
536 STATIC void ucs2_writei2c_cmd(struct afb_req request, json_object *j_obj) {
537
538     static uint8_t i2c_data[I2C_MAX_DATA_SZ];
539     uint8_t i2c_data_sz = 0;
540     uint16_t node_addr = 0;
541     struct afb_req *async_req_ptr = NULL;
542
543     node_addr = (uint16_t)json_object_get_int(json_object_object_get(j_obj, "node"));
544     AFB_NOTICE("node_address: 0x%02X", node_addr);
545
546     if (node_addr == 0) {
547         afb_req_fail_f(request, "query-params","params wrong or missing");
548         goto OnErrorExit;
549     }
550
551     if (json_object_get_type(json_object_object_get(j_obj, "data"))==json_type_array) {
552         int size = json_object_array_length(json_object_object_get(j_obj, "data"));
553         if ((size > 0) && (size <= I2C_MAX_DATA_SZ)) {
554
555             int32_t i;
556             int32_t val;
557             struct json_object *j_elem;
558             struct json_object *j_arr = json_object_object_get(j_obj, "data");
559
560             for (i = 0; i < size; i++) {
561
562
563                 j_elem = json_object_array_get_idx(j_arr, i);
564                 val = json_object_get_int(j_elem);
565                 if ((val < 0) && (val > 0xFF)){
566                     i = 0;
567                     break;
568                 }
569                 i2c_data[i] = (uint8_t)json_object_get_int(j_elem);
570             }
571
572             i2c_data_sz = (uint8_t)i;
573         }
574     }
575
576     if (i2c_data_sz == 0) {
577         AFB_NOTICE("data: invalid or not found");
578         afb_req_fail_f(request, "query-params","params wrong or missing");
579         goto OnErrorExit;
580     }
581
582     async_req_ptr = malloc(sizeof(afb_req));
583     *async_req_ptr = request;
584
585     if (UCSI_I2CWrite(  &ucsContextS->ucsiData,   /* UCSI_Data_t *pPriv*/
586                         node_addr,                /* uint16_t targetAddress*/
587                         false,                    /* bool isBurst*/
588                         0u,                       /* block count */
589                         0x2Au,                    /* i2c slave address */
590                         0x03E8u,                  /* timeout 1000 milliseconds */
591                         i2c_data_sz,              /* uint8_t dataLen */
592                         &i2c_data[0],             /* uint8_t *pData */
593                         &ucs2_writei2c_CB,        /* callback*/
594                         (void*)async_req_ptr      /* callback argument */
595                   )) {
596         /* asynchronous command is running */
597         afb_req_addref(request);
598     }
599     else {
600         AFB_NOTICE("i2c write: scheduling command failed");
601         afb_req_fail_f(request, "query-command-queue","command queue overload");
602         free(async_req_ptr);
603         async_req_ptr = NULL;
604         goto OnErrorExit;
605     }
606
607 OnErrorExit:
608     return;
609 }
610
611 /* parse array or single command */
612 PUBLIC void ucs2_writei2c (struct afb_req request) {
613
614     struct json_object *j_obj;
615
616     /* check UNICENS is initialised */
617     if (!ucsContextS) {
618         afb_req_fail_f(request, "unicens-init","Should Load Config before using setvol");
619         goto OnErrorExit;
620     }
621
622     j_obj = afb_req_json(request);
623     if (!j_obj) {
624         afb_req_fail_f(request, "query-notjson","query=%s not a valid json entry", afb_req_value(request,""));
625         goto OnErrorExit;
626     };
627
628     AFB_DEBUG("request: %s", json_object_to_json_string(j_obj));
629
630     if (json_object_get_type(j_obj)==json_type_array) {
631
632         int cnt;
633         int len = json_object_array_length(j_obj);
634
635         if (len != 1) {
636             afb_req_fail_f(request, "query-array","query of multiple commands is not supported");
637             goto OnErrorExit;
638         }
639
640         for (cnt = 0; cnt < len; cnt++) {
641
642             json_object *j_cmd = json_object_array_get_idx(j_obj, cnt);
643             ucs2_writei2c_cmd(request, j_cmd);
644         }
645     }
646     else {
647         ucs2_writei2c_cmd(request, j_obj);
648     }
649
650  OnErrorExit:
651     return;
652 }
653
654 PUBLIC int ucs2_initbinding(void) {
655 #ifndef DISABLE_AUTOSTART
656     char *filename = GetDefaultConfig();
657     if (filename != NULL) {
658
659         AFB_NOTICE("AUTO-LOAD configuration: %s", filename);
660         if (StartConfiguration(filename) == 0) {
661             AFB_NOTICE("AUTO-LOAD successful");
662         } else {
663             AFB_NOTICE("AUTO-LOAD failed");
664         }
665         free(filename);
666     }
667 #endif
668     return 0;
669 }