network.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. time.tm_year= getIntegerToken(content, &token[i + 1]) - 1900;
  100. i++;
  101. }else if (jsoneq(content, &token[i], "MM") == 0) {
  102. time.tm_mon= getIntegerToken(content, &token[i + 1]) - 1;
  103. i++;
  104. }else if (jsoneq(content, &token[i], "DD") == 0) {
  105. time.tm_mday = getIntegerToken(content, &token[i + 1]);
  106. i++;
  107. }else if (jsoneq(content, &token[i], "hh") == 0) {
  108. time.tm_hour = getIntegerToken(content, &token[i + 1]);
  109. i++;
  110. }else if (jsoneq(content, &token[i], "mm") == 0) {
  111. time.tm_min = getIntegerToken(content, &token[i + 1]);
  112. i++;
  113. }else if (jsoneq(content, &token[i], "ss") == 0) {
  114. time.tm_sec = getIntegerToken(content, &token[i + 1]);
  115. i++;
  116. }
  117. }
  118. printf("Alarm time is: %02d:%02d:%02d\n", time.tm_hour, time.tm_min, time.tm_sec);
  119. printf("Alarm date is: %02d.%02d.%02d\n\n", time.tm_mday, (time.tm_mon + 1), (time.tm_year + 1900));
  120. X12RtcSetAlarm(0,&time,0b11111111);
  121. NutDelay(1000);
  122. }