Browse Source

cosmetics, rework of server.c, relates to #240

Stasik0 10 years ago
parent
commit
6c0616bb79
2 changed files with 28 additions and 40 deletions
  1. 18 30
      examples/server.c
  2. 10 10
      src/server/ua_services_view.c

+ 18 - 30
examples/server.c

@@ -151,12 +151,8 @@ static UA_StatusCode writeLedStatus(void *handle, const UA_Variant *data) {
 	return UA_STATUSCODE_GOOD;
 }
 
-static void printLedStatus(UA_Server *server, void *data) {
-	UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, ledStatus ? "LED is on" : "LED is off");
-}
-
 static void stopHandler(int sign) {
-	printf("Received Ctrl-C\n");
+    UA_LOG_INFO(logger, UA_LOGCATEGORY_SERVER, "Received Ctrl-C\n");
 	running = 0;
 }
 
@@ -199,11 +195,6 @@ int main(int argc, char** argv) {
     UA_ByteString_deleteMembers(&certificate);
 	UA_Server_addNetworkLayer(server, ServerNetworkLayerTCP_new(UA_ConnectionConfig_standard, 16664));
 
-	// print the status every 2 sec
-	UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL,
-			.work.methodCall = {.method = printLedStatus, .data = NULL} };
-	UA_Server_addRepeatedWorkItem(server, &work, 2000, NULL);
-
 	// add node with the datetime data source
 	UA_DataSource dateDataSource = (UA_DataSource)
         {.handle = NULL,
@@ -215,10 +206,9 @@ int main(int argc, char** argv) {
                                         UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
                                         UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES));
 
-	if(!(temperatureFile = fopen("/sys/class/thermal/thermal_zone0/temp", "r"))){
-		UA_LOG_WARNING(logger, UA_LOGCATEGORY_USERLAND, "[Linux specific] Can not open temperature file, no temperature node will be added");
-	} else {
-		// add node with the datetime data source
+	//cpu temperature monitoring for linux machines
+	if((temperatureFile = fopen("/sys/class/thermal/thermal_zone0/temp", "r"))){
+		// add node with the data source
 		UA_DataSource temperatureDataSource = (UA_DataSource)
     	    {.handle = NULL,
 			.read = readTemperature,
@@ -230,11 +220,9 @@ int main(int argc, char** argv) {
                                             UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES));
 	}
 
-	if (	!(triggerFile = fopen("/sys/class/leds/led0/trigger", "w"))
-		|| 	!(ledFile = fopen("/sys/class/leds/led0/brightness", "w"))) {
-		UA_LOG_WARNING(logger, UA_LOGCATEGORY_USERLAND, "[Raspberry Pi specific] Can not open trigger or LED file (try to run server with sudo if on a Raspberry PI)");
-		UA_LOG_WARNING(logger, UA_LOGCATEGORY_USERLAND, "An LED node will be added but no physical LED will be operated");
-	} else {
+	//LED control for rpi
+	if (	(triggerFile = fopen("/sys/class/leds/led0/trigger", "w"))
+		&& 	(ledFile = fopen("/sys/class/leds/led0/brightness", "w"))) {
 		//setting led mode to manual
 		fprintf(triggerFile, "%s", "none");
 		fflush(triggerFile);
@@ -242,18 +230,18 @@ int main(int argc, char** argv) {
 		//turning off led initially
 		fprintf(ledFile, "%s", "1");
 		fflush(ledFile);
-	}
 
-	// add node with the LED status data source
-	UA_DataSource ledStatusDataSource = (UA_DataSource)
-   		{.handle = NULL,
-		.read = readLedStatus,
-		.release = releaseLedStatus,
-		.write = writeLedStatus};
-	const UA_QualifiedName statusName = UA_QUALIFIEDNAME(0, "status LED");
-	UA_Server_addDataSourceVariableNode(server, ledStatusDataSource, statusName, UA_NODEID_NULL,
-                                        UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
-                                        UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES));
+        // add node with the LED status data source
+        UA_DataSource ledStatusDataSource = (UA_DataSource)
+            {.handle = NULL,
+            .read = readLedStatus,
+            .release = releaseLedStatus,
+            .write = writeLedStatus};
+        const UA_QualifiedName statusName = UA_QUALIFIEDNAME(0, "status LED");
+        UA_Server_addDataSourceVariableNode(server, ledStatusDataSource, statusName, UA_NODEID_NULL,
+                                            UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
+                                            UA_NODEID_NUMERIC(0, UA_NS0ID_ORGANIZES));
+    }
 
 	// add a static variable node to the adresspace
     UA_Variant *myIntegerVariant = UA_Variant_new();

+ 10 - 10
src/server/ua_services_view.c

@@ -136,6 +136,14 @@ static UA_StatusCode findsubtypes(UA_NodeStore *ns, const UA_NodeId *root, UA_No
     return UA_STATUSCODE_GOOD;
 }
 
+static void removeCp(struct ContinuationPointEntry *cp, UA_Session* session){
+    session->availableContinuationPoints++;
+    UA_ByteString_deleteMembers(&cp->identifier);
+    UA_BrowseDescription_deleteMembers(&cp->browseDescription);
+    LIST_REMOVE(cp, pointers);
+    UA_free(cp);
+}
+
 /**
  * Results for a single browsedescription. This is the inner loop for both Browse and BrowseNext
  * @param session Session to save continuationpoints
@@ -262,11 +270,7 @@ static void browse(UA_Session *session, UA_NodeStore *ns, struct ContinuationPoi
     if(cp) {
         if(referencesIndex == node->referencesSize) {
             /* all done, remove a finished continuationPoint */
-            session->availableContinuationPoints++;
-            UA_ByteString_deleteMembers(&cp->identifier);
-            UA_BrowseDescription_deleteMembers(&cp->browseDescription);
-            LIST_REMOVE(cp, pointers);
-            UA_free(cp);
+            removeCp(cp, session);
         } else {
             /* update the cp and return the cp identifier */
             cp->continuationIndex += referencesCount;
@@ -382,11 +386,7 @@ void Service_BrowseNext(UA_Server *server, UA_Session *session, const UA_BrowseN
            struct ContinuationPointEntry *cp = UA_NULL;
            LIST_FOREACH(cp, &session->continuationPoints, pointers) {
                if(UA_ByteString_equal(&cp->identifier, &request->continuationPoints[i])) {
-                   LIST_REMOVE(cp, pointers);
-                   UA_ByteString_deleteMembers(&cp->identifier);
-                   UA_BrowseDescription_deleteMembers(&cp->browseDescription);
-                   UA_free(cp);
-                   session->availableContinuationPoints++;
+                   removeCp(cp, session);
                    break;
                }
            }