opcuaServer.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. ============================================================================
  3. Name : opcuaServer.c
  4. Author :
  5. Version :
  6. Copyright : Your copyright notice
  7. Description : lala
  8. ============================================================================
  9. */
  10. #ifdef RASPI
  11. #define _POSIX_C_SOURCE 199309L //to use nanosleep
  12. #include <pthread.h>
  13. #endif
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include "networklayer.h"
  17. #include "ua_application.h"
  18. #include <sys/mman.h>
  19. #include <sys/types.h>
  20. #include <sys/wait.h>
  21. #include <unistd.h>
  22. #include <time.h>
  23. #include <fcntl.h>
  24. #ifdef RASPI
  25. #include "raspberrypi_io.h"
  26. #endif
  27. UA_Int32 serverCallback(void * arg) {
  28. char *name = (char *) arg;
  29. printf("%s does whatever servers do\n",name);
  30. Namespace* ns0 = (Namespace*)UA_indexedList_find(appMockup.namespaces, 0)->payload;
  31. UA_Int32 retval;
  32. UA_Node const * node;
  33. UA_ExpandedNodeId serverStatusNodeId = NS0EXPANDEDNODEID(2256);
  34. retval = Namespace_get(ns0, &(serverStatusNodeId.nodeId),&node, UA_NULL);
  35. if(retval == UA_SUCCESS){
  36. ((UA_ServerStatusDataType*)(((UA_VariableNode*)node)->value.data))->currentTime = UA_DateTime_now();
  37. }
  38. const UA_Node *foundNode1 = UA_NULL;
  39. Namespace_Entry_Lock *lock;
  40. //node which should be filled with data (float value)
  41. UA_NodeId tmpNodeId1;
  42. tmpNodeId1.encodingByte = UA_NODEIDTYPE_TWOBYTE;
  43. tmpNodeId1.identifier.numeric = 108;
  44. tmpNodeId1.namespace = 0;
  45. if(Namespace_get(ns0,&tmpNodeId1, &foundNode1,&lock) != UA_SUCCESS){
  46. return UA_ERROR;
  47. }
  48. #ifdef RASPI
  49. #else
  50. *((float*)((UA_VariableNode *)foundNode1)->value.data) = *((float*)((UA_VariableNode *)foundNode1)->value.data) + 0.2f;
  51. #endif
  52. return UA_SUCCESS;
  53. }
  54. #ifdef RASPI
  55. static float sharedTemperature = 0;
  56. static UA_Boolean sharedLED1 = 0;
  57. static UA_Boolean sharedLED2 = 0;
  58. static void* ioloop(void* ptr){
  59. {
  60. if(initIO())
  61. {
  62. printf("ERROR while initializing the IO \n");
  63. }
  64. else
  65. {
  66. struct timespec t = {0, 50*1000*1000};
  67. int j = 0;
  68. for(;j<25;j++){
  69. writePin(j%2,0);
  70. writePin((j+1)%2,2);
  71. nanosleep(&t, NULL);
  72. }
  73. writePin(0,0);
  74. writePin(0,2);
  75. printf("IO successfully initalized \n");
  76. }
  77. printf("done - io init \n");
  78. struct timespec t = {0, 50*1000*1000};
  79. while(1){
  80. readTemp(&sharedTemperature);
  81. writePin(sharedLED1,0);
  82. writePin(sharedLED2,2);
  83. nanosleep(&t, NULL);
  84. }
  85. }
  86. return UA_NULL;
  87. }
  88. #endif
  89. int main(int argc, char** argv) {
  90. appMockup_init();
  91. NL_data* nl = NL_init(&NL_Description_TcpBinary,16664);
  92. struct timeval tv = {1, 0}; // 1 second
  93. #ifdef RASPI
  94. Namespace* ns0 = (Namespace*)UA_indexedList_find(appMockup.namespaces, 0)->payload;
  95. const UA_Node *foundNode1 = UA_NULL;
  96. const UA_Node *foundNode2 = UA_NULL;
  97. const UA_Node *foundNode3 = UA_NULL;
  98. Namespace_Entry_Lock *lock;
  99. //node which should be filled with data (float value)
  100. UA_NodeId tmpNodeId1;
  101. UA_NodeId tmpNodeId2;
  102. UA_NodeId tmpNodeId3;
  103. tmpNodeId1.encodingByte = UA_NODEIDTYPE_TWOBYTE;
  104. tmpNodeId1.identifier.numeric = 108;
  105. tmpNodeId1.namespace = 0;
  106. tmpNodeId2.encodingByte = UA_NODEIDTYPE_TWOBYTE;
  107. tmpNodeId2.identifier.numeric = 109;
  108. tmpNodeId2.namespace = 0;
  109. tmpNodeId3.encodingByte = UA_NODEIDTYPE_TWOBYTE;
  110. tmpNodeId3.identifier.numeric = 110;
  111. tmpNodeId3.namespace = 0;
  112. if(Namespace_get(ns0,&tmpNodeId1, &foundNode1,&lock) != UA_SUCCESS){
  113. _exit(1);
  114. }
  115. if(Namespace_get(ns0,&tmpNodeId2, &foundNode2,&lock) != UA_SUCCESS){
  116. _exit(1);
  117. }
  118. if(Namespace_get(ns0,&tmpNodeId3, &foundNode3,&lock) != UA_SUCCESS){
  119. _exit(1);
  120. }
  121. ((UA_VariableNode *)foundNode1)->value.data = &sharedTemperature;
  122. ((UA_VariableNode *)foundNode2)->value.data = &sharedLED1;
  123. ((UA_VariableNode *)foundNode3)->value.data = &sharedLED2;
  124. pthread_t t;
  125. pthread_create(&t, NULL, &ioloop, UA_NULL);
  126. printf("raspi enabled \n");
  127. //pid_t i = fork();
  128. //printf("process id i=%d \n",i);
  129. #endif
  130. NL_msgLoop(nl, &tv, serverCallback, argv[0]);
  131. }