network.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. void parseAlarmJson(char* content){
  88. int r;
  89. int i;
  90. jsmn_parser p;
  91. jsmntok_t token[50]; /* We expect no more than 128 tokens */
  92. jsmn_init(&p);
  93. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  94. if (r < 0) {
  95. printf("Failed to parse JSON: %d \n", r);
  96. }else{
  97. printf("Aantal tokens found: %d \n", r);
  98. }
  99. struct _tm time = GetRTCTime();
  100. for (i = 1; i < r; i++) {
  101. if (jsoneq(content, &token[i], "YYYY") == 0) {
  102. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  103. i++;
  104. }else if (jsoneq(content, &token[i], "MM") == 0) {
  105. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  106. i++;
  107. }else if (jsoneq(content, &token[i], "DD") == 0) {
  108. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  109. i++;
  110. }else if (jsoneq(content, &token[i], "hh") == 0) {
  111. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  112. i++;
  113. }else if (jsoneq(content, &token[i], "mm") == 0) {
  114. time.tm_min = getIntegerToken(content, &token[i + 1]);
  115. i++;
  116. }else if (jsoneq(content, &token[i], "ss") == 0) {
  117. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  118. i++;
  119. }
  120. }
  121. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  122. printf("Alarm date is: %02d.%02d.%02d\n\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  123. X12RtcSetAlarm(0,&time,0b11111111);
  124. NutDelay(1000);
  125. }
  126. bool NetworkIsReceiving(void){
  127. return isReceiving;
  128. }