12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
- * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
- *
- * Copyright 2018 (c) Jose Cabral, fortiss GmbH
- */
- #ifdef UA_ARCHITECTURE_PUBSUBTSN
- #include <open62541/types.h>
- /* Global malloc singletons */
- #ifdef UA_ENABLE_MALLOC_SINGLETON
- void * (*UA_globalMalloc)(size_t size) = malloc;
- void (*UA_globalFree)(void *ptr) = free;
- void * (*UA_globalCalloc)(size_t nelem, size_t elsize) = calloc;
- void * (*UA_globalRealloc)(void *ptr, size_t size) = realloc;
- #endif
- unsigned int UA_socket_set_blocking(UA_SOCKET sockfd){
- int opts = fcntl(sockfd, F_GETFL);
- if(opts < 0 || fcntl(sockfd, F_SETFL, opts & (~O_NONBLOCK)) < 0)
- return UA_STATUSCODE_BADINTERNALERROR;
- return UA_STATUSCODE_GOOD;
- }
- unsigned int UA_socket_set_nonblocking(UA_SOCKET sockfd){
- int opts = fcntl(sockfd, F_GETFL);
- if(opts < 0 || fcntl(sockfd, F_SETFL, opts | O_NONBLOCK) < 0)
- return UA_STATUSCODE_BADINTERNALERROR;
- return UA_STATUSCODE_GOOD;
- }
- void UA_initialize_architecture_network(void){
- }
- void UA_deinitialize_architecture_network(void){
- }
- unsigned int scheduledTrafficClass=7; // valid values are in the range [1,7]; 1−low priority, 7−high priority
- int socket_TSN(int domain, int type, int protocol){
- int socketfd = socket(domain, type, protocol);
- if(type == SOCK_STREAM) {
- printf("socket_TSN: creating a TCP socket\n");
- } else if(type == SOCK_DGRAM) {
- // TFR: It is assumed that UDP sockets are only used for PubSub and shell be sent as scheduled traffic with highest priority
- // TFR: This assumption may not be accurate in any circumstance, but is goot enough for testing at the moment
- printf("socket_TSN: creating a UDP socket\n");
- int res = setsockopt(socketfd, SOL_SOCKET, SO_PRIORITY, &scheduledTrafficClass, sizeof(scheduledTrafficClass));
- if(res != 0){
- fprintf(stderr, "Could not set socket priority. Failed with error code %d\n", res);
- return EXIT_FAILURE;
- } else {
- printf("socket_TSN: Successfully set priority for socket %d to 7\n", socketfd);
- }
- } else {
- printf("socket_TSN: creating a socket of unknown type\n");
- }
- return socketfd;
- }
- int bind_TSN(int sockfd, const struct sockaddr *addr, socklen_t addrlen){
- // printf("bind_TSN\n");
- // char *ip = inet_ntoa(addr->sin_addr);
- // printf("bind_TSN to %s\n", ip);
- // TFR TODO: Check if the socket should be used for scheduled traffic based on its IP address
- // TFR TOOD: If so, set the SO_PRIORITY to 7
- /*
- int traffic_class=7 // valid values are in the range [1,7]; 1−low priority, 7−high priority
- if(setsockopt(socketfd, SOL_SOCKET, SO_PRIORITY, &traffic_class, sizeof(traffic_class)) != 0)
- {
- fprintf(stderr,"Could not set socket priority\n");
- exit(-3);
- }
- */
- return bind(sockfd, addr, addrlen);
- }
- ssize_t send_TSN(int sockfd, const void *buf, size_t len, int flags){
- return send(sockfd, buf, len, flags);
- }
- ssize_t sendto_TSN(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen){
- return sendto(sockfd, buf, len, flags, dest_addr, addrlen);
- }
- #endif /* UA_ARCHITECTURE_PUBSUBTSN */
|