network.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. //
  2. // Created by janco on 25-2-16.
  3. //
  4. #include <dev/board.h>
  5. #include <dev/debug.h>
  6. #include <sys/timer.h>
  7. #include <sys/confnet.h>
  8. #include <sys/socket.h>
  9. #include <netinet/tcp.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12. #include <time.h>
  13. #include <net/route.h>
  14. #include <dev/nicrtl.h>
  15. #include <stdio.h>
  16. #include <io.h>
  17. #include <arpa/inet.h>
  18. #include <pro/dhcp.h>
  19. #include <pro/sntp.h>
  20. #include "ntp.h"
  21. #include "network.h"
  22. #include "jsmn.h"
  23. #include "rtc.h"
  24. #include "alarm.h"
  25. bool isReceiving;
  26. bool hasNetwork;
  27. void NetworkInit() {
  28. hasNetwork = false;
  29. /* Register de internet controller. */
  30. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  31. printf("Registering failed. \n");
  32. }/* Netwerk configureren op dhcp */
  33. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  34. printf("DHCP failed. \n");
  35. }else {
  36. printf("Ik heb een internet connectie. Ip is: %s \n\n", inet_ntoa(confnet.cdn_ip_addr));
  37. }
  38. NutSleep(2000);
  39. hasNetwork = true;
  40. }
  41. char* httpGet(char address[]){
  42. isReceiving = true;
  43. NutDelay(1000);
  44. printf("\n\n #-- HTTP get -- #\n");
  45. TCPSOCKET* sock = NutTcpCreateSocket();
  46. char http[strlen(address) + 49]; //49 chars based on get command. Change this number if you change the domain name
  47. sprintf(http, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  48. int len = sizeof(http);
  49. char buffer[700];
  50. memset(buffer, 0, 700);
  51. if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  52. printf("Can't connect to server\n");
  53. }else{
  54. //FILE *stream;
  55. //stream = _fdopen((int) sock, "r b");
  56. if(NutTcpSend(sock, http, len) != len){
  57. printf("Writing headers failed.\n");
  58. NutDelay(1000);
  59. }else{
  60. printf("Headers %s writed. Now reading.", http);
  61. NutDelay(1000);
  62. NutTcpReceive(sock, buffer, sizeof(buffer));
  63. //fread(buffer, 1, sizeof(buffer), stream);
  64. NutDelay(1000);
  65. printf(buffer);
  66. };
  67. //fclose(stream);
  68. }
  69. NutTcpCloseSocket(sock);
  70. int i;
  71. int enters = 0;
  72. int t = 0;
  73. char* content = (char*) calloc(1 , sizeof(buffer));
  74. for(i = 0; i < strlen(buffer); i++)
  75. {
  76. if(enters == 4) {
  77. content[t] = buffer[i];
  78. t++;
  79. }else {
  80. if (buffer[i] == '\n' || buffer[i] == '\r') {
  81. enters++;
  82. }
  83. else {
  84. enters = 0;
  85. }
  86. }
  87. }
  88. content[t] = '\0';
  89. printf("\nContent size: %d, Content: %s \n", t, content);
  90. isReceiving = false;
  91. return content;
  92. }
  93. int getTimeZone()
  94. {
  95. char* content = httpGet("/gettimezone.php");
  96. int timezone = atoi(content);
  97. free(content);
  98. printf("%d", timezone);
  99. return timezone;
  100. }
  101. void parseAlarmJson(char* content){
  102. int r;
  103. int i;
  104. jsmn_parser p;
  105. jsmntok_t token[300]; /* We expect no more than 128 tokens */
  106. jsmn_init(&p);
  107. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  108. if (r < 0) {
  109. printf("Failed to parse JSON: %d \n", r);
  110. }else{
  111. printf("Aantal tokens found: %d \n", r);
  112. }
  113. for(i = 1; i < r; i++)
  114. {
  115. struct _tm time = GetRTCTime();
  116. for (i = i; !(i % 14 == 0); i++) {
  117. if (jsoneq(content, &token[i], "YYYY") == 0) {
  118. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  119. i++;
  120. }else if (jsoneq(content, &token[i], "MM") == 0) {
  121. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  122. i++;
  123. }else if (jsoneq(content, &token[i], "DD") == 0) {
  124. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  125. i++;
  126. }else if (jsoneq(content, &token[i], "hh") == 0) {
  127. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  128. i++;
  129. }else if (jsoneq(content, &token[i], "mm") == 0) {
  130. time.tm_min = getIntegerToken(content, &token[i + 1]);
  131. i++;
  132. }else if (jsoneq(content, &token[i], "ss") == 0) {
  133. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  134. i++;
  135. }
  136. }
  137. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  138. printf("Alarm date is: %02d.%02d.%02d\n\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  139. X12RtcSetAlarm(0,&time,0b11111111);
  140. NutDelay(1000);
  141. }
  142. }
  143. bool NetworkIsReceiving(void){
  144. return isReceiving;
  145. }
  146. bool hasNetworkConnection(void){
  147. return hasNetwork;
  148. }