network.c 4.2 KB

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