network.c 4.4 KB

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