From b3c9ea473a3a37cc31df2343384d54c9ce745dee Mon Sep 17 00:00:00 2001 From: Josh Polansky Date: Tue, 7 Jun 2022 13:21:48 -0700 Subject: [PATCH 1/2] Implement test semaphore functions --- ANSIC.lby | 2 +- OMJSON.fun | 16 ++++++++++++++++ jsonWebSocketServer.c | 32 +++++++++++++++++++++++++++++++- 3 files changed, 48 insertions(+), 2 deletions(-) diff --git a/ANSIC.lby b/ANSIC.lby index 85db5c2..80bc7ff 100644 --- a/ANSIC.lby +++ b/ANSIC.lby @@ -1,6 +1,6 @@  - + LICENSE.txt VersionHistory.txt diff --git a/OMJSON.fun b/OMJSON.fun index 0de4ffa..9eb31f5 100644 --- a/OMJSON.fun +++ b/OMJSON.fun @@ -22,6 +22,8 @@ FUNCTION_BLOCK jsonWebSocketServer (*Serve variables via JSON and WebSockets*) ( END_VAR VAR_OUTPUT ClientInfo : ARRAY[0..JSON_MAI_CLIENTS] OF jsonWSS_client_info_typ; + lock : lockComms; + unlock : unlockComms; Error : BOOL; ErrorID : UINT; ErrorString : STRING[JSON_STRLEN_ERROR]; @@ -196,3 +198,17 @@ FUNCTION jsonEscape : UDINT (*Escape JSON string*) reentry : UDINT; END_VAR END_FUNCTION + +FUNCTION_BLOCK lockComms + VAR_INPUT + key : UDINT; + active : BOOL; + END_VAR +END_FUNCTION_BLOCK + +FUNCTION_BLOCK unlockComms + VAR_INPUT + key : UDINT; + active : BOOL; + END_VAR +END_FUNCTION_BLOCK diff --git a/jsonWebSocketServer.c b/jsonWebSocketServer.c index 36ac713..074cf68 100644 --- a/jsonWebSocketServer.c +++ b/jsonWebSocketServer.c @@ -16,6 +16,7 @@ #endif #include "OMJSON.h" +#include #include #include "jsonAux.h" #include "sha1.h" @@ -88,6 +89,10 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) // TODO: Clear buffers on init + RtkCreateCriticalSection( NULL, &t->lock.key ); + t->lock.active = 1; + memcpy( &t->unlock, &t->lock, sizeof(t->lock)); + // Set up tcpServer t->internal.tcpServer.IN.CFG.Mode = TCPCOMM_MODE_SERVER; strcpy(t->internal.tcpServer.IN.CFG.LocalIPAddress, t->ServerIP); @@ -108,6 +113,8 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) } // if(!initialized) + memcpy( &t->unlock, &t->lock, sizeof(t->lock)); + // Check license //--------------- @@ -121,8 +128,10 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) // Manage TCP server //------------------- - + + lockComms( &t->lock ); TCPManageConnection(&t->internal.tcpServer); + unlockComms( &t->unlock ); if (t->internal.tcpServer.OUT.NewConnectionAvailable) { @@ -183,7 +192,11 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) t->internal.client[index].debug.oldDataReceived = t->internal.client[index].tcpStream.OUT.DataReceived; + lockComms( &t->lock ); + TCPStreamReceive(&t->internal.client[index].tcpStream); + + unlockComms( &t->unlock ); if (t->internal.client[index].debug.oldDataReceived && t->internal.client[index].tcpStream.OUT.DataReceived) { // Break here to figure out what happens @@ -531,7 +544,11 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) } // NewDataAvailable && !Sending + lockComms( &t->lock ); + TCPStreamSend(&t->internal.client[index].tcpStream); + + unlockComms( &t->unlock ); t->internal.client[index].tcpStream.IN.CMD.Send = 0; @@ -556,3 +573,16 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) } // End For Clients } // End Fn + + +void lockComms(struct lockComms* inst){ + if( inst->key && inst->active){ + RtkEnterCriticalSection( inst->key ); + } +} +void unlockComms(struct unlockComms* inst){ + + if( inst->key && inst->active ){ + RtkLeaveCriticalSection( inst->key ); + } +} From 7401a856815589d53d0aaab24a86a3cd5ffa88f5 Mon Sep 17 00:00:00 2001 From: Josh Polansky Date: Thu, 16 Jun 2022 13:53:13 -0700 Subject: [PATCH 2/2] Use comma calls --- OMJSON.fun | 15 --------------- jsonWebSocketServer.c | 31 ++++++------------------------- 2 files changed, 6 insertions(+), 40 deletions(-) diff --git a/OMJSON.fun b/OMJSON.fun index 9eb31f5..cf68c6a 100644 --- a/OMJSON.fun +++ b/OMJSON.fun @@ -22,8 +22,6 @@ FUNCTION_BLOCK jsonWebSocketServer (*Serve variables via JSON and WebSockets*) ( END_VAR VAR_OUTPUT ClientInfo : ARRAY[0..JSON_MAI_CLIENTS] OF jsonWSS_client_info_typ; - lock : lockComms; - unlock : unlockComms; Error : BOOL; ErrorID : UINT; ErrorString : STRING[JSON_STRLEN_ERROR]; @@ -199,16 +197,3 @@ FUNCTION jsonEscape : UDINT (*Escape JSON string*) END_VAR END_FUNCTION -FUNCTION_BLOCK lockComms - VAR_INPUT - key : UDINT; - active : BOOL; - END_VAR -END_FUNCTION_BLOCK - -FUNCTION_BLOCK unlockComms - VAR_INPUT - key : UDINT; - active : BOOL; - END_VAR -END_FUNCTION_BLOCK diff --git a/jsonWebSocketServer.c b/jsonWebSocketServer.c index 074cf68..f361882 100644 --- a/jsonWebSocketServer.c +++ b/jsonWebSocketServer.c @@ -16,7 +16,6 @@ #endif #include "OMJSON.h" -#include #include #include "jsonAux.h" #include "sha1.h" @@ -89,9 +88,6 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) // TODO: Clear buffers on init - RtkCreateCriticalSection( NULL, &t->lock.key ); - t->lock.active = 1; - memcpy( &t->unlock, &t->lock, sizeof(t->lock)); // Set up tcpServer t->internal.tcpServer.IN.CFG.Mode = TCPCOMM_MODE_SERVER; @@ -113,9 +109,6 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) } // if(!initialized) - memcpy( &t->unlock, &t->lock, sizeof(t->lock)); - - // Check license //--------------- @@ -129,9 +122,9 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) // Manage TCP server //------------------- - lockComms( &t->lock ); + lockComms( 0 ); TCPManageConnection(&t->internal.tcpServer); - unlockComms( &t->unlock ); + unlockComms( 0 ); if (t->internal.tcpServer.OUT.NewConnectionAvailable) { @@ -192,11 +185,11 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) t->internal.client[index].debug.oldDataReceived = t->internal.client[index].tcpStream.OUT.DataReceived; - lockComms( &t->lock ); + lockComms( 0 ); TCPStreamReceive(&t->internal.client[index].tcpStream); - unlockComms( &t->unlock ); + unlockComms( 0 ); if (t->internal.client[index].debug.oldDataReceived && t->internal.client[index].tcpStream.OUT.DataReceived) { // Break here to figure out what happens @@ -544,11 +537,9 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) } // NewDataAvailable && !Sending - lockComms( &t->lock ); - + lockComms( 0 ); TCPStreamSend(&t->internal.client[index].tcpStream); - - unlockComms( &t->unlock ); + unlockComms( 0 ); t->internal.client[index].tcpStream.IN.CMD.Send = 0; @@ -575,14 +566,4 @@ void jsonWebSocketServer(struct jsonWebSocketServer* t) } // End Fn -void lockComms(struct lockComms* inst){ - if( inst->key && inst->active){ - RtkEnterCriticalSection( inst->key ); - } -} -void unlockComms(struct unlockComms* inst){ - if( inst->key && inst->active ){ - RtkLeaveCriticalSection( inst->key ); - } -}