network.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. void NetworkInit() {
  24. /* Register de internet controller. */
  25. if (NutRegisterDevice(&DEV_ETHER, 0, 0)) {
  26. printf("Registering failed.");
  27. }/* Netwerk configureren op dhcp */
  28. else if (NutDhcpIfConfig(DEV_ETHER_NAME, NULL, 0)) {
  29. /* Done. */
  30. }else {
  31. printf("Ik heb een internet connectie. Ip is: %s", inet_ntoa(confnet.cdn_ip_addr));
  32. }
  33. }
  34. char* httpGet(char address[]){
  35. printf("komt in httpget()");
  36. NutDelay(1000);
  37. TCPSOCKET* sock = NutTcpCreateSocket();
  38. char http[150];
  39. sprintf(http, "GET %s HTTP/1.1\r\nHost: saltyradio.jancokock.me \r\n\r\n", address);
  40. printf("%s", http);
  41. NutDelay(100);
  42. char buffer[250];
  43. int len = sizeof(http);
  44. if (NutTcpConnect(sock, inet_addr("62.195.226.247"), 80)) {
  45. printf("Can't connect to server\n");
  46. }else{
  47. FILE *stream;
  48. stream = _fdopen((int) sock, "r b");
  49. if(NutTcpSend(sock, http, len) != len){
  50. printf("Writing headers failed.");
  51. NutDelay(1000);
  52. }else{
  53. printf("Headers writed. Now reading.");
  54. NutDelay(1000);
  55. NutTcpReceive(sock, buffer, sizeof(buffer));
  56. //fread(buffer, 1, sizeof(buffer), stream);
  57. NutDelay(1000);
  58. printf(buffer);
  59. };
  60. fclose(stream);
  61. }
  62. NutTcpCloseSocket(sock);
  63. int i;
  64. int enters = 0;
  65. int t = 0;
  66. char* content = (char*) calloc(1 , sizeof(buffer));
  67. for(i = 0; i < strlen(buffer); i++)
  68. {
  69. if(enters > 3) {
  70. enters = 10;
  71. content[t] = buffer[i];
  72. t++;
  73. }
  74. if(buffer[i] == '\n' || buffer[i] == '\r')
  75. {
  76. enters++;
  77. }
  78. else if(enters < 10)
  79. {
  80. enters = 0;
  81. }
  82. }
  83. content[t] = '\0';
  84. printf("\nContent size %d \n", t);
  85. printf("\nContent: %s \n", content);
  86. return content;
  87. }
  88. void parseAlarmJson(char* content){
  89. int r;
  90. int i;
  91. jsmn_parser p;
  92. jsmntok_t token[50]; /* We expect no more than 128 tokens */
  93. jsmn_init(&p);
  94. r = jsmn_parse(&p, content, strlen(content), token, sizeof(token)/sizeof(token[0]));
  95. if (r < 0) {
  96. printf("Failed to parse JSON: %d \n", r);
  97. }else{
  98. printf("Aantal tokens found: %d \n", r);
  99. }
  100. //struct _tm time;
  101. //X12RtcGetClock(time);
  102. //NutDelay(100);
  103. for (i = 1; i < r; i++) {
  104. if (jsoneq(content, &token[i], "hh") == 0) {
  105. /* We may use strndup() to fetch string value */
  106. printf("Tijd seconde: %.*s\n", token[i+1].end-token[i+1].start, content + token[i+1].start);
  107. i++;
  108. }else if (jsoneq(content, &token[i], "mm") == 0) {
  109. /* We may use strndup() to fetch string value */
  110. printf("Tijd minuten: %.*s\n", token[i+1].end-token[i+1].start, content + token[i+1].start);
  111. i++;
  112. }
  113. }
  114. }