network.c 4.6 KB

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