enables automatic network setup on bindinginit 17/11817/3
authorTobias Jahnke <tobias.jahnke@microchip.com>
Fri, 10 Nov 2017 07:57:11 +0000 (08:57 +0100)
committerTobias Jahnke <tobias.jahnke@microchip.com>
Sat, 11 Nov 2017 21:15:37 +0000 (22:15 +0100)
Change-Id: Ie0c9f8cf04647c315949f0f2cecf61bb08b69e6d
Signed-off-by: Tobias Jahnke <tobias.jahnke@microchip.com>
ucs2-afb/ucs_binding.c

index a234410..d343ec8 100644 (file)
@@ -315,22 +315,16 @@ STATIC char* GetDefaultConfig(void) {
     return NULL;
 }
 
-STATIC UcsXmlVal_t* ParseFile(struct afb_req request) {
+STATIC UcsXmlVal_t* ParseFile(const char *filename) {
     char *xmlBuffer;
     ssize_t readSize;
     int fdHandle ;
     struct stat fdStat;
     UcsXmlVal_t* ucsConfig;
 
-    const char *filename = afb_req_value(request, "filename");
-    if (!filename) {
-        afb_req_fail_f (request, "filename-missing", "No filename given");
-        goto OnErrorExit;
-    }
-
     fdHandle = open(filename, O_RDONLY);
     if (fdHandle <= 0) {
-        afb_req_fail_f (request, "fileread-error", "File not accessible: '%s' err=%s", filename, strerror(fdHandle));
+        AFB_ERROR("File not accessible: '%s' err=%s", filename, strerror(fdHandle));
         goto OnErrorExit;
     }
 
@@ -342,13 +336,13 @@ STATIC UcsXmlVal_t* ParseFile(struct afb_req request) {
     xmlBuffer[readSize] = '\0'; /* In any case, terminate it. */
 
     if (readSize != fdStat.st_size)  {
-        afb_req_fail_f (request, "fileread-fail", "File to read fullfile '%s' size(%d!=%d)", filename, (int)readSize, (int)fdStat.st_size);
+        AFB_ERROR("File to read fullfile '%s' size(%d!=%d)", filename, (int)readSize, (int)fdStat.st_size);
         goto OnErrorExit;
     }
 
     ucsConfig = UcsXml_Parse(xmlBuffer);
     if (!ucsConfig)  {
-        afb_req_fail_f (request, "filexml-error", "File XML invalid: '%s'", filename);
+        AFB_ERROR("File XML invalid: '%s'", filename);
         goto OnErrorExit;
     }
 
@@ -358,22 +352,25 @@ STATIC UcsXmlVal_t* ParseFile(struct afb_req request) {
     return NULL;
 }
 
-PUBLIC void ucs2_initialise (struct afb_req request) {
+PUBLIC int StartConfiguration(const char *filename) {
     static UcsXmlVal_t *ucsConfig;
     static ucsContextT ucsContext;
 
     sd_event_source *evtSource;
     int err;
-
+    
     /* Read and parse XML file */
-    ucsConfig = ParseFile (request);
-    if (NULL == ucsConfig) goto OnErrorExit;
+    ucsConfig = ParseFile(filename);
+    if (NULL == ucsConfig) {
+        AFB_ERROR ("Cannot access or load file: '%s'", filename);
+        goto OnErrorExit;
+    }
 
     /* When ucsContextS is set, do not initalize UNICENS, CDEVs or system hooks, just load new XML */
     if (!ucsContextS)
     {
         if (!ucsContextS && !InitializeCdevs(&ucsContext))  {
-            afb_req_fail_f (request, "devnit-error", "Fail to initialise device [rx=%s tx=%s]", CONTROL_CDEV_RX, CONTROL_CDEV_TX);
+            AFB_ERROR ("Fail to initialise device [rx=%s tx=%s]", CONTROL_CDEV_RX, CONTROL_CDEV_TX);
             goto OnErrorExit;
         }
 
@@ -383,7 +380,7 @@ PUBLIC void ucs2_initialise (struct afb_req request) {
         /* register aplayHandle file fd into binder mainloop */
         err = sd_event_add_io(afb_daemon_get_event_loop(), &evtSource, ucsContext.rx.fileHandle, EPOLLIN, onReadCB, &ucsContext);
         if (err < 0) {
-            afb_req_fail_f (request, "register-mainloop", "Cannot hook events to mainloop");
+            AFB_ERROR ("Cannot hook events to mainloop");
             goto OnErrorExit;
         }
 
@@ -392,7 +389,26 @@ PUBLIC void ucs2_initialise (struct afb_req request) {
     }
     /* Initialise UNICENS with parsed config */
     if (!UCSI_NewConfig(&ucsContext.ucsiData, ucsConfig))   {
-        afb_req_fail_f (request, "UNICENS-init", "Fail to initialize UNICENS");
+        AFB_ERROR ("Fail to initialize UNICENS");
+        goto OnErrorExit;
+    }
+    
+    return 0;
+
+ OnErrorExit:
+    return -1;
+}
+
+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;
     }
     
@@ -624,11 +640,18 @@ PUBLIC void ucs2_writei2c (struct afb_req request) {
 }
 
 PUBLIC int ucs2_initbinding(void) {
-    char *config_file = GetDefaultConfig();
-    if (config_file != NULL) {
+#ifndef DISABLE_AUTOSTART
+    char *filename = GetDefaultConfig();
+    if (filename != NULL) {
         
-        free(config_file);
+        AFB_NOTICE("AUTO-LOAD configuration: %s", filename);
+        if (StartConfiguration(filename) == 0) {
+            AFB_NOTICE("AUTO-LOAD successful");
+        } else {
+            AFB_NOTICE("AUTO-LOAD failed");
+        }
+        free(filename);
     }
-    
+#endif   
     return 0;
 }