ua_architecture_functions.c 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This work is licensed under a Creative Commons CCZero 1.0 Universal License.
  2. * See http://creativecommons.org/publicdomain/zero/1.0/ for more information.
  3. *
  4. * Copyright 2018 (c) Jose Cabral, fortiss GmbH
  5. */
  6. #ifdef UA_ARCHITECTURE_PUBSUBTSN
  7. #include <open62541/types.h>
  8. /* Global malloc singletons */
  9. #ifdef UA_ENABLE_MALLOC_SINGLETON
  10. void * (*UA_globalMalloc)(size_t size) = malloc;
  11. void (*UA_globalFree)(void *ptr) = free;
  12. void * (*UA_globalCalloc)(size_t nelem, size_t elsize) = calloc;
  13. void * (*UA_globalRealloc)(void *ptr, size_t size) = realloc;
  14. #endif
  15. unsigned int UA_socket_set_blocking(UA_SOCKET sockfd){
  16. int opts = fcntl(sockfd, F_GETFL);
  17. if(opts < 0 || fcntl(sockfd, F_SETFL, opts & (~O_NONBLOCK)) < 0)
  18. return UA_STATUSCODE_BADINTERNALERROR;
  19. return UA_STATUSCODE_GOOD;
  20. }
  21. unsigned int UA_socket_set_nonblocking(UA_SOCKET sockfd){
  22. int opts = fcntl(sockfd, F_GETFL);
  23. if(opts < 0 || fcntl(sockfd, F_SETFL, opts | O_NONBLOCK) < 0)
  24. return UA_STATUSCODE_BADINTERNALERROR;
  25. return UA_STATUSCODE_GOOD;
  26. }
  27. void UA_initialize_architecture_network(void){
  28. }
  29. void UA_deinitialize_architecture_network(void){
  30. }
  31. unsigned int scheduledTrafficClass=7; // valid values are in the range [1,7]; 1−low priority, 7−high priority
  32. int socket_TSN(int domain, int type, int protocol){
  33. int socketfd = socket(domain, type, protocol);
  34. if(type == SOCK_STREAM) {
  35. printf("socket_TSN: creating a TCP socket\n");
  36. } else if(type == SOCK_DGRAM) {
  37. // TFR: It is assumed that UDP sockets are only used for PubSub and shell be sent as scheduled traffic with highest priority
  38. // TFR: This assumption may not be accurate in any circumstance, but is goot enough for testing at the moment
  39. printf("socket_TSN: creating a UDP socket\n");
  40. int res = setsockopt(socketfd, SOL_SOCKET, SO_PRIORITY, &scheduledTrafficClass, sizeof(scheduledTrafficClass));
  41. if(res != 0){
  42. fprintf(stderr, "Could not set socket priority. Failed with error code %d\n", res);
  43. return EXIT_FAILURE;
  44. } else {
  45. printf("socket_TSN: Successfully set priority for socket %d to 7\n", socketfd);
  46. }
  47. } else {
  48. printf("socket_TSN: creating a socket of unknown type\n");
  49. }
  50. return socketfd;
  51. }
  52. int bind_TSN(int sockfd, const struct sockaddr *addr, socklen_t addrlen){
  53. // printf("bind_TSN\n");
  54. // char *ip = inet_ntoa(addr->sin_addr);
  55. // printf("bind_TSN to %s\n", ip);
  56. // TFR TODO: Check if the socket should be used for scheduled traffic based on its IP address
  57. // TFR TOOD: If so, set the SO_PRIORITY to 7
  58. /*
  59. int traffic_class=7 // valid values are in the range [1,7]; 1−low priority, 7−high priority
  60. if(setsockopt(socketfd, SOL_SOCKET, SO_PRIORITY, &traffic_class, sizeof(traffic_class)) != 0)
  61. {
  62. fprintf(stderr,"Could not set socket priority\n");
  63. exit(-3);
  64. }
  65. */
  66. return bind(sockfd, addr, addrlen);
  67. }
  68. ssize_t send_TSN(int sockfd, const void *buf, size_t len, int flags){
  69. return send(sockfd, buf, len, flags);
  70. }
  71. ssize_t sendto_TSN(int sockfd, const void *buf, size_t len, int flags, const struct sockaddr *dest_addr, socklen_t addrlen){
  72. return sendto(sockfd, buf, len, flags, dest_addr, addrlen);
  73. }
  74. #endif /* UA_ARCHITECTURE_PUBSUBTSN */