Browse Source

fix two coverity warnings

Julius Pfrommer 10 years ago
parent
commit
83e3af6ed0
5 changed files with 52 additions and 22 deletions
  1. 1 1
      examples/opcuaServer.c
  2. 30 9
      include/ua_server.h
  3. 2 0
      include/ua_types.h
  4. 19 10
      src/server/ua_server_worker.c
  5. 0 2
      src/ua_util.h

+ 1 - 1
examples/opcuaServer.c

@@ -69,7 +69,7 @@ int main(int argc, char** argv) {
 
 
     UA_WorkItem work = {.type = UA_WORKITEMTYPE_METHODCALL, .work.methodCall = {.method = testCallback, .data = UA_NULL} };
-    UA_Server_addRepeatedWorkItem(server, &work, 20000000); // call every 2 sec
+    UA_Server_addRepeatedWorkItem(server, &work, 20000000, UA_NULL); // call every 2 sec
 
 	//add a node to the adresspace
     UA_Int32 *myInteger = UA_Int32_new();

+ 30 - 9
include/ua_server.h

@@ -100,21 +100,42 @@ typedef struct UA_WorkItem {
 } UA_WorkItem;
 
 /**
- * Add work that is executed at a given time in the future. If the indicated
- * time lies in the past, the work is executed immediately.
+ * @param server The server object.
  *
- * The work pointer is not freed but copied to an internal representation
+ * @param work Pointer to the WorkItem that shall be added. The pointer is not
+ *        freed but copied to an internal representation.
+ *
+ * @param time The time when the work shall be executed. If the time lies in the
+ *        past, the work will be executed in the next iteration of the server's
+ *        main loop
+ *
+ * @param resultWorkGuid Upon success, the pointed value is set to the guid of
+ *        the workitem in the server queue. This can be used to cancel the work
+ *        later on. If the pointer is null, the guid is not set.
+ *
+ * @return Upon sucess, UA_STATUSCODE_GOOD is returned. An error code otherwise.
  */
-UA_Guid UA_EXPORT UA_Server_addTimedWorkItem(UA_Server *server, UA_WorkItem *work, UA_DateTime time);
+UA_StatusCode UA_EXPORT UA_Server_addTimedWorkItem(UA_Server *server, const UA_WorkItem *work,
+                                                   UA_DateTime time, UA_Guid *resultWorkGuid);
 
 /**
- *  Add work that is executed repeatedly with the given interval (in 100ns). If
- *  work with the same repetition interval already exists, the first execution
- *  might occur sooner.
+ * @param server The server object.
+ *
+ * @param work Pointer to the WorkItem that shall be added. The pointer is not
+ *        freed but copied to an internal representation.
+ *
+ * @param interval The work that is executed repeatedly with the given interval
+ *        (in 100ns). If work with the same repetition interval already exists,
+ *        the first execution might occur sooner.
+ *
+ * @param resultWorkGuid Upon success, the pointed value is set to the guid of
+ *        the workitem in the server queue. This can be used to cancel the work
+ *        later on. If the pointer is null, the guid is not set.
  *
- * The work pointer is not freed but copied to an internal representation
+ * @return Upon sucess, UA_STATUSCODE_GOOD is returned. An error code otherwise.
  */
-UA_Guid UA_EXPORT UA_Server_addRepeatedWorkItem(UA_Server *server, UA_WorkItem *work, UA_UInt32 interval);
+UA_StatusCode UA_EXPORT UA_Server_addRepeatedWorkItem(UA_Server *server, const UA_WorkItem *work,
+                                                      UA_UInt32 interval, UA_Guid *resultWorkGuid);
 
 /** Remove timed or repeated work */
 UA_Boolean UA_EXPORT UA_Server_removeWorkItem(UA_Server *server, UA_Guid workId);

+ 2 - 0
include/ua_types.h

@@ -63,6 +63,8 @@ extern "C" {
 
 /** @brief A two-state logical value (true or false). */
 typedef bool UA_Boolean;
+#define UA_TRUE true
+#define UA_FALSE false
 
 /** @brief An integer value between -129 and 127. */
 typedef int8_t UA_SByte;

+ 19 - 10
src/server/ua_server_worker.c

@@ -133,8 +133,8 @@ struct UA_TimedWork {
 };
 
 /* The item is copied and not freed by this function. */
-static UA_Guid addTimedWork(UA_Server *server, UA_WorkItem *item, UA_DateTime firstTime,
-                            UA_UInt32 repetitionInterval) {
+static UA_StatusCode addTimedWork(UA_Server *server, const UA_WorkItem *item, UA_DateTime firstTime,
+                                  UA_UInt32 repetitionInterval, UA_Guid *resultWorkGuid) {
     UA_TimedWork *tw, *lastTw = UA_NULL;
 
     // search for matching entry
@@ -156,11 +156,15 @@ static UA_Guid addTimedWork(UA_Server *server, UA_WorkItem *item, UA_DateTime fi
         tw->workIds = UA_realloc(tw->workIds, sizeof(UA_Guid)*tw->workSize);
         tw->work[tw->workSize-1] = *item;
         tw->workIds[tw->workSize-1] = UA_Guid_random(&server->random_seed);
-        return tw->workIds[tw->workSize-1];
+        if(resultWorkGuid)
+            *resultWorkGuid = tw->workIds[tw->workSize-1];
+        return UA_STATUSCODE_GOOD;
     }
 
     // create a new entry
-    tw = UA_malloc(sizeof(UA_TimedWork));
+    if(!(tw = UA_malloc(sizeof(UA_TimedWork))))
+        return UA_STATUSCODE_BADOUTOFMEMORY;
+
     tw->workSize = 1;
     tw->time = firstTime;
     tw->repetitionInterval = repetitionInterval;
@@ -173,16 +177,21 @@ static UA_Guid addTimedWork(UA_Server *server, UA_WorkItem *item, UA_DateTime fi
     else
         LIST_INSERT_HEAD(&server->timedWork, tw, pointers);
 
-    return tw->workIds[0];
+    if(resultWorkGuid)
+        *resultWorkGuid = tw->workIds[0];
+
+    return UA_STATUSCODE_GOOD;
 }
 
 // Currently, these functions need to get the server mutex, but should be sufficiently fast
-UA_Guid UA_Server_addTimedWorkItem(UA_Server *server, UA_WorkItem *work, UA_DateTime time) {
-    return addTimedWork(server, work, time, 0);
+UA_StatusCode UA_Server_addTimedWorkItem(UA_Server *server, const UA_WorkItem *work, UA_DateTime time,
+                                         UA_Guid *resultWorkGuid) {
+    return addTimedWork(server, work, time, 0, resultWorkGuid);
 }
 
-UA_Guid UA_Server_addRepeatedWorkItem(UA_Server *server, UA_WorkItem *work, UA_UInt32 interval) {
-    return addTimedWork(server, work, UA_DateTime_now() + interval, interval);
+UA_StatusCode UA_Server_addRepeatedWorkItem(UA_Server *server, const UA_WorkItem *work, UA_UInt32 interval,
+                                            UA_Guid *resultWorkGuid) {
+    return addTimedWork(server, work, UA_DateTime_now() + interval, interval, resultWorkGuid);
 }
 
 /** Dispatches timed work, returns the timeout until the next timed work in ms */
@@ -395,7 +404,7 @@ UA_StatusCode UA_Server_run(UA_Server *server, UA_UInt16 nThreads, UA_Boolean *r
     UA_WorkItem processDelayed = {.type = UA_WORKITEMTYPE_METHODCALL,
                                   .work.methodCall = {.method = dispatchDelayedWork,
                                                       .data = UA_NULL} };
-    UA_Server_addRepeatedWorkItem(server, &processDelayed, 10000000);
+    UA_Server_addRepeatedWorkItem(server, &processDelayed, 10000000, UA_NULL);
 #endif
 
     // 2) Start the networklayers

+ 0 - 2
src/ua_util.h

@@ -15,8 +15,6 @@
 #include "ua_types.h"
 
 #define UA_NULL ((void *)0)
-#define UA_TRUE (42 == 42)
-#define UA_FALSE (!UA_TRUE)
 
 // subtract from nodeids to get from the encoding to the content
 #define UA_ENCODINGOFFSET_XML 1