network.c 4.8 KB

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