4040/* OS Specific include */
4141#include "net.h"
4242#include "ringbuf.h"
43+ #ifdef __APPLE__
44+ #include <dispatch/dispatch.h>
45+ #endif
4346
4447/** @file linux/dlmstp.c Provides Linux-specific DataLink functions for MS/TP. */
4548
@@ -123,7 +126,12 @@ void dlmstp_cleanup(
123126 close (poSharedData -> RS485_Handle );
124127
125128 pthread_cond_destroy (& poSharedData -> Received_Frame_Flag );
129+ #ifdef __APPLE__
130+ dispatch_release (poSharedData -> Receive_Packet_Flag );
131+ #endif
132+ #ifdef __linux__
126133 sem_destroy (& poSharedData -> Receive_Packet_Flag );
134+ #endif
127135 pthread_cond_destroy (& poSharedData -> Master_Done_Flag );
128136 pthread_mutex_destroy (& poSharedData -> Received_Frame_Mutex );
129137 pthread_mutex_destroy (& poSharedData -> Master_Done_Mutex );
@@ -175,7 +183,12 @@ uint16_t dlmstp_receive(
175183 unsigned timeout )
176184{ /* milliseconds to wait for a packet */
177185 uint16_t pdu_len = 0 ;
186+ #ifdef __APPLE__
187+ dispatch_time_t dis_time ;
188+ #endif
189+ #ifdef __linux__
178190 struct timespec abstime ;
191+ #endif
179192 int rv = 0 ;
180193 SHARED_MSTP_DATA * poSharedData ;
181194 struct mstp_port_struct_t * mstp_port =
@@ -190,8 +203,14 @@ uint16_t dlmstp_receive(
190203 (void ) max_pdu ;
191204 /* see if there is a packet available, and a place
192205 to put the reply (if necessary) and process it */
206+ #ifdef __APPLE__
207+ dis_time = dispatch_time (DISPATCH_TIME_NOW , timeout * 1000000 );
208+ rv = dispatch_semaphore_wait (poSharedData -> Receive_Packet_Flag , dis_time );
209+ #endif
210+ #ifdef __linux__
193211 get_abstime (& abstime , timeout );
194212 rv = sem_timedwait (& poSharedData -> Receive_Packet_Flag , & abstime );
213+ #endif
195214 if (rv == 0 ) {
196215 if (poSharedData -> Receive_Packet .ready ) {
197216 if (poSharedData -> Receive_Packet .pdu_len ) {
@@ -356,7 +375,12 @@ uint16_t MSTP_Put_Receive(
356375 mstp_port -> SourceAddress );
357376 poSharedData -> Receive_Packet .pdu_len = mstp_port -> DataLength ;
358377 poSharedData -> Receive_Packet .ready = true;
378+ #ifdef __APPLE__
379+ dispatch_semaphore_signal (poSharedData -> Receive_Packet_Flag );
380+ #endif
381+ #ifdef __linux__
359382 sem_post (& poSharedData -> Receive_Packet_Flag );
383+ #endif
360384 }
361385
362386 return pdu_len ;
@@ -870,7 +894,7 @@ bool dlmstp_init(
870894 void * poPort ,
871895 char * ifname )
872896{
873- unsigned long hThread = 0 ;
897+ pthread_t hThread ;
874898 int rv = 0 ;
875899 SHARED_MSTP_DATA * poSharedData ;
876900 struct mstp_port_struct_t * mstp_port =
@@ -894,15 +918,19 @@ bool dlmstp_init(
894918 /* initialize packet queue */
895919 poSharedData -> Receive_Packet .ready = false;
896920 poSharedData -> Receive_Packet .pdu_len = 0 ;
921+ #ifdef __APPLE__
922+ poSharedData -> Receive_Packet_Flag = dispatch_semaphore_create (0 );
923+ #endif
924+ #ifdef __linux__
897925 rv = sem_init (& poSharedData -> Receive_Packet_Flag , 0 , 0 );
926+ #endif
898927 if (rv != 0 ) {
899928 fprintf (stderr ,
900929 "MS/TP Interface: %s\n cannot allocate PThread Condition.\n" ,
901930 ifname );
902931 exit (1 );
903932 }
904933
905- struct termios newtio ;
906934 printf ("RS485: Initializing %s" , poSharedData -> RS485_Port_Name );
907935 /*
908936 Open device for reading and writing.
@@ -922,6 +950,36 @@ bool dlmstp_init(
922950 /* efficient blocking for the read */
923951 fcntl (poSharedData -> RS485_Handle , F_SETFL , 0 );
924952#endif
953+
954+ #ifdef __APPLE__
955+ struct termios options ;
956+
957+ tcgetattr (poSharedData -> RS485_Handle , & options );
958+ cfsetispeed (& options , poSharedData -> RS485_Baud );
959+ cfsetospeed (& options , poSharedData -> RS485_Baud );
960+
961+ //No parity 8N1
962+ options .c_cflag &= ~PARENB ;
963+ options .c_cflag &= ~CSTOPB ;
964+ options .c_cflag &= ~CSIZE ;
965+ options .c_cflag |= CS8 ;
966+
967+ //No flow control
968+ options .c_cflag &= ~CRTSCTS ;
969+
970+ //Turn off s/w flow control
971+ options .c_iflag &= ~(IXON | IXOFF | IXANY );
972+
973+ //Turn on read and ignore ctrl lines
974+ options .c_cflag |= (CLOCAL | CREAD );
975+
976+ if ( tcsetattr (poSharedData -> RS485_Handle , TCSANOW , & options ) < 0 ) {
977+ perror (poSharedData -> RS485_Port_Name );
978+ exit (-1 );
979+ }
980+ #endif
981+ #ifdef __linux__
982+ struct termios newtio ;
925983 /* save current serial port settings */
926984 tcgetattr (poSharedData -> RS485_Handle , & poSharedData -> RS485_oldtio );
927985 /* clear struct for new port settings */
@@ -943,6 +1001,9 @@ bool dlmstp_init(
9431001 newtio .c_lflag = 0 ;
9441002 /* activate the settings for the port after flushing I/O */
9451003 tcsetattr (poSharedData -> RS485_Handle , TCSAFLUSH , & newtio );
1004+
1005+ #endif
1006+
9461007 /* flush any data waiting */
9471008 usleep (200000 );
9481009 tcflush (poSharedData -> RS485_Handle , TCIOFLUSH );
0 commit comments